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 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "HumanController.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "core/command/ConsoleCommand.h" |
---|
33 | #include "worldentities/ControllableEntity.h" |
---|
34 | #include "worldentities/pawns/Pawn.h" |
---|
35 | #include "gametypes/Gametype.h" |
---|
36 | #include "infos/PlayerInfo.h" |
---|
37 | #include "Radar.h" |
---|
38 | #include "Scene.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | extern const std::string __CC_fire_name = "fire"; |
---|
43 | extern const std::string __CC_suicide_name = "suicide"; |
---|
44 | const std::string __CC_boost_name = "boost"; |
---|
45 | |
---|
46 | SetConsoleCommand("HumanController", "moveFrontBack", &HumanController::moveFrontBack ).addShortcut().setAsInputCommand(); |
---|
47 | SetConsoleCommand("HumanController", "moveRightLeft", &HumanController::moveRightLeft ).addShortcut().setAsInputCommand(); |
---|
48 | SetConsoleCommand("HumanController", "moveUpDown", &HumanController::moveUpDown ).addShortcut().setAsInputCommand(); |
---|
49 | SetConsoleCommand("HumanController", "rotateYaw", &HumanController::rotateYaw ).addShortcut().setAsInputCommand(); |
---|
50 | SetConsoleCommand("HumanController", "rotatePitch", &HumanController::rotatePitch ).addShortcut().setAsInputCommand(); |
---|
51 | SetConsoleCommand("HumanController", "rotateRoll", &HumanController::rotateRoll ).addShortcut().setAsInputCommand(); |
---|
52 | SetConsoleCommand("HumanController", "toggleFormationFlight", &HumanController::toggleFormationFlight).addShortcut().keybindMode(KeybindMode::OnPress); |
---|
53 | SetConsoleCommand("HumanController", "FFChangeMode", &HumanController::FFChangeMode).addShortcut().keybindMode(KeybindMode::OnPress); |
---|
54 | SetConsoleCommand("HumanController", __CC_fire_name, &HumanController::fire ).addShortcut().keybindMode(KeybindMode::OnHold); |
---|
55 | SetConsoleCommand("HumanController", "reload", &HumanController::reload ).addShortcut(); |
---|
56 | SetConsoleCommand("HumanController", __CC_boost_name, &HumanController::keepBoost ).addShortcut().keybindMode(KeybindMode::OnHold); |
---|
57 | SetConsoleCommand("HumanController", "greet", &HumanController::greet ).addShortcut(); |
---|
58 | SetConsoleCommand("HumanController", "switchCamera", &HumanController::switchCamera ).addShortcut(); |
---|
59 | SetConsoleCommand("HumanController", "mouseLook", &HumanController::mouseLook ).addShortcut(); |
---|
60 | SetConsoleCommand("HumanController", __CC_suicide_name, &HumanController::suicide ).addShortcut(); |
---|
61 | SetConsoleCommand("HumanController", "toggleGodMode", &HumanController::toggleGodMode ).addShortcut(); |
---|
62 | SetConsoleCommand("HumanController", "addBots", &HumanController::addBots ).addShortcut().defaultValues(1); |
---|
63 | SetConsoleCommand("HumanController", "killBots", &HumanController::killBots ).addShortcut().defaultValues(0); |
---|
64 | SetConsoleCommand("HumanController", "cycleNavigationFocus", &HumanController::cycleNavigationFocus).addShortcut(); |
---|
65 | SetConsoleCommand("HumanController", "releaseNavigationFocus", &HumanController::releaseNavigationFocus).addShortcut(); |
---|
66 | SetConsoleCommand("HumanController", "myposition", &HumanController::myposition ).addShortcut(); |
---|
67 | |
---|
68 | CreateUnloadableFactory(HumanController); |
---|
69 | |
---|
70 | HumanController* HumanController::localController_s = 0; |
---|
71 | /*static*/ const float HumanController::BOOSTING_TIME = 0.1f; |
---|
72 | |
---|
73 | HumanController::HumanController(BaseObject* creator) : FormationController(creator) |
---|
74 | { |
---|
75 | RegisterObject(HumanController); |
---|
76 | |
---|
77 | this->controlPaused_ = false; |
---|
78 | this->boosting_ = false; |
---|
79 | this->boosting_ = false; |
---|
80 | this->tempMaster=NULL; |
---|
81 | HumanController::localController_s = this; |
---|
82 | this->boostingTimeout_.setTimer(HumanController::BOOSTING_TIME, false, createExecutor(createFunctor(&HumanController::terminateBoosting, this))); |
---|
83 | this->boostingTimeout_.stopTimer(); |
---|
84 | this->state_=FREE; |
---|
85 | } |
---|
86 | |
---|
87 | HumanController::~HumanController() |
---|
88 | { |
---|
89 | if (HumanController::localController_s) |
---|
90 | { |
---|
91 | HumanController::localController_s->removeFromFormation(); |
---|
92 | } |
---|
93 | HumanController::localController_s = 0; |
---|
94 | } |
---|
95 | |
---|
96 | void HumanController::tick(float dt) |
---|
97 | { |
---|
98 | if (GameMode::playsSound() && HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
99 | { |
---|
100 | Camera* camera = HumanController::localController_s->controllableEntity_->getCamera(); |
---|
101 | if (!camera) |
---|
102 | orxout(internal_warning) << "HumanController, Warning: Using a ControllableEntity without Camera" << endl; |
---|
103 | } |
---|
104 | |
---|
105 | // commandslaves when Master of a formation |
---|
106 | if (HumanController::localController_s && HumanController::localController_s->state_==MASTER) |
---|
107 | { |
---|
108 | if (HumanController::localController_s->mode_!=ATTACK) |
---|
109 | HumanController::localController_s->commandSlaves(); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | void HumanController::moveFrontBack(const Vector2& value) |
---|
114 | { |
---|
115 | if (HumanController::localController_s) |
---|
116 | HumanController::localController_s->frontback(value); |
---|
117 | } |
---|
118 | |
---|
119 | void HumanController::frontback(const Vector2& value) |
---|
120 | { |
---|
121 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
122 | HumanController::localController_s->controllableEntity_->moveFrontBack(value); |
---|
123 | } |
---|
124 | |
---|
125 | void HumanController::moveRightLeft(const Vector2& value) |
---|
126 | { |
---|
127 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
128 | HumanController::localController_s->controllableEntity_->moveRightLeft(value); |
---|
129 | } |
---|
130 | |
---|
131 | void HumanController::moveUpDown(const Vector2& value) |
---|
132 | { |
---|
133 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
134 | HumanController::localController_s->controllableEntity_->moveUpDown(value); |
---|
135 | } |
---|
136 | |
---|
137 | void HumanController::yaw(const Vector2& value) |
---|
138 | { |
---|
139 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
140 | HumanController::localController_s->controllableEntity_->rotateYaw(value); |
---|
141 | } |
---|
142 | |
---|
143 | void HumanController::pitch(const Vector2& value) |
---|
144 | { |
---|
145 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
146 | HumanController::localController_s->controllableEntity_->rotatePitch(value); |
---|
147 | } |
---|
148 | |
---|
149 | void HumanController::rotateYaw(const Vector2& value) |
---|
150 | { |
---|
151 | if (HumanController::localController_s) |
---|
152 | HumanController::localController_s->yaw(value); |
---|
153 | } |
---|
154 | |
---|
155 | void HumanController::rotatePitch(const Vector2& value) |
---|
156 | { |
---|
157 | if (HumanController::localController_s) |
---|
158 | HumanController::localController_s->pitch(value); |
---|
159 | } |
---|
160 | |
---|
161 | void HumanController::rotateRoll(const Vector2& value) |
---|
162 | { |
---|
163 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
164 | HumanController::localController_s->controllableEntity_->rotateRoll(value); |
---|
165 | } |
---|
166 | |
---|
167 | void HumanController::fire(unsigned int firemode) |
---|
168 | { |
---|
169 | if (HumanController::localController_s) |
---|
170 | HumanController::localController_s->doFire(firemode); |
---|
171 | } |
---|
172 | |
---|
173 | void HumanController::doFire(unsigned int firemode) |
---|
174 | { |
---|
175 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
176 | { |
---|
177 | HumanController::localController_s->controllableEntity_->fire(firemode); |
---|
178 | //if human fires, set slaves free. See FormationController::forceFreeSlaves() |
---|
179 | if (HumanController::localController_s->state_==MASTER && HumanController::localController_s->mode_==NORMAL) |
---|
180 | { |
---|
181 | HumanController::localController_s->forceFreeSlaves(); |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | void HumanController::reload() |
---|
187 | { |
---|
188 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
189 | HumanController::localController_s->controllableEntity_->reload(); |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | @brief |
---|
194 | Static method,keeps boosting. |
---|
195 | */ |
---|
196 | /*static*/ void HumanController::keepBoost() |
---|
197 | { |
---|
198 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
199 | HumanController::localController_s->keepBoosting(); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | @brief |
---|
204 | Starts, or keeps the boosting mode. |
---|
205 | Resets the boosting timeout and ells the ControllableEntity to boost (or not boost anymore). |
---|
206 | */ |
---|
207 | void HumanController::keepBoosting(void) |
---|
208 | { |
---|
209 | if(this->boostingTimeout_.isActive()) |
---|
210 | { |
---|
211 | this->boostingTimeout_.stopTimer(); |
---|
212 | this->boostingTimeout_.startTimer(); |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | this->boosting_ = true; |
---|
217 | this->boostingTimeout_.startTimer(); |
---|
218 | |
---|
219 | this->controllableEntity_->boost(this->boosting_); |
---|
220 | // orxout() << "Start boosting" << endl; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | @brief |
---|
226 | Terminates the boosting mode. |
---|
227 | */ |
---|
228 | void HumanController::terminateBoosting(void) |
---|
229 | { |
---|
230 | this->boosting_ = false; |
---|
231 | this->boostingTimeout_.stopTimer(); |
---|
232 | |
---|
233 | this->controllableEntity_->boost(this->boosting_); |
---|
234 | // orxout() << "Stop boosting" << endl; |
---|
235 | } |
---|
236 | |
---|
237 | void HumanController::greet() |
---|
238 | { |
---|
239 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
240 | HumanController::localController_s->controllableEntity_->greet(); |
---|
241 | } |
---|
242 | |
---|
243 | void HumanController::switchCamera() |
---|
244 | { |
---|
245 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
246 | HumanController::localController_s->controllableEntity_->switchCamera(); |
---|
247 | } |
---|
248 | |
---|
249 | void HumanController::mouseLook() |
---|
250 | { |
---|
251 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
252 | HumanController::localController_s->controllableEntity_->mouseLook(); |
---|
253 | } |
---|
254 | |
---|
255 | void HumanController::suicide() |
---|
256 | { |
---|
257 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
258 | { |
---|
259 | Pawn* pawn = orxonox_cast<Pawn*>(HumanController::localController_s->controllableEntity_); |
---|
260 | if (pawn) |
---|
261 | pawn->kill(); |
---|
262 | else if (HumanController::localController_s->player_) |
---|
263 | HumanController::localController_s->player_->stopControl(); |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | void HumanController::toggleGodMode() |
---|
268 | { |
---|
269 | if (HumanController::localController_s) |
---|
270 | HumanController::localController_s->setGodMode(!HumanController::localController_s->getGodMode()); |
---|
271 | } |
---|
272 | |
---|
273 | void HumanController::myposition() |
---|
274 | { |
---|
275 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
276 | { |
---|
277 | const Vector3& position = HumanController::localController_s->controllableEntity_->getPosition(); |
---|
278 | const Quaternion& orientation = HumanController::localController_s->controllableEntity_->getOrientation(); |
---|
279 | |
---|
280 | orxout(message) << "position=\"" << position.x << ", " << position.y << ", " << position.z << "\" " |
---|
281 | << "orientation=\"" << orientation.w << ", " << orientation.x << ", " << orientation.y << ", " << orientation.z << "\"" << endl; |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | @brief |
---|
287 | toggle the formation. Not usable, if formationflight is disabled generally (formationFlight_) |
---|
288 | */ |
---|
289 | void HumanController::toggleFormationFlight() |
---|
290 | { |
---|
291 | if (HumanController::localController_s) |
---|
292 | { |
---|
293 | if (!HumanController::localController_s->formationFlight_) |
---|
294 | { |
---|
295 | return; //dont use when formationFlight is disabled |
---|
296 | } |
---|
297 | if (HumanController::localController_s->state_==MASTER) |
---|
298 | { |
---|
299 | HumanController::localController_s->loseMasterState(); |
---|
300 | orxout(message) <<"FormationFlight disabled "<< endl; |
---|
301 | } else //SLAVE or FREE |
---|
302 | { |
---|
303 | HumanController::localController_s->takeLeadOfFormation(); |
---|
304 | orxout(message) <<"FormationFlight enabled "<< endl; |
---|
305 | } |
---|
306 | |
---|
307 | } |
---|
308 | |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | @brief |
---|
313 | Switch through the different Modes of formationflight. You must be a master of a formation to use. |
---|
314 | */ |
---|
315 | void HumanController::FFChangeMode() |
---|
316 | { |
---|
317 | if (HumanController::localController_s && HumanController::localController_s->state_==MASTER) |
---|
318 | { |
---|
319 | switch (HumanController::localController_s->getMode()) { |
---|
320 | case NORMAL: |
---|
321 | HumanController::localController_s->setMode(DEFEND); |
---|
322 | orxout(message) <<"Mode: DEFEND "<< endl; |
---|
323 | break; |
---|
324 | case DEFEND: |
---|
325 | HumanController::localController_s->setMode(ATTACK); |
---|
326 | orxout(message) <<"Mode: ATTACK "<< endl; |
---|
327 | break; |
---|
328 | case ATTACK: |
---|
329 | HumanController::localController_s->setMode(NORMAL); |
---|
330 | orxout(message) <<"Mode: NORMAL "<< endl; |
---|
331 | break; |
---|
332 | default: //catch all non-formation related states |
---|
333 | break; |
---|
334 | } |
---|
335 | } |
---|
336 | } |
---|
337 | |
---|
338 | |
---|
339 | //used, when slaves are in DEFEND mode. |
---|
340 | void HumanController::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage) |
---|
341 | { |
---|
342 | if (!this->formationFlight_ || this->state_!=MASTER || this->mode_!=DEFEND) return; |
---|
343 | this->masterAttacked(originator); |
---|
344 | } |
---|
345 | |
---|
346 | void HumanController::addBots(unsigned int amount) |
---|
347 | { |
---|
348 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype()) |
---|
349 | HumanController::localController_s->controllableEntity_->getGametype()->addBots(amount); |
---|
350 | } |
---|
351 | |
---|
352 | void HumanController::killBots(unsigned int amount) |
---|
353 | { |
---|
354 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->getGametype()) |
---|
355 | HumanController::localController_s->controllableEntity_->getGametype()->killBots(amount); |
---|
356 | } |
---|
357 | |
---|
358 | Pawn* HumanController::getLocalControllerEntityAsPawn() |
---|
359 | { |
---|
360 | if (HumanController::localController_s) |
---|
361 | return orxonox_cast<Pawn*>(HumanController::localController_s->getControllableEntity()); |
---|
362 | else |
---|
363 | return NULL; |
---|
364 | } |
---|
365 | |
---|
366 | void HumanController::cycleNavigationFocus() |
---|
367 | { |
---|
368 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
369 | HumanController::localController_s->controllableEntity_->getScene()->getRadar()->cycleFocus(); |
---|
370 | } |
---|
371 | |
---|
372 | void HumanController::releaseNavigationFocus() |
---|
373 | { |
---|
374 | if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) |
---|
375 | HumanController::localController_s->controllableEntity_->getScene()->getRadar()->releaseFocus(); |
---|
376 | } |
---|
377 | |
---|
378 | void HumanController::pauseControl() |
---|
379 | { |
---|
380 | if (HumanController::localController_s) |
---|
381 | HumanController::localController_s->doPauseControl(); |
---|
382 | } |
---|
383 | |
---|
384 | void HumanController::resumeControl() |
---|
385 | { |
---|
386 | if (HumanController::localController_s) |
---|
387 | HumanController::localController_s->doResumeControl(); |
---|
388 | } |
---|
389 | } |
---|