]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/race/sv_race.qc
Merge branch 'master' into terencehill/scoreboard_item_stats
[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, PlayerPreThink)
132 {
133         entity player = M_ARGV(0, entity);
134
135         if(!IS_PLAYER(player))
136                 return;
137
138         // we need to perform this in PlayerPreThink as PlayerPhysics may not be called (vehicle support)
139         float dt = frametime;
140         player.race_movetime_frac += dt;
141         float f = floor(player.race_movetime_frac);
142         player.race_movetime_frac -= f;
143         player.race_movetime_count += f;
144         player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
145 }
146
147 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
148 {
149         entity player = M_ARGV(0, entity);
150         //float dt = M_ARGV(1, float);
151
152 #ifdef SVQC
153         if(IS_PLAYER(player))
154         {
155                 if (player.race_penalty)
156                         if (time > player.race_penalty)
157                                 player.race_penalty = 0;
158                 if(player.race_penalty)
159                 {
160                         player.velocity = '0 0 0';
161                         set_movetype(player, MOVETYPE_NONE);
162                         player.disableclientprediction = 2;
163                 }
164         }
165 #endif
166
167         // force kbd movement for fairness
168         float wishspeed;
169         vector wishvel;
170
171         // if record times matter
172         // ensure nothing EVIL is being done (i.e. div0_evade)
173         // this hinders joystick users though
174         // but it still gives SOME analog control
175         wishvel.x = fabs(CS(player).movement.x);
176         wishvel.y = fabs(CS(player).movement.y);
177         if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
178         {
179                 wishvel.z = 0;
180                 wishspeed = vlen(wishvel);
181                 if(wishvel.x >= 2 * wishvel.y)
182                 {
183                         // pure X motion
184                         if(CS(player).movement.x > 0)
185                                 CS(player).movement_x = wishspeed;
186                         else
187                                 CS(player).movement_x = -wishspeed;
188                         CS(player).movement_y = 0;
189                 }
190                 else if(wishvel.y >= 2 * wishvel.x)
191                 {
192                         // pure Y motion
193                         CS(player).movement_x = 0;
194                         if(CS(player).movement.y > 0)
195                                 CS(player).movement_y = wishspeed;
196                         else
197                                 CS(player).movement_y = -wishspeed;
198                 }
199                 else
200                 {
201                         // diagonal
202                         if(CS(player).movement.x > 0)
203                                 CS(player).movement_x = M_SQRT1_2 * wishspeed;
204                         else
205                                 CS(player).movement_x = -M_SQRT1_2 * wishspeed;
206                         if(CS(player).movement.y > 0)
207                                 CS(player).movement_y = M_SQRT1_2 * wishspeed;
208                         else
209                                 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
210                 }
211         }
212 }
213
214 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
215 {
216         float s;
217
218         Score_NicePrint(NULL);
219
220         race_ClearRecords();
221         PlayerScore_Sort(race_place, 0, true, false);
222
223         FOREACH_CLIENT(true, {
224                 if(it.race_place)
225                 {
226                         s = GameRules_scoring_add(it, RACE_FASTEST, 0);
227                         if(!s)
228                                 it.race_place = 0;
229                 }
230                 race_EventLog(ftos(it.race_place), it);
231         });
232
233         if(g_race_qualifying == 2)
234         {
235                 g_race_qualifying = 0;
236                 independent_players = 0;
237                 cvar_set("fraglimit", ftos(race_fraglimit));
238                 cvar_set("leadlimit", ftos(race_leadlimit));
239                 cvar_set("timelimit", ftos(race_timelimit));
240                 race_ScoreRules();
241         }
242 }
243
244 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
245 {
246         entity player = M_ARGV(0, entity);
247
248         race_PreparePlayer(player);
249         player.race_checkpoint = -1;
250
251         string rr = RACE_RECORD;
252
253         if(IS_REAL_CLIENT(player))
254         {
255                 msg_entity = player;
256                 race_send_recordtime(MSG_ONE);
257                 race_send_speedaward(MSG_ONE);
258
259                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
260                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
261                 race_send_speedaward_alltimebest(MSG_ONE);
262
263                 float i;
264                 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
265                 race_send_rankings_cnt(MSG_ONE);
266                 for (i = 1; i <= m; ++i)
267                 {
268                         race_SendRankings(i, 0, 0, MSG_ONE);
269                 }
270         }
271 }
272
273 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
274 {
275         entity player = M_ARGV(0, entity);
276
277         if(g_race_qualifying)
278         {
279                 if(GameRules_scoring_add(player, RACE_FASTEST, 0))
280                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
281                 else
282                         player.frags = FRAGS_SPECTATOR;
283         }
284
285         race_PreparePlayer(player);
286         player.race_checkpoint = -1;
287 }
288
289 MUTATOR_HOOKFUNCTION(rc, PlayerSpawn)
290 {
291         entity player = M_ARGV(0, entity);
292         entity spawn_spot = M_ARGV(1, entity);
293
294         if(spawn_spot.target == "")
295                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
296                 race_PreparePlayer(player);
297
298         // if we need to respawn, do it right
299         player.race_respawn_checkpoint = player.race_checkpoint;
300         player.race_respawn_spotref = spawn_spot;
301
302         player.race_place = 0;
303 }
304
305 MUTATOR_HOOKFUNCTION(rc, PutClientInServer)
306 {
307         entity player = M_ARGV(0, entity);
308
309         if(IS_PLAYER(player))
310         if(!game_stopped)
311         {
312                 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
313                         race_PreparePlayer(player);
314                 else // respawn
315                         race_RetractPlayer(player);
316
317                 race_AbandonRaceCheck(player);
318         }
319 }
320
321 MUTATOR_HOOKFUNCTION(rc, PlayerDies)
322 {
323         entity frag_target = M_ARGV(2, entity);
324
325         frag_target.respawn_flags |= RESPAWN_FORCE;
326         race_AbandonRaceCheck(frag_target);
327 }
328
329 MUTATOR_HOOKFUNCTION(rc, HavocBot_ChooseRole)
330 {
331         entity bot = M_ARGV(0, entity);
332
333         bot.havocbot_role = havocbot_role_race;
334         return true;
335 }
336
337 MUTATOR_HOOKFUNCTION(rc, GetPressedKeys)
338 {
339         entity player = M_ARGV(0, entity);
340
341         if(CS_CVAR(player).cvar_cl_allow_uidtracking == 1 && CS_CVAR(player).cvar_cl_allow_uid2name == 1)
342         {
343                 if (!player.stored_netname)
344                         player.stored_netname = strzone(uid2name(player.crypto_idfp));
345                 if(player.stored_netname != player.netname)
346                 {
347                         db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
348                         strcpy(player.stored_netname, player.netname);
349                 }
350         }
351
352         if (!IS_OBSERVER(player))
353         {
354                 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
355                 {
356                         speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
357                         speedaward_holder = player.netname;
358                         speedaward_uid = player.crypto_idfp;
359                         speedaward_lastupdate = time;
360                 }
361                 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
362                 {
363                         string rr = RACE_RECORD;
364                         race_send_speedaward(MSG_ALL);
365                         speedaward_lastsent = speedaward_speed;
366                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
367                         {
368                                 speedaward_alltimebest = speedaward_speed;
369                                 speedaward_alltimebest_holder = speedaward_holder;
370                                 speedaward_alltimebest_uid = speedaward_uid;
371                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
372                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
373                                 race_send_speedaward_alltimebest(MSG_ALL);
374                         }
375                 }
376         }
377 }
378
379 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
380 {
381         if(g_race_qualifying)
382                 return true; // in qualifying, you don't lose score by observing
383 }
384
385 MUTATOR_HOOKFUNCTION(rc, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
386 {
387         M_ARGV(0, float) = race_teams;
388         return true;
389 }
390
391 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
392 {
393         // announce remaining frags if not in qualifying mode
394         if(!g_race_qualifying)
395                 return true;
396 }
397
398 MUTATOR_HOOKFUNCTION(rc, GetRecords)
399 {
400         int record_page = M_ARGV(0, int);
401         string ret_string = M_ARGV(1, string);
402
403         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
404         {
405                 if(MapInfo_Get_ByID(i))
406                 {
407                         float r = race_readTime(MapInfo_Map_bspname, 1);
408
409                         if(!r)
410                                 continue;
411
412                         string h = race_readName(MapInfo_Map_bspname, 1);
413                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
414                 }
415         }
416
417         M_ARGV(1, string) = ret_string;
418 }
419
420 MUTATOR_HOOKFUNCTION(rc, HideTeamNagger)
421 {
422         return true; // doesn't work so well
423 }
424
425 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
426 {
427         entity player = M_ARGV(0, entity);
428
429         stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
430 }
431
432 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
433 {
434         float checkrules_timelimit = M_ARGV(1, float);
435         float checkrules_fraglimit = M_ARGV(2, float);
436
437         if(checkrules_timelimit >= 0)
438         {
439                 if(!g_race_qualifying)
440                 {
441                         M_ARGV(0, float) = WinningCondition_Race(checkrules_fraglimit);
442                         return true;
443                 }
444                 else if(g_race_qualifying == 2)
445                 {
446                         M_ARGV(0, float) = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
447                         return true;
448                 }
449         }
450 }
451
452 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
453 {
454         if(g_race_qualifying == 2)
455                 warmup_stage = 0;
456 }
457
458 void race_Initialize()
459 {
460         race_ScoreRules();
461         if(g_race_qualifying == 2)
462                 warmup_stage = 0;
463         radar_showenemies = true;
464 }
465
466 void rc_SetLimits()
467 {
468         int fraglimit_override, leadlimit_override;
469         float timelimit_override, qualifying_override;
470
471         if(autocvar_g_race_teams)
472         {
473                 GameRules_teams(true);
474                 race_teams = BITS(bound(2, autocvar_g_race_teams, 4));
475         }
476         else
477                 race_teams = 0;
478
479         qualifying_override = autocvar_g_race_qualifying_timelimit_override;
480         fraglimit_override = autocvar_g_race_laps_limit;
481         leadlimit_override = 0; // currently not supported by race
482         timelimit_override = autocvar_timelimit_override;
483
484         float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
485
486         if(autocvar_g_campaign)
487         {
488                 g_race_qualifying = 1;
489                 independent_players = 1;
490         }
491         else if(want_qualifying)
492         {
493                 g_race_qualifying = 2;
494                 independent_players = 1;
495                 race_fraglimit = (fraglimit_override >= 0) ? fraglimit_override : autocvar_fraglimit;
496                 race_leadlimit = (leadlimit_override >= 0) ? leadlimit_override : autocvar_leadlimit;
497                 race_timelimit = (timelimit_override >= 0) ? timelimit_override : autocvar_timelimit;
498                 qualifying_override = (qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit;
499                 fraglimit_override = 0;
500                 leadlimit_override = 0;
501                 timelimit_override = qualifying_override;
502         }
503         else
504                 g_race_qualifying = 0;
505     GameRules_limit_score(fraglimit_override);
506     GameRules_limit_lead(leadlimit_override);
507     GameRules_limit_time(timelimit_override);
508     GameRules_limit_time_qualifying(qualifying_override);
509 }