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