1 --[[function basicSerialize(o)
2 if type(o) == "number" then
4 else -- assume it is a string
5 return string.format("%q", o)
9 function dump2(name, value, saved)
10 saved = saved or {} -- initial value
12 if type(value) == "number" or type(value) == "string" then
13 io.write(basicSerialize(value), "\n")
14 elseif type(value) == "table" then
15 if saved[value] then -- value already saved?
16 io.write(saved[value], "\n") -- use its previous name
18 saved[value] = name -- save name for next time
19 io.write("{}\n") -- create a new table
20 for k,v in pairs(value) do -- save its fields
21 local fieldname = string.format("%s[%s]", name,
23 save(fieldname, v, saved)
27 error("cannot save a " .. type(value))
31 --[[function dump(o, name, dumped, s)
35 s = s .. name .. " = "
36 if type(o) == "number" then
38 elseif type(o) == "string" then
39 s = s .. string.format("%q", o)
40 elseif type(o) == "boolean" then
42 elseif type(o) == "function" then
44 elseif type(o) == "nil" then
46 elseif type(o) == "table" then
52 for k,v in pairs(o) do
53 t[#t+1] = dump(v, k, dumped)
55 s = s .. "{" .. table.concat(t, ", ") .. "}"
58 error("cannot dump a " .. type(o))
64 function basic_dump2(o)
65 if type(o) == "number" then
67 elseif type(o) == "string" then
68 return string.format("%q", o)
69 elseif type(o) == "boolean" then
71 elseif type(o) == "function" then
73 elseif type(o) == "userdata" then
75 elseif type(o) == "nil" then
78 error("cannot dump a " .. type(o))
83 function dump2(o, name, dumped)
87 if type(o) == "number" or type(o) == "string" or type(o) == "boolean"
88 or type(o) == "function" or type(o) == "nil"
89 or type(o) == "userdata" then
90 io.write(basic_dump2(o), "\n")
91 elseif type(o) == "table" then
93 io.write(dumped[o], "\n")
96 io.write("{}\n") -- new table
97 for k,v in pairs(o) do
98 local fieldname = string.format("%s[%s]", name, basic_dump2(k))
99 dump2(v, fieldname, dumped)
103 error("cannot dump a " .. type(o))
108 function dump(o, dumped)
109 dumped = dumped or {}
110 if type(o) == "number" then
112 elseif type(o) == "string" then
113 return string.format("%q", o)
114 elseif type(o) == "table" then
116 return "<circular reference>"
120 for k,v in pairs(o) do
121 t[#t+1] = "" .. k .. " = " .. dump(v, dumped)
123 return "{" .. table.concat(t, ", ") .. "}"
124 elseif type(o) == "boolean" then
126 elseif type(o) == "function" then
129 error("cannot dump a " .. type(o))
135 print("minetest dump: "..dump(minetest))
137 --local TNT = minetest.new_entity {
139 -- Maybe handle gravity and collision this way? dunno
142 boundingbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
144 textures = {"tnt_top.png","tnt_bottom.png","tnt_side.png","tnt_side.png","tnt_side.png","tnt_side.png"},
145 -- Initial value for our timer
147 -- List names of state variables, for serializing object state
148 state_variables = {"timer"},
151 -- Called periodically
152 function TNT:on_step(dtime)
153 --print("TNT:on_step()")
156 -- Called when object is punched
157 function TNT:on_punch(hitter)
158 print("TNT:on_punch()")
161 -- Called when object is right-clicked
162 function TNT:on_rightclick(clicker)
163 print("TNT:on_rightclick()")
164 pos = self.object:getpos()
165 print("TNT:on_rightclick(): object position: "..dump(pos))
166 pos = {x=pos.x+0.5+1, y=pos.y+0.5, z=pos.z+0.5}
167 minetest.env:add_node(pos, 0)
170 print("TNT dump: "..dump(TNT))
172 print("Registering TNT");
173 minetest.register_entity("TNT", TNT)
175 --print("minetest.registered_entities: "..dump(minetest.registered_entities))
176 print("minetest.registered_entities:")
177 dump2(minetest.registered_entities)