[2254] | 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 "Engine.h" |
---|
| 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
[9667] | 32 | #include "core/config/ConfigValueIncludes.h" |
---|
[8727] | 33 | #include "core/Template.h" |
---|
[2254] | 34 | #include "core/XMLPort.h" |
---|
[8727] | 35 | #include "util/Math.h" |
---|
| 36 | |
---|
[5735] | 37 | #include "Scene.h" |
---|
| 38 | #include "worldentities/pawns/SpaceShip.h" |
---|
[2254] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
[9667] | 42 | RegisterClass(Engine); |
---|
[2254] | 43 | |
---|
[8727] | 44 | /** |
---|
| 45 | @brief |
---|
| 46 | Constructor. Registers and initializes the object. |
---|
| 47 | */ |
---|
[9667] | 48 | Engine::Engine(Context* context) : Item(context) |
---|
[2254] | 49 | { |
---|
| 50 | RegisterObject(Engine); |
---|
| 51 | |
---|
[11071] | 52 | this->ship_ = nullptr; |
---|
[2254] | 53 | this->shipID_ = OBJECTID_UNKNOWN; |
---|
[8727] | 54 | this->relativePosition_ = Vector3::ZERO; |
---|
[2254] | 55 | |
---|
[12029] | 56 | this->boostFactor_ = 1.2f; |
---|
[2254] | 57 | |
---|
[8727] | 58 | this->maxSpeedFront_ = 0.0f; |
---|
| 59 | this->maxSpeedBack_ = 0.0f; |
---|
| 60 | this->maxSpeedLeftRight_ = 0.0f; |
---|
| 61 | this->maxSpeedUpDown_ = 0.0f; |
---|
[2254] | 62 | |
---|
[12029] | 63 | this->accelerationFront_ = 1.0f; |
---|
[8727] | 64 | this->accelerationBrake_ = 0.0f; |
---|
| 65 | this->accelerationBack_ = 0.0f; |
---|
| 66 | this->accelerationLeftRight_ = 0.0f; |
---|
| 67 | this->accelerationUpDown_ = 0.0f; |
---|
[2254] | 68 | |
---|
[8727] | 69 | this->speedAdd_ = 0.0f; |
---|
[12029] | 70 | this->speedMultiply_ = 1.0f; ////////////////////////////////////////// |
---|
[6709] | 71 | |
---|
[2478] | 72 | this->setConfigValues(); |
---|
[2254] | 73 | this->registerVariables(); |
---|
| 74 | } |
---|
| 75 | |
---|
[8727] | 76 | /** |
---|
| 77 | @brief |
---|
| 78 | Destructor. Destroys the object and removes it from the SpaceShip. |
---|
| 79 | */ |
---|
[2254] | 80 | Engine::~Engine() |
---|
| 81 | { |
---|
[8706] | 82 | if (this->isInitialized()) |
---|
[2350] | 83 | { |
---|
[8727] | 84 | // Remove the engine from the ShapeShip. |
---|
[8706] | 85 | if (this->ship_ && this->ship_->hasEngine(this)) |
---|
| 86 | this->ship_->removeEngine(this); |
---|
[2350] | 87 | } |
---|
[2254] | 88 | } |
---|
| 89 | |
---|
| 90 | void Engine::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 91 | { |
---|
| 92 | SUPER(Engine, XMLPort, xmlelement, mode); |
---|
| 93 | |
---|
| 94 | XMLPortParam(Engine, "boostfactor", setBoostFactor, getBoostFactor, xmlelement, mode); |
---|
| 95 | |
---|
| 96 | XMLPortParam(Engine, "speedfront", setMaxSpeedFront, setMaxSpeedFront, xmlelement, mode); |
---|
| 97 | XMLPortParam(Engine, "speedback", setMaxSpeedBack, setMaxSpeedBack, xmlelement, mode); |
---|
| 98 | XMLPortParam(Engine, "speedleftright", setMaxSpeedLeftRight, setMaxSpeedLeftRight, xmlelement, mode); |
---|
| 99 | XMLPortParam(Engine, "speedupdown", setMaxSpeedUpDown, setMaxSpeedUpDown, xmlelement, mode); |
---|
| 100 | |
---|
| 101 | XMLPortParam(Engine, "accelerationfront", setAccelerationFront, setAccelerationFront, xmlelement, mode); |
---|
| 102 | XMLPortParam(Engine, "accelerationbrake", setAccelerationBrake, setAccelerationBrake, xmlelement, mode); |
---|
| 103 | XMLPortParam(Engine, "accelerationback", setAccelerationBack, setAccelerationBack, xmlelement, mode); |
---|
| 104 | XMLPortParam(Engine, "accelerationleftright", setAccelerationLeftRight, setAccelerationLeftRight, xmlelement, mode); |
---|
| 105 | XMLPortParam(Engine, "accelerationupdown", setAccelerationUpDown, setAccelerationUpDown, xmlelement, mode); |
---|
[8706] | 106 | |
---|
| 107 | XMLPortParam(Engine, "position", setRelativePosition, getRelativePosition, xmlelement, mode); |
---|
| 108 | XMLPortParam(Engine, "template", setEngineTemplate, getEngineTemplate, xmlelement, mode); |
---|
[2254] | 109 | } |
---|
| 110 | |
---|
[2478] | 111 | void Engine::setConfigValues() |
---|
| 112 | { |
---|
| 113 | } |
---|
| 114 | |
---|
[2254] | 115 | void Engine::registerVariables() |
---|
| 116 | { |
---|
[3280] | 117 | registerVariable(this->shipID_, VariableDirection::ToClient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID)); |
---|
[2254] | 118 | |
---|
[3280] | 119 | registerVariable(this->boostFactor_, VariableDirection::ToClient); |
---|
[2254] | 120 | |
---|
[3280] | 121 | registerVariable(this->maxSpeedFront_, VariableDirection::ToClient); |
---|
| 122 | registerVariable(this->maxSpeedBack_, VariableDirection::ToClient); |
---|
| 123 | registerVariable(this->maxSpeedLeftRight_, VariableDirection::ToClient); |
---|
| 124 | registerVariable(this->maxSpeedUpDown_, VariableDirection::ToClient); |
---|
[2254] | 125 | |
---|
[3280] | 126 | registerVariable(this->accelerationFront_, VariableDirection::ToClient); |
---|
| 127 | registerVariable(this->accelerationBrake_, VariableDirection::ToClient); |
---|
| 128 | registerVariable(this->accelerationBack_, VariableDirection::ToClient); |
---|
| 129 | registerVariable(this->accelerationLeftRight_, VariableDirection::ToClient); |
---|
| 130 | registerVariable(this->accelerationUpDown_, VariableDirection::ToClient); |
---|
[6709] | 131 | |
---|
| 132 | registerVariable(this->speedAdd_, VariableDirection::ToClient); |
---|
| 133 | registerVariable(this->speedMultiply_, VariableDirection::ToClient); |
---|
[2254] | 134 | } |
---|
| 135 | |
---|
| 136 | void Engine::networkcallback_shipID() |
---|
| 137 | { |
---|
[11071] | 138 | this->ship_ = nullptr; |
---|
[2254] | 139 | |
---|
| 140 | if (this->shipID_ != OBJECTID_UNKNOWN) |
---|
| 141 | { |
---|
| 142 | Synchronisable* object = Synchronisable::getSynchronisable(this->shipID_); |
---|
| 143 | if (object) |
---|
[3325] | 144 | this->addToSpaceShip(orxonox_cast<SpaceShip*>(object)); |
---|
[2254] | 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
[8727] | 148 | /** |
---|
| 149 | @brief |
---|
| 150 | Run the engine for a given time interval. |
---|
| 151 | Is called each tick by SpaceShip. |
---|
| 152 | @param dt |
---|
| 153 | The time since last tick. |
---|
| 154 | */ |
---|
| 155 | void Engine::run(float dt) |
---|
[2254] | 156 | { |
---|
[11071] | 157 | if (this->ship_ == nullptr) |
---|
[2254] | 158 | { |
---|
[8727] | 159 | if (this->shipID_ != 0) |
---|
[2254] | 160 | { |
---|
| 161 | this->networkcallback_shipID(); |
---|
| 162 | |
---|
[11071] | 163 | if (this->ship_ == nullptr) |
---|
[2254] | 164 | return; |
---|
| 165 | } |
---|
| 166 | else |
---|
| 167 | return; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | if (!this->isActive()) |
---|
| 171 | return; |
---|
| 172 | |
---|
[8727] | 173 | // Get the desired steering direction and amount, clipped to length 1 at maximum. |
---|
| 174 | Vector3 steering = (this->getSteering().length() > 1.0f ? this->getSteering().normalisedCopy() : this->getSteering()); |
---|
[2361] | 175 | |
---|
[8727] | 176 | // Get the ships velocity. |
---|
[2488] | 177 | Vector3 velocity = this->ship_->getLocalVelocity(); |
---|
[2254] | 178 | Vector3 acceleration = Vector3::ZERO; |
---|
| 179 | |
---|
[8727] | 180 | // If there is forward steering action. |
---|
| 181 | if (steering.z < 0) |
---|
[2254] | 182 | { |
---|
| 183 | if (this->maxSpeedFront_ != 0) |
---|
| 184 | { |
---|
[8727] | 185 | float boostfactor = (this->ship_->isBoosting() ? this->boostFactor_ : 1.0f); // Boost factor is 1.0 if not boosting. |
---|
| 186 | // Boosting can lead to velocities larger the maximal forward velocity. |
---|
| 187 | acceleration.z = steering.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f); |
---|
[2254] | 188 | } |
---|
| 189 | } |
---|
[8727] | 190 | // If there is backward steering action. |
---|
| 191 | else if (steering.z > 0) |
---|
[2254] | 192 | { |
---|
[8727] | 193 | // Either breaking |
---|
[2254] | 194 | if (velocity.z < 0) |
---|
[8727] | 195 | acceleration.z = steering.z * this->accelerationBrake_; |
---|
| 196 | // or backward flight. |
---|
[2254] | 197 | else if (this->maxSpeedBack_ != 0) |
---|
[8727] | 198 | acceleration.z = steering.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f); |
---|
[2254] | 199 | } |
---|
[8727] | 200 | // If there is left-right steering action. |
---|
[2254] | 201 | if (this->maxSpeedLeftRight_ != 0) |
---|
| 202 | { |
---|
[8727] | 203 | if (steering.x < 0) |
---|
| 204 | acceleration.x = steering.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); |
---|
| 205 | else if (steering.x > 0) |
---|
| 206 | acceleration.x = steering.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); |
---|
[2254] | 207 | } |
---|
[8727] | 208 | // If there is up-down steering action. |
---|
[2254] | 209 | if (this->maxSpeedUpDown_ != 0) |
---|
| 210 | { |
---|
[8727] | 211 | if (steering.y < 0) |
---|
| 212 | acceleration.y = steering.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); |
---|
| 213 | else if (steering.y > 0) |
---|
| 214 | acceleration.y = steering.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); |
---|
[2254] | 215 | } |
---|
| 216 | |
---|
[8706] | 217 | // NOTE: Bullet always uses global coordinates. |
---|
| 218 | this->ship_->addAcceleration(this->ship_->getOrientation() * (acceleration*this->getSpeedMultiply()+Vector3(0,0,-this->getSpeedAdd())), this->ship_->getOrientation() * this->relativePosition_); |
---|
[2254] | 219 | } |
---|
| 220 | |
---|
[8727] | 221 | /** |
---|
| 222 | @brief |
---|
| 223 | Adds the Engine to the input SpaceShip. |
---|
| 224 | @param ship |
---|
| 225 | A pointer to the SpaceShip to which the engine is added. |
---|
| 226 | */ |
---|
[2254] | 227 | void Engine::addToSpaceShip(SpaceShip* ship) |
---|
| 228 | { |
---|
| 229 | this->ship_ = ship; |
---|
[3060] | 230 | |
---|
[2254] | 231 | if (ship) |
---|
[2256] | 232 | { |
---|
[2254] | 233 | this->shipID_ = ship->getObjectID(); |
---|
[8706] | 234 | if (!ship->hasEngine(this)) |
---|
| 235 | ship->addEngine(this); |
---|
[2256] | 236 | } |
---|
[2254] | 237 | } |
---|
| 238 | |
---|
[8727] | 239 | /** |
---|
| 240 | @brief |
---|
| 241 | Get the direction and magnitude of the steering imposed upon the Engine. |
---|
| 242 | @return |
---|
| 243 | Returns the direction and magnitude of the steering imposed upon the Engine. |
---|
| 244 | Is the zero vector if the Engine doesn't belong to a ship. |
---|
| 245 | */ |
---|
| 246 | const Vector3& Engine::getSteering() const |
---|
[2254] | 247 | { |
---|
| 248 | if (this->ship_) |
---|
| 249 | return this->ship_->getSteeringDirection(); |
---|
| 250 | else |
---|
| 251 | return Vector3::ZERO; |
---|
| 252 | } |
---|
[6709] | 253 | |
---|
[8727] | 254 | /** |
---|
| 255 | @brief |
---|
| 256 | Load the engine template. |
---|
| 257 | Causes all parameters specified by the template to be applied to the Engine. |
---|
| 258 | */ |
---|
[8706] | 259 | void Engine::loadEngineTemplate() |
---|
[8079] | 260 | { |
---|
[8706] | 261 | if(!this->engineTemplate_.empty()) |
---|
[8079] | 262 | { |
---|
[8858] | 263 | orxout(verbose, context::templates) << "Loading an engine template: " << this->engineTemplate_ << endl; |
---|
[8706] | 264 | Template *temp = Template::getTemplate(this->engineTemplate_); |
---|
| 265 | if(temp) |
---|
| 266 | { |
---|
| 267 | this->addTemplate(temp); |
---|
| 268 | } |
---|
[8079] | 269 | } |
---|
| 270 | } |
---|
[8727] | 271 | |
---|
[2254] | 272 | } |
---|