[1694] | 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 | * Reto Grieder |
---|
| 24 | * Co-authors: |
---|
| 25 | * Fabian 'x3n' Landau |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "GSDedicated.h" |
---|
| 30 | |
---|
[3196] | 31 | #include "util/Debug.h" |
---|
| 32 | #include "util/Sleep.h" |
---|
[2896] | 33 | #include "core/Clock.h" |
---|
[1694] | 34 | #include "core/CommandLine.h" |
---|
[3198] | 35 | #include "core/CommandExecutor.h" |
---|
[2896] | 36 | #include "core/Game.h" |
---|
| 37 | #include "core/GameMode.h" |
---|
[1694] | 38 | #include "network/Server.h" |
---|
| 39 | |
---|
[3198] | 40 | #include <iostream> |
---|
| 41 | #include <iomanip> |
---|
| 42 | #include <boost/bind.hpp> |
---|
| 43 | |
---|
| 44 | #ifndef ORXONOX_PLATFORM_WINDOWS |
---|
| 45 | #include <termios.h> |
---|
| 46 | #endif |
---|
| 47 | |
---|
| 48 | |
---|
[1694] | 49 | namespace orxonox |
---|
| 50 | { |
---|
[3198] | 51 | const unsigned int MAX_COMMAND_LENGTH = 255; |
---|
| 52 | |
---|
[2896] | 53 | AddGameState(GSDedicated, "dedicated"); |
---|
[3198] | 54 | |
---|
| 55 | termios* GSDedicated::originalTerminalSettings_; |
---|
[2896] | 56 | |
---|
| 57 | GSDedicated::GSDedicated(const std::string& name) |
---|
| 58 | : GameState(name) |
---|
[1694] | 59 | , server_(0) |
---|
[2662] | 60 | , timeSinceLastUpdate_(0) |
---|
[3198] | 61 | , closeThread_(false) |
---|
[3205] | 62 | , cleanLine_(true) |
---|
[3198] | 63 | , inputIterator_(0) |
---|
| 64 | , cursorX_(0) |
---|
| 65 | , cursorY_(0) |
---|
[1694] | 66 | { |
---|
[3198] | 67 | this->commandLine_ = new unsigned char[MAX_COMMAND_LENGTH]; |
---|
| 68 | // memset( this->commandLine_, 0, MAX_COMMAND_LENGTH ); |
---|
[1694] | 69 | } |
---|
| 70 | |
---|
| 71 | GSDedicated::~GSDedicated() |
---|
| 72 | { |
---|
| 73 | } |
---|
| 74 | |
---|
[2896] | 75 | void GSDedicated::activate() |
---|
[1694] | 76 | { |
---|
[2896] | 77 | GameMode::setHasServer(true); |
---|
[3198] | 78 | |
---|
| 79 | this->inputThread_ = new boost::thread(boost::bind(&GSDedicated::inputThread, this)); |
---|
| 80 | |
---|
| 81 | #ifndef ORXONOX_PLATFORM_WINDOWS |
---|
| 82 | this->originalTerminalSettings_ = new termios; |
---|
| 83 | this->setTerminalMode(); |
---|
| 84 | #endif |
---|
[1696] | 85 | |
---|
[2171] | 86 | this->server_ = new Server(CommandLine::getValue("port")); |
---|
[2087] | 87 | COUT(0) << "Loading scene in server mode" << std::endl; |
---|
[1694] | 88 | |
---|
| 89 | server_->open(); |
---|
| 90 | } |
---|
| 91 | |
---|
[2896] | 92 | void GSDedicated::deactivate() |
---|
[1694] | 93 | { |
---|
| 94 | this->server_->close(); |
---|
[1755] | 95 | delete this->server_; |
---|
[3198] | 96 | |
---|
| 97 | closeThread_ = true; |
---|
| 98 | #ifndef ORXONOX_PLATFORM_WINDOWS |
---|
| 99 | std::cout << "\033[0G\033[K"; |
---|
| 100 | std::cout.flush(); |
---|
| 101 | resetTerminalMode(); |
---|
| 102 | delete this->originalTerminalSettings_; |
---|
| 103 | #endif |
---|
[3283] | 104 | COUT(0) << "Press enter to end the game..." << std::endl; |
---|
| 105 | inputThread_->join(); |
---|
[3240] | 106 | delete this->inputThread_; |
---|
[1694] | 107 | |
---|
[3283] | 108 | if (this->commandLine_ ) |
---|
| 109 | delete[] this->commandLine_; |
---|
| 110 | |
---|
[2896] | 111 | GameMode::setHasServer(false); |
---|
[1694] | 112 | } |
---|
| 113 | |
---|
[2896] | 114 | void GSDedicated::update(const Clock& time) |
---|
[1694] | 115 | { |
---|
[2662] | 116 | timeSinceLastUpdate_ += time.getDeltaTime(); |
---|
[3263] | 117 | //if (timeSinceLastUpdate_ >= NETWORK_PERIOD) |
---|
[2662] | 118 | { |
---|
| 119 | timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD; |
---|
[2896] | 120 | server_->update(time); |
---|
[2662] | 121 | } |
---|
[3263] | 122 | /*else |
---|
[2662] | 123 | { |
---|
[3205] | 124 | msleep(static_cast<unsigned int>((NETWORK_PERIOD - timeSinceLastUpdate_)*1000)); |
---|
| 125 | msleep(static_cast<unsigned int>(NETWORK_PERIOD*1000)); // NOTE: this is to throttle the non-network framerate |
---|
[2662] | 126 | // COUT(0) << "sleeping for " << (int)((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000) << " usec" << endl; |
---|
[3263] | 127 | }*/ |
---|
[3198] | 128 | processQueue(); |
---|
| 129 | printLine(); |
---|
[1694] | 130 | } |
---|
[3198] | 131 | |
---|
| 132 | void GSDedicated::inputThread() |
---|
| 133 | { |
---|
| 134 | unsigned char c; |
---|
| 135 | unsigned int escapeChar=0; |
---|
| 136 | while(!closeThread_) |
---|
| 137 | { |
---|
| 138 | c = getchar(); |
---|
| 139 | { |
---|
| 140 | // boost::recursive_mutex::scoped_lock(this->inputLineMutex_); |
---|
| 141 | if ( inputIterator_>=MAX_COMMAND_LENGTH-1 && c!='\n' ) |
---|
| 142 | continue; |
---|
| 143 | if( escapeChar > 0 ) |
---|
| 144 | { |
---|
| 145 | if( c == '[' ) |
---|
| 146 | { |
---|
| 147 | escapeChar = 2; |
---|
| 148 | continue; |
---|
| 149 | } |
---|
| 150 | else if ( escapeChar == 2 ) |
---|
| 151 | { |
---|
| 152 | switch (c) |
---|
| 153 | { |
---|
| 154 | case 'A': //keyup |
---|
| 155 | |
---|
| 156 | break; |
---|
| 157 | case 'B': //keydown |
---|
| 158 | |
---|
| 159 | break; |
---|
| 160 | case 'C': //keyright |
---|
| 161 | if(cursorX_<inputIterator_) |
---|
| 162 | ++cursorX_; |
---|
| 163 | break; |
---|
| 164 | case 'D': //keyleft |
---|
| 165 | if(cursorX_>0) |
---|
| 166 | --cursorX_; |
---|
| 167 | break; |
---|
| 168 | default: //not supported... |
---|
| 169 | // std::cout << endl << c << endl; |
---|
| 170 | break; |
---|
| 171 | } |
---|
| 172 | escapeChar = 0; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | else // not in escape sequence mode |
---|
| 176 | { |
---|
| 177 | switch (c) |
---|
| 178 | { |
---|
| 179 | case '\n': |
---|
| 180 | this->cleanLine_ = true; |
---|
| 181 | { |
---|
| 182 | boost::recursive_mutex::scoped_lock(this->inputQueueMutex_); |
---|
| 183 | boost::recursive_mutex::scoped_lock(this->inputLineMutex_); |
---|
| 184 | this->commandQueue_.push( std::string((const char*)this->commandLine_,inputIterator_) ); |
---|
| 185 | } |
---|
| 186 | memset( this->commandLine_, 0, inputIterator_ ); |
---|
| 187 | inputIterator_ = 0; |
---|
| 188 | this->cursorX_ = 0; |
---|
| 189 | this->cursorY_ = 0; |
---|
| 190 | std::cout << endl; |
---|
| 191 | break; |
---|
| 192 | case 127: // backspace |
---|
| 193 | case '\b': |
---|
| 194 | deleteCharacter( this->cursorX_ ); |
---|
| 195 | break; |
---|
| 196 | case '\t': |
---|
| 197 | { |
---|
| 198 | // boost::recursive_mutex::scoped_lock(this->inputLineMutex_); |
---|
| 199 | std::cout << endl << CommandExecutor::hint( std::string((const char*)this->commandLine_,inputIterator_) ) << endl; |
---|
| 200 | strncpy((char*)this->commandLine_, CommandExecutor::complete( std::string((const char*)this->commandLine_,inputIterator_) ).c_str(), MAX_COMMAND_LENGTH); |
---|
[3263] | 201 | this->inputIterator_ = strlen((const char*)this->commandLine_); |
---|
| 202 | this->cursorX_ = this->inputIterator_; |
---|
[3198] | 203 | break; |
---|
| 204 | } |
---|
| 205 | case '\033': // 1. escape character |
---|
| 206 | escapeChar = 1; |
---|
| 207 | break; |
---|
| 208 | default: |
---|
| 209 | insertCharacter( this->cursorX_, c ); |
---|
| 210 | break; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | void GSDedicated::printLine() |
---|
| 218 | { |
---|
| 219 | #ifndef ORXONOX_PLATFORM_WINDOWS |
---|
| 220 | // set cursor to the begining of the line and erase the line |
---|
| 221 | std::cout << "\033[0G\033[K"; |
---|
| 222 | // boost::recursive_mutex::scoped_lock(this->inputLineMutex_); |
---|
| 223 | // print status line |
---|
| 224 | std::cout << std::fixed << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgFPS() << " fps, " << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgTickTime() << " ms avg ticktime # "; |
---|
| 225 | //save cursor position |
---|
| 226 | std::cout << "\033[s"; |
---|
| 227 | //print commandLine buffer |
---|
| 228 | std::cout << std::string((const char*)this->commandLine_, inputIterator_); |
---|
| 229 | //restore cursor position and move it cursorX_ to the right |
---|
| 230 | std::cout << "\033[u"; |
---|
| 231 | if( this->cursorX_ > 0 ) |
---|
| 232 | std::cout << "\033[" << this->cursorX_ << "C"; |
---|
| 233 | std::cout.flush(); |
---|
| 234 | #endif |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | void GSDedicated::processQueue() |
---|
| 238 | { |
---|
| 239 | std::string tempstr; |
---|
| 240 | { |
---|
| 241 | boost::recursive_mutex::scoped_lock lock1(this->inputQueueMutex_); |
---|
| 242 | while(true) |
---|
| 243 | { |
---|
| 244 | if ( !this->commandQueue_.empty() ) |
---|
| 245 | { |
---|
| 246 | tempstr = this->commandQueue_.front(); |
---|
| 247 | this->commandQueue_.pop(); |
---|
| 248 | lock1.unlock(); |
---|
| 249 | } |
---|
| 250 | else |
---|
| 251 | break; |
---|
| 252 | CommandExecutor::execute(tempstr, true); |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | void GSDedicated::setTerminalMode() |
---|
| 258 | { |
---|
| 259 | #ifndef ORXONOX_PLATFORM_WINDOWS |
---|
| 260 | termios new_settings; |
---|
| 261 | |
---|
| 262 | tcgetattr(0,this->originalTerminalSettings_); |
---|
| 263 | new_settings = *this->originalTerminalSettings_; |
---|
| 264 | new_settings.c_lflag &= ~( ICANON | ECHO ); |
---|
| 265 | // new_settings.c_lflag |= ( ISIG | IEXTEN ); |
---|
| 266 | new_settings.c_cc[VTIME] = 0; |
---|
| 267 | new_settings.c_cc[VMIN] = 1; |
---|
| 268 | tcsetattr(0,TCSANOW,&new_settings); |
---|
| 269 | COUT(0) << endl; |
---|
| 270 | // atexit(&GSDedicated::resetTerminalMode); |
---|
| 271 | #endif |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | void GSDedicated::resetTerminalMode() |
---|
| 275 | { |
---|
| 276 | #ifndef ORXONOX_PLATFORM_WINDOWS |
---|
| 277 | tcsetattr(0, TCSANOW, GSDedicated::originalTerminalSettings_); |
---|
| 278 | #endif |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | void GSDedicated::insertCharacter( unsigned int position, char c ) |
---|
| 282 | { |
---|
| 283 | // std::cout << endl << (unsigned int)c << endl; |
---|
| 284 | // check that we do not exceed MAX_COMMAND_LENGTH |
---|
| 285 | if( inputIterator_+1 < MAX_COMMAND_LENGTH ) |
---|
| 286 | { |
---|
| 287 | // if cursor not at end of line then move the rest of the line |
---|
| 288 | if( position != this->inputIterator_ ) |
---|
| 289 | memmove( this->commandLine_+position+1, this->commandLine_+position, this->inputIterator_-position); |
---|
| 290 | // boost::recursive_mutex::scoped_lock(this->inputLineMutex_); |
---|
| 291 | this->commandLine_[position] = c; |
---|
| 292 | ++this->cursorX_; |
---|
| 293 | ++this->inputIterator_; |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | void GSDedicated::deleteCharacter( unsigned int position ) |
---|
| 297 | { |
---|
| 298 | // boost::recursive_mutex::scoped_lock(this->inputLineMutex_); |
---|
| 299 | if ( this->inputIterator_>0 && position>0 ) |
---|
| 300 | { |
---|
| 301 | if ( position != this->inputIterator_ ) |
---|
| 302 | memmove( this->commandLine_+position-1, this->commandLine_+position, this->inputIterator_-position); |
---|
| 303 | --this->cursorX_; |
---|
| 304 | --this->inputIterator_; |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
[1694] | 308 | } |
---|