| [8087] | 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 |  *      Maurus Kaufmann | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include "SpaceBoundaries.h" | 
|---|
 | 30 |  | 
|---|
| [8632] | 31 | #include <OgreBillboardSet.h> | 
|---|
 | 32 |  | 
|---|
 | 33 | #include "core/CoreIncludes.h" | 
|---|
| [9667] | 34 | #include "core/object/ObjectListIterator.h" | 
|---|
| [8201] | 35 | #include "core/XMLPort.h" | 
|---|
| [8632] | 36 |  | 
|---|
 | 37 | #include "graphics/Billboard.h" | 
|---|
 | 38 | #include "infos/PlayerInfo.h" | 
|---|
 | 39 | #include "worldentities/WorldEntity.h" | 
|---|
| [8201] | 40 | #include "worldentities/pawns/Pawn.h" | 
|---|
| [8087] | 41 |  | 
|---|
 | 42 | namespace orxonox | 
|---|
 | 43 | { | 
|---|
| [9667] | 44 |     RegisterClass(SpaceBoundaries); | 
|---|
| [8087] | 45 |  | 
|---|
| [9667] | 46 |     SpaceBoundaries::SpaceBoundaries(Context* context) : StaticEntity(context) | 
|---|
| [8087] | 47 |     { | 
|---|
| [8614] | 48 |         RegisterObject(SpaceBoundaries); | 
|---|
 | 49 |  | 
|---|
| [8201] | 50 |         this->setMaxDistance(3000); | 
|---|
| [8614] | 51 |         this->setWarnDistance(this->getMaxDistance()); | 
|---|
 | 52 |         this->setShowDistance(this->getMaxDistance()); | 
|---|
| [8404] | 53 |         this->setReaction(0); | 
|---|
| [8087] | 54 |     } | 
|---|
 | 55 |     SpaceBoundaries::~SpaceBoundaries() | 
|---|
 | 56 |     { | 
|---|
| [8470] | 57 |         if (this->isInitialized()) | 
|---|
 | 58 |         { | 
|---|
 | 59 |             this->pawnsIn_.clear(); | 
|---|
| [8767] | 60 |  | 
|---|
| [11071] | 61 |             for(BillboardAdministration& billboard : this->billboards_) | 
|---|
| [8301] | 62 |             { | 
|---|
| [11071] | 63 |                 if( billboard.billy != nullptr) | 
|---|
| [8470] | 64 |                 { | 
|---|
| [11071] | 65 |                     delete billboard.billy; | 
|---|
| [8470] | 66 |                 } | 
|---|
| [8301] | 67 |             } | 
|---|
| [8470] | 68 |             this->billboards_.clear(); | 
|---|
| [8301] | 69 |         } | 
|---|
| [8087] | 70 |     } | 
|---|
| [8767] | 71 |  | 
|---|
| [8281] | 72 |     void SpaceBoundaries::checkWhoIsIn() | 
|---|
 | 73 |     { | 
|---|
 | 74 |         pawnsIn_.clear(); | 
|---|
| [11071] | 75 |         for(Pawn* currentPawn : ObjectList<Pawn>()) | 
|---|
| [8281] | 76 |         { | 
|---|
| [8614] | 77 |             if( this->reaction_ == 0 ) | 
|---|
| [8281] | 78 |             { | 
|---|
| [8614] | 79 |                 float distance = this->computeDistance(currentPawn); | 
|---|
 | 80 |                 if(distance <= this->maxDistance_) | 
|---|
 | 81 |                 { | 
|---|
 | 82 |                     pawnsIn_.push_back(currentPawn); | 
|---|
 | 83 |                 } | 
|---|
| [11105] | 84 |             } | 
|---|
 | 85 |             else if (this->reaction_ == 2) | 
|---|
 | 86 |             { | 
|---|
| [8614] | 87 |                 float distance = this->computeDistance(currentPawn); | 
|---|
 | 88 |                 if(distance >= this->maxDistance_) | 
|---|
 | 89 |                 { | 
|---|
 | 90 |                     pawnsIn_.push_back(currentPawn); | 
|---|
 | 91 |                 } | 
|---|
| [11105] | 92 |             } | 
|---|
 | 93 |             else | 
|---|
 | 94 |             { | 
|---|
| [8281] | 95 |                 pawnsIn_.push_back(currentPawn); | 
|---|
 | 96 |             } | 
|---|
 | 97 |         } | 
|---|
 | 98 |     } | 
|---|
| [8767] | 99 |  | 
|---|
| [8660] | 100 |     void SpaceBoundaries::positionBillboard(const Vector3& position, float alpha) | 
|---|
| [8301] | 101 |     { | 
|---|
| [8660] | 102 |         size_t current; | 
|---|
 | 103 |         for (current = 0; current < this->billboards_.size(); ++current) | 
|---|
 | 104 |             if (!this->billboards_[current].usedYet) | 
|---|
| [8301] | 105 |                 break; | 
|---|
| [8660] | 106 |  | 
|---|
 | 107 |         if (current == this->billboards_.size()) | 
|---|
| [8301] | 108 |         { | 
|---|
| [9667] | 109 |             Billboard* billboard = new Billboard(this->getContext()); | 
|---|
| [8660] | 110 |             billboard->setPosition(position); | 
|---|
| [8694] | 111 |             billboard->setSyncMode(ObjectDirection::None); | 
|---|
| [8660] | 112 |             this->setBillboardOptions(billboard); | 
|---|
 | 113 |             BillboardAdministration ba = {true, billboard}; | 
|---|
 | 114 |             this->billboards_.push_back(ba); | 
|---|
| [8301] | 115 |         } | 
|---|
| [8660] | 116 |  | 
|---|
 | 117 |         this->billboards_[current].billy->setPosition(position); | 
|---|
 | 118 |         this->billboards_[current].billy->setVisible(true); | 
|---|
 | 119 |         this->billboards_[current].billy->setColour(ColourValue(1, 1, 1, alpha)); | 
|---|
 | 120 |         this->billboards_[current].usedYet = true; | 
|---|
 | 121 |  | 
|---|
| [9941] | 122 |         Vector3 directionVector = (this->getWorldPosition() - position).normalisedCopy(); // vector from the position of the billboard to the center of the sphere | 
|---|
| [8660] | 123 |         this->billboards_[current].billy->setCommonDirection(directionVector); | 
|---|
 | 124 |  | 
|---|
 | 125 |         Vector3 upVector = Vector3(directionVector.z, directionVector.z, -(directionVector.x + directionVector.y)); // vector perpendicular to the direction vector | 
|---|
 | 126 |         upVector.normalise(); | 
|---|
 | 127 |         this->billboards_[current].billy->setCommonUpVector(upVector); | 
|---|
| [8301] | 128 |     } | 
|---|
| [8767] | 129 |  | 
|---|
| [8301] | 130 |     void SpaceBoundaries::setBillboardOptions(Billboard *billy) | 
|---|
 | 131 |     { | 
|---|
| [11071] | 132 |         if(billy != nullptr) | 
|---|
| [8301] | 133 |         { | 
|---|
| [8614] | 134 |             billy->setMaterial("Grid"); | 
|---|
 | 135 |             billy->setBillboardType(Ogre::BBT_PERPENDICULAR_COMMON); | 
|---|
 | 136 |             billy->setDefaultDimensions(150, 150); | 
|---|
| [8301] | 137 |             billy->setVisible(true); | 
|---|
 | 138 |         } | 
|---|
 | 139 |     } | 
|---|
| [8767] | 140 |  | 
|---|
| [8301] | 141 |     void SpaceBoundaries::removeAllBillboards() | 
|---|
 | 142 |     { | 
|---|
| [11071] | 143 |         for(BillboardAdministration& billboard : this->billboards_) | 
|---|
| [8301] | 144 |         { | 
|---|
| [11071] | 145 |             billboard.usedYet = false; | 
|---|
 | 146 |             billboard.billy->setVisible(false); | 
|---|
| [8301] | 147 |         } | 
|---|
 | 148 |     } | 
|---|
| [8767] | 149 |  | 
|---|
| [8110] | 150 |     void SpaceBoundaries::setMaxDistance(float r) | 
|---|
 | 151 |     { | 
|---|
| [8166] | 152 |         this->maxDistance_ = r; | 
|---|
| [8110] | 153 |     } | 
|---|
 | 154 |     float SpaceBoundaries::getMaxDistance() | 
|---|
 | 155 |     { | 
|---|
| [8166] | 156 |         return this->maxDistance_; | 
|---|
| [8110] | 157 |     } | 
|---|
| [8767] | 158 |  | 
|---|
| [8110] | 159 |     void SpaceBoundaries::setWarnDistance(float r) | 
|---|
 | 160 |     { | 
|---|
| [8166] | 161 |         this->warnDistance_ = r; | 
|---|
| [8110] | 162 |     } | 
|---|
 | 163 |     float SpaceBoundaries::getWarnDistance() | 
|---|
 | 164 |     { | 
|---|
| [8166] | 165 |         return this->warnDistance_; | 
|---|
| [8110] | 166 |     } | 
|---|
| [8767] | 167 |  | 
|---|
| [8244] | 168 |     void SpaceBoundaries::setShowDistance(float r) | 
|---|
 | 169 |     { | 
|---|
 | 170 |         this->showDistance_ = r; | 
|---|
 | 171 |     } | 
|---|
 | 172 |     float SpaceBoundaries::getShowDistance() | 
|---|
 | 173 |     { | 
|---|
 | 174 |         return this->showDistance_; | 
|---|
 | 175 |     } | 
|---|
| [8767] | 176 |  | 
|---|
| [8201] | 177 |     void SpaceBoundaries::setHealthDecrease(float amount) | 
|---|
 | 178 |     { | 
|---|
 | 179 |         this->healthDecrease_ = amount/1000; | 
|---|
 | 180 |     } | 
|---|
 | 181 |     float SpaceBoundaries::getHealthDecrease() | 
|---|
 | 182 |     { | 
|---|
 | 183 |         return this->healthDecrease_; | 
|---|
 | 184 |     } | 
|---|
| [8767] | 185 |  | 
|---|
| [8404] | 186 |     void SpaceBoundaries::setReaction(int mode) | 
|---|
 | 187 |     { | 
|---|
 | 188 |         this->reaction_ = mode; | 
|---|
 | 189 |     } | 
|---|
 | 190 |     int SpaceBoundaries::getReaction() | 
|---|
 | 191 |     { | 
|---|
 | 192 |         return this->reaction_; | 
|---|
 | 193 |     } | 
|---|
| [8087] | 194 |  | 
|---|
 | 195 |     void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 196 |     { | 
|---|
| [8110] | 197 |         SUPER(SpaceBoundaries, XMLPort, xmlelement, mode); | 
|---|
| [8087] | 198 |  | 
|---|
| [8110] | 199 |         XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode); | 
|---|
 | 200 |         XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode); | 
|---|
| [8614] | 201 |         XMLPortParam(SpaceBoundaries, "showDistance", setShowDistance, getShowDistance, xmlelement, mode); | 
|---|
| [8201] | 202 |         XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode); | 
|---|
| [8404] | 203 |         XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode); | 
|---|
| [8087] | 204 |     } | 
|---|
| [8767] | 205 |  | 
|---|
| [8110] | 206 |     void SpaceBoundaries::tick(float dt) | 
|---|
 | 207 |     { | 
|---|
| [8404] | 208 |         this->checkWhoIsIn(); | 
|---|
| [8301] | 209 |         this->removeAllBillboards(); | 
|---|
| [8767] | 210 |  | 
|---|
| [11105] | 211 |         if(!this->isActive()) | 
|---|
 | 212 |             return; | 
|---|
 | 213 |  | 
|---|
| [8301] | 214 |         float distance; | 
|---|
 | 215 |         bool humanItem; | 
|---|
| [11071] | 216 |         for(Pawn* currentPawn : pawnsIn_) | 
|---|
| [8110] | 217 |         { | 
|---|
| [8404] | 218 |             if( currentPawn && currentPawn->getNode() )  | 
|---|
| [8110] | 219 |             { | 
|---|
| [8404] | 220 |                 distance = this->computeDistance(currentPawn); | 
|---|
 | 221 |                 humanItem = this->isHumanPlayer(currentPawn); | 
|---|
| [8858] | 222 | //                orxout() << "Distance:" << distance << endl; // message for debugging | 
|---|
| [8614] | 223 |                 if(distance > this->warnDistance_ && distance < this->maxDistance_) // Display warning | 
|---|
| [8164] | 224 |                 { | 
|---|
| [8404] | 225 |                     if(humanItem) | 
|---|
 | 226 |                     { | 
|---|
| [8614] | 227 |                         this->displayWarning("Attention! You are close to the boundary!"); | 
|---|
| [8404] | 228 |                     } | 
|---|
| [8201] | 229 |                 } | 
|---|
| [11083] | 230 |                 if(/* humanItem &&*/ std::abs(this->maxDistance_ - distance) < this->showDistance_ ) | 
|---|
| [8164] | 231 |                 { | 
|---|
| [8660] | 232 |                     this->displayBoundaries(currentPawn, 1.0f - fabs(this->maxDistance_ - distance) / this->showDistance_); // Show the boundary | 
|---|
| [8164] | 233 |                 } | 
|---|
| [8404] | 234 |                 if(distance > this->maxDistance_ && (this->reaction_ == 1) ) | 
|---|
 | 235 |                 { | 
|---|
 | 236 |                     if( humanItem ) | 
|---|
 | 237 |                     { | 
|---|
| [8858] | 238 | //                        orxout() << "Health should be decreasing!" << endl; | 
|---|
| [8404] | 239 |                         this->displayWarning("You are out of the area now!"); | 
|---|
 | 240 |                     } | 
|---|
 | 241 |                     currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_); | 
|---|
 | 242 |                 } | 
|---|
| [8614] | 243 |                 if( (this->reaction_ == 0) && (distance + 100 > this->maxDistance_)) // Exception: A Pawn can't move more than 100 units per tick. | 
|---|
| [8404] | 244 |                 { | 
|---|
 | 245 |                     this->conditionalBounceBack(currentPawn, distance, dt); | 
|---|
 | 246 |                 } | 
|---|
| [8614] | 247 |                 if( this->reaction_ == 2 && (distance - 100 < this->maxDistance_) ) | 
|---|
 | 248 |                 { | 
|---|
 | 249 |                     this->conditionalBounceBack(currentPawn, distance, dt); | 
|---|
 | 250 |                 } | 
|---|
| [8110] | 251 |             } | 
|---|
 | 252 |         } | 
|---|
 | 253 |     } | 
|---|
| [8767] | 254 |  | 
|---|
| [8110] | 255 |     float SpaceBoundaries::computeDistance(WorldEntity *item) | 
|---|
 | 256 |     { | 
|---|
| [11071] | 257 |         if(item != nullptr) | 
|---|
| [8301] | 258 |         { | 
|---|
| [9941] | 259 |             Vector3 itemPosition = item->getWorldPosition(); | 
|---|
 | 260 |             return (itemPosition.distance(this->getWorldPosition())); | 
|---|
| [8301] | 261 |         } else { | 
|---|
 | 262 |             return -1; | 
|---|
 | 263 |         } | 
|---|
| [8110] | 264 |     } | 
|---|
| [8767] | 265 |  | 
|---|
| [8164] | 266 |     void SpaceBoundaries::displayWarning(const std::string warnText) | 
|---|
| [8767] | 267 |     { | 
|---|
| [8614] | 268 |         // TODO | 
|---|
| [8164] | 269 |     } | 
|---|
| [8767] | 270 |  | 
|---|
| [8660] | 271 |     void SpaceBoundaries::displayBoundaries(Pawn *item, float alpha) | 
|---|
| [8244] | 272 |     { | 
|---|
| [8767] | 273 |  | 
|---|
| [9941] | 274 |         Vector3 direction = item->getWorldPosition() - this->getWorldPosition(); | 
|---|
| [8244] | 275 |         direction.normalise(); | 
|---|
| [8767] | 276 |  | 
|---|
| [9941] | 277 |         Vector3 boundaryPosition = this->getWorldPosition() + direction * this->maxDistance_; | 
|---|
| [8767] | 278 |  | 
|---|
| [8660] | 279 |         this->positionBillboard(boundaryPosition, alpha); | 
|---|
| [8244] | 280 |     } | 
|---|
| [8767] | 281 |  | 
|---|
| [8404] | 282 |     void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt) | 
|---|
| [8244] | 283 |     { | 
|---|
| [9941] | 284 |         Vector3 normal = item->getWorldPosition() - this->getWorldPosition(); | 
|---|
| [8404] | 285 |         normal.normalise(); | 
|---|
 | 286 |         Vector3 velocity = item->getVelocity(); | 
|---|
 | 287 |         float normalSpeed = item->getVelocity().dotProduct(normal); | 
|---|
| [8767] | 288 |  | 
|---|
| [8614] | 289 |         /* Check, whether the Pawn would leave the boundary in the next tick, if so send it back. */ | 
|---|
 | 290 |         if( this->reaction_ == 0 && currentDistance + normalSpeed * dt > this->maxDistance_ - 10 ) // -10: "security measure" | 
|---|
| [8244] | 291 |         { | 
|---|
| [8614] | 292 |             bounceBack(item, &normal, &velocity); | 
|---|
 | 293 |         } else if (this->reaction_ == 2 && currentDistance - normalSpeed * dt < this->maxDistance_ + 10 ) // 10: "security measure" | 
|---|
 | 294 |         { | 
|---|
 | 295 |             normal = normal * (-1); | 
|---|
 | 296 |             bounceBack(item, &normal, &velocity); | 
|---|
| [8244] | 297 |         } | 
|---|
 | 298 |     } | 
|---|
| [8767] | 299 |  | 
|---|
| [8614] | 300 |     void SpaceBoundaries::bounceBack(Pawn *item, Vector3 *normal, Vector3 *velocity) | 
|---|
 | 301 |     { | 
|---|
 | 302 |         float dampingFactor = 0.5; | 
|---|
 | 303 |         *velocity = velocity->reflect(*normal); | 
|---|
 | 304 |         Vector3 acceleration = item->getAcceleration(); | 
|---|
 | 305 |         acceleration = acceleration.reflect(*normal); | 
|---|
| [8767] | 306 |  | 
|---|
| [9941] | 307 |         item->lookAt( *velocity + this->getWorldPosition() ); | 
|---|
| [8767] | 308 |  | 
|---|
| [8614] | 309 |         item->setAcceleration(acceleration * dampingFactor); | 
|---|
 | 310 |         item->setVelocity(*velocity * dampingFactor); | 
|---|
| [8767] | 311 |  | 
|---|
| [9941] | 312 |         item->setPosition( item->getWorldPosition() - *normal * 10 ); // Set the position of the Pawn to be well inside the boundary. | 
|---|
| [8614] | 313 |     } | 
|---|
| [8767] | 314 |  | 
|---|
| [8164] | 315 |     bool SpaceBoundaries::isHumanPlayer(Pawn *item) | 
|---|
 | 316 |     { | 
|---|
| [11071] | 317 |         if(item != nullptr) | 
|---|
| [8164] | 318 |         { | 
|---|
| [8201] | 319 |             if(item->getPlayer()) | 
|---|
 | 320 |             { | 
|---|
 | 321 |                 return item->getPlayer()->isHumanPlayer(); | 
|---|
 | 322 |             } | 
|---|
| [8164] | 323 |         } | 
|---|
| [8201] | 324 |         return false; | 
|---|
| [8164] | 325 |     } | 
|---|
| [8767] | 326 |  | 
|---|
| [8087] | 327 | } | 
|---|