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/state.qh>
14 #include "../common/triggers/subs.qh"
15 #include "../lib/warpzone/util_server.qh"
16 #include "../lib/warpzone/common.qh"
17 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
19 IntrusiveList g_race_targets;
20 STATIC_INIT(g_race_targets) { g_race_targets = IL_NEW(); }
22 void race_InitSpectator()
25 if(msg_entity.enemy.race_laptime)
26 race_SendNextCheckpoint(msg_entity.enemy, 1);
29 void W_Porto_Fail(entity this, float failhard);
31 float race_readTime(string map, float pos)
33 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
35 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
38 string race_readUID(string map, float pos)
40 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
42 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
45 float race_readPos(string map, float t)
47 for(int i = 1; i <= RANKINGS_CNT; ++i)
49 int mytime = race_readTime(map, i);
50 if(!mytime || mytime > t)
54 return 0; // pos is zero if unranked
57 void race_writeTime(string map, float t, string myuid)
59 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
62 newpos = race_readPos(map, t);
65 for(i = 1; i <= RANKINGS_CNT; ++i)
67 if(race_readUID(map, i) == myuid)
72 // player improved his existing record, only have to iterate on ranks between new and old recs
73 for (i = prevpos; i > newpos; --i)
75 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
76 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
81 // player has no ranked record yet
82 for (i = RANKINGS_CNT; i > newpos; --i)
84 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
85 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
89 // store new time itself
90 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
91 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
94 string race_readName(string map, float pos)
96 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
98 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
102 const float MAX_CHECKPOINTS = 255;
104 spawnfunc(target_checkpoint);
107 .float race_penalty_accumulator;
108 .string race_penalty_reason;
109 .float race_checkpoint; // player: next checkpoint that has to be reached
110 .entity race_lastpenalty;
114 float race_checkpoint_records[MAX_CHECKPOINTS];
115 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
116 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
117 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
118 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
120 .float race_checkpoint_record[MAX_CHECKPOINTS];
122 float race_highest_checkpoint;
123 float race_timed_checkpoint;
126 float defragcpexists;
128 float race_NextCheckpoint(float f)
130 if(f >= race_highest_checkpoint)
136 float race_PreviousCheckpoint(float f)
141 return race_highest_checkpoint;
147 // 0 = common start/finish
150 float race_CheckpointNetworkID(float f)
152 if(race_timed_checkpoint)
156 else if(f == race_timed_checkpoint)
157 return 255; // finish
162 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
167 int cp = e.race_checkpoint;
168 float recordtime = race_checkpoint_records[cp];
169 float myrecordtime = e.race_checkpoint_record[cp];
170 string recordholder = race_checkpoint_recordholders[cp];
171 if(recordholder == e.netname)
174 if(!IS_REAL_CLIENT(e))
179 WRITESPECTATABLE_MSG_ONE(msg_entity, {
180 WriteHeader(MSG_ONE, TE_CSQC_RACE);
183 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
184 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
185 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
188 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
189 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
190 WriteInt24_t(MSG_ONE, recordtime);
192 WriteInt24_t(MSG_ONE, myrecordtime);
193 WriteString(MSG_ONE, recordholder);
197 void race_send_recordtime(float msg)
199 // send the server best time
200 WriteHeader(msg, TE_CSQC_RACE);
201 WriteByte(msg, RACE_NET_SERVER_RECORD);
202 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
206 void race_send_speedaward(float msg)
208 // send the best speed of the round
209 WriteHeader(msg, TE_CSQC_RACE);
210 WriteByte(msg, RACE_NET_SPEED_AWARD);
211 WriteInt24_t(msg, floor(speedaward_speed+0.5));
212 WriteString(msg, speedaward_holder);
215 void race_send_speedaward_alltimebest(float msg)
217 // send the best speed
218 WriteHeader(msg, TE_CSQC_RACE);
219 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
220 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
221 WriteString(msg, speedaward_alltimebest_holder);
224 void race_SendRankings(float pos, float prevpos, float del, float msg)
226 WriteHeader(msg, TE_CSQC_RACE);
227 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
228 WriteShort(msg, pos);
229 WriteShort(msg, prevpos);
230 WriteShort(msg, del);
231 WriteString(msg, race_readName(GetMapname(), pos));
232 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
235 void race_SendStatus(float id, entity e)
237 if(!IS_REAL_CLIENT(e))
246 WRITESPECTATABLE_MSG_ONE(msg_entity, {
247 WriteHeader(msg, TE_CSQC_RACE);
248 WriteByte(msg, RACE_NET_SERVER_STATUS);
250 WriteString(msg, e.netname);
254 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
256 // netname only used TEMPORARILY for printing
257 int newpos = race_readPos(map, t);
259 int player_prevpos = 0;
260 for(int i = 1; i <= RANKINGS_CNT; ++i)
262 if(race_readUID(map, i) == myuid)
267 string oldrec_holder;
268 if (player_prevpos && (player_prevpos < newpos || !newpos))
270 oldrec = race_readTime(GetMapname(), player_prevpos);
271 race_SendStatus(0, e); // "fail"
273 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
278 // no ranking, time worse than the worst ranked
279 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
280 race_SendStatus(0, e); // "fail"
282 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
286 // if we didn't hit a return yet, we have a new record!
288 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
292 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
296 if(uid2name(myuid) == "^1Unregistered Player")
299 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
303 oldrec = race_readTime(GetMapname(), newpos);
304 oldrec_holder = race_readName(GetMapname(), newpos);
307 race_writeTime(GetMapname(), t, myuid);
309 if (newpos == 1 && showmessage)
311 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
312 race_send_recordtime(MSG_ALL);
315 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
317 strunzone(rankings_reply);
318 rankings_reply = strzone(getrankings());
320 if(newpos == player_prevpos)
323 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
324 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
325 else { race_SendStatus(1, e); } // "new time"
330 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
331 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
332 else { race_SendStatus(2, e); } // "new rank"
337 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
338 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
339 else { race_SendStatus(2, e); } // "new rank"
343 void race_deleteTime(string map, float pos)
345 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
347 for(int i = pos; i <= RANKINGS_CNT; ++i)
349 string therank = ftos(i);
350 if (i == RANKINGS_CNT)
352 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
353 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
357 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
358 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
362 race_SendRankings(pos, 0, 1, MSG_ALL);
364 race_send_recordtime(MSG_ALL);
367 strunzone(rankings_reply);
368 rankings_reply = strzone(getrankings());
371 void race_SendTime(entity e, float cp, float t, float tvalid)
375 if(g_race_qualifying)
376 t += e.race_penalty_accumulator;
378 t = TIME_ENCODE(t); // make integer
381 if(cp == race_timed_checkpoint) // finish line
382 if (!CS(e).race_completed)
385 if(g_race_qualifying)
387 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
389 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
393 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
395 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
397 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
398 snew = TIME_ENCODE(time - game_starttime);
399 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
400 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
402 if(autocvar_fraglimit)
403 if(l >= autocvar_fraglimit)
404 race_StartCompleting();
408 CS(e).race_completed = 1;
409 MAKE_INDEPENDENT_PLAYER(e);
410 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
416 if(g_race_qualifying)
423 recordtime = race_checkpoint_records[cp];
424 float myrecordtime = e.race_checkpoint_record[cp];
425 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
426 if(recordholder == e.netname)
431 if(cp == race_timed_checkpoint)
433 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
434 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
436 if(t < myrecordtime || myrecordtime == 0)
437 e.race_checkpoint_record[cp] = t; // resending done below
439 if(t < recordtime || recordtime == 0)
441 race_checkpoint_records[cp] = t;
442 if(race_checkpoint_recordholders[cp])
443 strunzone(race_checkpoint_recordholders[cp]);
444 race_checkpoint_recordholders[cp] = strzone(e.netname);
445 if(g_race_qualifying)
446 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, LAMBDA(race_SendNextCheckpoint(it, 0)));
459 if(IS_REAL_CLIENT(e))
461 if(g_race_qualifying)
463 FOREACH_CLIENT(IS_REAL_CLIENT(it),
465 if(it == e || (IS_SPEC(it) && it.enemy == e))
468 WriteHeader(MSG_ONE, TE_CSQC_RACE);
469 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
470 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
471 WriteInt24_t(MSG_ONE, t); // time to that intermediate
472 WriteInt24_t(MSG_ONE, recordtime); // previously best time
473 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
474 WriteString(MSG_ONE, recordholder); // record holder
480 else // RACE! Not Qualifying
482 float mylaps, lother, othtime;
483 entity oth = race_checkpoint_lastplayers[cp];
486 mylaps = PlayerScore_Add(e, SP_RACE_LAPS, 0);
487 lother = race_checkpoint_lastlaps[cp];
488 othtime = race_checkpoint_lasttimes[cp];
491 mylaps = lother = othtime = 0;
493 if(IS_REAL_CLIENT(e))
496 WRITESPECTATABLE_MSG_ONE(msg_entity, {
497 WriteHeader(MSG_ONE, TE_CSQC_RACE);
498 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
499 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
502 WriteInt24_t(MSG_ONE, 0);
503 WriteByte(MSG_ONE, 0);
504 WriteByte(MSG_ONE, 0);
508 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
509 WriteByte(MSG_ONE, mylaps - lother);
510 WriteByte(MSG_ONE, etof(oth)); // record holder
515 race_checkpoint_lastplayers[cp] = e;
516 race_checkpoint_lasttimes[cp] = time;
517 race_checkpoint_lastlaps[cp] = mylaps;
519 if(IS_REAL_CLIENT(oth))
522 WRITESPECTATABLE_MSG_ONE(msg_entity, {
523 WriteHeader(MSG_ONE, TE_CSQC_RACE);
524 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
525 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
528 WriteInt24_t(MSG_ONE, 0);
529 WriteByte(MSG_ONE, 0);
530 WriteByte(MSG_ONE, 0);
534 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
535 WriteByte(MSG_ONE, lother - mylaps);
536 WriteByte(MSG_ONE, etof(e) - 1); // record holder
543 void race_ClearTime(entity e)
545 e.race_checkpoint = 0;
547 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
548 e.race_penalty_accumulator = 0;
549 e.race_lastpenalty = NULL;
551 if(!IS_REAL_CLIENT(e))
555 WRITESPECTATABLE_MSG_ONE(msg_entity, {
556 WriteHeader(MSG_ONE, TE_CSQC_RACE);
557 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
561 void checkpoint_passed(entity this, entity player)
563 if(player.personal && autocvar_g_allow_checkpoints)
564 return; // practice mode!
566 if(player.classname == "porto")
568 // do not allow portalling through checkpoints
569 trace_plane_normal = normalize(-1 * player.velocity);
570 W_Porto_Fail(player, 0);
574 string oldmsg; // used twice
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 int cp_amount = 0, largest_cp_id = 0;
605 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
608 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
612 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
614 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
619 largest_cp_id = it.race_checkpoint;
620 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
622 it.race_checkpoint = largest_cp_id + 1; // finish line
624 race_highest_checkpoint = largest_cp_id + 1;
625 race_timed_checkpoint = largest_cp_id + 1;
631 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
633 it.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 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
692 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
698 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
704 if(this.spawnflags & 4)
705 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, player.origin, '0 0 0');
709 void checkpoint_touch(entity this, entity toucher)
711 EXACTTRIGGER_TOUCH(this, toucher);
712 checkpoint_passed(this, toucher);
715 void checkpoint_use(entity this, entity actor, entity trigger)
717 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
720 checkpoint_passed(this, actor);
723 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
725 entity own = this.owner;
727 own = this.realowner; // target support
729 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
731 else if(view.race_checkpoint == own.race_checkpoint)
737 void trigger_race_checkpoint_verify(entity this)
739 static bool have_verified;
740 if (have_verified) return;
741 have_verified = true;
743 bool qual = g_race_qualifying;
745 int pl_race_checkpoint = 0;
746 int pl_race_place = 0;
749 for (int i = 0; i <= race_highest_checkpoint; ++i) {
750 pl_race_checkpoint = race_NextCheckpoint(i);
752 // race only (middle of the race)
753 g_race_qualifying = false;
755 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
756 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
761 g_race_qualifying = 1;
762 pl_race_place = race_lowest_place_spawn;
763 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
764 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
767 // race only (initial spawn)
768 g_race_qualifying = 0;
769 for (int p = 1; p <= race_highest_place_spawn; ++p) {
771 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
772 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
777 } else if (!defrag_ents) {
779 pl_race_checkpoint = race_NextCheckpoint(0);
780 g_race_qualifying = 1;
781 pl_race_place = race_lowest_place_spawn;
782 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
783 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
786 pl_race_checkpoint = race_NextCheckpoint(0);
787 g_race_qualifying = 1;
788 pl_race_place = 0; // there's only one spawn on defrag maps
790 // check if a defragcp file already exists, then read it and apply the checkpoint order
795 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
797 while ((l = fgets(fh))) {
798 len = tokenize_console(l);
800 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
803 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
804 if (argv(0) == cp.targetname) {
805 cp.race_checkpoint = stof(argv(1));
813 g_race_qualifying = qual;
815 IL_EACH(g_race_targets, true,
818 FOREACH_ENTITY_STRING(target, cpt.targetname,
820 vector org = (it.absmin + it.absmax) * 0.5;
821 if(cpt.race_checkpoint == 0)
822 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
824 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
826 it.sprite.realowner = cpt;
827 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
831 if (race_timed_checkpoint) {
833 IL_EACH(g_race_targets, true,
836 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
837 FOREACH_ENTITY_STRING(target, cpt.targetname, {
838 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
841 if(it.classname == "target_checkpoint") {
842 if(it.race_checkpoint == -2)
843 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
846 if (defragcpexists != -1) {
847 float largest_cp_id = 0;
848 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
849 if (cp.race_checkpoint > largest_cp_id) {
850 largest_cp_id = cp.race_checkpoint;
853 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
854 cp.race_checkpoint = largest_cp_id + 1; // finish line
856 race_highest_checkpoint = largest_cp_id + 1;
857 race_timed_checkpoint = largest_cp_id + 1;
859 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
860 cp.race_checkpoint = 255; // finish line
862 race_highest_checkpoint = 255;
863 race_timed_checkpoint = 255;
866 IL_EACH(g_racecheckpoints, it.sprite,
868 if (it.race_checkpoint == 0) {
869 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
870 } else if (it.race_checkpoint == race_timed_checkpoint) {
871 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
878 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
879 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
880 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
886 // These just make the game crash on some maps with oddly shaped triggers.
887 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
888 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
889 // (happened on item triggers too)
894 //setsize(targ, trigger.mins, trigger.maxs);
895 //setorigin(targ, trigger.origin);
903 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
905 if(g_race_qualifying)
908 if(this.race_checkpoint != 0)
910 if(spot.race_place != race_lowest_place_spawn)
915 if(this.race_checkpoint != player.race_respawn_checkpoint)
917 // try reusing the previous spawn
918 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
919 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
920 if(this.race_checkpoint == 0)
922 int 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) { delete(this); return; }
941 this.use = checkpoint_use;
942 if (!(this.spawnflags & 1))
943 settouch(this, checkpoint_touch);
945 o = (this.absmin + this.absmax) * 0.5;
946 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
947 waypoint_spawnforitem_force(this, trace_endpos);
948 this.nearestwaypointtimeout = -1;
950 if(this.message == "")
951 this.message = "went backwards";
952 if (this.message2 == "")
953 this.message2 = "was pushed backwards by";
954 if (this.race_penalty_reason == "")
955 this.race_penalty_reason = "missing a checkpoint";
957 this.race_checkpoint = this.cnt;
959 if(this.race_checkpoint > race_highest_checkpoint)
961 race_highest_checkpoint = this.race_checkpoint;
962 if(this.spawnflags & 8)
963 race_timed_checkpoint = this.race_checkpoint;
965 race_timed_checkpoint = 0;
968 if(!this.race_penalty)
970 if(this.race_checkpoint)
971 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
973 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
976 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
977 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
979 IL_PUSH(g_racecheckpoints, this);
981 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
984 spawnfunc(target_checkpoint) // defrag entity
986 if(!g_race && !g_cts) { delete(this); return; }
989 // if this is targeted, then it probably isn't a trigger
990 bool is_trigger = !boolean(!this.nottargeted && this.targetname != "");
995 this.use = checkpoint_use;
996 if (is_trigger && !(this.spawnflags & 1))
997 settouch(this, checkpoint_touch);
999 vector org = this.origin;
1001 // bots should only pathfind to this if it is a valid touchable trigger
1004 org = (this.absmin + this.absmax) * 0.5;
1005 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1006 waypoint_spawnforitem_force(this, trace_endpos);
1007 this.nearestwaypointtimeout = -1;
1010 if(this.message == "")
1011 this.message = "went backwards";
1012 if (this.message2 == "")
1013 this.message2 = "was pushed backwards by";
1014 if (this.race_penalty_reason == "")
1015 this.race_penalty_reason = "missing a checkpoint";
1017 if(this.classname == "target_startTimer")
1018 this.race_checkpoint = 0;
1020 this.race_checkpoint = -2;
1022 race_timed_checkpoint = 1;
1024 IL_PUSH(g_race_targets, this);
1026 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1029 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1030 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1032 void race_AbandonRaceCheck(entity p)
1034 if(race_completing && !CS(p).race_completed)
1036 CS(p).race_completed = 1;
1037 MAKE_INDEPENDENT_PLAYER(p);
1038 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1039 ClientData_Touch(p);
1043 void race_StartCompleting()
1045 race_completing = 1;
1046 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), LAMBDA(race_AbandonRaceCheck(it)));
1049 void race_PreparePlayer(entity this)
1051 race_ClearTime(this);
1052 this.race_place = 0;
1053 this.race_started = 0;
1054 this.race_respawn_checkpoint = 0;
1055 this.race_respawn_spotref = NULL;
1058 void race_RetractPlayer(entity this)
1060 if(!g_race && !g_cts)
1062 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1063 race_ClearTime(this);
1064 this.race_checkpoint = this.race_respawn_checkpoint;
1067 spawnfunc(info_player_race)
1069 if(!g_race && !g_cts) { delete(this); return; }
1071 spawnfunc_info_player_deathmatch(this);
1073 if(this.race_place > race_highest_place_spawn)
1074 race_highest_place_spawn = this.race_place;
1075 if(this.race_place < race_lowest_place_spawn)
1076 race_lowest_place_spawn = this.race_place;
1079 void race_ClearRecords()
1081 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1083 race_checkpoint_records[j] = 0;
1084 if(race_checkpoint_recordholders[j])
1085 strunzone(race_checkpoint_recordholders[j]);
1086 race_checkpoint_recordholders[j] = string_null;
1089 FOREACH_CLIENT(true, LAMBDA(
1090 float p = it.race_place;
1091 race_PreparePlayer(it);
1096 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1098 if(g_race_qualifying)
1100 pl.race_penalty_accumulator += penalty;
1101 if(IS_REAL_CLIENT(pl))
1104 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1105 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1106 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1107 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1108 WriteString(MSG_ONE, reason);
1114 pl.race_penalty = time + penalty;
1115 if(IS_REAL_CLIENT(pl))
1118 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1119 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1120 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1121 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1122 WriteString(MSG_ONE, reason);
1128 void penalty_touch(entity this, entity toucher)
1130 EXACTTRIGGER_TOUCH(this, toucher);
1131 if(toucher.race_lastpenalty != this)
1133 toucher.race_lastpenalty = this;
1134 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1138 void penalty_use(entity this, entity actor, entity trigger)
1140 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1143 spawnfunc(trigger_race_penalty)
1145 // TODO: find out why this wasnt done:
1146 //if(!g_cts && !g_race) { remove(this); return; }
1150 this.use = penalty_use;
1151 if (!(this.spawnflags & 1))
1152 settouch(this, penalty_touch);
1154 if (this.race_penalty_reason == "")
1155 this.race_penalty_reason = "missing a checkpoint";
1156 if (!this.race_penalty)
1157 this.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(CS(e).race_completed)
1175 return l; // not fractional
1178 float bestfraction, fraction;
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 IL_EACH(g_racecheckpoints, true,
1192 if(it.race_checkpoint != lastcpindex)
1197 o0 = (it.absmin + it.absmax) * 0.5;
1198 IL_EACH(g_racecheckpoints, true,
1200 if(it.race_checkpoint != nextcpindex)
1202 o1 = (it.absmin + it.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;