00001 //////////////////////////////////////////////////////////////////////////////// 00002 // Scorched3D (c) 2000-2009 00003 // 00004 // This file is part of Scorched3D. 00005 // 00006 // Scorched3D is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // Scorched3D is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with Scorched3D; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 //////////////////////////////////////////////////////////////////////////////// 00020 00021 #include <tank/TankDeadContainer.h> 00022 #include <tank/TankState.h> 00023 #include <tank/TankScore.h> 00024 #include <tank/TankAccessories.h> 00025 #include <net/NetBuffer.h> 00026 #include <server/ScorchedServer.h> 00027 #include <common/OptionsScorched.h> 00028 #include <common/Logger.h> 00029 00030 TankDeadContainer::TankDeadContainer() 00031 { 00032 00033 } 00034 00035 TankDeadContainer::~TankDeadContainer() 00036 { 00037 00038 } 00039 00040 void TankDeadContainer::addTank(Tank *tank) 00041 { 00042 // Check if we should store residual players 00043 if (!ScorchedServer::instance()->getOptionsGame().getResidualPlayers() || 00044 tank->getState().getState() == TankState::sPending || 00045 tank->getState().getState() == TankState::sLoading || 00046 tank->getState().getState() == TankState::sInitializing || 00047 !tank->getUniqueId()[0] || 00048 tank->getDestinationId() == 0) return; 00049 00050 // Find/create buffer 00051 NetBuffer *buffer = 0; 00052 std::map<std::string, NetBuffer *>::iterator finditor = 00053 deadTanks_.find(tank->getUniqueId()); 00054 if (finditor != deadTanks_.end()) 00055 { 00056 buffer = finditor->second; 00057 } 00058 else 00059 { 00060 buffer = new NetBuffer(); 00061 } 00062 buffer->reset(); 00063 00064 // Write to buffer 00065 if (!tank->getAccessories().writeMessage(*buffer, true) || 00066 !tank->getScore().writeMessage(*buffer)) 00067 { 00068 Logger::log("ERROR: Failed to update residual player info (write)"); 00069 return; 00070 } 00071 00072 // Save buffer 00073 deadTanks_[tank->getUniqueId()] = buffer; 00074 } 00075 00076 bool TankDeadContainer::getTank(Tank *tank) 00077 { 00078 // Get the buffer 00079 std::map<std::string, NetBuffer *>::iterator finditor = 00080 deadTanks_.find(tank->getUniqueId()); 00081 if (finditor == deadTanks_.end()) return false; 00082 NetBuffer *buffer = finditor->second; 00083 00084 // Read from the buffer 00085 NetBufferReader reader(*buffer); 00086 if (!tank->getAccessories().readMessage(reader) || 00087 !tank->getScore().readMessage(reader)) 00088 { 00089 Logger::log("ERROR: Failed to update residual player info (read)"); 00090 return false; 00091 } 00092 00093 // Don't get credited for the new game stats 00094 tank->getScore().resetTotalEarnedStats(); 00095 00096 // Tidy 00097 delete buffer; 00098 deadTanks_.erase(finditor); 00099 00100 return true; 00101 } 00102 00103 void TankDeadContainer::clearTanks() 00104 { 00105 std::map<std::string, NetBuffer *>::iterator itor; 00106 for (itor = deadTanks_.begin(); 00107 itor != deadTanks_.end(); 00108 itor++) 00109 { 00110 NetBuffer *current = (*itor).second; 00111 delete current; 00112 } 00113 deadTanks_.clear(); 00114 } 00115
1.5.3