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

270 lines
8.5 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")
2017-11-20 23:27:03 -08:00
local mapUtils = require("MapUtils")
local mathUtils = require("MathUtils")
-- constants
2017-11-20 23:27:03 -08:00
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
-- imported functions
2017-11-20 23:27:03 -08:00
local canMoveChunkDirection = mapUtils.canMoveChunkDirection
local getNeighborChunks = mapUtils.getNeighborChunks
2017-11-20 23:27:03 -08:00
local tableRemove = table.remove
local tableInsert = table.insert
local distortPosition = mathUtils.distortPosition
-- module code
2019-10-19 12:13:48 -07:00
function movementUtils.findMovementPosition(surface, position)
local pos = position
pos = surface.find_non_colliding_position("chunk-scanner-squad-movement-rampant", pos, 10, 2, false)
2019-10-19 12:13:48 -07:00
return pos
end
2019-10-20 17:53:16 -07:00
function movementUtils.findMovementPositionEntity(entityName, surface, position)
local pos = position
pos = surface.find_non_colliding_position(entityName, pos, 5, 4, true)
2019-10-20 17:53:16 -07:00
return pos
end
2019-10-19 12:13:48 -07:00
function movementUtils.findMovementPositionDistort(surface, position)
local pos = position
pos = surface.find_non_colliding_position("chunk-scanner-squad-movement-rampant", pos, 10, 2, false)
2019-10-20 17:53:16 -07:00
return distortPosition(pos, 8)
end
2021-02-13 20:49:54 -08:00
function movementUtils.addMovementPenalty(squad, chunk)
2020-05-16 22:06:55 -07:00
if (chunk == -1) then
2020-05-15 13:51:38 -07:00
return
end
2020-05-23 21:17:18 -07:00
local penalties = squad.penalties
local penaltyCount = #penalties
for i=1,penaltyCount do
local penalty = penalties[i]
if (penalty.c.id == chunk.id) then
2021-04-04 21:46:43 -07:00
penalty.v = penalty.v + 1
2020-05-23 21:17:18 -07:00
if (penalty.v > 2) then
squad.kamikaze = true
end
return
end
end
2021-04-04 21:46:43 -07:00
if (penaltyCount == 10) then
tableRemove(penalties, 10)
2020-05-23 21:17:18 -07:00
end
tableInsert(penalties,
1,
{ v = 1,
c = chunk })
end
2017-11-20 23:27:03 -08:00
--[[
Expects all neighbors adjacent to a chunk
--]]
2021-02-13 20:49:54 -08:00
function movementUtils.scoreNeighborsForAttack(map, chunk, neighborDirectionChunks, scoreFunction)
2020-05-15 13:51:38 -07:00
local highestChunk = -1
2017-11-20 23:27:03 -08:00
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-05 22:25:43 -08:00
local highestDirection
2019-03-08 22:23:00 -08:00
2017-11-20 23:27:03 -08:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-15 13:51:38 -07:00
if (neighborChunk ~= -1) then
if (chunk == -1) or canMoveChunkDirection(map, x, chunk, neighborChunk) then
2021-02-13 20:49:54 -08:00
local score = scoreFunction(map, neighborChunk)
2020-05-15 13:51:38 -07:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
2017-11-20 23:27:03 -08:00
end
end
end
2019-02-05 22:25:43 -08:00
local nextHighestChunk = -1
local nextHighestScore = highestScore
local nextHighestDirection
2020-05-15 13:51:38 -07:00
if (highestChunk ~= -1) then
neighborDirectionChunks = getNeighborChunks(map, highestChunk.x, highestChunk.y)
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
if ((neighborChunk ~= -1) and ((chunk == -1) or (neighborChunk.id ~= chunk.id)) and
2019-10-19 12:13:48 -07:00
canMoveChunkDirection(map, x, highestChunk, neighborChunk)) then
2021-02-13 20:49:54 -08:00
local score = scoreFunction(map, neighborChunk)
if (score > nextHighestScore) then
nextHighestScore = score
nextHighestChunk = neighborChunk
nextHighestDirection = x
end
end
end
end
return highestChunk, highestDirection, nextHighestChunk, nextHighestDirection
2017-11-20 23:27:03 -08:00
end
2018-09-23 21:56:45 -07:00
--[[
Expects all neighbors adjacent to a chunk
--]]
2021-02-13 20:49:54 -08:00
function movementUtils.scoreNeighborsForSettling(map, chunk, neighborDirectionChunks, scoreFunction)
2020-05-15 13:51:38 -07:00
local highestChunk = -1
2018-09-23 21:56:45 -07:00
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection = 0
2019-10-20 13:45:43 -07:00
2018-09-23 21:56:45 -07:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-15 13:51:38 -07:00
if (neighborChunk ~= -1) then
if (chunk == -1) or canMoveChunkDirection(map, x, chunk, neighborChunk) then
2021-02-13 20:49:54 -08:00
local score = scoreFunction(map, neighborChunk)
2020-05-15 13:51:38 -07:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
2018-09-23 21:56:45 -07:00
end
end
end
2021-02-13 20:49:54 -08:00
if (chunk ~= -1) and (scoreFunction(map, chunk) > highestScore) then
2020-05-15 13:51:38 -07:00
return chunk, 0, -1, 0
2018-09-23 21:56:45 -07:00
end
2019-02-05 22:25:43 -08:00
2020-05-15 13:51:38 -07:00
local nextHighestChunk = -1
local nextHighestScore = highestScore
local nextHighestDirection = 0
2019-10-20 13:45:43 -07:00
2020-05-15 13:51:38 -07:00
if (highestChunk ~= -1) then
2019-10-19 12:13:48 -07:00
neighborDirectionChunks = getNeighborChunks(map, highestChunk.x, highestChunk.y)
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
if ((neighborChunk ~= -1) and ((chunk == -1) or (neighborChunk.id ~= chunk.id)) and
2019-10-19 12:13:48 -07:00
canMoveChunkDirection(map, x, highestChunk, neighborChunk)) then
2021-02-13 20:49:54 -08:00
local score = scoreFunction(map, neighborChunk)
2019-10-19 12:13:48 -07:00
if (score > nextHighestScore) then
nextHighestScore = score
nextHighestChunk = neighborChunk
nextHighestDirection = x
end
end
end
end
return highestChunk, highestDirection, nextHighestChunk, nextHighestDirection
2018-09-23 21:56:45 -07:00
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)
2020-05-15 13:51:38 -07:00
local highestChunk = -1
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-05 22:25:43 -08:00
local highestDirection
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2021-02-13 20:49:54 -08:00
if (neighborChunk ~= -1) and
canMoveChunkDirection(map, x, chunk, neighborChunk) and
validFunction(map, chunk, neighborChunk)
then
2020-05-23 20:47:14 -07:00
local score = scoreFunction(map, neighborChunk)
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
end
end
2020-05-25 17:05:01 -07:00
if (chunk ~= -1) and (scoreFunction(map, chunk) > highestScore) then
2020-05-15 13:51:38 -07:00
return -1, -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)
2020-05-15 13:51:38 -07:00
local highestChunk = -1
2017-11-20 23:27:03 -08:00
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-05 22:25:43 -08:00
local highestDirection
2019-10-20 13:45:43 -07:00
2017-11-20 23:27:03 -08:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-15 13:51:38 -07:00
if (neighborChunk ~= -1) then
if (chunk == -1) or canMoveChunkDirection(map, x, chunk, neighborChunk) then
2020-05-15 13:51:38 -07:00
local score = scoreFunction(map, neighborChunk)
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
2017-11-20 23:27:03 -08:00
end
end
end
2019-02-05 22:25:43 -08:00
local nextHighestChunk = -1
local nextHighestScore = highestScore
local nextHighestDirection
2020-05-15 13:51:38 -07:00
if (highestChunk ~= -1) then
2019-10-20 13:45:43 -07:00
neighborDirectionChunks = getNeighborChunks(map, highestChunk.x, highestChunk.y)
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
if ((neighborChunk ~= -1) and ((chunk == -1) or (neighborChunk.id ~= chunk.id)) and
2019-10-20 13:45:43 -07:00
canMoveChunkDirection(map, x, highestChunk, neighborChunk)) then
local score = scoreFunction(map, neighborChunk)
if (score > nextHighestScore) then
nextHighestScore = score
nextHighestChunk = neighborChunk
nextHighestDirection = x
end
end
end
end
if (nextHighestChunk == nil) then
2020-05-15 13:51:38 -07:00
nextHighestChunk = -1
2019-10-20 13:45:43 -07:00
end
return highestChunk, highestDirection, nextHighestChunk, nextHighestDirection
2017-11-20 23:27:03 -08:00
end
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-05 22:25:43 -08:00
function movementUtils.scoreNeighborsForFormation(neighborChunks, validFunction, scoreFunction, map)
2020-05-15 13:51:38 -07:00
local highestChunk = -1
2017-11-20 23:27:03 -08:00
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection
for x=1,8 do
local neighborChunk = neighborChunks[x]
2020-05-15 13:51:38 -07:00
if (neighborChunk ~= -1) and validFunction(map, neighborChunk) then
2020-05-23 20:47:14 -07:00
local score = scoreFunction(map, neighborChunk)
2017-11-20 23:27:03 -08:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
2017-11-20 23:27:03 -08:00
end
end
end
return highestChunk, highestDirection
end
2019-02-15 20:17:30 -08:00
movementUtilsG = movementUtils
return movementUtils