Merge pull request #140 from tacigar/tacigar/saving_data

Fix get_staticdata and on_activate, and remove core_name field of maidroid.
This commit is contained in:
tacigar 2017-01-02 09:17:33 +09:00 committed by GitHub
commit 4fa83404d5
1 changed files with 22 additions and 55 deletions

View File

@ -61,12 +61,13 @@ end
-- maidroid.maidroid.get_core_name returns a name of a maidroid's current core. -- maidroid.maidroid.get_core_name returns a name of a maidroid's current core.
function maidroid.maidroid.get_core_name(self) function maidroid.maidroid.get_core_name(self)
return self.core_name local inv = self:get_inventory()
return inv:get_stack("core", 1):get_name()
end end
-- maidroid.maidroid.get_core returns a maidroid's current core definition. -- maidroid.maidroid.get_core returns a maidroid's current core definition.
function maidroid.maidroid.get_core(self) function maidroid.maidroid.get_core(self)
local name = self:get_core_name(self) local name = self:get_core_name()
if name ~= "" then if name ~= "" then
return maidroid.registered_cores[name] return maidroid.registered_cores[name]
end end
@ -342,14 +343,15 @@ function maidroid.register_maidroid(product_name, def)
end end
local function update_infotext(self) local function update_infotext(self)
if self.core_name ~= "" then local core_name = self:get_core_name()
if core_name ~= "" then
local infotext = "" local infotext = ""
if self.pause then if self.pause then
infotext = infotext .. "this maidroid is paused\n" infotext = infotext .. "this maidroid is paused\n"
else else
infotext = infotext .. "this maidroid is active\n" infotext = infotext .. "this maidroid is active\n"
end end
infotext = infotext .. "[Core] : " .. self.core_name infotext = infotext .. "[Core] : " .. core_name
self.object:set_properties{infotext = infotext} self.object:set_properties{infotext = infotext}
return return
@ -366,7 +368,6 @@ function maidroid.register_maidroid(product_name, def)
local core_name = stack:get_name() local core_name = stack:get_name()
local core = maidroid.registered_cores[core_name] local core = maidroid.registered_cores[core_name]
core.on_start(self) core.on_start(self)
self.core_name = core_name
update_infotext(self) update_infotext(self)
end end
@ -386,8 +387,8 @@ function maidroid.register_maidroid(product_name, def)
on_take = function(inv, listname, index, stack, player) on_take = function(inv, listname, index, stack, player)
if listname == "core" then if listname == "core" then
local core = maidroid.registered_cores[self.core_name] local core_name = stack:get_name()
self.core_name = "" local core = maidroid.registered_cores[core_name]
core.on_stop(self) core.on_stop(self)
update_infotext(self) update_infotext(self)
@ -443,29 +444,9 @@ function maidroid.register_maidroid(product_name, def)
self.nametag = data["nametag"] self.nametag = data["nametag"]
local inventory = create_inventory(self) local inventory = create_inventory(self)
local core_name = data["inventory"]["core"] for list_name, list in pairs(data["inventory"]) do
local items = data["inventory"]["main"] inventory:set_list(list_name, list)
local wield_item = data["inventory"]["wield_item"]
if core_name ~= "" then -- set a core
local core_stack = ItemStack(core_name)
core_stack:set_count(1)
inventory:add_item("core", core_stack)
self.core_name = core_name
end end
for _, item in ipairs(items) do -- set items
local item_stack = ItemStack(item["name"])
item_stack:set_count(item["count"])
inventory:add_item("main", item_stack)
end
if wield_item["name"] ~= "" then
local item_stack = ItemStack(wield_item["name"])
item_stack:set_count(wield_item["count"])
inventory:add_item("wield_item", item_stack)
end
end end
update_infotext(self) update_infotext(self)
@ -490,30 +471,18 @@ function maidroid.register_maidroid(product_name, def)
["product_name"] = self.product_name, ["product_name"] = self.product_name,
["manufacturing_number"] = self.manufacturing_number, ["manufacturing_number"] = self.manufacturing_number,
["nametag"] = self.nametag, ["nametag"] = self.nametag,
["inventory"] = { ["inventory"] = {},
["main"] = {},
["core"] = self.core_name,
["wield_item"] = nil,
},
} }
-- set main list. -- set lists.
for _, item in ipairs(inventory:get_list("main")) do for list_name, list in pairs(inventory:get_lists()) do
local count = item:get_count() data["inventory"][list_name] = {}
local itemname = item:get_name()
if count ~= 0 then for i, item in ipairs(list) do
local itemdata = {count = count, name = itemname} data["inventory"][list_name][i] = item:to_string()
table.insert(data["inventory"]["main"], itemdata)
end end
end end
do -- set wield_item list.
local item = self:get_wield_item_stack()
local count = item:get_count()
local itemname = item:get_name()
local itemdata = {count = count, name = itemname}
data["inventory"]["wield_item"] = itemdata
end
return minetest.serialize(data) return minetest.serialize(data)
end end
@ -546,8 +515,8 @@ function maidroid.register_maidroid(product_name, def)
pickup_item(self) pickup_item(self)
-- do core method. -- do core method.
if (not self.pause) and self.core_name ~= "" then local core = self:get_core()
local core = maidroid.registered_cores[self.core_name] if (not self.pause) and core then
core.on_step(self, dtime) core.on_step(self, dtime)
end end
end end
@ -563,16 +532,15 @@ function maidroid.register_maidroid(product_name, def)
-- on_punch is a callback function that is called when a player punch then. -- on_punch is a callback function that is called when a player punch then.
local function on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir) local function on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
local core = self:get_core()
if self.pause == true then if self.pause == true then
self.pause = false self.pause = false
if self.core_name ~= "" then if core then
local core = maidroid.registered_cores[self.core_name]
core.on_resume(self) core.on_resume(self)
end end
else else
self.pause = true self.pause = true
if self.core_name ~= "" then if core then
local core = maidroid.registered_cores[self.core_name]
core.on_pause(self) core.on_pause(self)
end end
end end
@ -601,7 +569,6 @@ function maidroid.register_maidroid(product_name, def)
pause = false, pause = false,
product_name = "", product_name = "",
manufacturing_number = -1, manufacturing_number = -1,
core_name = "",
-- callback methods. -- callback methods.
on_activate = on_activate, on_activate = on_activate,