Advertisement
Janne252

Core.scar

Feb 17th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 36.11 KB | None | 0 0
  1. import("ScarUtil.scar")
  2. import("WinConditions/victorypointplusannihilate.scar")
  3. import("Fatalities/Fatalities.scar")
  4. import("WinConditions/lib.scar")
  5.  
  6. function Villagers_PreInit()
  7.     --loadfile("scar/villagers/core.scar")()
  8.     Rule_AddOneShot(Villagers_Init, 1/8)
  9. end
  10.  
  11. Scar_AddInit(Villagers_PreInit)
  12.  
  13. function Villagers_Init()
  14.     g_guid = "6886f8c95f9c4483b5f790e4198047eb"
  15.     g_enable_messages = true
  16.     g_text = ""
  17.     g_text_line_count = 0
  18.     Msg("Initialized")
  19.     player_generic = World_GetPlayerAt(1)
  20.     AI_EnableAll(false)
  21.     colorUnPacker = {
  22.         __call = function(t)
  23.             return unpack(t)
  24.         end
  25.     }
  26.     Colors = {
  27.         food = {255, 0, 0, 255},
  28.         wood = {97, 160, 63, 255},
  29.         gold = {254, 221, 0, 255},
  30.         special = {255, 0, 252, 255},
  31.     }
  32.    
  33.     g_configuration = {
  34.         max_villagers_per_resource = 10,
  35.         max_villagers_per_farm = 10,
  36.         population_per_house = 8,
  37.         max_houses_per_player = 10,
  38.     }
  39.     RT_FOOD = 1
  40.     RT_WOOD = 2
  41.     RT_GOLD = 3
  42.     FARMTYPE = {
  43.         FOOD = 1,
  44.         GOLD = 2,
  45.     }
  46.     for key, _t in pairs(Colors) do
  47.         setmetatable(_t, colorUnPacker)
  48.     end
  49.    
  50.     SQUAD_SIZE_DEFAULT = 0
  51.     g_max_villagers_per_resource = 2
  52.     g_resource_gather = {}
  53.     g_delete_resource_entity = {}
  54.     g_villager_ability_call = {}
  55.     g_villager_house_simulation = {}
  56.     g_player_house_counter = {}
  57.     g_villager_farm = {}
  58.     g_player_plantation = {}
  59.     g_player_outpost_level = {}
  60.     g_player_wall_level = {}
  61.     g_gates = {}
  62.     g_skip_gate_open = {}
  63.     g_delayed_task = {}
  64.     g_upgrade_wall_once_finished = {}
  65.     g_ebp_bush_food = Villagers_GetEntityBlueprint("bush_food_01")
  66.     g_ebp_pine_wood = Villagers_GetEntityBlueprint("pine_wood_01")
  67.     g_ebp_gold_mine = Villagers_GetEntityBlueprint("gold_mine_01")
  68.     g_ebp_farm = Villagers_GetEntityBlueprint("west_german_farm")
  69.     g_ebp_plantation = Villagers_GetEntityBlueprint("west_german_plantation_mp")
  70.     g_ebp_farm_crates = BP_GetEntityBlueprint("crates_wood_open_s_01")
  71.     g_ebp_west_german_wall = Villagers_GetEntityBlueprint("west_german_wall_mp")
  72.     g_ebp_west_german_wall_gate = Villagers_GetEntityBlueprint("west_german_wall_gate_mp")
  73.     g_ebp_west_german_wall_upgraded = Villagers_GetEntityBlueprint("west_german_wall_upgraded_mp")
  74.     g_ebp_west_german_wall_upgraded_gate = Villagers_GetEntityBlueprint("west_german_wall_upgraded_gate_mp")
  75.     g_ebp_wall_gate_left = Villagers_GetEntityBlueprint("west_german_wall_gate_left_mp")
  76.     g_ebp_wall_gate_right = Villagers_GetEntityBlueprint("west_german_wall_gate_right_mp")
  77.     g_ebp_west_german_outpost = Villagers_GetEntityBlueprint("west_german_outpost_mp")
  78.     g_sbp_hidden_sniper = Villagers_GetSquadBlueprint("hidden_sniper_squad_mp")
  79.     g_upgrade_outpost_cannon = Villagers_GetUpgradeBlueprint("outpost_cannon_upgrade")
  80.     g_upgrade_wall_upgraded = Villagers_GetUpgradeBlueprint("wall_gate_upgrade")
  81.     g_sbp_west_german_villager = Villagers_GetSquadBlueprint("west_german_villager_mp")
  82.     g_ability_gather_food = Villagers_GetAbilityBlueprint("gather_resources_food_ability")
  83.     g_ability_gather_wood = Villagers_GetAbilityBlueprint("gather_resources_wood_ability")
  84.     g_ability_gather_gold = Villagers_GetAbilityBlueprint("gather_resources_gold_ability")
  85.     g_upgrade_max_houses_reached = Villagers_GetUpgradeBlueprint("villagers_max_houses_reached")
  86.     g_ability_open_gate = Villagers_GetAbilityBlueprint("open_gate_ability")
  87.     g_offsets = {OFFSET_FRONT, OFFSET_FRONT_RIGHT, OFFSET_RIGHT, OFFSET_BACK_RIGHT, OFFSET_BACK, OFFSET_BACK_LEFT, OFFSET_LEFT, OFFSET_FRONT_LEFT}
  88.     CUE.VILLAGER_IDLE = {icon = "Icons_events_event_cue_network", sound = "General_alert"}
  89.    
  90.     g_resources = {
  91.         food = {ebp = Villagers_GetRandomFoodEBP, autoalign = true, resource_type = RT_FOOD, ability = g_ability_gather_food},
  92.         wood = {ebp = Villagers_GetRandomWoodEBP, autoalign = false, resource_type = RT_WOOD, ability = g_ability_gather_wood},
  93.         gold = {ebp = Villagers_GetRandomGoldEBP, autoalign = true, resource_type = RT_GOLD, ability = g_ability_gather_gold},
  94.     }
  95.     g_resource_minimap_icon = {
  96.         food = Villagers_GetIcon("resource_minimap_icon_food"),
  97.         wood = Villagers_GetIcon("resource_minimap_icon_wood"),
  98.         gold = Villagers_GetIcon("resource_minimap_icon_gold"),
  99.         special = Villagers_GetIcon("resource_minimap_icon_special"),
  100.     }
  101.     g_ebp_victory_point_marker = Villagers_GetEntityBlueprint("villagers_victory_point_marker")
  102.     g_ebp_territory_point_marker = Villagers_GetEntityBlueprint("villagers_territory_point_marker")
  103.     g_ebp_fuel_point_marker = Villagers_GetEntityBlueprint("villagers_fuel_point_marker")
  104.     g_ebp_munition_point_marker = Villagers_GetEntityBlueprint("villagers_munition_point_marker")
  105.  
  106.     Players_ForEach(function(pid, idx, player)
  107.         local key = Player_GetUniqueKey(player)
  108.         g_player_outpost_level[key] = {level = 0}
  109.         g_player_wall_level[key] = {level = 0}
  110.         g_player_house_counter[key] = {player = player, count = 0, max = false}
  111.         local pos = Player_GetStartingPosition(player)
  112.        
  113.         Repeat(5, function()
  114.             local spawnPos = Util_GetRandomPosition(Util_GetOffsetPosition(pos, Table_GetRandomItem(g_offsets), 18), 10)
  115.             local towardsPos = Util_GetRandomPosition(pos, 15)
  116.             local entity = Entity_CreateAndSpawnToward(g_resources.food.ebp(), nil, spawnPos, towardsPos)
  117.             if g_resources.food.autoalign then
  118.                 Entity_AutoAlign(entity)
  119.             end
  120.         end)
  121.         Repeat(10, function()
  122.             local spawnPos = Util_GetRandomPosition(Util_GetOffsetPosition(pos, Table_GetRandomItem(g_offsets), 18), 10)
  123.             local towardsPos = Util_GetRandomPosition(pos, 15)
  124.             local entity = Entity_CreateAndSpawnToward(g_resources.wood.ebp(), nil, spawnPos, towardsPos)
  125.             if g_resources.wood.autoalign then
  126.                 Entity_AutoAlign(entity)
  127.             end
  128.         end)
  129.         Repeat(5, function()
  130.             local spawnPos = Util_GetRandomPosition(Util_GetOffsetPosition(pos, Table_GetRandomItem(g_offsets), 18), 10)
  131.             local towardsPos = Util_GetRandomPosition(pos, 15)
  132.             local entity = Entity_CreateAndSpawnToward(g_resources.gold.ebp(), nil, spawnPos, towardsPos)
  133.             if g_resources.gold.autoalign then
  134.                 Entity_AutoAlign(entity)
  135.             end
  136.         end)
  137.         Player_SetResource(player, RT_Manpower, 9000)
  138.         Player_SetResource(player, RT_Munition, 9000)
  139.         Player_SetResource(player, RT_Fuel, 9000)
  140.         Modify_PlayerResourceRate(player, RT_Manpower, 0, MUT_Multiplication)
  141.         Modify_PlayerResourceRate(player, RT_Munition, 0, MUT_Multiplication)
  142.         Modify_PlayerResourceRate(player, RT_Fuel, 0, MUT_Multiplication)
  143.         Player_SetPopCapOverride(player, 20)
  144.         local townCenter = Player_GetTownCenter(player)
  145.         if townCenter then
  146.             Rule_AddEntityEvent(Villagers_GenericBuildingDestroyed, townCenter, GE_EntityKilled)
  147.         end
  148.     end)
  149.     Rule_Add(Villagers_ResourceGatherSystem)
  150.    
  151.     World_ForEachEntitiesByBlueprint(g_ebp_victory_point_marker, function(eid, idx, entity)
  152.     local pos = Entity_GetPosition(entity)
  153.         Villagers_PopulatePoint(pos, g_resources.food, 10)
  154.         Villagers_PopulatePoint(pos, g_resources.wood, 5)
  155.         Villagers_PopulatePoint(pos, g_resources.gold, 2)
  156.         MapIcon_CreatePosition(pos, g_resource_minimap_icon.special, 50, Colors.special())
  157.     end)
  158.  
  159.     World_ForEachEntitiesByBlueprint(g_ebp_territory_point_marker, function(eid, idx, entity)
  160.         local pos = Entity_GetPosition(entity)
  161.         Villagers_PopulatePoint(pos, g_resources.food, 20)
  162.         MapIcon_CreatePosition(pos, g_resource_minimap_icon.food, 50, Colors.food())
  163.     end)
  164.    
  165.     World_ForEachEntitiesByBlueprint(g_ebp_fuel_point_marker, function(eid, idx, entity)
  166.         local pos = Entity_GetPosition(entity)
  167.         Villagers_PopulatePoint(pos, g_resources.wood, 40)
  168.         MapIcon_CreatePosition(pos, g_resource_minimap_icon.wood, 50, Colors.wood())
  169.     end)
  170.  
  171.     World_ForEachEntitiesByBlueprint(g_ebp_munition_point_marker, function(eid, idx, entity)
  172.         local pos = Entity_GetPosition(entity)
  173.         Villagers_PopulatePoint(pos, g_resources.gold, 5)
  174.         MapIcon_CreatePosition(pos, g_resource_minimap_icon.gold, 50, Colors.gold())
  175.     end)
  176. end
  177.  
  178. function Player_UpgradeOutposts(player, level)
  179.     local playerKey = Player_GetUniqueKey(player)
  180.     if level == 1 then
  181.         local egroup = Player_GetEntities(player)
  182.         EGroup_Filter(egroup, g_ebp_west_german_outpost, FILTER_KEEP)
  183.         EGroup_ForEach(egroup, function(egid, idx, entity)
  184.             local pos = Entity_GetPosition(entity)
  185.             local sgroup = Entity_GetGarrisonedSquads(entity)
  186.             Cmd_InstantUpgrade(sgroup, g_upgrade_outpost_cannon)
  187.         end)
  188.        
  189.     elseif level == 2 then
  190.        
  191.     end
  192.     g_player_outpost_level[playerKey].level = level
  193. end
  194.  
  195. function Player_UpgradeWalls(player, level)
  196.     local playerKey = Player_GetUniqueKey(player)
  197.     if level == 1 then
  198.         local egroup = Player_GetEntities(player)
  199.         EGroup_Filter(egroup, {g_ebp_west_german_wall, g_ebp_west_german_wall_gate}, FILTER_KEEP)
  200.         EGroup_ForEach(egroup, function(egid, idx, entity)
  201.             local pos = Entity_GetPosition(entity)
  202.             local heading = Entity_GetHeading(entity)
  203.             local towardsPos = GetDirectionalOffset(pos, heading, 5, 0)
  204.             local oldKey = Entity_GetUniqueKey(entity)
  205.             local progress = Entity_GetBuildingProgress(entity)
  206.             local destroy = true
  207.             local health = Entity_GetHealthPercentage(entity)
  208.             if Entity_GetBlueprint(entity) == g_ebp_west_german_wall then
  209.                
  210.                 if progress == 1 then
  211.                     local newEntity = Entity_CreateAndSpawnToward(g_ebp_west_german_wall_upgraded, player, pos, towardsPos)
  212.                     Entity_SetHealth(newEntity, health)
  213.                     if Entity_HasUpgrade(entity, g_upgrade_wall_upgraded) then
  214.                         Cmd_InstantUpgrade(Entity_GetTempEGroup(entity), g_upgrade_wall_upgraded)
  215.                     end
  216.                 else
  217.                     destroy = false
  218.                     g_upgrade_wall_once_finished[oldKey] = {entityId = Entity_GetGameID(entity), pos = pos, towardsPos = towardsPos, health = health, ebp = g_ebp_west_german_wall_upgraded, owner = player}
  219.                 end
  220.             elseif Entity_GetBlueprint(entity) == g_ebp_west_german_wall_gate then
  221.                 local newEntity = Entity_CreateAndSpawnToward(g_ebp_west_german_wall_upgraded_gate, player, pos, towardsPos)
  222.                 Entity_SetHealth(newEntity, health)
  223.                 local newKey = Entity_GetUniqueKey(newEntity)
  224.                 local oldGate = g_gates[oldKey]
  225.                 if oldGate.isOpen then
  226.                     g_skip_gate_open[newKey] = true
  227.                     Cmd_Ability(Entity_GetTempEGroup(newEntity), g_ability_open_gate)
  228.                 end
  229.                 g_gates[newKey] = oldGate
  230.                 g_gates[oldKey] = nil
  231.             end
  232.             if destroy then
  233.                 Entity_Destroy(entity)
  234.             end
  235.            
  236.         end)
  237.        
  238.     elseif level == 2 then
  239.        
  240.     end
  241.     g_player_wall_level[playerKey].level = level
  242. end
  243.  
  244. function Player_GetTownCenter(player, faction)
  245.     faction = faction or Player_GetRaceName(player)
  246.    
  247.     local towncenter_ebp = {
  248.         ["german"] = {ebp = nil},
  249.         ["soviet"] = {ebp = nil},
  250.         ["west_german"] = {ebp = Villagers_GetEntityBlueprint("west_german_hq_villagers_mp")},
  251.         ["aef"] = {ebp = nil},
  252.     }
  253.     if towncenter_ebp[faction] and towncenter_ebp[faction].ebp then
  254.         Msg("checking with " .. faction)
  255.         local filter = towncenter_ebp[faction].ebp
  256.         local egroup = Player_GetEntities(player)
  257.         Msg("#1 count: " .. EGroup_CountSpawned(egroup))
  258.         EGroup_Filter(egroup, filter, FILTER_KEEP)
  259.         Msg("#2 count: " .. EGroup_CountSpawned(egroup))
  260.         if not EGroup_IsEmpty(egroup) then
  261.             Msg("returning " .. Entity_GetName(EGroup_GetSpawnedEntityAt(egroup, 1)))
  262.             return EGroup_GetSpawnedEntityAt(egroup, 1)
  263.         end
  264.     end
  265. end
  266. function Villagers_EventTooManyVillagers(pos)
  267.     UI_CreateMinimapBlip(pos, 2, BT_General)
  268. end
  269.  
  270. function Villagers_EventFarmFull(farmEntity)
  271.     UI_CreateMinimapBlip(Entity_GetPosition(farmEntity), 2, BT_General)
  272.     EventCue_Create(CUE.VILLAGER_IDLE, Util_CreateLocString("Farm maximum Villager count reached!"), nil, Entity_GetTempEGroup(farmEntity))
  273. end
  274. function Villagers_GetRandomFoodEBP()
  275.     return g_ebp_bush_food
  276. end
  277. function Villagers_GetRandomWoodEBP()
  278.     local wood_ebps = {
  279.         {ebp = Villagers_GetEntityBlueprint("wood_pine_01"), weight = 10},
  280.         {ebp = Villagers_GetEntityBlueprint("wood_pine_02"), weight = 10},
  281.         {ebp = Villagers_GetEntityBlueprint("wood_pine_03"), weight = 15},
  282.         {ebp = Villagers_GetEntityBlueprint("wood_pine_04"), weight = 15},
  283.         {ebp = Villagers_GetEntityBlueprint("wood_pine_05"), weight = 15},
  284.         {ebp = Villagers_GetEntityBlueprint("wood_pine_06"), weight = 15},
  285.         {ebp = Villagers_GetEntityBlueprint("wood_pine_07"), weight = 5},
  286.         {ebp = Villagers_GetEntityBlueprint("wood_pine_08"), weight = 3},
  287.     }
  288.    
  289.     local result = {}
  290.     for key, value in ipairs(wood_ebps) do
  291.         for i = 1, value.weight do
  292.             table.insert(result, value)
  293.         end
  294.     end
  295.     return Table_GetRandomItem(result).ebp
  296. end
  297. function Villagers_GetRandomGoldEBP()
  298.     return g_ebp_gold_mine
  299. end
  300.  
  301. function Villagers_PopulatePoint(pos, resource, amount)
  302.     Repeat(amount, function()
  303.         local spawnPos = Util_GetRandomPosition(pos, 20)
  304.         local towardsPos = Util_GetRandomPosition(pos, 20)
  305.         local entity = Entity_CreateAndSpawnToward(resource.ebp(), nil, spawnPos, towardsPos)
  306.         if resource.autoalign then
  307.             Entity_AutoAlign(entity)
  308.         end
  309.     end)
  310. end
  311.  
  312. function Villagers_ResourceGatherSystem()
  313.     for key, item in pairs(g_resource_gather) do
  314.        
  315.         if Squad_IsValid(item.villager) and Entity_IsValid(item.target) and Entity_GetHealth(Entity_FromWorldID(item.target)) > 0 then
  316.             local villager = Squad_FromWorldID(item.villager)
  317.             local entity = Entity_FromWorldID(item.target)
  318.             item.gather_ticks = item.gather_ticks + 1
  319.            
  320.             if (item.gather_ticks == 7) then
  321.                 item.gather_ticks = 0
  322.                 local percentage = 1
  323.                 if not Entity_GetInvulnerable(entity) then
  324.                     percentage = Entity_AddHealth(entity, -1)
  325.                 end
  326.                 Entity_GatherResource(item.caster, entity)
  327.                 if percentage == 0 then
  328.                    
  329.                     local pos = Entity_GetPosition(entity)
  330.                     local villagers = Villagers_GetVillagersGatheringResource(item.target)
  331.                    
  332.  
  333.                     for key2, _item in ipairs(villagers) do
  334.                         Msg("iterating over villager" .. _item.villager)
  335.                         if Squad_IsValid(_item.villager) then
  336.                             local _villager = Squad_FromWorldID(_item.villager)
  337.                             local sg_villager = Squad_GetTempSGroup(_villager)
  338.                             Villagers_EndResourceGathering(_villager, entity)
  339.                            
  340.                             Villager_FindNewResources(_villager, entity)
  341.                         end
  342.                     end
  343.                     Entity_Kill(entity)
  344.                     local delete = Villagers_DeleteResourceEntity(entity)
  345.                 end
  346.             end
  347.         else
  348.             if not Entity_IsValid(item.target) or (Entity_IsValid(item.target) and Entity_GetHealth(Entity_FromWorldID(item.target)) == 0) then
  349.                 Msg("checking.....")
  350.                 local villagers = Villagers_GetVillagersGatheringResource(item.target)
  351.                 for key2, villager in pairs(villagers) do
  352.                     if Squad_IsValid(villager.villager) then
  353.                         local squad = Squad_FromWorldID(villager.villager)
  354.                         Villagers_EndResourceGathering(squad, nil)
  355.                         Villager_FindNewResources(squad, villager.rt, Squad_GetPosition(squad))
  356.                         Msg("end resource via dead")
  357.                     end
  358.                 end
  359.             end
  360.         end
  361.     end
  362.    
  363.     for key, entity in pairs(g_delete_resource_entity) do
  364.         local delete = false
  365.         if Entity_IsValid(entity.id) then
  366.             entity.when = entity.when - 1
  367.             if entity.when == 0 then
  368.                 local _entity = Entity_FromWorldID(entity.id)
  369.                 Entity_Destroy(_entity)
  370.                 delete = true
  371.             end
  372.         else
  373.             delete = true
  374.         end
  375.         if delete then
  376.             g_delete_resource_entity[key] = nil
  377.         end
  378.     end
  379.    
  380.     for key, ability in pairs(g_villager_ability_call) do
  381.         local delete = false
  382.         if Squad_IsValid(ability.squadId) and Entity_IsValid(ability.target) then
  383.             ability.when = ability.when - 1
  384.             if ability.when == 0 then
  385.                 local squad = Squad_FromWorldID(ability.squadId)
  386.                 local target = Entity_GetTempEGroup(Entity_FromWorldID(ability.target))
  387.                
  388.                 Command_SquadEntityAbility(Squad_GetPlayerOwner(squad), Squad_GetTempSGroup(squad), target, ability.ability, true, false)
  389.                 delete = true
  390.             end
  391.         else
  392.             delete = true
  393.         end
  394.         if delete then
  395.             g_villager_ability_call[key] = nil
  396.         end
  397.     end
  398.    
  399.     for key, building in pairs(g_villager_house_simulation) do
  400.         local delete = false
  401.         if Entity_IsValid(building.id) then
  402.             local entity = Entity_FromWorldID(building.id)
  403.             local progress = Entity_GetBuildingProgress(entity)
  404.             if progress > 0 then
  405.                
  406.                 local pos = Entity_GetPosition(entity)
  407.                 local buildingHeight = 3.5
  408.                 if not building.y then
  409.                     building.y = pos.y - buildingHeight
  410.                 end
  411.                
  412.                 pos.y = building.y + (progress * buildingHeight)
  413.                 Entity_SetPosition(entity, pos)
  414.                 if progress == 1 then
  415.                     delete = true
  416.                 end
  417.             end
  418.         else
  419.             delete = true
  420.         end
  421.        
  422.         if delete then
  423.             g_villager_house_simulation[key] = nil
  424.         end
  425.     end
  426.    
  427.     for key, farm in pairs(g_villager_farm) do
  428.         local rectangle = farm.rectangle
  429.         if Entity_IsValid(farm.entityId) then
  430.             local farmEntity = Entity_FromWorldID(farm.entityId)
  431.             local delete = false
  432.             for key, extraVillager in pairs(farm.extra_villagers) do
  433.                 if Squad_IsValid(extraVillager.squadId) then
  434.                     local squad = Squad_FromWorldID(extraVillager.squadId)
  435.                     local squadPos = Squad_GetPosition(squad)
  436.                     local inside = PointInPolygon(rectangle, squadPos.x, squadPos.z)
  437.                     if not inside then
  438.                         delete = true
  439.                     end
  440.                 else
  441.                     delete = true
  442.                 end
  443.                 if delete then
  444.                     farm.extra_villagers[key] = nil
  445.                 end
  446.             end
  447.             for key, slot in ipairs(farm.slots) do
  448.                 local free_slot = false
  449.                 if Squad_IsValid(slot.squadId) then
  450.                     local squad = Squad_FromWorldID(slot.squadId)
  451.                     local squadPos = Squad_GetPosition(squad)
  452.                     local inside = PointInPolygon(rectangle, squadPos.x, squadPos.z)
  453.                     if inside then
  454.                         --Msg("!![" .. World_GetGameTime() .. "] Squad " .. Squad_GetUniqueKey(squad) .. " is in range of the farm!")
  455.                     end
  456.                     if not inside then
  457.                         free_slot = true
  458.                     end
  459.                 else
  460.                     free_slot = true
  461.                 end
  462.                 if free_slot and slot.occupied then
  463.                     slot.occupied = false
  464.                     slot.squadId = 0
  465.                     Msg("Slot " .. key .. " is now free.")
  466.                     farm.villagerCount = farm.villagerCount - 1
  467.                 end
  468.             end
  469.            
  470.             local sg_villagers = Team_GetSquadsNearPoint(farm.team, farm.pos, 15)
  471.             SGroup_Filter(sg_villagers, g_sbp_west_german_villager, FILTER_KEEP)
  472.             SGroup_ForEach(sg_villagers, function(sgid, idx, squad)
  473.                 local squadPos = Squad_GetPosition(squad)
  474.                 local inside = PointInPolygon(rectangle, squadPos.x, squadPos.z)
  475.                 if inside then
  476.                     --Msg("[" .. World_GetGameTime() .. "] Squad " .. Squad_GetUniqueKey(squad) .. " is in range of the farm!")
  477.                 end
  478.                 if inside and not Villagers_SquadIsInFarm(farm, squad) then
  479.                    
  480.                     local slot = Villagers_Farm_GetAvailableSlot(farm, squad)
  481.                     local sg_villager = Squad_GetTempSGroup(squad)
  482.                     local squadKey = Squad_GetUniqueKey(squad)
  483.                     if slot then
  484.                         Cmd_Move(sg_villager, Entity_GetPosition(slot))
  485.                         local ability = nil
  486.                         if farm.type == FARMTYPE.FOOD then
  487.                             ability = g_ability_gather_food
  488.                         elseif farm.type == FARMTYPE.GOLD then
  489.                             ability = g_ability_gather_gold
  490.                         end
  491.                         g_villager_ability_call[squadKey] = {squadId = Squad_GetGameID(squad), ability = ability, target = Entity_GetGameID(slot), when = 8}
  492.                         Msg("squad " .. squadKey .. " is now working for the farm! (" .. BP_GetName(ability) .. ")")
  493.                     else
  494.                         local nextAvailableFarm = Villagers_FindAvailableFarm(farm)
  495.                         if nextAvailableFarm then
  496.                             Cmd_Move(sg_villager, Entity_GetPosition(nextAvailableFarm))
  497.                         elseif not farm.extra_villagers[squadKey] then
  498.                             farm.extra_villagers[squadKey] = {squadId = Squad_GetGameID(squad)}
  499.                             Villagers_EventFarmFull(farmEntity)
  500.                         end
  501.                     end
  502.                 end
  503.             end)
  504.         end
  505.     end
  506.    
  507.     for key, task in ipairs(g_delayed_task) do
  508.         task.when = task.when - 1
  509.         if task.when == 0 then
  510.             task.f()
  511.             table.remove(g_delayed_task, key)
  512.         end
  513.     end
  514.    
  515.     for key, wall in pairs(g_upgrade_wall_once_finished) do
  516.         local delete = false
  517.         if Entity_IsValid(wall.entityId) then
  518.             local entity = Entity_FromWorldID(wall.entityId)
  519.             if Entity_GetBuildingProgress(entity) == 1 then
  520.                 Entity_Destroy(entity)
  521.                 local newEntity = Entity_CreateAndSpawnToward(wall.ebp, wall.owner, wall.pos, wall.towardsPos)
  522.                 delete = true
  523.             end
  524.         else
  525.             delete = true
  526.         end
  527.        
  528.         if delete then
  529.             g_upgrade_wall_once_finished[key] = nil
  530.         end
  531.     end
  532. end
  533.  
  534. function Villagers_OpenGate(caster, target)
  535.     local key = Entity_GetUniqueKey(caster)
  536.    
  537.     if g_skip_gate_open[key] then
  538.         g_skip_gate_open[key] = nil
  539.     elseif g_gates[key] then
  540.         g_gates[key].isOpen = true
  541.         Msg("OpenGate: " .. scartype_tostring(caster) .. " and " .. scartype_tostring(target))
  542.         local gate = g_gates[key]
  543.         local health = Entity_GetHealthPercentage(caster)
  544.         local bp = Entity_GetBlueprint(caster)
  545.         Entity_Destroy(caster)
  546.    
  547.         local newGate = Entity_CreateAndSpawnToward(bp, gate.owner, gate.open.pos, gate.open.towards)
  548.         Entity_SetHealth(newGate, health)
  549.         local newKey = Entity_GetUniqueKey(newGate)
  550.         g_gates[newKey] = gate
  551.         g_gates[key] = nil
  552.         g_skip_gate_open[newKey] = true
  553.         Cmd_Ability(Entity_GetTempEGroup(newGate), g_ability_open_gate)
  554.         Misc_SelectEntity(newGate)
  555.     end
  556. end
  557.  
  558. function Villagers_CloseGate(caster, target)
  559.     Msg("CloseGate: " .. scartype_tostring(caster) .. " and " .. scartype_tostring(target))
  560.     local key = Entity_GetUniqueKey(caster)
  561.     if g_gates[key] then
  562.         g_gates[key].isOpen = false
  563.         local gate = g_gates[key]
  564.         local health = Entity_GetHealthPercentage(caster)
  565.         local bp = Entity_GetBlueprint(caster)
  566.         Entity_Destroy(caster)
  567.         local newGate = Entity_CreateAndSpawnToward(bp, gate.owner, gate.closed.pos, gate.closed.towards)
  568.         Entity_SetHealth(newGate, health)
  569.         local newKey = Entity_GetUniqueKey(newGate)
  570.         g_gates[newKey] = gate
  571.         g_gates[key] = nil
  572.         Misc_SelectEntity(newGate)
  573.     end
  574. end
  575. function Villagers_WallGateUpgradeComplete(caster, target)
  576.  
  577.     Msg(Entity_GetUniqueKey(caster) .. " and " .. Entity_GetUniqueKey(target))
  578.    
  579.     local pos = Entity_GetPosition(caster)
  580.     local heading = Entity_GetHeading(caster)
  581.     local owner = Entity_GetPlayerOwner(caster)
  582.     local playerKey = Player_GetUniqueKey(owner)
  583.     local facingPos = GetDirectionalOffset(pos, heading, 3, 0)
  584.     local health = Entity_GetHealthPercentage(caster)
  585.     Entity_Destroy(caster)
  586.     local bp = nil
  587.     if g_player_wall_level[playerKey].level == 0 then
  588.         bp = g_ebp_west_german_wall_gate
  589.     else
  590.         bp = g_ebp_west_german_wall_upgraded_gate
  591.     end
  592.     local gate = Entity_CreateAndSpawnToward(bp, owner, pos, facingPos)
  593.     Entity_SetHealth(gate, health)
  594.     Villagers_RegisterGate(gate, false)
  595.     Misc_SelectEntity(gate)
  596.    
  597.     local pos_visual_gate_left = GetDirectionalOffset(pos, heading, 4, 90)
  598.     local pos_visual_gate_right = GetDirectionalOffset(pos, heading, 4, -90)
  599.    
  600.     local towardsPos_left = GetDirectionalOffset(pos, heading, 5.7,  135)
  601.     local towardsPos_right = GetDirectionalOffset(pos, heading, 5.7, -135)
  602.  
  603.     local visual_gate_left = Entity_CreateAndSpawnToward(g_ebp_wall_gate_left, owner, pos_visual_gate_left, towardsPos_left)
  604.     local visual_gate_right = Entity_CreateAndSpawnToward(g_ebp_wall_gate_right, owner, pos_visual_gate_right, towardsPos_right)
  605.    
  606.     --dr_drawline(Pos_AddHeight(pos_visual_gate_left, 5), Pos_AddHeight(towardsPos_left, 5), 255, 255, 255, "view" )
  607.     --dr_drawline(Pos_AddHeight(pos_visual_gate_right, 5), Pos_AddHeight(towardsPos_right, 5), 255, 0, 0, "view" )
  608.     --Entity_SetInvulnerable(visual_gate_left, true, -1)
  609.     --Entity_SetInvulnerable(visual_gate_right, true, -1)
  610. end
  611.  
  612. function Villagers_RegisterGate(entity, isOpen)
  613.     local id = Entity_GetGameID(entity)
  614.     local key = Entity_GetUniqueKey(entity)
  615.     local owner = Entity_GetPlayerOwner(entity)
  616.     local pos = Entity_GetPosition(entity)
  617.     local heading = Entity_GetHeading(entity)
  618.     local towards_closed = GetDirectionalOffset(pos, heading, 3, 0)
  619.    
  620.     local pos_open = GetDirectionalOffset(pos, heading, 4, 292.5)
  621.     local towards_open = GetDirectionalOffset(pos, heading, 3, 60)
  622.     local heading_closed = Heading_Rotate(heading, 0)
  623.     local heading_open = Heading_Rotate(heading, 90)
  624.    
  625.    
  626.     g_gates[key] = {entityId = id, owner = owner, isOpen = isOpen, closed = {pos = pos, towards = towards_closed, heading = heading_closed}, open = {pos = pos_open, towards = towards_open, heading = heading_open}}  
  627. end
  628.  
  629. function Villagers_PlayerWallUpgradeCompleted(caster, target)
  630.     Player_UpgradeWalls(caster, 1)
  631. end
  632.  
  633. function Villagers_PlantationCompleted(caster, target)
  634.     local key = Entity_GetUniqueKey(caster)
  635.     Rule_AddEntityEvent(Villagers_FarmDestroyed, caster, GE_EntityKilled)
  636.     local pos = Entity_GetPosition(caster)
  637.     local heading = Entity_GetHeading(caster)
  638.     local heading_2 = Heading_Rotate(heading, World_GetRand(0, 360))
  639.     Entity_SetHeading(caster, heading_2, false)
  640.     local owner = Entity_GetPlayerOwner(caster)
  641.     local plantation = {
  642.         type = FARMTYPE.GOLD,
  643.         slots = {},
  644.         owner = owner,
  645.         entityId = Entity_GetGameID(caster),
  646.         pos = pos, team = Player_GetTeam(owner),
  647.         villagerCount = 0,
  648.         extra_villagers = {},
  649.         extra_entities = {},
  650.         diagonalDistance = 14,
  651.     }
  652.     local bushes = {
  653.         {pos = Util_GetOffsetPosition(pos, OFFSET_LEFT, 4), ebp = Villagers_GetEntityBlueprint("plantation_bush_main_01")},
  654.         {pos = Util_GetOffsetPosition(pos, OFFSET_RIGHT, 4), ebp = Villagers_GetEntityBlueprint("plantation_bush_main_02")},
  655.     }
  656.    
  657.     local pos_shed_01 = GetDirectionalOffset(pos, heading, 12, 60)
  658.     local pos_shed_02 = GetDirectionalOffset(pos, heading, 10, 110)
  659.     local towardsPos_shed_02 = GetDirectionalOffset(pos, heading, 4, 180)
  660.     local shed_01 = Entity_CreateAndSpawnToward(Villagers_GetEntityBlueprint("plantation_shed_01"), owner, pos_shed_01, pos_shed_01)
  661.     local shed_02 = Entity_CreateAndSpawnToward(Villagers_GetEntityBlueprint("plantation_shed_02"), owner, pos_shed_02, towardsPos_shed_02)
  662.     local pos_barrel_01 = GetDirectionalOffset(pos, heading, 6, 90)
  663.     local pos_barrel_02 = GetDirectionalOffset(pos, heading, 6, -90)
  664.     local ebp_barrel = BP_GetEntityBlueprint("wood_dark_m_03")
  665.     local barrel_01 = Entity_CreateAndSpawnToward(ebp_barrel, nil, pos_barrel_01, Util_GetRandomPosition(pos, 10))
  666.     local barrel_02 = Entity_CreateAndSpawnToward(ebp_barrel, nil, pos_barrel_02, Util_GetRandomPosition(pos, 10))
  667.     table.insert(plantation.extra_entities, Entity_GetGameID(shed_01))
  668.     table.insert(plantation.extra_entities, Entity_GetGameID(shed_02))
  669.     table.insert(plantation.extra_entities, Entity_GetGameID(barrel_01))
  670.     table.insert(plantation.extra_entities, Entity_GetGameID(barrel_02))
  671.     local function _newFarmSlot(entity)
  672.         return {entity = entity, occupied = false, squadId = 0}
  673.     end
  674.    
  675.     local bush_ebp = {Villagers_GetEntityBlueprint("plantation_bush_01"), Villagers_GetEntityBlueprint("plantation_bush_02"), Villagers_GetEntityBlueprint("plantation_bush_03"), Villagers_GetEntityBlueprint("plantation_bush_04")}
  676.     for key, bush in ipairs(bushes) do
  677.         local headingPos = Util_GetOffsetPosition(bush.pos, OFFSET_FRONT, 6)
  678.         --headingPos = Util_GetRandomPosition(headingPos, 3)
  679.         local entity = Entity_CreateAndSpawnToward(bush.ebp, nil, bush.pos, pos)
  680.         table.insert(plantation.slots, _newFarmSlot(entity))
  681.         local spawnPos = headingPos
  682.         headingPos = Util_GetOffsetPosition(headingPos, OFFSET_RIGHT, 2)
  683.         local bush_front_01 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
  684.         spawnPos = headingPos
  685.         headingPos = Util_GetOffsetPosition(headingPos, OFFSET_LEFT, 2)
  686.         local bush_front_02 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
  687.        
  688.         spawnPos = Util_GetOffsetPosition(bush.pos, OFFSET_BACK, 6)
  689.         headingPos = Util_GetOffsetPosition(spawnPos, OFFSET_RIGHT, 2)
  690.         local bush_back_01 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
  691.         spawnPos = headingPos
  692.         headingPos = Util_GetOffsetPosition(headingPos, OFFSET_LEFT, 2)
  693.         local bush_back_02 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
  694.         table.insert(plantation.slots, _newFarmSlot(bush_front_01))
  695.         table.insert(plantation.slots, _newFarmSlot(bush_front_02))
  696.         table.insert(plantation.slots, _newFarmSlot(bush_back_01))
  697.         table.insert(plantation.slots, _newFarmSlot(bush_back_02))
  698.     end
  699.     plantation.rectangle = Villagers_GetFarmRectangle(plantation)
  700.     g_villager_farm[key] = plantation
  701. end
  702. function Villagers_FarmCompleted(caster, target)
  703.     local key = Entity_GetUniqueKey(caster)
  704.     Rule_AddEntityEvent(Villagers_FarmDestroyed, caster, GE_EntityKilled)
  705.     local pos = Entity_GetPosition(caster)
  706.     local heading = Entity_GetHeading(caster)
  707.     local pos_crates = GetDirectionalOffset(pos, heading, 3, 0)
  708.     local crates = Entity_CreateAndSpawnToward(g_ebp_farm_crates, nil, pos_crates, Util_GetRandomPosition(pos, 10))
  709.     local owner = Entity_GetPlayerOwner(caster)
  710.     local farm = {
  711.         type = FARMTYPE.FOOD,
  712.         slots = {},
  713.         owner = owner,
  714.         entityId = Entity_GetGameID(caster),
  715.         pos = pos,
  716.         team = Player_GetTeam(owner),
  717.         villagerCount = 0,
  718.         extra_villagers = {},
  719.         extra_entities = {Entity_GetGameID(crates)},
  720.         diagonalDistance = 12,
  721.     }
  722.     local bushes = {
  723.         {pos = Util_GetOffsetPosition(pos, OFFSET_LEFT, 4), ebp = Villagers_GetEntityBlueprint("farm_bush_01")},
  724.         {pos = Util_GetOffsetPosition(pos, OFFSET_RIGHT, 4), ebp = Villagers_GetEntityBlueprint("farm_bush_02")},
  725.     }
  726.    
  727.     local function _newFarmSlot(entity)
  728.         return {entity = entity, occupied = false, squadId = 0}
  729.     end
  730.    
  731.     local bush_ebp = {Villagers_GetEntityBlueprint("farm_bush_regular_01"), Villagers_GetEntityBlueprint("farm_bush_regular_02"), Villagers_GetEntityBlueprint("farm_bush_regular_03")}
  732.     for key, bush in ipairs(bushes) do
  733.         local headingPos = Util_GetOffsetPosition(bush.pos, OFFSET_FRONT, 6)
  734.         --headingPos = Util_GetRandomPosition(headingPos, 3)
  735.         local entity = Entity_CreateAndSpawnToward(bush.ebp, nil, bush.pos, pos)
  736.         table.insert(farm.slots, _newFarmSlot(entity))
  737.         local spawnPos = headingPos
  738.         headingPos = Util_GetOffsetPosition(headingPos, OFFSET_RIGHT, 2)
  739.         local bush_front_01 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
  740.         spawnPos = headingPos
  741.         headingPos = Util_GetOffsetPosition(headingPos, OFFSET_LEFT, 2)
  742.         local bush_front_02 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
  743.        
  744.         spawnPos = Util_GetOffsetPosition(bush.pos, OFFSET_BACK, 6)
  745.         headingPos = Util_GetOffsetPosition(spawnPos, OFFSET_RIGHT, 2)
  746.         local bush_back_01 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
  747.         spawnPos = headingPos
  748.         headingPos = Util_GetOffsetPosition(headingPos, OFFSET_LEFT, 2)
  749.         local bush_back_02 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
  750.         table.insert(farm.slots, _newFarmSlot(bush_front_01))
  751.         table.insert(farm.slots, _newFarmSlot(bush_front_02))
  752.         table.insert(farm.slots, _newFarmSlot(bush_back_01))
  753.         table.insert(farm.slots, _newFarmSlot(bush_back_02))
  754.     end
  755.    
  756.     for key, slot in ipairs(farm.slots) do
  757.         Entity_SetInvulnerable(slot.entity, true, -1)
  758.     end
  759.     farm.rectangle = Villagers_GetFarmRectangle(farm)
  760.     g_villager_farm[key] = farm
  761. end
  762.  
  763. function Villagers_FarmUpgrade01Completed(caster, target)
  764.    
  765. end
  766.  
  767. function Villagers_FarmUpgrade02Completed(caster, target)
  768.    
  769. end
  770. function Villagers_OutpostUpgrade01Completed(caster, target)
  771.     --Msg(scartype_tostring(caster) .. " and " .. scartype_tostring(target))
  772.     Player_UpgradeOutposts(caster, 1)
  773.    
  774. end
  775. function Villagers_OutpostUpgrade02Completed(caster, target)
  776.     Player_UpgradeOutposts(caster, 2)
  777. end
  778.  
  779. function Villagers_TownCenterSpawned(caster, target)
  780.     --Entity_SetInvulnerableMinCap(caster, 0.2, -1)
  781.     --Msg("set towncenter invulnerable cap")
  782. end
  783.  
  784. function Villagers_ChurchCompleted(caster, target)
  785.    
  786. end
  787. function Villagers_OutpostCompleted(caster, target)
  788.     local pos = Entity_GetPosition(caster)
  789.     local owner = Entity_GetPlayerOwner(caster)
  790.     local playerKey = Player_GetUniqueKey(owner)
  791.     local sbp = g_sbp_hidden_sniper
  792.     local sniper = Squad_CreateAndSpawnToward(sbp, owner, SQUAD_SIZE_DEFAULT, pos, pos)
  793.     Cmd_Garrison(Squad_GetTempSGroup(sniper), Entity_GetTempEGroup(caster), true, false, true)
  794.    
  795.     local outpostLevel = g_player_outpost_level[playerKey].level
  796.     if outpostLevel == 1 then
  797.         Cmd_InstantUpgrade(Squad_GetTempSGroup(sniper), g_upgrade_outpost_cannon)
  798.     elseif outpostLevel == 2 then
  799.        
  800.     end
  801.  
  802. end
  803. function Villagers_WallCompleted(caster, target)
  804.     Rule_AddEntityEvent(Villagers_WallDestroyed, caster, GE_EntityKilled)
  805. end
  806.  
  807. function Villagers_WallDestroyed(entity, killer)
  808.     local key = Entity_GetUniqueKey(entity)
  809.     g_gates[key] = nil
  810. end
  811. function Villagers_PlantationDestroyed(entity, killer)
  812.    
  813. end
  814. function Villagers_FarmDestroyed(entity, killer)
  815.     Villagers_DeleteResourceEntity(entity, 8)
  816.     local key = Entity_GetUniqueKey(entity)
  817.     local farm = g_villager_farm[key]
  818.     if farm then
  819.         for key, slot in ipairs(farm.slots) do
  820.             Entity_Kill(slot.entity)
  821.             Villagers_DeleteResourceEntity(slot.entity, World_GetRand(6, 14))
  822.         end
  823.         for key, value in ipairs(farm.extra_entities) do
  824.             if Entity_IsValid(value) then
  825.                 local entity = Entity_FromWorldID(value)
  826.                 Entity_Kill(entity)
  827.                 Villagers_DeleteResourceEntity(entity, World_GetRand(6, 14))
  828.             end
  829.         end
  830.         g_villager_farm[key] = nil
  831.     end
  832. end
  833.  
  834. function Villagers_BeginResourceGathering(caster, target)
  835.     caster = Entity_CheckForParentSquad(caster)
  836.     --Msg(scartype_tostring(caster) .. "started gathering stuff from " .. scartype_tostring(target))
  837.     local key = Squad_GetUniqueKey(caster)
  838.     local villagers = Villagers_GetVillagersGatheringResource(Entity_GetGameID(target))
  839.     if table.getn(villagers) < g_configuration.max_villagers_per_resource then
  840.         g_resource_gather[key] = {villager = Squad_GetGameID(caster), target = Entity_GetGameID(target), gather_ticks = 0, caster = Squad_GetPlayerOwner(caster), rt = Villagers_GetEntityResourceType(target)}
  841.     else
  842.         Cmd_Stop(Squad_GetTempSGroup(caster))
  843.         Villager_FindNewResources(caster, target)
  844.         EventCue_Create(CUE.VILLAGER_IDLE, Util_CreateLocString("No room for additional Villagers"), nil, Entity_GetTempEGroup(target))
  845.     end
  846. end
  847.  
  848. function Villagers_EndResourceGathering(caster, target)
  849.     caster = Entity_CheckForParentSquad(caster)
  850.     Msg(scartype_tostring(caster) .. "stopped gathering stuff from " .. scartype_tostring(target))
  851.     local key = Squad_GetUniqueKey(caster)
  852.     g_resource_gather[key] = nil
  853. end
  854.  
  855. function Villagers_VillagerSpawned(caster)
  856.     caster = Entity_CheckForParentSquad(caster)
  857.     Rule_AddSquadEvent(Villagers_VillagerKilled, caster, GE_SquadKilled)
  858.     Msg("Villager spawned!")
  859. end
  860.  
  861. function Villagers_HouseStarted(caster, target)
  862.     Rule_AddEntityEvent(Villagers_HouseDestroyed, caster, GE_EntityKilled)
  863.     local key = Entity_GetUniqueKey(caster)
  864.     g_villager_house_simulation[key] = {id = Entity_GetGameID(caster)}
  865.     Villagers_PlayerHouseCounter_Add(Entity_GetPlayerOwner(caster), 1)
  866. end
  867.  
  868. function Villagers_GenericBuildingStarted(caster, target)
  869.     Rule_AddEntityEvent(Villagers_GenericBuildingDestroyed, caster, GE_EntityKilled)
  870. end
  871.  
  872. function Villagers_GenericBuildingDestroyed(entity, killer)
  873.     Villagers_DeleteResourceEntity(entity)
  874. end
  875.  
  876. function Villagers_HouseCompleted(caster, target)
  877.     local owner = Entity_GetPlayerOwner(caster)
  878.     Player_AddPopulation(owner, g_configuration.population_per_house)
  879. end
  880.  
  881. function Villagers_VillagerKilled(squad, killer)
  882.     local key = Squad_GetUniqueKey(squad)
  883.     g_resource_gather[key] = nil
  884.     Msg("removed resource gather from dead villager")
  885. end
  886.  
  887. function Villagers_HouseDestroyed(entity, killer)
  888.     local key = Entity_GetUniqueKey(entity)
  889.     g_villager_house_simulation[key] = nil
  890.     if Entity_GetBuildingProgress(entity) == 1 then
  891.         local owner = Entity_GetPlayerOwner(entity)
  892.         Player_AddPopulation(owner, 0 - g_configuration.population_per_house)
  893.     end
  894.     --Entity_Destroy(entity)
  895.     Villagers_DeleteResourceEntity(entity)
  896.     Villagers_PlayerHouseCounter_Add(Entity_GetPlayerOwner(entity), -1)
  897. end
  898.  
  899. function Villagers_PlayerHouseCounter_Add(player, amount)
  900.     local key = Player_GetUniqueKey(player)
  901.     local counter = g_player_house_counter[key]
  902.     counter.count = counter.count + amount
  903.     Msg("House count: " .. counter.count)
  904.     if counter.count >= g_configuration.max_houses_per_player and not counter.max then
  905.         counter.max = true
  906.         Msg("adding max house count upgrade")
  907.         Player_CompleteUpgrade(player, g_upgrade_max_houses_reached)
  908.     elseif counter.count < g_configuration.max_houses_per_player and counter.max then
  909.         counter.max = false
  910.         Player_RemoveUpgrade(player, g_upgrade_max_houses_reached)
  911.         Msg("removing max house count upgrade")
  912.     end
  913. end
  914.  
  915.  
  916. --[[
  917. FRONT_LEFT  FRONT   FRONT_RIGHT
  918.  
  919.              /\
  920.             /  \
  921. LEFT       /_  _\        RIGHT
  922.              ||
  923.              ||
  924.              ||
  925.              
  926. BACK_LEFT   BACK    BACK_RIGHT
  927. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement