-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMapFile.cpp
More file actions
1250 lines (1037 loc) · 34 KB
/
CMapFile.cpp
File metadata and controls
1250 lines (1037 loc) · 34 KB
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
#include "stdafx.h"
#include "CMapFile.h"
#include "GlobalFunctions.h"
extern int g_cLocks;
// IUnknown functions
// ******************
ULONG __stdcall CMapFile::AddRef()
{
return ++m_cRef;
}
ULONG __stdcall CMapFile::Release()
{
if (--m_cRef != 0)
return m_cRef;
delete this;
return 0;
}
HRESULT __stdcall CMapFile::QueryInterface(REFIID riid, void** ppv)
{
if (riid == IID_IUnknown)
*ppv = (IUnknown*)(MapFile*)this;
else if (riid == IID_MapFile)
*ppv = (MapFile*)this;
else if (riid == IID_ISupportErrorInfo)
*ppv = (ISupportErrorInfo*)this;
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
// TileGroup functions
// *******************
HRESULT CMapFile::get_TileWidth(int *width)
{
*width = tileWidth;
return S_OK;
}
HRESULT CMapFile::get_TileHeight(int *height)
{
*height = mapHeadInfo.tileHeight;
return S_OK;
}
HRESULT CMapFile::get_TileSetManager(TileSetManager **tileSetManager)
{
this->tileSetManager->QueryInterface(IID_TileSetManager, (void**)tileSetManager);
return S_OK;
}
HRESULT CMapFile::get_MappingIndex(int tileX, int tileY, int *tileMappingIndex)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Get the tile mapping index
*tileMappingIndex = (tileData[tileOffset] & 0x0000FFE0) >> 5;
return S_OK;
}
HRESULT CMapFile::put_MappingIndex(int tileX, int tileY, int tileMappingIndex)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
// Check for invalid mapping index
if ((tileMappingIndex & ~0x7FF) != 0) // Only 11 bits allowed
return E_INVALIDARG;
// **TODO** Check if index is out of array bounds?
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Set the tile mapping index
tileData[tileOffset] = (tileData[tileOffset] & 0xFFFF001F) | (tileMappingIndex << 5);
return S_OK;
}
HRESULT CMapFile::Draw(int destDC, int sourcePixelX, int sourcePixelY, int pixelWidth, int pixelHeight)
{
int startXTile = sourcePixelX >> 5;
int startYTile = sourcePixelY >> 5;
int endXTile = (sourcePixelX+pixelWidth+31) >> 5;
int endYTile = (sourcePixelY+pixelHeight+31) >> 5;
int x, y;
int tileXUpper, tileXLower;
int tileOffset;
int tile;
int tileIndex;
TileSet *tileSet;
CTileSetManager::TileSetTileMapping *tileMap;
int destPixelX = -(sourcePixelX & 0x1F);
int destPixelY = -(sourcePixelY & 0x1F);
if (endYTile > mapHeadInfo.tileHeight)
endYTile = mapHeadInfo.tileHeight;
if (endXTile > tileWidth)
endXTile = tileWidth;
// Draw all tiles in visible range
for (y = startYTile; y < endYTile; y++)
{
for (x = startXTile; x < endXTile; x++)
{
// Calculate the offset of the tile
tileXUpper = x >> 5;
tileXLower = x & 0x1F;
tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + y) << 5) + tileXLower;
// Load the tile data
tile = tileData[tileOffset];
// Get the tile index
tileIndex = (tile & 0x0000FFE0) >> 5;
// Make sure the tileIndex is in range
if (tileIndex >= tileSetManager->numMappings)
tileIndex = 0;
// Get the tile mapping
tileMap = &tileSetManager->mapping[tileIndex];
// Paste the tile
tileSet = tileSetManager->tileSetInfo[tileMap->tileSetIndex].tileSet;
if (tileSet)
{
tileSet->PasteTile(destDC, destPixelX+((x-startXTile)*32), destPixelY+((y-startYTile)*32), tileMap->tileIndex);
}
}
}
return S_OK;
}
// MapFile functions
// *****************
HRESULT CMapFile::get_AroundTheWorld(int *bAroundTheWorld)
{
*bAroundTheWorld = (mapHeadInfo.lgTileWidth >= 9);
return S_OK;
}
HRESULT CMapFile::get_TileData(
/* [in] */ int tileX,
/* [in] */ int tileY,
/* [retval][out] */ int __RPC_FAR *tile)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Get the tileData result value
*tile = tileData[tileOffset];
return S_OK;
}
HRESULT CMapFile::put_TileData(int tileX, int tileY, int newTile)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
tileData[tileOffset] = newTile;
return S_OK;
}
HRESULT CMapFile::get_CellType(int tileX, int tileY, int *cellType)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Get the tileData result value
*cellType = tileData[tileOffset] & 0x1F;
return S_OK;
}
HRESULT CMapFile::put_CellType(int tileX, int tileY, int newCellType)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
// Check for invalid cell type
if ((newCellType & ~0x1F) != 0) // Only 5 bits allowed
return E_INVALIDARG;
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
tileData[tileOffset] = (tileData[tileOffset] & 0xFFFFFFE0) | (newCellType & 0x1F);
return S_OK;
}
HRESULT CMapFile::get_UnitIndex(int tileX, int tileY, int *unitIndex)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Get the tileData result value
*unitIndex = (tileData[tileOffset] >> 16) & 0x7FF;
return S_OK;
}
HRESULT CMapFile::put_UnitIndex(int tileX, int tileY, int newUnitIndex)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
// Check for invalid unit index
if ((newUnitIndex & ~0x7FF) != 0) // Only 11 bits allowed
return E_INVALIDARG;
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
tileData[tileOffset] = (tileData[tileOffset] & 0xF800FFFF) | (newUnitIndex << 16);
return S_OK;
}
// Bit flags
// *********
HRESULT CMapFile::get_Lava(int tileX, int tileY, int *lava)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Return the Lava presence bit
*lava = (tileData[tileOffset] >> 27) & 0x1;
return S_OK;
}
HRESULT CMapFile::put_Lava(int tileX, int tileY, int lava)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Set the Lava presence bit
tileData[tileOffset] |= (lava & 0x1) << 27;
return S_OK;
}
HRESULT CMapFile::get_LavaPossible(int tileX, int tileY, int *lavaPossible)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Return the LavaPossible bit
*lavaPossible = (tileData[tileOffset] >> 28) & 0x1;
return S_OK;
}
HRESULT CMapFile::put_LavaPossible(int tileX, int tileY, int lavaPossible)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Set the LavaPossible bit
tileData[tileOffset] |= (lavaPossible & 0x1) << 28;
return S_OK;
}
HRESULT CMapFile::get_Expand(int tileX, int tileY, int *expand)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Return the Lava/Microbe Expand presence bit
*expand = (tileData[tileOffset] >> 29) & 0x1;
return S_OK;
}
HRESULT CMapFile::put_Expand(int tileX, int tileY, int expand)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Set the Lava/Microbe Expand presence bit
tileData[tileOffset] |= (expand & 0x1) << 29;
return S_OK;
}
HRESULT CMapFile::get_Microbe(int tileX, int tileY, int *microbe)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Return the Microbe presence bit
*microbe = (tileData[tileOffset] >> 30) & 0x1;
return S_OK;
}
HRESULT CMapFile::put_Microbe(int tileX, int tileY, int microbe)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Set the Microbe presence bit
tileData[tileOffset] |= (microbe & 0x1) << 30;
return S_OK;
}
HRESULT CMapFile::get_WallOrBuilding(int tileX, int tileY, int *wallOrBuilding)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Return the Wall or Building presence bit (Non-moveable tile)
*wallOrBuilding = (tileData[tileOffset] >> 31) & 0x1;
return S_OK;
}
HRESULT CMapFile::put_WallOrBuilding(int tileX, int tileY, int wallOrBuilding)
{
// Error check array indicies
if ((tileX < 0) || (tileX >= tileWidth) || (tileY < 0) || (tileY >= mapHeadInfo.tileHeight))
{
PostErrorMsg(L"Invalid tile location");
return E_INVALIDARG;
}
int tileXUpper = tileX >> 5;
int tileXLower = tileX & 0x1F;
int tileOffset = (((tileXUpper*mapHeadInfo.tileHeight) + tileY) << 5) + tileXLower;
// Set the Wall or Building presence bit (Non-moveable tile)
tileData[tileOffset] |= (wallOrBuilding & 0x1) << 31;
return S_OK;
}
// Copy and Paste
// **************
HRESULT CMapFile::Copy(int tileX1, int tileY1, int tileX2, int tileY2, TileGroup **tileGroup)
{
int copyTileWidth = tileX2 - tileX1 + 1;
int copyTileHeight = tileY2 - tileY1 + 1;
CTileGroup *copyTileGroup;
TileSetManager *copyTileSetManager;
// Create an object to hold the copied tile data
*tileGroup = NULL;
copyTileGroup = new CTileGroup(copyTileWidth, copyTileHeight, tileSource);
// Check for errors creating object
if (copyTileGroup == NULL)
return E_OUTOFMEMORY;
// Obtain the desired interface
copyTileGroup->QueryInterface(IID_TileGroup, (void**)tileGroup);
copyTileGroup->Release();
// Check for errors
if (*tileGroup == NULL)
return E_FAIL;
// Obtain the new tile group's tile set manager
(*tileGroup)->get_TileSetManager(©TileSetManager);
// Allocate space for compatibility maps
// *************************************
int *map; // Compatibility map for mapping array
int *tileSetMap; // Compatibility map for tile set array
int sourceMappings; // Number of mapping entries in source tile set manager
int destMappings; // Current number of mapping entries in dest tile set manager
int sourceTileSets; // Number of tile sets in source tile set manager
int destTileSets; // Current number of tile sets in dest tile set manager
// Initialize variables
tileSetManager->get_NumMappings(&sourceMappings);
destMappings = 0;
tileSetManager->get_NumTileSets(&sourceTileSets);
destTileSets = 0;
// Allocate space for a compatibility map and tile set map
map = new int[sourceMappings];
tileSetMap = new int[sourceTileSets];
// Check for memory allocation errors
if ((map == NULL) || (tileSetMap == NULL))
{
// Failed to allocate memory. Cleanup and abort.
if (map != NULL)
delete [] map;
if (tileSetMap != NULL)
delete [] tileSetMap;
copyTileSetManager->Release();
(*tileGroup)->Release();
return E_OUTOFMEMORY;
}
// Initialize all entries to unused
memset(map, -1, sourceMappings*4);
memset(tileSetMap, -1, sourceTileSets*4);
// Translate and copy tile data
// ****************************
int x, y;
int mappingIndex;
int tileSetIndex;
int tileSetTileIndex;
int numTileReplacements;
int cycleDelay;
BSTR tileSetName;
// Create the compatibilty map and copy data
for (y = tileY1; y <= tileY2; y++)
{
for (x = tileX1; x <= tileX2; x++)
{
// Get the tile mapping index
get_MappingIndex(x, y, &mappingIndex);
// Check if compatibility map entry is not defined
if (map[mappingIndex] == -1)
{
// Try to define this entry
// Ensure a tile set mapping exists
tileSetManager->get_TileSetIndex(mappingIndex, &tileSetIndex);
// Check if tile set mapping is undefined
if (tileSetMap[tileSetIndex] == -1)
{
// Get the tile set name
tileSetName = NULL;
tileSetManager->get_TileSetName(tileSetIndex, &tileSetName);
// Load the tile set and obtain the index
copyTileSetManager->AddTileSet(tileSetName, &tileSetMap[tileSetIndex]);
// Free the tile set name string
SysFreeString(tileSetName);
}
// Get the tile set tile index
tileSetManager->get_TileSetTileIndex(mappingIndex, &tileSetTileIndex);
// Fill in a new mapping entry
copyTileSetManager->MapInTiles(tileSetMap[tileSetIndex], tileSetTileIndex, 1, &map[mappingIndex]);
// Copy the animation properties
tileSetManager->get_NumTileReplacements(mappingIndex, &numTileReplacements);
tileSetManager->get_CycleDelay(mappingIndex, &cycleDelay);
copyTileSetManager->put_NumTileReplacements(map[mappingIndex], numTileReplacements);
copyTileSetManager->put_CycleDelay(map[mappingIndex], cycleDelay);
}
// Copy the tile data
(*tileGroup)->put_MappingIndex(x-tileX1, y-tileY1, map[mappingIndex]);
}
}
// Cleanup and exit
delete [] map;
delete [] tileSetMap;
return S_OK;
}
HRESULT CMapFile::Paste(int tileX, int tileY, TileGroup *tileGroup)
{
TileSetManager *sourceTileSetManager;
int sourceTileSets;
int sourceMappings;
int *map;
int *tileSetMap;
bool *newTileSetMap;
int tileSetIndex;
int tileIndex;
int numTileReplacements;
int cycleDelay;
int i, j, mappingIndex;
int sourceWidth, sourceHeight;
int numNewMappings;
HRESULT retval;
BSTR tileSetName = NULL;
// Get the source TileSetManager
tileGroup->get_TileSetManager(&sourceTileSetManager);
// Get the number of tile sets and tile mappings from the source
sourceTileSetManager->get_NumTileSets(&sourceTileSets);
sourceTileSetManager->get_NumMappings(&sourceMappings);
// Allocate space for a compatibility map and tile set map
map = new int[sourceMappings];
tileSetMap = new int[sourceTileSets];
newTileSetMap = new bool[sourceTileSets];
// Check for memory allocation errors
if ((map == NULL) || (tileSetMap == NULL) || (newTileSetMap == NULL))
{
// Failed to allocate memory. Cleanup and abort.
if (map != NULL)
delete [] map;
if (tileSetMap != NULL)
delete [] tileSetMap;
if (newTileSetMap != NULL)
delete [] newTileSetMap;
sourceTileSetManager->Release();
return E_OUTOFMEMORY;
}
// Initialize all entries to unused
memset(map, -1, sourceMappings*4);
memset(tileSetMap, -1, sourceTileSets*4);
memset(newTileSetMap, false, sourceTileSets*sizeof(bool));
try
{
// Try to load all the tile sets and create a tile set mapping
for (i = 0; i < sourceTileSets; i++)
{
// Get the tile set name
sourceTileSetManager->get_TileSetName(i, &tileSetName);
// Try to load the tile set
retval = tileSetManager->AddTileSet(tileSetName, &tileSetMap[i]);
// Check for errors loading the tile set
if (FAILED(retval))
{
// Check if the error was not a duplicate tile set
if (tileSetMap[i] < 0)
{
// Error loading tile set. Abort
throw L"Could not load all needed tile sets for paste operation.";
}
}
else
{
newTileSetMap[i] = true; // A new tile set was successfully loaded
}
}
// Create (initial) compatability map and count number of new mappings needed
numNewMappings = 0;
for (i = 0; i < sourceMappings; i++)
{
// Get the attributes for this mapping index
sourceTileSetManager->get_TileSetIndex(i, &tileSetIndex);
sourceTileSetManager->get_TileSetTileIndex(i, &tileIndex);
sourceTileSetManager->get_NumTileReplacements(i, &numTileReplacements);
sourceTileSetManager->get_CycleDelay(i, &cycleDelay);
// Lookup index of dest mapping index
tileSetManager->GetMappingIndex(tileSetMap[tileSetIndex], tileIndex, numTileReplacements, cycleDelay, &map[i]);
// Check if a new mapping needs to be created
if (map[i] < 0)
numNewMappings++; // A new mapping will be needed for this entry
}
// Check if there is not enough room for the new mappings
if (tileSetManager->numMappings + numNewMappings >= 2048)
throw L"Not enough free mapping entries to perform the paste operation.";
// Add the needed mapping entries
for (i = 0; i < sourceMappings; i++)
{
// Check if this entry still needs to be filled in
if (map[i] < 0)
{
// Get the attributes for this mapping index
sourceTileSetManager->get_TileSetIndex(i, &tileSetIndex);
sourceTileSetManager->get_TileSetTileIndex(i, &tileIndex);
sourceTileSetManager->get_NumTileReplacements(i, &numTileReplacements);
sourceTileSetManager->get_CycleDelay(i, &cycleDelay);
// Add a new mapping entry
tileSetManager->MapInTiles(tileSetMap[tileSetIndex], tileIndex, 1, &map[i]);
// Fill in the other entries
tileSetManager->put_NumTileReplacements(map[i], numTileReplacements);
tileSetManager->put_CycleDelay(map[i], cycleDelay);
}
}
// Copy the tile data
tileGroup->get_TileWidth(&sourceWidth);
tileGroup->get_TileHeight(&sourceHeight);
// Clip coordinates to destination map size
if (tileX + sourceWidth >= tileWidth)
sourceWidth = tileWidth - tileX;
if (tileY + sourceHeight >= mapHeadInfo.tileHeight)
sourceHeight = mapHeadInfo.tileHeight - tileY;
for (j = 0; j < sourceHeight; j++)
{
for (i = 0; i < sourceWidth; i++)
{
// Get the mapping index
tileGroup->get_MappingIndex(i, j, &mappingIndex);
// Set the dest mapping index
put_MappingIndex(tileX + i, tileY + j, map[mappingIndex]);
}
}
}
catch(WCHAR *errorMsg)
{
// Unload new tile sets
for (i = 0; i < sourceTileSets; i++)
{
// Check if this tile set is new
if (newTileSetMap[i] == true)
{
// Get the tile set name
sourceTileSetManager->get_TileSetName(i, &tileSetName);
// Unload the tile set
tileSetManager->RemoveTileSet(tileSetName, &tileSetMap[i]);
}
}
// Release memory
SysFreeString(tileSetName);
delete [] map;
delete [] tileSetMap;
delete [] newTileSetMap;
sourceTileSetManager->Release();
// Return a COM error message
PostErrorMsg(errorMsg);
return E_FAIL; // Failed to complete paste operation
}
catch(...)
{
// Unexpected error
return E_FAIL;
}
// Release memory
SysFreeString(tileSetName);
delete [] map;
delete [] tileSetMap;
delete [] newTileSetMap;
sourceTileSetManager->Release();
// Success
return S_OK;
}
HRESULT CMapFile::get_NumTileGroups(int *numTileGroups)
{
*numTileGroups = this->numTileGroups;
return S_OK;
}
HRESULT CMapFile::get_TileGroup(int tileGroupIndex, TileGroup **tileGroup)
{
*tileGroup = NULL;
// Error check index range
if ((tileGroupIndex < 0) || (tileGroupIndex >= numTileGroups))
return E_INVALIDARG;
*tileGroup = tileGroupInfo[tileGroupIndex].tileGroup;
if (tileGroup != NULL)
(*tileGroup)->AddRef();
return S_OK;
}
HRESULT CMapFile::get_TileGroupName(int tileGroupIndex, BSTR *tileGroupName)
{
// Error check index range
if ((tileGroupIndex < 0) || (tileGroupIndex >= numTileGroups))
return E_INVALIDARG;
// Reallocate space for the returned string
int nameLen = tileGroupInfo[tileGroupIndex].nameLen;
SysReAllocStringLen(tileGroupName, NULL, nameLen);
// Convert the name to unicode
MultiByteToWideChar(
CP_ACP,
0,
tileGroupInfo[tileGroupIndex].name,
nameLen,
*tileGroupName,
nameLen
);
return S_OK;
}
HRESULT CMapFile::put_TileGroupName(int tileGroupIndex, BSTR tileGroupName)
{
// Error check index range
if ((tileGroupIndex < 0) || (tileGroupIndex >= numTileGroups))
return E_INVALIDARG;
// Get the length of the new name string
int nameLen = SysStringLen(tileGroupName);
// Allocate space for the name
char *name = new char[nameLen+1];
// Check for allocation errors
if (name == NULL)
return E_OUTOFMEMORY;
// Convert the BSTR to ASCII
WideCharToMultiByte(CP_ACP, 0, tileGroupName, nameLen, name, nameLen, 0, 0);
// NULL terminate it
name[nameLen] = 0;
// Replace the string
delete [] tileGroupInfo[tileGroupIndex].name;
tileGroupInfo[tileGroupIndex].name = name;
tileGroupInfo[tileGroupIndex].nameLen = nameLen;
return S_OK;
}
// **TODO** This function needs to load tile sets into the map's tile set manager
// if needed and also create any needed mapping entries.
HRESULT CMapFile::AddTileGroup(TileGroup *newTileGroup)
{
return E_NOTIMPL;
TileGroupInfo *newTileGroupInfo;
// Try to extend the TileGroupInfo array
newTileGroupInfo = new TileGroupInfo[numTileGroups + 1];
// Check for memory allocation errors
if (newTileGroupInfo == NULL)
return E_OUTOFMEMORY;
// Copy the data to the new array
memcpy(newTileGroupInfo, tileGroupInfo, sizeof(tileGroupInfo[0])*numTileGroups);
// Release the old array
delete [] tileGroupInfo;
// Replace the array with the new one
tileGroupInfo = newTileGroupInfo;
// Store the tile group to the new entry
tileGroupInfo[numTileGroups].nameLen = 0;
tileGroupInfo[numTileGroups].name = NULL;
tileGroupInfo[numTileGroups].tileGroup = newTileGroup;
newTileGroup->AddRef();
// Update number of tile groups
numTileGroups++;
return E_NOTIMPL;
}
HRESULT CMapFile::RemoveTileGroup(int tileGroupIndex)
{
// Error check index range
if ((tileGroupIndex < 0) || (tileGroupIndex >= numTileGroups))
return E_INVALIDARG;
// **TODO**
return E_NOTIMPL;
}
int CMapFile::LoadMap(StreamReader *stream, TileSetSource *tileSetSource, int loadFlags)
{
// **NOTE**: Changed the 'format errors' to just post an error rather than throw an exception.
// Although they can cause problems, it shouldn't prevent the map from loading.
int numBytesRead;
int status;
int temp;
try
{
// Check if saved game header is to be loaded from the file
if ((loadFlags & SavedGameHeader) == SavedGameHeader)
{
// Load a saved game file header **TODO**
}
// Load the .map file header
stream->Read(sizeof(mapHeadInfo), (int)&mapHeadInfo, &numBytesRead);
// Check for errors
stream->get_Status(&status);
if (status)
throw L"Read error. Could not read map header data."; // Failed. Error reading file.
if (mapHeadInfo.tag < 0x1010)
throw L"Format error: Invalid map header tag.";
// Update map header fields
// Round height up to nearest power of 2
int &x = mapHeadInfo.tileHeight;
x = x - 1;
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >>16);
x++;
tileWidth = 1 << mapHeadInfo.lgTileWidth; // Calculate map width
// Allocate space for tile data
tileData = new int[tileWidth*mapHeadInfo.tileHeight];
// Read in the tile data
stream->Read(tileWidth*mapHeadInfo.tileHeight*4, (int)tileData, &numBytesRead);
// Check for errors
stream->get_Status(&status);
if (status)
throw L"Read error. Could not read map tile data."; // Failed. Error reading file.
// Read clipping rect
stream->Read(sizeof(clipRect), (int)&clipRect, &numBytesRead);
// Load Tile Set mapping info
tileSetManager = new CTileSetManager(tileSetSource, mapHeadInfo.numTileSets, stream);
// Read tag
stream->Read(4, (int)&temp, &numBytesRead);
// Check if tag matches the opening tag of the file
if (temp != mapHeadInfo.tag)
throw L"Format error: Tag did not match header tag.";
// Check if Unit data is to be loaded from the file
if ((loadFlags & Units) == Units)
{
// Load Unit data **TODO**
}
// Read tag
stream->Read(4, (int)&temp, &numBytesRead);
// Check if tag matches the opening tag of the file
if (temp != mapHeadInfo.tag)
throw L"Format error: Tag did not match header tag.";
// Check if tile groups are to be loaded from the file
if ((loadFlags & TileGroups) == TileGroups)
{
// Load tile groups from file
// **************************
int i;
// Load the header data
stream->Read(4, (int)&numTileGroups, &numBytesRead);
stream->Read(4, (int)&temp, &numBytesRead);
// Allocate space for the tile group info
tileGroupInfo = new TileGroupInfo[numTileGroups];
// Initialize the array
memset(tileGroupInfo, 0, sizeof(tileGroupInfo[0])*numTileGroups);
// Read all group info
for (i = 0; i < numTileGroups; i++)
{
int width, height;
int x, y;
// Read the dimensions
stream->Read(4, (int)&width, &numBytesRead);
stream->Read(4, (int)&height, &numBytesRead);
// Create a new TileGroup
tileGroupInfo[i].tileGroup = new CTileGroup(width, height, tileSetManager);
// Read in the tile data
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{