-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
197 lines (177 loc) · 4.14 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
-- helpers:
local meaning = {
[0] = "up", "down", "left",
"right", "jump", "aux1",
"sneak", "LMB", "RMB"
}
local function bits_to_table(bits)
if type(bits) ~= "number" then
return bits
end
local t = {}
for i = #meaning, 0, -1 do
local n = 2^i
if bits >= n then
bits = bits - n
t[meaning[i]] = true
else
t[meaning[i]] = false
end
end
return t
end
local function table_to_bits(t)
if type(t) ~= "table" then
return t
end
local bits = 0
for i = 0, #meaning do
if t[meaning[i]] then
bits = bits + 2^i
end
end
return bits
end
local function make_true(t)
local out = {}
for i = 1, #t do
out[t[i]] = true
end
for i = 0, #meaning do
out[meaning[i]] = out[meaning[i]] or false
end
return out
end
local function look_differences(bitsa, bitsb)
if not bitsb then
return 511
end
local diff = 0
for i = #meaning, 0, -1 do
local n = 2^i
local a = bitsa - n >= 0
local b = bitsb - n >= 0
if a ~= b then
diff = diff + n
end
bitsa = bitsa - ((a and n) or 0)
bitsb = bitsb - ((b and n) or 0)
end
return diff
end
local function is_different(diff_shall, diff_is)
if diff_shall == 511 then
return true
end
for i = #meaning, 0, -1 do
local n = 2^i
local a = diff_shall - n >= 0
local b = diff_is - n >= 0
if a and b then
return true
end
diff_shall = diff_shall - ((a and n) or 0)
diff_is = diff_is - ((b and n) or 0)
end
return false
end
local function handle_keys(keys, func)
local keyst = type(keys)
if keyst == "function" then
func = keys
keys = 511
elseif keyst == "string" then
for i = 0, #meaning do
if keys == meaning[i] then
keys = 2^i
break
end
end
elseif keyst == "table" then
keys = table_to_bits((#keys >= 1 and make_true(keys)) or keys)
elseif keyst ~= "number" then
keys = 511
end
return keys, func
end
local function add_func_to_callback_origins(func)
-- this is taken from minetest/builtin/game/register.lua
minetest.callback_origins[func] = {
mod = core.get_current_modname() or "??",
name = debug.getinfo(1, "n").name or "??"
}
end
--------------------------------------------------------------------------------
-- global functions:
keyevent = {}
local keyevents_bits = {}
function keyevent.register_on_keypress_bits(keys, func)
keys, func = handle_keys(keys, func)
add_func_to_callback_origins(func)
if not keyevents_bits[keys] then
keyevents_bits[keys] = {func}
return
end
keyevents_bits[keys][#keyevents_bits[keys] + 1] = func
end
local keyevents = {}
function keyevent.register_on_keypress(keys, func)
keys, func = handle_keys(keys, func)
add_func_to_callback_origins(func)
if not keyevents[keys] then
keyevents[keys] = {func}
return
end
keyevents[keys][#keyevents[keys] + 1] = func
end
--------------------------------------------------------------------------------
-- use minetest registration fuctions:
local function on_step(dtime, player, old_keys, keys, player_name)
if keys == old_keys then
return
end
local diff = look_differences(keys, old_keys)
for diff_shall, fs in pairs(keyevents_bits) do
if is_different(diff_shall, diff) then
minetest.run_callbacks(fs, -1, keys, old_keys, dtime, player_name)
end
end
local keys_t = bits_to_table(keys)
local old_keys_t = bits_to_table(old_keys)
for diff_shall, fs in pairs(keyevents) do
if is_different(diff_shall, diff) then
minetest.run_callbacks(fs, -1, keys_t, old_keys_t, dtime, player_name)
end
end
return keys
end
if INIT == "client" then -- csm
local localplayer
minetest.register_on_connect(function()
localplayer = minetest.localplayer
end)
local keys
local f = on_step
function on_step(dtime)
if not localplayer then
return
end
keys = f(dtime, localplayer, keys, localplayer:get_key_pressed()) or keys
end
elseif INIT == "game" then -- ssm
local keys = {}
local f = on_step
function on_step(dtime)
local players = minetest.get_connected_players()
for i = 1, #players do
local player_name = players[i]:get_player_name()
keys[player_name] = f(dtime, players[i], keys[player_name],
players[i]:get_player_control_bits(), player_name) or
keys[player_name]
end
end
else
function on_step()
end
end
minetest.register_globalstep(on_step)