]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_race.qc
Makefile: use `-I.`
[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
14                 rc_SetLimits();
15                 race_Initialize();
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 <server/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         float n, c;
114
115         n = 0;
116         c = 0;
117         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
118                 ++n;
119                 if(it.race_completed)
120                         ++c;
121         ));
122         if(n && (n == c))
123                 return WINNING_YES;
124         wc = WinningCondition_Scores(fraglimit, 0);
125
126         // ALWAYS initiate overtime, unless EVERYONE has finished the race!
127         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
128         // do NOT support equality when the laps are all raced!
129                 return WINNING_STARTSUDDENDEATHOVERTIME;
130         else
131                 return WINNING_NEVER;
132 }
133
134 float WinningCondition_QualifyingThenRace(float limit)
135 {
136         float wc;
137         wc = WinningCondition_Scores(limit, 0);
138
139         // NEVER initiate overtime
140         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
141         {
142                 return WINNING_YES;
143         }
144
145         return wc;
146 }
147
148 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
149 {SELFPARAM();
150         self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
151         float f = floor(self.race_movetime_frac);
152         self.race_movetime_frac -= f;
153         self.race_movetime_count += f;
154         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
155
156 #ifdef SVQC
157         if(IS_PLAYER(self))
158         {
159                 if (self.race_penalty)
160                         if (time > self.race_penalty)
161                                 self.race_penalty = 0;
162                 if(self.race_penalty)
163                 {
164                         self.velocity = '0 0 0';
165                         self.movetype = MOVETYPE_NONE;
166                         self.disableclientprediction = 2;
167                 }
168         }
169 #endif
170
171         // force kbd movement for fairness
172         float wishspeed;
173         vector wishvel;
174
175         // if record times matter
176         // ensure nothing EVIL is being done (i.e. div0_evade)
177         // this hinders joystick users though
178         // but it still gives SOME analog control
179         wishvel.x = fabs(self.movement.x);
180         wishvel.y = fabs(self.movement.y);
181         if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
182         {
183                 wishvel.z = 0;
184                 wishspeed = vlen(wishvel);
185                 if(wishvel.x >= 2 * wishvel.y)
186                 {
187                         // pure X motion
188                         if(self.movement.x > 0)
189                                 self.movement_x = wishspeed;
190                         else
191                                 self.movement_x = -wishspeed;
192                         self.movement_y = 0;
193                 }
194                 else if(wishvel.y >= 2 * wishvel.x)
195                 {
196                         // pure Y motion
197                         self.movement_x = 0;
198                         if(self.movement.y > 0)
199                                 self.movement_y = wishspeed;
200                         else
201                                 self.movement_y = -wishspeed;
202                 }
203                 else
204                 {
205                         // diagonal
206                         if(self.movement.x > 0)
207                                 self.movement_x = M_SQRT1_2 * wishspeed;
208                         else
209                                 self.movement_x = -M_SQRT1_2 * wishspeed;
210                         if(self.movement.y > 0)
211                                 self.movement_y = M_SQRT1_2 * wishspeed;
212                         else
213                                 self.movement_y = -M_SQRT1_2 * wishspeed;
214                 }
215         }
216
217         return false;
218 }
219
220 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
221 {
222         float s;
223
224         Score_NicePrint(world);
225
226         race_ClearRecords();
227         PlayerScore_Sort(race_place, 0, 1, 0);
228
229         FOREACH_CLIENT(true, LAMBDA(
230                 if(it.race_place)
231                 {
232                         s = PlayerScore_Add(it, SP_RACE_FASTEST, 0);
233                         if(!s)
234                                 it.race_place = 0;
235                 }
236                 race_EventLog(ftos(it.race_place), it);
237         ));
238
239         if(g_race_qualifying == 2)
240         {
241                 g_race_qualifying = 0;
242                 independent_players = 0;
243                 cvar_set("fraglimit", ftos(race_fraglimit));
244                 cvar_set("leadlimit", ftos(race_leadlimit));
245                 cvar_set("timelimit", ftos(race_timelimit));
246                 race_ScoreRules();
247         }
248
249         return false;
250 }
251
252 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
253 {SELFPARAM();
254         race_PreparePlayer();
255         self.race_checkpoint = -1;
256
257         string rr = RACE_RECORD;
258
259         if(IS_REAL_CLIENT(self))
260         {
261                 msg_entity = self;
262                 race_send_recordtime(MSG_ONE);
263                 race_send_speedaward(MSG_ONE);
264
265                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
266                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
267                 race_send_speedaward_alltimebest(MSG_ONE);
268
269                 float i;
270                 for (i = 1; i <= RANKINGS_CNT; ++i)
271                 {
272                         race_SendRankings(i, 0, 0, MSG_ONE);
273                 }
274         }
275
276         return false;
277 }
278
279 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
280 {SELFPARAM();
281         if(g_race_qualifying)
282         if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
283                 self.frags = FRAGS_LMS_LOSER;
284         else
285                 self.frags = FRAGS_SPECTATOR;
286
287         race_PreparePlayer();
288         self.race_checkpoint = -1;
289
290         return false;
291 }
292
293 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
294 {SELFPARAM();
295         if(spawn_spot.target == "")
296                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
297                 race_PreparePlayer();
298
299         // if we need to respawn, do it right
300         self.race_respawn_checkpoint = self.race_checkpoint;
301         self.race_respawn_spotref = spawn_spot;
302
303         self.race_place = 0;
304
305         return false;
306 }
307
308 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
309 {SELFPARAM();
310         if(IS_PLAYER(self))
311         if(!gameover)
312         {
313                 if(self.killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
314                         race_PreparePlayer();
315                 else // respawn
316                         race_RetractPlayer();
317
318                 race_AbandonRaceCheck(self);
319         }
320         return false;
321 }
322
323 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
324 {SELFPARAM();
325         self.respawn_flags |= RESPAWN_FORCE;
326         race_AbandonRaceCheck(self);
327         return false;
328 }
329
330 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
331 {SELFPARAM();
332         self.havocbot_role = havocbot_role_race;
333         return true;
334 }
335
336 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
337 {SELFPARAM();
338         if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
339         {
340                 if (!self.stored_netname)
341                         self.stored_netname = strzone(uid2name(self.crypto_idfp));
342                 if(self.stored_netname != self.netname)
343                 {
344                         db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
345                         strunzone(self.stored_netname);
346                         self.stored_netname = strzone(self.netname);
347                 }
348         }
349
350         if (!IS_OBSERVER(self))
351         {
352                 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
353                 {
354                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
355                         speedaward_holder = self.netname;
356                         speedaward_uid = self.crypto_idfp;
357                         speedaward_lastupdate = time;
358                 }
359                 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
360                 {
361                         string rr = RACE_RECORD;
362                         race_send_speedaward(MSG_ALL);
363                         speedaward_lastsent = speedaward_speed;
364                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
365                         {
366                                 speedaward_alltimebest = speedaward_speed;
367                                 speedaward_alltimebest_holder = speedaward_holder;
368                                 speedaward_alltimebest_uid = speedaward_uid;
369                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
370                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
371                                 race_send_speedaward_alltimebest(MSG_ALL);
372                         }
373                 }
374         }
375         return false;
376 }
377
378 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
379 {
380         if(g_race_qualifying)
381                 return true; // in qualifying, you don't lose score by observing
382
383         return false;
384 }
385
386 MUTATOR_HOOKFUNCTION(rc, GetTeamCount, CBC_ORDER_EXCLUSIVE)
387 {
388         ret_float = race_teams;
389         return false;
390 }
391
392 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
393 {
394         // announce remaining frags if not in qualifying mode
395         if(!g_race_qualifying)
396                 return true;
397
398         return false;
399 }
400
401 MUTATOR_HOOKFUNCTION(rc, GetRecords)
402 {
403         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
404         {
405                 if(MapInfo_Get_ByID(i))
406                 {
407                         float r = race_readTime(MapInfo_Map_bspname, 1);
408
409                         if(!r)
410                                 continue;
411
412                         string h = race_readName(MapInfo_Map_bspname, 1);
413                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
414                 }
415         }
416
417         return false;
418 }
419
420 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
421 {
422         stuffcmd(fix_client, "cl_cmd settemp cl_movecliptokeyboard 2\n");
423         return false;
424 }
425
426 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
427 {
428         if(checkrules_timelimit >= 0)
429         {
430                 if(!g_race_qualifying)
431                 {
432                         ret_float = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
433                         return true;
434                 }
435                 else if(g_race_qualifying == 2)
436                 {
437                         ret_float = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
438                         return true;
439                 }
440         }
441
442         return false;
443 }
444
445 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
446 {
447         if(g_race_qualifying == 2)
448                 warmup_stage = 0;
449         return false;
450 }
451
452 void race_Initialize()
453 {
454         race_ScoreRules();
455         if(g_race_qualifying == 2)
456                 warmup_stage = 0;
457 }
458
459 void rc_SetLimits()
460 {
461         int fraglimit_override, leadlimit_override;
462         float timelimit_override, qualifying_override;
463
464         if(autocvar_g_race_teams)
465         {
466                 ActivateTeamplay();
467                 race_teams = bound(2, autocvar_g_race_teams, 4);
468                 have_team_spawns = -1; // request team spawns
469         }
470         else
471                 race_teams = 0;
472
473         qualifying_override = autocvar_g_race_qualifying_timelimit_override;
474         fraglimit_override = autocvar_g_race_laps_limit;
475         leadlimit_override = 0; // currently not supported by race
476         timelimit_override = -1; // use default if we don't set it below
477
478         // we need to find out the correct value for g_race_qualifying
479         float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
480
481         if(autocvar_g_campaign)
482         {
483                 g_race_qualifying = 1;
484                 independent_players = 1;
485         }
486         else if(!autocvar_g_campaign && want_qualifying)
487         {
488                 g_race_qualifying = 2;
489                 independent_players = 1;
490                 race_fraglimit = (race_fraglimit >= 0) ? fraglimit_override : autocvar_fraglimit;
491                 race_leadlimit = (race_leadlimit >= 0) ? leadlimit_override : autocvar_leadlimit;
492                 race_timelimit = (race_timelimit >= 0) ? timelimit_override : autocvar_timelimit;
493                 fraglimit_override = 0;
494                 leadlimit_override = 0;
495                 timelimit_override = autocvar_g_race_qualifying_timelimit;
496         }
497         else
498                 g_race_qualifying = 0;
499
500         SetLimits(fraglimit_override, leadlimit_override, timelimit_override, qualifying_override);
501 }
502
503 #endif