1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-18 02:58:32 +02:00
Rampant/libs/MapUtils.lua

235 lines
7.3 KiB
Lua
Raw Normal View History

2019-02-15 20:17:30 -08:00
if mapUtilsG then
return mapUtilsG
end
local mapUtils = {}
-- imports
local constants = require("Constants")
2018-09-23 21:56:45 -07:00
local chunkPropertyUtils = require("ChunkPropertyUtils")
-- constants
2017-06-10 01:38:20 -07:00
local CHUNK_NORTH_SOUTH = constants.CHUNK_NORTH_SOUTH
local CHUNK_EAST_WEST = constants.CHUNK_EAST_WEST
local CHUNK_IMPASSABLE = constants.CHUNK_IMPASSABLE
2017-06-10 01:38:20 -07:00
local CHUNK_ALL_DIRECTIONS = constants.CHUNK_ALL_DIRECTIONS
local CHUNK_SIZE = constants.CHUNK_SIZE
2017-11-20 23:27:03 -08:00
local CHUNK_SIZE_DIVIDER = constants.CHUNK_SIZE_DIVIDER
-- imported functions
local mFloor = math.floor
2018-09-23 21:56:45 -07:00
local getPassable = chunkPropertyUtils.getPassable
-- module code
2018-01-13 21:48:21 -08:00
function mapUtils.getChunkByXY(map, x, y)
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
end
2020-05-15 13:51:38 -07:00
return -1
end
2018-01-13 21:48:21 -08:00
function mapUtils.getChunkByPosition(map, position)
local chunkX = map[mFloor(position.x * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE]
if chunkX then
local chunkY = mFloor(position.y * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
2020-05-15 13:51:38 -07:00
return chunkX[chunkY] or -1
end
2020-05-15 13:51:38 -07:00
return -1
end
function mapUtils.getChunkById(map, chunkId)
return map.chunkIdToChunk[chunkId] or -1
end
2017-11-20 23:27:03 -08:00
function mapUtils.positionToChunkXY(position)
local chunkX = mFloor(position.x * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
local chunkY = mFloor(position.y * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
return chunkX, chunkY
end
2021-11-25 22:49:28 -08:00
function mapUtils.queueGeneratedChunk(universe, event)
local map = universe.maps[event.surface.index]
if not map then
return
end
event.tick = (event.tick or game.tick) + 20
event.id = map.eventId
map.pendingChunks[event.id] = event
map.eventId = map.eventId + 1
2021-11-25 22:49:28 -08:00
end
--[[
1 2 3
2017-06-08 22:18:59 -07:00
\|/
4- -5
2017-06-08 22:18:59 -07:00
/|\
6 7 8
]]--
2018-01-13 21:48:21 -08:00
function mapUtils.getNeighborChunks(map, x, y)
2021-02-19 23:31:36 -08:00
local neighbors = map.universe.neighbors
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
2020-05-15 13:51:38 -07:00
neighbors[1] = xChunks[chunkYRow1] or -1
neighbors[4] = xChunks[y] or -1
neighbors[6] = xChunks[chunkYRow3] 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
neighbors[6] = -1
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
2020-05-15 13:51:38 -07:00
neighbors[3] = xChunks[chunkYRow1] or -1
neighbors[5] = xChunks[y] or -1
neighbors[8] = xChunks[chunkYRow3] or -1
2017-08-08 01:19:51 -07:00
else
2020-05-15 13:51:38 -07:00
neighbors[3] = -1
neighbors[5] = -1
neighbors[8] = -1
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
2020-05-15 13:51:38 -07:00
neighbors[2] = xChunks[chunkYRow1] or -1
neighbors[7] = xChunks[chunkYRow3] or -1
2017-08-08 01:19:51 -07:00
else
2020-05-15 13:51:38 -07:00
neighbors[2] = -1
neighbors[7] = -1
end
return neighbors
end
2018-09-23 21:56:45 -07:00
--[[
1 2 3
\|/
4- -5
/|\
6 7 8
]]--
function mapUtils.canMoveChunkDirection(map, direction, startChunk, endChunk)
2016-10-30 20:24:14 -07:00
local canMove = false
2018-09-23 21:56:45 -07:00
local startPassable = getPassable(map, startChunk)
local endPassable = getPassable(map, endChunk)
if (startPassable == CHUNK_ALL_DIRECTIONS) then
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
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
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
else
canMove = (endPassable ~= CHUNK_IMPASSABLE)
2016-10-30 20:24:14 -07:00
end
return canMove
end
2018-01-13 21:48:21 -08:00
function mapUtils.getCardinalChunks(map, x, y)
2021-02-19 23:31:36 -08:00
local neighbors = map.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
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
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
end
return neighbors
end
function mapUtils.positionFromDirectionAndChunk(direction, startPosition, endPosition, scaling)
if (direction == 1) then
endPosition.x = startPosition.x - CHUNK_SIZE * (scaling - 0.1)
endPosition.y = startPosition.y - CHUNK_SIZE * (scaling - 0.1)
elseif (direction == 2) then
endPosition.x = startPosition.x
endPosition.y = startPosition.y - CHUNK_SIZE * (scaling + 0.25)
elseif (direction == 3) then
endPosition.x = startPosition.x + CHUNK_SIZE * (scaling - 0.1)
endPosition.y = startPosition.y - CHUNK_SIZE * (scaling - 0.1)
elseif (direction == 4) then
endPosition.x = startPosition.x - CHUNK_SIZE * (scaling + 0.25)
endPosition.y = startPosition.y
elseif (direction == 5) then
endPosition.x = startPosition.x + CHUNK_SIZE * (scaling + 0.25)
endPosition.y = startPosition.y
elseif (direction == 6) then
endPosition.x = startPosition.x - CHUNK_SIZE * (scaling - 0.1)
endPosition.y = startPosition.y + CHUNK_SIZE * (scaling - 0.1)
elseif (direction == 7) then
endPosition.x = startPosition.x
endPosition.y = startPosition.y + CHUNK_SIZE * (scaling + 0.25)
elseif (direction == 8) then
endPosition.x = startPosition.x + CHUNK_SIZE * (scaling - 0.1)
endPosition.y = startPosition.y + CHUNK_SIZE * (scaling - 0.1)
end
2017-12-31 11:12:40 -08:00
return endPosition
end
function mapUtils.positionFromDirectionAndFlat(direction, startPosition, endPosition)
local lx = startPosition.x
local ly = startPosition.y
if (direction == 1) then
lx = lx - CHUNK_SIZE
ly = ly - CHUNK_SIZE
elseif (direction == 2) then
ly = ly - CHUNK_SIZE
elseif (direction == 3) then
lx = lx + CHUNK_SIZE
ly = ly - CHUNK_SIZE
elseif (direction == 4) then
lx = lx - CHUNK_SIZE
elseif (direction == 5) then
lx = lx + CHUNK_SIZE
elseif (direction == 6) then
lx = lx - CHUNK_SIZE
ly = ly + CHUNK_SIZE
elseif (direction == 7) then
ly = ly + CHUNK_SIZE
elseif (direction == 8) then
lx = lx + CHUNK_SIZE
2021-02-13 20:49:54 -08:00
ly = ly + CHUNK_SIZE
end
endPosition.x = lx
endPosition.y = ly
end
2019-02-15 20:17:30 -08:00
mapUtilsG = mapUtils
return mapUtils