-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.cs
1829 lines (1471 loc) · 61.7 KB
/
Map.cs
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
/* Map */
namespace SharpDune;
/* Types of available landscapes. */
enum LandscapeType
{
LST_NORMAL_SAND = 0, /*<! Flat sand. */
LST_PARTIAL_ROCK = 1, /*!< Edge of a rocky area (mostly sand). */
LST_ENTIRELY_DUNE = 2, /*!< Entirely sand dunes. */
LST_PARTIAL_DUNE = 3, /*!< Partial sand dunes. */
LST_ENTIRELY_ROCK = 4, /*!< Center part of rocky area. */
LST_MOSTLY_ROCK = 5, /*!< Edge of a rocky area (mostly rocky). */
LST_ENTIRELY_MOUNTAIN = 6, /*!< Center part of the mountain. */
LST_PARTIAL_MOUNTAIN = 7, /*!< Edge of a mountain. */
LST_SPICE = 8, /*!< Sand with spice. */
LST_THICK_SPICE = 9, /*!< Sand with thick spice. */
LST_CONCRETE_SLAB = 10, /*!< Concrete slab. */
LST_WALL = 11, /*!< Wall. */
LST_STRUCTURE = 12, /*!< Structure. */
LST_DESTROYED_WALL = 13, /*!< Destroyed wall. */
LST_BLOOM_FIELD = 14, /*!< Bloom field. */
LST_MAX = 15
}
/*
* A Tile as stored in the memory in the map.
*/
[StructLayout(LayoutKind.Explicit)]
class CTile
{
/* 0000 01FF PACK */
[FieldOffset(0)] internal ushort groundTileID; /*!< The "Icon" which is drawn on this Tile. */
/* 0000 FE00 PACK */
[FieldOffset(2)] internal ushort overlayTileID; /*!< The Overlay which is drawn over this Tile. */
/* 0007 0000 PACK */
[FieldOffset(4)] internal byte houseID; /*!< Which House owns this Tile. */
/* 0008 0000 PACK */
[FieldOffset(5)] internal bool isUnveiled; /*!< There is no fog on the Tile. */
/* 0010 0000 PACK */
[FieldOffset(6)] internal bool hasUnit; /*!< There is a Unit on the Tile. */
/* 0020 0000 PACK */
[FieldOffset(7)] internal bool hasStructure; /*!< There is a Structure on the Tile. */
/* 0040 0000 PACK */
[FieldOffset(8)] internal bool hasAnimation; /*!< There is animation going on the Tile. */
/* 0080 0000 PACK */
[FieldOffset(9)] internal bool hasExplosion; /*!< There is an explosion on the Tile. */
/* FF00 0000 PACK */
[FieldOffset(10)] internal ushort index; /*!< Index of the Structure / Unit (index 1 is Structure/Unit 0, etc). */
}
/*
* Information about LandscapeType.
*/
class LandscapeInfo
{
internal byte[] movementSpeed = new byte[6]; /*!< Per MovementType the speed a Unit has on this LandscapeType. */
internal bool letUnitWobble; /*!< True if a Unit on this LandscapeType should wobble around while moving on it. */
internal bool isValidForStructure; /*!< True if a Structure with notOnConcrete false can be build on this LandscapeType. */
internal bool isSand; /*!< True if the LandscapeType is a sand tile (sand, dune, spice, thickspice, bloom). */
internal bool isValidForStructure2; /*!< True if a Structure with notOnConcrete true can be build on this LandscapeType. */
internal bool canBecomeSpice; /*!< True if the LandscapeType can become a spice tile. */
internal byte craterType; /*!< Type of crater on tile; 0 for none, 1 for sand, 2 for concrete. */
internal ushort radarColour; /*!< Colour used on radar for this LandscapeType. */
internal ushort spriteID; /*!< Sprite used on map for this LandscapeType. */
}
/* Definition of the map size of a map scale. */
class MapInfo
{
internal ushort minX; /*!< Minimal X position of the map. */
internal ushort minY; /*!< Minimal Y position of the map. */
internal ushort sizeX; /*!< Width of the map. */
internal ushort sizeY; /*!< Height of the map. */
}
static class Map
{
internal static ushort[] g_mapTileID = new ushort[64 * 64];
internal static CTile[] g_map = new CTile[64 * 64]; /*!< All map data. */
internal static byte[][] g_functions = [ //[3][3]
[0, 1, 0], [2, 3, 0], [0, 1, 0]
];
internal static ushort g_changedTilesCount;
internal static ushort[] g_changedTiles = new ushort[200];
internal static byte[] g_changedTilesMap = new byte[512];
static readonly bool s_debugNoExplosionDamage; /*!< When non-zero, explosions do no damage to their surrounding. */
internal static byte[] g_dirtyMinimap = new byte[512];
internal static byte[] g_displayedMinimap = new byte[512];
internal static byte[] g_dirtyViewport = new byte[512];
internal static byte[] g_displayedViewport = new byte[512];
/*
* Map definitions.
* Map sizes: [0] is 62x62, [1] is 32x32, [2] is 21x21.
*/
internal static MapInfo[] g_mapInfos = [ //[3]
new() { minX = 1, minY = 1, sizeX = 62, sizeY = 62 },
new() { minX = 16, minY = 16, sizeX = 32, sizeY = 32 },
new() { minX = 21, minY = 21, sizeX = 21, sizeY = 21 }
];
internal static ushort g_dirtyViewportCount;
internal static bool g_selectionRectangleNeedRepaint;
/*
* Type of landscape for the landscape sprites.
*
* 0=normal sand, 1=partial rock, 5=mostly rock, 4=entirely rock,
* 3=partial sand dunes, 2=entirely sand dunes, 7=partial mountain,
* 6=entirely mountain, 8=spice, 9=thick spice
* @see Map_GetLandscapeType
*/
static readonly ushort[] _landscapeSpriteMap = [
0, 1, 1, 1, 5, 1, 5, 5, 5, 5, /* Sprites 127-136 */
5, 5, 5, 5, 5, 5, 4, 3, 3, 3, /* Sprites 137-146 */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* Sprites 147-156 */
3, 3, 2, 7, 7, 7, 7, 7, 7, 7, /* Sprites 157-166 */
7, 7, 7, 7, 7, 7, 7, 7, 6, 8, /* Sprites 167-176 */
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, /* Sprites 177-186 */
8, 8, 8, 8, 8, 9, 9, 9, 9, 9, /* Sprites 187-196 */
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, /* Sprites 197-206 */
9, /* Sprite 207 (bloom sprites 208 and 209 are already caught). */
];
static readonly ushort[][][] _offsetTable = [ //[2][21][4]
[
[0, 0, 4, 0], [4, 0, 4, 4], [0, 0, 0, 4], [0, 4, 4, 4], [0, 0, 0, 2],
[0, 2, 0, 4], [0, 0, 2, 0], [2, 0, 4, 0], [4, 0, 4, 2], [4, 2, 4, 4],
[0, 4, 2, 4], [2, 4, 4, 4], [0, 0, 4, 4], [2, 0, 2, 2], [0, 0, 2, 2],
[4, 0, 2, 2], [0, 2, 2, 2], [2, 2, 4, 2], [2, 2, 0, 4], [2, 2, 4, 4],
[2, 2, 2, 4]
],
[
[0, 0, 4, 0], [4, 0, 4, 4], [0, 0, 0, 4], [0, 4, 4, 4], [0, 0, 0, 2],
[0, 2, 0, 4], [0, 0, 2, 0], [2, 0, 4, 0], [4, 0, 4, 2], [4, 2, 4, 4],
[0, 4, 2, 4], [2, 4, 4, 4], [4, 0, 0, 4], [2, 0, 2, 2], [0, 0, 2, 2],
[4, 0, 2, 2], [0, 2, 2, 2], [2, 2, 4, 2], [2, 2, 0, 4], [2, 2, 4, 4],
[2, 2, 2, 4]
]
];
/*
* Get type of landscape of a tile.
*
* @param packed The packed tile to examine.
* @return The type of landscape at the tile.
*/
internal static ushort Map_GetLandscapeType(ushort packed)
{
CTile t;
short spriteOffset;
t = g_map[packed];
if (t.groundTileID == g_builtSlabTileID) return (ushort)LandscapeType.LST_CONCRETE_SLAB;
if (t.groundTileID == g_bloomTileID || t.groundTileID == g_bloomTileID + 1) return (ushort)LandscapeType.LST_BLOOM_FIELD;
if (t.groundTileID > g_wallTileID && t.groundTileID < g_wallTileID + 75) return (ushort)LandscapeType.LST_WALL;
if (t.overlayTileID == g_wallTileID) return (ushort)LandscapeType.LST_DESTROYED_WALL;
if (Structure_Get_ByPackedTile(packed) != null) return (ushort)LandscapeType.LST_STRUCTURE;
spriteOffset = (short)(t.groundTileID - g_landscapeTileID); /* Offset in the landscape icon group. */
if (spriteOffset is < 0 or > 80) return (ushort)LandscapeType.LST_ENTIRELY_ROCK;
return _landscapeSpriteMap[spriteOffset];
}
/*
* Sets the selection on given packed tile.
*
* @param packed The packed tile to set the selection on.
*/
internal static void Map_SetSelection(ushort packed)
{
if (g_selectionType == (ushort)SelectionType.TARGET) return;
if (g_selectionType == (ushort)SelectionType.PLACE)
{
g_selectionState = Structure_IsValidBuildLocation(packed, (StructureType)g_structureActiveType);
g_selectionPosition = packed;
return;
}
if ((packed != 0xFFFF && g_map[packed].overlayTileID != g_veiledTileID) || g_debugScenario)
{
CStructure s;
s = Structure_Get_ByPackedTile(packed);
if (s != null)
{
StructureInfo si;
si = g_table_structureInfo[s.o.type];
if (s.o.houseID == (byte)g_playerHouseID && g_selectionType != (ushort)SelectionType.MENTAT)
{
GUI_DisplayHint(si.o.hintStringID, si.o.spriteID);
}
packed = Tile_PackTile(s.o.position);
Map_SetSelectionSize(si.layout);
Structure_UpdateMap(s);
}
else
{
Map_SetSelectionSize((ushort)StructureLayout.STRUCTURE_LAYOUT_1x1);
}
if (g_selectionType != (ushort)SelectionType.TARGET)
{
CUnit u;
u = Unit_Get_ByPackedTile(packed);
if (u != null)
{
if (u.o.type != (byte)UnitType.UNIT_CARRYALL)
{
Unit_Select(u);
}
}
else
{
if (g_unitSelected != null)
{
Unit_Select(null);
}
}
}
g_selectionPosition = packed;
return;
}
Map_SetSelectionSize((ushort)StructureLayout.STRUCTURE_LAYOUT_1x1);
g_selectionPosition = packed;
return;
}
/*
* Checks if the given position is inside the map.
*
* @param position The tile (packed) to check.
* @return True if the position is valid.
*/
internal static bool Map_IsValidPosition(ushort position)
{
ushort x, y;
MapInfo mapInfo;
if ((position & 0xC000) != 0) return false;
x = Tile_GetPackedX(position);
y = Tile_GetPackedY(position);
mapInfo = g_mapInfos[g_scenario.mapScale];
return (mapInfo.minX <= x && x < (mapInfo.minX + mapInfo.sizeX) && mapInfo.minY <= y && y < (mapInfo.minY + mapInfo.sizeY));
}
static ushort selectionLayout;
/*
* Sets the selection size for the given layout.
*
* @param layout The layout to determine selection size from.
* @return The previous layout.
* @see StructureLayout
*/
internal static ushort Map_SetSelectionSize(ushort layout)
{
ushort oldLayout;
ushort oldPosition;
oldLayout = selectionLayout;
if ((layout & 0x80) != 0) return oldLayout;
oldPosition = Map_SetSelectionObjectPosition(0xFFFF);
selectionLayout = layout;
g_selectionWidth = g_table_structure_layoutSize[layout].width;
g_selectionHeight = g_table_structure_layoutSize[layout].height;
Map_SetSelectionObjectPosition(oldPosition);
return oldLayout;
}
static ushort selectionPosition = 0xFFFF;
/*
* Sets the selection object to the given position.
*
* @param packed The position to set.
* @return The previous position.
*/
internal static ushort Map_SetSelectionObjectPosition(ushort packed)
{
ushort oldPacked;
oldPacked = selectionPosition;
if (packed == oldPacked) return oldPacked;
Map_InvalidateSelection(oldPacked, false);
if (packed != 0xFFFF) Map_InvalidateSelection(packed, true);
selectionPosition = packed;
return oldPacked;
}
static void Map_InvalidateSelection(ushort packed, bool enable)
{
ushort x, y;
if (packed == 0xFFFF) return;
for (y = 0; y < g_selectionHeight; y++)
{
for (x = 0; x < g_selectionWidth; x++)
{
ushort curPacked;
curPacked = (ushort)(packed + Tile_PackXY(x, y));
Map_Update(curPacked, 0, false);
if (enable)
{
BitArray_Set(g_displayedViewport, curPacked);
}
else
{
BitArray_Clear(g_displayedViewport, curPacked);
}
}
}
}
static readonly short[] offsets = [
-64, /* up */
-63, /* up right */
1, /* right */
65, /* down rigth */
64, /* down */
63, /* down left */
-1, /* left */
-65, /* up left */
0
];
/*
* Updates ??.
*
* @param packed The packed tile.
* @param type The type of update.
* @param ignoreInvisible Wether to ignore tile visibility check.
*/
internal static void Map_Update(ushort packed, ushort type, bool ignoreInvisible)
{
if (!ignoreInvisible && !Map_IsTileVisible(packed)) return;
switch (type)
{
default:
case 0:
{
byte i;
ushort curPacked = 0;
if (BitArray_Test(g_dirtyMinimap, packed)) return;
g_dirtyViewportCount++;
for (i = 0; i < 9; i++)
{
curPacked = (ushort)((packed + offsets[i]) & 0xFFF);
BitArray_Set(g_dirtyViewport, curPacked);
if (BitArray_Test(g_displayedViewport, curPacked)) g_selectionRectangleNeedRepaint = true;
}
BitArray_Set(g_dirtyMinimap, curPacked);
return;
}
case 1:
case 2:
case 3:
BitArray_Set(g_dirtyViewport, packed);
return;
}
}
/*
* Check if a position is unveiled (the fog is removed).
*
* @param position For which position to check.
* @return True if and only if the position is unveiled.
*/
internal static bool Map_IsPositionUnveiled(ushort position)
{
CTile t;
if (g_debugScenario) return true;
t = g_map[position];
if (!t.isUnveiled) return false;
if (!Tile_IsUnveiled(t.overlayTileID)) return false;
return true;
}
/*
* Checks wether a packed tile is visible in the viewport.
*
* @param packed The packed tile.
* @return True if the tile is visible.
*/
static bool Map_IsTileVisible(ushort packed)
{
byte x, y;
byte x2, y2;
x = (byte)Tile_GetPackedX(packed);
y = (byte)Tile_GetPackedY(packed);
x2 = (byte)Tile_GetPackedX(g_minimapPosition);
y2 = (byte)Tile_GetPackedY(g_minimapPosition);
return x >= x2 && x < x2 + 15 && y >= y2 && y < y2 + 10;
}
static readonly ushort[] tileOffsets = [
0x0080, 0x0088, 0x0090, 0x0098,
0x00A0, 0x00A8, 0x00B0, 0x00B8,
0x00C0, 0x00C8, 0x00D0, 0x00D8,
0x00E0, 0x00E8, 0x00F0, 0x00F8,
0x0100, 0x0180
];
/*
* Around a position, run a certain function for all tiles within a certain radius.
*
* @note Radius is in a 1/4th of a tile unit.
*
* @param radius The radius of the to-update tiles.
* @param position The position to go from.
* @param unit The unit to update for (can be NULL if function < 2).
* @param function The function to call.
*/
internal static void Map_UpdateAround(ushort radius, Tile32 position, CUnit unit, byte function)
{
short i, j;
var diff = new Tile32();
ushort lastPacked;
if (radius == 0 || (position.x == 0 && position.y == 0)) return;
radius--;
/* If radius is bigger or equal than 32, update all tiles in a 5x5 grid around the unit. */
if (radius >= 32)
{
ushort x = Tile_GetPosX(position);
ushort y = Tile_GetPosY(position);
for (i = -2; i <= 2; i++)
{
for (j = -2; j <= 2; j++)
{
ushort curPacked;
if (x + i < 0 || x + i >= 64 || y + j < 0 || y + j >= 64) continue;
curPacked = Tile_PackXY((ushort)(x + i), (ushort)(y + j));
BitArray_Set(g_dirtyViewport, curPacked);
g_dirtyViewportCount++;
switch (function)
{
case 0: Map_Update(curPacked, 0, false); break;
case 1: Map_Update(curPacked, 3, false); break;
case 2: Unit_RemoveFromTile(unit, curPacked); break;
case 3: Unit_AddToTile(unit, curPacked); break;
default: break;
}
}
}
return;
}
radius = Math.Max(radius, (ushort)15);
position.x -= tileOffsets[radius - 15];
position.y -= tileOffsets[radius - 15];
diff.x = 0;
diff.y = 0;
lastPacked = 0;
i = 0;
do
{
var curTile = Tile_AddTileDiff(position, diff);
if (Tile_IsValid(curTile))
{
var curPacked = Tile_PackTile(curTile);
if (curPacked != lastPacked)
{
BitArray_Set(g_dirtyViewport, curPacked);
g_dirtyViewportCount++;
switch (function)
{
case 0: Map_Update(curPacked, 0, false); break;
case 1: Map_Update(curPacked, 3, false); break;
case 2: Unit_RemoveFromTile(unit, curPacked); break;
case 3: Unit_AddToTile(unit, curPacked); break;
default: break;
}
lastPacked = curPacked;
}
}
if (i == 8) break;
diff = g_table_tilediff[radius + 1][i++];
} while ((diff.x != 0) || (diff.y != 0));
}
/*
* Mark a specific tile as dirty, so it gets a redrawn next time.
*
* @param packed The tile to mark as dirty.
*/
internal static void Map_MarkTileDirty(ushort packed)
{
if (BitArray_Test(g_displayedMinimap, packed) && g_scenario.mapScale + 1 == 0) return;
BitArray_Set(g_changedTilesMap, packed);
if (g_changedTilesCount < g_changedTiles.Length /*Common.lengthof<ushort>(g_changedTiles)*/) g_changedTiles[g_changedTilesCount++] = packed;
}
/*
* Unveil a tile for a House.
* @param packed The tile to unveil.
* @param houseID The house to unveil for.
* @return True if tile was freshly unveiled.
*/
internal static bool Map_UnveilTile(ushort packed, byte houseID)
{
CStructure s;
CUnit u;
CTile t;
if (houseID != (byte)g_playerHouseID) return false;
if (Tile_IsOutOfMap(packed)) return false;
t = g_map[packed];
if (t.isUnveiled && Tile_IsUnveiled(t.overlayTileID)) return false;
t.isUnveiled = true;
Map_MarkTileDirty(packed);
u = Unit_Get_ByPackedTile(packed);
if (u != null) Unit_HouseUnitCount_Add(u, houseID);
s = Structure_Get_ByPackedTile(packed);
if (s != null)
{
var sv = s;
sv.o.seenByHouses |= (byte)(1 << houseID);
if (houseID == (byte)HouseType.HOUSE_ATREIDES) sv.o.seenByHouses |= 1 << (byte)HouseType.HOUSE_FREMEN;
}
Map_UnveilTile_Neighbour(packed);
Map_UnveilTile_Neighbour((ushort)(packed + 1));
Map_UnveilTile_Neighbour((ushort)(packed - 1));
Map_UnveilTile_Neighbour((ushort)(packed - 64));
Map_UnveilTile_Neighbour((ushort)(packed + 64));
return true;
}
/*
* After unveiling, check neighbour tiles. This function handles one neighbour.
* @param packed The neighbour tile of an unveiled tile.
*/
static void Map_UnveilTile_Neighbour(ushort packed)
{
ushort tileID;
CTile t;
if (Tile_IsOutOfMap(packed)) return;
t = g_map[packed];
tileID = 15;
if (t.isUnveiled)
{
int i;
if (g_validateStrictIfZero == 0 && Tile_IsUnveiled(t.overlayTileID)) return;
tileID = 0;
for (i = 0; i < 4; i++)
{
var neighbour = (ushort)(packed + g_table_mapDiff[i]);
if (Tile_IsOutOfMap(neighbour) || !g_map[neighbour].isUnveiled)
{
tileID |= (ushort)(1 << i);
}
}
}
if (tileID != 0)
{
if (tileID != 15)
{
var u = Unit_Get_ByPackedTile(packed);
if (u != null) Unit_HouseUnitCount_Add(u, (byte)g_playerHouseID);
}
tileID = g_iconMap[g_iconMap[(int)IconMapEntries.ICM_ICONGROUP_FOG_OF_WAR] + tileID];
}
t.overlayTileID = tileID;
Map_Update(packed, 0, false);
}
/* Border tiles of the viewport relative to the top-left. */
static readonly ushort[] viewportBorder = [
0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E,
0x0040, 0x004E,
0x0080, 0x008E,
0x00C0, 0x00CE,
0x0100, 0x010E,
0x0140, 0x014E,
0x0180, 0x018E,
0x01C0, 0x01CE,
0x0200, 0x020E,
0x0240, 0x0241, 0x0242, 0x0243, 0x0244, 0x0245, 0x0246, 0x0247, 0x0248, 0x0249, 0x024A, 0x024B, 0x024C, 0x024D, 0x024E,
0xFFFF
];
static ushort minimapPreviousPosition;
/*
* Update the minimap position.
*
* @param packed The new position.
* @param forceUpdate If true force the update even if the position didn't change.
*/
internal static void Map_UpdateMinimapPosition(ushort packed, bool forceUpdate)
{
bool cleared;
Screen oldScreenID;
if (packed != 0xFFFF && packed == minimapPreviousPosition && !forceUpdate) return;
if (g_selectionType == (ushort)SelectionType.MENTAT) return;
oldScreenID = GFX_Screen_SetActive(Screen.NO1);
cleared = false;
if (minimapPreviousPosition != 0xFFFF && minimapPreviousPosition != packed)
{
var m = viewportBorder;
cleared = true;
for (var mPointer = 0; m[mPointer] != 0xFFFF; mPointer++)
{
ushort curPacked;
curPacked = (ushort)(minimapPreviousPosition + m[mPointer]);
BitArray_Clear(g_displayedMinimap, curPacked);
GUI_Widget_Viewport_DrawTile(curPacked);
}
}
if (packed != 0xFFFF && (packed != minimapPreviousPosition || forceUpdate))
{
var m = viewportBorder;
ushort mapScale;
MapInfo mapInfo;
ushort left, top, right, bottom;
mapScale = g_scenario.mapScale;
mapInfo = g_mapInfos[mapScale];
left = (ushort)((Tile_GetPackedX(packed) - mapInfo.minX) * (mapScale + 1) + 256);
right = (ushort)(left + mapScale * 15 + 14);
top = (ushort)((Tile_GetPackedY(packed) - mapInfo.minY) * (mapScale + 1) + 136);
bottom = (ushort)(top + mapScale * 10 + 9);
GUI_DrawWiredRectangle(left, top, right, bottom, 15);
for (var mPointer = 0; m[mPointer] != 0xFFFF; mPointer++)
{
ushort curPacked;
curPacked = (ushort)(packed + m[mPointer]);
BitArray_Set(g_displayedMinimap, curPacked);
}
}
if (cleared && oldScreenID == Screen.NO0)
{
GUI_Mouse_Hide_Safe();
GUI_Screen_Copy(32, 136, 32, 136, 8, 64, Screen.NO1, Screen.NO0);
GUI_Mouse_Show_Safe();
}
GFX_Screen_SetActive(oldScreenID);
minimapPreviousPosition = packed;
}
/*
* Perform a bloom explosion, filling the area with spice.
* @param packed Center position.
* @param houseID %House causing the explosion.
*/
internal static void Map_Bloom_ExplodeSpice(ushort packed, byte houseID)
{
if (g_validateStrictIfZero == 0)
{
var u = Unit_Get_ByPackedTile(packed);
Unit_Remove(u);
g_map[packed].groundTileID = (ushort)(g_mapTileID[packed] & 0x1FF);
Map_MakeExplosion((ushort)ExplosionType.EXPLOSION_SPICE_BLOOM_TREMOR, Tile_UnpackTile(packed), 0, 0);
}
if (houseID == (byte)g_playerHouseID) Sound_Output_Feedback(36);
Map_FillCircleWithSpice(packed, 5);
}
/*
* Change amount of spice at \a packed position.
* @param packed Position in the world to modify.
* @param dir Direction of change, > 0 means add spice, < 0 means remove spice.
*/
internal static void Map_ChangeSpiceAmount(ushort packed, short dir)
{
ushort type;
ushort spriteID;
if (dir == 0) return;
type = Map_GetLandscapeType(packed);
if (type == (ushort)LandscapeType.LST_THICK_SPICE && dir > 0) return;
if (type != (ushort)LandscapeType.LST_SPICE && type != (ushort)LandscapeType.LST_THICK_SPICE && dir < 0) return;
if (type != (ushort)LandscapeType.LST_NORMAL_SAND && type != (ushort)LandscapeType.LST_ENTIRELY_DUNE && type != (ushort)LandscapeType.LST_SPICE && dir > 0) return;
type = dir > 0
? (type == (ushort)LandscapeType.LST_SPICE) ? (ushort)LandscapeType.LST_THICK_SPICE : (ushort)LandscapeType.LST_SPICE
: (type == (ushort)LandscapeType.LST_THICK_SPICE) ? (ushort)LandscapeType.LST_SPICE : (ushort)LandscapeType.LST_NORMAL_SAND;
spriteID = 0;
if (type == (ushort)LandscapeType.LST_SPICE) spriteID = 49;
if (type == (ushort)LandscapeType.LST_THICK_SPICE) spriteID = 65;
spriteID = (ushort)(g_iconMap[g_iconMap[(int)IconMapEntries.ICM_ICONGROUP_LANDSCAPE] + spriteID] & 0x1FF);
g_mapTileID[packed] = (ushort)(0x8000 | spriteID);
g_map[packed].groundTileID = spriteID;
Map_FixupSpiceEdges(packed);
Map_FixupSpiceEdges((ushort)(packed + 1));
Map_FixupSpiceEdges((ushort)(packed - 1));
Map_FixupSpiceEdges((ushort)(packed - 64));
Map_FixupSpiceEdges((ushort)(packed + 64));
}
/*
* Fixes edges of spice / thick spice to show sand / normal spice for better looks.
* @param packed Position to check and possible fix edges of.
*/
static void Map_FixupSpiceEdges(ushort packed)
{
ushort type;
ushort spriteID;
packed &= 0xFFF;
type = Map_GetLandscapeType(packed);
spriteID = 0;
if (type is ((ushort)LandscapeType.LST_SPICE) or ((ushort)LandscapeType.LST_THICK_SPICE))
{
byte i;
for (i = 0; i < 4; i++)
{
var curPacked = (ushort)(packed + g_table_mapDiff[i]);
ushort curType;
if (Tile_IsOutOfMap(curPacked))
{
if (type is ((ushort)LandscapeType.LST_SPICE) or ((ushort)LandscapeType.LST_THICK_SPICE)) spriteID |= (ushort)(1 << i);
continue;
}
curType = Map_GetLandscapeType(curPacked);
if (type == (ushort)LandscapeType.LST_SPICE)
{
if (curType is ((ushort)LandscapeType.LST_SPICE) or ((ushort)LandscapeType.LST_THICK_SPICE)) spriteID |= (ushort)(1 << i);
continue;
}
if (curType == (ushort)LandscapeType.LST_THICK_SPICE) spriteID |= (ushort)(1 << i);
}
spriteID += (ushort)((type == (ushort)LandscapeType.LST_SPICE) ? 49 : 65);
spriteID = (ushort)(g_iconMap[g_iconMap[(int)IconMapEntries.ICM_ICONGROUP_LANDSCAPE] + spriteID] & 0x1FF);
g_mapTileID[packed] = (ushort)(0x8000 | spriteID);
g_map[packed].groundTileID = spriteID;
}
Map_Update(packed, 0, false);
}
/*
* Make an explosion on the given position, of a certain type. All units in the
* neighbourhoud get an amount of damage related to their distance to the
* explosion.
* @param type The type of explosion.
* @param position The position of the explosion.
* @param hitpoints The amount of hitpoints to give people in the neighbourhoud.
* @param unitOriginEncoded The unit that fired the bullet.
*/
internal static void Map_MakeExplosion(ushort type, Tile32 position, ushort hitpoints, ushort unitOriginEncoded)
{
var reactionDistance = (ushort)((type == (ushort)ExplosionType.EXPLOSION_DEATH_HAND) ? 32 : 16);
var positionPacked = Tile_PackTile(position);
if (!s_debugNoExplosionDamage && hitpoints != 0)
{
var find = new PoolFindStruct
{
houseID = (byte)HouseType.HOUSE_INVALID,
index = 0xFFFF,
type = 0xFFFF
};
while (true)
{
UnitInfo ui;
ushort distance;
CTeam t;
CUnit u;
CUnit us;
CUnit attack;
u = Unit_Find(find);
if (u == null) break;
ui = g_table_unitInfo[u.o.type];
distance = (ushort)(Tile_GetDistance(position, u.o.position) >> 4);
if (distance >= reactionDistance) continue;
if (!(u.o.type == (byte)UnitType.UNIT_SANDWORM && type == (ushort)ExplosionType.EXPLOSION_SANDWORM_SWALLOW) && u.o.type != (byte)UnitType.UNIT_FRIGATE)
{
Unit_Damage(u, (ushort)(hitpoints >> (distance >> 2)), 0);
}
if (u.o.houseID == (byte)g_playerHouseID) continue;
us = Tools_Index_GetUnit(unitOriginEncoded);
if (us == null) continue;
if (us == u) continue;
if (House_AreAllied(Unit_GetHouseID(u), Unit_GetHouseID(us))) continue;
t = Unit_GetTeam(u);
if (t != null)
{
UnitInfo targetInfo;
CUnit target;
if (t.action == (ushort)TeamActionType.TEAM_ACTION_STAGING)
{
Unit_RemoveFromTeam(u);
Unit_SetAction(u, ActionType.ACTION_HUNT);
continue;
}
target = Tools_Index_GetUnit(t.target);
if (target == null) continue;
targetInfo = g_table_unitInfo[target.o.type];
if (targetInfo.bulletType == (byte)UnitType.UNIT_INVALID) t.target = unitOriginEncoded;
continue;
}
if (u.o.type == (byte)UnitType.UNIT_HARVESTER)
{
var uis = g_table_unitInfo[us.o.type];
if (uis.movementType == (ushort)MovementType.MOVEMENT_FOOT && u.targetMove == 0)
{
if (u.actionID != (byte)ActionType.ACTION_MOVE) Unit_SetAction(u, ActionType.ACTION_MOVE);
u.targetMove = unitOriginEncoded;
continue;
}
}
if (ui.bulletType == (byte)UnitType.UNIT_INVALID) continue;
if (u.actionID == (byte)ActionType.ACTION_GUARD && u.o.flags.byScenario)
{
Unit_SetAction(u, ActionType.ACTION_HUNT);
}
if (u.targetAttack != 0 && u.actionID != (byte)ActionType.ACTION_HUNT) continue;
attack = Tools_Index_GetUnit(u.targetAttack);
if (attack != null)
{
var packed = Tile_PackTile(u.o.position);
if (Tile_GetDistancePacked(Tools_Index_GetPackedTile(u.targetAttack), packed) <= ui.fireDistance) continue;
}
Unit_SetTarget(u, unitOriginEncoded);
}
}
if (!s_debugNoExplosionDamage && hitpoints != 0)
{
var s = Structure_Get_ByPackedTile(positionPacked);
if (s != null)
{
if (type == (ushort)ExplosionType.EXPLOSION_IMPACT_LARGE)
{
var si = g_table_structureInfo[s.o.type];
if (si.o.hitpoints / 2 > s.o.hitpoints)
{
type = (ushort)ExplosionType.EXPLOSION_SMOKE_PLUME;
}
}
Structure_HouseUnderAttack(s.o.houseID);
Structure_Damage(s, hitpoints, 0);
}
}
if (Map_GetLandscapeType(positionPacked) == (ushort)LandscapeType.LST_WALL && hitpoints != 0)
{
if ((g_table_structureInfo[(int)StructureType.STRUCTURE_WALL].o.hitpoints <= hitpoints) ||
(Tools_Random_256() <= (hitpoints * 256 / g_table_structureInfo[(int)StructureType.STRUCTURE_WALL].o.hitpoints)))
{
Map_UpdateWall(positionPacked);
}
}
Explosion_Start(type, position);
}
/*
* Fill a circular area with spice.
* @param packed Center position of the area.
* @param radius Radius of the circle.
*/
internal static void Map_FillCircleWithSpice(ushort packed, ushort radius)
{
ushort x;
ushort y;
int i;
int j;
if (radius == 0) return;
x = Tile_GetPackedX(packed);
y = Tile_GetPackedY(packed);
for (i = -radius; i <= radius; i++)
{
for (j = -radius; j <= radius; j++)
{
var curPacked = Tile_PackXY((ushort)(x + j), (ushort)(y + i));
var distance = Tile_GetDistancePacked(packed, curPacked);
if (distance > radius) continue;