Subversion Repositories spk

Rev

Rev 242 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
// X3Data.cpp: implementation of the CX3Data class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
 
5
#include "stdafx.h"
6
#include "X3Data.h"
7
 
8
//#include "Common\CUtils.h"
9
#include <math.h>
10
 
11
//////////////////////////////////////////////////////////////////////
12
// Construction/Destruction
13
//////////////////////////////////////////////////////////////////////
14
 
242 cycrow 15
 
16
CX3Data::CX3Data(const Utils::WString &progDir)
1 cycrow 17
{
18
	m_sProgDir = progDir;
19
	m_iLastUpdate = 0;
20
	m_bLocked = false;
21
 
22
	Reset();
23
}
24
 
25
CX3Data::~CX3Data()
26
{
27
 
28
}
29
 
30
void CX3Data::Reset ()
31
{
242 cycrow 32
	m_sPilot = L"No Pilot Found";
33
	m_sGametime = L"0";
1 cycrow 34
 
35
	m_iShips = m_iAccount = m_iStations = m_iInsurance = 0;
36
	m_iHull = m_iShields = m_iLaser = m_iMaxLaser = m_iLaserProgress = 0;
37
	m_iMissiles = m_iEnemies = m_iEnemies2 = 0;
38
 
242 cycrow 39
	m_sAccount = L"0";
1 cycrow 40
	m_iVersion = 1;
41
 
42
	m_shipData.Reset();
43
	m_targetData.Reset();
44
 
242 cycrow 45
	_lCustomList.clear();
1 cycrow 46
 
47
	m_bLoaded = false;
48
 
49
	int i;
50
	for ( i = 0; i < MAXSHIPCLASS; i++ )
51
		m_iShipClass[i] = 0;
52
	for ( i = 0; i < MAXRACEREP; i++ )
53
		m_Rep[i].Reset();
54
}
55
 
242 cycrow 56
bool CX3Data::readFile(const Utils::WString &sLogfile, const Utils::WString &sLogNumber)
1 cycrow 57
{
242 cycrow 58
	Utils::WString logfile = sLogfile;
59
	Utils::WString lognumber = sLogNumber;
60
	lognumber.padNumber(5);
61
	if ( !logfile.empty() )
62
		logfile += L"/";
63
	logfile += L"log";
64
	logfile += lognumber;
65
	logfile += L".txt";
1 cycrow 66
 
242 cycrow 67
	logfile = logfile.findReplace(L"//", L"/");
68
	logfile = logfile.findReplace(L"/", L"\\");
1 cycrow 69
 
242 cycrow 70
	FILE *id = _wfopen(logfile.c_str(), L"r");
1 cycrow 71
	if ( !id )
72
		return false;
73
 
74
	bool ret = false;
75
 
242 cycrow 76
	_lCustomList.clear();
1 cycrow 77
 
78
	bool noUpdate = false;
79
	bool resetCustom = false;
80
 
242 cycrow 81
	Utils::WStringList custom;
1 cycrow 82
 
83
	while ( !feof(id) )
84
	{
242 cycrow 85
		Utils::WString line;
86
		line = GetEndOfLine ( id, NULL, false );
87
		Utils::WString arg = line.token(L":", 1 );
88
		Utils::WString rest = line.tokens(L":", 2);
89
		rest.removeFirstSpace();
90
		if ( arg.Compare(L"PILOT") )
1 cycrow 91
		{
92
			m_bLoaded = true;
93
			m_sPilot = rest;
94
			m_iMissiles = 0;
95
			m_iEnemies = 0;
96
		}
242 cycrow 97
		else if ( arg.Compare(L"Version") )
98
			m_iVersion = rest.toInt();
99
		else if ( arg.Compare(L"SHIPS") )
100
			m_iShips = rest.toInt();
101
		else if ( arg.Compare(L"STATIONS") )
102
			m_iStations = rest.toInt();
103
		else if ( arg.Compare(L"ACCOUNT") )
1 cycrow 104
		{
105
			m_sAccount = rest;
242 cycrow 106
			m_iAccount = m_sAccount.removeChar(L",;'").toInt();
1 cycrow 107
		}
242 cycrow 108
		else if ( arg.Compare(L"TIME") )
1 cycrow 109
		{
242 cycrow 110
			int time = rest.toInt();
1 cycrow 111
			if ( m_iLastUpdate != time )
112
			{
113
				ret = true;
114
				m_iLastUpdate = time;
115
				resetCustom = true;
116
			}
117
			else
118
				noUpdate = true;
242 cycrow 119
			m_sGametime.convertTime(rest.toLong(), true);
1 cycrow 120
		}
242 cycrow 121
		else if ( arg.Compare(L"ARGON") )
122
			setRaceRep(REP_ARGON, arg, rest, true);
123
		else if ( arg.Compare(L"BORON") )
124
			setRaceRep(REP_BORON, arg, rest, true);
125
		else if ( arg.Compare(L"SPLIT") )
126
			setRaceRep(REP_SPLIT, arg, rest, true);
127
		else if ( arg.Compare(L"PARANID") )
128
			setRaceRep(REP_PARANID, arg, rest, true);
129
		else if ( arg.Compare(L"TELADI") )
130
			setRaceRep(REP_TELADI, arg, rest, true);
131
		else if ( arg.Compare(L"GONER") )
132
			setRaceRep(REP_GONER, arg, rest, true);
133
		else if ( arg.Compare(L"TERRAN") )
134
			setRaceRep(REP_TERRAN, arg, rest, true);
135
		else if ( arg.Compare(L"ATF") )
136
			setRaceRep(REP_ATF, arg, rest, true);
137
		else if ( arg.Compare(L"RACEREP") ) // new race rep
138
			setRaceRep(rest.token(L":", 1).toInt() + 2, rest.token(L":", 2), rest.tokens(L":", 3));
139
		else if ( arg.Compare(L"COMBAT") )
140
			setRaceRep(REP_FIGHT, L"Combat", rest);
141
		else if ( arg.Compare(L"TRADE") )
142
			setRaceRep(REP_TRADE, L"Trade", rest);
143
		else if ( arg.Compare(L"SHIPCLASS") )
1 cycrow 144
		{
242 cycrow 145
			std::vector<Utils::WString> str;
146
			size_t max = rest.tokenise(L":", str);
1 cycrow 147
			if ( max > 12 ) max = 12;
251 cycrow 148
			for (size_t i = 0; i < max; i++ )
242 cycrow 149
				m_iShipClass[i] = str[i].toInt();
1 cycrow 150
		}
242 cycrow 151
		else if ( arg.Compare(L"SHIPNAME") )
1 cycrow 152
		{
242 cycrow 153
			m_shipData.sName = rest.token(L":", 1);
154
			m_shipData.sClass = rest.token(L":", 2);
155
			m_shipData.sID = rest.token(L":", 3);
156
			m_shipData.iSpeed = rest.token(L":", 4).toInt();
1 cycrow 157
		}
242 cycrow 158
		else if ( arg.Compare(L"SHIPENV") )
1 cycrow 159
			m_shipData.sEnv = rest;
242 cycrow 160
		else if ( arg.Compare(L"BARS") )
1 cycrow 161
		{
242 cycrow 162
			m_iHull = rest.token (L":", 1).toInt();
163
			m_iShields = rest.token (L":", 2).toInt();
164
			m_iLaser = rest.token (L":", 3).toInt();
165
			m_iMaxLaser = rest.token(L":", 4).toInt();
1 cycrow 166
			m_iLaserProgress = (int)(((float)m_iLaser / (float)m_iMaxLaser) * 100.0f);
167
		}
242 cycrow 168
		else if ( arg.Compare(L"TARGET") )
1 cycrow 169
		{
242 cycrow 170
			m_targetData.sName		= rest.token(L":", 1);
171
			m_targetData.iHull		= rest.token(L":", 2).toInt();
172
			m_targetData.iShields	= rest.token(L":", 3).toInt();
173
			m_targetData.iSpeed		= rest.token(L":", 4).toInt();
174
			m_targetData.sClass		= rest.token(L":", 5);
175
			m_targetData.sID		= rest.token(L":", 6);
176
			m_targetData.iDist		= rest.token(L":", 7).toInt();
177
			m_targetData.sRelation	= rest.token(L":", 8);
1 cycrow 178
		}
242 cycrow 179
		else if ( arg.Compare(L"MISSILES") )
180
			m_iMissiles = rest.toInt();
181
		else if ( arg.Compare(L"ENEMIES") )
182
			m_iEnemies = rest.toInt();
183
		else if ( arg.Compare(L"COMBAT") )
184
			m_iEnemies2 = rest.token(L":", 1).toInt();
185
		else if ( arg.Compare(L"CUSTOM") )
186
			custom.pushBackUnique(rest.token(L":", 1), rest.tokens(L":", 2));
187
		else if ( arg.Compare(L"CUSTOMDISPLAY") )
1 cycrow 188
		{
189
			if ( !noUpdate )
242 cycrow 190
				_lCustomFile.pushBack(rest.token(L"|", 1), rest.tokens(L"|", 2));
1 cycrow 191
		}
242 cycrow 192
		else if ( arg.Compare(L"INSURANCE") )
193
			m_iInsurance = rest.toInt();
1 cycrow 194
 
195
	}
196
	fclose ( id );
197
 
198
	DATA_LOCKED
242 cycrow 199
	if ( resetCustom || custom.size())
200
		_lCustomList.clear();
201
	for(auto itr = custom.begin(); itr != custom.end(); itr++)
202
		_lCustomList.pushBack((*itr)->str, (*itr)->data);
1 cycrow 203
	DATA_UNLOCKED
204
 
205
	if ( noUpdate ) return false;
206
 
207
	return ret;
208
}
209
 
242 cycrow 210
void CX3Data::setRaceRep(int rep, const Utils::WString &race, const Utils::WString &rest, bool old)
1 cycrow 211
{
212
	m_Rep[rep].bIncludeRace = true;
213
	m_Rep[rep].sRace = race;
214
	if ( old )
215
		m_Rep[rep].sRank = rest;
216
	else
217
	{
242 cycrow 218
		m_Rep[rep].iRep = rest.token(L":", 1).toInt();
219
		m_Rep[rep].iRepPct = rest.token(L":", 2).toInt();
1 cycrow 220
	}
221
}
222
 
242 cycrow 223
Utils::WString CX3Data::_getRepPercentage (const Utils::WString &rank ) const
1 cycrow 224
{
242 cycrow 225
	return rank.token(L":", 1);
1 cycrow 226
 
242 cycrow 227
	long amt = rank.token(L":", 2).toLong();
1 cycrow 228
	long upper = 0, lower = -1000000, r = 0;
229
	long checkamt = 0;
230
	bool breakout= false;
231
	while (1)
232
	{
233
		switch ( r )
234
		{
235
			case 0:
236
				checkamt = -10000;
237
				break;
238
			case 1:
239
				checkamt = -10000;
240
				break;
241
			case 2:
242
				checkamt = -1000;
243
				break;
244
			case 3:
245
				checkamt = -100;
246
				break;
247
			case 4:
248
				checkamt = -10;
249
				break;
250
			case 5:
251
				checkamt = 10;
252
				break;
253
			case 6:
254
				checkamt = 33;
255
				break;
256
			case 7:
257
				checkamt = 100;
258
				break;
259
			case 8:
260
				checkamt = 333;
261
				break;
262
			case 9:
263
				checkamt = 1000;
264
				break;
265
			case 10:
266
				checkamt = 3333;
267
				break;
268
			case 11:
269
				checkamt = 10000;
270
				break;
271
			case 12:
272
				checkamt = 33333;
273
				break;
274
			case 13:
275
				checkamt = 100000;
276
				break;
277
			case 14:
278
				checkamt = 333333;
279
				break;
280
			case 15:
281
				breakout = true;
282
				break;
283
		}
284
		lower = upper;
285
		upper = checkamt;
286
		if ( (amt < checkamt) || (breakout) )
287
			break;
288
		++r;
289
	}
290
 
291
	float a = (float)upper - lower;
292
	float b = (float)amt - lower;
293
 
294
	float p = ((b / a) * 100);
295
	int iP = (int)p;
242 cycrow 296
	Utils::WString fullRank = rank.token(L":", 1);
1 cycrow 297
	if ( !breakout )
298
	{
242 cycrow 299
		fullRank += L" (";
300
		fullRank += (long)iP;
301
		fullRank += L"%)";
1 cycrow 302
	}
303
	return fullRank;
304
}
305
 
242 cycrow 306
Utils::WString CX3Data::_getRankPercentage(const Utils::WString &rank) const
1 cycrow 307
{
242 cycrow 308
	long amt = rank.token(L":", 2).toLong();
1 cycrow 309
	long upper = 0, lower = 0, r = 0;
310
 
311
	long checkamt = 0;
312
	while (1)
313
	{
314
		switch ( r )
315
		{
316
			case 0:
317
				checkamt = 2;
318
				break;
319
			case 1:
320
				checkamt = 5;
321
				break;
322
			case 2:
323
				checkamt = 10;
324
				break;
325
			case 3:
326
				checkamt = 20;
327
				break;
328
			case 4:
329
				checkamt = 50;
330
				break;
331
			case 5:
332
				checkamt = 70;
333
				break;
334
			case 6:
335
				checkamt = 90;
336
				break;
337
			case 7:
338
				checkamt = 130;
339
				break;
340
			case 8:
341
				checkamt = 150;
342
				break;
343
			case 9:
344
				checkamt = 250;
345
				break;
346
			case 10:
347
				checkamt = 450;
348
				break;
349
			case 11:
350
				checkamt = 700;
351
				break;
352
			case 12:
353
				checkamt = 900;
354
				break;
355
			case 13:
356
				checkamt = 1000;
357
				break;
358
			case 14:
359
				checkamt = 1400;
360
				break;
361
			case 15:
362
				checkamt = 2000;
363
				break;
364
			case 16:
365
				checkamt = 2500;
366
				break;
367
			case 17:
368
				checkamt = 6000;
369
				break;
370
			case 18:
371
				checkamt = 9000;
372
				break;
373
			case 19:
374
				checkamt = 16000;
375
				break;
376
			case 20:
377
				checkamt = 25000;
378
				break;
379
			case 21:
380
				checkamt = 44000;
381
				break;
382
			case 22:
383
				checkamt = 60000;
384
				break;
385
			case 23:
386
				checkamt = 86000;
387
				break;
388
			case 24:
389
				checkamt = 140000;
390
				break;
391
			case 25:
392
				checkamt = 280000;
393
				break;
394
			case 26:
395
				checkamt = 420000;
396
				break;
397
			case 27:
398
				checkamt = 500000;
399
				break;
400
			case 28:
401
				checkamt = 700000;
402
				break;
403
			case 29:
404
				checkamt = 1000000;
405
				break;
406
			case 30:
407
				checkamt = -1;
408
				break;
409
		}
410
		lower = upper;
411
		upper = checkamt;
412
		if ( (amt < checkamt) || (r >= 30) )
413
			break;
414
		++r;
415
	}
416
 
417
//	--upper;
418
	float a = (float)upper - lower;
419
	float b = (float)amt - lower;
420
 
421
	float p = ((b / a) * 100);
422
	int iP = (int)p;
423
 
242 cycrow 424
	Utils::WString fullRank = rank.token(L":", 1);
1 cycrow 425
	if ( checkamt >= 0 )
426
	{
242 cycrow 427
		fullRank += L" (";
428
		fullRank += (long)iP;
429
		fullRank += L"%)";
1 cycrow 430
	}
431
	return fullRank;
432
}