From 2078572811a1c80ebd1228b24b35392ba02f4908 Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Sun, 15 Jan 2017 16:12:07 -0700 Subject: Add crates for packing up, transporting, and unpacking digtron arrays --- class_layout.lua | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'class_layout.lua') diff --git a/class_layout.lua b/class_layout.lua index 3b3b739..135cfff 100644 --- a/class_layout.lua +++ b/class_layout.lua @@ -25,7 +25,7 @@ end function DigtronLayout.create(pos, player) local self = {} - setmetatable(self,DigtronLayout) + setmetatable(self, DigtronLayout) --initialize. We're assuming that the start position is a controller digtron, should be a safe assumption since only the controller node should call this self.traction = 0 @@ -314,7 +314,7 @@ function DigtronLayout.write_layout_image(self, player) -- create the new one for k, node_image in pairs(self.all) do - minetest.add_node(node_image.pos, node_image.node) + minetest.set_node(node_image.pos, node_image.node) minetest.get_meta(node_image.pos):from_table(node_image.meta) minetest.log("action", string.format("%s adds Digtron component %s at (%d, %d, %d)", player:get_player_name(), node_image.node.name, node_image.pos.x, node_image.pos.y, node_image.pos.z)) @@ -324,3 +324,36 @@ function DigtronLayout.write_layout_image(self, player) end end end + + +--------------------------------------------------------------------------------------------- +-- Serialization. Currently only serializes the data that is needed by the crate, upgrade this function if more is needed + +function DigtronLayout.serialize(self) + -- serialize can't handle ItemStack objects, convert them to strings. + for _, node_image in pairs(self.all) do + for k, inv in pairs(node_image.meta.inventory) do + for index, item in pairs(inv) do + inv[index] = item:to_string() + end + end + end + + return minetest.serialize({controller=self.controller, all=self.all}) +end + +function DigtronLayout.deserialize(layout_string) + local self = {} + setmetatable(self, DigtronLayout) + + if not layout_string or layout_string == "" then + return nil + end + deserialized_layout = minetest.deserialize(layout_string) + + self.all = deserialized_layout.all + self.controller = deserialized_layout.controller + self.old_pos_pointset = Pointset.create() -- needed by the write_layout method, leave empty + + return self +end \ No newline at end of file -- cgit v1.2.3