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, PlayerPhysics)
133 entity player = M_ARGV(0, entity);
134 float dt = M_ARGV(1, float);
136 player.race_movetime_frac += dt;
137 float f = floor(player.race_movetime_frac);
138 player.race_movetime_frac -= f;
139 player.race_movetime_count += f;
140 player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
143 if(IS_PLAYER(player))
145 if (player.race_penalty)
146 if (time > player.race_penalty)
147 player.race_penalty = 0;
148 if(player.race_penalty)
150 player.velocity = '0 0 0';
151 set_movetype(player, MOVETYPE_NONE);
152 player.disableclientprediction = 2;
157 // force kbd movement for fairness
161 // if record times matter
162 // ensure nothing EVIL is being done (i.e. div0_evade)
163 // this hinders joystick users though
164 // but it still gives SOME analog control
165 wishvel.x = fabs(CS(player).movement.x);
166 wishvel.y = fabs(CS(player).movement.y);
167 if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
170 wishspeed = vlen(wishvel);
171 if(wishvel.x >= 2 * wishvel.y)
174 if(CS(player).movement.x > 0)
175 CS(player).movement_x = wishspeed;
177 CS(player).movement_x = -wishspeed;
178 CS(player).movement_y = 0;
180 else if(wishvel.y >= 2 * wishvel.x)
183 CS(player).movement_x = 0;
184 if(CS(player).movement.y > 0)
185 CS(player).movement_y = wishspeed;
187 CS(player).movement_y = -wishspeed;
192 if(CS(player).movement.x > 0)
193 CS(player).movement_x = M_SQRT1_2 * wishspeed;
195 CS(player).movement_x = -M_SQRT1_2 * wishspeed;
196 if(CS(player).movement.y > 0)
197 CS(player).movement_y = M_SQRT1_2 * wishspeed;
199 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
204 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
208 Score_NicePrint(NULL);
211 PlayerScore_Sort(race_place, 0, true, false);
213 FOREACH_CLIENT(true, {
216 s = GameRules_scoring_add(it, RACE_FASTEST, 0);
220 race_EventLog(ftos(it.race_place), it);
223 if(g_race_qualifying == 2)
225 g_race_qualifying = 0;
226 independent_players = 0;
227 cvar_set("fraglimit", ftos(race_fraglimit));
228 cvar_set("leadlimit", ftos(race_leadlimit));
229 cvar_set("timelimit", ftos(race_timelimit));
234 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
236 entity player = M_ARGV(0, entity);
238 race_PreparePlayer(player);
239 player.race_checkpoint = -1;
241 string rr = RACE_RECORD;
243 if(IS_REAL_CLIENT(player))
246 race_send_recordtime(MSG_ONE);
247 race_send_speedaward(MSG_ONE);
249 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
250 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
251 race_send_speedaward_alltimebest(MSG_ONE);
254 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
255 race_send_rankings_cnt(MSG_ONE);
256 for (i = 1; i <= m; ++i)
258 race_SendRankings(i, 0, 0, MSG_ONE);
263 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
265 entity player = M_ARGV(0, entity);
267 if(g_race_qualifying)
269 if(GameRules_scoring_add(player, RACE_FASTEST, 0))
270 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
272 player.frags = FRAGS_SPECTATOR;
275 race_PreparePlayer(player);
276 player.race_checkpoint = -1;
279 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
281 entity player = M_ARGV(0, entity);
282 entity spawn_spot = M_ARGV(1, entity);
284 if(spawn_spot.target == "")
285 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
286 race_PreparePlayer(player);
288 // if we need to respawn, do it right
289 player.race_respawn_checkpoint = player.race_checkpoint;
290 player.race_respawn_spotref = spawn_spot;
292 player.race_place = 0;
295 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
297 entity player = M_ARGV(0, entity);
299 if(IS_PLAYER(player))
302 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
303 race_PreparePlayer(player);
305 race_RetractPlayer(player);
307 race_AbandonRaceCheck(player);
311 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
313 entity frag_target = M_ARGV(2, entity);
315 frag_target.respawn_flags |= RESPAWN_FORCE;
316 race_AbandonRaceCheck(frag_target);
319 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
321 entity bot = M_ARGV(0, entity);
323 bot.havocbot_role = havocbot_role_race;
327 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
329 entity player = M_ARGV(0, entity);
331 if(CS_CVAR(player).cvar_cl_allow_uidtracking == 1 && CS_CVAR(player).cvar_cl_allow_uid2name == 1)
333 if (!player.stored_netname)
334 player.stored_netname = strzone(uid2name(player.crypto_idfp));
335 if(player.stored_netname != player.netname)
337 db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
338 strcpy(player.stored_netname, player.netname);
342 if (!IS_OBSERVER(player))
344 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
346 speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
347 speedaward_holder = player.netname;
348 speedaward_uid = player.crypto_idfp;
349 speedaward_lastupdate = time;
351 if (speedaward_speed > speedaward_lastsent && (time - speedaward_lastupdate > 1 || intermission_running))
353 string rr = RACE_RECORD;
354 race_send_speedaward(MSG_ALL);
355 speedaward_lastsent = speedaward_speed;
356 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
358 speedaward_alltimebest = speedaward_speed;
359 speedaward_alltimebest_holder = speedaward_holder;
360 speedaward_alltimebest_uid = speedaward_uid;
361 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
362 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
363 race_send_speedaward_alltimebest(MSG_ALL);
369 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
371 if(g_race_qualifying)
372 return true; // in qualifying, you don't lose score by observing
375 MUTATOR_HOOKFUNCTION(rc, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
377 M_ARGV(0, float) = race_teams;
381 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
383 // announce remaining frags if not in qualifying mode
384 if(!g_race_qualifying)
388 MUTATOR_HOOKFUNCTION(rc, GetRecords)
390 int record_page = M_ARGV(0, int);
391 string ret_string = M_ARGV(1, string);
393 for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
395 if(MapInfo_Get_ByID(i))
397 float r = race_readTime(MapInfo_Map_bspname, 1);
402 string h = race_readName(MapInfo_Map_bspname, 1);
403 ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
407 M_ARGV(1, string) = ret_string;
410 MUTATOR_HOOKFUNCTION(rc, HideTeamNagger)
412 return true; // doesn't work so well
415 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
417 entity player = M_ARGV(0, entity);
419 stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
422 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
424 float checkrules_timelimit = M_ARGV(1, float);
425 float checkrules_fraglimit = M_ARGV(2, float);
427 if(checkrules_timelimit >= 0)
429 if(!g_race_qualifying)
431 M_ARGV(0, float) = WinningCondition_Race(checkrules_fraglimit);
434 else if(g_race_qualifying == 2)
436 M_ARGV(0, float) = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
442 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
444 if(g_race_qualifying == 2)
448 void race_Initialize()
451 if(g_race_qualifying == 2)
453 radar_showenemies = true;
458 int fraglimit_override, leadlimit_override;
459 float timelimit_override, qualifying_override;
461 if(autocvar_g_race_teams)
463 GameRules_teams(true);
464 race_teams = BITS(bound(2, autocvar_g_race_teams, 4));
469 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
470 fraglimit_override = autocvar_g_race_laps_limit;
471 leadlimit_override = 0; // currently not supported by race
472 timelimit_override = autocvar_timelimit_override;
474 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
476 if(autocvar_g_campaign)
478 g_race_qualifying = 1;
479 independent_players = 1;
481 else if(want_qualifying)
483 g_race_qualifying = 2;
484 independent_players = 1;
485 race_fraglimit = (fraglimit_override >= 0) ? fraglimit_override : autocvar_fraglimit;
486 race_leadlimit = (leadlimit_override >= 0) ? leadlimit_override : autocvar_leadlimit;
487 race_timelimit = (timelimit_override >= 0) ? timelimit_override : autocvar_timelimit;
488 qualifying_override = (qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit;
489 fraglimit_override = 0;
490 leadlimit_override = 0;
491 timelimit_override = qualifying_override;
494 g_race_qualifying = 0;
495 GameRules_limit_score(fraglimit_override);
496 GameRules_limit_lead(leadlimit_override);
497 GameRules_limit_time(timelimit_override);
498 GameRules_limit_time_qualifying(qualifying_override);