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

141 lines
4.0 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_IMPASSABLE = constants.CHUNK_IMPASSABLE
local CHUNK_SIZE = constants.CHUNK_SIZE
-- imported functions
local mFloor = math.floor
-- module code
function mapUtils.getChunkByPosition(regionMap, x, y)
local chunkX = regionMap[mFloor(x * 0.03125)]
if (chunkX ~= nil) then
return chunkX[mFloor(y * 0.03125)]
end
return nil
end
function mapUtils.getChunkByIndex(regionMap, x, y)
local chunkX = regionMap[x]
if (chunkX ~= nil) then
return chunkX[y]
end
return nil
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
]]--
2017-06-10 10:38:20 +02:00
function mapUtils.getNeighborChunks(regionMap, chunkX, chunkY)
local chunkYRow1 = chunkY - 1
local chunkYRow3 = chunkY + 1
2017-06-10 10:38:20 +02:00
local neighbors = regionMap.neighbors
local xChunks = regionMap[chunkX-1]
2017-05-28 06:50:37 +02:00
if xChunks then
neighbors[1] = xChunks[chunkYRow1]
neighbors[4] = xChunks[chunkY]
neighbors[6] = xChunks[chunkYRow3]
end
xChunks = regionMap[chunkX+1]
2017-05-28 06:50:37 +02:00
if xChunks then
neighbors[3] = xChunks[chunkYRow1]
neighbors[5] = xChunks[chunkY]
neighbors[8] = xChunks[chunkYRow3]
end
xChunks = regionMap[chunkX]
2017-05-28 06:50:37 +02:00
if xChunks then
neighbors[2] = xChunks[chunkYRow1]
neighbors[7] = xChunks[chunkYRow3]
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]
if ((direction == 2) or (direction == 7)) and (startPassable == CHUNK_NORTH_SOUTH) and (endPassable == CHUNK_NORTH_SOUTH) then
2016-10-31 05:24:14 +02:00
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
2016-10-31 05:24:14 +02:00
canMove = true
2017-06-10 10:38:20 +02:00
elseif (startPassable == CHUNK_ALL_DIRECTIONS) and (endPassable == CHUNK_ALL_DIRECTIONS) then
canMove = true
elseif (startPassable ~= CHUNK_IMPASSABLE) and (endPassable == CHUNK_ALL_DIRECTIONS) then
2016-10-31 05:24:14 +02:00
canMove = true
end
return canMove
end
2017-06-10 10:38:20 +02:00
function mapUtils.getCardinalChunks(regionMap, chunkX, chunkY)
local xChunks = regionMap[chunkX]
2017-06-10 10:38:20 +02:00
local neighbors = regionMap.cardinalNeighbors
2017-05-28 06:50:37 +02:00
if xChunks then
2016-10-31 05:24:14 +02:00
neighbors[1] = xChunks[chunkY-1]
neighbors[4] = xChunks[chunkY+1]
end
xChunks = regionMap[chunkX-1]
2017-05-28 06:50:37 +02:00
if xChunks then
2016-10-31 05:24:14 +02:00
neighbors[2] = xChunks[chunkY]
end
xChunks = regionMap[chunkX+1]
2017-05-28 06:50:37 +02:00
if xChunks then
2016-10-31 05:24:14 +02:00
neighbors[3] = xChunks[chunkY]
end
return neighbors
end
2017-06-08 02:57:24 +02:00
function mapUtils.positionFromDirectionAndChunk(direction, startPosition, position, scaling)
if (direction == 1) then
2017-06-08 02:57:24 +02:00
position.x = startPosition.x - CHUNK_SIZE * scaling
position.y = startPosition.y - CHUNK_SIZE * scaling
elseif (direction == 2) then
2016-10-31 05:24:14 +02:00
position.x = startPosition.x
2017-06-08 02:57:24 +02:00
position.y = startPosition.y - CHUNK_SIZE * scaling
elseif (direction == 3) then
2017-06-08 02:57:24 +02:00
position.x = startPosition.x + CHUNK_SIZE * scaling
position.y = startPosition.y - CHUNK_SIZE * scaling
elseif (direction == 4) then
2017-06-08 02:57:24 +02:00
position.x = startPosition.x - CHUNK_SIZE * scaling
2016-10-31 05:24:14 +02:00
position.y = startPosition.y
elseif (direction == 5) then
2017-06-08 02:57:24 +02:00
position.x = startPosition.x + CHUNK_SIZE * scaling
2016-10-31 05:24:14 +02:00
position.y = startPosition.y
elseif (direction == 6) then
2017-06-08 02:57:24 +02:00
position.x = startPosition.x - CHUNK_SIZE * scaling
position.y = startPosition.y + CHUNK_SIZE * scaling
elseif (direction == 7) then
2016-10-31 05:24:14 +02:00
position.x = startPosition.x
2017-06-08 02:57:24 +02:00
position.y = startPosition.y + CHUNK_SIZE * scaling
elseif (direction == 8) then
2017-06-08 02:57:24 +02:00
position.x = startPosition.x + CHUNK_SIZE * scaling
position.y = startPosition.y + CHUNK_SIZE * scaling
end
return position
end
return mapUtils