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 "AIController.h" |
---|
30 | |
---|
31 | #include "util/Math.h" |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | #include "core/command/Executor.h" |
---|
34 | #include "worldentities/ControllableEntity.h" |
---|
35 | #include "worldentities/pawns/Pawn.h" |
---|
36 | |
---|
37 | namespace orxonox |
---|
38 | { |
---|
39 | const float AIController::ACTION_INTERVAL = 1.0f; |
---|
40 | |
---|
41 | CreateFactory(AIController); |
---|
42 | |
---|
43 | AIController::AIController(BaseObject* creator) : ArtificialController(creator) |
---|
44 | { |
---|
45 | RegisterObject(AIController); |
---|
46 | |
---|
47 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this))); |
---|
48 | } |
---|
49 | |
---|
50 | AIController::~AIController() |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | void AIController::action() |
---|
55 | { |
---|
56 | float random; |
---|
57 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
58 | |
---|
59 | if (this->state_ == FREE) |
---|
60 | { |
---|
61 | |
---|
62 | if (this->formationFlight_) |
---|
63 | { |
---|
64 | // return to Master after being forced free |
---|
65 | if (this->freedomCount_ == 1) |
---|
66 | { |
---|
67 | this->state_ = SLAVE; |
---|
68 | this->freedomCount_ = 0; |
---|
69 | } |
---|
70 | |
---|
71 | random = rnd(maxrand); |
---|
72 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
---|
73 | this->searchNewMaster(); |
---|
74 | } |
---|
75 | |
---|
76 | // search enemy |
---|
77 | random = rnd(maxrand); |
---|
78 | if (random < 15 && (!this->target_)) |
---|
79 | this->searchNewTarget(); |
---|
80 | |
---|
81 | // forget enemy |
---|
82 | random = rnd(maxrand); |
---|
83 | if (random < 5 && (this->target_)) |
---|
84 | this->forgetTarget(); |
---|
85 | |
---|
86 | // next enemy |
---|
87 | random = rnd(maxrand); |
---|
88 | if (random < 10 && (this->target_)) |
---|
89 | this->searchNewTarget(); |
---|
90 | |
---|
91 | // fly somewhere |
---|
92 | random = rnd(maxrand); |
---|
93 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
94 | this->searchRandomTargetPosition(); |
---|
95 | |
---|
96 | // stop flying |
---|
97 | random = rnd(maxrand); |
---|
98 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
99 | this->bHasTargetPosition_ = false; |
---|
100 | |
---|
101 | // fly somewhere else |
---|
102 | random = rnd(maxrand); |
---|
103 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
104 | this->searchRandomTargetPosition(); |
---|
105 | |
---|
106 | // shoot |
---|
107 | random = rnd(maxrand); |
---|
108 | if (!(this->passive_) && random < 75 && (this->target_ && !this->bShooting_)) |
---|
109 | this->bShooting_ = true; |
---|
110 | |
---|
111 | // stop shooting |
---|
112 | random = rnd(maxrand); |
---|
113 | if (random < 25 && (this->bShooting_)) |
---|
114 | this->bShooting_ = false; |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | if (this->state_ == SLAVE && this->mode_==ATTACK) |
---|
119 | { |
---|
120 | if (!this->target_) |
---|
121 | { |
---|
122 | this->searchNewTarget(); |
---|
123 | } |
---|
124 | |
---|
125 | // next enemy |
---|
126 | random = rnd(maxrand); |
---|
127 | if (random < 10 && (this->target_)) |
---|
128 | this->searchNewTarget(); |
---|
129 | |
---|
130 | // shoot |
---|
131 | random = rnd(maxrand); |
---|
132 | if (!(this->passive_) && random < 75 && (this->target_ && !this->bShooting_)) |
---|
133 | this->bShooting_ = true; |
---|
134 | |
---|
135 | // stop shooting |
---|
136 | random = rnd(maxrand); |
---|
137 | if (random < 25 && (this->bShooting_)) |
---|
138 | this->bShooting_ = false; |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | if (this->state_ == MASTER) |
---|
143 | { |
---|
144 | |
---|
145 | |
---|
146 | this->commandSlaves(); |
---|
147 | |
---|
148 | if (this->specificMasterAction_ != NONE) |
---|
149 | this->specificMasterActionHold(); |
---|
150 | |
---|
151 | else { |
---|
152 | |
---|
153 | // make 180 degree turn - a specific Master Action |
---|
154 | random = rnd(1000.0f); |
---|
155 | if (random < 5) |
---|
156 | this->turn180Init(); |
---|
157 | |
---|
158 | // spin around - a specific Master Action |
---|
159 | random = rnd(1000.0f); |
---|
160 | if (random < 5) |
---|
161 | this->spinInit(); |
---|
162 | |
---|
163 | /*// follow a randomly chosen human - a specific Master Action |
---|
164 | random = rnd(1000.0f); |
---|
165 | if (random < 1) |
---|
166 | this->followRandomHumanInit(); |
---|
167 | */ |
---|
168 | // lose master status (only if less than 4 slaves in formation) |
---|
169 | random = rnd(maxrand); |
---|
170 | if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) |
---|
171 | this->loseMasterState(); |
---|
172 | |
---|
173 | // look out for outher masters if formation is small |
---|
174 | random = rnd(maxrand); |
---|
175 | if(this->slaves_.size() < 3 && random < 20) |
---|
176 | this->searchNewMaster(); |
---|
177 | |
---|
178 | // search enemy |
---|
179 | random = rnd(maxrand); |
---|
180 | if (random < 15 && (!this->target_)) |
---|
181 | this->searchNewTarget(); |
---|
182 | |
---|
183 | // forget enemy |
---|
184 | random = rnd(maxrand); |
---|
185 | if (random < 5 && (this->target_)) |
---|
186 | this->forgetTarget(); |
---|
187 | |
---|
188 | // next enemy |
---|
189 | random = rnd(maxrand); |
---|
190 | if (random < 10 && (this->target_)) |
---|
191 | this->searchNewTarget(); |
---|
192 | |
---|
193 | // fly somewhere |
---|
194 | random = rnd(maxrand); |
---|
195 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
196 | this->searchRandomTargetPosition(); |
---|
197 | |
---|
198 | |
---|
199 | // fly somewhere else |
---|
200 | random = rnd(maxrand); |
---|
201 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
202 | this->searchRandomTargetPosition(); |
---|
203 | |
---|
204 | // shoot |
---|
205 | random = rnd(maxrand); |
---|
206 | if (!(this->passive_) && random < 9 && (this->target_ && !this->bShooting_)) |
---|
207 | { |
---|
208 | this->bShooting_ = true; |
---|
209 | this->forceFreeSlaves(); |
---|
210 | } |
---|
211 | |
---|
212 | // stop shooting |
---|
213 | random = rnd(maxrand); |
---|
214 | if (random < 25 && (this->bShooting_)) |
---|
215 | this->bShooting_ = false; |
---|
216 | |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | } |
---|
221 | |
---|
222 | void AIController::tick(float dt) |
---|
223 | { |
---|
224 | if (!this->isActive()) |
---|
225 | return; |
---|
226 | |
---|
227 | if (this->state_ == MASTER) |
---|
228 | { |
---|
229 | if (this->specificMasterAction_ == NONE) |
---|
230 | { |
---|
231 | if (this->target_) |
---|
232 | { |
---|
233 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
234 | this->forgetTarget(); |
---|
235 | else this->aimAtTarget(); |
---|
236 | } |
---|
237 | |
---|
238 | if (this->bHasTargetPosition_) |
---|
239 | this->moveToTargetPosition(); |
---|
240 | |
---|
241 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(math::pi / 20.0f)) |
---|
242 | this->getControllableEntity()->fire(0); |
---|
243 | } |
---|
244 | |
---|
245 | if (this->specificMasterAction_ == TURN180) |
---|
246 | this->turn180(); |
---|
247 | |
---|
248 | if (this->specificMasterAction_ == SPIN) |
---|
249 | this->spin(); |
---|
250 | if (this->specificMasterAction_ == FOLLOW) |
---|
251 | this->follow(); |
---|
252 | } |
---|
253 | |
---|
254 | if (this->state_ == SLAVE && this->mode_!=ATTACK) |
---|
255 | { |
---|
256 | |
---|
257 | if (this->bHasTargetPosition_) |
---|
258 | this->moveToTargetPosition(); |
---|
259 | |
---|
260 | } |
---|
261 | |
---|
262 | if (this->state_ == FREE || (this->state_==SLAVE && this->mode_==ATTACK) ) |
---|
263 | { |
---|
264 | if (this->target_) |
---|
265 | { |
---|
266 | if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */ |
---|
267 | this->forgetTarget(); |
---|
268 | else this->aimAtTarget(); |
---|
269 | } |
---|
270 | |
---|
271 | if (this->bHasTargetPosition_) |
---|
272 | this->moveToTargetPosition(); |
---|
273 | |
---|
274 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(math::pi / 20.0f)) |
---|
275 | this->getControllableEntity()->fire(0); |
---|
276 | } |
---|
277 | |
---|
278 | SUPER(AIController, tick, dt); |
---|
279 | } |
---|
280 | |
---|
281 | } |
---|