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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * Dominik Solenicki |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "SectionController.h" |
---|
30 | //TODO: formation vectors are wrong, fix it. |
---|
31 | // split classes. |
---|
32 | // weaponsystem. |
---|
33 | //-> Math ? |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | |
---|
38 | RegisterClass(SectionController); |
---|
39 | |
---|
40 | //Leaders share the fact that they have Wingmans |
---|
41 | SectionController::SectionController(Context* context) : LeaderController(context) |
---|
42 | { |
---|
43 | RegisterObject(SectionController); |
---|
44 | this->setFormationMode(FormationMode::FINGER4); |
---|
45 | |
---|
46 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this))); |
---|
47 | this->myWingman_ = 0; |
---|
48 | this->myDivisionLeader_ = 0; |
---|
49 | this->rank_ = Rank::SECTIONLEADER; |
---|
50 | this->bFirstAction_ = true; |
---|
51 | //orxout(internal_error) << this << "Was created" << endl; |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | SectionController::~SectionController() |
---|
56 | { |
---|
57 | // if (this->myWingman_) |
---|
58 | // { |
---|
59 | // this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_); |
---|
60 | // } |
---|
61 | for (size_t i = 0; i < this->actionpoints_.size(); ++i) |
---|
62 | { |
---|
63 | if(this->actionpoints_[i]) |
---|
64 | this->actionpoints_[i]->destroy(); |
---|
65 | } |
---|
66 | this->parsedActionpoints_.clear(); |
---|
67 | this->actionpoints_.clear(); |
---|
68 | } |
---|
69 | void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
70 | { |
---|
71 | SUPER(SectionController, XMLPort, xmlelement, mode); |
---|
72 | |
---|
73 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
74 | } |
---|
75 | |
---|
76 | //----in tick, move (or look) and shoot---- |
---|
77 | void SectionController::tick(float dt) |
---|
78 | { |
---|
79 | if (!this->isActive()) |
---|
80 | return; |
---|
81 | |
---|
82 | |
---|
83 | SUPER(SectionController, tick, dt); |
---|
84 | } |
---|
85 | |
---|
86 | void SectionController::action() |
---|
87 | { |
---|
88 | if (!this || !this->getControllableEntity()) |
---|
89 | return; |
---|
90 | |
---|
91 | //----If no leader, find one---- |
---|
92 | if (!myDivisionLeader_) |
---|
93 | { |
---|
94 | LeaderController* newDivisionLeader = findNewDivisionLeader(); |
---|
95 | this->myDivisionLeader_ = newDivisionLeader; |
---|
96 | |
---|
97 | } |
---|
98 | //----If have leader---- |
---|
99 | else |
---|
100 | { |
---|
101 | } |
---|
102 | if (!myDivisionLeader_) |
---|
103 | { |
---|
104 | |
---|
105 | ActionpointController::action(); |
---|
106 | if (!this || !this->getControllableEntity()) |
---|
107 | return; |
---|
108 | if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty())) |
---|
109 | { |
---|
110 | if (this->myWingman_) |
---|
111 | { |
---|
112 | this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_); |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | else if (myDivisionLeader_) |
---|
117 | { |
---|
118 | switch (myDivisionLeader_->getAction()) |
---|
119 | { |
---|
120 | case Action::FIGHT: |
---|
121 | { |
---|
122 | if (!this->hasTarget()) |
---|
123 | { |
---|
124 | this->chooseTarget(); |
---|
125 | } |
---|
126 | break; |
---|
127 | } |
---|
128 | case Action::FIGHTALL: |
---|
129 | { |
---|
130 | if (!this->hasTarget()) |
---|
131 | { |
---|
132 | this->chooseTarget(); |
---|
133 | } |
---|
134 | break; |
---|
135 | } |
---|
136 | case Action::ATTACK: |
---|
137 | { |
---|
138 | if (!this->hasTarget()) |
---|
139 | { |
---|
140 | this->chooseTarget(); |
---|
141 | } |
---|
142 | break; |
---|
143 | } |
---|
144 | default: |
---|
145 | { |
---|
146 | ControllableEntity* myEntity = this->getControllableEntity(); |
---|
147 | Vector3 myPosition = myEntity->getWorldPosition(); |
---|
148 | if (!this->myDivisionLeader_) |
---|
149 | { |
---|
150 | return; |
---|
151 | } |
---|
152 | ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity(); |
---|
153 | Quaternion orient = leaderEntity->getWorldOrientation(); |
---|
154 | Vector3 leaderPosition = leaderEntity->getWorldPosition(); |
---|
155 | |
---|
156 | Vector3 targetRelativePosition = getFormationPosition(); |
---|
157 | if (!this->myDivisionLeader_) |
---|
158 | { |
---|
159 | return; |
---|
160 | } |
---|
161 | Vector3 targetAbsolutePosition = |
---|
162 | (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5) |
---|
163 | + (orient* (targetRelativePosition))); |
---|
164 | |
---|
165 | this->setAction (Action::FLY, targetAbsolutePosition, orient); |
---|
166 | if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f) |
---|
167 | { |
---|
168 | this->boostControl(); |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | this->getControllableEntity()->boost(false); |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | if (this->hasTarget()) |
---|
177 | { |
---|
178 | //----choose where to go---- |
---|
179 | this->maneuver(); |
---|
180 | //----fire if you can---- |
---|
181 | this->bShooting_ = this->canFire(); |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT |
---|
189 | //POST: this->target_ is set unless division leader doesn't have one |
---|
190 | void SectionController::chooseTarget() |
---|
191 | { |
---|
192 | //----If division leader fights, cover him by fighting emenies close to his target---- |
---|
193 | Action::Value action = this->myDivisionLeader_->getAction(); |
---|
194 | |
---|
195 | Pawn* target; |
---|
196 | if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK) |
---|
197 | { |
---|
198 | //----if he has a target---- |
---|
199 | if (this->myDivisionLeader_->hasTarget()) |
---|
200 | { |
---|
201 | //----try to find a new target if division leader has wingman (doing fine) and no good target already set---- |
---|
202 | if ( this->myDivisionLeader_->hasWingman() && |
---|
203 | !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) ) |
---|
204 | { |
---|
205 | |
---|
206 | bool foundTarget = false; |
---|
207 | //----new target should be close to division's target---- |
---|
208 | Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition(); |
---|
209 | Gametype* gt = this->getGametype(); |
---|
210 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
211 | { |
---|
212 | //----is enemy?---- |
---|
213 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) |
---|
214 | continue; |
---|
215 | //----in range?---- |
---|
216 | if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && |
---|
217 | (*itP) != this->myDivisionLeader_->getTarget()) |
---|
218 | { |
---|
219 | foundTarget = true; |
---|
220 | target = (*itP); |
---|
221 | //orxout(internal_error) << "Found target" << endl; |
---|
222 | break; |
---|
223 | } |
---|
224 | } |
---|
225 | //----no target? then attack same target as division leader---- |
---|
226 | if (!foundTarget) |
---|
227 | { |
---|
228 | target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget()); |
---|
229 | } |
---|
230 | } |
---|
231 | //----if division leader doesn't have a wingman, support his fire---- |
---|
232 | else |
---|
233 | { |
---|
234 | target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget()); |
---|
235 | } |
---|
236 | } |
---|
237 | //----If he fights but doesn't have a target, wait for him to get one---- |
---|
238 | else |
---|
239 | { |
---|
240 | |
---|
241 | } |
---|
242 | this->setTarget (orxonox_cast<ControllableEntity*>(target)); |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | } |
---|
247 | } |
---|
248 | Vector3 SectionController::getFormationPosition () |
---|
249 | { |
---|
250 | this->setFormationMode( this->myDivisionLeader_->getFormationMode() ); |
---|
251 | Vector3* targetRelativePosition; |
---|
252 | switch (this->formationMode_){ |
---|
253 | case FormationMode::WALL: |
---|
254 | { |
---|
255 | targetRelativePosition = new Vector3 (-400, 0, 0); |
---|
256 | break; |
---|
257 | } |
---|
258 | case FormationMode::FINGER4: |
---|
259 | { |
---|
260 | targetRelativePosition = new Vector3 (-400, 0, 200); |
---|
261 | break; |
---|
262 | } |
---|
263 | |
---|
264 | case FormationMode::DIAMOND: |
---|
265 | { |
---|
266 | targetRelativePosition = new Vector3 (-400, 0, 200); |
---|
267 | break; |
---|
268 | } |
---|
269 | } |
---|
270 | return *targetRelativePosition; |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | |
---|
275 | LeaderController* SectionController::findNewDivisionLeader() |
---|
276 | { |
---|
277 | |
---|
278 | if (!this->getControllableEntity()) |
---|
279 | return 0; |
---|
280 | |
---|
281 | LeaderController* closestLeader = 0; |
---|
282 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
283 | //go through all pawns |
---|
284 | for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it) |
---|
285 | { |
---|
286 | //0ptr or not DivisionController? |
---|
287 | if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity())) |
---|
288 | continue; |
---|
289 | //same team? |
---|
290 | if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) |
---|
291 | continue; |
---|
292 | |
---|
293 | //is equal to this? |
---|
294 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
---|
295 | continue; |
---|
296 | |
---|
297 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
298 | |
---|
299 | if (distance < minDistance && !(it->hasFollower())) |
---|
300 | { |
---|
301 | closestLeader = *it; |
---|
302 | minDistance = distance; |
---|
303 | } |
---|
304 | |
---|
305 | } |
---|
306 | if (closestLeader) |
---|
307 | { |
---|
308 | if (closestLeader->setFollower(this)) |
---|
309 | return closestLeader; |
---|
310 | } |
---|
311 | return 0; |
---|
312 | } |
---|
313 | bool SectionController::setWingman(ActionpointController* cwingman) |
---|
314 | { |
---|
315 | WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman); |
---|
316 | |
---|
317 | if (!this->myWingman_) |
---|
318 | { |
---|
319 | this->myWingman_ = wingman; |
---|
320 | return true; |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | return false; |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | bool SectionController::hasWingman() |
---|
329 | { |
---|
330 | if (this->myWingman_) |
---|
331 | return true; |
---|
332 | else |
---|
333 | return false; |
---|
334 | } |
---|
335 | } |
---|