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