Getting the max value.

JFD

Kathigitarkh
Joined
Oct 19, 2010
Messages
9,132
Location
The Kingdom of New Zealand
I'm trying to compare a player's tile count to the tile count of every other (major) player, to see if they have the most tiles. I've tried to piece apart what Firaxis has done with the Demographics code, but haven't managed to replicate the method successfully. I've spent many hours trying, so thought before I'd return to it, I'd ask for some advice. I will need to replicate this method to check other such values, so some general information on how to go about this would be appreciated. Thanks.
 
Hm, okay. I'm counting the player's plot count to everyone elses' successfully, but don't know how to assess if the player has the most of them all. I thought I could have a counter and then compare that to the number of players; so for every other player that the player beats, it adds +1 to a local variable. But this doesn't seem to add +1 for every time that it returns true that the player is beating another player, instead just setting the variable to 1. So what am I doing wrong that's painfully obvious to someone else?

Here's my function, for reference:
Spoiler :

Code:
function GetSteppeCulturalObjectives(player, otherPlayer)
  local culturalObjectives = 0
  local hasLargestLandEmpire = false
  local numCivsBested = 0
  if player:GetNumPlots() > otherPlayer:GetNumPlots() then
	
  print(otherPlayer:GetName() .. " has this num plots: " .. otherPlayer:GetNumPlots())
	numCivsBested = numCivsBested + 1
        if numCivsBested >= 2 then --arbitrary number for now
		hasLargestLandEmpire = true
	end
  end

	if hasLargestLandEmpire then
	  print("I beat them all")
         culturalObjectives = culturalObjectives + 5
	end
	
	print(numCivsBested)
	
	return culturalObjectives
end


Thanks for any help.
 
You might want to try something like this:
Code:
function PlotCountRank( refPlayerID )
	local rank = 1;
	local refNumPlots = Players[ refPlayerID ]:GetNumPlots();
	for playerID = 0, GameDefines.MAX_CIV_PLAYERS-1 do
		local player = Players[ playerID ];
		if player
			and playerID ~= refPlayerID
			and player:IsAlive()
			and refNumPlots < player:GetNumPlots()
		then
			rank = rank + 1;
		end
	end
	return rank
end
Cheers
 
If it's just a simple question -- does the player have the most plots? -- then code it that way. You only need to check other players until you find out that it is not true.

Code:
function HasMostPlots( playerID )
	local numPlotsToBeat = Players[playerID]:GetNumPlots()
	for loopPlayerID = 0, GameDefines.MAX_CIV_PLAYERS-1 do
		if loopPlayerID ~= playerID then
			local loopPlayer = Players[ loopPlayerID ]
			if loopPlayer and loopPlayer:IsAlive() then
				if numPlotsToBeat <= loopPlayer:GetNumPlots() then
					return false
				end
			end
		end
	end
	return true
end

Tie goes against the player the way I coded it. Change <= to < if you want player to win ties.
 
Back
Top Bottom