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