]> git.lizzy.rs Git - crafter_client.git/blob - player_input.lua
Overhaul player input to stop spamming modchannel
[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 state = 0
35 local old_state = 0
36 local running = false
37 local run_discharge_timer = 0
38 local old_up = false
39 local sneak = false
40 local old_sneak = false
41 local bunny_hop = false
42 local old_bunny_hop = false
43
44 --attempt to tell the server to allow us to run
45 local send_server_movement_state = function(state)
46         player_movement_state:send_all(state)
47         --print(state)
48 end
49
50 --receive the server states
51 minetest.register_on_modchannel_message(function(channel_name, sender, message)
52         if channel_name == "player.player_movement_state" then
53                 running = message
54         end
55 end)
56
57 --check player's input on the "up" key
58 minetest.register_globalstep(function(dtime)
59
60         if not minetest.localplayer then
61                 return
62         end
63         
64         local input = minetest.get_control_bits(minetest.localplayer)
65         local vel = minetest.localplayer:get_velocity()
66         --local oldvel = minetest.localplayer:get_last_velocity()
67         
68         --cancel running if the player bumps into something
69         --this doesn't work because the engine's collision detection is messed up
70         --[[
71         print(vel.x,oldvel.x)
72         if running == true and ((vel.x == 0 and oldvel.x ~= 0) or (vel.z == 0 and oldvel.z ~= 0)) then
73                 running = false
74                 bunny_hop = false
75                 run_discharge_timer = 0
76                 state = 0
77         end
78         ]]--
79         
80         --reset the run flag
81         if running == true and (input.up == false or input.sneak == true or input.down == true) then
82                 running = false
83                 bunny_hop = false
84                 state = 0
85         end
86         
87         
88         --check if need to tell server to bunnyhop
89         if running == true and vel.y > 0 and input.jump == true and bunny_hop == false then
90                 state = 2
91                 bunny_hop = true
92         end
93         
94         --stop bunny hopping
95         if bunny_hop == true and input.jump == false and running == true and vel.y == 0 then
96                 bunny_hop = false
97                 state = 1
98         end
99         
100         
101         --half second window to double tap running
102         if run_discharge_timer > 0 then
103                 run_discharge_timer = run_discharge_timer - dtime
104                 if run_discharge_timer <= 0 then
105                         run_discharge_timer = 0
106                 end
107                 --initialize double tap run
108                 if old_up == false and input.up == true then
109                         run_discharge_timer = 0
110                         running = true
111                         state = 1
112                 end
113         end
114         
115         
116         --check if new input of walking forwards
117         if input.up and input.down == false and input.sneak == false and old_up == false and running == false and run_discharge_timer <= 0 then
118                 run_discharge_timer = 0.2
119         end
120         
121         --add this here so the player can sneak
122         if input.sneak == true then
123                 run_discharge_timer = 0
124                 sneak = true
125                 bunny_hop = false
126                 run = false
127         end
128         
129
130         --set the sneak state
131         if sneak == true and old_sneak == false then
132                 state = 3
133         elseif input.sneak == false and old_sneak == true then
134                 sneak = false
135                 state = 0
136         end
137         
138         --only send if state has changed
139         if state ~= old_state then
140                 send_server_movement_state(tostring(state))
141         end
142         
143         --save old value
144         old_up = input.up
145         old_sneak = input.sneak
146         old_bunny_hop = bunny_hop
147         old_state = state
148 end)