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 IntrusiveList g_race_targets;
19 STATIC_INIT(g_race_targets) { g_race_targets = IL_NEW(); }
21 void race_InitSpectator()
24 if(msg_entity.enemy.race_laptime)
25 race_SendNextCheckpoint(msg_entity.enemy, 1);
28 void W_Porto_Fail(entity this, float failhard);
30 float race_readTime(string map, float pos)
32 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
34 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
37 string race_readUID(string map, float pos)
39 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
41 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
44 float race_readPos(string map, float t)
46 for(int i = 1; i <= RANKINGS_CNT; ++i)
48 int mytime = race_readTime(map, i);
49 if(!mytime || mytime > t)
53 return 0; // pos is zero if unranked
56 void race_writeTime(string map, float t, string myuid)
58 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
61 newpos = race_readPos(map, t);
64 for(i = 1; i <= RANKINGS_CNT; ++i)
66 if(race_readUID(map, i) == myuid)
71 // player improved his existing record, only have to iterate on ranks between new and old recs
72 for (i = prevpos; i > newpos; --i)
74 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
75 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
80 // player has no ranked record yet
81 for (i = RANKINGS_CNT; i > newpos; --i)
83 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
84 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
88 // store new time itself
89 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
90 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
93 string race_readName(string map, float pos)
95 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
97 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
101 const float MAX_CHECKPOINTS = 255;
103 spawnfunc(target_checkpoint);
106 .float race_penalty_accumulator;
107 .string race_penalty_reason;
108 .float race_checkpoint; // player: next checkpoint that has to be reached
109 .entity race_lastpenalty;
113 float race_checkpoint_records[MAX_CHECKPOINTS];
114 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
115 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
116 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
117 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
119 .float race_checkpoint_record[MAX_CHECKPOINTS];
121 float race_highest_checkpoint;
122 float race_timed_checkpoint;
125 float defragcpexists;
127 float race_NextCheckpoint(float f)
129 if(f >= race_highest_checkpoint)
135 float race_PreviousCheckpoint(float f)
140 return race_highest_checkpoint;
146 // 0 = common start/finish
149 float race_CheckpointNetworkID(float f)
151 if(race_timed_checkpoint)
155 else if(f == race_timed_checkpoint)
156 return 255; // finish
161 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
166 int cp = e.race_checkpoint;
167 float recordtime = race_checkpoint_records[cp];
168 float myrecordtime = e.race_checkpoint_record[cp];
169 string recordholder = race_checkpoint_recordholders[cp];
170 if(recordholder == e.netname)
173 if(!IS_REAL_CLIENT(e))
178 WRITESPECTATABLE_MSG_ONE(msg_entity, {
179 WriteHeader(MSG_ONE, TE_CSQC_RACE);
182 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
183 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
184 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
187 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
188 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
189 WriteInt24_t(MSG_ONE, recordtime);
191 WriteInt24_t(MSG_ONE, myrecordtime);
192 WriteString(MSG_ONE, recordholder);
196 void race_send_recordtime(float msg)
198 // send the server best time
199 WriteHeader(msg, TE_CSQC_RACE);
200 WriteByte(msg, RACE_NET_SERVER_RECORD);
201 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
205 void race_send_speedaward(float msg)
207 // send the best speed of the round
208 WriteHeader(msg, TE_CSQC_RACE);
209 WriteByte(msg, RACE_NET_SPEED_AWARD);
210 WriteInt24_t(msg, floor(speedaward_speed+0.5));
211 WriteString(msg, speedaward_holder);
214 void race_send_speedaward_alltimebest(float msg)
216 // send the best speed
217 WriteHeader(msg, TE_CSQC_RACE);
218 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
219 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
220 WriteString(msg, speedaward_alltimebest_holder);
223 void race_SendRankings(float pos, float prevpos, float del, float msg)
225 WriteHeader(msg, TE_CSQC_RACE);
226 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
227 WriteShort(msg, pos);
228 WriteShort(msg, prevpos);
229 WriteShort(msg, del);
230 WriteString(msg, race_readName(GetMapname(), pos));
231 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
234 void race_SendStatus(float id, entity e)
236 if(!IS_REAL_CLIENT(e))
245 WRITESPECTATABLE_MSG_ONE(msg_entity, {
246 WriteHeader(msg, TE_CSQC_RACE);
247 WriteByte(msg, RACE_NET_SERVER_STATUS);
249 WriteString(msg, e.netname);
253 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
255 // netname only used TEMPORARILY for printing
256 int newpos = race_readPos(map, t);
258 int player_prevpos = 0;
259 for(int i = 1; i <= RANKINGS_CNT; ++i)
261 if(race_readUID(map, i) == myuid)
266 string oldrec_holder;
267 if (player_prevpos && (player_prevpos < newpos || !newpos))
269 oldrec = race_readTime(GetMapname(), player_prevpos);
270 race_SendStatus(0, e); // "fail"
272 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
277 // no ranking, time worse than the worst ranked
278 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
279 race_SendStatus(0, e); // "fail"
281 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
285 // if we didn't hit a return yet, we have a new record!
287 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
291 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
295 if(uid2name(myuid) == "^1Unregistered Player")
298 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
302 oldrec = race_readTime(GetMapname(), newpos);
303 oldrec_holder = race_readName(GetMapname(), newpos);
306 race_writeTime(GetMapname(), t, myuid);
308 if (newpos == 1 && showmessage)
310 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
311 race_send_recordtime(MSG_ALL);
314 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
316 strunzone(rankings_reply);
317 rankings_reply = strzone(getrankings());
319 if(newpos == player_prevpos)
322 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
323 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
324 else { race_SendStatus(1, e); } // "new time"
329 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
330 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
331 else { race_SendStatus(2, e); } // "new rank"
336 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
337 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
338 else { race_SendStatus(2, e); } // "new rank"
342 void race_deleteTime(string map, float pos)
344 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
346 for(int i = pos; i <= RANKINGS_CNT; ++i)
348 string therank = ftos(i);
349 if (i == RANKINGS_CNT)
351 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
352 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
356 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
357 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
361 race_SendRankings(pos, 0, 1, MSG_ALL);
363 race_send_recordtime(MSG_ALL);
366 strunzone(rankings_reply);
367 rankings_reply = strzone(getrankings());
370 void race_SendTime(entity e, float cp, float t, float tvalid)
374 if(g_race_qualifying)
375 t += e.race_penalty_accumulator;
377 t = TIME_ENCODE(t); // make integer
380 if(cp == race_timed_checkpoint) // finish line
381 if (!CS(e).race_completed)
384 if(g_race_qualifying)
386 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
388 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
392 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
394 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
396 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
397 snew = TIME_ENCODE(time - game_starttime);
398 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
399 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
401 if(autocvar_fraglimit)
402 if(l >= autocvar_fraglimit)
403 race_StartCompleting();
407 CS(e).race_completed = 1;
408 MAKE_INDEPENDENT_PLAYER(e);
409 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
415 if(g_race_qualifying)
422 recordtime = race_checkpoint_records[cp];
423 float myrecordtime = e.race_checkpoint_record[cp];
424 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
425 if(recordholder == e.netname)
430 if(cp == race_timed_checkpoint)
432 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
433 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
435 if(t < myrecordtime || myrecordtime == 0)
436 e.race_checkpoint_record[cp] = t; // resending done below
438 if(t < recordtime || recordtime == 0)
440 race_checkpoint_records[cp] = t;
441 if(race_checkpoint_recordholders[cp])
442 strunzone(race_checkpoint_recordholders[cp]);
443 race_checkpoint_recordholders[cp] = strzone(e.netname);
444 if(g_race_qualifying)
445 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, LAMBDA(race_SendNextCheckpoint(it, 0)));
458 if(IS_REAL_CLIENT(e))
460 if(g_race_qualifying)
462 FOREACH_CLIENT(IS_REAL_CLIENT(it),
464 if(it == e || (IS_SPEC(it) && it.enemy == e))
467 WriteHeader(MSG_ONE, TE_CSQC_RACE);
468 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
469 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
470 WriteInt24_t(MSG_ONE, t); // time to that intermediate
471 WriteInt24_t(MSG_ONE, recordtime); // previously best time
472 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
473 WriteString(MSG_ONE, recordholder); // record holder
479 else // RACE! Not Qualifying
481 float mylaps, lother, othtime;
482 entity oth = race_checkpoint_lastplayers[cp];
485 mylaps = PlayerScore_Add(e, SP_RACE_LAPS, 0);
486 lother = race_checkpoint_lastlaps[cp];
487 othtime = race_checkpoint_lasttimes[cp];
490 mylaps = lother = othtime = 0;
492 if(IS_REAL_CLIENT(e))
495 WRITESPECTATABLE_MSG_ONE(msg_entity, {
496 WriteHeader(MSG_ONE, TE_CSQC_RACE);
497 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
498 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
501 WriteInt24_t(MSG_ONE, 0);
502 WriteByte(MSG_ONE, 0);
503 WriteByte(MSG_ONE, 0);
507 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
508 WriteByte(MSG_ONE, mylaps - lother);
509 WriteByte(MSG_ONE, etof(oth)); // record holder
514 race_checkpoint_lastplayers[cp] = e;
515 race_checkpoint_lasttimes[cp] = time;
516 race_checkpoint_lastlaps[cp] = mylaps;
518 if(IS_REAL_CLIENT(oth))
521 WRITESPECTATABLE_MSG_ONE(msg_entity, {
522 WriteHeader(MSG_ONE, TE_CSQC_RACE);
523 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
524 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
527 WriteInt24_t(MSG_ONE, 0);
528 WriteByte(MSG_ONE, 0);
529 WriteByte(MSG_ONE, 0);
533 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
534 WriteByte(MSG_ONE, lother - mylaps);
535 WriteByte(MSG_ONE, etof(e) - 1); // record holder
542 void race_ClearTime(entity e)
544 e.race_checkpoint = 0;
546 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
547 e.race_penalty_accumulator = 0;
548 e.race_lastpenalty = NULL;
550 if(!IS_REAL_CLIENT(e))
554 WRITESPECTATABLE_MSG_ONE(msg_entity, {
555 WriteHeader(MSG_ONE, TE_CSQC_RACE);
556 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
560 void checkpoint_passed(entity this, entity player)
562 if(player.personal && autocvar_g_allow_checkpoints)
563 return; // practice mode!
565 if(player.classname == "porto")
567 // do not allow portalling through checkpoints
568 trace_plane_normal = normalize(-1 * player.velocity);
569 W_Porto_Fail(player, 0);
573 string oldmsg; // used twice
578 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
580 oldmsg = this.message;
582 SUB_UseTargets(this, player, player);
583 this.message = oldmsg;
586 if (!IS_PLAYER(player))
590 * Remove unauthorized equipment
592 Portal_ClearAll(player);
594 player.porto_forbidden = 2; // decreased by 1 each StartFrame
598 if(this.race_checkpoint == -2)
600 this.race_checkpoint = player.race_checkpoint;
603 int cp_amount = 0, largest_cp_id = 0;
604 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
607 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
611 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
613 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
618 largest_cp_id = it.race_checkpoint;
619 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
621 it.race_checkpoint = largest_cp_id + 1; // finish line
623 race_highest_checkpoint = largest_cp_id + 1;
624 race_timed_checkpoint = largest_cp_id + 1;
630 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
632 it.race_checkpoint = 1;
634 race_highest_checkpoint = 1;
635 race_timed_checkpoint = 1;
639 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
641 if(this.race_penalty)
643 if(player.race_lastpenalty != this)
645 player.race_lastpenalty = this;
646 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
650 if(player.race_penalty)
656 if(this.spawnflags & 2)
658 oldmsg = this.message;
660 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
661 this.message = oldmsg;
664 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
665 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
666 player.race_respawn_checkpoint = this.race_checkpoint;
667 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
668 player.race_started = 1;
670 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
672 if(!this.race_checkpoint) // start line
674 player.race_laptime = time;
675 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
676 player.race_penalty_accumulator = 0;
677 player.race_lastpenalty = NULL;
680 if(g_race_qualifying)
681 race_SendNextCheckpoint(player, 0);
683 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
686 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
689 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
691 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
697 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
703 if(this.spawnflags & 4)
704 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, player.origin, '0 0 0');
708 void checkpoint_touch(entity this, entity toucher)
710 EXACTTRIGGER_TOUCH(this, toucher);
711 checkpoint_passed(this, toucher);
714 void checkpoint_use(entity this, entity actor, entity trigger)
716 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
719 checkpoint_passed(this, actor);
722 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
724 entity own = this.owner;
726 own = this.realowner; // target support
728 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
730 else if(view.race_checkpoint == own.race_checkpoint)
736 void trigger_race_checkpoint_verify(entity this)
738 static bool have_verified;
739 if (have_verified) return;
740 have_verified = true;
742 bool qual = g_race_qualifying;
744 int pl_race_checkpoint = 0;
745 int pl_race_place = 0;
748 for (int i = 0; i <= race_highest_checkpoint; ++i) {
749 pl_race_checkpoint = race_NextCheckpoint(i);
751 // race only (middle of the race)
752 g_race_qualifying = false;
754 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
755 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
760 g_race_qualifying = 1;
761 pl_race_place = race_lowest_place_spawn;
762 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
763 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
766 // race only (initial spawn)
767 g_race_qualifying = 0;
768 for (int p = 1; p <= race_highest_place_spawn; ++p) {
770 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
771 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
776 } else if (!defrag_ents) {
778 pl_race_checkpoint = race_NextCheckpoint(0);
779 g_race_qualifying = 1;
780 pl_race_place = race_lowest_place_spawn;
781 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
782 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
785 pl_race_checkpoint = race_NextCheckpoint(0);
786 g_race_qualifying = 1;
787 pl_race_place = 0; // there's only one spawn on defrag maps
789 // check if a defragcp file already exists, then read it and apply the checkpoint order
794 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
796 while ((l = fgets(fh))) {
797 len = tokenize_console(l);
799 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
802 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
803 if (argv(0) == cp.targetname) {
804 cp.race_checkpoint = stof(argv(1));
812 g_race_qualifying = qual;
814 IL_EACH(g_race_targets, true,
817 FOREACH_ENTITY_STRING(target, cpt.targetname,
819 vector org = (it.absmin + it.absmax) * 0.5;
820 if(cpt.race_checkpoint == 0)
821 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
823 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
825 it.sprite.realowner = cpt;
826 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
830 if (race_timed_checkpoint) {
832 IL_EACH(g_race_targets, true,
835 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
836 FOREACH_ENTITY_STRING(target, cpt.targetname, {
837 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
840 if(it.classname == "target_checkpoint") {
841 if(it.race_checkpoint == -2)
842 defragcpexists = -1; // 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
845 if (defragcpexists != -1) {
846 float largest_cp_id = 0;
847 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
848 if (cp.race_checkpoint > largest_cp_id) {
849 largest_cp_id = cp.race_checkpoint;
852 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
853 cp.race_checkpoint = largest_cp_id + 1; // finish line
855 race_highest_checkpoint = largest_cp_id + 1;
856 race_timed_checkpoint = largest_cp_id + 1;
858 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
859 cp.race_checkpoint = 255; // finish line
861 race_highest_checkpoint = 255;
862 race_timed_checkpoint = 255;
865 IL_EACH(g_racecheckpoints, it.sprite,
867 if (it.race_checkpoint == 0) {
868 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
869 } else if (it.race_checkpoint == race_timed_checkpoint) {
870 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
877 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
878 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
879 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 this, entity player, entity spot, vector current)
904 if(g_race_qualifying)
907 if(this.race_checkpoint != 0)
909 if(spot.race_place != race_lowest_place_spawn)
914 if(this.race_checkpoint != player.race_respawn_checkpoint)
916 // try reusing the previous spawn
917 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
918 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
919 if(this.race_checkpoint == 0)
921 int pl = player.race_place;
922 if(pl > race_highest_place_spawn)
924 if(pl == 0 && !player.race_started)
925 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
926 if(spot.race_place != pl)
933 spawnfunc(trigger_race_checkpoint)
936 if(!g_race && !g_cts) { delete(this); return; }
940 this.use = checkpoint_use;
941 if (!(this.spawnflags & 1))
942 settouch(this, checkpoint_touch);
944 o = (this.absmin + this.absmax) * 0.5;
945 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
946 waypoint_spawnforitem_force(this, trace_endpos);
947 this.nearestwaypointtimeout = -1;
949 if(this.message == "")
950 this.message = "went backwards";
951 if (this.message2 == "")
952 this.message2 = "was pushed backwards by";
953 if (this.race_penalty_reason == "")
954 this.race_penalty_reason = "missing a checkpoint";
956 this.race_checkpoint = this.cnt;
958 if(this.race_checkpoint > race_highest_checkpoint)
960 race_highest_checkpoint = this.race_checkpoint;
961 if(this.spawnflags & 8)
962 race_timed_checkpoint = this.race_checkpoint;
964 race_timed_checkpoint = 0;
967 if(!this.race_penalty)
969 if(this.race_checkpoint)
970 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
972 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
975 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
976 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
978 IL_PUSH(g_racecheckpoints, this);
980 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
983 spawnfunc(target_checkpoint) // defrag entity
985 if(!g_race && !g_cts) { delete(this); return; }
988 // if this is targeted, then it probably isn't a trigger
989 bool is_trigger = !boolean(!this.nottargeted && this.targetname != "");
994 this.use = checkpoint_use;
995 if (is_trigger && !(this.spawnflags & 1))
996 settouch(this, checkpoint_touch);
998 vector org = this.origin;
1000 // bots should only pathfind to this if it is a valid touchable trigger
1003 org = (this.absmin + this.absmax) * 0.5;
1004 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1005 waypoint_spawnforitem_force(this, trace_endpos);
1006 this.nearestwaypointtimeout = -1;
1009 if(this.message == "")
1010 this.message = "went backwards";
1011 if (this.message2 == "")
1012 this.message2 = "was pushed backwards by";
1013 if (this.race_penalty_reason == "")
1014 this.race_penalty_reason = "missing a checkpoint";
1016 if(this.classname == "target_startTimer")
1017 this.race_checkpoint = 0;
1019 this.race_checkpoint = -2;
1021 race_timed_checkpoint = 1;
1023 IL_PUSH(g_race_targets, this);
1025 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1028 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1029 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1031 void race_AbandonRaceCheck(entity p)
1033 if(race_completing && !CS(p).race_completed)
1035 CS(p).race_completed = 1;
1036 MAKE_INDEPENDENT_PLAYER(p);
1037 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1038 ClientData_Touch(p);
1042 void race_StartCompleting()
1044 race_completing = 1;
1045 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), LAMBDA(race_AbandonRaceCheck(it)));
1048 void race_PreparePlayer(entity this)
1050 race_ClearTime(this);
1051 this.race_place = 0;
1052 this.race_started = 0;
1053 this.race_respawn_checkpoint = 0;
1054 this.race_respawn_spotref = NULL;
1057 void race_RetractPlayer(entity this)
1059 if(!g_race && !g_cts)
1061 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1062 race_ClearTime(this);
1063 this.race_checkpoint = this.race_respawn_checkpoint;
1066 spawnfunc(info_player_race)
1068 if(!g_race && !g_cts) { delete(this); return; }
1070 spawnfunc_info_player_deathmatch(this);
1072 if(this.race_place > race_highest_place_spawn)
1073 race_highest_place_spawn = this.race_place;
1074 if(this.race_place < race_lowest_place_spawn)
1075 race_lowest_place_spawn = this.race_place;
1078 void race_ClearRecords()
1080 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1082 race_checkpoint_records[j] = 0;
1083 if(race_checkpoint_recordholders[j])
1084 strunzone(race_checkpoint_recordholders[j]);
1085 race_checkpoint_recordholders[j] = string_null;
1088 FOREACH_CLIENT(true, LAMBDA(
1089 float p = it.race_place;
1090 race_PreparePlayer(it);
1095 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1097 if(g_race_qualifying)
1099 pl.race_penalty_accumulator += penalty;
1100 if(IS_REAL_CLIENT(pl))
1103 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1104 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1105 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1106 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1107 WriteString(MSG_ONE, reason);
1113 pl.race_penalty = time + penalty;
1114 if(IS_REAL_CLIENT(pl))
1117 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1118 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1119 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1120 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1121 WriteString(MSG_ONE, reason);
1127 void penalty_touch(entity this, entity toucher)
1129 EXACTTRIGGER_TOUCH(this, toucher);
1130 if(toucher.race_lastpenalty != this)
1132 toucher.race_lastpenalty = this;
1133 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1137 void penalty_use(entity this, entity actor, entity trigger)
1139 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1142 spawnfunc(trigger_race_penalty)
1144 // TODO: find out why this wasnt done:
1145 //if(!g_cts && !g_race) { remove(this); return; }
1149 this.use = penalty_use;
1150 if (!(this.spawnflags & 1))
1151 settouch(this, penalty_touch);
1153 if (this.race_penalty_reason == "")
1154 this.race_penalty_reason = "missing a checkpoint";
1155 if (!this.race_penalty)
1156 this.race_penalty = 5;
1159 float race_GetFractionalLapCount(entity e)
1161 // interesting metrics (idea by KrimZon) to maybe sort players in the
1162 // scoreboard, immediately updates when overtaking
1164 // requires the track to be built so you never get farther away from the
1165 // next checkpoint, though, and current Xonotic race maps are not built that
1168 // also, this code is slow and would need optimization (i.e. "next CP"
1169 // links on CP entities)
1172 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1173 if(CS(e).race_completed)
1174 return l; // not fractional
1177 float bestfraction, fraction;
1179 float nextcpindex, lastcpindex;
1181 nextcpindex = max(e.race_checkpoint, 0);
1182 lastcpindex = e.race_respawn_checkpoint;
1183 lastcp = e.race_respawn_spotref;
1185 if(nextcpindex == lastcpindex)
1189 IL_EACH(g_racecheckpoints, true,
1191 if(it.race_checkpoint != lastcpindex)
1196 o0 = (it.absmin + it.absmax) * 0.5;
1197 IL_EACH(g_racecheckpoints, true,
1199 if(it.race_checkpoint != nextcpindex)
1201 o1 = (it.absmin + it.absmax) * 0.5;
1204 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1205 if(fraction < bestfraction)
1206 bestfraction = fraction;
1210 // we are at CP "nextcpindex - bestfraction"
1211 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1212 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1214 nc = race_highest_checkpoint + 1;
1215 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;