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);
145 for(i = 1; i <= RANKINGS_CNT; ++i)
147 if(race_readUID(map, i) == myuid)
152 string oldrec_holder;
153 if (player_prevpos && (player_prevpos < newpos || !newpos))
155 oldrec = race_readTime(GetMapname(), player_prevpos);
156 race_SendStatus(0, e); // "fail"
157 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
159 } else if (!newpos) { // no ranking, time worse than the worst ranked
160 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
161 race_SendStatus(0, e); // "fail"
162 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
166 // if we didn't hit a return yet, we have a new record!
168 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
171 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
175 oldrec = race_readTime(GetMapname(), newpos);
176 oldrec_holder = race_readName(GetMapname(), newpos);
179 race_writeTime(GetMapname(), t, myuid);
182 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
183 race_send_recordtime(MSG_ALL);
186 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
188 strunzone(rankings_reply);
189 rankings_reply = strzone(getrankings());
191 if(newpos == player_prevpos)
193 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
194 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
195 else { race_SendStatus(1, e); } // "new time"
199 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
200 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
201 else { race_SendStatus(2, e); } // "new rank"
205 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
206 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
207 else { race_SendStatus(2, e); } // "new rank"
211 void race_deleteTime(string map, float pos) {
219 for (i = pos; i <= RANKINGS_CNT; ++i) {
220 if (i == RANKINGS_CNT) {
221 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
222 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
225 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
226 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
230 race_SendRankings(pos, 0, 1, MSG_ALL);
232 race_send_recordtime(MSG_ALL);
235 strunzone(rankings_reply);
236 rankings_reply = strzone(getrankings());
239 void race_SendTime(entity e, float cp, float t, float tvalid)
244 if(g_race_qualifying)
245 t += e.race_penalty_accumulator;
247 t = TIME_ENCODE(t); // make integer
248 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
251 if(cp == race_timed_checkpoint) // finish line
252 if not(e.race_completed)
255 if(g_race_qualifying)
257 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
259 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
263 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
265 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
267 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
268 snew = TIME_ENCODE(time - game_starttime);
269 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
270 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
272 if(autocvar_fraglimit)
273 if(l >= autocvar_fraglimit)
274 race_StartCompleting();
278 e.race_completed = 1;
279 MAKE_INDEPENDENT_PLAYER(e);
280 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
288 if(g_race_qualifying)
292 recordtime = race_checkpoint_records[cp];
293 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
294 if(recordholder == e.netname)
298 if(cp == race_timed_checkpoint)
300 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
301 if(g_cts && autocvar_g_cts_finish_kill_delay)
306 if(t < recordtime || recordtime == 0)
308 race_checkpoint_records[cp] = t;
309 if(race_checkpoint_recordholders[cp])
310 strunzone(race_checkpoint_recordholders[cp]);
311 race_checkpoint_recordholders[cp] = strzone(e.netname);
312 if(g_race_qualifying)
314 FOR_EACH_REALPLAYER(p)
315 if(p.race_checkpoint == cp)
316 race_SendNextCheckpoint(p, 0);
330 if(g_race_qualifying)
332 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
333 WriteByte(MSG_ONE, SVC_TEMPENTITY);
334 WriteByte(MSG_ONE, TE_CSQC_RACE);
335 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
336 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
337 WriteInt24_t(MSG_ONE, t); // time to that intermediate
338 WriteInt24_t(MSG_ONE, recordtime); // previously best time
339 WriteString(MSG_ONE, recordholder); // record holder
343 else // RACE! Not Qualifying
345 float lself, lother, othtime;
347 oth = race_checkpoint_lastplayers[cp];
350 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
351 lother = race_checkpoint_lastlaps[cp];
352 othtime = race_checkpoint_lasttimes[cp];
355 lself = lother = othtime = 0;
358 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
359 WriteByte(MSG_ONE, SVC_TEMPENTITY);
360 WriteByte(MSG_ONE, TE_CSQC_RACE);
361 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
362 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
365 WriteInt24_t(MSG_ONE, 0);
366 WriteByte(MSG_ONE, 0);
367 WriteString(MSG_ONE, "");
371 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
372 WriteByte(MSG_ONE, lself - lother);
373 WriteString(MSG_ONE, oth.netname); // record holder
377 race_checkpoint_lastplayers[cp] = e;
378 race_checkpoint_lasttimes[cp] = time;
379 race_checkpoint_lastlaps[cp] = lself;
382 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
383 WriteByte(MSG_ONE, SVC_TEMPENTITY);
384 WriteByte(MSG_ONE, TE_CSQC_RACE);
385 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
386 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
389 WriteInt24_t(MSG_ONE, 0);
390 WriteByte(MSG_ONE, 0);
391 WriteString(MSG_ONE, "");
395 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
396 WriteByte(MSG_ONE, lother - lself);
397 WriteString(MSG_ONE, e.netname); // record holder
403 void race_ClearTime(entity e)
405 e.race_checkpoint = 0;
407 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
408 e.race_penalty_accumulator = 0;
409 e.race_lastpenalty = world;
412 WRITESPECTATABLE_MSG_ONE({
413 WriteByte(MSG_ONE, SVC_TEMPENTITY);
414 WriteByte(MSG_ONE, TE_CSQC_RACE);
415 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
419 void dumpsurface(entity e)
423 print("Surfaces of ", etos(e), ":\n");
425 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
429 n = getsurfacenumpoints(e, si);
432 print(" Surface ", ftos(si), ":\n");
433 norm = getsurfacenormal(e, si);
434 print(" Normal = ", vtos(norm), "\n");
435 for(ni = 0; ni < n; ++ni)
437 vec = getsurfacepoint(e, si, ni);
438 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
443 void checkpoint_passed()
448 if(other.classname == "porto")
450 // do not allow portalling through checkpoints
451 trace_plane_normal = normalize(-1 * other.velocity);
460 if not((self.spawnflags & 2) && (other.classname == "player"))
463 oldmsg = self.message;
466 self.message = oldmsg;
469 if(other.classname != "player")
473 * Remove unauthorized equipment
475 Portal_ClearAll(other);
477 other.porto_forbidden = 2; // decreased by 1 each StartFrame
480 if(self.race_checkpoint == -2)
482 self.race_checkpoint = other.race_checkpoint;
485 float largest_cp_id = 0;
487 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
489 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
491 largest_cp_id = cp.race_checkpoint;
492 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
493 cp.race_checkpoint = largest_cp_id + 1; // finish line
494 race_highest_checkpoint = largest_cp_id + 1;
495 race_timed_checkpoint = largest_cp_id + 1;
497 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
498 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
504 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
505 cp.race_checkpoint = 1;
506 race_highest_checkpoint = 1;
507 race_timed_checkpoint = 1;
511 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
513 if(self.race_penalty)
515 if(other.race_lastpenalty != self)
517 other.race_lastpenalty = self;
518 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
522 if(other.race_penalty)
528 if(self.spawnflags & 2)
531 oldmsg = self.message;
534 self.message = oldmsg;
537 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
538 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
539 other.race_respawn_checkpoint = self.race_checkpoint;
540 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
541 other.race_started = 1;
543 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
545 if(!self.race_checkpoint) // start line
547 other.race_laptime = time;
548 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
549 other.race_penalty_accumulator = 0;
550 other.race_lastpenalty = world;
553 if(g_race_qualifying)
554 race_SendNextCheckpoint(other, 0);
556 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
559 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
562 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
563 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
568 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
574 if(self.spawnflags & 4)
575 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
579 void checkpoint_touch()
585 void checkpoint_use()
587 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
594 float race_waypointsprite_visible_for_player(entity e)
596 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
598 else if(e.race_checkpoint == self.owner.race_checkpoint)
605 void trigger_race_checkpoint_verify()
615 qual = g_race_qualifying;
619 self.classname = "player";
623 for(i = 0; i <= race_highest_checkpoint; ++i)
625 self.race_checkpoint = race_NextCheckpoint(i);
627 // race only (middle of the race)
628 g_race_qualifying = 0;
630 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
631 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
636 g_race_qualifying = 1;
637 self.race_place = race_lowest_place_spawn;
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 qualifying) - bailing out"));
641 // race only (initial spawn)
642 g_race_qualifying = 0;
643 for(p = 1; p <= race_highest_place_spawn; ++p)
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 initially spawning in race) - bailing out"));
652 else if(!defrag_ents)
655 self.race_checkpoint = race_NextCheckpoint(0);
656 g_race_qualifying = 1;
657 self.race_place = race_lowest_place_spawn;
658 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
659 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
663 self.race_checkpoint = race_NextCheckpoint(0);
664 g_race_qualifying = 1;
665 self.race_place = 0; // there's only one spawn on defrag maps
667 // check if a defragcp file already exists, then read it and apply the checkpoint order
672 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
675 while((l = fgets(fh)))
677 len = tokenize_console(l);
679 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
682 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
683 if(argv(0) == cp.targetname)
684 cp.race_checkpoint = stof(argv(1));
690 g_race_qualifying = qual;
692 if(race_timed_checkpoint) {
694 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
695 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
696 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
697 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
699 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
700 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
704 if(defragcpexists != -1){
705 float largest_cp_id = 0;
706 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
707 if(cp.race_checkpoint > largest_cp_id)
708 largest_cp_id = cp.race_checkpoint;
709 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
710 cp.race_checkpoint = largest_cp_id + 1; // finish line
711 race_highest_checkpoint = largest_cp_id + 1;
712 race_timed_checkpoint = largest_cp_id + 1;
714 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
715 cp.race_checkpoint = 255; // finish line
716 race_highest_checkpoint = 255;
717 race_timed_checkpoint = 255;
721 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
724 if(cp.race_checkpoint == 0)
725 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
726 else if(cp.race_checkpoint == race_timed_checkpoint)
727 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
733 entity trigger, targ;
734 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
735 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
736 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
742 // These just make the game crash on some maps with oddly shaped triggers.
743 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
744 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
745 // (happened on item triggers too)
750 //setsize(targ, trigger.mins, trigger.maxs);
751 //setorigin(targ, trigger.origin);
759 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
761 if(g_race_qualifying)
764 if(self.race_checkpoint != 0)
766 if(spot.race_place != race_lowest_place_spawn)
771 if(self.race_checkpoint != player.race_respawn_checkpoint)
773 // try reusing the previous spawn
774 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
775 current_x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
776 if(self.race_checkpoint == 0)
779 pl = player.race_place;
780 if(pl > race_highest_place_spawn)
782 if(pl == 0 && !player.race_started)
783 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
784 if(spot.race_place != pl)
791 void spawnfunc_trigger_race_checkpoint()
794 if(!g_race && !g_cts)
802 self.use = checkpoint_use;
803 if not(self.spawnflags & 1)
804 self.touch = checkpoint_touch;
806 o = (self.absmin + self.absmax) * 0.5;
807 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
808 waypoint_spawnforitem_force(self, trace_endpos);
809 self.nearestwaypointtimeout = time + 1000000000;
811 if(self.message == "")
812 self.message = "went backwards";
813 if (self.message2 == "")
814 self.message2 = "was pushed backwards by";
815 if (self.race_penalty_reason == "")
816 self.race_penalty_reason = "missing a checkpoint";
818 self.race_checkpoint = self.cnt;
820 if(self.race_checkpoint > race_highest_checkpoint)
822 race_highest_checkpoint = self.race_checkpoint;
823 if(self.spawnflags & 8)
824 race_timed_checkpoint = self.race_checkpoint;
826 race_timed_checkpoint = 0;
829 if(!self.race_penalty)
831 if(self.race_checkpoint)
832 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
834 WaypointSprite_SpawnFixed("race-start-finish", o, self, sprite, RADARICON_NONE, '1 0.5 0');
837 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
838 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
840 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
843 void spawnfunc_target_checkpoint() // defrag entity
846 if(!g_race && !g_cts)
855 self.use = checkpoint_use;
856 if not(self.spawnflags & 1)
857 self.touch = checkpoint_touch;
859 o = (self.absmin + self.absmax) * 0.5;
860 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
861 waypoint_spawnforitem_force(self, trace_endpos);
862 self.nearestwaypointtimeout = time + 1000000000;
864 if(self.message == "")
865 self.message = "went backwards";
866 if (self.message2 == "")
867 self.message2 = "was pushed backwards by";
868 if (self.race_penalty_reason == "")
869 self.race_penalty_reason = "missing a checkpoint";
871 if(self.classname == "target_startTimer")
872 self.race_checkpoint = 0;
874 self.race_checkpoint = -2;
876 race_timed_checkpoint = 1;
878 if(self.race_checkpoint == 0)
879 WaypointSprite_SpawnFixed("race-start", o, self, sprite, RADARICON_NONE, '1 0.5 0');
881 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
883 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
885 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
888 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
889 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
891 void race_AbandonRaceCheck(entity p)
893 if(race_completing && !p.race_completed)
895 p.race_completed = 1;
896 MAKE_INDEPENDENT_PLAYER(p);
897 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
902 void race_StartCompleting()
907 if(p.deadflag != DEAD_NO)
908 race_AbandonRaceCheck(p);
911 void race_PreparePlayer()
913 race_ClearTime(self);
915 self.race_started = 0;
916 self.race_respawn_checkpoint = 0;
917 self.race_respawn_spotref = world;
920 void race_RetractPlayer()
922 if(!g_race && !g_cts)
924 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
925 race_ClearTime(self);
926 self.race_checkpoint = self.race_respawn_checkpoint;
931 if(!g_race && !g_cts)
934 race_AbandonRaceCheck(self);
939 if(!g_race && !g_cts)
941 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
942 race_PreparePlayer();
944 race_RetractPlayer();
946 race_AbandonRaceCheck(self);
949 void race_PostSpawn(entity spot)
951 if(!g_race && !g_cts)
954 if(spot.target == "")
955 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
956 race_PreparePlayer();
958 // if we need to respawn, do it right
959 self.race_respawn_checkpoint = self.race_checkpoint;
960 self.race_respawn_spotref = spot;
965 void race_PreSpawnObserver()
967 if(!g_race && !g_cts)
969 race_PreparePlayer();
970 self.race_checkpoint = -1;
973 void spawnfunc_info_player_race (void)
975 if(!g_race && !g_cts)
981 spawnfunc_info_player_deathmatch();
983 if(self.race_place > race_highest_place_spawn)
984 race_highest_place_spawn = self.race_place;
985 if(self.race_place < race_lowest_place_spawn)
986 race_lowest_place_spawn = self.race_place;
989 void race_ClearRecords()
994 for(i = 0; i < MAX_CHECKPOINTS; ++i)
996 race_checkpoint_records[i] = 0;
997 if(race_checkpoint_recordholders[i])
998 strunzone(race_checkpoint_recordholders[i]);
999 race_checkpoint_recordholders[i] = string_null;
1003 FOR_EACH_CLIENT(self)
1006 p = self.race_place;
1007 race_PreparePlayer();
1008 self.race_place = p;
1013 void race_ReadyRestart()
1017 Score_NicePrint(world);
1019 race_ClearRecords();
1020 PlayerScore_Sort(race_place, 0, 1, 0);
1027 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1031 print(e.netname, " = ", ftos(e.race_place), "\n");
1034 if(g_race_qualifying == 2)
1036 g_race_qualifying = 0;
1037 independent_players = 0;
1038 cvar_set("fraglimit", ftos(race_fraglimit));
1039 cvar_set("leadlimit", ftos(race_leadlimit));
1040 cvar_set("timelimit", ftos(race_timelimit));
1045 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1047 if(g_race_qualifying)
1049 pl.race_penalty_accumulator += penalty;
1051 WRITESPECTATABLE_MSG_ONE({
1052 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1053 WriteByte(MSG_ONE, TE_CSQC_RACE);
1054 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1055 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1056 WriteString(MSG_ONE, reason);
1061 pl.race_penalty = time + penalty;
1063 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1064 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1065 WriteByte(MSG_ONE, TE_CSQC_RACE);
1066 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1067 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1068 WriteString(MSG_ONE, reason);
1073 void penalty_touch()
1076 if(other.race_lastpenalty != self)
1078 other.race_lastpenalty = self;
1079 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1085 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1088 void spawnfunc_trigger_race_penalty()
1092 self.use = penalty_use;
1093 if not(self.spawnflags & 1)
1094 self.touch = penalty_touch;
1096 if (self.race_penalty_reason == "")
1097 self.race_penalty_reason = "missing a checkpoint";
1098 if (!self.race_penalty)
1099 self.race_penalty = 5;
1102 float race_GetFractionalLapCount(entity e)
1104 // interesting metrics (idea by KrimZon) to maybe sort players in the
1105 // scoreboard, immediately updates when overtaking
1107 // requires the track to be built so you never get farther away from the
1108 // next checkpoint, though, and current Xonotic race maps are not built that
1111 // also, this code is slow and would need optimization (i.e. "next CP"
1112 // links on CP entities)
1115 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1116 if(e.race_completed)
1117 return l; // not fractional
1120 float bestfraction, fraction;
1121 entity lastcp, cp0, cp1;
1122 float nextcpindex, lastcpindex;
1124 nextcpindex = max(e.race_checkpoint, 0);
1125 lastcpindex = e.race_respawn_checkpoint;
1126 lastcp = e.race_respawn_spotref;
1128 if(nextcpindex == lastcpindex)
1132 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1134 if(cp0.race_checkpoint != lastcpindex)
1139 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1140 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1142 if(cp1.race_checkpoint != nextcpindex)
1144 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1147 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1148 if(fraction < bestfraction)
1149 bestfraction = fraction;
1153 // we are at CP "nextcpindex - bestfraction"
1154 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1155 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1157 nc = race_highest_checkpoint + 1;
1158 c = (mod(nextcpindex - race_timed_checkpoint + nc + nc - 1, nc) + 1) - bestfraction;