1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * Dominik Solenicki |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "DivisionController.h" |
---|
30 | #include "infos/PlayerInfo.h" |
---|
31 | |
---|
32 | namespace orxonox |
---|
33 | { |
---|
34 | |
---|
35 | RegisterClass(DivisionController); |
---|
36 | |
---|
37 | //Leaders share the fact that they have Wingmans |
---|
38 | DivisionController::DivisionController(Context* context) : LeaderController(context) |
---|
39 | { |
---|
40 | RegisterObject(DivisionController); |
---|
41 | |
---|
42 | this->setFormationMode(FormationMode::DIAMOND); |
---|
43 | this->target_ = 0; |
---|
44 | this->myFollower_ = 0; |
---|
45 | this->myWingman_ = 0; |
---|
46 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DivisionController::action, this))); |
---|
47 | this->rank_ = Rank::DIVISIONLEADER; |
---|
48 | } |
---|
49 | |
---|
50 | DivisionController::~DivisionController() |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | void DivisionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
55 | { |
---|
56 | SUPER(DivisionController, XMLPort, xmlelement, mode); |
---|
57 | |
---|
58 | //XMLPortParam(DivisionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
59 | } |
---|
60 | void DivisionController::tick(float dt) |
---|
61 | { |
---|
62 | if (!this->isActive()) |
---|
63 | return; |
---|
64 | SUPER(DivisionController, tick, dt); |
---|
65 | } |
---|
66 | void DivisionController::action() |
---|
67 | { |
---|
68 | if (this->startAttackingEnemiesThatAreClose()) |
---|
69 | { |
---|
70 | Point p = { Action::FIGHT, "", Vector3::ZERO }; |
---|
71 | |
---|
72 | if (this->myWingman_) |
---|
73 | { |
---|
74 | this->myWingman_->parsedActionpoints_.push_back(p); |
---|
75 | } |
---|
76 | if (this->myFollower_) |
---|
77 | { |
---|
78 | this->myFollower_->parsedActionpoints_.push_back(p); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | if (this->action_ == Action::NONE) |
---|
83 | { |
---|
84 | this->executeActionpoint(); |
---|
85 | } |
---|
86 | if (this->action_ == Action::FIGHT || this->action_ == Action::FIGHTALL) |
---|
87 | { |
---|
88 | if (!this->hasTarget()) |
---|
89 | { |
---|
90 | //----find a target---- |
---|
91 | ControllableEntity* newTarget = this->closestTarget(); |
---|
92 | if (this->action_ == Action::FIGHT) |
---|
93 | { |
---|
94 | if (newTarget && |
---|
95 | CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_) |
---|
96 | { |
---|
97 | this->setAction (Action::FIGHT, newTarget); |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | this->nextActionpoint(); |
---|
102 | if (this->myWingman_) |
---|
103 | { |
---|
104 | this->myWingman_->nextActionpoint(); |
---|
105 | } |
---|
106 | if (this->myFollower_) |
---|
107 | { |
---|
108 | this->myFollower_->nextActionpoint(); |
---|
109 | } |
---|
110 | return; |
---|
111 | } |
---|
112 | } |
---|
113 | else if (this->action_ == Action::FIGHTALL) |
---|
114 | { |
---|
115 | if (newTarget && newTarget->getController()) |
---|
116 | { |
---|
117 | this->setAction (Action::FIGHTALL, newTarget); |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | this->nextActionpoint(); |
---|
122 | if (this->myWingman_) |
---|
123 | { |
---|
124 | this->myWingman_->nextActionpoint(); |
---|
125 | } |
---|
126 | if (this->myFollower_) |
---|
127 | { |
---|
128 | this->myFollower_->nextActionpoint(); |
---|
129 | } |
---|
130 | return; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | } |
---|
135 | else if (this->hasTarget()) |
---|
136 | { |
---|
137 | //----fly in formation if far enough---- |
---|
138 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
139 | |
---|
140 | if (diffVector.length() > this->attackRange_) |
---|
141 | { |
---|
142 | if (this->action_ == Action::FIGHT) |
---|
143 | { |
---|
144 | this->nextActionpoint(); |
---|
145 | if (this->myWingman_) |
---|
146 | { |
---|
147 | this->myWingman_->nextActionpoint(); |
---|
148 | } |
---|
149 | if (this->myFollower_) |
---|
150 | { |
---|
151 | this->myFollower_->nextActionpoint(); |
---|
152 | } |
---|
153 | return; |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | this->setTargetPositionOfWingman(); |
---|
158 | this->setTargetPositionOfFollower(); |
---|
159 | } |
---|
160 | } |
---|
161 | else |
---|
162 | { |
---|
163 | //----wingmans shall support the fire of their leaders---- |
---|
164 | if (this->myWingman_) |
---|
165 | { |
---|
166 | this->myWingman_->setAction (Action::FIGHT, this->target_); |
---|
167 | } |
---|
168 | if (this->myFollower_) |
---|
169 | { |
---|
170 | this->myFollower_->setAction (Action::FIGHT); |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | else if (this->action_ == Action::FLY) |
---|
176 | { |
---|
177 | if (this->squaredDistanceToTarget() <= this->squaredaccuracy_) |
---|
178 | { |
---|
179 | this->nextActionpoint(); |
---|
180 | if (this->myWingman_) |
---|
181 | { |
---|
182 | this->myWingman_->nextActionpoint(); |
---|
183 | } |
---|
184 | if (this->myFollower_) |
---|
185 | { |
---|
186 | this->myFollower_->nextActionpoint(); |
---|
187 | } |
---|
188 | return; |
---|
189 | } |
---|
190 | this->setTargetPositionOfWingman(); |
---|
191 | this->setTargetPositionOfFollower(); |
---|
192 | } |
---|
193 | else if (this->action_ == Action::PROTECT) |
---|
194 | { |
---|
195 | if (!this->getProtect()) |
---|
196 | { |
---|
197 | this->nextActionpoint(); |
---|
198 | if (this->myWingman_) |
---|
199 | { |
---|
200 | this->myWingman_->nextActionpoint(); |
---|
201 | } |
---|
202 | if (this->myFollower_) |
---|
203 | { |
---|
204 | this->myFollower_->nextActionpoint(); |
---|
205 | } |
---|
206 | return; |
---|
207 | } |
---|
208 | |
---|
209 | Vector3* targetRelativePosition; |
---|
210 | |
---|
211 | targetRelativePosition = new Vector3 (0, 300, 300); |
---|
212 | |
---|
213 | Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + |
---|
214 | (this->getProtect()->getWorldOrientation()* (*targetRelativePosition))); |
---|
215 | this->setTargetPosition(targetAbsolutePosition); |
---|
216 | |
---|
217 | this->setTargetPositionOfWingman(); |
---|
218 | this->setTargetPositionOfFollower(); |
---|
219 | |
---|
220 | } |
---|
221 | else if (this->action_ == Action::ATTACK) |
---|
222 | { |
---|
223 | if (!this->hasTarget()) |
---|
224 | { |
---|
225 | this->nextActionpoint(); |
---|
226 | if (this->myWingman_) |
---|
227 | { |
---|
228 | this->myWingman_->nextActionpoint(); |
---|
229 | } |
---|
230 | if (this->myFollower_) |
---|
231 | { |
---|
232 | this->myFollower_->nextActionpoint(); |
---|
233 | } |
---|
234 | return; |
---|
235 | } |
---|
236 | //----fly in formation if far enough---- |
---|
237 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
238 | if (diffVector.length() > this->attackRange_) |
---|
239 | { |
---|
240 | this->setTargetPositionOfWingman(); |
---|
241 | this->setTargetPositionOfFollower(); |
---|
242 | } |
---|
243 | else |
---|
244 | { |
---|
245 | //----wingmans shall support the fire of their leaders---- |
---|
246 | if (this->myWingman_) |
---|
247 | { |
---|
248 | this->myWingman_->setAction (Action::FIGHT, this->target_); |
---|
249 | } |
---|
250 | if (this->myFollower_) |
---|
251 | { |
---|
252 | this->myFollower_->setAction (Action::FIGHT); |
---|
253 | } |
---|
254 | } |
---|
255 | } |
---|
256 | if (this->hasTarget()) |
---|
257 | { |
---|
258 | //----choose where to go---- |
---|
259 | this->maneuver(); |
---|
260 | //----fire if you can---- |
---|
261 | this->bShooting_ = this->canFire(); |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | void DivisionController::setTargetPositionOfWingman() |
---|
266 | { |
---|
267 | if (!this->myWingman_) |
---|
268 | return; |
---|
269 | Vector3* targetRelativePositionOfWingman; |
---|
270 | switch (this->formationMode_){ |
---|
271 | case FormationMode::WALL: |
---|
272 | { |
---|
273 | targetRelativePositionOfWingman = new Vector3 (400, 0, 0); |
---|
274 | break; |
---|
275 | } |
---|
276 | case FormationMode::FINGER4: |
---|
277 | { |
---|
278 | targetRelativePositionOfWingman = new Vector3 (400, 0, 200); |
---|
279 | break; |
---|
280 | } |
---|
281 | |
---|
282 | case FormationMode::DIAMOND: |
---|
283 | { |
---|
284 | targetRelativePositionOfWingman = new Vector3 (400, 0, 200); |
---|
285 | break; |
---|
286 | } |
---|
287 | } |
---|
288 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
---|
289 | |
---|
290 | Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + |
---|
291 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); |
---|
292 | |
---|
293 | myWingman_->setAction( Action::FLY, targetAbsolutePositionOfWingman, orient); |
---|
294 | |
---|
295 | } |
---|
296 | void DivisionController::setTargetPositionOfFollower() |
---|
297 | { |
---|
298 | if (!this->myFollower_) |
---|
299 | return; |
---|
300 | this->myFollower_->setFormationMode(this->formationMode_); |
---|
301 | |
---|
302 | Vector3* targetRelativePositionOfFollower; |
---|
303 | switch (this->formationMode_){ |
---|
304 | case FormationMode::WALL: |
---|
305 | { |
---|
306 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 0); |
---|
307 | break; |
---|
308 | } |
---|
309 | case FormationMode::FINGER4: |
---|
310 | { |
---|
311 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); |
---|
312 | break; |
---|
313 | } |
---|
314 | |
---|
315 | case FormationMode::DIAMOND: |
---|
316 | { |
---|
317 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); |
---|
318 | break; |
---|
319 | } |
---|
320 | } |
---|
321 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
---|
322 | |
---|
323 | Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + |
---|
324 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower))); |
---|
325 | |
---|
326 | myFollower_->setAction ( Action::FLY, targetAbsolutePositionOfFollower, orient ); |
---|
327 | } |
---|
328 | bool DivisionController::setWingman(CommonController* cwingman) |
---|
329 | { |
---|
330 | |
---|
331 | WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman); |
---|
332 | if (!this->myWingman_) |
---|
333 | { |
---|
334 | this->myWingman_ = wingman; |
---|
335 | return true; |
---|
336 | } |
---|
337 | else |
---|
338 | { |
---|
339 | return false; |
---|
340 | } |
---|
341 | } |
---|
342 | bool DivisionController::setFollower(LeaderController* myFollower) |
---|
343 | { |
---|
344 | if (!this->myFollower_) |
---|
345 | { |
---|
346 | this->myFollower_ = myFollower; |
---|
347 | return true; |
---|
348 | } |
---|
349 | else |
---|
350 | { |
---|
351 | return false; |
---|
352 | } |
---|
353 | } |
---|
354 | bool DivisionController::hasWingman() |
---|
355 | { |
---|
356 | if (this->myWingman_) |
---|
357 | return true; |
---|
358 | else |
---|
359 | return false; |
---|
360 | } |
---|
361 | bool DivisionController::hasFollower() |
---|
362 | { |
---|
363 | if (this->myFollower_) |
---|
364 | return true; |
---|
365 | else |
---|
366 | return false; |
---|
367 | } |
---|
368 | } |
---|