3 void W_Porto_Fail(float failhard);
5 float race_readTime(string map, float pos)
7 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
9 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
12 string race_readUID(string map, float pos)
14 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
16 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
19 float race_readPos(string map, float t)
22 for (i = 1; i <= RANKINGS_CNT; ++i)
23 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
26 return 0; // pos is zero if unranked
29 void race_writeTime(string map, float t, string myuid)
31 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
34 newpos = race_readPos(map, t);
37 for(i = 1; i <= RANKINGS_CNT; ++i)
39 if(race_readUID(map, i) == myuid)
44 // player improved his existing record, only have to iterate on ranks between new and old recs
45 for (i = prevpos; i > newpos; --i)
47 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
48 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
53 // player has no ranked record yet
54 for (i = RANKINGS_CNT; i > newpos; --i)
56 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
57 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
61 // store new time itself
62 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
63 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
66 string race_readName(string map, float pos)
68 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
70 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
74 const float MAX_CHECKPOINTS = 255;
76 void spawnfunc_target_checkpoint();
79 .float race_penalty_accumulator;
80 .string race_penalty_reason;
81 .float race_checkpoint; // player: next checkpoint that has to be reached
83 .entity race_lastpenalty;
87 float race_checkpoint_records[MAX_CHECKPOINTS];
88 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
89 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
90 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
91 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
93 float race_highest_checkpoint;
94 float race_timed_checkpoint;
99 float race_NextCheckpoint(float f)
101 if(f >= race_highest_checkpoint)
107 float race_PreviousCheckpoint(float f)
112 return race_highest_checkpoint;
118 // 0 = common start/finish
121 float race_CheckpointNetworkID(float f)
123 if(race_timed_checkpoint)
127 else if(f == race_timed_checkpoint)
128 return 255; // finish
133 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
142 cp = e.race_checkpoint;
143 recordtime = race_checkpoint_records[cp];
144 recordholder = race_checkpoint_recordholders[cp];
145 if(recordholder == e.netname)
148 if(!IS_REAL_CLIENT(e))
153 WRITESPECTATABLE_MSG_ONE({
154 WriteByte(MSG_ONE, SVC_TEMPENTITY);
155 WriteByte(MSG_ONE, TE_CSQC_RACE);
158 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
159 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
160 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
163 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
164 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
165 WriteInt24_t(MSG_ONE, recordtime);
166 WriteString(MSG_ONE, recordholder);
170 void race_send_recordtime(float msg)
172 // send the server best time
173 WriteByte(msg, SVC_TEMPENTITY);
174 WriteByte(msg, TE_CSQC_RACE);
175 WriteByte(msg, RACE_NET_SERVER_RECORD);
176 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
179 void race_SendRankings(float pos, float prevpos, float del, float msg)
181 WriteByte(msg, SVC_TEMPENTITY);
182 WriteByte(msg, TE_CSQC_RACE);
183 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
184 WriteShort(msg, pos);
185 WriteShort(msg, prevpos);
186 WriteShort(msg, del);
187 WriteString(msg, race_readName(GetMapname(), pos));
188 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
191 void race_SendStatus(float id, entity e)
193 if(!IS_REAL_CLIENT(e))
202 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
203 WriteByte(msg, SVC_TEMPENTITY);
204 WriteByte(msg, TE_CSQC_RACE);
205 WriteByte(msg, RACE_NET_SERVER_STATUS);
207 WriteString(msg, e.netname);
211 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
213 // netname only used TEMPORARILY for printing
214 float newpos, player_prevpos;
215 newpos = race_readPos(map, t);
219 for(i = 1; i <= RANKINGS_CNT; ++i)
221 if(race_readUID(map, i) == myuid)
226 string oldrec_holder;
227 if (player_prevpos && (player_prevpos < newpos || !newpos))
229 oldrec = race_readTime(GetMapname(), player_prevpos);
230 race_SendStatus(0, e); // "fail"
231 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
236 // no ranking, time worse than the worst ranked
237 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
238 race_SendStatus(0, e); // "fail"
239 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
243 // if we didn't hit a return yet, we have a new record!
245 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
248 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
252 oldrec = race_readTime(GetMapname(), newpos);
253 oldrec_holder = race_readName(GetMapname(), newpos);
256 race_writeTime(GetMapname(), t, myuid);
260 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
261 race_send_recordtime(MSG_ALL);
264 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
266 strunzone(rankings_reply);
267 rankings_reply = strzone(getrankings());
269 if(newpos == player_prevpos)
271 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
272 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
273 else { race_SendStatus(1, e); } // "new time"
277 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
278 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
279 else { race_SendStatus(2, e); } // "new rank"
283 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
284 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
285 else { race_SendStatus(2, e); } // "new rank"
289 void race_deleteTime(string map, float pos)
298 for (i = pos; i <= RANKINGS_CNT; ++i)
300 if (i == RANKINGS_CNT)
302 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
303 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
307 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
308 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
312 race_SendRankings(pos, 0, 1, MSG_ALL);
314 race_send_recordtime(MSG_ALL);
317 strunzone(rankings_reply);
318 rankings_reply = strzone(getrankings());
321 void race_SendTime(entity e, float cp, float t, float tvalid)
326 if(g_race_qualifying)
327 t += e.race_penalty_accumulator;
329 t = TIME_ENCODE(t); // make integer
330 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
333 if(cp == race_timed_checkpoint) // finish line
334 if (!e.race_completed)
337 if(g_race_qualifying)
339 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
341 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
345 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
347 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
349 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
350 snew = TIME_ENCODE(time - game_starttime);
351 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
352 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
354 if(autocvar_fraglimit)
355 if(l >= autocvar_fraglimit)
356 race_StartCompleting();
360 e.race_completed = 1;
361 MAKE_INDEPENDENT_PLAYER(e);
362 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
370 if(g_race_qualifying)
374 recordtime = race_checkpoint_records[cp];
375 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
376 if(recordholder == e.netname)
381 if(cp == race_timed_checkpoint)
383 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
384 if(g_cts && autocvar_g_cts_finish_kill_delay)
389 if(t < recordtime || recordtime == 0)
391 race_checkpoint_records[cp] = t;
392 if(race_checkpoint_recordholders[cp])
393 strunzone(race_checkpoint_recordholders[cp]);
394 race_checkpoint_recordholders[cp] = strzone(e.netname);
395 if(g_race_qualifying)
397 FOR_EACH_REALPLAYER(p)
398 if(p.race_checkpoint == cp)
399 race_SendNextCheckpoint(p, 0);
412 if(IS_REAL_CLIENT(e))
415 if(g_race_qualifying)
417 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
418 WriteByte(MSG_ONE, SVC_TEMPENTITY);
419 WriteByte(MSG_ONE, TE_CSQC_RACE);
420 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
421 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
422 WriteInt24_t(MSG_ONE, t); // time to that intermediate
423 WriteInt24_t(MSG_ONE, recordtime); // previously best time
424 WriteString(MSG_ONE, recordholder); // record holder
429 else // RACE! Not Qualifying
431 float lself, lother, othtime;
433 oth = race_checkpoint_lastplayers[cp];
436 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
437 lother = race_checkpoint_lastlaps[cp];
438 othtime = race_checkpoint_lasttimes[cp];
441 lself = lother = othtime = 0;
443 if(IS_REAL_CLIENT(e))
446 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
447 WriteByte(MSG_ONE, SVC_TEMPENTITY);
448 WriteByte(MSG_ONE, TE_CSQC_RACE);
449 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
450 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
453 WriteInt24_t(MSG_ONE, 0);
454 WriteByte(MSG_ONE, 0);
455 WriteString(MSG_ONE, "");
459 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
460 WriteByte(MSG_ONE, lself - lother);
461 WriteString(MSG_ONE, oth.netname); // record holder
466 race_checkpoint_lastplayers[cp] = e;
467 race_checkpoint_lasttimes[cp] = time;
468 race_checkpoint_lastlaps[cp] = lself;
470 if(IS_REAL_CLIENT(oth))
473 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
474 WriteByte(MSG_ONE, SVC_TEMPENTITY);
475 WriteByte(MSG_ONE, TE_CSQC_RACE);
476 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
477 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
480 WriteInt24_t(MSG_ONE, 0);
481 WriteByte(MSG_ONE, 0);
482 WriteString(MSG_ONE, "");
486 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
487 WriteByte(MSG_ONE, lother - lself);
488 WriteString(MSG_ONE, e.netname); // record holder
495 void race_ClearTime(entity e)
497 e.race_checkpoint = 0;
499 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
500 e.race_penalty_accumulator = 0;
501 e.race_lastpenalty = world;
503 if(!IS_REAL_CLIENT(e))
507 WRITESPECTATABLE_MSG_ONE({
508 WriteByte(MSG_ONE, SVC_TEMPENTITY);
509 WriteByte(MSG_ONE, TE_CSQC_RACE);
510 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
514 void dumpsurface(entity e)
518 print("Surfaces of ", etos(e), ":\n");
520 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
524 n = getsurfacenumpoints(e, si);
527 print(" Surface ", ftos(si), ":\n");
528 norm = getsurfacenormal(e, si);
529 print(" Normal = ", vtos(norm), "\n");
530 for(ni = 0; ni < n; ++ni)
532 vec = getsurfacepoint(e, si, ni);
533 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
538 void checkpoint_passed()
543 if(other.classname == "porto")
545 // do not allow portalling through checkpoints
546 trace_plane_normal = normalize(-1 * other.velocity);
555 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
558 oldmsg = self.message;
561 self.message = oldmsg;
564 if (!IS_PLAYER(other))
568 * Remove unauthorized equipment
570 Portal_ClearAll(other);
572 other.porto_forbidden = 2; // decreased by 1 each StartFrame
576 if(self.race_checkpoint == -2)
578 self.race_checkpoint = other.race_checkpoint;
581 float largest_cp_id = 0;
583 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
586 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
588 largest_cp_id = cp.race_checkpoint;
589 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
590 cp.race_checkpoint = largest_cp_id + 1; // finish line
591 race_highest_checkpoint = largest_cp_id + 1;
592 race_timed_checkpoint = largest_cp_id + 1;
594 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
596 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
603 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
604 cp.race_checkpoint = 1;
605 race_highest_checkpoint = 1;
606 race_timed_checkpoint = 1;
610 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
612 if(self.race_penalty)
614 if(other.race_lastpenalty != self)
616 other.race_lastpenalty = self;
617 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
621 if(other.race_penalty)
627 if(self.spawnflags & 2)
630 oldmsg = self.message;
633 self.message = oldmsg;
636 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
637 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
638 other.race_respawn_checkpoint = self.race_checkpoint;
639 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
640 other.race_started = 1;
642 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
644 if(!self.race_checkpoint) // start line
646 other.race_laptime = time;
647 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
648 other.race_penalty_accumulator = 0;
649 other.race_lastpenalty = world;
652 if(g_race_qualifying)
653 race_SendNextCheckpoint(other, 0);
655 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
658 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
661 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
662 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
667 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
673 if(self.spawnflags & 4)
674 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
678 void checkpoint_touch()
684 void checkpoint_use()
686 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
693 float race_waypointsprite_visible_for_player(entity e)
695 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
697 else if(e.race_checkpoint == self.owner.race_checkpoint)
704 void trigger_race_checkpoint_verify()
714 qual = g_race_qualifying;
718 self.classname = "player";
722 for(i = 0; i <= race_highest_checkpoint; ++i)
724 self.race_checkpoint = race_NextCheckpoint(i);
726 // race only (middle of the race)
727 g_race_qualifying = 0;
729 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
730 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
735 g_race_qualifying = 1;
736 self.race_place = race_lowest_place_spawn;
737 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
738 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
740 // race only (initial spawn)
741 g_race_qualifying = 0;
742 for(p = 1; p <= race_highest_place_spawn; ++p)
745 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
746 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
751 else if(!defrag_ents)
754 self.race_checkpoint = race_NextCheckpoint(0);
755 g_race_qualifying = 1;
756 self.race_place = race_lowest_place_spawn;
757 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
758 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
762 self.race_checkpoint = race_NextCheckpoint(0);
763 g_race_qualifying = 1;
764 self.race_place = 0; // there's only one spawn on defrag maps
766 // check if a defragcp file already exists, then read it and apply the checkpoint order
771 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
774 while((l = fgets(fh)))
776 len = tokenize_console(l);
779 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
782 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
783 if(argv(0) == cp.targetname)
784 cp.race_checkpoint = stof(argv(1));
790 g_race_qualifying = qual;
792 if(race_timed_checkpoint)
796 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
797 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
798 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
799 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
801 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
803 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
807 if(defragcpexists != -1)
809 float largest_cp_id = 0;
810 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
811 if(cp.race_checkpoint > largest_cp_id)
812 largest_cp_id = cp.race_checkpoint;
813 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
814 cp.race_checkpoint = largest_cp_id + 1; // finish line
815 race_highest_checkpoint = largest_cp_id + 1;
816 race_timed_checkpoint = largest_cp_id + 1;
820 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
821 cp.race_checkpoint = 255; // finish line
822 race_highest_checkpoint = 255;
823 race_timed_checkpoint = 255;
828 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
831 if(cp.race_checkpoint == 0)
832 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
833 else if(cp.race_checkpoint == race_timed_checkpoint)
834 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
841 entity trigger, targ;
842 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
843 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
844 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
851 // These just make the game crash on some maps with oddly shaped triggers.
852 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
853 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
854 // (happened on item triggers too)
859 //setsize(targ, trigger.mins, trigger.maxs);
860 //setorigin(targ, trigger.origin);
868 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
870 if(g_race_qualifying)
873 if(self.race_checkpoint != 0)
875 if(spot.race_place != race_lowest_place_spawn)
880 if(self.race_checkpoint != player.race_respawn_checkpoint)
882 // try reusing the previous spawn
883 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
884 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
885 if(self.race_checkpoint == 0)
888 pl = player.race_place;
889 if(pl > race_highest_place_spawn)
891 if(pl == 0 && !player.race_started)
892 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
893 if(spot.race_place != pl)
900 void spawnfunc_trigger_race_checkpoint()
903 if(!g_race && !g_cts) { remove(self); return; }
907 self.use = checkpoint_use;
908 if (!(self.spawnflags & 1))
909 self.touch = checkpoint_touch;
911 o = (self.absmin + self.absmax) * 0.5;
912 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
913 waypoint_spawnforitem_force(self, trace_endpos);
914 self.nearestwaypointtimeout = time + 1000000000;
916 if(self.message == "")
917 self.message = "went backwards";
918 if (self.message2 == "")
919 self.message2 = "was pushed backwards by";
920 if (self.race_penalty_reason == "")
921 self.race_penalty_reason = "missing a checkpoint";
923 self.race_checkpoint = self.cnt;
925 if(self.race_checkpoint > race_highest_checkpoint)
927 race_highest_checkpoint = self.race_checkpoint;
928 if(self.spawnflags & 8)
929 race_timed_checkpoint = self.race_checkpoint;
931 race_timed_checkpoint = 0;
934 if(!self.race_penalty)
936 if(self.race_checkpoint)
937 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
939 WaypointSprite_SpawnFixed("race-start-finish", o, self, sprite, RADARICON_NONE, '1 0.5 0');
942 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
943 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
945 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
948 void spawnfunc_target_checkpoint() // defrag entity
951 if(!g_race && !g_cts) { remove(self); return; }
956 self.use = checkpoint_use;
957 if (!(self.spawnflags & 1))
958 self.touch = checkpoint_touch;
960 o = (self.absmin + self.absmax) * 0.5;
961 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
962 waypoint_spawnforitem_force(self, trace_endpos);
963 self.nearestwaypointtimeout = time + 1000000000;
965 if(self.message == "")
966 self.message = "went backwards";
967 if (self.message2 == "")
968 self.message2 = "was pushed backwards by";
969 if (self.race_penalty_reason == "")
970 self.race_penalty_reason = "missing a checkpoint";
972 if(self.classname == "target_startTimer")
973 self.race_checkpoint = 0;
975 self.race_checkpoint = -2;
977 race_timed_checkpoint = 1;
979 if(self.race_checkpoint == 0)
980 WaypointSprite_SpawnFixed("race-start", o, self, sprite, RADARICON_NONE, '1 0.5 0');
982 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
984 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
986 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
989 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
990 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
992 void race_AbandonRaceCheck(entity p)
994 if(race_completing && !p.race_completed)
996 p.race_completed = 1;
997 MAKE_INDEPENDENT_PLAYER(p);
998 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1003 void race_StartCompleting()
1006 race_completing = 1;
1008 if(p.deadflag != DEAD_NO)
1009 race_AbandonRaceCheck(p);
1012 void race_PreparePlayer()
1014 race_ClearTime(self);
1015 self.race_place = 0;
1016 self.race_started = 0;
1017 self.race_respawn_checkpoint = 0;
1018 self.race_respawn_spotref = world;
1021 void race_RetractPlayer()
1023 if(!g_race && !g_cts)
1025 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1026 race_ClearTime(self);
1027 self.race_checkpoint = self.race_respawn_checkpoint;
1030 void spawnfunc_info_player_race (void)
1032 if(!g_race && !g_cts) { remove(self); return; }
1034 spawnfunc_info_player_deathmatch();
1036 if(self.race_place > race_highest_place_spawn)
1037 race_highest_place_spawn = self.race_place;
1038 if(self.race_place < race_lowest_place_spawn)
1039 race_lowest_place_spawn = self.race_place;
1042 void race_ClearRecords()
1047 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1049 race_checkpoint_records[i] = 0;
1050 if(race_checkpoint_recordholders[i])
1051 strunzone(race_checkpoint_recordholders[i]);
1052 race_checkpoint_recordholders[i] = string_null;
1056 FOR_EACH_CLIENT(self)
1059 p = self.race_place;
1060 race_PreparePlayer();
1061 self.race_place = p;
1066 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1068 if(g_race_qualifying)
1070 pl.race_penalty_accumulator += penalty;
1071 if(IS_REAL_CLIENT(pl))
1074 WRITESPECTATABLE_MSG_ONE({
1075 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1076 WriteByte(MSG_ONE, TE_CSQC_RACE);
1077 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1078 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1079 WriteString(MSG_ONE, reason);
1085 pl.race_penalty = time + penalty;
1086 if(IS_REAL_CLIENT(pl))
1089 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1090 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1091 WriteByte(MSG_ONE, TE_CSQC_RACE);
1092 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1093 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1094 WriteString(MSG_ONE, reason);
1100 void penalty_touch()
1103 if(other.race_lastpenalty != self)
1105 other.race_lastpenalty = self;
1106 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1112 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1115 void spawnfunc_trigger_race_penalty()
1119 self.use = penalty_use;
1120 if (!(self.spawnflags & 1))
1121 self.touch = penalty_touch;
1123 if (self.race_penalty_reason == "")
1124 self.race_penalty_reason = "missing a checkpoint";
1125 if (!self.race_penalty)
1126 self.race_penalty = 5;
1129 float race_GetFractionalLapCount(entity e)
1131 // interesting metrics (idea by KrimZon) to maybe sort players in the
1132 // scoreboard, immediately updates when overtaking
1134 // requires the track to be built so you never get farther away from the
1135 // next checkpoint, though, and current Xonotic race maps are not built that
1138 // also, this code is slow and would need optimization (i.e. "next CP"
1139 // links on CP entities)
1142 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1143 if(e.race_completed)
1144 return l; // not fractional
1147 float bestfraction, fraction;
1148 entity lastcp, cp0, cp1;
1149 float nextcpindex, lastcpindex;
1151 nextcpindex = max(e.race_checkpoint, 0);
1152 lastcpindex = e.race_respawn_checkpoint;
1153 lastcp = e.race_respawn_spotref;
1155 if(nextcpindex == lastcpindex)
1159 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1161 if(cp0.race_checkpoint != lastcpindex)
1166 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1167 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1169 if(cp1.race_checkpoint != nextcpindex)
1171 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1174 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1175 if(fraction < bestfraction)
1176 bestfraction = fraction;
1180 // we are at CP "nextcpindex - bestfraction"
1181 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1182 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1184 nc = race_highest_checkpoint + 1;
1185 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;