1 float race_readTime(string map, float pos)
3 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
5 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
8 string race_readUID(string map, float pos)
10 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
12 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
15 float race_readPos(string map, float t)
18 for (i = 1; i <= RANKINGS_CNT; ++i)
19 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
22 return 0; // pos is zero if unranked
25 void race_writeTime(string map, float t, string myuid)
27 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
30 newpos = race_readPos(map, t);
33 for(i = 1; i <= RANKINGS_CNT; ++i)
35 if(race_readUID(map, i) == myuid)
40 // player improved his existing record, only have to iterate on ranks between new and old recs
41 for (i = prevpos; i > newpos; --i)
43 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
44 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
49 // player has no ranked record yet
50 for (i = RANKINGS_CNT; i > newpos; --i)
52 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
53 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
57 // store new time itself
58 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
59 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
62 string race_readName(string map, float pos)
64 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
66 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
70 #define MAX_CHECKPOINTS 255
72 void spawnfunc_target_checkpoint();
75 .float race_penalty_accumulator;
76 .string race_penalty_reason;
77 .float race_checkpoint; // player: next checkpoint that has to be reached
79 .entity race_lastpenalty;
83 float race_checkpoint_records[MAX_CHECKPOINTS];
84 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
85 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
86 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
87 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
89 float race_highest_checkpoint;
90 float race_timed_checkpoint;
95 float race_NextCheckpoint(float f)
97 if(f >= race_highest_checkpoint)
103 float race_PreviousCheckpoint(float f)
108 return race_highest_checkpoint;
114 // 0 = common start/finish
117 float race_CheckpointNetworkID(float f)
119 if(race_timed_checkpoint)
123 else if(f == race_timed_checkpoint)
124 return 255; // finish
129 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
138 cp = e.race_checkpoint;
139 recordtime = race_checkpoint_records[cp];
140 recordholder = race_checkpoint_recordholders[cp];
141 if(recordholder == e.netname)
146 WRITESPECTATABLE_MSG_ONE({
147 WriteByte(MSG_ONE, SVC_TEMPENTITY);
148 WriteByte(MSG_ONE, TE_CSQC_RACE);
151 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
152 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
153 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
156 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
157 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
158 WriteInt24_t(MSG_ONE, recordtime);
159 WriteString(MSG_ONE, recordholder);
163 void race_send_recordtime(float msg)
165 // send the server best time
166 WriteByte(msg, SVC_TEMPENTITY);
167 WriteByte(msg, TE_CSQC_RACE);
168 WriteByte(msg, RACE_NET_SERVER_RECORD);
169 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
172 void race_SendRankings(float pos, float prevpos, float del, float msg)
174 WriteByte(msg, SVC_TEMPENTITY);
175 WriteByte(msg, TE_CSQC_RACE);
176 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
177 WriteShort(msg, pos);
178 WriteShort(msg, prevpos);
179 WriteShort(msg, del);
180 WriteString(msg, race_readName(GetMapname(), pos));
181 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
184 void race_SendStatus(float id, entity e)
192 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
193 WriteByte(msg, SVC_TEMPENTITY);
194 WriteByte(msg, TE_CSQC_RACE);
195 WriteByte(msg, RACE_NET_SERVER_STATUS);
197 WriteString(msg, e.netname);
201 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
203 // netname only used TEMPORARILY for printing
204 float newpos, player_prevpos;
205 newpos = race_readPos(map, t);
209 for(i = 1; i <= RANKINGS_CNT; ++i)
211 if(race_readUID(map, i) == myuid)
216 string oldrec_holder;
217 if (player_prevpos && (player_prevpos < newpos || !newpos))
219 oldrec = race_readTime(GetMapname(), player_prevpos);
220 race_SendStatus(0, e); // "fail"
221 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
226 // no ranking, time worse than the worst ranked
227 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
228 race_SendStatus(0, e); // "fail"
229 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
233 // if we didn't hit a return yet, we have a new record!
235 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
238 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
242 oldrec = race_readTime(GetMapname(), newpos);
243 oldrec_holder = race_readName(GetMapname(), newpos);
246 race_writeTime(GetMapname(), t, myuid);
250 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
251 race_send_recordtime(MSG_ALL);
254 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
256 strunzone(rankings_reply);
257 rankings_reply = strzone(getrankings());
259 if(newpos == player_prevpos)
261 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
262 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
263 else { race_SendStatus(1, e); } // "new time"
267 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
268 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
269 else { race_SendStatus(2, e); } // "new rank"
273 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
274 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
275 else { race_SendStatus(2, e); } // "new rank"
279 void race_deleteTime(string map, float pos)
288 for (i = pos; i <= RANKINGS_CNT; ++i)
290 if (i == RANKINGS_CNT)
292 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
293 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
297 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
298 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
302 race_SendRankings(pos, 0, 1, MSG_ALL);
304 race_send_recordtime(MSG_ALL);
307 strunzone(rankings_reply);
308 rankings_reply = strzone(getrankings());
311 void race_SendTime(entity e, float cp, float t, float tvalid)
316 if(g_race_qualifying)
317 t += e.race_penalty_accumulator;
319 t = TIME_ENCODE(t); // make integer
320 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
323 if(cp == race_timed_checkpoint) // finish line
324 if (!e.race_completed)
327 if(g_race_qualifying)
329 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
331 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
335 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
337 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
339 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
340 snew = TIME_ENCODE(time - game_starttime);
341 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
342 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
344 if(autocvar_fraglimit)
345 if(l >= autocvar_fraglimit)
346 race_StartCompleting();
350 e.race_completed = 1;
351 MAKE_INDEPENDENT_PLAYER(e);
352 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
360 if(g_race_qualifying)
364 recordtime = race_checkpoint_records[cp];
365 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
366 if(recordholder == e.netname)
371 if(cp == race_timed_checkpoint)
373 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
374 if(g_cts && autocvar_g_cts_finish_kill_delay)
379 if(t < recordtime || recordtime == 0)
381 race_checkpoint_records[cp] = t;
382 if(race_checkpoint_recordholders[cp])
383 strunzone(race_checkpoint_recordholders[cp]);
384 race_checkpoint_recordholders[cp] = strzone(e.netname);
385 if(g_race_qualifying)
387 FOR_EACH_REALPLAYER(p)
388 if(p.race_checkpoint == cp)
389 race_SendNextCheckpoint(p, 0);
403 if(g_race_qualifying)
405 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
406 WriteByte(MSG_ONE, SVC_TEMPENTITY);
407 WriteByte(MSG_ONE, TE_CSQC_RACE);
408 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
409 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
410 WriteInt24_t(MSG_ONE, t); // time to that intermediate
411 WriteInt24_t(MSG_ONE, recordtime); // previously best time
412 WriteString(MSG_ONE, recordholder); // record holder
416 else // RACE! Not Qualifying
418 float lself, lother, othtime;
420 oth = race_checkpoint_lastplayers[cp];
423 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
424 lother = race_checkpoint_lastlaps[cp];
425 othtime = race_checkpoint_lasttimes[cp];
428 lself = lother = othtime = 0;
431 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
432 WriteByte(MSG_ONE, SVC_TEMPENTITY);
433 WriteByte(MSG_ONE, TE_CSQC_RACE);
434 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
435 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
438 WriteInt24_t(MSG_ONE, 0);
439 WriteByte(MSG_ONE, 0);
440 WriteString(MSG_ONE, "");
444 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
445 WriteByte(MSG_ONE, lself - lother);
446 WriteString(MSG_ONE, oth.netname); // record holder
450 race_checkpoint_lastplayers[cp] = e;
451 race_checkpoint_lasttimes[cp] = time;
452 race_checkpoint_lastlaps[cp] = lself;
455 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
456 WriteByte(MSG_ONE, SVC_TEMPENTITY);
457 WriteByte(MSG_ONE, TE_CSQC_RACE);
458 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
459 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
462 WriteInt24_t(MSG_ONE, 0);
463 WriteByte(MSG_ONE, 0);
464 WriteString(MSG_ONE, "");
468 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
469 WriteByte(MSG_ONE, lother - lself);
470 WriteString(MSG_ONE, e.netname); // record holder
476 void race_ClearTime(entity e)
478 e.race_checkpoint = 0;
480 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
481 e.race_penalty_accumulator = 0;
482 e.race_lastpenalty = world;
485 WRITESPECTATABLE_MSG_ONE({
486 WriteByte(MSG_ONE, SVC_TEMPENTITY);
487 WriteByte(MSG_ONE, TE_CSQC_RACE);
488 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
492 void dumpsurface(entity e)
496 print("Surfaces of ", etos(e), ":\n");
498 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
502 n = getsurfacenumpoints(e, si);
505 print(" Surface ", ftos(si), ":\n");
506 norm = getsurfacenormal(e, si);
507 print(" Normal = ", vtos(norm), "\n");
508 for(ni = 0; ni < n; ++ni)
510 vec = getsurfacepoint(e, si, ni);
511 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
516 void checkpoint_passed()
521 if(other.classname == "porto")
523 // do not allow portalling through checkpoints
524 trace_plane_normal = normalize(-1 * other.velocity);
533 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
536 oldmsg = self.message;
539 self.message = oldmsg;
542 if (!IS_PLAYER(other))
546 * Remove unauthorized equipment
548 Portal_ClearAll(other);
550 other.porto_forbidden = 2; // decreased by 1 each StartFrame
554 if(self.race_checkpoint == -2)
556 self.race_checkpoint = other.race_checkpoint;
559 float largest_cp_id = 0;
561 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
564 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
566 largest_cp_id = cp.race_checkpoint;
567 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
568 cp.race_checkpoint = largest_cp_id + 1; // finish line
569 race_highest_checkpoint = largest_cp_id + 1;
570 race_timed_checkpoint = largest_cp_id + 1;
572 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
574 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
581 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
582 cp.race_checkpoint = 1;
583 race_highest_checkpoint = 1;
584 race_timed_checkpoint = 1;
588 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
590 if(self.race_penalty)
592 if(other.race_lastpenalty != self)
594 other.race_lastpenalty = self;
595 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
599 if(other.race_penalty)
605 if(self.spawnflags & 2)
608 oldmsg = self.message;
611 self.message = oldmsg;
614 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
615 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
616 other.race_respawn_checkpoint = self.race_checkpoint;
617 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
618 other.race_started = 1;
620 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
622 if(!self.race_checkpoint) // start line
624 other.race_laptime = time;
625 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
626 other.race_penalty_accumulator = 0;
627 other.race_lastpenalty = world;
630 if(g_race_qualifying)
631 race_SendNextCheckpoint(other, 0);
633 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
636 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
639 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
640 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
645 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
651 if(self.spawnflags & 4)
652 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
656 void checkpoint_touch()
662 void checkpoint_use()
664 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
671 float race_waypointsprite_visible_for_player(entity e)
673 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
675 else if(e.race_checkpoint == self.owner.race_checkpoint)
682 void trigger_race_checkpoint_verify()
692 qual = g_race_qualifying;
696 self.classname = "player";
700 for(i = 0; i <= race_highest_checkpoint; ++i)
702 self.race_checkpoint = race_NextCheckpoint(i);
704 // race only (middle of the race)
705 g_race_qualifying = 0;
707 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
708 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
713 g_race_qualifying = 1;
714 self.race_place = race_lowest_place_spawn;
715 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
716 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
718 // race only (initial spawn)
719 g_race_qualifying = 0;
720 for(p = 1; p <= race_highest_place_spawn; ++p)
723 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
724 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
729 else if(!defrag_ents)
732 self.race_checkpoint = race_NextCheckpoint(0);
733 g_race_qualifying = 1;
734 self.race_place = race_lowest_place_spawn;
735 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, FALSE))
736 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
740 self.race_checkpoint = race_NextCheckpoint(0);
741 g_race_qualifying = 1;
742 self.race_place = 0; // there's only one spawn on defrag maps
744 // check if a defragcp file already exists, then read it and apply the checkpoint order
749 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
752 while((l = fgets(fh)))
754 len = tokenize_console(l);
757 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
760 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
761 if(argv(0) == cp.targetname)
762 cp.race_checkpoint = stof(argv(1));
768 g_race_qualifying = qual;
770 if(race_timed_checkpoint)
774 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
775 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
776 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
777 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
779 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
781 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
785 if(defragcpexists != -1)
787 float largest_cp_id = 0;
788 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
789 if(cp.race_checkpoint > largest_cp_id)
790 largest_cp_id = cp.race_checkpoint;
791 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
792 cp.race_checkpoint = largest_cp_id + 1; // finish line
793 race_highest_checkpoint = largest_cp_id + 1;
794 race_timed_checkpoint = largest_cp_id + 1;
798 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
799 cp.race_checkpoint = 255; // finish line
800 race_highest_checkpoint = 255;
801 race_timed_checkpoint = 255;
806 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
809 if(cp.race_checkpoint == 0)
810 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
811 else if(cp.race_checkpoint == race_timed_checkpoint)
812 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
819 entity trigger, targ;
820 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
821 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
822 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
829 // These just make the game crash on some maps with oddly shaped triggers.
830 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
831 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
832 // (happened on item triggers too)
837 //setsize(targ, trigger.mins, trigger.maxs);
838 //setorigin(targ, trigger.origin);
846 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
848 if(g_race_qualifying)
851 if(self.race_checkpoint != 0)
853 if(spot.race_place != race_lowest_place_spawn)
858 if(self.race_checkpoint != player.race_respawn_checkpoint)
860 // try reusing the previous spawn
861 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
862 current_x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
863 if(self.race_checkpoint == 0)
866 pl = player.race_place;
867 if(pl > race_highest_place_spawn)
869 if(pl == 0 && !player.race_started)
870 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
871 if(spot.race_place != pl)
878 void spawnfunc_trigger_race_checkpoint()
881 if(!g_race && !g_cts) { remove(self); return; }
885 self.use = checkpoint_use;
886 if (!(self.spawnflags & 1))
887 self.touch = checkpoint_touch;
889 o = (self.absmin + self.absmax) * 0.5;
890 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
891 waypoint_spawnforitem_force(self, trace_endpos);
892 self.nearestwaypointtimeout = time + 1000000000;
894 if(self.message == "")
895 self.message = "went backwards";
896 if (self.message2 == "")
897 self.message2 = "was pushed backwards by";
898 if (self.race_penalty_reason == "")
899 self.race_penalty_reason = "missing a checkpoint";
901 self.race_checkpoint = self.cnt;
903 if(self.race_checkpoint > race_highest_checkpoint)
905 race_highest_checkpoint = self.race_checkpoint;
906 if(self.spawnflags & 8)
907 race_timed_checkpoint = self.race_checkpoint;
909 race_timed_checkpoint = 0;
912 if(!self.race_penalty)
914 if(self.race_checkpoint)
915 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
917 WaypointSprite_SpawnFixed("race-start-finish", o, self, sprite, RADARICON_NONE, '1 0.5 0');
920 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
921 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
923 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
926 void spawnfunc_target_checkpoint() // defrag entity
929 if(!g_race && !g_cts) { remove(self); return; }
934 self.use = checkpoint_use;
935 if (!(self.spawnflags & 1))
936 self.touch = checkpoint_touch;
938 o = (self.absmin + self.absmax) * 0.5;
939 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
940 waypoint_spawnforitem_force(self, trace_endpos);
941 self.nearestwaypointtimeout = time + 1000000000;
943 if(self.message == "")
944 self.message = "went backwards";
945 if (self.message2 == "")
946 self.message2 = "was pushed backwards by";
947 if (self.race_penalty_reason == "")
948 self.race_penalty_reason = "missing a checkpoint";
950 if(self.classname == "target_startTimer")
951 self.race_checkpoint = 0;
953 self.race_checkpoint = -2;
955 race_timed_checkpoint = 1;
957 if(self.race_checkpoint == 0)
958 WaypointSprite_SpawnFixed("race-start", o, self, sprite, RADARICON_NONE, '1 0.5 0');
960 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
962 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
964 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
967 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
968 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
970 void race_AbandonRaceCheck(entity p)
972 if(race_completing && !p.race_completed)
974 p.race_completed = 1;
975 MAKE_INDEPENDENT_PLAYER(p);
976 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
981 void race_StartCompleting()
986 if(p.deadflag != DEAD_NO)
987 race_AbandonRaceCheck(p);
990 void race_PreparePlayer()
992 race_ClearTime(self);
994 self.race_started = 0;
995 self.race_respawn_checkpoint = 0;
996 self.race_respawn_spotref = world;
999 void race_RetractPlayer()
1001 if(!g_race && !g_cts)
1003 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1004 race_ClearTime(self);
1005 self.race_checkpoint = self.race_respawn_checkpoint;
1008 void spawnfunc_info_player_race (void)
1010 if(!g_race && !g_cts) { remove(self); return; }
1012 spawnfunc_info_player_deathmatch();
1014 if(self.race_place > race_highest_place_spawn)
1015 race_highest_place_spawn = self.race_place;
1016 if(self.race_place < race_lowest_place_spawn)
1017 race_lowest_place_spawn = self.race_place;
1020 void race_ClearRecords()
1025 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1027 race_checkpoint_records[i] = 0;
1028 if(race_checkpoint_recordholders[i])
1029 strunzone(race_checkpoint_recordholders[i]);
1030 race_checkpoint_recordholders[i] = string_null;
1034 FOR_EACH_CLIENT(self)
1037 p = self.race_place;
1038 race_PreparePlayer();
1039 self.race_place = p;
1044 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1046 if(g_race_qualifying)
1048 pl.race_penalty_accumulator += penalty;
1050 WRITESPECTATABLE_MSG_ONE({
1051 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1052 WriteByte(MSG_ONE, TE_CSQC_RACE);
1053 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1054 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1055 WriteString(MSG_ONE, reason);
1060 pl.race_penalty = time + penalty;
1062 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1063 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1064 WriteByte(MSG_ONE, TE_CSQC_RACE);
1065 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1066 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1067 WriteString(MSG_ONE, reason);
1072 void penalty_touch()
1075 if(other.race_lastpenalty != self)
1077 other.race_lastpenalty = self;
1078 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1084 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1087 void spawnfunc_trigger_race_penalty()
1091 self.use = penalty_use;
1092 if (!(self.spawnflags & 1))
1093 self.touch = penalty_touch;
1095 if (self.race_penalty_reason == "")
1096 self.race_penalty_reason = "missing a checkpoint";
1097 if (!self.race_penalty)
1098 self.race_penalty = 5;
1101 float race_GetFractionalLapCount(entity e)
1103 // interesting metrics (idea by KrimZon) to maybe sort players in the
1104 // scoreboard, immediately updates when overtaking
1106 // requires the track to be built so you never get farther away from the
1107 // next checkpoint, though, and current Xonotic race maps are not built that
1110 // also, this code is slow and would need optimization (i.e. "next CP"
1111 // links on CP entities)
1114 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1115 if(e.race_completed)
1116 return l; // not fractional
1119 float bestfraction, fraction;
1120 entity lastcp, cp0, cp1;
1121 float nextcpindex, lastcpindex;
1123 nextcpindex = max(e.race_checkpoint, 0);
1124 lastcpindex = e.race_respawn_checkpoint;
1125 lastcp = e.race_respawn_spotref;
1127 if(nextcpindex == lastcpindex)
1131 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1133 if(cp0.race_checkpoint != lastcpindex)
1138 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1139 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1141 if(cp1.race_checkpoint != nextcpindex)
1143 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1146 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1147 if(fraction < bestfraction)
1148 bestfraction = fraction;
1152 // we are at CP "nextcpindex - bestfraction"
1153 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1154 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1156 nc = race_highest_checkpoint + 1;
1157 c = (mod(nextcpindex - race_timed_checkpoint + nc + nc - 1, nc) + 1) - bestfraction;