Request to have some-one double-check my lua thought-process

LeeS

Imperator
Joined
Jul 23, 2013
Messages
7,241
Location
Illinois, USA
I've been working on an lua for a steam friend. The lua makes golden ages occur 50% faster than normal and gives a free social policy when a golden age is started or extended. Everything appears to be working correctly but I can't help having a nagging doubt that there isn't some large bug inherent in what I am doing waiting to manifest itself. So I would appreciate a double-check on my "process" here as to whether there really is an inherent flaw in my design.

I've made two dummy buildings:
  1. BUILDING_DUMMY_LRS_GA_MARKER. I had intended to use this as a marker for whether a golden age had already been processed by the lua. But after a few redrafts I am now not sure I really need it. So the first piece of feedback would be, do you think I can safely remove this building from the mod?
  2. BUILDING_DUMMY_LRS_POLICY. This has the <FreePolicies>1</FreePolicies> but otherwise doesn't do anything. When a new golden age is detected during turn processing I give this building in the capital and then take it away.

This is the lua in its current state.
  1. I am using variable gGoldenAgeTurnsRemaining to store the number of golden age turns left on the previous turn as well as when the game re-loads from a save I am grabbing the number of golden age turns remaining and storing that in this variable.
  2. During turn processing I am grabbing the number of golden age turns remaining and placing that in variable iTurns. If iTurns is less than the previous number of golden age turns, nothing new happens except that I set the variable gGoldenAgeTurnsRemaining equal to the current value of iTurns. If iTurns is greater than gGoldenAgeTurnsRemaining, the lua fires off the code to add the BUILDING_DUMMY_LRS_POLICY to the capital, giving a free social policy, and then the BUILDING_DUMMY_LRS_POLICY is removed again.
  3. This seems to deal correctly with, say, Taj Majal being constructed in the middle of a golden age. But I have a nagging feeling I'm missing something fundamental in the overall method I am using. I am really just looking for a double-check of my "thought-process" here in how I have constructed the lua.
  4. I did not try to key off the notifications system because I'm not sure if AI players get notifications when they enter or extend an existing golden age.

Here is the lua:
Spoiler :
Code:
-- GoldenAgesFaster
-- Author: LeeS
-- DateCreated: 9/6/2014 8:29:25 AM
-------------------------------------------------------------------------------------------------
--gRequiredCivilization determines which civilization gets the golden age benefits
--gGoldenAgeModifier multiplies against the civilization excess happiness and adds the result to the Golden Age Progress Meter
-------------------------------------------------------------------------------------------------
gRequiredCivilization = "CIVILIZATION_ROME"
gGoldenAgeModifier = .5
gGoldenPolicyBuilding = GameInfoTypes.BUILDING_DUMMY_LRS_POLICY
gGoldenAgeMarkerDummy = GameInfoTypes.BUILDING_DUMMY_LRS_GA_MARKER
gGoldenAgeTurnsRemaining = 0

-------------------------------------------------------------------------------------------------
--On Game Loading get the GoldenAgeTurns remaining
-------------------------------------------------------------------------------------------------

function GoldenAgeTurnsLeftOnLoading()
	for i = 0, GameDefines.MAX_MAJOR_CIVS - 1 do
		local pPlayer = Players[i]
		if pPlayer:GetCivilizationType() == GameInfoTypes[gRequiredCivilization] then
			gGoldenAgeTurnsRemaining = pPlayer:GetGoldenAgeTurns()
			if gGoldenAgeTurnsRemaining == nil then
				gGoldenAgeTurnsRemaining = 0
			end
		end
	end
end
Events.LoadScreenClose.Add(GoldenAgeTurnsLeftOnLoading)

-------------------------------------------------------------------------------------------------
--Adjust Golden age progress meter by a percentage of excess happiness based on the setting of variable gGoldenAgeModifier
--Adds a free social policy dummy to the capital when a Golden Age starts or is extended, and than removes the dummy from the capital right afterward
--Adds a marker building to the capital as being processed for the current Golden Age so that the free policy only fires once per golden age, but this
--	marker dummy may not actually be needed
-------------------------------------------------------------------------------------------------

function FasterGoldenAges(iPlayer)
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() == GameInfoTypes[gRequiredCivilization] then
		local pCity = pPlayer:GetCapitalCity();

		-------------------------------------------------------------------------
		--Adjust the Golden Age Progress Meter when the civ is not in a Golden Age
		-------------------------------------------------------------------------

		iGApoints = pPlayer:GetExcessHappiness()
		iGApoints = math.floor(iGApoints * gGoldenAgeModifier)

		if iGApoints > 0 then
			if not pPlayer:IsGoldenAge() then
				pPlayer:ChangeGoldenAgeProgressMeter(iGApoints)
			end
		end

		-------------------------------------------------------------------------
		--Check for Golden Age and only run rest of code when golden age is true
		-------------------------------------------------------------------------

		if pPlayer:IsGoldenAge() then
			iNewGoldenAge = 0
			iTurns = pPlayer:GetGoldenAgeTurns()
			if iTurns > gGoldenAgeTurnsRemaining then
				iNewGoldenAge = 1
			end
			gGoldenAgeTurnsRemaining = iTurns
			if iNewGoldenAge == 1 then	--a golden age has been started or extended when this is true
				--add and then remove the free social policy building
				pCity:SetNumRealBuilding(gGoldenAgeMarkerDummy, 1)
				pCity:SetNumRealBuilding(gGoldenPolicyBuilding, 1)
				pCity:SetNumRealBuilding(gGoldenPolicyBuilding, 0)
			end
			--not in a golden age, so cancel presence of both dummy buildings in the capital
			else pCity:SetNumRealBuilding(gGoldenAgeMarkerDummy, 0);
				pCity:SetNumRealBuilding(gGoldenPolicyBuilding, 0);
		end
	end
end
GameEvents.PlayerDoTurn.Add(FasterGoldenAges)

Also my Buildings code:
Spoiler :
Code:
<GameData>
	<Buildings>
		<!-- Golden Age Marker Dummy -->
		<Row>
			<Type>BUILDING_DUMMY_LRS_GA_MARKER</Type>
			<BuildingClass>BUILDINGCLASS_DUMMY_LRS_GA_MARKER</BuildingClass>
			<Cost>-1</Cost>
			<FaithCost>-1</FaithCost>
			<PrereqTech>NULL</PrereqTech>
			<GreatWorkCount>-1</GreatWorkCount>
			<ArtDefineTag>NONE</ArtDefineTag>
			<MinAreaSize>-1</MinAreaSize>
			<NeverCapture>true</NeverCapture>
			<HurryCostModifier>-1</HurryCostModifier>
			<PortraitIndex>0</PortraitIndex>
			<IconAtlas>CIV_COLOR_ATLAS</IconAtlas>
			<Description>TXT_KEY_DUMMY_LRS_GA_MARKER</Description>
		</Row>
		<!-- Social Policy Dummy -->
		<Row>
			<Type>BUILDING_DUMMY_LRS_POLICY</Type>
			<BuildingClass>BUILDINGCLASS_DUMMY_LRS_POLICY</BuildingClass>
			<Cost>-1</Cost>
			<FaithCost>-1</FaithCost>
			<PrereqTech>NULL</PrereqTech>
			<GreatWorkCount>-1</GreatWorkCount>
			<ArtDefineTag>NONE</ArtDefineTag>
			<MinAreaSize>-1</MinAreaSize>
			<NeverCapture>true</NeverCapture>
			<HurryCostModifier>-1</HurryCostModifier>
			<FreePolicies>1</FreePolicies>
			<PortraitIndex>0</PortraitIndex>
			<IconAtlas>CIV_COLOR_ATLAS</IconAtlas>
			<Description>TXT_KEY_DUMMY_LRS_POLICY</Description>
		</Row>
	</Buildings>

	<BuildingClasses>
		<Row>
			<Type>BUILDINGCLASS_DUMMY_LRS_GA_MARKER</Type>
			<DefaultBuilding>BUILDING_DUMMY_LRS_GA_MARKER</DefaultBuilding>
			<Description>TXT_KEY_DUMMY_LRS_GA_MARKER</Description>
			<NoLimit>true</NoLimit>
		</Row>
		<Row>
			<Type>BUILDINGCLASS_DUMMY_LRS_POLICY</Type>
			<DefaultBuilding>BUILDING_DUMMY_LRS_POLICY</DefaultBuilding>
			<Description>TXT_KEY_DUMMY_LRS_POLICY</Description>
			<NoLimit>true</NoLimit>
		</Row>
	</BuildingClasses>

	<Language_en_US>
		<Row Tag="TXT_KEY_DUMMY_LRS_POLICY">
			<Text>Policy Dummy</Text>
		</Row>
		<Row Tag="TXT_KEY_DUMMY_LRS_GA_MARKER">
			<Text>GA Marker</Text>
		</Row>
	</Language_en_US>
</GameData>

If it's easier I can zip and attach the built version of the mod.

And finally to re-iterate: so far as I can see everything is working properly but I just have this feeling I'm missing something basic. Plus I'd like a confirmation one way or another as to whether you think I can/cannot get by without the BUILDING_DUMMY_LRS_GA_MARKER building.
 
Why not just use pPlayer:SetNumFreePolicies(1) instead of adding and removing the dummy building that effectively does the same?

As for point 4) you are correct in that the AI would not receive any notifications
 
Top Bottom