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 | * ... |
---|
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 | |
---|
62 | // return to Master after being forced free |
---|
63 | if (this->freedomCount_ == 1) |
---|
64 | { |
---|
65 | this->state_ = SLAVE; |
---|
66 | this->freedomCount_ = 0; |
---|
67 | } |
---|
68 | |
---|
69 | random = rnd(maxrand); |
---|
70 | if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree())) |
---|
71 | this->searchNewMaster(); |
---|
72 | |
---|
73 | |
---|
74 | // search enemy |
---|
75 | random = rnd(maxrand); |
---|
76 | if (random < 15 && (!this->target_)) |
---|
77 | this->searchNewTarget(); |
---|
78 | |
---|
79 | // forget enemy |
---|
80 | random = rnd(maxrand); |
---|
81 | if (random < 5 && (this->target_)) |
---|
82 | this->forgetTarget(); |
---|
83 | |
---|
84 | // next enemy |
---|
85 | random = rnd(maxrand); |
---|
86 | if (random < 10 && (this->target_)) |
---|
87 | this->searchNewTarget(); |
---|
88 | |
---|
89 | // fly somewhere |
---|
90 | random = rnd(maxrand); |
---|
91 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
92 | this->searchRandomTargetPosition(); |
---|
93 | |
---|
94 | // stop flying |
---|
95 | random = rnd(maxrand); |
---|
96 | if (random < 10 && (this->bHasTargetPosition_ && !this->target_)) |
---|
97 | this->bHasTargetPosition_ = false; |
---|
98 | |
---|
99 | // fly somewhere else |
---|
100 | random = rnd(maxrand); |
---|
101 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
102 | this->searchRandomTargetPosition(); |
---|
103 | |
---|
104 | |
---|
105 | random = rnd(maxrand); |
---|
106 | if (random < 75 && (this->target_ && !this->bShooting_)) |
---|
107 | this->bShooting_ = true; |
---|
108 | |
---|
109 | // stop shooting |
---|
110 | random = rnd(maxrand); |
---|
111 | if (random < 25 && (this->bShooting_)) |
---|
112 | this->bShooting_ = false; |
---|
113 | |
---|
114 | } |
---|
115 | |
---|
116 | if (this->state_ == SLAVE) |
---|
117 | { |
---|
118 | // this->bShooting_ = true; |
---|
119 | } |
---|
120 | |
---|
121 | if (this->state_ == MASTER)//MASTER |
---|
122 | { |
---|
123 | |
---|
124 | |
---|
125 | this->commandSlaves(); |
---|
126 | |
---|
127 | |
---|
128 | // lose master status (only if less than 4 slaves in formation) |
---|
129 | random = rnd(maxrand); |
---|
130 | if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 5 ) |
---|
131 | this->loseMasterState(); |
---|
132 | |
---|
133 | // look out for outher masters if formation is small |
---|
134 | random = rnd(maxrand); |
---|
135 | if(this->slaves_.size() < 3 && random < 20) |
---|
136 | this->searchNewMaster(); |
---|
137 | |
---|
138 | // search enemy |
---|
139 | random = rnd(maxrand); |
---|
140 | if (random < 15 && (!this->target_)) |
---|
141 | this->searchNewTarget(); |
---|
142 | |
---|
143 | // forget enemy |
---|
144 | random = rnd(maxrand); |
---|
145 | if (random < 5 && (this->target_)) |
---|
146 | this->forgetTarget(); |
---|
147 | |
---|
148 | // next enemy |
---|
149 | random = rnd(maxrand); |
---|
150 | if (random < 10 && (this->target_)) |
---|
151 | this->searchNewTarget(); |
---|
152 | |
---|
153 | // fly somewhere |
---|
154 | random = rnd(maxrand); |
---|
155 | if (random < 50 && (!this->bHasTargetPosition_ && !this->target_)) |
---|
156 | this->searchRandomTargetPosition(); |
---|
157 | |
---|
158 | |
---|
159 | // fly somewhere else |
---|
160 | random = rnd(maxrand); |
---|
161 | if (random < 30 && (this->bHasTargetPosition_ && !this->target_)) |
---|
162 | this->searchRandomTargetPosition(); |
---|
163 | |
---|
164 | // shoot |
---|
165 | random = rnd(maxrand); |
---|
166 | if (random < 5 && (this->target_ && !this->bShooting_)) |
---|
167 | { |
---|
168 | this->bShooting_ = true; |
---|
169 | this->forceFreeSlaves(); |
---|
170 | } |
---|
171 | // stop shooting |
---|
172 | random = rnd(maxrand); |
---|
173 | if (random < 25 && (this->bShooting_)) |
---|
174 | this->bShooting_ = false; |
---|
175 | } |
---|
176 | |
---|
177 | } |
---|
178 | |
---|
179 | void AIController::tick(float dt) |
---|
180 | { |
---|
181 | if (!this->isActive()) |
---|
182 | return; |
---|
183 | |
---|
184 | if (this->state_ == MASTER) |
---|
185 | { |
---|
186 | if (this->target_) |
---|
187 | this->aimAtTarget(); |
---|
188 | |
---|
189 | if (this->bHasTargetPosition_) |
---|
190 | this->moveToTargetPosition(); |
---|
191 | |
---|
192 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f)) |
---|
193 | this->getControllableEntity()->fire(0); |
---|
194 | } |
---|
195 | |
---|
196 | if (this->state_ == SLAVE) |
---|
197 | { |
---|
198 | |
---|
199 | if (this->bHasTargetPosition_) |
---|
200 | this->moveToTargetPosition(); |
---|
201 | |
---|
202 | } |
---|
203 | |
---|
204 | if (this->state_ == FREE) |
---|
205 | { |
---|
206 | if (this->target_) |
---|
207 | this->aimAtTarget(); |
---|
208 | |
---|
209 | if (this->bHasTargetPosition_) |
---|
210 | this->moveToTargetPosition(); |
---|
211 | |
---|
212 | if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f)) |
---|
213 | this->getControllableEntity()->fire(0); |
---|
214 | } |
---|
215 | |
---|
216 | SUPER(AIController, tick, dt); |
---|
217 | } |
---|
218 | |
---|
219 | } |
---|