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