2019-02-15 20:17:30 -08:00
|
|
|
if movementUtilsG then
|
|
|
|
return movementUtilsG
|
|
|
|
end
|
2017-06-12 20:16:43 -07:00
|
|
|
local movementUtils = {}
|
|
|
|
|
|
|
|
-- imports
|
|
|
|
|
|
|
|
local constants = require("Constants")
|
2017-11-20 23:27:03 -08:00
|
|
|
local mapUtils = require("MapUtils")
|
2017-06-12 20:16:43 -07:00
|
|
|
local mathUtils = require("MathUtils")
|
|
|
|
|
|
|
|
-- constants
|
|
|
|
|
2019-02-05 22:25:43 -08:00
|
|
|
local MOVEMENT_PENALTY_AMOUNT = constants.MOVEMENT_PENALTY_AMOUNT
|
2017-06-12 20:16:43 -07:00
|
|
|
|
2017-11-20 23:27:03 -08:00
|
|
|
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
|
|
|
|
|
2017-06-12 20:16:43 -07:00
|
|
|
-- imported functions
|
|
|
|
|
2017-11-20 23:27:03 -08:00
|
|
|
local canMoveChunkDirection = mapUtils.canMoveChunkDirection
|
2019-10-13 22:49:52 -07:00
|
|
|
local getNeighborChunks = mapUtils.getNeighborChunks
|
2017-11-20 23:27:03 -08:00
|
|
|
|
2018-02-14 00:28:42 -08:00
|
|
|
-- local recycleBiters = unitGroupUtils.recycleBiters
|
2017-06-12 20:16:43 -07: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)
|
2017-06-12 20:16:43 -07:00
|
|
|
local pos = position
|
2019-10-19 12:13:48 -07:00
|
|
|
if not surface.can_place_entity({name="chunk-scanner-squad-movement-rampant", position=pos}) then
|
|
|
|
pos = surface.find_non_colliding_position("chunk-scanner-squad-movement-rampant", pos, 15, 2, true)
|
2017-06-12 20:16:43 -07:00
|
|
|
end
|
2019-10-19 12:13:48 -07:00
|
|
|
return pos
|
2017-06-12 20:16:43 -07:00
|
|
|
end
|
|
|
|
|
2019-10-20 17:53:16 -07:00
|
|
|
function movementUtils.findMovementPositionEntity(entityName, surface, position)
|
|
|
|
local pos = position
|
|
|
|
if not surface.can_place_entity({name=entityName, position=pos}) then
|
2019-11-03 22:19:22 -08:00
|
|
|
pos = surface.find_non_colliding_position(entityName, pos, 5, 4, true)
|
2019-10-20 17:53:16 -07:00
|
|
|
end
|
|
|
|
return pos
|
|
|
|
end
|
|
|
|
|
2019-10-19 12:13:48 -07:00
|
|
|
function movementUtils.findMovementPositionDistort(surface, position)
|
2019-10-13 22:49:52 -07:00
|
|
|
local pos = position
|
2019-10-19 12:13:48 -07:00
|
|
|
if not surface.can_place_entity({name="chunk-scanner-squad-movement-rampant", position=pos}) then
|
|
|
|
pos = surface.find_non_colliding_position("chunk-scanner-squad-movement-rampant", pos, 15, 2, true)
|
2019-10-13 22:49:52 -07:00
|
|
|
end
|
2019-10-20 17:53:16 -07:00
|
|
|
return distortPosition(pos, 8)
|
2019-10-13 22:49:52 -07:00
|
|
|
end
|
|
|
|
|
2020-05-16 15:34:54 -07:00
|
|
|
function movementUtils.addMovementPenalty(squad, chunk)
|
|
|
|
if (chunk == -1) or (squad.chunk == chunk) then
|
2020-05-15 13:51:38 -07:00
|
|
|
return
|
|
|
|
end
|
2020-05-16 15:34:54 -07:00
|
|
|
local penalties = squad.penalties
|
2017-06-12 20:16:43 -07:00
|
|
|
for i=1,#penalties do
|
|
|
|
local penalty = penalties[i]
|
2017-12-20 19:50:36 -08:00
|
|
|
if (penalty.c == chunk) then
|
2019-03-08 22:23:00 -08:00
|
|
|
penalty.v = (2 * penalty.v) + MOVEMENT_PENALTY_AMOUNT
|
2020-05-16 15:34:54 -07:00
|
|
|
print(penalty.v)
|
|
|
|
if (penalty.v > MOVEMENT_PENALTY_AMOUNT * 5) then
|
|
|
|
print("erel", penalty, squad.group.group_number)
|
|
|
|
end
|
2017-06-12 20:16:43 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if (#penalties == 7) then
|
|
|
|
tableRemove(penalties, 7)
|
|
|
|
end
|
2020-05-16 15:34:54 -07:00
|
|
|
print(squad.group.group_number)
|
2017-12-20 19:50:36 -08:00
|
|
|
tableInsert(penalties,
|
2019-10-13 22:49:52 -07:00
|
|
|
1,
|
|
|
|
{ v = MOVEMENT_PENALTY_AMOUNT,
|
|
|
|
c = chunk })
|
2017-06-12 20:16:43 -07:00
|
|
|
end
|
|
|
|
|
2019-03-08 22:23:00 -08:00
|
|
|
function movementUtils.lookupMovementPenalty(squad, chunk)
|
2017-06-12 20:16:43 -07:00
|
|
|
local penalties = squad.penalties
|
|
|
|
for i=1,#penalties do
|
|
|
|
local penalty = penalties[i]
|
2019-03-08 22:23:00 -08:00
|
|
|
if (penalty.c == chunk) then
|
2017-06-12 20:16:43 -07:00
|
|
|
return penalty.v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2017-11-20 23:27:03 -08:00
|
|
|
--[[
|
|
|
|
Expects all neighbors adjacent to a chunk
|
|
|
|
--]]
|
2019-11-29 16:49:22 -08:00
|
|
|
function movementUtils.scoreNeighborsForAttack(map, chunk, neighborDirectionChunks, scoreFunction, squad)
|
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
|
|
|
|
2020-05-15 13:51:38 -07:00
|
|
|
local nextHighestChunk = -1
|
2019-10-13 22:49:52 -07:00
|
|
|
local nextHighestScore = -MAGIC_MAXIMUM_NUMBER
|
|
|
|
local nextHighestDirection
|
|
|
|
|
2019-11-29 16:49:22 -08:00
|
|
|
local natives = map.natives
|
2020-05-15 13:51:38 -07:00
|
|
|
|
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
|
|
|
|
2020-05-15 13:51:38 -07:00
|
|
|
if (neighborChunk ~= -1) then
|
|
|
|
if canMoveChunkDirection(map, x, chunk, neighborChunk) or (chunk == -1) then
|
|
|
|
local score = scoreFunction(natives, squad, 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
|
|
|
|
2020-05-15 13:51:38 -07:00
|
|
|
if (highestChunk ~= -1) then
|
2019-10-13 22:49:52 -07:00
|
|
|
neighborDirectionChunks = getNeighborChunks(map, highestChunk.x, highestChunk.y)
|
|
|
|
for x=1,8 do
|
|
|
|
local neighborChunk = neighborDirectionChunks[x]
|
|
|
|
|
2020-05-15 13:51:38 -07:00
|
|
|
if ((neighborChunk ~= -1) and (neighborChunk ~= chunk) and
|
2019-10-19 12:13:48 -07:00
|
|
|
canMoveChunkDirection(map, x, highestChunk, neighborChunk)) then
|
2019-10-13 22:49:52 -07:00
|
|
|
local score = scoreFunction(natives, squad, neighborChunk)
|
|
|
|
if (score > nextHighestScore) then
|
|
|
|
nextHighestScore = score
|
|
|
|
nextHighestChunk = neighborChunk
|
|
|
|
nextHighestDirection = x
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-19 12:13:48 -07:00
|
|
|
if (nextHighestChunk == nil) then
|
2020-05-15 13:51:38 -07:00
|
|
|
nextHighestChunk = -1
|
2019-10-19 12:13:48 -07:00
|
|
|
end
|
2019-10-20 13:45:43 -07:00
|
|
|
|
2019-10-13 22:49:52 -07:00
|
|
|
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
|
|
|
|
--]]
|
|
|
|
function movementUtils.scoreNeighborsForSettling(map, chunk, neighborDirectionChunks, scoreFunction, squad)
|
2020-05-15 13:51:38 -07:00
|
|
|
local highestChunk = -1
|
2018-09-23 21:56:45 -07:00
|
|
|
local highestScore = -MAGIC_MAXIMUM_NUMBER
|
2019-12-08 19:31:51 -08:00
|
|
|
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 canMoveChunkDirection(map, x, chunk, neighborChunk) or (chunk == -1) then
|
|
|
|
local score = scoreFunction(squad, neighborChunk)
|
|
|
|
if (score > highestScore) then
|
|
|
|
highestScore = score
|
|
|
|
highestChunk = neighborChunk
|
|
|
|
highestDirection = x
|
|
|
|
end
|
2018-09-23 21:56:45 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-15 13:51:38 -07:00
|
|
|
if (chunk ~= -1) and (scoreFunction(squad, chunk) > highestScore) then
|
|
|
|
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
|
2019-10-19 12:13:48 -07:00
|
|
|
local nextHighestScore = -MAGIC_MAXIMUM_NUMBER
|
2019-12-08 19:31:51 -08:00
|
|
|
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]
|
|
|
|
|
2020-05-15 13:51:38 -07:00
|
|
|
if ((neighborChunk ~= -1) and (neighborChunk ~= chunk) and
|
2019-10-19 12:13:48 -07:00
|
|
|
canMoveChunkDirection(map, x, highestChunk, neighborChunk)) then
|
|
|
|
local score = scoreFunction(squad, neighborChunk)
|
|
|
|
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
|
2018-02-14 00:28:42 -08:00
|
|
|
local highestScore = -MAGIC_MAXIMUM_NUMBER
|
2019-02-05 22:25:43 -08:00
|
|
|
local highestDirection
|
2018-02-14 00:28:42 -08:00
|
|
|
for x=1,8 do
|
|
|
|
local neighborChunk = neighborDirectionChunks[x]
|
2020-05-15 13:51:38 -07:00
|
|
|
if (neighborChunk ~= -1) and canMoveChunkDirection(map, x, chunk, neighborChunk) and validFunction(map, chunk, neighborChunk) then
|
2018-02-14 00:28:42 -08:00
|
|
|
local score = scoreFunction(neighborChunk)
|
|
|
|
if (score > highestScore) then
|
|
|
|
highestScore = score
|
|
|
|
highestChunk = neighborChunk
|
|
|
|
highestDirection = x
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-15 13:51:38 -07:00
|
|
|
if (chunk ~= -1) and (scoreFunction(chunk) > highestScore) then
|
|
|
|
return -1, -1
|
2018-02-14 00:28:42 -08:00
|
|
|
end
|
2019-02-05 22:25:43 -08:00
|
|
|
|
2018-02-14 00:28:42 -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
|
|
|
|
2020-05-15 13:51:38 -07:00
|
|
|
local nextHighestChunk = -1
|
2019-10-20 13:45:43 -07:00
|
|
|
local nextHighestScore = -MAGIC_MAXIMUM_NUMBER
|
|
|
|
local nextHighestDirection
|
|
|
|
|
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 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-20 23:27:03 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-05 22:25:43 -08:00
|
|
|
|
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]
|
|
|
|
|
2020-05-15 13:51:38 -07:00
|
|
|
if ((neighborChunk ~= -1) and (neighborChunk ~= chunk) 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
|
2017-11-20 23:27:03 -08:00
|
|
|
local score = scoreFunction(neighborChunk)
|
|
|
|
if (score > highestScore) then
|
|
|
|
highestScore = score
|
|
|
|
highestChunk = neighborChunk
|
2019-10-13 22:49:52 -07:00
|
|
|
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
|
2017-06-12 20:16:43 -07:00
|
|
|
return movementUtils
|