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