Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import("ScarUtil.scar")
- import("WinConditions/victorypointplusannihilate.scar")
- import("Fatalities/Fatalities.scar")
- import("WinConditions/lib.scar")
- function Villagers_PreInit()
- --loadfile("scar/villagers/core.scar")()
- Rule_AddOneShot(Villagers_Init, 1/8)
- end
- Scar_AddInit(Villagers_PreInit)
- function Villagers_Init()
- g_guid = "6886f8c95f9c4483b5f790e4198047eb"
- g_enable_messages = true
- g_text = ""
- g_text_line_count = 0
- Msg("Initialized")
- player_generic = World_GetPlayerAt(1)
- AI_EnableAll(false)
- colorUnPacker = {
- __call = function(t)
- return unpack(t)
- end
- }
- Colors = {
- food = {255, 0, 0, 255},
- wood = {97, 160, 63, 255},
- gold = {254, 221, 0, 255},
- special = {255, 0, 252, 255},
- }
- g_configuration = {
- max_villagers_per_resource = 10,
- max_villagers_per_farm = 10,
- population_per_house = 8,
- max_houses_per_player = 10,
- }
- RT_FOOD = 1
- RT_WOOD = 2
- RT_GOLD = 3
- FARMTYPE = {
- FOOD = 1,
- GOLD = 2,
- }
- for key, _t in pairs(Colors) do
- setmetatable(_t, colorUnPacker)
- end
- SQUAD_SIZE_DEFAULT = 0
- g_max_villagers_per_resource = 2
- g_resource_gather = {}
- g_delete_resource_entity = {}
- g_villager_ability_call = {}
- g_villager_house_simulation = {}
- g_player_house_counter = {}
- g_villager_farm = {}
- g_player_plantation = {}
- g_player_outpost_level = {}
- g_player_wall_level = {}
- g_gates = {}
- g_skip_gate_open = {}
- g_delayed_task = {}
- g_upgrade_wall_once_finished = {}
- g_ebp_bush_food = Villagers_GetEntityBlueprint("bush_food_01")
- g_ebp_pine_wood = Villagers_GetEntityBlueprint("pine_wood_01")
- g_ebp_gold_mine = Villagers_GetEntityBlueprint("gold_mine_01")
- g_ebp_farm = Villagers_GetEntityBlueprint("west_german_farm")
- g_ebp_plantation = Villagers_GetEntityBlueprint("west_german_plantation_mp")
- g_ebp_farm_crates = BP_GetEntityBlueprint("crates_wood_open_s_01")
- g_ebp_west_german_wall = Villagers_GetEntityBlueprint("west_german_wall_mp")
- g_ebp_west_german_wall_gate = Villagers_GetEntityBlueprint("west_german_wall_gate_mp")
- g_ebp_west_german_wall_upgraded = Villagers_GetEntityBlueprint("west_german_wall_upgraded_mp")
- g_ebp_west_german_wall_upgraded_gate = Villagers_GetEntityBlueprint("west_german_wall_upgraded_gate_mp")
- g_ebp_wall_gate_left = Villagers_GetEntityBlueprint("west_german_wall_gate_left_mp")
- g_ebp_wall_gate_right = Villagers_GetEntityBlueprint("west_german_wall_gate_right_mp")
- g_ebp_west_german_outpost = Villagers_GetEntityBlueprint("west_german_outpost_mp")
- g_sbp_hidden_sniper = Villagers_GetSquadBlueprint("hidden_sniper_squad_mp")
- g_upgrade_outpost_cannon = Villagers_GetUpgradeBlueprint("outpost_cannon_upgrade")
- g_upgrade_wall_upgraded = Villagers_GetUpgradeBlueprint("wall_gate_upgrade")
- g_sbp_west_german_villager = Villagers_GetSquadBlueprint("west_german_villager_mp")
- g_ability_gather_food = Villagers_GetAbilityBlueprint("gather_resources_food_ability")
- g_ability_gather_wood = Villagers_GetAbilityBlueprint("gather_resources_wood_ability")
- g_ability_gather_gold = Villagers_GetAbilityBlueprint("gather_resources_gold_ability")
- g_upgrade_max_houses_reached = Villagers_GetUpgradeBlueprint("villagers_max_houses_reached")
- g_ability_open_gate = Villagers_GetAbilityBlueprint("open_gate_ability")
- g_offsets = {OFFSET_FRONT, OFFSET_FRONT_RIGHT, OFFSET_RIGHT, OFFSET_BACK_RIGHT, OFFSET_BACK, OFFSET_BACK_LEFT, OFFSET_LEFT, OFFSET_FRONT_LEFT}
- CUE.VILLAGER_IDLE = {icon = "Icons_events_event_cue_network", sound = "General_alert"}
- g_resources = {
- food = {ebp = Villagers_GetRandomFoodEBP, autoalign = true, resource_type = RT_FOOD, ability = g_ability_gather_food},
- wood = {ebp = Villagers_GetRandomWoodEBP, autoalign = false, resource_type = RT_WOOD, ability = g_ability_gather_wood},
- gold = {ebp = Villagers_GetRandomGoldEBP, autoalign = true, resource_type = RT_GOLD, ability = g_ability_gather_gold},
- }
- g_resource_minimap_icon = {
- food = Villagers_GetIcon("resource_minimap_icon_food"),
- wood = Villagers_GetIcon("resource_minimap_icon_wood"),
- gold = Villagers_GetIcon("resource_minimap_icon_gold"),
- special = Villagers_GetIcon("resource_minimap_icon_special"),
- }
- g_ebp_victory_point_marker = Villagers_GetEntityBlueprint("villagers_victory_point_marker")
- g_ebp_territory_point_marker = Villagers_GetEntityBlueprint("villagers_territory_point_marker")
- g_ebp_fuel_point_marker = Villagers_GetEntityBlueprint("villagers_fuel_point_marker")
- g_ebp_munition_point_marker = Villagers_GetEntityBlueprint("villagers_munition_point_marker")
- Players_ForEach(function(pid, idx, player)
- local key = Player_GetUniqueKey(player)
- g_player_outpost_level[key] = {level = 0}
- g_player_wall_level[key] = {level = 0}
- g_player_house_counter[key] = {player = player, count = 0, max = false}
- local pos = Player_GetStartingPosition(player)
- Repeat(5, function()
- local spawnPos = Util_GetRandomPosition(Util_GetOffsetPosition(pos, Table_GetRandomItem(g_offsets), 18), 10)
- local towardsPos = Util_GetRandomPosition(pos, 15)
- local entity = Entity_CreateAndSpawnToward(g_resources.food.ebp(), nil, spawnPos, towardsPos)
- if g_resources.food.autoalign then
- Entity_AutoAlign(entity)
- end
- end)
- Repeat(10, function()
- local spawnPos = Util_GetRandomPosition(Util_GetOffsetPosition(pos, Table_GetRandomItem(g_offsets), 18), 10)
- local towardsPos = Util_GetRandomPosition(pos, 15)
- local entity = Entity_CreateAndSpawnToward(g_resources.wood.ebp(), nil, spawnPos, towardsPos)
- if g_resources.wood.autoalign then
- Entity_AutoAlign(entity)
- end
- end)
- Repeat(5, function()
- local spawnPos = Util_GetRandomPosition(Util_GetOffsetPosition(pos, Table_GetRandomItem(g_offsets), 18), 10)
- local towardsPos = Util_GetRandomPosition(pos, 15)
- local entity = Entity_CreateAndSpawnToward(g_resources.gold.ebp(), nil, spawnPos, towardsPos)
- if g_resources.gold.autoalign then
- Entity_AutoAlign(entity)
- end
- end)
- Player_SetResource(player, RT_Manpower, 9000)
- Player_SetResource(player, RT_Munition, 9000)
- Player_SetResource(player, RT_Fuel, 9000)
- Modify_PlayerResourceRate(player, RT_Manpower, 0, MUT_Multiplication)
- Modify_PlayerResourceRate(player, RT_Munition, 0, MUT_Multiplication)
- Modify_PlayerResourceRate(player, RT_Fuel, 0, MUT_Multiplication)
- Player_SetPopCapOverride(player, 20)
- local townCenter = Player_GetTownCenter(player)
- if townCenter then
- Rule_AddEntityEvent(Villagers_GenericBuildingDestroyed, townCenter, GE_EntityKilled)
- end
- end)
- Rule_Add(Villagers_ResourceGatherSystem)
- World_ForEachEntitiesByBlueprint(g_ebp_victory_point_marker, function(eid, idx, entity)
- local pos = Entity_GetPosition(entity)
- Villagers_PopulatePoint(pos, g_resources.food, 10)
- Villagers_PopulatePoint(pos, g_resources.wood, 5)
- Villagers_PopulatePoint(pos, g_resources.gold, 2)
- MapIcon_CreatePosition(pos, g_resource_minimap_icon.special, 50, Colors.special())
- end)
- World_ForEachEntitiesByBlueprint(g_ebp_territory_point_marker, function(eid, idx, entity)
- local pos = Entity_GetPosition(entity)
- Villagers_PopulatePoint(pos, g_resources.food, 20)
- MapIcon_CreatePosition(pos, g_resource_minimap_icon.food, 50, Colors.food())
- end)
- World_ForEachEntitiesByBlueprint(g_ebp_fuel_point_marker, function(eid, idx, entity)
- local pos = Entity_GetPosition(entity)
- Villagers_PopulatePoint(pos, g_resources.wood, 40)
- MapIcon_CreatePosition(pos, g_resource_minimap_icon.wood, 50, Colors.wood())
- end)
- World_ForEachEntitiesByBlueprint(g_ebp_munition_point_marker, function(eid, idx, entity)
- local pos = Entity_GetPosition(entity)
- Villagers_PopulatePoint(pos, g_resources.gold, 5)
- MapIcon_CreatePosition(pos, g_resource_minimap_icon.gold, 50, Colors.gold())
- end)
- end
- function Player_UpgradeOutposts(player, level)
- local playerKey = Player_GetUniqueKey(player)
- if level == 1 then
- local egroup = Player_GetEntities(player)
- EGroup_Filter(egroup, g_ebp_west_german_outpost, FILTER_KEEP)
- EGroup_ForEach(egroup, function(egid, idx, entity)
- local pos = Entity_GetPosition(entity)
- local sgroup = Entity_GetGarrisonedSquads(entity)
- Cmd_InstantUpgrade(sgroup, g_upgrade_outpost_cannon)
- end)
- elseif level == 2 then
- end
- g_player_outpost_level[playerKey].level = level
- end
- function Player_UpgradeWalls(player, level)
- local playerKey = Player_GetUniqueKey(player)
- if level == 1 then
- local egroup = Player_GetEntities(player)
- EGroup_Filter(egroup, {g_ebp_west_german_wall, g_ebp_west_german_wall_gate}, FILTER_KEEP)
- EGroup_ForEach(egroup, function(egid, idx, entity)
- local pos = Entity_GetPosition(entity)
- local heading = Entity_GetHeading(entity)
- local towardsPos = GetDirectionalOffset(pos, heading, 5, 0)
- local oldKey = Entity_GetUniqueKey(entity)
- local progress = Entity_GetBuildingProgress(entity)
- local destroy = true
- local health = Entity_GetHealthPercentage(entity)
- if Entity_GetBlueprint(entity) == g_ebp_west_german_wall then
- if progress == 1 then
- local newEntity = Entity_CreateAndSpawnToward(g_ebp_west_german_wall_upgraded, player, pos, towardsPos)
- Entity_SetHealth(newEntity, health)
- if Entity_HasUpgrade(entity, g_upgrade_wall_upgraded) then
- Cmd_InstantUpgrade(Entity_GetTempEGroup(entity), g_upgrade_wall_upgraded)
- end
- else
- destroy = false
- 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}
- end
- elseif Entity_GetBlueprint(entity) == g_ebp_west_german_wall_gate then
- local newEntity = Entity_CreateAndSpawnToward(g_ebp_west_german_wall_upgraded_gate, player, pos, towardsPos)
- Entity_SetHealth(newEntity, health)
- local newKey = Entity_GetUniqueKey(newEntity)
- local oldGate = g_gates[oldKey]
- if oldGate.isOpen then
- g_skip_gate_open[newKey] = true
- Cmd_Ability(Entity_GetTempEGroup(newEntity), g_ability_open_gate)
- end
- g_gates[newKey] = oldGate
- g_gates[oldKey] = nil
- end
- if destroy then
- Entity_Destroy(entity)
- end
- end)
- elseif level == 2 then
- end
- g_player_wall_level[playerKey].level = level
- end
- function Player_GetTownCenter(player, faction)
- faction = faction or Player_GetRaceName(player)
- local towncenter_ebp = {
- ["german"] = {ebp = nil},
- ["soviet"] = {ebp = nil},
- ["west_german"] = {ebp = Villagers_GetEntityBlueprint("west_german_hq_villagers_mp")},
- ["aef"] = {ebp = nil},
- }
- if towncenter_ebp[faction] and towncenter_ebp[faction].ebp then
- Msg("checking with " .. faction)
- local filter = towncenter_ebp[faction].ebp
- local egroup = Player_GetEntities(player)
- Msg("#1 count: " .. EGroup_CountSpawned(egroup))
- EGroup_Filter(egroup, filter, FILTER_KEEP)
- Msg("#2 count: " .. EGroup_CountSpawned(egroup))
- if not EGroup_IsEmpty(egroup) then
- Msg("returning " .. Entity_GetName(EGroup_GetSpawnedEntityAt(egroup, 1)))
- return EGroup_GetSpawnedEntityAt(egroup, 1)
- end
- end
- end
- function Villagers_EventTooManyVillagers(pos)
- UI_CreateMinimapBlip(pos, 2, BT_General)
- end
- function Villagers_EventFarmFull(farmEntity)
- UI_CreateMinimapBlip(Entity_GetPosition(farmEntity), 2, BT_General)
- EventCue_Create(CUE.VILLAGER_IDLE, Util_CreateLocString("Farm maximum Villager count reached!"), nil, Entity_GetTempEGroup(farmEntity))
- end
- function Villagers_GetRandomFoodEBP()
- return g_ebp_bush_food
- end
- function Villagers_GetRandomWoodEBP()
- local wood_ebps = {
- {ebp = Villagers_GetEntityBlueprint("wood_pine_01"), weight = 10},
- {ebp = Villagers_GetEntityBlueprint("wood_pine_02"), weight = 10},
- {ebp = Villagers_GetEntityBlueprint("wood_pine_03"), weight = 15},
- {ebp = Villagers_GetEntityBlueprint("wood_pine_04"), weight = 15},
- {ebp = Villagers_GetEntityBlueprint("wood_pine_05"), weight = 15},
- {ebp = Villagers_GetEntityBlueprint("wood_pine_06"), weight = 15},
- {ebp = Villagers_GetEntityBlueprint("wood_pine_07"), weight = 5},
- {ebp = Villagers_GetEntityBlueprint("wood_pine_08"), weight = 3},
- }
- local result = {}
- for key, value in ipairs(wood_ebps) do
- for i = 1, value.weight do
- table.insert(result, value)
- end
- end
- return Table_GetRandomItem(result).ebp
- end
- function Villagers_GetRandomGoldEBP()
- return g_ebp_gold_mine
- end
- function Villagers_PopulatePoint(pos, resource, amount)
- Repeat(amount, function()
- local spawnPos = Util_GetRandomPosition(pos, 20)
- local towardsPos = Util_GetRandomPosition(pos, 20)
- local entity = Entity_CreateAndSpawnToward(resource.ebp(), nil, spawnPos, towardsPos)
- if resource.autoalign then
- Entity_AutoAlign(entity)
- end
- end)
- end
- function Villagers_ResourceGatherSystem()
- for key, item in pairs(g_resource_gather) do
- if Squad_IsValid(item.villager) and Entity_IsValid(item.target) and Entity_GetHealth(Entity_FromWorldID(item.target)) > 0 then
- local villager = Squad_FromWorldID(item.villager)
- local entity = Entity_FromWorldID(item.target)
- item.gather_ticks = item.gather_ticks + 1
- if (item.gather_ticks == 7) then
- item.gather_ticks = 0
- local percentage = 1
- if not Entity_GetInvulnerable(entity) then
- percentage = Entity_AddHealth(entity, -1)
- end
- Entity_GatherResource(item.caster, entity)
- if percentage == 0 then
- local pos = Entity_GetPosition(entity)
- local villagers = Villagers_GetVillagersGatheringResource(item.target)
- for key2, _item in ipairs(villagers) do
- Msg("iterating over villager" .. _item.villager)
- if Squad_IsValid(_item.villager) then
- local _villager = Squad_FromWorldID(_item.villager)
- local sg_villager = Squad_GetTempSGroup(_villager)
- Villagers_EndResourceGathering(_villager, entity)
- Villager_FindNewResources(_villager, entity)
- end
- end
- Entity_Kill(entity)
- local delete = Villagers_DeleteResourceEntity(entity)
- end
- end
- else
- if not Entity_IsValid(item.target) or (Entity_IsValid(item.target) and Entity_GetHealth(Entity_FromWorldID(item.target)) == 0) then
- Msg("checking.....")
- local villagers = Villagers_GetVillagersGatheringResource(item.target)
- for key2, villager in pairs(villagers) do
- if Squad_IsValid(villager.villager) then
- local squad = Squad_FromWorldID(villager.villager)
- Villagers_EndResourceGathering(squad, nil)
- Villager_FindNewResources(squad, villager.rt, Squad_GetPosition(squad))
- Msg("end resource via dead")
- end
- end
- end
- end
- end
- for key, entity in pairs(g_delete_resource_entity) do
- local delete = false
- if Entity_IsValid(entity.id) then
- entity.when = entity.when - 1
- if entity.when == 0 then
- local _entity = Entity_FromWorldID(entity.id)
- Entity_Destroy(_entity)
- delete = true
- end
- else
- delete = true
- end
- if delete then
- g_delete_resource_entity[key] = nil
- end
- end
- for key, ability in pairs(g_villager_ability_call) do
- local delete = false
- if Squad_IsValid(ability.squadId) and Entity_IsValid(ability.target) then
- ability.when = ability.when - 1
- if ability.when == 0 then
- local squad = Squad_FromWorldID(ability.squadId)
- local target = Entity_GetTempEGroup(Entity_FromWorldID(ability.target))
- Command_SquadEntityAbility(Squad_GetPlayerOwner(squad), Squad_GetTempSGroup(squad), target, ability.ability, true, false)
- delete = true
- end
- else
- delete = true
- end
- if delete then
- g_villager_ability_call[key] = nil
- end
- end
- for key, building in pairs(g_villager_house_simulation) do
- local delete = false
- if Entity_IsValid(building.id) then
- local entity = Entity_FromWorldID(building.id)
- local progress = Entity_GetBuildingProgress(entity)
- if progress > 0 then
- local pos = Entity_GetPosition(entity)
- local buildingHeight = 3.5
- if not building.y then
- building.y = pos.y - buildingHeight
- end
- pos.y = building.y + (progress * buildingHeight)
- Entity_SetPosition(entity, pos)
- if progress == 1 then
- delete = true
- end
- end
- else
- delete = true
- end
- if delete then
- g_villager_house_simulation[key] = nil
- end
- end
- for key, farm in pairs(g_villager_farm) do
- local rectangle = farm.rectangle
- if Entity_IsValid(farm.entityId) then
- local farmEntity = Entity_FromWorldID(farm.entityId)
- local delete = false
- for key, extraVillager in pairs(farm.extra_villagers) do
- if Squad_IsValid(extraVillager.squadId) then
- local squad = Squad_FromWorldID(extraVillager.squadId)
- local squadPos = Squad_GetPosition(squad)
- local inside = PointInPolygon(rectangle, squadPos.x, squadPos.z)
- if not inside then
- delete = true
- end
- else
- delete = true
- end
- if delete then
- farm.extra_villagers[key] = nil
- end
- end
- for key, slot in ipairs(farm.slots) do
- local free_slot = false
- if Squad_IsValid(slot.squadId) then
- local squad = Squad_FromWorldID(slot.squadId)
- local squadPos = Squad_GetPosition(squad)
- local inside = PointInPolygon(rectangle, squadPos.x, squadPos.z)
- if inside then
- --Msg("!![" .. World_GetGameTime() .. "] Squad " .. Squad_GetUniqueKey(squad) .. " is in range of the farm!")
- end
- if not inside then
- free_slot = true
- end
- else
- free_slot = true
- end
- if free_slot and slot.occupied then
- slot.occupied = false
- slot.squadId = 0
- Msg("Slot " .. key .. " is now free.")
- farm.villagerCount = farm.villagerCount - 1
- end
- end
- local sg_villagers = Team_GetSquadsNearPoint(farm.team, farm.pos, 15)
- SGroup_Filter(sg_villagers, g_sbp_west_german_villager, FILTER_KEEP)
- SGroup_ForEach(sg_villagers, function(sgid, idx, squad)
- local squadPos = Squad_GetPosition(squad)
- local inside = PointInPolygon(rectangle, squadPos.x, squadPos.z)
- if inside then
- --Msg("[" .. World_GetGameTime() .. "] Squad " .. Squad_GetUniqueKey(squad) .. " is in range of the farm!")
- end
- if inside and not Villagers_SquadIsInFarm(farm, squad) then
- local slot = Villagers_Farm_GetAvailableSlot(farm, squad)
- local sg_villager = Squad_GetTempSGroup(squad)
- local squadKey = Squad_GetUniqueKey(squad)
- if slot then
- Cmd_Move(sg_villager, Entity_GetPosition(slot))
- local ability = nil
- if farm.type == FARMTYPE.FOOD then
- ability = g_ability_gather_food
- elseif farm.type == FARMTYPE.GOLD then
- ability = g_ability_gather_gold
- end
- g_villager_ability_call[squadKey] = {squadId = Squad_GetGameID(squad), ability = ability, target = Entity_GetGameID(slot), when = 8}
- Msg("squad " .. squadKey .. " is now working for the farm! (" .. BP_GetName(ability) .. ")")
- else
- local nextAvailableFarm = Villagers_FindAvailableFarm(farm)
- if nextAvailableFarm then
- Cmd_Move(sg_villager, Entity_GetPosition(nextAvailableFarm))
- elseif not farm.extra_villagers[squadKey] then
- farm.extra_villagers[squadKey] = {squadId = Squad_GetGameID(squad)}
- Villagers_EventFarmFull(farmEntity)
- end
- end
- end
- end)
- end
- end
- for key, task in ipairs(g_delayed_task) do
- task.when = task.when - 1
- if task.when == 0 then
- task.f()
- table.remove(g_delayed_task, key)
- end
- end
- for key, wall in pairs(g_upgrade_wall_once_finished) do
- local delete = false
- if Entity_IsValid(wall.entityId) then
- local entity = Entity_FromWorldID(wall.entityId)
- if Entity_GetBuildingProgress(entity) == 1 then
- Entity_Destroy(entity)
- local newEntity = Entity_CreateAndSpawnToward(wall.ebp, wall.owner, wall.pos, wall.towardsPos)
- delete = true
- end
- else
- delete = true
- end
- if delete then
- g_upgrade_wall_once_finished[key] = nil
- end
- end
- end
- function Villagers_OpenGate(caster, target)
- local key = Entity_GetUniqueKey(caster)
- if g_skip_gate_open[key] then
- g_skip_gate_open[key] = nil
- elseif g_gates[key] then
- g_gates[key].isOpen = true
- Msg("OpenGate: " .. scartype_tostring(caster) .. " and " .. scartype_tostring(target))
- local gate = g_gates[key]
- local health = Entity_GetHealthPercentage(caster)
- local bp = Entity_GetBlueprint(caster)
- Entity_Destroy(caster)
- local newGate = Entity_CreateAndSpawnToward(bp, gate.owner, gate.open.pos, gate.open.towards)
- Entity_SetHealth(newGate, health)
- local newKey = Entity_GetUniqueKey(newGate)
- g_gates[newKey] = gate
- g_gates[key] = nil
- g_skip_gate_open[newKey] = true
- Cmd_Ability(Entity_GetTempEGroup(newGate), g_ability_open_gate)
- Misc_SelectEntity(newGate)
- end
- end
- function Villagers_CloseGate(caster, target)
- Msg("CloseGate: " .. scartype_tostring(caster) .. " and " .. scartype_tostring(target))
- local key = Entity_GetUniqueKey(caster)
- if g_gates[key] then
- g_gates[key].isOpen = false
- local gate = g_gates[key]
- local health = Entity_GetHealthPercentage(caster)
- local bp = Entity_GetBlueprint(caster)
- Entity_Destroy(caster)
- local newGate = Entity_CreateAndSpawnToward(bp, gate.owner, gate.closed.pos, gate.closed.towards)
- Entity_SetHealth(newGate, health)
- local newKey = Entity_GetUniqueKey(newGate)
- g_gates[newKey] = gate
- g_gates[key] = nil
- Misc_SelectEntity(newGate)
- end
- end
- function Villagers_WallGateUpgradeComplete(caster, target)
- Msg(Entity_GetUniqueKey(caster) .. " and " .. Entity_GetUniqueKey(target))
- local pos = Entity_GetPosition(caster)
- local heading = Entity_GetHeading(caster)
- local owner = Entity_GetPlayerOwner(caster)
- local playerKey = Player_GetUniqueKey(owner)
- local facingPos = GetDirectionalOffset(pos, heading, 3, 0)
- local health = Entity_GetHealthPercentage(caster)
- Entity_Destroy(caster)
- local bp = nil
- if g_player_wall_level[playerKey].level == 0 then
- bp = g_ebp_west_german_wall_gate
- else
- bp = g_ebp_west_german_wall_upgraded_gate
- end
- local gate = Entity_CreateAndSpawnToward(bp, owner, pos, facingPos)
- Entity_SetHealth(gate, health)
- Villagers_RegisterGate(gate, false)
- Misc_SelectEntity(gate)
- local pos_visual_gate_left = GetDirectionalOffset(pos, heading, 4, 90)
- local pos_visual_gate_right = GetDirectionalOffset(pos, heading, 4, -90)
- local towardsPos_left = GetDirectionalOffset(pos, heading, 5.7, 135)
- local towardsPos_right = GetDirectionalOffset(pos, heading, 5.7, -135)
- local visual_gate_left = Entity_CreateAndSpawnToward(g_ebp_wall_gate_left, owner, pos_visual_gate_left, towardsPos_left)
- local visual_gate_right = Entity_CreateAndSpawnToward(g_ebp_wall_gate_right, owner, pos_visual_gate_right, towardsPos_right)
- --dr_drawline(Pos_AddHeight(pos_visual_gate_left, 5), Pos_AddHeight(towardsPos_left, 5), 255, 255, 255, "view" )
- --dr_drawline(Pos_AddHeight(pos_visual_gate_right, 5), Pos_AddHeight(towardsPos_right, 5), 255, 0, 0, "view" )
- --Entity_SetInvulnerable(visual_gate_left, true, -1)
- --Entity_SetInvulnerable(visual_gate_right, true, -1)
- end
- function Villagers_RegisterGate(entity, isOpen)
- local id = Entity_GetGameID(entity)
- local key = Entity_GetUniqueKey(entity)
- local owner = Entity_GetPlayerOwner(entity)
- local pos = Entity_GetPosition(entity)
- local heading = Entity_GetHeading(entity)
- local towards_closed = GetDirectionalOffset(pos, heading, 3, 0)
- local pos_open = GetDirectionalOffset(pos, heading, 4, 292.5)
- local towards_open = GetDirectionalOffset(pos, heading, 3, 60)
- local heading_closed = Heading_Rotate(heading, 0)
- local heading_open = Heading_Rotate(heading, 90)
- 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}}
- end
- function Villagers_PlayerWallUpgradeCompleted(caster, target)
- Player_UpgradeWalls(caster, 1)
- end
- function Villagers_PlantationCompleted(caster, target)
- local key = Entity_GetUniqueKey(caster)
- Rule_AddEntityEvent(Villagers_FarmDestroyed, caster, GE_EntityKilled)
- local pos = Entity_GetPosition(caster)
- local heading = Entity_GetHeading(caster)
- local heading_2 = Heading_Rotate(heading, World_GetRand(0, 360))
- Entity_SetHeading(caster, heading_2, false)
- local owner = Entity_GetPlayerOwner(caster)
- local plantation = {
- type = FARMTYPE.GOLD,
- slots = {},
- owner = owner,
- entityId = Entity_GetGameID(caster),
- pos = pos, team = Player_GetTeam(owner),
- villagerCount = 0,
- extra_villagers = {},
- extra_entities = {},
- diagonalDistance = 14,
- }
- local bushes = {
- {pos = Util_GetOffsetPosition(pos, OFFSET_LEFT, 4), ebp = Villagers_GetEntityBlueprint("plantation_bush_main_01")},
- {pos = Util_GetOffsetPosition(pos, OFFSET_RIGHT, 4), ebp = Villagers_GetEntityBlueprint("plantation_bush_main_02")},
- }
- local pos_shed_01 = GetDirectionalOffset(pos, heading, 12, 60)
- local pos_shed_02 = GetDirectionalOffset(pos, heading, 10, 110)
- local towardsPos_shed_02 = GetDirectionalOffset(pos, heading, 4, 180)
- local shed_01 = Entity_CreateAndSpawnToward(Villagers_GetEntityBlueprint("plantation_shed_01"), owner, pos_shed_01, pos_shed_01)
- local shed_02 = Entity_CreateAndSpawnToward(Villagers_GetEntityBlueprint("plantation_shed_02"), owner, pos_shed_02, towardsPos_shed_02)
- local pos_barrel_01 = GetDirectionalOffset(pos, heading, 6, 90)
- local pos_barrel_02 = GetDirectionalOffset(pos, heading, 6, -90)
- local ebp_barrel = BP_GetEntityBlueprint("wood_dark_m_03")
- local barrel_01 = Entity_CreateAndSpawnToward(ebp_barrel, nil, pos_barrel_01, Util_GetRandomPosition(pos, 10))
- local barrel_02 = Entity_CreateAndSpawnToward(ebp_barrel, nil, pos_barrel_02, Util_GetRandomPosition(pos, 10))
- table.insert(plantation.extra_entities, Entity_GetGameID(shed_01))
- table.insert(plantation.extra_entities, Entity_GetGameID(shed_02))
- table.insert(plantation.extra_entities, Entity_GetGameID(barrel_01))
- table.insert(plantation.extra_entities, Entity_GetGameID(barrel_02))
- local function _newFarmSlot(entity)
- return {entity = entity, occupied = false, squadId = 0}
- end
- local bush_ebp = {Villagers_GetEntityBlueprint("plantation_bush_01"), Villagers_GetEntityBlueprint("plantation_bush_02"), Villagers_GetEntityBlueprint("plantation_bush_03"), Villagers_GetEntityBlueprint("plantation_bush_04")}
- for key, bush in ipairs(bushes) do
- local headingPos = Util_GetOffsetPosition(bush.pos, OFFSET_FRONT, 6)
- --headingPos = Util_GetRandomPosition(headingPos, 3)
- local entity = Entity_CreateAndSpawnToward(bush.ebp, nil, bush.pos, pos)
- table.insert(plantation.slots, _newFarmSlot(entity))
- local spawnPos = headingPos
- headingPos = Util_GetOffsetPosition(headingPos, OFFSET_RIGHT, 2)
- local bush_front_01 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
- spawnPos = headingPos
- headingPos = Util_GetOffsetPosition(headingPos, OFFSET_LEFT, 2)
- local bush_front_02 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
- spawnPos = Util_GetOffsetPosition(bush.pos, OFFSET_BACK, 6)
- headingPos = Util_GetOffsetPosition(spawnPos, OFFSET_RIGHT, 2)
- local bush_back_01 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
- spawnPos = headingPos
- headingPos = Util_GetOffsetPosition(headingPos, OFFSET_LEFT, 2)
- local bush_back_02 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
- table.insert(plantation.slots, _newFarmSlot(bush_front_01))
- table.insert(plantation.slots, _newFarmSlot(bush_front_02))
- table.insert(plantation.slots, _newFarmSlot(bush_back_01))
- table.insert(plantation.slots, _newFarmSlot(bush_back_02))
- end
- plantation.rectangle = Villagers_GetFarmRectangle(plantation)
- g_villager_farm[key] = plantation
- end
- function Villagers_FarmCompleted(caster, target)
- local key = Entity_GetUniqueKey(caster)
- Rule_AddEntityEvent(Villagers_FarmDestroyed, caster, GE_EntityKilled)
- local pos = Entity_GetPosition(caster)
- local heading = Entity_GetHeading(caster)
- local pos_crates = GetDirectionalOffset(pos, heading, 3, 0)
- local crates = Entity_CreateAndSpawnToward(g_ebp_farm_crates, nil, pos_crates, Util_GetRandomPosition(pos, 10))
- local owner = Entity_GetPlayerOwner(caster)
- local farm = {
- type = FARMTYPE.FOOD,
- slots = {},
- owner = owner,
- entityId = Entity_GetGameID(caster),
- pos = pos,
- team = Player_GetTeam(owner),
- villagerCount = 0,
- extra_villagers = {},
- extra_entities = {Entity_GetGameID(crates)},
- diagonalDistance = 12,
- }
- local bushes = {
- {pos = Util_GetOffsetPosition(pos, OFFSET_LEFT, 4), ebp = Villagers_GetEntityBlueprint("farm_bush_01")},
- {pos = Util_GetOffsetPosition(pos, OFFSET_RIGHT, 4), ebp = Villagers_GetEntityBlueprint("farm_bush_02")},
- }
- local function _newFarmSlot(entity)
- return {entity = entity, occupied = false, squadId = 0}
- end
- local bush_ebp = {Villagers_GetEntityBlueprint("farm_bush_regular_01"), Villagers_GetEntityBlueprint("farm_bush_regular_02"), Villagers_GetEntityBlueprint("farm_bush_regular_03")}
- for key, bush in ipairs(bushes) do
- local headingPos = Util_GetOffsetPosition(bush.pos, OFFSET_FRONT, 6)
- --headingPos = Util_GetRandomPosition(headingPos, 3)
- local entity = Entity_CreateAndSpawnToward(bush.ebp, nil, bush.pos, pos)
- table.insert(farm.slots, _newFarmSlot(entity))
- local spawnPos = headingPos
- headingPos = Util_GetOffsetPosition(headingPos, OFFSET_RIGHT, 2)
- local bush_front_01 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
- spawnPos = headingPos
- headingPos = Util_GetOffsetPosition(headingPos, OFFSET_LEFT, 2)
- local bush_front_02 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
- spawnPos = Util_GetOffsetPosition(bush.pos, OFFSET_BACK, 6)
- headingPos = Util_GetOffsetPosition(spawnPos, OFFSET_RIGHT, 2)
- local bush_back_01 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
- spawnPos = headingPos
- headingPos = Util_GetOffsetPosition(headingPos, OFFSET_LEFT, 2)
- local bush_back_02 = Entity_CreateAndSpawnToward(Table_GetRandomItem(bush_ebp), nil, spawnPos, Util_GetRandomPosition(headingPos, 1))
- table.insert(farm.slots, _newFarmSlot(bush_front_01))
- table.insert(farm.slots, _newFarmSlot(bush_front_02))
- table.insert(farm.slots, _newFarmSlot(bush_back_01))
- table.insert(farm.slots, _newFarmSlot(bush_back_02))
- end
- for key, slot in ipairs(farm.slots) do
- Entity_SetInvulnerable(slot.entity, true, -1)
- end
- farm.rectangle = Villagers_GetFarmRectangle(farm)
- g_villager_farm[key] = farm
- end
- function Villagers_FarmUpgrade01Completed(caster, target)
- end
- function Villagers_FarmUpgrade02Completed(caster, target)
- end
- function Villagers_OutpostUpgrade01Completed(caster, target)
- --Msg(scartype_tostring(caster) .. " and " .. scartype_tostring(target))
- Player_UpgradeOutposts(caster, 1)
- end
- function Villagers_OutpostUpgrade02Completed(caster, target)
- Player_UpgradeOutposts(caster, 2)
- end
- function Villagers_TownCenterSpawned(caster, target)
- --Entity_SetInvulnerableMinCap(caster, 0.2, -1)
- --Msg("set towncenter invulnerable cap")
- end
- function Villagers_ChurchCompleted(caster, target)
- end
- function Villagers_OutpostCompleted(caster, target)
- local pos = Entity_GetPosition(caster)
- local owner = Entity_GetPlayerOwner(caster)
- local playerKey = Player_GetUniqueKey(owner)
- local sbp = g_sbp_hidden_sniper
- local sniper = Squad_CreateAndSpawnToward(sbp, owner, SQUAD_SIZE_DEFAULT, pos, pos)
- Cmd_Garrison(Squad_GetTempSGroup(sniper), Entity_GetTempEGroup(caster), true, false, true)
- local outpostLevel = g_player_outpost_level[playerKey].level
- if outpostLevel == 1 then
- Cmd_InstantUpgrade(Squad_GetTempSGroup(sniper), g_upgrade_outpost_cannon)
- elseif outpostLevel == 2 then
- end
- end
- function Villagers_WallCompleted(caster, target)
- Rule_AddEntityEvent(Villagers_WallDestroyed, caster, GE_EntityKilled)
- end
- function Villagers_WallDestroyed(entity, killer)
- local key = Entity_GetUniqueKey(entity)
- g_gates[key] = nil
- end
- function Villagers_PlantationDestroyed(entity, killer)
- end
- function Villagers_FarmDestroyed(entity, killer)
- Villagers_DeleteResourceEntity(entity, 8)
- local key = Entity_GetUniqueKey(entity)
- local farm = g_villager_farm[key]
- if farm then
- for key, slot in ipairs(farm.slots) do
- Entity_Kill(slot.entity)
- Villagers_DeleteResourceEntity(slot.entity, World_GetRand(6, 14))
- end
- for key, value in ipairs(farm.extra_entities) do
- if Entity_IsValid(value) then
- local entity = Entity_FromWorldID(value)
- Entity_Kill(entity)
- Villagers_DeleteResourceEntity(entity, World_GetRand(6, 14))
- end
- end
- g_villager_farm[key] = nil
- end
- end
- function Villagers_BeginResourceGathering(caster, target)
- caster = Entity_CheckForParentSquad(caster)
- --Msg(scartype_tostring(caster) .. "started gathering stuff from " .. scartype_tostring(target))
- local key = Squad_GetUniqueKey(caster)
- local villagers = Villagers_GetVillagersGatheringResource(Entity_GetGameID(target))
- if table.getn(villagers) < g_configuration.max_villagers_per_resource then
- g_resource_gather[key] = {villager = Squad_GetGameID(caster), target = Entity_GetGameID(target), gather_ticks = 0, caster = Squad_GetPlayerOwner(caster), rt = Villagers_GetEntityResourceType(target)}
- else
- Cmd_Stop(Squad_GetTempSGroup(caster))
- Villager_FindNewResources(caster, target)
- EventCue_Create(CUE.VILLAGER_IDLE, Util_CreateLocString("No room for additional Villagers"), nil, Entity_GetTempEGroup(target))
- end
- end
- function Villagers_EndResourceGathering(caster, target)
- caster = Entity_CheckForParentSquad(caster)
- Msg(scartype_tostring(caster) .. "stopped gathering stuff from " .. scartype_tostring(target))
- local key = Squad_GetUniqueKey(caster)
- g_resource_gather[key] = nil
- end
- function Villagers_VillagerSpawned(caster)
- caster = Entity_CheckForParentSquad(caster)
- Rule_AddSquadEvent(Villagers_VillagerKilled, caster, GE_SquadKilled)
- Msg("Villager spawned!")
- end
- function Villagers_HouseStarted(caster, target)
- Rule_AddEntityEvent(Villagers_HouseDestroyed, caster, GE_EntityKilled)
- local key = Entity_GetUniqueKey(caster)
- g_villager_house_simulation[key] = {id = Entity_GetGameID(caster)}
- Villagers_PlayerHouseCounter_Add(Entity_GetPlayerOwner(caster), 1)
- end
- function Villagers_GenericBuildingStarted(caster, target)
- Rule_AddEntityEvent(Villagers_GenericBuildingDestroyed, caster, GE_EntityKilled)
- end
- function Villagers_GenericBuildingDestroyed(entity, killer)
- Villagers_DeleteResourceEntity(entity)
- end
- function Villagers_HouseCompleted(caster, target)
- local owner = Entity_GetPlayerOwner(caster)
- Player_AddPopulation(owner, g_configuration.population_per_house)
- end
- function Villagers_VillagerKilled(squad, killer)
- local key = Squad_GetUniqueKey(squad)
- g_resource_gather[key] = nil
- Msg("removed resource gather from dead villager")
- end
- function Villagers_HouseDestroyed(entity, killer)
- local key = Entity_GetUniqueKey(entity)
- g_villager_house_simulation[key] = nil
- if Entity_GetBuildingProgress(entity) == 1 then
- local owner = Entity_GetPlayerOwner(entity)
- Player_AddPopulation(owner, 0 - g_configuration.population_per_house)
- end
- --Entity_Destroy(entity)
- Villagers_DeleteResourceEntity(entity)
- Villagers_PlayerHouseCounter_Add(Entity_GetPlayerOwner(entity), -1)
- end
- function Villagers_PlayerHouseCounter_Add(player, amount)
- local key = Player_GetUniqueKey(player)
- local counter = g_player_house_counter[key]
- counter.count = counter.count + amount
- Msg("House count: " .. counter.count)
- if counter.count >= g_configuration.max_houses_per_player and not counter.max then
- counter.max = true
- Msg("adding max house count upgrade")
- Player_CompleteUpgrade(player, g_upgrade_max_houses_reached)
- elseif counter.count < g_configuration.max_houses_per_player and counter.max then
- counter.max = false
- Player_RemoveUpgrade(player, g_upgrade_max_houses_reached)
- Msg("removing max house count upgrade")
- end
- end
- --[[
- FRONT_LEFT FRONT FRONT_RIGHT
- /\
- / \
- LEFT /_ _\ RIGHT
- ||
- ||
- ||
- BACK_LEFT BACK BACK_RIGHT
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement