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