]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/cts/sv_cts.qc
Merge branch 'terencehill/clientkill' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / cts / sv_cts.qc
1 #include "sv_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 MUTATOR_HOOKFUNCTION(cts, PlayerPhysics)
68 {
69         entity player = M_ARGV(0, entity);
70         float dt = M_ARGV(1, float);
71
72         player.race_movetime_frac += dt;
73         float f = floor(player.race_movetime_frac);
74         player.race_movetime_frac -= f;
75         player.race_movetime_count += f;
76         player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
77
78         if(IS_PLAYER(player))
79         {
80                 if (player.race_penalty)
81                         if (time > player.race_penalty)
82                                 player.race_penalty = 0;
83                 if(player.race_penalty)
84                 {
85                         player.velocity = '0 0 0';
86                         set_movetype(player, MOVETYPE_NONE);
87                         player.disableclientprediction = 2;
88                 }
89         }
90
91         // force kbd movement for fairness
92         float wishspeed;
93         vector wishvel;
94
95         // if record times matter
96         // ensure nothing EVIL is being done (i.e. div0_evade)
97         // this hinders joystick users though
98         // but it still gives SOME analog control
99         wishvel.x = fabs(CS(player).movement.x);
100         wishvel.y = fabs(CS(player).movement.y);
101         if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
102         {
103                 wishvel.z = 0;
104                 wishspeed = vlen(wishvel);
105                 if(wishvel.x >= 2 * wishvel.y)
106                 {
107                         // pure X motion
108                         if(CS(player).movement.x > 0)
109                                 CS(player).movement_x = wishspeed;
110                         else
111                                 CS(player).movement_x = -wishspeed;
112                         CS(player).movement_y = 0;
113                 }
114                 else if(wishvel.y >= 2 * wishvel.x)
115                 {
116                         // pure Y motion
117                         CS(player).movement_x = 0;
118                         if(CS(player).movement.y > 0)
119                                 CS(player).movement_y = wishspeed;
120                         else
121                                 CS(player).movement_y = -wishspeed;
122                 }
123                 else
124                 {
125                         // diagonal
126                         if(CS(player).movement.x > 0)
127                                 CS(player).movement_x = M_SQRT1_2 * wishspeed;
128                         else
129                                 CS(player).movement_x = -M_SQRT1_2 * wishspeed;
130                         if(CS(player).movement.y > 0)
131                                 CS(player).movement_y = M_SQRT1_2 * wishspeed;
132                         else
133                                 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
134                 }
135         }
136 }
137
138 MUTATOR_HOOKFUNCTION(cts, reset_map_global)
139 {
140         float s;
141
142         Score_NicePrint(NULL);
143
144         race_ClearRecords();
145         PlayerScore_Sort(race_place, 0, 1, 0);
146
147         FOREACH_CLIENT(true, {
148                 if(it.race_place)
149                 {
150                         s = GameRules_scoring_add(it, RACE_FASTEST, 0);
151                         if(!s)
152                                 it.race_place = 0;
153                 }
154                 cts_EventLog(ftos(it.race_place), it);
155         });
156
157         if(g_race_qualifying == 2)
158         {
159                 g_race_qualifying = 0;
160                 independent_players = 0;
161                 cvar_set("fraglimit", ftos(race_fraglimit));
162                 cvar_set("leadlimit", ftos(race_leadlimit));
163                 cvar_set("timelimit", ftos(race_timelimit));
164                 cts_ScoreRules();
165         }
166 }
167
168 MUTATOR_HOOKFUNCTION(cts, ClientConnect)
169 {
170         entity player = M_ARGV(0, entity);
171
172         race_PreparePlayer(player);
173         player.race_checkpoint = -1;
174
175         if(IS_REAL_CLIENT(player))
176         {
177                 string rr = CTS_RECORD;
178
179                 msg_entity = player;
180                 race_send_recordtime(MSG_ONE);
181                 race_send_speedaward(MSG_ONE);
182
183                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
184                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
185                 race_send_speedaward_alltimebest(MSG_ONE);
186
187                 float i;
188                 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
189                 race_send_rankings_cnt(MSG_ONE);
190                 for (i = 1; i <= m; ++i)
191                 {
192                         race_SendRankings(i, 0, 0, MSG_ONE);
193                 }
194         }
195 }
196
197 MUTATOR_HOOKFUNCTION(cts, AbortSpeedrun)
198 {
199         entity player = M_ARGV(0, entity);
200
201         if(autocvar_g_allow_checkpoints)
202                 race_PreparePlayer(player); // nice try
203 }
204
205 MUTATOR_HOOKFUNCTION(cts, MakePlayerObserver)
206 {
207         entity player = M_ARGV(0, entity);
208
209         if(GameRules_scoring_add(player, RACE_FASTEST, 0))
210                 player.frags = FRAGS_LMS_LOSER;
211         else
212                 player.frags = FRAGS_SPECTATOR;
213
214         race_PreparePlayer(player);
215         player.race_checkpoint = -1;
216 }
217
218 MUTATOR_HOOKFUNCTION(cts, PlayerSpawn)
219 {
220         entity player = M_ARGV(0, entity);
221         entity spawn_spot = M_ARGV(1, entity);
222
223         if(spawn_spot.target == "")
224                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
225                 race_PreparePlayer(player);
226
227         // if we need to respawn, do it right
228         player.race_respawn_checkpoint = player.race_checkpoint;
229         player.race_respawn_spotref = spawn_spot;
230
231         player.race_place = 0;
232 }
233
234 MUTATOR_HOOKFUNCTION(cts, PutClientInServer)
235 {
236         entity player = M_ARGV(0, entity);
237
238         if(IS_PLAYER(player))
239         if(!game_stopped)
240         {
241                 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
242                         race_PreparePlayer(player);
243                 else // respawn
244                         race_RetractPlayer(player);
245
246                 race_AbandonRaceCheck(player);
247         }
248 }
249
250 MUTATOR_HOOKFUNCTION(cts, PlayerDies)
251 {
252         entity frag_target = M_ARGV(2, entity);
253
254         frag_target.respawn_flags |= RESPAWN_FORCE;
255         race_AbandonRaceCheck(frag_target);
256 }
257
258 MUTATOR_HOOKFUNCTION(cts, HavocBot_ChooseRole)
259 {
260         entity bot = M_ARGV(0, entity);
261
262         bot.havocbot_role = havocbot_role_cts;
263         return true;
264 }
265
266 MUTATOR_HOOKFUNCTION(cts, GetPressedKeys)
267 {
268         entity player = M_ARGV(0, entity);
269
270         if(CS(player).cvar_cl_allow_uidtracking == 1 && CS(player).cvar_cl_allow_uid2name == 1)
271         {
272                 if (!player.stored_netname)
273                         player.stored_netname = strzone(uid2name(player.crypto_idfp));
274                 if(player.stored_netname != player.netname)
275                 {
276                         db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
277                         strcpy(player.stored_netname, player.netname);
278                 }
279         }
280
281         if (!IS_OBSERVER(player))
282         {
283                 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
284                 {
285                         speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
286                         speedaward_holder = player.netname;
287                         speedaward_uid = player.crypto_idfp;
288                         speedaward_lastupdate = time;
289                 }
290                 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
291                 {
292                         string rr = CTS_RECORD;
293                         race_send_speedaward(MSG_ALL);
294                         speedaward_lastsent = speedaward_speed;
295                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
296                         {
297                                 speedaward_alltimebest = speedaward_speed;
298                                 speedaward_alltimebest_holder = speedaward_holder;
299                                 speedaward_alltimebest_uid = speedaward_uid;
300                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
301                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
302                                 race_send_speedaward_alltimebest(MSG_ALL);
303                         }
304                 }
305         }
306 }
307
308 MUTATOR_HOOKFUNCTION(cts, ForbidThrowCurrentWeapon)
309 {
310         // no weapon dropping in CTS
311         return true;
312 }
313
314 MUTATOR_HOOKFUNCTION(cts, FilterItem)
315 {
316         entity item = M_ARGV(0, entity);
317
318         if (Item_IsLoot(item))
319         {
320                 return true;
321         }
322 }
323
324 MUTATOR_HOOKFUNCTION(cts, Damage_Calculate)
325 {
326         entity frag_attacker = M_ARGV(1, entity);
327         entity frag_target = M_ARGV(2, entity);
328         float frag_deathtype = M_ARGV(3, float);
329         float frag_damage = M_ARGV(4, float);
330
331         if(frag_target == frag_attacker || frag_deathtype == DEATH_FALL.m_id)
332         if(!autocvar_g_cts_selfdamage)
333         {
334                 frag_damage = 0;
335                 M_ARGV(4, float) = frag_damage;
336         }
337 }
338
339 MUTATOR_HOOKFUNCTION(cts, ForbidPlayerScore_Clear)
340 {
341         return true; // in CTS, you don't lose score by observing
342 }
343
344 MUTATOR_HOOKFUNCTION(cts, GetRecords)
345 {
346         int record_page = M_ARGV(0, int);
347         string ret_string = M_ARGV(1, string);
348
349         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
350         {
351                 if(MapInfo_Get_ByID(i))
352                 {
353                         float r = race_readTime(MapInfo_Map_bspname, 1);
354
355                         if(!r)
356                                 continue;
357
358                         string h = race_readName(MapInfo_Map_bspname, 1);
359                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
360                 }
361         }
362
363         M_ARGV(1, string) = ret_string;
364 }
365
366 MUTATOR_HOOKFUNCTION(cts, ClientKill)
367 {
368         M_ARGV(1, float) = 0; // kill delay
369 }
370
371 MUTATOR_HOOKFUNCTION(cts, Race_FinalCheckpoint)
372 {
373         entity player = M_ARGV(0, entity);
374
375         // useful to prevent cheating by running back to the start line and starting out with more speed
376         if(autocvar_g_cts_finish_kill_delay)
377                 ClientKill_Silent(player, autocvar_g_cts_finish_kill_delay);
378 }
379
380 MUTATOR_HOOKFUNCTION(cts, HideTeamNagger)
381 {
382         return true; // doesn't work so well (but isn't cts a teamless mode?)
383 }
384
385 MUTATOR_HOOKFUNCTION(cts, FixClientCvars)
386 {
387         entity player = M_ARGV(0, entity);
388
389         stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
390 }
391
392 MUTATOR_HOOKFUNCTION(cts, WantWeapon)
393 {
394         M_ARGV(1, float) = (M_ARGV(0, entity) == WEP_SHOTGUN); // want weapon = weapon info
395         M_ARGV(3, bool) = true; // want mutator blocked
396         return true;
397 }
398
399 MUTATOR_HOOKFUNCTION(cts, ForbidDropCurrentWeapon)
400 {
401         return true;
402 }
403
404 void cts_Initialize()
405 {
406         cts_ScoreRules();
407 }