]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_race.qc
Bot AI: slightly increase timeout for finding a new goal when bot is chasing a player...
[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                 IL_EACH(g_racecheckpoints, true,
22                 {
23                         if(it.cnt == this.race_checkpoint)
24                                 navigation_routerating(this, it, 1000000, 5000);
25                         else if(this.race_checkpoint == -1)
26                                 navigation_routerating(this, it, 1000000, 5000);
27                 });
28
29                 navigation_goalrating_end(this);
30
31                 navigation_goalrating_timeout_set(this);
32         }
33 }
34
35 void race_ScoreRules()
36 {
37     GameRules_score_enabled(false);
38         GameRules_scoring(race_teams, 0, 0, {
39         if (race_teams) {
40             field_team(ST_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
41             field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
42             field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
43             field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
44         } else if (g_race_qualifying) {
45             field(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
46         } else {
47             field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
48             field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
49             field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
50         }
51         });
52 }
53
54 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
55 {
56         if(autocvar_sv_eventlog)
57                 GameLogEcho(strcat(":race:", mode, ":", ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
58 }
59
60 float WinningCondition_Race(float fraglimit)
61 {
62         float wc;
63         float n, c;
64
65         n = 0;
66         c = 0;
67         FOREACH_CLIENT(IS_PLAYER(it), {
68                 ++n;
69                 if(CS(it).race_completed)
70                         ++c;
71         });
72         if(n && (n == c))
73                 return WINNING_YES;
74         wc = WinningCondition_Scores(fraglimit, 0);
75
76         // ALWAYS initiate overtime, unless EVERYONE has finished the race!
77         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
78         // do NOT support equality when the laps are all raced!
79                 return WINNING_STARTSUDDENDEATHOVERTIME;
80         else
81                 return WINNING_NEVER;
82 }
83
84 float WinningCondition_QualifyingThenRace(float limit)
85 {
86         float wc;
87         wc = WinningCondition_Scores(limit, 0);
88
89         // NEVER initiate overtime
90         if(wc == WINNING_YES || wc == WINNING_STARTSUDDENDEATHOVERTIME)
91         {
92                 return WINNING_YES;
93         }
94
95         return wc;
96 }
97
98 MUTATOR_HOOKFUNCTION(rc, ClientKill)
99 {
100         if(g_race_qualifying)
101                 M_ARGV(1, float) = 0; // killtime
102 }
103
104 MUTATOR_HOOKFUNCTION(rc, AbortSpeedrun)
105 {
106         entity player = M_ARGV(0, entity);
107
108         if(autocvar_g_allow_checkpoints)
109                 race_PreparePlayer(player); // nice try
110 }
111
112 MUTATOR_HOOKFUNCTION(rc, PlayerPhysics)
113 {
114         entity player = M_ARGV(0, entity);
115         float dt = M_ARGV(1, float);
116
117         player.race_movetime_frac += dt;
118         float f = floor(player.race_movetime_frac);
119         player.race_movetime_frac -= f;
120         player.race_movetime_count += f;
121         player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
122
123 #ifdef SVQC
124         if(IS_PLAYER(player))
125         {
126                 if (player.race_penalty)
127                         if (time > player.race_penalty)
128                                 player.race_penalty = 0;
129                 if(player.race_penalty)
130                 {
131                         player.velocity = '0 0 0';
132                         set_movetype(player, MOVETYPE_NONE);
133                         player.disableclientprediction = 2;
134                 }
135         }
136 #endif
137
138         // force kbd movement for fairness
139         float wishspeed;
140         vector wishvel;
141
142         // if record times matter
143         // ensure nothing EVIL is being done (i.e. div0_evade)
144         // this hinders joystick users though
145         // but it still gives SOME analog control
146         wishvel.x = fabs(CS(player).movement.x);
147         wishvel.y = fabs(CS(player).movement.y);
148         if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
149         {
150                 wishvel.z = 0;
151                 wishspeed = vlen(wishvel);
152                 if(wishvel.x >= 2 * wishvel.y)
153                 {
154                         // pure X motion
155                         if(CS(player).movement.x > 0)
156                                 CS(player).movement_x = wishspeed;
157                         else
158                                 CS(player).movement_x = -wishspeed;
159                         CS(player).movement_y = 0;
160                 }
161                 else if(wishvel.y >= 2 * wishvel.x)
162                 {
163                         // pure Y motion
164                         CS(player).movement_x = 0;
165                         if(CS(player).movement.y > 0)
166                                 CS(player).movement_y = wishspeed;
167                         else
168                                 CS(player).movement_y = -wishspeed;
169                 }
170                 else
171                 {
172                         // diagonal
173                         if(CS(player).movement.x > 0)
174                                 CS(player).movement_x = M_SQRT1_2 * wishspeed;
175                         else
176                                 CS(player).movement_x = -M_SQRT1_2 * wishspeed;
177                         if(CS(player).movement.y > 0)
178                                 CS(player).movement_y = M_SQRT1_2 * wishspeed;
179                         else
180                                 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
181                 }
182         }
183 }
184
185 MUTATOR_HOOKFUNCTION(rc, reset_map_global)
186 {
187         float s;
188
189         Score_NicePrint(NULL);
190
191         race_ClearRecords();
192         PlayerScore_Sort(race_place, 0, 1, 0);
193
194         FOREACH_CLIENT(true, {
195                 if(it.race_place)
196                 {
197                         s = GameRules_scoring_add(it, RACE_FASTEST, 0);
198                         if(!s)
199                                 it.race_place = 0;
200                 }
201                 race_EventLog(ftos(it.race_place), it);
202         });
203
204         if(g_race_qualifying == 2)
205         {
206                 g_race_qualifying = 0;
207                 independent_players = 0;
208                 cvar_set("fraglimit", ftos(race_fraglimit));
209                 cvar_set("leadlimit", ftos(race_leadlimit));
210                 cvar_set("timelimit", ftos(race_timelimit));
211                 race_ScoreRules();
212         }
213 }
214
215 MUTATOR_HOOKFUNCTION(rc, ClientConnect)
216 {
217         entity player = M_ARGV(0, entity);
218
219         race_PreparePlayer(player);
220         player.race_checkpoint = -1;
221
222         string rr = RACE_RECORD;
223
224         if(IS_REAL_CLIENT(player))
225         {
226                 msg_entity = player;
227                 race_send_recordtime(MSG_ONE);
228                 race_send_speedaward(MSG_ONE);
229
230                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
231                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
232                 race_send_speedaward_alltimebest(MSG_ONE);
233
234                 float i;
235                 for (i = 1; i <= RANKINGS_CNT; ++i)
236                 {
237                         race_SendRankings(i, 0, 0, MSG_ONE);
238                 }
239         }
240 }
241
242 MUTATOR_HOOKFUNCTION(rc, MakePlayerObserver)
243 {
244         entity player = M_ARGV(0, entity);
245
246         if(g_race_qualifying)
247         if(GameRules_scoring_add(player, RACE_FASTEST, 0))
248                 player.frags = FRAGS_LMS_LOSER;
249         else
250                 player.frags = FRAGS_SPECTATOR;
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         if(CS(player).cvar_cl_allow_uidtracking == 1 && CS(player).cvar_cl_allow_uid2name == 1)
309         {
310                 if (!player.stored_netname)
311                         player.stored_netname = strzone(uid2name(player.crypto_idfp));
312                 if(player.stored_netname != player.netname)
313                 {
314                         db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
315                         strunzone(player.stored_netname);
316                         player.stored_netname = strzone(player.netname);
317                 }
318         }
319
320         if (!IS_OBSERVER(player))
321         {
322                 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
323                 {
324                         speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
325                         speedaward_holder = player.netname;
326                         speedaward_uid = player.crypto_idfp;
327                         speedaward_lastupdate = time;
328                 }
329                 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
330                 {
331                         string rr = RACE_RECORD;
332                         race_send_speedaward(MSG_ALL);
333                         speedaward_lastsent = speedaward_speed;
334                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
335                         {
336                                 speedaward_alltimebest = speedaward_speed;
337                                 speedaward_alltimebest_holder = speedaward_holder;
338                                 speedaward_alltimebest_uid = speedaward_uid;
339                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
340                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
341                                 race_send_speedaward_alltimebest(MSG_ALL);
342                         }
343                 }
344         }
345 }
346
347 MUTATOR_HOOKFUNCTION(rc, ForbidPlayerScore_Clear)
348 {
349         if(g_race_qualifying)
350                 return true; // in qualifying, you don't lose score by observing
351 }
352
353 MUTATOR_HOOKFUNCTION(rc, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
354 {
355         M_ARGV(0, float) = race_teams;
356 }
357
358 MUTATOR_HOOKFUNCTION(rc, Scores_CountFragsRemaining)
359 {
360         // announce remaining frags if not in qualifying mode
361         if(!g_race_qualifying)
362                 return true;
363 }
364
365 MUTATOR_HOOKFUNCTION(rc, GetRecords)
366 {
367         int record_page = M_ARGV(0, int);
368         string ret_string = M_ARGV(1, string);
369
370         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
371         {
372                 if(MapInfo_Get_ByID(i))
373                 {
374                         float r = race_readTime(MapInfo_Map_bspname, 1);
375
376                         if(!r)
377                                 continue;
378
379                         string h = race_readName(MapInfo_Map_bspname, 1);
380                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
381                 }
382         }
383
384         M_ARGV(1, string) = ret_string;
385 }
386
387 MUTATOR_HOOKFUNCTION(rc, HideTeamNagger)
388 {
389         return true; // doesn't work so well
390 }
391
392 MUTATOR_HOOKFUNCTION(rc, FixClientCvars)
393 {
394         entity player = M_ARGV(0, entity);
395
396         stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
397 }
398
399 MUTATOR_HOOKFUNCTION(rc, CheckRules_World)
400 {
401         float checkrules_timelimit = M_ARGV(1, float);
402         float checkrules_fraglimit = M_ARGV(2, float);
403
404         if(checkrules_timelimit >= 0)
405         {
406                 if(!g_race_qualifying)
407                 {
408                         M_ARGV(0, float) = WinningCondition_Race(checkrules_fraglimit);
409                         return true;
410                 }
411                 else if(g_race_qualifying == 2)
412                 {
413                         M_ARGV(0, float) = WinningCondition_QualifyingThenRace(checkrules_fraglimit);
414                         return true;
415                 }
416         }
417 }
418
419 MUTATOR_HOOKFUNCTION(rc, ReadLevelCvars)
420 {
421         if(g_race_qualifying == 2)
422                 warmup_stage = 0;
423 }
424
425 void race_Initialize()
426 {
427         race_ScoreRules();
428         if(g_race_qualifying == 2)
429                 warmup_stage = 0;
430 }
431
432 void rc_SetLimits()
433 {
434         int fraglimit_override, leadlimit_override;
435         float timelimit_override, qualifying_override;
436
437         if(autocvar_g_race_teams)
438         {
439                 GameRules_teams(true);
440                 race_teams = BITS(bound(2, autocvar_g_race_teams, 4));
441         }
442         else
443                 race_teams = 0;
444
445         qualifying_override = autocvar_g_race_qualifying_timelimit_override;
446         fraglimit_override = autocvar_g_race_laps_limit;
447         leadlimit_override = 0; // currently not supported by race
448         timelimit_override = autocvar_timelimit_override;
449
450         float want_qualifying = ((qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit) > 0;
451
452         if(autocvar_g_campaign)
453         {
454                 g_race_qualifying = 1;
455                 independent_players = 1;
456         }
457         else if(want_qualifying)
458         {
459                 g_race_qualifying = 2;
460                 independent_players = 1;
461                 race_fraglimit = (fraglimit_override >= 0) ? fraglimit_override : autocvar_fraglimit;
462                 race_leadlimit = (leadlimit_override >= 0) ? leadlimit_override : autocvar_leadlimit;
463                 race_timelimit = (timelimit_override >= 0) ? timelimit_override : autocvar_timelimit;
464                 qualifying_override = (qualifying_override >= 0) ? qualifying_override : autocvar_g_race_qualifying_timelimit;
465                 fraglimit_override = 0;
466                 leadlimit_override = 0;
467                 timelimit_override = qualifying_override;
468         }
469         else
470                 g_race_qualifying = 0;
471     GameRules_limit_score(fraglimit_override);
472     GameRules_limit_lead(leadlimit_override);
473     GameRules_limit_time(timelimit_override);
474     GameRules_limit_time_qualifying(qualifying_override);
475 }