]> git.lizzy.rs Git - crafter_client.git/commitdiff
Overhaul player input to stop spamming modchannel
authoroilboi <47129783+oilboi@users.noreply.github.com>
Sat, 11 Apr 2020 06:25:57 +0000 (02:25 -0400)
committeroilboi <47129783+oilboi@users.noreply.github.com>
Sat, 11 Apr 2020 06:25:57 +0000 (02:25 -0400)
player_input.lua

index 56932b4a662e7085677bb121d87d655e972fce6c..358b05ec227154f82674fd23397f91abbfcd8c33 100644 (file)
@@ -31,16 +31,20 @@ end
 --double tap running
 
 --set up our initial values
+local state = 0
+local old_state = 0
 local running = false
 local run_discharge_timer = 0
 local old_up = false
 local sneak = false
 local old_sneak = false
 local bunny_hop = false
+local old_bunny_hop = false
 
 --attempt to tell the server to allow us to run
 local send_server_movement_state = function(state)
        player_movement_state:send_all(state)
+       --print(state)
 end
 
 --receive the server states
@@ -52,58 +56,48 @@ end)
 
 --check player's input on the "up" key
 minetest.register_globalstep(function(dtime)
+
        if not minetest.localplayer then
                return
        end
        
        local input = minetest.get_control_bits(minetest.localplayer)
-       local vel = minetest.localplayer:get_velocity().y
-       local oldvel = minetest.localplayer:get_last_velocity().y
+       local vel = minetest.localplayer:get_velocity()
+       --local oldvel = minetest.localplayer:get_last_velocity()
        
-       --reset the run flag
-       if running == true and (input.up == false or input.sneak == true or input.down == true) then
+       --cancel running if the player bumps into something
+       --this doesn't work because the engine's collision detection is messed up
+       --[[
+       print(vel.x,oldvel.x)
+       if running == true and ((vel.x == 0 and oldvel.x ~= 0) or (vel.z == 0 and oldvel.z ~= 0)) then
                running = false
                bunny_hop = false
-               send_server_movement_state("0")
+               run_discharge_timer = 0
+               state = 0
        end
+       ]]--
        
-       --add this here so the player can sneak
-       if input.sneak == true then
-               sneak = true
-       end
-       
-       --stop bunnyhopping on land
-       if bunny_hop == true and vel == 0 and oldvel < 0 then
+       --reset the run flag
+       if running == true and (input.up == false or input.sneak == true or input.down == true) then
+               running = false
                bunny_hop = false
+               state = 0
        end
        
+       
        --check if need to tell server to bunnyhop
-       if running == true and vel > 0 and input.jump == true and bunny_hop == false then
-               send_server_movement_state("2")
+       if running == true and vel.y > 0 and input.jump == true and bunny_hop == false then
+               state = 2
                bunny_hop = true
-       elseif bunny_hop == false then
-               if running == true then
-                       send_server_movement_state("1")
-                       bunny_hop = false
-               elseif sneak == true then
-                       send_server_movement_state("3")
-                       bunny_hop = false
-               else
-                       send_server_movement_state("0")
-               end
        end
        
-       
-       
-       
-       --set the sneak state
-       if sneak == true and old_sneak == false then
-               send_server_movement_state("3")
-       elseif input.sneak == false and old_sneak == true then
-               sneak = false
-               send_server_movement_state("0")
+       --stop bunny hopping
+       if bunny_hop == true and input.jump == false and running == true and vel.y == 0 then
+               bunny_hop = false
+               state = 1
        end
        
+       
        --half second window to double tap running
        if run_discharge_timer > 0 then
                run_discharge_timer = run_discharge_timer - dtime
@@ -114,15 +108,41 @@ minetest.register_globalstep(function(dtime)
                if old_up == false and input.up == true then
                        run_discharge_timer = 0
                        running = true
-                       --print("running toggle on")
-                       send_server_movement_state("1")
+                       state = 1
                end
        end
+       
+       
        --check if new input of walking forwards
        if input.up and input.down == false and input.sneak == false and old_up == false and running == false and run_discharge_timer <= 0 then
                run_discharge_timer = 0.2
        end
+       
+       --add this here so the player can sneak
+       if input.sneak == true then
+               run_discharge_timer = 0
+               sneak = true
+               bunny_hop = false
+               run = false
+       end
+       
+
+       --set the sneak state
+       if sneak == true and old_sneak == false then
+               state = 3
+       elseif input.sneak == false and old_sneak == true then
+               sneak = false
+               state = 0
+       end
+       
+       --only send if state has changed
+       if state ~= old_state then
+               send_server_movement_state(tostring(state))
+       end
+       
        --save old value
        old_up = input.up
        old_sneak = input.sneak
+       old_bunny_hop = bunny_hop
+       old_state = state
 end)