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