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