]> git.lizzy.rs Git - minetest.git/blob - builtin/item.lua
Fix lost pause support in singleplayer
[minetest.git] / builtin / item.lua
1 -- Minetest: builtin/item.lua
2
3 local function copy_pointed_thing(pointed_thing)
4         return {
5                 type  = pointed_thing.type,
6                 above = vector.new(pointed_thing.above),
7                 under = vector.new(pointed_thing.under),
8                 ref   = pointed_thing.ref,
9         }
10 end
11
12 --
13 -- Item definition helpers
14 --
15
16 function minetest.inventorycube(img1, img2, img3)
17         img2 = img2 or img1
18         img3 = img3 or img1
19         return "[inventorycube"
20                         .. "{" .. img1:gsub("%^", "&")
21                         .. "{" .. img2:gsub("%^", "&")
22                         .. "{" .. img3:gsub("%^", "&")
23 end
24
25 function minetest.get_pointed_thing_position(pointed_thing, above)
26         if pointed_thing.type == "node" then
27                 if above then
28                         -- The position where a node would be placed
29                         return pointed_thing.above
30                 else
31                         -- The position where a node would be dug
32                         return pointed_thing.under
33                 end
34         elseif pointed_thing.type == "object" then
35                 obj = pointed_thing.ref
36                 if obj ~= nil then
37                         return obj:getpos()
38                 else
39                         return nil
40                 end
41         else
42                 return nil
43         end
44 end
45
46 function minetest.dir_to_facedir(dir, is6d)
47         --account for y if requested
48         if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
49
50                 --from above
51                 if dir.y < 0 then
52                         if math.abs(dir.x) > math.abs(dir.z) then
53                                 if dir.x < 0 then
54                                         return 19
55                                 else
56                                         return 13
57                                 end
58                         else
59                                 if dir.z < 0 then
60                                         return 10
61                                 else
62                                         return 4
63                                 end
64                         end
65
66                 --from below
67                 else
68                         if math.abs(dir.x) > math.abs(dir.z) then
69                                 if dir.x < 0 then
70                                         return 15
71                                 else
72                                         return 17
73                                 end
74                         else
75                                 if dir.z < 0 then
76                                         return 6
77                                 else
78                                         return 8
79                                 end
80                         end
81                 end
82
83         --otherwise, place horizontally
84         elseif math.abs(dir.x) > math.abs(dir.z) then
85                 if dir.x < 0 then
86                         return 3
87                 else
88                         return 1
89                 end
90         else
91                 if dir.z < 0 then
92                         return 2
93                 else
94                         return 0
95                 end
96         end
97 end
98
99 function minetest.facedir_to_dir(facedir)
100         --a table of possible dirs
101         return ({{x=0, y=0, z=1},
102                                         {x=1, y=0, z=0},
103                                         {x=0, y=0, z=-1},
104                                         {x=-1, y=0, z=0},
105                                         {x=0, y=-1, z=0},
106                                         {x=0, y=1, z=0}})
107
108                                         --indexed into by a table of correlating facedirs
109                                         [({[0]=1, 2, 3, 4, 
110                                                 5, 2, 6, 4,
111                                                 6, 2, 5, 4,
112                                                 1, 5, 3, 6,
113                                                 1, 6, 3, 5,
114                                                 1, 4, 3, 2})
115
116                                                 --indexed into by the facedir in question
117                                                 [facedir]]
118 end
119
120 function minetest.dir_to_wallmounted(dir)
121         if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
122                 if dir.y < 0 then
123                         return 1
124                 else
125                         return 0
126                 end
127         elseif math.abs(dir.x) > math.abs(dir.z) then
128                 if dir.x < 0 then
129                         return 3
130                 else
131                         return 2
132                 end
133         else
134                 if dir.z < 0 then
135                         return 5
136                 else
137                         return 4
138                 end
139         end
140 end
141
142 function minetest.get_node_drops(nodename, toolname)
143         local drop = ItemStack({name=nodename}):get_definition().drop
144         if drop == nil then
145                 -- default drop
146                 return {nodename}
147         elseif type(drop) == "string" then
148                 -- itemstring drop
149                 return {drop}
150         elseif drop.items == nil then
151                 -- drop = {} to disable default drop
152                 return {}
153         end
154
155         -- Extended drop table
156         local got_items = {}
157         local got_count = 0
158         local _, item, tool
159         for _, item in ipairs(drop.items) do
160                 local good_rarity = true
161                 local good_tool = true
162                 if item.rarity ~= nil then
163                         good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
164                 end
165                 if item.tools ~= nil then
166                         good_tool = false
167                         for _, tool in ipairs(item.tools) do
168                                 if tool:sub(1, 1) == '~' then
169                                         good_tool = toolname:find(tool:sub(2)) ~= nil
170                                 else
171                                         good_tool = toolname == tool
172                                 end
173                                 if good_tool then
174                                         break
175                                 end
176                         end
177                 end
178                 if good_rarity and good_tool then
179                         got_count = got_count + 1
180                         for _, add_item in ipairs(item.items) do
181                                 got_items[#got_items+1] = add_item
182                         end
183                         if drop.max_items ~= nil and got_count == drop.max_items then
184                                 break
185                         end
186                 end
187         end
188         return got_items
189 end
190
191 function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
192         local item = itemstack:peek_item()
193         local def = itemstack:get_definition()
194         if def.type ~= "node" or pointed_thing.type ~= "node" then
195                 return itemstack, false
196         end
197
198         local under = pointed_thing.under
199         local oldnode_under = minetest.get_node_or_nil(under)
200         local above = pointed_thing.above
201         local oldnode_above = minetest.get_node_or_nil(above)
202
203         if not oldnode_under or not oldnode_above then
204                 minetest.log("info", placer:get_player_name() .. " tried to place"
205                         .. " node in unloaded position " .. minetest.pos_to_string(above))
206                 return itemstack, false
207         end
208
209         local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
210         olddef_under = olddef_under or minetest.nodedef_default
211         local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
212         olddef_above = olddef_above or minetest.nodedef_default
213
214         if not olddef_above.buildable_to and not olddef_under.buildable_to then
215                 minetest.log("info", placer:get_player_name() .. " tried to place"
216                         .. " node in invalid position " .. minetest.pos_to_string(above)
217                         .. ", replacing " .. oldnode_above.name)
218                 return itemstack, false
219         end
220
221         -- Place above pointed node
222         local place_to = {x = above.x, y = above.y, z = above.z}
223
224         -- If node under is buildable_to, place into it instead (eg. snow)
225         if olddef_under.buildable_to then
226                 minetest.log("info", "node under is buildable to")
227                 place_to = {x = under.x, y = under.y, z = under.z}
228         end
229
230         if minetest.is_protected(place_to, placer:get_player_name()) then
231                 minetest.log("action", placer:get_player_name()
232                                 .. " tried to place " .. def.name
233                                 .. " at protected position "
234                                 .. minetest.pos_to_string(place_to))
235                 minetest.record_protection_violation(place_to, placer:get_player_name())
236                 return itemstack
237         end
238
239         minetest.log("action", placer:get_player_name() .. " places node "
240                 .. def.name .. " at " .. minetest.pos_to_string(place_to))
241         
242         local oldnode = minetest.get_node(place_to)
243         local newnode = {name = def.name, param1 = 0, param2 = param2}
244
245         -- Calculate direction for wall mounted stuff like torches and signs
246         if def.paramtype2 == 'wallmounted' and not param2 then
247                 local dir = {
248                         x = under.x - above.x,
249                         y = under.y - above.y,
250                         z = under.z - above.z
251                 }
252                 newnode.param2 = minetest.dir_to_wallmounted(dir)
253         -- Calculate the direction for furnaces and chests and stuff
254         elseif def.paramtype2 == 'facedir' and not param2 then
255                 local placer_pos = placer:getpos()
256                 if placer_pos then
257                         local dir = {
258                                 x = above.x - placer_pos.x,
259                                 y = above.y - placer_pos.y,
260                                 z = above.z - placer_pos.z
261                         }
262                         newnode.param2 = minetest.dir_to_facedir(dir)
263                         minetest.log("action", "facedir: " .. newnode.param2)
264                 end
265         end
266
267         -- Check if the node is attached and if it can be placed there
268         if minetest.get_item_group(def.name, "attached_node") ~= 0 and
269                 not check_attached_node(place_to, newnode) then
270                 minetest.log("action", "attached node " .. def.name ..
271                         " can not be placed at " .. minetest.pos_to_string(place_to))
272                 return itemstack, false
273         end
274
275         -- Add node and update
276         minetest.add_node(place_to, newnode)
277
278         local take_item = true
279
280         -- Run callback
281         if def.after_place_node then
282                 -- Deepcopy place_to and pointed_thing because callback can modify it
283                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
284                 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
285                 if def.after_place_node(place_to_copy, placer, itemstack,
286                                 pointed_thing_copy) then
287                         take_item = false
288                 end
289         end
290
291         -- Run script hook
292         local _, callback
293         for _, callback in ipairs(minetest.registered_on_placenodes) do
294                 -- Deepcopy pos, node and pointed_thing because callback can modify them
295                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
296                 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
297                 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
298                 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
299                 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
300                         take_item = false
301                 end
302         end
303
304         if take_item then
305                 itemstack:take_item()
306         end
307         return itemstack, true
308 end
309
310 function minetest.item_place_object(itemstack, placer, pointed_thing)
311         local pos = minetest.get_pointed_thing_position(pointed_thing, true)
312         if pos ~= nil then
313                 local item = itemstack:take_item()
314                 minetest.add_item(pos, item)
315         end
316         return itemstack
317 end
318
319 function minetest.item_place(itemstack, placer, pointed_thing, param2)
320         -- Call on_rightclick if the pointed node defines it
321         if pointed_thing.type == "node" and placer and
322                         not placer:get_player_control().sneak then
323                 local n = minetest.get_node(pointed_thing.under)
324                 local nn = n.name
325                 if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].on_rightclick then
326                         return minetest.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
327                                         placer, itemstack, pointed_thing) or itemstack, false
328                 end
329         end
330
331         if itemstack:get_definition().type == "node" then
332                 return minetest.item_place_node(itemstack, placer, pointed_thing, param2)
333         end
334         return itemstack
335 end
336
337 function minetest.item_drop(itemstack, dropper, pos)
338         if dropper.get_player_name then
339                 local v = dropper:get_look_dir()
340                 local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
341                 local obj = minetest.add_item(p, itemstack)
342                 if obj then
343                         v.x = v.x*2
344                         v.y = v.y*2 + 1
345                         v.z = v.z*2
346                         obj:setvelocity(v)
347                 end
348         else
349                 minetest.add_item(pos, itemstack)
350         end
351         return ItemStack("")
352 end
353
354 function minetest.item_eat(hp_change, replace_with_item)
355         return function(itemstack, user, pointed_thing)  -- closure
356                 if itemstack:take_item() ~= nil then
357                         user:set_hp(user:get_hp() + hp_change)
358                         itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
359                 end
360                 return itemstack
361         end
362 end
363
364 function minetest.node_punch(pos, node, puncher, pointed_thing)
365         -- Run script hook
366         for _, callback in ipairs(minetest.registered_on_punchnodes) do
367                 -- Copy pos and node because callback can modify them
368                 local pos_copy = vector.new(pos)
369                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
370                 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
371                 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
372         end
373 end
374
375 function minetest.handle_node_drops(pos, drops, digger)
376         -- Add dropped items to object's inventory
377         if digger:get_inventory() then
378                 local _, dropped_item
379                 for _, dropped_item in ipairs(drops) do
380                         local left = digger:get_inventory():add_item("main", dropped_item)
381                         if not left:is_empty() then
382                                 local p = {
383                                         x = pos.x + math.random()/2-0.25,
384                                         y = pos.y + math.random()/2-0.25,
385                                         z = pos.z + math.random()/2-0.25,
386                                 }
387                                 minetest.add_item(p, left)
388                         end
389                 end
390         end
391 end
392
393 function minetest.node_dig(pos, node, digger)
394         local def = ItemStack({name=node.name}):get_definition()
395         if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
396                 minetest.log("info", digger:get_player_name() .. " tried to dig "
397                         .. node.name .. " which is not diggable "
398                         .. minetest.pos_to_string(pos))
399                 return
400         end
401
402         if minetest.is_protected(pos, digger:get_player_name()) then
403                 minetest.log("action", digger:get_player_name()
404                                 .. " tried to dig " .. node.name
405                                 .. " at protected position "
406                                 .. minetest.pos_to_string(pos))
407                 minetest.record_protection_violation(pos, digger:get_player_name())
408                 return
409         end
410
411         minetest.log('action', digger:get_player_name() .. " digs "
412                 .. node.name .. " at " .. minetest.pos_to_string(pos))
413
414         local wielded = digger:get_wielded_item()
415         local drops = minetest.get_node_drops(node.name, wielded:get_name())
416         
417         local wdef = wielded:get_definition()
418         local tp = wielded:get_tool_capabilities()
419         local dp = minetest.get_dig_params(def.groups, tp)
420         if wdef and wdef.after_use then
421                 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
422         else
423                 -- Wear out tool
424                 if not minetest.setting_getbool("creative_mode") then
425                         wielded:add_wear(dp.wear)
426                 end
427         end
428         digger:set_wielded_item(wielded)
429         
430         -- Handle drops
431         minetest.handle_node_drops(pos, drops, digger)
432
433         local oldmetadata = nil
434         if def.after_dig_node then
435                 oldmetadata = minetest.get_meta(pos):to_table()
436         end
437
438         -- Remove node and update
439         minetest.remove_node(pos)
440         
441         -- Run callback
442         if def.after_dig_node then
443                 -- Copy pos and node because callback can modify them
444                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
445                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
446                 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
447         end
448
449         -- Run script hook
450         local _, callback
451         for _, callback in ipairs(minetest.registered_on_dignodes) do
452                 -- Copy pos and node because callback can modify them
453                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
454                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
455                 callback(pos_copy, node_copy, digger)
456         end
457 end
458
459 -- This is used to allow mods to redefine minetest.item_place and so on
460 -- NOTE: This is not the preferred way. Preferred way is to provide enough
461 --       callbacks to not require redefining global functions. -celeron55
462 local function redef_wrapper(table, name)
463         return function(...)
464                 return table[name](...)
465         end
466 end
467
468 --
469 -- Item definition defaults
470 --
471
472 minetest.nodedef_default = {
473         -- Item properties
474         type="node",
475         -- name intentionally not defined here
476         description = "",
477         groups = {},
478         inventory_image = "",
479         wield_image = "",
480         wield_scale = {x=1,y=1,z=1},
481         stack_max = 99,
482         usable = false,
483         liquids_pointable = false,
484         tool_capabilities = nil,
485         node_placement_prediction = nil,
486
487         -- Interaction callbacks
488         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
489         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
490         on_use = nil,
491         can_dig = nil,
492
493         on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
494         on_rightclick = nil,
495         on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
496
497         on_receive_fields = nil,
498         
499         on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
500         on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
501         on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
502
503         -- Node properties
504         drawtype = "normal",
505         visual_scale = 1.0,
506         -- Don't define these because otherwise the old tile_images and
507         -- special_materials wouldn't be read
508         --tiles ={""},
509         --special_tiles = {
510         --      {name="", backface_culling=true},
511         --      {name="", backface_culling=true},
512         --},
513         alpha = 255,
514         post_effect_color = {a=0, r=0, g=0, b=0},
515         paramtype = "none",
516         paramtype2 = "none",
517         is_ground_content = true,
518         sunlight_propagates = false,
519         walkable = true,
520         pointable = true,
521         diggable = true,
522         climbable = false,
523         buildable_to = false,
524         liquidtype = "none",
525         liquid_alternative_flowing = "",
526         liquid_alternative_source = "",
527         liquid_viscosity = 0,
528         drowning = 0,
529         light_source = 0,
530         damage_per_second = 0,
531         selection_box = {type="regular"},
532         legacy_facedir_simple = false,
533         legacy_wallmounted = false,
534 }
535
536 minetest.craftitemdef_default = {
537         type="craft",
538         -- name intentionally not defined here
539         description = "",
540         groups = {},
541         inventory_image = "",
542         wield_image = "",
543         wield_scale = {x=1,y=1,z=1},
544         stack_max = 99,
545         liquids_pointable = false,
546         tool_capabilities = nil,
547
548         -- Interaction callbacks
549         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
550         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
551         on_use = nil,
552 }
553
554 minetest.tooldef_default = {
555         type="tool",
556         -- name intentionally not defined here
557         description = "",
558         groups = {},
559         inventory_image = "",
560         wield_image = "",
561         wield_scale = {x=1,y=1,z=1},
562         stack_max = 1,
563         liquids_pointable = false,
564         tool_capabilities = nil,
565
566         -- Interaction callbacks
567         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
568         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
569         on_use = nil,
570 }
571
572 minetest.noneitemdef_default = {  -- This is used for the hand and unknown items
573         type="none",
574         -- name intentionally not defined here
575         description = "",
576         groups = {},
577         inventory_image = "",
578         wield_image = "",
579         wield_scale = {x=1,y=1,z=1},
580         stack_max = 99,
581         liquids_pointable = false,
582         tool_capabilities = nil,
583
584         -- Interaction callbacks
585         on_place = redef_wrapper(minetest, 'item_place'),
586         on_drop = nil,
587         on_use = nil,
588 }
589