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