1 #include "gamemode_race.qh"
3 #include <server/race.qh>
5 #define autocvar_g_race_laps_limit cvar("g_race_laps_limit")
6 float autocvar_g_race_qualifying_timelimit;
7 float autocvar_g_race_qualifying_timelimit_override;
8 int autocvar_g_race_teams;
11 .float race_checkpoint;
12 void havocbot_role_race(entity this)
17 if (navigation_goalrating_timeout(this))
19 navigation_goalrating_start(this);
21 IL_EACH(g_racecheckpoints, true,
23 if(it.cnt == this.race_checkpoint)
24 navigation_routerating(this, it, 1000000, 5000);
25 else if(this.race_checkpoint == -1)
26 navigation_routerating(this, it, 1000000, 5000);
29 navigation_goalrating_end(this);
31 navigation_goalrating_timeout_set(this);
35 void race_ScoreRules()
37 GameRules_score_enabled(false);
38 GameRules_scoring(race_teams, 0, 0, {
40 field_team(ST_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
41 field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
42 field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
43 field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
44 } else if (g_race_qualifying) {
45 field(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
47 field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
48 field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
49 field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
54 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
56 if(autocvar_sv_eventlog)
57 GameLogEcho(strcat(":race:", mode, ":", ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
60 float WinningCondition_Race(float fraglimit)
67 FOREACH_CLIENT(IS_PLAYER(it), {
69 if(CS(it).race_completed)
74 wc = WinningCondition_Scores(fraglimit, 0);
76 // ALWAYS initiate overtime, unless EVERYONE has finished the race!
77 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
78 // do NOT support equality when the laps are all raced!
79 return WINNING_STARTSUDDENDEATHOVERTIME;
84 float WinningCondition_QualifyingThenRace(float limit)
87 wc = WinningCondition_Scores(limit, 0);
89 // NEVER initiate overtime
90 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
98 MUTATOR_HOOKFUNCTION(rc, ClientKill)
100 if(g_race_qualifying)
101 M_ARGV(1, float) = 0; // killtime
104 MUTATOR_HOOKFUNCTION(rc, AbortSpeedrun)
106 entity player = M_ARGV(0, entity);
108 if(autocvar_g_allow_checkpoints)
109 race_PreparePlayer(player); // nice try
112 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
114 entity player = M_ARGV(0, entity);
115 float dt = M_ARGV(1, float);
117 player.race_movetime_frac += dt;
118 float f = floor(player.race_movetime_frac);
119 player.race_movetime_frac -= f;
120 player.race_movetime_count += f;
121 player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
124 if(IS_PLAYER(player))
126 if (player.race_penalty)
127 if (time > player.race_penalty)
128 player.race_penalty = 0;
129 if(player.race_penalty)
131 player.velocity = '0 0 0';
132 set_movetype(player, MOVETYPE_NONE);
133 player.disableclientprediction = 2;
138 // force kbd movement for fairness
142 // if record times matter
143 // ensure nothing EVIL is being done (i.e. div0_evade)
144 // this hinders joystick users though
145 // but it still gives SOME analog control
146 wishvel.x = fabs(CS(player).movement.x);
147 wishvel.y = fabs(CS(player).movement.y);
148 if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
151 wishspeed = vlen(wishvel);
152 if(wishvel.x >= 2 * wishvel.y)
155 if(CS(player).movement.x > 0)
156 CS(player).movement_x = wishspeed;
158 CS(player).movement_x = -wishspeed;
159 CS(player).movement_y = 0;
161 else if(wishvel.y >= 2 * wishvel.x)
164 CS(player).movement_x = 0;
165 if(CS(player).movement.y > 0)
166 CS(player).movement_y = wishspeed;
168 CS(player).movement_y = -wishspeed;
173 if(CS(player).movement.x > 0)
174 CS(player).movement_x = M_SQRT1_2 * wishspeed;
176 CS(player).movement_x = -M_SQRT1_2 * wishspeed;
177 if(CS(player).movement.y > 0)
178 CS(player).movement_y = M_SQRT1_2 * wishspeed;
180 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
185 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
189 Score_NicePrint(NULL);
192 PlayerScore_Sort(race_place, 0, 1, 0);
194 FOREACH_CLIENT(true, {
197 s = GameRules_scoring_add(it, RACE_FASTEST, 0);
201 race_EventLog(ftos(it.race_place), it);
204 if(g_race_qualifying == 2)
206 g_race_qualifying = 0;
207 independent_players = 0;
208 cvar_set("fraglimit", ftos(race_fraglimit));
209 cvar_set("leadlimit", ftos(race_leadlimit));
210 cvar_set("timelimit", ftos(race_timelimit));
215 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
217 entity player = M_ARGV(0, entity);
219 race_PreparePlayer(player);
220 player.race_checkpoint = -1;
222 string rr = RACE_RECORD;
224 if(IS_REAL_CLIENT(player))
227 race_send_recordtime(MSG_ONE);
228 race_send_speedaward(MSG_ONE);
230 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
231 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
232 race_send_speedaward_alltimebest(MSG_ONE);
235 for (i = 1; i <= RANKINGS_CNT; ++i)
237 race_SendRankings(i, 0, 0, MSG_ONE);
242 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
244 entity player = M_ARGV(0, entity);
246 if(g_race_qualifying)
247 if(GameRules_scoring_add(player, RACE_FASTEST, 0))
248 player.frags = FRAGS_LMS_LOSER;
250 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, PlayerDies)
290 entity frag_target = M_ARGV(2, entity);
292 frag_target.respawn_flags |= RESPAWN_FORCE;
293 race_AbandonRaceCheck(frag_target);
296 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
298 entity bot = M_ARGV(0, entity);
300 bot.havocbot_role = havocbot_role_race;
304 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
306 entity player = M_ARGV(0, entity);
308 if(CS(player).cvar_cl_allow_uidtracking == 1 && CS(player).cvar_cl_allow_uid2name == 1)
310 if (!player.stored_netname)
311 player.stored_netname = strzone(uid2name(player.crypto_idfp));
312 if(player.stored_netname != player.netname)
314 db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
315 strunzone(player.stored_netname);
316 player.stored_netname = strzone(player.netname);
320 if (!IS_OBSERVER(player))
322 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
324 speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
325 speedaward_holder = player.netname;
326 speedaward_uid = player.crypto_idfp;
327 speedaward_lastupdate = time;
329 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
331 string rr = RACE_RECORD;
332 race_send_speedaward(MSG_ALL);
333 speedaward_lastsent = speedaward_speed;
334 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
336 speedaward_alltimebest = speedaward_speed;
337 speedaward_alltimebest_holder = speedaward_holder;
338 speedaward_alltimebest_uid = speedaward_uid;
339 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
340 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
341 race_send_speedaward_alltimebest(MSG_ALL);
347 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
349 if(g_race_qualifying)
350 return true; // in qualifying, you don't lose score by observing
353 MUTATOR_HOOKFUNCTION(rc, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
355 M_ARGV(0, float) = race_teams;
358 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
360 // announce remaining frags if not in qualifying mode
361 if(!g_race_qualifying)
365 MUTATOR_HOOKFUNCTION(rc, GetRecords)
367 int record_page = M_ARGV(0, int);
368 string ret_string = M_ARGV(1, string);
370 for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
372 if(MapInfo_Get_ByID(i))
374 float r = race_readTime(MapInfo_Map_bspname, 1);
379 string h = race_readName(MapInfo_Map_bspname, 1);
380 ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
384 M_ARGV(1, string) = ret_string;
387 MUTATOR_HOOKFUNCTION(rc, HideTeamNagger)
389 return true; // doesn't work so well
392 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
394 entity player = M_ARGV(0, entity);
396 stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
399 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
401 float checkrules_timelimit = M_ARGV(1, float);
402 float checkrules_fraglimit = M_ARGV(2, float);
404 if(checkrules_timelimit >= 0)
406 if(!g_race_qualifying)
408 M_ARGV(0, float) = WinningCondition_Race(checkrules_fraglimit);
411 else if(g_race_qualifying == 2)
413 M_ARGV(0, float) = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
419 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
421 if(g_race_qualifying == 2)
425 void race_Initialize()
428 if(g_race_qualifying == 2)
434 int fraglimit_override, leadlimit_override;
435 float timelimit_override, qualifying_override;
437 if(autocvar_g_race_teams)
439 GameRules_teams(true);
440 race_teams = BITS(bound(2, autocvar_g_race_teams, 4));
445 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
446 fraglimit_override = autocvar_g_race_laps_limit;
447 leadlimit_override = 0; // currently not supported by race
448 timelimit_override = autocvar_timelimit_override;
450 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
452 if(autocvar_g_campaign)
454 g_race_qualifying = 1;
455 independent_players = 1;
457 else if(want_qualifying)
459 g_race_qualifying = 2;
460 independent_players = 1;
461 race_fraglimit = (fraglimit_override >= 0) ? fraglimit_override : autocvar_fraglimit;
462 race_leadlimit = (leadlimit_override >= 0) ? leadlimit_override : autocvar_leadlimit;
463 race_timelimit = (timelimit_override >= 0) ? timelimit_override : autocvar_timelimit;
464 qualifying_override = (qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit;
465 fraglimit_override = 0;
466 leadlimit_override = 0;
467 timelimit_override = qualifying_override;
470 g_race_qualifying = 0;
471 GameRules_limit_score(fraglimit_override);
472 GameRules_limit_lead(leadlimit_override);
473 GameRules_limit_time(timelimit_override);
474 GameRules_limit_time_qualifying(qualifying_override);