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 "WingmanController.h" |
---|
30 | |
---|
31 | |
---|
32 | namespace orxonox |
---|
33 | { |
---|
34 | |
---|
35 | RegisterClass(WingmanController); |
---|
36 | |
---|
37 | //ActionpointController contains all common functionality of AI Controllers |
---|
38 | WingmanController::WingmanController(Context* context) : ActionpointController(context) |
---|
39 | { |
---|
40 | RegisterObject(WingmanController); |
---|
41 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); |
---|
42 | this->myLeader_ = 0; |
---|
43 | this->bFirstAction_ = true; |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | WingmanController::~WingmanController() |
---|
48 | { |
---|
49 | for (size_t i = 0; i < this->actionpoints_.size(); ++i) |
---|
50 | { |
---|
51 | if(this->actionpoints_[i]) |
---|
52 | this->actionpoints_[i]->destroy(); |
---|
53 | } |
---|
54 | this->parsedActionpoints_.clear(); |
---|
55 | this->actionpoints_.clear(); |
---|
56 | } |
---|
57 | |
---|
58 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
59 | { |
---|
60 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
61 | |
---|
62 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
63 | } |
---|
64 | |
---|
65 | //----in tick, move (or look) and shoot---- |
---|
66 | void WingmanController::tick(float dt) |
---|
67 | { |
---|
68 | if (!this->isActive()) |
---|
69 | return; |
---|
70 | if (this->myLeader_ && this->myLeader_->getAction() != Action::FIGHT && this->myLeader_->getAction() != |
---|
71 | Action::FIGHTALL && this->myLeader_->getAction() != Action::ATTACK) |
---|
72 | { |
---|
73 | } |
---|
74 | |
---|
75 | SUPER(WingmanController, tick, dt); |
---|
76 | } |
---|
77 | |
---|
78 | //----action for hard calculations---- |
---|
79 | void WingmanController::action() |
---|
80 | { |
---|
81 | |
---|
82 | //----If no leader, find one---- |
---|
83 | if (!this->myLeader_) |
---|
84 | { |
---|
85 | ActionpointController* newLeader = (findNewLeader()); |
---|
86 | this->myLeader_ = newLeader; |
---|
87 | if (this->myLeader_) |
---|
88 | { |
---|
89 | //spread copyOrientation called equally among the division |
---|
90 | if (this->myLeader_->getIdentifier()->getName() == "SectionController") |
---|
91 | this->actionCounter_ = 2; |
---|
92 | else |
---|
93 | this->actionCounter_ = 5; |
---|
94 | } |
---|
95 | } |
---|
96 | //----If have leader, he will deal with logic---- |
---|
97 | else |
---|
98 | { |
---|
99 | |
---|
100 | } |
---|
101 | if (!this->myLeader_) |
---|
102 | { |
---|
103 | ActionpointController::action(); |
---|
104 | } |
---|
105 | else if (this->myLeader_) |
---|
106 | { |
---|
107 | switch (this->myLeader_->getAction()) |
---|
108 | { |
---|
109 | case Action::FIGHT: |
---|
110 | { |
---|
111 | if (!this->hasTarget()) |
---|
112 | { |
---|
113 | this->setTarget(this->myLeader_->getTarget()); |
---|
114 | } |
---|
115 | break; |
---|
116 | } |
---|
117 | case Action::FIGHTALL: |
---|
118 | { |
---|
119 | if (!this->hasTarget()) |
---|
120 | { |
---|
121 | this->setTarget(this->myLeader_->getTarget()); |
---|
122 | } |
---|
123 | break; |
---|
124 | } |
---|
125 | case Action::ATTACK: |
---|
126 | { |
---|
127 | if (!this->hasTarget()) |
---|
128 | { |
---|
129 | this->setTarget(this->myLeader_->getTarget()); |
---|
130 | } |
---|
131 | break; |
---|
132 | } |
---|
133 | default: |
---|
134 | { |
---|
135 | |
---|
136 | ControllableEntity* myEntity = this->getControllableEntity(); |
---|
137 | Vector3 myPosition = myEntity->getWorldPosition(); |
---|
138 | if (!this->myLeader_) |
---|
139 | { |
---|
140 | return; |
---|
141 | } |
---|
142 | ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); |
---|
143 | Quaternion orient = leaderEntity->getWorldOrientation(); |
---|
144 | Vector3 leaderPosition = leaderEntity->getWorldPosition(); |
---|
145 | Vector3 targetRelativePosition = getFormationPosition(); |
---|
146 | if (!this->myLeader_) |
---|
147 | { |
---|
148 | return; |
---|
149 | } |
---|
150 | Vector3 targetAbsolutePosition = |
---|
151 | (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5) |
---|
152 | + (orient* (targetRelativePosition))); |
---|
153 | //let ship finish rotating. also don't call copyOrientation to often as it is a slow function. |
---|
154 | if (this->actionCounter_ % 9 == 0 && !this->bHasTargetOrientation_) |
---|
155 | this->setAction (Action::FLY, targetAbsolutePosition, orient); |
---|
156 | else |
---|
157 | this->setAction (Action::FLY, targetAbsolutePosition); |
---|
158 | if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f) |
---|
159 | { |
---|
160 | this->boostControl(); |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | this->getControllableEntity()->boost(false); |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | if (this->hasTarget()) |
---|
169 | { |
---|
170 | //----choose where to go---- |
---|
171 | this->maneuver(); |
---|
172 | //----fire if you can---- |
---|
173 | this->bShooting_ = this->canFire(); |
---|
174 | } |
---|
175 | } |
---|
176 | this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | Vector3 WingmanController::getFormationPosition () |
---|
181 | { |
---|
182 | this->setFormationMode( this->myLeader_->getFormationMode() ); |
---|
183 | Vector3* targetRelativePosition; |
---|
184 | |
---|
185 | if (this->myLeader_->getIdentifier()->getName() == "DivisionController") |
---|
186 | { |
---|
187 | switch (this->formationMode_){ |
---|
188 | case FormationMode::WALL: |
---|
189 | { |
---|
190 | targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); |
---|
191 | break; |
---|
192 | } |
---|
193 | case FormationMode::FINGER4: |
---|
194 | { |
---|
195 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
196 | break; |
---|
197 | } |
---|
198 | case FormationMode::DIAMOND: |
---|
199 | { |
---|
200 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
201 | break; |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | else |
---|
206 | { |
---|
207 | |
---|
208 | switch (this->formationMode_){ |
---|
209 | case FormationMode::WALL: |
---|
210 | { |
---|
211 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); |
---|
212 | break; |
---|
213 | } |
---|
214 | case FormationMode::FINGER4: |
---|
215 | { |
---|
216 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
217 | break; |
---|
218 | } |
---|
219 | case FormationMode::DIAMOND: |
---|
220 | { |
---|
221 | targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_); |
---|
222 | break; |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | Vector3 result = *targetRelativePosition; |
---|
227 | delete targetRelativePosition; |
---|
228 | return result; |
---|
229 | } |
---|
230 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
231 | ActionpointController* WingmanController::findNewLeader() |
---|
232 | { |
---|
233 | |
---|
234 | if (!this->getControllableEntity()) |
---|
235 | return 0; |
---|
236 | |
---|
237 | //----vars for finding the closest leader---- |
---|
238 | ActionpointController* closestLeader = 0; |
---|
239 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
240 | Gametype* gt = this->getGametype(); |
---|
241 | |
---|
242 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it) |
---|
243 | { |
---|
244 | //----0ptr or not a leader or dead?---- |
---|
245 | if (!it || |
---|
246 | (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || |
---|
247 | !(it->getControllableEntity())) |
---|
248 | continue; |
---|
249 | |
---|
250 | //----same team?---- |
---|
251 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
---|
252 | continue; |
---|
253 | |
---|
254 | //----check distance---- |
---|
255 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
256 | if (distance < minDistance && !(it->hasWingman())) |
---|
257 | { |
---|
258 | closestLeader = *it; |
---|
259 | minDistance = distance; |
---|
260 | } |
---|
261 | |
---|
262 | } |
---|
263 | if (closestLeader) |
---|
264 | { |
---|
265 | //----Racing conditions---- |
---|
266 | if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) |
---|
267 | { |
---|
268 | return closestLeader; |
---|
269 | } |
---|
270 | |
---|
271 | } |
---|
272 | |
---|
273 | return 0; |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | } |
---|