3 // TODO: split into sv_cts
5 #include <server/race.qh>
6 #include <server/items.qh>
8 float autocvar_g_cts_finish_kill_delay;
9 bool autocvar_g_cts_selfdamage;
12 .float race_checkpoint;
13 void havocbot_role_cts(entity this)
18 if (navigation_goalrating_timeout(this))
20 navigation_goalrating_start(this);
22 bool raw_touch_check = true;
23 int cp = this.race_checkpoint;
25 LABEL(search_racecheckpoints)
26 IL_EACH(g_racecheckpoints, true,
28 if(it.cnt == cp || cp == -1)
30 // redirect bot to next goal if it touched the waypoint of an untouchable checkpoint
31 // e.g. checkpoint in front of Stormkeep's warpzone
32 // the same workaround is applied in Race game mode
33 if (raw_touch_check && vdist(this.origin - it.nearestwaypoint.origin, <, 30))
35 cp = race_NextCheckpoint(cp);
36 raw_touch_check = false;
37 goto search_racecheckpoints;
39 navigation_routerating(this, it, 1000000, 5000);
43 navigation_goalrating_end(this);
45 navigation_goalrating_timeout_set(this);
51 GameRules_score_enabled(false);
52 GameRules_scoring(0, 0, 0, {
53 if (g_race_qualifying) {
54 field(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
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);
63 void cts_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
65 if(autocvar_sv_eventlog)
66 GameLogEcho(strcat(":cts:", mode, ":", ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
69 void KillIndicator_Think(entity this);
70 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
72 e.killindicator = spawn();
73 e.killindicator.owner = e;
74 setthink(e.killindicator, KillIndicator_Think);
75 e.killindicator.nextthink = time + (e.lip) * 0.05;
76 e.killindicator.cnt = ceil(autocvar_g_cts_finish_kill_delay);
77 e.killindicator.health = 1; // this is used to indicate that it should be silent
81 MUTATOR_HOOKFUNCTION(cts, PlayerPhysics)
83 entity player = M_ARGV(0, entity);
84 float dt = M_ARGV(1, float);
86 player.race_movetime_frac += dt;
87 float f = floor(player.race_movetime_frac);
88 player.race_movetime_frac -= f;
89 player.race_movetime_count += f;
90 player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
95 if (player.race_penalty)
96 if (time > player.race_penalty)
97 player.race_penalty = 0;
98 if(player.race_penalty)
100 player.velocity = '0 0 0';
101 set_movetype(player, MOVETYPE_NONE);
102 player.disableclientprediction = 2;
107 // force kbd movement for fairness
111 // if record times matter
112 // ensure nothing EVIL is being done (i.e. div0_evade)
113 // this hinders joystick users though
114 // but it still gives SOME analog control
115 wishvel.x = fabs(CS(player).movement.x);
116 wishvel.y = fabs(CS(player).movement.y);
117 if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
120 wishspeed = vlen(wishvel);
121 if(wishvel.x >= 2 * wishvel.y)
124 if(CS(player).movement.x > 0)
125 CS(player).movement_x = wishspeed;
127 CS(player).movement_x = -wishspeed;
128 CS(player).movement_y = 0;
130 else if(wishvel.y >= 2 * wishvel.x)
133 CS(player).movement_x = 0;
134 if(CS(player).movement.y > 0)
135 CS(player).movement_y = wishspeed;
137 CS(player).movement_y = -wishspeed;
142 if(CS(player).movement.x > 0)
143 CS(player).movement_x = M_SQRT1_2 * wishspeed;
145 CS(player).movement_x = -M_SQRT1_2 * wishspeed;
146 if(CS(player).movement.y > 0)
147 CS(player).movement_y = M_SQRT1_2 * wishspeed;
149 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
154 MUTATOR_HOOKFUNCTION(cts, reset_map_global)
158 Score_NicePrint(NULL);
161 PlayerScore_Sort(race_place, 0, 1, 0);
163 FOREACH_CLIENT(true, {
166 s = GameRules_scoring_add(it, RACE_FASTEST, 0);
170 cts_EventLog(ftos(it.race_place), it);
173 if(g_race_qualifying == 2)
175 g_race_qualifying = 0;
176 independent_players = 0;
177 cvar_set("fraglimit", ftos(race_fraglimit));
178 cvar_set("leadlimit", ftos(race_leadlimit));
179 cvar_set("timelimit", ftos(race_timelimit));
184 MUTATOR_HOOKFUNCTION(cts, ClientConnect)
186 entity player = M_ARGV(0, entity);
188 race_PreparePlayer(player);
189 player.race_checkpoint = -1;
191 if(IS_REAL_CLIENT(player))
193 string rr = CTS_RECORD;
196 race_send_recordtime(MSG_ONE);
197 race_send_speedaward(MSG_ONE);
199 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
200 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
201 race_send_speedaward_alltimebest(MSG_ONE);
204 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
205 race_send_rankings_cnt(MSG_ONE);
206 for (i = 1; i <= m; ++i)
208 race_SendRankings(i, 0, 0, MSG_ONE);
213 MUTATOR_HOOKFUNCTION(cts, AbortSpeedrun)
215 entity player = M_ARGV(0, entity);
217 if(autocvar_g_allow_checkpoints)
218 race_PreparePlayer(player); // nice try
221 MUTATOR_HOOKFUNCTION(cts, MakePlayerObserver)
223 entity player = M_ARGV(0, entity);
225 if(GameRules_scoring_add(player, RACE_FASTEST, 0))
226 player.frags = FRAGS_LMS_LOSER;
228 player.frags = FRAGS_SPECTATOR;
230 race_PreparePlayer(player);
231 player.race_checkpoint = -1;
234 MUTATOR_HOOKFUNCTION(cts, PlayerSpawn)
236 entity player = M_ARGV(0, entity);
237 entity spawn_spot = M_ARGV(1, entity);
239 if(spawn_spot.target == "")
240 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
241 race_PreparePlayer(player);
243 // if we need to respawn, do it right
244 player.race_respawn_checkpoint = player.race_checkpoint;
245 player.race_respawn_spotref = spawn_spot;
247 player.race_place = 0;
250 MUTATOR_HOOKFUNCTION(cts, PutClientInServer)
252 entity player = M_ARGV(0, entity);
254 if(IS_PLAYER(player))
257 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
258 race_PreparePlayer(player);
260 race_RetractPlayer(player);
262 race_AbandonRaceCheck(player);
266 MUTATOR_HOOKFUNCTION(cts, PlayerDies)
268 entity frag_target = M_ARGV(2, entity);
270 frag_target.respawn_flags |= RESPAWN_FORCE;
271 race_AbandonRaceCheck(frag_target);
274 MUTATOR_HOOKFUNCTION(cts, HavocBot_ChooseRole)
276 entity bot = M_ARGV(0, entity);
278 bot.havocbot_role = havocbot_role_cts;
282 MUTATOR_HOOKFUNCTION(cts, GetPressedKeys)
284 entity player = M_ARGV(0, entity);
286 if(CS(player).cvar_cl_allow_uidtracking == 1 && CS(player).cvar_cl_allow_uid2name == 1)
288 if (!player.stored_netname)
289 player.stored_netname = strzone(uid2name(player.crypto_idfp));
290 if(player.stored_netname != player.netname)
292 db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
293 strcpy(player.stored_netname, player.netname);
297 if (!IS_OBSERVER(player))
299 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
301 speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
302 speedaward_holder = player.netname;
303 speedaward_uid = player.crypto_idfp;
304 speedaward_lastupdate = time;
306 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
308 string rr = CTS_RECORD;
309 race_send_speedaward(MSG_ALL);
310 speedaward_lastsent = speedaward_speed;
311 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
313 speedaward_alltimebest = speedaward_speed;
314 speedaward_alltimebest_holder = speedaward_holder;
315 speedaward_alltimebest_uid = speedaward_uid;
316 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
317 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
318 race_send_speedaward_alltimebest(MSG_ALL);
324 MUTATOR_HOOKFUNCTION(cts, ForbidThrowCurrentWeapon)
326 // no weapon dropping in CTS
330 MUTATOR_HOOKFUNCTION(cts, FilterItem)
332 entity item = M_ARGV(0, entity);
334 if (Item_IsLoot(item))
340 MUTATOR_HOOKFUNCTION(cts, Damage_Calculate)
342 entity frag_attacker = M_ARGV(1, entity);
343 entity frag_target = M_ARGV(2, entity);
344 float frag_deathtype = M_ARGV(3, float);
345 float frag_damage = M_ARGV(4, float);
347 if(frag_target == frag_attacker || frag_deathtype == DEATH_FALL.m_id)
348 if(!autocvar_g_cts_selfdamage)
351 M_ARGV(4, float) = frag_damage;
355 MUTATOR_HOOKFUNCTION(cts, ForbidPlayerScore_Clear)
357 return true; // in CTS, you don't lose score by observing
360 MUTATOR_HOOKFUNCTION(cts, GetRecords)
362 int record_page = M_ARGV(0, int);
363 string ret_string = M_ARGV(1, string);
365 for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
367 if(MapInfo_Get_ByID(i))
369 float r = race_readTime(MapInfo_Map_bspname, 1);
374 string h = race_readName(MapInfo_Map_bspname, 1);
375 ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
379 M_ARGV(1, string) = ret_string;
382 void ClientKill_Now(entity this);
383 MUTATOR_HOOKFUNCTION(cts, ClientKill)
385 entity player = M_ARGV(0, entity);
387 M_ARGV(1, float) = 0; // kill delay
389 if(player.killindicator && player.killindicator.health == 1) // player.killindicator.health == 1 means that the kill indicator was spawned by CTS_ClientKill
391 delete(player.killindicator);
392 player.killindicator = NULL;
394 ClientKill_Now(player); // allow instant kill in this case
399 MUTATOR_HOOKFUNCTION(cts, Race_FinalCheckpoint)
401 entity player = M_ARGV(0, entity);
403 if(autocvar_g_cts_finish_kill_delay)
404 CTS_ClientKill(player);
407 MUTATOR_HOOKFUNCTION(cts, HideTeamNagger)
409 return true; // doesn't work so well (but isn't cts a teamless mode?)
412 MUTATOR_HOOKFUNCTION(cts, FixClientCvars)
414 entity player = M_ARGV(0, entity);
416 stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
419 MUTATOR_HOOKFUNCTION(cts, WantWeapon)
421 M_ARGV(1, float) = (M_ARGV(0, entity) == WEP_SHOTGUN); // want weapon = weapon info
422 M_ARGV(3, bool) = true; // want mutator blocked
426 MUTATOR_HOOKFUNCTION(cts, ForbidDropCurrentWeapon)
431 void cts_Initialize()