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