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