[8137] | 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" |
---|
[8382] | 35 | |
---|
[8434] | 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/LuaState.h" |
---|
| 38 | #include "core/GUIManager.h" |
---|
[8192] | 39 | #include "infos/HumanPlayer.h" |
---|
[8186] | 40 | #include "worldentities/pawns/Pawn.h" |
---|
| 41 | #include "interfaces/PlayerTrigger.h" |
---|
[8382] | 42 | #include "core/command/ConsoleCommand.h" |
---|
[8137] | 43 | |
---|
[8434] | 44 | #include "ToluaBindDocking.h" |
---|
[8137] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
[8434] | 48 | // Register tolua_open function when loading the library |
---|
| 49 | DeclareToluaInterface(Docking); |
---|
| 50 | |
---|
[8185] | 51 | CreateFactory(Dock); |
---|
[8137] | 52 | |
---|
[8382] | 53 | SetConsoleCommand("Dock", "dock", &Dock::cmdDock).addShortcut().setAsInputCommand(); |
---|
| 54 | SetConsoleCommand("Dock", "undock", &Dock::cmdUndock).addShortcut().setAsInputCommand(); |
---|
| 55 | |
---|
[8137] | 56 | Dock::Dock(BaseObject* creator) : StaticEntity(creator) |
---|
| 57 | { |
---|
| 58 | RegisterObject(Dock); |
---|
[8185] | 59 | COUT(0) << "Registering dock..." << std::endl; |
---|
[8137] | 60 | } |
---|
| 61 | |
---|
| 62 | Dock::~Dock() |
---|
| 63 | { |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | void Dock::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 68 | { |
---|
| 69 | SUPER(Dock, XMLPort, xmlelement, mode); |
---|
| 70 | |
---|
[8151] | 71 | XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode); |
---|
[8137] | 72 | XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode); |
---|
| 73 | |
---|
| 74 | COUT(0) << "Dock created.." << std::endl; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void Dock::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 78 | { |
---|
| 79 | SUPER(Dock, XMLEventPort, xmlelement, mode); |
---|
| 80 | |
---|
| 81 | XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | bool Dock::execute(bool bTriggered, BaseObject* trigger) |
---|
[8185] | 86 | { |
---|
[8186] | 87 | PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger); |
---|
| 88 | Pawn* pawn = NULL; |
---|
| 89 | |
---|
[8188] | 90 | // Check whether it is a player trigger and extract pawn from it |
---|
[8186] | 91 | if(pTrigger != NULL) |
---|
| 92 | { |
---|
[8188] | 93 | if(!pTrigger->isForPlayer()) { // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one. |
---|
[8382] | 94 | COUT(2) << "Docking:execute PlayerTrigger was not triggered by a player.." << std::endl; |
---|
[8186] | 95 | return false; |
---|
[8188] | 96 | } |
---|
| 97 | pawn = pTrigger->getTriggeringPlayer(); |
---|
| 98 | } else { |
---|
[8382] | 99 | COUT(2) << "Docking::execute Not a player trigger, can't extract pawn from it.." << std::endl; |
---|
[8188] | 100 | return false; |
---|
[8186] | 101 | } |
---|
| 102 | if(pawn == NULL) |
---|
| 103 | { |
---|
[8382] | 104 | COUT(2) << "Docking::execute Can't retrieve Pawn from Trigger. (" << trigger->getIdentifier()->getName() << ")" << std::endl; |
---|
[8186] | 105 | return false; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // Extract the PlayerInfo from the Pawn. |
---|
| 109 | PlayerInfo* player = pawn->getPlayer(); |
---|
| 110 | if(player == NULL) |
---|
| 111 | { |
---|
[8382] | 112 | COUT(2) << "The PlayerInfo* is NULL." << std::endl; |
---|
[8186] | 113 | return false; |
---|
| 114 | } |
---|
| 115 | |
---|
[8257] | 116 | COUT(0) << "Dock triggered by player: " << player->getName() << ".." << std::endl; |
---|
[8188] | 117 | |
---|
[8434] | 118 | if(bTriggered) |
---|
| 119 | { |
---|
[8382] | 120 | // Add player to this Docks candidates |
---|
| 121 | candidates.insert(player); |
---|
| 122 | |
---|
[8434] | 123 | // Show docking dialog |
---|
| 124 | GUIManager::showGUI("DockingDialog"); |
---|
[8382] | 125 | //DockingEffect::invokeEffect(docking::DOCKING, player, effects_); |
---|
[8434] | 126 | } |
---|
| 127 | else |
---|
| 128 | { |
---|
[8382] | 129 | // Remove player from candidates list |
---|
| 130 | candidates.erase(player); |
---|
| 131 | |
---|
| 132 | //DockingEffect::invokeEffect(docking::RELEASE, player, effects_); |
---|
[8185] | 133 | } |
---|
| 134 | |
---|
| 135 | return true; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
[8434] | 139 | void Dock::cmdDock() |
---|
| 140 | { |
---|
[8382] | 141 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
[8434] | 142 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
| 143 | { |
---|
[8382] | 144 | if(it->dock(player)) |
---|
| 145 | break; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
[8434] | 149 | void Dock::cmdUndock() |
---|
| 150 | { |
---|
[8382] | 151 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
[8434] | 152 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
| 153 | { |
---|
[8382] | 154 | if(it->undock(player)) |
---|
| 155 | break; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
[8434] | 160 | bool Dock::dock(PlayerInfo* player) |
---|
| 161 | { |
---|
[8382] | 162 | // Check if player is a candidate |
---|
[8434] | 163 | if(candidates.find(player) == candidates.end()) |
---|
| 164 | { |
---|
| 165 | COUT(0) << "Player is not a candidate!" << std::endl; |
---|
[8382] | 166 | return false; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | // Remove player from candidates, add to docked players and invoke docking effect |
---|
| 170 | candidates.erase(player); |
---|
| 171 | docked.insert(player); |
---|
| 172 | DockingEffect::invokeEffect(docking::ATTACH, player, effects); |
---|
| 173 | return true; |
---|
| 174 | } |
---|
| 175 | |
---|
[8434] | 176 | bool Dock::undock(PlayerInfo* player) |
---|
| 177 | { |
---|
[8382] | 178 | // Check if player is docked to this Dock |
---|
[8434] | 179 | if(docked.find(player) == docked.end()) |
---|
| 180 | { |
---|
[8382] | 181 | COUT(0) << "Player is not docked to this Dock." << std::endl; |
---|
| 182 | return false; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | // Remove player from docked, add to candidates and reverse DockingEffect |
---|
| 186 | docked.erase(player); |
---|
| 187 | candidates.insert(player); |
---|
| 188 | DockingEffect::invokeEffect(docking::RELEASE, player, effects); |
---|
| 189 | return true; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
[8434] | 193 | unsigned int Dock::getNumberOfActiveDocks() |
---|
| 194 | { |
---|
| 195 | int i = 0; |
---|
| 196 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
| 197 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
| 198 | { |
---|
| 199 | if(it->candidates.find(player) != it->candidates.end()) |
---|
| 200 | i++; |
---|
| 201 | } |
---|
| 202 | return i; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | Dock* Dock::getActiveDockAtIndex(unsigned int index) |
---|
| 206 | { |
---|
| 207 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
| 208 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
| 209 | { |
---|
| 210 | if(it->candidates.find(player) != it->candidates.end()) |
---|
| 211 | { |
---|
| 212 | if(index == 0) |
---|
| 213 | return *it; |
---|
| 214 | index--; |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | return NULL; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | bool Dock::addEffect(DockingEffect* effect) |
---|
| 222 | { |
---|
[8185] | 223 | assert(effect); |
---|
[8382] | 224 | effects.push_back(effect); |
---|
[8185] | 225 | return true; |
---|
| 226 | } |
---|
| 227 | |
---|
[8434] | 228 | const DockingEffect* Dock::getEffect(unsigned int index) const |
---|
| 229 | { |
---|
[8185] | 230 | int i = index; |
---|
[8434] | 231 | for (std::list<DockingEffect*>::const_iterator effect = this->effects.begin(); effect != this->effects.end(); ++effect) |
---|
| 232 | { |
---|
[8151] | 233 | if(i == 0) |
---|
| 234 | return *effect; |
---|
| 235 | i--; |
---|
| 236 | } |
---|
| 237 | return NULL; |
---|
[8185] | 238 | } |
---|
[8137] | 239 | } |
---|