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