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