]> git.lizzy.rs Git - Crafter.git/blob - mods/player/player_interaction.lua
Use actual state modifiers with ticks to control satiation and hunger instead of...
[Crafter.git] / mods / player / player_interaction.lua
1 --hurt sound and disable fall damage group handling
2 minetest.register_on_player_hpchange(function(player, hp_change, reason)
3         if reason.type == "fall" then
4                 if minetest.get_node_group(minetest.get_node(player:get_pos()).name, "disable_fall_damage") > 0 then
5                         return(0)
6                 end
7         end
8         if hp_change < 0 then
9                 minetest.sound_play("hurt", {object=player, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
10         end
11         return(hp_change)
12 end, true)
13
14 --throw all items on death
15 minetest.register_on_dieplayer(function(player, reason)
16         local pos = player:getpos()
17         local inv = player:get_inventory()
18         
19         for i = 1,inv:get_size("main") do
20                 local stack = inv:get_stack("main", i)
21                 local name = stack:get_name()
22                 local count = stack:get_count()
23                 if name ~= "" then
24                         local obj = minetest.add_item(pos, name.." "..count)
25                         if obj then
26                                 obj:setvelocity(vector.new(math.random(-3,3),math.random(4,8),math.random(-3,3)))
27                         end
28                         inv:set_stack("main", i, ItemStack(""))
29                 else
30                         inv:set_stack("main", i, ItemStack(""))
31                 end
32         end
33 end)
34
35
36 --this dumps the players crafting table on closing the inventory
37 dump_craft = function(player)
38         local inv = player:get_inventory()
39         local pos = player:getpos()
40         pos.y = pos.y + player:get_properties().eye_height
41         for i = 1,inv:get_size("craft") do
42                 local item = inv:get_stack("craft", i)
43                 local obj = minetest.add_item(pos, item)
44                 if obj then
45                         local x=math.random(-2,2)*math.random()
46                         local y=math.random(2,5)
47                         local z=math.random(-2,2)*math.random()
48                         obj:setvelocity({x=x, y=y, z=z})
49                 end
50                 inv:set_stack("craft", i, nil)
51         end
52 end
53
54
55 --play sound to keep up with player's placing vs inconsistent client placing sound 
56 minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack, pointed_thing)
57         local node = minetest.registered_nodes[newnode.name]
58         local sound = node.sounds
59         local placing = ""
60         if sound then
61                 placing = sound.placing
62         end
63         --only play the sound when is defined
64         if type(placing) == "table" then
65                 minetest.sound_play(placing.name, {
66                           pos = pos,
67                           gain = placing.gain,
68                           --pitch = math.random(60,100)/100
69                 })
70         end
71 end)
72
73 --replace stack when empty (building)
74 minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack, pointed_thing)
75         local old = itemstack:get_name()
76         --pass through to check
77         minetest.after(0,function(pos, newnode, placer, oldnode, itemstack, pointed_thing,old)
78                 if not placer then
79                         return
80                 end
81                 local new = placer:get_wielded_item():get_name()
82                 if old ~= new and new == "" then
83                         local inv = placer:get_inventory()
84                         --check if another stack
85                         if inv:contains_item("main", old) then
86                                 --print("moving stack")
87                                 --run through inventory
88                                 for i = 1,inv:get_size("main") do
89                                         --if found set wielded item and remove old stack
90                                         if inv:get_stack("main", i):get_name() == old then
91                                                 local count = inv:get_stack("main", i):get_count()
92                                                 placer:set_wielded_item(old.." "..count)
93                                                 inv:set_stack("main",i,ItemStack(""))   
94                                                 minetest.sound_play("pickup", {
95                                                           to_player = player,
96                                                           gain = 0.7,
97                                                           pitch = math.random(60,100)/100
98                                                 })
99                                                 return                          
100                                         end
101                                 end
102                         end
103                 end
104         end,pos, newnode, placer, oldnode, itemstack, pointed_thing,old)
105 end)
106
107 --this throws the player when they're punched
108 minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
109         dir = vector.multiply(dir,10)
110         local vel = player:get_player_velocity()
111         dir.y = 0
112         if vel.y <= 0 then
113                 dir.y = 7
114         end
115         player:add_player_velocity(dir)
116 end)