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"
158 Send_KillNotification(e.netname, TIME_ENCODED_TOSTRING(t), "", RACE_FAIL, MSG_RACE);
160 } else if (!newpos) { // no ranking, time worse than the worst ranked
161 recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - race_readTime(GetMapname(), RANKINGS_CNT)), "]");
162 bprint(mynetname, "^7 couldn't break the ", race_placeName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(race_readTime(GetMapname(), RANKINGS_CNT)), recorddifference, "\n");
163 race_SendStatus(0, e); // "fail"
164 Send_KillNotification(e.netname, TIME_ENCODED_TOSTRING(t), "", RACE_FAIL, MSG_RACE);
168 // if we didn't hit a return yet, we have a new record!
170 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
173 bprint(mynetname, "^1 scored a new record with ^7", TIME_ENCODED_TOSTRING(t), "^1, but lacks a UID, so the record will unfortunately be lost.\n");
177 oldrec = race_readTime(GetMapname(), newpos);
178 oldrec_holder = race_readName(GetMapname(), newpos);
181 race_writeTime(GetMapname(), t, myuid);
184 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
185 race_send_recordtime(MSG_ALL);
188 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
190 strunzone(rankings_reply);
191 rankings_reply = strzone(getrankings());
193 if(newpos == player_prevpos) {
194 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
195 bprint(mynetname, "^1 improved their 1st place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
196 } else if (oldrec == 0) {
197 bprint(mynetname, "^1 set the 1st place record with ", TIME_ENCODED_TOSTRING(t), "\n");
199 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
200 bprint(mynetname, "^1 broke ", oldrec_holder, "^1's 1st place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
202 race_SendStatus(3, e); // "new server record"
203 Send_KillNotification(e.netname, TIME_ENCODED_TOSTRING(t), "", RACE_SERVER_RECORD, MSG_RACE);
205 if(newpos == player_prevpos) {
206 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
207 bprint(mynetname, "^5 improved their ", race_placeName(newpos), " ^5place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
208 race_SendStatus(1, e); // "new time"
209 Send_KillNotification(e.netname, TIME_ENCODED_TOSTRING(t), "", RACE_NEW_TIME, MSG_RACE);
210 } else if (oldrec == 0) {
211 bprint(mynetname, "^2 set the ", race_placeName(newpos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
212 race_SendStatus(2, e); // "new rank"
213 Send_KillNotification(e.netname, TIME_ENCODED_TOSTRING(t), "", RACE_NEW_RANK, MSG_RACE);
215 recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
216 bprint(mynetname, "^2 broke ", oldrec_holder, "^2's ", race_placeName(newpos), " ^2place record with ", strcat(TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
217 race_SendStatus(2, e); // "new rank"
218 Send_KillNotification(e.netname, TIME_ENCODED_TOSTRING(t), "", RACE_NEW_TIME, MSG_RACE);
223 void race_deleteTime(string map, float pos) {
231 for (i = pos; i <= RANKINGS_CNT; ++i) {
232 if (i == RANKINGS_CNT) {
233 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
234 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
237 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
238 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
242 race_SendRankings(pos, 0, 1, MSG_ALL);
244 race_send_recordtime(MSG_ALL);
247 strunzone(rankings_reply);
248 rankings_reply = strzone(getrankings());
251 void race_SendTime(entity e, float cp, float t, float tvalid)
256 if(g_race_qualifying)
257 t += e.race_penalty_accumulator;
259 t = TIME_ENCODE(t); // make integer
260 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
263 if(cp == race_timed_checkpoint) // finish line
264 if not(e.race_completed)
267 if(g_race_qualifying)
269 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
271 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
275 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
276 snew = TIME_ENCODE(time - game_starttime);
277 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
278 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
280 if(autocvar_fraglimit)
281 if(l >= autocvar_fraglimit)
282 race_StartCompleting();
286 e.race_completed = 1;
287 MAKE_INDEPENDENT_PLAYER(e);
288 bprint(e.netname, "^7 has finished the race.\n");
296 if(g_race_qualifying)
300 recordtime = race_checkpoint_records[cp];
301 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
302 if(recordholder == e.netname)
306 if(cp == race_timed_checkpoint)
308 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
309 if(g_cts && autocvar_g_cts_finish_kill_delay)
314 if(t < recordtime || recordtime == 0)
316 race_checkpoint_records[cp] = t;
317 if(race_checkpoint_recordholders[cp])
318 strunzone(race_checkpoint_recordholders[cp]);
319 race_checkpoint_recordholders[cp] = strzone(e.netname);
320 if(g_race_qualifying)
322 FOR_EACH_REALPLAYER(p)
323 if(p.race_checkpoint == cp)
324 race_SendNextCheckpoint(p, 0);
338 if(g_race_qualifying)
340 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
341 WriteByte(MSG_ONE, SVC_TEMPENTITY);
342 WriteByte(MSG_ONE, TE_CSQC_RACE);
343 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
344 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
345 WriteInt24_t(MSG_ONE, t); // time to that intermediate
346 WriteInt24_t(MSG_ONE, recordtime); // previously best time
347 WriteString(MSG_ONE, recordholder); // record holder
351 else // RACE! Not Qualifying
353 float lself, lother, othtime;
355 oth = race_checkpoint_lastplayers[cp];
358 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
359 lother = race_checkpoint_lastlaps[cp];
360 othtime = race_checkpoint_lasttimes[cp];
363 lself = lother = othtime = 0;
366 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
367 WriteByte(MSG_ONE, SVC_TEMPENTITY);
368 WriteByte(MSG_ONE, TE_CSQC_RACE);
369 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
370 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
373 WriteInt24_t(MSG_ONE, 0);
374 WriteByte(MSG_ONE, 0);
375 WriteString(MSG_ONE, "");
379 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
380 WriteByte(MSG_ONE, lself - lother);
381 WriteString(MSG_ONE, oth.netname); // record holder
385 race_checkpoint_lastplayers[cp] = e;
386 race_checkpoint_lasttimes[cp] = time;
387 race_checkpoint_lastlaps[cp] = lself;
390 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
391 WriteByte(MSG_ONE, SVC_TEMPENTITY);
392 WriteByte(MSG_ONE, TE_CSQC_RACE);
393 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
394 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
397 WriteInt24_t(MSG_ONE, 0);
398 WriteByte(MSG_ONE, 0);
399 WriteString(MSG_ONE, "");
403 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
404 WriteByte(MSG_ONE, lother - lself);
405 WriteString(MSG_ONE, e.netname); // record holder
411 void race_ClearTime(entity e)
413 e.race_checkpoint = 0;
415 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
416 e.race_penalty_accumulator = 0;
417 e.race_lastpenalty = world;
420 WRITESPECTATABLE_MSG_ONE({
421 WriteByte(MSG_ONE, SVC_TEMPENTITY);
422 WriteByte(MSG_ONE, TE_CSQC_RACE);
423 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
427 void dumpsurface(entity e)
431 print("Surfaces of ", etos(e), ":\n");
433 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
437 n = getsurfacenumpoints(e, si);
440 print(" Surface ", ftos(si), ":\n");
441 norm = getsurfacenormal(e, si);
442 print(" Normal = ", vtos(norm), "\n");
443 for(ni = 0; ni < n; ++ni)
445 vec = getsurfacepoint(e, si, ni);
446 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
451 void checkpoint_passed()
456 if(other.classname == "porto")
458 // do not allow portalling through checkpoints
459 trace_plane_normal = normalize(-1 * other.velocity);
468 if not((self.spawnflags & 2) && (other.classname == "player"))
471 oldmsg = self.message;
474 self.message = oldmsg;
477 if(other.classname != "player")
481 * Remove unauthorized equipment
483 Portal_ClearAll(other);
485 other.porto_forbidden = 2; // decreased by 1 each StartFrame
488 if(self.race_checkpoint == -2)
490 self.race_checkpoint = other.race_checkpoint;
495 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
497 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
499 largest_cp_id = cp.race_checkpoint;
500 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
501 cp.race_checkpoint = largest_cp_id + 1; // finish line
502 race_highest_checkpoint = largest_cp_id + 1;
503 race_timed_checkpoint = largest_cp_id + 1;
505 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
506 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
512 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
513 cp.race_checkpoint = 1;
514 race_highest_checkpoint = 1;
515 race_timed_checkpoint = 1;
519 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
521 if(self.race_penalty)
523 if(other.race_lastpenalty != self)
525 other.race_lastpenalty = self;
526 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
530 if(other.race_penalty)
536 if(self.spawnflags & 2)
539 oldmsg = self.message;
542 self.message = oldmsg;
545 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
546 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
547 other.race_respawn_checkpoint = self.race_checkpoint;
548 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
549 other.race_started = 1;
551 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
553 if(!self.race_checkpoint) // start line
555 other.race_laptime = time;
556 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
557 other.race_penalty_accumulator = 0;
558 other.race_lastpenalty = world;
561 if(g_race_qualifying)
562 race_SendNextCheckpoint(other, 0);
564 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
567 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
570 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
571 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
576 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
582 if(self.spawnflags & 4)
583 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
587 void checkpoint_touch()
593 void checkpoint_use()
595 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
602 float race_waypointsprite_visible_for_player(entity e)
604 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
606 else if(e.race_checkpoint == self.owner.race_checkpoint)
613 void trigger_race_checkpoint_verify()
623 qual = g_race_qualifying;
627 self.classname = "player";
631 for(i = 0; i <= race_highest_checkpoint; ++i)
633 self.race_checkpoint = race_NextCheckpoint(i);
635 // race only (middle of the race)
636 g_race_qualifying = 0;
638 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
639 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
644 g_race_qualifying = 1;
645 self.race_place = race_lowest_place_spawn;
646 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
647 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
649 // race only (initial spawn)
650 g_race_qualifying = 0;
651 for(p = 1; p <= race_highest_place_spawn; ++p)
654 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
655 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
660 else if(!defrag_ents)
663 self.race_checkpoint = race_NextCheckpoint(0);
664 g_race_qualifying = 1;
665 self.race_place = race_lowest_place_spawn;
666 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
667 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
671 self.race_checkpoint = race_NextCheckpoint(0);
672 g_race_qualifying = 1;
673 self.race_place = 0; // there's only one spawn on defrag maps
675 // check if a defragcp file already exists, then read it and apply the checkpoint order
680 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
683 while((l = fgets(fh)))
685 len = tokenize_console(l);
687 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
690 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
691 if(argv(0) == cp.targetname)
692 cp.race_checkpoint = stof(argv(1));
698 g_race_qualifying = qual;
700 if(race_timed_checkpoint) {
702 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
703 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
704 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
705 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
707 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
708 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
712 if(defragcpexists != -1){
714 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
715 if(cp.race_checkpoint > largest_cp_id)
716 largest_cp_id = cp.race_checkpoint;
717 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
718 cp.race_checkpoint = largest_cp_id + 1; // finish line
719 race_highest_checkpoint = largest_cp_id + 1;
720 race_timed_checkpoint = largest_cp_id + 1;
722 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
723 cp.race_checkpoint = 255; // finish line
724 race_highest_checkpoint = 255;
725 race_timed_checkpoint = 255;
729 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
732 if(cp.race_checkpoint == 0)
733 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
734 else if(cp.race_checkpoint == race_timed_checkpoint)
735 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
741 entity trigger, targ;
742 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
743 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
744 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
750 // These just make the game crash on some maps with oddly shaped triggers.
751 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
752 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
753 // (happened on item triggers too)
758 //setsize(targ, trigger.mins, trigger.maxs);
759 //setorigin(targ, trigger.origin);
767 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
769 if(g_race_qualifying)
772 if(self.race_checkpoint != 0)
774 if(spot.race_place != race_lowest_place_spawn)
779 if(self.race_checkpoint != player.race_respawn_checkpoint)
781 // try reusing the previous spawn
782 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
783 current_x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
784 if(self.race_checkpoint == 0)
787 pl = player.race_place;
788 if(pl > race_highest_place_spawn)
790 if(pl == 0 && !player.race_started)
791 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
792 if(spot.race_place != pl)
799 void spawnfunc_trigger_race_checkpoint()
802 if(!g_race && !g_cts)
810 self.use = checkpoint_use;
811 if not(self.spawnflags & 1)
812 self.touch = checkpoint_touch;
814 o = (self.absmin + self.absmax) * 0.5;
815 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
816 waypoint_spawnforitem_force(self, trace_endpos);
817 self.nearestwaypointtimeout = time + 1000000000;
820 self.message = "went backwards";
822 self.message2 = "was pushed backwards by";
823 if (!self.race_penalty_reason)
824 self.race_penalty_reason = "missing a checkpoint";
826 self.race_checkpoint = self.cnt;
828 if(self.race_checkpoint > race_highest_checkpoint)
830 race_highest_checkpoint = self.race_checkpoint;
831 if(self.spawnflags & 8)
832 race_timed_checkpoint = self.race_checkpoint;
834 race_timed_checkpoint = 0;
837 if(!self.race_penalty)
839 if(self.race_checkpoint)
840 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
842 WaypointSprite_SpawnFixed("race-start-finish", o, self, sprite, RADARICON_NONE, '1 0.5 0');
845 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
846 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
848 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
851 void spawnfunc_target_checkpoint() // defrag entity
854 if(!g_race && !g_cts)
863 self.use = checkpoint_use;
864 if not(self.spawnflags & 1)
865 self.touch = checkpoint_touch;
867 o = (self.absmin + self.absmax) * 0.5;
868 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
869 waypoint_spawnforitem_force(self, trace_endpos);
870 self.nearestwaypointtimeout = time + 1000000000;
873 self.message = "went backwards";
875 self.message2 = "was pushed backwards by";
876 if (!self.race_penalty_reason)
877 self.race_penalty_reason = "missing a checkpoint";
879 if(self.classname == "target_startTimer")
880 self.race_checkpoint = 0;
882 self.race_checkpoint = -2;
884 race_timed_checkpoint = 1;
886 if(self.race_checkpoint == 0)
887 WaypointSprite_SpawnFixed("race-start", o, self, sprite, RADARICON_NONE, '1 0.5 0');
889 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
891 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
893 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
896 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
897 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
899 void race_AbandonRaceCheck(entity p)
901 if(race_completing && !p.race_completed)
903 p.race_completed = 1;
904 MAKE_INDEPENDENT_PLAYER(p);
905 bprint(p.netname, "^7 has abandoned the race.\n");
910 void race_StartCompleting()
915 if(p.deadflag != DEAD_NO)
916 race_AbandonRaceCheck(p);
919 void race_PreparePlayer()
921 race_ClearTime(self);
923 self.race_started = 0;
924 self.race_respawn_checkpoint = 0;
925 self.race_respawn_spotref = world;
928 void race_RetractPlayer()
930 if(!g_race && !g_cts)
932 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
933 race_ClearTime(self);
934 self.race_checkpoint = self.race_respawn_checkpoint;
939 if(!g_race && !g_cts)
942 race_AbandonRaceCheck(self);
947 if(!g_race && !g_cts)
949 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
950 race_PreparePlayer();
952 race_RetractPlayer();
954 race_AbandonRaceCheck(self);
957 void race_PostSpawn(entity spot)
959 if(!g_race && !g_cts)
962 if(spot.target == "")
963 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
964 race_PreparePlayer();
966 // if we need to respawn, do it right
967 self.race_respawn_checkpoint = self.race_checkpoint;
968 self.race_respawn_spotref = spot;
973 void race_PreSpawnObserver()
975 if(!g_race && !g_cts)
977 race_PreparePlayer();
978 self.race_checkpoint = -1;
981 void spawnfunc_info_player_race (void)
983 if(!g_race && !g_cts)
989 spawnfunc_info_player_deathmatch();
991 if(self.race_place > race_highest_place_spawn)
992 race_highest_place_spawn = self.race_place;
993 if(self.race_place < race_lowest_place_spawn)
994 race_lowest_place_spawn = self.race_place;
997 void race_ClearRecords()
1002 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1004 race_checkpoint_records[i] = 0;
1005 if(race_checkpoint_recordholders[i])
1006 strunzone(race_checkpoint_recordholders[i]);
1007 race_checkpoint_recordholders[i] = string_null;
1011 FOR_EACH_CLIENT(self)
1014 p = self.race_place;
1015 race_PreparePlayer();
1016 self.race_place = p;
1021 void race_ReadyRestart()
1025 Score_NicePrint(world);
1027 race_ClearRecords();
1028 PlayerScore_Sort(race_place);
1035 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1039 print(e.netname, " = ", ftos(e.race_place), "\n");
1042 if(g_race_qualifying == 2)
1044 g_race_qualifying = 0;
1045 independent_players = 0;
1046 cvar_set("fraglimit", ftos(race_fraglimit));
1047 cvar_set("leadlimit", ftos(race_leadlimit));
1048 cvar_set("timelimit", ftos(race_timelimit));
1053 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1055 if(g_race_qualifying)
1057 pl.race_penalty_accumulator += penalty;
1059 WRITESPECTATABLE_MSG_ONE({
1060 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1061 WriteByte(MSG_ONE, TE_CSQC_RACE);
1062 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1063 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1064 WriteString(MSG_ONE, reason);
1069 pl.race_penalty = time + penalty;
1071 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1072 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1073 WriteByte(MSG_ONE, TE_CSQC_RACE);
1074 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1075 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1076 WriteString(MSG_ONE, reason);
1081 void penalty_touch()
1084 if(other.race_lastpenalty != self)
1086 other.race_lastpenalty = self;
1087 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1093 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1096 void spawnfunc_trigger_race_penalty()
1100 self.use = penalty_use;
1101 if not(self.spawnflags & 1)
1102 self.touch = penalty_touch;
1104 if (!self.race_penalty_reason)
1105 self.race_penalty_reason = "missing a checkpoint";
1106 if (!self.race_penalty)
1107 self.race_penalty = 5;
1110 float race_GetFractionalLapCount(entity e)
1112 // interesting metrics (idea by KrimZon) to maybe sort players in the
1113 // scoreboard, immediately updates when overtaking
1115 // requires the track to be built so you never get farther away from the
1116 // next checkpoint, though, and current Xonotic race maps are not built that
1119 // also, this code is slow and would need optimization (i.e. "next CP"
1120 // links on CP entities)
1123 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1124 if(e.race_completed)
1125 return l; // not fractional
1128 float bestfraction, fraction;
1129 entity lastcp, cp0, cp1;
1130 float nextcpindex, lastcpindex;
1132 nextcpindex = max(e.race_checkpoint, 0);
1133 lastcpindex = e.race_respawn_checkpoint;
1134 lastcp = e.race_respawn_spotref;
1136 if(nextcpindex == lastcpindex)
1140 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1142 if(cp0.race_checkpoint != lastcpindex)
1147 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1148 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1150 if(cp1.race_checkpoint != nextcpindex)
1152 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1155 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1156 if(fraction < bestfraction)
1157 bestfraction = fraction;
1161 // we are at CP "nextcpindex - bestfraction"
1162 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1163 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1165 nc = race_highest_checkpoint + 1;
1166 c = (mod(nextcpindex - race_timed_checkpoint + nc + nc - 1, nc) + 1) - bestfraction;