3 #include "cl_client.qh"
6 #include "spawnpoints.qh"
8 #include "command/getreplies.qh"
9 #include "../common/deathtypes/all.qh"
10 #include "../common/notifications/all.qh"
11 #include "../common/mapinfo.qh"
12 #include "../common/triggers/subs.qh"
13 #include "../lib/warpzone/util_server.qh"
14 #include "../lib/warpzone/common.qh"
15 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
17 void race_InitSpectator()
20 if(msg_entity.enemy.race_laptime)
21 race_SendNextCheckpoint(msg_entity.enemy, 1);
24 void W_Porto_Fail(entity this, float failhard);
26 float race_readTime(string map, float pos)
28 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
30 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
33 string race_readUID(string map, float pos)
35 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
37 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
40 float race_readPos(string map, float t)
43 for (i = 1; i <= RANKINGS_CNT; ++i)
44 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
47 return 0; // pos is zero if unranked
50 void race_writeTime(string map, float t, string myuid)
52 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
55 newpos = race_readPos(map, t);
58 for(i = 1; i <= RANKINGS_CNT; ++i)
60 if(race_readUID(map, i) == myuid)
65 // player improved his existing record, only have to iterate on ranks between new and old recs
66 for (i = prevpos; i > newpos; --i)
68 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
69 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
74 // player has no ranked record yet
75 for (i = RANKINGS_CNT; i > newpos; --i)
77 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
78 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
82 // store new time itself
83 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
84 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
87 string race_readName(string map, float pos)
89 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
91 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
95 const float MAX_CHECKPOINTS = 255;
97 spawnfunc(target_checkpoint);
100 .float race_penalty_accumulator;
101 .string race_penalty_reason;
102 .float race_checkpoint; // player: next checkpoint that has to be reached
103 .entity race_lastpenalty;
107 float race_checkpoint_records[MAX_CHECKPOINTS];
108 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
109 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
110 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
111 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
113 float race_highest_checkpoint;
114 float race_timed_checkpoint;
117 float defragcpexists;
119 float race_NextCheckpoint(float f)
121 if(f >= race_highest_checkpoint)
127 float race_PreviousCheckpoint(float f)
132 return race_highest_checkpoint;
138 // 0 = common start/finish
141 float race_CheckpointNetworkID(float f)
143 if(race_timed_checkpoint)
147 else if(f == race_timed_checkpoint)
148 return 255; // finish
153 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
162 cp = e.race_checkpoint;
163 recordtime = race_checkpoint_records[cp];
164 recordholder = race_checkpoint_recordholders[cp];
165 if(recordholder == e.netname)
168 if(!IS_REAL_CLIENT(e))
173 WRITESPECTATABLE_MSG_ONE(msg_entity, {
174 WriteHeader(MSG_ONE, TE_CSQC_RACE);
177 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
178 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
179 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
182 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
183 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
184 WriteInt24_t(MSG_ONE, recordtime);
185 WriteString(MSG_ONE, recordholder);
189 void race_send_recordtime(float msg)
191 // send the server best time
192 WriteHeader(msg, TE_CSQC_RACE);
193 WriteByte(msg, RACE_NET_SERVER_RECORD);
194 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
198 void race_send_speedaward(float msg)
200 // send the best speed of the round
201 WriteHeader(msg, TE_CSQC_RACE);
202 WriteByte(msg, RACE_NET_SPEED_AWARD);
203 WriteInt24_t(msg, floor(speedaward_speed+0.5));
204 WriteString(msg, speedaward_holder);
207 void race_send_speedaward_alltimebest(float msg)
209 // send the best speed
210 WriteHeader(msg, TE_CSQC_RACE);
211 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
212 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
213 WriteString(msg, speedaward_alltimebest_holder);
216 void race_SendRankings(float pos, float prevpos, float del, float msg)
218 WriteHeader(msg, TE_CSQC_RACE);
219 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
220 WriteShort(msg, pos);
221 WriteShort(msg, prevpos);
222 WriteShort(msg, del);
223 WriteString(msg, race_readName(GetMapname(), pos));
224 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
227 void race_SendStatus(float id, entity e)
229 if(!IS_REAL_CLIENT(e))
238 WRITESPECTATABLE_MSG_ONE(msg_entity, {
239 WriteHeader(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, NULL, 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, NULL, 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, NULL, 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, NULL, 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, NULL, 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, NULL, 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_remove(ServerProgsDB, strcat(map, rr, "time", ftos(i)));
338 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)));
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)
360 if(g_race_qualifying)
361 t += e.race_penalty_accumulator;
363 t = TIME_ENCODE(t); // make integer
364 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
367 if(cp == race_timed_checkpoint) // finish line
368 if (!e.race_completed)
371 if(g_race_qualifying)
373 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
375 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
379 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
381 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
383 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
384 snew = TIME_ENCODE(time - game_starttime);
385 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
386 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
388 if(autocvar_fraglimit)
389 if(l >= autocvar_fraglimit)
390 race_StartCompleting();
394 e.race_completed = 1;
395 MAKE_INDEPENDENT_PLAYER(e);
396 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
404 if(g_race_qualifying)
408 recordtime = race_checkpoint_records[cp];
409 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
410 if(recordholder == e.netname)
415 if(cp == race_timed_checkpoint)
417 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
418 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
420 if(t < recordtime || recordtime == 0)
422 race_checkpoint_records[cp] = t;
423 if(race_checkpoint_recordholders[cp])
424 strunzone(race_checkpoint_recordholders[cp]);
425 race_checkpoint_recordholders[cp] = strzone(e.netname);
426 if(g_race_qualifying)
428 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, LAMBDA(race_SendNextCheckpoint(it, 0)));
441 if(IS_REAL_CLIENT(e))
444 if(g_race_qualifying)
446 WRITESPECTATABLE_MSG_ONE(msg_entity, {
447 WriteHeader(MSG_ONE, TE_CSQC_RACE);
448 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
449 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
450 WriteInt24_t(MSG_ONE, t); // time to that intermediate
451 WriteInt24_t(MSG_ONE, recordtime); // previously best time
452 WriteString(MSG_ONE, recordholder); // record holder
457 else // RACE! Not Qualifying
459 float mylaps, lother, othtime;
461 oth = race_checkpoint_lastplayers[cp];
464 mylaps = PlayerScore_Add(e, SP_RACE_LAPS, 0);
465 lother = race_checkpoint_lastlaps[cp];
466 othtime = race_checkpoint_lasttimes[cp];
469 mylaps = lother = othtime = 0;
471 if(IS_REAL_CLIENT(e))
474 WRITESPECTATABLE_MSG_ONE(msg_entity, {
475 WriteHeader(MSG_ONE, TE_CSQC_RACE);
476 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
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 - race_checkpoint_lasttimes[cp]));
487 WriteByte(MSG_ONE, mylaps - lother);
488 WriteString(MSG_ONE, oth.netname); // record holder
493 race_checkpoint_lastplayers[cp] = e;
494 race_checkpoint_lasttimes[cp] = time;
495 race_checkpoint_lastlaps[cp] = mylaps;
497 if(IS_REAL_CLIENT(oth))
500 WRITESPECTATABLE_MSG_ONE(msg_entity, {
501 WriteHeader(MSG_ONE, TE_CSQC_RACE);
502 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
503 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
506 WriteInt24_t(MSG_ONE, 0);
507 WriteByte(MSG_ONE, 0);
508 WriteString(MSG_ONE, "");
512 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
513 WriteByte(MSG_ONE, lother - mylaps);
514 WriteString(MSG_ONE, e.netname); // record holder
521 void race_ClearTime(entity e)
523 e.race_checkpoint = 0;
525 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
526 e.race_penalty_accumulator = 0;
527 e.race_lastpenalty = NULL;
529 if(!IS_REAL_CLIENT(e))
533 WRITESPECTATABLE_MSG_ONE(msg_entity, {
534 WriteHeader(MSG_ONE, TE_CSQC_RACE);
535 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
539 void dumpsurface(entity e)
543 LOG_INFO("Surfaces of ", etos(e), ":\n");
545 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
549 n = getsurfacenumpoints(e, si);
552 LOG_INFO(" Surface ", ftos(si), ":\n");
553 norm = getsurfacenormal(e, si);
554 LOG_INFO(" Normal = ", vtos(norm), "\n");
555 for(ni = 0; ni < n; ++ni)
557 vec = getsurfacepoint(e, si, ni);
558 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
563 void checkpoint_passed(entity this, entity player)
568 if(player.classname == "porto")
570 // do not allow portalling through checkpoints
571 trace_plane_normal = normalize(-1 * player.velocity);
572 W_Porto_Fail(player, 0);
579 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
581 oldmsg = this.message;
583 SUB_UseTargets(this, player, player);
584 this.message = oldmsg;
587 if (!IS_PLAYER(player))
591 * Remove unauthorized equipment
593 Portal_ClearAll(player);
595 player.porto_forbidden = 2; // decreased by 1 each StartFrame
599 if(this.race_checkpoint == -2)
601 this.race_checkpoint = player.race_checkpoint;
604 float largest_cp_id = 0;
606 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
609 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
611 largest_cp_id = cp.race_checkpoint;
612 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
613 cp.race_checkpoint = largest_cp_id + 1; // finish line
614 race_highest_checkpoint = largest_cp_id + 1;
615 race_timed_checkpoint = largest_cp_id + 1;
617 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
619 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
626 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
627 cp.race_checkpoint = 1;
628 race_highest_checkpoint = 1;
629 race_timed_checkpoint = 1;
633 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
635 if(this.race_penalty)
637 if(player.race_lastpenalty != this)
639 player.race_lastpenalty = this;
640 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
644 if(player.race_penalty)
650 if(this.spawnflags & 2)
652 oldmsg = this.message;
654 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
655 this.message = oldmsg;
658 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
659 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
660 player.race_respawn_checkpoint = this.race_checkpoint;
661 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
662 player.race_started = 1;
664 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
666 if(!this.race_checkpoint) // start line
668 player.race_laptime = time;
669 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
670 player.race_penalty_accumulator = 0;
671 player.race_lastpenalty = NULL;
674 if(g_race_qualifying)
675 race_SendNextCheckpoint(player, 0);
677 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
680 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
683 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
684 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
689 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
695 if(this.spawnflags & 4)
696 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, player.origin, '0 0 0');
700 void checkpoint_touch(entity this, entity toucher)
702 EXACTTRIGGER_TOUCH(this, toucher);
703 checkpoint_passed(this, toucher);
706 void checkpoint_use(entity this, entity actor, entity trigger)
708 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
711 checkpoint_passed(this, actor);
714 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
716 if(view.race_checkpoint == -1 || this.owner.race_checkpoint == -2)
718 else if(view.race_checkpoint == this.owner.race_checkpoint)
724 void trigger_race_checkpoint_verify(entity this)
726 static bool have_verified;
727 if (have_verified) return;
728 have_verified = true;
730 bool qual = g_race_qualifying;
732 int pl_race_checkpoint = 0;
733 int pl_race_place = 0;
736 for (int i = 0; i <= race_highest_checkpoint; ++i) {
737 pl_race_checkpoint = race_NextCheckpoint(i);
739 // race only (middle of the race)
740 g_race_qualifying = false;
742 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
743 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
748 g_race_qualifying = 1;
749 pl_race_place = race_lowest_place_spawn;
750 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
751 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
754 // race only (initial spawn)
755 g_race_qualifying = 0;
756 for (int p = 1; p <= race_highest_place_spawn; ++p) {
758 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
759 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
764 } else if (!defrag_ents) {
766 pl_race_checkpoint = race_NextCheckpoint(0);
767 g_race_qualifying = 1;
768 pl_race_place = race_lowest_place_spawn;
769 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
770 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
773 pl_race_checkpoint = race_NextCheckpoint(0);
774 g_race_qualifying = 1;
775 pl_race_place = 0; // there's only one spawn on defrag maps
777 // check if a defragcp file already exists, then read it and apply the checkpoint order
782 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
784 while ((l = fgets(fh))) {
785 len = tokenize_console(l);
787 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
790 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
791 if (argv(0) == cp.targetname) {
792 cp.race_checkpoint = stof(argv(1));
800 g_race_qualifying = qual;
802 if (race_timed_checkpoint) {
804 for (entity cp = NULL; (cp = find(cp, classname, "target_startTimer"));) {
805 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
807 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
808 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
810 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
811 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
815 if (defragcpexists != -1) {
816 float largest_cp_id = 0;
817 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
818 if (cp.race_checkpoint > largest_cp_id) {
819 largest_cp_id = cp.race_checkpoint;
822 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
823 cp.race_checkpoint = largest_cp_id + 1; // finish line
825 race_highest_checkpoint = largest_cp_id + 1;
826 race_timed_checkpoint = largest_cp_id + 1;
828 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
829 cp.race_checkpoint = 255; // finish line
831 race_highest_checkpoint = 255;
832 race_timed_checkpoint = 255;
835 for (entity cp = NULL; (cp = find(cp, classname, "trigger_race_checkpoint")); ) {
837 if (cp.race_checkpoint == 0) {
838 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
839 } else if (cp.race_checkpoint == race_timed_checkpoint) {
840 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
848 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
849 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
850 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
856 // These just make the game crash on some maps with oddly shaped triggers.
857 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
858 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
859 // (happened on item triggers too)
864 //setsize(targ, trigger.mins, trigger.maxs);
865 //setorigin(targ, trigger.origin);
873 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
875 if(g_race_qualifying)
878 if(this.race_checkpoint != 0)
880 if(spot.race_place != race_lowest_place_spawn)
885 if(this.race_checkpoint != player.race_respawn_checkpoint)
887 // try reusing the previous spawn
888 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
889 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
890 if(this.race_checkpoint == 0)
892 int pl = player.race_place;
893 if(pl > race_highest_place_spawn)
895 if(pl == 0 && !player.race_started)
896 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
897 if(spot.race_place != pl)
904 spawnfunc(trigger_race_checkpoint)
907 if(!g_race && !g_cts) { delete(this); return; }
911 this.use = checkpoint_use;
912 if (!(this.spawnflags & 1))
913 settouch(this, checkpoint_touch);
915 o = (this.absmin + this.absmax) * 0.5;
916 tracebox(o, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
917 waypoint_spawnforitem_force(this, trace_endpos);
918 this.nearestwaypointtimeout = time + 1000000000;
920 if(this.message == "")
921 this.message = "went backwards";
922 if (this.message2 == "")
923 this.message2 = "was pushed backwards by";
924 if (this.race_penalty_reason == "")
925 this.race_penalty_reason = "missing a checkpoint";
927 this.race_checkpoint = this.cnt;
929 if(this.race_checkpoint > race_highest_checkpoint)
931 race_highest_checkpoint = this.race_checkpoint;
932 if(this.spawnflags & 8)
933 race_timed_checkpoint = this.race_checkpoint;
935 race_timed_checkpoint = 0;
938 if(!this.race_penalty)
940 if(this.race_checkpoint)
941 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
943 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
946 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
947 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
949 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
952 spawnfunc(target_checkpoint) // defrag entity
955 if(!g_race && !g_cts) { delete(this); return; }
960 this.use = checkpoint_use;
961 if (!(this.spawnflags & 1))
962 settouch(this, checkpoint_touch);
964 o = (this.absmin + this.absmax) * 0.5;
965 tracebox(o, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
966 waypoint_spawnforitem_force(this, trace_endpos);
967 this.nearestwaypointtimeout = time + 1000000000;
969 if(this.message == "")
970 this.message = "went backwards";
971 if (this.message2 == "")
972 this.message2 = "was pushed backwards by";
973 if (this.race_penalty_reason == "")
974 this.race_penalty_reason = "missing a checkpoint";
976 if(this.classname == "target_startTimer")
977 this.race_checkpoint = 0;
979 this.race_checkpoint = -2;
981 race_timed_checkpoint = 1;
983 if(this.race_checkpoint == 0)
984 WaypointSprite_SpawnFixed(WP_RaceStart, o, this, sprite, RADARICON_NONE);
986 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
988 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
990 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
993 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
994 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
996 void race_AbandonRaceCheck(entity p)
998 if(race_completing && !p.race_completed)
1000 p.race_completed = 1;
1001 MAKE_INDEPENDENT_PLAYER(p);
1002 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1003 ClientData_Touch(p);
1007 void race_StartCompleting()
1009 race_completing = 1;
1010 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), LAMBDA(race_AbandonRaceCheck(it)));
1013 void race_PreparePlayer(entity this)
1015 race_ClearTime(this);
1016 this.race_place = 0;
1017 this.race_started = 0;
1018 this.race_respawn_checkpoint = 0;
1019 this.race_respawn_spotref = NULL;
1022 void race_RetractPlayer(entity this)
1024 if(!g_race && !g_cts)
1026 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1027 race_ClearTime(this);
1028 this.race_checkpoint = this.race_respawn_checkpoint;
1031 spawnfunc(info_player_race)
1033 if(!g_race && !g_cts) { delete(this); return; }
1035 spawnfunc_info_player_deathmatch(this);
1037 if(this.race_place > race_highest_place_spawn)
1038 race_highest_place_spawn = this.race_place;
1039 if(this.race_place < race_lowest_place_spawn)
1040 race_lowest_place_spawn = this.race_place;
1043 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;
1055 FOREACH_CLIENT(true, LAMBDA(
1056 float p = it.race_place;
1057 race_PreparePlayer(it);
1062 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1064 if(g_race_qualifying)
1066 pl.race_penalty_accumulator += penalty;
1067 if(IS_REAL_CLIENT(pl))
1070 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1071 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1072 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1073 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1074 WriteString(MSG_ONE, reason);
1080 pl.race_penalty = time + penalty;
1081 if(IS_REAL_CLIENT(pl))
1084 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1085 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1086 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1087 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1088 WriteString(MSG_ONE, reason);
1094 void penalty_touch(entity this, entity toucher)
1096 EXACTTRIGGER_TOUCH(this, toucher);
1097 if(toucher.race_lastpenalty != this)
1099 toucher.race_lastpenalty = this;
1100 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1104 void penalty_use(entity this, entity actor, entity trigger)
1106 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1109 spawnfunc(trigger_race_penalty)
1111 // TODO: find out why this wasnt done:
1112 //if(!g_cts && !g_race) { remove(this); return; }
1116 this.use = penalty_use;
1117 if (!(this.spawnflags & 1))
1118 settouch(this, penalty_touch);
1120 if (this.race_penalty_reason == "")
1121 this.race_penalty_reason = "missing a checkpoint";
1122 if (!this.race_penalty)
1123 this.race_penalty = 5;
1126 float race_GetFractionalLapCount(entity e)
1128 // interesting metrics (idea by KrimZon) to maybe sort players in the
1129 // scoreboard, immediately updates when overtaking
1131 // requires the track to be built so you never get farther away from the
1132 // next checkpoint, though, and current Xonotic race maps are not built that
1135 // also, this code is slow and would need optimization (i.e. "next CP"
1136 // links on CP entities)
1139 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1140 if(e.race_completed)
1141 return l; // not fractional
1144 float bestfraction, fraction;
1145 entity lastcp, cp0, cp1;
1146 float nextcpindex, lastcpindex;
1148 nextcpindex = max(e.race_checkpoint, 0);
1149 lastcpindex = e.race_respawn_checkpoint;
1150 lastcp = e.race_respawn_spotref;
1152 if(nextcpindex == lastcpindex)
1156 for(cp0 = NULL; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1158 if(cp0.race_checkpoint != lastcpindex)
1163 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1164 for(cp1 = NULL; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1166 if(cp1.race_checkpoint != nextcpindex)
1168 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1171 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1172 if(fraction < bestfraction)
1173 bestfraction = fraction;
1177 // we are at CP "nextcpindex - bestfraction"
1178 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1179 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1181 nc = race_highest_checkpoint + 1;
1182 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;