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 | |
---|
31 | namespace orxonox |
---|
32 | { |
---|
33 | |
---|
34 | RegisterClass(SectionController); |
---|
35 | |
---|
36 | //Leaders share the fact that they have Wingmans |
---|
37 | SectionController::SectionController(Context* context) : ActionpointController(context) |
---|
38 | { |
---|
39 | RegisterObject(SectionController); |
---|
40 | this->setFormationMode(FormationMode::FINGER4); |
---|
41 | |
---|
42 | this->myWingman_ = nullptr; |
---|
43 | this->myDivisionLeader_ = nullptr; |
---|
44 | this->bFirstAction_ = true; |
---|
45 | |
---|
46 | } |
---|
47 | |
---|
48 | SectionController::~SectionController() |
---|
49 | { |
---|
50 | for (WorldEntity* actionpoint : this->actionpoints_) |
---|
51 | { |
---|
52 | if (actionpoint) |
---|
53 | actionpoint->destroy(); |
---|
54 | } |
---|
55 | this->parsedActionpoints_.clear(); |
---|
56 | this->actionpoints_.clear(); |
---|
57 | } |
---|
58 | |
---|
59 | void SectionController::action() |
---|
60 | { |
---|
61 | if (!this || !this->getControllableEntity() || !this->isActive()) |
---|
62 | return; |
---|
63 | |
---|
64 | //----If no leader, find one---- |
---|
65 | if (!myDivisionLeader_) |
---|
66 | { |
---|
67 | ActionpointController* newDivisionLeader = findNewDivisionLeader(); |
---|
68 | if (!this || !this->getControllableEntity()) |
---|
69 | return; |
---|
70 | |
---|
71 | this->myDivisionLeader_ = newDivisionLeader; |
---|
72 | } |
---|
73 | //----If have leader---- |
---|
74 | else |
---|
75 | { |
---|
76 | } |
---|
77 | if (!myDivisionLeader_) |
---|
78 | { |
---|
79 | ActionpointController::action(); |
---|
80 | if (!this || !this->getControllableEntity()) |
---|
81 | return; |
---|
82 | if (!(this->parsedActionpoints_.empty() && this->loopActionpoints_.empty())) |
---|
83 | { |
---|
84 | if (this->myWingman_) |
---|
85 | { |
---|
86 | this->myWingman_->takeActionpoints(this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_); |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | else if (myDivisionLeader_) |
---|
91 | { |
---|
92 | if (this->myDivisionLeader_->bKeepFormation_ || !(this->myDivisionLeader_->getAction() == Action::FIGHT |
---|
93 | || this->myDivisionLeader_->getAction() == Action::FIGHTALL |
---|
94 | || this->myDivisionLeader_->getAction() == Action::ATTACK)) |
---|
95 | { |
---|
96 | this->keepFormation(); |
---|
97 | } |
---|
98 | else if (!this->myDivisionLeader_->bKeepFormation_) |
---|
99 | { |
---|
100 | if (!this || !this->getControllableEntity()) |
---|
101 | return; |
---|
102 | |
---|
103 | if (!this->hasTarget()) |
---|
104 | { |
---|
105 | this->chooseTarget(); |
---|
106 | } |
---|
107 | |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT |
---|
115 | //POST: this->target_ is set unless division leader doesn't have one |
---|
116 | void SectionController::chooseTarget() |
---|
117 | { |
---|
118 | //----If division leader fights, cover him by fighting emenies close to his target---- |
---|
119 | Action action = this->myDivisionLeader_->getAction(); |
---|
120 | |
---|
121 | if (action == Action::FIGHT || action == Action::FIGHTALL || action == Action::ATTACK) |
---|
122 | { |
---|
123 | Pawn* target; |
---|
124 | //----if he has a target---- |
---|
125 | if (this->myDivisionLeader_->hasTarget()) |
---|
126 | { |
---|
127 | //----try to find a new target if division leader has wingman (doing fine) and no good target already set---- |
---|
128 | if ( this->myDivisionLeader_->hasWingman() && |
---|
129 | !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) ) |
---|
130 | { |
---|
131 | bool foundTarget = false; |
---|
132 | //----new target should be close to division's target---- |
---|
133 | Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition(); |
---|
134 | Gametype* gt = this->getGametype(); |
---|
135 | for (Pawn* pawn : ObjectList<Pawn>()) |
---|
136 | { |
---|
137 | //----is enemy?---- |
---|
138 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(pawn), gt) ) |
---|
139 | continue; |
---|
140 | //----in range?---- |
---|
141 | if ((pawn->getWorldPosition() - divisionTargetPosition).length() < 3000 && |
---|
142 | pawn != this->myDivisionLeader_->getTarget()) |
---|
143 | { |
---|
144 | foundTarget = true; |
---|
145 | target = pawn; |
---|
146 | break; |
---|
147 | } |
---|
148 | } |
---|
149 | //----no target? then attack same target as division leader---- |
---|
150 | if (!foundTarget) |
---|
151 | { |
---|
152 | target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget()); |
---|
153 | } |
---|
154 | } |
---|
155 | //----if division leader doesn't have a wingman, support his fire---- |
---|
156 | else |
---|
157 | { |
---|
158 | target = orxonox_cast<Pawn*>(this->myDivisionLeader_->getTarget()); |
---|
159 | } |
---|
160 | } |
---|
161 | //----If he fights but doesn't have a target, wait for him to get one---- |
---|
162 | else |
---|
163 | { |
---|
164 | |
---|
165 | } |
---|
166 | this->setTarget (orxonox_cast<ControllableEntity*>(target)); |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | } |
---|
171 | } |
---|
172 | Vector3 SectionController::getFormationPosition () |
---|
173 | { |
---|
174 | this->setFormationMode( this->myDivisionLeader_->getFormationMode() ); |
---|
175 | this->spread_ = this->myDivisionLeader_->getSpread(); |
---|
176 | switch (this->formationMode_){ |
---|
177 | case FormationMode::WALL: |
---|
178 | return Vector3 (-2.0f*this->spread_, 0, 0); |
---|
179 | |
---|
180 | case FormationMode::FINGER4: |
---|
181 | return Vector3 (-2.0f*this->spread_, 0, 1.0f*this->spread_); |
---|
182 | |
---|
183 | case FormationMode::DIAMOND: |
---|
184 | return Vector3 (-2.0f*this->spread_, 0, 1.0f*this->spread_); |
---|
185 | |
---|
186 | default: |
---|
187 | return Vector3::ZERO; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | void SectionController::keepFormation() |
---|
192 | { |
---|
193 | this->bKeepFormation_ = true; |
---|
194 | ControllableEntity* leaderEntity = this->myDivisionLeader_->getControllableEntity(); |
---|
195 | Vector3 targetRelativePosition = this->getFormationPosition(); |
---|
196 | if (!leaderEntity) |
---|
197 | return; |
---|
198 | FlyingController::keepFormation(leaderEntity, targetRelativePosition); |
---|
199 | } |
---|
200 | |
---|
201 | ActionpointController* SectionController::findNewDivisionLeader() |
---|
202 | { |
---|
203 | |
---|
204 | if (!this->getControllableEntity()) |
---|
205 | return nullptr; |
---|
206 | |
---|
207 | ActionpointController* closestLeader = nullptr; |
---|
208 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
209 | //go through all pawns |
---|
210 | for (ActionpointController* controller : ObjectList<ActionpointController>()) |
---|
211 | { |
---|
212 | //0ptr or not DivisionController? |
---|
213 | if (!controller || !(controller->getIdentifier()->getName() == "DivisionController") || !(controller->getControllableEntity())) |
---|
214 | continue; |
---|
215 | //same team? |
---|
216 | if ((this->getControllableEntity()->getTeam() != controller->getControllableEntity()->getTeam())) |
---|
217 | continue; |
---|
218 | |
---|
219 | //is equal to this? |
---|
220 | if (orxonox_cast<ControllableEntity*>(controller) == this->getControllableEntity()) |
---|
221 | continue; |
---|
222 | |
---|
223 | float distance = CommonController::distance (controller->getControllableEntity(), this->getControllableEntity()); |
---|
224 | |
---|
225 | if (distance < minDistance && !(controller->hasFollower())) |
---|
226 | { |
---|
227 | closestLeader = controller; |
---|
228 | minDistance = distance; |
---|
229 | } |
---|
230 | |
---|
231 | } |
---|
232 | if (closestLeader) |
---|
233 | { |
---|
234 | if (closestLeader->setFollower(this)) |
---|
235 | return closestLeader; |
---|
236 | } |
---|
237 | return nullptr; |
---|
238 | } |
---|
239 | |
---|
240 | bool SectionController::setWingman(ActionpointController* newWingman) |
---|
241 | { |
---|
242 | |
---|
243 | if (!this->myWingman_) |
---|
244 | { |
---|
245 | this->myWingman_ = newWingman; |
---|
246 | newWingman->takeActionpoints (this->parsedActionpoints_, this->loopActionpoints_, this->bLoop_); |
---|
247 | return true; |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | return false; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | bool SectionController::hasWingman() |
---|
256 | { |
---|
257 | if (this->myWingman_) |
---|
258 | return true; |
---|
259 | else |
---|
260 | return false; |
---|
261 | } |
---|
262 | } |
---|