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 | |
---|
29 | #include "WeaponSystem.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "worldentities/pawns/Pawn.h" |
---|
33 | |
---|
34 | #include "WeaponSlot.h" |
---|
35 | #include "WeaponPack.h" |
---|
36 | #include "WeaponSet.h" |
---|
37 | #include "Weapon.h" |
---|
38 | #include "Munition.h" |
---|
39 | |
---|
40 | /* WeaponSystem |
---|
41 | * |
---|
42 | * www.orxonox.net/wiki/WeaponSystem |
---|
43 | */ |
---|
44 | |
---|
45 | namespace orxonox |
---|
46 | { |
---|
47 | CreateFactory(WeaponSystem); |
---|
48 | |
---|
49 | WeaponSystem::WeaponSystem(BaseObject* creator) : BaseObject(creator) |
---|
50 | { |
---|
51 | RegisterObject(WeaponSystem); |
---|
52 | |
---|
53 | this->pawn_ = 0; |
---|
54 | } |
---|
55 | |
---|
56 | WeaponSystem::~WeaponSystem() |
---|
57 | { |
---|
58 | if (this->isInitialized()) |
---|
59 | { |
---|
60 | if (this->pawn_) |
---|
61 | this->pawn_->setWeaponSystem(0); |
---|
62 | |
---|
63 | while (!this->weaponSets_.empty()) |
---|
64 | delete (this->weaponSets_.begin()->second); |
---|
65 | |
---|
66 | while (!this->weaponPacks_.empty()) |
---|
67 | delete (*this->weaponPacks_.begin()); |
---|
68 | |
---|
69 | while (!this->weaponSlots_.empty()) |
---|
70 | delete (*this->weaponSlots_.begin()); |
---|
71 | |
---|
72 | while (!this->munitions_.empty()) |
---|
73 | { delete (this->munitions_.begin()->second); this->munitions_.erase(this->munitions_.begin()); } |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void WeaponSystem::addWeaponSlot(WeaponSlot * wSlot) |
---|
78 | { |
---|
79 | if (!wSlot) |
---|
80 | return; |
---|
81 | |
---|
82 | this->weaponSlots_.push_back(wSlot); |
---|
83 | wSlot->setWeaponSystem(this); |
---|
84 | } |
---|
85 | |
---|
86 | void WeaponSystem::removeWeaponSlot(WeaponSlot * wSlot) |
---|
87 | { |
---|
88 | if (!wSlot) |
---|
89 | return; |
---|
90 | |
---|
91 | if (wSlot->getWeapon()) |
---|
92 | this->removeWeaponPack(wSlot->getWeapon()->getWeaponPack()); |
---|
93 | |
---|
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 | } |
---|
102 | } |
---|
103 | |
---|
104 | WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const |
---|
105 | { |
---|
106 | unsigned int i = 0; |
---|
107 | for (std::vector<WeaponSlot*>::const_iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it) |
---|
108 | { |
---|
109 | ++i; |
---|
110 | if (i > index) |
---|
111 | return (*it); |
---|
112 | } |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | bool WeaponSystem::addWeaponSet(WeaponSet * wSet) |
---|
117 | { |
---|
118 | if (wSet) |
---|
119 | return this->addWeaponSet(wSet, wSet->getDesiredFiremode()); |
---|
120 | else |
---|
121 | return false; |
---|
122 | } |
---|
123 | |
---|
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; |
---|
138 | } |
---|
139 | |
---|
140 | void WeaponSystem::removeWeaponSet(WeaponSet * wSet) |
---|
141 | { |
---|
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 | { |
---|
153 | unsigned int i = 0; |
---|
154 | for (std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it) |
---|
155 | { |
---|
156 | ++i; |
---|
157 | if (i > index) |
---|
158 | return it->second; |
---|
159 | } |
---|
160 | return 0; |
---|
161 | } |
---|
162 | |
---|
163 | bool WeaponSystem::canAddWeaponPack(WeaponPack * wPack) |
---|
164 | { |
---|
165 | if (!wPack) |
---|
166 | return false; |
---|
167 | |
---|
168 | unsigned int freeSlots = 0; |
---|
169 | for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it) |
---|
170 | { |
---|
171 | if (!(*it)->isOccupied()) |
---|
172 | ++freeSlots; |
---|
173 | } |
---|
174 | |
---|
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; |
---|
185 | for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it) |
---|
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 | |
---|
204 | this->weaponPacks_.insert(wPack); |
---|
205 | wPack->setWeaponSystem(this); |
---|
206 | |
---|
207 | return true; |
---|
208 | } |
---|
209 | |
---|
210 | void WeaponSystem::removeWeaponPack(WeaponPack * wPack) |
---|
211 | { |
---|
212 | // Remove all weapons from their WeaponSlot |
---|
213 | unsigned int i = 0; |
---|
214 | Weapon* weapon = 0; |
---|
215 | while ((weapon = wPack->getWeapon(i++))) |
---|
216 | weapon->getWeaponSlot()->removeWeapon(); |
---|
217 | |
---|
218 | // Remove all added links from the WeaponSets |
---|
219 | for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it) |
---|
220 | it->second->removeWeaponmodeLink(wPack); |
---|
221 | |
---|
222 | // Remove the WeaponPack from the WeaponSystem |
---|
223 | this->weaponPacks_.erase(wPack); |
---|
224 | } |
---|
225 | |
---|
226 | WeaponPack * WeaponSystem::getWeaponPack(unsigned int index) const |
---|
227 | { |
---|
228 | unsigned int i = 0; |
---|
229 | for (std::set<WeaponPack*>::const_iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); ++it) |
---|
230 | { |
---|
231 | ++i; |
---|
232 | if (i > index) |
---|
233 | return (*it); |
---|
234 | } |
---|
235 | return 0; |
---|
236 | } |
---|
237 | |
---|
238 | bool WeaponSystem::swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2) |
---|
239 | { |
---|
240 | if (!wSlot1 || !wSlot2) |
---|
241 | return false; |
---|
242 | |
---|
243 | Weapon* weapon1 = wSlot1->getWeapon(); |
---|
244 | Weapon* weapon2 = wSlot2->getWeapon(); |
---|
245 | |
---|
246 | wSlot1->attachWeapon(weapon2); |
---|
247 | wSlot2->attachWeapon(weapon1); |
---|
248 | |
---|
249 | return true; |
---|
250 | // In the future, certain weapons might not fit to some slots. Swapping would then be |
---|
251 | // impossible and the returnvalue would be false. |
---|
252 | } |
---|
253 | |
---|
254 | void WeaponSystem::changeWeaponmode(WeaponPack * wPack, WeaponSet * wSet, unsigned int weaponmode) |
---|
255 | { |
---|
256 | if (!wPack || !wSet) |
---|
257 | return; |
---|
258 | |
---|
259 | // Check if the WeaponPack belongs to this WeaponSystem |
---|
260 | std::set<WeaponPack *>::iterator it1 = this->weaponPacks_.find(wPack); |
---|
261 | if (it1 == this->weaponPacks_.end()) |
---|
262 | return; |
---|
263 | |
---|
264 | // Check if the WeaponSet belongs to this WeaponSystem |
---|
265 | bool foundWeaponSet = false; |
---|
266 | for (std::map<unsigned int, WeaponSet *>::iterator it2 = this->weaponSets_.begin(); it2 != this->weaponSets_.end(); ++it2) |
---|
267 | { |
---|
268 | if (it2->second == wSet) |
---|
269 | { |
---|
270 | foundWeaponSet = true; |
---|
271 | break; |
---|
272 | } |
---|
273 | } |
---|
274 | if (!foundWeaponSet) |
---|
275 | return; |
---|
276 | |
---|
277 | // Finally set the link between firemode and weaponmode |
---|
278 | wSet->setWeaponmodeLink(wPack, weaponmode); |
---|
279 | } |
---|
280 | |
---|
281 | void WeaponSystem::fire(unsigned int firemode) |
---|
282 | { |
---|
283 | std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.find(firemode); |
---|
284 | if (it != this->weaponSets_.end() && it->second) |
---|
285 | it->second->fire(); |
---|
286 | } |
---|
287 | |
---|
288 | void WeaponSystem::reload() |
---|
289 | { |
---|
290 | for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it) |
---|
291 | it->second->reload(); |
---|
292 | } |
---|
293 | |
---|
294 | Munition * WeaponSystem::getMunition(SubclassIdentifier<Munition> * identifier) |
---|
295 | { |
---|
296 | if (!identifier || !identifier->getIdentifier()) |
---|
297 | return 0; |
---|
298 | |
---|
299 | std::map<Identifier *, Munition *>::iterator it = this->munitions_.find(identifier->getIdentifier()); |
---|
300 | if (it != this->munitions_.end()) |
---|
301 | { |
---|
302 | return it->second; |
---|
303 | } |
---|
304 | else if (identifier->getIdentifier()->isA(Class(Munition))) |
---|
305 | { |
---|
306 | Munition* munition = identifier->fabricate(this); |
---|
307 | this->munitions_[identifier->getIdentifier()] = munition; |
---|
308 | return munition; |
---|
309 | } |
---|
310 | else |
---|
311 | { |
---|
312 | return 0; |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|