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 | * Reto Grieder |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | #include "MultiStateEngine.h" |
---|
31 | |
---|
32 | extern "C" { |
---|
33 | #include <lua.h> |
---|
34 | } |
---|
35 | |
---|
36 | #include "util/Convert.h" |
---|
37 | #include "util/StringUtils.h" |
---|
38 | #include "core/CoreIncludes.h" |
---|
39 | #include "core/GameMode.h" |
---|
40 | #include "core/LuaState.h" |
---|
41 | #include "core/XMLPort.h" |
---|
42 | #include "worldentities/EffectContainer.h" |
---|
43 | #include "worldentities/pawns/SpaceShip.h" |
---|
44 | #include "sound/WorldSound.h" |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 0; |
---|
49 | static const float MAX_VELOCITY_NORMAL = 111; |
---|
50 | static const float MAX_VELOCITY_BOOST = 221; |
---|
51 | |
---|
52 | CreateFactory(MultiStateEngine); |
---|
53 | |
---|
54 | MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator) |
---|
55 | { |
---|
56 | RegisterObject(MultiStateEngine); |
---|
57 | |
---|
58 | if( GameMode::isMaster() ) |
---|
59 | { |
---|
60 | this->defEngineSndNormal_ = new WorldSound(this); |
---|
61 | this->defEngineSndBoost_ = new WorldSound(this); |
---|
62 | this->defEngineSndNormal_->setLooping(true); |
---|
63 | this->defEngineSndBoost_->setLooping(true); |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | this->defEngineSndBoost_ = 0; |
---|
68 | this->defEngineSndNormal_ = 0; |
---|
69 | } |
---|
70 | |
---|
71 | this->lua_ = new LuaState(); |
---|
72 | this->state_ = 0; |
---|
73 | |
---|
74 | this->registerVariables(); |
---|
75 | } |
---|
76 | |
---|
77 | MultiStateEngine::~MultiStateEngine() |
---|
78 | { |
---|
79 | if (this->isInitialized() && !this->getShip()) |
---|
80 | { |
---|
81 | // We have no ship, so the effects are not attached and won't be destroyed automatically |
---|
82 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
83 | for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2) |
---|
84 | (*it2)->destroy(); |
---|
85 | if( this->defEngineSndNormal_ ) |
---|
86 | delete this->defEngineSndNormal_; |
---|
87 | if( this->defEngineSndBoost_ ) |
---|
88 | delete this->defEngineSndBoost_; |
---|
89 | delete this->lua_; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
94 | { |
---|
95 | SUPER(MultiStateEngine, XMLPort, xmlelement, mode); |
---|
96 | XMLPortObject(MultiStateEngine, EffectContainer, "", addEffectContainer, getEffectContainer, xmlelement, mode); |
---|
97 | XMLPortParam(MultiStateEngine, "defEngineSndNormal", setDefEngSndNormal, getDefEngSndNormal, xmlelement, mode); |
---|
98 | XMLPortParam(MultiStateEngine, "defEngineSndBoost", setDefEngSndBoost, getDefEngSndBoost, xmlelement, mode); |
---|
99 | } |
---|
100 | |
---|
101 | void MultiStateEngine::registerVariables() |
---|
102 | { |
---|
103 | registerVariable(this->state_, VariableDirection::ToServer); |
---|
104 | } |
---|
105 | |
---|
106 | void MultiStateEngine::tick(float dt) |
---|
107 | { |
---|
108 | if (this->getShip()) |
---|
109 | { |
---|
110 | // if (this->getShip()->hasLocalController()) |
---|
111 | if (GameMode::isMaster() && this->getShip()->getController()) |
---|
112 | { |
---|
113 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
114 | |
---|
115 | const Vector3& direction = this->getDirection(); |
---|
116 | const Vector3& velocity = this->getShip()->getLocalVelocity(); |
---|
117 | |
---|
118 | float pitch = velocity.length(); |
---|
119 | bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD); |
---|
120 | |
---|
121 | int newState = 0; |
---|
122 | if (this->getShip()->getBoost() && forward) |
---|
123 | { |
---|
124 | newState = Boost; |
---|
125 | pitch = pitch/MAX_VELOCITY_BOOST + 1; |
---|
126 | pitch = pitch > 2 ? 2 : pitch; |
---|
127 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
128 | defEngineSndBoost_->setPitch(pitch); |
---|
129 | } |
---|
130 | else if (forward && !newState) // newState == Boost |
---|
131 | { |
---|
132 | newState = Normal; |
---|
133 | pitch = pitch/MAX_VELOCITY_NORMAL + 1; |
---|
134 | pitch = pitch > 2 ? 2 : pitch; |
---|
135 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
136 | defEngineSndNormal_->setPitch(pitch); |
---|
137 | } |
---|
138 | else if (direction.z > 0 && velocity.z < 0) |
---|
139 | newState = Brake; |
---|
140 | else |
---|
141 | newState = Idle; |
---|
142 | |
---|
143 | if (newState != this->state_) |
---|
144 | { |
---|
145 | int changes = newState | this->state_; |
---|
146 | if (changes & Idle) |
---|
147 | { |
---|
148 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle); |
---|
149 | lua_setglobal(this->lua_->getInternalLuaState(), "idle"); |
---|
150 | } |
---|
151 | if (changes & Normal) |
---|
152 | { |
---|
153 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal); |
---|
154 | lua_setglobal(this->lua_->getInternalLuaState(), "normal"); |
---|
155 | if(newState & Normal) |
---|
156 | { |
---|
157 | defEngineSndNormal_->play(); |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | defEngineSndNormal_->stop(); |
---|
162 | } |
---|
163 | } |
---|
164 | if (changes & Brake) |
---|
165 | { |
---|
166 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake); |
---|
167 | lua_setglobal(this->lua_->getInternalLuaState(), "brake"); |
---|
168 | } |
---|
169 | if (changes & Boost) |
---|
170 | { |
---|
171 | lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost); |
---|
172 | lua_setglobal(this->lua_->getInternalLuaState(), "boost"); |
---|
173 | if(newState & Boost) |
---|
174 | { |
---|
175 | defEngineSndBoost_->play(); |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | defEngineSndBoost_->stop(); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | // Update all effect conditions |
---|
184 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
185 | (*it)->updateCondition(); |
---|
186 | |
---|
187 | this->state_ = newState; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | if (GameMode::isMaster()) |
---|
192 | { |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | SUPER(MultiStateEngine, tick, dt); |
---|
197 | } |
---|
198 | |
---|
199 | void MultiStateEngine::addToSpaceShip(SpaceShip* ship) |
---|
200 | { |
---|
201 | Engine::addToSpaceShip(ship); |
---|
202 | |
---|
203 | if (!ship) |
---|
204 | return; |
---|
205 | |
---|
206 | if( this->defEngineSndNormal_ ) |
---|
207 | this->getShip()->attach(defEngineSndNormal_); |
---|
208 | if( this->defEngineSndBoost_ ) |
---|
209 | this->getShip()->attach(defEngineSndBoost_); |
---|
210 | |
---|
211 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
212 | for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2) |
---|
213 | this->getShip()->attach(*it2); |
---|
214 | } |
---|
215 | |
---|
216 | void MultiStateEngine::addEffectContainer(EffectContainer* effect) |
---|
217 | { |
---|
218 | if (effect == NULL) |
---|
219 | return; |
---|
220 | effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size())); |
---|
221 | this->effectContainers_.push_back(effect); |
---|
222 | if (this->getShip()) |
---|
223 | { |
---|
224 | for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it) |
---|
225 | this->getShip()->attach(*it); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const |
---|
230 | { |
---|
231 | unsigned int i = 0; |
---|
232 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
233 | { |
---|
234 | if (i == index) |
---|
235 | return (*it); |
---|
236 | } |
---|
237 | return NULL; |
---|
238 | } |
---|
239 | |
---|
240 | void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound) |
---|
241 | { |
---|
242 | if( defEngineSndNormal_ ) |
---|
243 | defEngineSndNormal_->setSource(engineSound); |
---|
244 | else |
---|
245 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
246 | } |
---|
247 | |
---|
248 | const std::string& MultiStateEngine::getDefEngSndNormal() |
---|
249 | { |
---|
250 | if( defEngineSndNormal_ ) |
---|
251 | return defEngineSndNormal_->getSource(); |
---|
252 | else |
---|
253 | assert(0); |
---|
254 | return BLANKSTRING; |
---|
255 | } |
---|
256 | |
---|
257 | void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound) |
---|
258 | { |
---|
259 | if( defEngineSndBoost_ ) |
---|
260 | defEngineSndBoost_->setSource(engineSound); |
---|
261 | else |
---|
262 | assert(0); |
---|
263 | } |
---|
264 | |
---|
265 | const std::string& MultiStateEngine::getDefEngSndBoost() |
---|
266 | { |
---|
267 | if( this->defEngineSndBoost_ ) |
---|
268 | return defEngineSndBoost_->getSource(); |
---|
269 | else |
---|
270 | assert(0); |
---|
271 | return BLANKSTRING; |
---|
272 | } |
---|
273 | } |
---|