[2106] | 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 | * Martin Polak |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[2912] | 29 | #include "WeaponSystem.h" |
---|
[2106] | 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
[9667] | 32 | #include "core/class/SubclassIdentifier.h" |
---|
[5735] | 33 | #include "worldentities/pawns/Pawn.h" |
---|
[2106] | 34 | |
---|
[2912] | 35 | #include "WeaponSlot.h" |
---|
| 36 | #include "WeaponPack.h" |
---|
| 37 | #include "WeaponSet.h" |
---|
[2914] | 38 | #include "Weapon.h" |
---|
[2918] | 39 | #include "Munition.h" |
---|
[2106] | 40 | |
---|
[2893] | 41 | /* WeaponSystem |
---|
[2106] | 42 | * |
---|
[2893] | 43 | * www.orxonox.net/wiki/WeaponSystem |
---|
[2106] | 44 | */ |
---|
| 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
[9667] | 48 | RegisterClass(WeaponSystem); |
---|
[2662] | 49 | |
---|
[9667] | 50 | WeaponSystem::WeaponSystem(Context* context) : BaseObject(context) |
---|
[2106] | 51 | { |
---|
| 52 | RegisterObject(WeaponSystem); |
---|
| 53 | |
---|
[10768] | 54 | this->pawn_ = nullptr; |
---|
[2106] | 55 | } |
---|
| 56 | |
---|
| 57 | WeaponSystem::~WeaponSystem() |
---|
| 58 | { |
---|
[2912] | 59 | if (this->isInitialized()) |
---|
| 60 | { |
---|
| 61 | if (this->pawn_) |
---|
[10768] | 62 | this->pawn_->setWeaponSystem(nullptr); |
---|
[2914] | 63 | |
---|
[2915] | 64 | while (!this->weaponSets_.empty()) |
---|
[5929] | 65 | this->weaponSets_.begin()->second->destroy(); |
---|
[2914] | 66 | |
---|
[2915] | 67 | while (!this->weaponPacks_.empty()) |
---|
[5929] | 68 | (*this->weaponPacks_.begin())->destroy(); |
---|
[2914] | 69 | |
---|
[2915] | 70 | while (!this->weaponSlots_.empty()) |
---|
[5929] | 71 | (*this->weaponSlots_.begin())->destroy(); |
---|
[2918] | 72 | |
---|
| 73 | while (!this->munitions_.empty()) |
---|
[5929] | 74 | { this->munitions_.begin()->second->destroy(); this->munitions_.erase(this->munitions_.begin()); } |
---|
[2912] | 75 | } |
---|
[2106] | 76 | } |
---|
| 77 | |
---|
[2914] | 78 | void WeaponSystem::addWeaponSlot(WeaponSlot * wSlot) |
---|
[2662] | 79 | { |
---|
[2914] | 80 | if (!wSlot) |
---|
[2912] | 81 | return; |
---|
| 82 | |
---|
[2915] | 83 | this->weaponSlots_.push_back(wSlot); |
---|
[2914] | 84 | wSlot->setWeaponSystem(this); |
---|
[2662] | 85 | } |
---|
| 86 | |
---|
[2914] | 87 | void WeaponSystem::removeWeaponSlot(WeaponSlot * wSlot) |
---|
[2912] | 88 | { |
---|
[2914] | 89 | if (!wSlot) |
---|
| 90 | return; |
---|
| 91 | |
---|
| 92 | if (wSlot->getWeapon()) |
---|
| 93 | this->removeWeaponPack(wSlot->getWeapon()->getWeaponPack()); |
---|
| 94 | |
---|
[2915] | 95 | for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it) |
---|
| 96 | { |
---|
| 97 | if ((*it) == wSlot) |
---|
| 98 | { |
---|
| 99 | this->weaponSlots_.erase(it); |
---|
| 100 | break; |
---|
| 101 | } |
---|
| 102 | } |
---|
[2914] | 103 | } |
---|
| 104 | |
---|
| 105 | WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const |
---|
| 106 | { |
---|
[2912] | 107 | unsigned int i = 0; |
---|
[10916] | 108 | for (WeaponSlot* weaponSlot : this->weaponSlots_) |
---|
[2912] | 109 | { |
---|
| 110 | ++i; |
---|
| 111 | if (i > index) |
---|
[10916] | 112 | return weaponSlot; |
---|
[2912] | 113 | } |
---|
[10768] | 114 | return nullptr; |
---|
[2912] | 115 | } |
---|
| 116 | |
---|
[2914] | 117 | bool WeaponSystem::addWeaponSet(WeaponSet * wSet) |
---|
[2662] | 118 | { |
---|
[2914] | 119 | if (wSet) |
---|
| 120 | return this->addWeaponSet(wSet, wSet->getDesiredFiremode()); |
---|
| 121 | else |
---|
| 122 | return false; |
---|
| 123 | } |
---|
[2912] | 124 | |
---|
[2914] | 125 | bool WeaponSystem::addWeaponSet(WeaponSet * wSet, unsigned int firemode) |
---|
| 126 | { |
---|
| 127 | if (!wSet || firemode >= WeaponSystem::MAX_FIRE_MODES) |
---|
| 128 | return false; |
---|
| 129 | |
---|
| 130 | std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.find(firemode); |
---|
| 131 | if (it == this->weaponSets_.end()) |
---|
| 132 | { |
---|
| 133 | this->weaponSets_[firemode] = wSet; |
---|
| 134 | wSet->setWeaponSystem(this); |
---|
| 135 | return true; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return false; |
---|
[2662] | 139 | } |
---|
| 140 | |
---|
[2914] | 141 | void WeaponSystem::removeWeaponSet(WeaponSet * wSet) |
---|
[2106] | 142 | { |
---|
[2914] | 143 | for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ) |
---|
| 144 | { |
---|
| 145 | if (it->second == wSet) |
---|
| 146 | this->weaponSets_.erase(it++); |
---|
| 147 | else |
---|
| 148 | ++it; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | WeaponSet * WeaponSystem::getWeaponSet(unsigned int index) const |
---|
| 153 | { |
---|
[2912] | 154 | unsigned int i = 0; |
---|
[10916] | 155 | for (const auto& mapEntry : this->weaponSets_) |
---|
[2912] | 156 | { |
---|
| 157 | ++i; |
---|
| 158 | if (i > index) |
---|
[10916] | 159 | return mapEntry.second; |
---|
[2912] | 160 | } |
---|
[10768] | 161 | return nullptr; |
---|
[2106] | 162 | } |
---|
| 163 | |
---|
[2914] | 164 | bool WeaponSystem::canAddWeaponPack(WeaponPack * wPack) |
---|
[2662] | 165 | { |
---|
[2912] | 166 | if (!wPack) |
---|
[2914] | 167 | return false; |
---|
[2912] | 168 | |
---|
[2914] | 169 | unsigned int freeSlots = 0; |
---|
[10916] | 170 | for (WeaponSlot* weaponSlot : this->weaponSlots_) |
---|
[2914] | 171 | { |
---|
[10916] | 172 | if (!weaponSlot->isOccupied()) |
---|
[2914] | 173 | ++freeSlots; |
---|
| 174 | } |
---|
[2912] | 175 | |
---|
[2914] | 176 | return (freeSlots >= wPack->getNumWeapons()); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | bool WeaponSystem::addWeaponPack(WeaponPack * wPack) |
---|
| 180 | { |
---|
| 181 | if (!this->canAddWeaponPack(wPack)) |
---|
| 182 | return false; |
---|
| 183 | |
---|
| 184 | // Attach all weapons to the first free slots (and to the Pawn) |
---|
| 185 | unsigned int i = 0; |
---|
[10916] | 186 | for (WeaponSlot* weaponSlot : this->weaponSlots_) |
---|
[2914] | 187 | { |
---|
[10916] | 188 | if (!weaponSlot->isOccupied() && i < wPack->getNumWeapons()) |
---|
[2914] | 189 | { |
---|
| 190 | Weapon* weapon = wPack->getWeapon(i); |
---|
[10916] | 191 | weaponSlot->attachWeapon(weapon); |
---|
[2914] | 192 | this->getPawn()->attach(weapon); |
---|
| 193 | ++i; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | // Assign the desired weaponmode to the firemodes |
---|
[10917] | 198 | for (const auto& mapEntry : this->weaponSets_) |
---|
[2914] | 199 | { |
---|
[10916] | 200 | unsigned int weaponmode = wPack->getDesiredWeaponmode(mapEntry.first); |
---|
[2914] | 201 | if (weaponmode != WeaponSystem::WEAPON_MODE_UNASSIGNED) |
---|
[10916] | 202 | mapEntry.second->setWeaponmodeLink(wPack, weaponmode); |
---|
[2914] | 203 | } |
---|
| 204 | |
---|
[6417] | 205 | this->weaponPacks_.push_back(wPack); |
---|
[2912] | 206 | wPack->setWeaponSystem(this); |
---|
[2914] | 207 | |
---|
| 208 | return true; |
---|
[2912] | 209 | } |
---|
| 210 | |
---|
[2914] | 211 | void WeaponSystem::removeWeaponPack(WeaponPack * wPack) |
---|
| 212 | { |
---|
| 213 | // Remove all weapons from their WeaponSlot |
---|
| 214 | unsigned int i = 0; |
---|
[10768] | 215 | Weapon* weapon = nullptr; |
---|
[3074] | 216 | while ((weapon = wPack->getWeapon(i++))) |
---|
[7163] | 217 | if (weapon->getWeaponSlot()) |
---|
| 218 | weapon->getWeaponSlot()->removeWeapon(); |
---|
[2914] | 219 | |
---|
| 220 | // Remove all added links from the WeaponSets |
---|
[10917] | 221 | for (const auto& mapEntry : this->weaponSets_) |
---|
[10916] | 222 | mapEntry.second->removeWeaponmodeLink(wPack); |
---|
[2914] | 223 | |
---|
| 224 | // Remove the WeaponPack from the WeaponSystem |
---|
[7183] | 225 | std::vector<WeaponPack*>::iterator it2 = std::find(this->weaponPacks_.begin(),this->weaponPacks_.end(), wPack); |
---|
| 226 | assert(it2 !=this->weaponPacks_.end()); |
---|
| 227 | this->weaponPacks_.erase(it2); |
---|
[2914] | 228 | } |
---|
| 229 | |
---|
[2912] | 230 | WeaponPack * WeaponSystem::getWeaponPack(unsigned int index) const |
---|
| 231 | { |
---|
| 232 | unsigned int i = 0; |
---|
[10916] | 233 | for (WeaponPack* weaponPack : this->weaponPacks_) |
---|
[2912] | 234 | { |
---|
| 235 | ++i; |
---|
| 236 | if (i > index) |
---|
[10916] | 237 | return weaponPack; |
---|
[2912] | 238 | } |
---|
[10768] | 239 | return nullptr; |
---|
[2914] | 240 | } |
---|
[2912] | 241 | |
---|
[2914] | 242 | bool WeaponSystem::swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2) |
---|
| 243 | { |
---|
[2915] | 244 | if (!wSlot1 || !wSlot2) |
---|
| 245 | return false; |
---|
| 246 | |
---|
| 247 | Weapon* weapon1 = wSlot1->getWeapon(); |
---|
| 248 | Weapon* weapon2 = wSlot2->getWeapon(); |
---|
| 249 | |
---|
| 250 | wSlot1->attachWeapon(weapon2); |
---|
| 251 | wSlot2->attachWeapon(weapon1); |
---|
| 252 | |
---|
| 253 | return true; |
---|
| 254 | // In the future, certain weapons might not fit to some slots. Swapping would then be |
---|
| 255 | // impossible and the returnvalue would be false. |
---|
[2914] | 256 | } |
---|
| 257 | |
---|
| 258 | void WeaponSystem::changeWeaponmode(WeaponPack * wPack, WeaponSet * wSet, unsigned int weaponmode) |
---|
| 259 | { |
---|
| 260 | if (!wPack || !wSet) |
---|
| 261 | return; |
---|
| 262 | |
---|
| 263 | // Check if the WeaponPack belongs to this WeaponSystem |
---|
[6417] | 264 | std::vector<WeaponPack *>::iterator it1 = std::find( this->weaponPacks_.begin(), this->weaponPacks_.end(), wPack ); |
---|
[2914] | 265 | if (it1 == this->weaponPacks_.end()) |
---|
| 266 | return; |
---|
| 267 | |
---|
| 268 | // Check if the WeaponSet belongs to this WeaponSystem |
---|
| 269 | bool foundWeaponSet = false; |
---|
[10917] | 270 | for (const auto& mapEntry : this->weaponSets_) |
---|
[2914] | 271 | { |
---|
[10916] | 272 | if (mapEntry.second == wSet) |
---|
[2914] | 273 | { |
---|
| 274 | foundWeaponSet = true; |
---|
| 275 | break; |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | if (!foundWeaponSet) |
---|
| 279 | return; |
---|
| 280 | |
---|
| 281 | // Finally set the link between firemode and weaponmode |
---|
| 282 | wSet->setWeaponmodeLink(wPack, weaponmode); |
---|
| 283 | } |
---|
| 284 | |
---|
[10650] | 285 | /** |
---|
| 286 | @brief |
---|
| 287 | Fires the @ref Orxonox::WeaponSet with the specified firemode. |
---|
| 288 | */ |
---|
[2918] | 289 | void WeaponSystem::fire(unsigned int firemode) |
---|
[2912] | 290 | { |
---|
[2918] | 291 | std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.find(firemode); |
---|
| 292 | if (it != this->weaponSets_.end() && it->second) |
---|
| 293 | it->second->fire(); |
---|
[2662] | 294 | } |
---|
| 295 | |
---|
[2918] | 296 | void WeaponSystem::reload() |
---|
| 297 | { |
---|
[10917] | 298 | for (const auto& mapEntry : this->weaponSets_) |
---|
[10916] | 299 | mapEntry.second->reload(); |
---|
[2918] | 300 | } |
---|
[2893] | 301 | |
---|
[2918] | 302 | Munition * WeaponSystem::getMunition(SubclassIdentifier<Munition> * identifier) |
---|
[2662] | 303 | { |
---|
[2918] | 304 | if (!identifier || !identifier->getIdentifier()) |
---|
[10768] | 305 | return nullptr; |
---|
[2918] | 306 | |
---|
| 307 | std::map<Identifier *, Munition *>::iterator it = this->munitions_.find(identifier->getIdentifier()); |
---|
| 308 | if (it != this->munitions_.end()) |
---|
| 309 | { |
---|
[2662] | 310 | return it->second; |
---|
[2918] | 311 | } |
---|
| 312 | else if (identifier->getIdentifier()->isA(Class(Munition))) |
---|
| 313 | { |
---|
[9667] | 314 | Munition* munition = identifier->fabricate(this->getContext()); |
---|
[2918] | 315 | this->munitions_[identifier->getIdentifier()] = munition; |
---|
| 316 | return munition; |
---|
| 317 | } |
---|
[2662] | 318 | else |
---|
[2918] | 319 | { |
---|
[10768] | 320 | return nullptr; |
---|
[2918] | 321 | } |
---|
[2662] | 322 | } |
---|
[2106] | 323 | } |
---|