]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/race/sv_race.qc
Merge branch 'master' into terencehill/race_cleanups
[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, true, false);
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         if(IS_REAL_CLIENT(player))
242         {
243                 msg_entity = player;
244                 race_send_recordtime(MSG_ONE);
245                 race_send_speedaward(MSG_ONE);
246
247                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), record_type, "speed/speed")));
248                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), record_type, "speed/crypto_idfp")));
249                 race_send_speedaward_alltimebest(MSG_ONE);
250
251                 float i;
252                 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
253                 race_send_rankings_cnt(MSG_ONE);
254                 for (i = 1; i <= m; ++i)
255                 {
256                         race_SendRankings(i, 0, 0, MSG_ONE);
257                 }
258         }
259 }
260
261 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
262 {
263         entity player = M_ARGV(0, entity);
264
265         if(g_race_qualifying)
266         {
267                 if(GameRules_scoring_add(player, RACE_FASTEST, 0))
268                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
269                 else
270                         player.frags = FRAGS_SPECTATOR;
271         }
272
273         race_PreparePlayer(player);
274         player.race_checkpoint = -1;
275 }
276
277 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
278 {
279         entity player = M_ARGV(0, entity);
280         entity spawn_spot = M_ARGV(1, entity);
281
282         if(spawn_spot.target == "")
283                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
284                 race_PreparePlayer(player);
285
286         // if we need to respawn, do it right
287         player.race_respawn_checkpoint = player.race_checkpoint;
288         player.race_respawn_spotref = spawn_spot;
289
290         player.race_place = 0;
291 }
292
293 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
294 {
295         entity player = M_ARGV(0, entity);
296
297         if(IS_PLAYER(player))
298         if(!game_stopped)
299         {
300                 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
301                         race_PreparePlayer(player);
302                 else // respawn
303                         race_RetractPlayer(player);
304
305                 race_AbandonRaceCheck(player);
306         }
307 }
308
309 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
310 {
311         entity frag_target = M_ARGV(2, entity);
312
313         frag_target.respawn_flags |= RESPAWN_FORCE;
314         race_AbandonRaceCheck(frag_target);
315 }
316
317 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
318 {
319         entity bot = M_ARGV(0, entity);
320
321         bot.havocbot_role = havocbot_role_race;
322         return true;
323 }
324
325 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
326 {
327         entity player = M_ARGV(0, entity);
328
329         race_checkAndWriteName(player);
330
331         if (!IS_OBSERVER(player))
332         {
333                 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
334                 {
335                         speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
336                         speedaward_holder = player.netname;
337                         speedaward_uid = player.crypto_idfp;
338                         speedaward_lastupdate = time;
339                 }
340                 if (speedaward_speed > speedaward_lastsent && (time - speedaward_lastupdate > 1 || intermission_running))
341                 {
342                         race_send_speedaward(MSG_ALL);
343                         speedaward_lastsent = speedaward_speed;
344                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
345                         {
346                                 speedaward_alltimebest = speedaward_speed;
347                                 speedaward_alltimebest_holder = speedaward_holder;
348                                 speedaward_alltimebest_uid = speedaward_uid;
349                                 db_put(ServerProgsDB, strcat(GetMapname(), record_type, "speed/speed"), ftos(speedaward_alltimebest));
350                                 db_put(ServerProgsDB, strcat(GetMapname(), record_type, "speed/crypto_idfp"), speedaward_alltimebest_uid);
351                                 race_send_speedaward_alltimebest(MSG_ALL);
352                         }
353                 }
354         }
355 }
356
357 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
358 {
359         if(g_race_qualifying)
360                 return true; // in qualifying, you don't lose score by observing
361 }
362
363 MUTATOR_HOOKFUNCTION(rc, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
364 {
365         M_ARGV(0, float) = race_teams;
366         return true;
367 }
368
369 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
370 {
371         // announce remaining frags if not in qualifying mode
372         if(!g_race_qualifying)
373                 return true;
374 }
375
376 MUTATOR_HOOKFUNCTION(rc, GetRecords)
377 {
378         int record_page = M_ARGV(0, int);
379         string ret_string = M_ARGV(1, string);
380
381         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
382         {
383                 if(MapInfo_Get_ByID(i))
384                 {
385                         float r = race_readTime(MapInfo_Map_bspname, 1);
386
387                         if(!r)
388                                 continue;
389
390                         string h = race_readName(MapInfo_Map_bspname, 1);
391                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
392                 }
393         }
394
395         M_ARGV(1, string) = ret_string;
396 }
397
398 MUTATOR_HOOKFUNCTION(rc, HideTeamNagger)
399 {
400         return true; // doesn't work so well
401 }
402
403 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
404 {
405         entity player = M_ARGV(0, entity);
406
407         stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
408 }
409
410 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
411 {
412         float checkrules_timelimit = M_ARGV(1, float);
413         float checkrules_fraglimit = M_ARGV(2, float);
414
415         if(checkrules_timelimit >= 0)
416         {
417                 if(!g_race_qualifying)
418                 {
419                         M_ARGV(0, float) = WinningCondition_Race(checkrules_fraglimit);
420                         return true;
421                 }
422                 else if(g_race_qualifying == 2)
423                 {
424                         M_ARGV(0, float) = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
425                         return true;
426                 }
427         }
428 }
429
430 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
431 {
432         if(g_race_qualifying == 2)
433                 warmup_stage = 0;
434 }
435
436 void race_Initialize()
437 {
438         race_ScoreRules();
439         if(g_race_qualifying == 2)
440                 warmup_stage = 0;
441         radar_showenemies = true;
442 }
443
444 void rc_SetLimits()
445 {
446         int fraglimit_override, leadlimit_override;
447         float timelimit_override, qualifying_override;
448
449         if(autocvar_g_race_teams)
450         {
451                 GameRules_teams(true);
452                 race_teams = BITS(bound(2, autocvar_g_race_teams, 4));
453         }
454         else
455                 race_teams = 0;
456
457         qualifying_override = autocvar_g_race_qualifying_timelimit_override;
458         fraglimit_override = autocvar_g_race_laps_limit;
459         leadlimit_override = 0; // currently not supported by race
460         timelimit_override = autocvar_timelimit_override;
461
462         float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
463
464         record_type = RACE_RECORD;
465
466         if(autocvar_g_campaign)
467         {
468                 g_race_qualifying = 1;
469                 independent_players = 1;
470         }
471         else if(want_qualifying)
472         {
473                 g_race_qualifying = 2;
474                 independent_players = 1;
475                 race_fraglimit = (fraglimit_override >= 0) ? fraglimit_override : autocvar_fraglimit;
476                 race_leadlimit = (leadlimit_override >= 0) ? leadlimit_override : autocvar_leadlimit;
477                 race_timelimit = (timelimit_override >= 0) ? timelimit_override : autocvar_timelimit;
478                 qualifying_override = (qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit;
479                 fraglimit_override = 0;
480                 leadlimit_override = 0;
481                 timelimit_override = qualifying_override;
482         }
483         else
484                 g_race_qualifying = 0;
485     GameRules_limit_score(fraglimit_override);
486     GameRules_limit_lead(leadlimit_override);
487     GameRules_limit_time(timelimit_override);
488     GameRules_limit_time_qualifying(qualifying_override);
489 }