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