-
Notifications
You must be signed in to change notification settings - Fork 3
/
car_menu.sp
4331 lines (3879 loc) · 118 KB
/
car_menu.sp
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
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <cstrike>
#include <halflife>
#include <tConomy>
#include <emitsoundany>
#include <rpg_npc_core>
#include <rpg_inventory_core>
#define MAX_CARS 128
#define MAX_ENTITIES 2048
#define MAX_LIGHTS 12
#define CAR_VERSION "0.90 GO"
#define VEHICLE_TYPE_AIRBOAT_RAYCAST 8
#define COLLISION_GROUP_PLAYER 5
#define HIDEHUD_WEAPONSELECTION 1
#define HIDEHUD_CROSSHAIR 256
#define HIDEHUD_INVEHICLE 1024
#define EF_NODRAW 32
new Handle:g_Cvar_Enable = INVALID_HANDLE;
new Handle:g_Cvar_Gas = INVALID_HANDLE;
new Handle:g_Cvar_GasUse = INVALID_HANDLE;
new Handle:g_Cvar_DelayH = INVALID_HANDLE;
new Handle:g_Cvar_Interval = INVALID_HANDLE;
new Handle:g_Cvar_Database = INVALID_HANDLE;
new Handle:g_Cvar_Debug = INVALID_HANDLE;
new Handle:g_Cvar_Override = INVALID_HANDLE;
new Handle:g_CarTimer = INVALID_HANDLE;
new Handle:g_GasTimer = INVALID_HANDLE;
new Handle:g_GasMenu = INVALID_HANDLE
new Handle:g_BuyMenu = INVALID_HANDLE;
new Handle:g_VIPBuyMenu = INVALID_HANDLE;
new Handle:g_InfoMenu = INVALID_HANDLE;
new Handle:g_VIPInfoMenu = INVALID_HANDLE;
new Handle:db_cars = INVALID_HANDLE;
new Handle:carbuykv;
new fuel_display[MAX_ENTITIES];
new mpg[MAX_ENTITIES];
new armour[MAXPLAYERS + 1];
public bool:AtGas[MAXPLAYERS + 1];
public bool:Driving[MAXPLAYERS + 1];
public bool:Needs_Reset[MAXPLAYERS + 1];
new Cars[MAXPLAYERS + 1];
new String:authid[MAXPLAYERS + 1][35];
new Float:CurrentEyeAngle[MAXPLAYERS + 1][3];
new g_CarIndex[4096];
new g_CarLightQuantity[MAX_ENTITIES];
new g_CarLights[MAX_ENTITIES][MAX_LIGHTS];
new g_CarQty = -1;
new g_SpawnedCars[MAXPLAYERS + 1];
new gas_remaining[MAX_ENTITIES];
new gas_into_this_car[MAXPLAYERS + 1];
// View Entity
new buttons2;
new ViewEnt[2048];
// Siren and Horn
public bool:CarSiren[MAX_ENTITIES + 1];
public bool:CarView[MAXPLAYERS + 1];
public bool:CarOn[MAX_ENTITIES + 1];
new Float:car_fuel[MAX_ENTITIES + 1];
public bool:CarHorn[MAXPLAYERS + 1];
new Handle:h_siren_a = INVALID_HANDLE;
new Handle:h_siren_b = INVALID_HANDLE;
new Handle:h_siren_c = INVALID_HANDLE;
new Handle:h_horn = INVALID_HANDLE;
// Car Buy Menu Load Arrays
new car_quantity = 0;
new car_quantity_vip = 0;
new car_quantity_disabled = 0;
new car_vip[MAX_CARS];
new String:car_name[MAX_CARS][32];
new String:car_model[MAX_CARS][256];
new String:car_script[MAX_CARS][256];
new car_skins[MAX_CARS];
new car_price[MAX_CARS];
new car_mpg[MAX_CARS];
new car_lights[MAX_CARS];
new car_police_lights[MAX_CARS];
new car_view_enabled[MAX_CARS];
new car_siren_enabled[MAX_CARS];
new car_driver_view[MAX_CARS];
new Float:car_gas[MAX_CARS];
new car_passengers[MAX_CARS];
new String:car_passenger_seat[MAX_CARS][11][128];
new String:car_passenger_attachment[MAX_CARS][11][32];
new car_passenger_mode[MAX_CARS][11];
new Float:car_passenger_position[MAX_CARS][11][3];
// Temporary Car Menu Arrays
new selected_car[MAXPLAYERS + 1];
new selected_car_stowed[MAXPLAYERS + 1];
new selected_car_skin[MAXPLAYERS + 1];
new selected_car_type[MAXPLAYERS + 1];
new selected_car_index[MAXPLAYERS + 1];
new Float:selected_car_fuel[MAXPLAYERS + 1];
new String:selected_car_name[MAXPLAYERS + 1][32];
new m_ArmorValue;
new MoneyOffset;
// Individual Car Arrays
new cars_type[MAX_ENTITIES];
new cars_index[MAX_ENTITIES];
new drivers_car[MAXPLAYERS + 1];
new Car_Entity[MAXPLAYERS + 1][10];
new Car_Type[MAXPLAYERS + 1][10];
new Car_Skin[MAXPLAYERS + 1][10];
new Float:Car_Gas[MAXPLAYERS + 1][10];
new Cars_Driver_Prop[MAX_ENTITIES];
new cars_seats[MAX_ENTITIES];
new cars_seat_entities[MAX_ENTITIES][11];
new is_chair[MAX_ENTITIES];
new chairs_car[MAX_ENTITIES];
new car_owner[MAX_ENTITIES];
new bool:can_stow[MAXPLAYERS + 1];
new cars_spawned[MAXPLAYERS + 1];
// Player Loading to Database
public bool:InQuery;
public bool:IsDisconnect[33];
public bool:Loaded[33];
// IP Security
new String:player_ip[MAXPLAYERS + 1][32];
new started = 0;
int g_iLastInteractedWith[MAXPLAYERS + 1];
char npctype[128] = "Car Vendor";
public Plugin:myinfo =
{
name = "Car Menu",
author = "Natalya",
description = "CS:S Car Menu",
version = CAR_VERSION,
url = "http://www.lady-natalya.info/"
}
public OnPluginStart()
{
// Load Plugin Requirements
LoadTranslations("plugin.car_menu");
m_ArmorValue = FindSendPropInfo("CCSPlayer", "m_ArmorValue");
MoneyOffset = FindSendPropOffs("CCSPlayer", "m_iAccount");
// Commands
RegConsoleCmd("sm_car_menu", Car_Menu, " -- Open the Car Menu.");
RegConsoleCmd("sm_menu_voiture", Car_Menu, " -- Ouvrir le menu voiture.");
RegConsoleCmd("sm_car_stow", Car_Stow_R, "Stow Your Car");
RegConsoleCmd("sm_ranger_voiture", Car_Stow_R, " -- Ranger la voiture.");
RegConsoleCmd("sm_car_on", Car_On, " -- Turn your car on.");
RegConsoleCmd("sm_car_exit", Car_Exit, "-- Exit a Car.");
RegConsoleCmd("sm_car_off", Car_Off, " -- Turn your car off.");
RegConsoleCmd("sm_siren", Car_Siren, " -- Toggle a police cruiser's siren.");
RegConsoleCmd("sm_car_view", Car_View, " -- Toggle between first and 3rd.");
RegConsoleCmd("sm_car_info", Car_Info, " -- View information about the car you are driving.");
RegConsoleCmd("sm_seat", Car_Seat, " -- Switch seats in a car if applicable.");
RegAdminCmd("sm_car_register", Car_Register, ADMFLAG_CUSTOM3, "Admin register a car to be drivable after plugin reload.");
RegAdminCmd("sm_car_kill", Car_Kill, ADMFLAG_CUSTOM3, "Admin Hard Delete a Car.");
RegAdminCmd("sm_car_stow_a", Car_Stow, ADMFLAG_CUSTOM3, "Admin Only");
RegAdminCmd("car_db_save", Command_DBSave, ADMFLAG_CUSTOM3, "Force Server DB Save");
RegConsoleCmd("sm_car_menu_super", Car_Menu_Super, "Super Car Menu, MotherFucker!!");
RegServerCmd("sm_verifyip", IP_Check, "RCON Only");
RegServerCmd("sm_give_steam", Steam_Check, "RCON Only");
RegServerCmd("sm_car_spawned", Car_Check, "RCON Only");
RegServerCmd("sm_car_name", Cars_Name, "RCON Only");
RegServerCmd("sm_car_skin", Cars_Skin, "RCON Only");
RegServerCmd("sm_car_skin_num", Cars_Skin2, "RCON Only");
RegServerCmd("sm_car_max_skins", Cars_Skin3, "RCON Only");
RegServerCmd("sm_car_set_skin", Cars_Skin4, "RCON Only");
RegServerCmd("sm_car_spawn", Car_Spawn, "RCON Only");
// Server Variables
CreateConVar("car_menu_version", CAR_VERSION, "Version of Car Menu on this server", FCVAR_PLUGIN | FCVAR_SPONLY | FCVAR_REPLICATED | FCVAR_NOTIFY);
g_Cvar_Enable = CreateConVar("car_menu_enabled", "1", " Enable/Disable the Car Menu plugin", FCVAR_PLUGIN);
g_Cvar_Database = CreateConVar("car_db_mode", "0", "DB Location 1 = remote or 2 = local -- 0 = failure", FCVAR_PLUGIN | FCVAR_REPLICATED | FCVAR_NOTIFY);
g_Cvar_Gas = CreateConVar("car_menu_gas_price", "3.00", "Set the price of gas.", FCVAR_PLUGIN);
g_Cvar_GasUse = CreateConVar("car_menu_gas_enabled", "1", "Use gas in cars?", FCVAR_PLUGIN);
// g_Cvar_DelayS = CreateConVar("car_stow_delay", "60.0", " Delay for car_stow command.", FCVAR_PLUGIN, true, 0.0);
g_Cvar_DelayH = CreateConVar("car_horn_delay", "1.0", " Delay between horn honks.", FCVAR_PLUGIN, true, 0.1);
g_Cvar_Interval = CreateConVar("car_save_interval", "3600.0", "Interval between Car DB saves in seconds. Set higher if server doesn't crash frequently.", FCVAR_PLUGIN);
g_Cvar_Debug = CreateConVar("car_debug_mode", "0", "Use 1 to turn Debug messages on or 0 to turn them off.", FCVAR_PLUGIN | FCVAR_REPLICATED | FCVAR_NOTIFY);
g_Cvar_Override = CreateConVar("car_vip_override", "0", "0 means no restrictions on spawning VIP cars, 1 means only VIPs can spawn VIP cars", FCVAR_PLUGIN | FCVAR_REPLICATED | FCVAR_NOTIFY);
// Hook Events
HookEvent("player_spawn", Event_PlayerSpawnPre, EventHookMode_Pre);
HookEvent("player_death", Event_PlayerDeathPre, EventHookMode_Pre);
HookEntityOutput("trigger_multiple", "OnStartTouch", OnStartTouch);
HookEntityOutput("trigger_multiple", "OnEndTouch", OnEndTouch);
// Load the car buy menu
ReadCarFile();
// Load Players Already In Game
for (new client = 1; client <= MaxClients; client++)
{
if (IsClientInGame(client))
{
SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
GetClientAuthId(client, AuthId_Engine, authid[client], sizeof(authid[]));
Reset_Car_Selection(client);
SDKHook(client, SDKHook_WeaponDrop, OnWeaponDrop);
SDKHook(client, SDKHook_PreThink, OnPreThink);
AtGas[client] = false;
Driving[client] = false;
Loaded[client] = false;
cars_spawned[client] = 0;
can_stow[client] = true;
CreateTimer(1.0, CreateSQLAccount, client);
}
}
// Thanks to Mitchell for +lookatweapon listener
AddCommandListener(Cmd_LookAtWeapon, "+lookatweapon");
}
// #######
// NATIVES
// #######
public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
{
CreateNative("IsClientDriving", NativeIsClientDriving);
CreateNative("SpawnChair", NativeSpawnChair);
CreateNative("IsEntityChair", NativeIsEntityChair);
CreateNative("GetCarOwner", NativeGetCarOwner);
RegPluginLibrary("car_menu");
return APLRes_Success;
}
// native functions
public NativeIsClientDriving(Handle:plugin, numParams)
{
new client = GetNativeCell(1);
return Driving[client];
}
public NativeIsEntityChair(Handle:plugin, numParams)
{
new entity = GetNativeCell(1);
return is_chair[entity];
}
public NativeGetCarOwner(Handle:plugin, numParams)
{
new car = GetNativeCell(1);
return car_owner[car];
}
public NativeSpawnChair(Handle:plugin, numParams)
{
// Create Variables
new String:model[255];
new skin = 0;
new client = 0;
new fail = -1;
// Get the variables from the command
GetNativeString(1, model, sizeof(model));
skin = GetNativeCell(2);
client = GetNativeCell(3);
// Do it do it do it!!
if (IsClientInGame(client))
{
new seat = SpawnSeat2(model, skin, client);
return seat;
}
else return fail;
}
public OnMapStart()
{
npc_registerNpcType(npctype);
if (GetConVarInt(g_Cvar_Enable))
{
g_BuyMenu = BuildBuyMenu();
g_VIPBuyMenu = BuildVIPBuyMenu();
g_InfoMenu = BuildInfoMenu();
g_VIPInfoMenu = BuildVIPInfoMenu();
g_CarQty = -1;
g_GasMenu = BuildGasMenu();
CreateTimer(300.0, LOLCar_Time, INVALID_HANDLE);
}
if (started == 0)
{
new db_mode = GetConVarInt(g_Cvar_Database);
if (db_mode == 0)
{
CreateTimer(1.0, FUCKING_CAR_MODE_NOT_0);
}
else InitializeCarDB();
}
}
public Action:FUCKING_CAR_MODE_NOT_0(Handle:Timer, any:client)
{
new db_mode = GetConVarInt(g_Cvar_Database);
if (db_mode == 0)
{
PrintToChatAll("\x03[Car] car_db_mode is 0 -- set it to 1 or 2");
CreateTimer(1.0, FUCKING_CAR_MODE_NOT_0);
}
else InitializeCarDB();
}
public Action:LOLCar_Time(Handle:timer)
{
new Float:interval_time = GetConVarFloat(g_Cvar_Interval);
g_CarTimer = CreateTimer(interval_time, Car_Time, INVALID_HANDLE, TIMER_REPEAT);
}
public Action:Car_Time(Handle:timer)
{
for (new client = 1; client <= MaxClients; client++)
{
if (IsClientInGame(client))
{
DBSave(client);
}
}
PrintToServer("[Car] Auto Save -- updated clients in SQL Database.");
LogMessage("[Car] Auto Save -- updated clients in SQL Database.");
}
public OnPluginEnd()
{
for (new client = 1; client <= MaxClients; client++)
{
if (IsClientInGame(client))
{
GetClientAuthId(client, AuthId_Engine, authid[client], sizeof(authid[]));
PrintToServer("[Car] Plugin Ending -- updating client in SQL Database.", authid[client]);
LogMessage("[Car] Plugin Ending -- updating client in SQL Database.", authid[client]);
DBSave(client);
}
}
UnhookEntityOutput("trigger_multiple", "OnStartTouch", OnStartTouch);
UnhookEntityOutput("trigger_multiple", "OnEndTouch", OnEndTouch);
}
public OnConfigsExecuted()
{
PrecacheSoundAny("vehicles/mustang_horn.mp3", true);
AddFileToDownloadsTable("sound/vehicles/mustang_horn.mp3");
PrecacheSoundAny("vehicles/police_siren_single.mp3", true);
AddFileToDownloadsTable("sound/vehicles/police_siren_single.mp3");
PrecacheSoundAny("natalya/doors/latchunlocked1.mp3", true);
AddFileToDownloadsTable("sound/natalya/doors/latchunlocked1.mp3");
PrecacheSoundAny("natalya/doors/default_locked.mp3", true);
AddFileToDownloadsTable("sound/natalya/doors/default_locked.mp3");
PrecacheSoundAny("natalya/buttons/lightswitch2.mp3", true);
AddFileToDownloadsTable("sound/natalya/buttons/lightswitch2.mp3");
}
public OnMapEnd()
{
for (new client = 1; client <= MaxClients; client++)
{
if (IsClientInGame(client))
{
if (Cars[client] > 0)
{
for (new index = 0; index < 10; index++)
{
if (IsValidEntity(Car_Entity[client][index]))
{
new car = Car_Entity[client][index];
new driver = GetEntPropEnt(car, Prop_Send, "m_hPlayer");
if (driver != -1)
{
LeaveVehicle(driver);
drivers_car[driver] = -1;
}
Car_Gas[client][index] = car_fuel[car];
Car_Entity[client][index] = -1;
CarOn[car] = false;
AcceptEntityInput(car, "Kill");
cars_index[car] = -1;
}
}
}
}
}
if (g_CarTimer != INVALID_HANDLE)
{
CloseHandle(g_CarTimer);
g_CarTimer = INVALID_HANDLE;
}
if (g_GasTimer != INVALID_HANDLE)
{
CloseHandle(g_GasTimer);
g_GasTimer = INVALID_HANDLE;
}
if (h_siren_a != INVALID_HANDLE)
{
CloseHandle(h_siren_a);
h_siren_a = INVALID_HANDLE;
}
if (h_siren_b != INVALID_HANDLE)
{
CloseHandle(h_siren_b);
h_siren_b = INVALID_HANDLE;
}
if (h_siren_c != INVALID_HANDLE)
{
CloseHandle(h_siren_c);
h_siren_c = INVALID_HANDLE;
}
if (h_horn != INVALID_HANDLE)
{
CloseHandle(h_horn);
h_horn = INVALID_HANDLE;
}
}
// ######
// CAR DB
// ######
public InitializeCarDB()
{
ServerCommand("mp_startmoney 0");
new String:error[255];
new db_mode = GetConVarInt(g_Cvar_Database);
if (db_mode == 1)
{
db_cars = SQL_Connect("ln-roleplay", true, error, sizeof(error));
if (db_cars == INVALID_HANDLE)
{
SetFailState("[car_menu.smx] %s", error);
}
// Stuff
new len = 0;
decl String:query[20000];
// Format the DB Car Table
len += Format(query[len], sizeof(query) - len, "CREATE TABLE IF NOT EXISTS `Cars`");
len += Format(query[len], sizeof(query) - len, " (`STEAMID` varchar(25) NOT NULL, `LASTONTIME` int(25) NOT NULL DEFAULT 0, ");
for (new i = 0; i < 10; i++) // 10 cars max per user is hardcoded into the plugin here. Who IRL has 10 cars?
{
len += Format(query[len], sizeof(query) - len, "`%iTYPE` int(25) NOT NULL DEFAULT 0, `%iSKIN` int(25) NOT NULL DEFAULT 0, `%iFUEL` float(7,4) NOT NULL DEFAULT 0, ", i, i, i, i);
}
len += Format(query[len], sizeof(query) - len, "PRIMARY KEY (`STEAMID`));");
// Lock and Load!!
SQL_LockDatabase(db_cars);
SQL_FastQuery(db_cars, query);
SQL_UnlockDatabase(db_cars);
started = 1;
}
else if (db_mode == 2) //sqlite
{
db_cars = SQLite_UseDatabase("rp_cars", error, sizeof(error));
if (db_cars == INVALID_HANDLE)
{
SetFailState("[car_menu.smx] %s", error);
}
// Stuff
new len = 0;
decl String:query[20000];
// Format the DB Car Table
len += Format(query[len], sizeof(query) - len, "CREATE TABLE IF NOT EXISTS `Cars`");
len += Format(query[len], sizeof(query) - len, " (`STEAMID` varchar(25) NOT NULL, `LASTONTIME` int(25) NOT NULL DEFAULT 0, ");
for (new i = 0; i < 10; i++) // 10 cars max per user is hardcoded into the plugin here. Who IRL has 10 cars?
{
len += Format(query[len], sizeof(query) - len, "`%iTYPE` int(25) NOT NULL DEFAULT 0, `%iSKIN` int(25) NOT NULL DEFAULT 0, `%iFUEL` float(7,4) NOT NULL DEFAULT 0, ", i, i, i, i);
}
len += Format(query[len], sizeof(query) - len, "PRIMARY KEY (`STEAMID`));");
// Lock and Load!!
SQL_LockDatabase(db_cars);
SQL_FastQuery(db_cars, query);
SQL_UnlockDatabase(db_cars);
started = 1;
}
else
{
Format(error, sizeof(error), "[Car] car_db_mode is %i when it needs to be 1 or 2.", db_mode);
SetFailState("[car_menu.smx] %s", error);
}
}
public InitializeClientonDB(client)
{
if (IsFakeClient(client))return true;
new String:SteamId[255];
new String:query[255];
new conuserid;
conuserid = GetClientUserId(client);
GetClientAuthId(client, AuthId_Engine, SteamId, sizeof(SteamId));
Format(query, sizeof(query), "SELECT LASTONTIME FROM Cars WHERE STEAMID = '%s';", SteamId);
SQL_TQuery(db_cars, T_CheckConnectingItems, query, conuserid);
return true;
}
public T_CheckConnectingItems(Handle:owner, Handle:hndl, const String:error[], any:data)
{
new client;
/* Make sure the client didn't disconnect while the thread was running */
if ((client = GetClientOfUserId(data)) == 0)
{
return true;
}
new String:SteamId[255];
GetClientAuthId(client, AuthId_Engine, SteamId, sizeof(SteamId));
if (hndl == INVALID_HANDLE)
{
LogError("Query failed! %s", error);
InitializeCarDB();
}
else
{
new String:buffer[512];
if (!SQL_GetRowCount(hndl))
{
// insert user
GetClientAuthId(client, AuthId_Engine, SteamId, sizeof(SteamId));
Format(buffer, sizeof(buffer), "INSERT INTO Cars (`STEAMID`,`LASTONTIME`, `0TYPE`, `0Skin`,`0FUEL`, `1TYPE`, `1Skin`,`1FUEL`, `2TYPE`, `2Skin`,`2FUEL`, `3TYPE`, `3Skin`,`3FUEL`, `4TYPE`, `4Skin`,`4FUEL`, `5TYPE`, `5Skin`,`5FUEL`, `6TYPE`, `6Skin`,`6FUEL`, `7TYPE`, `7Skin`,`7FUEL`, `8TYPE`, `8Skin`,`8FUEL`, `9TYPE`, `9Skin`,`9FUEL`) VALUES ('%s',%i, -1, 0, 0.0, -1, 0, 0.0, -1, 0, 0.0, -1, 0, 0.0, -1, 0, 0.0, -1, 0, 0.0, -1, 0, 0.0, -1, 0, 0.0, -1, 0, 0.0, -1, 0, 0.0);", SteamId, GetTime());
SQL_FastQuery(db_cars, buffer);
for (new X = 0; X < 10; X++)
{
Car_Type[client][X] = 0;
Car_Skin[client][X] = 0;
Car_Gas[client][X] = 0.0;
Car_Entity[client][X] = -1;
}
Cars[client] = 0;
}
else
{
Format(buffer, sizeof(buffer), "SELECT * FROM `Cars` WHERE STEAMID = '%s';", SteamId);
SQL_TQuery(db_cars, DBCarLoad_Callback, buffer, data);
}
FinallyLoaded(client);
}
return true;
}
public SQLErrorCheckCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if (!StrEqual("", error))
{
LogMessage("SQL Error: %s", error);
}
}
public FinallyLoaded(client)
{
if (!IsClientConnected(client))return true;
InQuery = false;
Loaded[client] = true;
IsDisconnect[client] = false;
return true;
}
// New Client Loading Done. Now for Client Data Updating
public Action:DBSave_Restart(Handle:Timer, any:client) {
DBSave(client);
return Plugin_Handled;
}
public DBSave(client)
{
if (!IsClientConnected(client))return true;
if (InQuery)
{
CreateTimer(1.0, DBSave_Restart, client);
return true;
}
if (Loaded[client]) {
InQuery = true;
new userid = GetClientUserId(client);
//Declare:
new String:SteamId[32], String:query[255];
new UnixTime = GetTime();
//Initialize:
GetClientAuthId(client, AuthId_Engine, SteamId, 32);
Format(query, sizeof(query), "UPDATE Cars SET LASTONTIME = %d WHERE STEAMID = '%s';", UnixTime, SteamId);
SQL_TQuery(db_cars, T_SaveCallback, query, userid);
new t;
new s;
new Float:f;
for (new X = 0; X < 10; X++)
{
t = Car_Type[client][X];
s = Car_Skin[client][X];
f = Car_Gas[client][X];
Format(query, sizeof(query), "UPDATE Cars SET `%iTYPE` = %i, `%iSKIN` = %i, `%iFUEL` = %f WHERE STEAMID = '%s';", X, t, X, s, X, f, SteamId);
SQL_TQuery(db_cars, T_SaveCallback, query, userid);
}
if (IsDisconnect[client])
{
Loaded[client] = false;
IsDisconnect[client] = false;
}
InQuery = false;
}
return true;
}
// Now we go to loading existing clients.
public T_SaveCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
/* Make sure the client didn't disconnect while the thread was running */
if (error[0])
{
LogError("[CAR DB ERROR] %s", error);
}
if (GetClientOfUserId(data) == 0)
{
return;
}
return;
}
public DBCarLoad_Callback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
new client = GetClientOfUserId(data);
Cars[client] = 0;
//Make sure the client didn't disconnect while the thread was running
if (client == 0)
{
return true;
}
if (hndl == INVALID_HANDLE)
{
LogError("Query failed! %s", error);
InitializeCarDB();
}
else
{
if (!SQL_GetRowCount(hndl))
{
LogError("Database error! SteamID not found!");
}
else
{
Cars[client] = 0;
while (SQL_FetchRow(hndl))
{
new count = 2;
new countb = 3;
new countc = 4;
for (new X = 0; X < 10; X++)
{
Car_Type[client][X] = SQL_FetchInt(hndl, (X + count));
if (Car_Type[client][X] > 0)
{
Cars[client] += 1;
Car_Skin[client][X] = SQL_FetchInt(hndl, (X + countb));
Car_Gas[client][X] = SQL_FetchFloat(hndl, (X + countc));
Car_Entity[client][X] = -1;
}
else(Car_Type[client][X] = 0);
count += 2;
countb += 2;
countc += 2;
}
}
}
}
return true;
}
// #######
// CLIENTS
// #######
public bool:OnClientConnect(client, String:Reject[], Len)
{
if (GetConVarInt(g_Cvar_Debug))
{
PrintToServer("[Car DEBUG] Player %N (#%i) has Connected.", client, client);
}
//Disable:
Loaded[client] = false;
Cars[client] = 0;
cars_spawned[client] = 0;
can_stow[client] = true;
for (new index = 0; index < 10; index++)
{
Car_Entity[client][index] = -1;
}
return true;
}
public OnClientPutInServer(client)
{
if (GetConVarInt(g_Cvar_Debug))
{
PrintToServer("[Car DEBUG] Player %N (#%i) is being put in the server.", client, client);
}
//Default Values:
CarHorn[client] = false;
Driving[client] = false;
Needs_Reset[client] = false;
Reset_Car_Selection(client);
drivers_car[client] = -1;
AtGas[client] = false;
if (GetConVarInt(g_Cvar_Debug))
{
PrintToServer("[Car DEBUG] Player %N (#%i) has had their defaults set.", client, client);
}
if (GetConVarInt(g_Cvar_Enable))
{
SDKHook(client, SDKHook_WeaponDrop, OnWeaponDrop);
SDKHook(client, SDKHook_PreThink, OnPreThink);
SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
if (GetConVarInt(g_Cvar_Debug))
{
PrintToServer("[Car DEBUG] Player %N (#%i) has had their SDK Hooks set up.", client, client);
}
if (!Loaded[client])
{
CreateTimer(1.0, CreateSQLAccount, client);
}
}
for (new index = 0; index < 10; index++)
{
Car_Entity[client][index] = -1;
}
}
public OnClientPostAdminCheck(client)
{
if (GetConVarInt(g_Cvar_Debug))
{
PrintToServer("[Car DEBUG] Player %N (#%i) is past the Admin Check.", client, client);
}
for (new index = 0; index < 10; index++)
{
Car_Entity[client][index] = -1;
}
}
public Action:CreateSQLAccount(Handle:Timer, any:client)
{
if (!IsClientConnected(client))
{
CreateTimer(1.0, CreateSQLAccount, client);
}
else
{
new String:SteamId[64];
GetClientAuthId(client, AuthId_Engine, SteamId, 64);
if (StrEqual(SteamId, "") || InQuery)
{
CreateTimer(1.0, CreateSQLAccount, client);
}
else
{
//PrintToChatAll("Connection Successful");
// InQuery stops it from loading more than one player at a time.
InQuery = true;
InitializeClientonDB(client);
}
}
}
public OnClientDisconnect(client)
{
CarHorn[client] = false;
Driving[client] = false;
AtGas[client] = false;
Reset_Car_Selection(client);
if (drivers_car[client] != -1)
{
if (client > 0)
{
if (IsClientInGame(client))
{
drivers_car[client] = -1
LeaveVehicle(client);
}
}
}
for (new index = 0; index < 10; index++)
{
new car;
car = Car_Entity[client][index];
if ((car > 0) && (IsValidEdict(car)) && (IsValidEntity(car)))
{
new String:ClassName[64];
GetEdictClassname(car, ClassName, 255);
if (StrEqual(ClassName, "prop_vehicle_driveable", false))
{
new driver = GetEntPropEnt(car, Prop_Send, "m_hPlayer");
if (driver != -1)
{
LeaveVehicle(driver);
}
}
AcceptEntityInput(car, "Kill");
//SetCarOwnership(client, car, 0);
CarOn[car] = false;
cars_index[car] = -1;
}
Car_Entity[client][index] = -1;
}
IsDisconnect[client] = true;
DBSave(client);
can_stow[client] = true;
cars_spawned[client] = 0;
SDKUnhook(client, SDKHook_OnTakeDamage, OnTakeDamage);
}
// ########
// CAR MENU
// ########
public Menu_Car(Handle:car, MenuAction:action, param1, param2)
{
if (action == MenuAction_Select)
{
Reset_Car_Selection(param1);
new String:info[32];
GetMenuItem(car, param2, info, sizeof(info));
if (StrEqual(info, "g_InvMenu"))
{
/* Create the menu Handle */
new Handle:inv = CreateMenu(Menu_Inventory);
decl String:title_str[32];
if (Cars[param1] <= 0)
{
Format(title_str, sizeof(title_str), "%T:", "Garage", param1);
decl String:cars_str[64];
Format(cars_str, sizeof(cars_str), "%T", "Car_Zero", param1);
AddMenuItem(inv, "-1", cars_str, ITEMDRAW_DISABLED);
}
if (Cars[param1] == 1)
{
Format(title_str, sizeof(title_str), "%T", "Car_One", param1);
}
if (Cars[param1] >> 1)
{
Format(title_str, sizeof(title_str), "%T", "Car_Qty", param1, Cars[param1]);
}
if (Cars[param1] > 0)
{
// Figure out what cars they have.
decl String:car_string[64];
decl t;
for (new index = 0; index < 10; index++)
{
t = Car_Type[param1][index];
if (t > 0)
{
Format(car_string, sizeof(car_string), "%i", index);
AddMenuItem(inv, car_string, car_name[t]);
}
}
}
SetMenuTitle(inv, title_str);
DisplayMenu(inv, param1, MENU_TIME_FOREVER);
}
if (StrEqual(info, "g_BuyMenu"))
{
DisplayMenu(g_BuyMenu, param1, MENU_TIME_FOREVER);
}
if (StrEqual(info, "g_InfoMenu"))
{
DisplayMenu(g_InfoMenu, param1, MENU_TIME_FOREVER); \
}
if (StrEqual(info, "g_VIPBuyMenu"))
{
DisplayMenu(g_VIPBuyMenu, param1, MENU_TIME_FOREVER);
}
if (StrEqual(info, "g_VIPInfoMenu"))
{
DisplayMenu(g_VIPInfoMenu, param1, MENU_TIME_FOREVER); \
}
}
if (action == MenuAction_End) {
delete car;
}
}
// #############
// CAR INVENTORY
// #############
public Menu_Inventory(Handle:inv, MenuAction:action, param1, param2)
{
if (action == MenuAction_Select)
{
new String:info[32];
GetMenuItem(inv, param2, info, sizeof(info));
new index = StringToInt(info);
if (index < 0)
{
PrintToChat(param1, "\x04[Car] %T (00)", "Selection_Error", param1);
return;
}
if (Car_Type[param1][index] < 1)
{
PrintToChat(param1, "\x04[Car] %T (01)", "Selection_Error", param1);
return;
}
selected_car[param1] = index;
if (Car_Entity[param1][index] > 0)
{
selected_car_stowed[param1] = 0;
}
else selected_car_stowed[param1] = 1;
selected_car_skin[param1] = Car_Skin[param1][index];
selected_car_fuel[param1] = Car_Gas[param1][index];
selected_car_index[param1] = index;
selected_car_type[param1] = Car_Type[param1][index];
new t = selected_car_type[param1];
new String:skin_str[48];
KvJumpToKey(carbuykv, car_name[t], false);
decl String:skin_num[4], String:colour[32];
Format(skin_num, sizeof(skin_num), "%i", selected_car_skin[param1]);
KvGetString(carbuykv, skin_num, colour, sizeof(colour), "UNKNOWN");
Format(skin_str, sizeof(skin_str), "Colour: %s", colour);
KvRewind(carbuykv);
new String:title_str[32];
new String:spawn_str[32];
new String:info_str[32];
new String:give_away_str[32];
Format(title_str, sizeof(title_str), "%s (#%i)", car_name[t], index);
Format(spawn_str, sizeof(spawn_str), "%T", "Spawn_Car", param1);
Format(info_str, sizeof(info_str), "%T", "Car_Info", param1);
Format(give_away_str, sizeof(give_away_str), "%T", "Give_Away_Car", param1);
if (selected_car_stowed[param1] == 0)