2 #include "../common/triggers/subs.qh"
4 void W_Porto_Fail(float failhard);
6 float race_readTime(string map, float pos)
8 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
10 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
13 string race_readUID(string map, float pos)
15 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
17 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
20 float race_readPos(string map, float t)
23 for (i = 1; i <= RANKINGS_CNT; ++i)
24 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
27 return 0; // pos is zero if unranked
30 void race_writeTime(string map, float t, string myuid)
32 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
35 newpos = race_readPos(map, t);
38 for(i = 1; i <= RANKINGS_CNT; ++i)
40 if(race_readUID(map, i) == myuid)
45 // player improved his existing record, only have to iterate on ranks between new and old recs
46 for (i = prevpos; i > newpos; --i)
48 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
49 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
54 // player has no ranked record yet
55 for (i = RANKINGS_CNT; i > newpos; --i)
57 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
58 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
62 // store new time itself
63 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
64 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
67 string race_readName(string map, float pos)
69 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
71 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
75 const float MAX_CHECKPOINTS = 255;
77 void spawnfunc_target_checkpoint();
80 .float race_penalty_accumulator;
81 .string race_penalty_reason;
82 .float race_checkpoint; // player: next checkpoint that has to be reached
84 .entity race_lastpenalty;
88 float race_checkpoint_records[MAX_CHECKPOINTS];
89 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
90 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
91 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
92 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
94 float race_highest_checkpoint;
95 float race_timed_checkpoint;
100 float race_NextCheckpoint(float f)
102 if(f >= race_highest_checkpoint)
108 float race_PreviousCheckpoint(float f)
113 return race_highest_checkpoint;
119 // 0 = common start/finish
122 float race_CheckpointNetworkID(float f)
124 if(race_timed_checkpoint)
128 else if(f == race_timed_checkpoint)
129 return 255; // finish
134 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
143 cp = e.race_checkpoint;
144 recordtime = race_checkpoint_records[cp];
145 recordholder = race_checkpoint_recordholders[cp];
146 if(recordholder == e.netname)
149 if(!IS_REAL_CLIENT(e))
154 WRITESPECTATABLE_MSG_ONE({
155 WriteByte(MSG_ONE, SVC_TEMPENTITY);
156 WriteByte(MSG_ONE, TE_CSQC_RACE);
159 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
160 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
161 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
164 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
165 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
166 WriteInt24_t(MSG_ONE, recordtime);
167 WriteString(MSG_ONE, recordholder);
171 void race_send_recordtime(float msg)
173 // send the server best time
174 WriteByte(msg, SVC_TEMPENTITY);
175 WriteByte(msg, TE_CSQC_RACE);
176 WriteByte(msg, RACE_NET_SERVER_RECORD);
177 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
180 void race_SendRankings(float pos, float prevpos, float del, float msg)
182 WriteByte(msg, SVC_TEMPENTITY);
183 WriteByte(msg, TE_CSQC_RACE);
184 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
185 WriteShort(msg, pos);
186 WriteShort(msg, prevpos);
187 WriteShort(msg, del);
188 WriteString(msg, race_readName(GetMapname(), pos));
189 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
192 void race_SendStatus(float id, entity e)
194 if(!IS_REAL_CLIENT(e))
203 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
204 WriteByte(msg, SVC_TEMPENTITY);
205 WriteByte(msg, TE_CSQC_RACE);
206 WriteByte(msg, RACE_NET_SERVER_STATUS);
208 WriteString(msg, e.netname);
212 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
214 // netname only used TEMPORARILY for printing
215 float newpos, player_prevpos;
216 newpos = race_readPos(map, t);
220 for(i = 1; i <= RANKINGS_CNT; ++i)
222 if(race_readUID(map, i) == myuid)
227 string oldrec_holder;
228 if (player_prevpos && (player_prevpos < newpos || !newpos))
230 oldrec = race_readTime(GetMapname(), player_prevpos);
231 race_SendStatus(0, e); // "fail"
232 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
237 // no ranking, time worse than the worst ranked
238 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
239 race_SendStatus(0, e); // "fail"
240 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
244 // if we didn't hit a return yet, we have a new record!
246 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
249 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
253 oldrec = race_readTime(GetMapname(), newpos);
254 oldrec_holder = race_readName(GetMapname(), newpos);
257 race_writeTime(GetMapname(), t, myuid);
261 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
262 race_send_recordtime(MSG_ALL);
265 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
267 strunzone(rankings_reply);
268 rankings_reply = strzone(getrankings());
270 if(newpos == player_prevpos)
272 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
273 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
274 else { race_SendStatus(1, e); } // "new time"
278 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
279 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
280 else { race_SendStatus(2, e); } // "new rank"
284 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
285 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
286 else { race_SendStatus(2, e); } // "new rank"
290 void race_deleteTime(string map, float pos)
299 for (i = pos; i <= RANKINGS_CNT; ++i)
301 if (i == RANKINGS_CNT)
303 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
304 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
308 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
309 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
313 race_SendRankings(pos, 0, 1, MSG_ALL);
315 race_send_recordtime(MSG_ALL);
318 strunzone(rankings_reply);
319 rankings_reply = strzone(getrankings());
322 void race_SendTime(entity e, float cp, float t, float tvalid)
327 if(g_race_qualifying)
328 t += e.race_penalty_accumulator;
330 t = TIME_ENCODE(t); // make integer
331 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
334 if(cp == race_timed_checkpoint) // finish line
335 if (!e.race_completed)
338 if(g_race_qualifying)
340 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
342 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
346 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
348 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
350 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
351 snew = TIME_ENCODE(time - game_starttime);
352 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
353 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
355 if(autocvar_fraglimit)
356 if(l >= autocvar_fraglimit)
357 race_StartCompleting();
361 e.race_completed = 1;
362 MAKE_INDEPENDENT_PLAYER(e);
363 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
371 if(g_race_qualifying)
375 recordtime = race_checkpoint_records[cp];
376 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
377 if(recordholder == e.netname)
382 if(cp == race_timed_checkpoint)
384 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
385 if(g_cts && autocvar_g_cts_finish_kill_delay)
390 if(t < recordtime || recordtime == 0)
392 race_checkpoint_records[cp] = t;
393 if(race_checkpoint_recordholders[cp])
394 strunzone(race_checkpoint_recordholders[cp]);
395 race_checkpoint_recordholders[cp] = strzone(e.netname);
396 if(g_race_qualifying)
398 FOR_EACH_REALPLAYER(p)
399 if(p.race_checkpoint == cp)
400 race_SendNextCheckpoint(p, 0);
413 if(IS_REAL_CLIENT(e))
416 if(g_race_qualifying)
418 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
419 WriteByte(MSG_ONE, SVC_TEMPENTITY);
420 WriteByte(MSG_ONE, TE_CSQC_RACE);
421 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
422 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
423 WriteInt24_t(MSG_ONE, t); // time to that intermediate
424 WriteInt24_t(MSG_ONE, recordtime); // previously best time
425 WriteString(MSG_ONE, recordholder); // record holder
430 else // RACE! Not Qualifying
432 float lself, lother, othtime;
434 oth = race_checkpoint_lastplayers[cp];
437 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
438 lother = race_checkpoint_lastlaps[cp];
439 othtime = race_checkpoint_lasttimes[cp];
442 lself = lother = othtime = 0;
444 if(IS_REAL_CLIENT(e))
447 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
448 WriteByte(MSG_ONE, SVC_TEMPENTITY);
449 WriteByte(MSG_ONE, TE_CSQC_RACE);
450 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
451 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
454 WriteInt24_t(MSG_ONE, 0);
455 WriteByte(MSG_ONE, 0);
456 WriteString(MSG_ONE, "");
460 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
461 WriteByte(MSG_ONE, lself - lother);
462 WriteString(MSG_ONE, oth.netname); // record holder
467 race_checkpoint_lastplayers[cp] = e;
468 race_checkpoint_lasttimes[cp] = time;
469 race_checkpoint_lastlaps[cp] = lself;
471 if(IS_REAL_CLIENT(oth))
474 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
475 WriteByte(MSG_ONE, SVC_TEMPENTITY);
476 WriteByte(MSG_ONE, TE_CSQC_RACE);
477 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
478 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
481 WriteInt24_t(MSG_ONE, 0);
482 WriteByte(MSG_ONE, 0);
483 WriteString(MSG_ONE, "");
487 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
488 WriteByte(MSG_ONE, lother - lself);
489 WriteString(MSG_ONE, e.netname); // record holder
496 void race_ClearTime(entity e)
498 e.race_checkpoint = 0;
500 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
501 e.race_penalty_accumulator = 0;
502 e.race_lastpenalty = world;
504 if(!IS_REAL_CLIENT(e))
508 WRITESPECTATABLE_MSG_ONE({
509 WriteByte(MSG_ONE, SVC_TEMPENTITY);
510 WriteByte(MSG_ONE, TE_CSQC_RACE);
511 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
515 void dumpsurface(entity e)
519 print("Surfaces of ", etos(e), ":\n");
521 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
525 n = getsurfacenumpoints(e, si);
528 print(" Surface ", ftos(si), ":\n");
529 norm = getsurfacenormal(e, si);
530 print(" Normal = ", vtos(norm), "\n");
531 for(ni = 0; ni < n; ++ni)
533 vec = getsurfacepoint(e, si, ni);
534 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
539 void checkpoint_passed()
544 if(other.classname == "porto")
546 // do not allow portalling through checkpoints
547 trace_plane_normal = normalize(-1 * other.velocity);
556 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
559 oldmsg = self.message;
562 self.message = oldmsg;
565 if (!IS_PLAYER(other))
569 * Remove unauthorized equipment
571 Portal_ClearAll(other);
573 other.porto_forbidden = 2; // decreased by 1 each StartFrame
577 if(self.race_checkpoint == -2)
579 self.race_checkpoint = other.race_checkpoint;
582 float largest_cp_id = 0;
584 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
587 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
589 largest_cp_id = cp.race_checkpoint;
590 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
591 cp.race_checkpoint = largest_cp_id + 1; // finish line
592 race_highest_checkpoint = largest_cp_id + 1;
593 race_timed_checkpoint = largest_cp_id + 1;
595 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
597 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
604 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
605 cp.race_checkpoint = 1;
606 race_highest_checkpoint = 1;
607 race_timed_checkpoint = 1;
611 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
613 if(self.race_penalty)
615 if(other.race_lastpenalty != self)
617 other.race_lastpenalty = self;
618 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
622 if(other.race_penalty)
628 if(self.spawnflags & 2)
631 oldmsg = self.message;
634 self.message = oldmsg;
637 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
638 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
639 other.race_respawn_checkpoint = self.race_checkpoint;
640 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
641 other.race_started = 1;
643 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
645 if(!self.race_checkpoint) // start line
647 other.race_laptime = time;
648 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
649 other.race_penalty_accumulator = 0;
650 other.race_lastpenalty = world;
653 if(g_race_qualifying)
654 race_SendNextCheckpoint(other, 0);
656 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
659 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
662 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
663 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
668 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
674 if(self.spawnflags & 4)
675 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
679 void checkpoint_touch()
685 void checkpoint_use()
687 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
694 float race_waypointsprite_visible_for_player(entity e)
696 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
698 else if(e.race_checkpoint == self.owner.race_checkpoint)
705 void trigger_race_checkpoint_verify()
715 qual = g_race_qualifying;
719 self.classname = "player";
723 for(i = 0; i <= race_highest_checkpoint; ++i)
725 self.race_checkpoint = race_NextCheckpoint(i);
727 // race only (middle of the race)
728 g_race_qualifying = 0;
730 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
731 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
736 g_race_qualifying = 1;
737 self.race_place = race_lowest_place_spawn;
738 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
739 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
741 // race only (initial spawn)
742 g_race_qualifying = 0;
743 for(p = 1; p <= race_highest_place_spawn; ++p)
746 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
747 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
752 else if(!defrag_ents)
755 self.race_checkpoint = race_NextCheckpoint(0);
756 g_race_qualifying = 1;
757 self.race_place = race_lowest_place_spawn;
758 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
759 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
763 self.race_checkpoint = race_NextCheckpoint(0);
764 g_race_qualifying = 1;
765 self.race_place = 0; // there's only one spawn on defrag maps
767 // check if a defragcp file already exists, then read it and apply the checkpoint order
772 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
775 while((l = fgets(fh)))
777 len = tokenize_console(l);
780 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
783 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
784 if(argv(0) == cp.targetname)
785 cp.race_checkpoint = stof(argv(1));
791 g_race_qualifying = qual;
793 if(race_timed_checkpoint)
797 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
798 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
799 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
800 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
802 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
804 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
808 if(defragcpexists != -1)
810 float largest_cp_id = 0;
811 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
812 if(cp.race_checkpoint > largest_cp_id)
813 largest_cp_id = cp.race_checkpoint;
814 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
815 cp.race_checkpoint = largest_cp_id + 1; // finish line
816 race_highest_checkpoint = largest_cp_id + 1;
817 race_timed_checkpoint = largest_cp_id + 1;
821 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
822 cp.race_checkpoint = 255; // finish line
823 race_highest_checkpoint = 255;
824 race_timed_checkpoint = 255;
829 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
832 if(cp.race_checkpoint == 0)
833 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
834 else if(cp.race_checkpoint == race_timed_checkpoint)
835 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
842 entity trigger, targ;
843 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
844 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
845 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
852 // These just make the game crash on some maps with oddly shaped triggers.
853 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
854 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
855 // (happened on item triggers too)
860 //setsize(targ, trigger.mins, trigger.maxs);
861 //setorigin(targ, trigger.origin);
869 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
871 if(g_race_qualifying)
874 if(self.race_checkpoint != 0)
876 if(spot.race_place != race_lowest_place_spawn)
881 if(self.race_checkpoint != player.race_respawn_checkpoint)
883 // try reusing the previous spawn
884 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
885 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
886 if(self.race_checkpoint == 0)
889 pl = player.race_place;
890 if(pl > race_highest_place_spawn)
892 if(pl == 0 && !player.race_started)
893 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
894 if(spot.race_place != pl)
901 void spawnfunc_trigger_race_checkpoint()
904 if(!g_race && !g_cts) { remove(self); return; }
908 self.use = checkpoint_use;
909 if (!(self.spawnflags & 1))
910 self.touch = checkpoint_touch;
912 o = (self.absmin + self.absmax) * 0.5;
913 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
914 waypoint_spawnforitem_force(self, trace_endpos);
915 self.nearestwaypointtimeout = time + 1000000000;
917 if(self.message == "")
918 self.message = "went backwards";
919 if (self.message2 == "")
920 self.message2 = "was pushed backwards by";
921 if (self.race_penalty_reason == "")
922 self.race_penalty_reason = "missing a checkpoint";
924 self.race_checkpoint = self.cnt;
926 if(self.race_checkpoint > race_highest_checkpoint)
928 race_highest_checkpoint = self.race_checkpoint;
929 if(self.spawnflags & 8)
930 race_timed_checkpoint = self.race_checkpoint;
932 race_timed_checkpoint = 0;
935 if(!self.race_penalty)
937 if(self.race_checkpoint)
938 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
940 WaypointSprite_SpawnFixed("race-start-finish", o, self, sprite, RADARICON_NONE, '1 0.5 0');
943 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
944 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
946 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
949 void spawnfunc_target_checkpoint() // defrag entity
952 if(!g_race && !g_cts) { remove(self); return; }
957 self.use = checkpoint_use;
958 if (!(self.spawnflags & 1))
959 self.touch = checkpoint_touch;
961 o = (self.absmin + self.absmax) * 0.5;
962 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
963 waypoint_spawnforitem_force(self, trace_endpos);
964 self.nearestwaypointtimeout = time + 1000000000;
966 if(self.message == "")
967 self.message = "went backwards";
968 if (self.message2 == "")
969 self.message2 = "was pushed backwards by";
970 if (self.race_penalty_reason == "")
971 self.race_penalty_reason = "missing a checkpoint";
973 if(self.classname == "target_startTimer")
974 self.race_checkpoint = 0;
976 self.race_checkpoint = -2;
978 race_timed_checkpoint = 1;
980 if(self.race_checkpoint == 0)
981 WaypointSprite_SpawnFixed("race-start", o, self, sprite, RADARICON_NONE, '1 0.5 0');
983 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
985 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
987 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
990 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
991 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
993 void race_AbandonRaceCheck(entity p)
995 if(race_completing && !p.race_completed)
997 p.race_completed = 1;
998 MAKE_INDEPENDENT_PLAYER(p);
999 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1000 ClientData_Touch(p);
1004 void race_StartCompleting()
1007 race_completing = 1;
1009 if(p.deadflag != DEAD_NO)
1010 race_AbandonRaceCheck(p);
1013 void race_PreparePlayer()
1015 race_ClearTime(self);
1016 self.race_place = 0;
1017 self.race_started = 0;
1018 self.race_respawn_checkpoint = 0;
1019 self.race_respawn_spotref = world;
1022 void race_RetractPlayer()
1024 if(!g_race && !g_cts)
1026 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1027 race_ClearTime(self);
1028 self.race_checkpoint = self.race_respawn_checkpoint;
1031 void spawnfunc_info_player_race (void)
1033 if(!g_race && !g_cts) { remove(self); return; }
1035 spawnfunc_info_player_deathmatch();
1037 if(self.race_place > race_highest_place_spawn)
1038 race_highest_place_spawn = self.race_place;
1039 if(self.race_place < race_lowest_place_spawn)
1040 race_lowest_place_spawn = self.race_place;
1043 void race_ClearRecords()
1048 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1050 race_checkpoint_records[i] = 0;
1051 if(race_checkpoint_recordholders[i])
1052 strunzone(race_checkpoint_recordholders[i]);
1053 race_checkpoint_recordholders[i] = string_null;
1057 FOR_EACH_CLIENT(self)
1060 p = self.race_place;
1061 race_PreparePlayer();
1062 self.race_place = p;
1067 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1069 if(g_race_qualifying)
1071 pl.race_penalty_accumulator += penalty;
1072 if(IS_REAL_CLIENT(pl))
1075 WRITESPECTATABLE_MSG_ONE({
1076 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1077 WriteByte(MSG_ONE, TE_CSQC_RACE);
1078 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1079 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1080 WriteString(MSG_ONE, reason);
1086 pl.race_penalty = time + penalty;
1087 if(IS_REAL_CLIENT(pl))
1090 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1091 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1092 WriteByte(MSG_ONE, TE_CSQC_RACE);
1093 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1094 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1095 WriteString(MSG_ONE, reason);
1101 void penalty_touch()
1104 if(other.race_lastpenalty != self)
1106 other.race_lastpenalty = self;
1107 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1113 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1116 void spawnfunc_trigger_race_penalty()
1120 self.use = penalty_use;
1121 if (!(self.spawnflags & 1))
1122 self.touch = penalty_touch;
1124 if (self.race_penalty_reason == "")
1125 self.race_penalty_reason = "missing a checkpoint";
1126 if (!self.race_penalty)
1127 self.race_penalty = 5;
1130 float race_GetFractionalLapCount(entity e)
1132 // interesting metrics (idea by KrimZon) to maybe sort players in the
1133 // scoreboard, immediately updates when overtaking
1135 // requires the track to be built so you never get farther away from the
1136 // next checkpoint, though, and current Xonotic race maps are not built that
1139 // also, this code is slow and would need optimization (i.e. "next CP"
1140 // links on CP entities)
1143 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1144 if(e.race_completed)
1145 return l; // not fractional
1148 float bestfraction, fraction;
1149 entity lastcp, cp0, cp1;
1150 float nextcpindex, lastcpindex;
1152 nextcpindex = max(e.race_checkpoint, 0);
1153 lastcpindex = e.race_respawn_checkpoint;
1154 lastcp = e.race_respawn_spotref;
1156 if(nextcpindex == lastcpindex)
1160 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1162 if(cp0.race_checkpoint != lastcpindex)
1167 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1168 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1170 if(cp1.race_checkpoint != nextcpindex)
1172 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1175 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1176 if(fraction < bestfraction)
1177 bestfraction = fraction;
1181 // we are at CP "nextcpindex - bestfraction"
1182 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1183 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1185 nc = race_highest_checkpoint + 1;
1186 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;