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_highest_checkpoint;
120 float race_timed_checkpoint;
123 float defragcpexists;
125 float race_NextCheckpoint(float f)
127 if(f >= race_highest_checkpoint)
133 float race_PreviousCheckpoint(float f)
138 return race_highest_checkpoint;
144 // 0 = common start/finish
147 float race_CheckpointNetworkID(float f)
149 if(race_timed_checkpoint)
153 else if(f == race_timed_checkpoint)
154 return 255; // finish
159 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
168 cp = e.race_checkpoint;
169 recordtime = race_checkpoint_records[cp];
170 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);
191 WriteString(MSG_ONE, recordholder);
195 void race_send_recordtime(float msg)
197 // send the server best time
198 WriteHeader(msg, TE_CSQC_RACE);
199 WriteByte(msg, RACE_NET_SERVER_RECORD);
200 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
204 void race_send_speedaward(float msg)
206 // send the best speed of the round
207 WriteHeader(msg, TE_CSQC_RACE);
208 WriteByte(msg, RACE_NET_SPEED_AWARD);
209 WriteInt24_t(msg, floor(speedaward_speed+0.5));
210 WriteString(msg, speedaward_holder);
213 void race_send_speedaward_alltimebest(float msg)
215 // send the best speed
216 WriteHeader(msg, TE_CSQC_RACE);
217 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
218 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
219 WriteString(msg, speedaward_alltimebest_holder);
222 void race_SendRankings(float pos, float prevpos, float del, float msg)
224 WriteHeader(msg, TE_CSQC_RACE);
225 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
226 WriteShort(msg, pos);
227 WriteShort(msg, prevpos);
228 WriteShort(msg, del);
229 WriteString(msg, race_readName(GetMapname(), pos));
230 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
233 void race_SendStatus(float id, entity e)
235 if(!IS_REAL_CLIENT(e))
244 WRITESPECTATABLE_MSG_ONE(msg_entity, {
245 WriteHeader(msg, TE_CSQC_RACE);
246 WriteByte(msg, RACE_NET_SERVER_STATUS);
248 WriteString(msg, e.netname);
252 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
254 // netname only used TEMPORARILY for printing
255 int newpos = race_readPos(map, t);
257 int player_prevpos = 0;
258 for(int i = 1; i <= RANKINGS_CNT; ++i)
260 if(race_readUID(map, i) == myuid)
265 string oldrec_holder;
266 if (player_prevpos && (player_prevpos < newpos || !newpos))
268 oldrec = race_readTime(GetMapname(), player_prevpos);
269 race_SendStatus(0, e); // "fail"
271 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
276 // no ranking, time worse than the worst ranked
277 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
278 race_SendStatus(0, e); // "fail"
280 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
284 // if we didn't hit a return yet, we have a new record!
286 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
290 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
294 if(uid2name(myuid) == "^1Unregistered Player")
297 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
301 oldrec = race_readTime(GetMapname(), newpos);
302 oldrec_holder = race_readName(GetMapname(), newpos);
305 race_writeTime(GetMapname(), t, myuid);
307 if (newpos == 1 && showmessage)
309 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
310 race_send_recordtime(MSG_ALL);
313 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
315 strunzone(rankings_reply);
316 rankings_reply = strzone(getrankings());
318 if(newpos == player_prevpos)
321 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
322 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
323 else { race_SendStatus(1, e); } // "new time"
328 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
329 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
330 else { race_SendStatus(2, e); } // "new rank"
335 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
336 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
337 else { race_SendStatus(2, e); } // "new rank"
341 void race_deleteTime(string map, float pos)
343 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
345 for(int i = pos; i <= RANKINGS_CNT; ++i)
347 string therank = ftos(i);
348 if (i == RANKINGS_CNT)
350 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
351 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
355 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
356 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
360 race_SendRankings(pos, 0, 1, MSG_ALL);
362 race_send_recordtime(MSG_ALL);
365 strunzone(rankings_reply);
366 rankings_reply = strzone(getrankings());
369 void race_SendTime(entity e, float cp, float t, float tvalid)
373 if(g_race_qualifying)
374 t += e.race_penalty_accumulator;
376 t = TIME_ENCODE(t); // make integer
379 if(cp == race_timed_checkpoint) // finish line
380 if (!e.race_completed)
383 if(g_race_qualifying)
385 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
387 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
391 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
393 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
395 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
396 snew = TIME_ENCODE(time - game_starttime);
397 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
398 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
400 if(autocvar_fraglimit)
401 if(l >= autocvar_fraglimit)
402 race_StartCompleting();
406 e.race_completed = 1;
407 MAKE_INDEPENDENT_PLAYER(e);
408 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
416 if(g_race_qualifying)
420 recordtime = race_checkpoint_records[cp];
421 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
422 if(recordholder == e.netname)
427 if(cp == race_timed_checkpoint)
429 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
430 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
432 if(t < recordtime || recordtime == 0)
434 race_checkpoint_records[cp] = t;
435 if(race_checkpoint_recordholders[cp])
436 strunzone(race_checkpoint_recordholders[cp]);
437 race_checkpoint_recordholders[cp] = strzone(e.netname);
438 if(g_race_qualifying)
440 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, LAMBDA(race_SendNextCheckpoint(it, 0)));
453 if(IS_REAL_CLIENT(e))
456 if(g_race_qualifying)
458 WRITESPECTATABLE_MSG_ONE(msg_entity, {
459 WriteHeader(MSG_ONE, TE_CSQC_RACE);
460 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
461 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
462 WriteInt24_t(MSG_ONE, t); // time to that intermediate
463 WriteInt24_t(MSG_ONE, recordtime); // previously best time
464 WriteString(MSG_ONE, recordholder); // record holder
469 else // RACE! Not Qualifying
471 float mylaps, lother, othtime;
473 oth = race_checkpoint_lastplayers[cp];
476 mylaps = PlayerScore_Add(e, SP_RACE_LAPS, 0);
477 lother = race_checkpoint_lastlaps[cp];
478 othtime = race_checkpoint_lasttimes[cp];
481 mylaps = lother = othtime = 0;
483 if(IS_REAL_CLIENT(e))
486 WRITESPECTATABLE_MSG_ONE(msg_entity, {
487 WriteHeader(MSG_ONE, TE_CSQC_RACE);
488 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
489 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
492 WriteInt24_t(MSG_ONE, 0);
493 WriteByte(MSG_ONE, 0);
494 WriteString(MSG_ONE, "");
498 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
499 WriteByte(MSG_ONE, mylaps - lother);
500 WriteString(MSG_ONE, oth.netname); // record holder
505 race_checkpoint_lastplayers[cp] = e;
506 race_checkpoint_lasttimes[cp] = time;
507 race_checkpoint_lastlaps[cp] = mylaps;
509 if(IS_REAL_CLIENT(oth))
512 WRITESPECTATABLE_MSG_ONE(msg_entity, {
513 WriteHeader(MSG_ONE, TE_CSQC_RACE);
514 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
515 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
518 WriteInt24_t(MSG_ONE, 0);
519 WriteByte(MSG_ONE, 0);
520 WriteString(MSG_ONE, "");
524 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
525 WriteByte(MSG_ONE, lother - mylaps);
526 WriteString(MSG_ONE, e.netname); // record holder
533 void race_ClearTime(entity e)
535 e.race_checkpoint = 0;
537 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
538 e.race_penalty_accumulator = 0;
539 e.race_lastpenalty = NULL;
541 if(!IS_REAL_CLIENT(e))
545 WRITESPECTATABLE_MSG_ONE(msg_entity, {
546 WriteHeader(MSG_ONE, TE_CSQC_RACE);
547 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
551 void dumpsurface(entity e)
555 LOG_INFO("Surfaces of ", etos(e), ":\n");
557 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
561 n = getsurfacenumpoints(e, si);
564 LOG_INFO(" Surface ", ftos(si), ":\n");
565 norm = getsurfacenormal(e, si);
566 LOG_INFO(" Normal = ", vtos(norm), "\n");
567 for(ni = 0; ni < n; ++ni)
569 vec = getsurfacepoint(e, si, ni);
570 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
575 void checkpoint_passed(entity this, entity player)
577 if(player.personal && autocvar_g_allow_checkpoints)
578 return; // practice mode!
583 if(player.classname == "porto")
585 // do not allow portalling through checkpoints
586 trace_plane_normal = normalize(-1 * player.velocity);
587 W_Porto_Fail(player, 0);
594 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
596 oldmsg = this.message;
598 SUB_UseTargets(this, player, player);
599 this.message = oldmsg;
602 if (!IS_PLAYER(player))
606 * Remove unauthorized equipment
608 Portal_ClearAll(player);
610 player.porto_forbidden = 2; // decreased by 1 each StartFrame
614 if(this.race_checkpoint == -2)
616 this.race_checkpoint = player.race_checkpoint;
619 float largest_cp_id = 0;
621 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
624 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
626 largest_cp_id = cp.race_checkpoint;
627 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
628 cp.race_checkpoint = largest_cp_id + 1; // finish line
629 race_highest_checkpoint = largest_cp_id + 1;
630 race_timed_checkpoint = largest_cp_id + 1;
632 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
634 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
641 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
642 cp.race_checkpoint = 1;
643 race_highest_checkpoint = 1;
644 race_timed_checkpoint = 1;
648 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
650 if(this.race_penalty)
652 if(player.race_lastpenalty != this)
654 player.race_lastpenalty = this;
655 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
659 if(player.race_penalty)
665 if(this.spawnflags & 2)
667 oldmsg = this.message;
669 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
670 this.message = oldmsg;
673 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
674 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
675 player.race_respawn_checkpoint = this.race_checkpoint;
676 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
677 player.race_started = 1;
679 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
681 if(!this.race_checkpoint) // start line
683 player.race_laptime = time;
684 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
685 player.race_penalty_accumulator = 0;
686 player.race_lastpenalty = NULL;
689 if(g_race_qualifying)
690 race_SendNextCheckpoint(player, 0);
692 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
695 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
698 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
699 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
704 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
710 if(this.spawnflags & 4)
711 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, player.origin, '0 0 0');
715 void checkpoint_touch(entity this, entity toucher)
717 EXACTTRIGGER_TOUCH(this, toucher);
718 checkpoint_passed(this, toucher);
721 void checkpoint_use(entity this, entity actor, entity trigger)
723 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
726 checkpoint_passed(this, actor);
729 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
731 entity own = this.owner;
733 own = this.realowner; // target support
735 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
737 else if(view.race_checkpoint == own.race_checkpoint)
743 void trigger_race_checkpoint_verify(entity this)
745 static bool have_verified;
746 if (have_verified) return;
747 have_verified = true;
749 bool qual = g_race_qualifying;
751 int pl_race_checkpoint = 0;
752 int pl_race_place = 0;
755 for (int i = 0; i <= race_highest_checkpoint; ++i) {
756 pl_race_checkpoint = race_NextCheckpoint(i);
758 // race only (middle of the race)
759 g_race_qualifying = false;
761 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
762 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
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 ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
773 // race only (initial spawn)
774 g_race_qualifying = 0;
775 for (int p = 1; p <= race_highest_place_spawn; ++p) {
777 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
778 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
783 } else if (!defrag_ents) {
785 pl_race_checkpoint = race_NextCheckpoint(0);
786 g_race_qualifying = 1;
787 pl_race_place = race_lowest_place_spawn;
788 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
789 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
792 pl_race_checkpoint = race_NextCheckpoint(0);
793 g_race_qualifying = 1;
794 pl_race_place = 0; // there's only one spawn on defrag maps
796 // check if a defragcp file already exists, then read it and apply the checkpoint order
801 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
803 while ((l = fgets(fh))) {
804 len = tokenize_console(l);
806 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
809 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
810 if (argv(0) == cp.targetname) {
811 cp.race_checkpoint = stof(argv(1));
819 g_race_qualifying = qual;
821 IL_EACH(g_race_targets, true,
824 FOREACH_ENTITY_STRING(target, cpt.targetname,
826 vector org = (it.absmin + it.absmax) * 0.5;
827 if(cpt.race_checkpoint == 0)
828 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
830 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
832 it.sprite.realowner = cpt;
833 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
837 if (race_timed_checkpoint) {
839 IL_EACH(g_race_targets, true,
842 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
843 FOREACH_ENTITY_STRING(target, cpt.targetname, {
844 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
847 if(it.classname == "target_checkpoint") {
848 if(it.race_checkpoint == -2)
849 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
852 if (defragcpexists != -1) {
853 float largest_cp_id = 0;
854 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
855 if (cp.race_checkpoint > largest_cp_id) {
856 largest_cp_id = cp.race_checkpoint;
859 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
860 cp.race_checkpoint = largest_cp_id + 1; // finish line
862 race_highest_checkpoint = largest_cp_id + 1;
863 race_timed_checkpoint = largest_cp_id + 1;
865 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
866 cp.race_checkpoint = 255; // finish line
868 race_highest_checkpoint = 255;
869 race_timed_checkpoint = 255;
872 IL_EACH(g_racecheckpoints, it.sprite,
874 if (it.race_checkpoint == 0) {
875 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
876 } else if (it.race_checkpoint == race_timed_checkpoint) {
877 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
884 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
885 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
886 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
892 // These just make the game crash on some maps with oddly shaped triggers.
893 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
894 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
895 // (happened on item triggers too)
900 //setsize(targ, trigger.mins, trigger.maxs);
901 //setorigin(targ, trigger.origin);
909 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
911 if(g_race_qualifying)
914 if(this.race_checkpoint != 0)
916 if(spot.race_place != race_lowest_place_spawn)
921 if(this.race_checkpoint != player.race_respawn_checkpoint)
923 // try reusing the previous spawn
924 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
925 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
926 if(this.race_checkpoint == 0)
928 int pl = player.race_place;
929 if(pl > race_highest_place_spawn)
931 if(pl == 0 && !player.race_started)
932 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
933 if(spot.race_place != pl)
940 spawnfunc(trigger_race_checkpoint)
943 if(!g_race && !g_cts) { delete(this); return; }
947 this.use = checkpoint_use;
948 if (!(this.spawnflags & 1))
949 settouch(this, checkpoint_touch);
951 o = (this.absmin + this.absmax) * 0.5;
952 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
953 waypoint_spawnforitem_force(this, trace_endpos);
954 this.nearestwaypointtimeout = -1;
956 if(this.message == "")
957 this.message = "went backwards";
958 if (this.message2 == "")
959 this.message2 = "was pushed backwards by";
960 if (this.race_penalty_reason == "")
961 this.race_penalty_reason = "missing a checkpoint";
963 this.race_checkpoint = this.cnt;
965 if(this.race_checkpoint > race_highest_checkpoint)
967 race_highest_checkpoint = this.race_checkpoint;
968 if(this.spawnflags & 8)
969 race_timed_checkpoint = this.race_checkpoint;
971 race_timed_checkpoint = 0;
974 if(!this.race_penalty)
976 if(this.race_checkpoint)
977 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
979 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
982 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
983 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
985 IL_PUSH(g_racecheckpoints, this);
987 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
990 spawnfunc(target_checkpoint) // defrag entity
992 if(!g_race && !g_cts) { delete(this); return; }
995 // if this is targeted, then it probably isn't a trigger
996 bool is_trigger = !boolean(!this.nottargeted && this.targetname != "");
1001 this.use = checkpoint_use;
1002 if (is_trigger && !(this.spawnflags & 1))
1003 settouch(this, checkpoint_touch);
1005 vector org = this.origin;
1007 // bots should only pathfind to this if it is a valid touchable trigger
1010 org = (this.absmin + this.absmax) * 0.5;
1011 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1012 waypoint_spawnforitem_force(this, trace_endpos);
1013 this.nearestwaypointtimeout = -1;
1016 if(this.message == "")
1017 this.message = "went backwards";
1018 if (this.message2 == "")
1019 this.message2 = "was pushed backwards by";
1020 if (this.race_penalty_reason == "")
1021 this.race_penalty_reason = "missing a checkpoint";
1023 if(this.classname == "target_startTimer")
1024 this.race_checkpoint = 0;
1026 this.race_checkpoint = -2;
1028 race_timed_checkpoint = 1;
1030 IL_PUSH(g_race_targets, this);
1032 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1035 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1036 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1038 void race_AbandonRaceCheck(entity p)
1040 if(race_completing && !p.race_completed)
1042 p.race_completed = 1;
1043 MAKE_INDEPENDENT_PLAYER(p);
1044 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1045 ClientData_Touch(p);
1049 void race_StartCompleting()
1051 race_completing = 1;
1052 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), LAMBDA(race_AbandonRaceCheck(it)));
1055 void race_PreparePlayer(entity this)
1057 race_ClearTime(this);
1058 this.race_place = 0;
1059 this.race_started = 0;
1060 this.race_respawn_checkpoint = 0;
1061 this.race_respawn_spotref = NULL;
1064 void race_RetractPlayer(entity this)
1066 if(!g_race && !g_cts)
1068 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1069 race_ClearTime(this);
1070 this.race_checkpoint = this.race_respawn_checkpoint;
1073 spawnfunc(info_player_race)
1075 if(!g_race && !g_cts) { delete(this); return; }
1077 spawnfunc_info_player_deathmatch(this);
1079 if(this.race_place > race_highest_place_spawn)
1080 race_highest_place_spawn = this.race_place;
1081 if(this.race_place < race_lowest_place_spawn)
1082 race_lowest_place_spawn = this.race_place;
1085 void race_ClearRecords()
1089 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1091 race_checkpoint_records[i] = 0;
1092 if(race_checkpoint_recordholders[i])
1093 strunzone(race_checkpoint_recordholders[i]);
1094 race_checkpoint_recordholders[i] = string_null;
1097 FOREACH_CLIENT(true, LAMBDA(
1098 float p = it.race_place;
1099 race_PreparePlayer(it);
1104 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1106 if(g_race_qualifying)
1108 pl.race_penalty_accumulator += penalty;
1109 if(IS_REAL_CLIENT(pl))
1112 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1113 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1114 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1115 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1116 WriteString(MSG_ONE, reason);
1122 pl.race_penalty = time + penalty;
1123 if(IS_REAL_CLIENT(pl))
1126 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1127 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1128 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1129 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1130 WriteString(MSG_ONE, reason);
1136 void penalty_touch(entity this, entity toucher)
1138 EXACTTRIGGER_TOUCH(this, toucher);
1139 if(toucher.race_lastpenalty != this)
1141 toucher.race_lastpenalty = this;
1142 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1146 void penalty_use(entity this, entity actor, entity trigger)
1148 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1151 spawnfunc(trigger_race_penalty)
1153 // TODO: find out why this wasnt done:
1154 //if(!g_cts && !g_race) { remove(this); return; }
1158 this.use = penalty_use;
1159 if (!(this.spawnflags & 1))
1160 settouch(this, penalty_touch);
1162 if (this.race_penalty_reason == "")
1163 this.race_penalty_reason = "missing a checkpoint";
1164 if (!this.race_penalty)
1165 this.race_penalty = 5;
1168 float race_GetFractionalLapCount(entity e)
1170 // interesting metrics (idea by KrimZon) to maybe sort players in the
1171 // scoreboard, immediately updates when overtaking
1173 // requires the track to be built so you never get farther away from the
1174 // next checkpoint, though, and current Xonotic race maps are not built that
1177 // also, this code is slow and would need optimization (i.e. "next CP"
1178 // links on CP entities)
1181 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1182 if(e.race_completed)
1183 return l; // not fractional
1186 float bestfraction, fraction;
1188 float nextcpindex, lastcpindex;
1190 nextcpindex = max(e.race_checkpoint, 0);
1191 lastcpindex = e.race_respawn_checkpoint;
1192 lastcp = e.race_respawn_spotref;
1194 if(nextcpindex == lastcpindex)
1198 IL_EACH(g_racecheckpoints, true,
1200 if(it.race_checkpoint != lastcpindex)
1205 o0 = (it.absmin + it.absmax) * 0.5;
1206 IL_EACH(g_racecheckpoints, true,
1208 if(it.race_checkpoint != nextcpindex)
1210 o1 = (it.absmin + it.absmax) * 0.5;
1213 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1214 if(fraction < bestfraction)
1215 bestfraction = fraction;
1219 // we are at CP "nextcpindex - bestfraction"
1220 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1221 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1223 nc = race_highest_checkpoint + 1;
1224 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;