From 4208b978082ad4fe29f444cd0a73c34bb0df9d47 Mon Sep 17 00:00:00 2001 From: nyje Date: Mon, 29 Jul 2019 16:25:42 +0000 Subject: [PATCH] with saves --- formspec.lua | 4 ++-- formspec_handler.lua | 12 ++++++------ init.lua | 44 +++++++++++++++++++++++++++++++++++++++++++- register_bot.lua | 11 ++++++++++- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/formspec.lua b/formspec.lua index 6f587cd..0544597 100644 --- a/formspec.lua +++ b/formspec.lua @@ -91,8 +91,8 @@ end local function panel_code(pos,program) return button(10,0,"vbots_gui_run.png","run",true) ..button(11,0,"vbots_gui_check.png","check") - ..button(12,0,"vbots_gui_load.png","load") - ..button(13,0,"vbots_gui_save.png","save") + ..button(12,0,"vbots_gui_load.png","load",true) + ..button(13,0,"vbots_gui_save.png","save",true) ..highlight(10,0,4,1,"5","5","f") ..button(15,0,"vbots_gui_exit.png","exit",true) diff --git a/formspec_handler.lua b/formspec_handler.lua index 4feffe3..7bc5bea 100644 --- a/formspec_handler.lua +++ b/formspec_handler.lua @@ -23,18 +23,18 @@ minetest.register_on_player_receive_fields(function(player, bot_key, fields) if fields.run then minetest.after(0, vbots.bot_togglestate, bot_data.pos, "on") end + if fields.save then + vbots.save(bot_data.pos) + end + if fields.load then + vbots.load(bot_data.pos,player) + end if fields.quit=="true" then return end if fields.commands then meta:set_int("panel", 0) end - if fields.save then - -- - end - if fields.load then - -- - end if fields.player_inv then meta:set_int("panel", 1) end diff --git a/init.lua b/init.lua index c4d4ef7..eb4c2cd 100644 --- a/init.lua +++ b/init.lua @@ -7,7 +7,6 @@ vbots={} vbots.modpath = minetest.get_modpath("vbots") vbots.bot_info = {} -vbots.storage = minetest.get_mod_storage() vbots.steptime = 0.3 local trashInv = minetest.create_detached_inventory( @@ -18,8 +17,51 @@ local trashInv = minetest.create_detached_inventory( end }) trashInv:set_size("main", 1) +local mod_storage = minetest.get_mod_storage() +vbots.save = function(pos) + local meta = minetest.get_meta(pos) + local botname = meta:get_string("name") + local meta_table = meta:to_table() + local inv_list = {} + for i,t in pairs(meta_table.inventory) do + for _,s in pairs(t) do + if s and s:get_count()>0 then + inv_list[#inv_list+1] = i.." "..s:get_name().." "..s:get_count() + end + end + end + local clean_meta = {meta_table.fields, inv_list} + mod_storage:set_string(botname,minetest.serialize(clean_meta)) +end + +vbots.load = function(pos,player) + local data = mod_storage:to_table().fields + local bot_list = "" + for n,d in pairs(data) do + bot_list = bot_list..n.."," + end + bot_list = bot_list:sub(1,#bot_list-1) + local formspec = "size[5,9;]".. + "textlist[0,0;5,9;saved;"..bot_list.."]" + minetest.show_formspec(player:get_player_name(), "loadbot", formspec) + --print(bot_list) +end + +minetest.register_on_player_receive_fields(function(player, key, fields) + local data = mod_storage:to_table().fields + local bot_list = {} + for n,d in pairs(data) do + bot_list[#bot_list+1] = n + end + if key == "loadbot" then + minetest.close_formspec(player:get_player_name(), "loadbot") + if fields.saved then + print( bot_list[tonumber(string.split(fields.saved,":")[2])] ) + end + end +end) ------------------------------------- -- Generate 32 bit key for formspec identification diff --git a/register_bot.lua b/register_bot.lua index 29846cf..834d1c6 100644 --- a/register_bot.lua +++ b/register_bot.lua @@ -59,14 +59,23 @@ local function bot_init(pos, placer) vbots.bot_info[bot_key] = { owner = bot_owner, pos = pos, name = bot_name} local meta = minetest.get_meta(pos) meta:set_int("program",0) + meta:mark_as_private("program") meta:set_int("panel",0) + meta:mark_as_private("panel") meta:set_string("key", bot_key) + meta:mark_as_private("key") meta:set_string("owner", bot_owner) + meta:mark_as_private("owner") meta:set_string("infotext", bot_name .. " (" .. bot_owner .. ")") + meta:mark_as_private("infotext") meta:set_string("name", bot_name) + meta:mark_as_private("name") meta:set_int("PC", 0) + meta:mark_as_private("PC") meta:set_int("PR", 0) + meta:mark_as_private("PR") meta:set_string("stack","") + meta:mark_as_private("stack") local inv = meta:get_inventory() inv:set_size("p0", 56) inv:set_size("p1", 56) @@ -369,7 +378,7 @@ local function register_bot(node_name,node_desc,node_tiles,node_groups) paramtype2 = "facedir", legacy_facedir_simple = true, groups = node_groups, - light = core.MAXLIGHT, + light_source = 14, on_blast = function() end, after_place_node = function(pos, placer, itemstack, pointed_thing) bot_init(pos, placer)