| [1505] | 1 | /* | 
|---|
 | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  *                    > www.orxonox.net < | 
|---|
| [1502] | 4 |  * | 
|---|
| [1505] | 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 |  * | 
|---|
| [1502] | 22 |  *   Author: | 
|---|
 | 23 |  *      Yuning Chai | 
|---|
| [1614] | 24 |  *      Felix Schulthess | 
|---|
| [1502] | 25 |  *   Co-authors: | 
|---|
| [1614] | 26 |  *      Reto Grieder | 
|---|
| [9939] | 27 |  *      Wolfgang Roenninger | 
|---|
| [1502] | 28 |  * | 
|---|
 | 29 |  */ | 
|---|
| [1283] | 30 |  | 
|---|
| [1604] | 31 | #include "HUDRadar.h" | 
|---|
| [1283] | 32 |  | 
|---|
| [1502] | 33 | #include <OgreOverlayManager.h> | 
|---|
| [1614] | 34 | #include <OgrePanelOverlayElement.h> | 
|---|
| [1502] | 35 |  | 
|---|
| [1614] | 36 | #include "util/Math.h" | 
|---|
| [3280] | 37 | #include "util/StringUtils.h" | 
|---|
| [1616] | 38 | #include "core/CoreIncludes.h" | 
|---|
 | 39 | #include "core/XMLPort.h" | 
|---|
| [3196] | 40 | #include "tools/TextureGenerator.h" | 
|---|
| [7880] | 41 | #include "worldentities/ControllableEntity.h" | 
|---|
| [7163] | 42 | #include "Scene.h" | 
|---|
 | 43 | #include "Radar.h" | 
|---|
| [9939] | 44 | #include "core/config/ConfigValueIncludes.h" | 
|---|
| [1502] | 45 |  | 
|---|
| [1283] | 46 | namespace orxonox | 
|---|
 | 47 | { | 
|---|
| [9667] | 48 |     RegisterClass(HUDRadar); | 
|---|
| [1604] | 49 |  | 
|---|
| [9667] | 50 |     HUDRadar::HUDRadar(Context* context) | 
|---|
 | 51 |         : OrxonoxOverlay(context) | 
|---|
| [1604] | 52 |     { | 
|---|
 | 53 |         RegisterObject(HUDRadar); | 
|---|
| [9939] | 54 |         this->setConfigValues(); | 
|---|
| [2087] | 55 |  | 
|---|
| [2662] | 56 |         this->marker_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() | 
|---|
| [2087] | 57 |             .createOverlayElement("Panel", "HUDRadar_marker_" + getUniqueNumberString())); | 
|---|
| [2662] | 58 |         this->marker_->setMaterialName("Orxonox/RadarMarker"); | 
|---|
 | 59 |         this->overlay_->add2D(this->marker_); | 
|---|
 | 60 |         this->marker_->hide(); | 
|---|
| [2087] | 61 |  | 
|---|
| [2662] | 62 |         this->setRadarSensitivity(1.0f); | 
|---|
 | 63 |         this->setHalfDotSizeDistance(3000.0f); | 
|---|
 | 64 |         this->setMaximumDotSize(0.1f); | 
|---|
| [9939] | 65 |         this->setMaximumDotSize3D(0.07f); | 
|---|
| [2087] | 66 |  | 
|---|
| [11071] | 67 |         this->shapeMaterials_[RadarViewable::Shape::Dot]      = "RadarDot.png"; | 
|---|
 | 68 |         this->shapeMaterials_[RadarViewable::Shape::Triangle] = "RadarTriangle.png"; | 
|---|
 | 69 |         this->shapeMaterials_[RadarViewable::Shape::Square]   = "RadarSquare.png"; | 
|---|
 | 70 |         this->owner_ = nullptr; | 
|---|
| [9939] | 71 |  | 
|---|
 | 72 |         this->map3DFront_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() | 
|---|
| [9945] | 73 |             .createOverlayElement("Panel", "HUDRadar_mapDreiDFront_" + getUniqueNumberString())); | 
|---|
| [9939] | 74 |         this->map3DFront_->setMaterialName("Orxonox/Radar3DFront"); | 
|---|
 | 75 |         this->overlay_->add2D(this->map3DFront_); | 
|---|
 | 76 |         this->map3DFront_->hide(); | 
|---|
 | 77 |  | 
|---|
 | 78 |         this->map3DBack_ = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() | 
|---|
| [9945] | 79 |             .createOverlayElement("Panel", "HUDRadar_mapDreiDBack_" + getUniqueNumberString())); | 
|---|
| [9939] | 80 |         this->map3DBack_->setMaterialName("Orxonox/Radar3DBack"); | 
|---|
 | 81 |         this->overlay_->add2D(this->map3DBack_); | 
|---|
 | 82 |         this->map3DBack_->hide(); | 
|---|
 | 83 |  | 
|---|
| [1302] | 84 |     } | 
|---|
| [1283] | 85 |  | 
|---|
| [1604] | 86 |     HUDRadar::~HUDRadar() | 
|---|
 | 87 |     { | 
|---|
| [2087] | 88 |         if (this->isInitialized()) | 
|---|
 | 89 |         { | 
|---|
| [1615] | 90 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->marker_); | 
|---|
| [9939] | 91 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->map3DFront_); | 
|---|
 | 92 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->map3DBack_); | 
|---|
 | 93 |  | 
|---|
| [11071] | 94 |             for (const auto& mapEntry : this->radarObjects_) | 
|---|
| [2087] | 95 |             { | 
|---|
| [11071] | 96 |                 Ogre::OverlayManager::getSingleton().destroyOverlayElement(mapEntry.second); | 
|---|
| [2087] | 97 |             } | 
|---|
| [1604] | 98 |         } | 
|---|
| [1302] | 99 |     } | 
|---|
| [1283] | 100 |  | 
|---|
| [9939] | 101 |  | 
|---|
 | 102 |  | 
|---|
 | 103 |     void HUDRadar::setConfigValues() | 
|---|
 | 104 |        { | 
|---|
 | 105 |            SetConfigValue(RadarMode_, true); | 
|---|
 | 106 |        } | 
|---|
 | 107 |  | 
|---|
| [7401] | 108 |     void HUDRadar::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| [1604] | 109 |     { | 
|---|
| [7401] | 110 |         SUPER(HUDRadar, XMLPort, xmlelement, mode); | 
|---|
| [1283] | 111 |  | 
|---|
| [7401] | 112 |         XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlelement, mode); | 
|---|
 | 113 |         XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlelement, mode); | 
|---|
 | 114 |         XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlelement, mode); | 
|---|
| [9939] | 115 |         XMLPortParam(HUDRadar, "maximumDotSize3D", setMaximumDotSize3D, getMaximumDotSize3D, xmlelement, mode); | 
|---|
 | 116 |         XMLPortParam(HUDRadar, "material2D", set2DMaterial, get2DMaterial, xmlelement, mode); | 
|---|
 | 117 |         XMLPortParam(HUDRadar, "material3DMiddle", set3DMaterial, get3DMaterial, xmlelement, mode); | 
|---|
 | 118 |         XMLPortParam(HUDRadar, "material3DFront", set3DMaterialFront, get3DMaterialFront, xmlelement, mode); | 
|---|
 | 119 |         XMLPortParam(HUDRadar, "material3DBack", set3DMaterialBack, get3DMaterialBack, xmlelement, mode); | 
|---|
 | 120 |         XMLPortParam(HUDRadar, "mapAngle3D", setMapAngle, getMapAngle, xmlelement, mode); | 
|---|
 | 121 |         XMLPortParam(HUDRadar, "detectionLimit", setDetectionLimit, getDetectionLimit, xmlelement, mode); | 
|---|
| [1609] | 122 |     } | 
|---|
| [1302] | 123 |  | 
|---|
| [7163] | 124 |     void HUDRadar::addObject(RadarViewable* object) | 
|---|
| [1604] | 125 |     { | 
|---|
| [9348] | 126 |         if (object == orxonox_cast<RadarViewable*>(this->owner_)) | 
|---|
| [2662] | 127 |             return; | 
|---|
| [8891] | 128 |         if( showObject(object) == false ) //do not show objects that are "invisible" or "radar invisible" | 
|---|
 | 129 |             return; | 
|---|
| [2662] | 130 |  | 
|---|
| [7163] | 131 |         // Make sure the object hasn't been added yet | 
|---|
 | 132 |         assert( this->radarObjects_.find(object) == this->radarObjects_.end() ); | 
|---|
| [1604] | 133 |  | 
|---|
| [7163] | 134 |         // Create everything needed to display the object on the radar and add it to the map | 
|---|
 | 135 |         Ogre::PanelOverlayElement* panel; | 
|---|
 | 136 |         panel = static_cast<Ogre::PanelOverlayElement*>( | 
|---|
 | 137 |             Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberString())); | 
|---|
 | 138 |         this->overlay_->add2D(panel); | 
|---|
 | 139 |         // get right material | 
|---|
 | 140 |         panel->setMaterialName(TextureGenerator::getMaterialName( | 
|---|
 | 141 |             shapeMaterials_[object->getRadarObjectShape()], object->getRadarObjectColour())); | 
|---|
| [8738] | 142 |         panel->hide(); | 
|---|
| [7163] | 143 |         this->radarObjects_[object] = panel; | 
|---|
 | 144 |     } | 
|---|
 | 145 |  | 
|---|
 | 146 |     void HUDRadar::removeObject(RadarViewable* object) | 
|---|
 | 147 |     { | 
|---|
 | 148 |         // If object was added at all then remove it | 
|---|
 | 149 |         std::map<RadarViewable*,Ogre::PanelOverlayElement*>::iterator it; | 
|---|
 | 150 |         it = this->radarObjects_.find( object ); | 
|---|
 | 151 |         if( it != this->radarObjects_.end() ) | 
|---|
| [1609] | 152 |         { | 
|---|
| [7163] | 153 |             Ogre::OverlayManager::getSingleton().destroyOverlayElement(it->second); | 
|---|
 | 154 |             this->radarObjects_.erase(it); | 
|---|
| [1609] | 155 |         } | 
|---|
| [7163] | 156 |     } | 
|---|
| [2662] | 157 |  | 
|---|
| [7163] | 158 |     void HUDRadar::objectChanged( RadarViewable* rv ) | 
|---|
| [8891] | 159 |     {// The new implementation behaves more precisely, since inactive RadarViewables are not displayed anymore. | 
|---|
 | 160 |         this->removeObject(rv); | 
|---|
 | 161 |         this->addObject(rv); | 
|---|
| [7163] | 162 |     } | 
|---|
 | 163 |  | 
|---|
 | 164 |     void HUDRadar::gatherObjects() | 
|---|
 | 165 |     { | 
|---|
 | 166 |         const std::set<RadarViewable*>& objectSet = this->getCreator()->getScene()->getRadar()->getRadarObjects(); | 
|---|
| [11071] | 167 |         for( RadarViewable* viewable : objectSet ) | 
|---|
 | 168 |             this->addObject(viewable); | 
|---|
| [8737] | 169 |         this->radarTick(0); | 
|---|
| [7163] | 170 |     } | 
|---|
 | 171 |  | 
|---|
 | 172 |     void HUDRadar::radarTick(float dt) | 
|---|
 | 173 |     { | 
|---|
 | 174 |         // Make sure the owner of the radar was defined | 
|---|
 | 175 |         if( !this->owner_ ) | 
|---|
| [7880] | 176 |             return; | 
|---|
| [7163] | 177 |  | 
|---|
 | 178 |         this->marker_->hide();      // in case that no object is in focus | 
|---|
 | 179 |         // get the focus object | 
|---|
 | 180 |         Radar* radar = this->getOwner()->getScene()->getRadar(); | 
|---|
 | 181 |         const RadarViewable* focusObject = radar->getFocus(); | 
|---|
 | 182 |  | 
|---|
 | 183 |         // update the distances for all objects | 
|---|
| [9939] | 184 |  | 
|---|
 | 185 |         if(RadarMode_) | 
|---|
 | 186 |         { | 
|---|
| [9945] | 187 |             this->setBackgroundMaterial(material3D_); | 
|---|
 | 188 |             this->map3DFront_->_notifyZOrder(this->overlay_->getZOrder() * 100 + 250); // it seems that the ZOrder of overlayelements is 100 times the ZOrder of the overlay | 
|---|
 | 189 |             this->map3DBack_->_notifyZOrder(this->overlay_->getZOrder() * 100 - 250); // 250 a little bit buffer so that the two shels are displayed all in the front / in the back | 
|---|
 | 190 |             this->map3DFront_->show(); | 
|---|
 | 191 |             this->map3DBack_->show(); | 
|---|
| [9939] | 192 |         } | 
|---|
 | 193 |         else | 
|---|
 | 194 |         { | 
|---|
| [9945] | 195 |             this->setBackgroundMaterial(material2D_); | 
|---|
 | 196 |             this->map3DFront_->hide(); | 
|---|
 | 197 |             this->map3DBack_->hide(); | 
|---|
| [9939] | 198 |         } | 
|---|
 | 199 |  | 
|---|
| [11071] | 200 |         for( const auto& mapEntry : this->radarObjects_ ) | 
|---|
| [1614] | 201 |         { | 
|---|
| [7163] | 202 |             // Make sure the object really is a WorldEntity | 
|---|
| [11071] | 203 |             const WorldEntity* wePointer = mapEntry.first->getWorldEntity(); | 
|---|
| [7163] | 204 |             if( !wePointer ) | 
|---|
 | 205 |             { | 
|---|
| [8858] | 206 |                 orxout(internal_error) << "Cannot display a non-WorldEntitiy on the radar" << endl; | 
|---|
| [7163] | 207 |                 assert(0); | 
|---|
 | 208 |             } | 
|---|
| [11071] | 209 |             bool isFocus = (mapEntry.first == focusObject); | 
|---|
| [7163] | 210 |             // set size to fit distance... | 
|---|
 | 211 |             float distance = (wePointer->getWorldPosition() - this->owner_->getPosition()).length(); | 
|---|
 | 212 |             // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda) | 
|---|
| [9939] | 213 |  | 
|---|
 | 214 |             float size; | 
|---|
 | 215 |             if(RadarMode_) | 
|---|
| [11071] | 216 |                 size = maximumDotSize3D_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance) * mapEntry.first->getRadarObjectScale(); | 
|---|
| [9939] | 217 |             else | 
|---|
| [11071] | 218 |                 size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance) * mapEntry.first->getRadarObjectScale(); | 
|---|
 | 219 |             mapEntry.second->setDimensions(size, size); | 
|---|
| [2662] | 220 |  | 
|---|
| [7163] | 221 |             // calc position on radar... | 
|---|
| [9939] | 222 |             Vector2 coord; | 
|---|
 | 223 |  | 
|---|
 | 224 |             if(RadarMode_) | 
|---|
 | 225 |             { | 
|---|
| [9945] | 226 |                 coord = get3DProjection(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition(), 0.6435011, detectionLimit_); | 
|---|
| [9939] | 227 |  | 
|---|
| [9945] | 228 |                 // set zOrder on screen | 
|---|
 | 229 |                 bool overXZPlain = isObjectHigherThanShipOnMap(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition(), this->mapAngle_); | 
|---|
| [9939] | 230 |  | 
|---|
| [9945] | 231 |                 int zOrder = determineMap3DZOrder(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition(), detectionLimit_); | 
|---|
 | 232 |                 if(overXZPlain == false /*&& (it->second->getZOrder() >  100 * this->overlay_->getZOrder())*/) // it appears that zOrder of attached Overlayelements is 100 times the zOrder of the Overlay | 
|---|
| [11071] | 233 |                     mapEntry.second->_notifyZOrder(this->overlay_->getZOrder() * 100 - 70 + zOrder); | 
|---|
| [9945] | 234 |                 if(overXZPlain == true /*&& (it->second->getZOrder() <= 100 * this->overlay_->getZOrder())*/) | 
|---|
| [11071] | 235 |                     mapEntry.second->_notifyZOrder(this->overlay_->getZOrder() * 100 + 70 + zOrder); | 
|---|
| [9939] | 236 |             } | 
|---|
 | 237 |             else | 
|---|
| [11052] | 238 |                 coord = get2DViewCoordinates(this->owner_->getPosition(), this->owner_->getOrientation() * WorldEntity::FRONT, this->owner_->getOrientation() * WorldEntity::UP, wePointer->getWorldPosition()); | 
|---|
| [9939] | 239 |  | 
|---|
| [7184] | 240 |             coord *= math::pi / 3.5f; // small adjustment to make it fit the texture | 
|---|
| [11071] | 241 |             mapEntry.second->setPosition((1.0f + coord.x - size) * 0.5f, (1.0f - coord.y - size) * 0.5f); | 
|---|
| [9939] | 242 |  | 
|---|
| [8891] | 243 |             if( distance < detectionLimit_ || detectionLimit_ < 0 ) | 
|---|
| [11071] | 244 |                 mapEntry.second->show(); | 
|---|
| [8891] | 245 |             else | 
|---|
| [11071] | 246 |                 mapEntry.second->hide(); | 
|---|
| [1613] | 247 |  | 
|---|
| [7163] | 248 |             // if this object is in focus, then set the focus marker | 
|---|
 | 249 |             if (isFocus) | 
|---|
 | 250 |             { | 
|---|
 | 251 |                 this->marker_->setDimensions(size * 1.5f, size * 1.5f); | 
|---|
 | 252 |                 this->marker_->setPosition((1.0f + coord.x - size * 1.5f) * 0.5f, (1.0f - coord.y - size * 1.5f) * 0.5f); | 
|---|
| [9939] | 253 |                 if(RadarMode_) | 
|---|
| [11071] | 254 |                     this->marker_->_notifyZOrder(mapEntry.second->getZOrder() -1); | 
|---|
| [7163] | 255 |                 this->marker_->show(); | 
|---|
 | 256 |             } | 
|---|
| [1613] | 257 |         } | 
|---|
| [1604] | 258 |     } | 
|---|
 | 259 |  | 
|---|
| [8891] | 260 |     bool HUDRadar::showObject(RadarViewable* rv) | 
|---|
 | 261 |     { | 
|---|
| [9348] | 262 |         if ( rv == orxonox_cast<RadarViewable*> ( this->getOwner() ) ) | 
|---|
| [8891] | 263 |             return false; | 
|---|
 | 264 |         assert( rv->getWorldEntity() ); | 
|---|
 | 265 |         if ( rv->getWorldEntity()->isVisible()==false || rv->getRadarVisibility()==false ) | 
|---|
 | 266 |             return false; | 
|---|
 | 267 |         return true; | 
|---|
 | 268 |     } | 
|---|
 | 269 |  | 
|---|
 | 270 |  | 
|---|
| [2662] | 271 |     void HUDRadar::changedOwner() | 
|---|
 | 272 |     { | 
|---|
| [7880] | 273 |         SUPER(HUDRadar, changedOwner); | 
|---|
| [2662] | 274 |  | 
|---|
| [7880] | 275 |         this->owner_ = orxonox_cast<ControllableEntity*>(this->getOwner()); | 
|---|
 | 276 |         assert(this->radarObjects_.size() == 0); | 
|---|
 | 277 |         this->gatherObjects(); | 
|---|
 | 278 |     } | 
|---|
| [1283] | 279 | } | 
|---|