TankScore.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 #include <tank/Tank.h>
00022 #include <tank/TankScore.h>
00023 #include <tank/TankState.h>
00024 #include <engine/ScorchedContext.h>
00025 #include <common/OptionsScorched.h>
00026 #include <common/Defines.h>
00027 #include <common/Logger.h>
00028 
00029 // The maximum amount of money allowed by anyone
00030 // Range limited to 0 -> maxMoney
00031 static const int maxMoney = 999999;
00032 
00033 TankScore::TankScore(ScorchedContext &context) : 
00034         context_(context), 
00035         totalMoneyEarned_(0), totalScoreEarned_(0),
00036         rank_(-1), tank_(0), skill_(1000), startSkill_(1000)
00037 {
00038         startTime_ = lastStatTime_ = time(0);
00039         newMatch();
00040 }
00041 
00042 TankScore::~TankScore()
00043 {
00044 
00045 }
00046 
00047 void TankScore::setSkill(int skill)
00048 {
00049         if (skill < 0) skill = 0;
00050         skill_ = skill;
00051         if (startSkill_ == 0) startSkill_ = skill;
00052 }
00053 
00054 void TankScore::newMatch()
00055 {
00056         resetTotalEarnedStats();
00057         money_ = 0;
00058         setMoney(context_.getOptionsGame().getStartMoney());
00059         wins_ = 0;
00060         kills_ = turnKills_ = 0;
00061         assists_ = 0;
00062         score_ = 0;
00063         missedMoves_ = 0;
00064 
00065         newGame();
00066 }
00067 
00068 void TankScore::newGame()
00069 {
00070         wonGame_ = false;
00071         hurtBy_.clear();
00072 }
00073 
00074 void TankScore::clientNewGame()
00075 {
00076         newGame();
00077 }
00078 
00079 void TankScore::setMoney(int money)
00080 {
00081         int oldMoney = money_;
00082         money_ = money;
00083         if (money_ > maxMoney) money_ = maxMoney;
00084         if (money_ < 0) money_ = 0;
00085 
00086         totalMoneyEarned_ += money_ - oldMoney;
00087 }
00088 
00089 void TankScore::setScore(int score)
00090 {
00091         int oldScore = score_;
00092         score_ = score;
00093         if (score_ < 0) score_ = 0;
00094 
00095         totalScoreEarned_ += score_ - oldScore;
00096 }
00097 
00098 const char *TankScore::getTimePlayedString()
00099 {
00100         static char timestr[256];
00101         time_t seconds = time(0) - startTime_;
00102         div_t playedTimeHr = div((int) seconds, 3600);
00103         div_t playedTime = div(playedTimeHr.rem, 60);
00104 
00105         snprintf(timestr, 256, "%i:%02i:%02i secs",
00106                 playedTimeHr.quot,
00107                 playedTime.quot,
00108                 playedTime.rem);
00109 
00110         return timestr;
00111 }
00112 
00113 const char *TankScore::getScoreString()
00114 {
00115         static char score[256];
00116         snprintf(score, 256, "%i (%i$ %iK %iA %iW %iL %iR)", 
00117                 getScore(), getMoney(), getKills(), getAssists(), 
00118                 getWins(), tank_->getState().getLives(),
00119                 getRank());
00120         return score;
00121 }
00122 
00123 bool TankScore::writeMessage(NetBuffer &buffer)
00124 {
00125         buffer.addToBuffer(kills_);
00126         buffer.addToBuffer(turnKills_);
00127         buffer.addToBuffer(assists_);
00128         buffer.addToBuffer(money_);
00129         buffer.addToBuffer(wins_);
00130         buffer.addToBuffer(score_);
00131         buffer.addToBuffer(rank_);
00132         buffer.addToBuffer(skill_);
00133         buffer.addToBuffer(startSkill_);
00134         buffer.addToBuffer((int) hurtBy_.size());
00135         std::set<unsigned int>::iterator itor;
00136         for (itor = hurtBy_.begin();
00137                 itor != hurtBy_.end();
00138                 itor++)
00139         {
00140                 buffer.addToBuffer(*itor);
00141         }
00142         return true;
00143 }
00144 
00145 bool TankScore::readMessage(NetBufferReader &reader)
00146 {
00147         if (!reader.getFromBuffer(kills_))
00148         {
00149                 Logger::log("TankScore::kills_ read failed");
00150                 return false;
00151         }
00152         if (!reader.getFromBuffer(turnKills_))
00153         {
00154                 Logger::log("TankScore::turnKills_ read failed");
00155                 return false;
00156         }
00157         if (!reader.getFromBuffer(assists_))
00158         {
00159                 Logger::log("TankScore::assists_ read failed");
00160                 return false;
00161         }
00162         if (!reader.getFromBuffer(money_))
00163         {
00164                 Logger::log("TankScore::money_ read failed");
00165                 return false;
00166         }
00167         if (!reader.getFromBuffer(wins_))
00168         {
00169                 Logger::log("TankScore::wins_ read failed");
00170                 return false;
00171         }
00172         if (!reader.getFromBuffer(score_))
00173         {
00174                 Logger::log("TankScore::score_ read failed");
00175                 return false;
00176         }
00177         if (!reader.getFromBuffer(rank_))
00178         {
00179                 Logger::log("TankScore::rank_ read failed");
00180                 return false;
00181         }
00182         if (!reader.getFromBuffer(skill_))
00183         {
00184                 Logger::log("TankScore::skill_ read failed");
00185                 return false;
00186         }
00187         if (!reader.getFromBuffer(startSkill_))
00188         {
00189                 Logger::log("TankScore::startSkill_ read failed");
00190                 return false;
00191         }
00192         int hb = 0;
00193         if (!reader.getFromBuffer(hb))
00194         {
00195                 Logger::log("TankScore::hb read failed");
00196                 return false;
00197         }
00198         hurtBy_.clear();
00199         for (int i=0; i<hb; i++)
00200         {
00201                 int hurtBy;
00202                 if (!reader.getFromBuffer(hurtBy))
00203                 {
00204                         Logger::log("TankScore::hurtBy_ read failed");
00205                         return false;
00206                 }
00207                 hurtBy_.insert(hurtBy);
00208         }
00209         return true;
00210 }
00211 
00212 time_t TankScore::getTimePlayedStat()
00213 {
00214         time_t val = lastStatTime_;
00215         lastStatTime_ = time(0);
00216         time_t res = lastStatTime_ - val;
00217         return res;
00218 }
00219 
00220 void TankScore::resetTotalEarnedStats()
00221 {
00222         totalScoreEarned_ = 0;
00223         totalMoneyEarned_ = 0;
00224 }

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