TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
racepenalties.cpp
Go to the documentation of this file.
1/***************************************************************************
2
3 file : racepenalties.cpp
4 created : Mon Jun 8 20:00:00 CET 2026
5 copyright : (C) 2026 by Bernhard Wymann
6 email : berniw@bluewin.ch
7
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
23
24#include "racepenalties.h"
25
26
38static int
40{
41 return MIN(car->_laps, s->_totLaps + 1) - 1;
42}
43
44
51static void
53{
54 tCarElt *car = cars[j];
55 cars[j] = cars[j - 1];
56 cars[j - 1] = car;
57 cars[j]->_pos = j + 1;
58 cars[j - 1]->_pos = j;
59}
60
61
71static void
73{
74 if (!pits || pits->type != TR_PIT_ON_TRACK_SIDE) {
75 return;
76 }
77
78 const tdble drivethrough = 10.0f;
79 const tdble stopandgo = 6.0f + drivethrough;
80 const tdble v0 = 84.0f;
81 const tdble v1 = pits->speedLimit;
82 const tdble dv = v0 - v1;
83 tdble dt = 0.0f;
84
85 if (dv > 1.0f && v1 > 1.0f) {
86 dt = (pits->nMaxPits * pits->len) * dv / (v0 * v1);
87 }
88
89 for (int i = 0; i < s->_ncars; i++) {
90 tCarElt *car = s->cars[i];
91 tCarPenalty *penalty = GF_TAILQ_FIRST(&(car->_penaltyList));
92 while (penalty) {
93 if (penalty->penalty == RM_PENALTY_DRIVETHROUGH) {
94 car->_penaltyTime += dt + drivethrough;
95 } else if (penalty->penalty == RM_PENALTY_STOPANDGO) {
96 car->_penaltyTime += dt + stopandgo;
97 } else {
98 GfError("Unknown penalty.");
99 }
100 penalty = GF_TAILQ_NEXT(penalty, link);
101 }
102 }
103}
104
105
106void
108{
109 if (!s || !s->cars) {
110 return;
111 }
112
114
115 for (int i = 1; i < s->_ncars; i++) {
116 int j = i;
117 while (j > 0) {
118 tCarElt *previous = s->cars[j - 1];
119 tCarElt *current = s->cars[j];
120
121 // The race order is already valid without penalties, so only penalized cars move down.
122 if (previous->_penaltyTime <= 0.0f) {
123 break;
124 }
125
126 const int previousLaps = completedResultLaps(s, previous);
127 const int currentLaps = completedResultLaps(s, current);
128 if (previousLaps != currentLaps) {
129 break;
130 }
131
132 // No penalty reordering is applied before one completed lap or for wrecked cars.
133 if (previousLaps < 1 ||
134 (s->raceInfo.maxDammage < previous->_dammage) ||
135 (s->raceInfo.maxDammage < current->_dammage))
136 {
137 i = s->_ncars; // Break outer loop.
138 break;
139 }
140
141 const double previousTime = previous->_curTime + previous->_penaltyTime;
142 const double currentTime = current->_curTime + current->_penaltyTime;
143
144 if (previousTime <= currentTime) {
145 break;
146 }
147
148 swapAdjacentCars(s->cars, j);
149 j--;
150 }
151 }
152}
#define RM_PENALTY_DRIVETHROUGH
Definition car.h:126
struct CarElt tCarElt
Car structure (tCarElt).
struct CarPenalty tCarPenalty
One penalty.
#define RM_PENALTY_STOPANDGO
Definition car.h:127
float tdble
Floating point type used in TORCS.
Definition tgf.h:47
#define GF_TAILQ_NEXT(elm, field)
Next element of a TAILQ.
Definition tgf.h:466
#define GF_TAILQ_FIRST(head)
First element of a TAILQ.
Definition tgf.h:463
struct Situation tSituation
cars situation used to inform the GUI and the drivers
static void applyPendingPitPenalties(tSituation *s, const tTrackPitInfo *pits)
Apply pending drive-through and stop-and-go penalties as time.
static int completedResultLaps(const tSituation *s, const tCarElt *car)
Return the result lap count used for final classification.
static void swapAdjacentCars(tCarElt **cars, int j)
Swap adjacent cars and refresh their result positions.
void ReApplyRaceTimePenalties(tSituation *s, const tTrackPitInfo *pits)
Apply race time penalties and update same-lap result order.
Race penalty application and penalty-aware race result ordering.
int penalty
penalty type
Definition car.h:132
tRaceAdmInfo raceInfo
Definition raceman.h:85
tCarElt ** cars
list of cars
Definition raceman.h:89
int maxDammage
Definition raceman.h:73
Pits Info Structure.
Definition track.h:469
tdble len
Lenght of each pit stop.
Definition track.h:483
tdble speedLimit
Speed limit between pitStart and pitEnd.
Definition track.h:485
int type
Type of Pit:
Definition track.h:470
int nMaxPits
number max of pits
Definition track.h:477
#define MIN(x, y)
Definition tgf.h:56
#define GfError
Definition tgf.h:350
#define TR_PIT_ON_TRACK_SIDE
The pits are on the track side.
Definition track.h:475