-
Notifications
You must be signed in to change notification settings - Fork 0
/
broblub.cpp
393 lines (347 loc) · 10.3 KB
/
broblub.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
/*
Group #: 3
Members: Chris Prosser, Jacob Gearhart, Kory Kappel, Andrew Miller
Course: COMP 322, Advanced Programming
Date: 28 October 2013
Description: This file implements the Broblub class,
the enemy creature displayed on the screen. Broblubs
operate with moderate intelligence, almost always
choosing to attack the closest Nimkip
*/
#include "Broblub.h"
using namespace moverNS;
void Broblub::attack(GridLoc& enemy){
level::runAttack(this, enemy);
}
void Broblub::attack(int x, int y){
level::runAttack(this, GridLoc(x,y));
}
//void Broblub::move(GridLoc& pos){
// this->setGridLoc(pos);
//}
void Broblub::move(GridLoc& p) {
GridLoc curPos = getGridLoc();
VECTOR2 dirVec = VECTOR2((float)(p.x - curPos.x),(float)(p.y - curPos.y));
obstacleAvoidanceSurroundings = level::getSurroundings(curPos);
visibleTiles = level::getSurroundings(curPos,getSightRadius());
//used to get the direction chosen and move the nimkip and objects they hold
moverNS::DIR chosenDirection;
/////////////////////////////////////////////////////////////////////////////////////////////////
//new Move function using ternary direction choices and obstacle avoidance
int dirVal;
int a,b,c,d;
if(dirVec.x < 0)
a = -1;
else if(dirVec.x > 0)
a = 1;
else
a = 0;
if(-dirVec.y < 0)
b = -1;
else if(-dirVec.y > 0)
b = 1;
else
b = 0;
c = a;
if(c < 0) c = 2;
d = b;
if(d < 0) d = 2;
dirVal = 3*c + d; //Convert ternary value to decimal
switch(dirVal) { //Reassigning to match enum direction values
case 0: dirVal = 8; break;
case 1: dirVal = 2; break;
case 2: dirVal = 3; break;
case 3: dirVal = 1; break;
case 4: dirVal = 5; break;
case 5: dirVal = 7; break;
case 6: dirVal = 0; break;
case 7: dirVal = 4; break;
case 8: dirVal = 6; break;
}
if(a == 0 && b == 0)
return;
chosenDirection = (moverNS::DIR) dirVal;
bool yes = true;
if(yes && dirVal!=this->getPrevDir() && level::getTileType(GridLoc(curPos.x + a,curPos.y - b)) == EMPTY) {
this->setPrevDir(-1);//set their previous direction to nothin useful
//make all directions available
for(int i = 0; i < 8; i++)
{
availableDirections[i]=true;
}
if(level::getTileType(GridLoc(curPos.x + a,curPos.y - b)) == COIN) {
level::collectCoin(GridLoc(curPos.x + a,curPos.y - b));
setScored(true);
}
setDir(chosenDirection);
if(getHeldObject())
getHeldObject()->setDir(chosenDirection);
return;
}
else
{
availableDirections[dirVal]=false;
this->setPrevDir(dirVal);
switch(chosenDirection)
{
case LEFT:
if(availableDirections[UP_LEFT] && obstacleAvoidanceSurroundings.NW==EMPTY)
setDir(UP_LEFT);
else if(availableDirections[DOWN_LEFT] &&obstacleAvoidanceSurroundings.SW==EMPTY)
setDir(DOWN_LEFT);
else if(availableDirections[UP] &&obstacleAvoidanceSurroundings.N==EMPTY)
setDir(UP);
else if(availableDirections[DOWN] &&obstacleAvoidanceSurroundings.S==EMPTY)
setDir(DOWN);
case RIGHT:
if(availableDirections[UP_RIGHT] &&obstacleAvoidanceSurroundings.NE==EMPTY)
setDir(UP_RIGHT);
else if(availableDirections[DOWN_RIGHT] &&obstacleAvoidanceSurroundings.SE==EMPTY)
setDir(DOWN_RIGHT);
else if(availableDirections[UP] &&obstacleAvoidanceSurroundings.N==EMPTY)
setDir(UP);
else if(availableDirections[DOWN] &&obstacleAvoidanceSurroundings.S==EMPTY)
setDir(DOWN);
case UP:
if(availableDirections[UP_LEFT] &&obstacleAvoidanceSurroundings.NW==EMPTY)
setDir(UP_LEFT);
else if(availableDirections[UP_RIGHT] && obstacleAvoidanceSurroundings.NE==EMPTY)
setDir(UP_RIGHT);
else if(availableDirections[RIGHT] &&obstacleAvoidanceSurroundings.E==EMPTY)
setDir(RIGHT);
else if(availableDirections[LEFT] &&obstacleAvoidanceSurroundings.W==EMPTY)
setDir(LEFT);
case DOWN:
if(availableDirections[DOWN_LEFT] &&obstacleAvoidanceSurroundings.SW==EMPTY)
setDir(DOWN_LEFT);
else if(availableDirections[DOWN_RIGHT] &&obstacleAvoidanceSurroundings.SE==EMPTY)
setDir(DOWN_RIGHT);
else if(availableDirections[RIGHT] &&obstacleAvoidanceSurroundings.E==EMPTY)
setDir(RIGHT);
else if(availableDirections[LEFT] &&obstacleAvoidanceSurroundings.W==EMPTY)
setDir(LEFT);
case UP_RIGHT:
if(availableDirections[UP] &&obstacleAvoidanceSurroundings.N==EMPTY)
setDir(UP);
else if(availableDirections[RIGHT] &&obstacleAvoidanceSurroundings.E==EMPTY)
setDir(RIGHT);
else if(availableDirections[UP_LEFT] &&obstacleAvoidanceSurroundings.NW==EMPTY)
setDir(UP_LEFT);
else if(availableDirections[DOWN_RIGHT] &&obstacleAvoidanceSurroundings.SE==EMPTY)
setDir(DOWN_RIGHT);
case DOWN_RIGHT:
if(availableDirections[DOWN] &&obstacleAvoidanceSurroundings.S==EMPTY)
setDir(DOWN);
else if(availableDirections[RIGHT] &&obstacleAvoidanceSurroundings.E==EMPTY)
setDir(RIGHT);
else if(availableDirections[DOWN_LEFT] &&obstacleAvoidanceSurroundings.SW==EMPTY)
setDir(DOWN_LEFT);
else if(availableDirections[UP_RIGHT] &&obstacleAvoidanceSurroundings.NE==EMPTY)
setDir(UP_RIGHT);
case UP_LEFT:
if(availableDirections[UP] &&obstacleAvoidanceSurroundings.N==EMPTY)
setDir(UP);
else if(availableDirections[LEFT] &&obstacleAvoidanceSurroundings.W==EMPTY)
setDir(LEFT);
else if(availableDirections[UP_RIGHT] &&obstacleAvoidanceSurroundings.NE==EMPTY)
setDir(UP_RIGHT);
else if(availableDirections[DOWN_LEFT] &&obstacleAvoidanceSurroundings.SW==EMPTY)
setDir(DOWN_LEFT);
case DOWN_LEFT:
if(availableDirections[DOWN] &&obstacleAvoidanceSurroundings.S==EMPTY)
setDir(DOWN);
else if(availableDirections[LEFT] &&obstacleAvoidanceSurroundings.W==EMPTY)
setDir(LEFT);
else if(availableDirections[DOWN_RIGHT] &&obstacleAvoidanceSurroundings.SE==EMPTY)
setDir(DOWN_RIGHT);
else if(availableDirections[UP_LEFT] &&obstacleAvoidanceSurroundings.NW==EMPTY)
setDir(UP_LEFT);
}
}
}
void Broblub::move(int x, int y){
move(GridLoc(x,y));
}
GridLoc Broblub::takeTurn(){
if(this->getHealth()>0)
{
chooseAction(); //either sets to attack nimkip, set to move toward a close nimkip, or set to move in pattern
goTowardsGoal(); //attack or move toward a nimkip or in a pattern
}
else
{
die();
}
return pos;
}
void BlackBroblub::goTowardsGoal()
{
switch(this->task)
{
//right now, the pattern is a counter-clockwise circle, which breaks as soon as a nimkip is in their range of vision
//I will try to implement different patterns that can be set in the constructor
case MOVE:
if(pos.x==destination.x && pos.y==destination.y)
{
if(direction==UP) {
direction=LEFT;
if(pos.x-3>0)
destination = GridLoc(pos.x-3,pos.y);
}
else if(direction==LEFT) {
direction=DOWN;
if(pos.y+3<level::getGridHeight())
destination = GridLoc(pos.x,pos.y+3);
}
else if(direction==DOWN) {
direction=RIGHT;
if(pos.x+3<level::getGridWidth())
destination = GridLoc(pos.x+3,pos.y);
}
else if(direction==RIGHT) {
direction=UP;
if(pos.y-3>0)
destination = GridLoc(pos.x,pos.y-3);
}
}
move(destination); //requires working move function
case WALK:
if(pos.x==destination.x && pos.y==destination.y)
{
//I don't think this should ever matter
}
else
move(destination);
break;
case ATTACK:
attack(target);
break;
}
//return GridLoc();
}
void RedBroblub::goTowardsGoal()
{
switch(this->task)
{
case MOVE:
moveRandom(surroundings);
move(destination); //requires working move function
case WALK:
if(pos.x==destination.x && pos.y==destination.y)
{
//I don't think this should ever matter
}
else
move(destination);
break;
case ATTACK:
attack(target);
break;
}
//return GridLoc();
}
void BlackBroblub::chooseAction()
{
visibleTiles = level::getSurroundings(pos,getSightRadius());
//here?
surroundings = level::getSurroundings(pos,1);
//^^^^^^^
int stuff=9;
for(int i = 0; i < surroundings.size(); i++)
{
if(surroundings[i].type==NIMKIP)
{
task = ATTACK;
target = surroundings[i];
return;
}
}
//if nimkip in view, set it as destination
for(int i = 0; i < visibleTiles.size(); i++)
{
if(visibleTiles[i].type==NIMKIP)
{
task = WALK;
destination = visibleTiles[i];
setSpotted();
return;
}
}
//if no nimkips in view and not already moving in a pattern, set task as pattern
if(task != MOVE)
{
task = MOVE;
direction = UP;
destination = GridLoc(pos.x,pos.y-2);
setNormal();
}
}
void RedBroblub::chooseAction()
{
visibleTiles = level::getSurroundings(pos,getSightRadius());
//here?
surroundings = level::getSurroundings(pos,1);
//^^^^^^^
int stuff=9;
for(int i = 0; i < surroundings.size(); i++)
{
if(surroundings[i].type==NIMKIP)
{
task = ATTACK;
target = surroundings[i];
return;
}
}
//if nimkip in view, set it as destination
for(int i = 0; i < visibleTiles.size(); i++)
{
if(visibleTiles[i].type==NIMKIP)
{
task = WALK;
destination = visibleTiles[i];
setSpotted();
return;
}
}
//if no nimkips in view and not already moving in a pattern, set task as pattern
if(task != MOVE)
{
task = MOVE;
setNormal();
}
}
//specific to the sight radius of 1
//if other enemies were made with a larger radius the movement would have to be altered
void RedBroblub::moveRandom(vector<GridLoc> temp)
{
int r;
GridLoc loc = pos;
r = rand()%temp.size(); //rgen?
//choosing the direction is specific to the sightRadius of 1
//this makes them automattically move to a space
//should be modified so that the choice merely chooses where to go and then they have a move function to get to it
//similar to the nimkip's movement
if(temp[r].type==EMPTY)
{
//this->chooseDir(loc,temp[r]);
destination = temp[r];
}
}
void Broblub::die()
{
setActive(false);
setVisible(false);
}
//Update these as well
void RedBroblub::setAtk(){ setCurrentFrame(RBRO_ATK); }
void RedBroblub::setNormal(){ setCurrentFrame(RBRO); }
void RedBroblub::setHurt(){ setCurrentFrame(RBRO_HURT); }
void RedBroblub::setSpotted(){ setCurrentFrame(RBRO_SPOT); }
bool RedBroblub::isNormal(){return (getCurrentFrame()==RBRO);}
bool RedBroblub::isAtk(){return (getCurrentFrame()==RBRO_ATK);}
void BlackBroblub::setAtk(){ setCurrentFrame(BBRO_ATK); }
void BlackBroblub::setNormal(){ setCurrentFrame(BBRO); }
void BlackBroblub::setHurt(){ setCurrentFrame(BBRO_HURT); }
void BlackBroblub::setSpotted(){ setCurrentFrame(BBRO_SPOT); }
bool BlackBroblub::isNormal(){return (getCurrentFrame()==BBRO);}
bool BlackBroblub::isAtk(){return (getCurrentFrame()==BBRO_ATK);}