-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tile.cpp
413 lines (363 loc) · 10.4 KB
/
Tile.cpp
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
/*
* File: Tile.cpp
* Author: brandon
*
* Created on May 2, 2011, 8:00 PM
*/
#include "Tile.h"
#include "GameLibrary.h"
#include "GLFrame.h"
#include <math.h>
Tile::Tile(Vector3f position, int chit, int resource) {
corners = new CornerNode*[6];
tiles = new Tile* [6];
for (int i = 0; i < 6; i++) {
corners[i] = NULL;
tiles[i] = NULL;
}
blocked = false;
port = false;
token = chit;
//model = new Model("hextile.mdl");
model = GameLibrary::getInstance()->getCachedModel("hextile.mdl");
this->position = position;
this->resource = resource;
model->changeColor(GameLibrary::getColorFor(resource));
color = GameLibrary::getColorFor(resource);
if (resource == WATER || resource == DESERT) {
blocked = true;
}
}
Tile::~Tile() {
for (int i = 0; i < 6; i++) {
//delete corners[i]; // TODO: MEMORY LEAK LOST ON CORNERS, fix
}
delete [] corners;
}
int Tile::getResource() {
return resource;
}
/**
* sets the nodes for this tile
* @param the tile to correct to
* @param position the side this tile is bordering on. starting at the bottom, and going arround CCW
* @return
*/
void Tile::setBorderingTile(Tile* tile, Vector3f tileDirection) {
int previous;
if (tileDirection.y < -EPSILON) {
if (tileDirection.x < -EPSILON) { // x less than 0
previous = 5;
} else {
if (tileDirection.x > EPSILON) // x greater than 0
previous = 1;
else { // x equal 0
previous = 0;
}
}
} else { // y > 0
if (tileDirection.x < -EPSILON) { // x less than 0
previous = 4;
} else {
if (tileDirection.x > EPSILON) // x greater than 0
previous = 2;
else { // x equals 0
previous = 3;
}
}
}
tiles[previous] = tile;
tile->tiles[(previous + 3) % 6] = this;
corners[previous] = tile->corners[(previous + 4) % 6];
corners[(previous + 1) % 6] = tile->corners[(previous + 3) % 6];
corners[previous]->setBorderringTile(tile);
corners[(previous + 1) % 6]->setBorderringTile(tile);
corners[previous]->setBorderringTile(this);
corners[(previous + 1) % 6]->setBorderringTile(this);
}
Tile* Tile::getBorderingTile(int direction) {
return tiles[direction];
}
void Tile::assignUnusedNodes() {
Vector3f direction;
float angle;
for (int i = 0; i < 6; i++) {
angle = (4 + i)*(PI / 3);
direction = Vector3f(cos(angle), sin(angle), 0); // DOES NOT HAVE TILE SEPERATION IN. ASSUMES DISTANCE = 1
if (corners[i] == NULL) {
corners[i] = new CornerNode(this);
corners[i]->setPosition(position + direction);
corners[i]->setIsLeft(i % 2 == 0);
//set node connection
if (resource == WATER) {
corners[i]->water = true;
}
}
}
updateConnected();
}
/**
* updates all indicies
*/
void Tile::updateConnected() {
for (int i = 0; i < 6; i++) {
if (corners[i] != NULL && corners[i - 1] != NULL) {
updateConnected(i);
}
}
}
/**
* updates individual index to connect to previous node
* @param i index of node
*/
void Tile::updateConnected(int i) {
if (i != 0) {
if (i == 2 || i == 3) {
corners[i]->setConnectedNode(corners[i - 1], 0); // if moving upwards, previous is below
} else if (i == 5) {
corners[i]->setConnectedNode(corners[i - 1], 2); // moving upwards, previous is above
corners[i]->setConnectedNode(corners[0], 0); // connect to first node, first node below
} else { // applys to 1 or 4
corners[i]->setConnectedNode(corners[i - 1], 1); // applys to 3rd node, connected is beside
}
}
}
/**
* The token is the number that you want to roll to get resources
* @return
*/
int Tile::getToken() {
return token;
}
Vector3f Tile::getPosition() {
return position;
}
bool Tile::hasPort() {
return port;
}
/**
* difference between hasPort bordersPort, and nextToPort is that the water tiles have the port, the surrounding tiles border port, and a tile which has a corner on a port is nextToPort
* @return
*/
bool Tile::nextToPort() {
for (int i = 0; i < 6; i++) {
if (corners[i]->getPort() != WATER) {
return true;
}
}
return false;
}
void Tile::addPort(int type, int direction) {
port = true;
corners[(direction)]->port = type;
corners[(direction + 1) % 6]->port = type;
}
bool Tile::bordersPort() {
for (int i = 0; i < 6; i++) {
if (getBorderingTile(i) != NULL && getBorderingTile(i)->hasPort()) {
return true; // dont put ports next to each other
}
}
return false;
}
bool Tile::isBlocked() {
return blocked;
}
void Tile::setBlocked(bool blocked) {
this->blocked = blocked;
}
void Tile::Draw() {
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glColor3f(color.r, color.g, color.b);
model->Draw();
if (!isBlocked())
drawNumber();
}
void Tile::drawNumber() {
glDisable(GL_LIGHTING);
glColor3f(0.7, 0.7, 0.9);
if (resource != WATER && resource != DESERT) {
GameLibrary::getGLFrame()->renderText(position.x, position.y, position.z + 0.1, QString::fromStdString(GameLibrary::IntToStr(token)));
}
glEnable(GL_LIGHTING);
}
void Tile::glGenerateDisplayList() {
glColor3f(color.r, color.g, color.b);
model->glGenerateDisplayList(position);
}
Tile::CornerNode* Tile::occupyCorner(int corner) {
if (!corners[corner]->isOccupied() && !corners[corner]->isOnWater()) {
for (int i = 0; i < 3; i++) {
if (corners[corner]->connected[i]->isOccupied()) {
throw "placing here breaks proximity";
}
}
corners[corner]->setOccupied();
return corners[corner];
}
throw "Could not place piece at corner " + corner;
}
Tile::CornerNode::CornerNode(Tile* creator) {
occupied = false;
water = false;
port = WATER; // -2 signifies no port
connected = new CornerNode*[3];
tiles = new Tile*[3];
roads = new Road*[3];
for (int i = 0; i < 3; i++) { // NULLify references
tiles[i] = NULL;
connected[i] = NULL;
roads[i] = NULL; // no players own roads
}
tiles[0] = creator;
}
Tile::CornerNode::~CornerNode() {
//NOTE, only works if tiles are deleted too, otherwise need to nullify tiles and connected
delete [] tiles;
delete [] connected;
delete [] roads;
}
void Tile::CornerNode::setOccupied() {
occupied = true;
}
bool Tile::CornerNode::isOccupied() {
return occupied;
}
bool Tile::CornerNode::isBridge(Tile::CornerNode* node) {
int sharedWaterTiles = 0;
int toWaterTile = 0;
for (int i = 0; i < 3; i++) {
if (node->tiles[i]->getResource() == WATER) {
toWaterTile++;
for (int j = 0; j < 3; j++) {
if (this->tiles[j] == node->tiles[i]) {
sharedWaterTiles++;
break;
}
}
}
}
return (sharedWaterTiles == 2 && toWaterTile != 3);
}
void Tile::CornerNode::setIsLeft(bool left) {
this->left = left;
}
bool Tile::CornerNode::isLeft() {
return left;
}
int Tile::CornerNode::getPort() {
return port;
}
Vector3f Tile::CornerNode::getPosition() {
return position;
}
void Tile::CornerNode::setPosition(Vector3f position) {
this->position = position;
}
Tile::CornerNode* Tile::getCorner(int corner) {
return corners[corner];
}
Road* Tile::CornerNode::getRoad(int index) {
return roads[index];
}
/**
* Gets node in the given direction
* @param direction
* @return
*/
Tile::CornerNode* Tile::CornerNode::getSelection(int direction) {
switch (direction) {
case 0: // down
return connected[0]; // return below
break;
case 1: // right
if (left)
return connected [1];
break;
case 2: // up
return connected [2]; // return above
break;
case 3: // left
if (!left)
return connected [1]; // return side
break;
}
return NULL;
}
Vector2f Tile::CornerNode::getVectorDirection(int direction) {
Vector2f dir;
switch (direction) {
case 0:
return Vector2f(sin(30), cos(30));
break;
case 1:
case 3:
return Vector2f(1, 0);
break;
case 2:
return Vector2f(sin(30), -cos(30));
break;
}
if (left)
dir.x *= -1;
return dir;
}
void Tile::CornerNode::setBorderringTile(Tile* tile) {
for (int i = 0; i < 3; i++) {
if (tiles[i] == tile) { // dont add duplicates
return;
}
if (tiles[i] == NULL) {
tiles[i] = tile;
return;
}
}
std::cout << this->position << " cannot border " << tile->position;
throw tile; // bordering tiles is full, something is wrong
}
void Tile::CornerNode::setConnectedNode(CornerNode* node, int position) {
connected[position] = node;
switch (position) {
case 0: // bottom
node->connected[2] = this; // set reference to self as top;
break;
case 1: // middle
node->connected[1] = this; //set reference to self as middle
break;
case 2: // top
node->connected[0] = this; //set reference to self as bottom
break;
}
}
void Tile::CornerNode::registerRoad(Road* road, int direction) {
this->roads[direction] = road;
}
bool Tile::CornerNode::cornerIsOn(int roll) {
for (int i = 0; i < 3; i++) {
if (tiles[i]->getToken() == roll) {
return true;
}
}
return false;
}
bool Tile::CornerNode::isOnWater() {
if (water == true) {
for (int i = 0; i < 3; i++) { // HACK, CUZ IF CORNERS GENERATED ON TILE, WILL BE SET AS water=true BY DEFAULT
if (tiles[i] != NULL && (tiles[i]->getResource() != WATER)) {
water = false;
return false;
}
}
}
return water;
}
int Tile::CornerNode::getTileToken(int tile) {
if (tile >= 0 && tile <= 3) {
return tiles[tile]->getToken();
}
throw "invalid tile number"; // invalid tile number
}
int Tile::CornerNode::getResource(int tile) {
return tiles[tile]->getResource();
}