-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainScript.lua
More file actions
325 lines (305 loc) · 11.4 KB
/
Copy pathMainScript.lua
File metadata and controls
325 lines (305 loc) · 11.4 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
mouse = {}
autoNewGame = false
autoNewShaman = false
afkDeath = false
function eventNewPlayer(playerName)
for i = 1, 255 do
tfm.exec.bindKeyboard(playerName, i, true, true)
end
mouse[playerName] = {
["spawn"] = {
["id"] = 0,
["need"] = false,
["ghost"] = false,
["2steps"] = false,
["1stStep"] = {["was"] = false, ["x"] = 0, ["y"] = 0},
["speed"] = -1,
["absoluteAngle"] = false,
["angle"] = 0
},
["jump"] = false,
["res"] = {["x"] = -1, ["y"] = -1}
}
tfm.exec.respawnPlayer(playerName)
end
function eventPlayerLeft(playerName)
if mouse[playerName]["spawn"]["need"] == true or mouse[playerName]["jump"] == true then
system.bindMouse(playerName, false)
end
mouse[playerName] = nil
end
function eventPlayerDied(playerName)
if mouse[playerName] == nil then
return
end
tfm.exec.respawnPlayer(playerName)
if mouse[playerName]["res"]["x"] ~= -1 or mouse[playerName]["res"]["y"] ~= -1 then
tfm.exec.movePlayer(playerName, mouse[playerName]["res"]["x"], mouse[playerName]["res"]["y"], false, 0, 0, false)
end
end
function eventNewGame()
for playerName in pairs(tfm.get.room.playerList) do
mouse[playerName]["res"]["x"], mouse[playerName]["res"]["y"] = -1, -1
end
end
function eventKeyboard(playerName, key, down, x, y)
if key == 79 then --'o'
tfm.exec.setShaman(playerName)
elseif key == 80 then --'p'
tfm.exec.addShamanObject(24, x, y + 10)
elseif key == 105 then -- gray-9
tfm.exec.setNameColor(playerName, math.random(0, 0xFFFFFF))
elseif key == 16 then --'Shift'
tfm.exec.movePlayer(playerName, x, y - 50, false, 0, -10, false)
elseif key == 9 then do -- 'tab'
if mouse[playerName]["spawn"]["need"] == false and mouse[playerName]["jump"] == false then
system.bindMouse(playerName, true)
end
mouse[playerName]["spawn"]["need"] = true
end elseif key == 192 then do -- 'apostrophe: [`] '
mouse[playerName]["spawn"]["need"] = false
if mouse[playerName]["jump"] == false then
system.bindMouse(playerName, false)
end
end elseif key == 219 then -- '['
mouse[playerName]["spawn"]["id"] = 26 -- blue portal
elseif key == 221 then -- ']'
mouse[playerName]["spawn"]["id"] = 27 -- orange portal
elseif key == 66 then -- 'b'
mouse[playerName]["spawn"]["id"] = 59 -- bubble
elseif key == 120 then -- 'F9'
mouse[playerName]["spawn"]["id"] = -1
elseif key == 45 then -- 'insert'
tfm.exec.explosion(x, y, 10, 50, false)
elseif key == 115 then -- 'F4'
if mouse[playerName]["spawn"]["need"] == false and mouse[playerName]["jump"] == false then
system.bindMouse(playerName, true)
end
mouse[playerName]["jump"] = true
elseif key == 104 then do -- grey '8'
tfm.exec.killPlayer(playerName)
eventPlayerDied(playerName)
end
end
end
function eventChatCommand(playerName, message)
message = string.lower(message)
if message == "clear" then do
local ids = {}
local k = 1
for i in pairs(tfm.get.room.objectList) do
ids[k] = i
k = k + 1
end
for i = 1, k - 1 do
tfm.exec.removeObject(ids[i])
ids[i] = nil
end
ids = nil
end elseif sameStart(message, "spawn") == true then
mouse[playerName]["spawn"]["id"] = tonumber(string.sub(message, 7, string.len(message)))
elseif message == "autonewgame" then do
autoNewGame = not autoNewGame
tfm.exec.disableAutoNewGame(not autoNewGame)
end elseif message == "autonewshaman" then do
autoNewShaman = not autoNewShaman
tfm.exec.disableAutoShaman(not autoNewShaman)
end elseif message == "afkdeath" then do
afkDeath = not afkDeath
tfm.exec.disableAfkDeath(not afkDeath)
end elseif message == "ghost" then
mouse[playerName]["spawn"]["ghost"] = not mouse[playerName]["spawn"]["ghost"]
elseif message == "2steps" then
if mouse[playerName]["spawn"]["2steps"] == false then
mouse[playerName]["spawn"]["2steps"] = true
else do
mouse[playerName]["spawn"]["2steps"] = false
mouse[playerName]["spawn"]["1stStep"]["was"] = false
end
end
elseif message == "cheese" then
tfm.exec.giveCheese(playerName)
elseif message == "win" then
tfm.exec.playerVictory(playerName)
elseif message == "res" then do
pl = tfm.get.room.playerList[playerName]
mouse[playerName]["res"]["x"] = pl["x"]
mouse[playerName]["res"]["y"] = pl["y"]
end elseif message == "res null" then
mouse[playerName]["res"]["x"], mouse[playerName]["res"]["y"] = -1, -1
elseif sameStart(message, "speed") then do
mouse[playerName]["spawn"]["speed"] = tonumber(string.sub(message, 7, string.len(message)))
print(mouse[playerName]["spawn"]["speed"])
end elseif sameStart(message, "angle") then
mouse[playerName]["spawn"]["angle"] = tonumber(string.sub(message, 7, string.len(message)))
elseif message == "absoluteangle" then
mouse[playerName]["spawn"]["absoluteAngle"] = not mouse[playerName]["spawn"]["absoluteAngle"]
end
end
function killObject(objX, objY)
local best, bestVal = -1, 2000
-- local bestValDebug = 99999
for i, val in pairs(tfm.get.room.objectList) do
local iVal = math.pow(val["x"] - objX, 2) + math.pow(val["y"] - objY, 2)
if iVal < bestVal then
best = val["id"]
bestVal = iVal
end
-- if iVal < bestValDebug then
-- bestValDebug = iVal
-- end
end
-- print("Best: " .. bestValDebug)
if best ~= -1 then
tfm.exec.removeObject(best)
end
end
function eventMouse(playerName, x, y)
if mouse[playerName]["spawn"]["need"] == true then
if mouse[playerName]["spawn"]["id"] == -1 then
killObject(x, y)
elseif mouse[playerName]["spawn"]["2steps"] == false then
tfm.exec.addShamanObject(mouse[playerName]["spawn"]["id"], x, y, mouse[playerName]["spawn"]["angle"], 0, 0, mouse[playerName]["spawn"]["ghost"])
else
if mouse[playerName]["spawn"]["1stStep"]["was"] == false then do
mouse[playerName]["spawn"]["1stStep"]["x"] = x
mouse[playerName]["spawn"]["1stStep"]["y"] = y
mouse[playerName]["spawn"]["1stStep"]["was"] = true
end else do
mouse[playerName]["spawn"]["1stStep"]["was"] = false
local vx = x - mouse[playerName]["spawn"]["1stStep"]["x"] -- vector x0->x1
local vy = y - mouse[playerName]["spawn"]["1stStep"]["y"] -- vector y0->y1
local dx, dy
if mouse[playerName]["spawn"]["speed"] < 0 then do
local len = math.pow(vx, 2) + math.pow(vy, 2)
dx = vx * len / 24000
dy = vy * len / 24000
local ans = percentLow(dx, dy)
dx, dy = ans["a"], ans["b"]
end
else do
dx = mouse[playerName]["spawn"]["speed"] * sign(vx)
dy = math.abs(dx) * sign(vy)
end
end
-- print("x0 = " .. mouse[playerName]["spawn"]["1stStep"]["x"] .. ", y0 = " .. mouse[playerName]["spawn"]["1stStep"]["y"])
-- print("x1 = " .. x .. ", y1 = " .. y)
print("dx = " .. dx .. ", dy = " .. dy)
local angle
if mouse[playerName]["spawn"]["absoluteAngle"] == true then
angle = mouse[playerName]["spawn"]["angle"]
else
if mouse[playerName]["spawn"]["id"] == 17 then
angle = mouse[playerName]["spawn"]["angle"] + getAngleCannon(vx, vy)
else
angle = mouse[playerName]["spawn"]["angle"] + getAngle(vx, vy)
end
end
tfm.exec.addShamanObject(mouse[playerName]["spawn"]["id"],
mouse[playerName]["spawn"]["1stStep"]["x"],
mouse[playerName]["spawn"]["1stStep"]["y"],
angle, dx, dy, mouse[playerName]["spawn"]["ghost"]
)
end
end
end
end
if mouse[playerName]["jump"] == true then do
tfm.exec.movePlayer(playerName, x, y, false, 0, 0, false)
if mouse[playerName]["spawn"]["need"] == false then do
system.bindMouse(playerName, false)
end
end
mouse[playerName]["jump"] = false
end
end
end
function eventEmotePlayed(playerName, emo)
if emo == 7 then
tfm.exec.setShaman(playerName)
elseif emo == 4 then
tfm.exec.setVampirePlayer(playerName)
end
end
function sameStart(str1, str2)
local len = math.min(string.len(str1), string.len(str2))
if string.sub(str1, 1, len) == string.sub(str2, 1, len) then
return true
else
return false
end
end
function absMin(a, b)
if math.abs(a) < math.abs(b) then
return a
else
return b
end
end
function sign(a)
if a > 0 then
return 1
elseif a == 0 then
return 0
else
return -1
end
end
function percentLow(a, b)
local swap = false
if absMin(40, b) == b and absMin(40, a) == a then
return {["a"] = a, ["b"] = b}
end
if absMin(a, b) == b then do
a, b = b, a
swap = true
end
end
local newB = 40 * sign(b)
local newA = a / b * newB
if swap == true then
newA, newB = newB, newA
end
return {["a"] = newA, ["b"] = newB}
end
function getAngle(x, y)
y = -y
if x == 0 then
if y >= 0 then
return -90
else
return 90
end
elseif y == 0 then
if x >= 0 then
return 0
else
return -180
end
end
if x > 0 then
return -math.deg(math.atan(y / x))
else
return -180 - math.deg(math.atan(y / x))
end
end
function getAngleCannon(x, y)
return getAngle(x, y) + 90
end
-- programm
for playerName in pairs(tfm.get.room.playerList) do
eventNewPlayer(playerName)
end
tfm.exec.disableAutoShaman(not autoNewShaman)
tfm.exec.disableAutoNewGame(not autoNewGame)
tfm.exec.disableAutoTimeLeft(true)
tfm.exec.disableAfkDeath(not afkDeath)
system.disableChatCommandDisplay("spawn", true)
system.disableChatCommandDisplay("ghost", true)
system.disableChatCommandDisplay("2steps", true)
system.disableChatCommandDisplay("angle", true)
system.disableChatCommandDisplay("absoluteangle", true)
system.disableChatCommandDisplay("speed", true)
system.disableChatCommandDisplay("win", true)
system.disableChatCommandDisplay("cheese", true)
system.disableChatCommandDisplay("res", true)