Limiting something to human players only?

zenos14

Chieftain
Joined
Apr 18, 2008
Messages
8
I am quite a bit new ad Lua modding, how would you go about limiting something to human players only?

Like lets take the snippet from the get artifact from excavation function

function ArtifactUtilities.GetAvailableArtifactTypesByCategory(playerType : number, artifactCategoryType : number)
local artifactIDs : table = {};
local player = Players[playerType];
if(player == nil) then
error("player was nil");
end

for artifactInfo : table in GameInfo.Artifacts() do
local categoryID : number = GameInfo.ArtifactCategories[artifactInfo.Category].ID;
if(artifactCategoryType == categoryID) then
if(Game.CanAcquireArtifactType(artifactInfo.ID) == true) then

local numArtifactsWithPreferedReward : number = ArtifactUtilities.GetNumPlayerArtifactsWithPreferedReward(player, artifactInfo.RewardPreference);
local chanceToAcquire : number = 100 / (numArtifactsWithPreferedReward + 1);

local roll : number = Game.Rand(100, "rolling to recieve artifact type that shared prefered reward with existing library type.");
if(roll <= chanceToAcquire) then
table.insert(artifactIDs, artifactInfo.ID);
end
end
end
end

return artifactIDs;
end

If I'm understanding it correctly I could add something like
if (not player:IsHuman()) then
return artifactIDs
end

Between the end and for lines in
if(player == nil) then
error("player was nil");
end

for artifactInfo : table in GameInfo.Artifacts() do
And it should prevent an AI controlled player from getting any artifacts or reducing the number available.

Am I correct in thinking it works like this?

I've been fiddling with the code for a while and though it's obvious when it breaks I'm not really sure if this is working
 
Yes, that should work. Though I have to say I don't like creating additional returns in a function if they're not needed as it can be the cause of some really stupid mistakes when you're working on a bigger project with tons of code. So it's probably just personal preference, but I'd do it like this:
Code:
function ArtifactUtilities.GetAvailableArtifactTypesByCateg ory(playerType : number, artifactCategoryType : number)
	local artifactIDs : table = {};
	local player = Players[playerType];
	if(player == nil) then
		error("player was nil");
	end

[B]	if (player:IsHuman()) then[/B]
		for artifactInfo : table in GameInfo.Artifacts() do
			local categoryID : number = GameInfo.ArtifactCategories[artifactInfo.Category].ID;
			if(artifactCategoryType == categoryID) then
				if(Game.CanAcquireArtifactType(artifactInfo.ID) == true) then

					local numArtifactsWithPreferedReward : number = ArtifactUtilities.GetNumPlayerArtifactsWithPrefere dReward(player, artifactInfo.RewardPreference);
					local chanceToAcquire : number = 100 / (numArtifactsWithPreferedReward + 1);

					local roll : number = Game.Rand(100, "rolling to recieve artifact type that shared prefered reward with existing library type.");
					if(roll <= chanceToAcquire) then
						table.insert(artifactIDs, artifactInfo.ID);
					end
				end
			end
		end
[B]	end[/B]


[B][I]	-- That way, any code that goes here would still be executed. Not that it
	-- makes a difference in this case (as you'd probably not want to add 
	-- anything to this utility-function, but it could prevent some confusion 
	-- in other cases.[/i][/B]

	return artifactIDs;
end
 
The simplest place to change (although it makes little difference) is the acquire artifact function:
Code:
function AcquireArtifact(playerType : number, artifactCategoryType : number)
	local player : table = Players[playerType];
	if(player == nil) then
		error("player was nil");
	end

	local acquisitionRate : number = player:GetArtifactAcquisitionRate(artifactCategoryType);
	if(acquisitionRate == nil) then
		error("acquisitionRate was nil");
	end

	local recieveArtifactRoll : number = Game.Rand(100, "rolling to recieve Old Earth Artifact");
	if(recieveArtifactRoll <= acquisitionRate) then
		local artifactType : number = ArtifactUtilities.ChooseArtifactFromCategory(playerType, artifactCategoryType);

		-- Artifact type of -1 means the player has acquired all artifacts of that type and won't be getting any more.
		if(artifactType ~= -1) then
			player:AddArtifact(artifactType);
		end
	else
		player:IncreaseArtifactAcquisitionRate(artifactCategoryType);
	end
end

Change it to:
Code:
function AcquireArtifact(playerType : number, artifactCategoryType : number)
	local player : table = Players[playerType];
	if(player == nil) then
		error("player was nil");
	end
	
[COLOR="Red"]	if(not player:IsHuman()) then
		return;
	end[/COLOR]

	local acquisitionRate : number = player:GetArtifactAcquisitionRate(artifactCategoryType);
	if(acquisitionRate == nil) then
		error("acquisitionRate was nil");
	end

	local recieveArtifactRoll : number = Game.Rand(100, "rolling to recieve Old Earth Artifact");
	if(recieveArtifactRoll <= acquisitionRate) then
		local artifactType : number = ArtifactUtilities.ChooseArtifactFromCategory(playerType, artifactCategoryType);

		-- Artifact type of -1 means the player has acquired all artifacts of that type and won't be getting any more.
		if(artifactType ~= -1) then
			player:AddArtifact(artifactType);
		end
	else
		player:IncreaseArtifactAcquisitionRate(artifactCategoryType);
	end
end
 
That one is in Artifacts.lua though, which I'd assume is a lot more likely to run into compatibility issues with other mods than a mod that replaces ArtifactUtilities.lua.
 
Thanks, I think I got it, wasn't sure cause it didn't seem to work at first
 
Back
Top Bottom