3 #include "cl_client.qh"
6 #include "spawnpoints.qh"
7 #include "bot/waypoints.qh"
8 #include "bot/navigation.qh"
9 #include "command/getreplies.qh"
10 #include "../common/deathtypes.qh"
11 #include "../common/notifications.qh"
12 #include "../common/mapinfo.qh"
13 #include "../common/triggers/subs.qh"
14 #include "../lib/warpzone/util_server.qh"
15 #include "../lib/warpzone/common.qh"
16 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
18 void W_Porto_Fail(float failhard);
20 float race_readTime(string map, float pos)
22 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
24 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
27 string race_readUID(string map, float pos)
29 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
31 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
34 float race_readPos(string map, float t)
37 for (i = 1; i <= RANKINGS_CNT; ++i)
38 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
41 return 0; // pos is zero if unranked
44 void race_writeTime(string map, float t, string myuid)
46 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
49 newpos = race_readPos(map, t);
52 for(i = 1; i <= RANKINGS_CNT; ++i)
54 if(race_readUID(map, i) == myuid)
59 // player improved his existing record, only have to iterate on ranks between new and old recs
60 for (i = prevpos; i > newpos; --i)
62 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
63 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
68 // player has no ranked record yet
69 for (i = RANKINGS_CNT; i > newpos; --i)
71 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
72 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
76 // store new time itself
77 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
78 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
81 string race_readName(string map, float pos)
83 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
85 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
89 const float MAX_CHECKPOINTS = 255;
91 spawnfunc(target_checkpoint);
94 .float race_penalty_accumulator;
95 .string race_penalty_reason;
96 .float race_checkpoint; // player: next checkpoint that has to be reached
97 .entity race_lastpenalty;
101 float race_checkpoint_records[MAX_CHECKPOINTS];
102 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
103 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
104 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
105 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
107 float race_highest_checkpoint;
108 float race_timed_checkpoint;
111 float defragcpexists;
113 float race_NextCheckpoint(float f)
115 if(f >= race_highest_checkpoint)
121 float race_PreviousCheckpoint(float f)
126 return race_highest_checkpoint;
132 // 0 = common start/finish
135 float race_CheckpointNetworkID(float f)
137 if(race_timed_checkpoint)
141 else if(f == race_timed_checkpoint)
142 return 255; // finish
147 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
156 cp = e.race_checkpoint;
157 recordtime = race_checkpoint_records[cp];
158 recordholder = race_checkpoint_recordholders[cp];
159 if(recordholder == e.netname)
162 if(!IS_REAL_CLIENT(e))
167 WRITESPECTATABLE_MSG_ONE({
168 WriteByte(MSG_ONE, SVC_TEMPENTITY);
169 WriteByte(MSG_ONE, TE_CSQC_RACE);
172 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
173 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
174 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
177 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
178 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
179 WriteInt24_t(MSG_ONE, recordtime);
180 WriteString(MSG_ONE, recordholder);
184 void race_send_recordtime(float msg)
186 // send the server best time
187 WriteByte(msg, SVC_TEMPENTITY);
188 WriteByte(msg, TE_CSQC_RACE);
189 WriteByte(msg, RACE_NET_SERVER_RECORD);
190 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
194 void race_send_speedaward(float msg)
196 // send the best speed of the round
197 WriteByte(msg, SVC_TEMPENTITY);
198 WriteByte(msg, TE_CSQC_RACE);
199 WriteByte(msg, RACE_NET_SPEED_AWARD);
200 WriteInt24_t(msg, floor(speedaward_speed+0.5));
201 WriteString(msg, speedaward_holder);
204 void race_send_speedaward_alltimebest(float msg)
206 // send the best speed
207 WriteByte(msg, SVC_TEMPENTITY);
208 WriteByte(msg, TE_CSQC_RACE);
209 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
210 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
211 WriteString(msg, speedaward_alltimebest_holder);
214 void race_SendRankings(float pos, float prevpos, float del, float msg)
216 WriteByte(msg, SVC_TEMPENTITY);
217 WriteByte(msg, TE_CSQC_RACE);
218 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
219 WriteShort(msg, pos);
220 WriteShort(msg, prevpos);
221 WriteShort(msg, del);
222 WriteString(msg, race_readName(GetMapname(), pos));
223 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
226 void race_SendStatus(float id, entity e)
228 if(!IS_REAL_CLIENT(e))
237 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
238 WriteByte(msg, SVC_TEMPENTITY);
239 WriteByte(msg, TE_CSQC_RACE);
240 WriteByte(msg, RACE_NET_SERVER_STATUS);
242 WriteString(msg, e.netname);
246 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
248 // netname only used TEMPORARILY for printing
249 float newpos, player_prevpos;
250 newpos = race_readPos(map, t);
254 for(i = 1; i <= RANKINGS_CNT; ++i)
256 if(race_readUID(map, i) == myuid)
261 string oldrec_holder;
262 if (player_prevpos && (player_prevpos < newpos || !newpos))
264 oldrec = race_readTime(GetMapname(), player_prevpos);
265 race_SendStatus(0, e); // "fail"
266 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
271 // no ranking, time worse than the worst ranked
272 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
273 race_SendStatus(0, e); // "fail"
274 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
278 // if we didn't hit a return yet, we have a new record!
280 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
283 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
287 oldrec = race_readTime(GetMapname(), newpos);
288 oldrec_holder = race_readName(GetMapname(), newpos);
291 race_writeTime(GetMapname(), t, myuid);
295 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
296 race_send_recordtime(MSG_ALL);
299 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
301 strunzone(rankings_reply);
302 rankings_reply = strzone(getrankings());
304 if(newpos == player_prevpos)
306 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
307 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
308 else { race_SendStatus(1, e); } // "new time"
312 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
313 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
314 else { race_SendStatus(2, e); } // "new rank"
318 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
319 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
320 else { race_SendStatus(2, e); } // "new rank"
324 void race_deleteTime(string map, float pos)
333 for (i = pos; i <= RANKINGS_CNT; ++i)
335 if (i == RANKINGS_CNT)
337 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
338 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
342 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
343 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
347 race_SendRankings(pos, 0, 1, MSG_ALL);
349 race_send_recordtime(MSG_ALL);
352 strunzone(rankings_reply);
353 rankings_reply = strzone(getrankings());
356 void race_SendTime(entity e, float cp, float t, float tvalid)
361 if(g_race_qualifying)
362 t += e.race_penalty_accumulator;
364 t = TIME_ENCODE(t); // make integer
365 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
368 if(cp == race_timed_checkpoint) // finish line
369 if (!e.race_completed)
372 if(g_race_qualifying)
374 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
376 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
380 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
382 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
384 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
385 snew = TIME_ENCODE(time - game_starttime);
386 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
387 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
389 if(autocvar_fraglimit)
390 if(l >= autocvar_fraglimit)
391 race_StartCompleting();
395 e.race_completed = 1;
396 MAKE_INDEPENDENT_PLAYER(e);
397 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
405 if(g_race_qualifying)
409 recordtime = race_checkpoint_records[cp];
410 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
411 if(recordholder == e.netname)
416 if(cp == race_timed_checkpoint)
418 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
419 if(g_cts && autocvar_g_cts_finish_kill_delay)
424 if(t < recordtime || recordtime == 0)
426 race_checkpoint_records[cp] = t;
427 if(race_checkpoint_recordholders[cp])
428 strunzone(race_checkpoint_recordholders[cp]);
429 race_checkpoint_recordholders[cp] = strzone(e.netname);
430 if(g_race_qualifying)
432 FOR_EACH_REALPLAYER(p)
433 if(p.race_checkpoint == cp)
434 race_SendNextCheckpoint(p, 0);
447 if(IS_REAL_CLIENT(e))
450 if(g_race_qualifying)
452 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
453 WriteByte(MSG_ONE, SVC_TEMPENTITY);
454 WriteByte(MSG_ONE, TE_CSQC_RACE);
455 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
456 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
457 WriteInt24_t(MSG_ONE, t); // time to that intermediate
458 WriteInt24_t(MSG_ONE, recordtime); // previously best time
459 WriteString(MSG_ONE, recordholder); // record holder
464 else // RACE! Not Qualifying
466 float lself, lother, othtime;
468 oth = race_checkpoint_lastplayers[cp];
471 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
472 lother = race_checkpoint_lastlaps[cp];
473 othtime = race_checkpoint_lasttimes[cp];
476 lself = lother = othtime = 0;
478 if(IS_REAL_CLIENT(e))
481 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
482 WriteByte(MSG_ONE, SVC_TEMPENTITY);
483 WriteByte(MSG_ONE, TE_CSQC_RACE);
484 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
485 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
488 WriteInt24_t(MSG_ONE, 0);
489 WriteByte(MSG_ONE, 0);
490 WriteString(MSG_ONE, "");
494 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
495 WriteByte(MSG_ONE, lself - lother);
496 WriteString(MSG_ONE, oth.netname); // record holder
501 race_checkpoint_lastplayers[cp] = e;
502 race_checkpoint_lasttimes[cp] = time;
503 race_checkpoint_lastlaps[cp] = lself;
505 if(IS_REAL_CLIENT(oth))
508 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
509 WriteByte(MSG_ONE, SVC_TEMPENTITY);
510 WriteByte(MSG_ONE, TE_CSQC_RACE);
511 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
512 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
515 WriteInt24_t(MSG_ONE, 0);
516 WriteByte(MSG_ONE, 0);
517 WriteString(MSG_ONE, "");
521 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
522 WriteByte(MSG_ONE, lother - lself);
523 WriteString(MSG_ONE, e.netname); // record holder
530 void race_ClearTime(entity e)
532 e.race_checkpoint = 0;
534 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
535 e.race_penalty_accumulator = 0;
536 e.race_lastpenalty = world;
538 if(!IS_REAL_CLIENT(e))
542 WRITESPECTATABLE_MSG_ONE({
543 WriteByte(MSG_ONE, SVC_TEMPENTITY);
544 WriteByte(MSG_ONE, TE_CSQC_RACE);
545 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
549 void dumpsurface(entity e)
553 LOG_INFO("Surfaces of ", etos(e), ":\n");
555 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
559 n = getsurfacenumpoints(e, si);
562 LOG_INFO(" Surface ", ftos(si), ":\n");
563 norm = getsurfacenormal(e, si);
564 LOG_INFO(" Normal = ", vtos(norm), "\n");
565 for(ni = 0; ni < n; ++ni)
567 vec = getsurfacepoint(e, si, ni);
568 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
573 void checkpoint_passed()
578 if(other.classname == "porto")
580 // do not allow portalling through checkpoints
581 trace_plane_normal = normalize(-1 * other.velocity);
590 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
593 oldmsg = self.message;
596 self.message = oldmsg;
599 if (!IS_PLAYER(other))
603 * Remove unauthorized equipment
605 Portal_ClearAll(other);
607 other.porto_forbidden = 2; // decreased by 1 each StartFrame
611 if(self.race_checkpoint == -2)
613 self.race_checkpoint = other.race_checkpoint;
616 float largest_cp_id = 0;
618 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
621 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
623 largest_cp_id = cp.race_checkpoint;
624 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
625 cp.race_checkpoint = largest_cp_id + 1; // finish line
626 race_highest_checkpoint = largest_cp_id + 1;
627 race_timed_checkpoint = largest_cp_id + 1;
629 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
631 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
638 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
639 cp.race_checkpoint = 1;
640 race_highest_checkpoint = 1;
641 race_timed_checkpoint = 1;
645 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
647 if(self.race_penalty)
649 if(other.race_lastpenalty != self)
651 other.race_lastpenalty = self;
652 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
656 if(other.race_penalty)
662 if(self.spawnflags & 2)
665 oldmsg = self.message;
668 self.message = oldmsg;
671 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
672 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
673 other.race_respawn_checkpoint = self.race_checkpoint;
674 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
675 other.race_started = 1;
677 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
679 if(!self.race_checkpoint) // start line
681 other.race_laptime = time;
682 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
683 other.race_penalty_accumulator = 0;
684 other.race_lastpenalty = world;
687 if(g_race_qualifying)
688 race_SendNextCheckpoint(other, 0);
690 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
693 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
696 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
697 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
702 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
708 if(self.spawnflags & 4)
709 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
713 void checkpoint_touch()
719 void checkpoint_use()
721 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
728 float race_waypointsprite_visible_for_player(entity e)
730 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
732 else if(e.race_checkpoint == self.owner.race_checkpoint)
739 void trigger_race_checkpoint_verify()
749 qual = g_race_qualifying;
752 self.classname = "player";
756 for(i = 0; i <= race_highest_checkpoint; ++i)
758 self.race_checkpoint = race_NextCheckpoint(i);
760 // race only (middle of the race)
761 g_race_qualifying = 0;
763 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
764 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
769 g_race_qualifying = 1;
770 self.race_place = race_lowest_place_spawn;
771 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
772 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
774 // race only (initial spawn)
775 g_race_qualifying = 0;
776 for(p = 1; p <= race_highest_place_spawn; ++p)
779 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
780 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
785 else if(!defrag_ents)
788 self.race_checkpoint = race_NextCheckpoint(0);
789 g_race_qualifying = 1;
790 self.race_place = race_lowest_place_spawn;
791 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
792 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
796 self.race_checkpoint = race_NextCheckpoint(0);
797 g_race_qualifying = 1;
798 self.race_place = 0; // there's only one spawn on defrag maps
800 // check if a defragcp file already exists, then read it and apply the checkpoint order
805 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
808 while((l = fgets(fh)))
810 len = tokenize_console(l);
813 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
816 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
817 if(argv(0) == cp.targetname)
818 cp.race_checkpoint = stof(argv(1));
824 g_race_qualifying = qual;
826 if(race_timed_checkpoint)
830 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
831 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
832 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
833 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
835 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
837 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
841 if(defragcpexists != -1)
843 float largest_cp_id = 0;
844 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
845 if(cp.race_checkpoint > largest_cp_id)
846 largest_cp_id = cp.race_checkpoint;
847 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
848 cp.race_checkpoint = largest_cp_id + 1; // finish line
849 race_highest_checkpoint = largest_cp_id + 1;
850 race_timed_checkpoint = largest_cp_id + 1;
854 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
855 cp.race_checkpoint = 255; // finish line
856 race_highest_checkpoint = 255;
857 race_timed_checkpoint = 255;
862 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
865 if(cp.race_checkpoint == 0)
866 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
867 else if(cp.race_checkpoint == race_timed_checkpoint)
868 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
875 entity trigger, targ;
876 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
877 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
878 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
885 // These just make the game crash on some maps with oddly shaped triggers.
886 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
887 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
888 // (happened on item triggers too)
893 //setsize(targ, trigger.mins, trigger.maxs);
894 //setorigin(targ, trigger.origin);
902 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
904 if(g_race_qualifying)
907 if(self.race_checkpoint != 0)
909 if(spot.race_place != race_lowest_place_spawn)
914 if(self.race_checkpoint != player.race_respawn_checkpoint)
916 // try reusing the previous spawn
917 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
918 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
919 if(self.race_checkpoint == 0)
922 pl = player.race_place;
923 if(pl > race_highest_place_spawn)
925 if(pl == 0 && !player.race_started)
926 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
927 if(spot.race_place != pl)
934 spawnfunc(trigger_race_checkpoint)
937 if(!g_race && !g_cts) { remove(self); return; }
941 self.use = checkpoint_use;
942 if (!(self.spawnflags & 1))
943 self.touch = checkpoint_touch;
945 o = (self.absmin + self.absmax) * 0.5;
946 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
947 waypoint_spawnforitem_force(self, trace_endpos);
948 self.nearestwaypointtimeout = time + 1000000000;
950 if(self.message == "")
951 self.message = "went backwards";
952 if (self.message2 == "")
953 self.message2 = "was pushed backwards by";
954 if (self.race_penalty_reason == "")
955 self.race_penalty_reason = "missing a checkpoint";
957 self.race_checkpoint = self.cnt;
959 if(self.race_checkpoint > race_highest_checkpoint)
961 race_highest_checkpoint = self.race_checkpoint;
962 if(self.spawnflags & 8)
963 race_timed_checkpoint = self.race_checkpoint;
965 race_timed_checkpoint = 0;
968 if(!self.race_penalty)
970 if(self.race_checkpoint)
971 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
973 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, self, sprite, RADARICON_NONE);
976 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
977 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
979 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
982 spawnfunc(target_checkpoint) // defrag entity
985 if(!g_race && !g_cts) { remove(self); return; }
990 self.use = checkpoint_use;
991 if (!(self.spawnflags & 1))
992 self.touch = checkpoint_touch;
994 o = (self.absmin + self.absmax) * 0.5;
995 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
996 waypoint_spawnforitem_force(self, trace_endpos);
997 self.nearestwaypointtimeout = time + 1000000000;
999 if(self.message == "")
1000 self.message = "went backwards";
1001 if (self.message2 == "")
1002 self.message2 = "was pushed backwards by";
1003 if (self.race_penalty_reason == "")
1004 self.race_penalty_reason = "missing a checkpoint";
1006 if(self.classname == "target_startTimer")
1007 self.race_checkpoint = 0;
1009 self.race_checkpoint = -2;
1011 race_timed_checkpoint = 1;
1013 if(self.race_checkpoint == 0)
1014 WaypointSprite_SpawnFixed(WP_RaceStart, o, self, sprite, RADARICON_NONE);
1016 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
1018 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1020 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1023 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1024 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1026 void race_AbandonRaceCheck(entity p)
1028 if(race_completing && !p.race_completed)
1030 p.race_completed = 1;
1031 MAKE_INDEPENDENT_PLAYER(p);
1032 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1033 ClientData_Touch(p);
1037 void race_StartCompleting()
1040 race_completing = 1;
1042 if(p.deadflag != DEAD_NO)
1043 race_AbandonRaceCheck(p);
1046 void race_PreparePlayer()
1048 race_ClearTime(self);
1049 self.race_place = 0;
1050 self.race_started = 0;
1051 self.race_respawn_checkpoint = 0;
1052 self.race_respawn_spotref = world;
1055 void race_RetractPlayer()
1057 if(!g_race && !g_cts)
1059 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1060 race_ClearTime(self);
1061 self.race_checkpoint = self.race_respawn_checkpoint;
1064 spawnfunc(info_player_race)
1066 if(!g_race && !g_cts) { remove(self); return; }
1068 spawnfunc_info_player_deathmatch(this);
1070 if(self.race_place > race_highest_place_spawn)
1071 race_highest_place_spawn = self.race_place;
1072 if(self.race_place < race_lowest_place_spawn)
1073 race_lowest_place_spawn = self.race_place;
1076 void race_ClearRecords()
1080 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1082 race_checkpoint_records[i] = 0;
1083 if(race_checkpoint_recordholders[i])
1084 strunzone(race_checkpoint_recordholders[i]);
1085 race_checkpoint_recordholders[i] = string_null;
1091 float p = e.race_place;
1092 WITH(entity, self, e, race_PreparePlayer());
1097 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1099 if(g_race_qualifying)
1101 pl.race_penalty_accumulator += penalty;
1102 if(IS_REAL_CLIENT(pl))
1105 WRITESPECTATABLE_MSG_ONE({
1106 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1107 WriteByte(MSG_ONE, TE_CSQC_RACE);
1108 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1109 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1110 WriteString(MSG_ONE, reason);
1116 pl.race_penalty = time + penalty;
1117 if(IS_REAL_CLIENT(pl))
1120 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1121 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1122 WriteByte(MSG_ONE, TE_CSQC_RACE);
1123 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1124 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1125 WriteString(MSG_ONE, reason);
1131 void penalty_touch()
1134 if(other.race_lastpenalty != self)
1136 other.race_lastpenalty = self;
1137 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1143 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1146 spawnfunc(trigger_race_penalty)
1150 self.use = penalty_use;
1151 if (!(self.spawnflags & 1))
1152 self.touch = penalty_touch;
1154 if (self.race_penalty_reason == "")
1155 self.race_penalty_reason = "missing a checkpoint";
1156 if (!self.race_penalty)
1157 self.race_penalty = 5;
1160 float race_GetFractionalLapCount(entity e)
1162 // interesting metrics (idea by KrimZon) to maybe sort players in the
1163 // scoreboard, immediately updates when overtaking
1165 // requires the track to be built so you never get farther away from the
1166 // next checkpoint, though, and current Xonotic race maps are not built that
1169 // also, this code is slow and would need optimization (i.e. "next CP"
1170 // links on CP entities)
1173 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1174 if(e.race_completed)
1175 return l; // not fractional
1178 float bestfraction, fraction;
1179 entity lastcp, cp0, cp1;
1180 float nextcpindex, lastcpindex;
1182 nextcpindex = max(e.race_checkpoint, 0);
1183 lastcpindex = e.race_respawn_checkpoint;
1184 lastcp = e.race_respawn_spotref;
1186 if(nextcpindex == lastcpindex)
1190 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1192 if(cp0.race_checkpoint != lastcpindex)
1197 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1198 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1200 if(cp1.race_checkpoint != nextcpindex)
1202 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1205 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1206 if(fraction < bestfraction)
1207 bestfraction = fraction;
1211 // we are at CP "nextcpindex - bestfraction"
1212 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1213 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1215 nc = race_highest_checkpoint + 1;
1216 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;