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 pathmap_format_h3m.pas
2544 lines (2134 loc) · 61.4 KB
/
map_format_h3m.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 map_format_h3m;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, math, fgl, typinfo, FileUtil, map, map_format, terrain,
stream_adapter, editor_types, object_options, editor_classes, lists_manager,
map_objects, editor_graphics, logical_id_condition,
logical_event_condition, logical_expression, position;
const
MAP_VERSION_ROE = $0e;
MAP_VERSION_AB = $15;
MAP_VERSION_SOD = $1c;
MAP_VERSION_WOG = $33;
MAP_VERSION_HOTA = 30;
TOTAL_FACTIONS = 9;
TOTAL_FACTIONS_ROE = 8;
type
TOwnerSize = (size1,size4);
TH3MQuestIdentifierMap = specialize TFPGMap<UInt32, TMapObject>;
TResolveProc = procedure (AValue: TMapObject) of object;
{ TLegacyMapObjectTemplate }
TLegacyMapObjectTemplate = class (TCollectionItem)
//strict private
// FDef: TAnimation;
private
FType: AnsiString;
FAllowedTerrains: TTerrainTypes;
FMask: TStrings;
FAnimation: string;
FMenuTerrains: TTerrainTypes;
Fsubtype: AnsiString;
FZIndex: Integer;
FVisitableFrom: TStrings;
function GetTID: integer;
procedure SetType(AValue: AnsiString);
procedure SetAllowedTerrains(AValue: TTerrainTypes);
procedure SetAnimation(AValue: string);
procedure SetMenuTerrains(AValue: TTerrainTypes);
procedure Setsubtype(AValue: AnsiString);
procedure SetZIndex(AValue: Integer);
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
property AllowedTerrains: TTerrainTypes read FAllowedTerrains write SetAllowedTerrains;
property MenuTerrains: TTerrainTypes read FMenuTerrains write SetMenuTerrains;
property TID: integer read GetTID;
property Animation:string read FAnimation write SetAnimation;
property Mask:TStrings read FMask;
property VisitableFrom: TStrings read FVisitableFrom;
property &type: AnsiString read FType write SetType;
property subtype: AnsiString read Fsubtype write Setsubtype;
property ZIndex: Integer read FZIndex write SetZIndex;
end;
{ TLegacyMapObjectTemplates }
TLegacyMapObjectTemplates = class (specialize TGArrayCollection<TLegacyMapObjectTemplate>)
private
FGraphicsManager: TGraphicsManager;
public
constructor Create(AGraphicsManager: TGraphicsManager);
end;
{ TQIResolveRequest }
TQIResolveRequest = class
public
Type
TResolveProc = procedure (AValue: string) of object;
private
FIdentifier: UInt32;
FResolveProc: TResolveProc;
public
property Identifier: UInt32 read FIdentifier write FIdentifier;
property ResolveProc:TResolveProc read FResolveProc write FResolveProc;
end;
TQIResolveRequests = specialize TFPGObjectList<TQIResolveRequest>;
{ TPositionResolveRequest }
TPositionResolveRequest = class
private
FAllowedTypes: TStrings;
FPosition: TPosition;
FResolveProc: TResolveProc;
function CheckObjectType(AObject: TMapObject): boolean;
function CheckObjectPosition(AObject: TMapObject): boolean;
public
constructor Create;
destructor Destroy; override;
property AllowedTypes: TStrings read FAllowedTypes;
property Position:TPosition read FPosition;
property ResolveProc:TResolveProc read FResolveProc write FResolveProc;
function CheckObject(AObject: TMapObject): Boolean;
end;
TPositionResolveRequests = specialize TFPGObjectList<TPositionResolveRequest>;
{ THeroResolveRequest }
THeroResolveRequest = class
private
FOwner: TPlayer;
FResolveProc: TResolveProc;
Ftype: AnsiString;
procedure SetOwner(AValue: TPlayer);
procedure Settype(AValue: AnsiString);
public
property Owner: TPlayer read FOwner write SetOwner;
property &type: AnsiString read Ftype write Settype;
property ResolveProc:TResolveProc read FResolveProc write FResolveProc;
end;
THeroResolveRequests = specialize TFPGObjectList<THeroResolveRequest>;
{ TMapReaderH3m }
TMapReaderH3m = class(TBaseMapFormatHandler, IMapReader, IObjectOptionsVisitor)
strict private
FSrc: TStreamReadAdapter;
FMapVersion: DWord;
FMap: TVCMIMap;
FTemplates: TLegacyMapObjectTemplates;
FCurrentObject: TMapObject;
FQuestIdentifierMap: TH3MQuestIdentifierMap;
FQILinksToResolve: TQIResolveRequests;
FPositionsToResolve: TPositionResolveRequests;
FHeroesToResolve: THeroResolveRequests;
class procedure CheckMapVersion(const AVersion: DWord); static;
function IsAtLeastAB: Boolean;
function IsAtLeastSoD: Boolean;
function IsWog: Boolean;
procedure SkipNotImpl(count: Integer);
procedure PushQIResolveRequest(AIdent: UInt32; AResolveProc: TQIResolveRequest.TResolveProc);
procedure ResolveQuestIdentifiers;
procedure ResolveTownPositions;
procedure ResolveHeroes;
procedure ResolveAll;
strict private
type
TIdToString = function(AId: TCustomID): AnsiString of object;
TCustomIDArray = array of TCustomID;
procedure ReadBitmask(var ADest: TCustomIDArray; AMaskSize: SizeInt; ACount: SizeInt; Negate: Boolean = True);
procedure ReadBitmask(ADest: TStrings;AMaskSize: SizeInt; ACount: SizeInt;
ACallback: TIdToString; Negate: Boolean = True);
procedure ReadBitmask(ADest: TLogicalIDCondition; AMaskSize: SizeInt; ACount: SizeInt;
ACallback: TIdToString; Negate: Boolean = True);
function ReadID1(ACallback: TIdToString; AIDRandom:TCustomID = ID_RANDOM): AnsiString;
function ReadID(ACallback: TIdToString; ASize: Integer): AnsiString; //none mask : $FF, $FFFF
procedure ReadArtifactSet(ADest: TStrings);
procedure ReadCreatureSet(ACreatureSet: TCreatureSet; nomber: Integer);
procedure ReadCreatureSet(ACreatureSet: TCreatureSet); //any size
procedure ReadQuestIdentifier;
procedure ReadResources(AresourceSet: TResourceSet);
procedure ReadPrimarySkills(ASkills: TPrimarySkills);
procedure MaybeReadSecondarySkills(ASkills: THeroSecondarySkills);
procedure ReadSecondarySkills4(ASkills: THeroSecondarySkills);
procedure ReadSecondarySkills1(ASkills: THeroSecondarySkills);
procedure ReadPosition(APosition: TPosition);
procedure ReadPlayerInfos(Attrs: TPlayerInfos);//+
procedure ReadPlayerInfo(APlayer: TPlayer; Attr: TPlayerInfo);//+?
procedure ReadSVLC();//+
procedure ReadTeams();//+
procedure ReadAllowedHeros();//+
procedure ReadDisposedHeros();//+
procedure ReadAllowedArtifacts();//+
procedure ReadAllowedSpells();//+
procedure ReadAllowedAbilities();//+
procedure ReadRumors();//+
procedure ReadPredefinedHeroes();//+
procedure ReadTerrain();//+
procedure ReadObjMask(obj: TLegacyMapObjectTemplate);//+
procedure ReadDefInfo();//+
procedure MaybeReadArtifactsOfHero(obj: THeroArtifacts);//+
procedure ReadArtifactsOfHero(obj: THeroArtifacts);//+
procedure ReadArtifactsToSlot(obj: THeroArtifacts; slot: Integer);//+
//result = Mission type
function ReadQuest(obj: TQuest): TQuestMission; //+
procedure ReadObjects();//+
procedure ReadEvents();
procedure MayBeReadGuards(AOptions: TGuardedObjectOptions);//+
procedure MayBeReadGuardsWithMessage(AOptions: TGuardedObjectOptions);//+
procedure ReadOwner(AOptions: TObjectOptions; size: TOwnerSize = TOwnerSize.size4); //+
procedure ReadHero(AOptions: THeroOptions);//+
public //IObjectOptionsVisitor
procedure VisitSignBottle(AOptions: TSignBottleOptions);//+
procedure VisitLocalEvent(AOptions: TLocalEventOptions);//+
procedure VisitPrison(AOptions: TPrisonOptions); //+
procedure VisitNormalHero(AOptions: TNormalHeroOptions); //+
procedure VisitRandomHero(AOptions: TRandomHeroOptions); //+
procedure VisitMonster(AOptions: TCreatureOptions);//+
procedure VisitSeerHut(AOptions: TSeerHutOptions);//+
procedure VisitWitchHut(AOptions:TWitchHutOptions);//+
procedure VisitScholar(AOptions: TScholarOptions);//+
procedure VisitGarrison(AOptions: TGarrisonOptions);//+
procedure VisitArtifact(AOptions: TArtifactOptions);//+
procedure VisitSpellScroll(AOptions: TSpellScrollOptions);//+
procedure VisitResource(AOptions: TResourceOptions);//+
procedure VisitTown(AOptions: TTownOptions);
procedure VisitAbandonedMine(AOptions: TAbandonedOptions);//+
procedure VisitMine(AOptions: TMineOptions);//+
procedure VisitShrine(AOptions: TShrineOptions);//+
procedure VisitPandorasBox(AOptions: TPandorasOptions);//+
procedure VisitGrail(AOptions: TGrailOptions);//+
procedure VisitRandomDwelling(AOptions: TRandomDwellingOptions);//+
procedure VisitRandomDwellingLVL(AOptions: TRandomDwellingLVLOptions);//+
procedure VisitRandomDwellingTown(AOptions: TRandomDwellingTownOptions);//+
procedure VisitQuestGuard(AOptions: TQuestGuardOptions);//+
procedure VisitOwnedObject(AOptions: TOwnedObjectOptions);//+
procedure VisitHeroPlaceholder(AOptions: THeroPlaceholderOptions);//+
public
constructor Create(AMapEnv: TMapEnvironment); override;
destructor Destroy; override;
function Read(AStream: TStream): TVCMIMap;
end;
implementation
uses LazLoggerBase, editor_consts, editor_utils;
{ THeroResolveRequest }
procedure THeroResolveRequest.Settype(AValue: AnsiString);
begin
Ftype:=AValue;
end;
procedure THeroResolveRequest.SetOwner(AValue: TPlayer);
begin
FOwner:=AValue;
end;
{ TPositionResolveRequest }
function TPositionResolveRequest.CheckObjectType(AObject: TMapObject): boolean;
begin
if AllowedTypes.Count = 0 then
begin
Result := True;
end
else
begin
Result := AllowedTypes.IndexOf(AObject.&Type) >= 0;
end;
end;
function TPositionResolveRequest.CheckObjectPosition(AObject: TMapObject): boolean;
begin
Result := AObject.EqualPosition(FPosition) or AObject.IsVisitableAt(FPosition.L, FPosition.X, FPosition.Y);
end;
constructor TPositionResolveRequest.Create;
begin
FPosition := TPosition.Create;
FAllowedTypes := TIdentifierSet.Create(nil);
end;
destructor TPositionResolveRequest.Destroy;
begin
FAllowedTypes.Free;
FPosition.Free;
inherited Destroy;
end;
function TPositionResolveRequest.CheckObject(AObject: TMapObject): Boolean;
begin
Result := CheckObjectPosition(AObject) and CheckObjectType(AObject);
end;
{ TLegacyMapObjectTemplates }
constructor TLegacyMapObjectTemplates.Create(AGraphicsManager: TGraphicsManager);
begin
inherited Create;
FGraphicsManager := AGraphicsManager;
end;
{ TLegacyMapObjectTemplate }
constructor TLegacyMapObjectTemplate.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
FMask := TStringList.Create;
FAllowedTerrains := ALL_TERRAINS;
FVisitableFrom := TStringList.Create;
end;
destructor TLegacyMapObjectTemplate.Destroy;
begin
FVisitableFrom.Free;
FMask.Free;
inherited Destroy;
end;
function TLegacyMapObjectTemplate.GetTID: integer;
begin
Result := inherited ID;
end;
procedure TLegacyMapObjectTemplate.SetType(AValue: AnsiString);
begin
if FType=AValue then Exit;
FType:=AValue;
end;
procedure TLegacyMapObjectTemplate.SetAllowedTerrains(AValue: TTerrainTypes);
begin
if FAllowedTerrains = AValue then Exit;
FAllowedTerrains := AValue;
end;
procedure TLegacyMapObjectTemplate.SetAnimation(AValue: string);
begin
AValue := NormalizeResourceName(AValue);
if FAnimation = AValue then Exit;
FAnimation := AValue;
//FDef := (Collection as TLegacyMapObjectTemplates).FGraphicsManager.GetMapAnimation(FAnimation);
//todo: load and check
end;
procedure TLegacyMapObjectTemplate.SetMenuTerrains(AValue: TTerrainTypes);
begin
if FMenuTerrains = AValue then Exit;
FMenuTerrains := AValue;
end;
procedure TLegacyMapObjectTemplate.Setsubtype(AValue: AnsiString);
begin
if Fsubtype=AValue then Exit;
Fsubtype:=AValue;
end;
procedure TLegacyMapObjectTemplate.SetZIndex(AValue: Integer);
begin
if FZIndex = AValue then Exit;
FZIndex := AValue;
end;
{ TMapReaderH3m }
class procedure TMapReaderH3m.CheckMapVersion(const AVersion: DWord);
begin
if (AVersion <> MAP_VERSION_AB)
and (AVersion <> MAP_VERSION_ROE)
and (AVersion <> MAP_VERSION_SOD)
and (AVersion <> MAP_VERSION_WOG)
{and (AVersion <> MAP_VERSION_HOTA)}then
begin
raise Exception.Create('Invalid map format '+IntToHex(AVersion,8));
end;
end;
function TMapReaderH3m.IsAtLeastAB: Boolean;
begin
Result := FMapVersion >= MAP_VERSION_AB;
end;
function TMapReaderH3m.IsAtLeastSoD: Boolean;
begin
Result := FMapVersion >= MAP_VERSION_SOD;
end;
constructor TMapReaderH3m.Create(AMapEnv: TMapEnvironment);
begin
inherited Create(AMapEnv);
FQuestIdentifierMap := TH3MQuestIdentifierMap.Create;
FQILinksToResolve := TQIResolveRequests.Create(True);
FPositionsToResolve := TPositionResolveRequests.Create(True);
FHeroesToResolve := THeroResolveRequests.Create(True);
FTemplates := TLegacyMapObjectTemplates.Create(AMapEnv.tm.GraphicsManager);
end;
destructor TMapReaderH3m.Destroy;
begin
FTemplates.Free;
FHeroesToResolve.Free;
FPositionsToResolve.Free;
FQILinksToResolve.Free;
FQuestIdentifierMap.Free;
inherited Destroy;
end;
function TMapReaderH3m.IsWog: Boolean;
begin
Result := FMapVersion >= MAP_VERSION_WOG;
end;
procedure TMapReaderH3m.MayBeReadGuards(AOptions: TGuardedObjectOptions);
var
is_guard: Boolean;
begin
is_guard := FSrc.ReadBoolean;
if is_guard then
begin
ReadCreatureSet(AOptions.Guards,7);
end;
end;
procedure TMapReaderH3m.MayBeReadGuardsWithMessage(
AOptions: TGuardedObjectOptions);
var
is_guard_msg: Boolean;
begin
is_guard_msg := FSrc.ReadBoolean;
if is_guard_msg then
begin
AOptions.GuardMessage := FSrc.ReadLocalizedString;
MayBeReadGuards(AOptions);
FSrc.Skip(4); //junk
end;
end;
function TMapReaderH3m.Read(AStream: TStream): TVCMIMap;
var
cr_params: TMapCreateParams;
lvl: TMapLevel;
begin
FQuestIdentifierMap.Clear;
FTemplates.Clear;
AStream.Seek(0,soBeginning);
FSrc.Create(AStream);
//main header part
with FSrc do
begin
FMapVersion := ReadDWord;
CheckMapVersion(FMapVersion);
ReadBoolean;//are any players
cr_params.Height := ReadDWord;
cr_params.Width := cr_params.Height;
cr_params.Levels := ReadByte + 1; //one level = 0
end;
FMap := TVCMIMap.CreateEmpty(FMapEnv);
lvl := FMap.MapLevels.Add;
lvl.Width:=cr_params.Width;
lvl.Height:=cr_params.Height;
lvl.Identifier := 'surface';
if cr_params.Levels>1 then
begin
lvl := FMap.MapLevels.Add;
lvl.Width:=cr_params.Width;
lvl.Height:=cr_params.Height;
lvl.Identifier := 'underground';
end;
FMap.CurrentLevelIndex := 0;
Result := FMap;
try
//rest of header
with FSrc do
begin
Result.Name := ReadLocalizedString;
Result.Description := ReadLocalizedString;
Result.Difficulty := TDifficulty(ReadByte);
if IsAtLeastAB() then
begin
Result.HeroLevelLimit := ReadByte;
end else
begin
Result.HeroLevelLimit := 0;
end;
end;
ReadPlayerInfos(FMap.Players);
ReadSVLC();
ReadTeams();
ReadAllowedHeros();
ReadDisposedHeros();
ReadAllowedArtifacts();
ReadAllowedSpells();
ReadAllowedAbilities();
ReadRumors();
ReadPredefinedHeroes();
ReadTerrain();
ReadDefInfo();
ReadObjects();
ReadEvents();
ResolveAll();
if IsWog then
begin
FMap.ModUsage.ForceMod('wog', true);
end;
except
FreeAndNil(Fmap);
raise;
end;
Result.Loaded;
FMap := nil;
end;
procedure TMapReaderH3m.ReadAllowedAbilities;
begin
if IsAtLeastSoD() then
begin
ReadBitmask(FMap.AllowedAbilities, 4, SKILL_QUANTITY,@FMapEnv.lm.SkillNidToString);
end;
end;
procedure TMapReaderH3m.ReadAllowedArtifacts;
var
cnt: Integer;
begin
if IsAtLeastAB() then
begin
cnt := ifthen(IsAtLeastSoD(),18,17);
ReadBitmask(FMap.AllowedArtifacts, cnt, ARTIFACT_QUANTITY, @FMapEnv.lm.ArtifactIndexToString, true);
end;
end;
procedure TMapReaderH3m.ReadAllowedHeros;
var
cnt: Integer;
begin
cnt := ifthen(IsAtLeastAB(),20,16);
ReadBitmask(FMap.AllowedHeroes, cnt, HERO_QUANTITY, @FMapEnv.lm.HeroIndexToString, False);
//unknown plaseholder
if IsAtLeastAB() then
begin
cnt :=FSrc.ReadDWord;
FSrc.Skip(cnt);
end;
end;
procedure TMapReaderH3m.ReadAllowedSpells;
begin
if IsAtLeastSoD() then
begin
ReadBitmask(FMap.AllowedSpells,9,SPELL_QUANTITY_ACTUAL,@(FMapEnv.lm.SpellIndexToString));
end;
end;
procedure TMapReaderH3m.VisitArtifact(AOptions: TArtifactOptions);
begin
MayBeReadGuardsWithMessage(AOptions);
end;
procedure TMapReaderH3m.ReadArtifactsOfHero(obj: THeroArtifacts);
var
i: Integer;
cnt: Word;
begin
with FSrc do
begin
for i := 0 to 15 do
begin
ReadArtifactsToSlot(obj,i);
end;
if IsAtLeastSoD() then
begin
ReadArtifactsToSlot(obj,16);//MACH4
end;
ReadArtifactsToSlot(obj,17); //spellbook
if IsAtLeastAB() then
ReadArtifactsToSlot(obj,18)//MISC5
else
skip(1);
cnt := ReadWord;
for i := 0 to cnt - 1 do
begin
ReadArtifactsToSlot(obj,19+i);
end;
end;
end;
procedure TMapReaderH3m.ReadArtifactsToSlot(obj: THeroArtifacts; slot: Integer);
var
artmask, aid: Word;
artId: AnsiString;
begin
artmask := ifthen(IsAtLeastAB(),$FFFF, $FF);
if IsAtLeastAB() then
begin
aid := FSrc.ReadWord;
end
else begin
aid := FSrc.ReadByte;
end;
if aid = artmask then
exit;
artId:=FMapEnv.lm.ArtifactIndexToString(aid);
obj.BySlotNumber[slot] := artId;
end;
procedure TMapReaderH3m.ReadBitmask(var ADest: TCustomIDArray; AMaskSize: SizeInt; ACount: SizeInt; Negate: Boolean);
var
real_count: SizeInt;
byte_idx: SizeInt;
mask_byte: Byte;
bit: UInt8;
flag: Boolean;
id: TCustomID;
begin
SetLength(ADest, 0);
SetLength(ADest, ACount);
real_count := 0;
for byte_idx := 0 to AMaskSize - 1 do
begin
mask_byte := FSrc.ReadByte;
for bit in [0..7] do
begin
flag := (mask_byte and (1 shl bit)) > 0;
flag := flag xor Negate;
id := byte_idx*8+bit;
if flag and (id < ACount) then
begin
ADest[real_count] := id;
inc(real_count);
end;
end;
end;
SetLength(ADest, real_count);
end;
procedure TMapReaderH3m.ReadBitmask(ADest: TStrings; AMaskSize: SizeInt;
ACount: SizeInt; ACallback: TIdToString; Negate: Boolean);
var
tmp: array of TCustomID;
iter: TCustomID;
begin
ADest.Clear;
SetLength(tmp, 0);
ReadBitmask(tmp, AMaskSize, ACount, Negate);
for iter in tmp do
begin
ADest.Add(ACallback(iter));
end;
end;
procedure TMapReaderH3m.ReadBitmask(ADest: TLogicalIDCondition;
AMaskSize: SizeInt; ACount: SizeInt; ACallback: TIdToString; Negate: Boolean);
var
tmp: TStringList;
begin
ADest.Clear;
tmp := TStringList.Create;
try
ReadBitmask(tmp, AMaskSize, ACount, ACallback, Negate);
ADest.AnyOf.Assign(tmp);
finally
tmp.Free;
end;
end;
function TMapReaderH3m.ReadID1(ACallback: TIdToString; AIDRandom: TCustomID): AnsiString;
var
index: TCustomID;
begin
index := FSrc.ReadIDByte;
if index = AIDRandom then
begin
Result := '';
end
else
begin
Result := ACallback(index);
end;
end;
function TMapReaderH3m.ReadID(ACallback: TIdToString; ASize: Integer): AnsiString;
var
id : TCustomID;
begin
id := -1;
if ASize = 1 then
begin
id := FSrc.ReadByte;
if id = $FF then
begin
id := -1;
end;
end
else if ASize = 2 then
begin
id := FSrc.ReadWord;
if id = $FFFF then
begin
id := -1;
end;
end
else
Assert(False, 'ReadID: invalid size');
if id >= 0 then
begin
Result := ACallback(id);
end
else begin
Result := '';
end;
end;
procedure TMapReaderH3m.ReadArtifactSet(ADest: TStrings);
var
cnt: Byte;
i: Integer;
artid: Word;
begin
ADest.Clear;
cnt := FSrc.ReadByte();
for i := 0 to cnt - 1 do
begin
artid := FSrc.ReadWord;
ADest.Add(FMapEnv.lm.ArtifactIndexToString(artid));
end;
end;
procedure TMapReaderH3m.ReadCreatureSet(ACreatureSet: TCreatureSet;
nomber: Integer);
var
maxID,
creid,crecnt: Integer;
version: Boolean;
i: Integer;
info: TCreatureInstInfo;
begin
version := IsAtLeastAB();
maxID := ifthen(version,$ffff, $ff);
ACreatureSet.Clear;
with FSrc do begin
for i := 0 to nomber - 1 do
begin
info := ACreatureSet.Add;
if version then
begin
creid := ReadWord;
end
else begin
creid := ReadByte;
end;
crecnt := ReadWord;
if creid = maxID then
Continue;// empty slot, leave with default values
if creid > maxID - $f then
begin
info.RawRandom:=maxID - creID - 1;
end
else
begin
info.&type := FMapEnv.lm.CreatureIndexToString(creid);
info.Amount := crecnt;
end;
end;
end;
end;
procedure TMapReaderH3m.ReadCreatureSet(ACreatureSet: TCreatureSet);
var
cnt: Byte;
i: Integer;
creid: Word;
crecnt: Word;
info: TCreatureInstInfo;
begin
ACreatureSet.Clear;
cnt := FSrc.ReadByte;
for i := 0 to cnt - 1 do
begin
creid := FSrc.ReadWord;
crecnt := FSrc.ReadWord;
info := ACreatureSet.Add;
info.&type := FMapEnv.lm.CreatureIndexToString(creid);
info.Amount := crecnt;
end;
end;
procedure TMapReaderH3m.ReadQuestIdentifier;
var
ident: DWord;
begin
if IsAtLeastAB() then
begin
ident := FSrc.ReadDWord;
FQuestIdentifierMap.KeyData[ident] := FCurrentObject;
end;
end;
procedure TMapReaderH3m.ReadResources(AresourceSet: TResourceSet);
var
i: Integer;
begin
for i := 0 to 7 - 1 do
begin
AresourceSet.Amount[TResType(i)] := FSrc.ReadInt32;
end;
end;
procedure TMapReaderH3m.ReadPrimarySkills(ASkills: TPrimarySkills);
begin
ASkills.Attack:=FSrc.ReadByte;
ASkills.Defence:=FSrc.ReadByte;
ASkills.Spellpower:=FSrc.ReadByte;
ASkills.Knowledge:=FSrc.ReadByte;
end;
procedure TMapReaderH3m.MaybeReadSecondarySkills(ASkills: THeroSecondarySkills);
begin
with FSrc do
if ReadBoolean then
begin
ReadSecondarySkills4(ASkills);
end;
end;
procedure TMapReaderH3m.ReadSecondarySkills4(ASkills: THeroSecondarySkills);
var
secSkill: THeroSecondarySkill;
cnt: DWord;
i: Integer;
begin
with FSrc do
begin
cnt := ReadDWord;
for i := 0 to cnt - 1 do
begin
secSkill := ASkills.Add;
secSkill.Identifier :=ReadID(@FMapEnv.lm.SkillNidToString,1);
secSkill.Level:=TSkillLevel(ReadByte);
end;
end;
end;
procedure TMapReaderH3m.ReadSecondarySkills1(ASkills: THeroSecondarySkills);
var
secSkill: THeroSecondarySkill;
cnt: Integer;
i: Integer;
begin
with FSrc do
begin
cnt := ReadByte;
for i := 0 to cnt - 1 do
begin
secSkill := ASkills.Add;
secSkill.Identifier :=ReadID(@FMapEnv.lm.SkillNidToString,1);
secSkill.Level:=TSkillLevel(ReadByte);
end;
end;
end;
procedure TMapReaderH3m.ReadPosition(APosition: TPosition);
begin
with APosition, FSrc do
begin
X:=ReadByte;
Y:=ReadByte;
L:=ReadByte;
end;
end;
procedure TMapReaderH3m.ReadDefInfo;
function read_terrains(): TTerrainTypes;
var
tt: TTerrainType;
i: Integer;
data: Word;
begin
data:= FSrc.ReadWord;
Result := [];
for tt in TTerrainType do
begin
i := Integer(tt);
if ((1 shl i) and data) > 0 then
begin
Result +=[tt];
end;
end;
end;
var
obj: TLegacyMapObjectTemplate;
cnt: DWord;
i: Integer;
b: Byte;
obj_type: TMapObjectType;
ID: DWord;
sub_id: DWord;
group: Byte;
begin
cnt := FSrc.ReadDWord;
for i := 0 to cnt - 1 do
begin
obj := FTemplates.Add;
obj.Animation := FSrc.ReadString;
ReadObjMask(obj);
obj.AllowedTerrains := read_terrains();
obj.MenuTerrains := read_terrains();
ID := FSrc.ReadDWord;
sub_id := FSrc.ReadDWord;
obj_type := FMapEnv.om.ResolveLegacyID(id,sub_id);
if Assigned(obj_type) then
begin
obj.&type := obj_type.MapObjectGroup.Identifier;
obj.subtype:=obj_type.Identifier;
end
else
begin
DebugLn(['Unknown object: TID ', i, ' ID ', ID, ' subid ', sub_id]);
end;
group := FSrc.ReadByte;
b := FSrc.ReadByte;
obj.ZIndex := b * Z_INDEX_OVERLAY;
FSrc.Skip(16); //junk
GenerateDefaultVisitableFrom(obj.VisitableFrom,group, TObj(ID));
//DebugLn(['Read TID ', i, ' ID ', ID, ' subid ', sub_id]);
end;
end;
procedure TMapReaderH3m.MaybeReadArtifactsOfHero(obj: THeroArtifacts);
begin
if FSrc.ReadBoolean then
begin
ReadArtifactsOfHero(obj);
end;
end;