Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Player_IsLocalPlayer(player)
- local pid = Player_GetID(player)
- if pid == Game_GetLocalPlayerID() then
- return true
- end
- return false
- end
- function Player_GetUniqueKey(player)
- return "player_" .. Player_GetID(player) .. "_" .. Player_GetName(player)
- end
- function Player_GetName(player)
- return Player_GetDisplayName(player)[1]
- end
- function Players_ForEach(f)
- for i = 1, World_GetPlayerCount() do
- local player = World_GetPlayerAt(i)
- local pid = Player_GetID(player)
- f(pid, i, player)
- end
- end
- function Players_ForEachInTeam(team, f)
- Players_ForEach(function(pid, idx, player)
- if Player_GetTeam(player) == team then
- f(pid, idx, player)
- end
- end)
- end
- function Player_AddPopulation(player, amount)
- local population = Player_GetMaxPopulation(player, CT_Personnel)
- Player_SetPopCapOverride(player, population + amount)
- end
- function Team_GetEntitiesNearPoint(team, pos, radius)
- radius = radius or 15
- local eg_villagers = EGroup_CreateIfNotFound("eg_villagers_near_farm")
- local eg_villagers_temp = EGroup_CreateIfNotFound("eg_villagers_temp")
- EGroup_Clear(eg_villagers)
- Players_ForEachInTeam(team, function(pid, idx, player)
- EGroup_Clear(eg_villagers_temp)
- World_GetEntitiesNearPoint(player, eg_villagers_temp, pos, radius, OT_Player)
- EGroup_AddGroup(eg_villagers, eg_villagers_temp)
- end)
- return eg_villagers
- end
- function Team_GetSquadsNearPoint(team, pos, radius)
- radius = radius or 15
- local sg_villagers = SGroup_CreateIfNotFound("sg_villagers_near_farm")
- local sg_villagers_temp = SGroup_CreateIfNotFound("sg_villagers_temp")
- SGroup_Clear(sg_villagers)
- Players_ForEachInTeam(team, function(pid, idx, player)
- SGroup_Clear(sg_villagers_temp)
- World_GetSquadsNearPoint(player, sg_villagers_temp, pos, radius, OT_Player)
- SGroup_AddGroup(sg_villagers, sg_villagers_temp)
- end)
- return sg_villagers
- end
- function EGroup_GetClosest(pos, egroup)
- local entities = {}
- EGroup_ForEach(egroup, function(egid, idx, entity)
- table.insert(entities, entity)
- end)
- return World_GetClosest(pos, entities)
- end
- function EGroup_AddGroup(egroup, grouptoadd)
- EGroup_ForEach(grouptoadd, function(egid, idx, entity)
- EGroup_Add(egroup, entity)
- end)
- return egroup
- end
- function Entity_GetUniqueKey(entity)
- return "entity_" .. Entity_GetGameID(entity)
- end
- function Entity_GetGarrisonedSquads(entity)
- local sgroup = SGroup_CreateIfNotFound("sg_villagers_squads_held")
- SGroup_Clear(sgroup)
- Entity_GetSquadsHeld(entity, sgroup)
- return sgroup
- end
- function Entity_AddHealth(entity, health)
- local max_health = Entity_GetHealthMax(entity)
- local current_health = Entity_GetHealth(entity)
- local new_health = current_health + health
- if (new_health > max_health) then
- new_health = max_health
- elseif (new_health < 0) then
- new_health = 0
- end
- local percentage = new_health / max_health
- Entity_SetHealth(entity, percentage)
- return percentage
- end
- function Entity_AutoAlign(entity)
- Entity_SetHeading(entity, Entity_GetHeading(entity), false)
- end
- function Entity_CheckForParentSquad(target)
- if scartype(target) == ST_ENTITY then
- if Entity_GetSquad(target) then
- return Entity_GetSquad(target)
- else
- return target
- end
- else
- return target
- end
- end
- function Entity_CreateAndSpawnToward(ebp, player, pos, toward, force_construct)
- force_construct = force_construct or true
- local neutral = false
- if not player then
- neutral = true
- player = World_GetPlayerAt(1)
- end
- local entity entity = Entity_Create(ebp, player, pos, toward)
- if neutral then
- Entity_SetWorldOwned(entity)
- end
- Entity_Spawn(entity)
- if force_construct then
- Entity_ForceConstruct(entity)
- end
- return entity
- end
- function Entity_GatherResource(caster, entity, amount)
- if not amount then
- amount = 1
- end
- local rt = Villagers_GetEntityResourceType(entity)
- if Entity_IsOfType(entity, "salvageable_object") then
- if rt == RT_FOOD then -- FOOD
- Player_AddResource(caster, RT_Manpower, amount)
- return RT_Manpower
- elseif rt == RT_GOLD then -- GOLD
- Player_AddResource(caster, RT_Munition, amount)
- return RT_Munition
- elseif RT_WOOD then -- WOOD
- Player_AddResource(caster, RT_Fuel, amount)
- return RT_Fuel
- end
- end
- end
- function Entity_GetName(entity)
- local bp = Entity_GetBlueprint(entity)
- return Util_GetBPName(bp)
- end
- function Entity_GetTempEGroup(entity, index)
- if not index then
- index = 1
- end
- local egroup = EGroup_CreateIfNotFound("eg_temp_villagers_" .. World_GetGameTime() .. index)
- EGroup_Clear(egroup)
- EGroup_Add(egroup, entity)
- return egroup
- end
- function Squad_GetUniqueKey(squad)
- return "squad_" .. Squad_GetGameID(squad)
- end
- function Squad_GetName(squad)
- local bp = Squad_GetBlueprint(squad)
- return Util_GetBPName(bp)
- end
- function Squad_GetTempSGroup(squad, index)
- if not index then
- index = 1
- end
- local sgroup = SGroup_CreateIfNotFound("sg_temp_villagers_" .. World_GetGameTime() .. index)
- SGroup_Clear(sgroup)
- SGroup_Add(sgroup, squad)
- return sgroup
- end
- function Util_GlobalMessage(title, displaytime)
- Game_TextTitleFade(title, 0, displaytime, 2)
- end
- function Util_GetBPName(bp)
- local name = BP_GetName(bp)
- return name:gsub(g_guid .. ":", "Villagers:")
- end
- function Util_CreateLocString(text)
- local tmpstr = LOC(text)
- tmpstr[1] = text
- return tmpstr
- end
- function Game_GetLocalPlayerID()
- return Player_GetID(Game_GetLocalPlayer())
- end
- function World_GetEntitiesByBlueprint(ebp)
- local result = {}
- if scartype(ebp) == ST_STRING then
- ebp = BP_GetEntityBlueprint(ebp)
- end
- for i = 0, World_GetNumEntities() -1 do
- local entity = World_GetEntity(i)
- if Entity_GetBlueprint(entity) == ebp then
- table.insert(result, entity)
- end
- end
- return result
- end
- function World_ForEachEntities(f)
- for i = 0, World_GetNumEntities() -1 do
- local entity = World_GetEntity(i)
- f(Entity_GetGameID(entity), i, entity)
- end
- end
- function World_ForEachEntitiesByBlueprint(ebp, f)
- for i = 0, World_GetNumEntities() -1 do
- local entity = World_GetEntity(i)
- if Entity_GetBlueprint(entity) == ebp then
- f(Entity_GetGameID(entity), i, entity)
- end
- end
- end
- function Msg(text)
- if g_enable_messages then
- g_text_line_count = g_text_line_count + 1
- if g_text_line_count == 100 then
- g_text = ""
- g_text_line_count = 0
- end
- g_text = text.."\n"..g_text
- dr_clear("VILLAGERS")
- dr_setautoclear("VILLAGERS", 0)
- dr_text2d("VILLAGERS", 0.615, 0.025, g_text, 0, 255, 0)
- end
- end
- function Villagers_GetIcon(icon)
- return "ModIcons_"..g_guid.."_"..icon
- end
- function Villagers_GetAbilityBlueprint(blueprint)
- return BP_GetAbilityBlueprint(g_guid .. ":" .. blueprint)
- end
- function Villagers_GetSquadBlueprint(blueprint)
- return BP_GetSquadBlueprint(g_guid .. ":" .. blueprint)
- end
- function Villagers_GetEntityBlueprint(blueprint)
- return BP_GetEntityBlueprint(g_guid .. ":" .. blueprint)
- end
- function Villagers_GetUpgradeBlueprint(blueprint)
- return BP_GetUpgradeBlueprint(g_guid .. ":" .. blueprint)
- end
- function Villagers_DeleteResourceEntity(entity, when)
- local key = Entity_GetUniqueKey(entity)
- local rt = Villagers_GetEntityResourceType(entity)
- if rt == RT_FOOD then -- FOOD
- g_delete_resource_entity[key] = {id = Entity_GetGameID(entity), when = when or 4}
- return true
- elseif rt == RT_GOLD then -- GOLD
- g_delete_resource_entity[key] = {id = Entity_GetGameID(entity), when = when or 4}
- return true
- elseif rt == RT_WOOD then -- WOOD
- return false
- else
- g_delete_resource_entity[key] = {id = Entity_GetGameID(entity), when = when or 24}
- return true
- end
- end
- function Villagers_GetVillagersGatheringResource(entityId)
- local result = {}
- for key, item in pairs(g_resource_gather) do
- if item.target == entityId then
- table.insert(result, item)
- end
- end
- return result
- end
- function Villagers_GetResourceTypeGatherAbility(rt)
- for key, resource in pairs(g_resources) do
- if resource.resource_type == rt then
- return resource.ability
- end
- end
- end
- function Villagers_GetEntityResourceType(entity)
- if Entity_IsOfType(entity, "salvageable_object") then
- if Entity_IsOfType(entity, "supply_accessory_1") then -- FOOD
- return RT_FOOD
- elseif Entity_IsOfType(entity, "supply_accessory_2") then -- GOLD
- return RT_GOLD
- elseif Entity_IsOfType(entity, "supply_accessory_3") then -- WOOD
- return RT_WOOD
- end
- end
- end
- function Villagers_GetResourceTypeEBPFilter(rt)
- for key, resource in pairs(g_resources) do
- if resource.resource_type == rt then
- return resource.ebp
- end
- end
- end
- function Villagers_FindResource(pos, rt, radius, entity)
- radius = radius or 25
- local eg_resources = EGroup_CreateIfNotFound("eg_villagers_resources")
- World_GetEntitiesNearPoint(player_generic, eg_resources, pos, radius, OT_Neutral)
- local ebpFilter = Villagers_GetResourceTypeEBPFilter(rt)
- EGroup_Filter(eg_resources, ebpFilter, FILTER_KEEP)
- if entity then
- EGroup_Remove(eg_resources, entity)
- end
- EGroup_ForEach(eg_resources, function(egid, idx, egEntity)
- local villagers = Villagers_GetVillagersGatheringResource(Entity_GetGameID(egEntity))
- if table.getn(villagers) >= g_max_villagers_per_resource then
- EGroup_Remove(eg_resources, egEntity)
- end
- end)
- if not EGroup_IsEmpty(eg_resources) then
- local entity = EGroup_GetClosest(pos, eg_resources)
- return entity
- end
- return nil
- end
- function Villager_FindNewResources(villager, oldEntity, pos)
- local rt
- if scartype(oldEntity) == ST_NUMBER then
- rt = oldEntity
- else
- rt = Villagers_GetEntityResourceType(oldEntity)
- pos = Entity_GetPosition(oldEntity)
- end
- local newResource = Villagers_FindResource(pos, rt, nil, nil)
- local sg_villager = Squad_GetTempSGroup(villager)
- if newResource then
- local eg_newResource = Entity_GetTempEGroup(newResource)
- local ability = Villagers_GetResourceTypeGatherAbility(rt)
- Cmd_Move(sg_villager, eg_newResource, true)
- g_villager_ability_call[Squad_GetUniqueKey(villager)] = {squadId = Squad_GetGameID(villager), ability = ability, target = Entity_GetGameID(newResource), when = 8}
- return true
- end
- EventCue_Create(CUE.VILLAGER_IDLE, Util_CreateLocString("Idle Villager"), nil, sg_villager)
- return false
- end
- function Villagers_GetFarmRectangle(farm)
- local pos = farm.pos
- local rectangle = {}
- local result = {}
- rectangle[0] = Util_GetOffsetPosition(pos, OFFSET_FRONT_LEFT, farm.diagonalDistance)
- rectangle[1] = Util_GetOffsetPosition(pos, OFFSET_FRONT_RIGHT, farm.diagonalDistance)
- rectangle[2] = Util_GetOffsetPosition(pos, OFFSET_BACK_RIGHT, farm.diagonalDistance)
- rectangle[3] = Util_GetOffsetPosition(pos, OFFSET_BACK_LEFT, farm.diagonalDistance)
- for key, value in pairs(rectangle) do
- Msg("inserted at " .. key)
- result[key] = {[0] = value.x, [1] = value.z}
- end
- local increaseY = 1
- for i = 0, 3 do
- rectangle[i].y = rectangle[i].y + increaseY
- end
- local _posString = function(pos)
- return "X: " .. (math.floor(pos.x * 100) / 100) .. ", Y: " .. (math.floor(pos.z * 100) / 100)
- end
- dr_drawline(rectangle[0], rectangle[1], 255, 255, 255, "view" )
- dr_drawline(rectangle[1], rectangle[2], 255, 255, 255, "view" )
- dr_drawline(rectangle[2], rectangle[3], 255, 255, 255, "view" )
- dr_drawline(rectangle[3], rectangle[0], 255, 255, 255, "view" )
- for i = 0, 3 do
- dr_text3dpos("view", rectangle[i], _posString(rectangle[i]), 255, 255, 255)
- end
- return result
- end
- function Villagers_SquadIsInFarm(farm, squad)
- local sid = Squad_GetGameID(squad)
- for key, slot in ipairs(farm.slots) do
- if slot.squadId == sid then
- return true
- end
- end
- return false
- end
- function Villagers_Farm_GetAvailableSlot(farm, squad)
- local sid = Squad_GetGameID(squad)
- for key, slot in ipairs(farm.slots) do
- if slot.squadId == sid then
- return slot.entity
- end
- end
- for key, slot in ipairs(farm.slots) do
- if slot.occupied == false then
- slot.occupied = true
- slot.squadId = sid
- farm.villagerCount = farm.villagerCount + 1
- Msg("slot " .. key .. " is now taken!")
- return slot.entity
- end
- end
- end
- function Villagers_FindAvailableFarm(farm)
- if Entity_IsValid(farm.entityId) then
- local farmEntity = Entity_FromWorldID(farm.entityId)
- local eg_farms = Team_GetEntitiesNearPoint(farm.team, farm.pos, 25)
- local result = nil
- EGroup_Remove(eg_farms, farmEntity)
- local filter = nil
- if farm.type == FARMTYPE.FOOD then
- filter = g_ebp_farm
- elseif farm.type == FARMTYPE.GOLD then
- filter = g_ebp_plantation
- end
- EGroup_Filter(eg_farms, filter, FILTER_KEEP)
- local eg_farms_not_full = EGroup_CreateIfNotFound("eg_farms_not_full")
- EGroup_Clear(eg_farms_not_full)
- for i = 1, EGroup_CountSpawned(eg_farms) do
- local entity = EGroup_GetSpawnedEntityAt(eg_farms, i)
- local farmKey = Entity_GetUniqueKey(entity)
- local _farm = g_villager_farm[farmKey]
- if _farm and _farm.villagerCount < g_configuration.max_villagers_per_farm then
- EGroup_Add(eg_farms_not_full, entity)
- end
- end
- return EGroup_GetClosest(farm.pos, eg_farms_not_full)
- end
- end
- function Pos_AddHeight(pos, height)
- return World_Pos(pos.x, pos.y + height, pos.z)
- end
- function Repeat(times, f)
- for i = 1, times do
- f()
- end
- end
- function Delay(delay, task)
- table.insert(g_delayed_task, {when = delay, f = task})
- end
- function dr_text3dpos(frame, pos, text, r, g, b)
- return dr_text3d(frame, pos.x, pos.y, pos.z, text, 255, 255, 255)
- end
- function PointInPolygon(polygon, x, y)
- --// ray-casting algorithm based on
- --// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
- --local x = point[0], y = point[1]
- local inside = false
- local length = table.getn(polygon)
- local j = length
- for i = 0, length do
- if i == 0 then
- j = length
- else
- j = i - 1
- end
- local xi, yi = polygon[i][0], polygon[i][1]
- local xj, yj = polygon[j][0], polygon[j][1]
- local intersect = ((yi > y) ~= (yj > y))
- and (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
- if (intersect) then inside = not inside end
- end
- return inside
- end
- function Heading_Rotate(heading, amount)
- local _heading = World_Pos(heading.x, heading.y, heading.z)
- local angle = math.atan2(_heading.z, _heading.x) + math.rad(amount)
- _heading.z = math.sin(angle)
- _heading.x = math.cos(angle)
- return _heading
- end
- function GetDirectionalOffset(pos, heading, offset, angle)
- angle = math.rad(angle)
- local alpha = math.atan2(heading.z, heading.x)
- local x = offset * math.cos(alpha + angle)
- local y = offset * math.sin(alpha + angle)
- Msg("Offsets: X:" .. x .. ", Y: " .. y)
- return World_Pos(pos.x + x, pos.y, pos.z + y)
- end
- function GetDirectionalOffsetPosition(pos, heading, offset, direction)
- --heading = heading or {x = 0, y = 0, z = 0}
- local angle = 0
- if direction == OFFSET_FRONT then
- angle = 0
- elseif direction == OFFSET_FRONT_RIGHT then
- angle = 45
- elseif direction == OFFSET_RIGHT then
- angle = 90
- elseif direction == OFFSET_BACK_RIGHT then
- angle = 135
- elseif direction == OFFSET_BACK then
- angle = 180
- elseif direction == OFFSET_BACK_LEFT then
- angle = 225
- elseif direction == OFFSET_LEFT then
- angle = 270
- elseif direction == OFFSET_FRONT_LEFT then
- angle = 315
- end
- Msg("using angle " .. angle)
- return GetDirectionalOffset(pos, heading, offset, angle)
- end
- --table.insert(g_delayed_task, {when = 8, f = function()
- --
- --end})
- --Msg("X: " .. math.deg(heading.x) .. ", Y: " .. math.deg(heading.y) .. ", Z: " .. math.deg(heading.z))
- --local angle = math.atan2(heading.z, heading.x)
- --Msg("Angle: " .. math.deg(angle))
- --Msg("New Heading Z: " .. math.deg(math.sin(angle)))
- --Msg("New Heading X: " .. math.deg(math.cos(angle)))
- --Msg("Gate Pos: X: " .. pos.x .. ", Y: " .. pos.y .. ", Z: " .. pos.z)
- --local pos1 = GetDirectionalOffsetPosition(pos, heading, 2, OFFSET_FRONT_RIGHT)
- --local pos2 = GetDirectionalOffsetPosition(pos, heading, 2, OFFSET_BACK_RIGHT)
- --local pos3 = GetDirectionalOffsetPosition(pos, heading, 2, OFFSET_BACK_LEFT)
- --local pos4 = GetDirectionalOffsetPosition(pos, heading, 2, OFFSET_FRONT_LEFT)
- --[[local pos1 = GetDirectionalOffset(pos, heading, 2, 67.5)
- local pos2 = GetDirectionalOffset(pos, heading, 2, 112.5)
- local pos3 = GetDirectionalOffset(pos, heading, 2, 247.5)
- local pos4 = GetDirectionalOffset(pos, heading, 2, 292.5)
- dr_text3dpos("view", pos1, 67.5 .. "°", 255, 255, 255)
- dr_text3dpos("view", pos2, 112.5 .. "°", 255, 255, 255)
- dr_text3dpos("view", pos3, 247.5 .. "°", 255, 255, 255)
- dr_text3dpos("view", pos4, 292.5 .. "°", 255, 255, 255)
- local pos_check_wall_back = GetDirectionalOffset(pos, heading, 3, 90)
- local pos_check_wall_front = GetDirectionalOffset(pos, heading, 3, -90)
- pos_check_wall_back.y = pos_check_wall_back.y + 2
- pos_check_wall_front.y = pos_check_wall_front.y + 2
- dr_text3dpos("view", pos_check_wall_back, "check back", 255, 255, 255)
- dr_text3dpos("view", pos_check_wall_front, "check front", 255, 255, 255)
- dr_drawline(pos, pos_check_wall_back, 255, 255, 255, "view" )
- dr_drawline(pos, pos_check_wall_front, 255, 255, 255, "view" )
- --World_GetEntitiesNearPoint( Player* player, EGroup* egroup, ScarPosition pos, float radius, <a href="enum_list.htm#OwnerType">OwnerType</a> ownerType
- local eg_back = EGroup_Create("eb_back")
- local eg_front = EGroup_Create("eg_front")
- --g_gates[gateKey] = {entityId = Entity_GetGameID(gate), owner = owner, isOpen = false, closed = {pos = pos, heading = heading}, open = {pos = pos_open, heading = heading_open}}
- World_GetEntitiesNearPoint(owner, eg_back, pos_check_wall_back, 1, OT_Player)
- World_GetEntitiesNearPoint(owner, eg_front, pos_check_wall_front, 1, OT_Player)
- EGroup_Filter(eg_back, g_ebp_west_german_wall_gate, FILTER_KEEP)
- EGroup_Filter(eg_front, g_ebp_west_german_wall_gate, FILTER_KEEP)
- --if not EGroup_IsEmpty(eg_back) and EGroup_IsEmpty(eg_front)
- Msg("Pos4 Pos: X: " .. pos4.x .. ", Y: " .. pos4.y .. ", Z: " .. pos4.z)
- --local pos2 = Util_GetOffsetPosition(pos, OFFSET_FRONT_RIGHT, 5)
- --local pos3 = Util_GetOffsetPosition(pos, OFFSET_BACK_RIGHT, 5)
- --local pos4 = Util_GetOffsetPosition(pos, OFFSET_BACK_LEFT, 5)
- local posList = {pos1, pos2, pos3, pos4}
- for key, value in ipairs(posList) do
- value.y = value.y + 1
- end
- dr_drawline(pos1, pos2, 255, 255, 255, "view" )
- dr_drawline(pos2, pos3, 255, 255, 255, "view" )
- dr_drawline(pos3, pos4, 255, 255, 255, "view" )
- dr_drawline(pos4, pos1, 255, 255, 255, "view" )
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement