People now have an inventory

master
Ciaran Gultnieks 2014-04-05 08:40:43 +01:00
parent 46652fc616
commit d3c99773d6
2 changed files with 86 additions and 46 deletions

View File

@ -5,41 +5,59 @@ if moddebug then dbg=moddebug.dbg("people") else dbg={v1=function() end,v2=funct
local lastformbyplayer = {}
minetest.register_on_player_receive_fields(function(sender, formname, fields)
if formname ~= "people:form" then return end
if fields.quit then return end
dbg.v2("Received form fields "..dump(fields))
if formname == "people:form" then
local playername = sender:get_player_name()
local entity = lastformbyplayer[playername]
if not entity then return end
if playername ~= entity.owner then return end
-- The main form...
if fields.program then
if string.sub(entity.fcode, 1, 1) ~= "@" then
entity.code = fields.code
else
entity.code = entity.fcode
end
local event = {type="program", programmer=playername}
local err = people.exec_event(entity, event)
if err then dbg.v1("Lua error "..err) end
elseif fields.preset then
if fields.preset == "Custom" then
if string.sub(entity.fcode, 1, 1) == "@" then
local f = string.sub(entity.fcode, 2)
entity.fcode = "HUH?"
for _, p in pairs(people.presets) do
if p.name == f then
entity.fcode = p.code
break
if fields.quit then return end
dbg.v2("Received form fields "..dump(fields))
local playername = sender:get_player_name()
local entity = lastformbyplayer[playername]
if not entity then return end
if playername ~= entity.owner then return end
if fields.inventory then
local formspec = "size[8,9]"..
"list[detached:people_"..entity.name..";main;0,0;8,4;]"..
"list[current_player;main;0,5;8,4;]"
minetest.show_formspec(playername, "people:invform", formspec)
elseif fields.program then
if string.sub(entity.fcode, 1, 1) ~= "@" then
entity.code = fields.code
else
entity.code = entity.fcode
end
local event = {type="program", programmer=playername}
local err = people.exec_event(entity, event)
if err then dbg.v1("Lua error "..err) end
elseif fields.preset then
if fields.preset == "Custom" then
if string.sub(entity.fcode, 1, 1) == "@" then
local f = string.sub(entity.fcode, 2)
entity.fcode = "HUH?"
for _, p in pairs(people.presets) do
if p.name == f then
entity.fcode = p.code
break
end
end
end
else
entity.fcode = "@"..fields.preset
end
else
entity.fcode = "@"..fields.preset
end
people.show_form(entity, playername)
people.show_form(entity, playername)
end
elseif formname == "people:invform" then
-- The inventory form...
-- Just a placeholder - doesn't need any handling currently
end
end)
@ -79,7 +97,8 @@ people.show_form = function(entity, playername)
presetnum = 1
entity.fcode = "@Example"
end
formspec = formspec.."button[3.75,6;2.5,1;program;Program]"..
formspec = formspec.."button[2.5,6;2.5,1;program;Program]"..
"button[5.5,6;2.5,1;inventory;Inventory]"..
"label[0,7;Preset]"..
"dropdown[2,7;3;preset;"..prelist..";"..presetnum + 1 .."]"
else

View File

@ -80,6 +80,16 @@ do
end
end
function people.get_inv(self, name)
local st = {}
local list = self.inventory:get_list(name)
if list then
for i=1,#list,1 do
table.insert(st,list[i]:to_string())
end
end
return st
end
minetest.register_entity("people:person" ,{
hp_max = 1,
@ -116,35 +126,36 @@ minetest.register_entity("people:person" ,{
local setactive = false
dbg.v3("on_activate, staticdata = "..dump(staticdata))
local restored
if staticdata and staticdata ~= "" then
local data = minetest.deserialize(staticdata)
if data then
if data.action then
self.action = data.action
restored = minetest.deserialize(staticdata)
if restored then
if restored.action then
self.action = restored.action
else
self.action = {"wait", time=5}
end
if data.memory then
self.memory = data.memory
if restored.memory then
self.memory = restored.memory
else
self.memory = {}
end
if data.code then
self.code = data.code
if restored.code then
self.code = restored.code
else
self.code = "@"..people.presets[1].name
end
if data.name then
self.name = data.name
if restored.name then
self.name = restored.name
end
if data.owner then
self.owner = data.owner
if restored.owner then
self.owner = restored.owner
end
if data.wait then
self.wait = data.wait
if restored.wait then
self.wait = restored.wait
end
if data.props then
self.props = data.props
if restored.props then
self.props = restored.props
end
end
@ -158,6 +169,15 @@ minetest.register_entity("people:person" ,{
people.people_set_active(self)
end
self.inventory = minetest.create_detached_inventory("people_"..self.name, nil)
self.inventory:set_size("main", 8*4);
if restored and restored.inv_main then
local inv_main = restored.inv_main
for i=1,#inv_main,1 do
self.inventory:set_stack("main", i, inv_main[i])
end
end
self.env = people.create_environment(self)
self:update_props()
@ -188,6 +208,7 @@ minetest.register_entity("people:person" ,{
memory = self.memory,
wait = self.wait,
props = self.props,
inv_main = people.get_inv(self, "main")
}
return minetest.serialize(data)
end,