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 field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
61 field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
62 field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
63 } else if (g_race_qualifying) {
64 field(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
66 field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
67 field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
68 field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
73 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
75 if(autocvar_sv_eventlog)
76 GameLogEcho(strcat(":race:", mode, ":", ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
79 float WinningCondition_Race(float fraglimit)
86 FOREACH_CLIENT(IS_PLAYER(it), {
88 if(CS(it).race_completed)
93 wc = WinningCondition_Scores(fraglimit, 0);
95 // ALWAYS initiate overtime, unless EVERYONE has finished the race!
96 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
97 // do NOT support equality when the laps are all raced!
98 return WINNING_STARTSUDDENDEATHOVERTIME;
100 return WINNING_NEVER;
103 float WinningCondition_QualifyingThenRace(float limit)
106 wc = WinningCondition_Scores(limit, 0);
108 // NEVER initiate overtime
109 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
117 MUTATOR_HOOKFUNCTION(rc, ClientKill)
119 if(g_race_qualifying)
120 M_ARGV(1, float) = 0; // killtime
123 MUTATOR_HOOKFUNCTION(rc, AbortSpeedrun)
125 entity player = M_ARGV(0, entity);
127 if(autocvar_g_allow_checkpoints)
128 race_PreparePlayer(player); // nice try
131 MUTATOR_HOOKFUNCTION(rc, PlayerPreThink)
133 entity player = M_ARGV(0, entity);
135 if(!IS_PLAYER(player))
138 // we need to perform this in PlayerPreThink as PlayerPhysics may not be called (vehicle support)
139 float dt = frametime;
140 player.race_movetime_frac += dt;
141 float f = floor(player.race_movetime_frac);
142 player.race_movetime_frac -= f;
143 player.race_movetime_count += f;
144 player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
147 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
149 entity player = M_ARGV(0, entity);
150 //float dt = M_ARGV(1, float);
153 if(IS_PLAYER(player))
155 if (player.race_penalty)
156 if (time > player.race_penalty)
157 player.race_penalty = 0;
158 if(player.race_penalty)
160 player.velocity = '0 0 0';
161 set_movetype(player, MOVETYPE_NONE);
162 player.disableclientprediction = 2;
167 // force kbd movement for fairness
171 // if record times matter
172 // ensure nothing EVIL is being done (i.e. div0_evade)
173 // this hinders joystick users though
174 // but it still gives SOME analog control
175 wishvel.x = fabs(CS(player).movement.x);
176 wishvel.y = fabs(CS(player).movement.y);
177 if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
180 wishspeed = vlen(wishvel);
181 if(wishvel.x >= 2 * wishvel.y)
184 if(CS(player).movement.x > 0)
185 CS(player).movement_x = wishspeed;
187 CS(player).movement_x = -wishspeed;
188 CS(player).movement_y = 0;
190 else if(wishvel.y >= 2 * wishvel.x)
193 CS(player).movement_x = 0;
194 if(CS(player).movement.y > 0)
195 CS(player).movement_y = wishspeed;
197 CS(player).movement_y = -wishspeed;
202 if(CS(player).movement.x > 0)
203 CS(player).movement_x = M_SQRT1_2 * wishspeed;
205 CS(player).movement_x = -M_SQRT1_2 * wishspeed;
206 if(CS(player).movement.y > 0)
207 CS(player).movement_y = M_SQRT1_2 * wishspeed;
209 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
214 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
218 Score_NicePrint(NULL);
221 PlayerScore_Sort(race_place, 0, true, false);
223 FOREACH_CLIENT(true, {
226 s = GameRules_scoring_add(it, RACE_FASTEST, 0);
230 race_EventLog(ftos(it.race_place), it);
233 if(g_race_qualifying == 2)
235 g_race_qualifying = 0;
236 independent_players = 0;
237 cvar_set("fraglimit", ftos(race_fraglimit));
238 cvar_set("leadlimit", ftos(race_leadlimit));
239 cvar_set("timelimit", ftos(race_timelimit));
244 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
246 entity player = M_ARGV(0, entity);
248 race_PreparePlayer(player);
249 player.race_checkpoint = -1;
251 string rr = RACE_RECORD;
253 if(IS_REAL_CLIENT(player))
256 race_send_recordtime(MSG_ONE);
257 race_send_speedaward(MSG_ONE);
259 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
260 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
261 race_send_speedaward_alltimebest(MSG_ONE);
264 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
265 race_send_rankings_cnt(MSG_ONE);
266 for (i = 1; i <= m; ++i)
268 race_SendRankings(i, 0, 0, MSG_ONE);
273 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
275 entity player = M_ARGV(0, entity);
277 if(g_race_qualifying)
279 if(GameRules_scoring_add(player, RACE_FASTEST, 0))
280 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
282 player.frags = FRAGS_SPECTATOR;
285 race_PreparePlayer(player);
286 player.race_checkpoint = -1;
289 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
291 entity player = M_ARGV(0, entity);
292 entity spawn_spot = M_ARGV(1, entity);
294 if(spawn_spot.target == "")
295 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
296 race_PreparePlayer(player);
298 // if we need to respawn, do it right
299 player.race_respawn_checkpoint = player.race_checkpoint;
300 player.race_respawn_spotref = spawn_spot;
302 player.race_place = 0;
305 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
307 entity player = M_ARGV(0, entity);
309 if(IS_PLAYER(player))
312 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
313 race_PreparePlayer(player);
315 race_RetractPlayer(player);
317 race_AbandonRaceCheck(player);
321 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
323 entity frag_target = M_ARGV(2, entity);
325 frag_target.respawn_flags |= RESPAWN_FORCE;
326 race_AbandonRaceCheck(frag_target);
329 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
331 entity bot = M_ARGV(0, entity);
333 bot.havocbot_role = havocbot_role_race;
337 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
339 entity player = M_ARGV(0, entity);
341 if(CS_CVAR(player).cvar_cl_allow_uidtracking == 1 && CS_CVAR(player).cvar_cl_allow_uid2name == 1)
343 if (!player.stored_netname)
344 player.stored_netname = strzone(uid2name(player.crypto_idfp));
345 if(player.stored_netname != player.netname)
347 db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
348 strcpy(player.stored_netname, player.netname);
352 if (!IS_OBSERVER(player))
354 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
356 speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
357 speedaward_holder = player.netname;
358 speedaward_uid = player.crypto_idfp;
359 speedaward_lastupdate = time;
361 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
363 string rr = RACE_RECORD;
364 race_send_speedaward(MSG_ALL);
365 speedaward_lastsent = speedaward_speed;
366 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
368 speedaward_alltimebest = speedaward_speed;
369 speedaward_alltimebest_holder = speedaward_holder;
370 speedaward_alltimebest_uid = speedaward_uid;
371 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
372 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
373 race_send_speedaward_alltimebest(MSG_ALL);
379 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
381 if(g_race_qualifying)
382 return true; // in qualifying, you don't lose score by observing
385 MUTATOR_HOOKFUNCTION(rc, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
387 M_ARGV(0, float) = race_teams;
391 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
393 // announce remaining frags if not in qualifying mode
394 if(!g_race_qualifying)
398 MUTATOR_HOOKFUNCTION(rc, GetRecords)
400 int record_page = M_ARGV(0, int);
401 string ret_string = M_ARGV(1, string);
403 for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
405 if(MapInfo_Get_ByID(i))
407 float r = race_readTime(MapInfo_Map_bspname, 1);
412 string h = race_readName(MapInfo_Map_bspname, 1);
413 ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
417 M_ARGV(1, string) = ret_string;
420 MUTATOR_HOOKFUNCTION(rc, HideTeamNagger)
422 return true; // doesn't work so well
425 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
427 entity player = M_ARGV(0, entity);
429 stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
432 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
434 float checkrules_timelimit = M_ARGV(1, float);
435 float checkrules_fraglimit = M_ARGV(2, float);
437 if(checkrules_timelimit >= 0)
439 if(!g_race_qualifying)
441 M_ARGV(0, float) = WinningCondition_Race(checkrules_fraglimit);
444 else if(g_race_qualifying == 2)
446 M_ARGV(0, float) = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
452 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
454 if(g_race_qualifying == 2)
458 void race_Initialize()
461 if(g_race_qualifying == 2)
463 radar_showenemies = true;
468 int fraglimit_override, leadlimit_override;
469 float timelimit_override, qualifying_override;
471 if(autocvar_g_race_teams)
473 GameRules_teams(true);
474 race_teams = BITS(bound(2, autocvar_g_race_teams, 4));
479 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
480 fraglimit_override = autocvar_g_race_laps_limit;
481 leadlimit_override = 0; // currently not supported by race
482 timelimit_override = autocvar_timelimit_override;
484 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
486 if(autocvar_g_campaign)
488 g_race_qualifying = 1;
489 independent_players = 1;
491 else if(want_qualifying)
493 g_race_qualifying = 2;
494 independent_players = 1;
495 race_fraglimit = (fraglimit_override >= 0) ? fraglimit_override : autocvar_fraglimit;
496 race_leadlimit = (leadlimit_override >= 0) ? leadlimit_override : autocvar_leadlimit;
497 race_timelimit = (timelimit_override >= 0) ? timelimit_override : autocvar_timelimit;
498 qualifying_override = (qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit;
499 fraglimit_override = 0;
500 leadlimit_override = 0;
501 timelimit_override = qualifying_override;
504 g_race_qualifying = 0;
505 GameRules_limit_score(fraglimit_override);
506 GameRules_limit_lead(leadlimit_override);
507 GameRules_limit_time(timelimit_override);
508 GameRules_limit_time_qualifying(qualifying_override);