[12304] | 1 | |
---|
| 2 | #include "PacmanBrown.h" |
---|
| 3 | |
---|
| 4 | #include "core/CoreIncludes.h" |
---|
| 5 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | namespace orxonox{ |
---|
| 9 | |
---|
| 10 | RegisterClass(PacmanBrown); |
---|
| 11 | |
---|
| 12 | PacmanBrown::PacmanBrown(Context* context) : PacmanGhost(context){ |
---|
| 13 | |
---|
| 14 | RegisterObject(PacmanBrown); |
---|
| 15 | |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | @brief |
---|
| 20 | Method for creating a ghost through XML. |
---|
| 21 | */ |
---|
| 22 | void PacmanBrown::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 23 | { |
---|
| 24 | SUPER(PacmanBrown, XMLPort, xmlelement, mode); |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | |
---|
[12320] | 28 | int PacmanBrown::absoluteDistance(Vector3 pos1, Vector3 pos2){ |
---|
[12304] | 29 | |
---|
[12320] | 30 | |
---|
| 31 | Ogre::Vector3 diffVector; |
---|
| 32 | diffVector.x=pos2.x-pos1.x; |
---|
| 33 | diffVector.y=pos2.y-pos1.y; //should always be 0 |
---|
| 34 | diffVector.z=pos2.z-pos1.z; |
---|
| 35 | int result = sqrt((diffVector.x)*(diffVector.x)+(diffVector.z)*(diffVector.z)); |
---|
| 36 | return result; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | |
---|
[12304] | 40 | void PacmanBrown::tick(float dt) |
---|
| 41 | { |
---|
| 42 | SUPER(PacmanGhost, tick, dt); |
---|
| 43 | |
---|
| 44 | this->actuelposition = this->getPosition(); |
---|
| 45 | |
---|
| 46 | //Stop, if target arrived |
---|
| 47 | if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){ |
---|
| 48 | this->ismoving = false; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | //Move, if ghost hasn't arrived yet |
---|
| 52 | if(this->ismoving){ |
---|
| 53 | if(!(abs(this->actuelposition.z-target_z)<0.5)) { |
---|
| 54 | velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z)); |
---|
| 55 | move(dt, actuelposition, velocity); |
---|
| 56 | } |
---|
| 57 | if(!(abs(this->actuelposition.x-target_x)<0.5)){ |
---|
| 58 | velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0); |
---|
| 59 | move(dt, actuelposition, velocity); |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | //Check on which position the ghost has arrived and set new target |
---|
| 64 | else{ |
---|
| 65 | |
---|
| 66 | while(lockmove){}; |
---|
| 67 | lockmove = true; |
---|
| 68 | |
---|
| 69 | //do brown behavior |
---|
| 70 | //put everything needed here |
---|
| 71 | |
---|
[12320] | 72 | Vector3 brownPos=Vector3(this->target_x, 10, this->target_z); |
---|
| 73 | |
---|
| 74 | if(absoluteDistance(this->lastPlayerPassedPoint, this->actuelposition)<50){ |
---|
| 75 | //no idea if 50 is large enough |
---|
| 76 | |
---|
| 77 | //if near player, flee |
---|
| 78 | |
---|
| 79 | this->isFleeing=true; |
---|
| 80 | Vector3 nextMove=getShortestPath( brownPos,Vector3(-215, 10, -195)); |
---|
| 81 | setNewTargetGhost(nextMove); |
---|
| 82 | |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | else if((this->isFleeing==true)&&(brownPos!=Vector3(-215,10,-195))){ |
---|
| 86 | //if fleeing, continue to flee |
---|
| 87 | Vector3 nextMove = getShortestPath(brownPos, Vector3(-215, 10, -195)); |
---|
| 88 | setNewTargetGhost(nextMove); |
---|
| 89 | } |
---|
| 90 | else if ((this->isFleeing==true)&&(brownPos==Vector3(-215,10,-195))){ |
---|
| 91 | //if fleeing and reached flee point, go back near player |
---|
| 92 | this->isFleeing=false; |
---|
| 93 | Vector3 nextMove = getShortestPath(brownPos, this->lastPlayerPassedPoint); |
---|
| 94 | setNewTargetGhost(nextMove); |
---|
| 95 | } |
---|
| 96 | else{ |
---|
| 97 | //if not fleeing and not near player, go near player |
---|
| 98 | Vector3 nextMove = getShortestPath(brownPos, this->lastPlayerPassedPoint); |
---|
| 99 | setNewTargetGhost(nextMove); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | /*if((this->fleeing==true)&&(this->actuelposition!=(-215,10,-195))){ |
---|
[12304] | 107 | //if fleeing, then go to corner map if not already there |
---|
| 108 | fleeMode(); |
---|
| 109 | } |
---|
| 110 | else{ // else go next to pacman |
---|
| 111 | |
---|
| 112 | if(absoluteDistance(this->pos, player.pos)<10){//no idea if 10 ok |
---|
| 113 | //if near player, flee away |
---|
| 114 | fleemode(); |
---|
| 115 | } |
---|
| 116 | else{ //go to neighboor of player nearest to brown pacman |
---|
| 117 | |
---|
| 118 | //first find nearest neighboor point of player to brown pacman |
---|
| 119 | //we can maybe call getShortestPath recursively to do so |
---|
| 120 | |
---|
| 121 | player.pos.nearestneighboor=findPlayerNeighboorNearestToPacman(player.neighboors); |
---|
| 122 | //btw the array player.neighboor can be defined with a big list as we |
---|
| 123 | //already did several times for similar things |
---|
| 124 | |
---|
| 125 | Vector3 nextMove = getShortestPath(this->actuelposition, player.pos.nearestneighboor); |
---|
| 126 | setNewTargetBrown(nextMove); |
---|
| 127 | } |
---|
| 128 | |
---|
[12320] | 129 | }*/ |
---|
| 130 | lockmove=false; //NEVER FORGET THIS ONE !!! |
---|
[12304] | 131 | |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | |
---|
[12320] | 137 | |
---|
[12304] | 138 | |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | |
---|
[12320] | 146 | |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | /*int graphDistance(Vector3 start, Vector3 goal){ |
---|
| 152 | |
---|
[12304] | 153 | Vector3 differenceVector= Vector3(abs(goal.x-start.x), 0,abs(goal.z-start.z)); |
---|
| 154 | |
---|
| 155 | return differenceVector.x+differenceVector.z; |
---|
[12320] | 156 | }*/ |
---|
[12304] | 157 | |
---|
[12320] | 158 | /*void setNewTargetBrown(Vector3 goalToGo){ |
---|
[12304] | 159 | |
---|
| 160 | this->target_x = goalToGo.x; |
---|
| 161 | this->target_z = goalToGo.z; |
---|
| 162 | this->ismoving = true; |
---|
[12320] | 163 | }*/ |
---|
[12304] | 164 | |
---|
| 165 | |
---|
| 166 | |
---|
[12320] | 167 | /*int PacmanBrown::absoluteDistance(Vector3 pos1, Vector3 pos2){ |
---|
[12304] | 168 | |
---|
| 169 | |
---|
| 170 | Vector3 diffVector; |
---|
| 171 | diffVector.x=pos2-pos1; |
---|
| 172 | diffVector.y=pos2-pos1; //should always be 0 |
---|
| 173 | diffVector.z=pos2-pos1; |
---|
[12320] | 174 | int result = sqrt((diffVector.x)*(diffVector.x)+(diffVector.z)*(diffVector.z)); |
---|
[12304] | 175 | return result; |
---|
[12320] | 176 | }*/ |
---|
[12304] | 177 | |
---|
| 178 | |
---|
[12320] | 179 | /*void fleeMode(){ //flees to corner of the map |
---|
[12304] | 180 | |
---|
| 181 | Vector3 cornerPos = Vector3(-215,10,-195); //let's take this. We can still change |
---|
| 182 | Vector3 nextMove = getShortestPath(this->actuelposition, cornerPos); |
---|
[12320] | 183 | setNewTargetGhost(nextMove); |
---|
[12304] | 184 | |
---|
[12320] | 185 | //while(this->actuelposition!=cornerPos){ |
---|
[12304] | 186 | |
---|
| 187 | continue moving and each time find next point to go until |
---|
| 188 | we reach the corner |
---|
[12320] | 189 | } |
---|
| 190 | }*/ |
---|
[12304] | 191 | |
---|
| 192 | |
---|
[12320] | 193 | /*Vector3 goAdjacentOfPlayer(Vector neighboorsOfPlayer[]){ |
---|
[12304] | 194 | //find ,nearest to brown, player neighboor |
---|
| 195 | |
---|
| 196 | int besTotDist=-1; |
---|
| 197 | Vector3 curPos=this->pos; |
---|
| 198 | Vector3 predPos; |
---|
| 199 | Vector3 chosenNeighbor; |
---|
| 200 | |
---|
| 201 | for(int i=0; i < 4; i++){ |
---|
| 202 | int totDist=0; |
---|
| 203 | |
---|
| 204 | if(neighboorsOfPlayer[i]!=0){ |
---|
| 205 | while(curPos!=neighboorsOfPlayer[i]){ |
---|
| 206 | |
---|
| 207 | predPos=curPos; |
---|
| 208 | curPos = getShortestPath(curPos, neighboorsOfPlayer[i]); |
---|
| 209 | totDist+=graphDistance(curPos,predPos); |
---|
| 210 | |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | if(besTotDist==-1){ |
---|
| 215 | besTotDist=totDist; |
---|
| 216 | chosenNeighbor=neighboorsOfPlayer[i]; |
---|
| 217 | } |
---|
| 218 | else if(besTotDist>totDist){ |
---|
| 219 | besTotDist=totDist; |
---|
| 220 | chosenNeighbor=neighboorsOfPlayer[i]; |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | return chosenNeighbor; |
---|
| 224 | |
---|
[12320] | 225 | }*/ |
---|
[12304] | 226 | |
---|
| 227 | |
---|
[12320] | 228 | /*Vector3 findPlayerNeighboorNearestToPacman(Vector3 neighboorArray[]){ |
---|
[12304] | 229 | //uh, i think it does the same think as the function above |
---|
| 230 | |
---|
| 231 | Vector3 nextMove; |
---|
| 232 | int distCost=-1; |
---|
| 233 | for(int i=0; i < 4; i++){ |
---|
| 234 | int totDist=0; |
---|
| 235 | Vector3 newCandidate=this->actuelposition; |
---|
| 236 | Vector3 predPos; |
---|
| 237 | |
---|
| 238 | if(neighboorArray[i]!=NULL){ |
---|
| 239 | while(nextMove!=neighboorArray[i]){ |
---|
| 240 | predPos=newCandidate; |
---|
| 241 | newCandidate=getShortestPath(newCandidate, neighboorArray[i]); |
---|
| 242 | totDist+=graphDistance(predPos, newCandidate); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | if(distCost==-1){ |
---|
| 246 | distCost=totDist; |
---|
| 247 | nextMove=gneighboorArray[i]; |
---|
| 248 | } |
---|
| 249 | else if(totDist<distCost){ |
---|
| 250 | distCost=totDist; |
---|
| 251 | nextMove=neighboorArray[i]; |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | return nextMove; |
---|
| 256 | |
---|
[12320] | 257 | }*/ |
---|
[12304] | 258 | |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | |
---|
[12320] | 263 | |
---|