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 | |
---|
47 | //orxout(internal_error) << this << "Was created" << endl; |
---|
48 | |
---|
49 | } |
---|
50 | |
---|
51 | SectionController::~SectionController() |
---|
52 | { |
---|
53 | |
---|
54 | } |
---|
55 | void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
56 | { |
---|
57 | SUPER(SectionController, XMLPort, xmlelement, mode); |
---|
58 | |
---|
59 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
60 | } |
---|
61 | |
---|
62 | //----in tick, move (or look) and shoot---- |
---|
63 | void SectionController::tick(float dt) |
---|
64 | { |
---|
65 | if (!this->isActive()) |
---|
66 | return; |
---|
67 | if (this->bHasTargetPosition_) |
---|
68 | { |
---|
69 | this->moveToTargetPosition(dt); |
---|
70 | } |
---|
71 | else if (this->bLookAtTarget_) |
---|
72 | { |
---|
73 | this->lookAtTarget(dt); |
---|
74 | } |
---|
75 | if (bShooting_) |
---|
76 | { |
---|
77 | this->doFire(); |
---|
78 | } |
---|
79 | |
---|
80 | SUPER(SectionController, tick, dt); |
---|
81 | } |
---|
82 | |
---|
83 | void SectionController::action() |
---|
84 | { |
---|
85 | //----If no leader, find one---- |
---|
86 | if (!myDivisionLeader_) |
---|
87 | { |
---|
88 | LeaderController* newDivisionLeader = findNewDivisionLeader(); |
---|
89 | this->myDivisionLeader_ = newDivisionLeader; |
---|
90 | |
---|
91 | if (newDivisionLeader) |
---|
92 | { |
---|
93 | //orxout(internal_error) << "new DivisionLeader set" << endl; |
---|
94 | } |
---|
95 | //----If no leader found, attack someone---- |
---|
96 | //----TODO: find closest enemy---- |
---|
97 | else |
---|
98 | { |
---|
99 | if ( !this->hasTarget() || this->action_ != Action::FIGHT ) |
---|
100 | { |
---|
101 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
102 | { |
---|
103 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) ) |
---|
104 | continue; |
---|
105 | |
---|
106 | this->setAction(Action::FIGHT, (*itP)); |
---|
107 | break; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|
114 | //----If have leader---- |
---|
115 | else |
---|
116 | { |
---|
117 | this->chooseTarget(); |
---|
118 | } |
---|
119 | |
---|
120 | //----action was set to fight---- |
---|
121 | if (this->action_ == Action::FIGHT) |
---|
122 | { |
---|
123 | //----choose where to go---- |
---|
124 | this->maneuver(); |
---|
125 | //----fire if you can---- |
---|
126 | this->bShooting_ = this->canFire(); |
---|
127 | |
---|
128 | if (this->target_) |
---|
129 | { |
---|
130 | //----wingmans shall support the fire of their leaders---- |
---|
131 | if (this->myWingman_) |
---|
132 | { |
---|
133 | this->myWingman_->setAction (Action::FIGHT, this->target_); |
---|
134 | } |
---|
135 | //----fly in formation if far enough---- |
---|
136 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
137 | if (diffVector.length() > 3000) |
---|
138 | { |
---|
139 | this->setTargetPositionOfWingman(); |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | //----action was set to fly---- |
---|
145 | else if (this->action_ == Action::FLY) |
---|
146 | { |
---|
147 | this->setTargetPositionOfWingman(); |
---|
148 | } |
---|
149 | |
---|
150 | //----action was set to protect---- |
---|
151 | else if (this->action_ == Action::PROTECT) |
---|
152 | { |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | } |
---|
158 | //PRE: myDivisionLeader_ != 0 |
---|
159 | //POST: this->target_ is set unless division leader doesn't have one |
---|
160 | void SectionController::chooseTarget() |
---|
161 | { |
---|
162 | //----If division leader fights, cover him by fighting emenies close to his target---- |
---|
163 | if (this->myDivisionLeader_->getAction() == Action::FIGHT) |
---|
164 | { |
---|
165 | //----if he has a target---- |
---|
166 | if (this->myDivisionLeader_->hasTarget()) |
---|
167 | { |
---|
168 | //----try to find a new target if division leader has wingman (doing fine) and no good target already set---- |
---|
169 | if ( this->myDivisionLeader_->hasWingman() && |
---|
170 | !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) ) |
---|
171 | { |
---|
172 | |
---|
173 | bool foundTarget = false; |
---|
174 | //----new target should be close to division's target---- |
---|
175 | Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition(); |
---|
176 | |
---|
177 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
178 | { |
---|
179 | //----is enemy?---- |
---|
180 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) ) |
---|
181 | continue; |
---|
182 | //----in range?---- |
---|
183 | if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 && |
---|
184 | (*itP) != this->myDivisionLeader_->getTarget()) |
---|
185 | { |
---|
186 | foundTarget = true; |
---|
187 | this->setAction(Action::FIGHT, (*itP)); |
---|
188 | //orxout(internal_error) << "Found target" << endl; |
---|
189 | break; |
---|
190 | } |
---|
191 | } |
---|
192 | //----no target? then attack same target as division leader---- |
---|
193 | if (!foundTarget) |
---|
194 | { |
---|
195 | this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); |
---|
196 | } |
---|
197 | } |
---|
198 | //----if division leader doesn't have a wingman, support his fire---- |
---|
199 | else |
---|
200 | { |
---|
201 | this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget()); |
---|
202 | } |
---|
203 | } |
---|
204 | //----If he fights but doesn't have a target, wait for him to get one---- |
---|
205 | else |
---|
206 | { |
---|
207 | |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | //----stay in formation---- |
---|
213 | void SectionController::setTargetPositionOfWingman() |
---|
214 | { |
---|
215 | if (!this->myWingman_) |
---|
216 | return; |
---|
217 | Vector3* targetRelativePositionOfWingman; |
---|
218 | switch (this->formationMode_){ |
---|
219 | case FormationMode::WALL: |
---|
220 | { |
---|
221 | targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); |
---|
222 | break; |
---|
223 | } |
---|
224 | case FormationMode::FINGER4: |
---|
225 | { |
---|
226 | targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); |
---|
227 | break; |
---|
228 | } |
---|
229 | case FormationMode::DIAMOND: |
---|
230 | { |
---|
231 | targetRelativePositionOfWingman = new Vector3 (400, -200, 0); |
---|
232 | break; |
---|
233 | } |
---|
234 | } |
---|
235 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
---|
236 | |
---|
237 | Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + |
---|
238 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); |
---|
239 | |
---|
240 | myWingman_->setAction (Action::FLY, targetAbsolutePositionOfWingman, orient); |
---|
241 | |
---|
242 | } |
---|
243 | |
---|
244 | LeaderController* SectionController::findNewDivisionLeader() |
---|
245 | { |
---|
246 | |
---|
247 | if (!this->getControllableEntity()) |
---|
248 | return 0; |
---|
249 | |
---|
250 | LeaderController* closestLeader = 0; |
---|
251 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
252 | //go through all pawns |
---|
253 | for (ObjectList<LeaderController>::iterator it = ObjectList<LeaderController>::begin(); it; ++it) |
---|
254 | { |
---|
255 | //0ptr or not DivisionController? |
---|
256 | if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity())) |
---|
257 | continue; |
---|
258 | //same team? |
---|
259 | if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) |
---|
260 | continue; |
---|
261 | |
---|
262 | //is equal to this? |
---|
263 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
---|
264 | continue; |
---|
265 | |
---|
266 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
267 | |
---|
268 | if (distance < minDistance && !(it->hasFollower())) |
---|
269 | { |
---|
270 | closestLeader = *it; |
---|
271 | minDistance = distance; |
---|
272 | } |
---|
273 | |
---|
274 | } |
---|
275 | if (closestLeader) |
---|
276 | { |
---|
277 | if (closestLeader->setFollower(this)) |
---|
278 | return closestLeader; |
---|
279 | } |
---|
280 | return 0; |
---|
281 | |
---|
282 | } |
---|
283 | bool SectionController::setWingman(CommonController* cwingman) |
---|
284 | { |
---|
285 | WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman); |
---|
286 | |
---|
287 | if (!this->myWingman_) |
---|
288 | { |
---|
289 | this->myWingman_ = wingman; |
---|
290 | return true; |
---|
291 | } |
---|
292 | else |
---|
293 | { |
---|
294 | return false; |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | bool SectionController::hasWingman() |
---|
299 | { |
---|
300 | if (this->myWingman_) |
---|
301 | return true; |
---|
302 | else |
---|
303 | return false; |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | } |
---|