forked from extremq/ninja
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
1383 lines (1221 loc) · 56.4 KB
/
init.lua
File metadata and controls
1383 lines (1221 loc) · 56.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- LOCALS FOR SPEED
local room = tfm.get.room
local displayParticle = tfm.exec.displayParticle
local movePlayer = tfm.exec.movePlayer
local setNameColor = tfm.exec.setNameColor
local addImage = tfm.exec.addImage
local bindKeyboard = system.bindKeyboard
local chatMessage = tfm.exec.chatMessage
local removeImage = tfm.exec.removeImage
local killPlayer = tfm.exec.killPlayer
local setPlayerScore = tfm.exec.setPlayerScore
local setMapName = ui.setMapName
local random = math.random
local addTextArea = ui.addTextArea
local removeTextArea = ui.removeTextArea
-- addImage = function() end
-- removeImage = function() end
local languages = {"ro", "en", "fr"}
local translations = {}
{% require-dir "translations" %}
-- Standard maps
stMapCodes = {{"@7725753", 3}, {"@7726015", 1}, {"@7726744", 2}, {"@7728063", 4}, {"@7731641", 2}, {"@7730637", 3}, {"@7732486", 2}, {"@6784223", 4}, {"@7734262", 3}, {"@7735744", 4}, {"@7735771", 3}, {"@7048028", 1}}
stMapsLeft = {{"@7725753", 3}, {"@7726015", 1}, {"@7726744", 2}, {"@7728063", 4}, {"@7731641", 2}, {"@7730637", 3}, {"@7732486", 2}, {"@6784223", 4}, {"@7734262", 3}, {"@7735744", 4}, {"@7735771", 3}, {"@7048028", 1}}
-- Hardcore maps
hcMapCodes = {{"@7733773", 6}, {"@7733777", 6}, {"@7734451", 6}}
hcMapsLeft = {{"@7733773", 6}, {"@7733777", 6}, {"@7734451", 6}}
modList = {['Extremq#0000'] = true, ['Railysse#0000'] = true}
modRoom = {}
opList = {}
lastMap = ""
mapWasSkipped = false
mapStartTime = 0
mapDiff = 0
mapCount = 1
VERSION = "1.5.4, 08.06.2020"
--CONSTANTS
MAPTIME = 4 * 60 + 3
BASETIME = MAPTIME -- after difficulty
STATSTIME = 10 * 1000
DASHCOOLDOWN = 1 * 1000
JUMPCOOLDOWN = 3 * 1000
REWINDCOOLDONW = 10 * 1000
GRAFFITICOOLDOWN = 15 * 1000
DASH_BTN_X = 675
DASH_BTN_Y = 340
JUMP_BTN_X = 740
JUMP_BTN_Y = 340
REWIND_BTN_X = 740
REWIND_BTN_Y = 275
MENU_BTN_X = 15
MENU_BTN_Y = 82
DASH_BTN_OFF = "172514f110f.png"
DASH_BTN_ON = "172514f2882.png"
JUMP_BTN_OFF = "172514f3ff1.png"
JUMP_BTN_ON = "172514f9089.png"
REWIND_BTN_OFF = "1725150689b.png"
REWIND_BTN_ON = "1725150800e.png"
REWIND_BTN_ACTIVE = "17257e94902.png"
HELP_IMG = "172533e3f7b.png"
CHECKPOINT_MOUSE = "17257fd86f3.png"
MENU_BUTTONS = "1725ce45065.png"
-- CHOOSE MAP
function randomMap(mapsLeft, mapCodes)
-- DELETE THE CHOSEN MAP
if #mapsLeft == 0 then
for key, value in pairs(mapCodes) do
table.insert(mapsLeft, value)
end
end
local pos = random(1, #mapsLeft)
local newMap = mapsLeft[pos]
-- IF THE MAPS ARE THE SAME, PICK AGAIN
if newMap[1] == lastMap then
table.remove(mapsLeft, pos)
pos = random(1, #mapsLeft)
newMap = mapsLeft[pos]
table.insert(mapsLeft, lastMap)
end
table.remove(mapsLeft, pos)
lastMap = newMap[1]
mapDiff = newMap[2]
MAPTIME = BASETIME + (mapDiff - 1) * 30
if mapDiff == 6 then
MAPTIME = 5 * 60
end
return newMap[1]
end
-- CHOOSE FLIP
function randomFlip()
local number = random()
mapStartTime = os.time()
if number < 0.5 then
return true
else
return false
end
end
tfm.exec.disableAutoTimeLeft(true)
tfm.exec.disableAutoScore(true)
tfm.exec.disableAutoShaman(true)
tfm.exec.disableAfkDeath(true)
tfm.exec.disableAutoNewGame(true)
tfm.exec.setAutoMapFlipMode(randomFlip())
tfm.exec.newGame(randomMap(stMapsLeft, stMapCodes))
tfm.exec.disablePhysicalConsumables(true)
system.disableChatCommandDisplay()
tfm.exec.setGameTime(MAPTIME, true)
keys = {0, 1, 2, 3, 32, 67, 71, 72, 77, 84, 88}
bestTime = 99999
function shopListing(values, imgId, tooltip, reqs)
return {
['values'] = values,
['imgId'] = imgId,
['tooltip'] = tooltip,
['reqs'] = reqs
}
end
shop = {
dashAcc = {
shopListing({3}, "1728b45b3eb.png", "This is the default particle.", "Free."),
shopListing({3, 31}, "1728b44464b.png", "Add some hearts to your dash!", "Secret."),
shopListing({3, 13}, "1728b442708.png", "Sleek. Just like you.", "Finish 1 map first.")
},
graffitiCol = {
shopListing('#ffffff', '#ffffff', "This is the default graffiti color.", "Free."),
shopListing('#000000', '#000000', "You're a dark person.", "Finish 10 maps."),
shopListing('#8c0404', '#8c0404', "Where's this... blood from?", "Dash 100 times.")
},
graffitiImgs = {
shopListing(nil, nil, "This is the default image (no image).", "Free."),
shopListing("17290c497e1.png", "17290c497e1.png", "Say cheese!", "Finish 1 harcore map.")
},
graffitiFonts = {
shopListing("Comic Sans MS", "Comic Sans MS", "This is the default font for graffitis.", "Free."),
shopListing("Papyrus", "Papyrus", "You seem old.", "Spray a graffiti 20."),
shopListing("Verdana", "Verdana", "A classic.", "Rewind 10 times.")
}
}
-- We save ids so when a player leaves we still have their id (mostly to remove graffitis)
playerIds = {}
playerStats = {
-- {
-- playtime = 0,
-- mapsFinished = 0,
-- mapsFinishedFirst = 0,
-- timesEnteredInHole = 0,
-- graffitiSprays = 0,
-- timesDashed = 0,
-- timesRewinded = 0,
-- hardcoreMaps = 0,
-- equipment = {0, 0, 0, 0}
-- }
}
cooldowns = {
-- id = {
-- lastDashTime = 0,
-- lastJumpTime = 0,
-- lastRewindTime = 0,
-- lastGraffitiTime = 0,
-- lastLeftPressTime = 0,
-- lastRightPressTime = 0,
-- lastJumpPressTime = 0,
-- checkpointTime = 0,
-- canRewind = false
-- }
}
imgs = {
-- id = {
-- jumpButtonId = 0,
-- dashButtonId = 0,
-- rewindButtonId = 0,
-- helpImgId = 0,
-- mouseImgId = 0,
-- menuImgId = 0
-- }
}
-- For efficiency
states = {
-- id = {
-- jumpState = false,
-- dashState = false,
-- rewindState = false
-- }
}
playerVars = {
-- id = {
-- playerBestTime = 0,
-- playerLastTime = 0,
-- playerPreferences = {true, true, false, false},
-- playerLanguage = "en",
-- playerFinished = false,
-- rewindPos = {x, y},
-- menuPage = 0,
-- helpOpen = false,
-- joinTime = os.time()
-- }
}
globalPlayerCount = 0
-- SCORE OF PLAYER
fastestplayer = -1
playerSortedBestTime = {}
-- TRUE/FALSE
playerCount = 0
playerWon = 0
mapfinished = false
admin = ""
customRoom = false
hasShownStats = false
-- RETURN PLAYER ID
function playerId(playerName)
return playerIds[playerName]
end
function showDashParticles(types, direction, x, y)
-- Only display particles to the players who haven't disabled the setting
for name, data in pairs(room.playerList) do
if playerVars[name].playerPreferences[2] == true then
for i = 1, #types do
displayParticle(types[i], x, y, random() * direction, random(), 0, 0, name)
displayParticle(types[i], x, y, random() * direction, -random(), 0, 0, name)
displayParticle(types[i], x, y, random() * direction, -random(), 0, 0, name)
displayParticle(types[i], x, y, random() * direction, -random(), 0, 0, name)
end
end
end
end
-- This is different because jump has other directions
function showJumpParticles(types, x, y)
-- Only display particles to the players who haven't disabled the setting
for name, data in pairs(room.playerList) do
if playerVars[name].playerPreferences[2] == true then
for i = 1, #types do
displayParticle(types[i], x, y, random(), -random()*2, 0, 0, name)
displayParticle(types[i], x, y, -random(), -random()*2, 0, 0, name)
displayParticle(types[i], x, y, -random(), -random()*2, 0, 0, name)
displayParticle(types[i], x, y, random(), -random()*2, 0, 0, name)
end
end
end
end
function showRewindParticles(type, playerName, x, y)
displayParticle(type, x, y, -random(), random(), 0, 0, playerName)
displayParticle(type, x, y, -random(), -random(), 0, 0, playerName)
displayParticle(type, x, y, -random(), -random(), 0, 0, playerName)
displayParticle(type, x, y, random(), -random(), 0, 0, playerName)
end
-- MOUSE POWERS
function eventKeyboard(playerName, keyCode, down, xPlayerPosition, yPlayerPosition)
local id = playerId(playerName)
local ostime = os.time()
-- Everything here is for gameplay, so we only check them if the player isnt dead
if room.playerList[playerName].isDead == false then
--[[
Because of the nature my dash works (both left and right keys share the same cooldown) I cannot shorten without checking for both
doublepress and keypress. (though i can make the checker variable an array but it would look ugly.)
]]--
if (keyCode == 0 or keyCode == 2) and ostime - cooldowns[playerName].lastDashTime > DASHCOOLDOWN then
local dashUsed = false
local direction = 1 -- assume its right
if keyCode == 0 then
direction = -1
end
-- we check wether its left or right and if we double-tapped or not (can't shorten this)
if keyCode == 2 and ostime - cooldowns[playerName].lastRightPressTime < 200 then
dashUsed = true;
elseif
keyCode == 0 and ostime - cooldowns[playerName].lastLeftPressTime < 200 then
dashUsed = true;
end
-- When we succesfully double tap without being on cooldown, we execute this.
if dashUsed == true then
-- Update cooldowns
cooldowns[playerName].lastDashTime = ostime
states[playerName].dashState = false
-- Update cd image
removeImage(imgs[playerName].dashButtonId)
imgs[playerName].dashButtonId = addImage(DASH_BTN_OFF, "&1", DASH_BTN_X, DASH_BTN_Y, playerName)
-- Update stats
playerStats[playerName].timesDashed = playerStats[playerName].timesDashed + 1
-- Move the palyer
movePlayer(playerName, 0, 0, true, 150 * direction, 0, false)
-- Now, we can change the 3 with whatever the player has equipped in the shop!
showDashParticles(shop.dashAcc[playerStats[playerName].equipment[1]].values, direction, xPlayerPosition, yPlayerPosition)
end
--[[
We check for the key, then if its a double press, then the cooldown. (by the way, if it fails to check, for example,
keyCode == 1 then it won't check the other conditions, so we put the most important conditions first then follow up with
those who are most likely to happen when we actually want to jump - its more likely that the player double presses when he has
the cooldown available instead of doublepressing when the cooldown is offline.)
]]--
elseif keyCode == 1 and ostime - cooldowns[playerName].lastJumpPressTime < 200 and ostime - cooldowns[playerName].lastJumpTime > JUMPCOOLDOWN then
-- Update cooldowns (press is for doublepress and the other for cooldown)
cooldowns[playerName].lastJumpTime = ostime
states[playerName].jumpState = false
-- Update jump cd image
removeImage(imgs[playerName].jumpButtonId)
imgs[playerName].jumpButtonId = addImage(JUMP_BTN_OFF, "&1", JUMP_BTN_X, JUMP_BTN_Y, playerName)
-- Update stats
playerStats[playerName].timesDashed = playerStats[playerName].timesDashed + 1
-- Move player
movePlayer(playerName, 0, 0, true, 0, -60, false)
-- Display jump particles
showJumpParticles(shop.dashAcc[playerStats[playerName].equipment[1]].values, xPlayerPosition, yPlayerPosition)
--[[
The rewind is a bit more complicated, since it has 3 states: available, in use, not available.
My first check is if I can rewind (state 2), then if my cooldown is available (state 1).
If state 1 is true, then next time we press space state 2 must be true. After we use state 2, we will be on cooldown.
The only states that enter this states 1 and 2.
]]--
elseif keyCode == 32 and ostime - cooldowns[playerName].lastRewindTime > REWINDCOOLDONW then
if cooldowns[playerName].canRewind == true then
-- Teleport the player to the checkpoint
movePlayer(playerName, playerVars[playerName].rewindPos[1], playerVars[playerName].rewindPos[2], false, 0, 0, false)
-- Update states & cooldowns
cooldowns[playerName].lastRewindTime = ostime
cooldowns[playerName].canRewind = false
-- Update hourglass
removeImage(imgs[playerName].rewindButtonId)
imgs[playerName].rewindButtonId = addImage(REWIND_BTN_ACTIVE, "&1", REWIND_BTN_X, REWIND_BTN_Y, playerName)
-- Remove the mouse (the checkpoint)
removeImage(imgs[playerName].mouseImgId)
-- Show teleport particle
displayParticle(36, xPlayerPosition, yPlayerPosition, 0, 0, 0, 0, nil)
-- Show random particles (only time we use this are when we create the checkpoint and when the checkpoint dies (3 times in the code))
showRewindParticles(2, playerName, xPlayerPosition, yPlayerPosition)
-- If the player didn't have cheese when he created the checkpoint, we remove it
if playerVars[playerName].rewindPos[3] == false then
tfm.exec.removeCheese(playerName)
end
-- Add to stats
playerStats[playerName].timesRewinded = playerStats[playerName].timesRewinded + 1
else
-- Update cooldowns
cooldowns[playerName].canRewind = true
cooldowns[playerName].checkpointTime = ostime
-- Save current player state (pos and cheese)
playerVars[playerName].rewindPos = {xPlayerPosition, yPlayerPosition, room.playerList[playerName].hasCheese}
-- Update hourglass
imgs[playerName].mouseImgId = addImage(CHECKPOINT_MOUSE, "_100", xPlayerPosition - 59/2, yPlayerPosition - 73/2, playerName)
removeImage(imgs[playerName].rewindButtonId)
imgs[playerName].rewindButtonId = addImage(REWIND_BTN_OFF, "&1", REWIND_BTN_X, REWIND_BTN_Y, playerName)
-- Show particles where we teleport to
showRewindParticles(2, playerName, playerVars[playerName].rewindPos[1], playerVars[playerName].rewindPos[2])
end
-- GRAFFITI (C)
elseif id ~= 0 and keyCode == 67 and ostime - cooldowns[playerName].lastGraffitiTime > GRAFFITICOOLDOWN then
-- Update cooldowns
cooldowns[playerName].lastGraffitiTime = ostime
-- Update stats
playerStats[playerName].graffitiSprays = playerStats[playerName].graffitiSprays + 1
-- Create graffiti
for player, data in pairs(room.playerList) do
local _id = data.id
-- If the player has graffitis enabled, we display them
if _id ~= 0 and playerVars[player].playerPreferences[1] == true then
addTextArea(id, "<p align='center'><font face='"..shop.graffitiFonts[playerStats[playerName].equipment[4]].imgId.."' size='16' color='"..shop.graffitiCol[playerStats[playerName].equipment[2]].imgId.."'>"..playerName.."</font></p>", player, xPlayerPosition - 300/2, yPlayerPosition - 25/2, 300, 25, 0x324650, 0x000000, 0, false)
end
end
end
-- This needs to be after dash/jump blocks.
if keyCode == 0 then
cooldowns[playerName].lastLeftPressTime = ostime
elseif keyCode == 1 then
cooldowns[playerName].lastJumpPressTime = ostime
elseif keyCode == 2 then
cooldowns[playerName].lastRightPressTime = ostime
end
end
-- These keys are for various other purposes
-- MORT (X) (mort is more likely to be called than the menu/help)
if keyCode == 88 then
killPlayer(playerName)
-- MENU (M)
elseif keyCode == 77 then
-- If we don't have the menu open, then we dont have an image
if imgs[playerName].menuImgId == -1 then
addTextArea(12, "<font color='#E9E9E9' size='10'><a href='event:ShopOpen'> "..translations[playerVars[playerName].playerLanguage].shopTitle.."</a>\n\n\n\n<a href='event:StatsOpen'> "..translations[playerVars[playerName].playerLanguage].profileTitle.."</a>\n\n\n\n<a href='event:LeaderOpen'> "..translations[playerVars[playerName].playerLanguage].leaderboardsTitle.."</a>\n\n\n\n<a href='event:SettingsOpen'> "..translations[playerVars[playerName].playerLanguage].settingsTitle.."</a>\n\n\n\n<a href='event:AboutOpen'> "..translations[playerVars[playerName].playerLanguage].aboutTitle.."</a>", playerName, 13, 103, 184, 220, 0x324650, 0x000000, 0, true)
imgs[playerName].menuImgId = addImage(MENU_BUTTONS, ":10", MENU_BTN_X, MENU_BTN_Y, playerName)
-- Else we had it already open, so we close the page
else
closePage(playerName)
end
-- OPEN GUIDE / HELP (H)
elseif keyCode == 72 then
-- Help system
if playerVars[playerName].helpOpen == false then
-- If we are on some page, we just update that textarea
if playerVars[playerName].menuPage ~= 0 then
updatePage("#ninja", translations[playerVars[playerName].playerLanguage].helpBody, playerName, "help")
else
createPage("#ninja", translations[playerVars[playerName].playerLanguage].helpBody, playerName, "help")
end
playerVars[playerName].helpOpen = true
elseif playerVars[playerName].helpOpen == true then
closePage(playerName)
playerVars[playerName].helpOpen = false
end
end
end
function eventPlayerDied(playerName)
local id = playerId(playerName)
playerVars[playerName].rewindPos = {0, 0, false}
-- Remove rewind Mouse
if imgs[playerName].mouseImgId ~= nil then
removeImage(imgs[playerName].mouseImgId)
end
end
-- UPDATE MAP NAME (custom timer)
function updateMapName(timeRemaining)
-- in case it hasn't loaded for some reason, we wait for 3 seconds
if MAPTIME * 1000 - timeRemaining < 3000 then
setMapName("Loading...<")
return
end
local floor = math.floor
local currentmapauthor = ""
local currentmapcode = ""
local difficulty = mapDiff
-- This part is in case anything bad happens to the values (sometimes tfm is crazy :D)
if room.xmlMapInfo == nil then
currentmapauthor = "?"
currentmapcode = "?"
else
currentmapauthor = room.xmlMapInfo.author
currentmapcode = "@"..room.xmlMapInfo.mapCode
end
if timeRemaining == nil then
timeRemaining = 0
end
local minutes = floor((timeRemaining/1000)/60)
local seconds = (floor(timeRemaining/1000)%60)
if seconds < 10 then
seconds = "0"..tostring(seconds)
end
if minutes < 10 then
minutes = "0"..tostring(minutes)
end
--print(currentmapcode.." "..currentmapauthor.." "..playerCount.." "..minutes.." "..seconds)
local difficultyMessage = "<J>"..difficulty.."/5</J>"
if difficulty == 6 then
difficultyMessage = "<R>HARDCORE</R>"
end
local name = currentmapauthor.." <G>-</G><N> "..currentmapcode.."</N> <G>-</G> Level: "..difficultyMessage.." <G>|<G> <N>Mice:</N> <J>"..playerCount.."</J> <G>|<G> <N>"..minutes..":"..seconds.."</N>"
-- Append record
if fastestplayer ~= -1 then
local record = (bestTime / 100)
name = name.." <G>|<G> <N2>Record: </N2><R>"..fastestplayer.." - "..record.."s</R>"
end
-- If the map is over, we show stats
if timeRemaining < 0 then
name = "STATISTICS TIME!"
end
name = name.."<"
setMapName(name)
end
function compare(a,b)
return a[2] < b[2]
end
function showStats()
-- Init some empty array
bestPlayers = {{"N/A", "N/A"}, {"N/A", "N/A"}, {"N/A", "N/A"}}
table.sort(playerSortedBestTime, compare)
for i = 1, #playerSortedBestTime do
if i == 4 then
break
end
bestPlayers[i][1] = playerSortedBestTime[i][1]
bestPlayers[i][2] = playerSortedBestTime[i][2]/100
end
local message = "\n\n\n\n\n\n\n<p align='center'>"
message = message.."<font color='#ffd700' size='24'>1. "..bestPlayers[1][1].." - "..bestPlayers[1][2].."s</font>\n"
message = message.."<font color='#c0c0c0' size='20'>2. "..bestPlayers[2][1].." - "..bestPlayers[2][2].."s</font>\n"
message = message.."<font color='#cd7f32' size='18'>3. "..bestPlayers[3][1].." - "..bestPlayers[3][2].."s</font></p>"
-- We open the stats for every player: if the player has a menu opened, we just update the text, otherwise create
for name, value in pairs(room.playerList) do
local _id = value.id
if playerVars[name].menuPage == 0 then
createPage(translations[playerVars[name].playerLanguage].leaderboardsTitle, message, name, "roomStats")
else
updatePage(translations[playerVars[name].playerLanguage].leaderboardsTitle, message, name, "roomStats")
end
end
-- If we had a best player, we update his firsts stat
if bestPlayers[1][1] ~= "N/A" then
playerStats[room.playerList[bestPlayers[1][1]].playerName].mapsFinishedFirst = playerStats[room.playerList[bestPlayers[1][1]].playerName].mapsFinishedFirst + 1
end
end
-- UI UPDATER & PLAYER RESPAWNER & REWINDER
function eventLoop(elapsedTime, timeRemaining)
local ostime = os.time()
-- Can't rely on elapsedTime
updateMapName(MAPTIME * 1000 - (ostime - mapStartTime))
--print(elapsedTime / 1000)
-- When time reaches 0, we kill everyone and show stats
if (elapsedTime >= MAPTIME * 1000 and elapsedTime < MAPTIME * 1000 + STATSTIME) then
for index, value in pairs(room.playerList) do
killPlayer(index)
end
if hasShownStats == false then
hasShownStats = true
showStats()
end
-- When passing the stats time or when skipping a map, we choose a new map
elseif elapsedTime >= MAPTIME * 1000 + STATSTIME or mapWasSkipped == true then
mapWasSkipped = false
mapCount = mapCount + 1
tfm.exec.setAutoMapFlipMode(randomFlip())
-- Choose maptipe
if mapCount % 5 == 0 then -- I don't want to run this yet
tfm.exec.newGame(randomMap(hcMapsLeft, hcMapCodes))
else
tfm.exec.newGame(randomMap(stMapsLeft, stMapCodes))
end
-- Reset player values.
resetAll()
-- Else we are currently in the round, we respawn/update the cooldown indicators
else
for playerName in pairs(room.playerList) do
local id = playerId(playerName)
-- RESPAWN PLAYER
tfm.exec.respawnPlayer(playerName)
-- UPDATE UI
--[[
This is where i use states: i basically keep track if i changed an icon's cooldown indicator. Why?
For example, lets say i have my cooldown ready. Without a state, i have no idea if i just got it now
or i had it already, so i have to remove the image and make it available, even if it was available.
With states, i can do it once and then just check if the state was changed (basically if i used the ability).
]]--
if states[playerName].jumpState == false and ostime - cooldowns[playerName].lastJumpTime > JUMPCOOLDOWN then
states[playerName].jumpState = true
removeImage(imgs[playerName].jumpButtonId)
imgs[playerName].jumpButtonId = addImage(JUMP_BTN_ON, "&1", JUMP_BTN_X, JUMP_BTN_Y, playerName)
end
if states[playerName].dashState == false and ostime - cooldowns[playerName].lastDashTime > DASHCOOLDOWN then
states[playerName].dashState = true
removeImage(imgs[playerName].dashButtonId)
imgs[playerName].dashButtonId = addImage(DASH_BTN_ON, "&1", DASH_BTN_X, DASH_BTN_Y, playerName)
end
-- Don't forget i have 3 states for rewind, this happens if we are in state 2 (can rewind) but passed the time we had.
if cooldowns[playerName].canRewind == true and ostime - cooldowns[playerName].checkpointTime > 3000 then
cooldowns[playerName].canRewind = false
cooldowns[playerName].lastRewindTime = ostime
removeImage(imgs[playerName].mouseImgId)
showRewindParticles(2, playerName, playerVars[playerName].rewindPos[1], playerVars[playerName].rewindPos[2])
end
if cooldowns[playerName].canRewind == true and states[playerName].rewindState ~= 2 then
states[playerName].rewindState = 2
removeImage(imgs[playerName].rewindButtonId)
imgs[playerName].rewindButtonId = addImage(REWIND_BTN_ACTIVE, "&1", REWIND_BTN_X, REWIND_BTN_Y, playerName)
elseif cooldowns[playerName].canRewind == false and states[playerName].rewindState ~= 1 and ostime - cooldowns[playerName].lastRewindTime > REWINDCOOLDONW then
states[playerName].rewindState = 1
removeImage(imgs[playerName].rewindButtonId)
imgs[playerName].rewindButtonId = addImage(REWIND_BTN_ON, "&1", REWIND_BTN_X, REWIND_BTN_Y, playerName)
elseif states[playerName].rewindState ~= 3 and ostime - cooldowns[playerName].lastRewindTime <= REWINDCOOLDONW then
states[playerName].rewindState = 3
removeImage(imgs[playerName].rewindButtonId)
imgs[playerName].rewindButtonId = addImage(REWIND_BTN_OFF, "&1", REWIND_BTN_X, REWIND_BTN_Y, playerName)
end
end
end
end
-- PLAYER COLOR SETTER
function eventPlayerRespawn(playerName)
local ostime = os.time()
id = playerId(playerName)
setColor(playerName)
-- UPDATE COOLDOWNS
cooldowns[playerName].lastJumpTime = ostime - JUMPCOOLDOWN
cooldowns[playerName].lastDashTime = ostime - DASHCOOLDOWN
cooldowns[playerName].lastRewindTime = ostime - 6000
cooldowns[playerName].checkpointTime = 0
cooldowns[playerName].canRewind = false
-- WHEN RESPAWNED, MAKE THE ABILITIES GREEN
removeImage(imgs[playerName].jumpButtonId)
imgs[playerName].jumpButtonId = addImage(JUMP_BTN_ON, "&1", JUMP_BTN_X, JUMP_BTN_Y, playerName)
removeImage(imgs[playerName].dashButtonId)
imgs[playerName].dashButtonId = addImage(DASH_BTN_ON, "&1", DASH_BTN_X, DASH_BTN_Y, playerName)
end
function setColor(playerName)
id = playerId(playerName)
local color = 0x40a594
-- IF BEST TIME
if playerName == fastestplayer then
color = 0xEB1D51
-- ELSEIF FINISHED
elseif playerVars[playerName].playerFinished == true then
color = 0xBABD2F
end
if modRoom[playerName] == true then
color = 0x2E72CB
end
setNameColor(playerName, color)
end
-- PLAYER WIN
function eventPlayerWon(playerName, timeElapsed, timeElapsedSinceRespawn)
local id = playerId(playerName)
if imgs[playerName].mouseImgId ~= nil then
removeImage(imgs[playerName].mouseImgId)
end
-- If we're a mod, then we don't count the win
if modRoom[playerName] == true or opList[playerName] == true then
return
end
playerStats[playerName].timesEnteredInHole = playerStats[playerName].timesEnteredInHole + 1
-- SEND CHAT MESSAGE FOR PLAYER
chatMessage(translations[playerVars[playerName].playerLanguage].finishedInfo.."(<V>"..(timeElapsedSinceRespawn/100).."s</V>)", playerName)
if playerVars[playerName].playerFinished == false then
playerStats[playerName].mapsFinished = playerStats[playerName].mapsFinished + 1
if mapDiff == 6 then
playerStats[playerName].hardcoreMaps = playerStats[playerName].hardcoreMaps + 1
end
playerWon = playerWon + 1
end
setPlayerScore(playerName, 1, true)
-- RESET TIMERS
playerVars[playerName].playerLastTime = timeElapsedSinceRespawn
playerVars[playerName].playerFinished = true
playerVars[playerName].playerBestTime = math.min(playerVars[playerName].playerBestTime, timeElapsedSinceRespawn)
--[[
If the player decides to leave and come back, we need to have his best time saved in a separate array.
This array will be used for stats at the end of the round, so it must work even if the player left,
came back, and had worse best time.
]]--
local foundvalue = false
for i = 1, #playerSortedBestTime do
if playerSortedBestTime[i][1] == playerName then
playerSortedBestTime[i][2] = math.min(playerVars[playerName].playerBestTime, playerSortedBestTime[i][2])
foundvalue = true
end
end
-- If this is the first time the player finishes the map, we take it as a best time.
if foundvalue == false then
table.insert(playerSortedBestTime, {playerName, playerVars[playerName].playerBestTime})
end
-- UPDATE "YOUR TIME"
ui.updateTextArea(5, "<p align='center'><font face='Lucida console' color='#ffffff'>"..translations[playerVars[playerName].playerLanguage].lastTime..": "..(timeElapsedSinceRespawn/100).."s", playerName)
ui.updateTextArea(4, "<p align='center'><font face='Lucida console' color='#ffffff'>"..translations[playerVars[playerName].playerLanguage].lastBestTime..": "..(playerVars[playerName].playerBestTime/100).."s", playerName)
-- bestTime is a global variable for record
if timeElapsedSinceRespawn <= bestTime then
bestTime = timeElapsedSinceRespawn
if fastestplayer ~= -1 then
local oldFastestPlayer = fastestplayer
fastestplayer = playerName
setColor(oldFastestPlayer)
else
fastestplayer = playerName
end
-- send message to everyone in their language
for index, value in pairs(room.playerList) do
local _id = room.playerList[index].id
local message = "<font color='#CB546B'>"..fastestplayer..translations[playerVars[index].playerLanguage].newRecord.." ("..(bestTime/100).."s)</font>"
chatMessage(message, index)
--print(message)
end
end
end
function eventPlayerLeft(playerName)
-- Throws an error if i retrieve playerId from room
local id = playerIds[playerName]
for player, data in pairs(room.playerList) do
removeTextArea(id, player)
end
-- We don't count souris
if string.find(playerName, '*') then
return
end
playerCount = playerCount - 1
end
-- CALL THIS WHEN A PLAYER FIRST JOINS A ROOM
function initPlayer(playerName)
-- ID USED FOR PLAYER OBJECTS
local id = room.playerList[playerName].id
playerIds[playerName] = id
-- NUMBER OF THE PLAYER SINCE MAP WAS CREATED
globalPlayerCount = globalPlayerCount + 1
-- IF FIRST PLAYER, (NEW MAP) MAKE ADMIN
if globalPlayerCount == 1 then
admin = playerName
end
modRoom[playerName] = false
opList[playerName] = false
-- BIND MOUSE
system.bindMouse(playerName, true)
-- CURRENT PLAYERCOUNT
playerCount = playerCount + 1
-- RESET SCORE
setPlayerScore(playerName, 0)
-- INIT PLAYER OBJECTS
cooldowns[playerName] = {
lastDashTime = 0,
lastJumpTime = 0,
lastRewindTime = 0,
lastGraffitiTime = 0,
lastLeftPressTime = 0,
lastRightPressTime = 0,
lastJumpPressTime = 0,
checkpointTime = 0,
canRewind = false
}
playerVars[playerName] = {
playerBestTime = 999999,
playerLastTime = 999999,
playerPreferences = {true, true, false, true},
playerLanguage = "en",
playerFinished = false,
rewindPos = {0, 0},
menuPage = 0,
helpOpen = false,
joinTime = os.time()
}
-- If the player finished
for key, value in pairs(playerSortedBestTime) do
if value[1] == playerName then
playerVars[playerName].playerFinished = true
end
end
playerStats[playerName] = {
playtime = 0,
mapsFinished = 0,
mapsFinishedFirst = 0,
timesEnteredInHole = 0,
graffitiSprays = 0,
timesDashed = 0,
timesRewinded = 0,
hardcoreMaps = 0,
equipment = {2, 3, 1, 3}
}
if playerName ~= "Extremq#0000" then
playerStats[playerName].equipment = {1, 1, 1, 1}
end
states[playerName] = {
jumpState = true,
dashState = true,
rewindState = 1
}
local jmpid = addImage(JUMP_BTN_ON, "&1", JUMP_BTN_X, JUMP_BTN_Y, playerName)
local dshid = addImage(DASH_BTN_ON, "&1", DASH_BTN_X, DASH_BTN_Y, playerName)
local rwdid = addImage(REWIND_BTN_ON, "&1", REWIND_BTN_X, REWIND_BTN_Y, playerName)
local hlpid = addImage(HELP_IMG, ":100", 114, 23, playerName)
addTextArea(10, "<a href='event:CloseWelcome'><font color='transparent'>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n <font></a>", playerName, 129, 29, 541, 342, 0x324650, 0x000000, 0, true)
imgs[playerName] = {
jumpButtonId = jmpid,
dashButtonId = dshid,
rewindButtonId = rwdid,
helpImgId = hlpid,
helpImgId = hlpid,
mouseImgId = nil,
menuImgId = -1,
shopWelcomeDash = nil,
shopWelcomeGraffiti = nil,
graffitiImg = nil
}
-- SET DEFAULT COLOR
setColor(playerName)
-- BIND KEYS
for index, key in pairs(keys) do
bindKeyboard(playerName, key, true, true)
end
-- AUTOMATICALLY CHOOSE LANGUAGE
chooselang(playerName)
generateHud(playerName)
end
function generateHud(playerName)
local id = playerId(playerName)
removeTextArea(6, playerName)
-- GENERATE UI
addTextArea(6, translations[playerVars[playerName].playerLanguage].helpToolTip, playerName, 267, 382, 265, 18, 0x324650, 0x000000, 0, true)
-- SEND HELP message
chatMessage(translations[playerVars[playerName].playerLanguage].welcomeInfo.."\n"..translations[playerVars[playerName].playerLanguage].devInfo.."\n"..translations[playerVars[playerName].playerLanguage].discordInfo, playerName)
end
function chooselang(playerName)
local id = playerId(playerName)
local community = room.playerList[playerName].community
-- FOR NOW, ONLY RO AND FR HAVE TRANSLATIONS
if community == "ro" then
playerVars[playerName].playerLanguage = "ro"
elseif community == "fr" then
playerVars[playerName].playerLanguage = "fr"
else
playerVars[playerName].playerLanguage = "en"
end
--print(translations[playerVars[id].playerLanguage].welcomeInfo)
--print(translations[playerVars[id].playerLanguage].devInfo)
end
-- WHEN SOMEBODY JOINS, INIT THE PLAYER
function eventNewPlayer(playerName)
initPlayer(playerName)
end
-- INIT ALL EXISTING PLAYERS
for playerName in pairs(room.playerList) do
initPlayer(playerName)
end
-- I need the X for mouse computations
function extractMapDimensions()
xml = tfm.get.room.xmlMapInfo.xml
local p = string.match(xml, '<P(.*)/>')
local x = string.match(p, 'L="(%d+)"')
if x == nil then
return 800
end
return tonumber(x)
end
function eventMouse(playerName, xMousePosition, yMousePosition)
local id = playerId(playerName)
local playerX = room.playerList[playerName].x
-- print("click at "..xMousePosition)
if modRoom[playerName] == true or opList[playerName] == true then
movePlayer(playerName, xMousePosition, yMousePosition, false, 0, 0, false)
else
--[[
I basically convert mouse coordinates into ui coordinates (only for x, i don't care about y)
in order to be able to open the menu when the mouse is in the left part of the screen.
:D
]]--
local uiMouseX = xMousePosition
local mapX = extractMapDimensions()
-- print("mapX ".. mapX)
if playerX > 400 and playerX < mapX - 400 then
uiMouseX = xMousePosition - (playerX - 400)
elseif playerX > mapX - 400 then
uiMouseX = xMousePosition - (mapX - 800)
end
-- print("uimouse "..uiMouseX)
if -100 <= uiMouseX and uiMouseX <= 250 then
if imgs[playerName].menuImgId == -1 then
addTextArea(12, "<font color='#E9E9E9' size='10'><a href='event:ShopOpen'> "..translations[playerVars[playerName].playerLanguage].shopTitle.."</a>\n\n\n\n<a href='event:StatsOpen'> "..translations[playerVars[playerName].playerLanguage].profileTitle.."</a>\n\n\n\n<a href='event:LeaderOpen'> "..translations[playerVars[playerName].playerLanguage].leaderboardsTitle.."</a>\n\n\n\n<a href='event:SettingsOpen'> "..translations[playerVars[playerName].playerLanguage].settingsTitle.."</a>\n\n\n\n<a href='event:AboutOpen'> "..translations[playerVars[playerName].playerLanguage].aboutTitle.."</a>", playerName, 13, 103, 184, 220, 0x324650, 0x000000, 0, true)
imgs[playerName].menuImgId = addImage(MENU_BUTTONS, ":10", MENU_BTN_X, MENU_BTN_Y, playerName)
else
closePage(playerName)
end
end
end
end
--[[
The way i manage UI in this module is basically this:
Every page of the UI is the same textarea.
When i open something for the first time, i use createPage.
When i open something and already have some ui active, i use updatePage.
This way i have standard UI and never have conflicts.
]]--
function pageOperation(title, body, playerName, pageId)
clear(playerName)
local id = playerId(playerName)
if playerVars[playerName].menuPage ~= "help" then
playerVars[playerName].helpOpen = false
end
local closebtn = "<p align='center'><font color='#CB546B'><a href='event:CloseMenu'>"..translations[playerVars[playerName].playerLanguage].Xbtn.."</a></font></p>"
local spaceLength = 40 - #translations[playerVars[playerName].playerLanguage].Xbtn - #title
local padding = ""
for i = 1, spaceLength do
padding = padding.." "
end
local pageTitle = "<font size='16' face='Lucida Console'>"..title.."<textformat>"..padding.."</textformat>"..closebtn.."</font>\n"
local pageBody = body
playerVars[playerName].menuPage = pageId
return pageTitle..pageBody
end
-- Used to create a page
function createPage(title, body, playerName, pageId)
ui.addTextArea(13, pageOperation(title, body, playerName, pageId), playerName, 198, 50, 406, 300, 0x241f13, 0xbfa26d, 1, true)
end
-- Used to update a page
function updatePage(title, body, playerName, pageId)
ui.updateTextArea(13, pageOperation(title, body, playerName, pageId), playerName)
end
-- Used to close a page
function closePage(playerName)
clear(playerName)
local id = playerId(playerName)
removeTextArea(13, playerName)
removeTextArea(12, playerName)
removeImage(imgs[playerName].menuImgId)
playerVars[playerName].menuPage = 0
playerVars[playerName].helpOpen = false
imgs[playerName].menuImgId = -1
end
-- Used to clear images from menu
function clear(playerName)
local page = playerVars[playerName].menuPage