1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-28 03:29:34 +02:00
Rampant/libs/MovementUtils.lua

205 lines
6.3 KiB
Lua
Raw Normal View History

2019-02-15 20:17:30 -08:00
if movementUtilsG then
return movementUtilsG
end
local movementUtils = {}
-- imports
local constants = require("Constants")
local unitGroupUtils = require("UnitGroupUtils")
2017-11-20 23:27:03 -08:00
local mapUtils = require("MapUtils")
local mathUtils = require("MathUtils")
-- constants
2019-02-05 22:25:43 -08:00
local MOVEMENT_PENALTY_AMOUNT = constants.MOVEMENT_PENALTY_AMOUNT
local MAX_PENALTY_BEFORE_PURGE = constants.MAX_PENALTY_BEFORE_PURGE
2017-11-20 23:27:03 -08:00
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
-- local RESOURCE_PHEROMONE = constants.RESOURCE_PHEROMONE
2017-11-20 23:27:03 -08:00
local SENTINEL_IMPASSABLE_CHUNK = constants.SENTINEL_IMPASSABLE_CHUNK
-- imported functions
2017-11-20 23:27:03 -08:00
local canMoveChunkDirection = mapUtils.canMoveChunkDirection
-- local recycleBiters = unitGroupUtils.recycleBiters
local tableRemove = table.remove
local tableInsert = table.insert
local distortPosition = mathUtils.distortPosition
-- module code
function movementUtils.findMovementPosition(surface, position, distort)
local pos = position
if not surface.can_place_entity({name="behemoth-biter", position=position}) then
2019-03-08 16:42:20 -08:00
pos = surface.find_non_colliding_position("behemoth-biter", position, 5, 2, true)
end
return (distort and distortPosition(pos)) or pos
end
2017-12-20 19:50:36 -08:00
function movementUtils.addMovementPenalty(natives, units, chunk)
local penalties = units.penalties
for i=1,#penalties do
local penalty = penalties[i]
2017-12-20 19:50:36 -08:00
if (penalty.c == chunk) then
2019-02-05 22:25:43 -08:00
penalty.v = penalty.v + MOVEMENT_PENALTY_AMOUNT
if (penalty.v > MAX_PENALTY_BEFORE_PURGE) then
tableRemove(penalties, i)
end
return
end
end
if (#penalties == 7) then
tableRemove(penalties, 7)
end
2017-12-20 19:50:36 -08:00
tableInsert(penalties,
1,
2019-02-05 22:25:43 -08:00
{ v = MOVEMENT_PENALTY_AMOUNT,
2017-12-20 19:50:36 -08:00
c = chunk })
end
2017-11-20 23:27:03 -08:00
function movementUtils.lookupMovementPenalty(squad, x, y)
local penalties = squad.penalties
for i=1,#penalties do
local penalty = penalties[i]
2017-11-20 23:27:03 -08:00
if (penalty.x == x) and (penalty.y == y) then
return penalty.v
end
end
return 0
end
2017-11-20 23:27:03 -08:00
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-10 22:14:17 -08:00
function movementUtils.scoreNeighborsForAttack(map, natives, chunk, neighborDirectionChunks, scoreFunction, squad)
2017-11-20 23:27:03 -08:00
local highestChunk = SENTINEL_IMPASSABLE_CHUNK
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-05 22:25:43 -08:00
local highestDirection
2017-11-20 23:27:03 -08:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2019-02-05 22:25:43 -08:00
2018-09-23 21:56:45 -07:00
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and canMoveChunkDirection(map, x, chunk, neighborChunk) then
2019-02-10 22:14:17 -08:00
local score = scoreFunction(natives, squad, neighborChunk)
2017-11-20 23:27:03 -08:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
end
end
2019-02-05 22:25:43 -08:00
2019-03-07 19:40:55 -08:00
-- if (chunk ~= SENTINEL_IMPASSABLE_CHUNK) and (scoreFunction(natives, squad, chunk) > highestScore) then
-- return SENTINEL_IMPASSABLE_CHUNK, -1
-- end
2019-02-05 22:25:43 -08:00
2017-11-20 23:27:03 -08:00
return highestChunk, highestDirection
end
2018-09-23 21:56:45 -07:00
--[[
Expects all neighbors adjacent to a chunk
--]]
function movementUtils.scoreNeighborsForSettling(map, chunk, neighborDirectionChunks, scoreFunction, squad)
local highestChunk = SENTINEL_IMPASSABLE_CHUNK
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-05 22:25:43 -08:00
local highestDirection
2018-09-23 21:56:45 -07:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and canMoveChunkDirection(map, x, chunk, neighborChunk) then
2019-02-05 22:25:43 -08:00
local score = scoreFunction(squad, neighborChunk)
2018-09-23 21:56:45 -07:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
end
end
if (chunk ~= SENTINEL_IMPASSABLE_CHUNK) and (scoreFunction(squad, chunk) > highestScore) then
return chunk, -1
end
2019-02-05 22:25:43 -08:00
2018-09-23 21:56:45 -07:00
return highestChunk, highestDirection
end
2017-11-20 23:27:03 -08:00
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-05 22:25:43 -08:00
function movementUtils.scoreNeighborsForResource(chunk, neighborDirectionChunks, validFunction, scoreFunction, map)
local highestChunk = SENTINEL_IMPASSABLE_CHUNK
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-05 22:25:43 -08:00
local highestDirection
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2018-09-23 21:56:45 -07:00
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and canMoveChunkDirection(map, x, chunk, neighborChunk) and validFunction(map, chunk, neighborChunk) then
local score = scoreFunction(neighborChunk)
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
end
end
2018-09-23 21:56:45 -07:00
if (chunk ~= SENTINEL_IMPASSABLE_CHUNK) and (scoreFunction(chunk) > highestScore) then
return SENTINEL_IMPASSABLE_CHUNK, -1
end
2019-02-05 22:25:43 -08:00
return highestChunk, highestDirection
end
2017-11-20 23:27:03 -08:00
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-05 22:25:43 -08:00
function movementUtils.scoreNeighborsForRetreat(chunk, neighborDirectionChunks, scoreFunction, map)
2017-11-20 23:27:03 -08:00
local highestChunk = SENTINEL_IMPASSABLE_CHUNK
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-05 22:25:43 -08:00
local highestDirection
2017-11-20 23:27:03 -08:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2018-09-23 21:56:45 -07:00
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and canMoveChunkDirection(map, x, chunk, neighborChunk) then
2018-01-13 21:48:21 -08:00
local score = scoreFunction(map, neighborChunk)
2017-11-20 23:27:03 -08:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
end
end
2019-02-05 22:25:43 -08:00
2017-11-20 23:27:03 -08:00
return highestChunk, highestDirection
end
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-05 22:25:43 -08:00
function movementUtils.scoreNeighborsForFormation(neighborChunks, validFunction, scoreFunction, map)
2017-11-20 23:27:03 -08:00
local highestChunk = SENTINEL_IMPASSABLE_CHUNK
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection
for x=1,8 do
local neighborChunk = neighborChunks[x]
2018-01-13 21:48:21 -08:00
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and validFunction(map, neighborChunk) then
2017-11-20 23:27:03 -08:00
local score = scoreFunction(neighborChunk)
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
end
end
return highestChunk, highestDirection
end
2019-02-15 20:17:30 -08:00
movementUtilsG = movementUtils
return movementUtils