]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_cts.qc
Fix compile
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_cts.qc
1 #include "gamemode_cts.qh"
2 #include <server/race.qh>
3
4 #ifndef GAMEMODE_CTS_H
5 #define GAMEMODE_CTS_H
6
7 void cts_Initialize();
8
9 REGISTER_MUTATOR(cts, false)
10 {
11         MUTATOR_ONADD
12         {
13                 if (time > 1) // game loads at time 1
14                         error("This is a game type and it cannot be added at runtime.");
15
16                 g_race_qualifying = true;
17                 independent_players = 1;
18                 SetLimits(0, 0, autocvar_timelimit_override, -1);
19
20                 cts_Initialize();
21         }
22
23         MUTATOR_ONROLLBACK_OR_REMOVE
24         {
25                 // we actually cannot roll back cts_Initialize here
26                 // BUT: we don't need to! If this gets called, adding always
27                 // succeeds.
28         }
29
30         MUTATOR_ONREMOVE
31         {
32                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
33                 return -1;
34         }
35
36         return 0;
37 }
38
39 // scores
40 const float ST_CTS_LAPS = 1;
41 const float SP_CTS_LAPS = 4;
42 const float SP_CTS_TIME = 5;
43 const float SP_CTS_FASTEST = 6;
44 #endif
45
46 #ifdef IMPLEMENTATION
47
48 #include <server/race.qh>
49
50 float autocvar_g_cts_finish_kill_delay;
51 bool autocvar_g_cts_selfdamage;
52
53 // legacy bot roles
54 .float race_checkpoint;
55 void havocbot_role_cts(entity this)
56 {
57         if(IS_DEAD(this))
58                 return;
59
60         if (this.bot_strategytime < time)
61         {
62                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
63                 navigation_goalrating_start(this);
64
65                 FOREACH_ENTITY_CLASS("trigger_race_checkpoint", true,
66                 {
67                         if(it.cnt == this.race_checkpoint)
68                                 navigation_routerating(this, it, 1000000, 5000);
69                         else if(this.race_checkpoint == -1)
70                                 navigation_routerating(this, it, 1000000, 5000);
71                 });
72
73                 navigation_goalrating_end(this);
74         }
75 }
76
77 void cts_ScoreRules()
78 {
79         ScoreRules_basics(0, 0, 0, false);
80         if(g_race_qualifying)
81         {
82                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_FASTEST, "fastest",   SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
83         }
84         else
85         {
86                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_LAPS,    "laps",      SFL_SORT_PRIO_PRIMARY);
87                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_TIME,    "time",      SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
88                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_FASTEST, "fastest",   SFL_LOWER_IS_BETTER | SFL_TIME);
89         }
90         ScoreRules_basics_end();
91 }
92
93 void cts_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
94 {
95         if(autocvar_sv_eventlog)
96                 GameLogEcho(strcat(":cts:", mode, ":", ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
97 }
98
99 void KillIndicator_Think(entity this);
100 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
101 {
102     e.killindicator = spawn();
103     e.killindicator.owner = e;
104     setthink(e.killindicator, KillIndicator_Think);
105     e.killindicator.nextthink = time + (e.lip) * 0.05;
106     e.killindicator.cnt = ceil(autocvar_g_cts_finish_kill_delay);
107     e.killindicator.health = 1; // this is used to indicate that it should be silent
108     e.lip = 0;
109 }
110
111 MUTATOR_HOOKFUNCTION(cts, PlayerPhysics)
112 {
113         entity player = M_ARGV(0, entity);
114
115         player.race_movetime_frac += PHYS_INPUT_TIMELENGTH;
116         float f = floor(player.race_movetime_frac);
117         player.race_movetime_frac -= f;
118         player.race_movetime_count += f;
119         player.race_movetime = player.race_movetime_frac + player.race_movetime_count;
120
121 #ifdef SVQC
122         if(IS_PLAYER(player))
123         {
124                 if (player.race_penalty)
125                         if (time > player.race_penalty)
126                                 player.race_penalty = 0;
127                 if(player.race_penalty)
128                 {
129                         player.velocity = '0 0 0';
130                         player.movetype = MOVETYPE_NONE;
131                         player.disableclientprediction = 2;
132                 }
133         }
134 #endif
135
136         // force kbd movement for fairness
137         float wishspeed;
138         vector wishvel;
139
140         // if record times matter
141         // ensure nothing EVIL is being done (i.e. div0_evade)
142         // this hinders joystick users though
143         // but it still gives SOME analog control
144         wishvel.x = fabs(player.movement.x);
145         wishvel.y = fabs(player.movement.y);
146         if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
147         {
148                 wishvel.z = 0;
149                 wishspeed = vlen(wishvel);
150                 if(wishvel.x >= 2 * wishvel.y)
151                 {
152                         // pure X motion
153                         if(player.movement.x > 0)
154                                 player.movement_x = wishspeed;
155                         else
156                                 player.movement_x = -wishspeed;
157                         player.movement_y = 0;
158                 }
159                 else if(wishvel.y >= 2 * wishvel.x)
160                 {
161                         // pure Y motion
162                         player.movement_x = 0;
163                         if(player.movement.y > 0)
164                                 player.movement_y = wishspeed;
165                         else
166                                 player.movement_y = -wishspeed;
167                 }
168                 else
169                 {
170                         // diagonal
171                         if(player.movement.x > 0)
172                                 player.movement_x = M_SQRT1_2 * wishspeed;
173                         else
174                                 player.movement_x = -M_SQRT1_2 * wishspeed;
175                         if(player.movement.y > 0)
176                                 player.movement_y = M_SQRT1_2 * wishspeed;
177                         else
178                                 player.movement_y = -M_SQRT1_2 * wishspeed;
179                 }
180         }
181 }
182
183 MUTATOR_HOOKFUNCTION(cts, reset_map_global)
184 {
185         float s;
186
187         Score_NicePrint(world);
188
189         race_ClearRecords();
190         PlayerScore_Sort(race_place, 0, 1, 0);
191
192         FOREACH_CLIENT(true, LAMBDA(
193                 if(it.race_place)
194                 {
195                         s = PlayerScore_Add(it, SP_RACE_FASTEST, 0);
196                         if(!s)
197                                 it.race_place = 0;
198                 }
199                 cts_EventLog(ftos(it.race_place), it);
200         ));
201
202         if(g_race_qualifying == 2)
203         {
204                 g_race_qualifying = 0;
205                 independent_players = 0;
206                 cvar_set("fraglimit", ftos(race_fraglimit));
207                 cvar_set("leadlimit", ftos(race_leadlimit));
208                 cvar_set("timelimit", ftos(race_timelimit));
209                 cts_ScoreRules();
210         }
211
212         return false;
213 }
214
215 MUTATOR_HOOKFUNCTION(cts, ClientConnect)
216 {
217         entity player = M_ARGV(0, entity);
218
219         race_PreparePlayer(player);
220         player.race_checkpoint = -1;
221
222         if(IS_REAL_CLIENT(player))
223         {
224                 string rr = CTS_RECORD;
225
226                 msg_entity = player;
227                 race_send_recordtime(MSG_ONE);
228                 race_send_speedaward(MSG_ONE);
229
230                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
231                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
232                 race_send_speedaward_alltimebest(MSG_ONE);
233
234                 float i;
235                 for (i = 1; i <= RANKINGS_CNT; ++i)
236                 {
237                         race_SendRankings(i, 0, 0, MSG_ONE);
238                 }
239         }
240
241         return false;
242 }
243
244 MUTATOR_HOOKFUNCTION(cts, MakePlayerObserver)
245 {
246         entity player = M_ARGV(0, entity);
247
248         if(PlayerScore_Add(player, SP_RACE_FASTEST, 0))
249                 player.frags = FRAGS_LMS_LOSER;
250         else
251                 player.frags = FRAGS_SPECTATOR;
252
253         race_PreparePlayer(player);
254         player.race_checkpoint = -1;
255
256         return false;
257 }
258
259 MUTATOR_HOOKFUNCTION(cts, PlayerSpawn)
260 {
261         entity player = M_ARGV(0, entity);
262         entity spawn_spot = M_ARGV(1, entity);
263
264         if(spawn_spot.target == "")
265                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
266                 race_PreparePlayer(player);
267
268         // if we need to respawn, do it right
269         player.race_respawn_checkpoint = player.race_checkpoint;
270         player.race_respawn_spotref = spawn_spot;
271
272         player.race_place = 0;
273
274         return false;
275 }
276
277 MUTATOR_HOOKFUNCTION(cts, PutClientInServer)
278 {
279         entity player = M_ARGV(0, entity);
280
281         if(IS_PLAYER(player))
282         if(!gameover)
283         {
284                 if(player.killcount == FRAGS_SPECTATOR /* initial spawn */ || g_race_qualifying) // spawn
285                         race_PreparePlayer(player);
286                 else // respawn
287                         race_RetractPlayer(player);
288
289                 race_AbandonRaceCheck(player);
290         }
291         return false;
292 }
293
294 MUTATOR_HOOKFUNCTION(cts, PlayerDies)
295 {
296         entity frag_target = M_ARGV(2, entity);
297         
298         frag_target.respawn_flags |= RESPAWN_FORCE;
299         race_AbandonRaceCheck(frag_target);
300         return false;
301 }
302
303 MUTATOR_HOOKFUNCTION(cts, HavocBot_ChooseRole)
304 {
305         entity bot = M_ARGV(0, entity);
306
307         bot.havocbot_role = havocbot_role_cts;
308         return true;
309 }
310
311 MUTATOR_HOOKFUNCTION(cts, GetPressedKeys)
312 {
313         entity player = M_ARGV(0, entity);
314
315         if(player.cvar_cl_allow_uidtracking == 1 && player.cvar_cl_allow_uid2name == 1)
316         {
317                 if (!player.stored_netname)
318                         player.stored_netname = strzone(uid2name(player.crypto_idfp));
319                 if(player.stored_netname != player.netname)
320                 {
321                         db_put(ServerProgsDB, strcat("/uid2name/", player.crypto_idfp), player.netname);
322                         strunzone(player.stored_netname);
323                         player.stored_netname = strzone(player.netname);
324                 }
325         }
326
327         if (!IS_OBSERVER(player))
328         {
329                 if(vdist(player.velocity - player.velocity_z * '0 0 1', >, speedaward_speed))
330                 {
331                         speedaward_speed = vlen(player.velocity - player.velocity_z * '0 0 1');
332                         speedaward_holder = player.netname;
333                         speedaward_uid = player.crypto_idfp;
334                         speedaward_lastupdate = time;
335                 }
336                 if (speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
337                 {
338                         string rr = CTS_RECORD;
339                         race_send_speedaward(MSG_ALL);
340                         speedaward_lastsent = speedaward_speed;
341                         if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
342                         {
343                                 speedaward_alltimebest = speedaward_speed;
344                                 speedaward_alltimebest_holder = speedaward_holder;
345                                 speedaward_alltimebest_uid = speedaward_uid;
346                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
347                                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
348                                 race_send_speedaward_alltimebest(MSG_ALL);
349                         }
350                 }
351         }
352 }
353
354 MUTATOR_HOOKFUNCTION(cts, ForbidThrowCurrentWeapon)
355 {
356         // no weapon dropping in CTS
357         return true;
358 }
359
360 MUTATOR_HOOKFUNCTION(cts, FilterItem)
361 {
362         entity item = M_ARGV(0, entity);
363
364         if(item.classname == "droppedweapon")
365                 return true;
366
367         return false;
368 }
369
370 MUTATOR_HOOKFUNCTION(cts, PlayerDamage_Calculate)
371 {
372         entity frag_attacker = M_ARGV(1, entity);
373         entity frag_target = M_ARGV(2, entity);
374         float frag_deathtype = M_ARGV(3, float);
375         float frag_damage = M_ARGV(4, float);
376
377         if(frag_target == frag_attacker || frag_deathtype == DEATH_FALL.m_id)
378         if(!autocvar_g_cts_selfdamage)
379         {
380                 frag_damage = 0;
381                 M_ARGV(4, float) = frag_damage;
382         }
383
384         return false;
385 }
386
387 MUTATOR_HOOKFUNCTION(cts, ForbidPlayerScore_Clear)
388 {
389         return true; // in CTS, you don't lose score by observing
390 }
391
392 MUTATOR_HOOKFUNCTION(cts, GetRecords)
393 {
394         int record_page = M_ARGV(0, int);
395         string ret_string = M_ARGV(1, string);
396
397         for(int i = record_page * 200; i < MapInfo_count && i < record_page * 200 + 200; ++i)
398         {
399                 if(MapInfo_Get_ByID(i))
400                 {
401                         float r = race_readTime(MapInfo_Map_bspname, 1);
402
403                         if(!r)
404                                 continue;
405
406                         string h = race_readName(MapInfo_Map_bspname, 1);
407                         ret_string = strcat(ret_string, strpad(32, MapInfo_Map_bspname), " ", strpad(-8, TIME_ENCODED_TOSTRING(r)), " ", h, "\n");
408                 }
409         }
410
411         M_ARGV(1, string) = ret_string;
412 }
413
414 void ClientKill_Now(entity this);
415 MUTATOR_HOOKFUNCTION(cts, ClientKill)
416 {
417     entity player = M_ARGV(0, entity);
418
419         M_ARGV(1, float) = 0; // kill delay
420
421         if(player.killindicator && player.killindicator.health == 1) // player.killindicator.health == 1 means that the kill indicator was spawned by CTS_ClientKill
422         {
423                 remove(player.killindicator);
424                 player.killindicator = world;
425
426                 ClientKill_Now(player); // allow instant kill in this case
427                 return;
428         }
429 }
430
431 MUTATOR_HOOKFUNCTION(cts, Race_FinalCheckpoint)
432 {
433         entity player = M_ARGV(0, entity);
434
435         if(autocvar_g_cts_finish_kill_delay)
436                 CTS_ClientKill(player);
437 }
438
439 MUTATOR_HOOKFUNCTION(cts, FixClientCvars)
440 {
441         entity player = M_ARGV(0, entity);
442
443         stuffcmd(player, "cl_cmd settemp cl_movecliptokeyboard 2\n");
444 }
445
446 MUTATOR_HOOKFUNCTION(cts, WantWeapon)
447 {
448         M_ARGV(1, float) = (M_ARGV(0, entity) == WEP_SHOTGUN); // want weapon = weapon info
449         M_ARGV(3, bool) = true; // want mutator blocked
450         return true;
451 }
452
453 void cts_Initialize()
454 {
455         cts_ScoreRules();
456 }
457
458 #endif