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

270 lines
8.7 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")
local chunkPropertyUtils = require("ChunkPropertyUtils")
-- constants
2017-11-21 09:27:03 +02:00
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
-- 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 tableRemove = table.remove
local tableInsert = table.insert
local distortPosition = mathUtils.distortPosition
-- module code
2019-10-19 21:13:48 +02: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 21:13:48 +02:00
return pos
end
2019-10-21 02:53:16 +02:00
function movementUtils.findMovementPositionEntity(entityName, surface, position)
local pos = position
pos = surface.find_non_colliding_position(entityName, pos, 5, 4, true)
2019-10-21 02:53:16 +02:00
return pos
end
2019-10-19 21:13:48 +02: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-21 02:53:16 +02:00
return distortPosition(pos, 8)
end
function movementUtils.addMovementPenalty(map, squad, chunk)
2020-05-17 07:06:55 +02:00
if (chunk == -1) then
2020-05-15 22:51:38 +02:00
return
end
2020-05-24 06:17:18 +02:00
local penalties = squad.penalties
local penaltyCount = #penalties
for i=1,penaltyCount do
local penalty = penalties[i]
if (penalty.c == chunk) then
penalty.v = ((penaltyCount > 1) and penalty.v + 1) or penalty.v
if (penalty.v > 2) then
2020-05-25 05:57:52 +02:00
-- print("movementThreshold", #penalties, squad.group.group_number, penalty.v, squad.settlers, squad.kamikaze, squad.status)
2020-05-24 06:17:18 +02:00
-- game.players[1].teleport(chunk, game.surfaces[1])
squad.kamikaze = true
end
return
end
end
if (penaltyCount == 7) then
tableRemove(penalties, 7)
end
tableInsert(penalties,
1,
{ v = 1,
c = chunk })
end
2017-11-21 09:27:03 +02:00
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-11-30 02:49:22 +02:00
function movementUtils.scoreNeighborsForAttack(map, chunk, neighborDirectionChunks, scoreFunction, squad)
2020-05-15 22:51:38 +02:00
local highestChunk = -1
2017-11-21 09:27:03 +02:00
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-06 08:25:43 +02:00
local highestDirection
2019-03-09 08:23:00 +02:00
2017-11-21 09:27:03 +02:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-15 22:51:38 +02:00
if (neighborChunk ~= -1) then
if canMoveChunkDirection(map, x, chunk, neighborChunk) or (chunk == -1) then
2020-05-23 23:29:56 +02:00
local score = scoreFunction(map, squad, neighborChunk)
2020-05-15 22:51:38 +02:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
2017-11-21 09:27:03 +02:00
end
end
end
2019-02-06 08:25:43 +02:00
local nextHighestChunk = -1
local nextHighestScore = highestScore
local nextHighestDirection
2020-05-15 22:51:38 +02:00
if (highestChunk ~= -1) then
neighborDirectionChunks = getNeighborChunks(map, highestChunk.x, highestChunk.y)
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-23 23:29:56 +02:00
if ((neighborChunk ~= -1) and (neighborChunk ~= chunk) and
2019-10-19 21:13:48 +02:00
canMoveChunkDirection(map, x, highestChunk, neighborChunk)) then
2020-05-23 23:29:56 +02:00
local score = scoreFunction(map, 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)
2020-05-15 22:51:38 +02:00
local highestChunk = -1
2018-09-24 06:56:45 +02:00
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection = 0
2019-10-20 22:45:43 +02:00
2018-09-24 06:56:45 +02:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-15 22:51:38 +02:00
if (neighborChunk ~= -1) then
if canMoveChunkDirection(map, x, chunk, neighborChunk) or (chunk == -1) then
2020-05-23 23:29:56 +02:00
local score = scoreFunction(map, squad, neighborChunk)
2020-05-15 22:51:38 +02:00
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
2018-09-24 06:56:45 +02:00
end
end
end
2020-05-23 23:29:56 +02:00
if (chunk ~= -1) and (scoreFunction(map, squad, chunk) > highestScore) then
2020-05-15 22:51:38 +02:00
return chunk, 0, -1, 0
2018-09-24 06:56:45 +02:00
end
2019-02-06 08:25:43 +02:00
2020-05-15 22:51:38 +02:00
local nextHighestChunk = -1
local nextHighestScore = highestScore
local nextHighestDirection = 0
2019-10-20 22:45:43 +02:00
2020-05-15 22:51:38 +02:00
if (highestChunk ~= -1) then
2019-10-19 21:13:48 +02:00
neighborDirectionChunks = getNeighborChunks(map, highestChunk.x, highestChunk.y)
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-15 22:51:38 +02:00
if ((neighborChunk ~= -1) and (neighborChunk ~= chunk) and
2019-10-19 21:13:48 +02:00
canMoveChunkDirection(map, x, highestChunk, neighborChunk)) then
2020-05-23 23:29:56 +02:00
local score = scoreFunction(map, squad, neighborChunk)
2019-10-19 21:13:48 +02:00
if (score > nextHighestScore) then
nextHighestScore = score
nextHighestChunk = neighborChunk
nextHighestDirection = x
end
end
end
end
return highestChunk, highestDirection, nextHighestChunk, nextHighestDirection
2018-09-24 06:56:45 +02:00
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)
2020-05-15 22:51:38 +02:00
local highestChunk = -1
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-06 08:25:43 +02:00
local highestDirection
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-15 22:51:38 +02:00
if (neighborChunk ~= -1) and canMoveChunkDirection(map, x, chunk, neighborChunk) and validFunction(map, chunk, neighborChunk) then
2020-05-24 05:47:14 +02:00
local score = scoreFunction(map, neighborChunk)
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
end
end
2020-05-15 22:51:38 +02:00
if (chunk ~= -1) and (scoreFunction(chunk) > highestScore) then
return -1, -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)
2020-05-15 22:51:38 +02:00
local highestChunk = -1
2017-11-21 09:27:03 +02:00
local highestScore = -MAGIC_MAXIMUM_NUMBER
2019-02-06 08:25:43 +02:00
local highestDirection
2019-10-20 22:45:43 +02:00
2017-11-21 09:27:03 +02:00
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-15 22:51:38 +02:00
if (neighborChunk ~= -1) then
if canMoveChunkDirection(map, x, chunk, neighborChunk) or (chunk == -1) then
local score = scoreFunction(map, neighborChunk)
if (score > highestScore) then
highestScore = score
highestChunk = neighborChunk
highestDirection = x
end
2017-11-21 09:27:03 +02:00
end
end
end
2019-02-06 08:25:43 +02:00
local nextHighestChunk = -1
local nextHighestScore = highestScore
local nextHighestDirection
2020-05-15 22:51:38 +02:00
if (highestChunk ~= -1) then
2019-10-20 22:45:43 +02:00
neighborDirectionChunks = getNeighborChunks(map, highestChunk.x, highestChunk.y)
for x=1,8 do
local neighborChunk = neighborDirectionChunks[x]
2020-05-15 22:51:38 +02:00
if ((neighborChunk ~= -1) and (neighborChunk ~= chunk) and
2019-10-20 22:45:43 +02: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 22:51:38 +02:00
nextHighestChunk = -1
2019-10-20 22:45:43 +02:00
end
return highestChunk, highestDirection, nextHighestChunk, nextHighestDirection
2017-11-21 09:27:03 +02:00
end
--[[
Expects all neighbors adjacent to a chunk
--]]
2019-02-06 08:25:43 +02:00
function movementUtils.scoreNeighborsForFormation(neighborChunks, validFunction, scoreFunction, map)
2020-05-15 22:51:38 +02:00
local highestChunk = -1
2017-11-21 09:27:03 +02:00
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection
for x=1,8 do
local neighborChunk = neighborChunks[x]
2020-05-15 22:51:38 +02:00
if (neighborChunk ~= -1) and validFunction(map, neighborChunk) then
2020-05-24 05:47:14 +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
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