1 #define MAX_CHECKPOINTS 255
3 void spawnfunc_target_checkpoint();
6 .float race_penalty_accumulator;
7 .string race_penalty_reason;
8 .float race_checkpoint; // player: next checkpoint that has to be reached
10 .entity race_lastpenalty;
14 float race_checkpoint_records[MAX_CHECKPOINTS];
15 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
16 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
17 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
18 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
20 float race_highest_checkpoint;
21 float race_timed_checkpoint;
26 float race_NextCheckpoint(float f)
28 if(f >= race_highest_checkpoint)
34 float race_PreviousCheckpoint(float f)
39 return race_highest_checkpoint;
45 // 0 = common start/finish
48 float race_CheckpointNetworkID(float f)
50 if(race_timed_checkpoint)
54 else if(f == race_timed_checkpoint)
60 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
69 cp = e.race_checkpoint;
70 recordtime = race_checkpoint_records[cp];
71 recordholder = race_checkpoint_recordholders[cp];
72 if(recordholder == e.netname)
77 WRITESPECTATABLE_MSG_ONE({
78 WriteByte(MSG_ONE, SVC_TEMPENTITY);
79 WriteByte(MSG_ONE, TE_CSQC_RACE);
82 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
83 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
84 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
87 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
88 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
89 WriteInt24_t(MSG_ONE, recordtime);
90 WriteString(MSG_ONE, recordholder);
94 void race_InitSpectator()
97 if(msg_entity.enemy.race_laptime)
98 race_SendNextCheckpoint(msg_entity.enemy, 1);
101 void race_send_recordtime(float msg)
103 // send the server best time
104 WriteByte(msg, SVC_TEMPENTITY);
105 WriteByte(msg, TE_CSQC_RACE);
106 WriteByte(msg, RACE_NET_SERVER_RECORD);
107 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
110 void race_SendRankings(float pos, float prevpos, float del, float msg)
112 WriteByte(msg, SVC_TEMPENTITY);
113 WriteByte(msg, TE_CSQC_RACE);
114 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
115 WriteShort(msg, pos);
116 WriteShort(msg, prevpos);
117 WriteShort(msg, del);
118 WriteString(msg, race_readName(GetMapname(), pos));
119 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
122 void race_SendStatus(float id, entity e)
130 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
131 WriteByte(msg, SVC_TEMPENTITY);
132 WriteByte(msg, TE_CSQC_RACE);
133 WriteByte(msg, RACE_NET_SERVER_STATUS);
135 WriteString(msg, e.netname);
139 void race_setTime(string map, float t, string myuid, string mynetname, entity e) { // netname only used TEMPORARILY for printing
140 float newpos, player_prevpos;
141 newpos = race_readPos(map, t);
144 for(i = 1; i <= RANKINGS_CNT; ++i)
146 if(race_readUID(map, i) == myuid)
151 string recorddifference, oldrec_holder;
152 if (player_prevpos && (player_prevpos < newpos || !newpos))
154 oldrec = race_readTime(GetMapname(), player_prevpos);
155 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
156 bprint(mynetname, "^7 couldn't break their ", race_placeName(player_prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
157 race_SendStatus(0, e); // "fail"
159 } else if (!newpos) { // no ranking, time worse than the worst ranked
160 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - race_readTime(GetMapname(), RANKINGS_CNT)), "]");
161 bprint(mynetname, "^7 couldn't break the ", race_placeName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(race_readTime(GetMapname(), RANKINGS_CNT)), recorddifference, "\n");
162 race_SendStatus(0, e); // "fail"
166 // if we didn't hit a return yet, we have a new record!
168 oldrec = race_readTime(GetMapname(), newpos);
169 oldrec_holder = race_readName(GetMapname(), newpos);
171 // move other rankings out of the way
172 if (player_prevpos) { // player improved his existing record, only have to iterate on ranks between new and old recs
173 for (i = player_prevpos; i > newpos; --i) {
174 race_writeTime(GetMapname(), race_readTime(GetMapname(), newpos-1), race_readUID(GetMapname(), newpos-1));
176 } else { // player has no ranked record yet
177 for (i = RANKINGS_CNT; i > newpos; --i) {
178 race_writeTime(GetMapname(), race_readTime(GetMapname(), newpos-1), race_readUID(GetMapname(), newpos-1));
183 race_writeTime(GetMapname(), t, myuid);
186 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
187 race_send_recordtime(MSG_ALL);
190 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
192 strunzone(rankings_reply);
193 rankings_reply = strzone(getrankings());
195 if(newpos == player_prevpos) {
196 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
197 bprint(mynetname, "^1 improved their 1st place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
198 } else if (oldrec == 0) {
199 bprint(mynetname, "^1 set the 1st place record with ", TIME_ENCODED_TOSTRING(t), "\n");
201 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
202 bprint(mynetname, "^1 broke ", oldrec_holder, "^1's 1st place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
204 race_SendStatus(3, e); // "new server record"
205 Send_KillNotification(e.netname, "", "", RACE_SERVER_RECORD, MSG_RACE);
207 if(newpos == player_prevpos) {
208 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
209 bprint(mynetname, "^5 improved their ", race_placeName(newpos), " ^5place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
210 race_SendStatus(1, e); // "new time"
211 Send_KillNotification(e.netname, "", "", RACE_NEW_TIME, MSG_RACE);
212 } else if (oldrec == 0) {
213 bprint(mynetname, "^2 set the ", race_placeName(newpos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
214 race_SendStatus(2, e); // "new rank"
215 Send_KillNotification(e.netname, "", "", RACE_NEW_RANK, MSG_RACE);
217 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
218 bprint(mynetname, "^2 broke ", oldrec_holder, "^2's ", race_placeName(newpos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
219 race_SendStatus(2, e); // "new rank"
220 Send_KillNotification(e.netname, "", "", RACE_NEW_TIME, MSG_RACE);
225 void race_deleteTime(string map, float pos) {
233 for (i = pos; i <= RANKINGS_CNT; ++i) {
234 if (i == RANKINGS_CNT) {
235 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
236 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
239 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
240 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
244 race_SendRankings(pos, 0, 1, MSG_ALL);
246 race_send_recordtime(MSG_ALL);
249 strunzone(rankings_reply);
250 rankings_reply = strzone(getrankings());
253 void race_SendTime(entity e, float cp, float t, float tvalid)
258 if(g_race_qualifying)
259 t += e.race_penalty_accumulator;
261 t = TIME_ENCODE(t); // make integer
262 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
265 if(cp == race_timed_checkpoint) // finish line
266 if not(e.race_completed)
269 if(g_race_qualifying)
271 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
273 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
277 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
278 snew = TIME_ENCODE(time - game_starttime);
279 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
280 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
282 if(cvar("fraglimit"))
283 if(l >= cvar("fraglimit"))
284 race_StartCompleting();
288 e.race_completed = 1;
289 MAKE_INDEPENDENT_PLAYER(e);
290 bprint(e.netname, "^7 has finished the race.\n");
298 if(g_race_qualifying)
302 recordtime = race_checkpoint_records[cp];
303 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
304 if(recordholder == e.netname)
308 if(cp == race_timed_checkpoint)
310 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
311 if(g_cts && cvar("g_cts_finish_kill_delay"))
313 CTS_ClientKill(cvar("g_cts_finish_kill_delay"));
316 if(t < recordtime || recordtime == 0)
318 race_checkpoint_records[cp] = t;
319 if(race_checkpoint_recordholders[cp])
320 strunzone(race_checkpoint_recordholders[cp]);
321 race_checkpoint_recordholders[cp] = strzone(e.netname);
322 if(g_race_qualifying)
324 FOR_EACH_REALPLAYER(p)
325 if(p.race_checkpoint == cp)
326 race_SendNextCheckpoint(p, 0);
340 if(g_race_qualifying)
342 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
343 WriteByte(MSG_ONE, SVC_TEMPENTITY);
344 WriteByte(MSG_ONE, TE_CSQC_RACE);
345 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
346 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
347 WriteInt24_t(MSG_ONE, t); // time to that intermediate
348 WriteInt24_t(MSG_ONE, recordtime); // previously best time
349 WriteString(MSG_ONE, recordholder); // record holder
353 else // RACE! Not Qualifying
355 float lself, lother, othtime;
357 oth = race_checkpoint_lastplayers[cp];
360 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
361 lother = race_checkpoint_lastlaps[cp];
362 othtime = race_checkpoint_lasttimes[cp];
365 lself = lother = othtime = 0;
368 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
369 WriteByte(MSG_ONE, SVC_TEMPENTITY);
370 WriteByte(MSG_ONE, TE_CSQC_RACE);
371 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
372 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
375 WriteInt24_t(MSG_ONE, 0);
376 WriteByte(MSG_ONE, 0);
377 WriteString(MSG_ONE, "");
381 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
382 WriteByte(MSG_ONE, lself - lother);
383 WriteString(MSG_ONE, oth.netname); // record holder
387 race_checkpoint_lastplayers[cp] = e;
388 race_checkpoint_lasttimes[cp] = time;
389 race_checkpoint_lastlaps[cp] = lself;
392 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
393 WriteByte(MSG_ONE, SVC_TEMPENTITY);
394 WriteByte(MSG_ONE, TE_CSQC_RACE);
395 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
396 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
399 WriteInt24_t(MSG_ONE, 0);
400 WriteByte(MSG_ONE, 0);
401 WriteString(MSG_ONE, "");
405 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
406 WriteByte(MSG_ONE, lother - lself);
407 WriteString(MSG_ONE, e.netname); // record holder
413 void race_ClearTime(entity e)
415 e.race_checkpoint = 0;
417 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
418 e.race_penalty_accumulator = 0;
419 e.race_lastpenalty = world;
422 WRITESPECTATABLE_MSG_ONE({
423 WriteByte(MSG_ONE, SVC_TEMPENTITY);
424 WriteByte(MSG_ONE, TE_CSQC_RACE);
425 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
429 void dumpsurface(entity e)
433 print("Surfaces of ", etos(e), ":\n");
435 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
439 n = getsurfacenumpoints(e, si);
442 print(" Surface ", ftos(si), ":\n");
443 norm = getsurfacenormal(e, si);
444 print(" Normal = ", vtos(norm), "\n");
445 for(ni = 0; ni < n; ++ni)
447 vec = getsurfacepoint(e, si, ni);
448 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
453 void checkpoint_passed()
458 if(other.classname == "porto")
460 // do not allow portalling through checkpoints
461 trace_plane_normal = normalize(-1 * other.velocity);
470 if not((self.spawnflags & 2) && (other.classname == "player"))
473 oldmsg = self.message;
476 self.message = oldmsg;
479 if(other.classname != "player")
483 * Remove unauthorized equipment
485 Portal_ClearAll(other);
487 other.porto_forbidden = 2; // decreased by 1 each StartFrame
490 if(self.race_checkpoint == -2)
492 self.race_checkpoint = other.race_checkpoint;
497 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
499 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
501 largest_cp_id = cp.race_checkpoint;
502 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
503 cp.race_checkpoint = largest_cp_id + 1; // finish line
504 race_highest_checkpoint = largest_cp_id + 1;
505 race_timed_checkpoint = largest_cp_id + 1;
507 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
508 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
514 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
515 cp.race_checkpoint = 1;
516 race_highest_checkpoint = 1;
517 race_timed_checkpoint = 1;
521 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
523 if(self.race_penalty)
525 if(other.race_lastpenalty != self)
527 other.race_lastpenalty = self;
528 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
532 if(other.race_penalty)
538 if(self.spawnflags & 2)
541 oldmsg = self.message;
544 self.message = oldmsg;
547 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
548 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
549 other.race_respawn_checkpoint = self.race_checkpoint;
550 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
551 other.race_started = 1;
553 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
555 if(!self.race_checkpoint) // start line
557 other.race_laptime = time;
558 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
559 other.race_penalty_accumulator = 0;
560 other.race_lastpenalty = world;
563 if(g_race_qualifying)
564 race_SendNextCheckpoint(other, 0);
566 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
569 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
572 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
573 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
578 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
584 if(self.spawnflags & 4)
585 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
589 void checkpoint_touch()
595 void checkpoint_use()
597 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
604 float race_waypointsprite_visible_for_player(entity e)
606 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
608 else if(e.race_checkpoint == self.owner.race_checkpoint)
615 void trigger_race_checkpoint_verify()
625 qual = g_race_qualifying;
629 self.classname = "player";
633 for(i = 0; i <= race_highest_checkpoint; ++i)
635 self.race_checkpoint = race_NextCheckpoint(i);
637 // race only (middle of the race)
638 g_race_qualifying = 0;
640 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
641 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
646 g_race_qualifying = 1;
647 self.race_place = race_lowest_place_spawn;
648 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
649 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
651 // race only (initial spawn)
652 g_race_qualifying = 0;
653 for(p = 1; p <= race_highest_place_spawn; ++p)
656 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
657 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
662 else if(!defrag_ents)
665 self.race_checkpoint = race_NextCheckpoint(0);
666 g_race_qualifying = 1;
667 self.race_place = race_lowest_place_spawn;
668 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
669 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
673 self.race_checkpoint = race_NextCheckpoint(0);
674 g_race_qualifying = 1;
675 self.race_place = 0; // there's only one spawn on defrag maps
677 // check if a defragcp file already exists, then read it and apply the checkpoint order
682 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
685 while((l = fgets(fh)))
687 len = tokenize_console(l);
689 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
692 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
693 if(argv(0) == cp.targetname)
694 cp.race_checkpoint = stof(argv(1));
700 g_race_qualifying = qual;
702 if(race_timed_checkpoint) {
704 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
705 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
706 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
707 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
709 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
710 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
714 if(defragcpexists != -1){
716 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
717 if(cp.race_checkpoint > largest_cp_id)
718 largest_cp_id = cp.race_checkpoint;
719 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
720 cp.race_checkpoint = largest_cp_id + 1; // finish line
721 race_highest_checkpoint = largest_cp_id + 1;
722 race_timed_checkpoint = largest_cp_id + 1;
724 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
725 cp.race_checkpoint = 255; // finish line
726 race_highest_checkpoint = 255;
727 race_timed_checkpoint = 255;
731 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
734 if(cp.race_checkpoint == 0)
735 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
736 else if(cp.race_checkpoint == race_timed_checkpoint)
737 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
743 entity trigger, targ;
744 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
745 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
746 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
750 setsize(targ, trigger.mins, trigger.maxs);
751 setorigin(targ, trigger.origin);
759 void spawnfunc_trigger_race_checkpoint()
762 if(!g_race && !g_cts)
770 self.use = checkpoint_use;
771 if not(self.spawnflags & 1)
772 self.touch = checkpoint_touch;
774 o = (self.absmin + self.absmax) * 0.5;
775 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
776 waypoint_spawnforitem_force(self, trace_endpos);
777 self.nearestwaypointtimeout = time + 1000000000;
780 self.message = "went backwards";
782 self.message2 = "was pushed backwards by";
783 if (!self.race_penalty_reason)
784 self.race_penalty_reason = "missing a checkpoint";
786 self.race_checkpoint = self.cnt;
788 if(self.race_checkpoint > race_highest_checkpoint)
790 race_highest_checkpoint = self.race_checkpoint;
791 if(self.spawnflags & 8)
792 race_timed_checkpoint = self.race_checkpoint;
794 race_timed_checkpoint = 0;
797 if(!self.race_penalty)
799 if(self.race_checkpoint)
800 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
802 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
805 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
807 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
810 void spawnfunc_target_checkpoint() // defrag entity
813 if(!g_race && !g_cts)
822 self.use = checkpoint_use;
823 if not(self.spawnflags & 1)
824 self.touch = checkpoint_touch;
826 o = (self.absmin + self.absmax) * 0.5;
827 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
828 waypoint_spawnforitem_force(self, trace_endpos);
829 self.nearestwaypointtimeout = time + 1000000000;
832 self.message = "went backwards";
834 self.message2 = "was pushed backwards by";
835 if (!self.race_penalty_reason)
836 self.race_penalty_reason = "missing a checkpoint";
838 if(self.classname == "target_startTimer")
839 self.race_checkpoint = 0;
841 self.race_checkpoint = -2;
843 race_timed_checkpoint = 1;
845 if(self.race_checkpoint == 0)
846 WaypointSprite_SpawnFixed("race-start", o, self, sprite);
848 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
850 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
852 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
855 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
856 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
858 void race_AbandonRaceCheck(entity p)
860 if(race_completing && !p.race_completed)
862 p.race_completed = 1;
863 MAKE_INDEPENDENT_PLAYER(p);
864 bprint(p.netname, "^7 has abandoned the race.\n");
869 void race_StartCompleting()
874 if(p.deadflag != DEAD_NO)
875 race_AbandonRaceCheck(p);
878 void race_PreparePlayer()
880 race_ClearTime(self);
882 self.race_started = 0;
883 self.race_respawn_checkpoint = 0;
884 self.race_respawn_spotref = world;
887 void race_RetractPlayer()
889 if(!g_race && !g_cts)
891 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
892 race_ClearTime(self);
893 self.race_checkpoint = self.race_respawn_checkpoint;
898 if(!g_race && !g_cts)
901 race_AbandonRaceCheck(self);
906 if(!g_race && !g_cts)
908 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
909 race_PreparePlayer();
911 race_RetractPlayer();
913 race_AbandonRaceCheck(self);
916 void race_PostSpawn(entity spot)
918 if(!g_race && !g_cts)
921 if(spot.target == "")
922 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
923 race_PreparePlayer();
925 // if we need to respawn, do it right
926 self.race_respawn_checkpoint = self.race_checkpoint;
927 self.race_respawn_spotref = spot;
932 void race_PreSpawnObserver()
934 if(!g_race && !g_cts)
936 race_PreparePlayer();
937 self.race_checkpoint = -1;
940 void spawnfunc_info_player_race (void)
942 if(!g_race && !g_cts)
948 spawnfunc_info_player_deathmatch();
950 if(self.race_place > race_highest_place_spawn)
951 race_highest_place_spawn = self.race_place;
952 if(self.race_place < race_lowest_place_spawn)
953 race_lowest_place_spawn = self.race_place;
956 void race_ClearRecords()
961 for(i = 0; i < MAX_CHECKPOINTS; ++i)
963 race_checkpoint_records[i] = 0;
964 if(race_checkpoint_recordholders[i])
965 strunzone(race_checkpoint_recordholders[i]);
966 race_checkpoint_recordholders[i] = string_null;
970 FOR_EACH_CLIENT(self)
974 race_PreparePlayer();
980 void race_ReadyRestart()
984 Score_NicePrint(world);
987 PlayerScore_Sort(race_place);
994 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
998 print(e.netname, " = ", ftos(e.race_place), "\n");
1001 if(g_race_qualifying == 2)
1003 g_race_qualifying = 0;
1004 independent_players = 0;
1005 cvar_set("fraglimit", ftos(race_fraglimit));
1006 cvar_set("leadlimit", ftos(race_leadlimit));
1007 cvar_set("timelimit", ftos(race_timelimit));
1012 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1014 if(g_race_qualifying)
1016 pl.race_penalty_accumulator += penalty;
1018 WRITESPECTATABLE_MSG_ONE({
1019 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1020 WriteByte(MSG_ONE, TE_CSQC_RACE);
1021 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1022 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1023 WriteString(MSG_ONE, reason);
1028 pl.race_penalty = time + penalty;
1030 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1031 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1032 WriteByte(MSG_ONE, TE_CSQC_RACE);
1033 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1034 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1035 WriteString(MSG_ONE, reason);
1040 void penalty_touch()
1043 if(other.race_lastpenalty != self)
1045 other.race_lastpenalty = self;
1046 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1052 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1055 void spawnfunc_trigger_race_penalty()
1059 self.use = penalty_use;
1060 if not(self.spawnflags & 1)
1061 self.touch = penalty_touch;
1063 if (!self.race_penalty_reason)
1064 self.race_penalty_reason = "missing a checkpoint";
1065 if (!self.race_penalty)
1066 self.race_penalty = 5;
1069 float race_GetFractionalLapCount(entity e)
1071 // interesting metrics (idea by KrimZon) to maybe sort players in the
1072 // scoreboard, immediately updates when overtaking
1074 // requires the track to be built so you never get farther away from the
1075 // next checkpoint, though, and current Xonotic race maps are not built that
1078 // also, this code is slow and would need optimization (i.e. "next CP"
1079 // links on CP entities)
1082 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1083 if(e.race_completed)
1084 return l; // not fractional
1087 float bestfraction, fraction;
1088 entity lastcp, cp0, cp1;
1089 float nextcpindex, lastcpindex;
1091 nextcpindex = max(e.race_checkpoint, 0);
1092 lastcpindex = e.race_respawn_checkpoint;
1093 lastcp = e.race_respawn_spotref;
1095 if(nextcpindex == lastcpindex)
1099 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1101 if(cp0.race_checkpoint != lastcpindex)
1106 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1107 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1109 if(cp1.race_checkpoint != nextcpindex)
1111 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1114 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1115 if(fraction < bestfraction)
1116 bestfraction = fraction;
1120 // we are at CP "nextcpindex - bestfraction"
1121 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1122 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1124 nc = race_highest_checkpoint + 1;
1125 c = (mod(nextcpindex - race_timed_checkpoint + nc + nc - 1, nc) + 1) - bestfraction;