-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
812 lines (742 loc) · 30.8 KB
/
Copy pathEnemy.cpp
File metadata and controls
812 lines (742 loc) · 30.8 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
#include "Enemy.h"
#include <iostream>
//#include "Grid.h"
//////////////////
///////////////////////////CONSTRUCTORS AND SETUP////////////////////////////////////////////
/////////////////
Enemy::Enemy(Ogre::SceneManager* SceneManager, std::string name, std::string filename, float height, float scale)
{
using namespace Ogre;
mSceneMgr = SceneManager; // keep a pointer to where this enemy will be
if (mSceneMgr == NULL)
{
std::cout << "ERROR: No valid scene manager in Enemy constructor" << std::endl;
return;
}
this->height = height;
this->scale = scale;
enemyGrid = NULL;
pathNodeFrom = NULL;
pathNodeTo = NULL;
startNodeID = -1;
this->totalDH = -1;
ready = false;
mBodyNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); // create a new scene node
mBodyEntity = mSceneMgr->createEntity(name, filename); // load the model
mBodyNode->attachObject(mBodyEntity); // attach the model to the scene node
mBodyNode->translate(0,height,0); // make the Ogre stand on the plane (almost)
mBodyNode->scale(scale,scale,scale); // Scale the figure
this->setupAnimations();
// configure walking parameters
mWalkSpeed = 35.0f;
mDirection = Ogre::Vector3::ZERO;
}
Enemy::~Enemy(){
// mSceneMgr->destroySceneNode(mBodyNode); // Note that OGRE does not recommend doing this. It prefers to use clear scene
// mSceneMgr->destroyEntity(mBodyEntity);
// if(enemyGrid!=NULL)
// delete enemyGrid;
// if(pathNodeFrom!=NULL)
// delete pathNodeFrom;
// if(pathNodeTo!=NULL)
// delete pathNodeTo;
enemyGrid = NULL;
pathNodeFrom = NULL;
pathNodeTo = NULL;
// mSceneMgr->clearScene();
mBodyNode = NULL;
mBodyEntity = NULL;
// if(mSceneMgr!=NULL)
// delete mSceneMgr;
mSceneMgr = NULL;
}
void Enemy::setPosition(float x, float y, float z)
{
this->mBodyNode->setPosition(x, y + height, z);
}
Ogre::Vector3 Enemy::getPosition()
{ return this->mBodyNode->getPosition();}
Ogre::AxisAlignedBox Enemy::getEnemyBoundingBox()
{
return mBodyEntity->getWorldBoundingBox();
}
// update is called at every frame from GameApplication::addTime
void Enemy::update(Ogre::Real deltaTime)
{
if(ready)
{
this->updateAnimations(deltaTime); // Update animation playback
this->updateLocomote(deltaTime); // Update Locomotion
}
}
void Enemy::setupAnimations()
{
this->mTimer = 0; // Start from the beginning
this->mVerticalVelocity = 0; // Not jumping
// this is very important due to the nature of the exported animations
mBodyEntity->getSkeleton()->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE);
// Name of the animations for this character
Ogre::String animNames[] =
{"IdleBase", "IdleTop", "RunBase", "RunTop", "HandsClosed", "HandsRelaxed", "DrawSwords",
"SliceVertical", "SliceHorizontal", "Dance", "JumpStart", "JumpLoop", "JumpEnd"};
// populate our animation list
for (int i = 0; i < 13; i++)
{
mAnims[i] = mBodyEntity->getAnimationState(animNames[i]);
mAnims[i]->setLoop(true);
mFadingIn[i] = false;
mFadingOut[i] = false;
}
// start off in the idle state (top and bottom together)
setBaseAnimation(ANIM_IDLE_BASE);
setTopAnimation(ANIM_IDLE_TOP);
// relax the hands since we're not holding anything
mAnims[ANIM_HANDS_RELAXED]->setEnabled(true);
}
//////////////////
////////////////////////////////////ANIMATION////////////////////////////////////////////
/////////////////
void Enemy::setBaseAnimation(AnimID id, bool reset)
{
if (mBaseAnimID >= 0 && mBaseAnimID < 13)
{
// if we have an old animation, fade it out
mFadingIn[mBaseAnimID] = false;
mFadingOut[mBaseAnimID] = true;
}
mBaseAnimID = id;
if (id != ANIM_NONE)
{
// if we have a new animation, enable it and fade it in
mAnims[id]->setEnabled(true);
mAnims[id]->setWeight(0);
mFadingOut[id] = false;
mFadingIn[id] = true;
if (reset) mAnims[id]->setTimePosition(0);
}
}
void Enemy::setTopAnimation(AnimID id, bool reset)
{
if (mTopAnimID >= 0 && mTopAnimID < 13)
{
// if we have an old animation, fade it out
mFadingIn[mTopAnimID] = false;
mFadingOut[mTopAnimID] = true;
}
mTopAnimID = id;
if (id != ANIM_NONE)
{
// if we have a new animation, enable it and fade it in
mAnims[id]->setEnabled(true);
mAnims[id]->setWeight(0);
mFadingOut[id] = false;
mFadingIn[id] = true;
if (reset) mAnims[id]->setTimePosition(0);
}
}
void Enemy::updateAnimations(Ogre::Real deltaTime)
{
using namespace Ogre;
Real baseAnimSpeed = 1;
Real topAnimSpeed = 1;
mTimer += deltaTime; // how much time has passed since the last update
//if (mTopAnimID != ANIM_IDLE_TOP)
//if (mTopAnimID != ANIM_NONE)
//if (mTimer >= mAnims[mTopAnimID]->getLength())
// {
// setTopAnimation(ANIM_IDLE_TOP, true);
// setBaseAnimation(ANIM_IDLE_BASE, true);
// mTimer = 0;
// }
// increment the current base and top animation times
if (mBaseAnimID != ANIM_NONE) mAnims[mBaseAnimID]->addTime(deltaTime * baseAnimSpeed);
if (mTopAnimID != ANIM_NONE) mAnims[mTopAnimID]->addTime(deltaTime * topAnimSpeed);
// apply smooth transitioning between our animations
fadeAnimations(deltaTime);
}
void Enemy::fadeAnimations(Ogre::Real deltaTime)
{
using namespace Ogre;
for (int i = 0; i < 13; i++)
{
if (mFadingIn[i])
{
// slowly fade this animation in until it has full weight
Real newWeight = mAnims[i]->getWeight() + deltaTime * 7.5f; //ANIM_FADE_SPEED;
mAnims[i]->setWeight(Math::Clamp<Real>(newWeight, 0, 1));
if (newWeight >= 1) mFadingIn[i] = false;
}
else if (mFadingOut[i])
{
// slowly fade this animation out until it has no weight, and then disable it
Real newWeight = mAnims[i]->getWeight() - deltaTime * 7.5f; //ANIM_FADE_SPEED;
mAnims[i]->setWeight(Math::Clamp<Real>(newWeight, 0, 1));
if (newWeight <= 0)
{
mAnims[i]->setEnabled(false);
mFadingOut[i] = false;
}
}
}
}
bool Enemy::nextLocation()
{
//if there are no more destinations to walk to, return false
if (mWalkList.empty())
{
this->totalDH = -1;
if(startNodeID==pathNodeFrom->getID())
{
startNodeID = pathNodeTo->getID();
bool pathFound = findPath(pathNodeTo, pathNodeFrom, 0);
}
else
{
startNodeID = pathNodeFrom->getID();
bool pathFound = findPath(pathNodeFrom, pathNodeTo, 0);
}
enemyGrid->reset(); //reset the node's fields for the next enemy
}
//otherwise, set up for the next destination
mDestination = mWalkList.front(); //set the next destination
mDestination.y = height; //make sure the avatar doesn't sink or hover
mWalkList.pop_front(); //remove the destination from the deque
mDirection = mDestination - mBodyNode->getPosition(); //define which direction the char will face
mDistance = mDirection.normalise(); //normalize the Direction vector
setDirection();
return true;
}
void Enemy::updateLocomote(Ogre::Real deltaTime)
{
if (mDirection == Ogre::Vector3::ZERO)
{
if (nextLocation())
{
setBaseAnimation(ANIM_RUN_BASE);
setTopAnimation(ANIM_RUN_TOP);
}
}
else
{
//slow the animation to work better with speed the character is moving
Ogre::Real move = mWalkSpeed * deltaTime;
mDistance -= move;
//did we reach our destination?
if (mDistance <= 0)
{
//make sure the avatar doesn't sink or hover
mBodyNode->setPosition(mDestination);
mDirection = Ogre::Vector3::ZERO;
//rotate the character towards the next destination
if (nextLocation())
{
setDirection();
}
else
{
setBaseAnimation(ANIM_IDLE_BASE);
setTopAnimation(ANIM_IDLE_TOP);
}
}
else
{//If the destination hasn't been reached, continue moving along the path
//mDirection is defined in nexxtLocation()
mBodyNode->translate(move * mDirection);
}
}
}
void Enemy::setDirection()
{
//quaternions represent the rotation of a 3D object
Ogre::Vector3 src = mBodyNode->getOrientation() * Ogre::Vector3::UNIT_Z;
/*
//this code could result in a division by zero if we ever need to rotate by exactly 180 degrees
Ogre::Quaternion quat = src.getRotationTo(mDirection);
mBodyNode->rotate(quat);
*/
//due to the limitations of floating point numbers, they may not
//actually equal 0, but be very close to it. "close enough."
//So we check for that instead.
if ((1.0 + src.dotProduct(mDirection)) < 0.0001)
{
mBodyNode->yaw(Ogre::Degree(180));
}
else
{
Ogre::Quaternion quat = src.getRotationTo(mDirection);
mBodyNode->rotate(quat);
}
}
//////////////////
////////////////////////////////////DEFINE TRANSLATION////////////////////////////////////////////
/////////////////
void Enemy::walkTo(GridNode* gn, Grid* g)
{
//save the grid as a field variable for setWayPoints() to access.
enemyGrid = g;
//Remember: z is rows, x is columns
//grab the grid square that the enemy is currently on
Ogre::Vector3 enemyCoor = enemyGrid->getGridPosition(this->mBodyNode->getPosition().z, this->mBodyNode->getPosition().x);
//find the node representing the above grid square and call the pathfinding function
pathNodeFrom = enemyGrid->getNode(enemyCoor.z, enemyCoor.x);
pathNodeTo = gn;
startNodeID = pathNodeFrom->getID();
bool pathFound = findPath(pathNodeFrom, pathNodeTo, 0);
enemyGrid->reset(); //reset the node's fields for the next enemy
ready = true;
}
//takes the goalNode and traces all the way back to the startNode and saves the path
void Enemy::setWayPoints(GridNode* gn)
{
//if any path was stored before, erase it because a more optimal A* path was found.
if(!mWalkList.empty())
{ mWalkList.clear();}
int count = 0;
//start by adding the goalNode...
mWalkList.push_front(gn->getPosition(enemyGrid->getRows(), enemyGrid->getCols()));
//...and work back to startNode from there
while(gn->getPrevNode() != NULL)
{
count++;
if(count==15)
break;
gn = gn->getPrevNode();
mWalkList.push_front(gn->getPosition(enemyGrid->getRows(), enemyGrid->getCols()));
}
}
//This is a RECURSIVE function that implements pathfinding through A*
bool Enemy::findPath(GridNode* currentNode, GridNode* goalNode, int costSoFar)
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//SET UP
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
std::vector<GridNode*> adjList; //the list of the current node's neighbors
std::vector<GridNode*> toTraverse; //the list of nodes to visit next
currentNode->setVisited(); //mark the current node as visited
int minimum = -2;
int nodeIndex = 0;
int prevNodeIndex = -2; //-2 because PNI+1 is checked and -1 still isn't a proper index
GridNode* bestNode = NULL;
bool toReturn = false;
// std::cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" <<std::endl;
// std::cout<< "currently at node "<< currentNode->getRow() << "," << currentNode->getColumn() <<std::endl;
// std::cout<<"goalNode is row,col "<<goalNode->getRow()<<", "<<goalNode->getColumn()<<std::endl;
// std::cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" <<std::endl;
//STOP CONDITION 1 : if the goal node is reached
if(currentNode->getID() == goalNode->getID()) //compare IDs
{
// std::cout<< "Path found!" <<std::endl;
if((this->totalDH > (currentNode->getHeuristic() + costSoFar))||(this->totalDH == -1))
{
// std::cout<< "This is currently the shortest path." <<std::endl;
setWayPoints(currentNode);
this->totalDH = currentNode->getHeuristic() + costSoFar;
}
else
// { std::cout<< "...however, it's not the shortest path." <<std::endl;}
enemyGrid->softReset();
return true;
}
//STOP CONDITION 2 : if this path's d+h surpasses the totalDH (set by another path)
//if((this->totalDH < (currentNode->getHeuristic() + costSoFar))&&(this->totalDH != -1))
//{
// std::cout<< "Path halted, shorter path found." <<std::endl;
// std::cout<< "totalDH = "<<totalDH << std::endl;
// std::cout<< "this node's cost total: "<<currentNode->getHeuristic()<<" + "<<costSoFar<<std::endl;
// enemyGrid->softReset();
// return true;
//}
if(this->totalDH!=-1)
{
return true;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//start by picking the node with the best cost+heuristic
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//compiles a list of adjacent, non-Null node. Their costs have been set.
adjList = enemyGrid->getWalkableAdjacentNodes(currentNode);//grab neighbors
int counter = -1; //for index tracking
for(GridNode* adjNode : adjList)
{
counter++; //for index tracking
if(adjNode != NULL)
{
//update each neighbor's g+h
if(!adjNode->isHeuristicSet())
{ adjNode->setHeuristic(enemyGrid->getDistance(adjNode, goalNode));}
//only check the visitable nodes
if(adjNode->notVisited())//&&(adjNode->isClear()))
{
//have the first visitable node's heuristic set the minimum
if(minimum == -2)
{ minimum = adjNode->getCost() + adjNode->getHeuristic();}
//is the node's g+h < the minimum? Is the node also NOT the previous node nor the start node?
if(((adjNode->getCost() + adjNode->getHeuristic()) <= minimum)&&((currentNode->getPrevNode()==NULL)||(adjNode->getID() != currentNode->getPrevNode()->getID()))&&(adjNode->getID()!=startNodeID))
{
minimum = adjNode->getCost() + adjNode->getHeuristic();
// std::cout<<"minimum is "<<minimum<<std::endl;
// std::cout<<"node's cost is "<<adjNode->getCost()<<std::endl;
// std::cout<<"node's heuristic is "<<adjNode->getHeuristic()<<std::endl;
bestNode = adjNode;
nodeIndex = counter;
// std::cout<< "best node has a Row,Col of " << bestNode->getRow() << "," << bestNode->getColumn() <<std::endl;
}
}
//is the node is visited, check if it's previous and save its index
else
{
if(adjNode->getID()==currentNode->getPrevNode()->getID())
prevNodeIndex=counter;
}
}
// else{ std::cout<<"null node found."<<std::endl;}
}
if((bestNode!=NULL)&&(bestNode->isClear()))
{
// std::cout<< "bestNode is walkable." <<std::endl;
toTraverse.push_back(bestNode);
}//add to list of nodes to traverse
// else
// { std::cout<<"Best node is a wall."<<std::endl;}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//store the walkable adjacent nodes as well
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(bestNode!=NULL)
{
//we start by checking the directly-adjacent nodes
//clockwise node
if(nodeIndex==7)
{
if((adjList[0]!=NULL)&&(adjList[0]->isClear())&&(adjList[0]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=0)&&(adjList[0]->getID()!=startNodeID)&&(prevNodeIndex!=7)&&(prevNodeIndex!=1))
{
// std::cout<< "directly-adjacent node "<< adjList[0]->getRow() << "," << adjList[0]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[0]);
} } }
else
{
if((adjList[nodeIndex+1]!=NULL)&&(adjList[nodeIndex+1]->isClear())&&(adjList[nodeIndex+1]->notVisited()))
{
//make sure it isn't the previous node or start node
if((prevNodeIndex!=nodeIndex+1)&&(adjList[nodeIndex+1]->getID()!=startNodeID))
{
//make sure it isn't adjacent to prevNode
//only checking (nodeIndex+1)+1, not (nodeIndex+1)-1
if((nodeIndex+2==8)&&(prevNodeIndex!=0))
{
// std::cout<< "directly-adj+acent node "<< adjList[nodeIndex+1]->getRow() << "," << adjList[nodeIndex+1]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex+1]);
}
else if(nodeIndex+2!=prevNodeIndex)
{
// std::cout<< "directly-adjacent node "<< adjList[nodeIndex+1]->getRow() << "," << adjList[nodeIndex+1]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex+1]);
} } } }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//counter-clockwise node
if(nodeIndex==0)
{
if((adjList[7]!=NULL)&&(adjList[7]->isClear())&&(adjList[7]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=7)&&(adjList[7]->getID()!=startNodeID)&&(prevNodeIndex!=6)&&(prevNodeIndex!=0))
{
// std::cout<< "directly-adjacent node "<< adjList[7]->getRow() << "," << adjList[7]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[7]);
} } }
else
{
if((adjList[nodeIndex-1]!=NULL)&&(adjList[nodeIndex-1]->isClear())&&(adjList[nodeIndex-1]->notVisited()))
{
//make sure it isn't the previous node or start node
if((prevNodeIndex!=nodeIndex-1)&&(adjList[nodeIndex-1]->getID()!=startNodeID))
{
//make sure it isn't adjacent to prevNode
if((nodeIndex-2==-1)&&(prevNodeIndex!=7))
{
// std::cout<< "directly-adjacent node "<< adjList[nodeIndex-1]->getRow() << "," << adjList[nodeIndex-1]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex-1]);
}
if(nodeIndex-2!=prevNodeIndex)
{
// std::cout<< "directly-adjacent node "<< adjList[nodeIndex-1]->getRow() << "," << adjList[nodeIndex-1]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex-1]);
} } } }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//check direct-diagonal nodes
//clockwise node
if(nodeIndex==7)
{
if((adjList[1]!=NULL)&&(adjList[1]->isClear())&&(adjList[1]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=1)&&(adjList[1]->getID()!=startNodeID)&&(prevNodeIndex!=2)&&(prevNodeIndex!=0))
{
// std::cout<< "directly-diagonal node "<< adjList[1]->getRow() << "," << adjList[1]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[1]);
} } }
else if(nodeIndex==6)
{
if((adjList[0]!=NULL)&&(adjList[0]->isClear())&&(adjList[0]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=0)&&(adjList[0]->getID()!=startNodeID)&&(prevNodeIndex!=7)&&(prevNodeIndex!=1))
{
// std::cout<< "directly-diagonal node "<< adjList[0]->getRow() << "," << adjList[0]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[0]);
} } }
else
{
if((adjList[nodeIndex+2]!=NULL)&&(adjList[nodeIndex+2]->isClear())&&(adjList[nodeIndex+2]->notVisited()))
{
//make sure it isn't the previous node or start node
if((prevNodeIndex!=nodeIndex+2)&&(adjList[nodeIndex+2]->getID()!=startNodeID))
{
//make sure it isn't adjacent to prevNode
//check (nodeIndex+2)+1 and (nodeIndex+2)-1
if(((nodeIndex+3==8)&&(prevNodeIndex!=0))&&(nodeIndex+1!=prevNodeIndex))
{
// std::cout<< "directly-diagonal node "<< adjList[nodeIndex+2]->getRow() << "," << adjList[nodeIndex+2]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex+2]);
}
else if ((nodeIndex+3!=prevNodeIndex)&&(nodeIndex+1!=prevNodeIndex))
{
// std::cout<< "directly-diagonal node "<< adjList[nodeIndex+2]->getRow() << "," << adjList[nodeIndex+2]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex+2]);
} } } }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//counter-clockwise node
if(nodeIndex==0)
{
if((adjList[6]!=NULL)&&(adjList[6]->isClear())&&(adjList[6]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=6)&&(adjList[6]->getID()!=startNodeID)&&(prevNodeIndex!=7)&&(prevNodeIndex!=5))
{
// std::cout<< "directly-diagonal node "<< adjList[6]->getRow() << "," << adjList[6]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[6]);
} } }
else if(nodeIndex==1)
{
if((adjList[7]!=NULL)&&(adjList[7]->isClear())&&(adjList[7]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=7)&&(adjList[7]->getID()!=startNodeID)&&(prevNodeIndex!=0)&&(prevNodeIndex!=6))
{
// std::cout<< "directly-diagonal node "<< adjList[7]->getRow() << "," << adjList[7]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[7]);
} } }
else
{
if((adjList[nodeIndex-2]!=NULL)&&(adjList[nodeIndex-2]->isClear())&&(adjList[nodeIndex-2]->notVisited()))
{
//make sure it isn't the previous node or start node
if((prevNodeIndex!=nodeIndex-2)&&(adjList[nodeIndex-2]->getID()!=startNodeID))
{
//make sure it isn't adjacent to prevNode
//check (nodeIndex-2)+1 and (nodeIndex-2)-1
if((nodeIndex-3==-1)&&(prevNodeIndex!=7)&&(prevNodeIndex!=nodeIndex-1))
{
// std::cout<< "directly-diagonal node "<< adjList[nodeIndex-2]->getRow() << "," << adjList[nodeIndex-2]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex-2]);
}
else if((nodeIndex-3!=prevNodeIndex)&&(prevNodeIndex!=nodeIndex-1))
{
// std::cout<< "directly-diagonal node "<< adjList[nodeIndex-2]->getRow() << "," << adjList[nodeIndex-2]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex-2]);
} } } }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//check adjacent-opposite nodes
//clockwise node
if(nodeIndex==7)
{
if((adjList[2]!=NULL)&&(adjList[2]->isClear())&&(adjList[2]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=2)&&(adjList[2]->getID()!=startNodeID)&&(prevNodeIndex!=3)&&(prevNodeIndex!=1))
{
// std::cout<< "adjacent-opposite node "<< adjList[2]->getRow() << "," << adjList[2]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[2]);
} } }
else if(nodeIndex==6)
{
if((adjList[1]!=NULL)&&(adjList[1]->isClear())&&(adjList[1]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=1)&&(adjList[1]->getID()!=startNodeID)&&(prevNodeIndex!=2)&&(prevNodeIndex!=0))
{
// std::cout<< "adjacent-opposite node "<< adjList[1]->getRow() << "," << adjList[1]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[1]);
} } }
else if(nodeIndex==5)
{
if((adjList[0]!=NULL)&&(adjList[0]->isClear())&&(adjList[0]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=0)&&(adjList[0]->getID()!=startNodeID)&&(prevNodeIndex!=7)&&(prevNodeIndex!=1))
{
// std::cout<< "adjacent-opposite node "<< adjList[0]->getRow() << "," << adjList[0]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[0]);
} } }
else
{
if((adjList[nodeIndex+3]!=NULL)&&(adjList[nodeIndex+3]->isClear())&&(adjList[nodeIndex+3]->notVisited()))
{
//make sure it isn't the previous node or start node
if((prevNodeIndex!=nodeIndex+3)&&(adjList[nodeIndex+3]->getID()!=startNodeID))
{
//make sure it isn't adjacent to prevNode
//check (nodeIndex+3)+1 and (nodeIndex+3)-1
if((nodeIndex+4==8)&&(prevNodeIndex!=0)&&(prevNodeIndex!=nodeIndex+2))
{
// std::cout<< "adjacent-opposite node "<< adjList[nodeIndex+3]->getRow() << "," << adjList[nodeIndex+3]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex+3]);
}
else if((nodeIndex+4!=prevNodeIndex)&&(nodeIndex+2!=prevNodeIndex))
{
// std::cout<< "adjacent-opposite node "<< adjList[nodeIndex+3]->getRow() << "," << adjList[nodeIndex+3]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex+3]);
} } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//counter-clockwise node
if(nodeIndex==0)
{
if((adjList[5]!=NULL)&&(adjList[5]->isClear())&&(adjList[5]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=5)&&(adjList[5]->getID()!=startNodeID)&&(prevNodeIndex!=4)&&(prevNodeIndex!=6))
{
// std::cout<< "adjacent-opposite node "<< adjList[5]->getRow() << "," << adjList[5]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[5]);
} } }
else if(nodeIndex==1)
{
if((adjList[6]!=NULL)&&(adjList[6]->isClear())&&(adjList[6]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=6)&&(adjList[6]->getID()!=startNodeID)&&(prevNodeIndex!=7)&&(prevNodeIndex!=5))
{
// std::cout<< "adjacent-opposite node "<< adjList[6]->getRow() << "," << adjList[6]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[6]);
} } }
else if(nodeIndex==2)
{
if((adjList[7]!=NULL)&&(adjList[7]->isClear())&&(adjList[7]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=7)&&(adjList[7]->getID()!=startNodeID)&&(prevNodeIndex!=6)&&(prevNodeIndex!=0))
{
// std::cout<< "adjacent-opposite node "<< adjList[7]->getRow() << "," << adjList[7]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[7]);
} } }
else
{
if((adjList[nodeIndex-3]!=NULL)&&(adjList[nodeIndex-3]->isClear())&&(adjList[nodeIndex-3]->notVisited()))
{
//make sure it isn't the previous node or start node
if((prevNodeIndex!=nodeIndex-3)&&(adjList[nodeIndex-3]->getID()!=startNodeID))
{
//make sure it isn't adjacent to prevNode
//check (nodeIndex-3)+1 and (nodeIndex-3)-1
if((nodeIndex-4==-1)&&(prevNodeIndex!=7)&&(prevNodeIndex!=nodeIndex-2))
{
// std::cout<< "adjacent-opposite node "<< adjList[nodeIndex-3]->getRow() << "," << adjList[nodeIndex-3]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex-3]);
}
else if((prevNodeIndex!=nodeIndex-4)&&(prevNodeIndex!=nodeIndex-2))
{
// std::cout<< "adjacent-opposite node "<< adjList[nodeIndex-3]->getRow() << "," << adjList[nodeIndex-3]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex-3]);
} } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//check the direct-accross node.
if(nodeIndex==0)
{
if((adjList[4]!=NULL)&&(adjList[4]->isClear())&&(adjList[4]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=4)&&(adjList[4]->getID()!=startNodeID)&&(prevNodeIndex!=3)&&(prevNodeIndex!=5))
{
// std::cout<< "direct-accross node "<< adjList[4]->getRow() << "," << adjList[4]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[4]);
} } }
else if(nodeIndex==1)
{
if((adjList[5]!=NULL)&&(adjList[5]->isClear())&&(adjList[5]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=5)&&(adjList[5]->getID()!=startNodeID)&&(prevNodeIndex!=4)&&(prevNodeIndex!=6))
{
// std::cout<< "direct-accross node "<< adjList[5]->getRow() << "," << adjList[5]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[5]);
} } }
else if(nodeIndex==2)
{
if((adjList[6]!=NULL)&&(adjList[6]->isClear())&&(adjList[6]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=6)&&(adjList[6]->getID()!=startNodeID)&&(prevNodeIndex!=7)&&(prevNodeIndex!=5))
{
// std::cout<< "direct-accross node "<< adjList[6]->getRow() << "," << adjList[6]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[6]);
} } }
else if(nodeIndex==3)
{
if((adjList[7]!=NULL)&&(adjList[7]->isClear())&&(adjList[7]->notVisited()))
{
//make sure it isn't the previous node, nor the start node, nor the nodes adjacent to the previous node
if((prevNodeIndex!=7)&&(adjList[7]->getID()!=startNodeID)&&(prevNodeIndex!=0)&&(prevNodeIndex!=6))
{
// std::cout<< "direct-accross node "<< adjList[7]->getRow() << "," << adjList[7]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[7]);
} } }
else if((adjList[nodeIndex-4]!=NULL)&&(adjList[nodeIndex-4]->isClear())&&(adjList[nodeIndex-4]->notVisited()))
{
if((prevNodeIndex!=nodeIndex-4)&&(adjList[nodeIndex-4]->getID()!=startNodeID))
{
//make sure it isn't adjacent to prevNode
//check (nodeIndex-4)+1 and (nodeIndex-4)-1
if((nodeIndex-5==-1)&&(prevNodeIndex!=7)&&(prevNodeIndex!=nodeIndex-3))
{
// std::cout<< "direct-accross node "<< adjList[nodeIndex-4]->getRow() << "," << adjList[nodeIndex-4]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex-4]);
}
else if((prevNodeIndex!=nodeIndex-5)&&(prevNodeIndex!=nodeIndex-3))
{
// std::cout<< "direct-accross node "<< adjList[nodeIndex-4]->getRow() << "," << adjList[nodeIndex-4]->getColumn() << " is walkable." <<std::endl;
toTraverse.push_back(adjList[nodeIndex-4]);
} } } }
if(toTraverse.empty())
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//have we reached a dead end? start backtracking
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// std::cout<< "A dead end was reached." <<std::endl;
//if we have backtracked to the starter node, there is no path to the goal
if(currentNode->getPrevNode() == NULL)
{
// std::cout<< "No path was found!" <<std::endl;
enemyGrid->softReset();
return false;
}
// std::cout<< "Turning around..." <<std::endl;
//otherwise, backup and pick another direction
toReturn = findPath(currentNode->getPrevNode(), goalNode, (costSoFar - currentNode->getCost()));
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Start following the chosen path(s)!
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for(GridNode* toVisit : toTraverse)//for each node in list of nodes to traverse
{
toVisit->setPrevNode(currentNode);
// std::cout<< "now at node "<< currentNode->getRow() << "," << currentNode->getColumn() <<std::endl;
// std::cout<< "about to visit "<< toVisit->getRow() << "," << toVisit->getColumn() <<std::endl;
toReturn = findPath(toVisit, goalNode, (costSoFar + currentNode->getCost()));
}
// std::cout<<"...backtracking to an unexplored branch... "<<std::endl;
currentNode->reset(); //sets prevNode to NULL to avoid complications
//don't forget to return whether or not a path has been found
return toReturn;
}