00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00030
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 }