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