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 | * Gani Aliguzhinov |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
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) : ActionpointController(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->bFirstAction_ = true; |
---|
50 | //orxout(internal_error) << this << "Was created" << endl; |
---|
51 | |
---|
52 | } |
---|
53 | |
---|
54 | SectionController::~SectionController() |
---|
55 | { |
---|
56 | for (size_t i = 0; i < this->actionpoints_.size(); ++i) |
---|
57 | { |
---|
58 | if(this->actionpoints_[i]) |
---|
59 | this->actionpoints_[i]->destroy(); |
---|
60 | } |
---|
61 | this->parsedActionpoints_.clear(); |
---|
62 | this->actionpoints_.clear(); |
---|
63 | } |
---|
64 | void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
65 | { |
---|
66 | SUPER(SectionController, XMLPort, xmlelement, mode); |
---|
67 | } |
---|
68 | |
---|
69 | //----in tick, move (or look) and shoot---- |
---|
70 | void SectionController::tick(float dt) |
---|
71 | { |
---|
72 | if (!this->isActive()) |
---|
73 | return; |
---|
74 | |
---|
75 | SUPER(SectionController, tick, dt); |
---|
76 | if (this->timeOffset_ >= 0.5f && this->timeOffset_ <= 1.0f && !this->bActionCalled_) |
---|
77 | { |
---|
78 | this->action(); |
---|
79 | this->bActionCalled_ = true; |
---|
80 | } |
---|
81 | if (this->timeOffset_ > 1.5f) |
---|
82 | { |
---|
83 | this->bActionCalled_ = false; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void SectionController::action() |
---|
88 | { |
---|
89 | if (!this || !this->getControllableEntity()) |
---|
90 | return; |
---|
91 | |
---|
92 | //----If no leader, find one---- |
---|
93 | if (!myDivisionLeader_) |
---|
94 | { |
---|
95 | ActionpointController* newDivisionLeader = findNewDivisionLeader(); |
---|
96 | this->myDivisionLeader_ = newDivisionLeader; |
---|
97 | //spread copyOrientation called equally among the division |
---|
98 | if (this->myDivisionLeader_) |
---|
99 | { |
---|
100 | this->actionCounter_ = 5; |
---|
101 | } |
---|
102 | } |
---|
103 | //----If have leader---- |
---|
104 | else |
---|
105 | { |
---|
106 | } |
---|
107 | if (!myDivisionLeader_) |
---|
108 | { |
---|
109 | ActionpointController::action(); |
---|
110 | if (!this || !this->getControllableEntity()) |
---|
111 | return; |
---|
112 | if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty())) |
---|
113 | { |
---|
114 | if (this->myWingman_) |
---|
115 | { |
---|
116 | this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_); |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | else if (myDivisionLeader_) |
---|
121 | { |
---|
122 | if (this->myDivisionLeader_->bKeepFormation_ || !(this->myDivisionLeader_->getAction() == Action::FIGHT |
---|
123 | || this->myDivisionLeader_->getAction() == Action::FIGHTALL |
---|
124 | || this->myDivisionLeader_->getAction() == Action::ATTACK)) |
---|
125 | { |
---|
126 | this->keepFormation(); |
---|
127 | } |
---|
128 | else if (!this->myDivisionLeader_->bKeepFormation_) |
---|
129 | { |
---|
130 | if (!this->hasTarget()) |
---|
131 | { |
---|
132 | this->chooseTarget(); |
---|
133 | } |
---|
134 | if (this->hasTarget()) |
---|
135 | { |
---|
136 | // this->maneuver(); |
---|
137 | // this->bShooting_ = this->canFire(); |
---|
138 | // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length()); |
---|
139 | // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_) |
---|
140 | // { |
---|
141 | // //----choose where to go---- |
---|
142 | // this->maneuver(); |
---|
143 | // } |
---|
144 | // else |
---|
145 | // { |
---|
146 | // this->dodgeTowards(healthPosition); |
---|
147 | // } |
---|
148 | // //----fire if you can---- |
---|
149 | // this->bShooting_ = this->canFire(); |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT |
---|
158 | //POST: this->target_ is set unless division leader doesn't have one |
---|
159 | void SectionController::chooseTarget() |
---|
160 | { |
---|
161 | //----If division leader fights, cover him by fighting emenies close to his target---- |
---|
162 | Action::Value action = this->myDivisionLeader_->getAction(); |
---|
163 | |
---|
164 | Pawn* target; |
---|
165 | if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK) |
---|
166 | { |
---|
167 | //----if he has a target---- |
---|
168 | if (this->myDivisionLeader_->hasTarget()) |
---|
169 | { |
---|
170 | //----try to find a new target if division leader has wingman (doing fine) and no good target already set---- |
---|
171 | if ( this->myDivisionLeader_->hasWingman() && |
---|
172 | !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) ) |
---|
173 | { |
---|
174 | |
---|
175 | bool foundTarget = false; |
---|
176 | //----new target should be close to division's target---- |
---|
177 | Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition(); |
---|
178 | Gametype* gt = this->getGametype(); |
---|
179 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
180 | { |
---|
181 | //----is enemy?---- |
---|
182 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) |
---|
183 | continue; |
---|
184 | //----in range?---- |
---|
185 | if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && |
---|
186 | (*itP) != this->myDivisionLeader_->getTarget()) |
---|
187 | { |
---|
188 | foundTarget = true; |
---|
189 | target = (*itP); |
---|
190 | //orxout(internal_error) << "Found target" << endl; |
---|
191 | break; |
---|
192 | } |
---|
193 | } |
---|
194 | //----no target? then attack same target as division leader---- |
---|
195 | if (!foundTarget) |
---|
196 | { |
---|
197 | target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget()); |
---|
198 | } |
---|
199 | } |
---|
200 | //----if division leader doesn't have a wingman, support his fire---- |
---|
201 | else |
---|
202 | { |
---|
203 | target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget()); |
---|
204 | } |
---|
205 | } |
---|
206 | //----If he fights but doesn't have a target, wait for him to get one---- |
---|
207 | else |
---|
208 | { |
---|
209 | |
---|
210 | } |
---|
211 | this->setTarget (orxonox_cast<ControllableEntity*>(target)); |
---|
212 | } |
---|
213 | else |
---|
214 | { |
---|
215 | } |
---|
216 | } |
---|
217 | Vector3 SectionController::getFormationPosition () |
---|
218 | { |
---|
219 | this->setFormationMode( this->myDivisionLeader_->getFormationMode() ); |
---|
220 | this->spread_ = this->myDivisionLeader_->getSpread(); |
---|
221 | Vector3* targetRelativePosition; |
---|
222 | switch (this->formationMode_){ |
---|
223 | case FormationMode::WALL: |
---|
224 | { |
---|
225 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0); |
---|
226 | break; |
---|
227 | } |
---|
228 | case FormationMode::FINGER4: |
---|
229 | { |
---|
230 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_); |
---|
231 | break; |
---|
232 | } |
---|
233 | |
---|
234 | case FormationMode::DIAMOND: |
---|
235 | { |
---|
236 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_); |
---|
237 | break; |
---|
238 | } |
---|
239 | } |
---|
240 | Vector3 result = *targetRelativePosition; |
---|
241 | delete targetRelativePosition; |
---|
242 | return result; |
---|
243 | } |
---|
244 | |
---|
245 | void SectionController::keepFormation() |
---|
246 | { |
---|
247 | this->bKeepFormation_ = true; |
---|
248 | ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity(); |
---|
249 | Vector3 targetRelativePosition = this->getFormationPosition(); |
---|
250 | if (!leaderEntity) |
---|
251 | return; |
---|
252 | FlyingController::keepFormation(leaderEntity, targetRelativePosition); |
---|
253 | } |
---|
254 | |
---|
255 | ActionpointController* SectionController::findNewDivisionLeader() |
---|
256 | { |
---|
257 | |
---|
258 | if (!this->getControllableEntity()) |
---|
259 | return 0; |
---|
260 | |
---|
261 | ActionpointController* closestLeader = 0; |
---|
262 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
263 | //go through all pawns |
---|
264 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it) |
---|
265 | { |
---|
266 | //0ptr or not DivisionController? |
---|
267 | if (!(it) || !((it)->getIdentifier()->getName() == "DivisionController") || !(it->getControllableEntity())) |
---|
268 | continue; |
---|
269 | //same team? |
---|
270 | if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) |
---|
271 | continue; |
---|
272 | |
---|
273 | //is equal to this? |
---|
274 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
---|
275 | continue; |
---|
276 | |
---|
277 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
278 | |
---|
279 | if (distance < minDistance && !(it->hasFollower())) |
---|
280 | { |
---|
281 | closestLeader = *it; |
---|
282 | minDistance = distance; |
---|
283 | } |
---|
284 | |
---|
285 | } |
---|
286 | if (closestLeader) |
---|
287 | { |
---|
288 | if (closestLeader->setFollower(this)) |
---|
289 | return closestLeader; |
---|
290 | } |
---|
291 | return 0; |
---|
292 | } |
---|
293 | bool SectionController::setWingman(ActionpointController* newWingman) |
---|
294 | { |
---|
295 | |
---|
296 | if (!this->myWingman_) |
---|
297 | { |
---|
298 | this->myWingman_ = newWingman; |
---|
299 | newWingman->takeActionpoints (this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_); |
---|
300 | return true; |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | return false; |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | bool SectionController::hasWingman() |
---|
309 | { |
---|
310 | if (this->myWingman_) |
---|
311 | return true; |
---|
312 | else |
---|
313 | return false; |
---|
314 | } |
---|
315 | } |
---|