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