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