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