]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_race.qc
This commit is dedicated to TimePath
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_race.qc
1 #ifndef GAMEMODE_RACE_H
2 #define GAMEMODE_RACE_H
3
4 void rc_SetLimits();
5 void race_Initialize();
6
7 REGISTER_MUTATOR(rc, false)
8 {
9         MUTATOR_ONADD
10         {
11                 if (time > 1) // game loads at time 1
12                         error("This is a game type and it cannot be added at runtime.");
13                 race_Initialize();
14
15                 rc_SetLimits();
16         }
17
18         MUTATOR_ONROLLBACK_OR_REMOVE
19         {
20                 // we actually cannot roll back race_Initialize here
21                 // BUT: we don't need to! If this gets called, adding always
22                 // succeeds.
23         }
24
25         MUTATOR_ONREMOVE
26         {
27                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
28                 return -1;
29         }
30
31         return 0;
32 }
33
34 float race_teams;
35
36 // scores
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;
41 #endif
42
43 #ifdef IMPLEMENTATION
44
45 #include "../../race.qh"
46
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;
51
52 // legacy bot roles
53 .float race_checkpoint;
54 void havocbot_role_race()
55 {SELFPARAM();
56         if(self.deadflag != DEAD_NO)
57                 return;
58
59         entity e;
60         if (self.bot_strategytime < time)
61         {
62                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
63                 navigation_goalrating_start();
64
65                 for(e = world; (e = find(e, classname, "trigger_race_checkpoint")) != world; )
66                 {
67                         if(e.cnt == self.race_checkpoint)
68                         {
69                                 navigation_routerating(e, 1000000, 5000);
70                         }
71                         else if(self.race_checkpoint == -1)
72                         {
73                                 navigation_routerating(e, 1000000, 5000);
74                         }
75                 }
76
77                 navigation_goalrating_end();
78         }
79 }
80
81 void race_ScoreRules()
82 {
83         ScoreRules_basics(race_teams, 0, 0, false);
84         if(race_teams)
85         {
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);
90         }
91         else if(g_race_qualifying)
92         {
93                 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest",   SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
94         }
95         else
96         {
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);
100         }
101         ScoreRules_basics_end();
102 }
103
104 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
105 {
106         if(autocvar_sv_eventlog)
107                 GameLogEcho(strcat(":race:", mode, ":", ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
108 }
109
110 float WinningCondition_Race(float fraglimit)
111 {
112         float wc;
113         entity p;
114         float n, c;
115
116         n = 0;
117         c = 0;
118         FOR_EACH_PLAYER(p)
119         {
120                 ++n;
121                 if(p.race_completed)
122                         ++c;
123         }
124         if(n && (n == c))
125                 return WINNING_YES;
126         wc = WinningCondition_Scores(fraglimit, 0);
127
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;
132         else
133                 return WINNING_NEVER;
134 }
135
136 float WinningCondition_QualifyingThenRace(float limit)
137 {
138         float wc;
139         wc = WinningCondition_Scores(limit, 0);
140
141         // NEVER initiate overtime
142         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
143         {
144                 return WINNING_YES;
145         }
146
147         return wc;
148 }
149
150 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
151 {SELFPARAM();
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;
157
158 #ifdef SVQC
159         if(IS_PLAYER(self))
160         {
161                 if (self.race_penalty)
162                         if (time > self.race_penalty)
163                                 self.race_penalty = 0;
164                 if(self.race_penalty)
165                 {
166                         self.velocity = '0 0 0';
167                         self.movetype = MOVETYPE_NONE;
168                         self.disableclientprediction = 2;
169                 }
170         }
171 #endif
172
173         // force kbd movement for fairness
174         float wishspeed;
175         vector wishvel;
176
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)
184         {
185                 wishvel.z = 0;
186                 wishspeed = vlen(wishvel);
187                 if(wishvel.x >= 2 * wishvel.y)
188                 {
189                         // pure X motion
190                         if(self.movement.x > 0)
191                                 self.movement_x = wishspeed;
192                         else
193                                 self.movement_x = -wishspeed;
194                         self.movement_y = 0;
195                 }
196                 else if(wishvel.y >= 2 * wishvel.x)
197                 {
198                         // pure Y motion
199                         self.movement_x = 0;
200                         if(self.movement.y > 0)
201                                 self.movement_y = wishspeed;
202                         else
203                                 self.movement_y = -wishspeed;
204                 }
205                 else
206                 {
207                         // diagonal
208                         if(self.movement.x > 0)
209                                 self.movement_x = M_SQRT1_2 * wishspeed;
210                         else
211                                 self.movement_x = -M_SQRT1_2 * wishspeed;
212                         if(self.movement.y > 0)
213                                 self.movement_y = M_SQRT1_2 * wishspeed;
214                         else
215                                 self.movement_y = -M_SQRT1_2 * wishspeed;
216                 }
217         }
218
219         return false;
220 }
221
222 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
223 {
224         float s;
225
226         Score_NicePrint(world);
227
228         race_ClearRecords();
229         PlayerScore_Sort(race_place, 0, 1, 0);
230
231         entity e;
232         FOR_EACH_CLIENT(e)
233         {
234                 if(e.race_place)
235                 {
236                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
237                         if(!s)
238                                 e.race_place = 0;
239                 }
240                 race_EventLog(ftos(e.race_place), e);
241         }
242
243         if(g_race_qualifying == 2)
244         {
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));
250                 race_ScoreRules();
251         }
252
253         return false;
254 }
255
256 MUTATOR_HOOKFUNCTION(rc, PlayerPreThink)
257 {SELFPARAM();
258         if(IS_SPEC(self) || IS_OBSERVER(self))
259         if(g_race_qualifying)
260         if(msg_entity.enemy.race_laptime)
261                 race_SendNextCheckpoint(msg_entity.enemy, 1);
262
263         return false;
264 }
265
266 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
267 {SELFPARAM();
268         race_PreparePlayer();
269         self.race_checkpoint = -1;
270
271         string rr = RACE_RECORD;
272
273         if(IS_REAL_CLIENT(self))
274         {
275                 msg_entity = self;
276                 race_send_recordtime(MSG_ONE);
277                 race_send_speedaward(MSG_ONE);
278
279                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
280                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
281                 race_send_speedaward_alltimebest(MSG_ONE);
282
283                 float i;
284                 for (i = 1; i <= RANKINGS_CNT; ++i)
285                 {
286                         race_SendRankings(i, 0, 0, MSG_ONE);
287                 }
288         }
289
290         return false;
291 }
292
293 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
294 {SELFPARAM();
295         if(g_race_qualifying)
296         if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
297                 self.frags = FRAGS_LMS_LOSER;
298         else
299                 self.frags = FRAGS_SPECTATOR;
300
301         race_PreparePlayer();
302         self.race_checkpoint = -1;
303
304         return false;
305 }
306
307 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
308 {SELFPARAM();
309         if(spawn_spot.target == "")
310                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
311                 race_PreparePlayer();
312
313         // if we need to respawn, do it right
314         self.race_respawn_checkpoint = self.race_checkpoint;
315         self.race_respawn_spotref = spawn_spot;
316
317         self.race_place = 0;
318
319         return false;
320 }
321
322 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
323 {SELFPARAM();
324         if(IS_PLAYER(self))
325         if(!gameover)
326         {
327                 if(self.killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
328                         race_PreparePlayer();
329                 else // respawn
330                         race_RetractPlayer();
331
332                 race_AbandonRaceCheck(self);
333         }
334         return false;
335 }
336
337 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
338 {SELFPARAM();
339         self.respawn_flags |= RESPAWN_FORCE;
340         race_AbandonRaceCheck(self);
341         return false;
342 }
343
344 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
345 {SELFPARAM();
346         self.havocbot_role = havocbot_role_race;
347         return true;
348 }
349
350 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
351 {SELFPARAM();
352         if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
353         {
354                 if (!self.stored_netname)
355                         self.stored_netname = strzone(uid2name(self.crypto_idfp));
356                 if(self.stored_netname != self.netname)
357                 {
358                         db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
359                         strunzone(self.stored_netname);
360                         self.stored_netname = strzone(self.netname);
361                 }
362         }
363
364         if (!IS_OBSERVER(self))
365         {
366                 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
367                 {
368                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
369                         speedaward_holder = self.netname;
370                         speedaward_uid = self.crypto_idfp;
371                         speedaward_lastupdate = time;
372                 }
373                 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
374                 {
375                         string rr = RACE_RECORD;
376                         race_send_speedaward(MSG_ALL);
377                         speedaward_lastsent = speedaward_speed;
378                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
379                         {
380                                 speedaward_alltimebest = speedaward_speed;
381                                 speedaward_alltimebest_holder = speedaward_holder;
382                                 speedaward_alltimebest_uid = speedaward_uid;
383                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
384                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
385                                 race_send_speedaward_alltimebest(MSG_ALL);
386                         }
387                 }
388         }
389         return false;
390 }
391
392 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
393 {
394         if(g_race_qualifying)
395                 return true; // in qualifying, you don't lose score by observing
396
397         return false;
398 }
399
400 MUTATOR_HOOKFUNCTION(rc, GetTeamCount, CBC_ORDER_EXCLUSIVE)
401 {
402         ret_float = race_teams;
403         return false;
404 }
405
406 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
407 {
408         // announce remaining frags if not in qualifying mode
409         if(!g_race_qualifying)
410                 return true;
411
412         return false;
413 }
414
415 MUTATOR_HOOKFUNCTION(rc, GetRecords)
416 {
417         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
418         {
419                 if(MapInfo_Get_ByID(i))
420                 {
421                         float r = race_readTime(MapInfo_Map_bspname, 1);
422
423                         if(!r)
424                                 continue;
425
426                         string h = race_readName(MapInfo_Map_bspname, 1);
427                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
428                 }
429         }
430
431         return false;
432 }
433
434 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
435 {
436         stuffcmd(fix_client, "cl_cmd settemp cl_movecliptokeyboard 2\n");
437         return false;
438 }
439
440 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
441 {
442         if(checkrules_timelimit >= 0)
443         {
444                 if(!g_race_qualifying)
445                 {
446                         ret_float = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
447                         return true;
448                 }
449                 else if(g_race_qualifying == 2)
450                 {
451                         ret_float = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
452                         return true;
453                 }
454         }
455
456         return false;
457 }
458
459 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
460 {
461         if(g_race_qualifying == 2)
462                 warmup_stage = 0;
463         return false;
464 }
465
466 void race_Initialize()
467 {
468         race_ScoreRules();
469         if(g_race_qualifying == 2)
470                 warmup_stage = 0;
471 }
472
473 void rc_SetLimits()
474 {
475         int fraglimit_override, leadlimit_override;
476         float timelimit_override, qualifying_override;
477
478         if(autocvar_g_race_teams)
479         {
480                 ActivateTeamplay();
481                 race_teams = bound(2, autocvar_g_race_teams, 4);
482                 have_team_spawns = -1; // request team spawns
483         }
484         else
485                 race_teams = 0;
486
487         qualifying_override = autocvar_g_race_qualifying_timelimit_override;
488         fraglimit_override = autocvar_g_race_laps_limit;
489         leadlimit_override = 0; // currently not supported by race
490         timelimit_override = -1; // use default if we don't set it below
491
492         // we need to find out the correct value for g_race_qualifying
493         float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
494
495         if(autocvar_g_campaign)
496         {
497                 g_race_qualifying = 1;
498                 independent_players = 1;
499         }
500         else if(!autocvar_g_campaign && want_qualifying)
501         {
502                 g_race_qualifying = 2;
503                 independent_players = 1;
504                 race_fraglimit = (race_fraglimit >= 0) ? fraglimit_override : autocvar_fraglimit;
505                 race_leadlimit = (race_leadlimit >= 0) ? leadlimit_override : autocvar_leadlimit;
506                 race_timelimit = (race_timelimit >= 0) ? timelimit_override : autocvar_timelimit;
507                 fraglimit_override = 0;
508                 leadlimit_override = 0;
509                 timelimit_override = autocvar_g_race_qualifying_timelimit;
510         }
511         else
512                 g_race_qualifying = 0;
513
514         SetLimits(fraglimit_override, leadlimit_override, timelimit_override, qualifying_override);
515 }
516
517 #endif