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