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