ComsPlayerStateMessage.cpp

Go to the documentation of this file.
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 #ifndef S3D_SERVER
00022 #include <client/ScorchedClient.h>
00023 #endif
00024 #include <server/ScorchedServer.h>
00025 #include <coms/ComsPlayerStateMessage.h>
00026 #include <tank/TankTeamScore.h>
00027 #include <tank/TankContainer.h>
00028 #include <tank/TankState.h>
00029 #include <target/TargetState.h>
00030 #include <movement/TargetMovement.h>
00031 #include <tankai/TankAIAdder.h>
00032 #include <common/Logger.h>
00033 #include <set>
00034 
00035 ComsPlayerStateMessage::ComsPlayerStateMessage(bool targets, bool accessories) : 
00036         ComsMessage("ComsPlayerStateMessage"),
00037         targets_(targets), accessories_(accessories)
00038 {
00039 
00040 }
00041 
00042 ComsPlayerStateMessage::~ComsPlayerStateMessage()
00043 {
00044 
00045 }
00046 
00047 bool ComsPlayerStateMessage::writeMessage(NetBuffer &buffer)
00048 {
00049         buffer.addToBuffer(targets_);
00050 
00051         // Team score
00052         if (!ScorchedServer::instance()->getContext().getTankTeamScore().
00053                 writeMessage(buffer)) return false;
00054 
00055         // Tanks
00056         std::map<unsigned int, Tank *> &tanks =
00057                 ScorchedServer::instance()->getTankContainer().getPlayingTanks();
00058         std::map<unsigned int, Tank *>::iterator itor;
00059         buffer.addToBuffer((int) tanks.size());
00060         for (itor = tanks.begin();
00061                 itor != tanks.end();
00062                 itor++)
00063         {
00064                 // Add each tank
00065                 Tank *tank = (*itor).second;
00066                 buffer.addToBuffer(tank->getPlayerId());
00067                 if (!tank->writeMessage(buffer, accessories_)) return false;
00068         }
00069 
00070         if (targets_)
00071         {
00072                 // Target movement
00073                 if (!ScorchedServer::instance()->getTargetMovement().writeMessage(buffer)) return false;
00074 
00075                 // Get list of targets to send
00076                 std::map<unsigned int, Target *> targets;
00077                 {
00078                         std::map<unsigned int, Target *> &possibletargets =
00079                                 ScorchedServer::instance()->getTargetContainer().getTargets();
00080                         std::map<unsigned int, Target *>::iterator itor;
00081                         for (itor = possibletargets.begin();
00082                                 itor != possibletargets.end();
00083                                 itor++)
00084                         {
00085                                 Target *target = (*itor).second;
00086                                 if (target->isTarget())
00087                                 {
00088                                         // send the state of targets than can move
00089                                         if (target->getTargetState().getMovement())
00090                                         {
00091                                                 targets[(*itor).first] = (*itor).second;
00092                                         }
00093                                 }
00094                         }
00095                 }
00096 
00097                 // Send targets
00098                 buffer.addToBuffer((int) targets.size());
00099                 std::map<unsigned int, Target *>::iterator itor;
00100                 for (itor = targets.begin();
00101                         itor != targets.end();
00102                         itor++)
00103                 {
00104                         // Add each tank
00105                         Target *target = (*itor).second;
00106                         buffer.addToBuffer(target->getPlayerId());
00107 
00108                         if (!target->writeMessage(buffer)) return false;
00109                 }
00110         }
00111 
00112         return true;
00113 }
00114 
00115 bool ComsPlayerStateMessage::readMessage(NetBufferReader &reader)
00116 {
00117 #ifndef S3D_SERVER
00118         if (!reader.getFromBuffer(targets_)) return false;
00119 
00120         if (!ScorchedClient::instance()->getContext().getTankTeamScore().
00121                 readMessage(reader)) return false;
00122 
00123         // Update all targets with the state from the targets on the
00124         // server
00125         int count = 0;
00126         if (!reader.getFromBuffer(count)) return false;
00127         for (int i=0; i<count; i++)
00128         {
00129                 unsigned int playerId;
00130                 if (!reader.getFromBuffer(playerId)) return false;
00131                 Tank *tank = ScorchedClient::instance()->getTankContainer().
00132                         getTankById(playerId);
00133                 if (tank)
00134                 {
00135                         if (!tank->readMessage(reader)) return false;
00136                 }
00137                 else
00138                 {
00139                         std::string name;
00140                         reader.getFromBuffer(name);
00141                         Logger::log(S3D::formatStringBuffer("Error: Failed to find tank %u\"%s\"",
00142                                 playerId, name.c_str()));
00143 
00144                         std::map<unsigned int, Tank *> &tanks = 
00145                                 ScorchedClient::instance()->getTankContainer().getAllTanks();
00146                         std::map<unsigned int, Tank *>::iterator itor;
00147                         for (itor = tanks.begin();
00148                                 itor != tanks.end();
00149                                 itor++)
00150                         {
00151                                 Tank *tank = itor->second;
00152                                 Logger::log(S3D::formatStringBuffer("  Possible tank %u\"%s\"",
00153                                         tank->getPlayerId(), tank->getCStrName().c_str()));
00154                         }
00155 
00156                         return false;
00157                 }
00158         }
00159 
00160         if (targets_)
00161         {
00162                 // Get all of the movement information
00163                 if (!ScorchedClient::instance()->getTargetMovement().readMessage(reader)) return false;
00164 
00165                 int count = 0;
00166                 if (!reader.getFromBuffer(count)) return false;
00167                 for (int i=0; i<count; i++)
00168                 {
00169                         unsigned int playerId;
00170                         if (!reader.getFromBuffer(playerId)) return false;
00171                         Target *target = ScorchedClient::instance()->getTargetContainer().
00172                                 getTargetById(playerId);
00173                         if (target)
00174                         {
00175                                 if (!target->readMessage(reader)) return false;
00176                         }
00177                         else
00178                         {
00179                                 std::string name;
00180                                 reader.getFromBuffer(name);
00181                                 Logger::log(S3D::formatStringBuffer("Error: Failed to find target %u\"%s\"",
00182                                         playerId, name.c_str()));
00183                                 return false;
00184                         }
00185                 }
00186         }
00187 #endif
00188         
00189         return true;
00190 }

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3