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->myLeader_ = 0; |
---|
42 | this->bFirstAction_ = true; |
---|
43 | |
---|
44 | } |
---|
45 | |
---|
46 | WingmanController::~WingmanController() |
---|
47 | { |
---|
48 | for (size_t i = 0; i < this->actionpoints_.size(); ++i) |
---|
49 | { |
---|
50 | if(this->actionpoints_[i]) |
---|
51 | this->actionpoints_[i]->destroy(); |
---|
52 | } |
---|
53 | this->parsedActionpoints_.clear(); |
---|
54 | this->actionpoints_.clear(); |
---|
55 | } |
---|
56 | |
---|
57 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
58 | { |
---|
59 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
60 | } |
---|
61 | |
---|
62 | //----in tick, move (or look) and shoot---- |
---|
63 | void WingmanController::tick(float dt) |
---|
64 | { |
---|
65 | if (!this->isActive()) |
---|
66 | return; |
---|
67 | |
---|
68 | SUPER(WingmanController, tick, dt); |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | //----action for hard calculations---- |
---|
73 | void WingmanController::action() |
---|
74 | { |
---|
75 | if (!this || !this->getControllableEntity() || !this->isActive()) |
---|
76 | return; |
---|
77 | //----If no leader, find one---- |
---|
78 | if (!this->myLeader_) |
---|
79 | { |
---|
80 | ActionpointController* newLeader = (findNewLeader()); |
---|
81 | if (!this || !this->getControllableEntity()) |
---|
82 | return; |
---|
83 | |
---|
84 | this->myLeader_ = newLeader; |
---|
85 | if (this->myLeader_) |
---|
86 | { |
---|
87 | |
---|
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 |
---|
102 | || this->myLeader_->getAction() == Action::FIGHTALL |
---|
103 | || this->myLeader_->getAction() == Action::ATTACK)) |
---|
104 | { |
---|
105 | this->keepFormation(); |
---|
106 | } |
---|
107 | else if (!this->myLeader_->bKeepFormation_) |
---|
108 | { |
---|
109 | if (!this || !this->getControllableEntity()) |
---|
110 | return; |
---|
111 | |
---|
112 | if (!this->hasTarget()) |
---|
113 | { |
---|
114 | this->setTarget(this->myLeader_->getTarget()); |
---|
115 | } |
---|
116 | |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | Vector3 WingmanController::getFormationPosition () |
---|
123 | { |
---|
124 | |
---|
125 | |
---|
126 | this->setFormationMode( this->myLeader_->getFormationMode() ); |
---|
127 | Vector3* targetRelativePosition; |
---|
128 | this->spread_ = this->myLeader_->getSpread(); |
---|
129 | if (this->myLeader_->getIdentifier()->getName() == "DivisionController") |
---|
130 | { |
---|
131 | switch (this->formationMode_){ |
---|
132 | case FormationMode::WALL: |
---|
133 | { |
---|
134 | targetRelativePosition = new Vector3 (2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); |
---|
135 | break; |
---|
136 | } |
---|
137 | case FormationMode::FINGER4: |
---|
138 | { |
---|
139 | targetRelativePosition = new Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
140 | break; |
---|
141 | } |
---|
142 | case FormationMode::DIAMOND: |
---|
143 | { |
---|
144 | targetRelativePosition = new Vector3 (2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
145 | break; |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | else |
---|
150 | { |
---|
151 | switch (this->formationMode_){ |
---|
152 | case FormationMode::WALL: |
---|
153 | { |
---|
154 | targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, 0 - 1.0f*this->tolerance_); |
---|
155 | break; |
---|
156 | } |
---|
157 | case FormationMode::FINGER4: |
---|
158 | { |
---|
159 | targetRelativePosition = new Vector3 (-2.0f*this->spread_, 0, this->spread_ - 1.0f*this->tolerance_); |
---|
160 | break; |
---|
161 | } |
---|
162 | case FormationMode::DIAMOND: |
---|
163 | { |
---|
164 | targetRelativePosition = new Vector3 (2.0f*this->spread_, -1.0f*this->spread_, 0 - 1.0f*this->tolerance_); |
---|
165 | break; |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | Vector3 result = *targetRelativePosition; |
---|
170 | delete targetRelativePosition; |
---|
171 | return result; |
---|
172 | } |
---|
173 | void WingmanController::keepFormation() |
---|
174 | { |
---|
175 | this->bKeepFormation_ = true; |
---|
176 | ControllableEntity* leaderEntity = this->myLeader_->getControllableEntity(); |
---|
177 | Vector3 targetRelativePosition = this->getFormationPosition(); |
---|
178 | if (!leaderEntity) |
---|
179 | return; |
---|
180 | FlyingController::keepFormation (leaderEntity, targetRelativePosition); |
---|
181 | } |
---|
182 | //----POST: closest leader that is ready to take a new wingman is returned---- |
---|
183 | ActionpointController* WingmanController::findNewLeader() |
---|
184 | { |
---|
185 | |
---|
186 | if (!this->getControllableEntity()) |
---|
187 | return 0; |
---|
188 | |
---|
189 | //----vars for finding the closest leader---- |
---|
190 | ActionpointController* closestLeader = 0; |
---|
191 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
192 | Gametype* gt = this->getGametype(); |
---|
193 | |
---|
194 | for (ObjectList<ActionpointController>::iterator it = ObjectList<ActionpointController>::begin(); it; ++it) |
---|
195 | { |
---|
196 | //----0ptr or not a leader or dead?---- |
---|
197 | if (!it || |
---|
198 | (it->getIdentifier()->getName() != "SectionController" && it->getIdentifier()->getName() != "DivisionController") || |
---|
199 | !(it->getControllableEntity())) |
---|
200 | continue; |
---|
201 | |
---|
202 | //----same team?---- |
---|
203 | if ( !CommonController::sameTeam (this->getControllableEntity(), (it)->getControllableEntity(), gt) ) |
---|
204 | continue; |
---|
205 | |
---|
206 | //----check distance---- |
---|
207 | float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity()); |
---|
208 | if (distance < minDistance && !(it->hasWingman())) |
---|
209 | { |
---|
210 | closestLeader = *it; |
---|
211 | minDistance = distance; |
---|
212 | } |
---|
213 | } |
---|
214 | if (closestLeader) |
---|
215 | { |
---|
216 | //----Racing conditions---- |
---|
217 | /*TODO: racing condition check is wrong and redundant, as there is no multithreading here, ticks get called one after another, |
---|
218 | so it can be simplified to a check of whether leader got a wingman*/ |
---|
219 | if (closestLeader->setWingman(orxonox_cast<ActionpointController*>(this))) |
---|
220 | { |
---|
221 | return closestLeader; |
---|
222 | } |
---|
223 | } |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | } |
---|