File: raceresults.cpp

package info (click to toggle)
torcs 1.3.3%2Bdfsg-0.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 680,248 kB
  • ctags: 14,190
  • sloc: cpp: 81,697; xml: 75,140; ansic: 9,746; makefile: 2,275; sh: 1,134
file content (404 lines) | stat: -rw-r--r-- 13,988 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/***************************************************************************

    file        : raceresults.cpp
    created     : Thu Jan  2 12:43:10 CET 2003
    copyright   : (C) 2002 by Eric Espi�                        
    email       : eric.espie@torcs.org   
    version     : $Id: raceresults.cpp,v 1.9.2.6 2012/02/16 16:13:12 berniw Exp $                                  

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/** @file   
    		
    @author	<a href=mailto:eric.espie@torcs.org>Eric Espie</a>
    @version	$Id: raceresults.cpp,v 1.9.2.6 2012/02/16 16:13:12 berniw Exp $
*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <tgfclient.h>
#include <robot.h>
#include <raceman.h>
#include <racescreens.h>
#include <robottools.h>
#include <portability.h>

#include "racemain.h"
#include "racegl.h"
#include "raceinit.h"
#include "raceengine.h"

#include "raceresults.h"

typedef struct
{
	const char *carName;
	char	*modName;
	int		drvIdx;
	int		points;
} tReStandings;

void
ReInitResults(void)
{
	struct tm *stm;
	time_t t;
	void *results;
	const int BUFSIZE = 1024;
	char buf[BUFSIZE];
	
	t = time(NULL);
	stm = localtime(&t);
	snprintf(buf, BUFSIZE, "%sresults/%s/results-%4d-%02d-%02d-%02d-%02d-%02d.xml",
		GetLocalDir(),
		ReInfo->_reFilename,
		stm->tm_year+1900,
		stm->tm_mon+1,
		stm->tm_mday,
		stm->tm_hour,
		stm->tm_min,
		stm->tm_sec
	);
	
	ReInfo->results = GfParmReadFile(buf, GFPARM_RMODE_STD | GFPARM_RMODE_CREAT);
	results = ReInfo->results;
	GfParmSetNum(results, RE_SECT_HEADER, RE_ATTR_DATE, NULL, (tdble)t);
	GfParmSetNum(results, RE_SECT_CURRENT, RE_ATTR_CUR_TRACK, NULL, 1);
	GfParmSetNum(results, RE_SECT_CURRENT, RE_ATTR_CUR_RACE, NULL, 1);
	GfParmSetNum(results, RE_SECT_CURRENT, RE_ATTR_CUR_DRIVER, NULL, 1);	
}

void
ReEventInitResults(void)
{
	int nCars;
	int i;
	void *results = ReInfo->results;
	void *params = ReInfo->params;
	const int BUFSIZE = 1024;
	char path[BUFSIZE], path2[BUFSIZE];
	
	nCars = GfParmGetEltNb(params, RM_SECT_DRIVERS);
	for (i = 1; i < nCars + 1; i++) {
		snprintf(path, BUFSIZE, "%s/%s/%d", ReInfo->track->name, RM_SECT_DRIVERS, i);
		snprintf(path2, BUFSIZE, "%s/%d", RM_SECT_DRIVERS, i);
		GfParmSetStr(results, path, RE_ATTR_DLL_NAME, GfParmGetStr(params, path2, RM_ATTR_MODULE, ""));
		GfParmSetNum(results, path, RE_ATTR_INDEX, NULL, GfParmGetNum(params, path2, RM_ATTR_IDX, (char*)NULL, 0));
    }
}

void
ReUpdateStandings(void)
{
	int maxDrv;
	int curDrv;
	int runDrv;
	char *modName;
	int drvIdx;
	int points;
	int i, j;
	int found;
	tReStandings	*standings = 0;
	void *results = ReInfo->results;
	const int BUFSIZE = 1024;
	char str1[BUFSIZE], str2[BUFSIZE], path[BUFSIZE], path2[BUFSIZE];
	
	snprintf(path, BUFSIZE, "%s/%s/%s/%s", ReInfo->track->name, RE_SECT_RESULTS, ReInfo->_reRaceName, RE_SECT_RANK);
	
	runDrv = GfParmGetEltNb(results, path);
	curDrv = GfParmGetEltNb(results, RE_SECT_STANDINGS);
	maxDrv = curDrv + runDrv;
	
	standings = (tReStandings *)calloc(maxDrv, sizeof(tReStandings));
	
	/* Read the current standings */
	for (i = 0; i < curDrv; i++) {
		snprintf(path2, BUFSIZE, "%s/%d", RE_SECT_STANDINGS, i + 1);
		standings[i].carName = strdup(GfParmGetStr(results, path2, RE_ATTR_NAME, 0));
		standings[i].modName = strdup(GfParmGetStr(results, path2, RE_ATTR_MODULE, 0));
		standings[i].drvIdx  = (int)GfParmGetNum(results, path2, RE_ATTR_IDX, NULL, 0);
		standings[i].points  = (int)GfParmGetNum(results, path2, RE_ATTR_POINTS, NULL, 0);
	}

	GfParmListClean(results, RE_SECT_STANDINGS);
	
	for (i = 0; i < runDrv; i++) {
		/* Search the driver in the standings */
		found = 0;
		snprintf(path, BUFSIZE, "%s/%s/%s/%s/%d", ReInfo->track->name, RE_SECT_RESULTS, ReInfo->_reRaceName, RE_SECT_RANK, i + 1);
		const char* carName = GfParmGetStr(results, path, RE_ATTR_NAME, 0);
		for (j = 0; j < curDrv; j++) {
			if (!strcmp(carName, standings[j].carName)) {
				found = 1;
				break;
			}
		}

		if (!found) {
			/* Add the new driver */
			curDrv++;
			standings[j].carName = strdup(carName);
			standings[j].modName = strdup(GfParmGetStr(results, path, RE_ATTR_MODULE, 0));
			standings[j].drvIdx  = (int)GfParmGetNum(results, path, RE_ATTR_IDX, NULL, 0);
			standings[j].points  = (int)GfParmGetNum(results, path, RE_ATTR_POINTS, NULL, 0);
		} else {
			/* Add the new points */
			standings[j].points += (int)GfParmGetNum(results, path, RE_ATTR_POINTS, NULL, 0);
		}
		/* bubble sort... */
		while (j > 0) {
			if (standings[j - 1].points >= standings[j].points) {
				break;
			}
			/* Swap with preceeding */
			carName = standings[j].carName;
			modName = standings[j].modName;
			drvIdx  = standings[j].drvIdx;
			points  = standings[j].points;
		
			standings[j].carName = standings[j - 1].carName;
			standings[j].modName = standings[j - 1].modName;
			standings[j].drvIdx  = standings[j - 1].drvIdx;
			standings[j].points  = standings[j - 1].points;
		
			standings[j - 1].carName = carName;
			standings[j - 1].modName = modName;
			standings[j - 1].drvIdx  = drvIdx;
			standings[j - 1].points  = points;
		
			j--;
		}
	}
	
	/* Store the standing back */
	for (i = 0; i < curDrv; i++) {
		snprintf(path, BUFSIZE, "%s/%d", RE_SECT_STANDINGS, i + 1);
		GfParmSetStr(results, path, RE_ATTR_NAME, standings[i].carName);
		//free(standings[i].carName);
		GfParmSetStr(results, path, RE_ATTR_MODULE, standings[i].modName);
		free(standings[i].modName);
		GfParmSetNum(results, path, RE_ATTR_IDX, NULL, standings[i].drvIdx);
		GfParmSetNum(results, path, RE_ATTR_POINTS, NULL, standings[i].points);
	}
	free(standings);
	
	snprintf(str1, BUFSIZE, "%sconfig/params.dtd", GetDataDir());
	snprintf(str2, BUFSIZE, "<?xml-stylesheet type=\"text/xsl\" href=\"file:///%sconfig/style.xsl\"?>", GetDataDir());
	
	GfParmSetDTD (results, str1, str2);
	GfParmWriteFile(0, results, "Results");
}


void
ReStoreRaceResults(const char *race)
{
	int i;
	int nCars;
	tCarElt *car;
	tSituation *s = ReInfo->s;
	char *carName;
	void *carparam;
	void *results = ReInfo->results;
	void *params = ReInfo->params;
	const int BUFSIZE = 1024;
	char buf[BUFSIZE], path[BUFSIZE], path2[BUFSIZE];
	
	/* Store the number of laps of the race */
	switch (ReInfo->s->_raceType) {
		case RM_TYPE_RACE:
			car = s->cars[0];
			if (car->_laps > s->_totLaps) car->_laps = s->_totLaps + 1;

			snprintf(path, BUFSIZE, "%s/%s/%s", ReInfo->track->name, RE_SECT_RESULTS, race);
			GfParmListClean(results, path);
			GfParmSetNum(results, path, RE_ATTR_LAPS, NULL, car->_laps - 1);
			
			for (i = 0; i < s->_ncars; i++) {
				snprintf(path, BUFSIZE, "%s/%s/%s/%s/%d", ReInfo->track->name, RE_SECT_RESULTS, race, RE_SECT_RANK, i + 1);
				car = s->cars[i];
				if (car->_laps > s->_totLaps) car->_laps = s->_totLaps + 1;
			
				GfParmSetStr(results, path, RE_ATTR_NAME, car->_name);
			
				snprintf(buf, BUFSIZE, "cars/%s/%s.xml", car->_carName, car->_carName);
				carparam = GfParmReadFile(buf, GFPARM_RMODE_STD);
				carName = GfParmGetName(carparam);
			
				GfParmSetStr(results, path, RE_ATTR_CAR, carName);
				GfParmSetNum(results, path, RE_ATTR_INDEX, NULL, car->index);
			
				GfParmSetNum(results, path, RE_ATTR_LAPS, NULL, car->_laps - 1);
				GfParmSetNum(results, path, RE_ATTR_TIME, NULL, car->_curTime);
				GfParmSetNum(results, path, RE_ATTR_BEST_LAP_TIME, NULL, car->_bestLapTime);
				GfParmSetNum(results, path, RE_ATTR_TOP_SPEED, NULL, car->_topSpeed);
				GfParmSetNum(results, path, RE_ATTR_DAMMAGES, NULL, car->_dammage);
				GfParmSetNum(results, path, RE_ATTR_NB_PIT_STOPS, NULL, car->_nbPitStops);
			
				GfParmSetStr(results, path, RE_ATTR_MODULE, car->_modName);
				GfParmSetNum(results, path, RE_ATTR_IDX, NULL, car->_driverIndex);
			
				snprintf(path2, BUFSIZE, "%s/%s/%d", race, RM_SECT_POINTS, i + 1);
				GfParmSetNum(results, path, RE_ATTR_POINTS, NULL,
						(int)GfParmGetNum(params, path2, RE_ATTR_POINTS, NULL, 0));

				GfParmReleaseHandle(carparam);
			}
			break;
			
		case RM_TYPE_PRACTICE:
			car = s->cars[0];
			snprintf(path, BUFSIZE, "%s/%s/%s", ReInfo->track->name, RE_SECT_RESULTS, race);
			GfParmSetStr(results, path, RM_ATTR_DRVNAME, car->_name);
			break;
			
		case RM_TYPE_QUALIF:
			car = s->cars[0];
			snprintf(path, BUFSIZE, "%s/%s/%s/%s", ReInfo->track->name, RE_SECT_RESULTS, race, RE_SECT_RANK);
			nCars = GfParmGetEltNb(results, path);
			for (i = nCars; i > 0; i--) {
				snprintf(path, BUFSIZE, "%s/%s/%s/%s/%d", ReInfo->track->name, RE_SECT_RESULTS, race, RE_SECT_RANK, i);
				float opponentBestLapTime = GfParmGetNum(results, path, RE_ATTR_BEST_LAP_TIME, NULL, 0);
			
				if (
					(car->_bestLapTime != 0.0) && 
					((car->_bestLapTime < opponentBestLapTime) || (opponentBestLapTime == 0.0))
				) {
					/* shift */
					snprintf(path2, BUFSIZE, "%s/%s/%s/%s/%d", ReInfo->track->name, RE_SECT_RESULTS, race, RE_SECT_RANK, i + 1);
					GfParmSetStr(results, path2, RE_ATTR_NAME, GfParmGetStr(results, path, RE_ATTR_NAME, ""));
					GfParmSetStr(results, path2, RE_ATTR_CAR, GfParmGetStr(results, path, RE_ATTR_CAR, ""));
					GfParmSetNum(results, path2, RE_ATTR_BEST_LAP_TIME, NULL, GfParmGetNum(results, path, RE_ATTR_BEST_LAP_TIME, NULL, 0));
					GfParmSetStr(results, path2, RE_ATTR_MODULE, GfParmGetStr(results, path, RM_ATTR_MODULE, ""));
					GfParmSetNum(results, path2, RE_ATTR_IDX, NULL, GfParmGetNum(results, path, RM_ATTR_IDX, NULL, 0));
					snprintf(path, BUFSIZE, "%s/%s/%d", race, RM_SECT_POINTS, i + 1);
					GfParmSetNum(results, path2, RE_ATTR_POINTS, NULL,
								(int)GfParmGetNum(params, path, RE_ATTR_POINTS, NULL, 0));
				} else {
					break;
				}
			}
			/* insert after */
			snprintf(path, BUFSIZE, "%s/%s/%s/%s/%d", ReInfo->track->name, RE_SECT_RESULTS, race, RE_SECT_RANK, i + 1);
			GfParmSetStr(results, path, RE_ATTR_NAME, car->_name);
			
			snprintf(buf, BUFSIZE, "cars/%s/%s.xml", car->_carName, car->_carName);
			carparam = GfParmReadFile(buf, GFPARM_RMODE_STD);
			carName = GfParmGetName(carparam);
			
			GfParmSetStr(results, path, RE_ATTR_CAR, carName);
			GfParmSetNum(results, path, RE_ATTR_BEST_LAP_TIME, NULL, car->_bestLapTime);
			GfParmSetStr(results, path, RE_ATTR_MODULE, car->_modName);
			GfParmSetNum(results, path, RE_ATTR_IDX, NULL, car->_driverIndex);
			snprintf(path2, BUFSIZE, "%s/%s/%d", race, RM_SECT_POINTS, i + 1);
			GfParmSetNum(results, path, RE_ATTR_POINTS, NULL,
						(int)GfParmGetNum(params, path2, RE_ATTR_POINTS, NULL, 0));
		
			GfParmReleaseHandle(carparam);
			break;
	}
}

void
ReUpdateQualifCurRes(tCarElt *car)
{
	int i;
	int nCars;
	int printed;
	int maxLines;
	void *carparam;
	char *carName;
	const char *race = ReInfo->_reRaceName;
	void *results = ReInfo->results;
	const int BUFSIZE = 1024;
	char buf[BUFSIZE], path[BUFSIZE];

	ReResEraseScreen();
	maxLines = ReResGetLines();
	
	snprintf(buf, BUFSIZE, "%s on %s - Lap %d", car->_name, ReInfo->track->name, car->_laps);
	ReResScreenSetTitle(buf);
	
	snprintf(buf, BUFSIZE, "cars/%s/%s.xml", car->_carName, car->_carName);
	carparam = GfParmReadFile(buf, GFPARM_RMODE_STD);
	carName = GfParmGetName(carparam);
	
	printed = 0;
	snprintf(path, BUFSIZE, "%s/%s/%s/%s", ReInfo->track->name, RE_SECT_RESULTS, race, RE_SECT_RANK);
	nCars = GfParmGetEltNb(results, path);
	nCars = MIN(nCars + 1, maxLines);
	for (i = 1; i < nCars; i++) {
		snprintf(path, BUFSIZE, "%s/%s/%s/%s/%d", ReInfo->track->name, RE_SECT_RESULTS, race, RE_SECT_RANK, i);
		if (!printed) {
			if ((car->_bestLapTime != 0.0) && (car->_bestLapTime < GfParmGetNum(results, path, RE_ATTR_BEST_LAP_TIME, NULL, 0))) {
				snprintf(buf, BUFSIZE, "%d - %s - %s (%s)", i, GfTime2Str(car->_bestLapTime, 0), car->_name, carName);
				ReResScreenSetText(buf, i - 1, 1);
				printed = 1;
			}
		}
		snprintf(buf, BUFSIZE, "%d - %s - %s (%s)", i + printed, GfTime2Str(GfParmGetNum(results, path, RE_ATTR_BEST_LAP_TIME, NULL, 0), 0),
		GfParmGetStr(results, path, RE_ATTR_NAME, ""), GfParmGetStr(results, path, RE_ATTR_CAR, ""));
		ReResScreenSetText(buf, i - 1 + printed, 0);
	}

	if (!printed) {
		snprintf(buf, BUFSIZE, "%d - %s - %s (%s)", i, GfTime2Str(car->_bestLapTime, 0), car->_name, carName);
		ReResScreenSetText(buf, i - 1, 1);
	}

	GfParmReleaseHandle(carparam);
	ReInfo->_refreshDisplay = 1;
}

void
ReSavePracticeLap(tCarElt *car)
{
	void *results = ReInfo->results;
	tReCarInfo *info = &(ReInfo->_reCarInfo[car->index]);
	const int BUFSIZE = 1024;
	char path[BUFSIZE];

	snprintf(path, BUFSIZE, "%s/%s/%s/%d", ReInfo->track->name, RE_SECT_RESULTS, ReInfo->_reRaceName, car->_laps - 1);
	GfParmSetNum(results, path, RE_ATTR_TIME, NULL, car->_lastLapTime);
	GfParmSetNum(results, path, RE_ATTR_BEST_LAP_TIME, NULL, car->_bestLapTime);
	GfParmSetNum(results, path, RE_ATTR_TOP_SPEED, NULL, info->topSpd);
	GfParmSetNum(results, path, RE_ATTR_BOT_SPEED, NULL, info->botSpd);
	GfParmSetNum(results, path, RE_ATTR_DAMMAGES, NULL, car->_dammage);    
}

int
ReDisplayResults(void)
{
	void *params = ReInfo->params;

	if (ReInfo->_displayMode != RM_DISP_MODE_CONSOLE) {
		if ((!strcmp(GfParmGetStr(params, ReInfo->_reRaceName, RM_ATTR_DISPRES, RM_VAL_YES), RM_VAL_YES)) ||
			(ReInfo->_displayMode == RM_DISP_MODE_NORMAL))
		{
			RmShowResults(ReInfo->_reGameScreen, ReInfo);
		} else {
			ReResShowCont();
		}

		return RM_ASYNC | RM_NEXT_STEP;
	}

	return RM_SYNC | RM_NEXT_STEP;
}


void
ReDisplayStandings(void)
{
	RmShowStandings(ReInfo->_reGameScreen, ReInfo);
}