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