1 #ifndef GAMEMODE_RACE_H
2 #define GAMEMODE_RACE_H
4 float g_race_qualifying;
8 const float ST_RACE_LAPS = 1;
9 const float SP_RACE_LAPS = 4;
10 const float SP_RACE_TIME = 5;
11 const float SP_RACE_FASTEST = 6;
16 #include "../../race.qh"
18 #define autocvar_g_race_laps_limit cvar("g_race_laps_limit")
19 float autocvar_g_race_qualifying_timelimit;
20 float autocvar_g_race_qualifying_timelimit_override;
21 int autocvar_g_race_teams;
24 .float race_checkpoint;
25 void havocbot_role_race()
27 if(self.deadflag != DEAD_NO)
31 if (self.bot_strategytime < time)
33 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
34 navigation_goalrating_start();
36 for(e = world; (e = find(e, classname, "trigger_race_checkpoint")) != world; )
38 if(e.cnt == self.race_checkpoint)
40 navigation_routerating(e, 1000000, 5000);
42 else if(self.race_checkpoint == -1)
44 navigation_routerating(e, 1000000, 5000);
48 navigation_goalrating_end();
52 void race_ScoreRules()
54 ScoreRules_basics(race_teams, 0, 0, false);
57 ScoreInfo_SetLabel_TeamScore( ST_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
58 ScoreInfo_SetLabel_PlayerScore(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
59 ScoreInfo_SetLabel_PlayerScore(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
60 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
62 else if(g_race_qualifying)
64 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
68 ScoreInfo_SetLabel_PlayerScore(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
69 ScoreInfo_SetLabel_PlayerScore(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
70 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
72 ScoreRules_basics_end();
75 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
77 if(autocvar_sv_eventlog)
78 GameLogEcho(strcat(":race:", mode, ":", ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
81 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
83 self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
84 float f = floor(self.race_movetime_frac);
85 self.race_movetime_frac -= f;
86 self.race_movetime_count += f;
87 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
92 if (self.race_penalty)
93 if (time > self.race_penalty)
94 self.race_penalty = 0;
97 self.velocity = '0 0 0';
98 self.movetype = MOVETYPE_NONE;
99 self.disableclientprediction = 2;
104 // force kbd movement for fairness
108 // if record times matter
109 // ensure nothing EVIL is being done (i.e. div0_evade)
110 // this hinders joystick users though
111 // but it still gives SOME analog control
112 wishvel.x = fabs(self.movement.x);
113 wishvel.y = fabs(self.movement.y);
114 if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
117 wishspeed = vlen(wishvel);
118 if(wishvel.x >= 2 * wishvel.y)
121 if(self.movement.x > 0)
122 self.movement_x = wishspeed;
124 self.movement_x = -wishspeed;
127 else if(wishvel.y >= 2 * wishvel.x)
131 if(self.movement.y > 0)
132 self.movement_y = wishspeed;
134 self.movement_y = -wishspeed;
139 if(self.movement.x > 0)
140 self.movement_x = M_SQRT1_2 * wishspeed;
142 self.movement_x = -M_SQRT1_2 * wishspeed;
143 if(self.movement.y > 0)
144 self.movement_y = M_SQRT1_2 * wishspeed;
146 self.movement_y = -M_SQRT1_2 * wishspeed;
153 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
157 Score_NicePrint(world);
160 PlayerScore_Sort(race_place, 0, 1, 0);
167 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
171 race_EventLog(ftos(e.race_place), e);
174 if(g_race_qualifying == 2)
176 g_race_qualifying = 0;
177 independent_players = 0;
178 cvar_set("fraglimit", ftos(race_fraglimit));
179 cvar_set("leadlimit", ftos(race_leadlimit));
180 cvar_set("timelimit", ftos(race_timelimit));
187 MUTATOR_HOOKFUNCTION(rc, PlayerPreThink)
189 if(IS_SPEC(self) || IS_OBSERVER(self))
190 if(g_race_qualifying)
191 if(msg_entity.enemy.race_laptime)
192 race_SendNextCheckpoint(msg_entity.enemy, 1);
197 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
199 race_PreparePlayer();
200 self.race_checkpoint = -1;
202 string rr = RACE_RECORD;
204 if(IS_REAL_CLIENT(self))
207 race_send_recordtime(MSG_ONE);
208 race_send_speedaward(MSG_ONE);
210 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
211 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
212 race_send_speedaward_alltimebest(MSG_ONE);
215 for (i = 1; i <= RANKINGS_CNT; ++i)
217 race_SendRankings(i, 0, 0, MSG_ONE);
224 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
226 if(g_race_qualifying)
227 if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
228 self.frags = FRAGS_LMS_LOSER;
230 self.frags = FRAGS_SPECTATOR;
232 race_PreparePlayer();
233 self.race_checkpoint = -1;
238 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
240 if(spawn_spot.target == "")
241 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
242 race_PreparePlayer();
244 // if we need to respawn, do it right
245 self.race_respawn_checkpoint = self.race_checkpoint;
246 self.race_respawn_spotref = spawn_spot;
253 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
258 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
259 race_PreparePlayer();
261 race_RetractPlayer();
263 race_AbandonRaceCheck(self);
268 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
270 self.respawn_flags |= RESPAWN_FORCE;
271 race_AbandonRaceCheck(self);
275 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
277 self.havocbot_role = havocbot_role_race;
281 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
283 if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
285 if (!self.stored_netname)
286 self.stored_netname = strzone(uid2name(self.crypto_idfp));
287 if(self.stored_netname != self.netname)
289 db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
290 strunzone(self.stored_netname);
291 self.stored_netname = strzone(self.netname);
295 if (!IS_OBSERVER(self))
297 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
299 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
300 speedaward_holder = self.netname;
301 speedaward_uid = self.crypto_idfp;
302 speedaward_lastupdate = time;
304 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
306 string rr = RACE_RECORD;
307 race_send_speedaward(MSG_ALL);
308 speedaward_lastsent = speedaward_speed;
309 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
311 speedaward_alltimebest = speedaward_speed;
312 speedaward_alltimebest_holder = speedaward_holder;
313 speedaward_alltimebest_uid = speedaward_uid;
314 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
315 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
316 race_send_speedaward_alltimebest(MSG_ALL);
323 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
325 if(g_race_qualifying)
326 return true; // in qualifying, you don't lose score by observing
331 MUTATOR_HOOKFUNCTION(rc, GetTeamCount, CBC_ORDER_EXCLUSIVE)
333 ret_float = race_teams;
337 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
339 // announce remaining frags if not in qualifying mode
340 if(!g_race_qualifying)
346 MUTATOR_HOOKFUNCTION(rc, GetRecords)
348 for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
350 if(MapInfo_Get_ByID(i))
352 float r = race_readTime(MapInfo_Map_bspname, 1);
357 string h = race_readName(MapInfo_Map_bspname, 1);
358 ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
365 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
367 stuffcmd(fix_client, "cl_cmd settemp cl_movecliptokeyboard 2\n");
371 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
373 if(g_race_qualifying == 2 && checkrules_timelimit >= 0)
375 ret_float = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
382 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
384 if(g_race_qualifying == 2)
389 void race_Initialize()
392 if(g_race_qualifying == 2)
398 int fraglimit_override, leadlimit_override;
399 float timelimit_override, qualifying_override;
401 if(autocvar_g_race_teams)
404 race_teams = bound(2, autocvar_g_race_teams, 4);
405 have_team_spawns = -1; // request team spawns
410 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
411 fraglimit_override = autocvar_g_race_laps_limit;
412 leadlimit_override = 0; // currently not supported by race
413 timelimit_override = -1; // use default if we don't set it below
415 // we need to find out the correct value for g_race_qualifying
416 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
418 if(autocvar_g_campaign)
420 g_race_qualifying = 1;
421 independent_players = 1;
423 else if(!autocvar_g_campaign && want_qualifying)
425 g_race_qualifying = 2;
426 independent_players = 1;
427 race_fraglimit = (race_fraglimit >= 0) ? fraglimit_override : autocvar_fraglimit;
428 race_leadlimit = (race_leadlimit >= 0) ? leadlimit_override : autocvar_leadlimit;
429 race_timelimit = (race_timelimit >= 0) ? timelimit_override : autocvar_timelimit;
430 fraglimit_override = 0;
431 leadlimit_override = 0;
432 timelimit_override = autocvar_g_race_qualifying_timelimit;
435 g_race_qualifying = 0;
437 SetLimits(fraglimit_override, leadlimit_override, timelimit_override, qualifying_override);
440 REGISTER_MUTATOR(rc, g_race)
446 if(time > 1) // game loads at time 1
447 error("This is a game type and it cannot be added at runtime.");
451 MUTATOR_ONROLLBACK_OR_REMOVE
453 // we actually cannot roll back race_Initialize here
454 // BUT: we don't need to! If this gets called, adding always
460 LOG_INFO("This is a game type and it cannot be removed at runtime.");