2022-01-14 14:08:58 -08:00
|
|
|
-- Copyright (C) 2022 veden
|
|
|
|
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU General Public License for more details.
|
|
|
|
|
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
|
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
if MapUtilsG then
|
|
|
|
return MapUtilsG
|
2019-02-15 20:17:30 -08:00
|
|
|
end
|
2023-03-11 12:53:06 -08:00
|
|
|
local MapUtils = {}
|
|
|
|
|
|
|
|
--
|
|
|
|
|
|
|
|
local Universe
|
|
|
|
local NeighborChunks
|
2016-08-04 21:47:51 -07:00
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
-- imports
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
local Constants = require("Constants")
|
|
|
|
local ChunkPropertyUtils = require("ChunkPropertyUtils")
|
2023-03-11 20:51:13 -08:00
|
|
|
local MathUtils = require("MathUtils")
|
2016-08-04 21:47:51 -07:00
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
-- Constants
|
2016-08-19 19:52:27 -07:00
|
|
|
|
2023-03-11 20:51:13 -08:00
|
|
|
local MAGIC_MAXIMUM_NUMBER = Constants.MAGIC_MAXIMUM_NUMBER
|
|
|
|
local ENEMY_PHEROMONE_MULTIPLER = Constants.ENEMY_PHEROMONE_MULTIPLER
|
|
|
|
local BASE_PHEROMONE = Constants.BASE_PHEROMONE
|
|
|
|
local PLAYER_PHEROMONE = Constants.PLAYER_PHEROMONE
|
|
|
|
local RESOURCE_PHEROMONE = Constants.RESOURCE_PHEROMONE
|
|
|
|
local ENEMY_PHEROMONE = Constants.ENEMY_PHEROMONE
|
|
|
|
|
|
|
|
local CHUNK_TICK = Constants.CHUNK_TICK
|
|
|
|
|
|
|
|
local VICTORY_SCENT = Constants.VICTORY_SCENT
|
|
|
|
local VICTORY_SCENT_MULTIPLER = Constants.VICTORY_SCENT_MULTIPLER
|
|
|
|
local VICTORY_SCENT_BOUND = Constants.VICTORY_SCENT_BOUND
|
|
|
|
local DEATH_PHEROMONE_GENERATOR_AMOUNT = Constants.DEATH_PHEROMONE_GENERATOR_AMOUNT
|
|
|
|
local TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT = Constants.TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT
|
2023-03-11 12:53:06 -08:00
|
|
|
local CHUNK_NORTH_SOUTH = Constants.CHUNK_NORTH_SOUTH
|
|
|
|
local CHUNK_EAST_WEST = Constants.CHUNK_EAST_WEST
|
|
|
|
local CHUNK_IMPASSABLE = Constants.CHUNK_IMPASSABLE
|
|
|
|
local CHUNK_ALL_DIRECTIONS = Constants.CHUNK_ALL_DIRECTIONS
|
2017-06-10 01:38:20 -07:00
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
local CHUNK_SIZE = Constants.CHUNK_SIZE
|
2016-08-19 19:52:27 -07:00
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
local CHUNK_SIZE_DIVIDER = Constants.CHUNK_SIZE_DIVIDER
|
2017-11-20 23:27:03 -08:00
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
-- imported functions
|
|
|
|
|
2023-03-11 20:51:13 -08:00
|
|
|
local addVictoryGenerator = ChunkPropertyUtils.addVictoryGenerator
|
|
|
|
local addPermanentDeathGenerator = ChunkPropertyUtils.addPermanentDeathGenerator
|
|
|
|
local addDeathGenerator = ChunkPropertyUtils.addDeathGenerator
|
|
|
|
local getCombinedDeathGenerator = ChunkPropertyUtils.getCombinedDeathGenerator
|
|
|
|
local getCombinedDeathGeneratorRating = ChunkPropertyUtils.getCombinedDeathGeneratorRating
|
|
|
|
local getEnemyStructureCount = ChunkPropertyUtils.getEnemyStructureCount
|
|
|
|
local setDeathGenerator = ChunkPropertyUtils.setDeathGenerator
|
|
|
|
local decayPlayerGenerator = ChunkPropertyUtils.decayPlayerGenerator
|
|
|
|
local getPathRating = ChunkPropertyUtils.getPathRating
|
|
|
|
local linearInterpolation = MathUtils.linearInterpolation
|
|
|
|
local decayDeathGenerator = ChunkPropertyUtils.decayDeathGenerator
|
|
|
|
|
|
|
|
local mMax = math.max
|
2016-08-04 21:47:51 -07:00
|
|
|
local mFloor = math.floor
|
2023-03-11 12:53:06 -08:00
|
|
|
local getPassable = ChunkPropertyUtils.getPassable
|
2022-04-09 20:15:14 -07:00
|
|
|
local tRemove = table.remove
|
|
|
|
local mCeil = math.ceil
|
2016-08-19 19:52:27 -07:00
|
|
|
|
|
|
|
-- module code
|
2016-08-04 21:47:51 -07:00
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.getChunkByXY(map, x, y)
|
2018-01-13 21:48:21 -08:00
|
|
|
local chunkX = map[x]
|
2017-08-08 01:19:51 -07:00
|
|
|
if chunkX then
|
2020-05-15 13:51:38 -07:00
|
|
|
return chunkX[y] or -1
|
2016-08-04 21:47:51 -07:00
|
|
|
end
|
2020-05-15 13:51:38 -07:00
|
|
|
return -1
|
2016-08-04 21:47:51 -07:00
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.getChunkByPosition(map, position)
|
2018-01-13 21:48:21 -08:00
|
|
|
local chunkX = map[mFloor(position.x * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE]
|
2017-12-28 21:38:10 -08:00
|
|
|
if chunkX then
|
2019-10-13 22:49:52 -07:00
|
|
|
local chunkY = mFloor(position.y * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
|
2020-05-15 13:51:38 -07:00
|
|
|
return chunkX[chunkY] or -1
|
2017-12-28 21:38:10 -08:00
|
|
|
end
|
2020-05-15 13:51:38 -07:00
|
|
|
return -1
|
2017-12-28 21:38:10 -08:00
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.positionToChunkXY(position)
|
2017-11-20 23:27:03 -08:00
|
|
|
local chunkX = mFloor(position.x * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
|
|
|
|
local chunkY = mFloor(position.y * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
|
|
|
|
return chunkX, chunkY
|
2016-08-17 22:55:08 -07:00
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.queueGeneratedChunk(event)
|
|
|
|
local map = Universe.maps[event.surface.index]
|
2021-12-05 11:39:39 -08:00
|
|
|
if not map then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
event.tick = (event.tick or game.tick) + 20
|
2023-03-11 18:14:42 -08:00
|
|
|
Universe.eventId = Universe.eventId + 1
|
2023-03-11 12:53:06 -08:00
|
|
|
event.id = Universe.eventId
|
2021-12-05 22:04:07 -08:00
|
|
|
event.map = map
|
2023-03-11 12:53:06 -08:00
|
|
|
Universe.pendingChunks[event.id] = event
|
2021-11-25 22:49:28 -08:00
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.nextMap()
|
|
|
|
local mapIterator = Universe.mapIterator
|
2021-12-07 22:44:22 -08:00
|
|
|
repeat
|
|
|
|
local map
|
2023-03-11 12:53:06 -08:00
|
|
|
Universe.mapIterator, map = next(Universe.maps, Universe.mapIterator)
|
2021-12-07 22:44:22 -08:00
|
|
|
if map and map.activeSurface then
|
|
|
|
return map
|
|
|
|
end
|
2023-03-11 12:53:06 -08:00
|
|
|
until mapIterator == Universe.mapIterator
|
2021-12-07 22:44:22 -08:00
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.removeChunkToNest(chunkId)
|
|
|
|
Universe.chunkToNests[chunkId] = nil
|
|
|
|
if (chunkId == Universe.processNestIterator) then
|
|
|
|
Universe.processNestIterator = nil
|
2022-01-14 11:17:33 -08:00
|
|
|
end
|
2023-03-11 12:53:06 -08:00
|
|
|
if (chunkId == Universe.processMigrationIterator) then
|
|
|
|
Universe.processMigrationIterator = nil
|
2022-01-14 11:17:33 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.findInsertionPoint(processQueue, chunk)
|
2022-04-09 20:15:14 -07:00
|
|
|
local low = 1
|
|
|
|
local high = #processQueue
|
|
|
|
local pivot
|
|
|
|
while (low <= high) do
|
|
|
|
pivot = mCeil((low + high) * 0.5)
|
|
|
|
local pivotChunk = processQueue[pivot]
|
|
|
|
if (pivotChunk.dOrigin > chunk.dOrigin) then
|
|
|
|
high = pivot - 1
|
|
|
|
elseif (pivotChunk.dOrigin <= chunk.dOrigin) then
|
|
|
|
low = pivot + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return low
|
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.removeProcessQueueChunk(processQueue, chunk)
|
|
|
|
local insertionPoint = MapUtils.findInsertionPoint(processQueue, chunk)
|
2022-04-09 20:15:14 -07:00
|
|
|
if insertionPoint > #processQueue then
|
|
|
|
insertionPoint = insertionPoint - 1
|
|
|
|
end
|
|
|
|
for i=insertionPoint,1,-1 do
|
|
|
|
local pqChunk = processQueue[i]
|
|
|
|
if pqChunk.id == chunk.id then
|
|
|
|
tRemove(processQueue, i)
|
|
|
|
return
|
|
|
|
elseif pqChunk.dOrigin < chunk.dOrigin then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.removeChunkFromMap(map, chunk)
|
2022-04-09 20:15:14 -07:00
|
|
|
local x = chunk.x
|
|
|
|
local y = chunk.y
|
2023-03-12 13:02:31 -07:00
|
|
|
|
|
|
|
if not map[x][y] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-12-14 22:03:29 -08:00
|
|
|
map[x][y] = nil
|
2023-03-12 13:02:31 -07:00
|
|
|
local chunkId = chunk.id
|
|
|
|
MapUtils.removeProcessQueueChunk(map.processQueue, chunk)
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
Universe.chunkToActiveNest[chunkId] = nil
|
|
|
|
Universe.chunkToActiveRaidNest[chunkId] = nil
|
|
|
|
Universe.chunkToDrained[chunkId] = nil
|
|
|
|
Universe.chunkToRetreats[chunkId] = nil
|
|
|
|
Universe.chunkToRallys[chunkId] = nil
|
|
|
|
Universe.chunkToPassScan[chunkId] = nil
|
|
|
|
Universe.chunkToNests[chunkId] = nil
|
2023-03-11 18:14:42 -08:00
|
|
|
Universe.chunkToTurrets[chunkId] = nil
|
|
|
|
Universe.chunkToUtilities[chunkId] = nil
|
|
|
|
Universe.chunkToHives[chunkId] = nil
|
2023-03-11 12:53:06 -08:00
|
|
|
Universe.vengenceQueue[chunkId] = nil
|
|
|
|
Universe.chunkToVictory[chunkId] = nil
|
2023-03-12 13:02:31 -07:00
|
|
|
|
|
|
|
local base = chunk.base
|
2022-04-09 20:15:14 -07:00
|
|
|
if base then
|
|
|
|
base.chunkCount = base.chunkCount - 1
|
|
|
|
end
|
2021-12-14 22:03:29 -08:00
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
if Universe.processNestIterator == chunkId then
|
|
|
|
Universe.processNestIterator = nil
|
2021-12-14 22:03:29 -08:00
|
|
|
end
|
2023-03-11 12:53:06 -08:00
|
|
|
if Universe.processActiveSpawnerIterator == chunkId then
|
|
|
|
Universe.processActiveSpawnerIterator = nil
|
2021-12-14 22:03:29 -08:00
|
|
|
end
|
2023-03-11 12:53:06 -08:00
|
|
|
if Universe.processActiveRaidSpawnerIterator == chunkId then
|
|
|
|
Universe.processActiveRaidSpawnerIterator = nil
|
2021-12-14 22:03:29 -08:00
|
|
|
end
|
2023-03-11 12:53:06 -08:00
|
|
|
if Universe.processMigrationIterator == chunkId then
|
|
|
|
Universe.processMigrationIterator = nil
|
2021-12-14 22:03:29 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-04 21:47:51 -07:00
|
|
|
--[[
|
|
|
|
1 2 3
|
2017-06-08 22:18:59 -07:00
|
|
|
\|/
|
2016-08-04 21:47:51 -07:00
|
|
|
4- -5
|
2017-06-08 22:18:59 -07:00
|
|
|
/|\
|
2016-08-04 21:47:51 -07:00
|
|
|
6 7 8
|
|
|
|
]]--
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.getNeighborChunks(map, x, y)
|
2017-11-20 23:27:03 -08:00
|
|
|
local chunkYRow1 = y - CHUNK_SIZE
|
|
|
|
local chunkYRow3 = y + CHUNK_SIZE
|
2018-01-13 21:48:21 -08:00
|
|
|
local xChunks = map[x-CHUNK_SIZE]
|
2017-05-27 21:50:37 -07:00
|
|
|
if xChunks then
|
2023-03-11 12:53:06 -08:00
|
|
|
NeighborChunks[1] = xChunks[chunkYRow1] or -1
|
|
|
|
NeighborChunks[4] = xChunks[y] or -1
|
|
|
|
NeighborChunks[6] = xChunks[chunkYRow3] or -1
|
2017-08-08 01:19:51 -07:00
|
|
|
else
|
2023-03-11 12:53:06 -08:00
|
|
|
NeighborChunks[1] = -1
|
|
|
|
NeighborChunks[4] = -1
|
|
|
|
NeighborChunks[6] = -1
|
2016-08-04 21:47:51 -07:00
|
|
|
end
|
2017-11-20 23:27:03 -08:00
|
|
|
|
2018-01-13 21:48:21 -08:00
|
|
|
xChunks = map[x+CHUNK_SIZE]
|
2017-05-27 21:50:37 -07:00
|
|
|
if xChunks then
|
2023-03-11 12:53:06 -08:00
|
|
|
NeighborChunks[3] = xChunks[chunkYRow1] or -1
|
|
|
|
NeighborChunks[5] = xChunks[y] or -1
|
|
|
|
NeighborChunks[8] = xChunks[chunkYRow3] or -1
|
2017-08-08 01:19:51 -07:00
|
|
|
else
|
2023-03-11 12:53:06 -08:00
|
|
|
NeighborChunks[3] = -1
|
|
|
|
NeighborChunks[5] = -1
|
|
|
|
NeighborChunks[8] = -1
|
2016-08-04 21:47:51 -07:00
|
|
|
end
|
2019-02-15 20:17:30 -08:00
|
|
|
|
2018-01-13 21:48:21 -08:00
|
|
|
xChunks = map[x]
|
2017-05-27 21:50:37 -07:00
|
|
|
if xChunks then
|
2023-03-11 12:53:06 -08:00
|
|
|
NeighborChunks[2] = xChunks[chunkYRow1] or -1
|
|
|
|
NeighborChunks[7] = xChunks[chunkYRow3] or -1
|
2017-08-08 01:19:51 -07:00
|
|
|
else
|
2023-03-11 12:53:06 -08:00
|
|
|
NeighborChunks[2] = -1
|
|
|
|
NeighborChunks[7] = -1
|
2016-08-17 22:55:08 -07:00
|
|
|
end
|
2023-03-11 12:53:06 -08:00
|
|
|
return NeighborChunks
|
2016-08-17 22:55:08 -07:00
|
|
|
end
|
|
|
|
|
2018-09-23 21:56:45 -07:00
|
|
|
|
|
|
|
--[[
|
|
|
|
1 2 3
|
|
|
|
\|/
|
|
|
|
4- -5
|
|
|
|
/|\
|
|
|
|
6 7 8
|
|
|
|
]]--
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.canMoveChunkDirection(map, direction, startChunk, endChunk)
|
2016-10-30 20:24:14 -07:00
|
|
|
local canMove = false
|
2023-03-11 17:21:22 -08:00
|
|
|
local startPassable = getPassable(startChunk)
|
|
|
|
local endPassable = getPassable(endChunk)
|
2018-09-23 21:56:45 -07:00
|
|
|
if (startPassable == CHUNK_ALL_DIRECTIONS) then
|
2019-10-13 22:49:52 -07:00
|
|
|
if ((direction == 1) or (direction == 3) or (direction == 6) or (direction == 8)) then
|
|
|
|
canMove = (endPassable == CHUNK_ALL_DIRECTIONS)
|
|
|
|
elseif (direction == 2) or (direction == 7) then
|
|
|
|
canMove = ((endPassable == CHUNK_NORTH_SOUTH) or (endPassable == CHUNK_ALL_DIRECTIONS))
|
|
|
|
elseif (direction == 4) or (direction == 5) then
|
|
|
|
canMove = ((endPassable == CHUNK_EAST_WEST) or (endPassable == CHUNK_ALL_DIRECTIONS))
|
|
|
|
end
|
2018-09-23 21:56:45 -07:00
|
|
|
elseif (startPassable == CHUNK_NORTH_SOUTH) then
|
2019-10-13 22:49:52 -07:00
|
|
|
if ((direction == 1) or (direction == 3) or (direction == 6) or (direction == 8)) then
|
|
|
|
canMove = (endPassable == CHUNK_ALL_DIRECTIONS)
|
|
|
|
elseif (direction == 2) or (direction == 7) then
|
|
|
|
canMove = ((endPassable == CHUNK_NORTH_SOUTH) or (endPassable == CHUNK_ALL_DIRECTIONS))
|
|
|
|
end
|
2018-09-23 21:56:45 -07:00
|
|
|
elseif (startPassable == CHUNK_EAST_WEST) then
|
2019-10-13 22:49:52 -07:00
|
|
|
if ((direction == 1) or (direction == 3) or (direction == 6) or (direction == 8)) then
|
|
|
|
canMove = (endPassable == CHUNK_ALL_DIRECTIONS)
|
|
|
|
elseif (direction == 4) or (direction == 5) then
|
|
|
|
canMove = ((endPassable == CHUNK_EAST_WEST) or (endPassable == CHUNK_ALL_DIRECTIONS))
|
|
|
|
end
|
2018-10-19 18:53:05 -07:00
|
|
|
else
|
2019-10-13 22:49:52 -07:00
|
|
|
canMove = (endPassable ~= CHUNK_IMPASSABLE)
|
2016-10-30 20:24:14 -07:00
|
|
|
end
|
|
|
|
return canMove
|
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.getCardinalChunks(map, x, y)
|
|
|
|
local neighbors = Universe.cardinalNeighbors
|
2018-01-13 21:48:21 -08:00
|
|
|
local xChunks = map[x]
|
2017-05-27 21:50:37 -07:00
|
|
|
if xChunks then
|
2020-05-15 13:51:38 -07:00
|
|
|
neighbors[1] = xChunks[y-CHUNK_SIZE] or -1
|
|
|
|
neighbors[4] = xChunks[y+CHUNK_SIZE] or -1
|
2017-08-08 01:19:51 -07:00
|
|
|
else
|
2020-05-15 13:51:38 -07:00
|
|
|
neighbors[1] = -1
|
|
|
|
neighbors[4] = -1
|
2016-08-17 22:55:08 -07:00
|
|
|
end
|
2019-02-15 20:17:30 -08:00
|
|
|
|
2018-01-13 21:48:21 -08:00
|
|
|
xChunks = map[x-CHUNK_SIZE]
|
2017-05-27 21:50:37 -07:00
|
|
|
if xChunks then
|
2020-05-15 13:51:38 -07:00
|
|
|
neighbors[2] = xChunks[y] or -1
|
2017-08-08 01:19:51 -07:00
|
|
|
else
|
2020-05-15 13:51:38 -07:00
|
|
|
neighbors[2] = -1
|
2016-08-17 22:55:08 -07:00
|
|
|
end
|
2019-02-15 20:17:30 -08:00
|
|
|
|
2018-01-13 21:48:21 -08:00
|
|
|
xChunks = map[x+CHUNK_SIZE]
|
2017-05-27 21:50:37 -07:00
|
|
|
if xChunks then
|
2020-05-15 13:51:38 -07:00
|
|
|
neighbors[3] = xChunks[y] or -1
|
2017-08-08 01:19:51 -07:00
|
|
|
else
|
2020-05-15 13:51:38 -07:00
|
|
|
neighbors[3] = -1
|
2016-08-17 22:55:08 -07:00
|
|
|
end
|
2016-08-19 19:52:27 -07:00
|
|
|
return neighbors
|
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.positionFromDirectionAndChunk(direction, startPosition, scaling)
|
2021-12-11 10:42:49 -08:00
|
|
|
local endPosition = {}
|
2016-08-19 19:52:27 -07:00
|
|
|
if (direction == 1) then
|
2019-10-13 22:49:52 -07:00
|
|
|
endPosition.x = startPosition.x - CHUNK_SIZE * (scaling - 0.1)
|
|
|
|
endPosition.y = startPosition.y - CHUNK_SIZE * (scaling - 0.1)
|
2016-08-19 19:52:27 -07:00
|
|
|
elseif (direction == 2) then
|
2019-10-13 22:49:52 -07:00
|
|
|
endPosition.x = startPosition.x
|
|
|
|
endPosition.y = startPosition.y - CHUNK_SIZE * (scaling + 0.25)
|
2016-08-19 19:52:27 -07:00
|
|
|
elseif (direction == 3) then
|
2019-10-13 22:49:52 -07:00
|
|
|
endPosition.x = startPosition.x + CHUNK_SIZE * (scaling - 0.1)
|
|
|
|
endPosition.y = startPosition.y - CHUNK_SIZE * (scaling - 0.1)
|
2016-08-19 19:52:27 -07:00
|
|
|
elseif (direction == 4) then
|
2019-10-13 22:49:52 -07:00
|
|
|
endPosition.x = startPosition.x - CHUNK_SIZE * (scaling + 0.25)
|
|
|
|
endPosition.y = startPosition.y
|
2016-08-19 19:52:27 -07:00
|
|
|
elseif (direction == 5) then
|
2019-10-13 22:49:52 -07:00
|
|
|
endPosition.x = startPosition.x + CHUNK_SIZE * (scaling + 0.25)
|
|
|
|
endPosition.y = startPosition.y
|
2016-08-19 19:52:27 -07:00
|
|
|
elseif (direction == 6) then
|
2019-10-13 22:49:52 -07:00
|
|
|
endPosition.x = startPosition.x - CHUNK_SIZE * (scaling - 0.1)
|
|
|
|
endPosition.y = startPosition.y + CHUNK_SIZE * (scaling - 0.1)
|
2016-08-19 19:52:27 -07:00
|
|
|
elseif (direction == 7) then
|
2019-10-13 22:49:52 -07:00
|
|
|
endPosition.x = startPosition.x
|
|
|
|
endPosition.y = startPosition.y + CHUNK_SIZE * (scaling + 0.25)
|
2016-08-19 19:52:27 -07:00
|
|
|
elseif (direction == 8) then
|
2019-10-13 22:49:52 -07:00
|
|
|
endPosition.x = startPosition.x + CHUNK_SIZE * (scaling - 0.1)
|
|
|
|
endPosition.y = startPosition.y + CHUNK_SIZE * (scaling - 0.1)
|
2016-08-19 19:52:27 -07:00
|
|
|
end
|
2017-12-31 11:12:40 -08:00
|
|
|
return endPosition
|
2016-08-04 21:47:51 -07:00
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.positionFromDirectionAndFlat(direction, startPosition, multipler)
|
2019-10-13 22:49:52 -07:00
|
|
|
local lx = startPosition.x
|
|
|
|
local ly = startPosition.y
|
2021-12-10 13:23:52 -08:00
|
|
|
if not multipler then
|
|
|
|
multipler = 1
|
|
|
|
end
|
2019-10-13 22:49:52 -07:00
|
|
|
if (direction == 1) then
|
2021-12-10 13:23:52 -08:00
|
|
|
lx = lx - CHUNK_SIZE * multipler
|
|
|
|
ly = ly - CHUNK_SIZE * multipler
|
2019-10-13 22:49:52 -07:00
|
|
|
elseif (direction == 2) then
|
2021-12-10 13:23:52 -08:00
|
|
|
ly = ly - CHUNK_SIZE * multipler
|
2019-10-13 22:49:52 -07:00
|
|
|
elseif (direction == 3) then
|
2021-12-10 13:23:52 -08:00
|
|
|
lx = lx + CHUNK_SIZE * multipler
|
|
|
|
ly = ly - CHUNK_SIZE * multipler
|
2019-10-13 22:49:52 -07:00
|
|
|
elseif (direction == 4) then
|
2021-12-10 13:23:52 -08:00
|
|
|
lx = lx - CHUNK_SIZE * multipler
|
2019-10-13 22:49:52 -07:00
|
|
|
elseif (direction == 5) then
|
2021-12-10 13:23:52 -08:00
|
|
|
lx = lx + CHUNK_SIZE * multipler
|
2019-10-13 22:49:52 -07:00
|
|
|
elseif (direction == 6) then
|
2021-12-10 13:23:52 -08:00
|
|
|
lx = lx - CHUNK_SIZE * multipler
|
|
|
|
ly = ly + CHUNK_SIZE * multipler
|
2019-10-13 22:49:52 -07:00
|
|
|
elseif (direction == 7) then
|
2021-12-10 13:23:52 -08:00
|
|
|
ly = ly + CHUNK_SIZE * multipler
|
2019-10-13 22:49:52 -07:00
|
|
|
elseif (direction == 8) then
|
2021-12-10 13:23:52 -08:00
|
|
|
lx = lx + CHUNK_SIZE * multipler
|
|
|
|
ly = ly + CHUNK_SIZE * multipler
|
2019-10-13 22:49:52 -07:00
|
|
|
end
|
2021-12-11 10:42:49 -08:00
|
|
|
return {
|
|
|
|
x = lx,
|
|
|
|
y = ly
|
|
|
|
}
|
2019-10-13 22:49:52 -07:00
|
|
|
end
|
|
|
|
|
2023-03-11 20:51:13 -08:00
|
|
|
function MapUtils.victoryScent(chunk, entityType)
|
|
|
|
local value = VICTORY_SCENT[entityType]
|
|
|
|
if value then
|
|
|
|
addVictoryGenerator(chunk, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MapUtils.disperseVictoryScent()
|
|
|
|
local chunkToVictory = Universe.chunkToVictory
|
|
|
|
local chunkId, pheromonePack = next(chunkToVictory, nil)
|
|
|
|
if not chunkId then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
chunkToVictory[chunkId] = nil
|
|
|
|
local chunk = pheromonePack.chunk
|
|
|
|
local map = chunk.map
|
|
|
|
if not map.surface.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local chunkX = chunk.x
|
|
|
|
local chunkY = chunk.y
|
|
|
|
local i = 1
|
|
|
|
for x=chunkX - VICTORY_SCENT_BOUND, chunkX + VICTORY_SCENT_BOUND,32 do
|
|
|
|
for y = chunkY - VICTORY_SCENT_BOUND, chunkY + VICTORY_SCENT_BOUND,32 do
|
|
|
|
local c = MapUtils.getChunkByXY(map, x, y)
|
|
|
|
if (c ~= -1) then
|
|
|
|
local amount = pheromonePack.v * VICTORY_SCENT_MULTIPLER[i]
|
|
|
|
addDeathGenerator(c, amount)
|
|
|
|
addPermanentDeathGenerator(c, amount)
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MapUtils.deathScent(chunk, structure)
|
|
|
|
local amount = -DEATH_PHEROMONE_GENERATOR_AMOUNT
|
|
|
|
if structure then
|
|
|
|
amount = -TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT
|
|
|
|
end
|
|
|
|
addDeathGenerator(chunk, amount)
|
|
|
|
addPermanentDeathGenerator(chunk, amount)
|
|
|
|
end
|
|
|
|
|
|
|
|
function MapUtils.processPheromone(map, chunk, tick, player)
|
|
|
|
if chunk[CHUNK_TICK] > tick then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
chunk[CHUNK_TICK] = tick
|
|
|
|
|
|
|
|
local chunkPlayer = chunk[PLAYER_PHEROMONE]
|
|
|
|
local chunkBase = -MAGIC_MAXIMUM_NUMBER
|
|
|
|
local chunkDeath = getCombinedDeathGenerator(chunk)
|
|
|
|
local chunkResource = -MAGIC_MAXIMUM_NUMBER
|
|
|
|
local chunkEnemy = chunk[ENEMY_PHEROMONE]
|
|
|
|
|
|
|
|
local chunkCount = 1
|
|
|
|
|
|
|
|
local enemyStructureCount = getEnemyStructureCount(chunk)
|
|
|
|
|
|
|
|
local tempNeighbors = MapUtils.getNeighborChunks(map, chunk.x, chunk.y)
|
|
|
|
for i=1,8 do
|
|
|
|
local tempPheromone
|
|
|
|
local neighbor = tempNeighbors[i]
|
|
|
|
if (neighbor ~= -1) then
|
|
|
|
if MapUtils.canMoveChunkDirection(map, i, chunk, neighbor) then
|
|
|
|
chunkCount = chunkCount + 1
|
|
|
|
chunkPlayer = chunkPlayer + neighbor[PLAYER_PHEROMONE]
|
|
|
|
chunkEnemy = chunkEnemy + neighbor[ENEMY_PHEROMONE]
|
|
|
|
chunkDeath = chunkDeath + getCombinedDeathGenerator(neighbor)
|
|
|
|
tempPheromone = neighbor[BASE_PHEROMONE]
|
|
|
|
if chunkBase < tempPheromone then
|
|
|
|
chunkBase = tempPheromone
|
|
|
|
end
|
|
|
|
tempPheromone = neighbor[RESOURCE_PHEROMONE]
|
|
|
|
if chunkResource < tempPheromone then
|
|
|
|
chunkResource = tempPheromone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setDeathGenerator(chunk, (chunkDeath / chunkCount) * 0.75)
|
|
|
|
|
|
|
|
if not player then
|
|
|
|
decayDeathGenerator(chunk)
|
|
|
|
end
|
|
|
|
|
|
|
|
decayPlayerGenerator(chunk)
|
|
|
|
|
|
|
|
local chunkDeathRating = getCombinedDeathGeneratorRating(chunk) * getPathRating(chunk)
|
|
|
|
|
|
|
|
chunk[PLAYER_PHEROMONE] = chunkDeathRating * mMax(
|
|
|
|
chunk.playerGenerator or 0,
|
|
|
|
(chunkPlayer / chunkCount) * 0.98
|
|
|
|
)
|
|
|
|
|
|
|
|
chunk[BASE_PHEROMONE] = chunkDeathRating * mMax(
|
|
|
|
chunk.playerBaseGenerator or 0,
|
|
|
|
chunkBase * 0.9
|
|
|
|
)
|
|
|
|
|
|
|
|
chunk[ENEMY_PHEROMONE] = chunkDeathRating * mMax(
|
|
|
|
enemyStructureCount * ENEMY_PHEROMONE_MULTIPLER,
|
|
|
|
(chunkEnemy / chunkCount) * 0.9
|
|
|
|
)
|
|
|
|
|
|
|
|
local resourcePheromoneGenerator = chunk.resourceGenerator or 0
|
|
|
|
if (resourcePheromoneGenerator > 0) then
|
|
|
|
chunkResource = linearInterpolation(resourcePheromoneGenerator, 15000, 20000)
|
|
|
|
end
|
|
|
|
if enemyStructureCount ~= 0 then
|
|
|
|
chunkResource = chunkResource * 0.0001
|
|
|
|
end
|
|
|
|
chunk[RESOURCE_PHEROMONE] = chunkDeathRating * chunkResource * 0.9
|
|
|
|
end
|
|
|
|
|
2023-03-11 12:53:06 -08:00
|
|
|
function MapUtils.init(universe)
|
|
|
|
Universe = universe
|
|
|
|
NeighborChunks = universe.neighbors
|
|
|
|
end
|
|
|
|
|
|
|
|
MapUtilsG = MapUtils
|
|
|
|
return MapUtils
|