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 "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 | |
---|
63 | //----in tick, move (or look) and shoot---- |
---|
64 | void WingmanController::tick(float dt) |
---|
65 | { |
---|
66 | if (!this->isActive()) |
---|
67 | return; |
---|
68 | |
---|
69 | SUPER(WingmanController, tick, dt); |
---|
70 | } |
---|
71 | |
---|
72 | //----action for hard calculations---- |
---|
73 | void WingmanController::action() |
---|
74 | { |
---|
75 | |
---|
76 | //----If no leader, find one---- |
---|
77 | if (!this->myLeader_) |
---|
78 | { |
---|
79 | ActionpointController* newLeader = (findNewLeader()); |
---|
80 | this->myLeader_ = newLeader; |
---|
81 | if (this->myLeader_) |
---|
82 | { |
---|
83 | //spread copyOrientation called equally among the division |
---|
84 | if (this->myLeader_->getIdentifier()->getName() == "SectionController") |
---|
85 | this->actionCounter_ = 1; |
---|
86 | else |
---|
87 | this->actionCounter_ = 4; |
---|
88 | } |
---|
89 | } |
---|
90 | //----If have leader, he will deal with logic---- |
---|
91 | else |
---|
92 | { |
---|
93 | |
---|
94 | } |
---|
95 | if (!this->myLeader_) |
---|
96 | { |
---|
97 | ActionpointController::action(); |
---|
98 | } |
---|
99 | else if (this->myLeader_) |
---|
100 | { |
---|
101 | if (this->myLeader_->bKeepFormation_ || !(this->myLeader_->getAction() == Action::FIGHT || this->myLeader_->getAction() == Action::FIGHTALL |
---|
102 | || this->myLeader_->getAction() == Action::ATTACK)) |
---|
103 | { |
---|
104 | this->keepFormation(); |
---|
105 | } |
---|
106 | else if (!this->myLeader_->bKeepFormation_) |
---|
107 | { |
---|
108 | if (!this->hasTarget()) |
---|
109 | { |
---|
110 | this->setTarget(this->myLeader_->getTarget()); |
---|
111 | } |
---|
112 | if (this->hasTarget()) |
---|
113 | { |
---|
114 | this->maneuver(); |
---|
115 | this->bShooting_ = this->canFire(); |
---|
116 | // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length()); |
---|
117 | // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_) |
---|
118 | // { |
---|
119 | // //----choose where to go---- |
---|
120 | // this->maneuver(); |
---|
121 | // } |
---|
122 | // else |
---|
123 | // { |
---|
124 | // this->dodgeTowards(healthPosition); |
---|
125 | // } |
---|
126 | // //----fire if you can---- |
---|
127 | // this->bShooting_ = this->canFire(); |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | Vector3 WingmanController::getFormationPosition () |
---|
136 | { |
---|
137 | this->setFormationMode( this->myLeader_->getFormationMode() ); |
---|
138 | Vector3* targetRelativePosition; |
---|
139 | this->spread_ = this->myLeader_->getSpread(); |
---|
140 | if (this->myLeader_->getIdentifier()->getName() == "DivisionController") |
---|
141 | { |
---|
142 | switch (this->formationMode_){ |
---|
143 | case FormationMode::WALL: |
---|
144 | { |
---|
145 | targetRelativePosition = new Vector3 (2*this->spread_, 0, 0 - this->tolerance_); |
---|
146 | break; |
---|
147 | } |
---|
148 | case FormationMode::FINGER4: |
---|
149 | { |
---|
150 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
151 | break; |
---|
152 | } |
---|
153 | case FormationMode::DIAMOND: |
---|
154 | { |
---|
155 | targetRelativePosition = new Vector3 (2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
156 | break; |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | else |
---|
161 | { |
---|
162 | |
---|
163 | switch (this->formationMode_){ |
---|
164 | case FormationMode::WALL: |
---|
165 | { |
---|
166 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, 0 - this->tolerance_); |
---|
167 | break; |
---|
168 | } |
---|
169 | case FormationMode::FINGER4: |
---|
170 | { |
---|
171 | targetRelativePosition = new Vector3 (-2*this->spread_, 0, this->spread_ - this->tolerance_); |
---|
172 | break; |
---|
173 | } |
---|
174 | case FormationMode::DIAMOND: |
---|
175 | { |
---|
176 | targetRelativePosition = new Vector3 (2*this->spread_, -this->spread_, 0 - this->tolerance_); |
---|
177 | break; |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|
181 | Vector3 result = *targetRelativePosition; |
---|
182 | delete targetRelativePosition; |
---|
183 | return result; |
---|
184 | } |
---|
185 | void WingmanController::keepFormation() |
---|
186 | { |
---|
187 | this->bKeepFormation_ = true; |
---|
188 | ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); |
---|
189 | Vector3 targetRelativePosition = this->getFormationPosition(); |
---|
190 | if (!leaderEntity) |
---|
191 | return; |
---|
192 | FlyingController::keepFormation (leaderEntity, targetRelativePosition); |
---|
193 | } |
---|
194 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
195 | ActionpointController* WingmanController::findNewLeader() |
---|
196 | { |
---|
197 | |
---|
198 | if (!this->getControllableEntity()) |
---|
199 | return 0; |
---|
200 | |
---|
201 | //----vars for finding the closest leader---- |
---|
202 | ActionpointController* closestLeader = 0; |
---|
203 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
204 | Gametype* gt = this->getGametype(); |
---|
205 | |
---|
206 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it) |
---|
207 | { |
---|
208 | //----0ptr or not a leader or dead?---- |
---|
209 | if (!it || |
---|
210 | (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || |
---|
211 | !(it->getControllableEntity())) |
---|
212 | continue; |
---|
213 | |
---|
214 | //----same team?---- |
---|
215 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
---|
216 | continue; |
---|
217 | |
---|
218 | //----check distance---- |
---|
219 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
220 | if (distance < minDistance && !(it->hasWingman())) |
---|
221 | { |
---|
222 | closestLeader = *it; |
---|
223 | minDistance = distance; |
---|
224 | } |
---|
225 | } |
---|
226 | if (closestLeader) |
---|
227 | { |
---|
228 | //----Racing conditions---- |
---|
229 | if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) |
---|
230 | { |
---|
231 | return closestLeader; |
---|
232 | } |
---|
233 | } |
---|
234 | return 0; |
---|
235 | } |
---|
236 | |
---|
237 | } |
---|