]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_race.qc
Merge branch 'master' into sev/luma
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_race.qc
1 // legacy bot roles
2 .float race_checkpoint;
3 void havocbot_role_race()
4 {
5         if(self.deadflag != DEAD_NO)
6                 return;
7
8         entity e;
9         if (self.bot_strategytime < time)
10         {
11                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
12                 navigation_goalrating_start();
13
14                 for(e = world; (e = find(e, classname, "trigger_race_checkpoint")) != world; )
15                 {
16                         if(e.cnt == self.race_checkpoint)
17                         {
18                                 navigation_routerating(e, 1000000, 5000);
19                         }
20                         else if(self.race_checkpoint == -1)
21                         {
22                                 navigation_routerating(e, 1000000, 5000);
23                         }
24                 }
25
26                 navigation_goalrating_end();
27         }
28 }
29
30 void race_ScoreRules()
31 {
32         ScoreRules_basics(race_teams, 0, 0, FALSE);
33         if(race_teams)
34         {
35                 ScoreInfo_SetLabel_TeamScore(  ST_RACE_LAPS,    "laps",      SFL_SORT_PRIO_PRIMARY);
36                 ScoreInfo_SetLabel_PlayerScore(SP_RACE_LAPS,    "laps",      SFL_SORT_PRIO_PRIMARY);
37                 ScoreInfo_SetLabel_PlayerScore(SP_RACE_TIME,    "time",      SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
38                 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest",   SFL_LOWER_IS_BETTER | SFL_TIME);
39         }
40         else if(g_race_qualifying)
41         {
42                 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest",   SFL_SORT_PRIO_PRIMARY | SFL_LOWER_IS_BETTER | SFL_TIME);
43         }
44         else
45         {
46                 ScoreInfo_SetLabel_PlayerScore(SP_RACE_LAPS,    "laps",      SFL_SORT_PRIO_PRIMARY);
47                 ScoreInfo_SetLabel_PlayerScore(SP_RACE_TIME,    "time",      SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER | SFL_TIME);
48                 ScoreInfo_SetLabel_PlayerScore(SP_RACE_FASTEST, "fastest",   SFL_LOWER_IS_BETTER | SFL_TIME);
49         }
50         ScoreRules_basics_end();
51 }
52
53 void race_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
54 {
55         if(autocvar_sv_eventlog)
56                 GameLogEcho(strcat(":race:", mode, ":", ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
57 }
58
59 MUTATOR_HOOKFUNCTION(race_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(race_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                 race_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                 race_ScoreRules();
139         }
140         
141         return FALSE;
142 }
143
144 MUTATOR_HOOKFUNCTION(race_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(race_ClientConnect)
155 {
156         race_PreparePlayer();
157         self.race_checkpoint = -1;
158
159         string rr = RACE_RECORD;
160
161         if(IS_REAL_CLIENT(self))
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(race_MakePlayerObserver)
182 {
183         if(g_race_qualifying)
184         if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
185                 self.frags = FRAGS_LMS_LOSER;
186         else
187                 self.frags = FRAGS_SPECTATOR;
188
189         race_PreparePlayer();
190         self.race_checkpoint = -1;
191
192         return FALSE;
193 }
194
195 MUTATOR_HOOKFUNCTION(race_PlayerSpawn)
196 {
197         if(spawn_spot.target == "")
198                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
199                 race_PreparePlayer();
200
201         // if we need to respawn, do it right
202         self.race_respawn_checkpoint = self.race_checkpoint;
203         self.race_respawn_spotref = spawn_spot;
204
205         self.race_place = 0;
206         
207         return FALSE;
208 }
209
210 MUTATOR_HOOKFUNCTION(race_PutClientInServer)
211 {
212         if(IS_PLAYER(self))
213         if(!gameover)
214         {
215                 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
216                         race_PreparePlayer();
217                 else // respawn
218                         race_RetractPlayer();
219
220                 race_AbandonRaceCheck(self);
221         }
222         return FALSE;
223 }
224
225 MUTATOR_HOOKFUNCTION(race_PlayerDies)
226 {
227         self.respawn_flags |= RESPAWN_FORCE;
228         race_AbandonRaceCheck(self);
229         return FALSE;
230 }
231
232 MUTATOR_HOOKFUNCTION(race_BotRoles)
233 {
234         self.havocbot_role = havocbot_role_race;
235         return TRUE;
236 }
237
238 MUTATOR_HOOKFUNCTION(race_PlayerPostThink)
239 {
240         if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
241         {
242                 if (!self.stored_netname)
243                         self.stored_netname = strzone(uid2name(self.crypto_idfp));
244                 if(self.stored_netname != self.netname)
245                 {
246                         db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
247                         strunzone(self.stored_netname);
248                         self.stored_netname = strzone(self.netname);
249                 }
250         }
251
252         return FALSE;
253 }
254
255 MUTATOR_HOOKFUNCTION(race_ForbidClearPlayerScore)
256 {
257         if(g_race_qualifying)
258                 return TRUE; // in qualifying, you don't lose score by observing
259
260         return FALSE;
261 }
262
263 MUTATOR_HOOKFUNCTION(race_GetTeamCount)
264 {
265         ret_float = race_teams;
266         return FALSE;
267 }
268
269 void race_Initialize()
270 {
271         race_ScoreRules();
272         if(g_race_qualifying == 2)
273                 warmup_stage = 0;
274 }
275
276 MUTATOR_DEFINITION(gamemode_race)
277 {
278         MUTATOR_HOOK(PlayerPhysics, race_PlayerPhysics, CBC_ORDER_ANY);
279         MUTATOR_HOOK(reset_map_global, race_ResetMap, CBC_ORDER_ANY);
280         MUTATOR_HOOK(PlayerPreThink, race_PlayerPreThink, CBC_ORDER_ANY);
281         MUTATOR_HOOK(ClientConnect, race_ClientConnect, CBC_ORDER_ANY);
282         MUTATOR_HOOK(MakePlayerObserver, race_MakePlayerObserver, CBC_ORDER_ANY);
283         MUTATOR_HOOK(PlayerSpawn, race_PlayerSpawn, CBC_ORDER_ANY);
284         MUTATOR_HOOK(PutClientInServer, race_PutClientInServer, CBC_ORDER_ANY);
285         MUTATOR_HOOK(PlayerDies, race_PlayerDies, CBC_ORDER_ANY);
286         MUTATOR_HOOK(HavocBot_ChooseRole, race_BotRoles, CBC_ORDER_ANY);
287         MUTATOR_HOOK(GetPressedKeys, race_PlayerPostThink, CBC_ORDER_ANY);
288         MUTATOR_HOOK(ForbidPlayerScore_Clear, race_ForbidClearPlayerScore, CBC_ORDER_ANY);
289         MUTATOR_HOOK(GetTeamCount, race_GetTeamCount, CBC_ORDER_ANY);
290
291         MUTATOR_ONADD
292         {
293                 if(time > 1) // game loads at time 1
294                         error("This is a game type and it cannot be added at runtime.");
295                 race_Initialize();
296         }
297
298         MUTATOR_ONROLLBACK_OR_REMOVE
299         {
300                 // we actually cannot roll back race_Initialize here
301                 // BUT: we don't need to! If this gets called, adding always
302                 // succeeds.
303         }
304
305         MUTATOR_ONREMOVE
306         {
307                 print("This is a game type and it cannot be removed at runtime.");
308                 return -1;
309         }
310
311         return 0;
312 }