Line data Source code
1 : /**
2 : * Class that can be tagged to any tile. Can be used to constrain placers and entity placement to given areas.
3 : */
4 : class TileClass
5 : {
6 :
7 : constructor(size)
8 : {
9 4 : this.size = size;
10 4 : this.width = Math.ceil(size / 16); // Need one entry per 16 tiles as each tile takes a single bit
11 4 : this.inclusionGrid = new Uint16Array(this.size * this.width);
12 : }
13 :
14 : /**
15 : * Returns true if the given position is part of the tileclass.
16 : */
17 : has(position)
18 : {
19 8 : if (position.x < 0 || position.x >= this.size || position.y < 0 || position.y >= this.size)
20 3 : return 0;
21 : // x >> 4 == Math.floor(x / 16); used to find the integer this x is included in
22 : // x & 0xF == x % 16; used to find the bit position of the given x
23 5 : return this.inclusionGrid[position.y * this.width + (position.x >> 4)] & (1 << (position.x & 0xF));
24 : }
25 :
26 : /**
27 : * Adds the given position to the tileclass.
28 : */
29 : add(position)
30 : {
31 4 : if (position.x < 0 || position.x >= this.size || position.y < 0 || position.y >= this.size)
32 0 : return;
33 4 : this.inclusionGrid[position.y * this.width + (position.x >> 4)] |= 1 << (position.x & 0xF);
34 : }
35 :
36 : /**
37 : * Removes the given position to the tileclass.
38 : */
39 : remove(position)
40 : {
41 0 : if (position.x < 0 || position.x >= this.size || position.y < 0 || position.y >= this.size)
42 0 : return;
43 0 : this.inclusionGrid[position.y * this.width + (position.x >> 4)] &= ~(1 << (position.x & 0xF));
44 : }
45 :
46 : /**
47 : * Count the number of tiles in the tileclass within the given radius of the given position.
48 : * Can return either the total number of members or nonmembers.
49 : */
50 : countInRadius(position, radius, returnMembers)
51 : {
52 15 : let members = 0;
53 15 : let total = 0;
54 15 : const radius2 = radius * radius;
55 15 : const [x, y] = [position.x, position.y];
56 :
57 15 : const yMin = Math.max(Math.ceil(y - radius), 0);
58 15 : const yMax = Math.min(Math.floor(y + radius), this.size - 1);
59 15 : for (let iy = yMin; iy <= yMax; ++iy)
60 : {
61 162 : const dy = iy - y;
62 162 : const dy2 = dy * dy;
63 162 : const delta = Math.sqrt(radius2 - dy2);
64 162 : const xMin = Math.max(Math.ceil(x - delta), 0);
65 162 : const xMax = Math.min(Math.floor(x + delta), this.size - 1);
66 :
67 162 : const indexXMin = xMin >> 4;
68 162 : const indexXMax = xMax >> 4;
69 162 : const indexY = iy * this.width;
70 162 : for (let indexX = indexXMin; indexX <= indexXMax; ++indexX)
71 : {
72 597 : const imin = indexX == indexXMin ? xMin & 0xF : 0;
73 597 : const imax = indexX == indexXMax ? xMax & 0xF : 15;
74 597 : total += imax - imin + 1;
75 597 : if (this.inclusionGrid[indexY + indexX])
76 16 : for (let i = imin; i <= imax; ++i)
77 98 : if (this.inclusionGrid[indexY + indexX] & (1 << i))
78 14 : ++members;
79 : }
80 : }
81 :
82 15 : return returnMembers ? members : total - members;
83 : }
84 :
85 : /**
86 : * Counts the number of tiles marked in the tileclass within the given radius of the given position.
87 : */
88 : countMembersInRadius(position, radius)
89 : {
90 9 : return this.countInRadius(position, radius, true);
91 : }
92 :
93 : /**
94 : * Counts the number of tiles not marked in the tileclass within the given radius of the given position.
95 : */
96 : countNonMembersInRadius(position, radius)
97 : {
98 6 : return this.countInRadius(position, radius, false);
99 : }
100 : }
|