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