This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Carsa's Commands.lua
1910 lines (1815 loc) · 60.1 KB
/
Carsa's Commands.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
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
VERSION = 1.03
ABOUT = {
{title="Add-On Name:", text="Carsa's Commands"},
{title="Version:", text=VERSION},
{title="More Info:", text="Look on the Steam Workshop for this script's page"}
}
PREFERENCE_DEFAULTS = {
auth_all = true,
cheats = true,
equip_on_respawn = true,
keep_inventory = false,
remove_vehicle_on_leave = true,
start_equipment = {15, 6, 11},
welcome_new = "Welcome to my server. Here are some rules:",
welcome_returning = "Welcome back to my server"
}
PLAYER_DATA_DEFAULTS = {
owner = false,
admin = false,
auth = false
}
DEFAULT_RULES = {
"Be kind to others",
"Please do not purposely lag the server"
}
STEAM_ID_MIN = "76561197960265729"
CAREER_SETTINGS = {true, true, true, true, true, true, false, false, false, false, true, false, false, false, true, nil, nil, nil, false, false, false, false, false, true, false, false, false, false, true, false, true, true, true, false}
CREATIVE_SETTINGS = {true, true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, nil, nil, nil, true, true, false, false, true, true, true, false, true, true, true, true, false, false, true}
CHEAT_SETTINGS = {3, 4, 5, 6, 7, 8, 10, 11, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 34}
LINE = "---------------------------------------------------------------------------"
IS_NEW_SAVE = false
MAKE_DS_OWNER = false
GAME_SETTING_OPTIONS = {
"third_person",
"third_person_vehicle",
"vehicle_damage",
"player_damage",
"npc_damage",
"sharks",
"fast_travel",
"teleport_vehicle",
"rogue_mode",
"auto_refuel",
"megalodon",
"map_show_players",
"map_show_vehicles",
"show_3d_waypoints",
"show_name_plates",
nil, -- day/night length
nil, -- sunrise
nil, -- sunset
"infinite_money",
"settings_menu",
"unlock_all_islands",
"infinite_batteries",
"infinite_fuel",
"engine_overheating",
"no_clip",
"map_teleport",
"cleanup_vehicle",
"clear_fow",
"vehicle_spawning",
"photo_mode",
"respawning",
"settings_menu_lock",
"despawn_on_leave",
"unlock_all_components"
}
-- 1 = clothing, 2 = large, 3 = small
EQUIPMENT_SLOTS = {2, 3, 3, 3, 3, 1}
EQUIPMENT_DATA = {
{name="diving suit", size=1, charges=0, ammo=100},
{name="firefighter", size=1, charges=0, ammo=0},
{name="scuba suit", size=1, charges=0, ammo=100},
{name="parachute", size=1, charges=1, ammo=0},
{name="parka", size=1, charges=0, ammo=0},
{name="binoculars", size=3, charges=0, ammo=0},
{name="cable", size=2, charges=0, ammo=0},
{name="compass", size=3, charges=0, ammo=0},
{name="defibrillator", size=2, charges=4, ammo=0},
{name="fire extinguisher", size=2, charges=0, ammo=9},
{name="first aid", size=3, charges=4, ammo=0},
{name="flare", size=3, charges=4, ammo=0},
{name="flaregun", size=3, charges=1, ammo=0},
{name="flaregun ammo", size=3, charges=4, ammo=0},
{name="flashlight", size=3, charges=0, ammo=100},
{name="hose", size=2, charges=0, ammo=0},
{name="night vision binoculars", size=3, charges=0, ammo=100},
{name="oxygen mask", size=3, charges=0, ammo=100},
{name="radio", size=3, charges=0, ammo=100},
{name="radio signal locator", size=2, charges=0, ammo=100},
{name="remote control", size=3, charges=0, ammo=100},
{name="rope", size=2, charges=0, ammo=0},
{name="strobe light", size=3, charges=0, ammo=100},
{name="strobe light infrared", size=3, charges=0, ammo=100},
{name="transponder", size=3, charges=0, ammo=100},
{name="underwater welding torch", size=2, charges=0, ammo=250},
{name="welding torch", size=2, charges=0, ammo=400},
{name="coal", size=3, charges=0, ammo=0},
{name="hazmat", size=1, charges=0, ammo=0},
{name="radiation detector", size=3, charges=0, ammo=100}
}
function clamp(v,low,high)
return math.min(math.max(v,low),high)
end
function numberToBool(n)
local t = {true , false}
return t[n] or nil
end
function stringToBool(value)
local stringToBool = {["true"] = true, ["false"] = false, ["1"] = true, ["0"] = false}
return stringToBool[value]
end
-- converts strings to round integers
function toInteger(n)
local num = tonumber(n)
if type(num) == "number" then
return math.floor(num)
else
return nil
end
end
-- returns the player's name and id formatted nicely
function playerName(peer_id)
return (server.getPlayerName(peer_id)).."("..string.format("%.0f", peer_id)..")"
end
function getSteamID(peer_id)
return PLAYER_LIST[peer_id].steam_id or nil
end
-- reformats server.getPlayerList() to be useful
function reformatPlayerList(list)
local player_list = {}
for k, v in pairs(list) do
player_list[v.id] = {steam_id = tostring(v.steam_id), name = v.name}
end
return player_list
end
-- general error reporting
function throwError(errorMessage, peer_id)
server.announce("ERROR", errorMessage..". Please visit the workshop page for this script to see how you can file a bug report.", peer_id or -1)
end
-- warn of non-critical issue
function throwWarning(warningMessage, peer_id)
server.announce("WARNING", warningMessage, peer_id or -1)
end
-- report bad inputs to user
function invalidArgument(peer_id, place, text)
server.announce("FAILED", "Argument "..place.." must be "..text, peer_id)
end
-- checks user input type and throws generic error if not valid
function validateInput(peer_id, input, place, accepts)
local text = ""
if type(accepts) == "table" then
for k, v in ipairs(accepts) do
if type(input) == v then
return true
end
if k > 1 then
text = text..", "..v
else
text = v
end
end
else
if type(input) == accepts then
return true
end
text = accepts
end
invalidArgument(peer_id, place, text)
return false
end
-- refreshes vehicle popups
function refreshVehicleUI()
for peer_index, peer_id in ipairs(ID_VIEWERS) do
for vehicle_id, vehicle_data in pairs(VEHICLE_LIST) do
local vehicle_pos, found = server.getVehiclePos(vehicle_id)
if found then
server.setPopup(peer_id, vehicle_data.ui_id, "", true, vehicle_id.."\n"..playerName(vehicle_data.owner), vehicle_pos[13], vehicle_pos[14], vehicle_pos[15], 50)
else
server.removePopup(peer_id, vehicle_data.ui_id)
end
end
end
end
-- refreshes popups to indicate the user is blocking teleports
function drawDenyTeleportUI(peer_id)
local data = PLAYER_DATA[getSteamID(peer_id)]
if data.deny_tp then
data.deny_tp_ui_id = server.getMapID()
server.setPopupScreen(peer_id, data.deny_tp_ui_id, "deny_tp", true, "Denying Teleports", 0.34, -0.88)
else
if data.deny_tp_ui_id then
server.removeMapID(peer_id, data.deny_tp_ui_id)
end
end
end
-- sorts a table by it's values' first value
function quicksort(A, lo, hi)
if lo < hi then
local p = partition(A, lo, hi)
quicksort(A, lo, p - 1)
quicksort(A, p + 1, hi)
end
return A
end
-- part of the quicksort algorithm
function partition(A, lo, hi)
local pivot = A[hi][1]
local i = lo
for j = lo, hi do
if A[j][1] < pivot then
A[i], A[j] = A[j], A[i]
i = i + 1
end
end
A[i], A[hi] = A[hi], A[i]
return i
end
-- sorts a table by it's keys
function sortByKeys(t)
local numbered_table = {}
for k, v in pairs(t) do
table.insert(numbered_table, k)
end
table.sort(numbered_table)
return numbered_table
end
-- converts multiple table values into one string
function concatTable(t, seperator)
local text = ""
for k, v in ipairs(t) do
if #text > 0 then
text = text..(seperator or " ")
end
text = text..tostring(v)
end
return text
end
-- returns a character's inventory as a table
function getInventory(character_id)
local inventory = {}
for i=1, #EQUIPMENT_SLOTS do
local equipment_id, is_success = server.getCharacterItem(character_id, i)
if not is_success then
throwWarning("Could not get character's inventory. Defaulting to empty.")
end
table.insert(inventory, (is_success and equipment_id) or 0)
end
return inventory
end
-- looks at a player's inventory and gives equipment where it fits.
function equipPlayer(peer_id, equipment_list, first_spawn)
local character_id, success = server.getPlayerCharacterID(peer_id)
if success then
-- get current inventory and find empty slots
local inventory = getInventory(character_id)
local open_slots = {}
for k, v in ipairs(EQUIPMENT_SLOTS) do
if not open_slots[v] then
open_slots[v] = {}
end
if (not first_spawn and inventory[k] == 0) or first_spawn then
table.insert(open_slots[v], k)
end
end
-- index requested equipment by size
local requested_inventory = {}
local new_inventory = {}
for i=1, clamp(#equipment_list, 0, #EQUIPMENT_SLOTS) do
local equipment_id = tonumber(equipment_list[i])
if equipment_id >= 0 or equipment_id <= #EQUIPMENT_SLOTS then
local size = EQUIPMENT_DATA[equipment_id].size
if not requested_inventory[size] then
requested_inventory[size] = {}
end
table.insert(requested_inventory[size], equipment_id)
end
end
-- assign slots for requested equipment
for slot_number, size in ipairs(EQUIPMENT_SLOTS) do
if requested_inventory[size] then
if #requested_inventory[size] < 1 then
requested_inventory[size] = nil
else
local equipment_id = table.remove(requested_inventory[size], 1)
if #open_slots[size] > 0 then
local slot = table.remove(open_slots[size], 1)
table.insert(new_inventory, {slot = slot, id = equipment_id, charges = EQUIPMENT_DATA[equipment_id]["charges"], ammo = EQUIPMENT_DATA[equipment_id]["ammo"]})
else
table.insert(new_inventory, {slot = slot_number, id = equipment_id, charges = EQUIPMENT_DATA[equipment_id]["charges"], ammo = EQUIPMENT_DATA[equipment_id]["ammo"]})
end
end
end
end
-- give player requested equipment
for k, v in ipairs(new_inventory) do
local is_success = server.setCharacterItem(character_id, v.slot, v.id, false, v.charges, v.ammo)
if not is_success then
throwWarning(string.format("%s %.0f %s %.0f %s %.0f", "Failed to give character", character_id, "equipment", v.id, "in slot", v.slot))
end
end
end
end
-- CALLBACK FUNCTIONS --
function onCreate(is_new)
-- construct weapon equipment list if weapon DLC active
if server.dlcWeapons() then
EQUIPMENT_DATA[31] = {name="C4", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[32] = {name="C4 detonator", size=3, charges=0, ammo=0}
EQUIPMENT_DATA[33] = {name="speargun", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[34] = {name="speargun ammo", size=3, charges=8, ammo=0}
EQUIPMENT_DATA[35] = {name="pistol", size=3, charges=17, ammo=0}
EQUIPMENT_DATA[36] = {name="pistol ammo", size=3, charges=17, ammo=0}
EQUIPMENT_DATA[37] = {name="smg", size=3, charges=40, ammo=0}
EQUIPMENT_DATA[38] = {name="smg ammo", size=3, charges=40, ammo=0}
EQUIPMENT_DATA[39] = {name="rifle", size=3, charges=30, ammo=0}
EQUIPMENT_DATA[40] = {name="rifle ammo", size=3, charges=30, ammo=0}
EQUIPMENT_DATA[41] = {name="grenade", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[42] = {name="machine gun ammo kinetic", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[43] = {name="machine gun ammo high explosive", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[44] = {name="machine gun ammo fragmentation", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[45] = {name="machine gun ammo armour piercing", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[46] = {name="machine gun ammo incendiary", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[47] = {name="light ammo kinetic", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[48] = {name="light ammo high explosive", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[49] = {name="light ammo fragmentation", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[50] = {name="light ammo armour piercing", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[51] = {name="light ammo incendiary", size=3, charges=50, ammo=0}
EQUIPMENT_DATA[52] = {name="rotary ammo kinetic", size=3, charges=25, ammo=0}
EQUIPMENT_DATA[53] = {name="rotary ammo high explosive", size=3, charges=25, ammo=0}
EQUIPMENT_DATA[54] = {name="rotary ammo fragmentation", size=3, charges=25, ammo=0}
EQUIPMENT_DATA[55] = {name="rotary ammo armour piercing", size=3, charges=25, ammo=0}
EQUIPMENT_DATA[56] = {name="rotary ammo incendiary", size=3, charges=25, ammo=0}
EQUIPMENT_DATA[57] = {name="heavy ammo kinetic", size=3, charges=10, ammo=0}
EQUIPMENT_DATA[58] = {name="heavy ammo high explosive", size=3, charges=10, ammo=0}
EQUIPMENT_DATA[59] = {name="heavy ammo fragmentation", size=3, charges=10, ammo=0}
EQUIPMENT_DATA[60] = {name="heavy ammo armour piercing", size=3, charges=10, ammo=0}
EQUIPMENT_DATA[61] = {name="heavy ammo incendiary", size=3, charges=10, ammo=0}
EQUIPMENT_DATA[62] = {name="battle shell kinetic", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[63] = {name="battle shell high explosive", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[64] = {name="battle shell fragmentation", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[65] = {name="battle shell armour piercing", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[66] = {name="battle shell incendiary", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[67] = {name="artillery shell kinetic", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[68] = {name="artillery shell high explosive", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[69] = {name="artillery shell fragmentation", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[70] = {name="artillery shell armour piercing", size=3, charges=1, ammo=0}
EQUIPMENT_DATA[71] = {name="artillery shell incendiary", size=3, charges=1, ammo=0}
end
IS_NEW_SAVE = is_new
first_to_join = true
-- check version
if g_savedata.version then
local data_version = tonumber(g_savedata.version)
if data_version > VERSION then
invalid_version = true
server.announce("WARNING", "Your code is older than your save data and may not be processed correctly. Please update the script to the latest version. This script will refuse to execute commands in order to protect your data.")
elseif data_version < VERSION then
server.announce("UPDATING", "Updating persistent data if necessary.")
for k, v in pairs(PREFERENCE_DEFAULTS) do
if not g_savedata.preferences[k] then
g_savedata.preferences[k] = v
end
end
server.announce("COMPLETE", "Update complete")
end
end
if not invalid_version then
g_savedata.version = string.format("%0.3f", VERSION)
-- define if undefined
g_savedata.vehicle_list = g_savedata.vehicle_list or {}
g_savedata.object_list = g_savedata.object_list or {}
g_savedata.player_data = g_savedata.player_data or {}
g_savedata.banned = g_savedata.banned or {}
g_savedata.preferences = g_savedata.preferences or PREFERENCE_DEFAULTS
g_savedata.rules = g_savedata.rules or DEFAULT_RULES
g_savedata.game_settings = g_savedata.game_settings or {}
-- create references
VEHICLE_LIST = g_savedata.vehicle_list
OBJECT_LIST = g_savedata.object_list -- Not in use yet
PLAYER_DATA = g_savedata.player_data
BANNED = g_savedata.banned
PREFERENCES = g_savedata.preferences
RULES = g_savedata.rules
GAME_SETTINGS = g_savedata.game_settings
PLAYER_LIST = reformatPlayerList(server.getPlayers())
JOIN_QUEUE = {}
TELEPORT_QUEUE = {}
ID_VIEWERS = {}
-- get game settings, check gamemode, set game settings
if is_new then
local game_settings = server.getGameSettings()
creative_mode = game_settings.settings_menu
for k, v in pairs(game_settings) do
local setting_value = (CREATIVE_SETTINGS[k] and creative_mode) or CAREER_SETTINGS[k]
if setting_value ~= nil then
server.setGameSetting(k, setting_value)
end
end
GAME_SETTINGS = (CREATIVE_SETTINGS and creative_mode) or CAREER_SETTINGS
end
-- get telport zones and index them by name
TELEPORT_ZONES = {}
local zones = server.getZones("cc_teleport_zone")
if #zones > 0 then
for k, v in pairs(zones) do
for index, tag in ipairs(v.tags) do
local front, back, label_type = tag:find("map_label=([^,])")
if label_type then
TELEPORT_ZONES[v.name] = {transform = v.transform, ui_id = server.getMapID(), label_type = label_type}
else
TELEPORT_ZONES[v.name] = {transform = v.transform, tags = v.tags}
end
end
end
else
throwWarning("No teleport zones could be found. You will not be able to use the ?tp command to teleport to named locations.")
end
end
end
function onPlayerJoin(steam_id, name, peer_id, admin, auth)
local first_join
if invalid_version and (first_to_join and peer_id == 0) or (PLAYER_DATA[steam_id] and PLAYER_DATA[steam_id].permissions and PLAYER_DATA[steam_id].permissions.owner) then -- delay version warnings for when an owner joins
server.announce("WARNING", "Your code is older than your save data and cannot be processed correctly. Please update the script to the latest version.", peer_id)
end
if not invalid_version then
for k, v in pairs(PLAYER_LIST) do -- in case player crashed and was not logged out properly
if v.steam_id == steam_id then
server.announce("Error", "Did your game crash recently? You appear to already be logged on to this server. You have been logged off and on again.", peer_id)
PLAYER_LIST[k] = nil
break
end
end
if PLAYER_DATA[steam_id] and PLAYER_DATA[steam_id].permissions then -- if returning player
local player = PLAYER_DATA[steam_id]
player.name = name -- refresh name
if player.permissions.banned then
server.kickPlayer(peer_id)
end
else
first_join = true
-- add new player's data to persistent data table
PLAYER_DATA[tostring(steam_id)] = {
name = name,
permissions = {
owner = peer_id < 1,
admin = false,
auth = false
}
}
end
-- add map labels for some teleport zones
for k, v in pairs(TELEPORT_ZONES) do
if v.ui_id then
server.addMapLabel(peer_id, v.ui_id, v.label_type, k, v.transform[13], v.transform[15]+5)
end
end
if PLAYER_DATA[tostring(steam_id)].deny_tp then
PLAYER_DATA[tostring(steam_id)].deny_tp_ui_id = server.getMapID()
drawDenyTeleportUI(peer_id)
end
-- add user data to non-persistent table
PLAYER_LIST[peer_id] = {
steam_id = tostring(steam_id),
name = name
}
-- add to JOIN_QUEUE to handle equiping and welcome messages
table.insert(JOIN_QUEUE, {id = peer_id, steam_id = tostring(steam_id), new = first_join})
end
end
function onPlayerLeave(steam_id, name, peer_id, is_admin, is_auth)
if not invalid_version then
if PREFERENCES.remove_vehicle_on_leave then
for i=#VEHICLE_LIST, 1, -1 do
if VEHICLE_LIST[i].owner == peer_id then
server.despawnVehicle(i, false) -- despawn vehicle when unloaded
table.remove(VEHICLE_LIST, i)
end
end
end
PLAYER_LIST[peer_id] = nil
end
end
function onPlayerDie(steam_id, name, peer_id, is_admin, is_auth)
if not invalid_version then
if PREFERENCES.keep_inventory then
local character_id = server.getPlayerCharacterID(peer_id)
PLAYER_DATA[tostring(steam_id)].inventory = getInventory(character_id)
end
end
end
function onPlayerRespawn(peer_id)
if not invalid_version then
if PREFERENCES.keep_inventory then
local steam_id = getSteamID(peer_id)
if PLAYER_DATA[steam_id].inventory then
equipPlayer(peer_id, PLAYER_DATA[steam_id].inventory)
local character_id = server.getPlayerCharacterID(peer_id)
return
end
end
if PREFERENCES.equip_on_respawn then
equipPlayer(peer_id, PREFERENCES.start_equipment)
end
end
end
function onVehicleSpawn(vehicle_id, peer_id, x, y, z, cost)
if not invalid_version then
if peer_id > -1 then
VEHICLE_LIST[vehicle_id] = {owner = peer_id, ui_id = server.getMapID()}
end
end
end
function onVehicleDespawn(vehicle_id, peer_id)
if not invalid_version then
if VEHICLE_LIST[vehicle_id] then
server.removeMapID(-1, VEHICLE_LIST[vehicle_id].ui_id)
VEHICLE_LIST[vehicle_id] = nil
end
end
end
local count = 0
function onTick()
if not invalid_version then
-- Make first player to join a DS an owner
if IS_NEW_SAVE then
local player_pos, is_success = server.getPlayerPos(0)
if is_success then
if player_pos[13] == 0 and player_pos[14] == 0 and player_pos[15] == 0 then
MAKE_DS_OWNER = true
else
end
IS_NEW_SAVE = false
end
end
refreshVehicleUI()
if count >= 60 then
for k, v in ipairs(JOIN_QUEUE) do --check if player has moved or looked around when joining
local peer_id = v.id
local player_matrix, success = server.getPlayerPos(peer_id)
if success then
local look_x, look_y, look_z, success = server.getPlayerLookDirection(peer_id)
local look_direction = {look_x, look_y, look_z}
if success then
local not_moving = true
local not_looking = true
if PLAYER_LIST[peer_id].last_position and PLAYER_LIST[peer_id].last_look_direction then
if matrix.distance(PLAYER_LIST[peer_id].last_position, player_matrix) > 0.2 then
not_moving = false
end
for h, c in ipairs(look_direction) do
if PLAYER_LIST[peer_id].last_look_direction[h] - c > 0.01 then
not_looking = false
end
end
end
if not_looking and not_moving then
PLAYER_LIST[peer_id].last_position = player_matrix
PLAYER_LIST[peer_id].last_look_direction = look_direction
else
if MAKE_DS_OWNER then
PLAYER_DATA[getSteamID(peer_id)].permissions.owner = true
MAKE_DS_OWNER = false
end
-- Display welcome message
if v.new then
if PREFERENCES.welcome_new then
server.announce("Welcome", PREFERENCES.welcome_new, peer_id) -- custom welcome message for new players
end
if #RULES > 0 then
printRules(peer_id)
end
-- Give player starting equipment as defined in preferences
local character_id, is_success = server.getPlayerCharacterID(peer_id)
for i=1, #EQUIPMENT_SLOTS do
server.setCharacterItem(character_id, i, 0, false)
end
equipPlayer(peer_id, PREFERENCES.start_equipment, true)
else
if PREFERENCES.welcome_returning then
server.announce("Welcome", PREFERENCES.welcome_returning, peer_id) -- custom welcome message for returning players
end
end
if PREFERENCES.auth_all then
server.addAuth(peer_id)
end
-- assign privilages
if PLAYER_DATA[v.steam_id] and PLAYER_DATA[v.steam_id].permissions then
if PLAYER_DATA[v.steam_id].permissions.admin then
server.addAdmin(peer_id)
end
if PLAYER_DATA[v.steam_id].permissions.auth then
server.addAuth(peer_id)
end
else
throwError("Persistent data for "..playerName(peer_id).." could not be found. It is either not defined or corrupted.")
end
table.remove(JOIN_QUEUE, k)
end
end
end
end
count = 0
else
count = count + 1
end
-- Re-teleport players to prevent them falling through the ground O_o
for i=#TELEPORT_QUEUE, 1, -1 do
local v = TELEPORT_QUEUE[i]
if v.time <= 0 then
server.setPlayerPos(v.peer_id, v.target_matrix)
table.remove(TELEPORT_QUEUE, i)
else
v.time = v.time - 1
end
end
end
end
-- ADMIN FUNCTIONS --
function banPlayer(...)
local args = {...}
local peer_id = table.remove(args, 1)
for k, v in ipairs(args) do
v = toInteger(v)
local valid = validateInput(peer_id, v, k, "number")
if valid then
if v == 0 then
server.announce("Failed", "The peer id 0 cannot be banned", peer_id)
else
if PLAYER_LIST[v] then
local target_steam_id = getSteamID(v)
if getSteamID(peer_id) ~= target_steam_id then
if PLAYER_DATA[target_steam_id] then
if not PLAYER_DATA[target_steam_id].permissions.admin then
PLAYER_DATA[target_steam_id].permissions.banned = true
BANNED[target_steam_id] = getSteamID(peer_id)
server.kickPlayer(v)
server.announce("Success", playerName(v) .. " has been banned from the server.", peer_id)
else
server.announce("Failed", "You cannot ban an admin.", peer_id)
end
end
else
server.announce("Failed", "You cannot ban yourself.", peer_id)
end
else
server.announce("Failed", "That player is not on the server.", peer_id)
end
end
end
end
end
function unbanPlayer(...)
local args = {...}
local peer_id = table.remove(args, 1)
for k, v in ipairs(args) do
if #v >= #STEAM_ID_MIN then
if PLAYER_DATA[v] then
if PLAYER_DATA[v].permissions.banned then
PLAYER_DATA[v].permissions.banned = false
BANNED[v] = nil
server.announce("Success", v.. " has been unbannned.", peer_id)
else
server.announce("Failed", v.." is not banned or the steam id is incorrect.", peer_id)
end
else
server.announce("Failed", v.." is not banned", peer_id)
end
else
server.announce("Failed", "Argument must be a steam id", peer_id)
end
end
end
function authorizePlayer(...)
local args = {...}
local peer_id = table.remove(args, 1)
for k, v in ipairs(args) do
v = toInteger(v)
local valid = validateInput(peer_id, v, k, "number")
if valid then
if PLAYER_LIST[v] then
local target_steam_id = getSteamID(v)
PLAYER_DATA[target_steam_id].permissions.auth = true
server.addAuth(v)
server.notify(v, "Permission Granted", "You were authorized", 4)
server.announce("Success", playerName(v).." was authorized.", peer_id)
else
server.announce("Failed", "That player is not on the server.", peer_id)
end
end
end
end
function deauthorizePlayer(...)
local args = {...}
local peer_id = table.remove(args, 1)
for k, v in ipairs(args) do
v = toInteger(v)
local valid = validateInput(peer_id, v, k, "number")
if valid then
if PLAYER_LIST[v] then
local target_steam_id = getSteamID(v)
PLAYER_DATA[target_steam_id].permissions.auth = false
server.removeAuth(v)
server.notify(v, "Permission Lost", "You are no longer authorized", 2)
server.announce("Success", playerName(v).." is no longer authorized.", peer_id)
else
server.announce("Failed", "That player is not on the server.", peer_id)
end
end
end
end
function adminPlayer(...)
local args = {...}
local peer_id = table.remove(args, 1)
for k, v in ipairs(args) do
v = toInteger(v)
local valid = validateInput(peer_id, v, k, "number")
if valid then
if PLAYER_LIST[v] then
local target_steam_id = getSteamID(v)
PLAYER_DATA[target_steam_id].permissions.admin = true
if not PLAYER_DATA[target_steam_id].permissions.auth then
authorizePlayer(peer_id)
end
server.addAdmin(v)
server.notify(v, "Permission Granted", "You were made an admin", 4)
server.announce("Success", playerName(v).." was made an admin.", peer_id)
else
server.announce("Failed", "That player is not on the server.", peer_id)
end
end
end
end
function deAdminPlayer(...)
local args = {...}
local peer_id = table.remove(args, 1)
for k, v in ipairs(args) do
v = toInteger(v)
local valid = validateInput(peer_id, v, k, "number")
if valid then
if PLAYER_LIST[v] then
local target_steam_id = getSteamID(v)
PLAYER_DATA[target_steam_id].permissions.admin = false
server.removeAdmin(v)
server.notify(v, "Permission Lost", "You are no longer an admin", 2)
server.announce("Success", playerName(v).." is no longer an admin.", peer_id)
else
server.announce("Failed", "That player is not on the server.", peer_id)
end
end
end
end
function addOwner(...)
local args = {...}
local peer_id = table.remove(args, 1)
for k, v in ipairs(args) do
v = toInteger(v)
local valid = validateInput(peer_id, v, k, "number")
if valid then
if PLAYER_LIST[v] then
local target_steam_id = getSteamID(v)
PLAYER_DATA[target_steam_id].permissions.owner = true
server.addAdmin(v)
server.notify(v, "Permission Granted", "You were made an owner", 4)
server.announce("Success", playerName(v).. " was made an owner.", peer_id)
else
server.announce("Failed", "That player is not on the server.", peer_id)
end
end
end
end
function removeOwner(...)
local args = {...}
local peer_id = table.remove(args, 1)
for k, v in ipairs(args) do
v = toInteger(v)
local valid = validateInput(peer_id, v, k, "number")
if valid then
if v ~= 0 then
if PLAYER_LIST[v] then
local target_steam_id = getSteamID(v)
PLAYER_DATA[target_steam_id].permissions.owner = false
server.notify(v, "Permission Lost", "You are no longer an owner", 2)
server.announce("Success", playerName(v).. " is no longer an owner.", peer_id)
else
server.announce("Failed", "That player is not on the server.", peer_id)
end
else
server.announce("Failed", "You cannot remove ownership status from peer 0.", peer_id)
end
end
end
end
function removeVehicle(...)
local args = {...}
local peer_id = table.remove(args, 1)
local target_id
local is_success
if #args > 0 then
for k, v in ipairs(args) do
v = toInteger(v)
valid = validateInput(peer_id, v, k, "number")
if valid then
if VEHICLE_LIST[v] then
target_id = v
else
server.announce("Warning", "The vehicle with the id of "..v.." does not exist", peer_id)
return
end
end
end
else
local player_matrix = server.getPlayerPos(peer_id)
local matrices = {}
for k,v in pairs(VEHICLE_LIST) do
table.insert(matrices, {matrix.distance(player_matrix, (server.getVehiclePos(k))), id=k})
end
matrices = quicksort(matrices, 1, #matrices)
target_id = matrices[1].id
end
if target_id then
is_success = server.despawnVehicle(target_id, true)
end
if not is_success then
server.announce("Success", string.format("%s %.0f %s", "Vehicle", target_id, "could not be removed."), peer_id)
end
end
function setVehicleEditable(peer_id, vehicle_id, state)
vehicle_id = toInteger(vehicle_id)
state = (state and stringToBool(state)) or false
local id_valid = validateInput(peer_id, vehicle_id, 1, "number")
local state_valid = validateInput(peer_id, state, 2, "boolean")
if id_valid and state_valid then
if VEHICLE_LIST[vehicle_id] then
server.setVehicleEditable(vehicle_id, state)
server.notify(peer_id, "Success", "Vehicle "..vehicle_id.." has been set to "..((state and "editable") or "non-editable"), 4)
else
server.announce("Failed", "The vehicle with the id of "..vehicle_id.." does not exist.", peer_id)
end
end
end
function tpPlayerToPlayer(...)
local args = {...}
local peer_id = table.remove(args, 1)
local peer_pos, success = server.getPlayerPos(peer_id)
if success then
for k, v in ipairs(args) do
if v == "*" then
for k, v in pairs(PLAYER_LIST) do
if k ~= peer_id then
local is_success = server.setPlayerPos(k, peer_pos)
if is_success then
server.announce("Success", "Telported "..playerName(k).." to your position.", peer_id)
else
server.announce("Failed", "Could not teleport "..playerName(k).." to your position.", peer_id)
end
end
end
server.announce("Success", "Teleported all players to your position.", peer_id)
return
else
v = toInteger(v)
local valid = validateInput(peer_id, v, k, "number")
if valid then
if PLAYER_LIST[v] then
local is_success = server.setPlayerPos(v, peer_pos)
if is_success then
server.announce("Success", "Telported "..playerName(k).." to your position.", peer_id)
else
server.announce("Failed", "Could not teleport "..playerName(k).." to your position.", peer_id)
end
else
server.announce("Warning", "The player with the id of "..v.." is not on this server.", peer_id)
end
end
end
end
else
server.announce("Failed", "Something went wrong, please try again.", peer_id)
end
end
function printVehicleList(peer_id)
server.announce(" ", "-------------------------- VEHICLE LIST --------------------------", peer_id)
for k, v in pairs(VEHICLE_LIST) do
local vehicle_pos, is_success = server.getVehiclePos(k)
if not is_success then -- if null vehicle (removed by clean all vehicles / one of the other weird bugs)
if VEHICLE_LIST[vehicle_id] then
server.removeMapID(-1, VEHICLE_LIST[vehicle_id].ui_id)
VEHICLE_LIST[vehicle_id] = nil
end
else
local vehicle_name, is_success = server.getVehicleName(k)
server.announce(" ", k.." | "..(is_success and vehicle_name or "unknown").." | "..playerName(v.owner), peer_id)
end
end
server.announce(" ", LINE, peer_id)
end
function printPlayerPermissions(peer_id, target_id)
target_id = toInteger(target_id)
local valid = validateInput(peer_id, target_id, 1, "number")
if valid then
if PLAYER_LIST[target_id] then
local steam_id = PLAYER_LIST[target_id].steam_id
local permissions = PLAYER_DATA[steam_id].permissions
local text = ""
for k, v in pairs(permissions) do
if #text > 0 and v and v ~= "" then
text = text..", "
end
if v then
text = text..k
end
end
server.announce(playerName(target_id), text, peer_id)
else
server.announce("Failed", "The player with the id of "..target_id.." is not on this server.", peer_id)
end
end
end
function printBannedPlayers(peer_id, page)
local page_length = 2
page = toInteger(page) or 1
page = math.floor(page)
local valid = validateInput(peer_id, page, 1, "number")
if valid then
local numbered_list = {}
for k, v in pairs(BANNED) do
table.insert(numbered_list, {k, v})
end
if #numbered_list > 0 then
server.announce(" ", "---------------------- BANNED PLAYERS -----------------------", peer_id)
local limit = clamp(page_length*page, 0, #numbered_list)
for i = clamp((page-1)*(page_length + 1), 1, #numbered_list), limit do
server.announce(numbered_list[i][1], "Banned by: "..(PLAYER_DATA[numbered_list[i][2]].name), peer_id)
end
server.announce(" ", "Page "..math.ceil(page).." of "..math.max(math.ceil(#numbered_list/page_length), 1))
server.announce(" ", LINE, peer_id)
else
server.announce("Ban List", "No one has been banned", peer_id)
end
end
end
function addRule(...)