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