Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_cts.qc
1 #include "gamemode_cts.qh"
2 #include "../_all.qh"
3
4 #include "gamemode.qh"
5
6 #include "../race.qh"
7
8 // legacy bot roles
9 .float race_checkpoint;
10 void havocbot_role_cts()
11 {
12         if(self.deadflag != DEAD_NO)
13                 return;
14
15         entity e;
16         if (self.bot_strategytime < time)
17         {
18                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
19                 navigation_goalrating_start();
20
21                 for(e = world; (e = find(e, classname, "trigger_race_checkpoint")) != world; )
22                 {
23                         if(e.cnt == self.race_checkpoint)
24                         {
25                                 navigation_routerating(e, 1000000, 5000);
26                         }
27                         else if(self.race_checkpoint == -1)
28                         {
29                                 navigation_routerating(e, 1000000, 5000);
30                         }
31                 }
32
33                 navigation_goalrating_end();
34         }
35 }
36
37 void cts_ScoreRules()
38 {
39         ScoreRules_basics(0, 0, 0, false);
40         if(g_race_qualifying)
41         {
42                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_FASTEST, "fastest",   SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
43         }
44         else
45         {
46                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_LAPS,    "laps",      SFL_SORT_PRIO_PRIMARY);
47                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_TIME,    "time",      SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
48                 ScoreInfo_SetLabel_PlayerScore(SP_CTS_FASTEST, "fastest",   SFL_LOWER_IS_BETTER | SFL_TIME);
49         }
50         ScoreRules_basics_end();
51 }
52
53 void cts_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
54 {
55         if(autocvar_sv_eventlog)
56                 GameLogEcho(strcat(":cts:", mode, ":", ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
57 }
58
59 MUTATOR_HOOKFUNCTION(cts_PlayerPhysics)
60 {
61         // force kbd movement for fairness
62         float wishspeed;
63         vector wishvel;
64
65         // if record times matter
66         // ensure nothing EVIL is being done (i.e. div0_evade)
67         // this hinders joystick users though
68         // but it still gives SOME analog control
69         wishvel.x = fabs(self.movement.x);
70         wishvel.y = fabs(self.movement.y);
71         if(wishvel.x != 0 && wishvel.y != 0 && wishvel.x != wishvel.y)
72         {
73                 wishvel.z = 0;
74                 wishspeed = vlen(wishvel);
75                 if(wishvel.x >= 2 * wishvel.y)
76                 {
77                         // pure X motion
78                         if(self.movement.x > 0)
79                                 self.movement_x = wishspeed;
80                         else
81                                 self.movement_x = -wishspeed;
82                         self.movement_y = 0;
83                 }
84                 else if(wishvel.y >= 2 * wishvel.x)
85                 {
86                         // pure Y motion
87                         self.movement_x = 0;
88                         if(self.movement.y > 0)
89                                 self.movement_y = wishspeed;
90                         else
91                                 self.movement_y = -wishspeed;
92                 }
93                 else
94                 {
95                         // diagonal
96                         if(self.movement.x > 0)
97                                 self.movement_x = M_SQRT1_2 * wishspeed;
98                         else
99                                 self.movement_x = -M_SQRT1_2 * wishspeed;
100                         if(self.movement.y > 0)
101                                 self.movement_y = M_SQRT1_2 * wishspeed;
102                         else
103                                 self.movement_y = -M_SQRT1_2 * wishspeed;
104                 }
105         }
106
107         return false;
108 }
109
110 MUTATOR_HOOKFUNCTION(cts_ResetMap)
111 {
112         float s;
113
114         Score_NicePrint(world);
115
116         race_ClearRecords();
117         PlayerScore_Sort(race_place, 0, 1, 0);
118
119         entity e;
120         FOR_EACH_CLIENT(e)
121         {
122                 if(e.race_place)
123                 {
124                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
125                         if(!s)
126                                 e.race_place = 0;
127                 }
128                 cts_EventLog(ftos(e.race_place), e);
129         }
130
131         if(g_race_qualifying == 2)
132         {
133                 g_race_qualifying = 0;
134                 independent_players = 0;
135                 cvar_set("fraglimit", ftos(race_fraglimit));
136                 cvar_set("leadlimit", ftos(race_leadlimit));
137                 cvar_set("timelimit", ftos(race_timelimit));
138                 cts_ScoreRules();
139         }
140
141         return false;
142 }
143
144 MUTATOR_HOOKFUNCTION(cts_PlayerPreThink)
145 {
146         if(IS_SPEC(self) || IS_OBSERVER(self))
147         if(g_race_qualifying)
148         if(msg_entity.enemy.race_laptime)
149                 race_SendNextCheckpoint(msg_entity.enemy, 1);
150
151         return false;
152 }
153
154 MUTATOR_HOOKFUNCTION(cts_ClientConnect)
155 {
156         race_PreparePlayer();
157         self.race_checkpoint = -1;
158
159         if(IS_REAL_CLIENT(self))
160         {
161                 string rr = CTS_RECORD;
162
163                 msg_entity = self;
164                 race_send_recordtime(MSG_ONE);
165                 race_send_speedaward(MSG_ONE);
166
167                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
168                 speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
169                 race_send_speedaward_alltimebest(MSG_ONE);
170
171                 float i;
172                 for (i = 1; i <= RANKINGS_CNT; ++i)
173                 {
174                         race_SendRankings(i, 0, 0, MSG_ONE);
175                 }
176         }
177
178         return false;
179 }
180
181 MUTATOR_HOOKFUNCTION(cts_MakePlayerObserver)
182 {
183         if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
184                 self.frags = FRAGS_LMS_LOSER;
185         else
186                 self.frags = FRAGS_SPECTATOR;
187
188         race_PreparePlayer();
189         self.race_checkpoint = -1;
190
191         return false;
192 }
193
194 MUTATOR_HOOKFUNCTION(cts_PlayerSpawn)
195 {
196         if(spawn_spot.target == "")
197                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
198                 race_PreparePlayer();
199
200         // if we need to respawn, do it right
201         self.race_respawn_checkpoint = self.race_checkpoint;
202         self.race_respawn_spotref = spawn_spot;
203
204         self.race_place = 0;
205
206         return false;
207 }
208
209 MUTATOR_HOOKFUNCTION(cts_PutClientInServer)
210 {
211         if(IS_PLAYER(self))
212         if(!gameover)
213         {
214                 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
215                         race_PreparePlayer();
216                 else // respawn
217                         race_RetractPlayer();
218
219                 race_AbandonRaceCheck(self);
220         }
221         return false;
222 }
223
224 MUTATOR_HOOKFUNCTION(cts_PlayerDies)
225 {
226         self.respawn_flags |= RESPAWN_FORCE;
227         race_AbandonRaceCheck(self);
228         return false;
229 }
230
231 MUTATOR_HOOKFUNCTION(cts_BotRoles)
232 {
233         self.havocbot_role = havocbot_role_cts;
234         return true;
235 }
236
237 MUTATOR_HOOKFUNCTION(cts_PlayerPostThink)
238 {
239         if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
240         {
241                 if (!self.stored_netname)
242                         self.stored_netname = strzone(uid2name(self.crypto_idfp));
243                 if(self.stored_netname != self.netname)
244                 {
245                         db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
246                         strunzone(self.stored_netname);
247                         self.stored_netname = strzone(self.netname);
248                 }
249         }
250
251         return false;
252 }
253
254 MUTATOR_HOOKFUNCTION(cts_ForbidThrowing)
255 {
256         // no weapon dropping in CTS
257         return true;
258 }
259
260 MUTATOR_HOOKFUNCTION(cts_FilterItem)
261 {
262         if(self.classname == "droppedweapon")
263                 return true;
264
265         return false;
266 }
267
268 MUTATOR_HOOKFUNCTION(cts_PlayerDamage)
269 {
270         if(frag_target == frag_attacker || frag_deathtype == DEATH_FALL)
271         if(!autocvar_g_cts_selfdamage)
272                 frag_damage = 0;
273
274         return false;
275 }
276
277 MUTATOR_HOOKFUNCTION(cts_ForbidClearPlayerScore)
278 {
279         return true; // in CTS, you don't lose score by observing
280 }
281
282 MUTATOR_HOOKFUNCTION(cts_SetMods)
283 {
284         g_cloaked = 1; // always enable cloak in CTS
285
286         return false;
287 }
288
289 void cts_Initialize()
290 {
291         cts_ScoreRules();
292 }
293
294 MUTATOR_DEFINITION(gamemode_cts)
295 {
296         MUTATOR_HOOK(PlayerPhysics, cts_PlayerPhysics, CBC_ORDER_ANY);
297         MUTATOR_HOOK(reset_map_global, cts_ResetMap, CBC_ORDER_ANY);
298         MUTATOR_HOOK(PlayerPreThink, cts_PlayerPreThink, CBC_ORDER_ANY);
299         MUTATOR_HOOK(ClientConnect, cts_ClientConnect, CBC_ORDER_ANY);
300         MUTATOR_HOOK(MakePlayerObserver, cts_MakePlayerObserver, CBC_ORDER_ANY);
301         MUTATOR_HOOK(PlayerSpawn, cts_PlayerSpawn, CBC_ORDER_ANY);
302         MUTATOR_HOOK(PutClientInServer, cts_PutClientInServer, CBC_ORDER_ANY);
303         MUTATOR_HOOK(PlayerDies, cts_PlayerDies, CBC_ORDER_ANY);
304         MUTATOR_HOOK(HavocBot_ChooseRole, cts_BotRoles, CBC_ORDER_ANY);
305         MUTATOR_HOOK(GetPressedKeys, cts_PlayerPostThink, CBC_ORDER_ANY);
306         MUTATOR_HOOK(ForbidThrowCurrentWeapon, cts_ForbidThrowing, CBC_ORDER_ANY);
307         MUTATOR_HOOK(FilterItem, cts_FilterItem, CBC_ORDER_ANY);
308         MUTATOR_HOOK(PlayerDamage_Calculate, cts_PlayerDamage, CBC_ORDER_ANY);
309         MUTATOR_HOOK(ForbidPlayerScore_Clear, cts_ForbidClearPlayerScore, CBC_ORDER_ANY);
310         MUTATOR_HOOK(SetModname, cts_SetMods, CBC_ORDER_ANY);
311
312         MUTATOR_ONADD
313         {
314                 if(time > 1) // game loads at time 1
315                         error("This is a game type and it cannot be added at runtime.");
316                 cts_Initialize();
317         }
318
319         MUTATOR_ONROLLBACK_OR_REMOVE
320         {
321                 // we actually cannot roll back cts_Initialize here
322                 // BUT: we don't need to! If this gets called, adding always
323                 // succeeds.
324         }
325
326         MUTATOR_ONREMOVE
327         {
328                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
329                 return -1;
330         }
331
332         return 0;
333 }