1 #include "gamemode_race.qh"
3 #ifndef GAMEMODE_RACE_H
4 #define GAMEMODE_RACE_H
7 void race_Initialize();
9 REGISTER_MUTATOR(rc, false)
13 if (time > 1) // game loads at time 1
14 error("This is a game type and it cannot be added at runtime.");
20 MUTATOR_ONROLLBACK_OR_REMOVE
22 // we actually cannot roll back race_Initialize here
23 // BUT: we don't need to! If this gets called, adding always
29 LOG_INFO("This is a game type and it cannot be removed at runtime.");
40 #include <server/race.qh>
42 #define autocvar_g_race_laps_limit cvar("g_race_laps_limit")
43 float autocvar_g_race_qualifying_timelimit;
44 float autocvar_g_race_qualifying_timelimit_override;
45 int autocvar_g_race_teams;
48 .float race_checkpoint;
49 void havocbot_role_race()
55 if (self.bot_strategytime < time)
57 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
58 navigation_goalrating_start();
60 for(e = world; (e = find(e, classname, "trigger_race_checkpoint")) != world; )
62 if(e.cnt == self.race_checkpoint)
64 navigation_routerating(e, 1000000, 5000);
66 else if(self.race_checkpoint == -1)
68 navigation_routerating(e, 1000000, 5000);
72 navigation_goalrating_end();
76 void race_ScoreRules()
78 ScoreRules_basics(race_teams, 0, 0, false);
81 ScoreInfo_SetLabel_TeamScore( ST_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
82 ScoreInfo_SetLabel_PlayerScore(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
83 ScoreInfo_SetLabel_PlayerScore(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
84 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
86 else if(g_race_qualifying)
88 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
92 ScoreInfo_SetLabel_PlayerScore(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
93 ScoreInfo_SetLabel_PlayerScore(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
94 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
96 ScoreRules_basics_end();
99 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
101 if(autocvar_sv_eventlog)
102 GameLogEcho(strcat(":race:", mode, ":", ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
105 float WinningCondition_Race(float fraglimit)
112 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
114 if(it.race_completed)
119 wc = WinningCondition_Scores(fraglimit, 0);
121 // ALWAYS initiate overtime, unless EVERYONE has finished the race!
122 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
123 // do NOT support equality when the laps are all raced!
124 return WINNING_STARTSUDDENDEATHOVERTIME;
126 return WINNING_NEVER;
129 float WinningCondition_QualifyingThenRace(float limit)
132 wc = WinningCondition_Scores(limit, 0);
134 // NEVER initiate overtime
135 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
143 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
145 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
146 float f = floor(self.race_movetime_frac);
147 self.race_movetime_frac -= f;
148 self.race_movetime_count += f;
149 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
154 if (self.race_penalty)
155 if (time > self.race_penalty)
156 self.race_penalty = 0;
157 if(self.race_penalty)
159 self.velocity = '0 0 0';
160 self.movetype = MOVETYPE_NONE;
161 self.disableclientprediction = 2;
166 // force kbd movement for fairness
170 // if record times matter
171 // ensure nothing EVIL is being done (i.e. div0_evade)
172 // this hinders joystick users though
173 // but it still gives SOME analog control
174 wishvel.x = fabs(self.movement.x);
175 wishvel.y = fabs(self.movement.y);
176 if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
179 wishspeed = vlen(wishvel);
180 if(wishvel.x >= 2 * wishvel.y)
183 if(self.movement.x > 0)
184 self.movement_x = wishspeed;
186 self.movement_x = -wishspeed;
189 else if(wishvel.y >= 2 * wishvel.x)
193 if(self.movement.y > 0)
194 self.movement_y = wishspeed;
196 self.movement_y = -wishspeed;
201 if(self.movement.x > 0)
202 self.movement_x = M_SQRT1_2 * wishspeed;
204 self.movement_x = -M_SQRT1_2 * wishspeed;
205 if(self.movement.y > 0)
206 self.movement_y = M_SQRT1_2 * wishspeed;
208 self.movement_y = -M_SQRT1_2 * wishspeed;
215 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
219 Score_NicePrint(world);
222 PlayerScore_Sort(race_place, 0, 1, 0);
224 FOREACH_CLIENT(true, LAMBDA(
227 s = PlayerScore_Add(it, SP_RACE_FASTEST, 0);
231 race_EventLog(ftos(it.race_place), it);
234 if(g_race_qualifying == 2)
236 g_race_qualifying = 0;
237 independent_players = 0;
238 cvar_set("fraglimit", ftos(race_fraglimit));
239 cvar_set("leadlimit", ftos(race_leadlimit));
240 cvar_set("timelimit", ftos(race_timelimit));
247 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
249 race_PreparePlayer();
250 self.race_checkpoint = -1;
252 string rr = RACE_RECORD;
254 if(IS_REAL_CLIENT(self))
257 race_send_recordtime(MSG_ONE);
258 race_send_speedaward(MSG_ONE);
260 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
261 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
262 race_send_speedaward_alltimebest(MSG_ONE);
265 for (i = 1; i <= RANKINGS_CNT; ++i)
267 race_SendRankings(i, 0, 0, MSG_ONE);
274 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
276 if(g_race_qualifying)
277 if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
278 self.frags = FRAGS_LMS_LOSER;
280 self.frags = FRAGS_SPECTATOR;
282 race_PreparePlayer();
283 self.race_checkpoint = -1;
288 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
290 if(spawn_spot.target == "")
291 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
292 race_PreparePlayer();
294 // if we need to respawn, do it right
295 self.race_respawn_checkpoint = self.race_checkpoint;
296 self.race_respawn_spotref = spawn_spot;
303 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
308 if(self.killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
309 race_PreparePlayer();
311 race_RetractPlayer();
313 race_AbandonRaceCheck(self);
318 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
320 frag_target.respawn_flags |= RESPAWN_FORCE;
321 race_AbandonRaceCheck(frag_target);
325 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
327 self.havocbot_role = havocbot_role_race;
331 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
333 if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
335 if (!self.stored_netname)
336 self.stored_netname = strzone(uid2name(self.crypto_idfp));
337 if(self.stored_netname != self.netname)
339 db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
340 strunzone(self.stored_netname);
341 self.stored_netname = strzone(self.netname);
345 if (!IS_OBSERVER(self))
347 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
349 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
350 speedaward_holder = self.netname;
351 speedaward_uid = self.crypto_idfp;
352 speedaward_lastupdate = time;
354 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
356 string rr = RACE_RECORD;
357 race_send_speedaward(MSG_ALL);
358 speedaward_lastsent = speedaward_speed;
359 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
361 speedaward_alltimebest = speedaward_speed;
362 speedaward_alltimebest_holder = speedaward_holder;
363 speedaward_alltimebest_uid = speedaward_uid;
364 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
365 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
366 race_send_speedaward_alltimebest(MSG_ALL);
373 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
375 if(g_race_qualifying)
376 return true; // in qualifying, you don't lose score by observing
381 MUTATOR_HOOKFUNCTION(rc, GetTeamCount, CBC_ORDER_EXCLUSIVE)
383 ret_float = race_teams;
387 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
389 // announce remaining frags if not in qualifying mode
390 if(!g_race_qualifying)
396 MUTATOR_HOOKFUNCTION(rc, GetRecords)
398 for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
400 if(MapInfo_Get_ByID(i))
402 float r = race_readTime(MapInfo_Map_bspname, 1);
407 string h = race_readName(MapInfo_Map_bspname, 1);
408 ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
415 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
417 stuffcmd(fix_client, "cl_cmd settemp cl_movecliptokeyboard 2\n");
421 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
423 if(checkrules_timelimit >= 0)
425 if(!g_race_qualifying)
427 ret_float = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
430 else if(g_race_qualifying == 2)
432 ret_float = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
440 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
442 if(g_race_qualifying == 2)
447 void race_Initialize()
450 if(g_race_qualifying == 2)
456 int fraglimit_override, leadlimit_override;
457 float timelimit_override, qualifying_override;
459 if(autocvar_g_race_teams)
462 race_teams = bound(2, autocvar_g_race_teams, 4);
463 have_team_spawns = -1; // request team spawns
468 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
469 fraglimit_override = autocvar_g_race_laps_limit;
470 leadlimit_override = 0; // currently not supported by race
471 timelimit_override = -1; // use default if we don't set it below
473 // we need to find out the correct value for g_race_qualifying
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(!autocvar_g_campaign && want_qualifying)
483 g_race_qualifying = 2;
484 independent_players = 1;
485 race_fraglimit = (race_fraglimit >= 0) ? fraglimit_override : autocvar_fraglimit;
486 race_leadlimit = (race_leadlimit >= 0) ? leadlimit_override : autocvar_leadlimit;
487 race_timelimit = (race_timelimit >= 0) ? timelimit_override : autocvar_timelimit;
488 fraglimit_override = 0;
489 leadlimit_override = 0;
490 timelimit_override = autocvar_g_race_qualifying_timelimit;
493 g_race_qualifying = 0;
495 SetLimits(fraglimit_override, leadlimit_override, timelimit_override, qualifying_override);