]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
get rid of all cheat stuff from other files;
[divverent/nexuiz.git] / data / qcsrc / server / race.qc
1 #define MAX_CHECKPOINTS 255
2
3 void spawnfunc_target_checkpoint();
4
5 .float race_penalty;
6 .float race_penalty_accumulator;
7 .string race_penalty_reason;
8 .float race_checkpoint; // player: next checkpoint that has to be reached
9 .float race_laptime;
10 .entity race_lastpenalty;
11
12 .entity sprite;
13
14 float race_checkpoint_records[MAX_CHECKPOINTS];
15 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
16 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
17 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
18 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
19
20 float race_highest_checkpoint;
21 float race_timed_checkpoint;
22
23 float defrag_ents;
24 float defragcpexists;
25
26 float race_NextCheckpoint(float f)
27 {
28         if(f >= race_highest_checkpoint)
29                 return 0;
30         else
31                 return f + 1;
32 }
33
34 float race_PreviousCheckpoint(float f)
35 {
36         if(f == -1)
37                 return 0;
38         else if(f == 0)
39                 return race_highest_checkpoint;
40         else
41                 return f - 1;
42 }
43
44 // encode as:
45 //   0 = common start/finish
46 // 254 = start
47 // 255 = finish
48 float race_CheckpointNetworkID(float f)
49 {
50         if(race_timed_checkpoint)
51         {
52                 if(f == 0)
53                         return 254; // start
54                 else if(f == race_timed_checkpoint)
55                         return 255; // finish
56         }
57         return f;
58 }
59
60 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
61 {
62         float recordtime;
63         string recordholder;
64         float cp;
65
66         if(!e.race_laptime)
67                 return;
68
69         cp = e.race_checkpoint;
70         recordtime = race_checkpoint_records[cp];
71         recordholder = race_checkpoint_recordholders[cp];
72         if(recordholder == e.netname)
73                 recordholder = "";
74
75         if(!spec)
76                 msg_entity = e;
77         WRITESPECTATABLE_MSG_ONE({
78                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
79                 WriteByte(MSG_ONE, TE_CSQC_RACE);
80                 if(spec)
81                 {
82                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
83                         //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
84                         WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
85                 }
86                 else
87                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
88                 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
89                 WriteInt24_t(MSG_ONE, recordtime);
90                 WriteString(MSG_ONE, recordholder);
91         });
92 }
93
94 void race_InitSpectator()
95 {
96         if(g_race_qualifying)
97                 if(msg_entity.enemy.race_laptime)
98                         race_SendNextCheckpoint(msg_entity.enemy, 1);
99 }
100
101 string rr;
102 float grecordtime[RANKINGS_CNT];
103 string grecordholder[RANKINGS_CNT];
104 #ifdef UID
105 string grecorduid[RANKINGS_CNT];
106 #endif
107 float worst_time; // last ranked time
108 float have_recs; // have we already read the records from the database before?
109 float race_GetTime(float pos) {
110         if(g_cts)
111                 rr = CTS_RECORD;
112         else
113                 rr = RACE_RECORD;
114
115         if (!have_recs) { // I guess this is better than checking if the first array item is empty, rumor has it that arrays are slow
116                 float i;
117                 for(i=0;i<RANKINGS_CNT;++i) {
118                         grecordtime[i] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i))));
119                         grecordholder[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i))));
120 #ifdef UID
121                         grecorduid[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i))));
122 #endif
123                 }
124                 grecordtime[0] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
125                 grecordholder[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname")));
126 #ifdef UID
127                 grecorduid[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "uid")));
128 #endif
129                 worst_time = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, strcat("time", ftos(RANKINGS_CNT-1)))));
130                 have_recs = 1;
131         }
132
133         return grecordtime[pos-1];
134 }
135
136 string race_GetName(float pos) { // these other functions assume that race_GetTime has been called >= once before
137         return grecordholder[pos-1];
138 }
139
140 #ifdef UID
141 float race_CheckUID(string myuid, string net_name) { // return existing UID or player name ranking pos, else 0
142         float i;
143         for (i=RANKINGS_CNT-1;i>=0;--i)
144                 if(grecorduid[i] == myuid)
145                         return i+1;
146         for (i=RANKINGS_CNT-1;i>=0;--i)
147                 if(grecordholder[i] == net_name)
148                         return i+1;
149         return 0;
150 }
151 #endif
152
153 float race_CheckName(string net_name) { // Does the name already exist in rankings? In that case, where? (otherwise 0)
154         float i;
155         for (i=RANKINGS_CNT-1;i>=0;--i)
156                 if(grecordholder[i] == net_name)
157                         return i+1;
158         return 0;
159 }
160
161 float race_GetPos(float t) {
162         float i;
163
164         if(worst_time == 0)
165         for (i=0;i<RANKINGS_CNT;++i)
166                 if (grecordtime[i] == 0 || grecordtime[i] > t)
167                         return i+1;
168
169         for (i=0;i<RANKINGS_CNT;++i)
170                 if (grecordtime[i] > t)
171                         return i+1;
172         return 0;
173 }
174
175 void race_send_recordtime(float msg)
176 {
177         // send the server best time
178         WriteByte(msg, SVC_TEMPENTITY);
179         WriteByte(msg, TE_CSQC_RACE);
180         WriteByte(msg, RACE_NET_SERVER_RECORD);
181         WriteInt24_t(msg, race_GetTime(1));
182 }
183
184 void race_SendRankings(float pos, float prevpos, float del, float msg)
185 {
186         WriteByte(msg, SVC_TEMPENTITY);
187         WriteByte(msg, TE_CSQC_RACE);
188         WriteByte(msg, RACE_NET_SERVER_RANKINGS);
189         WriteShort(msg, pos);
190         WriteShort(msg, prevpos);
191         WriteShort(msg, del);
192         WriteString(msg, race_GetName(pos));
193         WriteInt24_t(msg, race_GetTime(pos));
194 }
195
196 void race_SendStatus(float id, entity e)
197 {
198         float msg;
199         if (id == 0)
200                 msg = MSG_ONE;
201         else
202                 msg = MSG_ALL;
203         msg_entity = e;
204         WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
205                 WriteByte(msg, SVC_TEMPENTITY);
206                 WriteByte(msg, TE_CSQC_RACE);
207                 WriteByte(msg, RACE_NET_SERVER_STATUS);
208                 WriteShort(msg, id);
209                 WriteString(msg, e.netname);
210         });
211 }
212
213 string race_PlaceName(float pos) {
214         if(pos == 1)
215                 return "1st";
216         else if(pos == 2)
217                 return "2nd";
218         else if(pos == 3)
219                 return "3rd";
220         else
221                 return strcat(ftos(pos), "th");
222 }
223
224 void race_SetTime(entity e, float t, float match_rec) {
225         float pos, prevpos;
226         pos = race_GetPos(t);
227 #ifdef UID
228         prevpos = race_CheckUID(e.uid, e.netname);
229 #else
230         prevpos = race_CheckName(e.netname);
231 #endif
232
233         float oldrec;
234         string recorddifference;
235         if (prevpos && (prevpos < pos || !pos))
236         {
237                 oldrec = race_GetTime(prevpos);
238                 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
239                 bprint(e.netname, "^7 couldn't break their ", race_PlaceName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
240                 race_SendStatus(0, e); // "fail"
241                 return;
242         } else if (!pos) { // no ranking, time worse than the worst ranked
243                 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - grecordtime[RANKINGS_CNT-1]), "]");
244                 bprint(e.netname, "^7 couldn't break the ", race_PlaceName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
245                 race_SendStatus(0, e); // "fail"
246                 return;
247         }
248
249         // move other rankings out of the way
250         float i;
251         if (prevpos) { // player improved his existing record
252                 if(prevpos == pos)
253                         oldrec = grecordtime[pos-1];
254                 else
255                         oldrec = grecordtime[pos-1];
256                 for (i=prevpos-1;i>pos-1;--i) {
257                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
258                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
259 #ifdef UID
260                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i-1]);
261 #endif
262                         grecordtime[i] = grecordtime[i-1];
263
264                         if (grecordholder[i])
265                                 strunzone(grecordholder[i]);
266                         grecordholder[i] = strzone(grecordholder[i-1]);
267 #ifdef UID
268                         if (grecorduid[i])
269                                 strunzone(grecorduid[i]);
270                         grecorduid[i] = strzone(grecorduid[i-1]);
271 #endif
272                 }
273         } else { // player has no ranked record yet
274                 oldrec = grecordtime[pos-1];
275                 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
276                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
277                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
278 #ifdef UID
279                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i-1]);
280 #endif
281                         grecordtime[i] = grecordtime[i-1];
282
283                         if (grecordholder[i])
284                                 strunzone(grecordholder[i]);
285                         grecordholder[i] = strzone(grecordholder[i-1]);
286 #ifdef UID
287                         if (grecorduid[i])
288                                 strunzone(grecorduid[i]);
289                         grecorduid[i] = strzone(grecorduid[i-1]);
290 #endif
291                 }
292         }
293         
294         // store new ranking
295         if (pos == 1) {
296                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
297                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
298 #ifdef UID
299                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid"), e.uid);
300 #endif
301
302                 grecordtime[0] = t;
303
304                 if (grecordholder[0])
305                         strunzone(grecordholder[0]);
306                 grecordholder[0] = strzone(e.netname);
307 #ifdef UID
308                 if (grecorduid[0])
309                         strunzone(grecorduid[0]);
310                 grecorduid[0] = strzone(e.uid);
311 #endif
312                 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
313                 race_send_recordtime(MSG_ALL);
314         } else {
315                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(pos-1)), ftos(t));
316                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(pos-1)), e.netname);
317 #ifdef UID
318                 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(pos-1)), e.uid);
319 #endif
320
321                 grecordtime[pos-1] = t;
322
323                 if (grecordholder[pos-1])
324                         strunzone(grecordholder[pos-1]);
325                 grecordholder[pos-1] = strzone(e.netname);
326 #ifdef UID
327                 if (grecorduid[pos-1])
328                         strunzone(grecorduid[pos-1]);
329                 grecorduid[pos-1] = strzone(e.uid);
330 #endif
331         }
332
333         if (pos == RANKINGS_CNT)
334                 worst_time = t;
335
336         race_SendRankings(pos, prevpos, 0, MSG_ALL);
337         if(rankings_reply)
338                 strunzone(rankings_reply);
339         rankings_reply = strzone(getrankings());
340         if(pos == 1) {
341                 if(pos == prevpos) {
342                         recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
343                         bprint(e.netname, "^1 improved their 1st place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
344                         race_SendStatus(3, e); // "new server record"
345                 } else {
346                         recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
347                         bprint(e.netname, "^1 broke ", grecordholder[pos], "^1's 1st place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
348                         race_SendStatus(3, e); // "new server record"
349                 }
350         } else {
351                 if(pos == prevpos) {
352                         recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
353                         bprint(e.netname, "^5 improved their ", race_PlaceName(pos), " ^5place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
354                         race_SendStatus(1, e); // "new time"
355                 } else if (oldrec == 0) {
356                         bprint(e.netname, "^2 set the ", race_PlaceName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
357                         race_SendStatus(2, e); // "new rank"
358                 } else {
359                         recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
360                         bprint(e.netname, "^2 broke ", grecordholder[pos], "^2's ", race_PlaceName(pos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
361                         race_SendStatus(2, e); // "new rank"
362                 }
363         }
364 }
365
366 void race_DeleteTime(float pos) {
367         float i;
368
369         for (i = pos-1; i <= RANKINGS_CNT-1; ++i) {
370                 if (i == 0) {
371                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(grecordtime[1]));
372                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), grecordholder[1]);
373 #ifdef UID
374                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid"), grecorduid[1]);
375 #endif
376                         grecordtime[0] = grecordtime[1];
377                         if (grecordholder[i])
378                                 strunzone(grecordholder[0]);
379                         grecordholder[0] = strzone(grecordholder[1]);
380
381 #ifdef UID
382                         if (grecorduid[i])
383                                 strunzone(grecorduid[0]);
384                         grecorduid[0] = strzone(grecorduid[1]);
385 #endif
386                 }
387                 else if (i == RANKINGS_CNT-1) {
388                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), string_null);
389                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), string_null);
390 #ifdef UID
391                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), string_null);
392 #endif
393                         grecordtime[i] = 0;
394                         if (grecordholder[i])
395                                 strunzone(grecordholder[i]);
396                         grecordholder[i] = string_null;
397
398 #ifdef UID
399                         if (grecorduid[i])
400                                 strunzone(grecorduid[i]);
401                         grecorduid[i] = string_null;
402 #endif
403                 }
404                 else {
405                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i+1]));
406                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i+1]);
407 #ifdef UID
408                         db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i+1]);
409 #endif
410                         grecordtime[i] = grecordtime[i+1];
411                         if (grecordholder[i])
412                                 strunzone(grecordholder[i]);
413                         grecordholder[i] = strzone(grecordholder[i+1]);
414
415 #ifdef UID
416                         if (grecorduid[i])
417                                 strunzone(grecorduid[i]);
418                         grecorduid[i] = strzone(grecorduid[i+1]);
419 #endif
420                 }
421         }
422
423         race_SendRankings(pos, 0, 1, MSG_ALL);
424         if(pos == 1)
425                 race_send_recordtime(MSG_ALL);
426
427         if(rankings_reply)
428                 strunzone(rankings_reply);
429         rankings_reply = strzone(getrankings());
430
431         worst_time = 0;
432 }
433
434 void race_SendTime(entity e, float cp, float t, float tvalid)
435 {
436         float snew, l;
437         entity p;
438
439         if(g_race_qualifying)
440                 t += e.race_penalty_accumulator;
441
442         t = TIME_ENCODE(t); // make integer
443         // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
444
445         if(tvalid)
446         if(cp == race_timed_checkpoint) // finish line
447         if not(e.race_completed)
448         {
449                 float s;
450                 if(g_race_qualifying)
451                 {
452                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
453                         if(!s || t < s)
454                                 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
455                 }
456                 else
457                 {
458                         s = PlayerScore_Add(e, SP_RACE_TIME, 0);
459                         snew = TIME_ENCODE(time - game_starttime);
460                         PlayerScore_Add(e, SP_RACE_TIME, snew - s);
461                         l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
462
463                         if(cvar("fraglimit"))
464                                 if(l >= cvar("fraglimit"))
465                                         race_StartCompleting();
466
467                         if(race_completing)
468                         {
469                                 e.race_completed = 1;
470                                 MAKE_INDEPENDENT_PLAYER(e);
471                                 bprint(e.netname, "^7 has finished the race.\n");
472                                 ClientData_Touch(e);
473                         }
474                 }
475         }
476
477         float recordtime;
478         string recordholder;
479         if(g_race_qualifying)
480         {
481                 if(tvalid)
482                 {
483                         recordtime = race_checkpoint_records[cp];
484                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
485                         if(recordholder == e.netname)
486                                 recordholder = "";
487
488                         if(t != 0) {
489                                 if(cp == race_timed_checkpoint)
490                                         race_SetTime(e, t, recordtime);
491
492                                 if(t < recordtime || recordtime == 0)
493                                 {
494                                         race_checkpoint_records[cp] = t;
495                                         if(race_checkpoint_recordholders[cp])
496                                                 strunzone(race_checkpoint_recordholders[cp]);
497                                         race_checkpoint_recordholders[cp] = strzone(e.netname);
498                                         if(g_race_qualifying)
499                                         {
500                                                 FOR_EACH_REALPLAYER(p)
501                                                         if(p.race_checkpoint == cp)
502                                                                 race_SendNextCheckpoint(p, 0);
503                                         }
504                                 }
505                         }
506                 }
507                 else
508                 {
509                         // dummies
510                         t = 0;
511                         recordtime = 0;
512                         recordholder = "";
513                 }
514
515                 msg_entity = e;
516                 if(g_race_qualifying)
517                 {
518                         WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
519                                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
520                                 WriteByte(MSG_ONE, TE_CSQC_RACE);
521                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
522                                 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
523                                 WriteInt24_t(MSG_ONE, t); // time to that intermediate
524                                 WriteInt24_t(MSG_ONE, recordtime); // previously best time
525                                 WriteString(MSG_ONE, recordholder); // record holder
526                         });
527                 }
528         }
529         else // RACE! Not Qualifying
530         {
531                 float lself, lother, othtime;
532                 entity oth;
533                 oth = race_checkpoint_lastplayers[cp];
534                 if(oth)
535                 {
536                         lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
537                         lother = race_checkpoint_lastlaps[cp];
538                         othtime = race_checkpoint_lasttimes[cp];
539                 }
540                 else
541                         lself = lother = othtime = 0;
542
543                 msg_entity = e;
544                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
545                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
546                         WriteByte(MSG_ONE, TE_CSQC_RACE);
547                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
548                         WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
549                         if(e == oth)
550                         {
551                                 WriteInt24_t(MSG_ONE, 0);
552                                 WriteByte(MSG_ONE, 0);
553                                 WriteString(MSG_ONE, "");
554                         }
555                         else
556                         {
557                                 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
558                                 WriteByte(MSG_ONE, lself - lother);
559                                 WriteString(MSG_ONE, oth.netname); // record holder
560                         }
561                 });
562
563                 race_checkpoint_lastplayers[cp] = e;
564                 race_checkpoint_lasttimes[cp] = time;
565                 race_checkpoint_lastlaps[cp] = lself;
566
567                 msg_entity = oth;
568                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
569                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
570                         WriteByte(MSG_ONE, TE_CSQC_RACE);
571                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
572                         WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
573                         if(e == oth)
574                         {
575                                 WriteInt24_t(MSG_ONE, 0);
576                                 WriteByte(MSG_ONE, 0);
577                                 WriteString(MSG_ONE, "");
578                         }
579                         else
580                         {
581                                 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
582                                 WriteByte(MSG_ONE, lother - lself);
583                                 WriteString(MSG_ONE, e.netname); // record holder
584                         }
585                 });
586         }
587 }
588
589 void race_ClearTime(entity e)
590 {
591         e.race_checkpoint = 0;
592         e.race_laptime = 0;
593         e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
594         e.race_penalty_accumulator = 0;
595         e.race_lastpenalty = world;
596
597         msg_entity = e;
598         WRITESPECTATABLE_MSG_ONE({
599                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
600                 WriteByte(MSG_ONE, TE_CSQC_RACE);
601                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
602         });
603 }
604
605 void dumpsurface(entity e)
606 {
607         float n, si, ni;
608         vector norm, vec;
609         print("Surfaces of ", etos(e), ":\n");
610
611         print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
612
613         for(si = 0; ; ++si)
614         {
615                 n = getsurfacenumpoints(e, si);
616                 if(n <= 0)
617                         break;
618                 print("  Surface ", ftos(si), ":\n");
619                 norm = getsurfacenormal(e, si);
620                 print("    Normal = ", vtos(norm), "\n");
621                 for(ni = 0; ni < n; ++ni)
622                 {
623                         vec = getsurfacepoint(e, si, ni);
624                         print("    Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
625                 }
626         }
627 }
628
629 void checkpoint_passed()
630 {
631         string oldmsg;
632         entity cp;
633
634         if(other.classname == "porto")
635         {
636                 // do not allow portalling through checkpoints
637                 trace_plane_normal = normalize(-1 * other.velocity);
638                 self = other;
639                 W_Porto_Fail(0);
640                 return;
641         }
642
643         /*
644          * Trigger targets
645          */
646         if not((self.spawnflags & 2) && (other.classname == "player"))
647         {
648                 activator = other;
649                 oldmsg = self.message;
650                 self.message = "";
651                 SUB_UseTargets();
652                 self.message = oldmsg;
653         }
654
655         if(other.classname != "player")
656                 return;
657
658         /*
659          * Remove unauthorized equipment
660          */
661         Portal_ClearAll(other);
662
663         other.porto_forbidden = 2; // decreased by 1 each StartFrame
664
665         if(defrag_ents) {
666                 if(self.race_checkpoint == -2) 
667                 {
668                         self.race_checkpoint = other.race_checkpoint;
669                 }
670
671                 float largest_cp_id;
672                 float cp_amount;
673                 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
674                         cp_amount += 1;
675                         if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
676                         {
677                                 largest_cp_id = cp.race_checkpoint;
678                                 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
679                                         cp.race_checkpoint = largest_cp_id + 1; // finish line
680                                 race_highest_checkpoint = largest_cp_id + 1;
681                                 race_timed_checkpoint = largest_cp_id + 1;
682
683                                 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
684                                         if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
685                                                 defragcpexists = -1;
686                                 }       
687                         }
688                 }
689                 if(cp_amount == 0) {
690                         for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
691                                 cp.race_checkpoint = 1;
692                         race_highest_checkpoint = 1;
693                         race_timed_checkpoint = 1;
694                 }
695         }
696
697         if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
698         {
699                 if(self.race_penalty)
700                 {
701                         if(other.race_lastpenalty != self)
702                         {
703                                 other.race_lastpenalty = self;
704                                 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
705                         }
706                 }
707
708                 if(other.race_penalty)
709                         return;
710
711                 /*
712                  * Trigger targets
713                  */
714                 if(self.spawnflags & 2)
715                 {
716                         activator = other;
717                         oldmsg = self.message;
718                         self.message = "";
719                         SUB_UseTargets();
720                         self.message = oldmsg;
721                 }
722
723                 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
724                         other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
725                 other.race_respawn_checkpoint = self.race_checkpoint;
726                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
727                 other.race_started = 1;
728
729                 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
730
731                 if(!self.race_checkpoint) // start line
732                 {
733                         other.race_laptime = time;
734                         other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
735                         other.race_penalty_accumulator = 0;
736                         other.race_lastpenalty = world;
737                 }
738
739                 if(g_race_qualifying)
740                         race_SendNextCheckpoint(other, 0);
741
742                 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
743                 {
744                         float fh;
745                         defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
746                         if(fh >= 0)
747                         {
748                                 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
749                                 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
750                         }
751                         fclose(fh);
752                 }
753         }
754         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
755         {
756                 // ignored
757         }
758         else
759         {
760                 if(self.spawnflags & 4)
761                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
762         }
763 }
764
765 void checkpoint_touch()
766 {
767         EXACTTRIGGER_TOUCH;
768         checkpoint_passed();
769 }
770
771 void checkpoint_use()
772 {
773         if(other.classname == "info_player_deathmatch") // a spawn, a spawn
774                 return;
775
776         other = activator;
777         checkpoint_passed();
778 }
779
780 float race_waypointsprite_visible_for_player(entity e)
781 {
782         if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
783                 return TRUE;
784         else if(e.race_checkpoint == self.owner.race_checkpoint)
785                 return TRUE;
786         else
787                 return FALSE;
788 }
789
790 float have_verified;
791 void trigger_race_checkpoint_verify()
792 {
793         entity oldself, cp;
794         float i, p;
795         float qual;
796
797         if(have_verified)
798                 return;
799         have_verified = 1;
800         
801         qual = g_race_qualifying;
802
803         oldself = self;
804         self = spawn();
805         self.classname = "player";
806
807         if(g_race)
808         {
809                 for(i = 0; i <= race_highest_checkpoint; ++i)
810                 {
811                         self.race_checkpoint = race_NextCheckpoint(i);
812
813                         // race only (middle of the race)
814                         g_race_qualifying = 0;
815                         self.race_place = 0;
816                         if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
817                                 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
818
819                         if(i == 0)
820                         {
821                                 // qualifying only
822                                 g_race_qualifying = 1;
823                                 self.race_place = race_lowest_place_spawn;
824                                 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
825                                         error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
826                                 
827                                 // race only (initial spawn)
828                                 g_race_qualifying = 0;
829                                 for(p = 1; p <= race_highest_place_spawn; ++p)
830                                 {
831                                         self.race_place = p;
832                                         if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
833                                                 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
834                                 }
835                         }
836                 }
837         }
838         else if(!defrag_ents)
839         {
840                 // qualifying only
841                 self.race_checkpoint = race_NextCheckpoint(0);
842                 g_race_qualifying = 1;
843                 self.race_place = race_lowest_place_spawn;
844                 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
845                         error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
846         }
847         else
848         {
849                 self.race_checkpoint = race_NextCheckpoint(0);
850                 g_race_qualifying = 1;
851                 self.race_place = 0; // there's only one spawn on defrag maps
852  
853                 // check if a defragcp file already exists, then read it and apply the checkpoint order
854                 float fh;
855                 float len;
856                 string l;
857
858                 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
859                 if(fh >= 0)
860                 {
861                         while((l = fgets(fh)))
862                         {
863                                 len = tokenize_console(l);
864                                 if(len != 2) {
865                                         defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
866                                         continue;
867                                 }
868                                 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
869                                         if(argv(0) == cp.targetname)
870                                                 cp.race_checkpoint = stof(argv(1));
871                         }
872                         fclose(fh);
873                 }
874         }
875
876         g_race_qualifying = qual;
877
878         if(race_timed_checkpoint) {
879                 if(defrag_ents) {
880                         for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
881                                 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
882                         for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
883                                 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
884
885                         for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
886                                 if(cp.race_checkpoint == -2) // something's wrong with the defrag cp file or it has not been written yet, set defragcpexists to -1 so that it will be rewritten when someone finishes
887                                         defragcpexists = -1;
888                         }
889
890                         if(defragcpexists != -1){
891                                 float largest_cp_id;
892                                 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
893                                         if(cp.race_checkpoint > largest_cp_id)
894                                                 largest_cp_id = cp.race_checkpoint;
895                                 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
896                                         cp.race_checkpoint = largest_cp_id + 1; // finish line
897                                 race_highest_checkpoint = largest_cp_id + 1;
898                                 race_timed_checkpoint = largest_cp_id + 1;
899                         } else {
900                                 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
901                                         cp.race_checkpoint = 255; // finish line
902                                 race_highest_checkpoint = 255;
903                                 race_timed_checkpoint = 255;
904                         }
905                 }
906                 else {
907                         for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
908                                 if(cp.sprite)
909                                 {
910                                         if(cp.race_checkpoint == 0)
911                                                 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
912                                         else if(cp.race_checkpoint == race_timed_checkpoint)
913                                                 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
914                                 }
915                 }
916         }
917
918         if(defrag_ents) {
919                 entity trigger, targ;
920                 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
921                         for(targ = world; (targ = find(targ, targetname, trigger.target)); )
922                                 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
923                                         targ.wait = -2;
924                                         targ.delay = 0;
925
926                                         setsize(targ, trigger.mins, trigger.maxs);
927                                         setorigin(targ, trigger.origin);
928                                         remove(trigger);
929                                 }
930         }
931         remove(self);
932         self = oldself;
933 }
934
935 void spawnfunc_trigger_race_checkpoint()
936 {
937         vector o;
938         if(!g_race && !g_cts)
939         {
940                 remove(self);
941                 return;
942         }
943
944         EXACTTRIGGER_INIT;
945
946         self.use = checkpoint_use;
947         if not(self.spawnflags & 1)
948                 self.touch = checkpoint_touch;
949
950         o = (self.absmin + self.absmax) * 0.5;
951         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
952         waypoint_spawnforitem_force(self, trace_endpos);
953         self.nearestwaypointtimeout = time + 1000000000;
954
955         if(!self.message)
956                 self.message = "went backwards";
957         if (!self.message2)
958                 self.message2 = "was pushed backwards by";
959         if (!self.race_penalty_reason)
960                 self.race_penalty_reason = "missing a checkpoint";
961         
962         self.race_checkpoint = self.cnt;
963
964         if(self.race_checkpoint > race_highest_checkpoint)
965         {
966                 race_highest_checkpoint = self.race_checkpoint;
967                 if(self.spawnflags & 8)
968                         race_timed_checkpoint = self.race_checkpoint;
969                 else
970                         race_timed_checkpoint = 0;
971         }
972
973         if(!self.race_penalty)
974         {
975                 if(self.race_checkpoint)
976                         WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
977                 else
978                         WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
979         }
980
981         self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
982
983         InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
984 }
985
986 void spawnfunc_target_checkpoint() // defrag entity
987 {
988         vector o;
989         if(!g_race && !g_cts)
990         {
991                 remove(self);
992                 return;
993         }
994         defrag_ents = 1;
995
996         EXACTTRIGGER_INIT;
997
998         self.use = checkpoint_use;
999         if not(self.spawnflags & 1)
1000                 self.touch = checkpoint_touch;
1001
1002         o = (self.absmin + self.absmax) * 0.5;
1003         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
1004         waypoint_spawnforitem_force(self, trace_endpos);
1005         self.nearestwaypointtimeout = time + 1000000000;
1006
1007         if(!self.message)
1008                 self.message = "went backwards";
1009         if (!self.message2)
1010                 self.message2 = "was pushed backwards by";
1011         if (!self.race_penalty_reason)
1012                 self.race_penalty_reason = "missing a checkpoint";
1013
1014         if(self.classname == "target_startTimer")
1015                 self.race_checkpoint = 0;
1016         else
1017                 self.race_checkpoint = -2;
1018
1019         race_timed_checkpoint = 1;
1020
1021         if(self.race_checkpoint == 0)
1022                 WaypointSprite_SpawnFixed("race-start", o, self, sprite);
1023         else
1024                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
1025
1026         self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1027
1028         InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1029 }
1030
1031 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
1032 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
1033
1034 void race_AbandonRaceCheck(entity p)
1035 {
1036         if(race_completing && !p.race_completed)
1037         {
1038                 p.race_completed = 1;
1039                 MAKE_INDEPENDENT_PLAYER(p);
1040                 bprint(p.netname, "^7 has abandoned the race.\n");
1041                 ClientData_Touch(p);
1042         }
1043 }
1044
1045 void race_StartCompleting()
1046 {
1047         entity p;
1048         race_completing = 1;
1049         FOR_EACH_PLAYER(p)
1050                 if(p.deadflag != DEAD_NO)
1051                         race_AbandonRaceCheck(p);
1052 }
1053
1054 void race_PreparePlayer()
1055 {
1056         race_ClearTime(self);
1057         self.race_place = 0;
1058         self.race_started = 0;
1059         self.race_respawn_checkpoint = 0;
1060         self.race_respawn_spotref = world;
1061 }
1062
1063 void race_RetractPlayer()
1064 {
1065         if(!g_race && !g_cts)
1066                 return;
1067         if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1068                 race_ClearTime(self);
1069         self.race_checkpoint = self.race_respawn_checkpoint;
1070 }
1071
1072 void race_PreDie()
1073 {
1074         if(!g_race && !g_cts)
1075                 return;
1076
1077         race_AbandonRaceCheck(self);
1078 }
1079
1080 void race_PreSpawn()
1081 {
1082         if(!g_race && !g_cts)
1083                 return;
1084         if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
1085                 race_PreparePlayer();
1086         else // respawn
1087                 race_RetractPlayer();
1088
1089         race_AbandonRaceCheck(self);
1090 }
1091
1092 void race_PostSpawn(entity spot)
1093 {
1094         if(!g_race && !g_cts)
1095                 return;
1096
1097         if(spot.target == "")
1098                 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
1099                 race_PreparePlayer();
1100
1101         // if we need to respawn, do it right
1102         self.race_respawn_checkpoint = self.race_checkpoint;
1103         self.race_respawn_spotref = spot;
1104
1105         self.race_place = 0;
1106 }
1107
1108 void race_PreSpawnObserver()
1109 {
1110         if(!g_race && !g_cts)
1111                 return;
1112         race_PreparePlayer();
1113         self.race_checkpoint = -1;
1114 }
1115
1116 void spawnfunc_info_player_race (void)
1117 {
1118         if(!g_race && !g_cts)
1119         {
1120                 remove(self);
1121                 return;
1122         }
1123         ++race_spawns;
1124         spawnfunc_info_player_deathmatch();
1125
1126         if(self.race_place > race_highest_place_spawn)
1127                 race_highest_place_spawn = self.race_place;
1128         if(self.race_place < race_lowest_place_spawn)
1129                 race_lowest_place_spawn = self.race_place;
1130 }
1131
1132 void race_ClearRecords()
1133 {
1134         float i;
1135         entity e;
1136
1137         for(i = 0; i < MAX_CHECKPOINTS; ++i)
1138         {
1139                 race_checkpoint_records[i] = 0;
1140                 if(race_checkpoint_recordholders[i])
1141                         strunzone(race_checkpoint_recordholders[i]);
1142                 race_checkpoint_recordholders[i] = string_null;
1143         }
1144
1145         e = self;
1146         FOR_EACH_CLIENT(self)
1147         {
1148                 float p;
1149                 p = self.race_place;
1150                 race_PreparePlayer();
1151                 self.race_place = p;
1152         }
1153         self = e;
1154 }
1155
1156 void race_ReadyRestart()
1157 {
1158         float s;
1159
1160         Score_NicePrint(world);
1161
1162         race_ClearRecords();
1163         PlayerScore_Sort(race_place);
1164
1165         entity e;
1166         FOR_EACH_CLIENT(e)
1167         {
1168                 if(e.race_place)
1169                 {
1170                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1171                         if(!s)
1172                                 e.race_place = 0;
1173                 }
1174                 print(e.netname, " = ", ftos(e.race_place), "\n");
1175         }
1176
1177         if(g_race_qualifying == 2)
1178         {
1179                 g_race_qualifying = 0;
1180                 independent_players = 0;
1181                 cvar_set("fraglimit", ftos(race_fraglimit));
1182                 cvar_set("leadlimit", ftos(race_leadlimit));
1183                 cvar_set("timelimit", ftos(race_timelimit));
1184                 ScoreRules_race();
1185         }
1186 }
1187
1188 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1189 {
1190         if(g_race_qualifying)
1191         {
1192                 pl.race_penalty_accumulator += penalty;
1193                 msg_entity = pl;
1194                 WRITESPECTATABLE_MSG_ONE({
1195                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
1196                         WriteByte(MSG_ONE, TE_CSQC_RACE);
1197                         WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1198                         WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1199                         WriteString(MSG_ONE, reason);
1200                 });
1201         }
1202         else
1203         {
1204                 pl.race_penalty = time + penalty;
1205                 msg_entity = pl;
1206                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1207                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
1208                         WriteByte(MSG_ONE, TE_CSQC_RACE);
1209                         WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1210                         WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1211                         WriteString(MSG_ONE, reason);
1212                 });
1213         }
1214 }
1215
1216 void penalty_touch()
1217 {
1218         EXACTTRIGGER_TOUCH;
1219         if(other.race_lastpenalty != self)
1220         {
1221                 other.race_lastpenalty = self;
1222                 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1223         }
1224 }
1225
1226 void penalty_use()
1227 {
1228         race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1229 }
1230
1231 void spawnfunc_trigger_race_penalty()
1232 {
1233         EXACTTRIGGER_INIT;
1234
1235         self.use = penalty_use;
1236         if not(self.spawnflags & 1)
1237                 self.touch = penalty_touch;
1238
1239         if (!self.race_penalty_reason)
1240                 self.race_penalty_reason = "missing a checkpoint";
1241         if (!self.race_penalty)
1242                 self.race_penalty = 5;
1243 }
1244
1245 float race_GetFractionalLapCount(entity e)
1246 {
1247         // interesting metrics (idea by KrimZon) to maybe sort players in the
1248         // scoreboard, immediately updates when overtaking
1249         //
1250         // requires the track to be built so you never get farther away from the
1251         // next checkpoint, though, and current Nexuiz race maps are not built that
1252         // way
1253         //
1254         // also, this code is slow and would need optimization (i.e. "next CP"
1255         // links on CP entities)
1256
1257         float l;
1258         l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1259         if(e.race_completed)
1260                 return l; // not fractional
1261         
1262         vector o0, o1;
1263         float bestfraction, fraction;
1264         entity lastcp, cp0, cp1;
1265         float nextcpindex, lastcpindex;
1266
1267         nextcpindex = max(e.race_checkpoint, 0);
1268         lastcpindex = e.race_respawn_checkpoint;
1269         lastcp = e.race_respawn_spotref;
1270
1271         if(nextcpindex == lastcpindex)
1272                 return l; // finish
1273         
1274         bestfraction = 1;
1275         for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1276         {
1277                 if(cp0.race_checkpoint != lastcpindex)
1278                         continue;
1279                 if(lastcp)
1280                         if(cp0 != lastcp)
1281                                 continue;
1282                 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1283                 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1284                 {
1285                         if(cp1.race_checkpoint != nextcpindex)
1286                                 continue;
1287                         o1 = (cp1.absmin + cp1.absmax) * 0.5;
1288                         if(o0 == o1)
1289                                 continue;
1290                         fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1291                         if(fraction < bestfraction)
1292                                 bestfraction = fraction;
1293                 }
1294         }
1295
1296         // we are at CP "nextcpindex - bestfraction"
1297         // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1298         // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1299         float c, nc;
1300         nc = race_highest_checkpoint + 1;
1301         c = (mod(nextcpindex - race_timed_checkpoint + nc + nc - 1, nc) + 1) - bestfraction;
1302
1303         return l + c / nc;
1304 }