| 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 |  *      Sven Stucki | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 |     @file Dock.cc | 
|---|
| 31 |     @brief Docking system main class | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "Dock.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "core/CoreIncludes.h" | 
|---|
| 37 | #include "core/LuaState.h" | 
|---|
| 38 | #include "core/GUIManager.h" | 
|---|
| 39 | #include "infos/HumanPlayer.h" | 
|---|
| 40 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 41 | #include "interfaces/PlayerTrigger.h" | 
|---|
| 42 | #include "core/command/ConsoleCommand.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include "ToluaBindDocking.h" | 
|---|
| 45 |  | 
|---|
| 46 | namespace orxonox | 
|---|
| 47 | { | 
|---|
| 48 |     // Register tolua_open function when loading the library | 
|---|
| 49 |     DeclareToluaInterface(Docking); | 
|---|
| 50 |  | 
|---|
| 51 |     CreateFactory(Dock); | 
|---|
| 52 |  | 
|---|
| 53 |     SetConsoleCommand("Dock", "dock",    &Dock::cmdDock).addShortcut().setAsInputCommand(); | 
|---|
| 54 |     SetConsoleCommand("Dock", "undock",  &Dock::cmdUndock).addShortcut().setAsInputCommand(); | 
|---|
| 55 |  | 
|---|
| 56 |     Dock::Dock(BaseObject* creator) : StaticEntity(creator) | 
|---|
| 57 |     { | 
|---|
| 58 |         RegisterObject(Dock); | 
|---|
| 59 |     } | 
|---|
| 60 |  | 
|---|
| 61 |     Dock::~Dock() | 
|---|
| 62 |     { | 
|---|
| 63 |     } | 
|---|
| 64 |  | 
|---|
| 65 |  | 
|---|
| 66 |     void Dock::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 67 |     { | 
|---|
| 68 |         SUPER(Dock, XMLPort, xmlelement, mode); | 
|---|
| 69 |  | 
|---|
| 70 |         XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode); | 
|---|
| 71 |         XMLPortObject(Dock, DockingAnimation, "animations", addAnimation, getAnimation, xmlelement, mode); | 
|---|
| 72 |         XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode); | 
|---|
| 73 |     } | 
|---|
| 74 |  | 
|---|
| 75 |     void Dock::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 76 |     { | 
|---|
| 77 |         SUPER(Dock, XMLEventPort, xmlelement, mode); | 
|---|
| 78 |  | 
|---|
| 79 |         XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode); | 
|---|
| 80 |     } | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 |     bool Dock::execute(bool bTriggered, BaseObject* trigger) | 
|---|
| 84 |     { | 
|---|
| 85 |         PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger); | 
|---|
| 86 |         Pawn* pawn = NULL; | 
|---|
| 87 |  | 
|---|
| 88 |         // Check whether it is a player trigger and extract pawn from it | 
|---|
| 89 |         if(pTrigger != NULL) | 
|---|
| 90 |         { | 
|---|
| 91 |             if(!pTrigger->isForPlayer()) {  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one. | 
|---|
| 92 |                 COUT(4) << "Docking:execute PlayerTrigger was not triggered by a player.." << std::endl; | 
|---|
| 93 |                 return false; | 
|---|
| 94 |             } | 
|---|
| 95 |             pawn = pTrigger->getTriggeringPlayer(); | 
|---|
| 96 |         } | 
|---|
| 97 |         else | 
|---|
| 98 |         { | 
|---|
| 99 |             COUT(4) << "Docking::execute Not a player trigger, can't extract pawn from it.." << std::endl; | 
|---|
| 100 |             return false; | 
|---|
| 101 |         } | 
|---|
| 102 |         if(pawn == NULL) | 
|---|
| 103 |         { | 
|---|
| 104 |             COUT(4) << "Docking::execute Can't retrieve Pawn from Trigger. (" << trigger->getIdentifier()->getName() << ")" << std::endl; | 
|---|
| 105 |             return false; | 
|---|
| 106 |         } | 
|---|
| 107 |  | 
|---|
| 108 |         // Extract the PlayerInfo from the Pawn. | 
|---|
| 109 |         PlayerInfo* player = pawn->getPlayer(); | 
|---|
| 110 |         if(player == NULL) | 
|---|
| 111 |         { | 
|---|
| 112 |             COUT(2) << "Docking::execute The PlayerInfo* is NULL." << std::endl; | 
|---|
| 113 |             return false; | 
|---|
| 114 |         } | 
|---|
| 115 |  | 
|---|
| 116 |         if(bTriggered) | 
|---|
| 117 |         { | 
|---|
| 118 |             // Add player to this Docks candidates | 
|---|
| 119 |             candidates.insert(player); | 
|---|
| 120 |  | 
|---|
| 121 |             // Show docking dialog | 
|---|
| 122 |             GUIManager::showGUI("DockingDialog"); | 
|---|
| 123 |         } | 
|---|
| 124 |         else | 
|---|
| 125 |         { | 
|---|
| 126 |             // Remove player from candidates list | 
|---|
| 127 |             candidates.erase(player); | 
|---|
| 128 |         } | 
|---|
| 129 |  | 
|---|
| 130 |         return true; | 
|---|
| 131 |     } | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 |     void Dock::cmdDock() | 
|---|
| 135 |     { | 
|---|
| 136 |         PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); | 
|---|
| 137 |         for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) | 
|---|
| 138 |         { | 
|---|
| 139 |             if(it->dock(player)) | 
|---|
| 140 |                 break; | 
|---|
| 141 |         } | 
|---|
| 142 |     } | 
|---|
| 143 |  | 
|---|
| 144 |     void Dock::cmdUndock() | 
|---|
| 145 |     { | 
|---|
| 146 |         PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); | 
|---|
| 147 |         for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) | 
|---|
| 148 |         { | 
|---|
| 149 |             if(it->undock(player)) | 
|---|
| 150 |                 break; | 
|---|
| 151 |         } | 
|---|
| 152 |     } | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 |     bool Dock::dock(PlayerInfo* player) | 
|---|
| 156 |     { | 
|---|
| 157 |         // Check if player is a candidate | 
|---|
| 158 |         if(candidates.find(player) == candidates.end()) | 
|---|
| 159 |         { | 
|---|
| 160 |             COUT(2) << "Dock::dock Player is not a candidate!" << std::endl; | 
|---|
| 161 |             return false; | 
|---|
| 162 |         } | 
|---|
| 163 |  | 
|---|
| 164 |         candidates.erase(player); | 
|---|
| 165 |         docked.insert(player); | 
|---|
| 166 |  | 
|---|
| 167 |         if (animations.empty()) | 
|---|
| 168 |             return dockingAnimationFinished(player); | 
|---|
| 169 |         else | 
|---|
| 170 |             DockingAnimation::invokeAnimation(true, player, animations); | 
|---|
| 171 |  | 
|---|
| 172 |         return true; | 
|---|
| 173 |     } | 
|---|
| 174 |  | 
|---|
| 175 |     bool Dock::dockingAnimationFinished(PlayerInfo* player) | 
|---|
| 176 |     { | 
|---|
| 177 |         if(docked.find(player) == docked.end()) | 
|---|
| 178 |         { | 
|---|
| 179 |             COUT(2) << "Dock::dockingAnimationFinished Player is not currently docked." << std::endl; | 
|---|
| 180 |             return false; | 
|---|
| 181 |         } | 
|---|
| 182 |  | 
|---|
| 183 |         DockingEffect::invokeEffect(true, player, effects); | 
|---|
| 184 |         return true; | 
|---|
| 185 |     } | 
|---|
| 186 |  | 
|---|
| 187 |     bool Dock::undock(PlayerInfo* player) | 
|---|
| 188 |     { | 
|---|
| 189 |         // Check if player is docked to this Dock | 
|---|
| 190 |         if(docked.find(player) == docked.end()) | 
|---|
| 191 |         { | 
|---|
| 192 |             COUT(2) << "Dock::undock Player is not docked to this Dock." << std::endl; | 
|---|
| 193 |             return false; | 
|---|
| 194 |         } | 
|---|
| 195 |  | 
|---|
| 196 |         docked.erase(player); | 
|---|
| 197 |         candidates.insert(player); | 
|---|
| 198 |  | 
|---|
| 199 |         DockingEffect::invokeEffect(false, player, effects); | 
|---|
| 200 |  | 
|---|
| 201 |         if (animations.empty()) | 
|---|
| 202 |             return undockingAnimationFinished(player); | 
|---|
| 203 |         else | 
|---|
| 204 |             DockingAnimation::invokeAnimation(false, player, animations); | 
|---|
| 205 |  | 
|---|
| 206 |         return true; | 
|---|
| 207 |     } | 
|---|
| 208 |  | 
|---|
| 209 |     bool Dock::undockingAnimationFinished(PlayerInfo* player) { | 
|---|
| 210 |         COUT(4) << "Dock::undockingAnimationFinished executed" << std::endl; | 
|---|
| 211 |         return true; | 
|---|
| 212 |     } | 
|---|
| 213 |  | 
|---|
| 214 |  | 
|---|
| 215 |     unsigned int Dock::getNumberOfActiveDocks() | 
|---|
| 216 |     { | 
|---|
| 217 |         int i = 0; | 
|---|
| 218 |         PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); | 
|---|
| 219 |         for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) | 
|---|
| 220 |         { | 
|---|
| 221 |             if(it->candidates.find(player) != it->candidates.end()) | 
|---|
| 222 |                 i++; | 
|---|
| 223 |         } | 
|---|
| 224 |         return i; | 
|---|
| 225 |     } | 
|---|
| 226 |  | 
|---|
| 227 |     Dock* Dock::getActiveDockAtIndex(unsigned int index) | 
|---|
| 228 |     { | 
|---|
| 229 |         PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); | 
|---|
| 230 |         for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) | 
|---|
| 231 |         { | 
|---|
| 232 |             if(it->candidates.find(player) != it->candidates.end()) | 
|---|
| 233 |             { | 
|---|
| 234 |                 if(index == 0) | 
|---|
| 235 |                     return *it; | 
|---|
| 236 |                 index--; | 
|---|
| 237 |             } | 
|---|
| 238 |         } | 
|---|
| 239 |         return NULL; | 
|---|
| 240 |     } | 
|---|
| 241 |  | 
|---|
| 242 |  | 
|---|
| 243 |     bool Dock::addEffect(DockingEffect* effect) | 
|---|
| 244 |     { | 
|---|
| 245 |         assert(effect); | 
|---|
| 246 |         effects.push_back(effect); | 
|---|
| 247 |         return true; | 
|---|
| 248 |     } | 
|---|
| 249 |  | 
|---|
| 250 |     const DockingEffect* Dock::getEffect(unsigned int i) const | 
|---|
| 251 |     { | 
|---|
| 252 |         for (std::list<DockingEffect*>::const_iterator effect = this->effects.begin(); effect != this->effects.end(); ++effect) | 
|---|
| 253 |         { | 
|---|
| 254 |             if(i == 0) | 
|---|
| 255 |                return *effect; | 
|---|
| 256 |             i--; | 
|---|
| 257 |         } | 
|---|
| 258 |         return NULL; | 
|---|
| 259 |     } | 
|---|
| 260 |  | 
|---|
| 261 |     bool Dock::addAnimation(DockingAnimation* animation) | 
|---|
| 262 |     { | 
|---|
| 263 |         assert(animation); | 
|---|
| 264 |         animation->setParent(this); | 
|---|
| 265 |         animations.push_back(animation); | 
|---|
| 266 |         return true; | 
|---|
| 267 |     } | 
|---|
| 268 |  | 
|---|
| 269 |     const DockingAnimation* Dock::getAnimation(unsigned int i) const | 
|---|
| 270 |     { | 
|---|
| 271 |         for (std::list<DockingAnimation*>::const_iterator animation = this->animations.begin(); animation != this->animations.end(); ++animation) | 
|---|
| 272 |         { | 
|---|
| 273 |             if(i == 0) | 
|---|
| 274 |                return *animation; | 
|---|
| 275 |             i--; | 
|---|
| 276 |         } | 
|---|
| 277 |         return NULL; | 
|---|
| 278 |     } | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|