-
Notifications
You must be signed in to change notification settings - Fork 2
/
LemNeoPieceManager.pas
566 lines (469 loc) · 15.4 KB
/
LemNeoPieceManager.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
unit LemNeoPieceManager;
{ The TNeoPieceManager class is used in a similar manner to how graphic sets were in the past.
It could be thought of as a huge dynamic graphic set. }
interface
uses
Dialogs,
PngInterface, LemNeoTheme, LemAnimationSet,
LemMetaTerrain, LemTerrainGroup, LemGadgetsMeta, LemGadgetsConstants, LemTypes, GR32, LemStrings,
Generics.Collections,
StrUtils, Classes, SysUtils,
LemNeoParser,
SharedGlobals;
const
RETAIN_PIECE_CYCLES = 20; // How many times Tidy can be called without a piece being used before it's discarded
COMPOSITE_PIECE_STYLE = '*GROUP'; // What to use for the style name in per-level composite pieces
type
TLabelRecord = record
GS: String;
Piece: String;
end;
TAliasKind = (rkStyle, rkGadget, rkTerrain, rkBackground, rkLemmings);
TStyleAlias = record
Source: TLabelRecord;
Dest: TLabelRecord;
DefWidth: Integer;
DefHeight: Integer;
Kind: TAliasKind;
end;
TDealiasResult = record
Piece: TLabelRecord;
DefWidth: Integer;
DefHeight: Integer;
end;
TUpscaleInfo = record
Source: TLabelRecord;
Kind: TAliasKind;
Settings: TUpscaleSettings;
end;
TNeoPieceManager = class
private
fTheme: TNeoTheme;
fTerrains: TMetaTerrains;
fObjects: TGadgetMetaInfoList;
fNeedCheckStyles: TStringList;
fLoadedPropertiesStyles: TStringList;
fAliases: TList<TStyleAlias>;
fUpscaling: TList<TUpscaleInfo>;
fLoadPropertiesStyle: String; // Temporary usage only.
function GetTerrainCount: Integer;
function GetObjectCount: Integer;
function FindTerrainIndexByIdentifier(Identifier: String): Integer;
function FindObjectIndexByIdentifier(Identifier: String): Integer;
function ObtainTerrain(Identifier: String): Integer;
function ObtainObject(Identifier: String): Integer;
function GetMetaTerrain(Identifier: String): TMetaTerrain;
function GetMetaObject(Identifier: String): TGadgetMetaInfo;
procedure LoadAliases(aStyle: String);
procedure AddAlias(aSection: TParserSection; const aIteration: Integer; aData: Pointer);
procedure LoadUpscaling(aStyle: String);
procedure AddUpscaling(aSection: TParserSection; const aIteration: Integer; aData: Pointer);
property TerrainCount: Integer read GetTerrainCount;
property ObjectCount: Integer read GetObjectCount;
public
constructor Create;
destructor Destroy; override;
procedure Tidy;
procedure RemoveCompositePieces;
procedure Clear;
procedure SetTheme(aTheme: TNeoTheme);
procedure RegenerateAutoAnims(aTheme: TNeoTheme; aAni: TBaseAnimationSet);
procedure MakePiecesFromGroups(aGroups: TTerrainGroups);
procedure MakePieceFromGroup(aGroup: TTerrainGroup);
procedure LoadProperties(aStyle: String);
function Dealias(aIdentifier: String; aKind: TAliasKind): TDealiasResult;
function GetUpscaleInfo(aIdentifier: String; aKind: TAliasKind): TUpscaleInfo;
property Terrains[Identifier: String]: TMetaTerrain read GetMetaTerrain;
property Objects[Identifier: String]: TGadgetMetaInfo read GetMetaObject;
property NeedCheckStyles: TStringList read fNeedCheckStyles;
end;
function SplitIdentifier(Identifier: String): TLabelRecord;
function CombineIdentifier(Identifier: TLabelRecord): String;
var
PieceManager: TNeoPieceManager; // Globalized as this does not need to have seperate instances
implementation
uses
GameControl, LemTerrain;
// These two standalone functions are just to help shifting labels around
function SplitIdentifier(Identifier: String): TLabelRecord;
var
i: Integer;
FoundDivider: Boolean;
begin
Result.GS := '';
Result.Piece := '';
FoundDivider := False;
for i := 1 to Length(Identifier) do
if Identifier[i] = ':' then
FoundDivider := True
else if FoundDivider then
Result.Piece := Result.Piece + Identifier[i]
else
Result.GS := Result.GS + Identifier[i];
end;
function CombineIdentifier(Identifier: TLabelRecord): String;
begin
// This one is much simpler.
Result := Identifier.GS + ':' + Identifier.Piece;
end;
// Constructor, destructor, usual boring stuff
procedure TNeoPieceManager.Clear;
begin
fTerrains.Clear;
fObjects.Clear;
fLoadedPropertiesStyles.Clear;
fAliases.Clear;
fUpscaling.Clear;
fNeedCheckStyles.Clear;
LoadProperties('default');
end;
constructor TNeoPieceManager.Create;
begin
inherited;
fTerrains := TMetaTerrains.Create;
fObjects := TGadgetMetaInfoList.Create;
fTheme := nil;
fLoadedPropertiesStyles := TStringList.Create;
fAliases := TList<TStyleAlias>.Create;
fUpscaling := TList<TUpscaleInfo>.Create;
fNeedCheckStyles := TStringList.Create;
fNeedCheckStyles.Sorted := True;
fNeedCheckStyles.Duplicates := dupIgnore;
LoadProperties('default');
end;
destructor TNeoPieceManager.Destroy;
begin
fTerrains.Free;
fObjects.Free;
fAliases.Free;
fUpscaling.Free;
fLoadedPropertiesStyles.Free;
fNeedCheckStyles.Free;
inherited;
end;
// Tidy-up function, clears out the lists. Might add stuff in the future so it retains frequently-used pieces.
procedure TNeoPieceManager.Tidy;
var
i: Integer;
begin
for i := fTerrains.Count-1 downto 0 do
begin
fTerrains[i].CyclesSinceLastUse := fTerrains[i].CyclesSinceLastUse + 1;
if fTerrains[i].CyclesSinceLastUse >= RETAIN_PIECE_CYCLES then
fTerrains.Delete(i);
end;
for i := fObjects.Count-1 downto 0 do
begin
fObjects[i].CyclesSinceLastUse := fObjects[i].CyclesSinceLastUse + 1;
if (fObjects[i].CyclesSinceLastUse >= RETAIN_PIECE_CYCLES) then
fObjects.Delete(i);
end;
end;
// Quick shortcuts to get number of pieces currently present
function TNeoPieceManager.GetTerrainCount: Integer;
begin
Result := fTerrains.Count;
end;
function TNeoPieceManager.GetObjectCount: Integer;
begin
Result := fObjects.Count;
end;
// Some functions to locate a piece in the internal arrays...
function TNeoPieceManager.FindTerrainIndexByIdentifier(Identifier: String): Integer;
begin
Identifier := Lowercase(Identifier);
for Result := 0 to TerrainCount-1 do
if fTerrains[Result].Identifier = Identifier then Exit;
// If it's not found
Result := ObtainTerrain(Identifier);
end;
function TNeoPieceManager.FindObjectIndexByIdentifier(Identifier: String): Integer;
begin
Identifier := Lowercase(Identifier);
for Result := 0 to ObjectCount-1 do
if fObjects[Result].Identifier = Identifier then Exit;
// If it's not found
Result := ObtainObject(Identifier);
end;
// ... And to load it if not found.
function TNeoPieceManager.ObtainTerrain(Identifier: String): Integer;
var
BasePath: String;
TerrainLabel: TLabelRecord;
T: TMetaTerrain;
begin
TerrainLabel := SplitIdentifier(Identifier);
Result := fTerrains.Count;
BasePath := AppPath + SFStyles + TerrainLabel.GS + SFPiecesTerrain + TerrainLabel.Piece;
if FileExists(BasePath + '.png') then // .nxmt is optional, but .png is not :)
T := TMetaTerrain.Create
else begin
Result := -1;
Exit;
end;
fTerrains.Add(T);
T.Load(TerrainLabel.GS, TerrainLabel.Piece);
end;
function TNeoPieceManager.ObtainObject(Identifier: String): Integer;
var
ObjectLabel: TLabelRecord;
MO: TGadgetMetaInfo;
begin
try
ObjectLabel := SplitIdentifier(Identifier);
Result := fObjects.Count;
MO := TGadgetMetaInfo.Create;
MO.Load(ObjectLabel.GS, ObjectLabel.Piece, fTheme);
fObjects.Add(MO);
except
Result := -1;
end;
end;
// Functions to get the metainfo
function TNeoPieceManager.GetMetaTerrain(Identifier: String): TMetaTerrain;
var
i: Integer;
begin
i := FindTerrainIndexByIdentifier(Identifier);
if i >= 0 then
begin
Result := fTerrains[i];
Result.CyclesSinceLastUse := 0;
end else
Result := nil;
end;
function TNeoPieceManager.GetMetaObject(Identifier: String): TGadgetMetaInfo;
var
i: Integer;
begin
i := FindObjectIndexByIdentifier(Identifier);
if i >= 0 then
begin
Result := fObjects[i];
Result.CyclesSinceLastUse := 0;
end else
Result := nil;
end;
// And the stuff for communicating with the theme
procedure TNeoPieceManager.SetTheme(aTheme: TNeoTheme);
var
i: Integer;
begin
fTheme := aTheme;
Tidy;
for i := 0 to fObjects.Count-1 do
if fObjects[i].Animations[False, False, False].AnyMasked then
fObjects[i].Remask(aTheme);
end;
procedure TNeoPieceManager.RegenerateAutoAnims(aTheme: TNeoTheme;
aAni: TBaseAnimationSet);
var
i: Integer;
begin
for i := 0 to fObjects.Count-1 do
fObjects[i].RegenerateAutoAnims(aTheme, aAni);
end;
// Functions for composite pieces
procedure TNeoPieceManager.MakePieceFromGroup(aGroup: TTerrainGroup);
var
BMP, HrBMP: TBitmap32;
T: TMetaTerrain;
function IsGroupSteel: Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to aGroup.Terrains.Count-1 do
if (aGroup.Terrains[i].DrawingFlags and tdf_Erase) = 0 then
begin
Result := Terrains[aGroup.Terrains[i].Identifier].IsSteel;
Exit;
end;
end;
begin
if GameParams.HighResolution then
HrBMP := TBitmap32.Create
else
HrBMP := nil;
BMP := TBitmap32.Create;
try
GameParams.Renderer.PrepareCompositePieceBitmaps(aGroup.Terrains, BMP, HrBMP);
T := fTerrains.Add;
T.LoadFromImage(BMP, HrBMP, COMPOSITE_PIECE_STYLE, aGroup.Name, IsGroupSteel);
finally
BMP.Free;
HrBMP.Free;
end;
end;
procedure TNeoPieceManager.MakePiecesFromGroups(aGroups: TTerrainGroups);
var
i: Integer;
begin
for i := 0 to aGroups.Count-1 do
MakePieceFromGroup(aGroups[i]);
end;
procedure TNeoPieceManager.RemoveCompositePieces;
var
i: Integer;
begin
for i := fTerrains.Count-1 downto 0 do
if fTerrains[i].GS = COMPOSITE_PIECE_STYLE then
fTerrains.Delete(i);
end;
// Aliases and upscaling
procedure TNeoPieceManager.LoadAliases(aStyle: String);
var
Parser: TParser;
begin
if not FileExists(AppPath + SFStyles + aStyle + '\alias.nxmi') then Exit;
Parser := TParser.Create;
try
Parser.LoadFromFile(AppPath + SFStyles + aStyle + '\alias.nxmi');
Parser.MainSection.DoForEachSection('GADGET', AddAlias, Pointer(rkGadget));
Parser.MainSection.DoForEachSection('TERRAIN', AddAlias, Pointer(rkTerrain));
Parser.MainSection.DoForEachSection('BACKGROUND', AddAlias, Pointer(rkBackground));
Parser.MainSection.DoForEachSection('STYLE', AddAlias, Pointer(rkStyle));
Parser.MainSection.DoForEachSection('LEMMINGS', AddAlias, Pointer(rkLemmings));
finally
Parser.Free;
end;
end;
procedure TNeoPieceManager.LoadProperties(aStyle: String);
begin
if fLoadedPropertiesStyles.IndexOf(aStyle) >= 0 then Exit;
fLoadedPropertiesStyles.Add(aStyle);
fLoadPropertiesStyle := aStyle;
LoadAliases(aStyle);
LoadUpscaling(aStyle);
end;
procedure TNeoPieceManager.LoadUpscaling(aStyle: String);
var
Parser: TParser;
begin
if not FileExists(AppPath + SFStyles + aStyle + '\upscaling.nxmi') then
begin
Exit;
end;
Parser := TParser.Create;
try
Parser.LoadFromFile(AppPath + SFStyles + aStyle + '\upscaling.nxmi');
Parser.MainSection.DoForEachSection('GADGET', AddUpscaling, Pointer(rkGadget));
Parser.MainSection.DoForEachSection('TERRAIN', AddUpscaling, Pointer(rkTerrain));
Parser.MainSection.DoForEachSection('BACKGROUND', AddUpscaling, Pointer(rkBackground));
Parser.MainSection.DoForEachSection('STYLE', AddUpscaling, Pointer(rkStyle));
Parser.MainSection.DoForEachSection('LEMMINGS', AddUpscaling, Pointer(rkLemmings));
finally
Parser.Free;
end;
end;
procedure TNeoPieceManager.AddAlias(aSection: TParserSection;
const aIteration: Integer; aData: Pointer);
var
Kind: TAliasKind absolute aData;
NewRec: TStyleAlias;
begin
NewRec.Source := SplitIdentifier(aSection.LineString['FROM']);
NewRec.Dest := SplitIdentifier(aSection.LineString['TO']);
NewRec.DefWidth := aSection.LineNumericDefault['WIDTH', -1];
NewRec.DefHeight := aSection.LineNumericDefault['HEIGHT', -1];
NewRec.Kind := Kind;
if NewRec.Source.GS = '' then NewRec.Source.GS := fLoadPropertiesStyle;
if NewRec.Dest.GS = '' then NewRec.Dest.GS := fLoadPropertiesStyle;
fAliases.Add(NewRec);
end;
procedure TNeoPieceManager.AddUpscaling(aSection: TParserSection;
const aIteration: Integer; aData: Pointer);
var
Kind: TAliasKind absolute aData;
NewRec: TUpscaleInfo;
function GetEdgeBehaviour(aLabel: String): TUpscaleEdgeBehaviour;
begin
Result := uebRepeat;
if Uppercase(aSection.LineTrimString[aLabel]) = 'MIRROR' then Result := uebMirror;
if Uppercase(aSection.LineTrimString[aLabel]) = 'BLANK' then Result := uebTransparent;
end;
begin
NewRec.Source.GS := fLoadPropertiesStyle;
NewRec.Kind := Kind;
NewRec.Settings.LeftSide := GetEdgeBehaviour('LEFT_EDGE');
NewRec.Settings.TopSide := GetEdgeBehaviour('TOP_EDGE');
NewRec.Settings.RightSide := GetEdgeBehaviour('RIGHT_EDGE');
NewRec.Settings.BottomSide := GetEdgeBehaviour('BOTTOM_EDGE');
NewRec.Settings.Mode := umPixelArt;
if Uppercase(aSection.LineTrimString['UPSCALE']) = 'ZOOM' then NewRec.Settings.Mode := umNearest;
if Uppercase(aSection.LineTrimString['UPSCALE']) = 'RESAMPLE' then NewRec.Settings.Mode := umFullColor;
aSection.DoForEachLine('PIECE', procedure(aLine: TParserLine; const aIteration: Integer)
begin
NewRec.Source.Piece := aLine.ValueTrimmed;
fUpscaling.Add(NewRec);
end);
end;
function TNeoPieceManager.Dealias(aIdentifier: String; aKind: TAliasKind): TDealiasResult;
var
LastIdent: TLabelRecord;
i: Integer;
begin
Result.Piece := SplitIdentifier(aIdentifier);
Result.DefWidth := 0;
Result.DefHeight := 0;
repeat
LastIdent := Result.Piece;
LoadProperties(Result.Piece.GS);
if aKind <> rkStyle then
for i := 0 to fAliases.Count-1 do
begin
if Result.Piece.GS <> fAliases[i].Source.GS then Continue;
if (fAliases[i].Kind = aKind) and (Result.Piece.Piece = fAliases[i].Source.Piece) then
begin
Result.Piece := fAliases[i].Dest;
Result.DefWidth := fAliases[i].DefWidth;
Result.DefHeight := fAliases[i].DefHeight;
end;
end;
for i := 0 to fAliases.Count-1 do
begin
if Result.Piece.GS <> fAliases[i].Source.GS then Continue;
if fAliases[i].Kind = rkStyle then
Result.Piece.GS := fAliases[i].Dest.GS;
end;
until (Result.Piece.GS = LastIdent.GS) and (Result.Piece.Piece = LastIdent.Piece);
end;
function TNeoPieceManager.GetUpscaleInfo(aIdentifier: String;
aKind: TAliasKind): TUpscaleInfo;
var
Ident: TLabelRecord;
i: Integer;
begin
Ident := SplitIdentifier(aIdentifier);
LoadProperties(Ident.GS);
// Fallback settings
FillChar(Result, SizeOf(TUpscaleInfo), 0);
if aKind = rkLemmings then
Result.Settings.Mode := umNearest
else
Result.Settings.Mode := umPixelArt;
if aKind in [rkTerrain, rkBackground] then
begin
Result.Settings.LeftSide := uebRepeat;
Result.Settings.TopSide := uebRepeat;
Result.Settings.RightSide := uebRepeat;
Result.Settings.BottomSide := uebRepeat;
end else begin
Result.Settings.LeftSide := uebTransparent;
Result.Settings.TopSide := uebTransparent;
Result.Settings.RightSide := uebTransparent;
Result.Settings.BottomSide := uebTransparent;
end;
for i := 0 to fUpscaling.Count-1 do
begin
if Ident.GS <> fUpscaling[i].Source.GS then Continue;
if fUpscaling[i].Kind = rkStyle then Result := fUpscaling[i];
if (fUpscaling[i].Kind = aKind) and
((aKind = rkLemmings) or (Ident.Piece = fUpscaling[i].Source.Piece)) then
begin
Result := fUpscaling[i];
Exit;
end;
end;
end;
end.