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