IsHasBuilding: Difference between revisions

From Civilization Modding Wiki
Jump to navigationJump to search
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Description==
==Description==
'''pCity:IsHasBuilding(buildingID)'''
Returns true if a subject [[Lua Game Objects/City|city]] has a building with a specified ID
Returns true if a subject [[Lua Game Objects/City|city]] has a building with a specified ID
==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)
   function CulturePerPop(iPlayer)
     '''local''' buildingID, buildingTime;
     local buildingID, buildingTime;
     '''local''' player = [[Players]][iPlayer];
     local player = Players[iPlayer];
     '''if''' IsValidPlayer(player) '''then'''
     if IsValidPlayer(player) then
       '''for''' pCity '''in''' player:[[Cities]]() '''do'''
       for pCity in player:Cities() do
         '''for''' row '''in''' [[GameInfo]].Building_CultureChangePerPop() '''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'''
           if pCity:'''''IsHasBuilding'''''(buildingID) then
             -- just built?
             -- just built?
             buildingTime = pCity:[[GetBuildingOriginalTime]](buildingID);
             buildingTime = pCity:GetBuildingOriginalTime(buildingID);
             '''if''' buildingTime == [[Game]]:[[GetGameTurnYear]]() '''then'''
             if buildingTime == [[Lua Game Objects/Game|Game]]:GetGameTurnYear() then
               pCity:[[ChangeJONSCulturePerTurnFromBuildings]](
               pCity:ChangeJONSCulturePerTurnFromBuildings(
                 math.floor(pCity:[[GetPopulation]]() / row.Pop * row.Culture));
                 math.floor(pCity:GetPopulation() / row.Pop * row.Culture));
             '''end'''
             end
          '''end'''
          end
        '''end'''
        end
       '''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 );