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