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 | |
---|
28 | |
---|
29 | void PacmanBrown::tick(float dt) |
---|
30 | { |
---|
31 | SUPER(PacmanGhost, tick, dt); |
---|
32 | |
---|
33 | this->actuelposition = this->getPosition(); |
---|
34 | |
---|
35 | //Stop, if target arrived |
---|
36 | if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){ |
---|
37 | this->ismoving = false; |
---|
38 | } |
---|
39 | |
---|
40 | //Move, if ghost hasn't arrived yet |
---|
41 | if(this->ismoving){ |
---|
42 | if(!(abs(this->actuelposition.z-target_z)<0.5)) { |
---|
43 | velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z)); |
---|
44 | move(dt, actuelposition, velocity); |
---|
45 | } |
---|
46 | if(!(abs(this->actuelposition.x-target_x)<0.5)){ |
---|
47 | velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0); |
---|
48 | move(dt, actuelposition, velocity); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | //Check on which position the ghost has arrived and set new target |
---|
53 | else{ |
---|
54 | |
---|
55 | while(lockmove){}; |
---|
56 | lockmove = true; |
---|
57 | |
---|
58 | //do brown behavior |
---|
59 | //put everything needed here |
---|
60 | |
---|
61 | if((this->fleeing==true)&&(this->actuelposition!=(-215,10,-195))){ |
---|
62 | //if fleeing, then go to corner map if not already there |
---|
63 | fleeMode(); |
---|
64 | } |
---|
65 | else{ // else go next to pacman |
---|
66 | |
---|
67 | if(absoluteDistance(this->pos, player.pos)<10){//no idea if 10 ok |
---|
68 | //if near player, flee away |
---|
69 | fleemode(); |
---|
70 | } |
---|
71 | else{ //go to neighboor of player nearest to brown pacman |
---|
72 | |
---|
73 | //first find nearest neighboor point of player to brown pacman |
---|
74 | //we can maybe call getShortestPath recursively to do so |
---|
75 | |
---|
76 | player.pos.nearestneighboor=findPlayerNeighboorNearestToPacman(player.neighboors); |
---|
77 | //btw the array player.neighboor can be defined with a big list as we |
---|
78 | //already did several times for similar things |
---|
79 | |
---|
80 | Vector3 nextMove = getShortestPath(this->actuelposition, player.pos.nearestneighboor); |
---|
81 | setNewTargetBrown(nextMove); |
---|
82 | } |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | int graphDistance(Vector3 start, Vector3 goal){ |
---|
100 | |
---|
101 | Vector3 differenceVector= Vector3(abs(goal.x-start.x), 0,abs(goal.z-start.z)); |
---|
102 | |
---|
103 | return differenceVector.x+differenceVector.z; |
---|
104 | } |
---|
105 | |
---|
106 | void setNewTargetBrown(Vector3 goalToGo){ |
---|
107 | |
---|
108 | this->target_x = goalToGo.x; |
---|
109 | this->target_z = goalToGo.z; |
---|
110 | this->ismoving = true; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | int absoluteDistance(Vector3 pos1, Vector3 pos2){ |
---|
116 | |
---|
117 | |
---|
118 | Vector3 diffVector; |
---|
119 | diffVector.x=pos2-pos1; |
---|
120 | diffVector.y=pos2-pos1; //should always be 0 |
---|
121 | diffVector.z=pos2-pos1; |
---|
122 | int result = sqrt(diffVector.x²+diffVector.z²); |
---|
123 | return result; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | void fleeMode(){ //flees to corner of the map |
---|
128 | |
---|
129 | Vector3 cornerPos = Vector3(-215,10,-195); //let's take this. We can still change |
---|
130 | Vector3 nextMove = getShortestPath(this->actuelposition, cornerPos); |
---|
131 | setNewTargetBrown(nextMove); |
---|
132 | |
---|
133 | /*//while(this->actuelposition!=cornerPos){ |
---|
134 | |
---|
135 | continue moving and each time find next point to go until |
---|
136 | we reach the corner |
---|
137 | }*/ |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | Vector3 goAdjacentOfPlayer(Vector neighboorsOfPlayer[]){ |
---|
142 | //find ,nearest to brown, player neighboor |
---|
143 | |
---|
144 | int besTotDist=-1; |
---|
145 | Vector3 curPos=this->pos; |
---|
146 | Vector3 predPos; |
---|
147 | Vector3 chosenNeighbor; |
---|
148 | |
---|
149 | for(int i=0; i < 4; i++){ |
---|
150 | int totDist=0; |
---|
151 | |
---|
152 | if(neighboorsOfPlayer[i]!=0){ |
---|
153 | while(curPos!=neighboorsOfPlayer[i]){ |
---|
154 | |
---|
155 | predPos=curPos; |
---|
156 | curPos = getShortestPath(curPos, neighboorsOfPlayer[i]); |
---|
157 | totDist+=graphDistance(curPos,predPos); |
---|
158 | |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | if(besTotDist==-1){ |
---|
163 | besTotDist=totDist; |
---|
164 | chosenNeighbor=neighboorsOfPlayer[i]; |
---|
165 | } |
---|
166 | else if(besTotDist>totDist){ |
---|
167 | besTotDist=totDist; |
---|
168 | chosenNeighbor=neighboorsOfPlayer[i]; |
---|
169 | } |
---|
170 | } |
---|
171 | return chosenNeighbor; |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | Vector3 findPlayerNeighboorNearestToPacman(Vector3 neighboorArray[]){ |
---|
177 | //uh, i think it does the same think as the function above |
---|
178 | |
---|
179 | Vector3 nextMove; |
---|
180 | int distCost=-1; |
---|
181 | for(int i=0; i < 4; i++){ |
---|
182 | int totDist=0; |
---|
183 | Vector3 newCandidate=this->actuelposition; |
---|
184 | Vector3 predPos; |
---|
185 | |
---|
186 | if(neighboorArray[i]!=NULL){ |
---|
187 | while(nextMove!=neighboorArray[i]){ |
---|
188 | predPos=newCandidate; |
---|
189 | newCandidate=getShortestPath(newCandidate, neighboorArray[i]); |
---|
190 | totDist+=graphDistance(predPos, newCandidate); |
---|
191 | } |
---|
192 | |
---|
193 | if(distCost==-1){ |
---|
194 | distCost=totDist; |
---|
195 | nextMove=gneighboorArray[i]; |
---|
196 | } |
---|
197 | else if(totDist<distCost){ |
---|
198 | distCost=totDist; |
---|
199 | nextMove=neighboorArray[i]; |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | return nextMove; |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | |
---|