Rev 242 | Blame | Compare with Previous | Last modification | View Log | RSS feed
// X3Data.cpp: implementation of the CX3Data class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "X3Data.h"
//#include "Common\CUtils.h"
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CX3Data::CX3Data(const Utils::WString &progDir)
{
m_sProgDir = progDir;
m_iLastUpdate = 0;
m_bLocked = false;
Reset();
}
CX3Data::~CX3Data()
{
}
void CX3Data::Reset ()
{
m_sPilot = L"No Pilot Found";
m_sGametime = L"0";
m_iShips = m_iAccount = m_iStations = m_iInsurance = 0;
m_iHull = m_iShields = m_iLaser = m_iMaxLaser = m_iLaserProgress = 0;
m_iMissiles = m_iEnemies = m_iEnemies2 = 0;
m_sAccount = L"0";
m_iVersion = 1;
m_shipData.Reset();
m_targetData.Reset();
_lCustomList.clear();
m_bLoaded = false;
int i;
for ( i = 0; i < MAXSHIPCLASS; i++ )
m_iShipClass[i] = 0;
for ( i = 0; i < MAXRACEREP; i++ )
m_Rep[i].Reset();
}
bool CX3Data::readFile(const Utils::WString &sLogfile, const Utils::WString &sLogNumber)
{
Utils::WString logfile = sLogfile;
Utils::WString lognumber = sLogNumber;
lognumber.padNumber(5);
if ( !logfile.empty() )
logfile += L"/";
logfile += L"log";
logfile += lognumber;
logfile += L".txt";
logfile = logfile.findReplace(L"//", L"/");
logfile = logfile.findReplace(L"/", L"\\");
FILE *id = _wfopen(logfile.c_str(), L"r");
if ( !id )
return false;
bool ret = false;
_lCustomList.clear();
bool noUpdate = false;
bool resetCustom = false;
Utils::WStringList custom;
while ( !feof(id) )
{
Utils::WString line;
line = GetEndOfLine ( id, NULL, false );
Utils::WString arg = line.token(L":", 1 );
Utils::WString rest = line.tokens(L":", 2);
rest.removeFirstSpace();
if ( arg.Compare(L"PILOT") )
{
m_bLoaded = true;
m_sPilot = rest;
m_iMissiles = 0;
m_iEnemies = 0;
}
else if ( arg.Compare(L"Version") )
m_iVersion = rest.toInt();
else if ( arg.Compare(L"SHIPS") )
m_iShips = rest.toInt();
else if ( arg.Compare(L"STATIONS") )
m_iStations = rest.toInt();
else if ( arg.Compare(L"ACCOUNT") )
{
m_sAccount = rest;
m_iAccount = m_sAccount.removeChar(L",;'").toInt();
}
else if ( arg.Compare(L"TIME") )
{
int time = rest.toInt();
if ( m_iLastUpdate != time )
{
ret = true;
m_iLastUpdate = time;
resetCustom = true;
}
else
noUpdate = true;
m_sGametime.convertTime(rest.toLong(), true);
}
else if ( arg.Compare(L"ARGON") )
setRaceRep(REP_ARGON, arg, rest, true);
else if ( arg.Compare(L"BORON") )
setRaceRep(REP_BORON, arg, rest, true);
else if ( arg.Compare(L"SPLIT") )
setRaceRep(REP_SPLIT, arg, rest, true);
else if ( arg.Compare(L"PARANID") )
setRaceRep(REP_PARANID, arg, rest, true);
else if ( arg.Compare(L"TELADI") )
setRaceRep(REP_TELADI, arg, rest, true);
else if ( arg.Compare(L"GONER") )
setRaceRep(REP_GONER, arg, rest, true);
else if ( arg.Compare(L"TERRAN") )
setRaceRep(REP_TERRAN, arg, rest, true);
else if ( arg.Compare(L"ATF") )
setRaceRep(REP_ATF, arg, rest, true);
else if ( arg.Compare(L"RACEREP") ) // new race rep
setRaceRep(rest.token(L":", 1).toInt() + 2, rest.token(L":", 2), rest.tokens(L":", 3));
else if ( arg.Compare(L"COMBAT") )
setRaceRep(REP_FIGHT, L"Combat", rest);
else if ( arg.Compare(L"TRADE") )
setRaceRep(REP_TRADE, L"Trade", rest);
else if ( arg.Compare(L"SHIPCLASS") )
{
std::vector<Utils::WString> str;
size_t max = rest.tokenise(L":", str);
if ( max > 12 ) max = 12;
for (size_t i = 0; i < max; i++ )
m_iShipClass[i] = str[i].toInt();
}
else if ( arg.Compare(L"SHIPNAME") )
{
m_shipData.sName = rest.token(L":", 1);
m_shipData.sClass = rest.token(L":", 2);
m_shipData.sID = rest.token(L":", 3);
m_shipData.iSpeed = rest.token(L":", 4).toInt();
}
else if ( arg.Compare(L"SHIPENV") )
m_shipData.sEnv = rest;
else if ( arg.Compare(L"BARS") )
{
m_iHull = rest.token (L":", 1).toInt();
m_iShields = rest.token (L":", 2).toInt();
m_iLaser = rest.token (L":", 3).toInt();
m_iMaxLaser = rest.token(L":", 4).toInt();
m_iLaserProgress = (int)(((float)m_iLaser / (float)m_iMaxLaser) * 100.0f);
}
else if ( arg.Compare(L"TARGET") )
{
m_targetData.sName = rest.token(L":", 1);
m_targetData.iHull = rest.token(L":", 2).toInt();
m_targetData.iShields = rest.token(L":", 3).toInt();
m_targetData.iSpeed = rest.token(L":", 4).toInt();
m_targetData.sClass = rest.token(L":", 5);
m_targetData.sID = rest.token(L":", 6);
m_targetData.iDist = rest.token(L":", 7).toInt();
m_targetData.sRelation = rest.token(L":", 8);
}
else if ( arg.Compare(L"MISSILES") )
m_iMissiles = rest.toInt();
else if ( arg.Compare(L"ENEMIES") )
m_iEnemies = rest.toInt();
else if ( arg.Compare(L"COMBAT") )
m_iEnemies2 = rest.token(L":", 1).toInt();
else if ( arg.Compare(L"CUSTOM") )
custom.pushBackUnique(rest.token(L":", 1), rest.tokens(L":", 2));
else if ( arg.Compare(L"CUSTOMDISPLAY") )
{
if ( !noUpdate )
_lCustomFile.pushBack(rest.token(L"|", 1), rest.tokens(L"|", 2));
}
else if ( arg.Compare(L"INSURANCE") )
m_iInsurance = rest.toInt();
}
fclose ( id );
DATA_LOCKED
if ( resetCustom || custom.size())
_lCustomList.clear();
for(auto itr = custom.begin(); itr != custom.end(); itr++)
_lCustomList.pushBack((*itr)->str, (*itr)->data);
DATA_UNLOCKED
if ( noUpdate ) return false;
return ret;
}
void CX3Data::setRaceRep(int rep, const Utils::WString &race, const Utils::WString &rest, bool old)
{
m_Rep[rep].bIncludeRace = true;
m_Rep[rep].sRace = race;
if ( old )
m_Rep[rep].sRank = rest;
else
{
m_Rep[rep].iRep = rest.token(L":", 1).toInt();
m_Rep[rep].iRepPct = rest.token(L":", 2).toInt();
}
}
Utils::WString CX3Data::_getRepPercentage (const Utils::WString &rank ) const
{
return rank.token(L":", 1);
long amt = rank.token(L":", 2).toLong();
long upper = 0, lower = -1000000, r = 0;
long checkamt = 0;
bool breakout= false;
while (1)
{
switch ( r )
{
case 0:
checkamt = -10000;
break;
case 1:
checkamt = -10000;
break;
case 2:
checkamt = -1000;
break;
case 3:
checkamt = -100;
break;
case 4:
checkamt = -10;
break;
case 5:
checkamt = 10;
break;
case 6:
checkamt = 33;
break;
case 7:
checkamt = 100;
break;
case 8:
checkamt = 333;
break;
case 9:
checkamt = 1000;
break;
case 10:
checkamt = 3333;
break;
case 11:
checkamt = 10000;
break;
case 12:
checkamt = 33333;
break;
case 13:
checkamt = 100000;
break;
case 14:
checkamt = 333333;
break;
case 15:
breakout = true;
break;
}
lower = upper;
upper = checkamt;
if ( (amt < checkamt) || (breakout) )
break;
++r;
}
float a = (float)upper - lower;
float b = (float)amt - lower;
float p = ((b / a) * 100);
int iP = (int)p;
Utils::WString fullRank = rank.token(L":", 1);
if ( !breakout )
{
fullRank += L" (";
fullRank += (long)iP;
fullRank += L"%)";
}
return fullRank;
}
Utils::WString CX3Data::_getRankPercentage(const Utils::WString &rank) const
{
long amt = rank.token(L":", 2).toLong();
long upper = 0, lower = 0, r = 0;
long checkamt = 0;
while (1)
{
switch ( r )
{
case 0:
checkamt = 2;
break;
case 1:
checkamt = 5;
break;
case 2:
checkamt = 10;
break;
case 3:
checkamt = 20;
break;
case 4:
checkamt = 50;
break;
case 5:
checkamt = 70;
break;
case 6:
checkamt = 90;
break;
case 7:
checkamt = 130;
break;
case 8:
checkamt = 150;
break;
case 9:
checkamt = 250;
break;
case 10:
checkamt = 450;
break;
case 11:
checkamt = 700;
break;
case 12:
checkamt = 900;
break;
case 13:
checkamt = 1000;
break;
case 14:
checkamt = 1400;
break;
case 15:
checkamt = 2000;
break;
case 16:
checkamt = 2500;
break;
case 17:
checkamt = 6000;
break;
case 18:
checkamt = 9000;
break;
case 19:
checkamt = 16000;
break;
case 20:
checkamt = 25000;
break;
case 21:
checkamt = 44000;
break;
case 22:
checkamt = 60000;
break;
case 23:
checkamt = 86000;
break;
case 24:
checkamt = 140000;
break;
case 25:
checkamt = 280000;
break;
case 26:
checkamt = 420000;
break;
case 27:
checkamt = 500000;
break;
case 28:
checkamt = 700000;
break;
case 29:
checkamt = 1000000;
break;
case 30:
checkamt = -1;
break;
}
lower = upper;
upper = checkamt;
if ( (amt < checkamt) || (r >= 30) )
break;
++r;
}
// --upper;
float a = (float)upper - lower;
float b = (float)amt - lower;
float p = ((b / a) * 100);
int iP = (int)p;
Utils::WString fullRank = rank.token(L":", 1);
if ( checkamt >= 0 )
{
fullRank += L" (";
fullRank += (long)iP;
fullRank += L"%)";
}
return fullRank;
}