This repository was archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathterrain.pas
1029 lines (832 loc) · 22.1 KB
/
terrain.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
{ This file is a part of Map editor for VCMI project
Copyright (C) 2013-2017 Alexander Shishkin [email protected]
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit terrain;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, contnrs,
fgl,
editor_types, editor_graphics, vcmi_json,
filesystem_base, editor_classes, transitions, editor_gl, LazLoggerBase;
const
RULE_DIRT = 'D';
RULE_SAND = 'S';
RULE_TRANSITION = 'T';
RULE_NATIVE = 'N';
RULE_NATIVE_STRONG = 'N!';
RULE_ANY = '?';
type
TFlipMode = (sameImage,diffImages);
TTerrainGroup = (
NORMAL,
DIRT,
SAND,
WATER,
ROCK);
const
TERRAIN_GROUPS: array [TTerrainType] of TTerrainGroup = (
TTerrainGroup.DIRT,
TTerrainGroup.SAND,
TTerrainGroup.NORMAL,
TTerrainGroup.NORMAL,
TTerrainGroup.NORMAL,
TTerrainGroup.NORMAL,
TTerrainGroup.NORMAL,
TTerrainGroup.NORMAL,
TTerrainGroup.WATER,
TTerrainGroup.ROCK);
type
{ TWeightedRule }
TWeightedRule = class
name : string;
points: Integer;
constructor Create();
destructor Destroy; override;
function IsStandartRule: boolean;
function IsDirt: boolean; inline;
function IsSand: boolean; inline;
function IsTrans: boolean; inline;
function IsAny: boolean; inline;
function IsNative: boolean; inline;
function Clone: TWeightedRule;
end;
//todo: use object or record
TRules = specialize TFPGObjectList<TWeightedRule>;
TRulesArray = array[0..8] of TRules ;
{$push}
{$m+}
{ TMappingTemplate }
TMappingTemplate = class (TNamedCollectionItem)
private
FValue: string;
procedure SetValue(AValue: string);
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
published
property Value: string read FValue write SetValue;
end;
{ TMappingTemplates }
TMappingTemplates = class(specialize TGNamedCollection<TMappingTemplate>)
public
constructor Create;
end;
{ TPatternTemplate }
TPatternTemplate = class (TCollectionItem)
private
FId: string;
FMapping: TMappingTemplates;
FMinPoints: integer;
FData: TStringList;
function GetData: TStrings;
procedure SetId(AValue: string);
procedure SetMinPoints(AValue: integer);
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
published
property Id:string read FId write SetId;
property MinPoints: integer read FMinPoints write SetMinPoints;
property Mapping: TMappingTemplates read FMapping;
property Data: TStrings read GetData;
end;
{ TPatternTemplates }
TPatternTemplates = class(specialize TGArrayCollection<TPatternTemplate>)
public
constructor Create;
end;
{ TPattern }
TPattern = class (TPersistent)
strict private
FData: TRulesArray;
FID: string;
FMinPoints: integer;
FDiffImages: Boolean;
FMappings: TMappings;
FMaxPoints: integer;
FRotationTypesCount: Integer;
function GetRData(idx: Integer): TRules;
procedure SetID(AValue: string);
procedure SetMinPoints(AValue: integer);
procedure SetDiffImages(AValue: Boolean);
procedure SetMaxPoints(AValue: integer);
procedure SetRotationTypesCount(AValue: Integer);
procedure FillFrom(ATemplate: TPatternTemplate);
private
FFlipped: array[1..3] of TPattern;
protected
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create();
constructor Create(ATemplate: TPatternTemplate);
destructor Destroy; override;
procedure SwapRules(idx1,idx2: Integer);
property RData[idx:Integer]: TRules read GetRData;
property Id:string read FID write SetID;
property MinPoints: integer read FMinPoints write SetMinPoints;
property MaxPoints: integer read FMaxPoints write SetMaxPoints;
property DiffImages: Boolean read FDiffImages write SetDiffImages;
property RotationTypesCount: Integer read FRotationTypesCount write SetRotationTypesCount;
property Mappings:TMappings read FMappings;
end;
TPatternsVector = specialize TFPGObjectList<TPattern>;
TTerrainViewMap = specialize TFPGMap<TTerrainGroup, TPatternsVector>;
TTerrainTypeMap = specialize TFPGMap<string, TPattern>;
{ TTerrainPatternConfig }
TTerrainPatternConfig = class
private
FTerrainType: TPatternTemplates;
FTerrainView: TPatternTemplates;
FViewMap: TTerrainViewMap;
FTypeMap: TTerrainTypeMap;
FFreeList: TFPObjectList;
procedure FlipPattern(APattern: TPattern; flip:Integer);
procedure PreparePattern(APattern: TPattern);
procedure ConvertTerrainTypes;
procedure ConvertTerrainViews;
public
constructor Create;
destructor Destroy; override;
procedure ConvertConfig;
function GetFlippedPattern(const APattern: TPattern; flip:Integer ): TPattern;
function GetTerrainViewPatternsForGroup(AGroup: TTerrainGroup): TPatternsVector;
//may return nil
function GetTerrainViewPatternById(AGroup: TTerrainGroup; AId:string): TPattern;
//will raise if not found
function GetTerrainTypePatternById(AId:string): TPattern;
function GetTerrainGroup(AGroup:string): TTerrainGroup;
published
property TerrainView:TPatternTemplates read FTerrainView;
property TerrainType:TPatternTemplates read FTerrainType;
end;
//todo: use terrain configuration
{ TTerrainConfig }
TTerrainConfigItem = class(TNamedCollectionItem)
published
end;
{ TTerriainConfig }
TTerriainConfig = class(specialize TGNamedCollection<TTerrainConfigItem>)
public
constructor Create;
end;
{$pop}
{ TTerrainManager }
TTerrainManager = class (TGraphicsConsumer)
private
FTerrainDefs: array [TTerrainType] of TAnimation;
FRiverDefs: array [TRiverType.clearRiver..TRiverType.lavaRiver] of TAnimation;
FRoadDefs: array [TRoadType.dirtRoad..TRoadType.cobblestoneRoad] of TAnimation;
FPatternConfig: TTerrainPatternConfig;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure LoadConfig;
procedure LoadTerrainGraphics;
procedure Render(AState: TLocalState; const tt: TTerrainType; sbt: UInt8; Flags: UInt8);
procedure RenderRiver(AState: TLocalState; const rt: TRiverType; const Dir: UInt8; Flags: UInt8);
procedure RenderRoad(AState: TLocalState; const rdt: TRoadType; const Dir: UInt8; Flags: UInt8);
function GetDefaultTerrain(const Level: Integer): TTerrainType;
function GetRandomNormalSubtype(const tt: TTerrainType): UInt8;
property PatternConfig: TTerrainPatternConfig read FPatternConfig;
end;
implementation
uses
RegExpr, editor_consts;
type
TTerrainViewInterval = record
min, max: uint8;
end;
const
TERRAIN_DEF_FILES: array[TTerrainType] of string = (
'DIRTTL',
'SANDTL',
'GRASTL',
'SNOWTL',
'SWMPTL',
'ROUGTL',
'SUBBTL',
'LAVATL',
'WATRTL',
'ROCKTL'
);
RIVER_DEF_FILES: array[TRiverType.clearRiver..TRiverType.lavaRiver] of string =
(
'CLRRVR','ICYRVR','MUDRVR','LAVRVR'
);
ROAD_DEF_FILES: array[TRoadType.dirtRoad..TRoadType.cobblestoneRoad] of string =
(
'DIRTRD','GRAVRD','COBBRD'
);
TERRAIN_PATTERNS_CONFIG_FILE = 'config/terrainViewPatterns.json';
TERRAIN_CONFIG_FILE = 'config/terrains.json';
procedure SetView(out V: TTerrainViewInterval; min,max: uint8);
begin
v.max:=max;
v.min:=min;
end;
procedure RexpSplit (rexp: TRegExpr; const AInputStr : RegExprString; APieces : TStrings);
var
PrevPos : PtrInt;
s : string;
begin
PrevPos := 1;
if rexp.Exec (AInputStr) then
REPEAT
s := Copy (AInputStr, PrevPos, rexp.MatchPos [0] - PrevPos);
APieces.Add (Trim(s));
PrevPos := rexp.MatchPos [0] + rexp.MatchLen [0];
UNTIL not rexp.ExecNext;
if PrevPos <= Length(AInputStr) then
begin
s := Copy (AInputStr, PrevPos, MaxInt);
APieces.Add(Trim(s));
end;
end;
procedure RexpSplit (Expr: string; const AInputStr : RegExprString; APieces : TStrings);
var
o: TRegExpr;
begin
o := TRegExpr.Create;
try
o.Expression:=Expr;
RexpSplit(o, AInputStr, APieces);
finally
o.Free;
end;
end;
procedure RexpSplit2(rexp: TRegExpr; const AInputStr : RegExprString; out AFirst: string; out ASecond: String);
var
sl: TStringList;
begin
AFirst:='';
ASecond:='';
sl := TStringList.Create;
try
RexpSplit(rexp, AInputStr, sl);
if sl.Count>0 then
begin
AFirst:=sl[0];
end;
if sl.Count>1 then
begin
ASecond:=sl[1];
end;
if sl.Count>2 then
begin
assert(false);
end;
finally
sl.Free;
end;
end;
procedure RexpSplit2(rexp: String; const AInputStr : RegExprString; out AFirst: string; out ASecond: String);
var
o: TRegExpr;
begin
o := TRegExpr.Create;
try
o.Expression:=rexp;
RexpSplit2(o, AInputStr, AFirst, ASecond);
finally
o.Free;
end;
end;
{ TTerriainConfig }
constructor TTerriainConfig.Create;
begin
inherited;
end;
{ TMappingTemplates }
constructor TMappingTemplates.Create;
begin
inherited Create;
end;
{ TMappingTemplate }
procedure TMappingTemplate.SetValue(AValue: string);
begin
if FValue=AValue then Exit;
FValue:=AValue;
end;
constructor TMappingTemplate.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
end;
destructor TMappingTemplate.Destroy;
begin
inherited Destroy;
end;
{ TPatternTemplates }
constructor TPatternTemplates.Create;
begin
inherited Create;
end;
{ TPatternTemplate }
function TPatternTemplate.GetData: TStrings;
begin
Result := FData;
end;
procedure TPatternTemplate.SetId(AValue: string);
begin
if FId=AValue then Exit;
FId:=AValue;
end;
procedure TPatternTemplate.SetMinPoints(AValue: integer);
begin
if FMinPoints=AValue then Exit;
FMinPoints:=AValue;
end;
constructor TPatternTemplate.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
FData := TStringList.Create;
FMapping := TMappingTemplates.Create;
end;
destructor TPatternTemplate.Destroy;
begin
FMapping.Free;
FData.Free;
inherited Destroy;
end;
{ TWeightedRule }
function TWeightedRule.Clone: TWeightedRule;
begin
Result := TWeightedRule.Create();
Result.name := name;
Result.points := points;
end;
constructor TWeightedRule.Create;
begin
points := 0;
name := '';
end;
destructor TWeightedRule.Destroy;
begin
inherited Destroy;
end;
function TWeightedRule.IsAny: boolean;
begin
Result := name = RULE_ANY;
end;
function TWeightedRule.IsNative: boolean;
begin
Result := name = RULE_NATIVE;
end;
function TWeightedRule.IsDirt: boolean;
begin
Result := name = RULE_DIRT;
end;
function TWeightedRule.IsSand: boolean;
begin
Result := name = RULE_SAND;
end;
function TWeightedRule.IsStandartRule: boolean;
begin
Result := (name = RULE_ANY)
or (name = RULE_DIRT)
or (name = RULE_NATIVE)
or (name = RULE_SAND)
or (name = RULE_TRANSITION)
or (name = RULE_NATIVE_STRONG);
end;
function TWeightedRule.IsTrans: boolean;
begin
Result := name = RULE_TRANSITION;
end;
{ TTerrainPatternConfig }
procedure TTerrainPatternConfig.ConvertConfig;
begin
ConvertTerrainTypes();
ConvertTerrainViews();
end;
procedure TTerrainPatternConfig.FlipPattern(APattern: TPattern; flip: Integer);
var
i: Integer;
y: Integer;
begin
Assert(flip > 0);
Assert(flip <= 3);
if flip in [FLIP_PATTERN_HORIZONTAL, FLIP_PATTERN_BOTH] then
for i := 0 to 3 - 1 do
begin
y := i*3;
APattern.SwapRules(Y+2,Y);
end;
if flip in [FLIP_PATTERN_VERTICAL, FLIP_PATTERN_BOTH] then
begin
for i := 0 to 3 - 1 do
begin
APattern.SwapRules(i,i+6);
end;
end;
end;
procedure TTerrainPatternConfig.PreparePattern(APattern: TPattern);
var
flipped:TPattern;
flip: Integer;
begin
for flip := 1 to 4 - 1 do
begin
flipped := TPattern.Create();
APattern.AssignTo(flipped);
FlipPattern(flipped,flip);
APattern.FFlipped[flip] := flipped;
end;
end;
procedure TTerrainPatternConfig.ConvertTerrainTypes;
var
pattern: TPattern;
i: Integer;
template: TPatternTemplate;
begin
for i := 0 to FTerrainType.Count - 1 do
begin
template := FTerrainType[i];
pattern := TPattern.Create(template);
PreparePattern(pattern);
FTypeMap.Add(pattern.Id,pattern);
FFreeList.Add(pattern);
end;
end;
procedure TTerrainPatternConfig.ConvertTerrainViews;
var
pattern: TPattern;
i: Integer;
template: TPatternTemplate;
j: Integer;
MappingString: String;
FlipModeStr, s2: string;
MappingsList: TStringList;
k: Integer;
Lower: string;
Upper: String;
m: TMapping;
begin
MappingsList := TStringList.Create;
try
for i := 0 to FTerrainView.Count - 1 do
begin
template := FTerrainView[i];
for j := 0 to template.Mapping.Count - 1 do
begin
pattern := TPattern.Create(template);
MappingString := template.Mapping[j].Value;
RexpSplit2('\s*:\s*',MappingString, FlipModeStr, s2);
if s2<>'' then
begin
//we have flip mode
MappingString := s2;
pattern.DiffImages:= ('D' = Copy(FlipModeStr, Length(FlipModeStr), 1));
if pattern.DiffImages then
begin
pattern.RotationTypesCount:=StrToInt(Copy(FlipModeStr,1, Length(FlipModeStr)-1));
Assert((pattern.RotationTypesCount = 2) or (pattern.RotationTypesCount = 4));
end;
end;
MappingsList.Clear;
RexpSplit('\s*,\s*', MappingString, MappingsList);
for k := 0 to MappingsList.Count - 1 do
begin
RexpSplit2('\s*-\s*', MappingsList[k], Lower, Upper);
m.Lower:=StrToInt(Lower);
if Upper = '' then
begin
m.Upper:=m.Lower;
end
else begin
m.Upper:=StrToInt(Upper);
end;
pattern.Mappings.PushBack(m);
end;
PreparePattern(pattern);
FViewMap.KeyData[GetTerrainGroup(template.Mapping[j].Identifier)].Add(pattern);
//DebugLn(['Loaded pattern ', pattern.Id]);
end;
end;
finally
MappingsList.Free;
end;
end;
constructor TTerrainPatternConfig.Create;
var
tt: TTerrainGroup;
v: TPatternsVector;
begin
FFreeList := TFPObjectList.Create(true);
FTerrainType := TPatternTemplates.Create;
FTerrainView := TPatternTemplates.Create;
FViewMap := TTerrainViewMap.Create;
FTypeMap := TTerrainTypeMap.Create;
for tt in TTerrainGroup do
begin
v:=TPatternsVector.Create(true);
FViewMap.Add(tt,v);
FFreeList.Add(v);
end;
end;
destructor TTerrainPatternConfig.Destroy;
begin
FTypeMap.Free;
FViewMap.Free;
FTerrainView.Free;
FTerrainType.Free;
FFreeList.Free;
inherited Destroy;
end;
function TTerrainPatternConfig.GetFlippedPattern(const APattern: TPattern;
flip: Integer): TPattern;
begin
if flip = 0 then
begin
Exit(APattern);
end
else begin
Assert(flip > 0);
Assert(flip < 4);
Exit(APattern.FFlipped[flip]);
end;
end;
function TTerrainPatternConfig.GetTerrainViewPatternsForGroup(
AGroup: TTerrainGroup): TPatternsVector;
begin
Result := FViewMap.KeyData[AGroup];
end;
function TTerrainPatternConfig.GetTerrainViewPatternById(AGroup: TTerrainGroup;
AId: string): TPattern;
var
item : TPattern;
begin
for item in GetTerrainViewPatternsForGroup(AGroup) do
begin
if item.Id = AID then
Exit(item);
end;
Exit(nil);
end;
function TTerrainPatternConfig.GetTerrainTypePatternById(AId: string): TPattern;
begin
Result := FTypeMap.KeyData[AID];
end;
function TTerrainPatternConfig.GetTerrainGroup(AGroup: string): TTerrainGroup;
begin
case AGroup of
'normal': Result := TTerrainGroup.NORMAL;
'dirt': Result := TTerrainGroup.DIRT;
'sand': Result := TTerrainGroup.SAND;
'water': Result := TTerrainGroup.WATER;
'rock': Result := TTerrainGroup.ROCK;
else
raise Exception.Create('Unknown terrain typename '+AGroup);
end;
end;
{ TPattern }
procedure TPattern.AssignTo(Dest: TPersistent);
var
dest_o: TPattern;
tmp: TWeightedRule;
i: Integer;
j: Integer;
begin
if Dest is TPattern then
begin
dest_o := TPattern(Dest);
dest_o.ID := ID;
dest_o.MinPoints := MinPoints;
dest_o.MaxPoints := MaxPoints;
dest_o.FDiffImages:=FDiffImages;
dest_o.FRotationTypesCount:=FRotationTypesCount;
for i := 0 to 9 - 1 do
begin
for j := 0 to RData[i].Count - 1 do
begin
tmp := RData[i].Items[j].Clone();
dest_o.RData[i].Add(tmp);
end;
end;
for i := 0 to SizeInt(FMappings.Size) - 1 do
begin
dest_o.FMappings.PushBack(FMappings[i]);
end;
end
else begin
inherited AssignTo(Dest);
end;
end;
constructor TPattern.Create();
var
i: Integer;
begin
for i := Low(FData) to High(FData) do
begin
FData[i] := TRules.Create(True);
end;
FMinPoints:=0;
FMaxPoints:=MaxInt;
FDiffImages:=false;
FMappings := TMappings.Create;
end;
constructor TPattern.Create(ATemplate: TPatternTemplate);
begin
Create();
FillFrom(ATemplate);
end;
destructor TPattern.Destroy;
var
i: Integer;
begin
FMappings.Free;
for i := Low(FData) to High(FData) do
begin
FData[i].Free;
end;
for i := Low(FFlipped) to High(FFlipped) do
begin
FFlipped[i].Free;
end;
inherited Destroy;
end;
function TPattern.GetRData(idx: Integer): TRules;
begin
Result := FData[idx];
end;
procedure TPattern.SetDiffImages(AValue: Boolean);
begin
if FDiffImages=AValue then Exit;
FDiffImages:=AValue;
end;
procedure TPattern.SetMaxPoints(AValue: integer);
begin
if FMaxPoints=AValue then Exit;
FMaxPoints:=AValue;
end;
procedure TPattern.SetRotationTypesCount(AValue: Integer);
begin
if FRotationTypesCount=AValue then Exit;
FRotationTypesCount:=AValue;
end;
procedure TPattern.FillFrom(ATemplate: TPatternTemplate);
var
rexp: TRegExpr;
i: Integer;
tmp:TStringList;
rule: TWeightedRule;
j: Integer;
p: SizeInt;
FStrData: TStrings;
begin
FID := ATemplate.Id;
FMinPoints:=ATemplate.MinPoints;
//todo: maxpoints
FStrData := ATemplate.Data;
if not FStrData.Count = 9 then raise Exception.Create('terrain config invalid');
tmp := TStringList.Create;
rexp := TRegExpr.Create;
try
rexp.Expression := '\s*,\s*';
for I := Low(FData) to High(FData) do
begin
tmp.Clear;
FData[i].Clear;
RexpSplit(rexp, FStrData[i],tmp);
for j := 0 to tmp.Count - 1 do
begin
rule := TWeightedRule.Create();
p := Pos('-',tmp[j]);
if p <> 0 then
begin
rule.name := Copy(tmp[j],1,p-1);
rule.points := StrToInt(Copy(tmp[j],p+1,MaxInt));
end
else
begin
rule.name := tmp[j];
end;
FData[i].Add(rule);
end;
end;
finally
rexp.Free;
tmp.Free;
end;
end;
procedure TPattern.SetID(AValue: string);
begin
if FID = AValue then Exit;
FID := AValue;
end;
procedure TPattern.SetMinPoints(AValue: integer);
begin
if FMinPoints = AValue then Exit;
FMinPoints := AValue;
end;
procedure TPattern.SwapRules(idx1, idx2: Integer);
var
tmp: TRules;
begin
tmp := FData[idx1];
FData[idx1] := FData[idx2];
FData[idx2] := tmp;
end;
{ TTerrainManager }
constructor TTerrainManager.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FPatternConfig := TTerrainPatternConfig.Create;
end;
destructor TTerrainManager.Destroy;
begin
FPatternConfig.Free;
inherited Destroy;
end;
function TTerrainManager.GetDefaultTerrain(const Level: Integer): TTerrainType;
begin
if Level <=0 then
begin
Result := TTerrainType.water;
end
else begin
Result := TTerrainType.rock;
end;
end;
function TTerrainManager.GetRandomNormalSubtype(const tt: TTerrainType): UInt8;
var
vews: TTerrainViewInterval;
begin
Result :=0;
case tt of
TTerrainType.dirt:SetView(vews,21,44);
TTerrainType.sand:SetView(vews,0,23);
TTerrainType.grass,
TTerrainType.snow,
TTerrainType.swamp,
TTerrainType.rough,
TTerrainType.subterra,
TTerrainType.lava:SetView(vews,49,63); //SetView(vews,49,72);
TTerrainType.water:SetView(vews,20,32);
TTerrainType.rock: SetView(vews,0,0);
else
raise Exception.Create('Unknown terrain: '+IntToStr(Ord(tt)));
end;
{ TODO : Handle decorative tiles }
Result := Random(vews.max-vews.min)+vews.min;
end;
procedure TTerrainManager.LoadConfig;
var
config: TJsonResource;
begin
config := TJsonResource.Create(TERRAIN_PATTERNS_CONFIG_FILE);
try
config.Load(ResourceLoader);
config.DestreamTo(FPatternConfig,'');
finally
config.Free;
end;
FPatternConfig.ConvertConfig;
end;
procedure TTerrainManager.LoadTerrainGraphics;
var
tt: TTerrainType;
rt: TRiverType;
rdt: TRoadType;
begin
for tt := Low(TTerrainType) to High(TTerrainType) do
begin
FTerrainDefs[tt] := GraphicsManager.Animations.GetGraphics(TERRAIN_DEF_FILES[tt]);
end;
for rt in [TRiverType.clearRiver..TRiverType.lavaRiver] do
begin
FRiverDefs[rt] := GraphicsManager.Animations.GetGraphics(RIVER_DEF_FILES[rt]);
end;