Advertisement
Janne252

How to: Classic population cap system - example

Feb 2nd, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.60 KB | None | 0 0
  1. function OnInitID()
  2.  
  3.     -- [[ Markers ]]
  4.  
  5.     -- [[ Squad Groups ]]
  6.  
  7.     -- [[ Entity Groups ]]
  8.  
  9.     territoryPointType = {
  10.         strategic = 1,
  11.         fuel = 2,
  12.         munition = 3,
  13.         victory = 4,
  14.     }
  15.    
  16.     _territory_point_blueprints = {
  17.         {blueprint = BP_GetEntityBlueprint("territory_point_mp"), type = territoryPointType.strategic},
  18.         {blueprint = BP_GetEntityBlueprint("territory_fuel_point_mp"), type = territoryPointType.fuel},
  19.         {blueprint = BP_GetEntityBlueprint("territory_munitions_point_mp"), type = territoryPointType.munition},
  20.         {blueprint = BP_GetEntityBlueprint("victory_point"), type = territoryPointType.victory},
  21.     }
  22.    
  23.     TerritoryPoints = {}
  24.  
  25.     _PopCapSystemConfig = {
  26.         default_basepop = {
  27.             [0] = 35,
  28.             [1] = 40,
  29.         },
  30.         grant_per_point = {
  31.             -- strategic
  32.             [1] = {
  33.                 [0] = 6,
  34.                 [1] = 7,
  35.             },
  36.             -- fuel
  37.             [2] = {
  38.                 [0] = 12,
  39.                 [1] = 16,
  40.             },
  41.             -- muni
  42.             [3] = {
  43.                 [0] = 12,
  44.                 [1] = 16,
  45.             },
  46.             -- victory
  47.             [4] = {
  48.                 [0] = 4,
  49.                 [1] = 5,
  50.             },
  51.         },
  52.     }
  53.    
  54.     for i = 1, World_GetPlayerCount() do
  55.         local player = World_GetPlayerAt(i)
  56.         Player_SetPopCapOverride(player, _PopCapSystemConfig.default_basepop[Player_GetRace(player)])
  57.     end
  58.    
  59.     for index = 0, World_GetNumEntities() -1 do
  60.         local entity = World_GetEntity(index)
  61.         if Entity_IsOfType(entity, "strategic_node") then
  62.             local _blueprint = Entity_GetBlueprint(entity)
  63.             local _type
  64.            
  65.             for key, item in ipairs(_territory_point_blueprints) do
  66.                 if _blueprint == item.blueprint then
  67.                     _type = item.type
  68.                 end
  69.             end
  70.             local _territory_point = {
  71.                 worldID = Entity_GetGameID(entity),
  72.                 blueprint = _blueprint,
  73.                 type = _type,
  74.                 owner = "world",
  75.                 granted = false,
  76.             }
  77.             table.insert(TerritoryPoints, _territory_point)
  78.         end        
  79.     end
  80.    
  81.     Rule_Add(Monitor_TerritorySectors)
  82. end
  83.  
  84. function Monitor_TerritorySectors()
  85.     for key, point in ipairs(TerritoryPoints) do
  86.         local entity = Entity_FromWorldID(point.worldID)
  87.         local current_owner = Entity_GetPlayerOwnerSafe(entity)
  88.         local previous_owner = point.owner
  89.        
  90.         -- Is owned by world
  91.         if current_owner == "world" then
  92.             if point.granted then
  93.                 --remove bonus
  94.                 if point.owner ~= "world" then
  95.                     GrantPopulationBonus(point.owner, point.type, "-")
  96.                     point.owner = "world"
  97.                 end
  98.                 point.granted = false
  99.             end
  100.         else
  101.             -- IS owned by player
  102.             if not World_IsInSupply(current_owner, Entity_GetPosition(entity)) then
  103.                 if point.granted then
  104.                     --remove bonus         
  105.                     GrantPopulationBonus(point.owner, point.type, "-")
  106.                     point.owner = current_owner
  107.                     point.granted = false
  108.                 end
  109.             else
  110.                 if not point.granted then
  111.                     point.owner = current_owner
  112.                     GrantPopulationBonus(point.owner, point.type, "+")
  113.                     --give bonus
  114.                     point.granted = true
  115.                 end
  116.             end
  117.         end
  118.     end
  119. end
  120.  
  121. function GrantPopulationBonus(player, _type, direction)
  122.     local current_population = Player_GetMaxPopulation(player, CT_Personnel)
  123.     local grant = _PopCapSystemConfig.grant_per_point[_type][Player_GetRace(player)]
  124.    
  125.     if direction == "-" then
  126.         -- turn it negative
  127.         grant = 0 - grant
  128.     end
  129.    
  130.     for i = 1, World_GetPlayerCount() do
  131.         local _player = World_GetPlayerAt(i)   
  132.         if Player_GetTeam(_player) == Player_GetTeam(player) then
  133.             Player_SetPopCapOverride(_player, current_population + grant)
  134.         end
  135.     end
  136. end
  137.  
  138. function Entity_GetPlayerOwnerSafe(entity)
  139.     if World_OwnsEntity(entity) then
  140.         return "world"
  141.     else
  142.         return Entity_GetPlayerOwner(entity)
  143.     end
  144. end
  145.  
  146. function Player_GetIDSafe(player)
  147.     if not player or player == "world" then
  148.         return "world"
  149.     else
  150.         return Player_GetID(player)
  151.     end
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement