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