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/net_linked.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 race_InitSpectator()
21 if(msg_entity.enemy.race_laptime)
22 race_SendNextCheckpoint(msg_entity.enemy, 1);
25 void W_Porto_Fail(entity this, float failhard);
27 float race_readTime(string map, float pos)
29 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
31 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
34 string race_readUID(string map, float pos)
36 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
38 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
41 float race_readPos(string map, float t)
44 for (i = 1; i <= RANKINGS_CNT; ++i)
45 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
48 return 0; // pos is zero if unranked
51 void race_writeTime(string map, float t, string myuid)
53 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
56 newpos = race_readPos(map, t);
59 for(i = 1; i <= RANKINGS_CNT; ++i)
61 if(race_readUID(map, i) == myuid)
66 // player improved his existing record, only have to iterate on ranks between new and old recs
67 for (i = prevpos; i > newpos; --i)
69 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
70 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
75 // player has no ranked record yet
76 for (i = RANKINGS_CNT; i > newpos; --i)
78 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
79 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
83 // store new time itself
84 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
85 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
88 string race_readName(string map, float pos)
90 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
92 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
96 const float MAX_CHECKPOINTS = 255;
98 spawnfunc(target_checkpoint);
101 .float race_penalty_accumulator;
102 .string race_penalty_reason;
103 .float race_checkpoint; // player: next checkpoint that has to be reached
104 .entity race_lastpenalty;
108 float race_checkpoint_records[MAX_CHECKPOINTS];
109 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
110 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
111 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
112 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
114 float race_highest_checkpoint;
115 float race_timed_checkpoint;
118 float defragcpexists;
120 float race_NextCheckpoint(float f)
122 if(f >= race_highest_checkpoint)
128 float race_PreviousCheckpoint(float f)
133 return race_highest_checkpoint;
139 // 0 = common start/finish
142 float race_CheckpointNetworkID(float f)
144 if(race_timed_checkpoint)
148 else if(f == race_timed_checkpoint)
149 return 255; // finish
154 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
163 cp = e.race_checkpoint;
164 recordtime = race_checkpoint_records[cp];
165 recordholder = race_checkpoint_recordholders[cp];
166 if(recordholder == e.netname)
169 if(!IS_REAL_CLIENT(e))
174 WRITESPECTATABLE_MSG_ONE(msg_entity, {
175 WriteHeader(MSG_ONE, TE_CSQC_RACE);
178 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
179 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
180 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
183 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
184 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
185 WriteInt24_t(MSG_ONE, recordtime);
186 WriteString(MSG_ONE, recordholder);
190 void race_send_recordtime(float msg)
192 // send the server best time
193 WriteHeader(msg, TE_CSQC_RACE);
194 WriteByte(msg, RACE_NET_SERVER_RECORD);
195 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
199 void race_send_speedaward(float msg)
201 // send the best speed of the round
202 WriteHeader(msg, TE_CSQC_RACE);
203 WriteByte(msg, RACE_NET_SPEED_AWARD);
204 WriteInt24_t(msg, floor(speedaward_speed+0.5));
205 WriteString(msg, speedaward_holder);
208 void race_send_speedaward_alltimebest(float msg)
210 // send the best speed
211 WriteHeader(msg, TE_CSQC_RACE);
212 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
213 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
214 WriteString(msg, speedaward_alltimebest_holder);
217 void race_SendRankings(float pos, float prevpos, float del, float msg)
219 WriteHeader(msg, TE_CSQC_RACE);
220 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
221 WriteShort(msg, pos);
222 WriteShort(msg, prevpos);
223 WriteShort(msg, del);
224 WriteString(msg, race_readName(GetMapname(), pos));
225 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
228 void race_SendStatus(float id, entity e)
230 if(!IS_REAL_CLIENT(e))
239 WRITESPECTATABLE_MSG_ONE(msg_entity, {
240 WriteHeader(msg, TE_CSQC_RACE);
241 WriteByte(msg, RACE_NET_SERVER_STATUS);
243 WriteString(msg, e.netname);
247 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
249 // netname only used TEMPORARILY for printing
250 float newpos, player_prevpos;
251 newpos = race_readPos(map, t);
255 for(i = 1; i <= RANKINGS_CNT; ++i)
257 if(race_readUID(map, i) == myuid)
262 string oldrec_holder;
263 if (player_prevpos && (player_prevpos < newpos || !newpos))
265 oldrec = race_readTime(GetMapname(), player_prevpos);
266 race_SendStatus(0, e); // "fail"
267 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
272 // no ranking, time worse than the worst ranked
273 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
274 race_SendStatus(0, e); // "fail"
275 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
279 // if we didn't hit a return yet, we have a new record!
281 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
284 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
288 if(uid2name(myuid) == "^1Unregistered Player")
290 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
294 oldrec = race_readTime(GetMapname(), newpos);
295 oldrec_holder = race_readName(GetMapname(), newpos);
298 race_writeTime(GetMapname(), t, myuid);
302 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
303 race_send_recordtime(MSG_ALL);
306 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
308 strunzone(rankings_reply);
309 rankings_reply = strzone(getrankings());
311 if(newpos == player_prevpos)
313 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
314 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
315 else { race_SendStatus(1, e); } // "new time"
319 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
320 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
321 else { race_SendStatus(2, e); } // "new rank"
325 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
326 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
327 else { race_SendStatus(2, e); } // "new rank"
331 void race_deleteTime(string map, float pos)
340 for (i = pos; i <= RANKINGS_CNT; ++i)
342 if (i == RANKINGS_CNT)
344 db_remove(ServerProgsDB, strcat(map, rr, "time", ftos(i)));
345 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)));
349 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
350 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
354 race_SendRankings(pos, 0, 1, MSG_ALL);
356 race_send_recordtime(MSG_ALL);
359 strunzone(rankings_reply);
360 rankings_reply = strzone(getrankings());
363 void race_SendTime(entity e, float cp, float t, float tvalid)
367 if(g_race_qualifying)
368 t += e.race_penalty_accumulator;
370 t = TIME_ENCODE(t); // make integer
371 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
374 if(cp == race_timed_checkpoint) // finish line
375 if (!e.race_completed)
378 if(g_race_qualifying)
380 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
382 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
386 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
388 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
390 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
391 snew = TIME_ENCODE(time - game_starttime);
392 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
393 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
395 if(autocvar_fraglimit)
396 if(l >= autocvar_fraglimit)
397 race_StartCompleting();
401 e.race_completed = 1;
402 MAKE_INDEPENDENT_PLAYER(e);
403 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
411 if(g_race_qualifying)
415 recordtime = race_checkpoint_records[cp];
416 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
417 if(recordholder == e.netname)
422 if(cp == race_timed_checkpoint)
424 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
425 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
427 if(t < recordtime || recordtime == 0)
429 race_checkpoint_records[cp] = t;
430 if(race_checkpoint_recordholders[cp])
431 strunzone(race_checkpoint_recordholders[cp]);
432 race_checkpoint_recordholders[cp] = strzone(e.netname);
433 if(g_race_qualifying)
435 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, LAMBDA(race_SendNextCheckpoint(it, 0)));
448 if(IS_REAL_CLIENT(e))
451 if(g_race_qualifying)
453 WRITESPECTATABLE_MSG_ONE(msg_entity, {
454 WriteHeader(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 mylaps, lother, othtime;
468 oth = race_checkpoint_lastplayers[cp];
471 mylaps = PlayerScore_Add(e, SP_RACE_LAPS, 0);
472 lother = race_checkpoint_lastlaps[cp];
473 othtime = race_checkpoint_lasttimes[cp];
476 mylaps = lother = othtime = 0;
478 if(IS_REAL_CLIENT(e))
481 WRITESPECTATABLE_MSG_ONE(msg_entity, {
482 WriteHeader(MSG_ONE, TE_CSQC_RACE);
483 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
484 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
487 WriteInt24_t(MSG_ONE, 0);
488 WriteByte(MSG_ONE, 0);
489 WriteString(MSG_ONE, "");
493 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
494 WriteByte(MSG_ONE, mylaps - lother);
495 WriteString(MSG_ONE, oth.netname); // record holder
500 race_checkpoint_lastplayers[cp] = e;
501 race_checkpoint_lasttimes[cp] = time;
502 race_checkpoint_lastlaps[cp] = mylaps;
504 if(IS_REAL_CLIENT(oth))
507 WRITESPECTATABLE_MSG_ONE(msg_entity, {
508 WriteHeader(MSG_ONE, TE_CSQC_RACE);
509 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
510 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
513 WriteInt24_t(MSG_ONE, 0);
514 WriteByte(MSG_ONE, 0);
515 WriteString(MSG_ONE, "");
519 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
520 WriteByte(MSG_ONE, lother - mylaps);
521 WriteString(MSG_ONE, e.netname); // record holder
528 void race_ClearTime(entity e)
530 e.race_checkpoint = 0;
532 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
533 e.race_penalty_accumulator = 0;
534 e.race_lastpenalty = NULL;
536 if(!IS_REAL_CLIENT(e))
540 WRITESPECTATABLE_MSG_ONE(msg_entity, {
541 WriteHeader(MSG_ONE, TE_CSQC_RACE);
542 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
546 void dumpsurface(entity e)
550 LOG_INFO("Surfaces of ", etos(e), ":\n");
552 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
556 n = getsurfacenumpoints(e, si);
559 LOG_INFO(" Surface ", ftos(si), ":\n");
560 norm = getsurfacenormal(e, si);
561 LOG_INFO(" Normal = ", vtos(norm), "\n");
562 for(ni = 0; ni < n; ++ni)
564 vec = getsurfacepoint(e, si, ni);
565 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
570 void checkpoint_passed(entity this, entity player)
575 if(player.classname == "porto")
577 // do not allow portalling through checkpoints
578 trace_plane_normal = normalize(-1 * player.velocity);
579 W_Porto_Fail(player, 0);
586 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
588 oldmsg = this.message;
590 SUB_UseTargets(this, player, player);
591 this.message = oldmsg;
594 if (!IS_PLAYER(player))
598 * Remove unauthorized equipment
600 Portal_ClearAll(player);
602 player.porto_forbidden = 2; // decreased by 1 each StartFrame
606 if(this.race_checkpoint == -2)
608 this.race_checkpoint = player.race_checkpoint;
611 float largest_cp_id = 0;
613 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
616 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
618 largest_cp_id = cp.race_checkpoint;
619 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
620 cp.race_checkpoint = largest_cp_id + 1; // finish line
621 race_highest_checkpoint = largest_cp_id + 1;
622 race_timed_checkpoint = largest_cp_id + 1;
624 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
626 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
633 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
634 cp.race_checkpoint = 1;
635 race_highest_checkpoint = 1;
636 race_timed_checkpoint = 1;
640 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
642 if(this.race_penalty)
644 if(player.race_lastpenalty != this)
646 player.race_lastpenalty = this;
647 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
651 if(player.race_penalty)
657 if(this.spawnflags & 2)
659 oldmsg = this.message;
661 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
662 this.message = oldmsg;
665 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
666 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
667 player.race_respawn_checkpoint = this.race_checkpoint;
668 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
669 player.race_started = 1;
671 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
673 if(!this.race_checkpoint) // start line
675 player.race_laptime = time;
676 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
677 player.race_penalty_accumulator = 0;
678 player.race_lastpenalty = NULL;
681 if(g_race_qualifying)
682 race_SendNextCheckpoint(player, 0);
684 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
687 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
690 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
691 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
696 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
702 if(this.spawnflags & 4)
703 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, player.origin, '0 0 0');
707 void checkpoint_touch(entity this, entity toucher)
709 EXACTTRIGGER_TOUCH(this, toucher);
710 checkpoint_passed(this, toucher);
713 void checkpoint_use(entity this, entity actor, entity trigger)
715 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
718 checkpoint_passed(this, actor);
721 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
723 if(view.race_checkpoint == -1 || this.owner.race_checkpoint == -2)
725 else if(view.race_checkpoint == this.owner.race_checkpoint)
731 void trigger_race_checkpoint_verify(entity this)
733 static bool have_verified;
734 if (have_verified) return;
735 have_verified = true;
737 bool qual = g_race_qualifying;
739 int pl_race_checkpoint = 0;
740 int pl_race_place = 0;
743 for (int i = 0; i <= race_highest_checkpoint; ++i) {
744 pl_race_checkpoint = race_NextCheckpoint(i);
746 // race only (middle of the race)
747 g_race_qualifying = false;
749 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
750 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
755 g_race_qualifying = 1;
756 pl_race_place = race_lowest_place_spawn;
757 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
758 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
761 // race only (initial spawn)
762 g_race_qualifying = 0;
763 for (int p = 1; p <= race_highest_place_spawn; ++p) {
765 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
766 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
771 } else if (!defrag_ents) {
773 pl_race_checkpoint = race_NextCheckpoint(0);
774 g_race_qualifying = 1;
775 pl_race_place = race_lowest_place_spawn;
776 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
777 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
780 pl_race_checkpoint = race_NextCheckpoint(0);
781 g_race_qualifying = 1;
782 pl_race_place = 0; // there's only one spawn on defrag maps
784 // check if a defragcp file already exists, then read it and apply the checkpoint order
789 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
791 while ((l = fgets(fh))) {
792 len = tokenize_console(l);
794 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
797 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
798 if (argv(0) == cp.targetname) {
799 cp.race_checkpoint = stof(argv(1));
807 g_race_qualifying = qual;
809 if (race_timed_checkpoint) {
811 for (entity cp = NULL; (cp = find(cp, classname, "target_startTimer"));) {
812 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
814 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
815 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
817 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
818 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
822 if (defragcpexists != -1) {
823 float largest_cp_id = 0;
824 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
825 if (cp.race_checkpoint > largest_cp_id) {
826 largest_cp_id = cp.race_checkpoint;
829 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
830 cp.race_checkpoint = largest_cp_id + 1; // finish line
832 race_highest_checkpoint = largest_cp_id + 1;
833 race_timed_checkpoint = largest_cp_id + 1;
835 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
836 cp.race_checkpoint = 255; // finish line
838 race_highest_checkpoint = 255;
839 race_timed_checkpoint = 255;
842 IL_EACH(g_racecheckpoints, it.sprite,
844 if (it.race_checkpoint == 0) {
845 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
846 } else if (it.race_checkpoint == race_timed_checkpoint) {
847 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
854 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
855 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
856 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
862 // These just make the game crash on some maps with oddly shaped triggers.
863 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
864 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
865 // (happened on item triggers too)
870 //setsize(targ, trigger.mins, trigger.maxs);
871 //setorigin(targ, trigger.origin);
879 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
881 if(g_race_qualifying)
884 if(this.race_checkpoint != 0)
886 if(spot.race_place != race_lowest_place_spawn)
891 if(this.race_checkpoint != player.race_respawn_checkpoint)
893 // try reusing the previous spawn
894 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
895 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
896 if(this.race_checkpoint == 0)
898 int pl = player.race_place;
899 if(pl > race_highest_place_spawn)
901 if(pl == 0 && !player.race_started)
902 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
903 if(spot.race_place != pl)
910 spawnfunc(trigger_race_checkpoint)
913 if(!g_race && !g_cts) { delete(this); return; }
917 this.use = checkpoint_use;
918 if (!(this.spawnflags & 1))
919 settouch(this, checkpoint_touch);
921 o = (this.absmin + this.absmax) * 0.5;
922 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
923 waypoint_spawnforitem_force(this, trace_endpos);
924 this.nearestwaypointtimeout = time + 1000000000;
926 if(this.message == "")
927 this.message = "went backwards";
928 if (this.message2 == "")
929 this.message2 = "was pushed backwards by";
930 if (this.race_penalty_reason == "")
931 this.race_penalty_reason = "missing a checkpoint";
933 this.race_checkpoint = this.cnt;
935 if(this.race_checkpoint > race_highest_checkpoint)
937 race_highest_checkpoint = this.race_checkpoint;
938 if(this.spawnflags & 8)
939 race_timed_checkpoint = this.race_checkpoint;
941 race_timed_checkpoint = 0;
944 if(!this.race_penalty)
946 if(this.race_checkpoint)
947 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
949 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
952 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
953 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
955 IL_PUSH(g_racecheckpoints, this);
957 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
960 spawnfunc(target_checkpoint) // defrag entity
962 if(!g_race && !g_cts) { delete(this); return; }
965 // if this is targeted, then it probably isn't a trigger
966 bool is_trigger = !boolean(!this.nottargeted && this.targetname != "");
971 this.use = checkpoint_use;
972 if (is_trigger && !(this.spawnflags & 1))
973 settouch(this, checkpoint_touch);
975 vector org = this.origin;
977 // bots should only pathfind to this if it is a valid touchable trigger
980 org = (this.absmin + this.absmax) * 0.5;
981 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
982 waypoint_spawnforitem_force(this, trace_endpos);
983 this.nearestwaypointtimeout = time + 1000000000;
986 if(this.message == "")
987 this.message = "went backwards";
988 if (this.message2 == "")
989 this.message2 = "was pushed backwards by";
990 if (this.race_penalty_reason == "")
991 this.race_penalty_reason = "missing a checkpoint";
993 if(this.classname == "target_startTimer")
994 this.race_checkpoint = 0;
996 this.race_checkpoint = -2;
998 race_timed_checkpoint = 1;
1000 if(this.race_checkpoint == 0)
1001 WaypointSprite_SpawnFixed(WP_RaceStart, org, this, sprite, RADARICON_NONE);
1003 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, this, sprite, RADARICON_NONE);
1005 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1007 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1010 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1011 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1013 void race_AbandonRaceCheck(entity p)
1015 if(race_completing && !p.race_completed)
1017 p.race_completed = 1;
1018 MAKE_INDEPENDENT_PLAYER(p);
1019 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1020 ClientData_Touch(p);
1024 void race_StartCompleting()
1026 race_completing = 1;
1027 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), LAMBDA(race_AbandonRaceCheck(it)));
1030 void race_PreparePlayer(entity this)
1032 race_ClearTime(this);
1033 this.race_place = 0;
1034 this.race_started = 0;
1035 this.race_respawn_checkpoint = 0;
1036 this.race_respawn_spotref = NULL;
1039 void race_RetractPlayer(entity this)
1041 if(!g_race && !g_cts)
1043 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1044 race_ClearTime(this);
1045 this.race_checkpoint = this.race_respawn_checkpoint;
1048 spawnfunc(info_player_race)
1050 if(!g_race && !g_cts) { delete(this); return; }
1052 spawnfunc_info_player_deathmatch(this);
1054 if(this.race_place > race_highest_place_spawn)
1055 race_highest_place_spawn = this.race_place;
1056 if(this.race_place < race_lowest_place_spawn)
1057 race_lowest_place_spawn = this.race_place;
1060 void race_ClearRecords()
1064 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1066 race_checkpoint_records[i] = 0;
1067 if(race_checkpoint_recordholders[i])
1068 strunzone(race_checkpoint_recordholders[i]);
1069 race_checkpoint_recordholders[i] = string_null;
1072 FOREACH_CLIENT(true, LAMBDA(
1073 float p = it.race_place;
1074 race_PreparePlayer(it);
1079 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1081 if(g_race_qualifying)
1083 pl.race_penalty_accumulator += penalty;
1084 if(IS_REAL_CLIENT(pl))
1087 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1088 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1089 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1090 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1091 WriteString(MSG_ONE, reason);
1097 pl.race_penalty = time + penalty;
1098 if(IS_REAL_CLIENT(pl))
1101 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1102 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1103 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1104 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1105 WriteString(MSG_ONE, reason);
1111 void penalty_touch(entity this, entity toucher)
1113 EXACTTRIGGER_TOUCH(this, toucher);
1114 if(toucher.race_lastpenalty != this)
1116 toucher.race_lastpenalty = this;
1117 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1121 void penalty_use(entity this, entity actor, entity trigger)
1123 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1126 spawnfunc(trigger_race_penalty)
1128 // TODO: find out why this wasnt done:
1129 //if(!g_cts && !g_race) { remove(this); return; }
1133 this.use = penalty_use;
1134 if (!(this.spawnflags & 1))
1135 settouch(this, penalty_touch);
1137 if (this.race_penalty_reason == "")
1138 this.race_penalty_reason = "missing a checkpoint";
1139 if (!this.race_penalty)
1140 this.race_penalty = 5;
1143 float race_GetFractionalLapCount(entity e)
1145 // interesting metrics (idea by KrimZon) to maybe sort players in the
1146 // scoreboard, immediately updates when overtaking
1148 // requires the track to be built so you never get farther away from the
1149 // next checkpoint, though, and current Xonotic race maps are not built that
1152 // also, this code is slow and would need optimization (i.e. "next CP"
1153 // links on CP entities)
1156 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1157 if(e.race_completed)
1158 return l; // not fractional
1161 float bestfraction, fraction;
1163 float nextcpindex, lastcpindex;
1165 nextcpindex = max(e.race_checkpoint, 0);
1166 lastcpindex = e.race_respawn_checkpoint;
1167 lastcp = e.race_respawn_spotref;
1169 if(nextcpindex == lastcpindex)
1173 IL_EACH(g_racecheckpoints, true,
1175 if(it.race_checkpoint != lastcpindex)
1180 o0 = (it.absmin + it.absmax) * 0.5;
1181 IL_EACH(g_racecheckpoints, true,
1183 if(it.race_checkpoint != nextcpindex)
1185 o1 = (it.absmin + it.absmax) * 0.5;
1188 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1189 if(fraction < bestfraction)
1190 bestfraction = fraction;
1194 // we are at CP "nextcpindex - bestfraction"
1195 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1196 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1198 nc = race_highest_checkpoint + 1;
1199 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;