16de70e8c85fc3ab0646bfeb17375809d39ba212
[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, -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 "../../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         FOREACH_CLIENT(true, LAMBDA(
196                 if(it.race_place)
197                 {
198                         s = PlayerScore_Add(it, SP_RACE_FASTEST, 0);
199                         if(!s)
200                                 it.race_place = 0;
201                 }
202                 cts_EventLog(ftos(it.race_place), it);
203         ));
204
205         if(g_race_qualifying == 2)
206         {
207                 g_race_qualifying = 0;
208                 independent_players = 0;
209                 cvar_set("fraglimit", ftos(race_fraglimit));
210                 cvar_set("leadlimit", ftos(race_leadlimit));
211                 cvar_set("timelimit", ftos(race_timelimit));
212                 cts_ScoreRules();
213         }
214
215         return false;
216 }
217
218 MUTATOR_HOOKFUNCTION(cts, ClientConnect)
219 {SELFPARAM();
220         race_PreparePlayer();
221         self.race_checkpoint = -1;
222
223         if(IS_REAL_CLIENT(self))
224         {
225                 string rr = CTS_RECORD;
226
227                 msg_entity = self;
228                 race_send_recordtime(MSG_ONE);
229                 race_send_speedaward(MSG_ONE);
230
231                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
232                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
233                 race_send_speedaward_alltimebest(MSG_ONE);
234
235                 float i;
236                 for (i = 1; i <= RANKINGS_CNT; ++i)
237                 {
238                         race_SendRankings(i, 0, 0, MSG_ONE);
239                 }
240         }
241
242         return false;
243 }
244
245 MUTATOR_HOOKFUNCTION(cts, MakePlayerObserver)
246 {SELFPARAM();
247         if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
248                 self.frags = FRAGS_LMS_LOSER;
249         else
250                 self.frags = FRAGS_SPECTATOR;
251
252         race_PreparePlayer();
253         self.race_checkpoint = -1;
254
255         return false;
256 }
257
258 MUTATOR_HOOKFUNCTION(cts, PlayerSpawn)
259 {SELFPARAM();
260         if(spawn_spot.target == "")
261                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
262                 race_PreparePlayer();
263
264         // if we need to respawn, do it right
265         self.race_respawn_checkpoint = self.race_checkpoint;
266         self.race_respawn_spotref = spawn_spot;
267
268         self.race_place = 0;
269
270         return false;
271 }
272
273 MUTATOR_HOOKFUNCTION(cts, PutClientInServer)
274 {SELFPARAM();
275         if(IS_PLAYER(self))
276         if(!gameover)
277         {
278                 if(self.killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
279                         race_PreparePlayer();
280                 else // respawn
281                         race_RetractPlayer();
282
283                 race_AbandonRaceCheck(self);
284         }
285         return false;
286 }
287
288 MUTATOR_HOOKFUNCTION(cts, PlayerDies)
289 {SELFPARAM();
290         self.respawn_flags |= RESPAWN_FORCE;
291         race_AbandonRaceCheck(self);
292         return false;
293 }
294
295 MUTATOR_HOOKFUNCTION(cts, HavocBot_ChooseRole)
296 {SELFPARAM();
297         self.havocbot_role = havocbot_role_cts;
298         return true;
299 }
300
301 MUTATOR_HOOKFUNCTION(cts, GetPressedKeys)
302 {SELFPARAM();
303         if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
304         {
305                 if (!self.stored_netname)
306                         self.stored_netname = strzone(uid2name(self.crypto_idfp));
307                 if(self.stored_netname != self.netname)
308                 {
309                         db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
310                         strunzone(self.stored_netname);
311                         self.stored_netname = strzone(self.netname);
312                 }
313         }
314
315         if (!IS_OBSERVER(self))
316         {
317                 if (vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
318                 {
319                         speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
320                         speedaward_holder = self.netname;
321                         speedaward_uid = self.crypto_idfp;
322                         speedaward_lastupdate = time;
323                 }
324                 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
325                 {
326                         string rr = CTS_RECORD;
327                         race_send_speedaward(MSG_ALL);
328                         speedaward_lastsent = speedaward_speed;
329                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
330                         {
331                                 speedaward_alltimebest = speedaward_speed;
332                                 speedaward_alltimebest_holder = speedaward_holder;
333                                 speedaward_alltimebest_uid = speedaward_uid;
334                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
335                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
336                                 race_send_speedaward_alltimebest(MSG_ALL);
337                         }
338                 }
339         }
340
341         return false;
342 }
343
344 MUTATOR_HOOKFUNCTION(cts, ForbidThrowCurrentWeapon)
345 {
346         // no weapon dropping in CTS
347         return true;
348 }
349
350 MUTATOR_HOOKFUNCTION(cts, FilterItem)
351 {SELFPARAM();
352         if(self.classname == "droppedweapon")
353                 return true;
354
355         return false;
356 }
357
358 MUTATOR_HOOKFUNCTION(cts, PlayerDamage_Calculate)
359 {
360         if(frag_target == frag_attacker || frag_deathtype == DEATH_FALL.m_id)
361         if(!autocvar_g_cts_selfdamage)
362                 frag_damage = 0;
363
364         return false;
365 }
366
367 MUTATOR_HOOKFUNCTION(cts, ForbidPlayerScore_Clear)
368 {
369         return true; // in CTS, you don't lose score by observing
370 }
371
372 MUTATOR_HOOKFUNCTION(cts, GetRecords)
373 {
374         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
375         {
376                 if(MapInfo_Get_ByID(i))
377                 {
378                         float r = race_readTime(MapInfo_Map_bspname, 1);
379
380                         if(!r)
381                                 continue;
382
383                         string h = race_readName(MapInfo_Map_bspname, 1);
384                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
385                 }
386         }
387
388         return false;
389 }
390
391 MUTATOR_HOOKFUNCTION(cts, ClientKill)
392 {
393         ret_float = 0;
394
395         if(self.killindicator && self.killindicator.health == 1) // self.killindicator.health == 1 means that the kill indicator was spawned by CTS_ClientKill
396         {
397                 remove(self.killindicator);
398                 self.killindicator = world;
399
400                 ClientKill_Now(); // allow instant kill in this case
401                 return;
402         }
403
404 }
405
406 MUTATOR_HOOKFUNCTION(cts, Race_FinalCheckpoint)
407 {
408         if(autocvar_g_cts_finish_kill_delay)
409                 CTS_ClientKill(race_player);
410
411         return false;
412 }
413
414 MUTATOR_HOOKFUNCTION(cts, FixClientCvars)
415 {
416         stuffcmd(fix_client, "cl_cmd settemp cl_movecliptokeyboard 2\n");
417         return false;
418 }
419
420 MUTATOR_HOOKFUNCTION(cts, WantWeapon)
421 {
422         ret_float = (want_weaponinfo == WEP_SHOTGUN);
423         want_mutatorblocked = true;
424         return true;
425 }
426
427 void cts_Initialize()
428 {
429         cts_ScoreRules();
430 }
431
432 #endif