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