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