1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-03 22:52:20 +02:00
Rampant/libs/MapUtils.lua

177 lines
5.7 KiB
Lua
Raw Normal View History

local mapUtils = {}
-- imports
local constants = require("Constants")
-- constants
2017-06-10 10:38:20 +02:00
local CHUNK_NORTH_SOUTH = constants.CHUNK_NORTH_SOUTH
local CHUNK_EAST_WEST = constants.CHUNK_EAST_WEST
local CHUNK_ALL_DIRECTIONS = constants.CHUNK_ALL_DIRECTIONS
local PASSABLE = constants.PASSABLE
local CHUNK_SIZE = constants.CHUNK_SIZE
2017-11-21 09:27:03 +02:00
local SENTINEL_IMPASSABLE_CHUNK = constants.SENTINEL_IMPASSABLE_CHUNK
local CHUNK_SIZE_DIVIDER = constants.CHUNK_SIZE_DIVIDER
-- imported functions
local mFloor = math.floor
-- module code
2018-01-14 07:48:21 +02:00
function mapUtils.getChunkByXY(map, x, y)
local chunkX = map[x]
2017-08-08 10:19:51 +02:00
if chunkX then
2017-11-21 09:27:03 +02:00
return chunkX[y] or SENTINEL_IMPASSABLE_CHUNK
end
2017-11-21 09:27:03 +02:00
return SENTINEL_IMPASSABLE_CHUNK
end
2018-01-14 07:48:21 +02: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
return chunkX[chunkY] or SENTINEL_IMPASSABLE_CHUNK
end
return SENTINEL_IMPASSABLE_CHUNK
end
2018-01-14 07:48:21 +02:00
function mapUtils.getChunkByUnalignedXY(map, x, y)
local chunkX = map[mFloor(x * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE]
if chunkX then
local chunkY = mFloor(y * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
return chunkX[chunkY] or SENTINEL_IMPASSABLE_CHUNK
end
return SENTINEL_IMPASSABLE_CHUNK
end
2017-11-21 09:27:03 +02: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
--[[
1 2 3
2017-06-09 07:18:59 +02:00
\|/
4- -5
2017-06-09 07:18:59 +02:00
/|\
6 7 8
]]--
2018-01-14 07:48:21 +02:00
function mapUtils.getNeighborChunks(map, x, y)
local neighbors = map.neighbors
2017-11-21 09:27:03 +02:00
local chunkYRow1 = y - CHUNK_SIZE
local chunkYRow3 = y + CHUNK_SIZE
2018-01-14 07:48:21 +02:00
local xChunks = map[x-CHUNK_SIZE]
2017-05-28 06:50:37 +02:00
if xChunks then
2017-11-21 09:27:03 +02:00
neighbors[1] = xChunks[chunkYRow1] or SENTINEL_IMPASSABLE_CHUNK
neighbors[4] = xChunks[y] or SENTINEL_IMPASSABLE_CHUNK
neighbors[6] = xChunks[chunkYRow3] or SENTINEL_IMPASSABLE_CHUNK
2017-08-08 10:19:51 +02:00
else
2017-11-21 09:27:03 +02:00
neighbors[1] = SENTINEL_IMPASSABLE_CHUNK
neighbors[4] = SENTINEL_IMPASSABLE_CHUNK
neighbors[6] = SENTINEL_IMPASSABLE_CHUNK
end
2017-11-21 09:27:03 +02:00
2018-01-14 07:48:21 +02:00
xChunks = map[x+CHUNK_SIZE]
2017-05-28 06:50:37 +02:00
if xChunks then
2017-11-21 09:27:03 +02:00
neighbors[3] = xChunks[chunkYRow1] or SENTINEL_IMPASSABLE_CHUNK
neighbors[5] = xChunks[y] or SENTINEL_IMPASSABLE_CHUNK
neighbors[8] = xChunks[chunkYRow3] or SENTINEL_IMPASSABLE_CHUNK
2017-08-08 10:19:51 +02:00
else
2017-11-21 09:27:03 +02:00
neighbors[3] = SENTINEL_IMPASSABLE_CHUNK
neighbors[5] = SENTINEL_IMPASSABLE_CHUNK
neighbors[8] = SENTINEL_IMPASSABLE_CHUNK
end
2018-01-14 07:48:21 +02:00
xChunks = map[x]
2017-05-28 06:50:37 +02:00
if xChunks then
2017-11-21 09:27:03 +02:00
neighbors[2] = xChunks[chunkYRow1] or SENTINEL_IMPASSABLE_CHUNK
neighbors[7] = xChunks[chunkYRow3] or SENTINEL_IMPASSABLE_CHUNK
2017-08-08 10:19:51 +02:00
else
2017-11-21 09:27:03 +02:00
neighbors[2] = SENTINEL_IMPASSABLE_CHUNK
neighbors[7] = SENTINEL_IMPASSABLE_CHUNK
end
return neighbors
end
2016-10-31 05:24:14 +02:00
function mapUtils.canMoveChunkDirection(direction, startChunk, endChunk)
local canMove = false
2017-06-10 10:38:20 +02:00
local startPassable = startChunk[PASSABLE]
local endPassable = endChunk[PASSABLE]
2017-11-21 09:27:03 +02:00
if (startPassable == CHUNK_ALL_DIRECTIONS) and (endPassable == CHUNK_ALL_DIRECTIONS) then
canMove = true
elseif ((direction == 2) or (direction == 7)) and (startPassable == CHUNK_NORTH_SOUTH) and (endPassable == CHUNK_NORTH_SOUTH) then
canMove = true
2017-06-10 10:38:20 +02:00
elseif ((direction == 4) or (direction == 5)) and (startPassable == CHUNK_EAST_WEST) and (endPassable == CHUNK_EAST_WEST) then
canMove = true
elseif (startChunk == SENTINEL_IMPASSABLE_CHUNK) and (endPassable == CHUNK_ALL_DIRECTIONS) then
2016-10-31 05:24:14 +02:00
canMove = true
end
return canMove
end
2018-01-14 07:48:21 +02:00
function mapUtils.getCardinalChunks(map, x, y)
local neighbors = map.cardinalNeighbors
local xChunks = map[x]
2017-05-28 06:50:37 +02:00
if xChunks then
2017-11-21 09:27:03 +02:00
neighbors[1] = xChunks[y-CHUNK_SIZE] or SENTINEL_IMPASSABLE_CHUNK
neighbors[4] = xChunks[y+CHUNK_SIZE] or SENTINEL_IMPASSABLE_CHUNK
2017-08-08 10:19:51 +02:00
else
2017-11-21 09:27:03 +02:00
neighbors[1] = SENTINEL_IMPASSABLE_CHUNK
neighbors[4] = SENTINEL_IMPASSABLE_CHUNK
end
2018-01-14 07:48:21 +02:00
xChunks = map[x-CHUNK_SIZE]
2017-05-28 06:50:37 +02:00
if xChunks then
2017-11-21 09:27:03 +02:00
neighbors[2] = xChunks[y] or SENTINEL_IMPASSABLE_CHUNK
2017-08-08 10:19:51 +02:00
else
2017-11-21 09:27:03 +02:00
neighbors[2] = SENTINEL_IMPASSABLE_CHUNK
end
2018-01-14 07:48:21 +02:00
xChunks = map[x+CHUNK_SIZE]
2017-05-28 06:50:37 +02:00
if xChunks then
2017-11-21 09:27:03 +02:00
neighbors[3] = xChunks[y] or SENTINEL_IMPASSABLE_CHUNK
2017-08-08 10:19:51 +02:00
else
2017-11-21 09:27:03 +02:00
neighbors[3] = SENTINEL_IMPASSABLE_CHUNK
end
return neighbors
end
function mapUtils.positionFromDirectionAndChunk(direction, startPosition, endPosition, scaling)
if (direction == 1) then
endPosition.x = startPosition.x - CHUNK_SIZE * scaling
endPosition.y = startPosition.y - CHUNK_SIZE * scaling
elseif (direction == 2) then
endPosition.x = startPosition.x
endPosition.y = startPosition.y - CHUNK_SIZE * scaling
elseif (direction == 3) then
endPosition.x = startPosition.x + CHUNK_SIZE * scaling
endPosition.y = startPosition.y - CHUNK_SIZE * scaling
elseif (direction == 4) then
endPosition.x = startPosition.x - CHUNK_SIZE * scaling
endPosition.y = startPosition.y
elseif (direction == 5) then
endPosition.x = startPosition.x + CHUNK_SIZE * scaling
endPosition.y = startPosition.y
elseif (direction == 6) then
endPosition.x = startPosition.x - CHUNK_SIZE * scaling
endPosition.y = startPosition.y + CHUNK_SIZE * scaling
elseif (direction == 7) then
endPosition.x = startPosition.x
endPosition.y = startPosition.y + CHUNK_SIZE * scaling
elseif (direction == 8) then
endPosition.x = startPosition.x + CHUNK_SIZE * scaling
endPosition.y = startPosition.y + CHUNK_SIZE * scaling
end
2017-12-31 21:12:40 +02:00
return endPosition
end
return mapUtils