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