forked from Kromster80/kam_remake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KM_CommonClasses.pas
750 lines (615 loc) · 20.7 KB
/
KM_CommonClasses.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
unit KM_CommonClasses;
{$I KaM_Remake.inc}
interface
uses
Classes, Math, SysUtils, KM_Points;
type
{ Extended with custom Read/Write commands which accept various types without asking for their length}
TKMemoryStream = class(TMemoryStream)
public
{$DEFINE PERMIT_ANSI_STRING}
{$IFDEF PERMIT_ANSI_STRING}
//Legacy format for campaigns info, maxlength 65k ansichars
procedure ReadA(out Value: AnsiString); reintroduce; overload;
procedure WriteA(const Value: AnsiString); reintroduce; overload;
//Assert for savegame sections
procedure ReadAssert(const Value: AnsiString);
//Ansistrings saved by PascalScript into savegame
procedure ReadHugeString(out Value: AnsiString);
procedure WriteHugeString(const Value: AnsiString);
{$ENDIF}
//Replacement of ReadAnsi for legacy use (campaign Ids (short names) in CMP)
procedure ReadBytes(out Value: TBytes);
procedure WriteBytes(const Value: TBytes);
//Unicode strings
procedure ReadW(out Value: UnicodeString); reintroduce; overload;
procedure WriteW(const Value: UnicodeString); reintroduce; overload;
//ZLib's decompression streams don't work with the normal TStreams.CopyFrom since
//it uses ReadBuffer. This procedure will work when Source is a TDecompressionStream
procedure CopyFromDecompression(Source: TStream);
procedure Write(const Value:TKMPointDir ); reintroduce; overload;
function Write(const Value:TKMDirection): Longint; reintroduce; overload;
function Write(const Value:TKMPoint ): Longint; reintroduce; overload;
function Write(const Value:TKMPointW): Longint; reintroduce; overload;
function Write(const Value:TKMPointF): Longint; reintroduce; overload;
function Write(const Value:TKMRect ): Longint; reintroduce; overload;
function Write(const Value:Single ): Longint; reintroduce; overload;
function Write(const Value:Integer ): Longint; reintroduce; overload;
function Write(const Value:Cardinal ): Longint; reintroduce; overload;
function Write(const Value:Byte ): Longint; reintroduce; overload;
function Write(const Value:Boolean ): Longint; reintroduce; overload;
function Write(const Value:Word ): Longint; reintroduce; overload;
function Write(const Value:ShortInt ): Longint; reintroduce; overload;
function Write(const Value:TDateTime): Longint; reintroduce; overload;
procedure Read(out Value:TKMPointDir); reintroduce; overload;
function Read(out Value:TKMDirection): Longint; reintroduce; overload;
function Read(out Value:TKMPoint ): Longint; reintroduce; overload;
function Read(out Value:TKMPointW ): Longint; reintroduce; overload;
function Read(out Value:TKMPointF ): Longint; reintroduce; overload;
function Read(out Value:TKMRect ): Longint; reintroduce; overload;
function Read(out Value:Single ): Longint; reintroduce; overload;
function Read(out Value:Integer ): Longint; reintroduce; overload;
function Read(out Value:Cardinal ): Longint; reintroduce; overload;
function Read(out Value:Byte ): Longint; reintroduce; overload;
function Read(out Value:Boolean ): Longint; reintroduce; overload;
function Read(out Value:Word ): Longint; reintroduce; overload;
function Read(out Value:ShortInt ): Longint; reintroduce; overload;
function Read(out Value:TDateTime ): Longint; reintroduce; overload;
end;
TStreamEvent = procedure (aData: TKMemoryStream) of object;
TStreamIntEvent = procedure (aData: TKMemoryStream; aSenderIndex: ShortInt) of object;
//TKMList owns items and frees them when they are deleted from the list
TKMList = class(TList)
protected
//This one function is enough to free all deleted/cleared/rewritten objects
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
end;
TKMPointList = class
private
fCount: Integer;
fItems: array of TKMPoint; //0..Count-1
function GetPoint(aIndex: Integer): TKMPoint; inline;
procedure SetPoint(aIndex: Integer; const aValue: TKMPoint); inline; //1..Count
public
constructor Create;
property Count: Integer read fCount write fCount;
property Items[aIndex: Integer]: TKMPoint read GetPoint write SetPoint; default;
procedure Clear; virtual;
procedure Copy(aSrc: TKMPointList);
procedure Add(aLoc: TKMPoint);
function Remove(aLoc: TKMPoint): Integer; virtual;
procedure Delete(aIndex: Integer);
procedure Insert(ID: Integer; aLoc: TKMPoint);
function GetRandom(out Point: TKMPoint): Boolean;
function GetClosest(aLoc: TKMPoint; out Point: TKMPoint): Boolean;
function Contains(const aLoc: TKMPoint): Boolean;
function IndexOf(const aLoc: TKMPoint): Integer;
procedure Inverse;
procedure SparseToDense;
function GetBounds(out Bounds: TKMRect): Boolean;
procedure SaveToStream(SaveStream: TKMemoryStream); virtual;
procedure LoadFromStream(LoadStream: TKMemoryStream); virtual;
end;
TKMPointTagList = class(TKMPointList)
public
Tag, Tag2: array of Cardinal; //0..Count-1
procedure Clear; override;
procedure Add(aLoc: TKMPoint; aTag: Cardinal; aTag2: Cardinal = 0); reintroduce;
function IndexOf(const aLoc: TKMPoint; aTag: Cardinal; aTag2: Cardinal): Integer;
procedure SortByTag;
function Remove(aLoc: TKMPoint): Integer; override;
procedure SaveToStream(SaveStream: TKMemoryStream); override;
procedure LoadFromStream(LoadStream: TKMemoryStream); override;
end;
TKMPointDirList = class //Used for finding fishing places, fighting positions, etc.
private
fItems: array of TKMPointDir; //0..Count-1
fCount: Integer;
function GetItem(aIndex: Integer): TKMPointDir;
public
procedure Clear;
procedure Add(aLoc: TKMPointDir);
property Count: Integer read fCount;
property Items[aIndex: Integer]: TKMPointDir read GetItem; default;
function GetRandom(out Point: TKMPointDir):Boolean;
procedure LoadFromStream(LoadStream: TKMemoryStream); virtual;
procedure SaveToStream(SaveStream: TKMemoryStream); virtual;
end;
TKMPointDirTagList = class(TKMPointDirList)
public
Tag: array of Cardinal; //0..Count-1
procedure Add(aLoc: TKMPointDir; aTag: Cardinal); reintroduce;
procedure SortByTag;
procedure SaveToStream(SaveStream: TKMemoryStream); override;
procedure LoadFromStream(LoadStream: TKMemoryStream); override;
end;
//Custom Exception that includes a TKMPoint
ELocError = class(Exception)
Loc: TKMPoint;
constructor Create(const aMsg: UnicodeString; aLoc: TKMPoint);
end;
implementation
uses
KM_Utils;
{ ELocError }
constructor ELocError.Create(const aMsg: UnicodeString; aLoc: TKMPoint);
begin
inherited Create(aMsg);
Loc := aLoc;
end;
{ TKMList }
//We were notified that the item is deleted from the list
procedure TKMList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if (Action = lnDeleted) then
TObject(Ptr).Free;
end;
{ TKMemoryStream }
{$IFDEF PERMIT_ANSI_STRING}
procedure TKMemoryStream.ReadA(out Value: AnsiString);
var I: Word;
begin
Read(I, SizeOf(I));
SetLength(Value, I);
if I > 0 then
Read(Pointer(Value)^, I);
end;
procedure TKMemoryStream.WriteA(const Value: AnsiString);
var I: Word;
begin
I := Length(Value);
inherited Write(I, SizeOf(I));
if I = 0 then Exit;
inherited Write(Pointer(Value)^, I);
end;
procedure TKMemoryStream.ReadHugeString(out Value: AnsiString);
var I: Cardinal;
begin
Read(I, SizeOf(I));
SetLength(Value, I);
if I > 0 then
Read(Pointer(Value)^, I);
end;
procedure TKMemoryStream.WriteHugeString(const Value: AnsiString);
var I: Cardinal;
begin
I := Length(Value);
inherited Write(I, SizeOf(I));
if I = 0 then Exit;
inherited Write(Pointer(Value)^, I);
end;
procedure TKMemoryStream.ReadAssert(const Value: AnsiString);
var S: AnsiString;
begin
ReadA(s);
Assert(s = Value, 'TKMemoryStream.Read <> Value: '+Value);
end;
{$ENDIF}
procedure TKMemoryStream.WriteW(const Value: UnicodeString);
var I: Word;
begin
I := Length(Value);
inherited Write(I, SizeOf(I));
if I = 0 then Exit;
inherited Write(Pointer(Value)^, I * SizeOf(WideChar));
end;
procedure TKMemoryStream.ReadBytes(out Value: TBytes);
var
I: Word;
begin
Read(I, SizeOf(I));
SetLength(Value, I);
if I > 0 then
Read(Pointer(Value)^, I);
end;
procedure TKMemoryStream.WriteBytes(const Value: TBytes);
var
I: Word;
begin
I := Length(Value);
inherited Write(I, SizeOf(I));
if I = 0 then Exit;
inherited Write(Pointer(Value)^, I);
end;
procedure TKMemoryStream.Write(const Value: TKMPointDir);
begin
Write(Value.Loc);
inherited Write(Value.Dir, SizeOf(Value.Dir));
end;
function TKMemoryStream.Write(const Value: TKMDirection): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:TKMPoint): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:TKMPointW): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:TKMPointF): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:TKMRect): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:single): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:integer): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:cardinal): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:byte): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:boolean): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:word): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:shortint): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
function TKMemoryStream.Write(const Value:TDateTime): Longint;
begin Result := inherited Write(Value, SizeOf(Value)); end;
procedure TKMemoryStream.ReadW(out Value: UnicodeString);
var I: Word;
begin
Read(I, SizeOf(I));
SetLength(Value, I);
if I > 0 then
Read(Pointer(Value)^, I * SizeOf(WideChar));
end;
procedure TKMemoryStream.Read(out Value: TKMPointDir);
begin
Read(Value.Loc);
Read(Value.Dir, SizeOf(Value.Dir));
end;
function TKMemoryStream.Read(out Value:TKMDirection): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:TKMPoint): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:TKMPointW): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:TKMPointF): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:TKMRect): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:single): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:integer): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:cardinal): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:byte): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:boolean): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:word): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:shortint): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
function TKMemoryStream.Read(out Value:TDateTime): Longint;
begin Result := inherited Read(Value, SizeOf(Value)); end;
procedure TKMemoryStream.CopyFromDecompression(Source: TStream);
const
MaxBufSize = $F000;
var
Count: Integer;
Buffer: PByte;
begin
Source.Position := 0;
GetMem(Buffer, MaxBufSize);
try
Count := Source.Read(Buffer^, MaxBufSize);
while Count > 0 do
begin
WriteBuffer(Buffer^, Count);
Count := Source.Read(Buffer^, MaxBufSize);
end;
finally
FreeMem(Buffer, MaxBufSize);
end;
end;
{ TKMPointList }
constructor TKMPointList.Create;
begin
inherited;
end;
procedure TKMPointList.Clear;
begin
fCount := 0;
end;
procedure TKMPointList.Add(aLoc: TKMPoint);
begin
if fCount >= Length(fItems) then
SetLength(fItems, fCount + 32);
fItems[fCount] := aLoc;
Inc(fCount);
end;
//Remove point from the list if is there. Return index of removed entry or -1 on failure
function TKMPointList.Remove(aLoc: TKMPoint): Integer;
var
I: Integer;
begin
Result := -1;
//Scan whole list to detect duplicate entries
for I := 0 to fCount - 1 do
if KMSamePoint(fItems[I], aLoc) then
Result := I;
//Remove found entry
if (Result <> -1) then
Delete(Result);
end;
procedure TKMPointList.Delete(aIndex:Integer);
begin
if not InRange(aIndex, 0, Count-1) then Exit;
if (aIndex <> fCount - 1) then
Move(fItems[aIndex+1], fItems[aIndex], SizeOf(fItems[aIndex]) * (fCount - 1 - aIndex));
Dec(fCount);
end;
//Insert an entry and check if list is still walkable
//Walkable means that every point is next to neighbour points }
procedure TKMPointList.Insert(ID: Integer; aLoc: TKMPoint);
begin
Assert(InRange(ID, 0, fCount));
//Grow the list
if fCount >= Length(fItems) then
SetLength(fItems, fCount + 32);
//Shift items towards end
if fCount <> 0 then
Move(fItems[ID], fItems[ID+1], SizeOf(fItems[ID]) * (fCount - ID));
fItems[ID] := aLoc;
Inc(fCount);
end;
function TKMPointList.GetRandom(out Point: TKMPoint): Boolean;
begin
Result := fCount <> 0;
if Result then
Point := fItems[KaMRandom(fCount)];
end;
function TKMPointList.GetClosest(aLoc: TKMPoint; out Point: TKMPoint): Boolean;
var
I: Integer;
begin
Result := fCount <> 0;
if Result then
begin
Point := fItems[0];
for I := 1 to fCount - 1 do
if KMLengthSqr(fItems[I], aLoc) < KMLengthSqr(Point, aLoc) then
Point := fItems[I];
end;
end;
function TKMPointList.Contains(const aLoc: TKMPoint): Boolean;
begin
Result := IndexOf(aLoc) <> -1;
end;
function TKMPointList.IndexOf(const aLoc: TKMPoint): Integer;
var
I: Integer;
begin
Result := -1;
for I := fCount - 1 downto 0 do
if KMSamePoint(aLoc, fItems[I]) then
begin
Result := I;
Break;
end;
end;
procedure TKMPointList.Copy(aSrc: TKMPointList);
begin
fCount := aSrc.Count;
SetLength(fItems, fCount);
Move(aSrc.fItems[0], fItems[0], SizeOf(fItems[0]) * fCount);
end;
function TKMPointList.GetPoint(aIndex: Integer): TKMPoint;
begin
Result := fItems[aIndex];
end;
procedure TKMPointList.SetPoint(aIndex: Integer; const aValue: TKMPoint);
begin
fItems[aIndex] := aValue;
end;
//Reverse the list
procedure TKMPointList.Inverse;
var
I: Integer;
begin
for I := 0 to fCount div 2 - 1 do
KMSwapPoints(fItems[I], fItems[fCount-1-I]);
end;
//Used in JPS pathfinding
//Take the sparse walk route with nodes in corners (A------B)
//and add all the missing nodes inbetween like so: (A123456B)
procedure TKMPointList.SparseToDense;
var
I,K,J: Integer;
Tmp: array of TKMPoint;
Span: Word;
C,N: ^TKMPoint;
begin
K := 0;
SetLength(Tmp, 8192);
for I := 0 to fCount - 1 do
begin
Tmp[K] := fItems[I];
Inc(K);
if (I <> fCount - 1) then
begin
C := @fItems[I];
N := @fItems[I+1];
Span := Max(Abs(N.X - C.X), Abs(N.Y - C.Y));
for J := 1 to Span - 1 do
begin
Tmp[K].X := C.X + Round((N.X - C.X) / Span * J);
Tmp[K].Y := C.Y + Round((N.Y - C.Y) / Span * J);
Inc(K);
end;
end;
end;
fCount := K;
SetLength(fItems, fCount);
Move(Tmp[0], fItems[0], SizeOf(fItems[0]) * fCount);
end;
function TKMPointList.GetBounds(out Bounds: TKMRect): Boolean;
var I: Integer;
begin
Result := fCount <> 0;
if Result then
begin
//Something to start with
Bounds.Left := fItems[0].X;
Bounds.Top := fItems[0].Y;
Bounds.Right := fItems[0].X;
Bounds.Bottom := fItems[0].Y;
for I := 1 to fCount - 1 do
begin
if fItems[I].X < Bounds.Left then Bounds.Left := fItems[I].X;
if fItems[I].Y < Bounds.Top then Bounds.Top := fItems[I].Y;
if fItems[I].X > Bounds.Right then Bounds.Right := fItems[I].X;
if fItems[I].Y > Bounds.Bottom then Bounds.Bottom := fItems[I].Y;
end;
end;
end;
procedure TKMPointList.SaveToStream(SaveStream: TKMemoryStream);
begin
SaveStream.Write(fCount);
if fCount > 0 then
SaveStream.Write(fItems[0], SizeOf(fItems[0]) * fCount);
end;
procedure TKMPointList.LoadFromStream(LoadStream: TKMemoryStream);
begin
LoadStream.Read(fCount);
SetLength(fItems, fCount);
if fCount > 0 then
LoadStream.Read(fItems[0], SizeOf(fItems[0]) * fCount);
end;
{ TKMPointTagList }
procedure TKMPointTagList.Clear;
begin
inherited;
end;
procedure TKMPointTagList.Add(aLoc: TKMPoint; aTag: Cardinal; aTag2: Cardinal = 0);
begin
inherited Add(aLoc);
if fCount >= Length(Tag) then SetLength(Tag, fCount + 32); //Expand the list
if fCount >= Length(Tag2) then SetLength(Tag2, fCount + 32); //+32 is just a way to avoid further expansions
Tag[fCount-1] := aTag;
Tag2[fCount-1] := aTag2;
end;
function TKMPointTagList.IndexOf(const aLoc: TKMPoint; aTag, aTag2: Cardinal): Integer;
var
I: Integer;
begin
Result := -1;
for I := fCount - 1 downto 0 do
if KMSamePoint(aLoc, fItems[I]) and (aTag = Tag[I]) and (aTag2 = Tag2[I]) then
begin
Result := I;
Break;
end;
end;
function TKMPointTagList.Remove(aLoc: TKMPoint): Integer;
begin
Result := inherited Remove(aLoc);
//Note that fCount is already decreased by 1
if (Result <> -1) and (Result <> fCount) then
begin
Move(Tag[Result+1], Tag[Result], SizeOf(Tag[Result]) * (fCount - Result));
Move(Tag2[Result+1], Tag2[Result], SizeOf(Tag2[Result]) * (fCount - Result));
end;
end;
procedure TKMPointTagList.SaveToStream(SaveStream: TKMemoryStream);
begin
inherited; //Writes Count
if fCount > 0 then
begin
SaveStream.Write(Tag[0], SizeOf(Tag[0]) * fCount);
SaveStream.Write(Tag2[0], SizeOf(Tag2[0]) * fCount);
end;
end;
procedure TKMPointTagList.SortByTag;
var I,K: Integer;
begin
for I := 0 to fCount - 1 do
for K := I + 1 to fCount - 1 do
if Tag[K] < Tag[I] then
begin
KMSwapPoints(fItems[I], fItems[K]);
KMSwapInt(Tag[I], Tag[K]);
KMSwapInt(Tag2[I], Tag2[K]);
end;
end;
procedure TKMPointTagList.LoadFromStream(LoadStream: TKMemoryStream);
begin
inherited; //Reads Count
SetLength(Tag, fCount);
SetLength(Tag2, fCount);
if fCount > 0 then
begin
LoadStream.Read(Tag[0], SizeOf(Tag[0]) * fCount);
LoadStream.Read(Tag2[0], SizeOf(Tag2[0]) * fCount);
end;
end;
{ TKMPointList }
procedure TKMPointDirList.Clear;
begin
fCount := 0;
end;
procedure TKMPointDirList.Add(aLoc: TKMPointDir);
begin
if fCount >= Length(fItems) then
SetLength(fItems, fCount + 32);
fItems[fCount] := aLoc;
inc(fCount);
end;
function TKMPointDirList.GetItem(aIndex: Integer): TKMPointDir;
begin
Assert(InRange(aIndex, 0, fCount - 1));
Result := fItems[aIndex];
end;
function TKMPointDirList.GetRandom(out Point: TKMPointDir):Boolean;
begin
Result := False;
if fCount > 0 then
begin
Point := fItems[KaMRandom(fCount)];
Result := True;
end;
end;
procedure TKMPointDirList.SaveToStream(SaveStream: TKMemoryStream);
begin
SaveStream.Write(fCount);
if fCount > 0 then
SaveStream.Write(fItems[0], SizeOf(fItems[0]) * fCount);
end;
procedure TKMPointDirList.LoadFromStream(LoadStream: TKMemoryStream);
begin
LoadStream.Read(fCount);
SetLength(fItems, fCount);
if fCount > 0 then
LoadStream.Read(fItems[0], SizeOf(fItems[0]) * fCount);
end;
procedure TKMPointDirTagList.Add(aLoc: TKMPointDir; aTag: Cardinal);
begin
inherited Add(aLoc);
if fCount >= Length(Tag) then SetLength(Tag, fCount + 32); //Expand the list
Tag[fCount-1] := aTag;
end;
procedure TKMPointDirTagList.SortByTag;
var
I, K: Integer;
begin
for I := 0 to fCount - 1 do
for K := I + 1 to fCount - 1 do
if Tag[K] < Tag[I] then
begin
KMSwapPointDir(fItems[I], fItems[K]);
KMSwapInt(Tag[I], Tag[K]);
end;
end;
procedure TKMPointDirTagList.SaveToStream(SaveStream: TKMemoryStream);
begin
inherited; //Writes Count
if fCount > 0 then
SaveStream.Write(Tag[0], SizeOf(Tag[0]) * fCount);
end;
procedure TKMPointDirTagList.LoadFromStream(LoadStream: TKMemoryStream);
begin
inherited; //Reads Count
SetLength(Tag, fCount);
if fCount > 0 then
LoadStream.Read(Tag[0], SizeOf(Tag[0]) * fCount);
end;
end.