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")
294 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
298 oldrec = race_readTime(GetMapname(), newpos);
299 oldrec_holder = race_readName(GetMapname(), newpos);
302 race_writeTime(GetMapname(), t, myuid);
304 if (newpos == 1 && showmessage)
306 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
307 race_send_recordtime(MSG_ALL);
310 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
312 strunzone(rankings_reply);
313 rankings_reply = strzone(getrankings());
315 if(newpos == player_prevpos)
318 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
319 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
320 else { race_SendStatus(1, e); } // "new time"
325 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
326 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
327 else { race_SendStatus(2, e); } // "new rank"
332 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
333 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
334 else { race_SendStatus(2, e); } // "new rank"
338 void race_deleteTime(string map, float pos)
340 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
342 for(int i = pos; i <= RANKINGS_CNT; ++i)
344 string therank = ftos(i);
345 if (i == RANKINGS_CNT)
347 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
348 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
352 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
353 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
357 race_SendRankings(pos, 0, 1, MSG_ALL);
359 race_send_recordtime(MSG_ALL);
362 strunzone(rankings_reply);
363 rankings_reply = strzone(getrankings());
366 void race_SendTime(entity e, float cp, float t, float tvalid)
370 if(g_race_qualifying)
371 t += e.race_penalty_accumulator;
373 t = TIME_ENCODE(t); // make integer
376 if(cp == race_timed_checkpoint) // finish line
377 if (!e.race_completed)
380 if(g_race_qualifying)
382 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
384 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
388 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
390 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
392 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
393 snew = TIME_ENCODE(time - game_starttime);
394 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
395 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
397 if(autocvar_fraglimit)
398 if(l >= autocvar_fraglimit)
399 race_StartCompleting();
403 e.race_completed = 1;
404 MAKE_INDEPENDENT_PLAYER(e);
405 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
413 if(g_race_qualifying)
417 recordtime = race_checkpoint_records[cp];
418 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
419 if(recordholder == e.netname)
424 if(cp == race_timed_checkpoint)
426 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
427 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
429 if(t < recordtime || recordtime == 0)
431 race_checkpoint_records[cp] = t;
432 if(race_checkpoint_recordholders[cp])
433 strunzone(race_checkpoint_recordholders[cp]);
434 race_checkpoint_recordholders[cp] = strzone(e.netname);
435 if(g_race_qualifying)
437 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, LAMBDA(race_SendNextCheckpoint(it, 0)));
450 if(IS_REAL_CLIENT(e))
453 if(g_race_qualifying)
455 WRITESPECTATABLE_MSG_ONE(msg_entity, {
456 WriteHeader(MSG_ONE, TE_CSQC_RACE);
457 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
458 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
459 WriteInt24_t(MSG_ONE, t); // time to that intermediate
460 WriteInt24_t(MSG_ONE, recordtime); // previously best time
461 WriteString(MSG_ONE, recordholder); // record holder
466 else // RACE! Not Qualifying
468 float mylaps, lother, othtime;
470 oth = race_checkpoint_lastplayers[cp];
473 mylaps = PlayerScore_Add(e, SP_RACE_LAPS, 0);
474 lother = race_checkpoint_lastlaps[cp];
475 othtime = race_checkpoint_lasttimes[cp];
478 mylaps = lother = othtime = 0;
480 if(IS_REAL_CLIENT(e))
483 WRITESPECTATABLE_MSG_ONE(msg_entity, {
484 WriteHeader(MSG_ONE, TE_CSQC_RACE);
485 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
486 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
489 WriteInt24_t(MSG_ONE, 0);
490 WriteByte(MSG_ONE, 0);
491 WriteString(MSG_ONE, "");
495 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
496 WriteByte(MSG_ONE, mylaps - lother);
497 WriteString(MSG_ONE, oth.netname); // record holder
502 race_checkpoint_lastplayers[cp] = e;
503 race_checkpoint_lasttimes[cp] = time;
504 race_checkpoint_lastlaps[cp] = mylaps;
506 if(IS_REAL_CLIENT(oth))
509 WRITESPECTATABLE_MSG_ONE(msg_entity, {
510 WriteHeader(MSG_ONE, TE_CSQC_RACE);
511 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
512 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
515 WriteInt24_t(MSG_ONE, 0);
516 WriteByte(MSG_ONE, 0);
517 WriteString(MSG_ONE, "");
521 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
522 WriteByte(MSG_ONE, lother - mylaps);
523 WriteString(MSG_ONE, e.netname); // record holder
530 void race_ClearTime(entity e)
532 e.race_checkpoint = 0;
534 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
535 e.race_penalty_accumulator = 0;
536 e.race_lastpenalty = NULL;
538 if(!IS_REAL_CLIENT(e))
542 WRITESPECTATABLE_MSG_ONE(msg_entity, {
543 WriteHeader(MSG_ONE, TE_CSQC_RACE);
544 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
548 void dumpsurface(entity e)
552 LOG_INFO("Surfaces of ", etos(e), ":\n");
554 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
558 n = getsurfacenumpoints(e, si);
561 LOG_INFO(" Surface ", ftos(si), ":\n");
562 norm = getsurfacenormal(e, si);
563 LOG_INFO(" Normal = ", vtos(norm), "\n");
564 for(ni = 0; ni < n; ++ni)
566 vec = getsurfacepoint(e, si, ni);
567 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
572 void checkpoint_passed(entity this, entity player)
574 if(player.personal && autocvar_g_allow_checkpoints)
575 return; // practice mode!
580 if(player.classname == "porto")
582 // do not allow portalling through checkpoints
583 trace_plane_normal = normalize(-1 * player.velocity);
584 W_Porto_Fail(player, 0);
591 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
593 oldmsg = this.message;
595 SUB_UseTargets(this, player, player);
596 this.message = oldmsg;
599 if (!IS_PLAYER(player))
603 * Remove unauthorized equipment
605 Portal_ClearAll(player);
607 player.porto_forbidden = 2; // decreased by 1 each StartFrame
611 if(this.race_checkpoint == -2)
613 this.race_checkpoint = player.race_checkpoint;
616 float largest_cp_id = 0;
618 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
621 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
623 largest_cp_id = cp.race_checkpoint;
624 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
625 cp.race_checkpoint = largest_cp_id + 1; // finish line
626 race_highest_checkpoint = largest_cp_id + 1;
627 race_timed_checkpoint = largest_cp_id + 1;
629 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
631 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
638 for(cp = NULL; (cp = find(cp, classname, "target_stopTimer"));)
639 cp.race_checkpoint = 1;
640 race_highest_checkpoint = 1;
641 race_timed_checkpoint = 1;
645 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
647 if(this.race_penalty)
649 if(player.race_lastpenalty != this)
651 player.race_lastpenalty = this;
652 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
656 if(player.race_penalty)
662 if(this.spawnflags & 2)
664 oldmsg = this.message;
666 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
667 this.message = oldmsg;
670 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
671 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
672 player.race_respawn_checkpoint = this.race_checkpoint;
673 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
674 player.race_started = 1;
676 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
678 if(!this.race_checkpoint) // start line
680 player.race_laptime = time;
681 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
682 player.race_penalty_accumulator = 0;
683 player.race_lastpenalty = NULL;
686 if(g_race_qualifying)
687 race_SendNextCheckpoint(player, 0);
689 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
692 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
695 for(cp = NULL; (cp = find(cp, classname, "target_checkpoint"));)
696 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
701 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
707 if(this.spawnflags & 4)
708 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, player.origin, '0 0 0');
712 void checkpoint_touch(entity this, entity toucher)
714 EXACTTRIGGER_TOUCH(this, toucher);
715 checkpoint_passed(this, toucher);
718 void checkpoint_use(entity this, entity actor, entity trigger)
720 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
723 checkpoint_passed(this, actor);
726 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
728 if(view.race_checkpoint == -1 || this.owner.race_checkpoint == -2)
730 else if(view.race_checkpoint == this.owner.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 if (race_timed_checkpoint) {
816 for (entity cp = NULL; (cp = find(cp, classname, "target_startTimer"));) {
817 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
819 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
820 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
822 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
823 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
827 if (defragcpexists != -1) {
828 float largest_cp_id = 0;
829 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
830 if (cp.race_checkpoint > largest_cp_id) {
831 largest_cp_id = cp.race_checkpoint;
834 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
835 cp.race_checkpoint = largest_cp_id + 1; // finish line
837 race_highest_checkpoint = largest_cp_id + 1;
838 race_timed_checkpoint = largest_cp_id + 1;
840 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
841 cp.race_checkpoint = 255; // finish line
843 race_highest_checkpoint = 255;
844 race_timed_checkpoint = 255;
847 IL_EACH(g_racecheckpoints, it.sprite,
849 if (it.race_checkpoint == 0) {
850 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
851 } else if (it.race_checkpoint == race_timed_checkpoint) {
852 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
859 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
860 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
861 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
867 // These just make the game crash on some maps with oddly shaped triggers.
868 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
869 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
870 // (happened on item triggers too)
875 //setsize(targ, trigger.mins, trigger.maxs);
876 //setorigin(targ, trigger.origin);
884 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
886 if(g_race_qualifying)
889 if(this.race_checkpoint != 0)
891 if(spot.race_place != race_lowest_place_spawn)
896 if(this.race_checkpoint != player.race_respawn_checkpoint)
898 // try reusing the previous spawn
899 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
900 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
901 if(this.race_checkpoint == 0)
903 int pl = player.race_place;
904 if(pl > race_highest_place_spawn)
906 if(pl == 0 && !player.race_started)
907 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
908 if(spot.race_place != pl)
915 spawnfunc(trigger_race_checkpoint)
918 if(!g_race && !g_cts) { delete(this); return; }
922 this.use = checkpoint_use;
923 if (!(this.spawnflags & 1))
924 settouch(this, checkpoint_touch);
926 o = (this.absmin + this.absmax) * 0.5;
927 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
928 waypoint_spawnforitem_force(this, trace_endpos);
929 this.nearestwaypointtimeout = time + 1000000000;
931 if(this.message == "")
932 this.message = "went backwards";
933 if (this.message2 == "")
934 this.message2 = "was pushed backwards by";
935 if (this.race_penalty_reason == "")
936 this.race_penalty_reason = "missing a checkpoint";
938 this.race_checkpoint = this.cnt;
940 if(this.race_checkpoint > race_highest_checkpoint)
942 race_highest_checkpoint = this.race_checkpoint;
943 if(this.spawnflags & 8)
944 race_timed_checkpoint = this.race_checkpoint;
946 race_timed_checkpoint = 0;
949 if(!this.race_penalty)
951 if(this.race_checkpoint)
952 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
954 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
957 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
958 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
960 IL_PUSH(g_racecheckpoints, this);
962 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
965 spawnfunc(target_checkpoint) // defrag entity
967 if(!g_race && !g_cts) { delete(this); return; }
970 // if this is targeted, then it probably isn't a trigger
971 bool is_trigger = !boolean(!this.nottargeted && this.targetname != "");
976 this.use = checkpoint_use;
977 if (is_trigger && !(this.spawnflags & 1))
978 settouch(this, checkpoint_touch);
980 vector org = this.origin;
982 // bots should only pathfind to this if it is a valid touchable trigger
985 org = (this.absmin + this.absmax) * 0.5;
986 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
987 waypoint_spawnforitem_force(this, trace_endpos);
988 this.nearestwaypointtimeout = time + 1000000000;
991 if(this.message == "")
992 this.message = "went backwards";
993 if (this.message2 == "")
994 this.message2 = "was pushed backwards by";
995 if (this.race_penalty_reason == "")
996 this.race_penalty_reason = "missing a checkpoint";
998 if(this.classname == "target_startTimer")
999 this.race_checkpoint = 0;
1001 this.race_checkpoint = -2;
1003 race_timed_checkpoint = 1;
1005 if(this.race_checkpoint == 0)
1006 WaypointSprite_SpawnFixed(WP_RaceStart, org, this, sprite, RADARICON_NONE);
1008 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, this, sprite, RADARICON_NONE);
1010 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1012 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1015 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1016 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1018 void race_AbandonRaceCheck(entity p)
1020 if(race_completing && !p.race_completed)
1022 p.race_completed = 1;
1023 MAKE_INDEPENDENT_PLAYER(p);
1024 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1025 ClientData_Touch(p);
1029 void race_StartCompleting()
1031 race_completing = 1;
1032 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), LAMBDA(race_AbandonRaceCheck(it)));
1035 void race_PreparePlayer(entity this)
1037 race_ClearTime(this);
1038 this.race_place = 0;
1039 this.race_started = 0;
1040 this.race_respawn_checkpoint = 0;
1041 this.race_respawn_spotref = NULL;
1044 void race_RetractPlayer(entity this)
1046 if(!g_race && !g_cts)
1048 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1049 race_ClearTime(this);
1050 this.race_checkpoint = this.race_respawn_checkpoint;
1053 spawnfunc(info_player_race)
1055 if(!g_race && !g_cts) { delete(this); return; }
1057 spawnfunc_info_player_deathmatch(this);
1059 if(this.race_place > race_highest_place_spawn)
1060 race_highest_place_spawn = this.race_place;
1061 if(this.race_place < race_lowest_place_spawn)
1062 race_lowest_place_spawn = this.race_place;
1065 void race_ClearRecords()
1069 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1071 race_checkpoint_records[i] = 0;
1072 if(race_checkpoint_recordholders[i])
1073 strunzone(race_checkpoint_recordholders[i]);
1074 race_checkpoint_recordholders[i] = string_null;
1077 FOREACH_CLIENT(true, LAMBDA(
1078 float p = it.race_place;
1079 race_PreparePlayer(it);
1084 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1086 if(g_race_qualifying)
1088 pl.race_penalty_accumulator += penalty;
1089 if(IS_REAL_CLIENT(pl))
1092 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1093 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1094 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1095 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1096 WriteString(MSG_ONE, reason);
1102 pl.race_penalty = time + penalty;
1103 if(IS_REAL_CLIENT(pl))
1106 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1107 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1108 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1109 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1110 WriteString(MSG_ONE, reason);
1116 void penalty_touch(entity this, entity toucher)
1118 EXACTTRIGGER_TOUCH(this, toucher);
1119 if(toucher.race_lastpenalty != this)
1121 toucher.race_lastpenalty = this;
1122 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1126 void penalty_use(entity this, entity actor, entity trigger)
1128 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1131 spawnfunc(trigger_race_penalty)
1133 // TODO: find out why this wasnt done:
1134 //if(!g_cts && !g_race) { remove(this); return; }
1138 this.use = penalty_use;
1139 if (!(this.spawnflags & 1))
1140 settouch(this, penalty_touch);
1142 if (this.race_penalty_reason == "")
1143 this.race_penalty_reason = "missing a checkpoint";
1144 if (!this.race_penalty)
1145 this.race_penalty = 5;
1148 float race_GetFractionalLapCount(entity e)
1150 // interesting metrics (idea by KrimZon) to maybe sort players in the
1151 // scoreboard, immediately updates when overtaking
1153 // requires the track to be built so you never get farther away from the
1154 // next checkpoint, though, and current Xonotic race maps are not built that
1157 // also, this code is slow and would need optimization (i.e. "next CP"
1158 // links on CP entities)
1161 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1162 if(e.race_completed)
1163 return l; // not fractional
1166 float bestfraction, fraction;
1168 float nextcpindex, lastcpindex;
1170 nextcpindex = max(e.race_checkpoint, 0);
1171 lastcpindex = e.race_respawn_checkpoint;
1172 lastcp = e.race_respawn_spotref;
1174 if(nextcpindex == lastcpindex)
1178 IL_EACH(g_racecheckpoints, true,
1180 if(it.race_checkpoint != lastcpindex)
1185 o0 = (it.absmin + it.absmax) * 0.5;
1186 IL_EACH(g_racecheckpoints, true,
1188 if(it.race_checkpoint != nextcpindex)
1190 o1 = (it.absmin + it.absmax) * 0.5;
1193 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1194 if(fraction < bestfraction)
1195 bestfraction = fraction;
1199 // we are at CP "nextcpindex - bestfraction"
1200 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1201 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1203 nc = race_highest_checkpoint + 1;
1204 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;