Open-Terrarium/saving.lua

71 lines
1.8 KiB
Lua
Raw Normal View History

2017-07-12 05:25:10 -04:00
--how the map saves and loads
2017-07-12 04:34:18 -04:00
savechannel = love.thread.getChannel("save")
2017-07-12 05:25:10 -04:00
loadchannel = love.thread.getChannel("load")
2017-07-12 04:34:18 -04:00
dofile("tserial.lua")
--note about threads
--when the thread stops doing stuff, it crashed without
--debug info
while true do
2017-07-12 05:31:02 -04:00
--print("test")
2017-07-12 04:34:18 -04:00
if savechannel:getCount( ) > 0 then
--print("failure")
2017-07-12 04:34:18 -04:00
local x = savechannel:pop()
2017-07-12 05:25:10 -04:00
local doing = x[1]
if doing == "saving_old" then
--print("saving old chunks")
2017-07-12 05:25:10 -04:00
local max_chunks = x[2]
local chunkx = x[3]
local chunky = x[4]
local loaded_chunks = x[5]
--print(max_chunks,chunkx,chunky,loaded_chunks)
2017-07-12 05:25:10 -04:00
for xx = chunkx-max_chunks,chunkx+max_chunks do
for yy = chunky-max_chunks,chunky+max_chunks do
2017-07-12 05:25:10 -04:00
if loaded_chunks[xx] and loaded_chunks[xx][yy] then
love.filesystem.write( "/map/"..xx.."_"..yy..".txt", TSerial.pack(loaded_chunks[xx][yy]))
2017-07-17 03:43:28 -04:00
--print("saving:"..xx.."_"..yy)
2017-07-12 05:25:10 -04:00
end
end
2017-07-12 04:34:18 -04:00
end
2017-07-12 05:25:10 -04:00
elseif doing == "load_old" then
--print("load old")
2017-07-12 05:25:10 -04:00
local max_chunks = x[2]
local loaded_chunks = x[3]
local xx = x[4]
local yy = x[5]
2017-07-12 05:25:10 -04:00
--print("pushing")
--print(xx,yy)
local file = love.filesystem.read("/map/"..xx.."_"..yy..".txt")
--print("failure")
--print(file)
2017-07-12 05:25:10 -04:00
loadchannel:push{TSerial.unpack(file),xx,yy}
--print("succesfully loaded "..xx.."_"..yy)
2017-07-12 05:25:10 -04:00
--print("file doesn't exist")
elseif doing == "save_new" then
--print("saving new")
local data = x[2]
local xx = x[3]
local yy = x[4]
love.filesystem.write( "/map/"..xx.."_"..yy..".txt", TSerial.pack(data))
loadchannel:push{data,xx,yy}
2017-07-12 04:34:18 -04:00
end
end
end
--[[
for xx = -max_chunks,max_chunks do
for yy = -max_chunks,max_chunks do
love.filesystem.write( "/map/"..chunkx+xx.."_"..chunky+yy..".txt", TSerial.pack(loaded_chunks[xx][yy]))
print("saving:"..chunkx+xx.."_"..chunky+yy)
end
end
]]--