]> git.lizzy.rs Git - minetest.git/blob - builtin/game/item.lua
Add strict module
[minetest.git] / builtin / game / 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 core.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 core.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 core.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 core.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 core.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 core.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 core.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 = core.get_node_or_nil(under)
200         local above = pointed_thing.above
201         local oldnode_above = core.get_node_or_nil(above)
202
203         if not oldnode_under or not oldnode_above then
204                 core.log("info", placer:get_player_name() .. " tried to place"
205                         .. " node in unloaded position " .. core.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 core.nodedef_default
211         local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
212         olddef_above = olddef_above or core.nodedef_default
213
214         if not olddef_above.buildable_to and not olddef_under.buildable_to then
215                 core.log("info", placer:get_player_name() .. " tried to place"
216                         .. " node in invalid position " .. core.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                 core.log("info", "node under is buildable to")
227                 place_to = {x = under.x, y = under.y, z = under.z}
228         end
229
230         if core.is_protected(place_to, placer:get_player_name()) then
231                 core.log("action", placer:get_player_name()
232                                 .. " tried to place " .. def.name
233                                 .. " at protected position "
234                                 .. core.pos_to_string(place_to))
235                 core.record_protection_violation(place_to, placer:get_player_name())
236                 return itemstack
237         end
238
239         core.log("action", placer:get_player_name() .. " places node "
240                 .. def.name .. " at " .. core.pos_to_string(place_to))
241         
242         local oldnode = core.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 = core.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 = core.dir_to_facedir(dir)
263                         core.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 core.get_item_group(def.name, "attached_node") ~= 0 and
269                 not check_attached_node(place_to, newnode) then
270                 core.log("action", "attached node " .. def.name ..
271                         " can not be placed at " .. core.pos_to_string(place_to))
272                 return itemstack, false
273         end
274
275         -- Add node and update
276         core.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(core.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 core.item_place_object(itemstack, placer, pointed_thing)
311         local pos = core.get_pointed_thing_position(pointed_thing, true)
312         if pos ~= nil then
313                 local item = itemstack:take_item()
314                 core.add_item(pos, item)
315         end
316         return itemstack
317 end
318
319 function core.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 = core.get_node(pointed_thing.under)
324                 local nn = n.name
325                 if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
326                         return core.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 core.item_place_node(itemstack, placer, pointed_thing, param2)
333         end
334         return itemstack
335 end
336
337 function core.item_drop(itemstack, dropper, pos)
338         if dropper.is_player then
339                 local v = dropper:get_look_dir()
340                 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
341                 local cs = itemstack:get_count()
342                 if dropper:get_player_control().sneak then
343                         cs = 1
344                 end
345                 local item = itemstack:take_item(cs)
346                 local obj = core.add_item(p, item)
347                 if obj then
348                         v.x = v.x*2
349                         v.y = v.y*2 + 2
350                         v.z = v.z*2
351                         obj:setvelocity(v)
352                 end
353
354         else
355                 core.add_item(pos, itemstack)
356         end
357         return itemstack
358 end
359
360 function core.item_eat(hp_change, replace_with_item)
361         return function(itemstack, user, pointed_thing)  -- closure
362                 for _, callback in pairs(core.registered_on_item_eats) do
363                         local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
364                         if result then
365                                 return result
366                         end
367                 end
368                 if itemstack:take_item() ~= nil then
369                         user:set_hp(user:get_hp() + hp_change)
370                         itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
371                 end
372                 return itemstack
373         end
374 end
375
376 function core.node_punch(pos, node, puncher, pointed_thing)
377         -- Run script hook
378         for _, callback in ipairs(core.registered_on_punchnodes) do
379                 -- Copy pos and node because callback can modify them
380                 local pos_copy = vector.new(pos)
381                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
382                 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
383                 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
384         end
385 end
386
387 function core.handle_node_drops(pos, drops, digger)
388         -- Add dropped items to object's inventory
389         if digger:get_inventory() then
390                 local _, dropped_item
391                 for _, dropped_item in ipairs(drops) do
392                         local left = digger:get_inventory():add_item("main", dropped_item)
393                         if not left:is_empty() then
394                                 local p = {
395                                         x = pos.x + math.random()/2-0.25,
396                                         y = pos.y + math.random()/2-0.25,
397                                         z = pos.z + math.random()/2-0.25,
398                                 }
399                                 core.add_item(p, left)
400                         end
401                 end
402         end
403 end
404
405 function core.node_dig(pos, node, digger)
406         local def = ItemStack({name=node.name}):get_definition()
407         if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
408                 core.log("info", digger:get_player_name() .. " tried to dig "
409                         .. node.name .. " which is not diggable "
410                         .. core.pos_to_string(pos))
411                 return
412         end
413
414         if core.is_protected(pos, digger:get_player_name()) then
415                 core.log("action", digger:get_player_name()
416                                 .. " tried to dig " .. node.name
417                                 .. " at protected position "
418                                 .. core.pos_to_string(pos))
419                 core.record_protection_violation(pos, digger:get_player_name())
420                 return
421         end
422
423         core.log('action', digger:get_player_name() .. " digs "
424                 .. node.name .. " at " .. core.pos_to_string(pos))
425
426         local wielded = digger:get_wielded_item()
427         local drops = core.get_node_drops(node.name, wielded:get_name())
428         
429         local wdef = wielded:get_definition()
430         local tp = wielded:get_tool_capabilities()
431         local dp = core.get_dig_params(def.groups, tp)
432         if wdef and wdef.after_use then
433                 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
434         else
435                 -- Wear out tool
436                 if not core.setting_getbool("creative_mode") then
437                         wielded:add_wear(dp.wear)
438                 end
439         end
440         digger:set_wielded_item(wielded)
441         
442         -- Handle drops
443         core.handle_node_drops(pos, drops, digger)
444
445         local oldmetadata = nil
446         if def.after_dig_node then
447                 oldmetadata = core.get_meta(pos):to_table()
448         end
449
450         -- Remove node and update
451         core.remove_node(pos)
452         
453         -- Run callback
454         if def.after_dig_node then
455                 -- Copy pos and node because callback can modify them
456                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
457                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
458                 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
459         end
460
461         -- Run script hook
462         local _, callback
463         for _, callback in ipairs(core.registered_on_dignodes) do
464                 -- Copy pos and node because callback can modify them
465                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
466                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
467                 callback(pos_copy, node_copy, digger)
468         end
469 end
470
471 -- This is used to allow mods to redefine core.item_place and so on
472 -- NOTE: This is not the preferred way. Preferred way is to provide enough
473 --       callbacks to not require redefining global functions. -celeron55
474 local function redef_wrapper(table, name)
475         return function(...)
476                 return table[name](...)
477         end
478 end
479
480 --
481 -- Item definition defaults
482 --
483
484 core.nodedef_default = {
485         -- Item properties
486         type="node",
487         -- name intentionally not defined here
488         description = "",
489         groups = {},
490         inventory_image = "",
491         wield_image = "",
492         wield_scale = {x=1,y=1,z=1},
493         stack_max = 99,
494         usable = false,
495         liquids_pointable = false,
496         tool_capabilities = nil,
497         node_placement_prediction = nil,
498
499         -- Interaction callbacks
500         on_place = redef_wrapper(core, 'item_place'), -- core.item_place
501         on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
502         on_use = nil,
503         can_dig = nil,
504
505         on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
506         on_rightclick = nil,
507         on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
508
509         on_receive_fields = nil,
510         
511         on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
512         on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
513         on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
514
515         -- Node properties
516         drawtype = "normal",
517         visual_scale = 1.0,
518         -- Don't define these because otherwise the old tile_images and
519         -- special_materials wouldn't be read
520         --tiles ={""},
521         --special_tiles = {
522         --      {name="", backface_culling=true},
523         --      {name="", backface_culling=true},
524         --},
525         alpha = 255,
526         post_effect_color = {a=0, r=0, g=0, b=0},
527         paramtype = "none",
528         paramtype2 = "none",
529         is_ground_content = true,
530         sunlight_propagates = false,
531         walkable = true,
532         pointable = true,
533         diggable = true,
534         climbable = false,
535         buildable_to = false,
536         liquidtype = "none",
537         liquid_alternative_flowing = "",
538         liquid_alternative_source = "",
539         liquid_viscosity = 0,
540         drowning = 0,
541         light_source = 0,
542         damage_per_second = 0,
543         selection_box = {type="regular"},
544         legacy_facedir_simple = false,
545         legacy_wallmounted = false,
546 }
547
548 core.craftitemdef_default = {
549         type="craft",
550         -- name intentionally not defined here
551         description = "",
552         groups = {},
553         inventory_image = "",
554         wield_image = "",
555         wield_scale = {x=1,y=1,z=1},
556         stack_max = 99,
557         liquids_pointable = false,
558         tool_capabilities = nil,
559
560         -- Interaction callbacks
561         on_place = redef_wrapper(core, 'item_place'), -- core.item_place
562         on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
563         on_use = nil,
564 }
565
566 core.tooldef_default = {
567         type="tool",
568         -- name intentionally not defined here
569         description = "",
570         groups = {},
571         inventory_image = "",
572         wield_image = "",
573         wield_scale = {x=1,y=1,z=1},
574         stack_max = 1,
575         liquids_pointable = false,
576         tool_capabilities = nil,
577
578         -- Interaction callbacks
579         on_place = redef_wrapper(core, 'item_place'), -- core.item_place
580         on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
581         on_use = nil,
582 }
583
584 core.noneitemdef_default = {  -- This is used for the hand and unknown items
585         type="none",
586         -- name intentionally not defined here
587         description = "",
588         groups = {},
589         inventory_image = "",
590         wield_image = "",
591         wield_scale = {x=1,y=1,z=1},
592         stack_max = 99,
593         liquids_pointable = false,
594         tool_capabilities = nil,
595
596         -- Interaction callbacks
597         on_place = redef_wrapper(core, 'item_place'),
598         on_drop = nil,
599         on_use = nil,
600 }