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