1 #ifndef GAMEMODE_RACE_H
2 #define GAMEMODE_RACE_H
5 void race_Initialize();
7 REGISTER_MUTATOR(rc, false)
11 if (time > 1) // game loads at time 1
12 error("This is a game type and it cannot be added at runtime.");
18 MUTATOR_ONROLLBACK_OR_REMOVE
20 // we actually cannot roll back race_Initialize here
21 // BUT: we don't need to! If this gets called, adding always
27 LOG_INFO("This is a game type and it cannot be removed at runtime.");
37 const float ST_RACE_LAPS = 1;
38 const float SP_RACE_LAPS = 4;
39 const float SP_RACE_TIME = 5;
40 const float SP_RACE_FASTEST = 6;
45 #include "../../race.qh"
47 #define autocvar_g_race_laps_limit cvar("g_race_laps_limit")
48 float autocvar_g_race_qualifying_timelimit;
49 float autocvar_g_race_qualifying_timelimit_override;
50 int autocvar_g_race_teams;
53 .float race_checkpoint;
54 void havocbot_role_race()
56 if(self.deadflag != DEAD_NO)
60 if (self.bot_strategytime < time)
62 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
63 navigation_goalrating_start();
65 for(e = world; (e = find(e, classname, "trigger_race_checkpoint")) != world; )
67 if(e.cnt == self.race_checkpoint)
69 navigation_routerating(e, 1000000, 5000);
71 else if(self.race_checkpoint == -1)
73 navigation_routerating(e, 1000000, 5000);
77 navigation_goalrating_end();
81 void race_ScoreRules()
83 ScoreRules_basics(race_teams, 0, 0, false);
86 ScoreInfo_SetLabel_TeamScore( ST_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
87 ScoreInfo_SetLabel_PlayerScore(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
88 ScoreInfo_SetLabel_PlayerScore(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
89 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
91 else if(g_race_qualifying)
93 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
97 ScoreInfo_SetLabel_PlayerScore(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
98 ScoreInfo_SetLabel_PlayerScore(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
99 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
101 ScoreRules_basics_end();
104 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
106 if(autocvar_sv_eventlog)
107 GameLogEcho(strcat(":race:", mode, ":", ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
110 float WinningCondition_Race(float fraglimit)
126 wc = WinningCondition_Scores(fraglimit, 0);
128 // ALWAYS initiate overtime, unless EVERYONE has finished the race!
129 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
130 // do NOT support equality when the laps are all raced!
131 return WINNING_STARTSUDDENDEATHOVERTIME;
133 return WINNING_NEVER;
136 float WinningCondition_QualifyingThenRace(float limit)
139 wc = WinningCondition_Scores(limit, 0);
141 // NEVER initiate overtime
142 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
150 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
152 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
153 float f = floor(self.race_movetime_frac);
154 self.race_movetime_frac -= f;
155 self.race_movetime_count += f;
156 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
161 if (self.race_penalty)
162 if (time > self.race_penalty)
163 self.race_penalty = 0;
164 if(self.race_penalty)
166 self.velocity = '0 0 0';
167 self.movetype = MOVETYPE_NONE;
168 self.disableclientprediction = 2;
173 // force kbd movement for fairness
177 // if record times matter
178 // ensure nothing EVIL is being done (i.e. div0_evade)
179 // this hinders joystick users though
180 // but it still gives SOME analog control
181 wishvel.x = fabs(self.movement.x);
182 wishvel.y = fabs(self.movement.y);
183 if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
186 wishspeed = vlen(wishvel);
187 if(wishvel.x >= 2 * wishvel.y)
190 if(self.movement.x > 0)
191 self.movement_x = wishspeed;
193 self.movement_x = -wishspeed;
196 else if(wishvel.y >= 2 * wishvel.x)
200 if(self.movement.y > 0)
201 self.movement_y = wishspeed;
203 self.movement_y = -wishspeed;
208 if(self.movement.x > 0)
209 self.movement_x = M_SQRT1_2 * wishspeed;
211 self.movement_x = -M_SQRT1_2 * wishspeed;
212 if(self.movement.y > 0)
213 self.movement_y = M_SQRT1_2 * wishspeed;
215 self.movement_y = -M_SQRT1_2 * wishspeed;
222 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
226 Score_NicePrint(world);
229 PlayerScore_Sort(race_place, 0, 1, 0);
236 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
240 race_EventLog(ftos(e.race_place), e);
243 if(g_race_qualifying == 2)
245 g_race_qualifying = 0;
246 independent_players = 0;
247 cvar_set("fraglimit", ftos(race_fraglimit));
248 cvar_set("leadlimit", ftos(race_leadlimit));
249 cvar_set("timelimit", ftos(race_timelimit));
256 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
258 race_PreparePlayer();
259 self.race_checkpoint = -1;
261 string rr = RACE_RECORD;
263 if(IS_REAL_CLIENT(self))
266 race_send_recordtime(MSG_ONE);
267 race_send_speedaward(MSG_ONE);
269 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
270 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
271 race_send_speedaward_alltimebest(MSG_ONE);
274 for (i = 1; i <= RANKINGS_CNT; ++i)
276 race_SendRankings(i, 0, 0, MSG_ONE);
283 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
285 if(g_race_qualifying)
286 if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
287 self.frags = FRAGS_LMS_LOSER;
289 self.frags = FRAGS_SPECTATOR;
291 race_PreparePlayer();
292 self.race_checkpoint = -1;
297 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
299 if(spawn_spot.target == "")
300 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
301 race_PreparePlayer();
303 // if we need to respawn, do it right
304 self.race_respawn_checkpoint = self.race_checkpoint;
305 self.race_respawn_spotref = spawn_spot;
312 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
317 if(self.killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
318 race_PreparePlayer();
320 race_RetractPlayer();
322 race_AbandonRaceCheck(self);
327 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
329 self.respawn_flags |= RESPAWN_FORCE;
330 race_AbandonRaceCheck(self);
334 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
336 self.havocbot_role = havocbot_role_race;
340 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
342 if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
344 if (!self.stored_netname)
345 self.stored_netname = strzone(uid2name(self.crypto_idfp));
346 if(self.stored_netname != self.netname)
348 db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
349 strunzone(self.stored_netname);
350 self.stored_netname = strzone(self.netname);
354 if (!IS_OBSERVER(self))
356 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
358 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
359 speedaward_holder = self.netname;
360 speedaward_uid = self.crypto_idfp;
361 speedaward_lastupdate = time;
363 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
365 string rr = RACE_RECORD;
366 race_send_speedaward(MSG_ALL);
367 speedaward_lastsent = speedaward_speed;
368 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
370 speedaward_alltimebest = speedaward_speed;
371 speedaward_alltimebest_holder = speedaward_holder;
372 speedaward_alltimebest_uid = speedaward_uid;
373 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
374 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
375 race_send_speedaward_alltimebest(MSG_ALL);
382 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
384 if(g_race_qualifying)
385 return true; // in qualifying, you don't lose score by observing
390 MUTATOR_HOOKFUNCTION(rc, GetTeamCount, CBC_ORDER_EXCLUSIVE)
392 ret_float = race_teams;
396 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
398 // announce remaining frags if not in qualifying mode
399 if(!g_race_qualifying)
405 MUTATOR_HOOKFUNCTION(rc, GetRecords)
407 for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
409 if(MapInfo_Get_ByID(i))
411 float r = race_readTime(MapInfo_Map_bspname, 1);
416 string h = race_readName(MapInfo_Map_bspname, 1);
417 ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
424 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
426 stuffcmd(fix_client, "cl_cmd settemp cl_movecliptokeyboard 2\n");
430 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
432 if(checkrules_timelimit >= 0)
434 if(!g_race_qualifying)
436 ret_float = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
439 else if(g_race_qualifying == 2)
441 ret_float = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
449 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
451 if(g_race_qualifying == 2)
456 void race_Initialize()
459 if(g_race_qualifying == 2)
465 int fraglimit_override, leadlimit_override;
466 float timelimit_override, qualifying_override;
468 if(autocvar_g_race_teams)
471 race_teams = bound(2, autocvar_g_race_teams, 4);
472 have_team_spawns = -1; // request team spawns
477 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
478 fraglimit_override = autocvar_g_race_laps_limit;
479 leadlimit_override = 0; // currently not supported by race
480 timelimit_override = -1; // use default if we don't set it below
482 // we need to find out the correct value for g_race_qualifying
483 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
485 if(autocvar_g_campaign)
487 g_race_qualifying = 1;
488 independent_players = 1;
490 else if(!autocvar_g_campaign && want_qualifying)
492 g_race_qualifying = 2;
493 independent_players = 1;
494 race_fraglimit = (race_fraglimit >= 0) ? fraglimit_override : autocvar_fraglimit;
495 race_leadlimit = (race_leadlimit >= 0) ? leadlimit_override : autocvar_leadlimit;
496 race_timelimit = (race_timelimit >= 0) ? timelimit_override : autocvar_timelimit;
497 fraglimit_override = 0;
498 leadlimit_override = 0;
499 timelimit_override = autocvar_g_race_qualifying_timelimit;
502 g_race_qualifying = 0;
504 SetLimits(fraglimit_override, leadlimit_override, timelimit_override, qualifying_override);