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 : ((g_ctf) ? CTF_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 : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
38 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
41 float race_readPos(string map, float t)
43 for(int i = 1; i <= RANKINGS_CNT; ++i)
45 int mytime = race_readTime(map, i);
46 if(!mytime || mytime > t)
50 return 0; // pos is zero if unranked
53 void race_writeTime(string map, float t, string myuid)
55 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
58 newpos = race_readPos(map, t);
61 for(i = 1; i <= RANKINGS_CNT; ++i)
63 if(race_readUID(map, i) == myuid)
68 // player improved his existing record, only have to iterate on ranks between new and old recs
69 for (i = prevpos; i > newpos; --i)
71 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
72 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
77 // player has no ranked record yet
78 for (i = RANKINGS_CNT; i > newpos; --i)
80 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
81 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
85 // store new time itself
86 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
87 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
90 string race_readName(string map, float pos)
92 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
94 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
98 const float MAX_CHECKPOINTS = 255;
100 spawnfunc(target_checkpoint);
103 .float race_penalty_accumulator;
104 .string race_penalty_reason;
105 .float race_checkpoint; // player: next checkpoint that has to be reached
106 .entity race_lastpenalty;
110 float race_checkpoint_records[MAX_CHECKPOINTS];
111 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
112 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
113 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
114 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
116 float race_highest_checkpoint;
117 float race_timed_checkpoint;
120 float defragcpexists;
122 float race_NextCheckpoint(float f)
124 if(f >= race_highest_checkpoint)
130 float race_PreviousCheckpoint(float f)
135 return race_highest_checkpoint;
141 // 0 = common start/finish
144 float race_CheckpointNetworkID(float f)
146 if(race_timed_checkpoint)
150 else if(f == race_timed_checkpoint)
151 return 255; // finish
156 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
165 cp = e.race_checkpoint;
166 recordtime = race_checkpoint_records[cp];
167 recordholder = race_checkpoint_recordholders[cp];
168 if(recordholder == e.netname)
171 if(!IS_REAL_CLIENT(e))
176 WRITESPECTATABLE_MSG_ONE(msg_entity, {
177 WriteHeader(MSG_ONE, TE_CSQC_RACE);
180 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
181 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
182 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
185 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
186 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
187 WriteInt24_t(MSG_ONE, recordtime);
188 WriteString(MSG_ONE, recordholder);
192 void race_send_recordtime(float msg)
194 // send the server best time
195 WriteHeader(msg, TE_CSQC_RACE);
196 WriteByte(msg, RACE_NET_SERVER_RECORD);
197 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
201 void race_send_speedaward(float msg)
203 // send the best speed of the round
204 WriteHeader(msg, TE_CSQC_RACE);
205 WriteByte(msg, RACE_NET_SPEED_AWARD);
206 WriteInt24_t(msg, floor(speedaward_speed+0.5));
207 WriteString(msg, speedaward_holder);
210 void race_send_speedaward_alltimebest(float msg)
212 // send the best speed
213 WriteHeader(msg, TE_CSQC_RACE);
214 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
215 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
216 WriteString(msg, speedaward_alltimebest_holder);
219 void race_SendRankings(float pos, float prevpos, float del, float msg)
221 WriteHeader(msg, TE_CSQC_RACE);
222 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
223 WriteShort(msg, pos);
224 WriteShort(msg, prevpos);
225 WriteShort(msg, del);
226 WriteString(msg, race_readName(GetMapname(), pos));
227 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
230 void race_SendStatus(float id, entity e)
232 if(!IS_REAL_CLIENT(e))
241 WRITESPECTATABLE_MSG_ONE(msg_entity, {
242 WriteHeader(msg, TE_CSQC_RACE);
243 WriteByte(msg, RACE_NET_SERVER_STATUS);
245 WriteString(msg, e.netname);
249 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
251 // netname only used TEMPORARILY for printing
252 int newpos = race_readPos(map, t);
254 int player_prevpos = 0;
255 for(int 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"
268 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
273 // no ranking, time worse than the worst ranked
274 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
275 race_SendStatus(0, e); // "fail"
277 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
281 // if we didn't hit a return yet, we have a new record!
283 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
287 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
291 if(uid2name(myuid) == "^1Unregistered Player")
293 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
297 oldrec = race_readTime(GetMapname(), newpos);
298 oldrec_holder = race_readName(GetMapname(), newpos);
301 race_writeTime(GetMapname(), t, myuid);
303 if (newpos == 1 && showmessage)
305 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
306 race_send_recordtime(MSG_ALL);
309 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
311 strunzone(rankings_reply);
312 rankings_reply = strzone(getrankings());
314 if(newpos == player_prevpos)
317 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
318 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
319 else { race_SendStatus(1, e); } // "new time"
324 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
325 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
326 else { race_SendStatus(2, e); } // "new rank"
331 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
332 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
333 else { race_SendStatus(2, e); } // "new rank"
337 void race_deleteTime(string map, float pos)
339 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
341 for(int i = pos; i <= RANKINGS_CNT; ++i)
343 string therank = ftos(i);
344 if (i == RANKINGS_CNT)
346 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
347 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
351 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
352 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
356 race_SendRankings(pos, 0, 1, MSG_ALL);
358 race_send_recordtime(MSG_ALL);
361 strunzone(rankings_reply);
362 rankings_reply = strzone(getrankings());
365 void race_SendTime(entity e, float cp, float t, float tvalid)
369 if(g_race_qualifying)
370 t += e.race_penalty_accumulator;
372 t = TIME_ENCODE(t); // make integer
375 if(cp == race_timed_checkpoint) // finish line
376 if (!e.race_completed)
379 if(g_race_qualifying)
381 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
383 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
387 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
389 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
391 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
392 snew = TIME_ENCODE(time - game_starttime);
393 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
394 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
396 if(autocvar_fraglimit)
397 if(l >= autocvar_fraglimit)
398 race_StartCompleting();
402 e.race_completed = 1;
403 MAKE_INDEPENDENT_PLAYER(e);
404 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
412 if(g_race_qualifying)
416 recordtime = race_checkpoint_records[cp];
417 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
418 if(recordholder == e.netname)
423 if(cp == race_timed_checkpoint)
425 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
426 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
428 if(t < recordtime || recordtime == 0)
430 race_checkpoint_records[cp] = t;
431 if(race_checkpoint_recordholders[cp])
432 strunzone(race_checkpoint_recordholders[cp]);
433 race_checkpoint_recordholders[cp] = strzone(e.netname);
434 if(g_race_qualifying)
436 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, LAMBDA(race_SendNextCheckpoint(it, 0)));
449 if(IS_REAL_CLIENT(e))
452 if(g_race_qualifying)
454 WRITESPECTATABLE_MSG_ONE(msg_entity, {
455 WriteHeader(MSG_ONE, TE_CSQC_RACE);
456 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
457 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
458 WriteInt24_t(MSG_ONE, t); // time to that intermediate
459 WriteInt24_t(MSG_ONE, recordtime); // previously best time
460 WriteString(MSG_ONE, recordholder); // record holder
465 else // RACE! Not Qualifying
467 float mylaps, lother, othtime;
469 oth = race_checkpoint_lastplayers[cp];
472 mylaps = PlayerScore_Add(e, SP_RACE_LAPS, 0);
473 lother = race_checkpoint_lastlaps[cp];
474 othtime = race_checkpoint_lasttimes[cp];
477 mylaps = lother = othtime = 0;
479 if(IS_REAL_CLIENT(e))
482 WRITESPECTATABLE_MSG_ONE(msg_entity, {
483 WriteHeader(MSG_ONE, TE_CSQC_RACE);
484 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
485 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
488 WriteInt24_t(MSG_ONE, 0);
489 WriteByte(MSG_ONE, 0);
490 WriteString(MSG_ONE, "");
494 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
495 WriteByte(MSG_ONE, mylaps - lother);
496 WriteString(MSG_ONE, oth.netname); // record holder
501 race_checkpoint_lastplayers[cp] = e;
502 race_checkpoint_lasttimes[cp] = time;
503 race_checkpoint_lastlaps[cp] = mylaps;
505 if(IS_REAL_CLIENT(oth))
508 WRITESPECTATABLE_MSG_ONE(msg_entity, {
509 WriteHeader(MSG_ONE, TE_CSQC_RACE);
510 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
511 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
514 WriteInt24_t(MSG_ONE, 0);
515 WriteByte(MSG_ONE, 0);
516 WriteString(MSG_ONE, "");
520 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
521 WriteByte(MSG_ONE, lother - mylaps);
522 WriteString(MSG_ONE, e.netname); // record holder
529 void race_ClearTime(entity e)
531 e.race_checkpoint = 0;
533 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
534 e.race_penalty_accumulator = 0;
535 e.race_lastpenalty = NULL;
537 if(!IS_REAL_CLIENT(e))
541 WRITESPECTATABLE_MSG_ONE(msg_entity, {
542 WriteHeader(MSG_ONE, TE_CSQC_RACE);
543 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
547 void dumpsurface(entity e)
551 LOG_INFO("Surfaces of ", etos(e), ":\n");
553 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
557 n = getsurfacenumpoints(e, si);
560 LOG_INFO(" Surface ", ftos(si), ":\n");
561 norm = getsurfacenormal(e, si);
562 LOG_INFO(" Normal = ", vtos(norm), "\n");
563 for(ni = 0; ni < n; ++ni)
565 vec = getsurfacepoint(e, si, ni);
566 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
571 void checkpoint_passed(entity this, entity player)
576 if(player.classname == "porto")
578 // do not allow portalling through checkpoints
579 trace_plane_normal = normalize(-1 * player.velocity);
580 W_Porto_Fail(player, 0);
587 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
589 oldmsg = this.message;
591 SUB_UseTargets(this, player, player);
592 this.message = oldmsg;
595 if (!IS_PLAYER(player))
599 * Remove unauthorized equipment
601 Portal_ClearAll(player);
603 player.porto_forbidden = 2; // decreased by 1 each StartFrame
607 if(this.race_checkpoint == -2)
609 this.race_checkpoint = player.race_checkpoint;
612 float largest_cp_id = 0;
614 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
617 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
619 largest_cp_id = cp.race_checkpoint;
620 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
621 cp.race_checkpoint = largest_cp_id + 1; // finish line
622 race_highest_checkpoint = largest_cp_id + 1;
623 race_timed_checkpoint = largest_cp_id + 1;
625 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
627 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
634 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
635 cp.race_checkpoint = 1;
636 race_highest_checkpoint = 1;
637 race_timed_checkpoint = 1;
641 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
643 if(this.race_penalty)
645 if(player.race_lastpenalty != this)
647 player.race_lastpenalty = this;
648 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
652 if(player.race_penalty)
658 if(this.spawnflags & 2)
660 oldmsg = this.message;
662 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
663 this.message = oldmsg;
666 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
667 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
668 player.race_respawn_checkpoint = this.race_checkpoint;
669 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
670 player.race_started = 1;
672 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
674 if(!this.race_checkpoint) // start line
676 player.race_laptime = time;
677 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
678 player.race_penalty_accumulator = 0;
679 player.race_lastpenalty = NULL;
682 if(g_race_qualifying)
683 race_SendNextCheckpoint(player, 0);
685 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
688 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
691 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
692 fputs(fh, strcat(cp.targetname, " ", ftos(cp.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 if(view.race_checkpoint == -1 || this.owner.race_checkpoint == -2)
726 else if(view.race_checkpoint == this.owner.race_checkpoint)
732 void trigger_race_checkpoint_verify(entity this)
734 static bool have_verified;
735 if (have_verified) return;
736 have_verified = true;
738 bool qual = g_race_qualifying;
740 int pl_race_checkpoint = 0;
741 int pl_race_place = 0;
744 for (int i = 0; i <= race_highest_checkpoint; ++i) {
745 pl_race_checkpoint = race_NextCheckpoint(i);
747 // race only (middle of the race)
748 g_race_qualifying = false;
750 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
751 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
756 g_race_qualifying = 1;
757 pl_race_place = race_lowest_place_spawn;
758 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
759 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
762 // race only (initial spawn)
763 g_race_qualifying = 0;
764 for (int p = 1; p <= race_highest_place_spawn; ++p) {
766 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
767 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
772 } else if (!defrag_ents) {
774 pl_race_checkpoint = race_NextCheckpoint(0);
775 g_race_qualifying = 1;
776 pl_race_place = race_lowest_place_spawn;
777 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false)) {
778 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
781 pl_race_checkpoint = race_NextCheckpoint(0);
782 g_race_qualifying = 1;
783 pl_race_place = 0; // there's only one spawn on defrag maps
785 // check if a defragcp file already exists, then read it and apply the checkpoint order
790 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
792 while ((l = fgets(fh))) {
793 len = tokenize_console(l);
795 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
798 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
799 if (argv(0) == cp.targetname) {
800 cp.race_checkpoint = stof(argv(1));
808 g_race_qualifying = qual;
810 if (race_timed_checkpoint) {
812 for (entity cp = NULL; (cp = find(cp, classname, "target_startTimer"));) {
813 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
815 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
816 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
818 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
819 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
823 if (defragcpexists != -1) {
824 float largest_cp_id = 0;
825 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
826 if (cp.race_checkpoint > largest_cp_id) {
827 largest_cp_id = cp.race_checkpoint;
830 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
831 cp.race_checkpoint = largest_cp_id + 1; // finish line
833 race_highest_checkpoint = largest_cp_id + 1;
834 race_timed_checkpoint = largest_cp_id + 1;
836 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
837 cp.race_checkpoint = 255; // finish line
839 race_highest_checkpoint = 255;
840 race_timed_checkpoint = 255;
843 IL_EACH(g_racecheckpoints, it.sprite,
845 if (it.race_checkpoint == 0) {
846 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
847 } else if (it.race_checkpoint == race_timed_checkpoint) {
848 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
855 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
856 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
857 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
863 // These just make the game crash on some maps with oddly shaped triggers.
864 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
865 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
866 // (happened on item triggers too)
871 //setsize(targ, trigger.mins, trigger.maxs);
872 //setorigin(targ, trigger.origin);
880 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
882 if(g_race_qualifying)
885 if(this.race_checkpoint != 0)
887 if(spot.race_place != race_lowest_place_spawn)
892 if(this.race_checkpoint != player.race_respawn_checkpoint)
894 // try reusing the previous spawn
895 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
896 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
897 if(this.race_checkpoint == 0)
899 int pl = player.race_place;
900 if(pl > race_highest_place_spawn)
902 if(pl == 0 && !player.race_started)
903 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
904 if(spot.race_place != pl)
911 spawnfunc(trigger_race_checkpoint)
914 if(!g_race && !g_cts) { delete(this); return; }
918 this.use = checkpoint_use;
919 if (!(this.spawnflags & 1))
920 settouch(this, checkpoint_touch);
922 o = (this.absmin + this.absmax) * 0.5;
923 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
924 waypoint_spawnforitem_force(this, trace_endpos);
925 this.nearestwaypointtimeout = time + 1000000000;
927 if(this.message == "")
928 this.message = "went backwards";
929 if (this.message2 == "")
930 this.message2 = "was pushed backwards by";
931 if (this.race_penalty_reason == "")
932 this.race_penalty_reason = "missing a checkpoint";
934 this.race_checkpoint = this.cnt;
936 if(this.race_checkpoint > race_highest_checkpoint)
938 race_highest_checkpoint = this.race_checkpoint;
939 if(this.spawnflags & 8)
940 race_timed_checkpoint = this.race_checkpoint;
942 race_timed_checkpoint = 0;
945 if(!this.race_penalty)
947 if(this.race_checkpoint)
948 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
950 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
953 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
954 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
956 IL_PUSH(g_racecheckpoints, this);
958 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
961 spawnfunc(target_checkpoint) // defrag entity
963 if(!g_race && !g_cts) { delete(this); return; }
966 // if this is targeted, then it probably isn't a trigger
967 bool is_trigger = !boolean(!this.nottargeted && this.targetname != "");
972 this.use = checkpoint_use;
973 if (is_trigger && !(this.spawnflags & 1))
974 settouch(this, checkpoint_touch);
976 vector org = this.origin;
978 // bots should only pathfind to this if it is a valid touchable trigger
981 org = (this.absmin + this.absmax) * 0.5;
982 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
983 waypoint_spawnforitem_force(this, trace_endpos);
984 this.nearestwaypointtimeout = time + 1000000000;
987 if(this.message == "")
988 this.message = "went backwards";
989 if (this.message2 == "")
990 this.message2 = "was pushed backwards by";
991 if (this.race_penalty_reason == "")
992 this.race_penalty_reason = "missing a checkpoint";
994 if(this.classname == "target_startTimer")
995 this.race_checkpoint = 0;
997 this.race_checkpoint = -2;
999 race_timed_checkpoint = 1;
1001 if(this.race_checkpoint == 0)
1002 WaypointSprite_SpawnFixed(WP_RaceStart, org, this, sprite, RADARICON_NONE);
1004 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, this, sprite, RADARICON_NONE);
1006 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1008 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1011 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1012 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1014 void race_AbandonRaceCheck(entity p)
1016 if(race_completing && !p.race_completed)
1018 p.race_completed = 1;
1019 MAKE_INDEPENDENT_PLAYER(p);
1020 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1021 ClientData_Touch(p);
1025 void race_StartCompleting()
1027 race_completing = 1;
1028 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), LAMBDA(race_AbandonRaceCheck(it)));
1031 void race_PreparePlayer(entity this)
1033 race_ClearTime(this);
1034 this.race_place = 0;
1035 this.race_started = 0;
1036 this.race_respawn_checkpoint = 0;
1037 this.race_respawn_spotref = NULL;
1040 void race_RetractPlayer(entity this)
1042 if(!g_race && !g_cts)
1044 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1045 race_ClearTime(this);
1046 this.race_checkpoint = this.race_respawn_checkpoint;
1049 spawnfunc(info_player_race)
1051 if(!g_race && !g_cts) { delete(this); return; }
1053 spawnfunc_info_player_deathmatch(this);
1055 if(this.race_place > race_highest_place_spawn)
1056 race_highest_place_spawn = this.race_place;
1057 if(this.race_place < race_lowest_place_spawn)
1058 race_lowest_place_spawn = this.race_place;
1061 void race_ClearRecords()
1065 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1067 race_checkpoint_records[i] = 0;
1068 if(race_checkpoint_recordholders[i])
1069 strunzone(race_checkpoint_recordholders[i]);
1070 race_checkpoint_recordholders[i] = string_null;
1073 FOREACH_CLIENT(true, LAMBDA(
1074 float p = it.race_place;
1075 race_PreparePlayer(it);
1080 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1082 if(g_race_qualifying)
1084 pl.race_penalty_accumulator += penalty;
1085 if(IS_REAL_CLIENT(pl))
1088 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1089 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1090 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1091 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1092 WriteString(MSG_ONE, reason);
1098 pl.race_penalty = time + penalty;
1099 if(IS_REAL_CLIENT(pl))
1102 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1103 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1104 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1105 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1106 WriteString(MSG_ONE, reason);
1112 void penalty_touch(entity this, entity toucher)
1114 EXACTTRIGGER_TOUCH(this, toucher);
1115 if(toucher.race_lastpenalty != this)
1117 toucher.race_lastpenalty = this;
1118 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1122 void penalty_use(entity this, entity actor, entity trigger)
1124 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1127 spawnfunc(trigger_race_penalty)
1129 // TODO: find out why this wasnt done:
1130 //if(!g_cts && !g_race) { remove(this); return; }
1134 this.use = penalty_use;
1135 if (!(this.spawnflags & 1))
1136 settouch(this, penalty_touch);
1138 if (this.race_penalty_reason == "")
1139 this.race_penalty_reason = "missing a checkpoint";
1140 if (!this.race_penalty)
1141 this.race_penalty = 5;
1144 float race_GetFractionalLapCount(entity e)
1146 // interesting metrics (idea by KrimZon) to maybe sort players in the
1147 // scoreboard, immediately updates when overtaking
1149 // requires the track to be built so you never get farther away from the
1150 // next checkpoint, though, and current Xonotic race maps are not built that
1153 // also, this code is slow and would need optimization (i.e. "next CP"
1154 // links on CP entities)
1157 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1158 if(e.race_completed)
1159 return l; // not fractional
1162 float bestfraction, fraction;
1164 float nextcpindex, lastcpindex;
1166 nextcpindex = max(e.race_checkpoint, 0);
1167 lastcpindex = e.race_respawn_checkpoint;
1168 lastcp = e.race_respawn_spotref;
1170 if(nextcpindex == lastcpindex)
1174 IL_EACH(g_racecheckpoints, true,
1176 if(it.race_checkpoint != lastcpindex)
1181 o0 = (it.absmin + it.absmax) * 0.5;
1182 IL_EACH(g_racecheckpoints, true,
1184 if(it.race_checkpoint != nextcpindex)
1186 o1 = (it.absmin + it.absmax) * 0.5;
1189 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1190 if(fraction < bestfraction)
1191 bestfraction = fraction;
1195 // we are at CP "nextcpindex - bestfraction"
1196 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1197 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1199 nc = race_highest_checkpoint + 1;
1200 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;