Could someone check my math?

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
IGNORE... SOLVED>>>

Used this instead for building count>>>


Code:
local Buildingcount = player:CountNumBuildings(iTrelleborg) + player:CountNumBuildings(iVik) + player:CountNumBuildings(iRunestone) + player:CountNumBuildings(iByrck)

I am using this bit of code to count certain buildings and allow a ratio of units to buildings to be built... using city can build. The code works very well, however, for some reason I can build more units initially than I ought to.

In testing, I added one building, a Vik. Given no units being built of the required type, that would give numUnits = 0. For building count = 1, math.ceil(BuildingCount/2) = 1.

math.ceil(BuildingCount/2)>numUnits so I can build a unit.

However, now numUnits = 1 and math.ceil(BuildingCount/2) = 1... 1>1 is false, so I should not be able to build another unit.

Here's the kicker... I can.

So, what is the hidden factor here? If I add more buildings in other cities, the math seems to work out... 2 and 3 buildings give 2/2 or 3/2 rounded to 2>2 so I can't build another unit at that ratio, as expected. It is just the 1/2 ratio that seems to be out of wack.

Code:
local city = player:GetCityByID(iCity)			
			if city:IsHasBuilding(iTrelleborg) or city:IsHasBuilding(iVik) or city:IsHasBuilding(iRunestone) or city:IsHasBuilding(iByrck) then
				
				local numUnits = 0	
				for unit in player:Units() do
					if unit:GetUnitType() == iLongship or unit:GetUnitType() == iFelagi then				
						numUnits = numUnits + 1
					end
				end
				
				local BuildingCount = 0
				for pCity in player:Cities() do			

					if pCity:IsHasBuilding(iTrelleborg) or city:IsHasBuilding(iVik) or city:IsHasBuilding(iRunestone) or city:IsHasBuilding(iByrck) then

					BuildingCount = BuildingCount + 1

					end
				end
				
				return math.ceil(BuildingCount/2) > numUnits
			else				
				return false	--needed when the city does not have a building that allows the unit		
			end

Why is the math a little off?

Thank-you.
 
Back
Top Bottom