[4826] | 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 |
---|
[4832] | 14 | co-programmer: Benjamin Grauer |
---|
[4951] | 15 | |
---|
| 16 | 2005-07-24: Benjamin Grauer: restructurate, so it can handle the new Weapons. |
---|
[4826] | 17 | */ |
---|
| 18 | |
---|
| 19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
| 20 | |
---|
| 21 | #include "weapon_manager.h" |
---|
| 22 | #include "weapon.h" |
---|
[4834] | 23 | #include "crosshair.h" |
---|
[4828] | 24 | |
---|
[5750] | 25 | #include "class_list.h" |
---|
[4834] | 26 | #include "load_param.h" |
---|
| 27 | #include "factory.h" |
---|
[4826] | 28 | #include "list.h" |
---|
[4837] | 29 | #include "t_animation.h" |
---|
[4953] | 30 | #include "null_parent.h" |
---|
[4826] | 31 | |
---|
[5750] | 32 | |
---|
[4826] | 33 | using namespace std; |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | /** |
---|
[4832] | 37 | * this initializes the weaponManager for a given nnumber of weapon slots |
---|
[4826] | 38 | * @param number of weapon slots of the model/ship <= 8 (limitied) |
---|
| 39 | */ |
---|
[4953] | 40 | WeaponManager::WeaponManager(PNode* parent) |
---|
[4826] | 41 | { |
---|
[4833] | 42 | this->init(); |
---|
[4953] | 43 | this->setParent(parent); |
---|
[4826] | 44 | } |
---|
| 45 | |
---|
[4949] | 46 | WeaponManager::WeaponManager(const TiXmlElement* root) |
---|
| 47 | { |
---|
| 48 | this->init(); |
---|
| 49 | this->loadParams(root); |
---|
| 50 | } |
---|
[4826] | 51 | |
---|
[4833] | 52 | /** |
---|
| 53 | * Destroys a WeaponManager |
---|
| 54 | */ |
---|
[4826] | 55 | WeaponManager::~WeaponManager() |
---|
| 56 | { |
---|
[4834] | 57 | // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.) |
---|
| 58 | //delete this->crosshair; |
---|
[4826] | 59 | } |
---|
| 60 | |
---|
[4834] | 61 | /** |
---|
| 62 | * initializes the WeaponManager |
---|
| 63 | */ |
---|
[4833] | 64 | void WeaponManager::init() |
---|
| 65 | { |
---|
| 66 | this->setClassID(CL_WEAPON_MANAGER, "WeaponManager"); |
---|
[4826] | 67 | |
---|
[4951] | 68 | this->parent = NULL; |
---|
| 69 | |
---|
| 70 | for (int i = 0; i < WM_MAX_CONFIGS; i++) |
---|
| 71 | for (int j = 0; j < WM_MAX_SLOTS; j++) |
---|
| 72 | this->configs[i][j] = NULL; |
---|
| 73 | |
---|
| 74 | for (int i = 0; i < WM_MAX_SLOTS; i++) |
---|
[4833] | 75 | { |
---|
[4959] | 76 | this->currentSlotConfig[i].capability = WTYPE_ALL; |
---|
[4951] | 77 | this->currentSlotConfig[i].currentWeapon = NULL; |
---|
| 78 | this->currentSlotConfig[i].nextWeapon = NULL; |
---|
[4992] | 79 | |
---|
| 80 | // NAMING |
---|
| 81 | char* tmpName; |
---|
| 82 | if (this->getName()) |
---|
| 83 | { |
---|
| 84 | tmpName = new char[strlen(this->getName()) + 10]; |
---|
| 85 | sprintf(tmpName, "%s_slot%d", this->getName(), i); |
---|
| 86 | } |
---|
| 87 | else |
---|
| 88 | { |
---|
| 89 | tmpName = new char[30]; |
---|
| 90 | sprintf(tmpName, "WeaponMan_slot%d", i); |
---|
| 91 | } |
---|
| 92 | this->currentSlotConfig[i].position.setName(tmpName); |
---|
[5208] | 93 | delete[] tmpName; |
---|
[4833] | 94 | } |
---|
[4895] | 95 | |
---|
[4951] | 96 | for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++) |
---|
| 97 | this->availiableWeapons[i] = NULL; |
---|
[4895] | 98 | |
---|
| 99 | |
---|
[4951] | 100 | this->currentConfigID = 0; |
---|
| 101 | this->slotCount = 2; |
---|
| 102 | this->weaponChange; |
---|
[4895] | 103 | |
---|
[4951] | 104 | // CROSSHAIR INITIALISATION |
---|
[4834] | 105 | this->crosshair = new Crosshair(); |
---|
[4837] | 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); |
---|
[4834] | 112 | } |
---|
[4833] | 113 | |
---|
[4834] | 114 | /** |
---|
[4837] | 115 | * loads the settings of the WeaponManager |
---|
[4834] | 116 | * @param root the XML-element to load from |
---|
| 117 | */ |
---|
| 118 | void WeaponManager::loadParams(const TiXmlElement* root) |
---|
| 119 | { |
---|
| 120 | static_cast<BaseObject*>(this)->loadParams(root); |
---|
[4972] | 121 | |
---|
[5671] | 122 | LoadParam(root, "slot-count", this, WeaponManager, setSlotCount) |
---|
[4834] | 123 | .describe("how many slots(cannons) the WeaponManager can handle"); |
---|
| 124 | |
---|
[5644] | 125 | LOAD_PARAM_START_CYCLE(root, element); |
---|
[5654] | 126 | { |
---|
| 127 | // CHECK IF THIS WORKS.... |
---|
| 128 | LoadParamXML_CYCLE(element, "weapons", this, WeaponManager, loadWeapons) |
---|
| 129 | .describe("loads Weapons"); |
---|
| 130 | } |
---|
[5644] | 131 | LOAD_PARAM_END_CYCLE(element); |
---|
[4833] | 132 | } |
---|
| 133 | |
---|
[4826] | 134 | /** |
---|
[4834] | 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 | { |
---|
[5644] | 140 | LOAD_PARAM_START_CYCLE(root, element); |
---|
[4834] | 141 | |
---|
| 142 | Weapon* newWeapon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(element)); |
---|
[5653] | 143 | /// @todo implement this !! |
---|
[4834] | 144 | |
---|
| 145 | |
---|
[5644] | 146 | LOAD_PARAM_END_CYCLE(element); |
---|
[4834] | 147 | } |
---|
| 148 | |
---|
[4992] | 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. |
---|
[5435] | 155 | * |
---|
| 156 | * The reason this function exists is that the WeaponManager is neither a WorldEntity nor |
---|
| 157 | * a PNode. |
---|
[4992] | 158 | */ |
---|
[4953] | 159 | void WeaponManager::setParent(PNode* parent) |
---|
| 160 | { |
---|
| 161 | if (parent == NULL) |
---|
| 162 | parent = NullParent::getInstance(); |
---|
| 163 | this->parent = parent; |
---|
| 164 | if (this->parent != NULL) |
---|
| 165 | { |
---|
| 166 | for (int i = 0; i < WM_MAX_SLOTS; i++) |
---|
| 167 | this->parent->addChild(&this->currentSlotConfig[i].position); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
[4834] | 171 | /** |
---|
| 172 | * sets the number of Slots the WeaponManager has |
---|
[4926] | 173 | * @param slotCount the number of slots |
---|
[4834] | 174 | */ |
---|
[4951] | 175 | void WeaponManager::setSlotCount(unsigned int slotCount) |
---|
[4834] | 176 | { |
---|
[4951] | 177 | if (slotCount <= WM_MAX_SLOTS) |
---|
| 178 | this->slotCount = slotCount; |
---|
| 179 | else |
---|
| 180 | this->slotCount = WM_MAX_SLOTS; |
---|
[4834] | 181 | } |
---|
| 182 | |
---|
[4972] | 183 | |
---|
| 184 | /** |
---|
| 185 | * sets the position of the Slot relative to the parent |
---|
| 186 | * @param slot the slot to set-up |
---|
| 187 | * @param position the position of the given slot |
---|
| 188 | */ |
---|
[4953] | 189 | void WeaponManager::setSlotPosition(int slot, const Vector& position) |
---|
| 190 | { |
---|
| 191 | if (slot < this->slotCount) |
---|
| 192 | this->currentSlotConfig[slot].position.setRelCoor(position); |
---|
| 193 | } |
---|
| 194 | |
---|
[4972] | 195 | |
---|
| 196 | /** |
---|
| 197 | * sets the relative rotation of the slot to its parent |
---|
| 198 | * @param slot the slot to set-up |
---|
| 199 | * @param rotation the relative rotation of the given slot |
---|
| 200 | */ |
---|
[4969] | 201 | void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation) |
---|
| 202 | { |
---|
| 203 | if (slot < this->slotCount) |
---|
| 204 | this->currentSlotConfig[slot].position.setRelDir(rotation); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | |
---|
[4834] | 208 | /** |
---|
[4832] | 209 | * adds a weapon to the selected weaponconfiguration into the selected slot |
---|
[4972] | 210 | * @param weapon the weapon to add |
---|
| 211 | * @param configID an identifier for the slot: number between 0..7 if not specified: slotID=next free slot |
---|
| 212 | * @param slotID an identifier for the weapon configuration, number between 0..3 |
---|
[4832] | 213 | * |
---|
| 214 | * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be |
---|
[4906] | 215 | * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free |
---|
[4832] | 216 | * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be |
---|
| 217 | * a error message. |
---|
[4826] | 218 | */ |
---|
[4832] | 219 | void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
[4826] | 220 | { |
---|
[4951] | 221 | if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount)) |
---|
[4826] | 222 | { |
---|
[5440] | 223 | PRINTF(2)("Slot %d of config %d is not availiabe (max: %d)\n", slotID, configID, this->slotCount); |
---|
[4951] | 224 | return; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | if (this->configs[configID][slotID] != NULL && configID > 0 && slotID > 0) |
---|
| 228 | PRINTF(3)("Weapon-slot %d/%d of %s already poulated, overwriting\n", configID, slotID, this->getName()); |
---|
| 229 | |
---|
| 230 | if (slotID == -1) // WM_FREE_SLOT |
---|
| 231 | { |
---|
[5441] | 232 | slotID = this->getNextFreeSlot(configID, weapon->getCapability()); |
---|
[4951] | 233 | if( slotID < 0 || slotID >= this->slotCount) |
---|
[4826] | 234 | { |
---|
[5441] | 235 | PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n"); |
---|
[4826] | 236 | return; |
---|
| 237 | } |
---|
| 238 | } |
---|
[4953] | 239 | |
---|
[5441] | 240 | if (!(this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLKINDS) && |
---|
| 241 | this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLDIRS) |
---|
| 242 | { |
---|
| 243 | PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n", |
---|
| 244 | slotID, weapon->getCapability(), this->currentSlotConfig[slotID].capability); |
---|
| 245 | return; |
---|
| 246 | } |
---|
| 247 | |
---|
[4953] | 248 | //! @todo check if the weapon is already assigned to another config in another slot |
---|
[4951] | 249 | this->configs[configID][slotID] = weapon; |
---|
[4953] | 250 | if (this->parent != NULL) |
---|
| 251 | weapon->setParent(parent); |
---|
[4826] | 252 | PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID); |
---|
| 253 | } |
---|
| 254 | |
---|
[4834] | 255 | /** |
---|
[4954] | 256 | * sets the capabilities of a Slot |
---|
| 257 | * @param slot the slot to set the capability |
---|
| 258 | * @param slotCapability the capability @see WM_SlotCapability |
---|
| 259 | */ |
---|
| 260 | void WeaponManager::setSlotCapability(int slot, long slotCapability) |
---|
| 261 | { |
---|
| 262 | if (slot > slotCount) |
---|
| 263 | return; |
---|
| 264 | this->currentSlotConfig[slot].capability = slotCapability; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | /** |
---|
[4834] | 269 | * removes a Weapon from the WeaponManager |
---|
[4954] | 270 | * |
---|
| 271 | * !! The weapon must be inactive before you can delete it, !! |
---|
| 272 | * !! because it will still be deactivated (if it is selected) !! |
---|
[4834] | 273 | */ |
---|
[4826] | 274 | void WeaponManager::removeWeapon(Weapon* weapon, int configID) |
---|
| 275 | { |
---|
[4954] | 276 | if (weapon == NULL) |
---|
| 277 | return; |
---|
| 278 | if (configID < 0) |
---|
| 279 | { |
---|
| 280 | for (int j = 0; j < WM_MAX_SLOTS; j++) |
---|
| 281 | { |
---|
| 282 | for (int i = 0; i < WM_MAX_CONFIGS; i++) |
---|
| 283 | { |
---|
| 284 | if (this->configs[i][j] == weapon) |
---|
| 285 | this->configs[i][j] = NULL; |
---|
| 286 | } |
---|
| 287 | if (this->currentSlotConfig[j].currentWeapon == weapon) |
---|
| 288 | { |
---|
| 289 | this->currentSlotConfig[j].nextWeapon = NULL; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | } |
---|
[4826] | 293 | } |
---|
| 294 | |
---|
| 295 | |
---|
| 296 | /** |
---|
[4832] | 297 | * changes to the next weapon configuration |
---|
[4826] | 298 | */ |
---|
[4954] | 299 | void WeaponManager::nextWeaponConfig() |
---|
[4826] | 300 | { |
---|
[4951] | 301 | ++this->currentConfigID; |
---|
| 302 | if (this->currentConfigID >= WM_MAX_CONFIGS) |
---|
| 303 | this->currentConfigID = 0; |
---|
[4952] | 304 | this->changeWeaponConfig(this->currentConfigID); |
---|
| 305 | } |
---|
[4826] | 306 | |
---|
[4953] | 307 | /** |
---|
| 308 | * changes to the previous configuration |
---|
| 309 | */ |
---|
[4952] | 310 | void WeaponManager::previousWeaponConfig() |
---|
| 311 | { |
---|
| 312 | --this->currentConfigID; |
---|
| 313 | if (this->currentConfigID < 0) |
---|
| 314 | this->currentConfigID = WM_MAX_CONFIGS -1; |
---|
| 315 | this->changeWeaponConfig(this->currentConfigID); |
---|
| 316 | } |
---|
| 317 | |
---|
[4953] | 318 | /** |
---|
| 319 | * change to a desired configuration |
---|
| 320 | * @param weaponConfig the configuration to jump to. |
---|
| 321 | */ |
---|
[4952] | 322 | void WeaponManager::changeWeaponConfig(int weaponConfig) |
---|
| 323 | { |
---|
| 324 | this->currentConfigID = weaponConfig; |
---|
| 325 | PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID); |
---|
[4951] | 326 | for (int i = 0; i < WM_MAX_SLOTS; i++) |
---|
[4826] | 327 | { |
---|
[4951] | 328 | this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i]; |
---|
[4953] | 329 | if (this->currentSlotConfig[i].currentWeapon != this->currentSlotConfig[i].nextWeapon) |
---|
| 330 | { |
---|
| 331 | if (this->currentSlotConfig[i].currentWeapon != NULL) |
---|
| 332 | (this->currentSlotConfig[i].currentWeapon->requestAction(WA_DEACTIVATE)); |
---|
| 333 | if (this->currentSlotConfig[i].nextWeapon != NULL && this->currentSlotConfig[i].nextWeapon->isActive()) |
---|
| 334 | this->currentSlotConfig[i].nextWeapon = NULL; |
---|
| 335 | } |
---|
[4951] | 336 | } |
---|
[4826] | 337 | } |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | /** |
---|
[4832] | 341 | * triggers fire of all weapons in the current weaponconfig |
---|
[4826] | 342 | */ |
---|
[4832] | 343 | void WeaponManager::fire() |
---|
[4826] | 344 | { |
---|
| 345 | Weapon* firingWeapon; |
---|
[4951] | 346 | for(int i = 0; i < this->slotCount; i++) |
---|
[4826] | 347 | { |
---|
[4951] | 348 | firingWeapon = this->currentSlotConfig[i].currentWeapon; |
---|
[4885] | 349 | if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT); |
---|
[4826] | 350 | } |
---|
[4837] | 351 | this->crosshair->setRotationSpeed(500); |
---|
| 352 | this->crossHairSizeAnim->replay(); |
---|
[4826] | 353 | } |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | /** |
---|
[4832] | 357 | * triggers tick of all weapons in the current weaponconfig |
---|
| 358 | * @param second passed since last tick |
---|
[4826] | 359 | */ |
---|
[4833] | 360 | void WeaponManager::tick(float dt) |
---|
[4826] | 361 | { |
---|
[4951] | 362 | Weapon* tickWeapon; |
---|
| 363 | |
---|
| 364 | // all weapons |
---|
| 365 | for(int i = 0; i < this->slotCount; i++) |
---|
[4826] | 366 | { |
---|
[4951] | 367 | |
---|
| 368 | tickWeapon = this->currentSlotConfig[i].currentWeapon; |
---|
| 369 | if (tickWeapon != this->currentSlotConfig[i].nextWeapon) // if no change occures |
---|
| 370 | { |
---|
| 371 | if (tickWeapon != NULL && tickWeapon->isActive()) |
---|
| 372 | { |
---|
| 373 | tickWeapon->requestAction(WA_DEACTIVATE); |
---|
| 374 | } |
---|
| 375 | else |
---|
| 376 | { |
---|
| 377 | tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon; |
---|
| 378 | if (tickWeapon != NULL) |
---|
[4953] | 379 | { |
---|
[4951] | 380 | tickWeapon->requestAction(WA_ACTIVATE); |
---|
[4953] | 381 | tickWeapon->setParent(&this->currentSlotConfig[i].position); |
---|
| 382 | } |
---|
[4951] | 383 | } |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | if( tickWeapon != NULL && tickWeapon->isActive()) |
---|
| 387 | tickWeapon->tickW(dt); |
---|
[4826] | 388 | } |
---|
[4834] | 389 | |
---|
| 390 | crosshair->setRotationSpeed(5); |
---|
[4826] | 391 | } |
---|
| 392 | |
---|
| 393 | |
---|
| 394 | /** |
---|
[4832] | 395 | * triggers draw of all weapons in the current weaponconfig |
---|
[4826] | 396 | */ |
---|
[4951] | 397 | void WeaponManager::draw() const |
---|
[4826] | 398 | { |
---|
[4951] | 399 | Weapon* drawWeapon; |
---|
| 400 | for (int i = 0; i < this->slotCount; i++) |
---|
[4826] | 401 | { |
---|
[4951] | 402 | drawWeapon = this->currentSlotConfig[i].currentWeapon; |
---|
| 403 | if( drawWeapon != NULL && drawWeapon->isVisible()) |
---|
| 404 | drawWeapon->draw(); |
---|
[4826] | 405 | } |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | |
---|
| 409 | /** |
---|
[4832] | 410 | * private gets the next free slot in a certain weaponconfig |
---|
| 411 | * @param the selected weaponconfig |
---|
[4826] | 412 | */ |
---|
[5440] | 413 | int WeaponManager::getNextFreeSlot(int configID, long capability) |
---|
[4826] | 414 | { |
---|
[4951] | 415 | for( int i = 0; i < this->slotCount; ++i) |
---|
[4826] | 416 | { |
---|
[5440] | 417 | if( this->configs[configID][i] == NULL && |
---|
[5441] | 418 | (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) && |
---|
| 419 | (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS)) |
---|
[4826] | 420 | return i; |
---|
| 421 | } |
---|
| 422 | return -1; |
---|
| 423 | } |
---|
| 424 | |
---|
[4951] | 425 | |
---|
| 426 | |
---|
[4953] | 427 | /** |
---|
| 428 | * outputs some nice debug information about the WeaponManager |
---|
| 429 | */ |
---|
[4951] | 430 | void WeaponManager::debug() const |
---|
| 431 | { |
---|
| 432 | PRINT(3)("WeaponManager Debug Information\n"); |
---|
| 433 | PRINT(3)("-------------------------------\n"); |
---|
| 434 | PRINT(3)("current Config is %d\n", this->currentConfigID); |
---|
| 435 | for (int i = 0; i < WM_MAX_CONFIGS; i++) |
---|
| 436 | { |
---|
| 437 | PRINT(3)("Listing Weapons in Configuration %d\n", i); |
---|
| 438 | for (int j = 0; j < WM_MAX_SLOTS; j++) |
---|
| 439 | { |
---|
| 440 | if (this->configs[i][j] != NULL) |
---|
| 441 | PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName()); |
---|
| 442 | } |
---|
| 443 | } |
---|
| 444 | } |
---|