1 float race_readTime(string map, float pos)
3 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
5 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
8 string race_readUID(string map, float pos)
10 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
12 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
15 float race_readPos(string map, float t)
18 for (i = 1; i <= RANKINGS_CNT; ++i)
19 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
22 return 0; // pos is zero if unranked
25 void race_writeTime(string map, float t, string myuid)
27 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
30 newpos = race_readPos(map, t);
33 for(i = 1; i <= RANKINGS_CNT; ++i)
35 if(race_readUID(map, i) == myuid)
40 // player improved his existing record, only have to iterate on ranks between new and old recs
41 for (i = prevpos; i > newpos; --i)
43 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
44 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
49 // player has no ranked record yet
50 for (i = RANKINGS_CNT; i > newpos; --i)
52 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
53 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
57 // store new time itself
58 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
59 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
62 string race_readName(string map, float pos)
64 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
66 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
70 #define MAX_CHECKPOINTS 255
72 void spawnfunc_target_checkpoint();
75 .float race_penalty_accumulator;
76 .string race_penalty_reason;
77 .float race_checkpoint; // player: next checkpoint that has to be reached
79 .entity race_lastpenalty;
83 float race_checkpoint_records[MAX_CHECKPOINTS];
84 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
85 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
86 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
87 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
89 float race_highest_checkpoint;
90 float race_timed_checkpoint;
95 float race_NextCheckpoint(float f)
97 if(f >= race_highest_checkpoint)
103 float race_PreviousCheckpoint(float f)
108 return race_highest_checkpoint;
114 // 0 = common start/finish
117 float race_CheckpointNetworkID(float f)
119 if(race_timed_checkpoint)
123 else if(f == race_timed_checkpoint)
124 return 255; // finish
129 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
138 cp = e.race_checkpoint;
139 recordtime = race_checkpoint_records[cp];
140 recordholder = race_checkpoint_recordholders[cp];
141 if(recordholder == e.netname)
144 if(!IS_REAL_CLIENT(e))
149 WRITESPECTATABLE_MSG_ONE({
150 WriteByte(MSG_ONE, SVC_TEMPENTITY);
151 WriteByte(MSG_ONE, TE_CSQC_RACE);
154 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
155 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
156 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
159 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
160 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
161 WriteInt24_t(MSG_ONE, recordtime);
162 WriteString(MSG_ONE, recordholder);
166 void race_send_recordtime(float msg)
168 // send the server best time
169 WriteByte(msg, SVC_TEMPENTITY);
170 WriteByte(msg, TE_CSQC_RACE);
171 WriteByte(msg, RACE_NET_SERVER_RECORD);
172 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
175 void race_SendRankings(float pos, float prevpos, float del, float msg)
177 WriteByte(msg, SVC_TEMPENTITY);
178 WriteByte(msg, TE_CSQC_RACE);
179 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
180 WriteShort(msg, pos);
181 WriteShort(msg, prevpos);
182 WriteShort(msg, del);
183 WriteString(msg, race_readName(GetMapname(), pos));
184 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
187 void race_SendStatus(float id, entity e)
189 if(!IS_REAL_CLIENT(e))
198 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
199 WriteByte(msg, SVC_TEMPENTITY);
200 WriteByte(msg, TE_CSQC_RACE);
201 WriteByte(msg, RACE_NET_SERVER_STATUS);
203 WriteString(msg, e.netname);
207 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
209 // netname only used TEMPORARILY for printing
210 float newpos, player_prevpos;
211 newpos = race_readPos(map, t);
215 for(i = 1; i <= RANKINGS_CNT; ++i)
217 if(race_readUID(map, i) == myuid)
222 string oldrec_holder;
223 if (player_prevpos && (player_prevpos < newpos || !newpos))
225 oldrec = race_readTime(GetMapname(), player_prevpos);
226 race_SendStatus(0, e); // "fail"
227 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
232 // no ranking, time worse than the worst ranked
233 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
234 race_SendStatus(0, e); // "fail"
235 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
239 // if we didn't hit a return yet, we have a new record!
241 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
244 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
248 oldrec = race_readTime(GetMapname(), newpos);
249 oldrec_holder = race_readName(GetMapname(), newpos);
252 race_writeTime(GetMapname(), t, myuid);
256 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
257 race_send_recordtime(MSG_ALL);
260 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
262 strunzone(rankings_reply);
263 rankings_reply = strzone(getrankings());
265 if(newpos == player_prevpos)
267 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
268 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
269 else { race_SendStatus(1, e); } // "new time"
273 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
274 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
275 else { race_SendStatus(2, e); } // "new rank"
279 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
280 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
281 else { race_SendStatus(2, e); } // "new rank"
285 void race_deleteTime(string map, float pos)
294 for (i = pos; i <= RANKINGS_CNT; ++i)
296 if (i == RANKINGS_CNT)
298 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
299 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
303 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
304 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
308 race_SendRankings(pos, 0, 1, MSG_ALL);
310 race_send_recordtime(MSG_ALL);
313 strunzone(rankings_reply);
314 rankings_reply = strzone(getrankings());
317 void race_SendTime(entity e, float cp, float t, float tvalid)
322 if(g_race_qualifying)
323 t += e.race_penalty_accumulator;
325 t = TIME_ENCODE(t); // make integer
326 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
329 if(cp == race_timed_checkpoint) // finish line
330 if (!e.race_completed)
333 if(g_race_qualifying)
335 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
337 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
341 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
343 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
345 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
346 snew = TIME_ENCODE(time - game_starttime);
347 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
348 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
350 if(autocvar_fraglimit)
351 if(l >= autocvar_fraglimit)
352 race_StartCompleting();
356 e.race_completed = 1;
357 MAKE_INDEPENDENT_PLAYER(e);
358 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
366 if(g_race_qualifying)
370 recordtime = race_checkpoint_records[cp];
371 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
372 if(recordholder == e.netname)
377 if(cp == race_timed_checkpoint)
379 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
380 if(g_cts && autocvar_g_cts_finish_kill_delay)
385 if(t < recordtime || recordtime == 0)
387 race_checkpoint_records[cp] = t;
388 if(race_checkpoint_recordholders[cp])
389 strunzone(race_checkpoint_recordholders[cp]);
390 race_checkpoint_recordholders[cp] = strzone(e.netname);
391 if(g_race_qualifying)
393 FOR_EACH_REALPLAYER(p)
394 if(p.race_checkpoint == cp)
395 race_SendNextCheckpoint(p, 0);
408 if(IS_REAL_CLIENT(e))
411 if(g_race_qualifying)
413 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
414 WriteByte(MSG_ONE, SVC_TEMPENTITY);
415 WriteByte(MSG_ONE, TE_CSQC_RACE);
416 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
417 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
418 WriteInt24_t(MSG_ONE, t); // time to that intermediate
419 WriteInt24_t(MSG_ONE, recordtime); // previously best time
420 WriteString(MSG_ONE, recordholder); // record holder
425 else // RACE! Not Qualifying
427 float lself, lother, othtime;
429 oth = race_checkpoint_lastplayers[cp];
432 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
433 lother = race_checkpoint_lastlaps[cp];
434 othtime = race_checkpoint_lasttimes[cp];
437 lself = lother = othtime = 0;
439 if(IS_REAL_CLIENT(e))
442 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
443 WriteByte(MSG_ONE, SVC_TEMPENTITY);
444 WriteByte(MSG_ONE, TE_CSQC_RACE);
445 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
446 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
449 WriteInt24_t(MSG_ONE, 0);
450 WriteByte(MSG_ONE, 0);
451 WriteString(MSG_ONE, "");
455 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
456 WriteByte(MSG_ONE, lself - lother);
457 WriteString(MSG_ONE, oth.netname); // record holder
462 race_checkpoint_lastplayers[cp] = e;
463 race_checkpoint_lasttimes[cp] = time;
464 race_checkpoint_lastlaps[cp] = lself;
466 if(IS_REAL_CLIENT(oth))
469 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
470 WriteByte(MSG_ONE, SVC_TEMPENTITY);
471 WriteByte(MSG_ONE, TE_CSQC_RACE);
472 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
473 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
476 WriteInt24_t(MSG_ONE, 0);
477 WriteByte(MSG_ONE, 0);
478 WriteString(MSG_ONE, "");
482 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
483 WriteByte(MSG_ONE, lother - lself);
484 WriteString(MSG_ONE, e.netname); // record holder
491 void race_ClearTime(entity e)
493 e.race_checkpoint = 0;
495 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
496 e.race_penalty_accumulator = 0;
497 e.race_lastpenalty = world;
499 if(!IS_REAL_CLIENT(e))
503 WRITESPECTATABLE_MSG_ONE({
504 WriteByte(MSG_ONE, SVC_TEMPENTITY);
505 WriteByte(MSG_ONE, TE_CSQC_RACE);
506 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
510 void dumpsurface(entity e)
514 print("Surfaces of ", etos(e), ":\n");
516 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
520 n = getsurfacenumpoints(e, si);
523 print(" Surface ", ftos(si), ":\n");
524 norm = getsurfacenormal(e, si);
525 print(" Normal = ", vtos(norm), "\n");
526 for(ni = 0; ni < n; ++ni)
528 vec = getsurfacepoint(e, si, ni);
529 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
534 void checkpoint_passed()
539 if(other.classname == "porto")
541 // do not allow portalling through checkpoints
542 trace_plane_normal = normalize(-1 * other.velocity);
551 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
554 oldmsg = self.message;
557 self.message = oldmsg;
560 if (!IS_PLAYER(other))
564 * Remove unauthorized equipment
566 Portal_ClearAll(other);
568 other.porto_forbidden = 2; // decreased by 1 each StartFrame
572 if(self.race_checkpoint == -2)
574 self.race_checkpoint = other.race_checkpoint;
577 float largest_cp_id = 0;
579 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
582 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
584 largest_cp_id = cp.race_checkpoint;
585 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
586 cp.race_checkpoint = largest_cp_id + 1; // finish line
587 race_highest_checkpoint = largest_cp_id + 1;
588 race_timed_checkpoint = largest_cp_id + 1;
590 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
592 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
599 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
600 cp.race_checkpoint = 1;
601 race_highest_checkpoint = 1;
602 race_timed_checkpoint = 1;
606 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
608 if(self.race_penalty)
610 if(other.race_lastpenalty != self)
612 other.race_lastpenalty = self;
613 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
617 if(other.race_penalty)
623 if(self.spawnflags & 2)
626 oldmsg = self.message;
629 self.message = oldmsg;
632 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
633 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
634 other.race_respawn_checkpoint = self.race_checkpoint;
635 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
636 other.race_started = 1;
638 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
640 if(!self.race_checkpoint) // start line
642 other.race_laptime = time;
643 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
644 other.race_penalty_accumulator = 0;
645 other.race_lastpenalty = world;
648 if(g_race_qualifying)
649 race_SendNextCheckpoint(other, 0);
651 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
654 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
657 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
658 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
663 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
669 if(self.spawnflags & 4)
670 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
674 void checkpoint_touch()
680 void checkpoint_use()
682 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
689 float race_waypointsprite_visible_for_player(entity e)
691 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
693 else if(e.race_checkpoint == self.owner.race_checkpoint)
700 void trigger_race_checkpoint_verify()
710 qual = g_race_qualifying;
714 self.classname = "player";
718 for(i = 0; i <= race_highest_checkpoint; ++i)
720 self.race_checkpoint = race_NextCheckpoint(i);
722 // race only (middle of the race)
723 g_race_qualifying = 0;
725 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
726 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
731 g_race_qualifying = 1;
732 self.race_place = race_lowest_place_spawn;
733 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
734 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
736 // race only (initial spawn)
737 g_race_qualifying = 0;
738 for(p = 1; p <= race_highest_place_spawn; ++p)
741 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
742 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
747 else if(!defrag_ents)
750 self.race_checkpoint = race_NextCheckpoint(0);
751 g_race_qualifying = 1;
752 self.race_place = race_lowest_place_spawn;
753 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
754 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
758 self.race_checkpoint = race_NextCheckpoint(0);
759 g_race_qualifying = 1;
760 self.race_place = 0; // there's only one spawn on defrag maps
762 // check if a defragcp file already exists, then read it and apply the checkpoint order
767 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
770 while((l = fgets(fh)))
772 len = tokenize_console(l);
775 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
778 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
779 if(argv(0) == cp.targetname)
780 cp.race_checkpoint = stof(argv(1));
786 g_race_qualifying = qual;
788 if(race_timed_checkpoint)
792 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
793 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
794 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
795 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
797 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
799 if(cp.race_checkpoint == -2) // something's wrong with the defrag cp file or it has not been written yet, set defragcpexists to -1 so that it will be rewritten when someone finishes
803 if(defragcpexists != -1)
805 float largest_cp_id = 0;
806 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
807 if(cp.race_checkpoint > largest_cp_id)
808 largest_cp_id = cp.race_checkpoint;
809 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
810 cp.race_checkpoint = largest_cp_id + 1; // finish line
811 race_highest_checkpoint = largest_cp_id + 1;
812 race_timed_checkpoint = largest_cp_id + 1;
816 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
817 cp.race_checkpoint = 255; // finish line
818 race_highest_checkpoint = 255;
819 race_timed_checkpoint = 255;
824 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
827 if(cp.race_checkpoint == 0)
828 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
829 else if(cp.race_checkpoint == race_timed_checkpoint)
830 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
837 entity trigger, targ;
838 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
839 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
840 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
847 // These just make the game crash on some maps with oddly shaped triggers.
848 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
849 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
850 // (happened on item triggers too)
855 //setsize(targ, trigger.mins, trigger.maxs);
856 //setorigin(targ, trigger.origin);
864 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
866 if(g_race_qualifying)
869 if(self.race_checkpoint != 0)
871 if(spot.race_place != race_lowest_place_spawn)
876 if(self.race_checkpoint != player.race_respawn_checkpoint)
878 // try reusing the previous spawn
879 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
880 current_x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
881 if(self.race_checkpoint == 0)
884 pl = player.race_place;
885 if(pl > race_highest_place_spawn)
887 if(pl == 0 && !player.race_started)
888 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
889 if(spot.race_place != pl)
896 void spawnfunc_trigger_race_checkpoint()
899 if(!g_race && !g_cts) { remove(self); return; }
903 self.use = checkpoint_use;
904 if (!(self.spawnflags & 1))
905 self.touch = checkpoint_touch;
907 o = (self.absmin + self.absmax) * 0.5;
908 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
909 waypoint_spawnforitem_force(self, trace_endpos);
910 self.nearestwaypointtimeout = time + 1000000000;
912 if(self.message == "")
913 self.message = "went backwards";
914 if (self.message2 == "")
915 self.message2 = "was pushed backwards by";
916 if (self.race_penalty_reason == "")
917 self.race_penalty_reason = "missing a checkpoint";
919 self.race_checkpoint = self.cnt;
921 if(self.race_checkpoint > race_highest_checkpoint)
923 race_highest_checkpoint = self.race_checkpoint;
924 if(self.spawnflags & 8)
925 race_timed_checkpoint = self.race_checkpoint;
927 race_timed_checkpoint = 0;
930 if(!self.race_penalty)
932 if(self.race_checkpoint)
933 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
935 WaypointSprite_SpawnFixed("race-start-finish", o, self, sprite, RADARICON_NONE, '1 0.5 0');
938 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
939 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
941 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
944 void spawnfunc_target_checkpoint() // defrag entity
947 if(!g_race && !g_cts) { remove(self); return; }
952 self.use = checkpoint_use;
953 if (!(self.spawnflags & 1))
954 self.touch = checkpoint_touch;
956 o = (self.absmin + self.absmax) * 0.5;
957 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
958 waypoint_spawnforitem_force(self, trace_endpos);
959 self.nearestwaypointtimeout = time + 1000000000;
961 if(self.message == "")
962 self.message = "went backwards";
963 if (self.message2 == "")
964 self.message2 = "was pushed backwards by";
965 if (self.race_penalty_reason == "")
966 self.race_penalty_reason = "missing a checkpoint";
968 if(self.classname == "target_startTimer")
969 self.race_checkpoint = 0;
971 self.race_checkpoint = -2;
973 race_timed_checkpoint = 1;
975 if(self.race_checkpoint == 0)
976 WaypointSprite_SpawnFixed("race-start", o, self, sprite, RADARICON_NONE, '1 0.5 0');
978 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
980 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
982 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
985 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
986 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
988 void race_AbandonRaceCheck(entity p)
990 if(race_completing && !p.race_completed)
992 p.race_completed = 1;
993 MAKE_INDEPENDENT_PLAYER(p);
994 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
999 void race_StartCompleting()
1002 race_completing = 1;
1004 if(p.deadflag != DEAD_NO)
1005 race_AbandonRaceCheck(p);
1008 void race_PreparePlayer()
1010 race_ClearTime(self);
1011 self.race_place = 0;
1012 self.race_started = 0;
1013 self.race_respawn_checkpoint = 0;
1014 self.race_respawn_spotref = world;
1017 void race_RetractPlayer()
1019 if(!g_race && !g_cts)
1021 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1022 race_ClearTime(self);
1023 self.race_checkpoint = self.race_respawn_checkpoint;
1026 void spawnfunc_info_player_race (void)
1028 if(!g_race && !g_cts) { remove(self); return; }
1030 spawnfunc_info_player_deathmatch();
1032 if(self.race_place > race_highest_place_spawn)
1033 race_highest_place_spawn = self.race_place;
1034 if(self.race_place < race_lowest_place_spawn)
1035 race_lowest_place_spawn = self.race_place;
1038 void race_ClearRecords()
1043 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1045 race_checkpoint_records[i] = 0;
1046 if(race_checkpoint_recordholders[i])
1047 strunzone(race_checkpoint_recordholders[i]);
1048 race_checkpoint_recordholders[i] = string_null;
1052 FOR_EACH_CLIENT(self)
1055 p = self.race_place;
1056 race_PreparePlayer();
1057 self.race_place = p;
1062 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1064 if(g_race_qualifying)
1066 pl.race_penalty_accumulator += penalty;
1067 if(IS_REAL_CLIENT(pl))
1070 WRITESPECTATABLE_MSG_ONE({
1071 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1072 WriteByte(MSG_ONE, TE_CSQC_RACE);
1073 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1074 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1075 WriteString(MSG_ONE, reason);
1081 pl.race_penalty = time + penalty;
1082 if(IS_REAL_CLIENT(pl))
1085 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1086 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1087 WriteByte(MSG_ONE, TE_CSQC_RACE);
1088 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1089 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1090 WriteString(MSG_ONE, reason);
1096 void penalty_touch()
1099 if(other.race_lastpenalty != self)
1101 other.race_lastpenalty = self;
1102 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1108 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1111 void spawnfunc_trigger_race_penalty()
1115 self.use = penalty_use;
1116 if (!(self.spawnflags & 1))
1117 self.touch = penalty_touch;
1119 if (self.race_penalty_reason == "")
1120 self.race_penalty_reason = "missing a checkpoint";
1121 if (!self.race_penalty)
1122 self.race_penalty = 5;
1125 float race_GetFractionalLapCount(entity e)
1127 // interesting metrics (idea by KrimZon) to maybe sort players in the
1128 // scoreboard, immediately updates when overtaking
1130 // requires the track to be built so you never get farther away from the
1131 // next checkpoint, though, and current Xonotic race maps are not built that
1134 // also, this code is slow and would need optimization (i.e. "next CP"
1135 // links on CP entities)
1138 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1139 if(e.race_completed)
1140 return l; // not fractional
1143 float bestfraction, fraction;
1144 entity lastcp, cp0, cp1;
1145 float nextcpindex, lastcpindex;
1147 nextcpindex = max(e.race_checkpoint, 0);
1148 lastcpindex = e.race_respawn_checkpoint;
1149 lastcp = e.race_respawn_spotref;
1151 if(nextcpindex == lastcpindex)
1155 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1157 if(cp0.race_checkpoint != lastcpindex)
1162 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1163 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1165 if(cp1.race_checkpoint != nextcpindex)
1167 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1170 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1171 if(fraction < bestfraction)
1172 bestfraction = fraction;
1176 // we are at CP "nextcpindex - bestfraction"
1177 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1178 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1180 nc = race_highest_checkpoint + 1;
1181 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;