-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexmap.js
150 lines (128 loc) · 3.38 KB
/
hexmap.js
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
function hexmap_distance(ox, oy, x, y)
{
var dx = x - ox;
var dy = y - oy;
var z = -(x + y);
var oz = -(ox + oy);
var dz = z - oz;
return Math.max(Math.abs(dx), Math.max(Math.abs(dy), Math.abs(dz)));
}
function hexmap_get_map_coord_x(buf_x)
{
return buf_x;
}
function hexmap_get_map_coord_y(buf_x, buf_y)
{
return buf_y - Math.floor(buf_x / 2);
}
function hexmap_get_bufx(x)
{
return x;
}
function hexmap_get_bufy(x, y)
{
return y + Math.floor(x / 2);
}
/*
* Map co-ordinates are organised in the following manner to give a straight
* x and y axis.
* This aids in distance and path-finding calculations.
* http://keekerdc.com/2011/03/hexagon-grids-coordinate-systems-and-distance-calculations/
*/
function HexMap(width, height)
{
this.width = width;
this.height = height;
this.map = new Array(width);
for (var x = 0; x < width; x++)
{
this.map[x] = new Array(height);
}
}
HexMap.prototype.getTile =
function(x, y)
{
nx = hexmap_get_bufx(x);
ny = hexmap_get_bufy(x, y);
try
{
return this.map[nx][ny];
}
catch(e)
{
/*
* FIXME: Need to return an error in this case,
* and handle this everywhere.
*/
console.log(x);
console.log(y);
console.log(nx);
console.log(ny);
}
}
HexMap.prototype.validHex =
function(x, y)
{
bufx = hexmap_get_bufx(x);
bufy = hexmap_get_bufy(x, y);
if (bufx < 0 || bufx >= this.width || bufy < 0 || bufy >= this.height)
{
return false;
}
return true;
}
HexMap.prototype._select_hex =
function(x, y, list, list_index)
{
if (!this.validHex(x, y))
{
return list_index;
}
list[list_index] = new Object();
list[list_index].x = x;
list[list_index].y = y;
return list_index + 1;
}
HexMap.prototype.getSurroundingHex =
function(x, y, r)
{
var i;
var minX = x - r;
var maxX = x + r;
/* Work out how big to make the array to fit the hexes */
rlist_max = 1;
for (i = r; i > 0; i--)
{
/*
* Currently over-estimates (I think)
*/
rlist_max += (6 * i);
}
var rlist = Array(rlist_max);
var rlist_index = 0;
for (i = minX; i <= maxX; i++)
{
/* don't add the center hex */
if (!(i == x))
{
rlist_index = this._select_hex(i, y, rlist, rlist_index)
}
}
for (j = r; j > 0; j --)
{
for (i = minX; i <= maxX; i++)
{
if (hexmap_distance(x, y, i, y + j) <= r)
{
rlist_index = this._select_hex(i, y + j, rlist, rlist_index)
}
if (hexmap_distance(x, y, i, y - j) <= r)
{
rlist_index = this._select_hex(i, y - j, rlist, rlist_index)
}
}
}
assert(rlist_index <= rlist_max);
rlist.length = rlist_index;
return rlist;
}