]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/cts/cts.qc
Clear out most references to .health and .armorvalue on the server side
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / cts / cts.qc
1 #include "cts.qh"
2
3 // TODO: split into sv_cts
4 #ifdef SVQC
5 #include <server/race.qh>
6 #include <server/items.qh>
7
8 float autocvar_g_cts_finish_kill_delay;
9 bool autocvar_g_cts_selfdamage;
10
11 // legacy bot roles
12 .float race_checkpoint;
13 void havocbot_role_cts(entity this)
14 {
15         if(IS_DEAD(this))
16                 return;
17
18         if (navigation_goalrating_timeout(this))
19         {
20                 navigation_goalrating_start(this);
21
22                 bool raw_touch_check = true;
23                 int cp = this.race_checkpoint;
24
25                 LABEL(search_racecheckpoints)
26                 IL_EACH(g_racecheckpoints, true,
27                 {
28                         if(it.cnt == cp || cp == -1)
29                         {
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))
34                                 {
35                                         cp = race_NextCheckpoint(cp);
36                                         raw_touch_check = false;
37                                         goto search_racecheckpoints;
38                                 }
39                                 navigation_routerating(this, it, 1000000, 5000);
40                         }
41                 });
42
43                 navigation_goalrating_end(this);
44
45                 navigation_goalrating_timeout_set(this);
46         }
47 }
48
49 void cts_ScoreRules()
50 {
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);
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 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
71 {
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.count = 1; // this is used to indicate that it should be silent
78     e.lip = 0;
79 }
80
81 MUTATOR_HOOKFUNCTION(cts, PlayerPhysics)
82 {
83         entity player = M_ARGV(0, entity);
84         float dt = M_ARGV(1, float);
85
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;
91
92 #ifdef SVQC
93         if(IS_PLAYER(player))
94         {
95                 if (player.race_penalty)
96                         if (time > player.race_penalty)
97                                 player.race_penalty = 0;
98                 if(player.race_penalty)
99                 {
100                         player.velocity = '0 0 0';
101                         set_movetype(player, MOVETYPE_NONE);
102                         player.disableclientprediction = 2;
103                 }
104         }
105 #endif
106
107         // force kbd movement for fairness
108         float wishspeed;
109         vector wishvel;
110
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)
118         {
119                 wishvel.z = 0;
120                 wishspeed = vlen(wishvel);
121                 if(wishvel.x >= 2 * wishvel.y)
122                 {
123                         // pure X motion
124                         if(CS(player).movement.x > 0)
125                                 CS(player).movement_x = wishspeed;
126                         else
127                                 CS(player).movement_x = -wishspeed;
128                         CS(player).movement_y = 0;
129                 }
130                 else if(wishvel.y >= 2 * wishvel.x)
131                 {
132                         // pure Y motion
133                         CS(player).movement_x = 0;
134                         if(CS(player).movement.y > 0)
135                                 CS(player).movement_y = wishspeed;
136                         else
137                                 CS(player).movement_y = -wishspeed;
138                 }
139                 else
140                 {
141                         // diagonal
142                         if(CS(player).movement.x > 0)
143                                 CS(player).movement_x = M_SQRT1_2 * wishspeed;
144                         else
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;
148                         else
149                                 CS(player).movement_y = -M_SQRT1_2 * wishspeed;
150                 }
151         }
152 }
153
154 MUTATOR_HOOKFUNCTION(cts, reset_map_global)
155 {
156         float s;
157
158         Score_NicePrint(NULL);
159
160         race_ClearRecords();
161         PlayerScore_Sort(race_place, 0, 1, 0);
162
163         FOREACH_CLIENT(true, {
164                 if(it.race_place)
165                 {
166                         s = GameRules_scoring_add(it, RACE_FASTEST, 0);
167                         if(!s)
168                                 it.race_place = 0;
169                 }
170                 cts_EventLog(ftos(it.race_place), it);
171         });
172
173         if(g_race_qualifying == 2)
174         {
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));
180                 cts_ScoreRules();
181         }
182 }
183
184 MUTATOR_HOOKFUNCTION(cts, ClientConnect)
185 {
186         entity player = M_ARGV(0, entity);
187
188         race_PreparePlayer(player);
189         player.race_checkpoint = -1;
190
191         if(IS_REAL_CLIENT(player))
192         {
193                 string rr = CTS_RECORD;
194
195                 msg_entity = player;
196                 race_send_recordtime(MSG_ONE);
197                 race_send_speedaward(MSG_ONE);
198
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);
202
203                 float i;
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)
207                 {
208                         race_SendRankings(i, 0, 0, MSG_ONE);
209                 }
210         }
211 }
212
213 MUTATOR_HOOKFUNCTION(cts, AbortSpeedrun)
214 {
215         entity player = M_ARGV(0, entity);
216
217         if(autocvar_g_allow_checkpoints)
218                 race_PreparePlayer(player); // nice try
219 }
220
221 MUTATOR_HOOKFUNCTION(cts, MakePlayerObserver)
222 {
223         entity player = M_ARGV(0, entity);
224
225         if(GameRules_scoring_add(player, RACE_FASTEST, 0))
226                 player.frags = FRAGS_LMS_LOSER;
227         else
228                 player.frags = FRAGS_SPECTATOR;
229
230         race_PreparePlayer(player);
231         player.race_checkpoint = -1;
232 }
233
234 MUTATOR_HOOKFUNCTION(cts, PlayerSpawn)
235 {
236         entity player = M_ARGV(0, entity);
237         entity spawn_spot = M_ARGV(1, entity);
238
239         if(spawn_spot.target == "")
240                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
241                 race_PreparePlayer(player);
242
243         // if we need to respawn, do it right
244         player.race_respawn_checkpoint = player.race_checkpoint;
245         player.race_respawn_spotref = spawn_spot;
246
247         player.race_place = 0;
248 }
249
250 MUTATOR_HOOKFUNCTION(cts, PutClientInServer)
251 {
252         entity player = M_ARGV(0, entity);
253
254         if(IS_PLAYER(player))
255         if(!game_stopped)
256         {
257                 if(CS(player).killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
258                         race_PreparePlayer(player);
259                 else // respawn
260                         race_RetractPlayer(player);
261
262                 race_AbandonRaceCheck(player);
263         }
264 }
265
266 MUTATOR_HOOKFUNCTION(cts, PlayerDies)
267 {
268         entity frag_target = M_ARGV(2, entity);
269
270         frag_target.respawn_flags |= RESPAWN_FORCE;
271         race_AbandonRaceCheck(frag_target);
272 }
273
274 MUTATOR_HOOKFUNCTION(cts, HavocBot_ChooseRole)
275 {
276         entity bot = M_ARGV(0, entity);
277
278         bot.havocbot_role = havocbot_role_cts;
279         return true;
280 }
281
282 MUTATOR_HOOKFUNCTION(cts, GetPressedKeys)
283 {
284         entity player = M_ARGV(0, entity);
285
286         if(CS(player).cvar_cl_allow_uidtracking == 1 && CS(player).cvar_cl_allow_uid2name == 1)
287         {
288                 if (!player.stored_netname)
289                         player.stored_netname = strzone(uid2name(player.crypto_idfp));
290                 if(player.stored_netname != player.netname)
291                 {
292                         db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
293                         strcpy(player.stored_netname, player.netname);
294                 }
295         }
296
297         if (!IS_OBSERVER(player))
298         {
299                 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
300                 {
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;
305                 }
306                 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
307                 {
308                         string rr = CTS_RECORD;
309                         race_send_speedaward(MSG_ALL);
310                         speedaward_lastsent = speedaward_speed;
311                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
312                         {
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);
319                         }
320                 }
321         }
322 }
323
324 MUTATOR_HOOKFUNCTION(cts, ForbidThrowCurrentWeapon)
325 {
326         // no weapon dropping in CTS
327         return true;
328 }
329
330 MUTATOR_HOOKFUNCTION(cts, FilterItem)
331 {
332         entity item = M_ARGV(0, entity);
333
334         if (Item_IsLoot(item))
335         {
336                 return true;
337         }
338 }
339
340 MUTATOR_HOOKFUNCTION(cts, Damage_Calculate)
341 {
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);
346
347         if(frag_target == frag_attacker || frag_deathtype == DEATH_FALL.m_id)
348         if(!autocvar_g_cts_selfdamage)
349         {
350                 frag_damage = 0;
351                 M_ARGV(4, float) = frag_damage;
352         }
353 }
354
355 MUTATOR_HOOKFUNCTION(cts, ForbidPlayerScore_Clear)
356 {
357         return true; // in CTS, you don't lose score by observing
358 }
359
360 MUTATOR_HOOKFUNCTION(cts, GetRecords)
361 {
362         int record_page = M_ARGV(0, int);
363         string ret_string = M_ARGV(1, string);
364
365         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
366         {
367                 if(MapInfo_Get_ByID(i))
368                 {
369                         float r = race_readTime(MapInfo_Map_bspname, 1);
370
371                         if(!r)
372                                 continue;
373
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");
376                 }
377         }
378
379         M_ARGV(1, string) = ret_string;
380 }
381
382 void ClientKill_Now(entity this);
383 MUTATOR_HOOKFUNCTION(cts, ClientKill)
384 {
385     entity player = M_ARGV(0, entity);
386
387         M_ARGV(1, float) = 0; // kill delay
388
389         if(player.killindicator && player.killindicator.count == 1) // player.killindicator.count == 1 means that the kill indicator was spawned by CTS_ClientKill
390         {
391                 delete(player.killindicator);
392                 player.killindicator = NULL;
393
394                 ClientKill_Now(player); // allow instant kill in this case
395                 return;
396         }
397 }
398
399 MUTATOR_HOOKFUNCTION(cts, Race_FinalCheckpoint)
400 {
401         entity player = M_ARGV(0, entity);
402
403         if(autocvar_g_cts_finish_kill_delay)
404                 CTS_ClientKill(player);
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 }
435 #endif