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