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