Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
broadcastDirection.hpp
Go to the documentation of this file.
1#pragma once
2
4
8inline int broadcastDirection(int srcX, int srcY, int dstX, int dstY, int mapW, int mapH,
9 Orientation dstFacing)
10{
11 if (srcX == dstX && srcY == dstY) return 0;
12
13 // shortest toroidal delta from dst to src
14 int dx = srcX - dstX;
15 int dy = srcY - dstY;
16 if (dx > mapW / 2) dx -= mapW;
17 if (dx < -mapW / 2) dx += mapW;
18 if (dy > mapH / 2) dy -= mapH;
19 if (dy < -mapH / 2) dy += mapH;
20
21 // rotate delta to be relative to receiver's facing direction
22 // N: no rotation, E: rotate -90, S: rotate 180, W: rotate 90
23 int rx, ry;
24 switch (dstFacing) {
25 case Orientation::N:
26 rx = dx;
27 ry = -dy;
28 break;
29 case Orientation::E:
30 rx = dy;
31 ry = dx;
32 break;
33 case Orientation::S:
34 rx = -dx;
35 ry = dy;
36 break;
37 case Orientation::W:
38 rx = -dy;
39 ry = -dx;
40 break;
41 default:
42 rx = dx;
43 ry = -dy;
44 break;
45 }
46
47 // spec numbers tiles trigonometrically (counterclockwise): 1=front, 2=front-left,
48 // 3=left, ..., 8=front-right. Mirror rx to turn the clockwise octants below into CCW.
49 rx = -rx;
50
51 // map to directions 1-8: 1=front, 2=front-left, ..., 8=front-right
52 if (ry > 0 && rx == 0) return 1;
53 if (ry > 0 && rx > 0) return 2;
54 if (ry == 0 && rx > 0) return 3;
55 if (ry < 0 && rx > 0) return 4;
56 if (ry < 0 && rx == 0) return 5;
57 if (ry < 0 && rx < 0) return 6;
58 if (ry == 0 && rx < 0) return 7;
59 if (ry > 0 && rx < 0) return 8;
60 return 0;
61}
Orientation
int broadcastDirection(int srcX, int srcY, int dstX, int dstY, int mapW, int mapH, Orientation dstFacing)