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