| 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 |  | 
|---|
| 31 | #include "worldentities/MobileEntity.h" | 
|---|
| 32 | #include "worldentities/ControllableEntity.h" | 
|---|
| 33 | #include "core/ObjectListIterator.h" | 
|---|
| 34 | #include "core/XMLPort.h" | 
|---|
| 35 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 36 | #include "infos/PlayerInfo.h" | 
|---|
| 37 | #include "interfaces/RadarViewable.h" | 
|---|
| 38 | #include "graphics/Billboard.h" | 
|---|
| 39 |  | 
|---|
| 40 | namespace orxonox | 
|---|
| 41 | { | 
|---|
| 42 |     CreateFactory(SpaceBoundaries); | 
|---|
| 43 |  | 
|---|
| 44 |     SpaceBoundaries::SpaceBoundaries(BaseObject* creator) : StaticEntity(creator) | 
|---|
| 45 |     { | 
|---|
| 46 |         /* Standardwerte, die zum Tragen kommen, | 
|---|
| 47 |          * falls im XML-File keine Werte spezifiziert wurden. */ | 
|---|
| 48 |         this->setMaxDistance(3000); | 
|---|
| 49 |         this->setWarnDistance(2000); | 
|---|
| 50 |         this->setShowDistance(2500); | 
|---|
| 51 |         this->setHealthDecrease(1); | 
|---|
| 52 |         this->setReaction(0); | 
|---|
| 53 |          | 
|---|
| 54 |         RegisterObject(SpaceBoundaries); | 
|---|
| 55 |          | 
|---|
| 56 |         // Show Boundaries on the radar. | 
|---|
| 57 |         this->centerRadar_ = new RadarViewable(this, this); | 
|---|
| 58 |         this->centerRadar_->setRadarObjectShape(RadarViewable::Dot); | 
|---|
| 59 |         this->centerRadar_->setRadarVisibility(false); | 
|---|
| 60 |     } | 
|---|
| 61 |     SpaceBoundaries::~SpaceBoundaries() | 
|---|
| 62 |     { | 
|---|
| 63 |         if (this->isInitialized()) | 
|---|
| 64 |         { | 
|---|
| 65 |             delete this->centerRadar_; | 
|---|
| 66 |          | 
|---|
| 67 |             this->pawnsIn_.clear(); | 
|---|
| 68 |          | 
|---|
| 69 |             for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++) | 
|---|
| 70 |             { | 
|---|
| 71 |                 if( current->billy != NULL) | 
|---|
| 72 |                 { | 
|---|
| 73 |                     delete current->billy; | 
|---|
| 74 |                 } | 
|---|
| 75 |             } | 
|---|
| 76 |             this->billboards_.clear(); | 
|---|
| 77 |         } | 
|---|
| 78 |     } | 
|---|
| 79 |      | 
|---|
| 80 |     void SpaceBoundaries::checkWhoIsIn() | 
|---|
| 81 |     { | 
|---|
| 82 |         pawnsIn_.clear(); | 
|---|
| 83 |         for(ObjectListIterator<Pawn> current = ObjectList<Pawn>::begin(); current != ObjectList<Pawn>::end(); ++current) | 
|---|
| 84 |         { | 
|---|
| 85 |             Pawn* currentPawn = *current; | 
|---|
| 86 |             float distance = this->computeDistance(currentPawn); | 
|---|
| 87 |             if(distance <= this->maxDistance_) | 
|---|
| 88 |             { | 
|---|
| 89 |                 pawnsIn_.push_back(currentPawn); | 
|---|
| 90 |             } | 
|---|
| 91 |         } | 
|---|
| 92 |     } | 
|---|
| 93 |      | 
|---|
| 94 |     void SpaceBoundaries::positionBillboard(const Vector3 position) | 
|---|
| 95 |     { | 
|---|
| 96 |         std::vector<billboardAdministration>::iterator current; | 
|---|
| 97 |         for( current = this->billboards_.begin(); current != this->billboards_.end(); current++) | 
|---|
| 98 |         { | 
|---|
| 99 |             if(!current->usedYet) | 
|---|
| 100 |             { | 
|---|
| 101 |                 break; | 
|---|
| 102 |             } | 
|---|
| 103 |         } | 
|---|
| 104 |         if( current == this->billboards_.end() ) | 
|---|
| 105 |         { | 
|---|
| 106 |             Billboard *tmp = new Billboard(this); | 
|---|
| 107 |             this->setBillboardOptions( tmp ); | 
|---|
| 108 |             tmp->setPosition(position); | 
|---|
| 109 |             billboardAdministration tmp2 = { true, tmp }; | 
|---|
| 110 |             this->billboards_.push_back( tmp2 ); | 
|---|
| 111 |              | 
|---|
| 112 |         } else { | 
|---|
| 113 |             current->billy->setPosition(position); | 
|---|
| 114 |             current->billy->setVisible(true); | 
|---|
| 115 |             current->usedYet = true; | 
|---|
| 116 |         } | 
|---|
| 117 |     } | 
|---|
| 118 |      | 
|---|
| 119 |     void SpaceBoundaries::setBillboardOptions(Billboard *billy) | 
|---|
| 120 |     { | 
|---|
| 121 |         if(billy != NULL) | 
|---|
| 122 |         { | 
|---|
| 123 |             billy->setMaterial("Shield"); | 
|---|
| 124 |             billy->setVisible(true); | 
|---|
| 125 |         } | 
|---|
| 126 |     } | 
|---|
| 127 |      | 
|---|
| 128 |     void SpaceBoundaries::removeAllBillboards() | 
|---|
| 129 |     { | 
|---|
| 130 |         for( std::vector<billboardAdministration>::iterator current = this->billboards_.begin(); current != this->billboards_.end(); current++ ) | 
|---|
| 131 |         { | 
|---|
| 132 |             current->usedYet = false; | 
|---|
| 133 |             current->billy->setVisible(false); | 
|---|
| 134 |         } | 
|---|
| 135 |     } | 
|---|
| 136 |      | 
|---|
| 137 |     void SpaceBoundaries::setMaxDistance(float r) | 
|---|
| 138 |     { | 
|---|
| 139 |         this->maxDistance_ = r; | 
|---|
| 140 |     } | 
|---|
| 141 |     float SpaceBoundaries::getMaxDistance() | 
|---|
| 142 |     { | 
|---|
| 143 |         return this->maxDistance_; | 
|---|
| 144 |     } | 
|---|
| 145 |      | 
|---|
| 146 |     void SpaceBoundaries::setWarnDistance(float r) | 
|---|
| 147 |     { | 
|---|
| 148 |         this->warnDistance_ = r; | 
|---|
| 149 |     } | 
|---|
| 150 |     float SpaceBoundaries::getWarnDistance() | 
|---|
| 151 |     { | 
|---|
| 152 |         return this->warnDistance_; | 
|---|
| 153 |     } | 
|---|
| 154 |      | 
|---|
| 155 |     void SpaceBoundaries::setShowDistance(float r) | 
|---|
| 156 |     { | 
|---|
| 157 |         this->showDistance_ = r; | 
|---|
| 158 |     } | 
|---|
| 159 |     float SpaceBoundaries::getShowDistance() | 
|---|
| 160 |     { | 
|---|
| 161 |         return this->showDistance_; | 
|---|
| 162 |     } | 
|---|
| 163 |      | 
|---|
| 164 |     void SpaceBoundaries::setHealthDecrease(float amount) | 
|---|
| 165 |     { | 
|---|
| 166 |         this->healthDecrease_ = amount/1000; | 
|---|
| 167 |     } | 
|---|
| 168 |     float SpaceBoundaries::getHealthDecrease() | 
|---|
| 169 |     { | 
|---|
| 170 |         return this->healthDecrease_; | 
|---|
| 171 |     } | 
|---|
| 172 |      | 
|---|
| 173 |     void SpaceBoundaries::setReaction(int mode) | 
|---|
| 174 |     { | 
|---|
| 175 |         this->reaction_ = mode; | 
|---|
| 176 |     } | 
|---|
| 177 |     int SpaceBoundaries::getReaction() | 
|---|
| 178 |     { | 
|---|
| 179 |         return this->reaction_; | 
|---|
| 180 |     } | 
|---|
| 181 |  | 
|---|
| 182 |     void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 183 |     { | 
|---|
| 184 |         SUPER(SpaceBoundaries, XMLPort, xmlelement, mode); | 
|---|
| 185 |  | 
|---|
| 186 |         XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode); | 
|---|
| 187 |         XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode); | 
|---|
| 188 |         XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode); | 
|---|
| 189 |         XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode); | 
|---|
| 190 |     } | 
|---|
| 191 |      | 
|---|
| 192 |     void SpaceBoundaries::tick(float dt) | 
|---|
| 193 |     { | 
|---|
| 194 |         this->checkWhoIsIn(); | 
|---|
| 195 |         this->removeAllBillboards(); | 
|---|
| 196 |         //COUT(0) << "Groesse der Liste: " << (int) pawnsIn_.size() << std::endl; | 
|---|
| 197 |          | 
|---|
| 198 |         float distance; | 
|---|
| 199 |         bool humanItem; | 
|---|
| 200 |         for( std::list<WeakPtr<Pawn> >::iterator current = pawnsIn_.begin(); current != pawnsIn_.end(); current++ ) | 
|---|
| 201 |         { | 
|---|
| 202 |             Pawn* currentPawn = current->get(); | 
|---|
| 203 |             if( currentPawn && currentPawn->getNode() )  | 
|---|
| 204 |             { | 
|---|
| 205 |                 distance = this->computeDistance(currentPawn); | 
|---|
| 206 |                 humanItem = this->isHumanPlayer(currentPawn); | 
|---|
| 207 |                 //COUT(0) << "Distanz:" << distance << std::endl; // message for debugging | 
|---|
| 208 |                 if(distance > this->warnDistance_ && distance < this->maxDistance_) // Zeige Warnung an! | 
|---|
| 209 |                 { | 
|---|
| 210 |                     //COUT(0) << "You are near by the boundaries!" << std::endl; // message for debugging | 
|---|
| 211 |                     if(humanItem) | 
|---|
| 212 |                     { | 
|---|
| 213 |                         //COUT(0) << "humanItem ist true" << std::endl; | 
|---|
| 214 |                         this->displayWarning("Attention! You are near by the boundaries!"); | 
|---|
| 215 |                     } | 
|---|
| 216 |                 } | 
|---|
| 217 |                 if( (this->maxDistance_ - distance) < this->showDistance_ ) | 
|---|
| 218 |                 { | 
|---|
| 219 |                     this->displayBoundaries(currentPawn); // Zeige Grenze an! | 
|---|
| 220 |                 } | 
|---|
| 221 |                 if(distance > this->maxDistance_ && (this->reaction_ == 1) ) | 
|---|
| 222 |                 { | 
|---|
| 223 |                     if( humanItem ) | 
|---|
| 224 |                     { | 
|---|
| 225 |                         //COUT(0) << "Health should be decreasing!" << std::endl; | 
|---|
| 226 |                         this->displayWarning("You are out of the area now!"); | 
|---|
| 227 |                     } | 
|---|
| 228 |                     currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_); | 
|---|
| 229 |                 } | 
|---|
| 230 |                 if( (this->reaction_ == 0) && (distance + 100 > this->maxDistance_)) // Annahme: Ein Pawn kann von einem Tick bis zum nächsten nicht mehr als 100 Distanzeinheiten zurücklegen. | 
|---|
| 231 |                 { | 
|---|
| 232 |                     this->conditionalBounceBack(currentPawn, distance, dt); | 
|---|
| 233 |                 } | 
|---|
| 234 |             } | 
|---|
| 235 |         } | 
|---|
| 236 |     } | 
|---|
| 237 |      | 
|---|
| 238 |     float SpaceBoundaries::computeDistance(WorldEntity *item) | 
|---|
| 239 |     { | 
|---|
| 240 |         if(item != NULL) | 
|---|
| 241 |         { | 
|---|
| 242 |             Vector3 itemPosition = item->getPosition(); | 
|---|
| 243 |             return (itemPosition.distance(this->getPosition())); | 
|---|
| 244 |         } else { | 
|---|
| 245 |             return -1; | 
|---|
| 246 |         } | 
|---|
| 247 |     } | 
|---|
| 248 |      | 
|---|
| 249 |     void SpaceBoundaries::displayWarning(const std::string warnText) | 
|---|
| 250 |     {    | 
|---|
| 251 |          | 
|---|
| 252 |     } | 
|---|
| 253 |      | 
|---|
| 254 |     void SpaceBoundaries::displayBoundaries(Pawn *item) | 
|---|
| 255 |     { | 
|---|
| 256 |          | 
|---|
| 257 |         Vector3 direction = item->getPosition() - this->getPosition(); | 
|---|
| 258 |         direction.normalise(); | 
|---|
| 259 |          | 
|---|
| 260 |         Vector3 boundaryPosition = this->getPosition() + direction * this->maxDistance_; | 
|---|
| 261 |          | 
|---|
| 262 |         this->positionBillboard(boundaryPosition); | 
|---|
| 263 |     } | 
|---|
| 264 |      | 
|---|
| 265 |     void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt) | 
|---|
| 266 |     { | 
|---|
| 267 |         Vector3 normal = item->getPosition() - this->getPosition(); | 
|---|
| 268 |         normal.normalise(); | 
|---|
| 269 |         Vector3 velocity = item->getVelocity(); | 
|---|
| 270 |         float normalSpeed = item->getVelocity().dotProduct(normal); | 
|---|
| 271 |          | 
|---|
| 272 |         /* Checke, ob das Pawn innerhalb des nächsten Ticks, das erlaubte Gebiet verlassen würde. | 
|---|
| 273 |            Falls ja: Spicke es zurück. */ | 
|---|
| 274 |         if( currentDistance + normalSpeed * dt > this->maxDistance_ ) | 
|---|
| 275 |         { | 
|---|
| 276 |             float dampingFactor = 0.5; | 
|---|
| 277 |             velocity = velocity.reflect(normal); | 
|---|
| 278 |             Vector3 acceleration = item->getAcceleration(); | 
|---|
| 279 |             acceleration = acceleration.reflect(normal); | 
|---|
| 280 |              | 
|---|
| 281 |             item->lookAt( velocity + this->getPosition() ); | 
|---|
| 282 |              | 
|---|
| 283 |             item->setAcceleration(acceleration * dampingFactor); | 
|---|
| 284 |             item->setVelocity(velocity * dampingFactor); | 
|---|
| 285 |         } | 
|---|
| 286 |     } | 
|---|
| 287 |      | 
|---|
| 288 |     bool SpaceBoundaries::isHumanPlayer(Pawn *item) | 
|---|
| 289 |     { | 
|---|
| 290 |         if(item != NULL) | 
|---|
| 291 |         { | 
|---|
| 292 |             if(item->getPlayer()) | 
|---|
| 293 |             { | 
|---|
| 294 |                 return item->getPlayer()->isHumanPlayer(); | 
|---|
| 295 |             } | 
|---|
| 296 |         } | 
|---|
| 297 |         return false; | 
|---|
| 298 |     } | 
|---|
| 299 |      | 
|---|
| 300 | } | 
|---|