From b4c189ab1b6233c7e7e260446e80b789f375b5d6 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 11 Feb 2023 15:33:13 +0000 Subject: change from random worldgen to be based on a hash of position; implement serverside chunk unloading --- server/server.lua | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'server/server.lua') diff --git a/server/server.lua b/server/server.lua index cc9903e..88f4efe 100644 --- a/server/server.lua +++ b/server/server.lua @@ -3,6 +3,7 @@ local json = require"common.dkjson" local chunk = require"common.chunk" local noise = require"noise" local coords = require"common.coords" +local Pos = coords.Pos local worldgen = require"worldgen" local MapS = require"map".MapS local posix_time = require"posix.time" @@ -34,7 +35,7 @@ local function random_color() return {math.random(),math.random(),math.random()} end local function make_player(peer) - local p = {pos={0,0},color=random_color(),peer=peer,id=nextid} + local p = {pos=Pos:make(0,0),color=random_color(),peer=peer,id=nextid} nextid = nextid + 1 return p end @@ -42,8 +43,8 @@ end local function player_info_part(player) return { id=player.id, - x=player.pos[1], - y=player.pos[2], + x=player.pos.x, + y=player.pos.y, color=player.color, } end @@ -95,8 +96,7 @@ local function handle_ev(ev) local op = j.t if op == "ppos" then local x,y = j.x,j.y - player.pos[1] = x - player.pos[2] = y + player.pos = coords.Pos:make(x,y) -- print(player.id,"-->",player.pos[1],player.pos[2]) for i,otherplayer in ipairs(playerlist) do if otherplayer ~= player then @@ -157,11 +157,30 @@ local ndts = 100 local ntick = 0 +local function player_near_chunk(cp) + -- true: chunk at cp should stay loaded (because of a nearby player), + -- false: no players nearby, can be unloaded + -- this is kind of inefficient at the moment + for _,player in ipairs(playerlist) do + local pcp = player.pos:to_hex():containing_chunk() + for _,neighb in ipairs(pcp:neighborhood()) do + if neighb == cp then + return true + end + end + end + return false +end + local function tick(ntick) if ntick % 30 == 0 then - print("saving things...") + print("saving things") for cp,ch in map:iter_chunks() do map:save_chunk(cp) + if not player_near_chunk(cp) then + print("unloading chunk",cp) + map:remove_chunk(cp) + end end end end -- cgit v1.2.3