-
Notifications
You must be signed in to change notification settings - Fork 1
/
yos_script.pas
2289 lines (2170 loc) · 85.4 KB
/
yos_script.pas
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
unit YOS_Script;
{$mode objfpc}{$H+}
interface
uses
ScriptHookV, Natives, Windows, ctypes, SysUtils, Classes,
YOS_Screen, YOS_WorldManager, YOS_Utils, YOS_DataFiles;
const
SCRIPT_MAJOR_VERSION = 2;
SCRIPT_MINOR_VERSION = 3;
type
// Forward declarations
TMissionThread = class;
TMissionScript = class;
// Script base types
TVariableLocation = (svlDirect = 0, svlLocal = 1, svlGlobal = 2);
TVariableType = (svtNone = 0, svtInt = 1, svtFloat = 2, svtString = 3, svtVector = 4, svtArray = 5, svtStringArray = 6, svtBlip = 7, svtPickup = 8, svtSavedObject = 9, svtForbiddenCube = 10, svtCarGenerator = 11);
TConditionalJumpEvaluationMethod = (cjmpSingle = 0, cjmpIfAnd = 1, cjmpIfOr = 2);
TNativeParamType = (nptNum32 = 0, nptVarNum32 = 1, nptInt32Ptr = 2, nptFloat32Ptr = 3, nptVector = 4, nptArray = 5, nptString = 6, nptStringPtr = 7);
TMissionThreadStatus = (tstRunning = 0, tstWaiting = 1, tstFinished = 2);
// Script complex types
TVariable = record
VarType: TVariableType;
Value: UINT32; // This is a raw, 32-bit data
end;
TStringVar = record
Used: boolean;
Value: string;
end;
TVectorVar = record
Used: boolean;
Value: Vector3;
end;
TArrayVar = record
Used: boolean;
Value: TGTA5Array; // This is an array of raw, 32-bit data
end;
TStringArrayVar = record
Used: boolean;
Value: array of string;
end;
TBlipVar = record
Used, IsMission, IsShortRange, Selectable, HasRadius: boolean;
X, Y, Z, Radius: cfloat;
Sprite, Color, Alpha: cint;
_handle: Blip;
end;
TPickupVar = record
Used, PickedUp, HasRotation: boolean;
X, Y, Z, XA, YA, ZA: cfloat;
PickupHash, ModelHash: Hash;
Value, RgTime, RgTimeRem: cint;
_handle: Pickup;
end;
TSavedObjectVar = record
Used: boolean;
_handle: GTAObject;
end;
TForbiddenCubeVar = record
Used: boolean;
CubeType: byte;
X1, Y1, Z1, X2, Y2, Z2: cfloat;
_handle: cint; // Handle used for Scenario Disabled Cubes
end;
TCarGeneratorVar = record
Used, IsActive: boolean;
Model: Hash;
X, Y, Z, A: cfloat;
_handle: cint;
end;
// Data storage types
TRespawnLocation = record
X, Y, Z, A: cfloat;
end;
TModelSwapRecord = record
OrigHash, NewHash: Hash;
X, Y, Z, Radius: cfloat;
end;
TModelHideRecord = record
ModelHash: Hash;
X, Y, Z, Radius: cfloat;
end;
TStoredVehicleData = record
Model: Hash;
PriColor, SecColor: cint;
CustColorPri, CustColorSec: record
Enabled: boolean;
R, G, B: cint;
end;
WheelType: cint;
Mods: array [0..49] of record
ModIndex: cint;
ModVariation: boolean;
end;
BulletProofTires: boolean;
TyreSmokeColor: record
R, G, B: cint;
end;
NumberPlateIndex: cint;
NumberPlateText: string;
PearlColor, RimColor: cint;
RoofState: boolean; // For Convertibles
Extras: array [0..60] of boolean;
Livery: cint;
NeonColor: record
R, G, B: cint;
end;
WindowTint, InteriorColor, DashboardColor, XenonColor: cint;
end;
TStoredPlayerData = record
X, Y, Z, A: cfloat;
Health, MaxHealth, Armor: cint;
Model: Hash;
OutfitData: record
Drawables: array [0..11] of record
DrawableVar, TextureVar, PaletteVar: cint;
end;
Props: array [0..2] of record
PropIndex, PropTexture: cint;
end;
end;
WeaponData: record
WeaponList: array of record
HashCode: Hash;
TintIndex, Ammo: cint;
Components: array of cint;
end;
end;
IsDriving: boolean;
VehicleData: TStoredVehicleData;
end;
TMultiplayerPersistenceData = record
Used: boolean;
Data: TStoredPlayerData;
end;
TVehiclePersistenceData = record
X, Y, Z, A: cfloat;
VehicleData: TStoredVehicleData;
end;
TStoredVehicleSlot = record
IsStored: boolean;
VehicleData: TStoredVehicleData;
end;
// Mission script thread manager class
TMissionThread = class (TObject)
private
// Management-only properties
script: TMissionScript;
lastRunTime, lastRunTimeDiff: INT64;
nativeCallResult: PUINT64;
isSavedGame, isSaveSuccessful: boolean;
// Persistent properties
offset: UINT64;
position: INT64;
status: TMissionThreadStatus;
waitTime: UINT32;
scriptName: string;
gosubLevel: byte;
gosubReturn: array [0..127] of INT64;
localVars: array [0..1023] of TVariable;
conditionalJumpEvaluationMethod: TConditionalJumpEvaluationMethod;
conditionalValues: array of boolean;
timers: array [0..15] of cfloat;
wbGosubActive: boolean;
wbGosubLevel: byte;
wbGosubReturn: INT64;
// Internal functions
function GetVarData(vloc: TVariableLocation; id: word): UINT32;
procedure SetVarData(vloc: TVariableLocation; vtype: TVariableType; id: word; value: UINT32);
function GetVarFloat(vloc: TVariableLocation; id: word): cfloat;
procedure SetVarFloat(vloc: TVariableLocation; id: word; value: cfloat);
function GetVarInt(vloc: TVariableLocation; id: word): cint;
procedure SetVarInt(vloc: TVariableLocation; id: word; value: cint);
procedure GetVarId(out vloc: TVariableLocation; out id: word);
function GetVarType(vloc: TVariableLocation; id: word): TVariableType;
procedure ClearVariable(vloc: TVariableLocation; id: word);
procedure AddConditionalResult(result, negate: boolean);
procedure AllocateString(vloc: TVariableLocation; id: word; value: string);
function GetNextParameterInt: cint;
function GetNextParameterFloat: cfloat;
function GetNextParameterString: string;
procedure CallOpcodeHandler(opcode: word; isPlayerDead, isPlayerBusted: boolean);
procedure Initialize;
public
// Public functions
constructor Create(owner: TMissionScript; fileOffset: UINT64);
constructor Create(owner: TMissionScript; stream: TStream; savMajorVer, savMinorVer: integer); // Used, when the thread data is loaded from a savegame file
destructor Destroy; override;
procedure Save(stream: TStream); // Used, when the thread data is saved into a savegame file
procedure Run(gameMenuType: TMenuType; isPlayerDead, isPlayerBusted: boolean);
property ThreadName: string read scriptName;
property ThreadStatus: TMissionThreadStatus read status write status;
end;
// Mission script manager class
TMissionScript = class (TObject)
private
// Management-only properties
bytes: TMemoryStream;
lastPickupCheckTime, lastPickupCheckTimeDiff: INT64;
isStarted: boolean;
// Persistent properties
threads: array of TMissionThread;
globalVars: array [0..65535] of TVariable;
globalStrings: array [0..8191] of TStringVar;
globalVectors: array [0..8191] of TVectorVar;
globalArrays: array [0..1023] of TArrayVar;
globalStringLists: array [0..127] of TStringArrayVar;
globalBlips: array [0..255] of TBlipVar;
globalPickups: array [0..255] of TPickupVar;
globalSavedObjects: array [0..255] of TSavedObjectVar;
globalForbiddenCubes: array [0..255] of TForbiddenCubeVar;
globalCarGenerators: array [0..511] of TCarGeneratorVar;
storedPlayerData: array [0..2] of TMultiplayerPersistenceData;
storedVehicleData: array of TVehiclePersistenceData;
storedVehicleSlots: array [0..15] of TStoredVehicleSlot;
enabledMaps, disabledMaps: TStrings;
modelSwapRecords: array of TModelSwapRecord;
modelHideRecords: array of TModelHideRecord;
hospitalRestarts, policeRestarts: array of TRespawnLocation;
overrideRestart: TRespawnLocation;
useOverrideRestart, isOnMission: boolean;
lastMission: string;
floatEqualThreshold: cfloat;
// Internal functions
procedure KillThreadByName(tname: string);
procedure CreateThread(offset: UINT64);
procedure ClearVariable(var variableRecord: TVariable);
procedure CreateManagedBlip(var blipRecord: TBlipVar);
procedure UpdateBlip(blipRecord: TBlipVar);
procedure CreateManagedPickup(var pickupRecord: TPickupVar);
procedure ManageObjects;
procedure RestoreManagedGameObject(var objectRecord: TSavedObjectVar; stream: TStream; savMajorVer, savMinorVer: integer); // Used, when the script data is loaded from a savegame file
procedure RestoreForbiddenCube(var forbiddenCubeRecord: TForbiddenCubeVar; stream: TStream; savMajorVer, savMinorVer: integer); // Used, when the script data is loaded from a savegame file
procedure RestoreCarGenerator(var carGeneratorRecord: TCarGeneratorVar; stream: TStream; savMajorVer, savMinorVer: integer); // Used, when the script data is loaded from a savegame file
procedure StoreManagedObject(objectRecord: TSavedObjectVar; stream: TStream); // Used, when the script data is saved into a savegame file
procedure StoreForbiddenCube(forbiddenCubeRecord: TForbiddenCubeVar; stream: TStream); // Used, when the script data is saved into a savegame file
procedure StoreCarGenerator(carGeneratorRecord: TCarGeneratorVar; stream: TStream); // Used, when the script data is saved into a savegame file
procedure StorePlayer(var data: TStoredPlayerData; actor: Ped);
procedure RestorePlayer(data: TStoredPlayerData; actor: Ped); // NOTE: this applies the stored properties onto the specified ped
procedure LoadPlayerData(var data: TStoredPlayerData; stream: TStream; savMajorVer, savMinorVer: integer); // Used, when the script data is loaded from a savegame file
procedure SavePlayerData(data: TStoredPlayerData; stream: TStream); // Used, when the script data is saved into a savegame file
procedure StoreVehicle(var data: TStoredVehicleData; veh: Vehicle);
function RestoreVehicle(data: TVehiclePersistenceData): Vehicle; // NOTE: this spawns a vehicle in the gameworld
procedure LoadVehicleData(var data: TStoredVehicleData; stream: TStream; savMajorVer, savMinorVer: integer); // Used, when the script data is loaded from a savegame file
procedure SaveVehicleData(data: TStoredVehicleData; stream: TStream); // Used, when the script data is saved into a savegame file
public
// Public functions
constructor Create(scriptfile: string);
destructor Destroy; override;
procedure Load(slot: integer); // Used, when the script data is loaded from a savegame file
procedure Save(slot: integer); // Used, when the script data is saved into a savegame file
procedure Reset;
procedure Run(gameMenuType: TMenuType; isPlayerDead, isPlayerBusted: boolean);
function IsAvailable: boolean;
function ThreadCount: integer; // Number of running threads
function FindNearestRespawnLocation(out location: TRespawnLocation; findHospital: boolean): boolean;
function GetInternalPlayerIndex: integer;
function GetActorInternalPlayerIndex(actor: Ped): integer;
function IsManagedBlip(handle: Blip): boolean;
function IsManagedPickup(handle: Pickup): boolean;
function IsManagedObject(handle: GTAObject): boolean;
function IsManagedCarGenerator(handle: cint): boolean;
property HasOverrideRestart: boolean read useOverrideRestart;
property OverrideRestartLocation: TRespawnLocation read overrideRestart;
property IsGameStarted: boolean read isStarted;
end;
var
MissionScript: TMissionScript;
implementation
var
_is_dll_init_final_: boolean; // This must be set to true during DLL init / final, to prevent object init/final from calling Natives, and causing a game crash!
WeaponModels, WeaponCompModels: TStrings;
{$I opcode_bin_helpers.inc}
{$I opcodes.inc}
// ################
// TMissionThread
// ################
{%region /fold 'TMissionThread'}
procedure TMissionThread.CallOpcodeHandler(opcode: word; isPlayerDead, isPlayerBusted: boolean);
begin
if (opcode >= $8000) then
OpCodes[opcode - $8000](Self, true, isPlayerDead, isPlayerBusted)
else
OpCodes[opcode](Self, false, isPlayerDead, isPlayerBusted);
end;
procedure TMissionThread.Initialize;
var
i: integer;
begin
script := nil;
lastRunTime := 0;
lastRunTimeDiff := 0;
nativeCallResult := nil;
isSavedGame := false;
isSaveSuccessful := false;
offset := 0;
position := 0;
status := tstRunning;
waitTime := 0;
scriptName := Format('NONAME_%s', [UpCase(HexStr(Pointer(Self)))]);
gosubLevel := 0;
for i := 0 to High(gosubReturn) do
gosubReturn[i] := 0;
for i := 0 to High(localVars) do
begin
localVars[i].VarType := svtNone;
localVars[i].Value := 0;
end;
conditionalJumpEvaluationMethod := cjmpSingle;
SetLength(conditionalValues, 0);
for i := 0 to High(timers) do
timers[i] := 0.0;
wbGosubActive := false;
wbGosubLevel := 0;
wbGosubReturn := 0;
end;
constructor TMissionThread.Create(owner: TMissionScript; fileOffset: UINT64);
begin
inherited Create;
Initialize;
script := owner;
offset := fileOffset;
end;
constructor TMissionThread.Create(owner: TMissionScript; stream: TStream; savMajorVer, savMinorVer: integer);
var
i: integer;
u32: UINT32;
begin
inherited Create;
Initialize;
script := owner;
isSavedGame := true;
offset := stream.ReadQWord;
position := INT64(stream.ReadQWord);
status := TMissionThreadStatus(stream.ReadByte);
waitTime := stream.ReadDWord;
scriptName := ReadCString(stream);
gosubLevel := stream.ReadByte;
for i := 0 to High(gosubReturn) do
gosubReturn[i] := INT64(stream.ReadQWord);
for i := 0 to High(localVars) do
begin
localVars[i].VarType := TVariableType(stream.ReadByte);
localVars[i].Value := stream.ReadDWord;
end;
conditionalJumpEvaluationMethod := TConditionalJumpEvaluationMethod(stream.ReadByte);
SetLength(conditionalValues, integer(stream.ReadDWord));
for i := 0 to High(conditionalValues) do
conditionalValues[i] := boolean(stream.ReadByte);
for i := 0 to High(timers) do
begin
u32 := stream.ReadDWord;
timers[i] := pcfloat(@u32)^;
end;
wbGosubActive := (stream.ReadByte <> 0);
if wbGosubActive then
begin
wbGosubLevel := stream.ReadByte;
wbGosubReturn := INT64(stream.ReadQWord);
end;
end;
destructor TMissionThread.Destroy;
var
i: integer;
begin
scriptName := '';
if (script <> nil) then
for i := 0 to High(localVars) do
script.ClearVariable(localVars[i]);
script := nil;
SetLength(conditionalValues, 0);
inherited;
end;
procedure TMissionThread.Save(stream: TStream);
var
i: integer;
f: cfloat;
begin
stream.WriteQWord(offset);
stream.WriteQWord(position);
stream.WriteByte(ord(status));
stream.WriteDWord(waitTime);
WriteCString(scriptName, stream);
stream.WriteByte(gosubLevel);
for i := 0 to High(gosubReturn) do
stream.WriteQWord(gosubReturn[i]);
for i := 0 to High(localVars) do
begin
stream.WriteByte(ord(localVars[i].VarType));
stream.WriteDWord(localVars[i].Value);
end;
stream.WriteByte(ord(conditionalJumpEvaluationMethod));
stream.WriteDWord(cint(Length(conditionalValues)));
for i := 0 to High(conditionalValues) do
if conditionalValues[i] then
stream.WriteByte(1)
else
stream.WriteByte(0);
for i := 0 to High(timers) do
begin
f := timers[i];
stream.WriteDWord(PUINT32(@f)^);
end;
if wbGosubActive then
begin
stream.WriteByte(1);
stream.WriteByte(wbGosubLevel);
stream.WriteQWord(wbGosubReturn);
end
else
stream.WriteByte(0);
isSaveSuccessful := true;
end;
procedure TMissionThread.Run(gameMenuType: TMenuType; isPlayerDead, isPlayerBusted: boolean);
var
currentTime: INT64;
i: integer;
begin
if (gameMenuType = gmInGame) then
while (status = tstRunning) or (status = tstWaiting) do
begin
// If the wasted-busted return is active, and the player is wasted or busted, perform a return from a wb gosub function
if wbGosubActive and (gosubLevel > wbGosubLevel) and (isPlayerDead or isPlayerBusted) then
begin
wbGosubActive := false;
gosubLevel := wbGosubLevel;
position := wbGosubReturn;
waitTime := 0;
end;
// If we have ANY amount of wait time remaining, we just wait...
if (waitTime > 0) then
begin
currentTime := CurrentTimeMs;
lastRunTimeDiff := lastRunTimeDiff + (currentTime - lastRunTime);
if (lastRunTimeDiff > 1000) then // We assume, that an 1 sec lag may means, that the user has paused their game...
lastRunTimeDiff := 0
else if (lastRunTimeDiff > 0) then
begin
for i := 0 to High(timers) do
timers[i] := timers[i] + (lastRunTimeDiff / 1000); // Increase Timers
if (lastRunTimeDiff >= waitTime) then
waitTime := 0
else
dec(waitTime, lastRunTimeDiff);
lastRunTimeDiff := 0
end;
lastRunTime := CurrentTimeMs;
break;
end
// If we have NO wait time remaining, but we're still in the 'waiting' state, switch back to the 'running' state
else if (status = tstWaiting) then
begin
status := tstRunning;
lastRunTime := CurrentTimeMs;
break;
end
// Go ahead boy...
else
begin
script.bytes.Position := offset + position;
inc(position, 2); {--->>> 2b opcode}
CallOpcodeHandler(script.bytes.ReadWord, isPlayerDead, isPlayerBusted);
for i := 0 to High(timers) do
timers[i] := timers[i] + ((CurrentTimeMs - lastRunTime) / 1000); // Increase Timers
lastRunTime := CurrentTimeMs;
end;
end
else
lastRunTime := CurrentTimeMs;
end;
{%endregion}
// ################
// TMissionScript
// ################
{%region /fold 'TMissionScript'}
procedure TMissionScript.KillThreadByName(tname: string);
var
i: integer;
begin
for i := 0 to High(threads) do
if (LowerCase(threads[i].ThreadName) = LowerCase(tname)) then
threads[i].ThreadStatus := tstFinished;
end;
procedure TMissionScript.CreateThread(offset: UINT64);
begin
SetLength(threads, Length(threads) + 1);
threads[High(threads)] := TMissionThread.Create(Self, offset);
end;
procedure TMissionScript.ClearVariable(var variableRecord: TVariable);
var
vindex: UINT32;
begin
// Free the related variable resources
vindex := variableRecord.Value;
case variableRecord.VarType of
svtString:
begin
globalStrings[vindex].Used := false;
globalStrings[vindex].Value := '';
end;
svtVector:
begin
globalVectors[vindex].Used := false;
ZeroMemory(@globalVectors[vindex].Value, sizeof(Vector3));
end;
svtArray:
begin
globalArrays[vindex].Used := false;
SetLength(globalArrays[vindex].Value, 0);
end;
svtStringArray:
begin
globalStringLists[vindex].Used := false;
SetLength(globalStringLists[vindex].Value, 0);
end;
svtBlip:
begin
globalBlips[vindex].Used := false;
if (not _is_dll_init_final_) and (globalBlips[vindex]._handle <> 0) then
begin
if (DOES_BLIP_EXIST(globalBlips[vindex]._handle) <> BOOL(0)) then
REMOVE_BLIP(@globalBlips[vindex]._handle);
end;
ZeroMemory(@globalBlips[vindex], sizeof(TBlipVar));
end;
svtPickup:
begin
globalPickups[vindex].Used := false;
if (not _is_dll_init_final_) and (globalPickups[vindex]._handle <> 0) then
begin
if (DOES_PICKUP_OBJECT_EXIST(globalPickups[vindex]._handle) <> BOOL(0)) then
REMOVE_PICKUP(globalPickups[vindex]._handle);
end;
ZeroMemory(@globalPickups[vindex], sizeof(TPickupVar));
end;
svtSavedObject:
begin
globalSavedObjects[vindex].Used := false;
ZeroMemory(@globalSavedObjects[vindex], sizeof(TSavedObjectVar));
end;
svtForbiddenCube:
begin
globalForbiddenCubes[vindex].Used := false;
if (not _is_dll_init_final_) then
begin
case globalForbiddenCubes[vindex].CubeType of
0: SET_PED_PATHS_BACK_TO_ORIGINAL(globalForbiddenCubes[vindex].X1, globalForbiddenCubes[vindex].Y1, globalForbiddenCubes[vindex].Z1, globalForbiddenCubes[vindex].X2, globalForbiddenCubes[vindex].Y2, globalForbiddenCubes[vindex].Z2, 1);
1: SET_ROADS_BACK_TO_ORIGINAL(globalForbiddenCubes[vindex].X1, globalForbiddenCubes[vindex].Y1, globalForbiddenCubes[vindex].Z1, globalForbiddenCubes[vindex].X2, globalForbiddenCubes[vindex].Y2, globalForbiddenCubes[vindex].Z2, 1);
2: REMOVE_SCENARIO_BLOCKING_AREA(globalForbiddenCubes[vindex]._handle, BOOL(0));
end;
end;
ZeroMemory(@globalForbiddenCubes[vindex], sizeof(TForbiddenCubeVar));
end;
svtCarGenerator:
begin
globalCarGenerators[vindex].Used := false;
if (not _is_dll_init_final_) and globalCarGenerators[vindex].IsActive and (globalCarGenerators[vindex]._handle <> 0) then
begin
if (DOES_SCRIPT_VEHICLE_GENERATOR_EXIST(globalCarGenerators[vindex]._handle) <> BOOL(0)) then
DELETE_SCRIPT_VEHICLE_GENERATOR(globalCarGenerators[vindex]._handle);
end;
ZeroMemory(@globalCarGenerators[vindex], sizeof(TCarGeneratorVar));
end;
end;
// Reset var record
variableRecord.VarType := svtNone;
variableRecord.Value := 0;
end;
procedure TMissionScript.CreateManagedBlip(var blipRecord: TBlipVar);
begin
if blipRecord.HasRadius then
begin
blipRecord._handle := ADD_BLIP_FOR_RADIUS(blipRecord.X, blipRecord.Y, blipRecord.Z, blipRecord.Radius);
SET_BLIP_ALPHA(blipRecord._handle, blipRecord.Alpha);
end
else
begin
blipRecord._handle := ADD_BLIP_FOR_COORD(blipRecord.X, blipRecord.Y, blipRecord.Z);
SET_BLIP_SPRITE(blipRecord._handle, blipRecord.Sprite);
end;
UpdateBlip(blipRecord);
end;
procedure TMissionScript.UpdateBlip(blipRecord: TBlipVar);
var
flags: cint;
begin
if (DOES_BLIP_EXIST(blipRecord._handle) <> BOOL(0)) then
begin
SET_BLIP_COLOUR(blipRecord._handle, blipRecord.Color);
SET_BLIP_AS_SHORT_RANGE(blipRecord._handle, BOOL(blipRecord.IsShortRange and (not blipRecord.HasRadius)));
if blipRecord.Selectable and (not blipRecord.HasRadius) then
flags := 2
else
flags := 8;
SET_BLIP_DISPLAY(blipRecord._handle, flags);
end;
end;
procedure TMissionScript.CreateManagedPickup(var pickupRecord: TPickupVar);
begin
if pickupRecord.HasRotation then
pickupRecord._handle := CREATE_PICKUP_ROTATE(pickupRecord.PickupHash, pickupRecord.X, pickupRecord.Y, pickupRecord.Z, pickupRecord.XA, pickupRecord.YA, pickupRecord.ZA, 8, pickupRecord.Value, 2, BOOL(1), pickupRecord.ModelHash)
else
pickupRecord._handle := CREATE_PICKUP(pickupRecord.PickupHash, pickupRecord.X, pickupRecord.Y, pickupRecord.Z, 512, pickupRecord.Value, BOOL(1), pickupRecord.ModelHash);
pickupRecord.PickedUp := false;
end;
procedure TMissionScript.ManageObjects;
var
currentTime: INT64;
i: integer;
begin
// Manages blips, that are not meant to be seen during missions
for i := 0 to High(globalBlips) do
if globalBlips[i].Used then
begin
if isOnMission then
begin
if (not globalBlips[i].IsMission) and (globalBlips[i]._handle <> 0) and (DOES_BLIP_EXIST(globalBlips[i]._handle) <> BOOL(0)) then
begin
REMOVE_BLIP(@globalBlips[i]._handle);
globalBlips[i]._handle := 0;
end;
end
else
begin
if (not globalBlips[i].IsMission) and ((globalBlips[i]._handle = 0) or (DOES_BLIP_EXIST(globalBlips[i]._handle) = BOOL(0))) then
CreateManagedBlip(globalBlips[i]);
end;
end;
// Manages pickups' regeneration and 'picked up' property
for i := 0 to High(globalPickups) do
if globalPickups[i].Used then
begin
if globalPickups[i].PickedUp then
begin
if (globalPickups[i].RgTimeRem = 0) and (globalPickups[i].RgTime >= 0) then
CreateManagedPickup(globalPickups[i])
else
begin
currentTime := currentTimeMs;
lastPickupCheckTimeDiff := lastPickupCheckTimeDiff + (currentTime - lastPickupCheckTime);
if (lastPickupCheckTimeDiff > 1000) then // We assume, that an 1 sec lag may means, that the user has paused their game...
lastPickupCheckTimeDiff := 0
else if (lastPickupCheckTimeDiff > 0) then
begin
if (lastPickupCheckTimeDiff > globalPickups[i].RgTimeRem) then
globalPickups[i].RgTimeRem := 0
else
dec(globalPickups[i].RgTimeRem, lastPickupCheckTimeDiff);
lastPickupCheckTimeDiff := 0;
end;
lastPickupCheckTime := currentTimeMs;
end;
end
else if (globalPickups[i]._handle = 0) or (DOES_PICKUP_OBJECT_EXIST(globalPickups[i]._handle) = BOOL(0)) then
begin
globalPickups[i].PickedUp := true;
globalPickups[i].RgTimeRem := globalPickups[i].RgTime;
if (globalPickups[i]._handle <> 0) then
REMOVE_PICKUP(globalPickups[i]._handle);
globalPickups[i]._handle := 0;
lastPickupCheckTimeDiff := 0;
end;
end;
lastPickupCheckTime := currentTimeMs;
end;
procedure TMissionScript.RestoreManagedGameObject(var objectRecord: TSavedObjectVar; stream: TStream; savMajorVer, savMinorVer: integer);
var
model: Hash;
u32: UINT32;
x, y, z, xa, ya, za: cfloat;
begin
model := stream.ReadDWord;
u32 := stream.ReadDWord;
x := pcfloat(@u32)^;
u32 := stream.ReadDWord;
y := pcfloat(@u32)^;
u32 := stream.ReadDWord;
z := pcfloat(@u32)^;
u32 := stream.ReadDWord;
xa := pcfloat(@u32)^;
u32 := stream.ReadDWord;
ya := pcfloat(@u32)^;
u32 := stream.ReadDWord;
za := pcfloat(@u32)^;
// Spawn object
if (model <> 0) then // Only try to load objects, that were valid upon being saved....
begin
while (HAS_MODEL_LOADED(model) = BOOL(0)) do
begin
REQUEST_MODEL(model);
GameScreen.DrawLoadingScreen;
ScriptHookVWait(0);
end;
objectRecord._handle := CREATE_OBJECT_NO_OFFSET(model, x, y, z, BOOL(0), BOOL(1), BOOL(0));
SET_ENTITY_ROTATION(objectRecord._handle, xa, ya, za, 1, BOOL(1));
SET_MODEL_AS_NO_LONGER_NEEDED(model);
end
else
objectRecord._handle := 0;
end;
procedure TMissionScript.RestoreForbiddenCube(var forbiddenCubeRecord: TForbiddenCubeVar; stream: TStream; savMajorVer, savMinorVer: integer);
var
u32: UINT32;
begin
forbiddenCubeRecord.CubeType := stream.ReadByte;
u32 := stream.ReadDWord;
forbiddenCubeRecord.X1 := pcfloat(@u32)^;
u32 := stream.ReadDWord;
forbiddenCubeRecord.Y1 := pcfloat(@u32)^;
u32 := stream.ReadDWord;
forbiddenCubeRecord.Z1 := pcfloat(@u32)^;
u32 := stream.ReadDWord;
forbiddenCubeRecord.X2 := pcfloat(@u32)^;
u32 := stream.ReadDWord;
forbiddenCubeRecord.Y2 := pcfloat(@u32)^;
u32 := stream.ReadDWord;
forbiddenCubeRecord.Z2 := pcfloat(@u32)^;
case forbiddenCubeRecord.CubeType of
0: SET_PED_PATHS_IN_AREA(forbiddenCubeRecord.X1, forbiddenCubeRecord.Y1, forbiddenCubeRecord.Z1, forbiddenCubeRecord.X2, forbiddenCubeRecord.Y2, forbiddenCubeRecord.Z2, BOOL(0), 0);
1: SET_ROADS_IN_AREA(forbiddenCubeRecord.X1, forbiddenCubeRecord.Y1, forbiddenCubeRecord.Z1, forbiddenCubeRecord.X2, forbiddenCubeRecord.Y2, forbiddenCubeRecord.Z2, BOOL(0), BOOL(1));
2: forbiddenCubeRecord._handle := ADD_SCENARIO_BLOCKING_AREA(forbiddenCubeRecord.X1, forbiddenCubeRecord.Y1, forbiddenCubeRecord.Z1, forbiddenCubeRecord.X2, forbiddenCubeRecord.Y2, forbiddenCubeRecord.Z2, BOOL(0), BOOL(1), BOOL(1), BOOL(1), 1);
end;
end;
procedure TMissionScript.RestoreCarGenerator(var carGeneratorRecord: TCarGeneratorVar; stream: TStream; savMajorVer, savMinorVer: integer);
var
u32: UINT32;
begin
carGeneratorRecord.IsActive := (stream.ReadByte <> 0);
carGeneratorRecord.Model := stream.ReadDWord;
u32 := stream.ReadDWord;
carGeneratorRecord.X := pcfloat(@u32)^;
u32 := stream.ReadDWord;
carGeneratorRecord.Y := pcfloat(@u32)^;
u32 := stream.ReadDWord;
carGeneratorRecord.Z := pcfloat(@u32)^;
u32 := stream.ReadDWord;
carGeneratorRecord.A := pcfloat(@u32)^;
if carGeneratorRecord.IsActive then
carGeneratorRecord._handle := CREATE_SCRIPT_VEHICLE_GENERATOR(carGeneratorRecord.X, carGeneratorRecord.Y, carGeneratorRecord.Z, carGeneratorRecord.A, 5.0, 3.0, carGeneratorRecord.Model, -1, -1, -1, -1, BOOL(1), BOOL(0), BOOL(0), BOOL(0), BOOL(1), -1)
else
carGeneratorRecord._handle := 0;
end;
procedure TMissionScript.StoreManagedObject(objectRecord: TSavedObjectVar; stream: TStream);
var
pos: Vector3;
cf: cfloat;
begin
if (objectRecord._handle <> 0) and (GET_ENTITY_TYPE(objectRecord._handle) = 3) then // Only try to write valid properties for actually valid objects
begin
stream.WriteDWord(GET_ENTITY_MODEL(objectRecord._handle));
pos := GET_ENTITY_COORDS(objectRecord._handle, BOOL(0));
stream.WriteDWord(PUINT32(@pos.x)^);
stream.WriteDWord(PUINT32(@pos.y)^);
stream.WriteDWord(PUINT32(@pos.z)^);
cf := GET_ENTITY_PITCH(objectRecord._handle);
stream.WriteDWord(PUINT32(@cf)^);
cf := GET_ENTITY_ROLL(objectRecord._handle);
stream.WriteDWord(PUINT32(@cf)^);
cf := GET_ENTITY_HEADING(objectRecord._handle);
stream.WriteDWord(PUINT32(@cf)^);
end
else
begin
// Dummy data for invalid, but still referenced objects. All fileds, even the floats, are 4 byte DWORDs. We fill the whole record with zeroes...
stream.WriteDWord(0); // Model
stream.WriteDWord(0); // X
stream.WriteDWord(0); // Y
stream.WriteDWord(0); // Z
stream.WriteDWord(0); // XA
stream.WriteDWord(0); // YA
stream.WriteDWord(0); // ZA
end;
end;
procedure TMissionScript.StoreForbiddenCube(forbiddenCubeRecord: TForbiddenCubeVar; stream: TStream);
var
cf: cfloat;
begin
stream.WriteByte(forbiddenCubeRecord.CubeType);
cf := forbiddenCubeRecord.X1;
stream.WriteDWord(PUINT32(@cf)^);
cf := forbiddenCubeRecord.Y1;
stream.WriteDWord(PUINT32(@cf)^);
cf := forbiddenCubeRecord.Z1;
stream.WriteDWord(PUINT32(@cf)^);
cf := forbiddenCubeRecord.X2;
stream.WriteDWord(PUINT32(@cf)^);
cf := forbiddenCubeRecord.Y2;
stream.WriteDWord(PUINT32(@cf)^);
cf := forbiddenCubeRecord.Z2;
stream.WriteDWord(PUINT32(@cf)^);
end;
procedure TMissionScript.StoreCarGenerator(carGeneratorRecord: TCarGeneratorVar; stream: TStream);
var
cf: cfloat;
begin
if carGeneratorRecord.IsActive then
stream.WriteByte(1)
else
stream.WriteByte(0);
stream.WriteDWord(carGeneratorRecord.Model);
cf := carGeneratorRecord.X;
stream.WriteDWord(PUINT32(@cf)^);
cf := carGeneratorRecord.Y;
stream.WriteDWord(PUINT32(@cf)^);
cf := carGeneratorRecord.Z;
stream.WriteDWord(PUINT32(@cf)^);
cf := carGeneratorRecord.A;
stream.WriteDWord(PUINT32(@cf)^);
end;
procedure TMissionScript.StorePlayer(var data: TStoredPlayerData; actor: Ped);
var
pos: Vector3;
veh: Vehicle;
hash1, hash2: Hash;
h, i, j, k: integer;
begin
// Basic data
pos := GET_ENTITY_COORDS(actor, BOOL(0));
data.X := pos.x;
data.Y := pos.y;
data.Z := pos.z;
data.A := GET_ENTITY_HEADING(actor);
data.Health := GET_ENTITY_HEALTH(actor);
data.MaxHealth := GET_PED_MAX_HEALTH(actor);
data.Armor := GET_PED_ARMOUR(actor);
// Model and outfit
data.Model := GET_ENTITY_MODEL(actor);
for i := 0 to 11 do
begin
data.OutfitData.Drawables[i].DrawableVar := GET_PED_DRAWABLE_VARIATION(actor, cint(i));
data.OutfitData.Drawables[i].TextureVar := GET_PED_TEXTURE_VARIATION(actor, cint(i));
data.OutfitData.Drawables[i].PaletteVar := GET_PED_PALETTE_VARIATION(actor, cint(i));
end;
for i := 0 to 2 do
begin
data.OutfitData.Props[i].PropIndex := GET_PED_PROP_INDEX(actor, cint(i), 1);
data.OutfitData.Props[i].PropTexture := GET_PED_PROP_TEXTURE_INDEX(actor, cint(i));
end;
// Weapon data
SetLength(data.WeaponData.WeaponList, 0);
for i := 0 to WeaponModels.Count - 1 do
begin
hash1 := GET_HASH_KEY(PChar(WeaponModels[i]));
if (HAS_PED_GOT_WEAPON(actor, hash1, BOOL(0)) <> BOOL(0)) then
begin
SetLength(data.WeaponData.WeaponList, Length(data.WeaponData.WeaponList) + 1);
h := High(data.WeaponData.WeaponList);
data.WeaponData.WeaponList[h].HashCode := hash1;
data.WeaponData.WeaponList[h].TintIndex := GET_PED_WEAPON_TINT_INDEX(actor, hash1);
data.WeaponData.WeaponList[h].Ammo := GET_AMMO_IN_PED_WEAPON(actor, hash1);
SetLength(data.WeaponData.WeaponList[h].Components, 0);
for j := 0 to WeaponCompModels.Count - 1 do
begin
hash2 := GET_HASH_KEY(PChar(WeaponCompModels[j]));
if (HAS_PED_GOT_WEAPON_COMPONENT(actor, hash1, hash2) <> BOOL(0)) then
begin
SetLength(data.WeaponData.WeaponList[h].Components, Length(data.WeaponData.WeaponList[h].Components) + 1);
k := High(data.WeaponData.WeaponList[h].Components);
data.WeaponData.WeaponList[h].Components[k] := hash2;
end;
end;
end;
end;
// Personal vehicle
veh := GET_VEHICLE_PED_IS_USING(actor);
data.IsDriving := (veh <> 0);
if data.IsDriving then
StoreVehicle(data.VehicleData, veh);
end;
procedure TMissionScript.RestorePlayer(data: TStoredPlayerData; actor: Ped);
var
i, j: integer;
vdata: TVehiclePersistenceData;
veh: Vehicle;
begin
// Basic data
SET_ENTITY_COORDS_NO_OFFSET(actor, data.X, data.Y, data.Z, BOOL(0), BOOL(0), BOOL(0));
SET_ENTITY_HEADING(actor, data.A);
SET_ENTITY_HEALTH(actor, data.Health, 0, 0);
SET_PED_MAX_HEALTH(actor, data.MaxHealth);
SET_PED_ARMOUR(actor, data.Armor);
// Model and outfit
if (actor = GET_PLAYER_PED(GET_PLAYER_INDEX)) then
begin
// Can only change the Player's model, but not of Peds'
while (HAS_MODEL_LOADED(data.model) = BOOL(0)) do
begin
REQUEST_MODEL(data.Model);
GameScreen.DrawLoadingScreen;
ScriptHookVWait(0);
end;
SET_PLAYER_MODEL(GET_PLAYER_INDEX, data.Model);
actor := GET_PLAYER_PED(GET_PLAYER_INDEX); // Changing Player model invalidates the original Ped!
SET_MODEL_AS_NO_LONGER_NEEDED(data.Model);
end;
if (GET_ENTITY_MODEL(actor) = data.Model) then
begin
// Should only try to apply outfit, if the current Ped model is the same as the stored Ped model
for i := 0 to 11 do
SET_PED_COMPONENT_VARIATION(actor, cint(i), data.OutfitData.Drawables[i].DrawableVar, data.OutfitData.Drawables[i].TextureVar, data.OutfitData.Drawables[i].PaletteVar);
for i := 0 to 2 do
SET_PED_PROP_INDEX(actor, cint(i), data.OutfitData.Props[i].PropIndex, data.OutfitData.Props[i].PropTexture, BOOL(0), 1);
end;
// Weapons
REMOVE_ALL_PED_WEAPONS(actor, BOOL(0));
for i := 0 to High(data.WeaponData.WeaponList) do
begin
while (HAS_WEAPON_ASSET_LOADED(data.WeaponData.WeaponList[i].HashCode) = BOOL(0)) do
begin
REQUEST_WEAPON_ASSET(data.WeaponData.WeaponList[i].HashCode, 31, 0);
GameScreen.DrawLoadingScreen;
ScriptHookVWait(0);
end;
GIVE_WEAPON_TO_PED(actor, data.WeaponData.WeaponList[i].HashCode, data.WeaponData.WeaponList[i].Ammo, BOOL(0), BOOL(0));
SET_PED_WEAPON_TINT_INDEX(actor, data.WeaponData.WeaponList[i].HashCode, data.WeaponData.WeaponList[i].TintIndex);
for j := 0 to High(data.WeaponData.WeaponList[i].Components) do
GIVE_WEAPON_COMPONENT_TO_PED(actor, data.WeaponData.WeaponList[i].HashCode, data.WeaponData.WeaponList[i].Components[j]);
REMOVE_WEAPON_ASSET(data.WeaponData.WeaponList[i].HashCode);
end;
// Personal vehicle
if data.IsDriving then
begin
// We spawn a vehicle at the center of the gameworld, and put the player into it, then put the vehicle at the proper location
ZeroMemory(@vdata, sizeof(TVehiclePersistenceData));
CopyMemory(@vdata.VehicleData, @data.VehicleData, sizeof(TStoredVehicleData));
veh := RestoreVehicle(vdata);
SET_PED_INTO_VEHICLE(actor, veh, -1);
SET_ENTITY_COORDS_NO_OFFSET(veh, data.X, data.Y, data.Z, BOOL(0), BOOL(0), BOOL(0));
SET_ENTITY_HEADING(veh, data.A);
SET_VEHICLE_AS_NO_LONGER_NEEDED(@veh);
end;
end;
procedure TMissionScript.LoadPlayerData(var data: TStoredPlayerData; stream: TStream; savMajorVer, savMinorVer: integer);
var
u32: UINT32;
i, j: integer;
begin
// Basic data
u32 := stream.ReadDWord;
data.X := pcfloat(@u32)^;
u32 := stream.ReadDWord;
data.Y := pcfloat(@u32)^;
u32 := stream.ReadDWord;
data.Z := pcfloat(@u32)^;
u32 := stream.ReadDWord;
data.A := pcfloat(@u32)^;
data.Health := stream.ReadDWord;
data.MaxHealth := stream.ReadDWord;
data.Armor := stream.ReadDWord;
// Model and outfit
data.Model := stream.ReadDWord;
for i := 0 to 11 do