1 | |
---|
2 | /* |
---|
3 | orxonox - the future of 3D-vertical-scrollers |
---|
4 | |
---|
5 | Copyright (C) 2004 orx |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | ### File Specific |
---|
13 | main-programmer: Patrick Boenzli |
---|
14 | co-programmer: Benjamin Grauer |
---|
15 | |
---|
16 | 2005-07-24: Benjamin Grauer: restructurate, so it can handle the new Weapons. |
---|
17 | */ |
---|
18 | |
---|
19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
20 | |
---|
21 | #include "weapon_manager.h" |
---|
22 | #include "weapon.h" |
---|
23 | #include "crosshair.h" |
---|
24 | |
---|
25 | #include "playable.h" |
---|
26 | |
---|
27 | #include "load_param.h" |
---|
28 | #include "factory.h" |
---|
29 | |
---|
30 | #include "t_animation.h" |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | |
---|
35 | /** |
---|
36 | * @brief this initializes the weaponManager for a given nnumber of weapon slots |
---|
37 | * @param number of weapon slots of the model/ship <= 8 (limitied) |
---|
38 | */ |
---|
39 | WeaponManager::WeaponManager(WorldEntity* parent) |
---|
40 | { |
---|
41 | this->init(); |
---|
42 | this->setParent(parent); |
---|
43 | } |
---|
44 | |
---|
45 | WeaponManager::WeaponManager(const TiXmlElement* root) |
---|
46 | { |
---|
47 | this->init(); |
---|
48 | this->loadParams(root); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * @brief Destroys a WeaponManager |
---|
53 | */ |
---|
54 | WeaponManager::~WeaponManager() |
---|
55 | { |
---|
56 | // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.) |
---|
57 | //delete this->crosshair; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * @brief initializes the WeaponManager |
---|
62 | */ |
---|
63 | void WeaponManager::init() |
---|
64 | { |
---|
65 | this->setClassID(CL_WEAPON_MANAGER, "WeaponManager"); |
---|
66 | |
---|
67 | this->parent = NULL; |
---|
68 | |
---|
69 | for (int i = 0; i < WM_MAX_CONFIGS; i++) |
---|
70 | for (int j = 0; j < WM_MAX_SLOTS; j++) |
---|
71 | this->configs[i][j] = NULL; |
---|
72 | |
---|
73 | for (int i = 0; i < WM_MAX_SLOTS; i++) |
---|
74 | { |
---|
75 | this->currentSlotConfig[i].capability = WTYPE_ALL; |
---|
76 | this->currentSlotConfig[i].currentWeapon = NULL; |
---|
77 | this->currentSlotConfig[i].nextWeapon = NULL; |
---|
78 | |
---|
79 | // NAMING |
---|
80 | char* tmpName; |
---|
81 | if (this->getName()) |
---|
82 | { |
---|
83 | tmpName = new char[strlen(this->getName()) + 10]; |
---|
84 | sprintf(tmpName, "%s_slot%d", this->getName(), i); |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | tmpName = new char[30]; |
---|
89 | sprintf(tmpName, "WeaponMan_slot%d", i); |
---|
90 | } |
---|
91 | this->currentSlotConfig[i].position.setName(tmpName); |
---|
92 | this->currentSlotConfig[i].position.deactivateNode(); |
---|
93 | delete[] tmpName; |
---|
94 | } |
---|
95 | |
---|
96 | for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++) |
---|
97 | this->availiableWeapons[i] = NULL; |
---|
98 | |
---|
99 | |
---|
100 | this->currentConfigID = 0; |
---|
101 | this->slotCount = 2; |
---|
102 | this->weaponChange; |
---|
103 | |
---|
104 | // CROSSHAIR INITIALISATION |
---|
105 | this->crosshair = new Crosshair(); |
---|
106 | |
---|
107 | this->crossHairSizeAnim = new tAnimation<Crosshair>(this->crosshair, &Crosshair::setSize); |
---|
108 | this->crossHairSizeAnim->setInfinity(ANIM_INF_REWIND); |
---|
109 | this->crossHairSizeAnim->addKeyFrame(50, .1, ANIM_LINEAR); |
---|
110 | this->crossHairSizeAnim->addKeyFrame(100, .05, ANIM_LINEAR); |
---|
111 | this->crossHairSizeAnim->addKeyFrame(50, .01, ANIM_LINEAR); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * loads the settings of the WeaponManager |
---|
116 | * @param root the XML-element to load from |
---|
117 | */ |
---|
118 | void WeaponManager::loadParams(const TiXmlElement* root) |
---|
119 | { |
---|
120 | BaseObject::loadParams(root); |
---|
121 | |
---|
122 | LoadParam(root, "slot-count", this, WeaponManager, setSlotCount) |
---|
123 | .describe("how many slots(cannons) the WeaponManager can handle"); |
---|
124 | |
---|
125 | LOAD_PARAM_START_CYCLE(root, element); |
---|
126 | { |
---|
127 | // CHECK IF THIS WORKS.... |
---|
128 | LoadParamXML_CYCLE(element, "weapons", this, WeaponManager, loadWeapons) |
---|
129 | .describe("loads Weapons"); |
---|
130 | } |
---|
131 | LOAD_PARAM_END_CYCLE(element); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * loads a Weapon onto the WeaponManager |
---|
136 | * @param root the XML-element to load the Weapons from |
---|
137 | */ |
---|
138 | void WeaponManager::loadWeapons(const TiXmlElement* root) |
---|
139 | { |
---|
140 | LOAD_PARAM_START_CYCLE(root, element); |
---|
141 | |
---|
142 | Weapon* newWeapon = dynamic_cast<Weapon*>(Factory::fabricate(element)); |
---|
143 | /// @todo implement this !! |
---|
144 | |
---|
145 | |
---|
146 | LOAD_PARAM_END_CYCLE(element); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * sets the Parent of the WeaponManager. |
---|
151 | * @param parent the parent of the WeaponManager |
---|
152 | * |
---|
153 | * this is used, to identify to which ship/man/whatever this WeaponManager is connected. |
---|
154 | * also all the Slots will be subconnected to this parent. |
---|
155 | * |
---|
156 | * The reason this function exists is that the WeaponManager is neither a WorldEntity nor |
---|
157 | * a PNode. |
---|
158 | */ |
---|
159 | void WeaponManager::setParent(WorldEntity* parent) |
---|
160 | { |
---|
161 | this->parent = parent; |
---|
162 | if (this->parent != NULL) |
---|
163 | { |
---|
164 | for (int i = 0; i < WM_MAX_SLOTS; i++) |
---|
165 | this->parent->addChild(&this->currentSlotConfig[i].position); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * sets the number of Slots the WeaponManager has |
---|
171 | * @param slotCount the number of slots |
---|
172 | */ |
---|
173 | void WeaponManager::setSlotCount(unsigned int slotCount) |
---|
174 | { |
---|
175 | if (slotCount <= WM_MAX_SLOTS) |
---|
176 | this->slotCount = slotCount; |
---|
177 | else |
---|
178 | this->slotCount = WM_MAX_SLOTS; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | /** |
---|
183 | * sets the position of the Slot relative to the parent |
---|
184 | * @param slot the slot to set-up |
---|
185 | * @param position the position of the given slot |
---|
186 | */ |
---|
187 | void WeaponManager::setSlotPosition(int slot, const Vector& position) |
---|
188 | { |
---|
189 | if (slot < this->slotCount) |
---|
190 | this->currentSlotConfig[slot].position.setRelCoor(position); |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | /** |
---|
195 | * sets the relative rotation of the slot to its parent |
---|
196 | * @param slot the slot to set-up |
---|
197 | * @param rotation the relative rotation of the given slot |
---|
198 | */ |
---|
199 | void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation) |
---|
200 | { |
---|
201 | if (slot < this->slotCount) |
---|
202 | this->currentSlotConfig[slot].position.setRelDir(rotation); |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | /** |
---|
207 | * adds a weapon to the selected weaponconfiguration into the selected slot |
---|
208 | * @param weapon the weapon to add |
---|
209 | * @param configID an identifier for the slot: number between 0..7 if not specified: slotID=next free slot |
---|
210 | * @param slotID an identifier for the weapon configuration, number between 0..3 |
---|
211 | * |
---|
212 | * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be |
---|
213 | * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free |
---|
214 | * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be |
---|
215 | * a error message. |
---|
216 | */ |
---|
217 | bool WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
218 | { |
---|
219 | if ( weapon == NULL ) |
---|
220 | return false; |
---|
221 | |
---|
222 | if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount)) |
---|
223 | { |
---|
224 | PRINTF(2)("Slot %d of config %d is not availiabe (max: %d) searching for suitable slot\n", slotID, configID, this->slotCount); |
---|
225 | if (configID >= WM_MAX_CONFIGS) |
---|
226 | configID = -1; |
---|
227 | if (slotID >= (int)this->slotCount) |
---|
228 | slotID = -1; |
---|
229 | } |
---|
230 | // if no ConfigID is supplied set to Current Config. |
---|
231 | if (configID <= -1) |
---|
232 | configID = this->currentConfigID; |
---|
233 | // |
---|
234 | if (configID > -1 && slotID == -1) |
---|
235 | { |
---|
236 | slotID = this->getNextFreeSlot(configID, weapon->getCapability()); |
---|
237 | if (slotID == -1) |
---|
238 | configID = -1; |
---|
239 | } |
---|
240 | |
---|
241 | if (configID > 0 && slotID > 0 && this->configs[configID][slotID] != NULL) |
---|
242 | { |
---|
243 | PRINTF(3)("Weapon-slot %d/%d of %s already poulated, remove weapon (%s::%s) first\n", configID, slotID, this->getName(), weapon->getClassName(), weapon->getName()); |
---|
244 | return false; |
---|
245 | } |
---|
246 | |
---|
247 | if (slotID <= -1) // WM_FREE_SLOT |
---|
248 | { |
---|
249 | slotID = this->getNextFreeSlot(configID, weapon->getCapability()); |
---|
250 | if( slotID < 0 || slotID >= this->slotCount) |
---|
251 | { |
---|
252 | PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n"); |
---|
253 | return false; |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | if (!(this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLKINDS) && |
---|
258 | this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLDIRS) |
---|
259 | { |
---|
260 | PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n", |
---|
261 | slotID, weapon->getCapability(), this->currentSlotConfig[slotID].capability); |
---|
262 | return false; |
---|
263 | } |
---|
264 | |
---|
265 | //! @todo check if the weapon is already assigned to another config in another slot |
---|
266 | if (this->configs[configID][slotID] != NULL) |
---|
267 | return false; |
---|
268 | |
---|
269 | this->configs[configID][slotID] = weapon; |
---|
270 | weapon->setAmmoContainer(this->getAmmoContainer(weapon->getProjectileType())); |
---|
271 | if(configID == this->currentConfigID) |
---|
272 | this->currentSlotConfig[slotID].nextWeapon = weapon; |
---|
273 | if (this->parent != NULL) |
---|
274 | { |
---|
275 | this->parent->addChild(weapon); |
---|
276 | if (this->parent->isA(CL_PLAYABLE)) |
---|
277 | dynamic_cast<Playable*>(this->parent)->weaponConfigChanged(); |
---|
278 | } |
---|
279 | PRINTF(3)("Added a new Weapon (%s::%s) to the WeaponManager: config %i/ slot %i\n", weapon->getClassName(), weapon->getName(), configID, slotID); |
---|
280 | return true; |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | * sets the capabilities of a Slot |
---|
285 | * @param slot the slot to set the capability |
---|
286 | * @param slotCapability the capability @see WM_SlotCapability |
---|
287 | */ |
---|
288 | void WeaponManager::setSlotCapability(int slot, long slotCapability) |
---|
289 | { |
---|
290 | if (slot > slotCount) |
---|
291 | return; |
---|
292 | this->currentSlotConfig[slot].capability = slotCapability; |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | /** |
---|
297 | * removes a Weapon from the WeaponManager |
---|
298 | * |
---|
299 | * !! The weapon must be inactive before you can delete it, !! |
---|
300 | * !! because it will still be deactivated (if it is selected) !! |
---|
301 | */ |
---|
302 | void WeaponManager::removeWeapon(Weapon* weapon, int configID) |
---|
303 | { |
---|
304 | if (weapon == NULL) |
---|
305 | return; |
---|
306 | if (configID < 0) |
---|
307 | { |
---|
308 | for (int j = 0; j < WM_MAX_SLOTS; j++) |
---|
309 | { |
---|
310 | for (int i = 0; i < WM_MAX_CONFIGS; i++) |
---|
311 | { |
---|
312 | if (this->configs[i][j] == weapon) |
---|
313 | this->configs[i][j] = NULL; |
---|
314 | } |
---|
315 | if (this->currentSlotConfig[j].currentWeapon == weapon) |
---|
316 | { |
---|
317 | this->currentSlotConfig[j].nextWeapon = NULL; |
---|
318 | } |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | /** |
---|
325 | * changes to the next weapon configuration |
---|
326 | */ |
---|
327 | void WeaponManager::nextWeaponConfig() |
---|
328 | { |
---|
329 | ++this->currentConfigID; |
---|
330 | if (this->currentConfigID >= WM_MAX_CONFIGS) |
---|
331 | this->currentConfigID = 0; |
---|
332 | this->changeWeaponConfig(this->currentConfigID); |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | * changes to the previous configuration |
---|
337 | */ |
---|
338 | void WeaponManager::previousWeaponConfig() |
---|
339 | { |
---|
340 | --this->currentConfigID; |
---|
341 | if (this->currentConfigID < 0) |
---|
342 | this->currentConfigID = WM_MAX_CONFIGS -1; |
---|
343 | this->changeWeaponConfig(this->currentConfigID); |
---|
344 | } |
---|
345 | |
---|
346 | /** |
---|
347 | * change to a desired configuration |
---|
348 | * @param weaponConfig the configuration to jump to. |
---|
349 | */ |
---|
350 | void WeaponManager::changeWeaponConfig(int weaponConfig) |
---|
351 | { |
---|
352 | this->currentConfigID = weaponConfig; |
---|
353 | PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID); |
---|
354 | for (int i = 0; i < WM_MAX_SLOTS; i++) |
---|
355 | this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i]; |
---|
356 | } |
---|
357 | |
---|
358 | |
---|
359 | /** |
---|
360 | * triggers fire of all weapons in the current weaponconfig |
---|
361 | */ |
---|
362 | void WeaponManager::fire() |
---|
363 | { |
---|
364 | Weapon* firingWeapon; |
---|
365 | for(int i = 0; i < this->slotCount; i++) |
---|
366 | { |
---|
367 | firingWeapon = this->currentSlotConfig[i].currentWeapon; |
---|
368 | if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT); |
---|
369 | } |
---|
370 | this->crosshair->setRotationSpeed(500); |
---|
371 | this->crossHairSizeAnim->replay(); |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | /** |
---|
376 | * triggers tick of all weapons in the current weaponconfig |
---|
377 | * @param second passed since last tick |
---|
378 | */ |
---|
379 | void WeaponManager::tick(float dt) |
---|
380 | { |
---|
381 | Weapon* tickWeapon; |
---|
382 | |
---|
383 | for(int i = 0; i < this->slotCount; i++) |
---|
384 | { |
---|
385 | /* |
---|
386 | NICE LITTLE DEBUG FUNCTION |
---|
387 | if (this->currentSlotConfig[i].currentWeapon != NULL || this->currentSlotConfig[i].nextWeapon != NULL) |
---|
388 | printf("%p %p\n", this->currentSlotConfig[i].currentWeapon, this->currentSlotConfig[i].nextWeapon);*/ |
---|
389 | |
---|
390 | // current Weapon in Slot i |
---|
391 | tickWeapon = this->currentSlotConfig[i].currentWeapon; |
---|
392 | // On A change (current != next) |
---|
393 | if (tickWeapon != this->currentSlotConfig[i].nextWeapon) |
---|
394 | { |
---|
395 | // if a Weapon is Active in slot i, deactivate it. |
---|
396 | if (tickWeapon != NULL ) |
---|
397 | { |
---|
398 | if (tickWeapon->isActive()) |
---|
399 | { |
---|
400 | tickWeapon->requestAction(WA_DEACTIVATE); |
---|
401 | continue; |
---|
402 | } |
---|
403 | else |
---|
404 | { |
---|
405 | tickWeapon->toList(OM_NULL); |
---|
406 | this->currentSlotConfig[i].currentWeapon = NULL; |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | // switching to next Weapon |
---|
411 | tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon; |
---|
412 | if (tickWeapon != NULL) |
---|
413 | { |
---|
414 | if (this->parent != NULL) |
---|
415 | tickWeapon->toList(this->parent->getOMListNumber()); |
---|
416 | tickWeapon->requestAction(WA_ACTIVATE); |
---|
417 | this->currentSlotConfig[i].position.activateNode(); |
---|
418 | tickWeapon->setParent(&this->currentSlotConfig[i].position); |
---|
419 | } |
---|
420 | else |
---|
421 | this->currentSlotConfig[i].position.deactivateNode(); |
---|
422 | if (this->parent != NULL && this->parent->isA(CL_PLAYABLE)) |
---|
423 | dynamic_cast<Playable*>(this->parent)->weaponConfigChanged(); |
---|
424 | } |
---|
425 | } |
---|
426 | } |
---|
427 | |
---|
428 | |
---|
429 | /** |
---|
430 | * triggers draw of all weapons in the current weaponconfig |
---|
431 | */ |
---|
432 | void WeaponManager::draw() const |
---|
433 | { |
---|
434 | Weapon* drawWeapon; |
---|
435 | for (int i = 0; i < this->slotCount; i++) |
---|
436 | { |
---|
437 | drawWeapon = this->currentSlotConfig[i].currentWeapon; |
---|
438 | if( drawWeapon != NULL && drawWeapon->isVisible()) |
---|
439 | drawWeapon->draw(); |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | |
---|
444 | /** |
---|
445 | * private gets the next free slot in a certain weaponconfig |
---|
446 | * @param the selected weaponconfig -1 if none found |
---|
447 | */ |
---|
448 | int WeaponManager::getNextFreeSlot(int configID, long capability) |
---|
449 | { |
---|
450 | if (configID == -1) |
---|
451 | { |
---|
452 | for (configID = 0; configID < WM_MAX_CONFIGS; configID++) |
---|
453 | for( int i = 0; i < this->slotCount; ++i) |
---|
454 | { |
---|
455 | if( this->configs[configID][i] == NULL && |
---|
456 | (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) && |
---|
457 | (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS)) |
---|
458 | return i; |
---|
459 | } |
---|
460 | } |
---|
461 | else |
---|
462 | { |
---|
463 | for( int i = 0; i < this->slotCount; ++i) |
---|
464 | { |
---|
465 | if( this->configs[configID][i] == NULL && |
---|
466 | (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) && |
---|
467 | (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS)) |
---|
468 | return i; |
---|
469 | } |
---|
470 | } |
---|
471 | return -1; |
---|
472 | } |
---|
473 | |
---|
474 | CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(ClassID projectileType) |
---|
475 | { |
---|
476 | for (unsigned int i = 0; i < this->ammo.size(); i++) |
---|
477 | { |
---|
478 | if (this->ammo[i]->getProjectileType() == projectileType) |
---|
479 | return this->ammo[i]; |
---|
480 | } |
---|
481 | this->ammo.push_back(CountPointer<AmmoContainer>(new AmmoContainer(projectileType))); |
---|
482 | return this->ammo.back(); |
---|
483 | } |
---|
484 | |
---|
485 | |
---|
486 | /** |
---|
487 | * outputs some nice debug information about the WeaponManager |
---|
488 | */ |
---|
489 | void WeaponManager::debug() const |
---|
490 | { |
---|
491 | PRINT(3)("WeaponManager Debug Information\n"); |
---|
492 | PRINT(3)("-------------------------------\n"); |
---|
493 | PRINT(3)("current Config is %d\n", this->currentConfigID); |
---|
494 | for (int i = 0; i < WM_MAX_CONFIGS; i++) |
---|
495 | { |
---|
496 | PRINT(3)("Listing Weapons in Configuration %d\n", i); |
---|
497 | for (int j = 0; j < WM_MAX_SLOTS; j++) |
---|
498 | { |
---|
499 | if (this->configs[i][j] != NULL) |
---|
500 | PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName()); |
---|
501 | } |
---|
502 | } |
---|
503 | } |
---|