Refactored scripts, added a smoother for Debug Islan

I've added a simple edge-smoothing function to debug island's generator. It
doesn't handle all edge cases (pun intended), but the proof of concept is
sound. I just wish I could release this...

I've also added exception checks to region.cpp
This commit is contained in:
Kayne Ruse
2015-01-12 01:14:24 +11:00
parent cf1008f0d9
commit f13e8479e4
4 changed files with 101 additions and 30 deletions
+12
View File
@@ -42,18 +42,30 @@ Region::Region(Region const& rhs): x(rhs.x), y(rhs.y) {
} }
Region::type_t Region::SetTile(int x, int y, int z, type_t v) { Region::type_t Region::SetTile(int x, int y, int z, type_t v) {
if (x < 0 || y < 0 || z < 0 || x >= REGION_WIDTH || y >= REGION_HEIGHT || z >= REGION_DEPTH) {
throw(std::out_of_range("Region::SetTile() argument out of range"));
}
return tiles[x][y][z] = v; return tiles[x][y][z] = v;
} }
Region::type_t Region::GetTile(int x, int y, int z) { Region::type_t Region::GetTile(int x, int y, int z) {
if (x < 0 || y < 0 || z < 0 || x >= REGION_WIDTH || y >= REGION_HEIGHT || z >= REGION_DEPTH) {
throw(std::out_of_range("Region::GetTile() argument out of range"));
}
return tiles[x][y][z]; return tiles[x][y][z];
} }
bool Region::SetSolid(int x, int y, bool b) { bool Region::SetSolid(int x, int y, bool b) {
if (x < 0 || y < 0 || x >= REGION_WIDTH || y >= REGION_HEIGHT) {
throw(std::out_of_range("Region::SetSolid() argument out of range"));
}
return solid[x * REGION_WIDTH + y] = b; return solid[x * REGION_WIDTH + y] = b;
} }
bool Region::GetSolid(int x, int y) { bool Region::GetSolid(int x, int y) {
if (x < 0 || y < 0 || x >= REGION_WIDTH || y >= REGION_HEIGHT) {
throw(std::out_of_range("Region::GetSolid() argument out of range"));
}
return solid[x * REGION_WIDTH + y]; return solid[x * REGION_WIDTH + y];
} }
+77 -21
View File
@@ -1,39 +1,95 @@
local mapSystem = require "map_system" local Region = require("map_system").Region
local mapMaker = {} local mapMaker = {}
--utility functions --utility functions
function mapMaker.sqr(x) return x*x end 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 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" --tile macros, mapped to the tilesheet "overworld.bmp"
mapMaker.edges = {}
mapMaker.edges.north = -16
mapMaker.edges.south = 16
mapMaker.edges.east = 1
mapMaker.edges.west = -1
mapMaker.water = 18 + 3 * 0 mapMaker.water = 18 + 3 * 0
mapMaker.sand = 18 + 3 * 1 mapMaker.sand = 18 + 3 * 1
mapMaker.plains = 18 + 3 * 2 mapMaker.plains = 18 + 3 * 2
mapMaker.grass = 18 + 3 * 3 mapMaker.grass = 18 + 3 * 3
mapMaker.dirt = 18 + 3 * 4 mapMaker.dirt = 18 + 3 * 4
--custom generation systems here --"edge" macros
function mapMaker.debugIsland(region) mapMaker.edges = {}
for i = 1, mapSystem.Region.GetWidth(region) do mapMaker.edges.north = -16
for j = 1, mapSystem.Region.GetHeight(region) do mapMaker.edges.south = 16
local dist = mapMaker.dist(0, 0, i + mapSystem.Region.GetX(region) -1, j + mapSystem.Region.GetY(region) -1) mapMaker.edges.east = 1
if dist < 10 then mapMaker.edges.west = -1
mapSystem.Region.SetTile(region, i, j, 1, mapMaker.plains)
elseif dist < 12 then --use these macros (mapped to "overworld.bmp" for now) to smooth the region's edges
mapSystem.Region.SetTile(region, i, j, 1, mapMaker.sand) function mapMaker.SmoothEdgesSimple(r)
else --make and pad an array to use
mapSystem.Region.SetTile(region, i, j, 1, mapMaker.water) local shiftArray = {}
mapSystem.Region.SetSolid(region, i, j, true) for i = 1, Region.GetWidth(r) do
shiftArray[i] = {}
for j = 1, Region.GetHeight(r) do
shiftArray[i][j] = 0
end
end
--build the array
for i = 1, Region.GetWidth(r) do
for j = 1, Region.GetHeight(r) do
--if (not region edge) and (west tile < this tile), etc.
if i > 1 and Region.GetTile(r, i - 1, j, 1) < Region.GetTile(r, i, j, 1) then
shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.west
end
if j > 1 and Region.GetTile(r, i, j - 1, 1) < Region.GetTile(r, i, j, 1) then
shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.north
end
if i < Region.GetWidth(r) and Region.GetTile(r, i + 1, j, 1) < Region.GetTile(r, i, j, 1) then
shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.east
end
if j < Region.GetHeight(r) and Region.GetTile(r, i, j + 1, 1) < Region.GetTile(r, i, j, 1) then
shiftArray[i][j] = shiftArray[i][j] + mapMaker.edges.south
end
end
end
--finally apply this
for i = 1, Region.GetWidth(r) do
for j = 1, Region.GetHeight(r) do
if shiftArray[i][j] ~= 0 then
Region.SetTile(r, i, j, 2, Region.GetTile(r, i, j, 1) + shiftArray[i][j])
Region.SetTile(r, i, j, 1, Region.GetTile(r, i, j, 1) - 3)
end end
end end
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, Region.GetWidth(r) do
for j = 1, Region.GetHeight(r) do
local dist = mapMaker.Dist(0, 0, i + Region.GetX(r) -1, j + Region.GetY(r) -1)
if dist < 10 then
Region.SetTile(r, i, j, 1, mapMaker.plains)
elseif dist < 12 then
Region.SetTile(r, i, j, 1, mapMaker.sand)
else
Region.SetTile(r, i, j, 1, mapMaker.water)
Region.SetSolid(r, i, j, true)
end
end
end
--examples of the smoothing function NOT working correctly
--[[
for j = 1, Region.GetHeight(r) do
Region.SetTile(r, 3, j, 1, mapMaker.dirt)
Region.SetTile(r, 4, j, 1, mapMaker.dirt)
Region.SetTile(r, 10, j, 1, mapMaker.dirt)
end
--]]
--A generic edge system
mapMaker.SmoothEdgesSimple(r)
end
return mapMaker return mapMaker
+8 -4
View File
@@ -1,11 +1,15 @@
local Region = require("map_system").Region
local mapSaver = {} local mapSaver = {}
function mapSaver.Load(region)
function mapSaver.Load(r)
--empty --empty
print("map_saver.lua:mapSaver.Load(region)") io.write("map_saver:Load(", Region.GetX(r), ", ", Region.GetY(r), ")\n")
end end
function mapSaver.Save(region) function mapSaver.Save(r)
--empty --empty
print("map_saver.lua:mapSaver.Save(region)") io.write("map_saver:Save(", Region.GetX(r), ", ", Region.GetY(r), ")\n")
end end
--TODO: create a flexible saving & loading system --TODO: create a flexible saving & loading system
return mapSaver return mapSaver
+4 -5
View File
@@ -1,9 +1,8 @@
print("Lua script check") print("Lua script check")
mapSystem = require "map_system" mapMaker = require("map_maker")
mapMaker = require "map_maker" mapSaver = require("map_saver")
mapSaver = require "map_saver" roomSystem = require("room_system")
roomSystem = require "room_system"
local function dumpTable(t) local function dumpTable(t)
print(t) print(t)
@@ -14,6 +13,6 @@ end
--NOTE: room 0 is the first that the client asks for, therefore it must exist --NOTE: room 0 is the first that the client asks for, therefore it must exist
local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp") local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp")
roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.debugIsland, mapSaver.Save) roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save)
print("Finished the lua script") print("Finished the lua script")