1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-16 02:33:53 +02:00
Rampant/libs/MovementUtils.lua

224 lines
7.2 KiB
Lua
Raw Normal View History

2019-02-16 06:17:30 +02:00
if movementUtilsG then
return movementUtilsG
end
local movementUtils = {}
-- imports
local constants = require("Constants")
2017-11-21 09:27:03 +02:00
local mapUtils = require("MapUtils")
local mathUtils = require("MathUtils")
-- constants
2019-02-06 08:25:43 +02:00
local MOVEMENT_PENALTY_AMOUNT = constants.MOVEMENT_PENALTY_AMOUNT
2017-11-21 09:27:03 +02:00
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
local SENTINEL_IMPASSABLE_CHUNK = constants.SENTINEL_IMPASSABLE_CHUNK
-- imported functions
2017-11-21 09:27:03 +02:00
local canMoveChunkDirection = mapUtils.canMoveChunkDirection
local getNeighborChunks = mapUtils.getNeighborChunks
2017-11-21 09:27:03 +02:00
-- 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
2019-03-09 08:23:00 +02:00
if not surface.can_place_entity({name="chunk-scanner-squad-movement-rampant", position=position}) then
pos = surface.find_non_colliding_position("chunk-scanner-squad-movement-rampant", position, 4, 2, true)
end
return (distort and distortPosition(pos)) or pos
end
function movementUtils.findMovementXY(surface, x, y)
local pos = position
if not surface.can_place_entity({name="chunk-scanner-squad-movement-rampant", position={}}) then
pos = surface.find_non_colliding_position("chunk-scanner-squad-movement-rampant", position, 4, 2, true)
end
return pos
end
2019-03-09 08:23:00 +02:00
function movementUtils.addMovementPenalty(units, chunk)
local penalties = units.penalties
for i=1,#penalties do
local penalty = penalties[i]
2017-12-21 05:50:36 +02:00
if (penalty.c == chunk) then
2019-03-09 08:23:00 +02:00
penalty.v = (2 * penalty.v) + MOVEMENT_PENALTY_AMOUNT
return
end
end
if (#penalties == 7) then
tableRemove(penalties, 7)
end
2017-12-21 05:50:36 +02:00
tableInsert(penalties,
1,
{ v = MOVEMENT_PENALTY_AMOUNT,
c = chunk })
end
2019-03-09 08:23:00 +02:00
function movementUtils.lookupMovementPenalty(squad, chunk)
local penalties = squad.penalties
for i=1,#penalties do
local penalty = penalties[i]
2019-03-09 08:23:00 +02:00
if (penalty.c == chunk) then
return penalty.v
end
end
return 0
end
2017-11-21 09:27:03 +02:00
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-11 08:14:17 +02:00
function movementUtils.scoreNeighborsForAttack(map, natives, chunk, neighborDirectionChunks, scoreFunction, squad)
2017-11-21 09:27:03 +02:00
local highestChunk = SENTINEL_IMPASSABLE_CHUNK
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-06 08:25:43 +02:00
local highestDirection
2019-03-09 08:23:00 +02:00
local nextHighestChunk = SENTINEL_IMPASSABLE_CHUNK
local nextHighestScore = -MAGIC_MAXIMUM_NUMBER
local nextHighestDirection
2017-11-21 09:27:03 +02:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2019-02-06 08:25:43 +02:00
2018-09-24 06:56:45 +02:00
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and canMoveChunkDirection(map, x, chunk, neighborChunk) then
2019-02-11 08:14:17 +02:00
local score = scoreFunction(natives, squad, neighborChunk)
2017-11-21 09:27:03 +02:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
end
end
2019-02-06 08:25:43 +02:00
if (highestChunk ~= SENTINEL_IMPASSABLE_CHUNK) then
neighborDirectionChunks = getNeighborChunks(map, highestChunk.x, highestChunk.y)
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and canMoveChunkDirection(map, x, highestChunk, neighborChunk) then
local score = scoreFunction(natives, squad, neighborChunk)
if (score > nextHighestScore) then
nextHighestScore = score
nextHighestChunk = neighborChunk
nextHighestDirection = x
end
end
end
end
return highestChunk, highestDirection, nextHighestChunk, nextHighestDirection
2017-11-21 09:27:03 +02:00
end
2018-09-24 06:56:45 +02: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-06 08:25:43 +02:00
local highestDirection
2018-09-24 06:56:45 +02:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and canMoveChunkDirection(map, x, chunk, neighborChunk) then
2019-02-06 08:25:43 +02:00
local score = scoreFunction(squad, neighborChunk)
2018-09-24 06:56:45 +02: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
2018-09-24 06:56:45 +02:00
end
2019-02-06 08:25:43 +02:00
2018-09-24 06:56:45 +02:00
return highestChunk, highestDirection
end
2017-11-21 09:27:03 +02:00
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-06 08:25:43 +02:00
function movementUtils.scoreNeighborsForResource(chunk, neighborDirectionChunks, validFunction, scoreFunction, map)
local highestChunk = SENTINEL_IMPASSABLE_CHUNK
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-06 08:25:43 +02:00
local highestDirection
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2018-09-24 06:56:45 +02: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-24 06:56:45 +02:00
if (chunk ~= SENTINEL_IMPASSABLE_CHUNK) and (scoreFunction(chunk) > highestScore) then
return SENTINEL_IMPASSABLE_CHUNK, -1
end
2019-02-06 08:25:43 +02:00
return highestChunk, highestDirection
end
2017-11-21 09:27:03 +02:00
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-06 08:25:43 +02:00
function movementUtils.scoreNeighborsForRetreat(chunk, neighborDirectionChunks, scoreFunction, map)
2017-11-21 09:27:03 +02:00
local highestChunk = SENTINEL_IMPASSABLE_CHUNK
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-06 08:25:43 +02:00
local highestDirection
2017-11-21 09:27:03 +02:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2018-09-24 06:56:45 +02:00
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and canMoveChunkDirection(map, x, chunk, neighborChunk) then
2018-01-14 07:48:21 +02:00
local score = scoreFunction(map, neighborChunk)
2017-11-21 09:27:03 +02:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
end
end
2019-02-06 08:25:43 +02:00
2017-11-21 09:27:03 +02:00
return highestChunk, highestDirection
end
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-06 08:25:43 +02:00
function movementUtils.scoreNeighborsForFormation(neighborChunks, validFunction, scoreFunction, map)
2017-11-21 09:27:03 +02:00
local highestChunk = SENTINEL_IMPASSABLE_CHUNK
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection
for x=1,8 do
local neighborChunk = neighborChunks[x]
2018-01-14 07:48:21 +02:00
if (neighborChunk ~= SENTINEL_IMPASSABLE_CHUNK) and validFunction(map, neighborChunk) then
2017-11-21 09:27:03 +02:00
local score = scoreFunction(neighborChunk)
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
2017-11-21 09:27:03 +02:00
end
end
end
return highestChunk, highestDirection
end
2019-02-16 06:17:30 +02:00
movementUtilsG = movementUtils
return movementUtils