Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
cl_race.qc
Go to the documentation of this file.
1#include "cl_race.qh"
2
3#include <client/draw.qh>
5
6// Race/CTS HUD mod icons
7float crecordtime_prev; // last remembered crecordtime
8float crecordtime_change_time; // time when crecordtime last changed
9float srecordtime_prev; // last remembered srecordtime
10float srecordtime_change_time; // time when srecordtime last changed
11
15
16// Check if the given name already exists in race rankings?
17// In that case, where? (otherwise return 0)
18int race_CheckName(string net_name)
19{
20 int rank = 0;
21 string zoned_name = strzone(strdecolorize(net_name));
22 for (int i = RANKINGS_CNT - 1; i >= 0; --i)
23 if (strdecolorize(grecordholder[i]) == zoned_name)
24 {
25 rank = i + 1;
26 break;
27 }
28 strfree(zoned_name);
29 return rank;
30}
31
32void race_showTime(string text, vector pos, vector timeText_ofs, float theTime, vector textSize, float f)
33{
34 drawstring_aspect(pos, text, textSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
35 drawstring_aspect(pos + timeText_ofs, TIME_ENCODED_TOSTRING(theTime, false), textSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
36 if (f < 1) {
37 drawstring_aspect_expanding(pos, text, textSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
38 drawstring_aspect_expanding(pos + timeText_ofs, TIME_ENCODED_TOSTRING(theTime, false), textSize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL, f);
39 }
40}
41
42void race_UpdateDB(string rr, float new_score, float old_score)
43{
44 // TODO: relocate this out of HUD code
45 db_put(ClientProgsDB, strcat(mi_shortname, rr, "time"), ftos(new_score));
47 {
49 i &= ~1;
50 cvar_set("cl_autodemo_delete", itos(i)); // don't delete demo with new record!
51 }
52 localcmd("\ncl_hook_race_pb \"",
53 TIME_ENCODED_TOSTRING(new_score, false), "\" \"",
54 TIME_ENCODED_TOSTRING(old_score, false), "\" \"",
55 (old_score ? TIME_ENCODED_TOSTRING(old_score - new_score, false) : "0"), "\" \"",
56 sprintf("%d", race_myrank), "\"\n");
57}
58
59void HUD_Mod_Race(vector pos, vector mySize, bool should_draw)
60{
61 if (!should_draw)
62 return;
63
65 float score = me.(scores(ps_primary));
66
67 if(!(scores_flags(ps_primary) & SFL_TIME) || teamplay) // race/cts record display on HUD
68 {
69 mod_active = 0; // hide it in this case!
70 return; // no records in the actual race
71 }
72
73 mod_active = 1;
74
75 // clientside personal record
76 string rr;
77 if(ISGAMETYPE(CTS))
78 rr = CTS_RECORD;
79 else
80 rr = RACE_RECORD;
81 float t = stof(db_get(ClientProgsDB, strcat(mi_shortname, rr, "time")));
82
83 if (score && (score < t || !t) && !isdemo())
84 race_UpdateDB(rr, score, t);
85
86 if(t != crecordtime_prev) {
89 }
90
91 vector textPos, medalPos;
92 float squareSize;
93 if(mySize.x > mySize.y) {
94 // text on left side
95 squareSize = min(mySize.y, mySize.x/2);
96 vector ofs = vec2(0.5 * max(0, mySize.x/2 - squareSize), 0.5 * (mySize.y - squareSize));
97 textPos = pos + ofs;
98 ofs.x += 0.5 * mySize.x;
99 medalPos = pos + ofs;
100 } else {
101 // text on top
102 squareSize = min(mySize.x, mySize.y/2);
103 vector ofs = vec2(0.5 * (mySize.x - squareSize), 0.5 * max(0, mySize.y/2 - squareSize));
104 textPos = pos + ofs;
105 ofs.y += 0.5 * mySize.y;
106 medalPos = pos + ofs;
107 }
108 vector textSize = vec2(squareSize, 0.25 * squareSize);
109
110 race_showTime(_("Personal best"), textPos, eY * 0.25 * squareSize, t, textSize, time - crecordtime_change_time);
111
112 // server record
114 if(t != srecordtime_prev) {
117 }
118
119 textPos += eY * 0.5 * squareSize;
120 race_showTime(_("Server best"), textPos, eY * 0.25 * squareSize, t, textSize, time - srecordtime_change_time);
121
126 }
127
128 // race "awards"
129 float a = bound(0, race_status_time - time, 1);
130 string s = textShortenToWidth(ColorTranslateRGB(race_status_name), squareSize, '1 1 0' * 0.1 * squareSize, stringwidth_colors);
131
132 float rank = 0;
133 if(race_status > 0)
135 string rankname = count_ordinal(rank);
136 vector namepos = medalPos + '0 0.8 0' * squareSize;
137 vector rankpos = medalPos + '0 0.15 0' * squareSize;
138
139 if(race_status == 0)
140 drawpic_aspect_skin(medalPos, "race_newfail", '1 1 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
141 else if(race_status == 1) {
142 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newtime", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
143 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
144 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
145 } else if(race_status == 2) {
146 if(strdecolorize(race_status_name) == strdecolorize(entcs_GetName(player_localnum)) || !race_myrank || race_myrank < rank)
147 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrankgreen", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
148 else
149 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrankyellow", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
150 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
151 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
152 } else if(race_status == 3) {
153 drawpic_aspect_skin(medalPos + '0.1 0 0' * squareSize, "race_newrecordserver", '1 1 0' * 0.8 * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
154 drawcolorcodedstring_aspect(namepos, s, '1 0.2 0' * squareSize, panel_fg_alpha * a, DRAWFLAG_NORMAL);
155 drawstring_aspect(rankpos, rankname, '1 0.15 0' * squareSize, '1 1 1', panel_fg_alpha * a, DRAWFLAG_NORMAL);
156 }
157
158 if (race_status_time - time <= 0) {
159 race_status_prev = -1;
160 race_status = -1;
163 }
164}
165
166REGISTER_MUTATOR(cl_race, true);
167
168MUTATOR_HOOKFUNCTION(cl_race, HUD_Physics_showoptional)
169{
170 return ISGAMETYPE(RACE); // show the optional physics panel
171}
172
173MUTATOR_HOOKFUNCTION(cl_race, HUD_StrafeHUD_showoptional)
174{
175 return ISGAMETYPE(RACE); // show the optional strafehud
176}
177
178MUTATOR_HOOKFUNCTION(cl_race, HUD_Score_show)
179{
180 return spectatee_status == -1 && ISGAMETYPE(RACE); // hide the score panel while observing
181}
182
183MUTATOR_HOOKFUNCTION(cl_race, DrawScoreboardItemStats)
184{
185 return ISGAMETYPE(RACE); // hide the item stats panel
186}
187
188MUTATOR_HOOKFUNCTION(cl_race, ShowRankings)
189{
190 if(ISGAMETYPE(RACE))
191 {
192 M_ARGV(0, string) = _("Rankings");
193 return true;
194 }
195}
196
197MUTATOR_HOOKFUNCTION(cl_race, DrawScoreboardAccuracy)
198{
199 return ISGAMETYPE(RACE); // accuracy is not a factor in this gametype
200}
201
202MUTATOR_HOOKFUNCTION(cl_race, ShowRaceTimer)
203{
204 return ISGAMETYPE(RACE); // show the race timer panel
205}
206
207MUTATOR_HOOKFUNCTION(cl_race, TeamRadar_Draw)
208{
209 return ISGAMETYPE(RACE); // show all competitors in a race
210}
#define REGISTER_MUTATOR(...)
Definition base.qh:295
#define MUTATOR_HOOKFUNCTION(...)
Definition base.qh:335
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
int race_CheckName(string net_name)
Definition cl_race.qc:18
float srecordtime_change_time
Definition cl_race.qc:10
void HUD_Mod_Race(vector pos, vector mySize, bool should_draw)
Definition cl_race.qc:59
float crecordtime_prev
Definition cl_race.qc:7
float srecordtime_prev
Definition cl_race.qc:9
float race_status_time
Definition cl_race.qc:12
void race_UpdateDB(string rr, float new_score, float old_score)
Definition cl_race.qc:42
void race_showTime(string text, vector pos, vector timeText_ofs, float theTime, vector textSize, float f)
Definition cl_race.qc:32
float crecordtime_change_time
Definition cl_race.qc:8
string race_status_name_prev
Definition cl_race.qc:14
int race_status_prev
Definition cl_race.qc:13
bool autocvar_cl_autodemo_delete_keeprecords
Definition cl_race.qh:3
void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag)
Definition draw.qc:109
void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag)
Definition draw.qc:115
void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp)
Definition draw.qc:137
#define drawpic_aspect_skin(pos, pic, sz, color, theAlpha, drawflag)
Definition draw.qh:78
entity playerslots[255]
Definition main.qh:84
int ClientProgsDB
Definition main.qh:204
#define ISGAMETYPE(NAME)
Definition main.qh:46
int spectatee_status
the -1 disables HUD panels before CSQC receives necessary data
Definition main.qh:197
string grecordholder[RANKINGS_CNT]
Definition main.qh:81
bool autocvar_cl_autodemo_delete
Definition main.qh:32
#define M_ARGV(x, type)
Definition events.qh:17
const int SFL_TIME
Display as mm:ss.s, value is stored as 10ths of a second (AND 0 is the worst possible value!...
Definition scores.qh:122
#define scores_flags(this)
Definition scores.qh:147
string textShortenToWidth(string theText, float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
Definition util.qc:1171
string mi_shortname
Definition util.qh:126
#define CTS_RECORD
Definition util.qh:98
#define TIME_ENCODED_TOSTRING(n, compact)
Definition util.qh:96
#define RACE_RECORD
Definition util.qh:97
const int RANKINGS_CNT
Definition constants.qh:31
ERASEABLE string count_ordinal(int interval)
Definition counting.qh:66
const float DRAWFLAG_NORMAL
float player_localnum
float time
string entcs_GetName(int i)
Definition ent_cs.qh:151
float panel_fg_alpha
Definition hud.qh:169
PlayerScoreField ps_primary
Definition hud.qh:117
#define itos(i)
Definition int.qh:6
ERASEABLE string db_get(int db, string key)
Definition map.qh:91
ERASEABLE void db_put(int db, string key, string value)
Definition map.qh:101
void localcmd(string command,...)
void cvar_set(string name, string value)
float isdemo()
float stof(string val,...)
float bound(float min, float value, float max)
float min(float f,...)
string ftos(float f)
string strzone(string s)
float max(float f,...)
bool mod_active
Definition modicons.qh:7
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
float race_myrank
Definition racetimer.qh:49
float race_server_record
Definition racetimer.qh:29
string race_status_name
Definition racetimer.qh:48
float race_status
Definition racetimer.qh:47
vector
Definition self.qh:92
ERASEABLE string ColorTranslateRGB(string s)
Definition string.qh:196
#define strfree(this)
Definition string.qh:59
#define strcpy(this, s)
Definition string.qh:52
float stringwidth_colors(string s, vector theSize)
Definition string.qh:30
bool teamplay
Definition teams.qh:59
const vector eY
Definition vector.qh:45
#define vec2(...)
Definition vector.qh:90