]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
fix timing in race
[divverent/nexuiz.git] / data / qcsrc / server / race.qc
1 #define MAX_CHECKPOINTS 255
2
3 .float race_checkpoint; // player: next checkpoint that has to be reached
4 .float race_laptime;
5
6 .entity sprite;
7
8 float race_checkpoint_records[MAX_CHECKPOINTS];
9 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
10 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
11 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
12 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
13
14 float race_highest_checkpoint;
15 float race_highest_place_spawn;
16
17 float race_NextCheckpoint(float f)
18 {
19         if(f >= race_highest_checkpoint)
20                 return 0;
21         else
22                 return f + 1;
23 }
24
25 float race_PreviousCheckpoint(float f)
26 {
27         if(f == -1)
28                 return 0;
29         else if(f == 0)
30                 return race_highest_checkpoint;
31         else
32                 return f - 1;
33 }
34
35 void race_SendNextCheckpoint(entity e)
36 {
37         float recordtime;
38         string recordholder;
39         float cp;
40
41         if(clienttype(e) != CLIENTTYPE_REAL)
42                 return;
43
44         if(!e.race_laptime)
45                 return;
46
47         cp = e.race_checkpoint;
48         recordtime = race_checkpoint_records[cp];
49         recordholder = race_checkpoint_recordholders[cp];
50         /*
51         recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
52         recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
53         */
54         if(recordholder == e.netname)
55                 recordholder = "";
56
57         msg_entity = e;
58         WriteByte(MSG_ONE, SVC_TEMPENTITY);
59         WriteByte(MSG_ONE, TE_CSQC_RACE);
60         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
61         WriteByte(MSG_ONE, cp); // checkpoint the player will be at next
62         WriteShort(MSG_ONE, recordtime);
63         WriteString(MSG_ONE, recordholder);
64 }
65
66 void race_SendTime(entity e, float cp, float t, float tvalid)
67 {
68         t = floor(0.5 + 10 * t); // make integer
69         float snew;
70
71         if(tvalid)
72         if(cp == 0) // finish line
73         {
74                 float s;
75                 if(g_race_qualifying)
76                 {
77                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
78                         if(!s || t < s)
79                                 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
80                 }
81                 else
82                 {
83                         s = PlayerScore_Add(e, SP_RACE_TIME, 0);
84                         snew = floor(0.5 + 10 * (time - restart_countdown));
85                         PlayerScore_Add(e, SP_RACE_TIME, snew - s);
86                         PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
87                 }
88         }
89
90         float recordtime;
91         string recordholder;
92
93         if(g_race_qualifying)
94         {
95                 if(tvalid)
96                 {
97                         recordtime = race_checkpoint_records[cp];
98                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
99                         if(recordholder == e.netname)
100                                 recordholder = "";
101
102                         if(t < recordtime || recordtime == 0)
103                         {
104                                 race_checkpoint_records[cp] = t;
105                                 if(race_checkpoint_recordholders[cp])
106                                         strunzone(race_checkpoint_recordholders[cp]);
107                                 race_checkpoint_recordholders[cp] = strzone(e.netname);
108                                 if(cp == 0)
109                                 {
110                                         float grecordtime;
111                                         string grecordholder;
112                                         grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
113                                         grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
114                                         if(grecordholder == e.netname)
115                                                 grecordholder = "";
116                                         if(grecordtime == 0)
117                                         {
118                                                 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
119                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
120                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
121                                         }
122                                         else if(t < grecordtime)
123                                         {
124                                                 if(grecordholder == "")
125                                                         bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
126                                                 else
127                                                         bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
128                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
129                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
130                                         }
131                                         else
132                                         {
133                                                 if(grecordholder == "")
134                                                         bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
135                                                 else
136                                                         bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
137                                         }
138                                 }
139
140                                 if(g_race_qualifying)
141                                 {
142                                         entity p;
143                                         FOR_EACH_REALPLAYER(p)
144                                                 if(p.race_checkpoint == cp)
145                                                         race_SendNextCheckpoint(p);
146                                 }
147                         }
148                 }
149                 else
150                 {
151                         // dummies
152                         t = 0;
153                         recordtime = 0;
154                         recordholder = "";
155                 }
156
157                 if(clienttype(e) == CLIENTTYPE_REAL)
158                 {
159                         msg_entity = e;
160                         if(g_race_qualifying)
161                         {
162                                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
163                                 WriteByte(MSG_ONE, TE_CSQC_RACE);
164                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
165                                 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
166                                 WriteShort(MSG_ONE, t); // time to that intermediate
167                                 WriteShort(MSG_ONE, recordtime); // previously best time
168                                 WriteString(MSG_ONE, recordholder); // record holder
169                         }
170                 }
171         }
172         else // RACE! Not Qualifying
173         {
174                 float lself, lother, othtime;
175                 entity oth;
176                 oth = race_checkpoint_lastplayers[cp];
177                 if(oth)
178                 {
179                         lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
180                         lother = race_checkpoint_lastlaps[cp];
181                         othtime = race_checkpoint_lasttimes[cp];
182                 }
183                 else
184                         lself = lother = othtime = 0;
185
186                 if(clienttype(e) == CLIENTTYPE_REAL)
187                 {
188                         msg_entity = e;
189                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
190                         WriteByte(MSG_ONE, TE_CSQC_RACE);
191                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
192                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
193                         if(e == oth)
194                         {
195                                 WriteShort(MSG_ONE, 0);
196                                 WriteByte(MSG_ONE, 0);
197                                 WriteString(MSG_ONE, "");
198                         }
199                         else
200                         {
201                                 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
202                                 WriteByte(MSG_ONE, lself - lother);
203                                 WriteString(MSG_ONE, oth.netname); // record holder
204                         }
205                 }
206
207                 race_checkpoint_lastplayers[cp] = e;
208                 race_checkpoint_lasttimes[cp] = time;
209                 race_checkpoint_lastlaps[cp] = lself;
210
211                 if(clienttype(oth) == CLIENTTYPE_REAL)
212                 {
213                         msg_entity = oth;
214                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
215                         WriteByte(MSG_ONE, TE_CSQC_RACE);
216                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
217                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
218                         if(e == oth)
219                         {
220                                 WriteShort(MSG_ONE, 0);
221                                 WriteByte(MSG_ONE, 0);
222                                 WriteString(MSG_ONE, "");
223                         }
224                         else
225                         {
226                                 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
227                                 WriteByte(MSG_ONE, lother - lself);
228                                 WriteString(MSG_ONE, e.netname); // record holder
229                         }
230                 }
231         }
232 }
233
234 void race_ClearTime(entity e)
235 {
236         e.race_checkpoint = -1;
237         e.race_laptime = 0;
238
239         if(clienttype(e) != CLIENTTYPE_REAL)
240                 return;
241
242         msg_entity = e;
243         WriteByte(MSG_ONE, SVC_TEMPENTITY);
244         WriteByte(MSG_ONE, TE_CSQC_RACE);
245         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
246 }
247
248 void checkpoint_touch()
249 {
250         if(other.classname != "player")
251                 return;
252
253         if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
254         {
255                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
256
257                 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
258
259                 if(!self.race_checkpoint) // finish line
260                         other.race_laptime = time;
261
262                 if(g_race_qualifying)
263                         race_SendNextCheckpoint(other);
264         }
265         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
266         {
267                 // ignored
268         }
269         else
270         {
271                 if(self.spawnflags & 4)
272                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
273         }
274 }
275
276 void checkpoint_use()
277 {
278         other = activator;
279         checkpoint_touch();
280 }
281
282 float race_waypointsprite_for_player(entity e)
283 {
284         if(e.race_checkpoint == -1)
285                 return self.modelindex;
286         else if(e.race_checkpoint == self.owner.race_checkpoint)
287                 return self.modelindex;
288         else
289                 return FALSE;
290 }
291
292 void spawnfunc_trigger_race_checkpoint()
293 {
294         vector o;
295         if(!g_race)
296         {
297                 remove(self);
298                 return;
299         }
300         InitTrigger();
301         self.use = checkpoint_use;
302         if not(self.spawnflags & 1)
303                 self.touch = checkpoint_touch;
304
305         o = (self.absmin + self.absmax) * 0.5;
306         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
307         self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
308         self.nearestwaypointtimeout = time + 1000000000;
309
310         if(!self.message)
311                 self.message = "went backwards";
312         
313         self.race_checkpoint = self.cnt;
314
315         if(self.race_checkpoint > race_highest_checkpoint)
316                 race_highest_checkpoint = self.race_checkpoint;
317
318         if(self.race_checkpoint)
319         {
320                 precache_model("models/sprites/race-checkpoint.sp2");
321                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
322         }
323         else
324         {
325                 precache_model("models/sprites/race-finish.sp2");
326                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
327         }
328         self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
329 }
330
331 void race_PreparePlayer()
332 {
333         race_ClearTime(self);
334         self.race_place = 0;
335 }
336
337 void race_RetractPlayer()
338 {
339         if(!g_race)
340                 return;
341         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
342         if(self.race_checkpoint == 0)
343         {
344                 race_ClearTime(self);
345                 self.race_checkpoint = 0;
346         }
347 }
348
349 void race_PreSpawn()
350 {
351         if(!g_race)
352                 return;
353         if(self.killcount == -666 || g_race_qualifying)
354                 race_PreparePlayer();
355 }
356
357 void race_PostSpawn(entity spot)
358 {
359         if(!g_race)
360                 return;
361         if(self.killcount != -666 && !g_race_qualifying)
362         {
363                 if(spot.target == "")
364                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
365                         race_PreparePlayer();
366                 else
367                         race_RetractPlayer();
368         }
369
370         if(spot.target != "" && self.race_checkpoint == -1)
371                 self.race_checkpoint = 0;
372
373         self.race_place = 0;
374 }
375
376 void race_PreSpawnObserver()
377 {
378         if(!g_race)
379                 return;
380         race_PreparePlayer();
381 }
382
383 void spawnfunc_info_player_race (void)
384 {
385         if(!g_race)
386         {
387                 remove(self);
388                 return;
389         }
390         ++race_spawns;
391         spawnfunc_info_player_deathmatch();
392
393         if(self.race_place > race_highest_place_spawn)
394                 race_highest_place_spawn = self.race_place;
395 }
396
397 void race_ClearRecords()
398 {
399         float i;
400         entity e;
401
402         for(i = 0; i < MAX_CHECKPOINTS; ++i)
403         {
404                 race_checkpoint_records[i] = 0;
405                 if(race_checkpoint_recordholders[i])
406                         strunzone(race_checkpoint_recordholders[i]);
407                 race_checkpoint_recordholders[i] = string_null;
408         }
409
410         FOR_EACH_CLIENT(e)
411                 race_ClearTime(e);
412 }
413
414 void race_ReadyRestart()
415 {
416         race_ClearRecords();
417
418         if(g_race_qualifying == 2)
419         {
420                 g_race_qualifying = 0;
421                 independent_players = 0;
422                 ScoreRules_race();
423         }
424
425         if(PlayerScore_Sort(race_place) <= race_highest_place_spawn)
426                 race_place_valid = 1;
427         else
428                 race_place_valid = 0;
429 }