forked from supertuxkart/stk-code
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgp_scoring.cpp
More file actions
167 lines (152 loc) · 5.65 KB
/
gp_scoring.cpp
File metadata and controls
167 lines (152 loc) · 5.65 KB
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
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2025 kimden
//
// 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 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "utils/gp_scoring.hpp"
#include "network/server_config.hpp"
#include "utils/string_utils.hpp"
#include "utils/log.hpp"
#include <cmath>
#include <algorithm>
std::shared_ptr<GPScoring> GPScoring::createFromIntParamString(const std::string& input)
{
std::shared_ptr<GPScoring> new_scoring = std::make_shared<GPScoring>();
std::set<std::string> available_scoring_types = {
"standard", "default", "", "inc", "fixed", "linear-gap", "exp-gap"
};
if (!input.empty())
{
std::vector<std::string> params = StringUtils::split(input, ' ');
if (params.empty())
{
new_scoring->m_type = "";
return new_scoring;
}
new_scoring->m_type = params[0];
if (available_scoring_types.count(params[0]) == 0)
throw std::logic_error("Unknown scoring type " + params[0]);
for (unsigned i = 1; i < params.size(); i++)
{
int param;
if (!StringUtils::fromString(params[i], param))
throw std::logic_error("Unable to parse integer from custom scoring data");
new_scoring->m_params.push_back(param);
}
}
return new_scoring;
} // createFromIntParamString
//-----------------------------------------------------------------------------
bool GPScoring::isStandard() const
{
return m_type == ""
|| m_type == "standard"
|| m_type == "default";
} // isStandard
//-----------------------------------------------------------------------------
void GPScoring::refreshCustomScores(int num_karts,
std::vector<int>& score_for_position)
{
if (m_type == "inc")
{
// Testing indicates that number of parameters is not validated.
// Do it when splitting into separate classes.
score_for_position.clear();
for (unsigned i = 2; i < m_params.size(); i++)
score_for_position.push_back(m_params[i]);
score_for_position.resize(num_karts, 0);
std::sort(score_for_position.begin(), score_for_position.end());
for (unsigned i = 1; i < score_for_position.size(); i++)
score_for_position[i] += score_for_position[i - 1];
std::reverse(score_for_position.begin(), score_for_position.end());
}
else if (m_type == "fixed")
{
score_for_position.clear();
for (unsigned i = 2; i < m_params.size(); i++)
score_for_position.push_back(m_params[i]);
score_for_position.resize(num_karts, 0);
}
else if (m_type == "linear-gap"
|| m_type == "exp-gap")
{
score_for_position.clear();
score_for_position.resize(num_karts, 0);
}
} // refreshCustomScores
//-----------------------------------------------------------------------------
int GPScoring::getPolePoints() const
{
return m_params[0];
} // getPolePoints
//-----------------------------------------------------------------------------
int GPScoring::getFastestLapPoints() const
{
return m_params[1];
} // getFastestLapPoints
//-----------------------------------------------------------------------------
int GPScoring::getScoreForPosition(int p, float time,
std::map<int, float>& race_times,
const std::vector<int>& score_for_position) const
{
race_times[p] = time;
if (m_type == "inc" || m_type == "fixed")
return score_for_position[p - 1];
if (m_type == "linear-gap"
|| m_type == "exp-gap")
{
double delta = time - race_times[1];
if (m_type == "exp-gap")
{
if (race_times[1] < 1e-6) // just in case
return 0;
delta = log(time / race_times[1]) / log(2);
}
double points = m_params[2] * 0.001;
bool continuous = (m_params[5] != 0);
double time_step = m_params[3] * 0.001;
double decrease = m_params[4] * 0.001;
delta /= time_step;
if (!continuous)
delta = floor(delta);
points -= delta * decrease;
if (points < 0.0)
points = 0.0;
return round(points);
}
Log::error("GPScoring", "Unknown scoring type: %s. Giving 0 points", m_type.c_str());
return 0;
} // getScoreForPosition
//-----------------------------------------------------------------------------
bool GPScoring::canGetScoreForPosition(int p, const std::map<int, float>& race_times) const
{
if (m_type == "linear-gap"
|| m_type == "exp-gap")
{
if (p == 1 || race_times.count(1))
return true;
return false;
}
return true;
} // canGetScoreForPosition
//-----------------------------------------------------------------------------
std::string GPScoring::toString() const
{
std::string res = m_type;
for (int param: m_params)
res += StringUtils::insertValues(" %d", param);
return res;
} // toString
//-----------------------------------------------------------------------------