]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_race.qc
Move bot roles into the mutator files
[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         msg_entity = self;
156         race_send_recordtime(MSG_ONE);
157         race_send_speedaward(MSG_ONE);
158
159         speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));
160         speedaward_alltimebest_holder = uid2name(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp")));
161         race_send_speedaward_alltimebest(MSG_ONE);
162
163         float i;
164         for (i = 1; i <= RANKINGS_CNT; ++i)
165         {
166                 race_SendRankings(i, 0, 0, MSG_ONE);
167         }
168
169         return FALSE;
170 }
171
172 MUTATOR_HOOKFUNCTION(race_MakePlayerObserver)
173 {
174         if(g_race_qualifying)
175         if(PlayerScore_Add(self, SP_RACE_FASTEST, 0))
176                 self.frags = FRAGS_LMS_LOSER;
177         else
178                 self.frags = FRAGS_SPECTATOR;
179
180         race_PreparePlayer();
181         self.race_checkpoint = -1;
182
183         return FALSE;
184 }
185
186 MUTATOR_HOOKFUNCTION(race_PlayerSpawn)
187 {
188         if(spawn_spot.target == "")
189                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
190                 race_PreparePlayer();
191
192         // if we need to respawn, do it right
193         self.race_respawn_checkpoint = self.race_checkpoint;
194         self.race_respawn_spotref = spawn_spot;
195
196         self.race_place = 0;
197         
198         return FALSE;
199 }
200
201 MUTATOR_HOOKFUNCTION(race_PutClientInServer)
202 {
203         if(IS_PLAYER(self))
204         if(!gameover)
205         {
206                 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
207                         race_PreparePlayer();
208                 else // respawn
209                         race_RetractPlayer();
210
211                 race_AbandonRaceCheck(self);
212         }
213         return FALSE;
214 }
215
216 MUTATOR_HOOKFUNCTION(race_PlayerDies)
217 {
218         self.respawn_flags |= RESPAWN_FORCE;
219         race_AbandonRaceCheck(self);
220         return FALSE;
221 }
222
223 MUTATOR_HOOKFUNCTION(race_BotRoles)
224 {
225         self.havocbot_role = havocbot_role_race;
226         return TRUE;
227 }
228
229 MUTATOR_HOOKFUNCTION(race_PlayerPostThink)
230 {
231         if(self.cvar_cl_allow_uidtracking == 1 && self.cvar_cl_allow_uid2name == 1)
232         {
233                 if (!self.stored_netname)
234                         self.stored_netname = strzone(uid2name(self.crypto_idfp));
235                 if(self.stored_netname != self.netname)
236                 {
237                         db_put(ServerProgsDB, strcat("/uid2name/", self.crypto_idfp), self.netname);
238                         strunzone(self.stored_netname);
239                         self.stored_netname = strzone(self.netname);
240                 }
241         }
242
243         return FALSE;
244 }
245
246 MUTATOR_HOOKFUNCTION(race_ForbidClearPlayerScore)
247 {
248         if(g_race_qualifying)
249                 return TRUE; // in qualifying, you don't lose score by observing
250
251         return FALSE;
252 }
253
254 MUTATOR_HOOKFUNCTION(race_GetTeamCount)
255 {
256         ret_float = race_teams;
257         return FALSE;
258 }
259
260 void race_Initialize()
261 {
262         race_ScoreRules();
263 }
264
265 MUTATOR_DEFINITION(gamemode_race)
266 {
267         MUTATOR_HOOK(PlayerPhysics, race_PlayerPhysics, CBC_ORDER_ANY);
268         MUTATOR_HOOK(reset_map_global, race_ResetMap, CBC_ORDER_ANY);
269         MUTATOR_HOOK(PlayerPreThink, race_PlayerPreThink, CBC_ORDER_ANY);
270         MUTATOR_HOOK(ClientConnect, race_ClientConnect, CBC_ORDER_ANY);
271         MUTATOR_HOOK(MakePlayerObserver, race_MakePlayerObserver, CBC_ORDER_ANY);
272         MUTATOR_HOOK(PlayerSpawn, race_PlayerSpawn, CBC_ORDER_ANY);
273         MUTATOR_HOOK(PutClientInServer, race_PutClientInServer, CBC_ORDER_ANY);
274         MUTATOR_HOOK(PlayerDies, race_PlayerDies, CBC_ORDER_ANY);
275         MUTATOR_HOOK(HavocBot_ChooseRule, race_BotRoles, CBC_ORDER_ANY);
276         MUTATOR_HOOK(GetPressedKeys, race_PlayerPostThink, CBC_ORDER_ANY);
277         MUTATOR_HOOK(ForbidPlayerScore_Clear, race_ForbidClearPlayerScore, CBC_ORDER_ANY);
278         MUTATOR_HOOK(GetTeamCount, race_GetTeamCount, CBC_ORDER_ANY);
279
280         MUTATOR_ONADD
281         {
282                 if(time > 1) // game loads at time 1
283                         error("This is a game type and it cannot be added at runtime.");
284                 race_Initialize();
285         }
286
287         MUTATOR_ONROLLBACK_OR_REMOVE
288         {
289                 // we actually cannot roll back race_Initialize here
290                 // BUT: we don't need to! If this gets called, adding always
291                 // succeeds.
292         }
293
294         MUTATOR_ONREMOVE
295         {
296                 print("This is a game type and it cannot be removed at runtime.");
297                 return -1;
298         }
299
300         return 0;
301 }