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