[Help] Science yield from Great Works?

Harkodos

Warlord
Joined
Feb 9, 2016
Messages
197
One of the Civilization Traits I was trying to make required the civilization to receive Science from Great Works that they owned, but after some trial and error today it seems that you can only have the Culture and Gold yields attached to the Policy_GreatWorkYieldChanges tag (which I think is dumb; this didn't work for Faith either way back when, which was frustrating).

So I was wondering if someone could help me rig up a work-around where it counts how many Great Works the player owns and then, using Lua, adds a unique dummybuilding to the cities containing them that provides 2 Science apiece.
 
I haven't yet tested it, but I think this should work; it's a modification of my first civ's UA Lua (+10% science from each Great Work of Writing; this code itself was heavily sourced from ViceVirtuoso), with alterations to allow for it to work with any Great Work type and to allow it to use a dummy defined in the Lua itself (I originally made it more extensible than it needed to be, by adding a "Klisz_GreatWritingDummyBuilding" column to the Traits table).

Code:
local iCivilization = GameInfoTypes.CIVILIZATION_ETC
local iDummyBuilding = GameInfoTypes.BUILDING_ETC

local iMaxCivs = GameDefines.MAX_MAJOR_CIVS
local iMaxPlayers = GameDefines.MAX_CIV_PLAYERS
local bPopup;
local tGreatWorkBuildings = {};
for pBuilding in GameInfo.Buildings() do
	if pBuilding.GreatWorkCount > 0 then
		local pBuildingClass = GameInfo.BuildingClasses("Type = '" ..pBuilding.BuildingClass.. "'")()
		tGreatWorkBuildings[pBuildingClass.ID] = pBuildingClass.Type
	end
end

function UpdateDummyBuildings(iPlayer)
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() ~= iCivilization then return end
	if pPlayer:IsEverAlive() and not pPlayer:IsMinorCiv() then
		for pCity in pPlayer:Cities() do
			local iBuildingsToAdd = 0
			for iBuildingClass, pBuildingClass in pairs(tGreatWorkBuildings) do
				local iCurrentWorks = pCity:GetNumGreatWorksInBuilding(iBuildingClass)
				print("In the " .. pBuildingClass .. " in " .. pCity:GetName() .. " there are " .. iCurrentWorks .. " Great Works")
				if iCurrentWorks > 0 then
					for i = 0, iCurrentWorks - 1, 1 do
						local iWorkType = pCity:GetBuildingGreatWork(iBuildingClass, i)
						print("Found a Great Work!")
						iBuildingsToAdd = iBuildingsToAdd + 1;
					end
				end
			end
			print("Adding " .. iBuildingsToAdd .. " buildings to " .. pCity:GetName())
			pCity:SetNumRealBuilding(iDummyBuilding, iBuildingsToAdd);
		end
	end
end

function UpdateDummyBuildingsUI()
	if bPopup then
		UpdateDummyBuildings(Game:GetActivePlayer())
	end
end

GameEvents.PlayerDoTurn.Add(UpdateDummyBuildings)
Events.SerialEventCityInfoDirty.Add(UpdateDummyBuildingsUI)

function SetCurrentPopup(popupInfo)
	local popupType = popupInfo.Type
	if popupType == ButtonPopupTypes.BUTTONPOPUP_CULTURE_OVERVIEW then
		bPopup = true
	end
end
Events.SerialEventGameMessagePopup.Add(SetCurrentPopup)

function ClearCurrentPopup()
	bPopup = nil;
end
Events.SerialEventGameMessagePopupProcessed.Add(ClearCurrentPopup)
 
I tried the code, and it worked wonderfully (though it does take a turn to kick in, but that's a minor gripe). I thank you for you generous aid.
 
Back
Top Bottom