]> git.lizzy.rs Git - crafter_client.git/blob - player_input.lua
Fix crashing and make water splash pleasant
[crafter_client.git] / player_input.lua
1 --0 is nothing
2 --1 is up
3 --2 is down
4 --4 is left
5 --8 is right
6 --16 is jump
7 --32 is auxilary
8 --64 is sneak
9 --128 is left click
10 --256 is right click
11
12 --make the data from get_key_pressed usable
13 --Thanks Thou shalt use my mods!
14 function minetest.get_control_bits(player)
15         local input = player:get_key_pressed()
16         local input_table = {}
17         --iterate through the table using the highest value first
18         local keys = {"rightclick","leftclick","sneak","aux","jump","right","left","down","up"}
19         for index,data in pairs(keys) do
20                 local modifier = math.pow(2, 9-index)
21                 if input >= modifier then
22                         input_table[data] = true
23                         input = input - modifier
24                 else
25                         input_table[data] = false
26                 end
27         end
28         return(input_table)
29 end
30
31 --double tap running
32
33 --set up our initial values
34 local running = false
35 local run_discharge_timer = 0
36 local old_up = false
37 local sneak = false
38 local old_sneak = false
39 local bunny_hop = false
40
41 --attempt to tell the server to allow us to run
42 local send_server_movement_state = function(state)
43         player_movement_state:send_all(state)
44 end
45
46 --receive the server states
47 minetest.register_on_modchannel_message(function(channel_name, sender, message)
48         if channel_name == "player.player_movement_state" then
49                 running = message
50         end
51 end)
52
53 --check player's input on the "up" key
54 minetest.register_globalstep(function(dtime)
55         if not minetest.localplayer then
56                 return
57         end
58         
59         local input = minetest.get_control_bits(minetest.localplayer)
60         local vel = minetest.localplayer:get_velocity().y
61         local oldvel = minetest.localplayer:get_last_velocity().y
62         
63         --reset the run flag
64         if running == true and (input.up == false or input.sneak == true or input.down == true) then
65                 running = false
66                 bunny_hop = false
67                 send_server_movement_state("0")
68         end
69         
70         --add this here so the player can sneak
71         if input.sneak == true then
72                 sneak = true
73         end
74         
75         --stop bunnyhopping on land
76         if bunny_hop == true and vel == 0 and oldvel < 0 then
77                 bunny_hop = false
78         end
79         
80         --check if need to tell server to bunnyhop
81         if running == true and vel > 0 and input.jump == true and bunny_hop == false then
82                 send_server_movement_state("2")
83                 bunny_hop = true
84         elseif bunny_hop == false then
85                 if running == true then
86                         send_server_movement_state("1")
87                         bunny_hop = false
88                 elseif sneak == true then
89                         send_server_movement_state("3")
90                         bunny_hop = false
91                 else
92                         send_server_movement_state("0")
93                 end
94         end
95         
96         
97         
98         
99         --set the sneak state
100         if sneak == true and old_sneak == false then
101                 send_server_movement_state("3")
102         elseif input.sneak == false and old_sneak == true then
103                 sneak = false
104                 send_server_movement_state("0")
105         end
106         
107         --half second window to double tap running
108         if run_discharge_timer > 0 then
109                 run_discharge_timer = run_discharge_timer - dtime
110                 if run_discharge_timer <= 0 then
111                         run_discharge_timer = 0
112                 end
113                 --initialize double tap run
114                 if old_up == false and input.up == true then
115                         run_discharge_timer = 0
116                         running = true
117                         --print("running toggle on")
118                         send_server_movement_state("1")
119                 end
120         end
121         --check if new input of walking forwards
122         if input.up and input.down == false and input.sneak == false and old_up == false and running == false and run_discharge_timer <= 0 then
123                 run_discharge_timer = 0.2
124         end
125         --save old value
126         old_up = input.up
127         old_sneak = input.sneak
128 end)