]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/race.qc
Merge branch 'master' into Juhu/strafehud
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / race.qc
1 #include "race.qh"
2
3 #include <common/weapons/_all.qh>
4 #include <common/stats.qh>
5 #include <server/damage.qh>
6 #include <server/gamelog.qh>
7 #include <server/intermission.qh>
8 #include <server/main.qh>
9 #include <server/mutators/_mod.qh>
10 #include <server/world.qh>
11 #include <server/weapons/common.qh>
12 #include "client.qh"
13 #include "cheats.qh"
14 #include "portals.qh"
15 #include "scores.qh"
16 #include "spawnpoints.qh"
17 #include "bot/api.qh"
18 #include "command/getreplies.qh"
19 #include "../common/deathtypes/all.qh"
20 #include "../common/notifications/all.qh"
21 #include <common/gamemodes/_mod.qh>
22 #include <common/gamemodes/rules.qh>
23 #include <common/net_linked.qh>
24 #include <common/state.qh>
25 #include <common/weapons/weapon/porto.qh>
26 #include "../common/mapobjects/subs.qh"
27 #include <common/mapobjects/triggers.qh>
28 #include "../lib/warpzone/util_server.qh"
29 #include "../lib/warpzone/common.qh"
30 #include <common/vehicles/sv_vehicles.qh>
31 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
32
33 string uid2name(string myuid)
34 {
35         string s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
36
37         // FIXME remove this later after 0.6 release
38         // convert old style broken records to correct style
39         if(s == "")
40         {
41                 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
42                 if(s != "")
43                 {
44                         db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
45                         db_remove(ServerProgsDB, strcat("uid2name", myuid));
46                 }
47         }
48
49         if(s == "")
50                 s = "^1Unregistered Player";
51         return s;
52 }
53
54 void write_recordmarker(entity pl, float tstart, float dt)
55 {
56     GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
57
58     // also write a marker into demo files for demotc-race-record-extractor to find
59     stuffcmd(pl,
60              strcat(
61                  strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
62                  " ", ftos(tstart), " ", ftos(dt), "\n"));
63 }
64
65 IntrusiveList g_race_targets;
66 IntrusiveList g_racecheckpoints;
67 STATIC_INIT(g_race)
68 {
69         g_race_targets = IL_NEW();
70         g_racecheckpoints = IL_NEW();
71 }
72
73 void race_InitSpectator()
74 {
75         if(g_race_qualifying)
76                 if(msg_entity.enemy.race_laptime)
77                         race_SendNextCheckpoint(msg_entity.enemy, 1);
78 }
79
80 float race_readTime(string map, float pos)
81 {
82         string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
83
84         return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
85 }
86
87 string race_readUID(string map, float pos)
88 {
89         string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
90
91         return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
92 }
93
94 float race_readPos(string map, float t)
95 {
96         for(int i = 1; i <= RANKINGS_CNT; ++i)
97         {
98                 int mytime = race_readTime(map, i);
99                 if(!mytime || mytime > t)
100                         return i;
101         }
102
103         return 0; // pos is zero if unranked
104 }
105
106 void race_writeTime(string map, float t, string myuid)
107 {
108         string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
109
110         float newpos;
111         newpos = race_readPos(map, t);
112
113         float i, prevpos = 0;
114         for(i = 1; i <= RANKINGS_CNT; ++i)
115         {
116                 if(race_readUID(map, i) == myuid)
117                         prevpos = i;
118         }
119         if (prevpos)
120         {
121                 // player improved his existing record, only have to iterate on ranks between new and old recs
122                 for (i = prevpos; i > newpos; --i)
123                 {
124                         db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
125                         db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
126                 }
127         }
128         else
129         {
130                 // player has no ranked record yet
131                 for (i = RANKINGS_CNT; i > newpos; --i)
132                 {
133                         float other_time = race_readTime(map, i - 1);
134                         if (other_time) {
135                                 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(other_time));
136                                 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
137                         }
138                 }
139         }
140
141         // store new time itself
142         db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
143         db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
144 }
145
146 string race_readName(string map, float pos)
147 {
148         string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
149
150         return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
151 }
152
153
154 const float MAX_CHECKPOINTS = 255;
155
156 .float race_penalty;
157 .float race_penalty_accumulator;
158 .string race_penalty_reason;
159 .float race_checkpoint; // player: next checkpoint that has to be reached
160 .entity race_lastpenalty;
161
162 .entity sprite;
163
164 float race_checkpoint_records[MAX_CHECKPOINTS];
165 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
166 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
167 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
168 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
169
170 .float race_checkpoint_record[MAX_CHECKPOINTS];
171
172 float race_highest_checkpoint;
173 float race_timed_checkpoint;
174
175 float defrag_ents;
176 float defragcpexists;
177
178 float race_NextCheckpoint(float f)
179 {
180         if(f >= race_highest_checkpoint)
181                 return 0;
182         else
183                 return f + 1;
184 }
185
186 float race_PreviousCheckpoint(float f)
187 {
188         if(f == -1)
189                 return 0;
190         else if(f == 0)
191                 return race_highest_checkpoint;
192         else
193                 return f - 1;
194 }
195
196 // encode as:
197 //   0 = common start/finish
198 // 254 = start
199 // 255 = finish
200 float race_CheckpointNetworkID(float f)
201 {
202         if(race_timed_checkpoint)
203         {
204                 if(f == 0)
205                         return 254; // start
206                 else if(f == race_timed_checkpoint)
207                         return 255; // finish
208         }
209         return f;
210 }
211
212 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
213 {
214         if(!e.race_laptime)
215                 return;
216
217         int cp = e.race_checkpoint;
218         float recordtime = race_checkpoint_records[cp];
219         float myrecordtime = e.race_checkpoint_record[cp];
220         string recordholder = race_checkpoint_recordholders[cp];
221         if(recordholder == e.netname)
222                 recordholder = "";
223
224         if(!IS_REAL_CLIENT(e))
225                 return;
226
227         if(!spec)
228                 msg_entity = e;
229         WRITESPECTATABLE_MSG_ONE(msg_entity, {
230                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
231                 if(spec)
232                 {
233                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
234                         //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
235                         WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
236                 }
237                 else
238                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
239                 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
240                 WriteInt24_t(MSG_ONE, recordtime);
241                 if(!spec)
242                         WriteInt24_t(MSG_ONE, myrecordtime);
243                 WriteString(MSG_ONE, recordholder);
244         });
245 }
246
247 void race_send_recordtime(float msg)
248 {
249         // send the server best time
250         WriteHeader(msg, TE_CSQC_RACE);
251         WriteByte(msg, RACE_NET_SERVER_RECORD);
252         WriteInt24_t(msg, race_readTime(GetMapname(), 1));
253 }
254
255
256 void race_send_speedaward(float msg)
257 {
258         // send the best speed of the round
259         WriteHeader(msg, TE_CSQC_RACE);
260         WriteByte(msg, RACE_NET_SPEED_AWARD);
261         WriteInt24_t(msg, floor(speedaward_speed+0.5));
262         WriteString(msg, speedaward_holder);
263 }
264
265 void race_send_speedaward_alltimebest(float msg)
266 {
267         // send the best speed
268         WriteHeader(msg, TE_CSQC_RACE);
269         WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
270         WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
271         WriteString(msg, speedaward_alltimebest_holder);
272 }
273
274 void race_send_rankings_cnt(float msg)
275 {
276         WriteHeader(msg, TE_CSQC_RACE);
277         WriteByte(msg, RACE_NET_RANKINGS_CNT);
278         int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
279         WriteByte(msg, m);
280 }
281
282 void race_SendRankings(float pos, float prevpos, float del, float msg)
283 {
284         WriteHeader(msg, TE_CSQC_RACE);
285         WriteByte(msg, RACE_NET_SERVER_RANKINGS);
286         WriteShort(msg, pos);
287         WriteShort(msg, prevpos);
288         WriteShort(msg, del);
289         WriteString(msg, race_readName(GetMapname(), pos));
290         WriteInt24_t(msg, race_readTime(GetMapname(), pos));
291 }
292
293 void race_SendStatus(float id, entity e)
294 {
295         if(!IS_REAL_CLIENT(e))
296                 return;
297
298         float msg;
299         if (id == 0)
300                 msg = MSG_ONE;
301         else
302                 msg = MSG_ALL;
303         msg_entity = e;
304         WRITESPECTATABLE_MSG_ONE(msg_entity, {
305                 WriteHeader(msg, TE_CSQC_RACE);
306                 WriteByte(msg, RACE_NET_SERVER_STATUS);
307                 WriteShort(msg, id);
308                 WriteString(msg, e.netname);
309         });
310 }
311
312 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
313 {
314         // netname only used TEMPORARILY for printing
315         int newpos = race_readPos(map, t);
316
317         int player_prevpos = 0;
318         for(int i = 1; i <= RANKINGS_CNT; ++i)
319         {
320                 if(race_readUID(map, i) == myuid)
321                         player_prevpos = i;
322         }
323
324         float oldrec;
325         string oldrec_holder;
326         if (player_prevpos && (player_prevpos < newpos || !newpos))
327         {
328                 oldrec = race_readTime(GetMapname(), player_prevpos);
329                 race_SendStatus(0, e); // "fail"
330                 if(showmessage)
331                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
332                 return;
333         }
334         else if (!newpos)
335         {
336                 // no ranking, time worse than the worst ranked
337                 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
338                 race_SendStatus(0, e); // "fail"
339                 if(showmessage)
340                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
341                 return;
342         }
343
344         // if we didn't hit a return yet, we have a new record!
345
346         // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
347         if(myuid == "")
348         {
349                 if(showmessage)
350                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
351                 return;
352         }
353
354         if(uid2name(myuid) == "^1Unregistered Player")
355         {
356                 if(showmessage)
357                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
358                 return;
359         }
360
361         oldrec = race_readTime(GetMapname(), newpos);
362         oldrec_holder = race_readName(GetMapname(), newpos);
363
364         // store new ranking
365         race_writeTime(GetMapname(), t, myuid);
366
367         if (newpos == 1 && showmessage)
368         {
369                 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
370                 race_send_recordtime(MSG_ALL);
371         }
372
373         race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
374         strcpy(rankings_reply, getrankings());
375
376         if(newpos == player_prevpos)
377         {
378                 if(showmessage)
379                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
380                 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
381                 else { race_SendStatus(1, e); } // "new time"
382         }
383         else if(oldrec == 0)
384         {
385                 if(showmessage)
386                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
387                 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
388                 else { race_SendStatus(2, e); } // "new rank"
389         }
390         else
391         {
392                 if(showmessage)
393                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
394                 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
395                 else { race_SendStatus(2, e); } // "new rank"
396         }
397 }
398
399 void race_deleteTime(string map, float pos)
400 {
401         string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
402
403         for(int i = pos; i <= RANKINGS_CNT; ++i)
404         {
405                 string therank = ftos(i);
406                 if (i == RANKINGS_CNT)
407                 {
408                         db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
409                         db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
410                 }
411                 else
412                 {
413                         db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
414                         db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
415                 }
416         }
417
418         race_SendRankings(pos, 0, 1, MSG_ALL);
419         if(pos == 1)
420                 race_send_recordtime(MSG_ALL);
421
422         strcpy(rankings_reply, getrankings());
423 }
424
425 void race_SendTime(entity e, float cp, float t, float tvalid)
426 {
427         float snew, l;
428
429         if(g_race_qualifying)
430                 t += e.race_penalty_accumulator;
431
432         t = TIME_ENCODE(t); // make integer
433
434         if(tvalid)
435         if(cp == race_timed_checkpoint) // finish line
436         if (!CS(e).race_completed)
437         {
438                 float s;
439                 if(g_race_qualifying)
440                 {
441                         s = GameRules_scoring_add(e, RACE_FASTEST, 0);
442                         if(!s || t < s)
443                                 GameRules_scoring_add(e, RACE_FASTEST, t - s);
444                 }
445                 else
446                 {
447                         s = GameRules_scoring_add(e, RACE_FASTEST, 0);
448                         if(!s || t < s)
449                                 GameRules_scoring_add(e, RACE_FASTEST, t - s);
450
451                         s = GameRules_scoring_add(e, RACE_TIME, 0);
452                         snew = TIME_ENCODE(time - game_starttime);
453                         GameRules_scoring_add(e, RACE_TIME, snew - s);
454                         l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
455
456                         if(autocvar_fraglimit)
457                                 if(l >= autocvar_fraglimit)
458                                         race_StartCompleting();
459
460                         if(race_completing)
461                         {
462                                 CS(e).race_completed = 1;
463                                 MAKE_INDEPENDENT_PLAYER(e);
464                                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
465                                 ClientData_Touch(e);
466                         }
467                 }
468         }
469
470         if(g_race_qualifying)
471         {
472                 float recordtime;
473                 string recordholder;
474
475                 if(tvalid)
476                 {
477                         recordtime = race_checkpoint_records[cp];
478                         float myrecordtime = e.race_checkpoint_record[cp];
479                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
480                         if(recordholder == e.netname)
481                                 recordholder = "";
482
483                         if(t != 0)
484                         {
485                                 if(cp == race_timed_checkpoint)
486                                 {
487                                         race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
488                                         MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
489                                 }
490                                 if(t < myrecordtime || myrecordtime == 0)
491                                         e.race_checkpoint_record[cp] = t; // resending done below
492
493                                 if(t < recordtime || recordtime == 0)
494                                 {
495                                         race_checkpoint_records[cp] = t;
496                                         strcpy(race_checkpoint_recordholders[cp], e.netname);
497                                         if(g_race_qualifying)
498                                                 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
499                                 }
500
501                         }
502                 }
503                 else
504                 {
505                         // dummies
506                         t = 0;
507                         recordtime = 0;
508                         recordholder = "";
509                 }
510
511                 if(IS_REAL_CLIENT(e))
512                 {
513                         if(g_race_qualifying)
514                         {
515                                 FOREACH_CLIENT(IS_REAL_CLIENT(it),
516                                 {
517                                         if(it == e || (IS_SPEC(it) && it.enemy == e))
518                                         {
519                                                 msg_entity = it;
520                                                 WriteHeader(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                                                 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
526                                                 WriteString(MSG_ONE, recordholder); // record holder
527                                         }
528                                 });
529                         }
530                 }
531         }
532         else // RACE! Not Qualifying
533         {
534                 float mylaps, lother, othtime;
535                 entity oth = race_checkpoint_lastplayers[cp];
536                 if(oth)
537                 {
538                         mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
539                         lother = race_checkpoint_lastlaps[cp];
540                         othtime = race_checkpoint_lasttimes[cp];
541                 }
542                 else
543                         mylaps = lother = othtime = 0;
544
545                 if(IS_REAL_CLIENT(e))
546                 {
547                         msg_entity = e;
548                         WRITESPECTATABLE_MSG_ONE(msg_entity, {
549                                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
550                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
551                                 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
552                                 if(e == oth)
553                                 {
554                                         WriteInt24_t(MSG_ONE, 0);
555                                         WriteByte(MSG_ONE, 0);
556                                         WriteByte(MSG_ONE, 0);
557                                 }
558                                 else
559                                 {
560                                         WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
561                                         WriteByte(MSG_ONE, mylaps - lother);
562                                         WriteByte(MSG_ONE, etof(oth)); // record holder
563                                 }
564                         });
565                 }
566
567                 race_checkpoint_lastplayers[cp] = e;
568                 race_checkpoint_lasttimes[cp] = time;
569                 race_checkpoint_lastlaps[cp] = mylaps;
570
571                 if(IS_REAL_CLIENT(oth))
572                 {
573                         msg_entity = oth;
574                         WRITESPECTATABLE_MSG_ONE(msg_entity, {
575                                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
576                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
577                                 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
578                                 if(e == oth)
579                                 {
580                                         WriteInt24_t(MSG_ONE, 0);
581                                         WriteByte(MSG_ONE, 0);
582                                         WriteByte(MSG_ONE, 0);
583                                 }
584                                 else
585                                 {
586                                         WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
587                                         WriteByte(MSG_ONE, lother - mylaps);
588                                         WriteByte(MSG_ONE, etof(e) - 1); // record holder
589                                 }
590                         });
591                 }
592         }
593 }
594
595 void race_ClearTime(entity e)
596 {
597         e.race_checkpoint = 0;
598         e.race_laptime = 0;
599         e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
600         e.race_penalty_accumulator = 0;
601         e.race_lastpenalty = NULL;
602
603         if(!IS_REAL_CLIENT(e))
604                 return;
605
606         msg_entity = e;
607         WRITESPECTATABLE_MSG_ONE(msg_entity, {
608                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
609                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
610         });
611 }
612
613 void checkpoint_passed(entity this, entity player)
614 {
615         if(IS_VEHICLE(player) && player.owner)
616                 player = player.owner;
617
618         if(player.personal && autocvar_g_allow_checkpoints)
619                 return; // practice mode!
620
621         if(player.classname == "porto")
622         {
623                 // do not allow portalling through checkpoints
624                 trace_plane_normal = normalize(-1 * player.velocity);
625                 W_Porto_Fail(player, 0);
626                 return;
627         }
628
629         string oldmsg; // used twice
630
631         /*
632          * Trigger targets
633          */
634         if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
635         {
636                 oldmsg = this.message;
637                 this.message = "";
638                 SUB_UseTargets(this, player, player);
639                 this.message = oldmsg;
640         }
641
642         if (!IS_PLAYER(player))
643                 return;
644
645         /*
646          * Remove unauthorized equipment
647          */
648         Portal_ClearAll(player);
649
650         player.porto_forbidden = 2; // decreased by 1 each StartFrame
651
652         if(defrag_ents)
653         {
654                 if(this.race_checkpoint == -2)
655                 {
656                         this.race_checkpoint = player.race_checkpoint;
657                 }
658
659                 int cp_amount = 0, largest_cp_id = 0;
660                 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
661                 {
662                         cp_amount += 1;
663                         if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
664                         {
665                                 if(!largest_cp_id)
666                                 {
667                                         IL_EACH(g_race_targets, it.classname == "target_checkpoint",
668                                         {
669                                                 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
670                                                         defragcpexists = -1;
671                                         });
672                                 }
673
674                                 largest_cp_id = it.race_checkpoint;
675                                 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
676                                 {
677                                         it.race_checkpoint = largest_cp_id + 1; // finish line
678                                 });
679                                 race_highest_checkpoint = largest_cp_id + 1;
680                                 race_timed_checkpoint = largest_cp_id + 1;
681                         }
682                 });
683
684                 if(!cp_amount)
685                 {
686                         IL_EACH(g_race_targets, it.classname == "target_stopTimer",
687                         {
688                                 it.race_checkpoint = 1;
689                         });
690                         race_highest_checkpoint = 1;
691                         race_timed_checkpoint = 1;
692                 }
693         }
694
695         if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
696         {
697                 if(this.race_penalty)
698                 {
699                         if(player.race_lastpenalty != this)
700                         {
701                                 player.race_lastpenalty = this;
702                                 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
703                         }
704                 }
705
706                 if(player.race_penalty)
707                         return;
708
709                 /*
710                  * Trigger targets
711                  */
712                 if(this.spawnflags & 2)
713                 {
714                         oldmsg = this.message;
715                         this.message = "";
716                         SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
717                         this.message = oldmsg;
718                 }
719
720                 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
721                         player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
722                 player.race_respawn_checkpoint = this.race_checkpoint;
723                 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
724                 player.race_started = 1;
725
726                 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
727
728                 if(!this.race_checkpoint) // start line
729                 {
730                         player.race_laptime = time;
731                         player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
732                         player.race_penalty_accumulator = 0;
733                         player.race_lastpenalty = NULL;
734                 }
735
736                 if(g_race_qualifying)
737                         race_SendNextCheckpoint(player, 0);
738
739                 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
740                 {
741                         float fh;
742                         defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
743                         if(fh >= 0)
744                         {
745                                 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
746                                 {
747                                         fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
748                                 });
749                         }
750                         fclose(fh);
751                 }
752         }
753         else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
754         {
755                 // ignored
756         }
757         else
758         {
759                 if(this.spawnflags & 4)
760                         Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
761         }
762 }
763
764 void checkpoint_touch(entity this, entity toucher)
765 {
766         EXACTTRIGGER_TOUCH(this, toucher);
767         checkpoint_passed(this, toucher);
768 }
769
770 void checkpoint_use(entity this, entity actor, entity trigger)
771 {
772         if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
773                 return;
774
775         checkpoint_passed(this, actor);
776 }
777
778 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
779 {
780         entity own = this.owner;
781         if(this.realowner)
782                 own = this.realowner; // target support
783
784         if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
785                 return true;
786         else if(view.race_checkpoint == own.race_checkpoint)
787                 return true;
788         else
789                 return false;
790 }
791
792 void trigger_race_checkpoint_verify(entity this)
793 {
794     static bool have_verified;
795         if (have_verified) return;
796         have_verified = true;
797
798         bool qual = g_race_qualifying;
799
800         int pl_race_checkpoint = 0;
801         int pl_race_place = 0;
802
803         if (g_race) {
804                 for (int i = 0; i <= race_highest_checkpoint; ++i) {
805                         pl_race_checkpoint = race_NextCheckpoint(i);
806
807                         // race only (middle of the race)
808                         g_race_qualifying = false;
809                         pl_race_place = 0;
810                         if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
811                                 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
812             }
813
814                         if (i == 0) {
815                                 // qualifying only
816                                 g_race_qualifying = 1;
817                                 pl_race_place = race_lowest_place_spawn;
818                                 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
819                                         error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
820                 }
821
822                                 // race only (initial spawn)
823                                 g_race_qualifying = 0;
824                                 for (int p = 1; p <= race_highest_place_spawn; ++p) {
825                                         pl_race_place = p;
826                                         if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
827                                                 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
828                     }
829                                 }
830                         }
831                 }
832         } else if (!defrag_ents) {
833                 // qualifying only
834                 pl_race_checkpoint = race_NextCheckpoint(0);
835                 g_race_qualifying = 1;
836                 pl_race_place = race_lowest_place_spawn;
837                 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
838                         error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
839         }
840         } else {
841                 pl_race_checkpoint = race_NextCheckpoint(0);
842                 g_race_qualifying = 1;
843                 pl_race_place = 0; // there's only one spawn on defrag maps
844
845                 // check if a defragcp file already exists, then read it and apply the checkpoint order
846                 float fh;
847                 float len;
848                 string l;
849
850                 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
851                 if (fh >= 0) {
852                         while ((l = fgets(fh))) {
853                                 len = tokenize_console(l);
854                                 if (len != 2) {
855                                         defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
856                                         continue;
857                                 }
858                                 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
859                                         if (argv(0) == cp.targetname) {
860                                                 cp.race_checkpoint = stof(argv(1));
861                     }
862                 }
863                         }
864                         fclose(fh);
865                 }
866         }
867
868         g_race_qualifying = qual;
869
870         IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
871         {
872                 if(it.targetname == "" || !it.targetname) // somehow this is a case...
873                         continue;
874                 entity cpt = it;
875                 FOREACH_ENTITY_STRING(target, cpt.targetname,
876                 {
877                         vector org = (it.absmin + it.absmax) * 0.5;
878                         if(cpt.race_checkpoint == 0)
879                                 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
880                         else
881                                 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
882
883                         it.sprite.realowner = cpt;
884                         it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
885                 });
886         });
887
888         if (race_timed_checkpoint) {
889                 if (defrag_ents) {
890                         IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
891                         {
892                                 entity cpt = it;
893                                 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
894                                         if(it.targetname == "" || !it.targetname) // somehow this is a case...
895                                                 continue;
896                                         FOREACH_ENTITY_STRING(target, cpt.targetname, {
897                                                 if(it.sprite)
898                                                         WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
899                                         });
900                                 }
901                                 if(it.classname == "target_checkpoint") {
902                                         if(it.race_checkpoint == -2)
903                                                 defragcpexists = -1; // 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
904                                 }
905                         });
906                         if (defragcpexists != -1) {
907                                 float largest_cp_id = 0;
908                                 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
909                                         if (cp.race_checkpoint > largest_cp_id) {
910                                                 largest_cp_id = cp.race_checkpoint;
911                     }
912                 }
913                                 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
914                                         cp.race_checkpoint = largest_cp_id + 1; // finish line
915                 }
916                                 race_highest_checkpoint = largest_cp_id + 1;
917                                 race_timed_checkpoint = largest_cp_id + 1;
918                         } else {
919                                 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
920                                         cp.race_checkpoint = 255; // finish line
921                 }
922                                 race_highest_checkpoint = 255;
923                                 race_timed_checkpoint = 255;
924                         }
925                 } else {
926                         IL_EACH(g_racecheckpoints, it.sprite,
927                         {
928                                 if (it.race_checkpoint == 0) {
929                                         WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
930                 } else if (it.race_checkpoint == race_timed_checkpoint) {
931                                         WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
932                                 }
933             });
934                 }
935         }
936
937         if (defrag_ents) {
938                 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
939                         for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
940                                 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
941                                         trigger.wait = 0;
942                                         trigger.delay = 0;
943                                         targ.wait = 0;
944                                         targ.delay = 0;
945
946                     // These just make the game crash on some maps with oddly shaped triggers.
947                     // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
948                     // and often one of them just passed through without being registered. Hope it's fixed  in a better way now.
949                     // (happened on item triggers too)
950                     //
951                                         //targ.wait = -2;
952                                         //targ.delay = 0;
953
954                                         //setsize(targ, trigger.mins, trigger.maxs);
955                                         //setorigin(targ, trigger.origin);
956                                         //remove(trigger);
957                                 }
958             }
959         }
960         }
961 }
962
963 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
964 {
965         if(g_race_qualifying)
966         {
967                 // spawn at first
968                 if(this.race_checkpoint != 0)
969                         return '-1 0 0';
970                 if(spot.race_place != race_lowest_place_spawn)
971                         return '-1 0 0';
972         }
973         else
974         {
975                 if(this.race_checkpoint != player.race_respawn_checkpoint)
976                         return '-1 0 0';
977                 // try reusing the previous spawn
978                 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
979                         current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
980                 if(this.race_checkpoint == 0)
981                 {
982                         int pl = player.race_place;
983                         if(pl > race_highest_place_spawn)
984                                 pl = 0;
985                         if(pl == 0 && !player.race_started)
986                                 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
987                         if(spot.race_place != pl)
988                                 return '-1 0 0';
989                 }
990         }
991         return current;
992 }
993
994 spawnfunc(trigger_race_checkpoint)
995 {
996         vector o;
997         if(!g_race && !g_cts) { delete(this); return; }
998
999         EXACTTRIGGER_INIT;
1000
1001         this.use = checkpoint_use;
1002         if (!(this.spawnflags & 1))
1003                 settouch(this, checkpoint_touch);
1004
1005         o = (this.absmin + this.absmax) * 0.5;
1006         tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
1007         waypoint_spawnforitem_force(this, trace_endpos);
1008         this.nearestwaypointtimeout = -1;
1009
1010         if(this.message == "")
1011                 this.message = "went backwards";
1012         if (this.message2 == "")
1013                 this.message2 = "was pushed backwards by";
1014         if (this.race_penalty_reason == "")
1015                 this.race_penalty_reason = "missing a checkpoint";
1016
1017         this.race_checkpoint = this.cnt;
1018
1019         if(this.race_checkpoint > race_highest_checkpoint)
1020         {
1021                 race_highest_checkpoint = this.race_checkpoint;
1022                 if(this.spawnflags & 8)
1023                         race_timed_checkpoint = this.race_checkpoint;
1024                 else
1025                         race_timed_checkpoint = 0;
1026         }
1027
1028         if(!this.race_penalty)
1029         {
1030                 if(this.race_checkpoint)
1031                         WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
1032                 else
1033                         WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
1034         }
1035
1036         this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1037         this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1038
1039         IL_PUSH(g_racecheckpoints, this);
1040
1041         InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1042 }
1043
1044 spawnfunc(target_checkpoint) // defrag entity
1045 {
1046         if(!g_race && !g_cts) { delete(this); return; }
1047         defrag_ents = 1;
1048
1049         // if this is targeted, then it probably isn't a trigger
1050         bool is_trigger = this.targetname == "";
1051
1052         if(is_trigger)
1053                 EXACTTRIGGER_INIT;
1054
1055         this.use = checkpoint_use;
1056         if (is_trigger && !(this.spawnflags & 1))
1057                 settouch(this, checkpoint_touch);
1058
1059         vector org = this.origin;
1060
1061         // bots should only pathfind to this if it is a valid touchable trigger
1062         if(is_trigger)
1063         {
1064                 org = (this.absmin + this.absmax) * 0.5;
1065                 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1066                 waypoint_spawnforitem_force(this, trace_endpos);
1067                 this.nearestwaypointtimeout = -1;
1068         }
1069
1070         if(this.message == "")
1071                 this.message = "went backwards";
1072         if (this.message2 == "")
1073                 this.message2 = "was pushed backwards by";
1074         if (this.race_penalty_reason == "")
1075                 this.race_penalty_reason = "missing a checkpoint";
1076
1077         if(this.classname == "target_startTimer")
1078                 this.race_checkpoint = 0;
1079         else
1080                 this.race_checkpoint = -2;
1081
1082         race_timed_checkpoint = 1;
1083
1084         IL_PUSH(g_race_targets, this);
1085
1086         InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1087 }
1088
1089 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1090 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1091
1092 void race_AbandonRaceCheck(entity p)
1093 {
1094         if(race_completing && !CS(p).race_completed)
1095         {
1096                 CS(p).race_completed = 1;
1097                 MAKE_INDEPENDENT_PLAYER(p);
1098                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1099                 ClientData_Touch(p);
1100         }
1101 }
1102
1103 void race_StartCompleting()
1104 {
1105         race_completing = 1;
1106         FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1107 }
1108
1109 void race_PreparePlayer(entity this)
1110 {
1111         race_ClearTime(this);
1112         this.race_place = 0;
1113         this.race_started = 0;
1114         this.race_respawn_checkpoint = 0;
1115         this.race_respawn_spotref = NULL;
1116 }
1117
1118 void race_RetractPlayer(entity this)
1119 {
1120         if(!g_race && !g_cts)
1121                 return;
1122         if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1123                 race_ClearTime(this);
1124         this.race_checkpoint = this.race_respawn_checkpoint;
1125 }
1126
1127 spawnfunc(info_player_race)
1128 {
1129         if(!g_race && !g_cts) { delete(this); return; }
1130         ++race_spawns;
1131         spawnfunc_info_player_deathmatch(this);
1132
1133         if(this.race_place > race_highest_place_spawn)
1134                 race_highest_place_spawn = this.race_place;
1135         if(this.race_place < race_lowest_place_spawn)
1136                 race_lowest_place_spawn = this.race_place;
1137 }
1138
1139 void race_ClearRecords()
1140 {
1141         for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1142         {
1143                 race_checkpoint_records[j] = 0;
1144                 strfree(race_checkpoint_recordholders[j]);
1145         }
1146
1147         FOREACH_CLIENT(true, {
1148                 float p = it.race_place;
1149                 race_PreparePlayer(it);
1150                 it.race_place = p;
1151         });
1152 }
1153
1154 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1155 {
1156         if(g_race_qualifying)
1157         {
1158                 pl.race_penalty_accumulator += penalty;
1159                 if(IS_REAL_CLIENT(pl))
1160                 {
1161                         msg_entity = pl;
1162                         WRITESPECTATABLE_MSG_ONE(msg_entity, {
1163                                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1164                                 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1165                                 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1166                                 WriteString(MSG_ONE, reason);
1167                         });
1168                 }
1169         }
1170         else
1171         {
1172                 pl.race_penalty = time + penalty;
1173                 if(IS_REAL_CLIENT(pl))
1174                 {
1175                         msg_entity = pl;
1176                         WRITESPECTATABLE_MSG_ONE(msg_entity, {
1177                                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1178                                 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1179                                 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1180                                 WriteString(MSG_ONE, reason);
1181                         });
1182                 }
1183         }
1184 }
1185
1186 void penalty_touch(entity this, entity toucher)
1187 {
1188         EXACTTRIGGER_TOUCH(this, toucher);
1189         if(toucher.race_lastpenalty != this)
1190         {
1191                 toucher.race_lastpenalty = this;
1192                 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1193         }
1194 }
1195
1196 void penalty_use(entity this, entity actor, entity trigger)
1197 {
1198         race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1199 }
1200
1201 spawnfunc(trigger_race_penalty)
1202 {
1203         // TODO: find out why this wasnt done:
1204         //if(!g_cts && !g_race) { remove(this); return; }
1205
1206         EXACTTRIGGER_INIT;
1207
1208         this.use = penalty_use;
1209         if (!(this.spawnflags & 1))
1210                 settouch(this, penalty_touch);
1211
1212         if (this.race_penalty_reason == "")
1213                 this.race_penalty_reason = "missing a checkpoint";
1214         if (!this.race_penalty)
1215                 this.race_penalty = 5;
1216 }
1217
1218 float race_GetFractionalLapCount(entity e)
1219 {
1220         // interesting metrics (idea by KrimZon) to maybe sort players in the
1221         // scoreboard, immediately updates when overtaking
1222         //
1223         // requires the track to be built so you never get farther away from the
1224         // next checkpoint, though, and current Xonotic race maps are not built that
1225         // way
1226         //
1227         // also, this code is slow and would need optimization (i.e. "next CP"
1228         // links on CP entities)
1229
1230         float l;
1231         l = GameRules_scoring_add(e, RACE_LAPS, 0);
1232         if(CS(e).race_completed)
1233                 return l; // not fractional
1234
1235         vector o0, o1;
1236         float bestfraction, fraction;
1237         entity lastcp;
1238         float nextcpindex, lastcpindex;
1239
1240         nextcpindex = max(e.race_checkpoint, 0);
1241         lastcpindex = e.race_respawn_checkpoint;
1242         lastcp = e.race_respawn_spotref;
1243
1244         if(nextcpindex == lastcpindex)
1245                 return l; // finish
1246
1247         bestfraction = 1;
1248         IL_EACH(g_racecheckpoints, true,
1249         {
1250                 if(it.race_checkpoint != lastcpindex)
1251                         continue;
1252                 if(lastcp)
1253                         if(it != lastcp)
1254                                 continue;
1255                 o0 = (it.absmin + it.absmax) * 0.5;
1256                 IL_EACH(g_racecheckpoints, true,
1257                 {
1258                         if(it.race_checkpoint != nextcpindex)
1259                                 continue;
1260                         o1 = (it.absmin + it.absmax) * 0.5;
1261                         if(o0 == o1)
1262                                 continue;
1263                         fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1264                         if(fraction < bestfraction)
1265                                 bestfraction = fraction;
1266                 });
1267         });
1268
1269         // we are at CP "nextcpindex - bestfraction"
1270         // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1271         // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1272         float c, nc;
1273         nc = race_highest_checkpoint + 1;
1274         c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;
1275
1276         return l + c / nc;
1277 }