]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_cts.qc
e72d898ce09b7dd0b86e5230a17cbeac27ae5aad
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_cts.qc
1 #include "../../race.qh"
2
3 #ifndef GAMEMODE_CTS_H
4 #define GAMEMODE_CTS_H
5
6 void cts_Initialize();
7
8 REGISTER_MUTATOR(cts, false)
9 {
10         MUTATOR_ONADD
11         {
12                 if (time > 1) // game loads at time 1
13                         error("This is a game type and it cannot be added at runtime.");
14
15                 g_race_qualifying = true;
16                 independent_players = 1;
17                 SetLimits(0, 0, 0, -1);
18
19                 cts_Initialize();
20         }
21
22         MUTATOR_ONROLLBACK_OR_REMOVE
23         {
24                 // we actually cannot roll back cts_Initialize here
25                 // BUT: we don't need to! If this gets called, adding always
26                 // succeeds.
27         }
28
29         MUTATOR_ONREMOVE
30         {
31                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
32                 return -1;
33         }
34
35         return 0;
36 }
37
38 // scores
39 const float ST_CTS_LAPS = 1;
40 const float SP_CTS_LAPS = 4;
41 const float SP_CTS_TIME = 5;
42 const float SP_CTS_FASTEST = 6;
43 #endif
44
45 #ifdef IMPLEMENTATION
46
47 #include "../../race.qh"
48
49 float autocvar_g_cts_finish_kill_delay;
50 bool autocvar_g_cts_selfdamage;
51
52 // legacy bot roles
53 .float race_checkpoint;
54 void havocbot_role_cts()
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 cts_ScoreRules()
82 {
83         ScoreRules_basics(0, 0, 0, false);
84         if(g_race_qualifying)
85         {
86                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_FASTEST, "fastest",   SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
87         }
88         else
89         {
90                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_LAPS,    "laps",      SFL_SORT_PRIO_PRIMARY);
91                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_TIME,    "time",      SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
92                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_FASTEST, "fastest",   SFL_LOWER_IS_BETTER | SFL_TIME);
93         }
94         ScoreRules_basics_end();
95 }
96
97 void cts_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
98 {
99         if(autocvar_sv_eventlog)
100                 GameLogEcho(strcat(":cts:", mode, ":", ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
101 }
102
103 void CTS_ClientKill(entity e) // silent version of ClientKill, used when player finishes a CTS run. Useful to prevent cheating by running back to the start line and starting out with more speed
104 {
105     e.killindicator = spawn();
106     e.killindicator.owner = e;
107     e.killindicator.think = KillIndicator_Think;
108     e.killindicator.nextthink = time + (e.lip) * 0.05;
109     e.killindicator.cnt = ceil(autocvar_g_cts_finish_kill_delay);
110     e.killindicator.health = 1; // this is used to indicate that it should be silent
111     e.lip = 0;
112 }
113
114 MUTATOR_HOOKFUNCTION(cts, PlayerPhysics)
115 {SELFPARAM();
116         self.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
117         float f = floor(self.race_movetime_frac);
118         self.race_movetime_frac -= f;
119         self.race_movetime_count += f;
120         self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
121
122 #ifdef SVQC
123         if(IS_PLAYER(self))
124         {
125                 if (self.race_penalty)
126                         if (time > self.race_penalty)
127                                 self.race_penalty = 0;
128                 if(self.race_penalty)
129                 {
130                         self.velocity = '0 0 0';
131                         self.movetype = MOVETYPE_NONE;
132                         self.disableclientprediction = 2;
133                 }
134         }
135 #endif
136
137         // force kbd movement for fairness
138         float wishspeed;
139         vector wishvel;
140
141         // if record times matter
142         // ensure nothing EVIL is being done (i.e. div0_evade)
143         // this hinders joystick users though
144         // but it still gives SOME analog control
145         wishvel.x = fabs(self.movement.x);
146         wishvel.y = fabs(self.movement.y);
147         if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
148         {
149                 wishvel.z = 0;
150                 wishspeed = vlen(wishvel);
151                 if(wishvel.x >= 2 * wishvel.y)
152                 {
153                         // pure X motion
154                         if(self.movement.x > 0)
155                                 self.movement_x = wishspeed;
156                         else
157                                 self.movement_x = -wishspeed;
158                         self.movement_y = 0;
159                 }
160                 else if(wishvel.y >= 2 * wishvel.x)
161                 {
162                         // pure Y motion
163                         self.movement_x = 0;
164                         if(self.movement.y > 0)
165                                 self.movement_y = wishspeed;
166                         else
167                                 self.movement_y = -wishspeed;
168                 }
169                 else
170                 {
171                         // diagonal
172                         if(self.movement.x > 0)
173                                 self.movement_x = M_SQRT1_2 * wishspeed;
174                         else
175                                 self.movement_x = -M_SQRT1_2 * wishspeed;
176                         if(self.movement.y > 0)
177                                 self.movement_y = M_SQRT1_2 * wishspeed;
178                         else
179                                 self.movement_y = -M_SQRT1_2 * wishspeed;
180                 }
181         }
182
183         return false;
184 }
185
186 MUTATOR_HOOKFUNCTION(cts, reset_map_global)
187 {
188         float s;
189
190         Score_NicePrint(world);
191
192         race_ClearRecords();
193         PlayerScore_Sort(race_place, 0, 1, 0);
194
195         entity e;
196         FOR_EACH_CLIENT(e)
197         {
198                 if(e.race_place)
199                 {
200                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
201                         if(!s)
202                                 e.race_place = 0;
203                 }
204                 cts_EventLog(ftos(e.race_place), e);
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                 cts_ScoreRules();
215         }
216
217         return false;
218 }
219
220 MUTATOR_HOOKFUNCTION(cts, PlayerPreThink)
221 {SELFPARAM();
222         if(IS_SPEC(self) || IS_OBSERVER(self))
223         if(g_race_qualifying)
224         if(msg_entity.enemy.race_laptime)
225                 race_SendNextCheckpoint(msg_entity.enemy, 1);
226
227         return false;
228 }
229
230 MUTATOR_HOOKFUNCTION(cts, ClientConnect)
231 {SELFPARAM();
232         race_PreparePlayer();
233         self.race_checkpoint = -1;
234
235         if(IS_REAL_CLIENT(self))
236         {
237                 string rr = CTS_RECORD;
238
239                 msg_entity = self;
240                 race_send_recordtime(MSG_ONE);
241                 race_send_speedaward(MSG_ONE);
242
243                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
244                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
245                 race_send_speedaward_alltimebest(MSG_ONE);
246
247                 float i;
248                 for (i = 1; i <= RANKINGS_CNT; ++i)
249                 {
250                         race_SendRankings(i, 0, 0, MSG_ONE);
251                 }
252         }
253
254         return false;
255 }
256
257 MUTATOR_HOOKFUNCTION(cts, MakePlayerObserver)
258 {SELFPARAM();
259         if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
260                 self.frags = FRAGS_LMS_LOSER;
261         else
262                 self.frags = FRAGS_SPECTATOR;
263
264         race_PreparePlayer();
265         self.race_checkpoint = -1;
266
267         return false;
268 }
269
270 MUTATOR_HOOKFUNCTION(cts, PlayerSpawn)
271 {SELFPARAM();
272         if(spawn_spot.target == "")
273                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
274                 race_PreparePlayer();
275
276         // if we need to respawn, do it right
277         self.race_respawn_checkpoint = self.race_checkpoint;
278         self.race_respawn_spotref = spawn_spot;
279
280         self.race_place = 0;
281
282         return false;
283 }
284
285 MUTATOR_HOOKFUNCTION(cts, PutClientInServer)
286 {SELFPARAM();
287         if(IS_PLAYER(self))
288         if(!gameover)
289         {
290                 if(self.killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
291                         race_PreparePlayer();
292                 else // respawn
293                         race_RetractPlayer();
294
295                 race_AbandonRaceCheck(self);
296         }
297         return false;
298 }
299
300 MUTATOR_HOOKFUNCTION(cts, PlayerDies)
301 {SELFPARAM();
302         self.respawn_flags |= RESPAWN_FORCE;
303         race_AbandonRaceCheck(self);
304         return false;
305 }
306
307 MUTATOR_HOOKFUNCTION(cts, HavocBot_ChooseRole)
308 {SELFPARAM();
309         self.havocbot_role = havocbot_role_cts;
310         return true;
311 }
312
313 MUTATOR_HOOKFUNCTION(cts, GetPressedKeys)
314 {SELFPARAM();
315         if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
316         {
317                 if (!self.stored_netname)
318                         self.stored_netname = strzone(uid2name(self.crypto_idfp));
319                 if(self.stored_netname != self.netname)
320                 {
321                         db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
322                         strunzone(self.stored_netname);
323                         self.stored_netname = strzone(self.netname);
324                 }
325         }
326
327         if (!IS_OBSERVER(self))
328         {
329                 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
330                 {
331                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
332                         speedaward_holder = self.netname;
333                         speedaward_uid = self.crypto_idfp;
334                         speedaward_lastupdate = time;
335                 }
336                 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
337                 {
338                         string rr = CTS_RECORD;
339                         race_send_speedaward(MSG_ALL);
340                         speedaward_lastsent = speedaward_speed;
341                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
342                         {
343                                 speedaward_alltimebest = speedaward_speed;
344                                 speedaward_alltimebest_holder = speedaward_holder;
345                                 speedaward_alltimebest_uid = speedaward_uid;
346                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
347                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
348                                 race_send_speedaward_alltimebest(MSG_ALL);
349                         }
350                 }
351         }
352
353         return false;
354 }
355
356 MUTATOR_HOOKFUNCTION(cts, ForbidThrowCurrentWeapon)
357 {
358         // no weapon dropping in CTS
359         return true;
360 }
361
362 MUTATOR_HOOKFUNCTION(cts, FilterItem)
363 {SELFPARAM();
364         if(self.classname == "droppedweapon")
365                 return true;
366
367         return false;
368 }
369
370 MUTATOR_HOOKFUNCTION(cts, PlayerDamage_Calculate)
371 {
372         if(frag_target == frag_attacker || frag_deathtype == DEATH_FALL.m_id)
373         if(!autocvar_g_cts_selfdamage)
374                 frag_damage = 0;
375
376         return false;
377 }
378
379 MUTATOR_HOOKFUNCTION(cts, ForbidPlayerScore_Clear)
380 {
381         return true; // in CTS, you don't lose score by observing
382 }
383
384 MUTATOR_HOOKFUNCTION(cts, GetRecords)
385 {
386         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
387         {
388                 if(MapInfo_Get_ByID(i))
389                 {
390                         float r = race_readTime(MapInfo_Map_bspname, 1);
391
392                         if(!r)
393                                 continue;
394
395                         string h = race_readName(MapInfo_Map_bspname, 1);
396                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
397                 }
398         }
399
400         return false;
401 }
402
403 MUTATOR_HOOKFUNCTION(cts, ClientKill)
404 {
405         ret_float = 0;
406
407         if(self.killindicator && self.killindicator.health == 1) // self.killindicator.health == 1 means that the kill indicator was spawned by CTS_ClientKill
408         {
409                 remove(self.killindicator);
410                 self.killindicator = world;
411
412                 ClientKill_Now(); // allow instant kill in this case
413                 return;
414         }
415
416 }
417
418 MUTATOR_HOOKFUNCTION(cts, Race_FinalCheckpoint)
419 {
420         if(autocvar_g_cts_finish_kill_delay)
421                 CTS_ClientKill(race_player);
422
423         return false;
424 }
425
426 MUTATOR_HOOKFUNCTION(cts, FixClientCvars)
427 {
428         stuffcmd(fix_client, "cl_cmd settemp cl_movecliptokeyboard 2\n");
429         return false;
430 }
431
432 MUTATOR_HOOKFUNCTION(cts, WantWeapon)
433 {
434         ret_float = (want_weaponinfo.weapon == WEP_SHOTGUN.m_id);
435         want_mutatorblocked = true;
436         return true;
437 }
438
439 void cts_Initialize()
440 {
441         cts_ScoreRules();
442 }
443
444 #endif