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