]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/race/sv_race.qc
Remove redundant SVQC check; avoid duplicated field definitions in Race GameRules_scoring
[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                 else if (g_race_qualifying)
61                         field(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
62                 if (race_teams || !g_race_qualifying)
63                 {
64                         field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
65                         field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
66                         field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
67                 }
68         });
69 }
70
71 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
72 {
73         if(autocvar_sv_eventlog)
74                 GameLogEcho(strcat(":race:", mode, ":", ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
75 }
76
77 float WinningCondition_Race(float fraglimit)
78 {
79         float wc;
80         float n, c;
81
82         n = 0;
83         c = 0;
84         FOREACH_CLIENT(IS_PLAYER(it), {
85                 ++n;
86                 if(CS(it).race_completed)
87                         ++c;
88         });
89         if(n && (n == c))
90                 return WINNING_YES;
91         wc = WinningCondition_Scores(fraglimit, 0);
92
93         // ALWAYS initiate overtime, unless EVERYONE has finished the race!
94         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
95         // do NOT support equality when the laps are all raced!
96                 return WINNING_STARTSUDDENDEATHOVERTIME;
97         else
98                 return WINNING_NEVER;
99 }
100
101 float WinningCondition_QualifyingThenRace(float limit)
102 {
103         float wc;
104         wc = WinningCondition_Scores(limit, 0);
105
106         // NEVER initiate overtime
107         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
108         {
109                 return WINNING_YES;
110         }
111
112         return wc;
113 }
114
115 MUTATOR_HOOKFUNCTION(rc, ClientKill)
116 {
117         if(g_race_qualifying)
118                 M_ARGV(1, float) = 0; // killtime
119 }
120
121 MUTATOR_HOOKFUNCTION(rc, AbortSpeedrun)
122 {
123         entity player = M_ARGV(0, entity);
124
125         if(autocvar_g_allow_checkpoints)
126                 race_PreparePlayer(player); // nice try
127 }
128
129 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
130 {
131         entity player = M_ARGV(0, entity);
132         float dt = M_ARGV(1, float);
133
134         player.race_movetime_frac += dt;
135         float f = floor(player.race_movetime_frac);
136         player.race_movetime_frac -= f;
137         player.race_movetime_count += f;
138         player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
139
140         if(IS_PLAYER(player))
141         {
142                 if (player.race_penalty)
143                         if (time > player.race_penalty)
144                                 player.race_penalty = 0;
145                 if(player.race_penalty)
146                 {
147                         player.velocity = '0 0 0';
148                         set_movetype(player, MOVETYPE_NONE);
149                         player.disableclientprediction = 2;
150                 }
151         }
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, true, false);
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         race_SendAll(player, false);
238 }
239
240 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
241 {
242         entity player = M_ARGV(0, entity);
243
244         if(g_race_qualifying)
245         {
246                 if(GameRules_scoring_add(player, RACE_FASTEST, 0))
247                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
248                 else
249                         player.frags = FRAGS_SPECTATOR;
250         }
251
252         race_PreparePlayer(player);
253         player.race_checkpoint = -1;
254 }
255
256 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
257 {
258         entity player = M_ARGV(0, entity);
259         entity spawn_spot = M_ARGV(1, entity);
260
261         if(spawn_spot.target == "")
262                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
263                 race_PreparePlayer(player);
264
265         // if we need to respawn, do it right
266         player.race_respawn_checkpoint = player.race_checkpoint;
267         player.race_respawn_spotref = spawn_spot;
268
269         player.race_place = 0;
270 }
271
272 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
273 {
274         entity player = M_ARGV(0, entity);
275
276         if(IS_PLAYER(player))
277         if(!game_stopped)
278         {
279                 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
280                         race_PreparePlayer(player);
281                 else // respawn
282                         race_RetractPlayer(player);
283
284                 race_AbandonRaceCheck(player);
285         }
286 }
287
288 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
289 {
290         entity frag_target = M_ARGV(2, entity);
291
292         frag_target.respawn_flags |= RESPAWN_FORCE;
293         race_AbandonRaceCheck(frag_target);
294 }
295
296 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
297 {
298         entity bot = M_ARGV(0, entity);
299
300         bot.havocbot_role = havocbot_role_race;
301         return true;
302 }
303
304 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
305 {
306         entity player = M_ARGV(0, entity);
307
308         race_checkAndWriteName(player);
309         race_SpeedAwardFrame(player);
310 }
311
312 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
313 {
314         if(g_race_qualifying)
315                 return true; // in qualifying, you don't lose score by observing
316 }
317
318 MUTATOR_HOOKFUNCTION(rc, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
319 {
320         M_ARGV(0, float) = race_teams;
321         return true;
322 }
323
324 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
325 {
326         // announce remaining frags if not in qualifying mode
327         if(!g_race_qualifying)
328                 return true;
329 }
330
331 MUTATOR_HOOKFUNCTION(rc, GetRecords)
332 {
333         int record_page = M_ARGV(0, int);
334         string ret_string = M_ARGV(1, string);
335
336         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
337         {
338                 if(MapInfo_Get_ByID(i))
339                 {
340                         float r = race_readTime(MapInfo_Map_bspname, 1);
341
342                         if(!r)
343                                 continue;
344
345                         string h = race_readName(MapInfo_Map_bspname, 1);
346                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
347                 }
348         }
349
350         M_ARGV(1, string) = ret_string;
351 }
352
353 MUTATOR_HOOKFUNCTION(rc, HideTeamNagger)
354 {
355         return true; // doesn't work so well
356 }
357
358 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
359 {
360         entity player = M_ARGV(0, entity);
361
362         stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
363 }
364
365 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
366 {
367         float checkrules_timelimit = M_ARGV(1, float);
368         float checkrules_fraglimit = M_ARGV(2, float);
369
370         if(checkrules_timelimit >= 0)
371         {
372                 if(!g_race_qualifying)
373                 {
374                         M_ARGV(0, float) = WinningCondition_Race(checkrules_fraglimit);
375                         return true;
376                 }
377                 else if(g_race_qualifying == 2)
378                 {
379                         M_ARGV(0, float) = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
380                         return true;
381                 }
382         }
383 }
384
385 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
386 {
387         if(g_race_qualifying == 2)
388                 warmup_stage = 0;
389 }
390
391 void race_Initialize()
392 {
393         race_ScoreRules();
394         if(g_race_qualifying == 2)
395                 warmup_stage = 0;
396         radar_showenemies = true;
397 }
398
399 void rc_SetLimits()
400 {
401         int fraglimit_override, leadlimit_override;
402         float timelimit_override, qualifying_override;
403
404         if(autocvar_g_race_teams)
405         {
406                 GameRules_teams(true);
407                 race_teams = BITS(bound(2, autocvar_g_race_teams, 4));
408         }
409         else
410                 race_teams = 0;
411
412         qualifying_override = autocvar_g_race_qualifying_timelimit_override;
413         fraglimit_override = autocvar_g_race_laps_limit;
414         leadlimit_override = 0; // currently not supported by race
415         timelimit_override = autocvar_timelimit_override;
416
417         float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
418
419         record_type = RACE_RECORD;
420
421         if(autocvar_g_campaign)
422         {
423                 g_race_qualifying = 1;
424                 independent_players = 1;
425         }
426         else if(want_qualifying)
427         {
428                 g_race_qualifying = 2;
429                 independent_players = 1;
430                 race_fraglimit = (fraglimit_override >= 0) ? fraglimit_override : autocvar_fraglimit;
431                 race_leadlimit = (leadlimit_override >= 0) ? leadlimit_override : autocvar_leadlimit;
432                 race_timelimit = (timelimit_override >= 0) ? timelimit_override : autocvar_timelimit;
433                 qualifying_override = (qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit;
434                 fraglimit_override = 0;
435                 leadlimit_override = 0;
436                 timelimit_override = qualifying_override;
437         }
438         else
439                 g_race_qualifying = 0;
440         GameRules_limit_score(fraglimit_override);
441         GameRules_limit_lead(leadlimit_override);
442         GameRules_limit_time(timelimit_override);
443         GameRules_limit_time_qualifying(qualifying_override);
444 }