]> git.lizzy.rs Git - crafter_client.git/blob - init.lua
Push incomplete version of doubletap running due to possible hardware failure
[crafter_client.git] / init.lua
1 --first we join the necessary channels so the mod can "listen" to what the server says
2 local weather = minetest.mod_channel_join("weather_nodes")
3 local weather_type = minetest.mod_channel_join("weather_type")
4 local running_send = minetest.mod_channel_join("running_send")
5 local running_receive = minetest.mod_channel_join("running_receive")
6
7
8 --we load everything seperately because it's easier to work on individual files than have everything jammed into one file
9 --not into seperate mods because that is unnecessary and cumbersome
10 local path = minetest.get_modpath("crafter_client")
11 dofile(path.."/weather_handling.lua")
12
13 --0 is nothing
14 --1 is up
15 --2 is down
16 --4 is left
17 --8 is right
18 --16 is jump
19 --32 is auxilary
20 --64 is sneak
21 --128 is left click
22 --256 is right click
23
24 --make the data from get_key_pressed usable
25 --Thanks Thou shalt use my mods!
26 function minetest.get_control_bits(player)
27         local input = player:get_key_pressed()
28         local input_table = {}
29         --iterate through the table using the highest value first
30         local keys = {"rightclick","leftclick","sneak","aux","jump","right","left","down","up"}
31         for index,data in pairs(keys) do
32                 local modifier = math.pow(2, 9-index)
33                 if input >= modifier then
34                         input_table[data] = true
35                         input = input - modifier
36                 else
37                         input_table[data] = false
38                 end
39         end
40         return(input_table)
41 end
42
43 --double tap running
44
45 --set up our initial values
46 local running = false
47 local run_discharge_timer = 0
48 local old_up = false
49
50 --attempt to tell the server to allow us to run
51 local send_server_run_state = function(state)
52         running_send:send_all(state)
53 end
54
55 --receive the server states
56 minetest.register_on_modchannel_message(function(channel_name, sender, message)
57         if channel_name == "running_receive" then
58                 running = (message == "true")
59         end
60 end)
61
62 --check player's input on the "up" key
63 minetest.register_globalstep(function(dtime)
64         local input = minetest.get_control_bits(minetest.localplayer)
65         
66         --reset the run flag
67         if running == true and (input.up == false or input.sneak == true or input.down == true) then
68                 running = false
69                 --print("running toggle off")
70                 send_server_run_state("false")
71         end
72         
73         --half second window to double tap running
74         if run_discharge_timer > 0 then
75                 run_discharge_timer = run_discharge_timer - dtime
76                 if run_discharge_timer <= 0 then
77                         run_discharge_timer = 0
78                 end
79                 --initialize double tap run
80                 if old_up == false and input.up == true then
81                         run_discharge_timer = 0
82                         running = true
83                         --print("running toggle on")
84                         send_server_run_state("true")
85                 end
86         end
87         --check if new input of walking forwards
88         if input.up and input.down == false and input.sneak == false and old_up == false and running == false and run_discharge_timer <= 0 then
89                 run_discharge_timer = 0.5
90         end
91         --save old value
92         old_up = input.up
93 end)
94
95