-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tile.cs
553 lines (465 loc) · 18.5 KB
/
Tile.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
/* Tile */
namespace SharpDune;
static class Tile
{
static readonly sbyte[] _stepX = [
0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45,
48, 51, 54, 57, 59, 62, 65, 67, 70, 73, 75, 78, 80, 82, 85, 87,
89, 91, 94, 96, 98, 100, 101, 103, 105, 107, 108, 110, 111, 113, 114, 116,
117, 118, 119, 120, 121, 122, 123, 123, 124, 125, 125, 126, 126, 126, 126, 126,
127, 126, 126, 126, 126, 126, 125, 125, 124, 123, 123, 122, 121, 120, 119, 118,
117, 116, 114, 113, 112, 110, 108, 107, 105, 103, 102, 100, 98, 96, 94, 91,
89, 87, 85, 82, 80, 78, 75, 73, 70, 67, 65, 62, 59, 57, 54, 51,
48, 45, 42, 39, 36, 33, 30, 27, 24, 21, 18, 15, 12, 9, 6, 3,
0, -3, -6, -9, -12, -15, -18, -21, -24, -27, -30, -33, -36, -39, -42, -45,
-48, -51, -54, -57, -59, -62, -65, -67, -70, -73, -75, -78, -80, -82, -85, -87,
-89, -91, -94, -96, -98, -100, -102, -103, -105, -107, -108, -110, -111, -113, -114, -116,
-117, -118, -119, -120, -121, -122, -123, -123, -124, -125, -125, -126, -126, -126, -126, -126,
-126, -126, -126, -126, -126, -126, -125, -125, -124, -123, -123, -122, -121, -120, -119, -118,
-117, -116, -114, -113, -112, -110, -108, -107, -105, -103, -102, -100, -98, -96, -94, -91,
-89, -87, -85, -82, -80, -78, -75, -73, -70, -67, -65, -62, -59, -57, -54, -51,
-48, -45, -42, -39, -36, -33, -30, -27, -24, -21, -18, -15, -12, -9, -6, -3
];
static readonly sbyte[] _stepY = [
127, 126, 126, 126, 126, 126, 125, 125, 124, 123, 123, 122, 121, 120, 119, 118,
117, 116, 114, 113, 112, 110, 108, 107, 105, 103, 102, 100, 98, 96, 94, 91,
89, 87, 85, 82, 80, 78, 75, 73, 70, 67, 65, 62, 59, 57, 54, 51,
48, 45, 42, 39, 36, 33, 30, 27, 24, 21, 18, 15, 12, 9, 6, 3,
0, -3, -6, -9, -12, -15, -18, -21, -24, -27, -30, -33, -36, -39, -42, -45,
-48, -51, -54, -57, -59, -62, -65, -67, -70, -73, -75, -78, -80, -82, -85, -87,
-89, -91, -94, -96, -98, -100, -102, -103, -105, -107, -108, -110, -111, -113, -114, -116,
-117, -118, -119, -120, -121, -122, -123, -123, -124, -125, -125, -126, -126, -126, -126, -126,
-126, -126, -126, -126, -126, -126, -125, -125, -124, -123, -123, -122, -121, -120, -119, -118,
-117, -116, -114, -113, -112, -110, -108, -107, -105, -103, -102, -100, -98, -96, -94, -91,
-89, -87, -85, -82, -80, -78, -75, -73, -70, -67, -65, -62, -59, -57, -54, -51,
-48, -45, -42, -39, -36, -33, -30, -27, -24, -21, -18, -15, -12, -9, -6, -3,
0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45,
48, 51, 54, 57, 59, 62, 65, 67, 70, 73, 75, 78, 80, 82, 85, 87,
89, 91, 94, 96, 98, 100, 101, 103, 105, 107, 108, 110, 111, 113, 114, 116,
117, 118, 119, 120, 121, 122, 123, 123, 124, 125, 125, 126, 126, 126, 126, 126
];
/*
* Check if a packed tile is out of map. Useful after additional or substraction.
* @param packed The packed tile to check.
* @return True if and only if the tile is out of map.
*/
/*extern bool Tile_IsOutOfMap(uint16 packed);*/
internal static bool Tile_IsOutOfMap(ushort packed) => (packed & 0xF000) != 0;
/*
* Unpacks a 12 bits packed tile and retrieves the X-position.
*
* @param packed The uint16 containing the 12 bits packed tile.
* @return The unpacked X-position.
*/
/*extern uint8 Tile_GetPackedX(uint16 packed);*/
internal static ushort Tile_GetPackedX(ushort packed) => (ushort)(packed & 0x3F);
/*
* Unpacks a 12 bits packed tile and retrieves the Y-position.
*
* @param packed The uint16 containing the 12 bits packed tile.
* @return The unpacked Y-position.
*/
/*extern uint8 Tile_GetPackedY(uint16 packed);*/
internal static ushort Tile_GetPackedY(ushort packed) => (ushort)((packed >> 6) & 0x3F);
/*
* Returns the X-position of the tile.
*
* @param tile The tile32 to get the X-position from.
* @return The X-position of the tile.
*/
/*extern uint8 Tile_GetPosX(tile32 tile);*/
internal static byte Tile_GetPosX(Tile32 tile) => (byte)((tile.x >> 8) & 0x3f);
/*
* Returns the Y-position of the tile.
*
* @param tile The tile32 to get the Y-position from.
* @return The Y-position of the tile.
*/
/*extern uint8 Tile_GetPosY(tile32 tile);*/
internal static byte Tile_GetPosY(Tile32 tile) => (byte)((tile.y >> 8) & 0x3f);
/*
* Packs a 32 bits tile class into a 12 bits packed tile.
*
* @param tile The tile32 to get it's Y-position from.
* @return The tile packed into 12 bits.
*/
/*extern uint16 Tile_PackTile(tile32 tile);*/
internal static ushort Tile_PackTile(Tile32 tile) => (ushort)((Tile_GetPosY(tile) << 6) | Tile_GetPosX(tile));
/*
* Packs an x and y coordinate into a 12 bits packed tile.
*
* @param x The X-coordinate from.
* @param x The Y-coordinate from.
* @return The coordinates packed into 12 bits.
*/
/*extern uint16 Tile_PackXY(uint16 x, uint16 y);*/
internal static ushort Tile_PackXY(ushort x, ushort y) => (ushort)((y << 6) | x);
/*
* Check whether a tile is valid.
*
* @param tile The tile32 to check for validity.
* @return True if valid, false if not.
*/
/*extern bool Tile_IsValid(tile32 tile);*/
/*#define Tile_IsValid(tile) (((tile).x & 0xc000) == 0 && ((tile).y & 0xc000) == 0)*/
internal static bool Tile_IsValid(Tile32 tile) => ((tile.x | tile.y) & 0xc000) == 0;
/*
* Adds two tiles together.
*
* @param from The origin.
* @param diff The difference.
* @return The new coordinates.
*/
internal static Tile32 Tile_AddTileDiff(Tile32 from, Tile32 diff)
{
var result = new Tile32
{
x = (ushort)(from.x + diff.x),
y = (ushort)(from.y + diff.y)
};
return result;
}
/*
* Remove fog in the radius around the given tile.
*
* @param tile The tile to remove fog around.
* @param radius The radius to remove fog around.
*/
internal static void Tile_RemoveFogInRadius(Tile32 tile, ushort radius)
{
ushort packed;
ushort x, y;
short i, j;
/* TODO this code could be simplified */
packed = Tile_PackTile(tile);
if (!Map_IsValidPosition(packed)) return;
/* setting tile from its packed position equals removing the
* non integer part */
x = Tile_GetPackedX(packed);
y = Tile_GetPackedY(packed);
Tile_MakeXY(ref tile, x, y);
for (i = (short)-radius; i <= radius; i++)
{
for (j = (short)-radius; j <= radius; j++)
{
var t = new Tile32();
if (x + i is < 0 or >= 64) continue;
if (y + j is < 0 or >= 64) continue;
var xi = (ushort)(x + i);
var yj = (ushort)(y + j);
packed = Tile_PackXY(xi, yj);
Tile_MakeXY(ref t, xi, yj);
if (Tile_GetDistanceRoundedUp(tile, t) > radius) continue;
Map_UnveilTile(packed, (byte)g_playerHouseID);
}
}
}
/*
* Make a tile32 from an X- and Y-position.
*
* @param x The X-position.
* @param y The Y-position.
* @return A tile32 at the top-left corner of the X- and Y-position.
*/
/*extern tile32 Tile_MakeXY(uint16 x, uint16 y);*/
internal static void/*tile32*/ Tile_MakeXY(ref Tile32 tile, ushort X, ushort Y)
{
tile.x = (ushort)(X << 8);
tile.y = (ushort)(Y << 8);
//return tile;
}
/*
* Calculates the rounded up distance between the two given packed tiles.
*
* @param from The origin.
* @param to The destination.
* @return The longest distance between the X or Y coordinates, plus half the shortest.
*/
internal static ushort Tile_GetDistanceRoundedUp(Tile32 from, Tile32 to) => (ushort)((Tile_GetDistance(from, to) + 0x80) >> 8);
/*
* Calculates the distance between the two given tiles.
*
* @param from The origin.
* @param to The destination.
* @return The longest distance between the X or Y coordinates, plus half the shortest.
*/
internal static ushort Tile_GetDistance(Tile32 from, Tile32 to)
{
var distance_x = (ushort)Math.Abs(from.x - to.x);
var distance_y = (ushort)Math.Abs(from.y - to.y);
if (distance_x > distance_y) return (ushort)(distance_x + (distance_y / 2));
return (ushort)(distance_y + (distance_x / 2));
}
static readonly ushort[] orientationOffsets = [0x40, 0x80, 0x0, 0xC0];
static readonly int[] directions = [
0x3FFF, 0x28BC, 0x145A, 0xD8E, 0xA27, 0x81B, 0x6BD, 0x5C3, 0x506, 0x474, 0x3FE, 0x39D, 0x34B, 0x306, 0x2CB, 0x297,
0x26A, 0x241, 0x21D, 0x1FC, 0x1DE, 0x1C3, 0x1AB, 0x194, 0x17F, 0x16B, 0x159, 0x148, 0x137, 0x128, 0x11A, 0x10C
];
/*
* Get to direction to follow to go from \a from to \a to.
*
* @param from The origin.
* @param to The destination.
* @return The direction.
*/
internal static sbyte Tile_GetDirection(Tile32 from, Tile32 to)
{
int dx;
int dy;
ushort i;
int gradient;
ushort baseOrientation;
bool invert;
ushort quadrant = 0;
dx = to.x - from.x;
dy = to.y - from.y;
if (Math.Abs(dx) + Math.Abs(dy) > 8000)
{
dx /= 2;
dy /= 2;
}
if (dy <= 0)
{
quadrant |= 0x2;
dy = -dy;
}
if (dx < 0)
{
quadrant |= 0x1;
dx = -dx;
}
baseOrientation = orientationOffsets[quadrant];
invert = false;
gradient = 0x7FFF;
if (dx >= dy)
{
if (dy != 0) gradient = (dx << 8) / dy;
}
else
{
invert = true;
if (dx != 0) gradient = (dy << 8) / dx;
}
for (i = 0; i < directions.Length /*lengthof(directions)*/; i++)
{
if (directions[i] <= gradient) break;
}
if (!invert) i = (ushort)(64 - i);
if (quadrant is 0 or 3) return (sbyte)((baseOrientation + 64 - i) & 0xFF);
return (sbyte)((baseOrientation + i) & 0xFF);
}
/*
* Convert an orientation that goes from 0 . . . 255 to one that goes from 0 . . . 7.
* @param orientation The 256-based orientation.
* @return A 8-based orientation.
*/
internal static byte Orientation_Orientation256ToOrientation8(byte orientation) => (byte)(((orientation + 16) / 32) & 0x7);
/*
* Convert an orientation that goes from 0 . . . 255 to one that goes from 0 . . . 15.
* @param orientation The 256-based orientation.
* @return A 16-based orientation.
*/
internal static byte Orientation_Orientation256ToOrientation16(byte orientation) => (byte)(((orientation + 8) / 16) & 0xF);
/*
* Unpacks a 12 bits packed tile to a 32 bits tile class.
*
* @param packed The uint16 containing the 12 bits packed tile information.
* @return The unpacked tile.
*/
internal static Tile32 Tile_UnpackTile(ushort packed)
{
var tile = new Tile32
{
x = (ushort)((((packed >> 0) & 0x3F) << 8) | 0x80),
y = (ushort)((((packed >> 6) & 0x3F) << 8) | 0x80)
};
return tile;
}
/*
* Calculates the distance between the two given packed tiles.
*
* @param packed_from The origin.
* @param packed_to The destination.
* @return The longest distance between the X or Y coordinates, plus half the shortest.
*/
internal static ushort Tile_GetDistancePacked(ushort packed_from, ushort packed_to)
{
var from = Tile_UnpackTile(packed_from);
var to = Tile_UnpackTile(packed_to);
return (ushort)(Tile_GetDistance(from, to) >> 8);
}
/*
* Returns the X-position of the tile.
*
* @param tile The tile32 to get the X-position from.
* @return The X-position of the tile.
*/
/*extern uint16 Tile_GetX(tile32 tile);*/
internal static ushort Tile_GetX(Tile32 tile) => tile.x;
/*
* Returns the Y-position of the tile.
*
* @param tile The tile32 to get the Y-position from.
* @return The Y-position of the tile.
*/
/*extern uint16 Tile_GetY(tile32 tile);*/
internal static ushort Tile_GetY(Tile32 tile) => tile.y;
/*
* Centers the offset of the tile.
*
* @param tile The tile to center.
*/
internal static Tile32 Tile_Center(Tile32 tile)
{
Tile32 result;
result = tile;
result.x = (ushort)((result.x & 0xff00) | 0x80);
result.y = (ushort)((result.y & 0xff00) | 0x80);
return result;
}
/*
* Get the tile from given tile at given maximum distance in random direction.
*
* @param tile The origin.
* @param distance The distance maximum.
* @param center Wether to center the offset of the tile.
* @return The tile.
*/
internal static Tile32 Tile_MoveByRandom(Tile32 tile, ushort distance, bool center)
{
ushort x;
ushort y;
var ret = new Tile32();
byte orientation;
ushort newDistance;
if (distance == 0) return tile;
x = Tile_GetX(tile);
y = Tile_GetY(tile);
newDistance = Tools_Random_256();
while (newDistance > distance) newDistance /= 2;
distance = newDistance;
orientation = Tools_Random_256();
x += (ushort)(((_stepX[orientation] * distance) / 128) * 16);
y -= (ushort)(((_stepY[orientation] * distance) / 128) * 16);
if (x > 16384 || y > 16384) return tile;
ret.x = x;
ret.y = y;
return center ? Tile_Center(ret) : ret;
}
/*
* Get the tile from given tile at given distance in given direction.
*
* @param tile The origin.
* @param orientation The direction to follow.
* @param distance The distance.
* @return The tile.
*/
internal static Tile32 Tile_MoveByDirection(Tile32 tile, short orientation, ushort distance)
{
int diffX, diffY;
int roundingOffsetX, roundingOffsetY;
distance = Math.Min(distance, (ushort)0xFF);
if (distance == 0) return tile;
diffX = _stepX[orientation & 0xFF];
diffY = _stepY[orientation & 0xFF];
/* Always round away from zero */
roundingOffsetX = diffX < 0 ? -64 : 64;
roundingOffsetY = diffY < 0 ? -64 : 64;
tile.x += (ushort)((diffX * distance + roundingOffsetX) / 128);
tile.y -= (ushort)((diffY * distance + roundingOffsetY) / 128);
return tile;
}
/*
* Get a tile in the direction of a destination, randomized a bit.
*
* @param packed_from The origin.
* @param packed_to The destination.
* @return A packed tile.
*/
internal static ushort Tile_GetTileInDirectionOf(ushort packed_from, ushort packed_to)
{
short distance;
byte direction;
if (packed_from == 0 || packed_to == 0) return 0;
distance = (short)Tile_GetDistancePacked(packed_from, packed_to);
direction = Tile_GetDirectionPacked(packed_to, packed_from);
if (distance <= 10) return 0;
while (true)
{
short dir;
Tile32 position;
ushort packed;
dir = (short)(31 + (Tools_Random_256() & 0x3F));
if ((Tools_Random_256() & 1) != 0) dir = (short)-dir;
position = Tile_UnpackTile(packed_to);
position = Tile_MoveByDirection(position, (short)(direction + dir), (ushort)(Math.Min(distance, (short)20) << 8));
packed = Tile_PackTile(position);
if (Map_IsValidPosition(packed)) return packed;
}
//return 0;
}
static readonly byte[] returnValues = [0x20, 0x40, 0x20, 0x00, 0xE0, 0xC0, 0xE0, 0x00, 0x60, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xA0, 0x80];
/*
* Get to direction to follow to go from packed_from to packed_to.
*
* @param packed_from The origin.
* @param packed_to The destination.
* @return The direction.
*/
internal static byte Tile_GetDirectionPacked(ushort packed_from, ushort packed_to)
{
short x1, y1, x2, y2;
short dx, dy;
ushort index;
x1 = (short)Tile_GetPackedX(packed_from);
y1 = (short)Tile_GetPackedY(packed_from);
x2 = (short)Tile_GetPackedX(packed_to);
y2 = (short)Tile_GetPackedY(packed_to);
index = 0;
dy = (short)(y1 - y2);
if (dy < 0)
{
index |= 0x8;
dy = (short)-dy;
}
dx = (short)(x2 - x1);
if (dx < 0)
{
index |= 0x4;
dx = (short)-dx;
}
if (dx >= dy)
{
if (((dx + 1) / 2) > dy) index |= 0x1;
}
else
{
index |= 0x2;
if (((dy + 1) / 2) > dx) index |= 0x1;
}
return returnValues[index];
}
/*
* Move to the given orientation looking from the current position.
* @note returns input position when going out-of-bounds.
* @param position The position to move from.
* @param orientation The orientation to move in.
* @return The new position, or the old in case of out-of-bounds.
*/
internal static Tile32 Tile_MoveByOrientation(Tile32 position, byte orientation)
{
short[] xOffsets = [0, 256, 256, 256, 0, -256, -256, -256]; //[8]
short[] yOffsets = [-256, -256, 0, 256, 256, 256, 0, -256]; //[8]
ushort x;
ushort y;
x = Tile_GetX(position);
y = Tile_GetY(position);
orientation = Orientation_Orientation256ToOrientation8(orientation);
x += (ushort)xOffsets[orientation];
y += (ushort)yOffsets[orientation];
if (x > 16384 || y > 16384) return position;
position.x = x;
position.y = y;
return position;
}
}