IsHasBuilding: Difference between revisions
From Civilization Modding Wiki
Jump to navigationJump to search
Killmeplease (talk | contribs) No edit summary |
Killmeplease (talk | contribs) No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
==Example== | ==Example== | ||
(from [http://forums.civfanatics.com/showthread.php?t=450146 Culture per Population mod]) | (from [http://forums.civfanatics.com/showthread.php?t=450146 Culture per Population mod]) | ||
-- culturePerPopulation | |||
-- Author: Moriboe | |||
-- DateCreated: 12/28/2011 | |||
-------------------------------------------------------------- | |||
-- add culture when wonder completed | -- add culture when wonder completed | ||
function CulturePerPop(iPlayer) | |||
local buildingID, buildingTime; | |||
local player = Players[iPlayer]; | |||
if IsValidPlayer(player) then | |||
for pCity in player:Cities() do | |||
for row in GameInfo.Building_CultureChangePerPop() do | |||
-- has wonder? | -- has wonder? | ||
buildingID = GameInfo.Buildings[row.BuildingType].ID; | buildingID = GameInfo.Buildings[row.BuildingType].ID; | ||
'' | if pCity:'''''IsHasBuilding'''''(buildingID) then | ||
-- just built? | -- just built? | ||
buildingTime = pCity: | buildingTime = pCity:GetBuildingOriginalTime(buildingID); | ||
if buildingTime == [[Lua Game Objects/Game|Game]]:GetGameTurnYear() then | |||
pCity: | pCity:ChangeJONSCulturePerTurnFromBuildings( | ||
math.floor(pCity: | math.floor(pCity:GetPopulation() / row.Pop * row.Culture)); | ||
''' | end | ||
end | |||
end | |||
end | |||
end | |||
end | |||
-- add/remove culture upon population change | |||
function ChangePopCulture(iX, iY, oldPopulation, newPopulation) | |||
local pCity = Map.GetPlot(iX, iY):GetPlotCity(); | |||
local oldCulture, newCulture, change = 0; | |||
for row in GameInfo.Building_CultureChangePerPop() do | |||
if pCity:'''''IsHasBuilding'''''(GameInfo.Buildings[row.BuildingType].ID) then | |||
oldCulture = math.floor(oldPopulation * row.Culture / row.Pop); | |||
newCulture = math.floor(newPopulation * row.Culture / row.Pop); | |||
pCity:ChangeJONSCulturePerTurnFromBuildings(newCulture - oldCulture); | |||
end | |||
end | |||
end | |||
-- add culture upon capture | |||
function SetPopCulture(playerID, bCapital, iX, iY, newPlayerID) | |||
local pCity = Map.GetPlot(iX, iY):GetPlotCity(); | |||
for row in GameInfo.Building_CultureChangePerPop() do | |||
if pCity:'''''IsHasBuilding'''''(GameInfo.Buildings[row.BuildingType].ID) then | |||
pCity:ChangeJONSCulturePerTurnFromBuildings( | |||
math.floor(pCity:GetPopulation() / row.Pop * row.Culture)); | |||
end | |||
end | |||
end | |||
function IsValidPlayer(player) | |||
return player ~= nil and player:IsAlive() and not player:IsBarbarian() | |||
end | |||
GameEvents.PlayerDoTurn.Add( CulturePerPop ); | |||
GameEvents.SetPopulation.Add( ChangePopCulture ); | |||
GameEvents.CityCaptureComplete.Add( SetPopCulture ); |
Latest revision as of 10:07, 14 February 2012
Description
pCity:IsHasBuilding(buildingID)
Returns true if a subject city has a building with a specified ID
Example
(from Culture per Population mod)
-- culturePerPopulation -- Author: Moriboe -- DateCreated: 12/28/2011 -------------------------------------------------------------- -- add culture when wonder completed function CulturePerPop(iPlayer) local buildingID, buildingTime; local player = Players[iPlayer]; if IsValidPlayer(player) then for pCity in player:Cities() do for row in GameInfo.Building_CultureChangePerPop() do -- has wonder? buildingID = GameInfo.Buildings[row.BuildingType].ID; if pCity:IsHasBuilding(buildingID) then -- just built? buildingTime = pCity:GetBuildingOriginalTime(buildingID); if buildingTime == Game:GetGameTurnYear() then pCity:ChangeJONSCulturePerTurnFromBuildings( math.floor(pCity:GetPopulation() / row.Pop * row.Culture)); end end end end end end -- add/remove culture upon population change function ChangePopCulture(iX, iY, oldPopulation, newPopulation) local pCity = Map.GetPlot(iX, iY):GetPlotCity(); local oldCulture, newCulture, change = 0; for row in GameInfo.Building_CultureChangePerPop() do if pCity:IsHasBuilding(GameInfo.Buildings[row.BuildingType].ID) then oldCulture = math.floor(oldPopulation * row.Culture / row.Pop); newCulture = math.floor(newPopulation * row.Culture / row.Pop); pCity:ChangeJONSCulturePerTurnFromBuildings(newCulture - oldCulture); end end end -- add culture upon capture function SetPopCulture(playerID, bCapital, iX, iY, newPlayerID) local pCity = Map.GetPlot(iX, iY):GetPlotCity(); for row in GameInfo.Building_CultureChangePerPop() do if pCity:IsHasBuilding(GameInfo.Buildings[row.BuildingType].ID) then pCity:ChangeJONSCulturePerTurnFromBuildings( math.floor(pCity:GetPopulation() / row.Pop * row.Culture)); end end end function IsValidPlayer(player) return player ~= nil and player:IsAlive() and not player:IsBarbarian() end GameEvents.PlayerDoTurn.Add( CulturePerPop ); GameEvents.SetPopulation.Add( ChangePopCulture ); GameEvents.CityCaptureComplete.Add( SetPopCulture );