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