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