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