Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Terrain is now generated as needed in a separate thread
- Loading branch information
Showing
6 changed files
with
226 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
-- TSerial v1.2, a simple table serializer which turns tables into Lua script | |||
-- by Taehl (SelfMadeSpirit@gmail.com) | |||
|
|||
-- Usage: table = TSerial.unpack( TSerial.pack(table) ) | |||
TSerial = {} | |||
function TSerial.pack(t) | |||
assert(type(t) == "table", "Can only TSerial.pack tables.") | |||
if not t then return nil end | |||
local s = "{" | |||
for k, v in pairs(t) do | |||
local tk, tv = type(k), type(v) | |||
if tk == "boolean" then k = k and "[true]" or "[false]" | |||
elseif tk == "number" then k = "["..k.."]" | |||
elseif tk == "string" then -- no transform needed | |||
elseif tk == "table" then k = "["..TSerial.pack(k).."]" | |||
else error("Attempted to Tserialize a table with an invalid key: "..tostring(k)) | |||
end | |||
if tv == "boolean" then v = v and "true" or "false" | |||
elseif tv == "number" then -- no transform needed | |||
elseif tv == "string" then v = string.format("%q", v) | |||
elseif tv == "table" then v = TSerial.pack(v) | |||
else error("Attempted to Tserialize a table with an invalid value: "..tostring(v)) | |||
end | |||
s = s..k.."="..v.."," | |||
end | |||
return s.."}" | |||
end | |||
|
|||
function TSerial.unpack(s) | |||
assert(type(s) == "string", "Can only TSerial.unpack strings.") | |||
loadstring("TSerial.table="..s)() | |||
local t = TSerial.table | |||
TSerial.table = nil | |||
return t | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,45 @@ | |||
this_thread = love.thread.getThread() | |||
require("love.timer") | |||
require("love.filesystem") | |||
love.filesystem.load("TSerial.lua")() | |||
love.filesystem.load("chunk.lua")() | |||
|
|||
-- Block codes | |||
AIR = 0 | |||
STONE = 1 | |||
DIRT = 3 | |||
COAL_ORE = 16 | |||
UNGENERATED = 255 | |||
|
|||
rand = {mySeed = 1, lastN = -1} | |||
function rand:get(seed, n) | |||
if n <= 0 then n = -2 * n | |||
else n = 2 * n - 1 | |||
end | |||
|
|||
if seed ~= self.mySeed or self.lastN < 0 or n <= self.lastN then | |||
self.mySeed = seed | |||
math.randomseed(seed) | |||
self.lastN = -1 | |||
end | |||
while self.lastN < n do | |||
num = math.random() | |||
self.lastN = self.lastN + 1 | |||
end | |||
return num - 0.5 | |||
end | |||
|
|||
run = true | |||
|
|||
while run do | |||
this_thread:send("ready", true) | |||
commandMsg = this_thread:demand("command") | |||
if commandMsg == "quit" then run = false | |||
else | |||
this_thread:send("ready", false) | |||
local command = TSerial.unpack(commandMsg) | |||
chunk = Chunk:new() | |||
chunk:generate(command.seed, command.r, command.c) | |||
this_thread:send("chunk", TSerial.pack(chunk)) | |||
end | |||
end |
Oops, something went wrong.