96 lines
2.9 KiB
Lua
96 lines
2.9 KiB
Lua
local regionAPI = require("region")
|
|
|
|
local mapMaker = {}
|
|
|
|
--utility functions
|
|
function mapMaker.Sqr(x) return x*x end
|
|
function mapMaker.Dist(x, y, i, j) return math.sqrt(mapMaker.Sqr(x - i) + mapMaker.Sqr(y - j)) end
|
|
|
|
--tile macros, mapped to the tilesheet "overworld.bmp"
|
|
mapMaker.blank = 0
|
|
mapMaker.water = 18 + 3 * 0
|
|
mapMaker.sand = 18 + 3 * 1
|
|
mapMaker.plains = 18 + 3 * 2
|
|
mapMaker.grass = 18 + 3 * 3
|
|
mapMaker.dirt = 18 + 3 * 4
|
|
|
|
--"edge" macros
|
|
mapMaker.edges = {}
|
|
mapMaker.edges.north = -16
|
|
mapMaker.edges.south = 16
|
|
mapMaker.edges.east = 1
|
|
mapMaker.edges.west = -1
|
|
|
|
--use these macros (mapped to "overworld.bmp" for now) to smooth the region's edges
|
|
function mapMaker.SmoothEdgesSimple(r)
|
|
--make and pad an array to use
|
|
local shiftArray = {}
|
|
for i = 1, regionAPI.GetWidth(r) do
|
|
shiftArray[i] = {}
|
|
for j = 1, regionAPI.GetHeight(r) do
|
|
shiftArray[i][j] = 0
|
|
end
|
|
end
|
|
|
|
--build the array
|
|
for i = 1, regionAPI.GetWidth(r) do
|
|
for j = 1, regionAPI.GetHeight(r) do
|
|
--if (not regionAPI edge) and (west tile < this tile), etc.
|
|
if i > 1 and regionAPI.GetTile(r, i - 1, j, 1) < regionAPI.GetTile(r, i, j, 1) then
|
|
shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.west
|
|
end
|
|
if j > 1 and regionAPI.GetTile(r, i, j - 1, 1) < regionAPI.GetTile(r, i, j, 1) then
|
|
shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.north
|
|
end
|
|
if i < regionAPI.GetWidth(r) and regionAPI.GetTile(r, i + 1, j, 1) < regionAPI.GetTile(r, i, j, 1) then
|
|
shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.east
|
|
end
|
|
if j < regionAPI.GetHeight(r) and regionAPI.GetTile(r, i, j + 1, 1) < regionAPI.GetTile(r, i, j, 1) then
|
|
shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.south
|
|
end
|
|
end
|
|
end
|
|
|
|
--finally apply this
|
|
for i = 1, regionAPI.GetWidth(r) do
|
|
for j = 1, regionAPI.GetHeight(r) do
|
|
if shiftArray[i][j] ~= 0 then
|
|
regionAPI.SetTile(r, i, j, 2, regionAPI.GetTile(r, i, j, 1) + shiftArray[i][j])
|
|
regionAPI.SetTile(r, i, j, 1, regionAPI.GetTile(r, i, j, 1) - 3)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
--custom generation systems here
|
|
function mapMaker.DebugIsland(r)
|
|
--basic distance check for each tile, placing an island around the world origin
|
|
for i = 1, regionAPI.GetWidth(r) do
|
|
for j = 1, regionAPI.GetHeight(r) do
|
|
local dist = mapMaker.Dist(0, 0, i + regionAPI.GetX(r) -1, j + regionAPI.GetY(r) -1)
|
|
if dist < 10 then
|
|
regionAPI.SetTile(r, i, j, 1, mapMaker.plains)
|
|
elseif dist < 12 then
|
|
regionAPI.SetTile(r, i, j, 1, mapMaker.sand)
|
|
else
|
|
regionAPI.SetTile(r, i, j, 1, mapMaker.water)
|
|
regionAPI.SetSolid(r, i, j, true)
|
|
end
|
|
end
|
|
end
|
|
|
|
--examples of the smoothing function NOT working correctly
|
|
--[[
|
|
for j = 1, regionAPI.GetHeight(r) do
|
|
regionAPI.SetTile(r, 3, j, 1, mapMaker.dirt)
|
|
regionAPI.SetTile(r, 4, j, 1, mapMaker.dirt)
|
|
|
|
regionAPI.SetTile(r, 10, j, 1, mapMaker.dirt)
|
|
end
|
|
--]]
|
|
|
|
--A generic edge system
|
|
mapMaker.SmoothEdgesSimple(r)
|
|
end
|
|
|
|
return mapMaker |