diff --git a/collision.lua b/collision.lua index 573610e..fb1d401 100644 --- a/collision.lua +++ b/collision.lua @@ -44,14 +44,16 @@ function collision(oldposx,oldposy) --print(chunkerx, chunkery, "|", squarex,squarey) --print( loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]) --if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or ore[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then - if ore[loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]]["collide"] ~= false then - player.playery = oldposy - if r == 2 then - player.on_block = true - fall = false - end - if r == 1 then - player.on_block = false + if loaded_chunks[chunkerx] and loaded_chunks[chunkerx][chunkery] and loaded_chunks[chunkerx][chunkery][squarex] and loaded_chunks[chunkerx][chunkery][squarex][squarey] then + if ore[loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]]["collide"] ~= false then + player.playery = oldposy + if r == 2 then + player.on_block = true + fall = false + end + if r == 1 then + player.on_block = false + end end end end @@ -92,10 +94,12 @@ function collision(oldposx,oldposy) end --if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or ore[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then - if ore[loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]]["collide"] ~= false then - player.inertiax = 0 - player.playerx = oldposx - --print("stopping x inertia and pos") + if loaded_chunks[chunkerx] and loaded_chunks[chunkerx][chunkery] and loaded_chunks[chunkerx][chunkery][squarex] and loaded_chunks[chunkerx][chunkery][squarex][squarey] then + if ore[loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]]["collide"] ~= false then + player.inertiax = 0 + player.playerx = oldposx + --print("stopping x inertia and pos") + end end end end diff --git a/creating_map_save.lua b/creating_map_save.lua new file mode 100644 index 0000000..e1631ba --- /dev/null +++ b/creating_map_save.lua @@ -0,0 +1,8 @@ +savechannel = love.thread.getChannel("save") + +dofile("tserial.lua") + +while true do + if savechannel:getCount( ) > 0 then + love.filesystem.write( "/map/"..chunkx+xx.."_"..chunky+yy..".txt", TSerial.pack(tiles)) +end diff --git a/main.lua b/main.lua index b97513a..baa82d5 100644 --- a/main.lua +++ b/main.lua @@ -76,6 +76,8 @@ function love.quit( ) end function love.update(dt) + + maplib.load_chunks() fpsGraph:update(dt) memGraph:update(dt) -- Update our custom graph diff --git a/map.lua b/map.lua index 15d1e34..c441339 100644 --- a/map.lua +++ b/map.lua @@ -2,7 +2,8 @@ maplib = {} -chunkx,chunky = math.random(-1000,1000),math.random(-1000,-1000) +--chunkx,chunky = math.random(-1000,1000),math.random(-1000,-1000) +chunkx,chunky = 5,1 --tile size map_max = 25 @@ -88,14 +89,39 @@ savethread = love.thread.newThread("saving.lua") savechannel = love.thread.getChannel("save") +loadchannel = love.thread.getChannel("load") + savethread:start() --saves all memory data into file function maplib.save_chunks() --print("saving chunks") - savechannel:push{max_chunks,chunkx,chunky,loaded_chunks} + savechannel:push{"saving_old",max_chunks,chunkx,chunky,loaded_chunks} end --maplib.save_chunks() + + +--watches and tells game to load up map tiles +function maplib.load_chunks() + for xx = -max_chunks,max_chunks do + for yy = -max_chunks,max_chunks do + --tell thread to load chunk + local chunkeyy = loadchannel:pop() + local data,xxer,yyer + if chunkeyy then + data = chunkeyy[1] + xxer = chunkeyy[2] + yyer = chunkeyy[3] + end + if xxer == xx and yyer == yy and loaded_chunks[xx][yy] == nil then + loaded_chunks[xx][yy] = data + elseif loaded_chunks[xx][yy] == nil then + savechannel:push{"load_old",max_chunks,chunkx,chunky,loaded_chunks,xx,yy} + end + end + end +end + --generates ore function maplib.generate_ore(tiles) @@ -253,10 +279,6 @@ function maplib.createmap() --save love.filesystem.write( "/map/"..chunkx+xx.."_"..chunky+yy..".txt", TSerial.pack(tiles)) loaded_chunks[xx][yy] = tiles - else - - tiles = TSerial.unpack(love.filesystem.read("/map/"..chunkx+xx.."_"..chunky+yy..".txt")) - loaded_chunks[xx][yy] = tiles end end end @@ -309,7 +331,9 @@ function maplib.draw() if drawx >= -scale and drawx < screenwidth and drawy >= -scale and drawy < screenheight then - love.graphics.draw(texture_table[loaded_chunks[xx][-yy][x][y]["block"]], drawx,drawy,0, scale/16, scale/16) + if loaded_chunks[xx][-yy] and loaded_chunks[xx][-yy][x] and loaded_chunks[xx][-yy][x][y] then + love.graphics.draw(texture_table[loaded_chunks[xx][-yy][x][y]["block"]], drawx,drawy,0, scale/16, scale/16) + end end --love.graphics.print(,, ) --if x == math.floor(map_max / 2) and y == math.floor(map_max / 2) then @@ -362,124 +386,128 @@ function maplib.liquid_flow(dt) --if not after_table[xx][yy][x][y] then -- after_table[xx][yy][x][y] = {} --end - - local block = loaded_chunks[xx][-yy][x][y]["block"] - - --if block == 5 then - -- print(-yy-1) - --end - --downward flow - if ore[block]["prop"] == "liquid" and y + 1 <= map_max and loaded_chunks[xx][-yy][x][y+1]["block"] == 1 then - if not after_table[xx] then - after_table[xx] = {} - end - if not after_table[xx][-yy] then - after_table[xx][-yy] = {} - end - if not after_table[xx][-yy][x] then - after_table[xx][-yy][x] = {} - end - if not after_table[xx][-yy][x][y+1] then - after_table[xx][-yy][x][y+1]= {} - end + if loaded_chunks[xx][-yy] and loaded_chunks[xx][-yy][x] and loaded_chunks[xx][-yy][x][y] then + - after_table[xx][-yy][x][y+1]["block"] = block - after_table[xx][-yy][x][y+1]["down"] = "down" - --flow into new chunk -1 y - elseif ore[block]["prop"] == "liquid" and y + 1 > map_max and loaded_chunks[xx] and loaded_chunks[xx][-yy-1] and loaded_chunks[xx][-yy-1][x] and loaded_chunks[xx][-yy-1][x][1] and loaded_chunks[xx][-yy-1][x][1]["block"] == 1 then - --print("flow down") - --forward workaround for table sub elements that have not been created - if not after_table[xx] then - after_table[xx] = {} + local block = loaded_chunks[xx][-yy][x][y]["block"] + + --if block == 5 then + -- print(-yy-1) + --end + --downward flow + if ore[block]["prop"] == "liquid" and y + 1 <= map_max and loaded_chunks[xx][-yy][x][y+1]["block"] == 1 then + if not after_table[xx] then + after_table[xx] = {} + end + if not after_table[xx][-yy] then + after_table[xx][-yy] = {} + end + if not after_table[xx][-yy][x] then + after_table[xx][-yy][x] = {} + end + if not after_table[xx][-yy][x][y+1] then + after_table[xx][-yy][x][y+1]= {} + end + + after_table[xx][-yy][x][y+1]["block"] = block + after_table[xx][-yy][x][y+1]["down"] = "down" + --flow into new chunk -1 y + elseif ore[block]["prop"] == "liquid" and y + 1 > map_max and loaded_chunks[xx] and loaded_chunks[xx][-yy-1] and loaded_chunks[xx][-yy-1][x] and loaded_chunks[xx][-yy-1][x][1] and loaded_chunks[xx][-yy-1][x][1]["block"] == 1 then + --print("flow down") + --forward workaround for table sub elements that have not been created + if not after_table[xx] then + after_table[xx] = {} + end + if not after_table[xx][-yy-1] then + after_table[xx][-yy-1] = {} + end + if not after_table[xx][-yy-1][x] then + after_table[xx][-yy-1][x] = {} + end + if not after_table[xx][-yy-1][x][1] then + --print("create new block element success") + after_table[xx][-yy-1][x][1]= {} + end + after_table[xx][-yy-1][x][1]["block"] = block + after_table[xx][-yy-1][x][1]["newy"] = "newy" end - if not after_table[xx][-yy-1] then - after_table[xx][-yy-1] = {} + --rightward flow + if ore[block]["prop"] == "liquid" and x + 1 <= map_max and loaded_chunks[xx][-yy][x+1][y]["block"] == 1 then + if not after_table[xx] then + after_table[xx] = {} + end + if not after_table[xx][-yy] then + after_table[xx][-yy] = {} + end + if not after_table[xx][-yy][x+1] then + after_table[xx][-yy][x+1] = {} + end + if not after_table[xx][-yy][x+1][y] then + after_table[xx][-yy][x+1][y]= {} + end + after_table[xx][-yy][x+1][y]["block"] = block + after_table[xx][-yy][x+1][y]["right"] = "right" + --flow into new chunk +1 x + elseif ore[block]["prop"] == "liquid" and x == map_max and loaded_chunks[xx+1] and loaded_chunks[xx+1][-yy] and loaded_chunks[xx+1][-yy][1] and loaded_chunks[xx+1][-yy][1][y] and loaded_chunks[xx+1][-yy][1][y]["block"] == 1 then + --hack to create new table sub element + if not after_table[xx+1] then + after_table[xx+1] = {} + end + if not after_table[xx+1][-yy] then + after_table[xx+1][-yy] = {} + end + if not after_table[xx+1][-yy][1] then + after_table[xx+1][-yy][1] = {} + end + if not after_table[xx+1][-yy][1][y] then + --print("create new block element success") + after_table[xx+1][-yy][1][y]= {} + end + --print(after_table[xx+1][-yy][1][y]["block"]) + after_table[xx+1][-yy][1][y]["block"] = block + after_table[xx+1][-yy][1][y]["xright"] = "xright" end - if not after_table[xx][-yy-1][x] then - after_table[xx][-yy-1][x] = {} + --print(xx-1) + --leftward flow + if ore[block]["prop"] == "liquid" and x - 1 >= 1 and loaded_chunks[xx][-yy][x-1][y]["block"] == 1 then + if not after_table[xx] then + after_table[xx] = {} + end + if not after_table[xx][-yy] then + after_table[xx][-yy] = {} + end + if not after_table[xx][-yy][x-1] then + after_table[xx][-yy][x-1] = {} + end + if not after_table[xx][-yy][x-1][y] then + after_table[xx][-yy][x-1][y]= {} + end + after_table[xx][-yy][x-1][y]["block"] = block + after_table[xx][-yy][x-1][y]["left"] = "left" + --flow into new chunk -1 x + elseif ore[block]["prop"] == "liquid" and x == 1 and loaded_chunks[xx-1] and loaded_chunks[xx-1][-yy] and loaded_chunks[xx-1][-yy][map_max] and loaded_chunks[xx-1][-yy][map_max][y] and loaded_chunks[xx-1][-yy][map_max][y]["block"] == 1 then + --print("Test") + --hack to create new table sub element + if not after_table[xx-1] then + after_table[xx-1] = {} + end + if not after_table[xx-1][-yy] then + after_table[xx-1][-yy] = {} + end + if not after_table[xx-1][-yy][map_max] then + after_table[xx-1][-yy][map_max] = {} + end + if not after_table[xx-1][-yy][map_max][y] then + --print("create new block element success") + after_table[xx-1][-yy][map_max][y]= {} + end + --print(after_table[xx-1][-yy][map_max][y]["block"]) + after_table[xx-1][-yy][map_max][y]["block"] = block + after_table[xx-1][-yy][map_max][y]["xleft"] = "xleft" end - if not after_table[xx][-yy-1][x][1] then - --print("create new block element success") - after_table[xx][-yy-1][x][1]= {} - end - after_table[xx][-yy-1][x][1]["block"] = block - after_table[xx][-yy-1][x][1]["newy"] = "newy" + --love.graphics.draw(texture_table[loaded_chunks[xx][-yy][x][y]["block"]], (((x*scale)-(player.playerx*scale))+((scale*map_max)/2))+(map_max*scale*xx)+offsetx, (((y*scale)-(player.playery*scale))+((scale*map_max)/2))+(map_max*scale*yy)+offsety-4,0, scale/16, scale/16) + end - --rightward flow - if ore[block]["prop"] == "liquid" and x + 1 <= map_max and loaded_chunks[xx][-yy][x+1][y]["block"] == 1 then - if not after_table[xx] then - after_table[xx] = {} - end - if not after_table[xx][-yy] then - after_table[xx][-yy] = {} - end - if not after_table[xx][-yy][x+1] then - after_table[xx][-yy][x+1] = {} - end - if not after_table[xx][-yy][x+1][y] then - after_table[xx][-yy][x+1][y]= {} - end - after_table[xx][-yy][x+1][y]["block"] = block - after_table[xx][-yy][x+1][y]["right"] = "right" - --flow into new chunk +1 x - elseif ore[block]["prop"] == "liquid" and x == map_max and loaded_chunks[xx+1] and loaded_chunks[xx+1][-yy] and loaded_chunks[xx+1][-yy][1] and loaded_chunks[xx+1][-yy][1][y] and loaded_chunks[xx+1][-yy][1][y]["block"] == 1 then - --hack to create new table sub element - if not after_table[xx+1] then - after_table[xx+1] = {} - end - if not after_table[xx+1][-yy] then - after_table[xx+1][-yy] = {} - end - if not after_table[xx+1][-yy][1] then - after_table[xx+1][-yy][1] = {} - end - if not after_table[xx+1][-yy][1][y] then - --print("create new block element success") - after_table[xx+1][-yy][1][y]= {} - end - --print(after_table[xx+1][-yy][1][y]["block"]) - after_table[xx+1][-yy][1][y]["block"] = block - after_table[xx+1][-yy][1][y]["xright"] = "xright" - end - --print(xx-1) - --leftward flow - if ore[block]["prop"] == "liquid" and x - 1 >= 1 and loaded_chunks[xx][-yy][x-1][y]["block"] == 1 then - if not after_table[xx] then - after_table[xx] = {} - end - if not after_table[xx][-yy] then - after_table[xx][-yy] = {} - end - if not after_table[xx][-yy][x-1] then - after_table[xx][-yy][x-1] = {} - end - if not after_table[xx][-yy][x-1][y] then - after_table[xx][-yy][x-1][y]= {} - end - after_table[xx][-yy][x-1][y]["block"] = block - after_table[xx][-yy][x-1][y]["left"] = "left" - --flow into new chunk -1 x - elseif ore[block]["prop"] == "liquid" and x == 1 and loaded_chunks[xx-1] and loaded_chunks[xx-1][-yy] and loaded_chunks[xx-1][-yy][map_max] and loaded_chunks[xx-1][-yy][map_max][y] and loaded_chunks[xx-1][-yy][map_max][y]["block"] == 1 then - --print("Test") - --hack to create new table sub element - if not after_table[xx-1] then - after_table[xx-1] = {} - end - if not after_table[xx-1][-yy] then - after_table[xx-1][-yy] = {} - end - if not after_table[xx-1][-yy][map_max] then - after_table[xx-1][-yy][map_max] = {} - end - if not after_table[xx-1][-yy][map_max][y] then - --print("create new block element success") - after_table[xx-1][-yy][map_max][y]= {} - end - --print(after_table[xx-1][-yy][map_max][y]["block"]) - after_table[xx-1][-yy][map_max][y]["block"] = block - after_table[xx-1][-yy][map_max][y]["xleft"] = "xleft" - end - --love.graphics.draw(texture_table[loaded_chunks[xx][-yy][x][y]["block"]], (((x*scale)-(player.playerx*scale))+((scale*map_max)/2))+(map_max*scale*xx)+offsetx, (((y*scale)-(player.playery*scale))+((scale*map_max)/2))+(map_max*scale*yy)+offsety-4,0, scale/16, scale/16) end end end diff --git a/map_load.lua b/map_load.lua new file mode 100644 index 0000000..a8c32a9 --- /dev/null +++ b/map_load.lua @@ -0,0 +1,12 @@ + + +while true do + --load old tiles + print("load old") + --tiles = TSerial.unpack(love.filesystem.read("/map/"..chunkx+xx.."_"..chunky+yy..".txt")) + savechannel:push{"load_old","/map/"..chunkx+xx.."_"..chunky+yy..".txt"} + + local tiles = loadchannel:pop() + print(tiles) + loaded_chunks[xx][yy] = tiles +end diff --git a/player.lua b/player.lua index bd5e3aa..90a6a37 100644 --- a/player.lua +++ b/player.lua @@ -129,25 +129,29 @@ function mine(key) --play sound and remove tile if left then --print(mx,my) - if loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] ~= 1 then - minesound:setPitch(love.math.random(50,100)/100) - minesound:stop() - minesound:play() - loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] = 1 - player.mining = true - --love.filesystem.write( "/map/"..chunkx+selected_chunkx.."_"..chunky+selected_chunky..".txt", TSerial.pack(loaded_chunks[selected_chunkx][selected_chunky])) - - score = score + 1 + if loaded_chunks[selected_chunkx] and loaded_chunks[selected_chunkx][selected_chunky] and loaded_chunks[selected_chunkx][selected_chunky][mx] and loaded_chunks[selected_chunkx][selected_chunky][mx][my] then + if loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] ~= 1 then + minesound:setPitch(love.math.random(50,100)/100) + minesound:stop() + minesound:play() + loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] = 1 + player.mining = true + --love.filesystem.write( "/map/"..chunkx+selected_chunkx.."_"..chunky+selected_chunky..".txt", TSerial.pack(loaded_chunks[selected_chunkx][selected_chunky])) + + score = score + 1 + end end elseif right then - if loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] == 1 and (mx ~= player.playerx or my ~= player.playery) then - placesound:setPitch(love.math.random(50,100)/100) - placesound:stop() - placesound:play() - loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] = player.selected - player.mining = false - --love.filesystem.write( "/map/"..chunkx+selected_chunkx.."_"..chunky+selected_chunky..".txt", TSerial.pack(loaded_chunks[selected_chunkx][selected_chunky])) - score = score + 1 + if loaded_chunks[selected_chunkx] and loaded_chunks[selected_chunkx][selected_chunky] and loaded_chunks[selected_chunkx][selected_chunky][mx] and loaded_chunks[selected_chunkx][selected_chunky][mx][my] then + if loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] == 1 and (mx ~= player.playerx or my ~= player.playery) then + placesound:setPitch(love.math.random(50,100)/100) + placesound:stop() + placesound:play() + loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] = player.selected + player.mining = false + --love.filesystem.write( "/map/"..chunkx+selected_chunkx.."_"..chunky+selected_chunky..".txt", TSerial.pack(loaded_chunks[selected_chunkx][selected_chunky])) + score = score + 1 + end end end end diff --git a/saving.lua b/saving.lua index ab6f893..fad6e94 100644 --- a/saving.lua +++ b/saving.lua @@ -1,4 +1,7 @@ +--how the map saves and loads + savechannel = love.thread.getChannel("save") +loadchannel = love.thread.getChannel("load") dofile("tserial.lua") @@ -7,21 +10,43 @@ dofile("tserial.lua") --debug info while true do + print("test") if savechannel:getCount( ) > 0 then - print("saving chunks") local x = savechannel:pop() - local max_chunks = x[1] - local chunkx = x[2] - local chunky = x[3] - local loaded_chunks = x[4] - - print(max_chunks,chunkx,chunky,loaded_chunks) - - 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) + local doing = x[1] + if doing == "saving_old" then + print("saving old chunks") + 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) + + for xx = -max_chunks,max_chunks do + for yy = -max_chunks,max_chunks do + if loaded_chunks[xx] and loaded_chunks[xx][yy] then + love.filesystem.write( "/map/"..chunkx+xx.."_"..chunky+yy..".txt", TSerial.pack(loaded_chunks[xx][yy])) + print("saving:"..chunkx+xx.."_"..chunky+yy) + end + end end + elseif doing == "load_old" then + print("load old") + print("loading old chunks") + local max_chunks = x[2] + local chunkx = x[3] + local chunky = x[4] + local loaded_chunks = x[5] + local xx = x[6] + local yy = x[7] + + --print("pushing") + local file = love.filesystem.read("/map/"..chunkx+xx.."_"..chunky+yy..".txt") + + loadchannel:push{TSerial.unpack(file),xx,yy} + print("succesfully loaded "..chunkx+xx.."_"..chunky+yy) + --print("file doesn't exist") end end