]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_cts.qc
Merge remote-tracking branch 'origin/terencehill/bot_waypoints' into terencehill...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_cts.qc
1 #include "gamemode_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                 IL_EACH(g_racecheckpoints, true,
21                 {
22                         if(it.cnt == this.race_checkpoint)
23                                 navigation_routerating(this, it, 1000000, 5000);
24                         else if(this.race_checkpoint == -1)
25                                 navigation_routerating(this, it, 1000000, 5000);
26                 });
27
28                 navigation_goalrating_end(this);
29
30                 navigation_goalrating_timeout_set(this);
31         }
32 }
33
34 void cts_ScoreRules()
35 {
36     GameRules_score_enabled(false);
37     GameRules_scoring(0, 0, 0, {
38         if (g_race_qualifying) {
39             field(SP_RACE_FASTEST, "fastest", SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
40         } else {
41             field(SP_RACE_LAPS, "laps", SFL_SORT_PRIO_PRIMARY);
42             field(SP_RACE_TIME, "time", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
43             field(SP_RACE_FASTEST, "fastest", SFL_LOWER_IS_BETTER | SFL_TIME);
44         }
45     });
46 }
47
48 void cts_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
49 {
50         if(autocvar_sv_eventlog)
51                 GameLogEcho(strcat(":cts:", mode, ":", ((actor != NULL) ? (strcat(":", ftos(actor.playerid))) : "")));
52 }
53
54 void KillIndicator_Think(entity this);
55 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
56 {
57     e.killindicator = spawn();
58     e.killindicator.owner = e;
59     setthink(e.killindicator, KillIndicator_Think);
60     e.killindicator.nextthink = time + (e.lip) * 0.05;
61     e.killindicator.cnt = ceil(autocvar_g_cts_finish_kill_delay);
62     e.killindicator.health = 1; // this is used to indicate that it should be silent
63     e.lip = 0;
64 }
65
66 MUTATOR_HOOKFUNCTION(cts, PlayerPhysics)
67 {
68         entity player = M_ARGV(0, entity);
69         float dt = M_ARGV(1, float);
70
71         player.race_movetime_frac += dt;
72         float f = floor(player.race_movetime_frac);
73         player.race_movetime_frac -= f;
74         player.race_movetime_count += f;
75         player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
76
77 #ifdef SVQC
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 #endif
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                 for (i = 1; i <= RANKINGS_CNT; ++i)
190                 {
191                         race_SendRankings(i, 0, 0, MSG_ONE);
192                 }
193         }
194 }
195
196 MUTATOR_HOOKFUNCTION(cts, AbortSpeedrun)
197 {
198         entity player = M_ARGV(0, entity);
199
200         if(autocvar_g_allow_checkpoints)
201                 race_PreparePlayer(player); // nice try
202 }
203
204 MUTATOR_HOOKFUNCTION(cts, MakePlayerObserver)
205 {
206         entity player = M_ARGV(0, entity);
207
208         if(GameRules_scoring_add(player, RACE_FASTEST, 0))
209                 player.frags = FRAGS_LMS_LOSER;
210         else
211                 player.frags = FRAGS_SPECTATOR;
212
213         race_PreparePlayer(player);
214         player.race_checkpoint = -1;
215 }
216
217 MUTATOR_HOOKFUNCTION(cts, PlayerSpawn)
218 {
219         entity player = M_ARGV(0, entity);
220         entity spawn_spot = M_ARGV(1, entity);
221
222         if(spawn_spot.target == "")
223                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
224                 race_PreparePlayer(player);
225
226         // if we need to respawn, do it right
227         player.race_respawn_checkpoint = player.race_checkpoint;
228         player.race_respawn_spotref = spawn_spot;
229
230         player.race_place = 0;
231 }
232
233 MUTATOR_HOOKFUNCTION(cts, PutClientInServer)
234 {
235         entity player = M_ARGV(0, entity);
236
237         if(IS_PLAYER(player))
238         if(!game_stopped)
239         {
240                 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
241                         race_PreparePlayer(player);
242                 else // respawn
243                         race_RetractPlayer(player);
244
245                 race_AbandonRaceCheck(player);
246         }
247 }
248
249 MUTATOR_HOOKFUNCTION(cts, PlayerDies)
250 {
251         entity frag_target = M_ARGV(2, entity);
252
253         frag_target.respawn_flags |= RESPAWN_FORCE;
254         race_AbandonRaceCheck(frag_target);
255 }
256
257 MUTATOR_HOOKFUNCTION(cts, HavocBot_ChooseRole)
258 {
259         entity bot = M_ARGV(0, entity);
260
261         bot.havocbot_role = havocbot_role_cts;
262         return true;
263 }
264
265 MUTATOR_HOOKFUNCTION(cts, GetPressedKeys)
266 {
267         entity player = M_ARGV(0, entity);
268
269         if(CS(player).cvar_cl_allow_uidtracking == 1 && CS(player).cvar_cl_allow_uid2name == 1)
270         {
271                 if (!player.stored_netname)
272                         player.stored_netname = strzone(uid2name(player.crypto_idfp));
273                 if(player.stored_netname != player.netname)
274                 {
275                         db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
276                         strunzone(player.stored_netname);
277                         player.stored_netname = strzone(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 void ClientKill_Now(entity this);
367 MUTATOR_HOOKFUNCTION(cts, ClientKill)
368 {
369     entity player = M_ARGV(0, entity);
370
371         M_ARGV(1, float) = 0; // kill delay
372
373         if(player.killindicator && player.killindicator.health == 1) // player.killindicator.health == 1 means that the kill indicator was spawned by CTS_ClientKill
374         {
375                 delete(player.killindicator);
376                 player.killindicator = NULL;
377
378                 ClientKill_Now(player); // allow instant kill in this case
379                 return;
380         }
381 }
382
383 MUTATOR_HOOKFUNCTION(cts, Race_FinalCheckpoint)
384 {
385         entity player = M_ARGV(0, entity);
386
387         if(autocvar_g_cts_finish_kill_delay)
388                 CTS_ClientKill(player);
389 }
390
391 MUTATOR_HOOKFUNCTION(cts, HideTeamNagger)
392 {
393         return true; // doesn't work so well (but isn't cts a teamless mode?)
394 }
395
396 MUTATOR_HOOKFUNCTION(cts, FixClientCvars)
397 {
398         entity player = M_ARGV(0, entity);
399
400         stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
401 }
402
403 MUTATOR_HOOKFUNCTION(cts, WantWeapon)
404 {
405         M_ARGV(1, float) = (M_ARGV(0, entity) == WEP_SHOTGUN); // want weapon = weapon info
406         M_ARGV(3, bool) = true; // want mutator blocked
407         return true;
408 }
409
410 MUTATOR_HOOKFUNCTION(cts, ForbidDropCurrentWeapon)
411 {
412         return true;
413 }
414
415 void cts_Initialize()
416 {
417         cts_ScoreRules();
418 }