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