summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-08-26 08:22:06 +0100
committerubq323 <ubq323@ubq323.website>2023-08-26 08:22:06 +0100
commite95fc908f3952737e41efce88c83ce7414e130b0 (patch)
tree42795f0bd98a7e790005ac6d5f41a0f836e5e8ea
parentaace7b1f9c80f5cb4b155d8b2db51f355b6f81b2 (diff)
add player inventories
-rw-r--r--client/game.lua22
-rw-r--r--client/util.lua15
-rw-r--r--net.txt3
-rw-r--r--server/chunk.lua4
-rw-r--r--server/map.lua4
-rw-r--r--server/player.lua2
-rw-r--r--server/server.lua18
7 files changed, 58 insertions, 10 deletions
diff --git a/client/game.lua b/client/game.lua
index 4fc148b..43e2352 100644
--- a/client/game.lua
+++ b/client/game.lua
@@ -204,7 +204,6 @@ local function handle_net()
remote_players[id]=nil
elseif op == "move" then
local id,x,y = j.id,j.x,j.y
- assert(remote_players[id],"wheeze "..id)
remote_players[id].pos.x = x
remote_players[id].pos.y = y
elseif op == "you" then
@@ -213,7 +212,10 @@ local function handle_net()
pos=coords.Pos:make(pl.x,pl.y),
color=pl.color, id=pl.id,
username=pl.username,
+ inv = {},
}
+ elseif op == "inv" then
+ local_player.inv = j.inv
elseif op == "chunk" then
local ch = ChunkC:from_packet_data(j)
map:add_chunk(ch)
@@ -240,8 +242,10 @@ local function update(dt)
local mh =
camera:screen_to_world(Pos:make(msx,msy)):to_hex():round()
if map:at(mh) == 0 and love.mouse.isDown(1) then
- map:set_at(mh,selected_tile)
- send_settile(mh,selected_tile)
+ if (local_player.inv[selected_tile] or 0) > 0 then
+ map:set_at(mh,selected_tile)
+ send_settile(mh,selected_tile)
+ end
elseif map:at(mh) ~= 0 and love.mouse.isDown(2) then
map:set_at(mh,0)
send_settile(mh,0)
@@ -337,6 +341,18 @@ local function draw()
},"\n"),10,10)
end
+ local inv_rows = {'inventory:'}
+ for t=1,9 do
+ local c = local_player.inv[t] or 0
+ local s = t..': '..util.tile_names[t]..' x'..c
+ if selected_tile == t then
+ s = s .. ' <--'
+ end
+ table.insert(inv_rows, s)
+ end
+ util.print_good(table.concat(inv_rows,'\n'),0,0)
+
+
if show_controls then
util.print_good(help_text,"center","center")
end
diff --git a/client/util.lua b/client/util.lua
index 2c7d90a..a1c4b35 100644
--- a/client/util.lua
+++ b/client/util.lua
@@ -13,6 +13,19 @@ local function print_good(str,x,y)
love.graphics.draw(text,x,y)
end
+local tile_names = {
+ 'red',
+ 'orange',
+ 'yellow',
+ 'green',
+ 'teal',
+ 'blue',
+ 'purple',
+ 'grey',
+ 'ubqorange',
+}
+
return {
- print_good=print_good
+ print_good=print_good,
+ tile_names=tile_names,
}
diff --git a/net.txt b/net.txt
index d5f5ef1..21e46d0 100644
--- a/net.txt
+++ b/net.txt
@@ -37,3 +37,6 @@ settile {q,r,tile}
chat {msg,from}
recieve chat message msg from player with name from.
+
+inv {inv={tile:count}}
+ tell client about inventory of its player
diff --git a/server/chunk.lua b/server/chunk.lua
index 20db704..da5b5b1 100644
--- a/server/chunk.lua
+++ b/server/chunk.lua
@@ -18,7 +18,7 @@ function ChunkS.apply_migrations(self)
end
end
-function ChunkS.load_from_disk(cls,cp)
+function ChunkS.invigorate(cls,cp)
-- tries to load from database. returns nil if not there.
local txn,dbi = db.get_db('chunks')
local d = dbi[tostring(cp)]
@@ -33,7 +33,7 @@ function ChunkS.load_from_disk(cls,cp)
return self
end
-function ChunkS.save_if_dirty(self,txn)
+function ChunkS.persist(self,txn)
if self.dirty then
print("saving chunk",self.cp)
local dbi = txn:open'chunks'
diff --git a/server/map.lua b/server/map.lua
index e8be2ff..2775f7c 100644
--- a/server/map.lua
+++ b/server/map.lua
@@ -18,7 +18,7 @@ function MapS.obtain(self,cp)
if ch then
return ch
else
- ch = ChunkS:load_from_disk(cp)
+ ch = ChunkS:invigorate(cp)
if not ch then
ch = worldgen.gen_chunk(cp)
end
@@ -34,7 +34,7 @@ function MapS.save_chunk(self,cp,txn)
local ch = self:chunk(cp)
if not ch then return end
- ch:save_if_dirty(txn)
+ ch:persist(txn)
end
return {MapS=MapS}
diff --git a/server/player.lua b/server/player.lua
index bd7b52a..105efff 100644
--- a/server/player.lua
+++ b/server/player.lua
@@ -15,6 +15,7 @@ function Player.make(cls,peer,username)
peer = peer,
id = nextid,
username = username,
+ inv = {},
}
nextid = nextid + 1
return setmetatable(self,cls)
@@ -27,6 +28,7 @@ function Player.info_part(self)
y=self.pos.y,
color=self.color,
username=self.username,
+ inv = self.inv,
}
end
diff --git a/server/server.lua b/server/server.lua
index 8a725af..3ec467c 100644
--- a/server/server.lua
+++ b/server/server.lua
@@ -48,6 +48,9 @@ end
local function chat_packet(fromplayer,msg)
return json.encode{t="chat",from=fromplayer.id,msg=msg}
end
+local function inv_packet(inv)
+ return json.encode{t='inv',inv=inv}
+end
local map = MapS:make()
@@ -128,8 +131,19 @@ local function handle_player_packet(player,ev)
end
elseif op == "settile" then
local h = coords.Hex:make(j.q,j.r)
- map:set_at(h,j.tile)
- -- print(player.id,"settile",h,j.tile)
+ local old_tile = map:at(h)
+ local new_tile = j.tile
+ local inv = player.inv
+
+ if old_tile == 0 then
+ inv[new_tile] = (inv[new_tile] or 0) - 1
+ elseif new_tile == 0 then
+ inv[old_tile] = (inv[old_tile] or 0) + 1
+ end
+ player.peer:send(inv_packet(inv))
+
+ map:set_at(h, new_tile)
+
for i,otherplayer in ipairs(playerlist) do
if otherplayer ~= player then
-- same packet structure s2c as c2s