]> git.lizzy.rs Git - Crafter.git/blobdiff - mods/player/player_interaction.lua
Optimize some locals
[Crafter.git] / mods / player / player_interaction.lua
index 2b7b4005b549bd95da79edbde78cae02e4ba2809..029987102704581389c8bfbad93a85f64d4a794a 100644 (file)
@@ -1,11 +1,74 @@
+local minetest,armor_class,math = minetest,armor_class,math
+
 --hurt sound and disable fall damage group handling
 minetest.register_on_player_hpchange(function(player, hp_change, reason)
        if reason.type == "fall" then
                if minetest.get_item_group(minetest.get_node(player:get_pos()).name, "disable_fall_damage") > 0 then
                        return(0)
+               else
+                       --boots absorb fall damage
+                       local fall_damage = math.floor(player:get_player_velocity().y+0.5)+13
+                       --print("fall damage:",fall_damage)
+                       local inv = player:get_inventory()
+                       local stack = inv:get_stack("armor_feet", 1)
+                       local name = stack:get_name()
+                       if name ~= "" then
+                               local absorption = 0
+
+                               absorption = minetest.get_item_group(name,"armor_level")*2
+                               --print("absorbtion:",absorption)
+                               local wear_level = ((9-minetest.get_item_group(name,"armor_level"))*8)*(5-minetest.get_item_group(name,"armor_type"))*math.abs(fall_damage)
+                               
+                               stack:add_wear(wear_level)
+                               
+                               inv:set_stack("armor_feet", 1, stack)
+                               
+                               local new_stack = inv:get_stack("armor_feet",1):get_name()
+
+                       if new_stack == "" then                                 
+                                       minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=math.random(80,100)/100})
+                                       armor_class.recalculate_armor(player)
+                                       armor_class.set_armor_gui(player)
+                                       --do particles too
+                               elseif minetest.get_item_group(new_stack,"boots") > 0 then 
+                                       local pos = player:get_pos()
+                                       minetest.add_particlespawner({
+                                               amount = 30,
+                                               time = 0.00001,
+                                               minpos = {x=pos.x-0.5, y=pos.y+0.1, z=pos.z-0.5},
+                                               maxpos = {x=pos.x+0.5, y=pos.y+0.1, z=pos.z+0.5},
+                                               minvel = vector.new(-0.5,1,-0.5),
+                                               maxvel = vector.new(0.5 ,2 ,0.5),
+                                               minacc = {x=0, y=-9.81, z=1},
+                                               maxacc = {x=0, y=-9.81, z=1},
+                                               minexptime = 0.5,
+                                               maxexptime = 1.5,
+                                               minsize = 0,
+                                               maxsize = 0,
+                                               --attached = player,
+                                               collisiondetection = true,
+                                               collision_removal = true,
+                                               vertical = false,
+                                               node = {name= name.."particletexture"},
+                                               --texture = "eat_particles_1.png"
+                                       })
+                                       minetest.sound_play("armor_fall_damage", {object=player, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})   
+                               end
+
+                               fall_damage = fall_damage + absorption
+
+                               if fall_damage >= 0 then
+                                       fall_damage = 0
+                               else
+                                       minetest.sound_play("hurt", {object=player, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
+                               end
+                       else
+                               minetest.sound_play("hurt", {object=player, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
+                       end
+                       --print("returned fall damage",fall_damage)
+                       return(fall_damage)
                end
-       end
-       if hp_change < 0 then
+       elseif hp_change < 0 then
                minetest.sound_play("hurt", {object=player, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
        end
        return(hp_change)
@@ -21,7 +84,7 @@ minetest.register_on_dieplayer(function(player, reason)
                local name = stack:get_name()
                local count = stack:get_count()
                if name ~= "" then
-                       local obj = minetest.add_item(pos, name.." "..count)
+                       local obj = minetest.add_item(pos, stack)
                        if obj then
                                obj:set_velocity(vector.new(math.random(-3,3),math.random(4,8),math.random(-3,3)))
                        end
@@ -34,7 +97,7 @@ minetest.register_on_dieplayer(function(player, reason)
        local stack = inv:get_stack("armor_head", 1)
        local name = stack:get_name()
        if name ~= "" then
-               local obj = minetest.add_item(pos, name)
+               local obj = minetest.add_item(pos, stack)
                if obj then
                        obj:set_velocity(vector.new(math.random(-3,3),math.random(4,8),math.random(-3,3)))
                end
@@ -44,7 +107,7 @@ minetest.register_on_dieplayer(function(player, reason)
        stack = inv:get_stack("armor_torso", 1)
        name = stack:get_name()
        if name ~= "" then
-               local obj = minetest.add_item(pos, name)
+               local obj = minetest.add_item(pos, stack)
                if obj then
                        obj:set_velocity(vector.new(math.random(-3,3),math.random(4,8),math.random(-3,3)))
                end
@@ -54,7 +117,7 @@ minetest.register_on_dieplayer(function(player, reason)
        stack = inv:get_stack("armor_legs", 1)
        name = stack:get_name()
        if name ~= "" then
-               local obj = minetest.add_item(pos, name)
+               local obj = minetest.add_item(pos, stack)
                if obj then
                        obj:set_velocity(vector.new(math.random(-3,3),math.random(4,8),math.random(-3,3)))
                end
@@ -65,7 +128,7 @@ minetest.register_on_dieplayer(function(player, reason)
        stack = inv:get_stack("armor_feet", 1)
        name = stack:get_name()
        if name ~= "" then
-               local obj = minetest.add_item(pos, name)
+               local obj = minetest.add_item(pos, stack)
                if obj then
                        obj:set_velocity(vector.new(math.random(-3,3),math.random(4,8),math.random(-3,3)))
                end
@@ -73,7 +136,7 @@ minetest.register_on_dieplayer(function(player, reason)
        end
 
 
-       recalculate_armor(player)
+       armor_class.recalculate_armor(player)
 end)
 
 
@@ -169,29 +232,34 @@ local do_critical_particles = function(pos)
 end
 
 --we need to do this to override the default damage mechanics
-local punch_timers = {}
 minetest.register_on_joinplayer(function(player)
-       local name = player:get_player_name()
-       punch_timers[name] = 0
+       local meta = player:get_meta()
+       meta:set_float("player_punch_timer",0)
 end)
 
 minetest.register_globalstep(function(dtime)
        for _,player in ipairs(minetest.get_connected_players()) do
-               local name = player:get_player_name()
+               local meta = player:get_meta()
+               local punch_timer = meta:get_float("player_punch_timer")
+
                --limit this so the game engine isn't calculating huge floats
-               if punch_timers[name] and punch_timers[name] <= 10 then
-                       punch_timers[name] = punch_timers[name] + dtime
+               if punch_timer > 0 then
+                       punch_timer = punch_timer - dtime
+                       if punch_timer < 0 then punch_timer = 0 end
+                       meta:set_float("player_punch_timer",punch_timer)
                end
        end
 end)
 
 --this throws the player when they're punched and activates the custom damage mechanics
 minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
-       local name = player:get_player_name()
+       local meta = player:get_meta()
+       local punch_timer = meta:get_float("player_punch_timer")
        local hurt = tool_capabilities.damage_groups.damage
        local hp = player:get_hp()
-       if punch_timers[name] > 0.8 and hp > 0 then
-               if hitter:is_player() then
+       if punch_timer <= 0 and hp > 0 then
+               meta:set_float("player_punch_timer",0.25)
+               if hitter:is_player() and hitter ~= player then
                        local puncher_vel = hitter:get_player_velocity().y
                        if puncher_vel < 0 then
                                hurt = hurt * 1.5
@@ -207,7 +275,21 @@ minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch,
                if vel.y <= 0 then
                        dir.y = 7
                end
-               punch_timers[name] = 0
+
+               local hp_modifier = math.ceil(armor_class.calculate_armor_absorbtion(player)/3)
+               --print("hp_modifier:",hp_modifier)
+               armor_class.damage_armor(player,math.abs(hurt))
+
+               --print("hurt:",hurt,"|","hp_modifier:",hp_modifier)
+               local modify_output = (hurt == 0)
+               
+               hurt = hurt - hp_modifier
+
+               if modify_output == false and hurt <= 0 then
+                       hurt = 1
+               elseif modify_output == true then
+                       hurt = 0
+               end
                player:add_player_velocity(dir)
                player:set_hp(hp-hurt)
        end