3 #include <server/client.qh>
4 #include <server/world.qh>
5 #include <server/gamelog.qh>
6 #include <server/intermission.qh>
7 #include <server/race.qh>
8 #include <common/ent_cs.qh>
9 #include <common/mapobjects/triggers.qh>
11 #define autocvar_g_race_laps_limit cvar("g_race_laps_limit")
12 float autocvar_g_race_qualifying_timelimit;
13 float autocvar_g_race_qualifying_timelimit_override;
14 int autocvar_g_race_teams;
17 .float race_checkpoint;
18 void havocbot_role_race(entity this)
23 if (navigation_goalrating_timeout(this))
25 navigation_goalrating_start(this);
27 bool raw_touch_check = true;
28 int cp = this.race_checkpoint;
30 LABEL(search_racecheckpoints)
31 IL_EACH(g_racecheckpoints, true,
33 if(it.cnt == cp || cp == -1)
35 // redirect bot to next goal if it touched the waypoint of an untouchable checkpoint
36 // e.g. checkpoint in front of Stormkeep's warpzone
37 // the same workaround is applied in CTS game mode
38 if (raw_touch_check && vdist(this.origin - it.nearestwaypoint.origin, <, 30))
40 cp = race_NextCheckpoint(cp);
41 raw_touch_check = false;
42 goto search_racecheckpoints;
44 navigation_routerating(this, it, 1000000, 5000);
48 navigation_goalrating_end(this);
50 navigation_goalrating_timeout_set(this);
54 void race_ScoreRules()
56 GameRules_score_enabled(false);
57 GameRules_scoring(race_teams, 0, 0, {
59 field_team(ST_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
60 else if (g_race_qualifying)
61 field(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
62 if (race_teams || !g_race_qualifying)
64 field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
65 field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
66 field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
71 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
73 if(autocvar_sv_eventlog)
74 GameLogEcho(strcat(":race:", mode, ":", ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
77 float WinningCondition_Race(float fraglimit)
84 FOREACH_CLIENT(IS_PLAYER(it), {
86 if(CS(it).race_completed)
91 wc = WinningCondition_Scores(fraglimit, 0);
93 // ALWAYS initiate overtime, unless EVERYONE has finished the race!
94 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
95 // do NOT support equality when the laps are all raced!
96 return WINNING_STARTSUDDENDEATHOVERTIME;
101 float WinningCondition_QualifyingThenRace(float limit)
104 wc = WinningCondition_Scores(limit, 0);
106 // NEVER initiate overtime
107 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
115 MUTATOR_HOOKFUNCTION(rc, ClientKill)
117 if(g_race_qualifying)
118 M_ARGV(1, float) = 0; // killtime
121 MUTATOR_HOOKFUNCTION(rc, AbortSpeedrun)
123 entity player = M_ARGV(0, entity);
125 if(autocvar_g_allow_checkpoints)
126 race_PreparePlayer(player); // nice try
129 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
131 entity player = M_ARGV(0, entity);
132 float dt = M_ARGV(1, float);
134 player.race_movetime_frac += dt;
135 float f = floor(player.race_movetime_frac);
136 player.race_movetime_frac -= f;
137 player.race_movetime_count += f;
138 player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
140 if(IS_PLAYER(player))
142 if (player.race_penalty)
143 if (time > player.race_penalty)
144 player.race_penalty = 0;
145 if(player.race_penalty)
147 player.velocity = '0 0 0';
148 set_movetype(player, MOVETYPE_NONE);
149 player.disableclientprediction = 2;
153 // force kbd movement for fairness
157 // if record times matter
158 // ensure nothing EVIL is being done (i.e. div0_evade)
159 // this hinders joystick users though
160 // but it still gives SOME analog control
161 wishvel.x = fabs(CS(player).movement.x);
162 wishvel.y = fabs(CS(player).movement.y);
163 if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
166 wishspeed = vlen(wishvel);
167 if(wishvel.x >= 2 * wishvel.y)
170 if(CS(player).movement.x > 0)
171 CS(player).movement_x = wishspeed;
173 CS(player).movement_x = -wishspeed;
174 CS(player).movement_y = 0;
176 else if(wishvel.y >= 2 * wishvel.x)
179 CS(player).movement_x = 0;
180 if(CS(player).movement.y > 0)
181 CS(player).movement_y = wishspeed;
183 CS(player).movement_y = -wishspeed;
188 if(CS(player).movement.x > 0)
189 CS(player).movement_x = M_SQRT1_2 * wishspeed;
191 CS(player).movement_x = -M_SQRT1_2 * wishspeed;
192 if(CS(player).movement.y > 0)
193 CS(player).movement_y = M_SQRT1_2 * wishspeed;
195 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
200 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
204 Score_NicePrint(NULL);
207 PlayerScore_Sort(race_place, 0, true, false);
209 FOREACH_CLIENT(true, {
212 s = GameRules_scoring_add(it, RACE_FASTEST, 0);
216 race_EventLog(ftos(it.race_place), it);
219 if(g_race_qualifying == 2)
221 g_race_qualifying = 0;
222 independent_players = 0;
223 cvar_set("fraglimit", ftos(race_fraglimit));
224 cvar_set("leadlimit", ftos(race_leadlimit));
225 cvar_set("timelimit", ftos(race_timelimit));
230 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
232 entity player = M_ARGV(0, entity);
234 race_PreparePlayer(player);
235 player.race_checkpoint = -1;
237 race_SendAll(player, false);
240 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
242 entity player = M_ARGV(0, entity);
244 if(g_race_qualifying)
246 if(GameRules_scoring_add(player, RACE_FASTEST, 0))
247 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
249 player.frags = FRAGS_SPECTATOR;
252 race_PreparePlayer(player);
253 player.race_checkpoint = -1;
256 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
258 entity player = M_ARGV(0, entity);
259 entity spawn_spot = M_ARGV(1, entity);
261 if(spawn_spot.target == "")
262 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
263 race_PreparePlayer(player);
265 // if we need to respawn, do it right
266 player.race_respawn_checkpoint = player.race_checkpoint;
267 player.race_respawn_spotref = spawn_spot;
269 player.race_place = 0;
272 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
274 entity player = M_ARGV(0, entity);
276 if(IS_PLAYER(player))
279 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
280 race_PreparePlayer(player);
282 race_RetractPlayer(player);
284 race_AbandonRaceCheck(player);
288 MUTATOR_HOOKFUNCTION(rc, PlayerDamaged)
290 int frag_deathtype = M_ARGV(5, int);
291 if (frag_deathtype == DEATH_KILL.m_id)
292 return true; // forbid logging damage
295 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
297 entity frag_target = M_ARGV(2, entity);
299 frag_target.respawn_flags |= RESPAWN_FORCE;
300 race_AbandonRaceCheck(frag_target);
303 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
305 entity bot = M_ARGV(0, entity);
307 bot.havocbot_role = havocbot_role_race;
311 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
313 entity player = M_ARGV(0, entity);
315 race_checkAndWriteName(player);
316 race_SpeedAwardFrame(player);
319 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
321 if(g_race_qualifying)
322 return true; // in qualifying, you don't lose score by observing
325 MUTATOR_HOOKFUNCTION(rc, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
327 M_ARGV(0, float) = race_teams;
331 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
333 // announce remaining frags if not in qualifying mode
334 if(!g_race_qualifying)
338 MUTATOR_HOOKFUNCTION(rc, GetRecords)
340 int record_page = M_ARGV(0, int);
341 string ret_string = M_ARGV(1, string);
343 for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
345 if(MapInfo_Get_ByID(i))
347 float r = race_readTime(MapInfo_Map_bspname, 1);
352 string h = race_readName(MapInfo_Map_bspname, 1);
353 ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
357 M_ARGV(1, string) = ret_string;
360 MUTATOR_HOOKFUNCTION(rc, HideTeamNagger)
362 return true; // doesn't work so well
365 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
367 entity player = M_ARGV(0, entity);
369 stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
372 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
374 float checkrules_timelimit = M_ARGV(1, float);
375 float checkrules_fraglimit = M_ARGV(2, float);
377 if(checkrules_timelimit >= 0)
379 if(!g_race_qualifying)
381 M_ARGV(0, float) = WinningCondition_Race(checkrules_fraglimit);
384 else if(g_race_qualifying == 2)
386 M_ARGV(0, float) = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
392 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
394 if(g_race_qualifying == 2)
398 void race_Initialize()
401 if(g_race_qualifying == 2)
403 radar_showenemies = true;
408 int fraglimit_override, leadlimit_override;
409 float timelimit_override, qualifying_override;
411 if(autocvar_g_race_teams)
413 GameRules_teams(true);
414 race_teams = BITS(bound(2, autocvar_g_race_teams, 4));
419 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
420 fraglimit_override = autocvar_g_race_laps_limit;
421 leadlimit_override = 0; // currently not supported by race
422 timelimit_override = autocvar_timelimit_override;
424 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
426 record_type = RACE_RECORD;
428 if(autocvar_g_campaign)
430 g_race_qualifying = 1;
431 independent_players = 1;
433 else if(want_qualifying)
435 g_race_qualifying = 2;
436 independent_players = 1;
437 race_fraglimit = (fraglimit_override >= 0) ? fraglimit_override : autocvar_fraglimit;
438 race_leadlimit = (leadlimit_override >= 0) ? leadlimit_override : autocvar_leadlimit;
439 race_timelimit = (timelimit_override >= 0) ? timelimit_override : autocvar_timelimit;
440 qualifying_override = (qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit;
441 fraglimit_override = 0;
442 leadlimit_override = 0;
443 timelimit_override = qualifying_override;
446 g_race_qualifying = 0;
447 GameRules_limit_score(fraglimit_override);
448 GameRules_limit_lead(leadlimit_override);
449 GameRules_limit_time(timelimit_override);
450 GameRules_limit_time_qualifying(qualifying_override);