]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/race.qc
Record player race move time in PlayerPreThink so it can be recorded while inside...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / race.qc
1 #include "race.qh"
2
3 #include <common/deathtypes/all.qh>
4 #include <common/gamemodes/_mod.qh>
5 #include <common/gamemodes/rules.qh>
6 #include <common/mapobjects/subs.qh>
7 #include <common/mapobjects/triggers.qh>
8 #include <common/mutators/mutator/waypoints/waypointsprites.qh>
9 #include <common/net_linked.qh>
10 #include <common/notifications/all.qh>
11 #include <common/state.qh>
12 #include <common/stats.qh>
13 #include <common/vehicles/sv_vehicles.qh>
14 #include <common/weapons/_all.qh>
15 #include <common/weapons/weapon/porto.qh>
16 #include <lib/warpzone/common.qh>
17 #include <lib/warpzone/util_server.qh>
18 #include <server/bot/api.qh>
19 #include <server/cheats.qh>
20 #include <server/client.qh>
21 #include <server/command/getreplies.qh>
22 #include <server/damage.qh>
23 #include <server/gamelog.qh>
24 #include <server/intermission.qh>
25 #include <server/main.qh>
26 #include <server/mutators/_mod.qh>
27 #include <server/portals.qh>
28 #include <server/scores.qh>
29 #include <server/spawnpoints.qh>
30 #include <server/weapons/common.qh>
31 #include <server/world.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         if(g_race_qualifying)
428                 t += e.race_penalty_accumulator;
429
430         t = TIME_ENCODE(t); // make integer
431
432         if(tvalid)
433         if(cp == race_timed_checkpoint) // finish line
434         if (!CS(e).race_completed)
435         {
436                 float s;
437                 if(g_race_qualifying)
438                 {
439                         s = GameRules_scoring_add(e, RACE_FASTEST, 0);
440                         if(!s || t < s)
441                                 GameRules_scoring_add(e, RACE_FASTEST, t - s);
442                 }
443                 else
444                 {
445                         s = GameRules_scoring_add(e, RACE_FASTEST, 0);
446                         if(!s || t < s)
447                                 GameRules_scoring_add(e, RACE_FASTEST, t - s);
448
449                         s = GameRules_scoring_add(e, RACE_TIME, 0);
450                         float snew = TIME_ENCODE(time - game_starttime);
451                         GameRules_scoring_add(e, RACE_TIME, snew - s);
452                         float l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
453
454                         if(autocvar_fraglimit)
455                                 if(l >= autocvar_fraglimit)
456                                         race_StartCompleting();
457
458                         if(race_completing)
459                         {
460                                 CS(e).race_completed = 1;
461                                 MAKE_INDEPENDENT_PLAYER(e);
462                                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
463                                 ClientData_Touch(e);
464                         }
465                 }
466         }
467
468         if(g_race_qualifying)
469         {
470                 float recordtime;
471                 string recordholder;
472
473                 if(tvalid)
474                 {
475                         recordtime = race_checkpoint_records[cp];
476                         float myrecordtime = e.race_checkpoint_record[cp];
477                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
478                         if(recordholder == e.netname)
479                                 recordholder = "";
480
481                         if(t != 0)
482                         {
483                                 if(cp == race_timed_checkpoint)
484                                 {
485                                         race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
486                                         MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
487                                 }
488                                 if(t < myrecordtime || myrecordtime == 0)
489                                         e.race_checkpoint_record[cp] = t; // resending done below
490
491                                 if(t < recordtime || recordtime == 0)
492                                 {
493                                         race_checkpoint_records[cp] = t;
494                                         strcpy(race_checkpoint_recordholders[cp], e.netname);
495                                         if(g_race_qualifying)
496                                                 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
497                                 }
498
499                         }
500                 }
501                 else
502                 {
503                         // dummies
504                         t = 0;
505                         recordtime = 0;
506                         recordholder = "";
507                 }
508
509                 if(IS_REAL_CLIENT(e))
510                 {
511                         if(g_race_qualifying)
512                         {
513                                 FOREACH_CLIENT(IS_REAL_CLIENT(it),
514                                 {
515                                         if(it == e || (IS_SPEC(it) && it.enemy == e))
516                                         {
517                                                 msg_entity = it;
518                                                 WriteHeader(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                                                 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
524                                                 WriteString(MSG_ONE, recordholder); // record holder
525                                         }
526                                 });
527                         }
528                 }
529         }
530         else // RACE! Not Qualifying
531         {
532                 float mylaps, lother, othtime;
533                 entity oth = race_checkpoint_lastplayers[cp];
534                 if(oth)
535                 {
536                         mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
537                         lother = race_checkpoint_lastlaps[cp];
538                         othtime = race_checkpoint_lasttimes[cp];
539                 }
540                 else
541                         mylaps = lother = othtime = 0;
542
543                 if(IS_REAL_CLIENT(e))
544                 {
545                         msg_entity = e;
546                         WRITESPECTATABLE_MSG_ONE(msg_entity, {
547                                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
548                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
549                                 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
550                                 if(e == oth)
551                                 {
552                                         WriteInt24_t(MSG_ONE, 0);
553                                         WriteByte(MSG_ONE, 0);
554                                         WriteByte(MSG_ONE, 0);
555                                 }
556                                 else
557                                 {
558                                         WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
559                                         WriteByte(MSG_ONE, mylaps - lother);
560                                         WriteByte(MSG_ONE, etof(oth)); // record holder
561                                 }
562                         });
563                 }
564
565                 race_checkpoint_lastplayers[cp] = e;
566                 race_checkpoint_lasttimes[cp] = time;
567                 race_checkpoint_lastlaps[cp] = mylaps;
568
569                 if(IS_REAL_CLIENT(oth))
570                 {
571                         msg_entity = oth;
572                         WRITESPECTATABLE_MSG_ONE(msg_entity, {
573                                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
574                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
575                                 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
576                                 if(e == oth)
577                                 {
578                                         WriteInt24_t(MSG_ONE, 0);
579                                         WriteByte(MSG_ONE, 0);
580                                         WriteByte(MSG_ONE, 0);
581                                 }
582                                 else
583                                 {
584                                         WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
585                                         WriteByte(MSG_ONE, lother - mylaps);
586                                         WriteByte(MSG_ONE, etof(e) - 1); // record holder
587                                 }
588                         });
589                 }
590         }
591 }
592
593 void race_ClearTime(entity e)
594 {
595         e.race_checkpoint = 0;
596         e.race_laptime = 0;
597         e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
598         e.race_penalty_accumulator = 0;
599         e.race_lastpenalty = NULL;
600
601         if(!IS_REAL_CLIENT(e))
602                 return;
603
604         msg_entity = e;
605         WRITESPECTATABLE_MSG_ONE(msg_entity, {
606                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
607                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
608         });
609 }
610
611 void checkpoint_passed(entity this, entity player)
612 {
613         if(IS_VEHICLE(player) && player.owner)
614                 player = player.owner;
615
616         if(player.personal && autocvar_g_allow_checkpoints)
617                 return; // practice mode!
618
619         if(player.classname == "porto")
620         {
621                 // do not allow portalling through checkpoints
622                 trace_plane_normal = normalize(-1 * player.velocity);
623                 W_Porto_Fail(player, 0);
624                 return;
625         }
626
627         string oldmsg; // used twice
628
629         /*
630          * Trigger targets
631          */
632         if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
633         {
634                 oldmsg = this.message;
635                 this.message = "";
636                 SUB_UseTargets(this, player, player);
637                 this.message = oldmsg;
638         }
639
640         if (!IS_PLAYER(player))
641                 return;
642
643         /*
644          * Remove unauthorized equipment
645          */
646         Portal_ClearAll(player);
647
648         player.porto_forbidden = 2; // decreased by 1 each StartFrame
649
650         if(defrag_ents)
651         {
652                 if(this.race_checkpoint == -2)
653                 {
654                         this.race_checkpoint = player.race_checkpoint;
655                 }
656
657                 int cp_amount = 0, largest_cp_id = 0;
658                 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
659                 {
660                         cp_amount += 1;
661                         if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
662                         {
663                                 if(!largest_cp_id)
664                                 {
665                                         IL_EACH(g_race_targets, it.classname == "target_checkpoint",
666                                         {
667                                                 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
668                                                         defragcpexists = -1;
669                                         });
670                                 }
671
672                                 largest_cp_id = it.race_checkpoint;
673                                 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
674                                 {
675                                         it.race_checkpoint = largest_cp_id + 1; // finish line
676                                 });
677                                 race_highest_checkpoint = largest_cp_id + 1;
678                                 race_timed_checkpoint = largest_cp_id + 1;
679                         }
680                 });
681
682                 if(!cp_amount)
683                 {
684                         IL_EACH(g_race_targets, it.classname == "target_stopTimer",
685                         {
686                                 it.race_checkpoint = 1;
687                         });
688                         race_highest_checkpoint = 1;
689                         race_timed_checkpoint = 1;
690                 }
691         }
692
693         if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
694         {
695                 if(this.race_penalty)
696                 {
697                         if(player.race_lastpenalty != this)
698                         {
699                                 player.race_lastpenalty = this;
700                                 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
701                         }
702                 }
703
704                 if(player.race_penalty)
705                         return;
706
707                 /*
708                  * Trigger targets
709                  */
710                 if(this.spawnflags & 2)
711                 {
712                         oldmsg = this.message;
713                         this.message = "";
714                         SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
715                         this.message = oldmsg;
716                 }
717
718                 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
719                         player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
720                 player.race_respawn_checkpoint = this.race_checkpoint;
721                 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
722                 player.race_started = 1;
723
724                 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
725
726                 if(!this.race_checkpoint) // start line
727                 {
728                         player.race_laptime = time;
729                         player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
730                         player.race_penalty_accumulator = 0;
731                         player.race_lastpenalty = NULL;
732                 }
733
734                 if(g_race_qualifying)
735                         race_SendNextCheckpoint(player, 0);
736
737                 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
738                 {
739                         float fh;
740                         defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
741                         if(fh >= 0)
742                         {
743                                 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
744                                 {
745                                         fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
746                                 });
747                         }
748                         fclose(fh);
749                 }
750         }
751         else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
752         {
753                 // ignored
754         }
755         else
756         {
757                 if(this.spawnflags & 4)
758                         Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
759         }
760 }
761
762 void checkpoint_touch(entity this, entity toucher)
763 {
764         EXACTTRIGGER_TOUCH(this, toucher);
765         checkpoint_passed(this, toucher);
766 }
767
768 void checkpoint_use(entity this, entity actor, entity trigger)
769 {
770         if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
771                 return;
772
773         checkpoint_passed(this, actor);
774 }
775
776 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
777 {
778         entity own = this.owner;
779         if(this.realowner)
780                 own = this.realowner; // target support
781
782         if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
783                 return true;
784         else if(view.race_checkpoint == own.race_checkpoint)
785                 return true;
786         else
787                 return false;
788 }
789
790 void trigger_race_checkpoint_verify(entity this)
791 {
792     static bool have_verified;
793         if (have_verified) return;
794         have_verified = true;
795
796         bool qual = g_race_qualifying;
797
798         int pl_race_checkpoint = 0;
799         int pl_race_place = 0;
800
801         if (g_race) {
802                 for (int i = 0; i <= race_highest_checkpoint; ++i) {
803                         pl_race_checkpoint = race_NextCheckpoint(i);
804
805                         // race only (middle of the race)
806                         g_race_qualifying = false;
807                         pl_race_place = 0;
808                         if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
809                                 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
810             }
811
812                         if (i == 0) {
813                                 // qualifying only
814                                 g_race_qualifying = 1;
815                                 pl_race_place = race_lowest_place_spawn;
816                                 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
817                                         error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
818                 }
819
820                                 // race only (initial spawn)
821                                 g_race_qualifying = 0;
822                                 for (int p = 1; p <= race_highest_place_spawn; ++p) {
823                                         pl_race_place = p;
824                                         if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
825                                                 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
826                     }
827                                 }
828                         }
829                 }
830         } else if (!defrag_ents) {
831                 // qualifying only
832                 pl_race_checkpoint = race_NextCheckpoint(0);
833                 g_race_qualifying = 1;
834                 pl_race_place = race_lowest_place_spawn;
835                 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
836                         error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
837         }
838         } else {
839                 pl_race_checkpoint = race_NextCheckpoint(0);
840                 g_race_qualifying = 1;
841                 pl_race_place = 0; // there's only one spawn on defrag maps
842
843                 // check if a defragcp file already exists, then read it and apply the checkpoint order
844                 float fh;
845                 float len;
846                 string l;
847
848                 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
849                 if (fh >= 0) {
850                         while ((l = fgets(fh))) {
851                                 len = tokenize_console(l);
852                                 if (len != 2) {
853                                         defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
854                                         continue;
855                                 }
856                                 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
857                                         if (argv(0) == cp.targetname) {
858                                                 cp.race_checkpoint = stof(argv(1));
859                     }
860                 }
861                         }
862                         fclose(fh);
863                 }
864         }
865
866         g_race_qualifying = qual;
867
868         IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
869         {
870                 if(it.targetname == "" || !it.targetname) // somehow this is a case...
871                         continue;
872                 entity cpt = it;
873                 FOREACH_ENTITY_STRING(target, cpt.targetname,
874                 {
875                         vector org = (it.absmin + it.absmax) * 0.5;
876                         if(cpt.race_checkpoint == 0)
877                                 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
878                         else
879                                 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
880
881                         it.sprite.realowner = cpt;
882                         it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
883                 });
884         });
885
886         if (race_timed_checkpoint) {
887                 if (defrag_ents) {
888                         IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
889                         {
890                                 entity cpt = it;
891                                 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
892                                         if(it.targetname == "" || !it.targetname) // somehow this is a case...
893                                                 continue;
894                                         FOREACH_ENTITY_STRING(target, cpt.targetname, {
895                                                 if(it.sprite)
896                                                         WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
897                                         });
898                                 }
899                                 if(it.classname == "target_checkpoint") {
900                                         if(it.race_checkpoint == -2)
901                                                 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
902                                 }
903                         });
904                         if (defragcpexists != -1) {
905                                 float largest_cp_id = 0;
906                                 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
907                                         if (cp.race_checkpoint > largest_cp_id) {
908                                                 largest_cp_id = cp.race_checkpoint;
909                     }
910                 }
911                                 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
912                                         cp.race_checkpoint = largest_cp_id + 1; // finish line
913                 }
914                                 race_highest_checkpoint = largest_cp_id + 1;
915                                 race_timed_checkpoint = largest_cp_id + 1;
916                         } else {
917                                 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
918                                         cp.race_checkpoint = 255; // finish line
919                 }
920                                 race_highest_checkpoint = 255;
921                                 race_timed_checkpoint = 255;
922                         }
923                 } else {
924                         IL_EACH(g_racecheckpoints, it.sprite,
925                         {
926                                 if (it.race_checkpoint == 0) {
927                                         WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
928                 } else if (it.race_checkpoint == race_timed_checkpoint) {
929                                         WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
930                                 }
931             });
932                 }
933         }
934
935         if (defrag_ents) {
936                 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
937                         for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
938                                 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
939                                         trigger.wait = 0;
940                                         trigger.delay = 0;
941                                         targ.wait = 0;
942                                         targ.delay = 0;
943
944                     // These just make the game crash on some maps with oddly shaped triggers.
945                     // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
946                     // and often one of them just passed through without being registered. Hope it's fixed  in a better way now.
947                     // (happened on item triggers too)
948                     //
949                                         //targ.wait = -2;
950                                         //targ.delay = 0;
951
952                                         //setsize(targ, trigger.mins, trigger.maxs);
953                                         //setorigin(targ, trigger.origin);
954                                         //remove(trigger);
955                                 }
956             }
957         }
958         }
959 }
960
961 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
962 {
963         if(g_race_qualifying)
964         {
965                 // spawn at first
966                 if(this.race_checkpoint != 0)
967                         return '-1 0 0';
968                 if(spot.race_place != race_lowest_place_spawn)
969                         return '-1 0 0';
970         }
971         else
972         {
973                 if(this.race_checkpoint != player.race_respawn_checkpoint)
974                         return '-1 0 0';
975                 // try reusing the previous spawn
976                 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
977                         current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
978                 if(this.race_checkpoint == 0)
979                 {
980                         int pl = player.race_place;
981                         if(pl > race_highest_place_spawn)
982                                 pl = 0;
983                         if(pl == 0 && !player.race_started)
984                                 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
985                         if(spot.race_place != pl)
986                                 return '-1 0 0';
987                 }
988         }
989         return current;
990 }
991
992 spawnfunc(trigger_race_checkpoint)
993 {
994         vector o;
995         if(!g_race && !g_cts) { delete(this); return; }
996
997         EXACTTRIGGER_INIT;
998
999         this.use = checkpoint_use;
1000         if (!(this.spawnflags & 1))
1001                 settouch(this, checkpoint_touch);
1002
1003         o = (this.absmin + this.absmax) * 0.5;
1004         tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
1005         waypoint_spawnforitem_force(this, trace_endpos);
1006         this.nearestwaypointtimeout = -1;
1007
1008         if(this.message == "")
1009                 this.message = "went backwards";
1010         if (this.message2 == "")
1011                 this.message2 = "was pushed backwards by";
1012         if (this.race_penalty_reason == "")
1013                 this.race_penalty_reason = "missing a checkpoint";
1014
1015         this.race_checkpoint = this.cnt;
1016
1017         if(this.race_checkpoint > race_highest_checkpoint)
1018         {
1019                 race_highest_checkpoint = this.race_checkpoint;
1020                 if(this.spawnflags & 8)
1021                         race_timed_checkpoint = this.race_checkpoint;
1022                 else
1023                         race_timed_checkpoint = 0;
1024         }
1025
1026         if(!this.race_penalty)
1027         {
1028                 if(this.race_checkpoint)
1029                         WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
1030                 else
1031                         WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
1032         }
1033
1034         this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1035         this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1036
1037         IL_PUSH(g_racecheckpoints, this);
1038
1039         InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1040 }
1041
1042 void target_checkpoint_setup(entity this)
1043 {
1044         if(!g_race && !g_cts) { delete(this); return; }
1045         defrag_ents = 1;
1046
1047         // if this is targeted, then it probably isn't a trigger
1048         bool is_trigger = this.targetname == "";
1049
1050         if(is_trigger)
1051                 EXACTTRIGGER_INIT;
1052
1053         this.use = checkpoint_use;
1054         if (is_trigger && !(this.spawnflags & 1))
1055                 settouch(this, checkpoint_touch);
1056
1057         vector org = this.origin;
1058
1059         // bots should only pathfind to this if it is a valid touchable trigger
1060         if(is_trigger)
1061         {
1062                 org = (this.absmin + this.absmax) * 0.5;
1063                 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1064                 waypoint_spawnforitem_force(this, trace_endpos);
1065                 this.nearestwaypointtimeout = -1;
1066         }
1067
1068         if(this.message == "")
1069                 this.message = "went backwards";
1070         if (this.message2 == "")
1071                 this.message2 = "was pushed backwards by";
1072         if (this.race_penalty_reason == "")
1073                 this.race_penalty_reason = "missing a checkpoint";
1074
1075         if(this.classname == "target_startTimer")
1076                 this.race_checkpoint = 0;
1077         else
1078                 this.race_checkpoint = -2;
1079
1080         race_timed_checkpoint = 1;
1081
1082         IL_PUSH(g_race_targets, this);
1083
1084         InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1085 }
1086
1087 spawnfunc(target_checkpoint)
1088 {
1089         // xonotic defrag entity
1090         target_checkpoint_setup(this);
1091 }
1092
1093 // compatibility entity names
1094 spawnfunc(target_startTimer) { target_checkpoint_setup(this); }
1095 spawnfunc(target_stopTimer) { target_checkpoint_setup(this); }
1096
1097 void race_AbandonRaceCheck(entity p)
1098 {
1099         if(race_completing && !CS(p).race_completed)
1100         {
1101                 CS(p).race_completed = 1;
1102                 MAKE_INDEPENDENT_PLAYER(p);
1103                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1104                 ClientData_Touch(p);
1105         }
1106 }
1107
1108 void race_StartCompleting()
1109 {
1110         race_completing = 1;
1111         FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1112 }
1113
1114 void race_PreparePlayer(entity this)
1115 {
1116         race_ClearTime(this);
1117         this.race_place = 0;
1118         this.race_started = 0;
1119         this.race_respawn_checkpoint = 0;
1120         this.race_respawn_spotref = NULL;
1121 }
1122
1123 void race_RetractPlayer(entity this)
1124 {
1125         if(!g_race && !g_cts)
1126                 return;
1127         if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1128                 race_ClearTime(this);
1129         this.race_checkpoint = this.race_respawn_checkpoint;
1130 }
1131
1132 spawnfunc(info_player_race)
1133 {
1134         if(!g_race && !g_cts) { delete(this); return; }
1135         ++race_spawns;
1136         spawnfunc_info_player_deathmatch(this);
1137
1138         if(this.race_place > race_highest_place_spawn)
1139                 race_highest_place_spawn = this.race_place;
1140         if(this.race_place < race_lowest_place_spawn)
1141                 race_lowest_place_spawn = this.race_place;
1142 }
1143
1144 void race_ClearRecords()
1145 {
1146         for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1147         {
1148                 race_checkpoint_records[j] = 0;
1149                 strfree(race_checkpoint_recordholders[j]);
1150         }
1151
1152         FOREACH_CLIENT(true, {
1153                 float p = it.race_place;
1154                 race_PreparePlayer(it);
1155                 it.race_place = p;
1156         });
1157 }
1158
1159 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1160 {
1161         if(g_race_qualifying)
1162         {
1163                 pl.race_penalty_accumulator += penalty;
1164                 if(IS_REAL_CLIENT(pl))
1165                 {
1166                         msg_entity = pl;
1167                         WRITESPECTATABLE_MSG_ONE(msg_entity, {
1168                                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1169                                 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1170                                 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1171                                 WriteString(MSG_ONE, reason);
1172                         });
1173                 }
1174         }
1175         else
1176         {
1177                 pl.race_penalty = time + penalty;
1178                 if(IS_REAL_CLIENT(pl))
1179                 {
1180                         msg_entity = pl;
1181                         WRITESPECTATABLE_MSG_ONE(msg_entity, {
1182                                 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1183                                 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1184                                 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1185                                 WriteString(MSG_ONE, reason);
1186                         });
1187                 }
1188         }
1189 }
1190
1191 void penalty_touch(entity this, entity toucher)
1192 {
1193         EXACTTRIGGER_TOUCH(this, toucher);
1194         if(toucher.race_lastpenalty != this)
1195         {
1196                 toucher.race_lastpenalty = this;
1197                 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1198         }
1199 }
1200
1201 void penalty_use(entity this, entity actor, entity trigger)
1202 {
1203         race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1204 }
1205
1206 spawnfunc(trigger_race_penalty)
1207 {
1208         // TODO: find out why this wasnt done:
1209         //if(!g_cts && !g_race) { remove(this); return; }
1210
1211         EXACTTRIGGER_INIT;
1212
1213         this.use = penalty_use;
1214         if (!(this.spawnflags & 1))
1215                 settouch(this, penalty_touch);
1216
1217         if (this.race_penalty_reason == "")
1218                 this.race_penalty_reason = "missing a checkpoint";
1219         if (!this.race_penalty)
1220                 this.race_penalty = 5;
1221 }
1222
1223 float race_GetFractionalLapCount(entity e)
1224 {
1225         // interesting metrics (idea by KrimZon) to maybe sort players in the
1226         // scoreboard, immediately updates when overtaking
1227         //
1228         // requires the track to be built so you never get farther away from the
1229         // next checkpoint, though, and current Xonotic race maps are not built that
1230         // way
1231         //
1232         // also, this code is slow and would need optimization (i.e. "next CP"
1233         // links on CP entities)
1234
1235         float l;
1236         l = GameRules_scoring_add(e, RACE_LAPS, 0);
1237         if(CS(e).race_completed)
1238                 return l; // not fractional
1239
1240         vector o0, o1;
1241         float bestfraction, fraction;
1242         entity lastcp;
1243         float nextcpindex, lastcpindex;
1244
1245         nextcpindex = max(e.race_checkpoint, 0);
1246         lastcpindex = e.race_respawn_checkpoint;
1247         lastcp = e.race_respawn_spotref;
1248
1249         if(nextcpindex == lastcpindex)
1250                 return l; // finish
1251
1252         bestfraction = 1;
1253         IL_EACH(g_racecheckpoints, true,
1254         {
1255                 if(it.race_checkpoint != lastcpindex)
1256                         continue;
1257                 if(lastcp)
1258                         if(it != lastcp)
1259                                 continue;
1260                 o0 = (it.absmin + it.absmax) * 0.5;
1261                 IL_EACH(g_racecheckpoints, true,
1262                 {
1263                         if(it.race_checkpoint != nextcpindex)
1264                                 continue;
1265                         o1 = (it.absmin + it.absmax) * 0.5;
1266                         if(o0 == o1)
1267                                 continue;
1268                         fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1269                         if(fraction < bestfraction)
1270                                 bestfraction = fraction;
1271                 });
1272         });
1273
1274         // we are at CP "nextcpindex - bestfraction"
1275         // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1276         // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1277         float c, nc;
1278         nc = race_highest_checkpoint + 1;
1279         c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;
1280
1281         return l + c / nc;
1282 }