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 (this.bot_strategytime < time)
19 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
20 navigation_goalrating_start(this);
22 IL_EACH(g_racecheckpoints, true,
24 if(it.cnt == this.race_checkpoint)
26 navigation_routerating(this, it, 1000000, 5000);
28 else if(this.race_checkpoint == -1)
30 navigation_routerating(this, it, 1000000, 5000);
34 navigation_goalrating_end(this);
38 void race_ScoreRules()
40 GameRules_score_enabled(false);
41 GameRules_scoring(race_teams, 0, 0, {
43 field_team(ST_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
44 field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
45 field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
46 field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
47 } else if (g_race_qualifying) {
48 field(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
50 field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
51 field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
52 field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
57 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
59 if(autocvar_sv_eventlog)
60 GameLogEcho(strcat(":race:", mode, ":", ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
63 float WinningCondition_Race(float fraglimit)
70 FOREACH_CLIENT(IS_PLAYER(it), {
72 if(CS(it).race_completed)
77 wc = WinningCondition_Scores(fraglimit, 0);
79 // ALWAYS initiate overtime, unless EVERYONE has finished the race!
80 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
81 // do NOT support equality when the laps are all raced!
82 return WINNING_STARTSUDDENDEATHOVERTIME;
87 float WinningCondition_QualifyingThenRace(float limit)
90 wc = WinningCondition_Scores(limit, 0);
92 // NEVER initiate overtime
93 if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
101 MUTATOR_HOOKFUNCTION(rc, ClientKill)
103 if(g_race_qualifying)
104 M_ARGV(1, float) = 0; // killtime
107 MUTATOR_HOOKFUNCTION(rc, AbortSpeedrun)
109 entity player = M_ARGV(0, entity);
111 if(autocvar_g_allow_checkpoints)
112 race_PreparePlayer(player); // nice try
115 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
117 entity player = M_ARGV(0, entity);
118 float dt = M_ARGV(1, float);
120 player.race_movetime_frac += dt;
121 float f = floor(player.race_movetime_frac);
122 player.race_movetime_frac -= f;
123 player.race_movetime_count += f;
124 player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
127 if(IS_PLAYER(player))
129 if (player.race_penalty)
130 if (time > player.race_penalty)
131 player.race_penalty = 0;
132 if(player.race_penalty)
134 player.velocity = '0 0 0';
135 set_movetype(player, MOVETYPE_NONE);
136 player.disableclientprediction = 2;
141 // force kbd movement for fairness
145 // if record times matter
146 // ensure nothing EVIL is being done (i.e. div0_evade)
147 // this hinders joystick users though
148 // but it still gives SOME analog control
149 wishvel.x = fabs(CS(player).movement.x);
150 wishvel.y = fabs(CS(player).movement.y);
151 if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
154 wishspeed = vlen(wishvel);
155 if(wishvel.x >= 2 * wishvel.y)
158 if(CS(player).movement.x > 0)
159 CS(player).movement_x = wishspeed;
161 CS(player).movement_x = -wishspeed;
162 CS(player).movement_y = 0;
164 else if(wishvel.y >= 2 * wishvel.x)
167 CS(player).movement_x = 0;
168 if(CS(player).movement.y > 0)
169 CS(player).movement_y = wishspeed;
171 CS(player).movement_y = -wishspeed;
176 if(CS(player).movement.x > 0)
177 CS(player).movement_x = M_SQRT1_2 * wishspeed;
179 CS(player).movement_x = -M_SQRT1_2 * wishspeed;
180 if(CS(player).movement.y > 0)
181 CS(player).movement_y = M_SQRT1_2 * wishspeed;
183 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
188 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
192 Score_NicePrint(NULL);
195 PlayerScore_Sort(race_place, 0, 1, 0);
197 FOREACH_CLIENT(true, {
200 s = GameRules_scoring_add(it, RACE_FASTEST, 0);
204 race_EventLog(ftos(it.race_place), it);
207 if(g_race_qualifying == 2)
209 g_race_qualifying = 0;
210 independent_players = 0;
211 cvar_set("fraglimit", ftos(race_fraglimit));
212 cvar_set("leadlimit", ftos(race_leadlimit));
213 cvar_set("timelimit", ftos(race_timelimit));
218 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
220 entity player = M_ARGV(0, entity);
222 race_PreparePlayer(player);
223 player.race_checkpoint = -1;
225 string rr = RACE_RECORD;
227 if(IS_REAL_CLIENT(player))
230 race_send_recordtime(MSG_ONE);
231 race_send_speedaward(MSG_ONE);
233 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
234 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
235 race_send_speedaward_alltimebest(MSG_ONE);
238 for (i = 1; i <= RANKINGS_CNT; ++i)
240 race_SendRankings(i, 0, 0, MSG_ONE);
245 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
247 entity player = M_ARGV(0, entity);
249 if(g_race_qualifying)
250 if(GameRules_scoring_add(player, RACE_FASTEST, 0))
251 player.frags = FRAGS_LMS_LOSER;
253 player.frags = FRAGS_SPECTATOR;
255 race_PreparePlayer(player);
256 player.race_checkpoint = -1;
259 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
261 entity player = M_ARGV(0, entity);
262 entity spawn_spot = M_ARGV(1, entity);
264 if(spawn_spot.target == "")
265 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
266 race_PreparePlayer(player);
268 // if we need to respawn, do it right
269 player.race_respawn_checkpoint = player.race_checkpoint;
270 player.race_respawn_spotref = spawn_spot;
272 player.race_place = 0;
275 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
277 entity player = M_ARGV(0, entity);
279 if(IS_PLAYER(player))
282 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
283 race_PreparePlayer(player);
285 race_RetractPlayer(player);
287 race_AbandonRaceCheck(player);
291 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
293 entity frag_target = M_ARGV(2, entity);
295 frag_target.respawn_flags |= RESPAWN_FORCE;
296 race_AbandonRaceCheck(frag_target);
299 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
301 entity bot = M_ARGV(0, entity);
303 bot.havocbot_role = havocbot_role_race;
307 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
309 entity player = M_ARGV(0, entity);
311 if(CS(player).cvar_cl_allow_uidtracking == 1 && CS(player).cvar_cl_allow_uid2name == 1)
313 if (!player.stored_netname)
314 player.stored_netname = strzone(uid2name(player.crypto_idfp));
315 if(player.stored_netname != player.netname)
317 db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
318 strunzone(player.stored_netname);
319 player.stored_netname = strzone(player.netname);
323 if (!IS_OBSERVER(player))
325 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
327 speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
328 speedaward_holder = player.netname;
329 speedaward_uid = player.crypto_idfp;
330 speedaward_lastupdate = time;
332 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
334 string rr = RACE_RECORD;
335 race_send_speedaward(MSG_ALL);
336 speedaward_lastsent = speedaward_speed;
337 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
339 speedaward_alltimebest = speedaward_speed;
340 speedaward_alltimebest_holder = speedaward_holder;
341 speedaward_alltimebest_uid = speedaward_uid;
342 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
343 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
344 race_send_speedaward_alltimebest(MSG_ALL);
350 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
352 if(g_race_qualifying)
353 return true; // in qualifying, you don't lose score by observing
356 MUTATOR_HOOKFUNCTION(rc, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
358 M_ARGV(0, float) = race_teams;
361 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
363 // announce remaining frags if not in qualifying mode
364 if(!g_race_qualifying)
368 MUTATOR_HOOKFUNCTION(rc, GetRecords)
370 int record_page = M_ARGV(0, int);
371 string ret_string = M_ARGV(1, string);
373 for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
375 if(MapInfo_Get_ByID(i))
377 float r = race_readTime(MapInfo_Map_bspname, 1);
382 string h = race_readName(MapInfo_Map_bspname, 1);
383 ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
387 M_ARGV(1, string) = ret_string;
390 MUTATOR_HOOKFUNCTION(rc, HideTeamNagger)
392 return true; // doesn't work so well
395 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
397 entity player = M_ARGV(0, entity);
399 stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
402 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
404 float checkrules_timelimit = M_ARGV(1, float);
405 float checkrules_fraglimit = M_ARGV(2, float);
407 if(checkrules_timelimit >= 0)
409 if(!g_race_qualifying)
411 M_ARGV(0, float) = WinningCondition_Race(checkrules_fraglimit);
414 else if(g_race_qualifying == 2)
416 M_ARGV(0, float) = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
422 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
424 if(g_race_qualifying == 2)
428 void race_Initialize()
431 if(g_race_qualifying == 2)
437 int fraglimit_override, leadlimit_override;
438 float timelimit_override, qualifying_override;
440 if(autocvar_g_race_teams)
442 GameRules_teams(true);
443 race_teams = BITS(bound(2, autocvar_g_race_teams, 4));
448 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
449 fraglimit_override = autocvar_g_race_laps_limit;
450 leadlimit_override = 0; // currently not supported by race
451 timelimit_override = autocvar_timelimit_override;
453 float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
455 if(autocvar_g_campaign)
457 g_race_qualifying = 1;
458 independent_players = 1;
460 else if(want_qualifying)
462 g_race_qualifying = 2;
463 independent_players = 1;
464 race_fraglimit = (fraglimit_override >= 0) ? fraglimit_override : autocvar_fraglimit;
465 race_leadlimit = (leadlimit_override >= 0) ? leadlimit_override : autocvar_leadlimit;
466 race_timelimit = (timelimit_override >= 0) ? timelimit_override : autocvar_timelimit;
467 qualifying_override = (qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit;
468 fraglimit_override = 0;
469 leadlimit_override = 0;
470 timelimit_override = qualifying_override;
473 g_race_qualifying = 0;
474 GameRules_limit_score(fraglimit_override);
475 GameRules_limit_lead(leadlimit_override);
476 GameRules_limit_time(timelimit_override);
477 GameRules_limit_time_qualifying(qualifying_override);