]> git.lizzy.rs Git - minetest.git/blob - builtin/item.lua
Move chat commands to Lua and remove servercommand.{cpp,h}
[minetest.git] / builtin / item.lua
1 -- Minetest: builtin/item.lua
2
3 --
4 -- Item definition helpers
5 --
6
7 function minetest.inventorycube(img1, img2, img3)
8         img2 = img2 or img1
9         img3 = img3 or img1
10         return "[inventorycube"
11                         .. "{" .. img1:gsub("%^", "&")
12                         .. "{" .. img2:gsub("%^", "&")
13                         .. "{" .. img3:gsub("%^", "&")
14 end
15
16 function minetest.get_pointed_thing_position(pointed_thing, above)
17         if pointed_thing.type == "node" then
18                 if above then
19                         -- The position where a node would be placed
20                         return pointed_thing.above
21                 else
22                         -- The position where a node would be dug
23                         return pointed_thing.under
24                 end
25         elseif pointed_thing.type == "object" then
26                 obj = pointed_thing.ref
27                 if obj ~= nil then
28                         return obj:getpos()
29                 else
30                         return nil
31                 end
32         else
33                 return nil
34         end
35 end
36
37 function minetest.dir_to_facedir(dir)
38         if math.abs(dir.x) > math.abs(dir.z) then
39                 if dir.x < 0 then
40                         return 3
41                 else
42                         return 1
43                 end
44         else
45                 if dir.z < 0 then
46                         return 2
47                 else
48                         return 0
49                 end
50         end
51 end
52
53 function minetest.dir_to_wallmounted(dir)
54         if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
55                 if dir.y < 0 then
56                         return 1
57                 else
58                         return 0
59                 end
60         elseif math.abs(dir.x) > math.abs(dir.z) then
61                 if dir.x < 0 then
62                         return 3
63                 else
64                         return 2
65                 end
66         else
67                 if dir.z < 0 then
68                         return 5
69                 else
70                         return 4
71                 end
72         end
73 end
74
75 function minetest.get_node_drops(nodename, toolname)
76         local drop = ItemStack({name=nodename}):get_definition().drop
77         if drop == nil then
78                 -- default drop
79                 return {ItemStack({name=nodename})}
80         elseif type(drop) == "string" then
81                 -- itemstring drop
82                 return {ItemStack(drop)}
83         elseif drop.items == nil then
84                 -- drop = {} to disable default drop
85                 return {}
86         end
87
88         -- Extended drop table
89         local got_items = {}
90         local got_count = 0
91         local _, item, tool
92         for _, item in ipairs(drop.items) do
93                 local good_rarity = true
94                 local good_tool = true
95                 if item.rarity ~= nil then
96                         good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
97                 end
98                 if item.tools ~= nil then
99                         good_tool = false
100                         for _, tool in ipairs(item.tools) do
101                                 if tool:sub(1, 1) == '~' then
102                                         good_tool = toolname:find(tool:sub(2)) ~= nil
103                                 else
104                                         good_tool = toolname == tool
105                                 end
106                                 if good_tool then
107                                         break
108                                 end
109                         end
110                 end
111                 if good_rarity and good_tool then
112                         got_count = got_count + 1
113                         for _, add_item in ipairs(item.items) do
114                                 got_items[#got_items+1] = add_item
115                         end
116                         if drop.max_items ~= nil and got_count == drop.max_items then
117                                 break
118                         end
119                 end
120         end
121         return got_items
122 end
123
124 function minetest.item_place_node(itemstack, placer, pointed_thing)
125         local item = itemstack:peek_item()
126         local def = itemstack:get_definition()
127         if def.type ~= "node" or pointed_thing.type ~= "node" then
128                 return itemstack
129         end
130
131         local under = pointed_thing.under
132         local oldnode_under = minetest.env:get_node(under)
133         local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
134         olddef_under = olddef_under or minetest.nodedef_default
135         local above = pointed_thing.above
136         local oldnode_above = minetest.env:get_node(above)
137         local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
138         olddef_above = olddef_above or minetest.nodedef_default
139
140         if not olddef_above.buildable_to and not olddef_under.buildable_to then
141                 minetest.log("info", placer:get_player_name() .. " tried to place"
142                         .. " node in invalid position " .. minetest.pos_to_string(above)
143                         .. ", replacing " .. oldnode_above.name)
144                 return
145         end
146
147         -- Place above pointed node
148         local place_to = {x = above.x, y = above.y, z = above.z}
149
150         -- If node under is buildable_to, place into it instead (eg. snow)
151         if olddef_under.buildable_to then
152                 minetest.log("info", "node under is buildable to")
153                 place_to = {x = under.x, y = under.y, z = under.z}
154         end
155
156         minetest.log("action", placer:get_player_name() .. " places node "
157                 .. def.name .. " at " .. minetest.pos_to_string(place_to))
158         
159         local oldnode = minetest.env:get_node(place_to)
160         local newnode = {name = def.name, param1 = 0, param2 = 0}
161
162         -- Calculate direction for wall mounted stuff like torches and signs
163         if def.paramtype2 == 'wallmounted' then
164                 local dir = {
165                         x = under.x - above.x,
166                         y = under.y - above.y,
167                         z = under.z - above.z
168                 }
169                 newnode.param2 = minetest.dir_to_wallmounted(dir)
170         -- Calculate the direction for furnaces and chests and stuff
171         elseif def.paramtype2 == 'facedir' then
172                 local placer_pos = placer:getpos()
173                 if placer_pos then
174                         local dir = {
175                                 x = above.x - placer_pos.x,
176                                 y = above.y - placer_pos.y,
177                                 z = above.z - placer_pos.z
178                         }
179                         newnode.param2 = minetest.dir_to_facedir(dir)
180                         minetest.log("action", "facedir: " .. newnode.param2)
181                 end
182         end
183
184         -- Add node and update
185         minetest.env:add_node(place_to, newnode)
186
187         -- Run callback
188         if def.after_place_node then
189                 -- Copy place_to because callback can modify it
190                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
191                 def.after_place_node(place_to_copy, placer)
192         end
193
194         -- Run script hook
195         local _, callback
196         for _, callback in ipairs(minetest.registered_on_placenodes) do
197                 -- Copy pos and node because callback can modify them
198                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
199                 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
200                 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
201                 callback(place_to_copy, newnode_copy, placer, oldnode_copy)
202         end
203
204         itemstack:take_item()
205         return itemstack
206 end
207
208 function minetest.item_place_object(itemstack, placer, pointed_thing)
209         local pos = minetest.get_pointed_thing_position(pointed_thing, true)
210         if pos ~= nil then
211                 local item = itemstack:take_item()
212                 minetest.env:add_item(pos, item)
213         end
214         return itemstack
215 end
216
217 function minetest.item_place(itemstack, placer, pointed_thing)
218         if itemstack:get_definition().type == "node" then
219                 return minetest.item_place_node(itemstack, placer, pointed_thing)
220         else
221                 return minetest.item_place_object(itemstack, placer, pointed_thing)
222         end
223 end
224
225 function minetest.item_drop(itemstack, dropper, pos)
226         if dropper.get_player_name then
227                 local v = dropper:get_look_dir()
228                 local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
229                 local obj = minetest.env:add_item(p, itemstack)
230                 v.x = v.x*2
231                 v.y = v.y*2 + 1
232                 v.z = v.z*2
233                 obj:setvelocity(v)
234         else
235                 minetest.env:add_item(pos, itemstack)
236         end
237         return ""
238 end
239
240 function minetest.item_eat(hp_change, replace_with_item)
241         return function(itemstack, user, pointed_thing)  -- closure
242                 if itemstack:take_item() ~= nil then
243                         user:set_hp(user:get_hp() + hp_change)
244                         itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
245                 end
246                 return itemstack
247         end
248 end
249
250 function minetest.node_punch(pos, node, puncher)
251         -- Run script hook
252         local _, callback
253         for _, callback in ipairs(minetest.registered_on_punchnodes) do
254                 -- Copy pos and node because callback can modify them
255                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
256                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
257                 callback(pos_copy, node_copy, puncher)
258         end
259 end
260
261 function minetest.node_dig(pos, node, digger)
262         minetest.debug("node_dig")
263
264         local def = ItemStack({name=node.name}):get_definition()
265         -- Check if def ~= 0 because we always want to be able to remove unknown nodes
266         if #def ~= 0 and not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
267                 minetest.debug("not diggable")
268                 minetest.log("info", digger:get_player_name() .. " tried to dig "
269                         .. node.name .. " which is not diggable "
270                         .. minetest.pos_to_string(pos))
271                 return
272         end
273
274         minetest.log('action', digger:get_player_name() .. " digs "
275                 .. node.name .. " at " .. minetest.pos_to_string(pos))
276
277         local wielded = digger:get_wielded_item()
278         local drops = minetest.get_node_drops(node.name, wielded:get_name())
279
280         -- Wear out tool
281         local tp = wielded:get_tool_capabilities()
282         local dp = minetest.get_dig_params(def.groups, tp)
283         wielded:add_wear(dp.wear)
284         digger:set_wielded_item(wielded)
285
286         -- Add dropped items to object's inventory
287         if digger:get_inventory() then
288                 local _, dropped_item
289                 for _, dropped_item in ipairs(drops) do
290                         digger:get_inventory():add_item("main", dropped_item)
291                 end
292         end
293
294         local oldmetadata = nil
295         if def.after_dig_node then
296                 oldmetadata = minetest.env:get_meta(pos):to_table()
297         end
298
299         -- Remove node and update
300         minetest.env:remove_node(pos)
301         
302         -- Run callback
303         if def.after_dig_node then
304                 -- Copy pos and node because callback can modify them
305                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
306                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
307                 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
308         end
309
310         -- Run script hook
311         local _, callback
312         for _, callback in ipairs(minetest.registered_on_dignodes) do
313                 -- Copy pos and node because callback can modify them
314                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
315                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
316                 callback(pos_copy, node_copy, digger)
317         end
318 end
319
320 -- This is used to allow mods to redefine minetest.item_place and so on
321 -- NOTE: This is not the preferred way. Preferred way is to provide enough
322 --       callbacks to not require redefining global functions. -celeron55
323 local function redef_wrapper(table, name)
324         return function(...)
325                 return table[name](...)
326         end
327 end
328
329 --
330 -- Item definition defaults
331 --
332
333 minetest.nodedef_default = {
334         -- Item properties
335         type="node",
336         -- name intentionally not defined here
337         description = "",
338         groups = {},
339         inventory_image = "",
340         wield_image = "",
341         wield_scale = {x=1,y=1,z=1},
342         stack_max = 99,
343         usable = false,
344         liquids_pointable = false,
345         tool_capabilities = nil,
346         node_placement_prediction = nil,
347
348         -- Interaction callbacks
349         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
350         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
351         on_use = nil,
352         can_dig = nil,
353
354         on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
355         on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
356
357         on_receive_fields = nil,
358         
359         on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
360         on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
361         on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
362
363         -- Node properties
364         drawtype = "normal",
365         visual_scale = 1.0,
366         -- Don't define these because otherwise the old tile_images and
367         -- special_materials wouldn't be read
368         --tiles ={""},
369         --special_tiles = {
370         --      {name="", backface_culling=true},
371         --      {name="", backface_culling=true},
372         --},
373         alpha = 255,
374         post_effect_color = {a=0, r=0, g=0, b=0},
375         paramtype = "none",
376         paramtype2 = "none",
377         is_ground_content = false,
378         sunlight_propagates = false,
379         walkable = true,
380         pointable = true,
381         diggable = true,
382         climbable = false,
383         buildable_to = false,
384         liquidtype = "none",
385         liquid_alternative_flowing = "",
386         liquid_alternative_source = "",
387         liquid_viscosity = 0,
388         light_source = 0,
389         damage_per_second = 0,
390         selection_box = {type="regular"},
391         legacy_facedir_simple = false,
392         legacy_wallmounted = false,
393 }
394
395 minetest.craftitemdef_default = {
396         type="craft",
397         -- name intentionally not defined here
398         description = "",
399         groups = {},
400         inventory_image = "",
401         wield_image = "",
402         wield_scale = {x=1,y=1,z=1},
403         stack_max = 99,
404         liquids_pointable = false,
405         tool_capabilities = nil,
406
407         -- Interaction callbacks
408         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
409         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
410         on_use = nil,
411 }
412
413 minetest.tooldef_default = {
414         type="tool",
415         -- name intentionally not defined here
416         description = "",
417         groups = {},
418         inventory_image = "",
419         wield_image = "",
420         wield_scale = {x=1,y=1,z=1},
421         stack_max = 1,
422         liquids_pointable = false,
423         tool_capabilities = nil,
424
425         -- Interaction callbacks
426         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
427         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
428         on_use = nil,
429 }
430
431 minetest.noneitemdef_default = {  -- This is used for the hand and unknown items
432         type="none",
433         -- name intentionally not defined here
434         description = "",
435         groups = {},
436         inventory_image = "",
437         wield_image = "",
438         wield_scale = {x=1,y=1,z=1},
439         stack_max = 99,
440         liquids_pointable = false,
441         tool_capabilities = nil,
442
443         -- Interaction callbacks
444         on_place = nil,
445         on_drop = nil,
446         on_use = nil,
447 }
448