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