Just checking if code logic correct

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
So the idea is to count the plots in a city area with a particular unique improvement... and then place multiple dummy buildings to give +experience to units built in the city with the unique improvements. As an example, 6 IMPROVEMENT_TRELLEBORG in a city radius would lead to 6 dummy buildings each giving +2% experience.

I would just like to check the code logic as my use of item counts is sometimes suspect :). Are the points where trelleborgplots = 0 and trelleborgplots = trelleborgplots+1 are placed the correct positions so the plot iteration is being done correctly for each city?

My other question is whether or not experience stacks with these multiple dummies... I think yes, but just want to check.

Code:
-- coding for Danish Trelleborg Improvement.

function TrellborgBuilding(iPlayer)
	
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_DENMARK then
	
		for city in pPlayer:Cities() do
			
			local trelleborgplots = 0
			for i = 0, city:GetNumCityPlots() - 1, 1 do
				
				local plot = city:GetCityIndexPlot(i)
				if plot and (plot:GetOwner() == iPlayer) then

					if plot:GetImprovementType() == GameInfoTypes.IMPROVEMENT_TRELLEBORG then
						
						if not plot:IsImprovementPillaged() then
						trelleborgplots = trelleborgplots+1
							
						end
					end					
				end
				city:SetNumRealBuilding(GameInfoTypes["BUILDING_TRELLEBORG"], trelleborgplots)
			end
		end
	end
end

GameEvents.PlayerDoTurn.Add(TrellborgBuilding)

Thank-you for your indulgence.
 
(Taking a quick break from C++, so not fully checked the code)

Code:
city:SetNumRealBuilding(GameInfoTypes["BUILDING_TRELLEBORG"], trelleborgplots)
is one "end" too high.

At the moment, the inner loop counts how many Trelleborgs are needed, but sets the new total every time around the loop - it just needs to set it once after the loop has completed

Also GameInfoTypes.CIVILIZATION_DENMARK and GameInfoTypes["BUILDING_TRELLEBORG"] require a table lookup every time they are used, so should be moved to local variables at the top of the file

Code:
local iDenmark = GameInfoTypes.CIVILIZATION_DENMARK
local iTrelleborg = GameInfoTypes.BUILDING_TRELLEBORG
and then use iDenmark and iTrelleborg instead
 
<Building_DomainFreeExperiences> and <Building_UnitCombatFreeExperiences> stack. Not sure about column <Experience> in table <Buildings> since I have that shown as unverified one way or the other in the thread on stacking issues related to dummy buildings.
 
Back
Top Bottom