Free building lua queries

Rob (R8XFT)

Ancient Briton
Retired Moderator
Joined
Aug 11, 2002
Messages
10,866
Location
Leeds (UK)
I'm using the following code by Leugi to give Israel a free shrine in their first four cities:
Code:
-- By Leugi


function OnCityFoundCheck (player, numBuilds, iBuilding, iTech, pTeam)
	local player = Players[iPlayer];
		if player:GetCivilizationType() ~= GameInfoTypes.CIVILIZATION_ISRAEL then return end
	local numBuilds = GetNumFreeBuildingonTech(player);
	local b = GetFreeBuilding(player);
	if b ~= nil then
		local iBuilding = GameInfoTypes["" .. b .. ""]
	end
	local t = GetFreeBuildingTechPrereq(player);
	if t ~= nil then
		local iTech = GameInfo.Technologies["" .. t .. ""].ID
	end
	local i = player:CountNumBuildings(GameInfoTypes["" .. b .. ""])
	local teamID = player:GetTeam();
	local pTeam = Teams[teamID];
	if (pTeam:IsHasTech(iTech)) then
	for pCity in player:Cities() do
		if i < numBuilds then
			pCity:SetNumRealBuilding(GameInfoTypes["" .. b .. ""], 1)
			local i = player:CountNumBuildings(GameInfoTypes["" .. b .. ""])
		end
	end
	end
	if i == numBuilds then
		GameEvents.PlayerCityFounded.Remove(OnCityFoundCheck);
	end
end



function TraitFreeBT(iPlayer)
	local player = Players[iPlayer];
		if player:GetCivilizationType() ~= GameInfoTypes.CIVILIZATION_ISRAEL then return end
	local numBuilds = GetNumFreeBuildingonTech(player);
	local b = GetFreeBuilding(player);
	local t = GetFreeBuildingTechPrereq(player);
	local iTech = GameInfoTypes["" .. t .. ""]
	local teamID = player:GetTeam();
	local pTeam = Teams[teamID];
	if b ~= nil and t ~=nil then
		if numBuilds == -1 then
			if (pTeam:IsHasTech(iTech)) then
				for pCity in player:Cities() do
					print ("Condition 1");
					pCity:SetNumRealBuilding(GameInfoTypes["" .. b .. ""], 1)
				end
			end
		elseif (player:GetNumCities() == numBuilds) then
			if (pTeam:IsHasTech(iTech)) then
				for pCity in player:Cities() do
					print ("Condition 2");
					pCity:SetNumRealBuilding(GameInfoTypes["" .. b .. ""], 1)
				end
			end
		elseif (player:GetNumCities() < numBuilds) then
			if (pTeam:IsHasTech(iTech)) then
				for pCity in player:Cities() do
					print ("Condition 3");
					print (t);
					print (iTech);
					pCity:SetNumRealBuilding(GameInfoTypes["" .. b .. ""], 1)
					GameEvents.PlayerCityFounded.Add(OnCityFoundCheck);
				end
			end
		elseif (player:GetNumCities() > numBuilds) then
			if (pTeam:IsHasTech(iTech)) then
				for pCity in player:Cities() do
					print ("Condition 4");
					local i = player:CountNumBuildings(GameInfoTypes["" .. b .. ""])
					if i < numBuilds then
						pCity:SetNumRealBuilding(GameInfoTypes["" .. b .. ""], 1)
						local i = player:CountNumBuildings(GameInfoTypes["" .. b .. ""])
					end
				end
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(TraitFreeBT);


function GetFreeBuilding(player)
	if player == nil or not player:IsAlive() or 
			player:IsBarbarian() or player:IsMinorCiv() then
		return nil;
	end
	local trait = GetMajorTrait(player);
	return trait.FreeBuildingonTech;
end

function GetFreeBuildingTechPrereq(player)
	if player == nil or not player:IsAlive() or 
			player:IsBarbarian() or player:IsMinorCiv() then
		return nil;
	end
	local trait = GetMajorTrait(player);
	return trait.FreeBuildingTechPrereq;
end

function GetNumFreeBuildingonTech(player)
	if player == nil or not player:IsAlive() or 
			player:IsBarbarian() or player:IsMinorCiv() then
		return -1;
	end
	local trait = GetMajorTrait(player);
	return trait.NumFreeBuildingonTech;
end

function GetMajorTrait(pPlayer)
	local leader = GameInfo.Leaders[pPlayer:GetLeaderType()];
	local leaderTrait = GameInfo.Leader_Traits("LeaderType ='" .. leader.Type .. "'")();
	return GameInfo.Traits[leaderTrait.TraitType];
end

To activate this in XML, I'm doing this with Israel's UA:
Code:
		<Row>
			<Type>TRAIT_CHOSEN_PEOPLE</Type>
			<Description>TXT_KEY_TRAIT_CHOSEN_PEOPLE</Description>
			<ShortDescription>TXT_KEY_TRAIT_CHOSEN_PEOPLE_SHORT</ShortDescription>
			<FreeBuildingonTech>BUILDING_SHRINE</FreeBuildingonTech>
			<FreeBuildingTechPrereq>TECH_WATER_DEITIES</FreeBuildingTechPrereq>
			<NumFreeBuildingonTech>4</NumFreeBuildingonTech>
		</Row>

I have two questions:

01. If I remove this line (which occurs in the lua code twice):
Code:
if player:GetCivilizationType() ~= GameInfoTypes.CIVILIZATION_ISRAEL then return end
will it mean that it will work for any civilization that I use the freebuildingontech etc XML code with - i.e. one instance of it in the lua will also cover free barracks in the first six cities on discovery of Logistics for the Spartans and free libraries in the first three cities on the discovery of Writing for another civ?

02. How do I adjust the code/add to the code to make the free buildings have no upkeep costs but the others (i.e. the shrines in city numbers five, six, seven etc) retain their upkeep fee? Though to be honest, if that building was free of maintenance fees for all instances, whether automatically built or not for that civ, I'd be happy enough ;).
 
With respect to question 1:
Yes, if you remove that line the code will run for all civilizations, not just Israel.

With respect to question 2:
What will probably work is to have the code give a dummy building that has a freeBuildingThisCity = Shrine. Players that capture such a city could get to keep the maint free shrine. It would take a bit of lua to swap out the building for a normal maint shrine but that may be more effort than it is worth.
 
Back
Top Bottom