3 #include <common/weapons/_all.qh>
4 #include <common/stats.qh>
5 #include <server/damage.qh>
6 #include <server/intermission.qh>
7 #include <server/world.qh>
8 #include <server/miscfunctions.qh>
9 #include <server/weapons/common.qh>
14 #include "spawnpoints.qh"
16 #include "command/getreplies.qh"
17 #include "../common/deathtypes/all.qh"
18 #include "../common/notifications/all.qh"
19 #include <common/gamemodes/_mod.qh>
20 #include <common/gamemodes/rules.qh>
21 #include <common/net_linked.qh>
22 #include <common/state.qh>
23 #include <common/weapons/weapon/porto.qh>
24 #include "../common/mapobjects/subs.qh"
25 #include <common/mapobjects/triggers.qh>
26 #include "../lib/warpzone/util_server.qh"
27 #include "../lib/warpzone/common.qh"
28 #include <common/vehicles/sv_vehicles.qh>
29 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
31 IntrusiveList g_race_targets;
32 IntrusiveList g_racecheckpoints;
35 g_race_targets = IL_NEW();
36 g_racecheckpoints = IL_NEW();
39 void race_InitSpectator()
42 if(msg_entity.enemy.race_laptime)
43 race_SendNextCheckpoint(msg_entity.enemy, 1);
46 float race_readTime(string map, float pos)
48 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
50 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
53 string race_readUID(string map, float pos)
55 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
57 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
60 float race_readPos(string map, float t)
62 for(int i = 1; i <= RANKINGS_CNT; ++i)
64 int mytime = race_readTime(map, i);
65 if(!mytime || mytime > t)
69 return 0; // pos is zero if unranked
72 void race_writeTime(string map, float t, string myuid)
74 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
77 newpos = race_readPos(map, t);
80 for(i = 1; i <= RANKINGS_CNT; ++i)
82 if(race_readUID(map, i) == myuid)
87 // player improved his existing record, only have to iterate on ranks between new and old recs
88 for (i = prevpos; i > newpos; --i)
90 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
91 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
96 // player has no ranked record yet
97 for (i = RANKINGS_CNT; i > newpos; --i)
99 float other_time = race_readTime(map, i - 1);
101 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(other_time));
102 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
107 // store new time itself
108 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
109 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
112 string race_readName(string map, float pos)
114 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
116 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
120 const float MAX_CHECKPOINTS = 255;
123 .float race_penalty_accumulator;
124 .string race_penalty_reason;
125 .float race_checkpoint; // player: next checkpoint that has to be reached
126 .entity race_lastpenalty;
130 float race_checkpoint_records[MAX_CHECKPOINTS];
131 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
132 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
133 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
134 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
136 .float race_checkpoint_record[MAX_CHECKPOINTS];
138 float race_highest_checkpoint;
139 float race_timed_checkpoint;
142 float defragcpexists;
144 float race_NextCheckpoint(float f)
146 if(f >= race_highest_checkpoint)
152 float race_PreviousCheckpoint(float f)
157 return race_highest_checkpoint;
163 // 0 = common start/finish
166 float race_CheckpointNetworkID(float f)
168 if(race_timed_checkpoint)
172 else if(f == race_timed_checkpoint)
173 return 255; // finish
178 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
183 int cp = e.race_checkpoint;
184 float recordtime = race_checkpoint_records[cp];
185 float myrecordtime = e.race_checkpoint_record[cp];
186 string recordholder = race_checkpoint_recordholders[cp];
187 if(recordholder == e.netname)
190 if(!IS_REAL_CLIENT(e))
195 WRITESPECTATABLE_MSG_ONE(msg_entity, {
196 WriteHeader(MSG_ONE, TE_CSQC_RACE);
199 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
200 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
201 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
204 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
205 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
206 WriteInt24_t(MSG_ONE, recordtime);
208 WriteInt24_t(MSG_ONE, myrecordtime);
209 WriteString(MSG_ONE, recordholder);
213 void race_send_recordtime(float msg)
215 // send the server best time
216 WriteHeader(msg, TE_CSQC_RACE);
217 WriteByte(msg, RACE_NET_SERVER_RECORD);
218 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
222 void race_send_speedaward(float msg)
224 // send the best speed of the round
225 WriteHeader(msg, TE_CSQC_RACE);
226 WriteByte(msg, RACE_NET_SPEED_AWARD);
227 WriteInt24_t(msg, floor(speedaward_speed+0.5));
228 WriteString(msg, speedaward_holder);
231 void race_send_speedaward_alltimebest(float msg)
233 // send the best speed
234 WriteHeader(msg, TE_CSQC_RACE);
235 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
236 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
237 WriteString(msg, speedaward_alltimebest_holder);
240 void race_send_rankings_cnt(float msg)
242 WriteHeader(msg, TE_CSQC_RACE);
243 WriteByte(msg, RACE_NET_RANKINGS_CNT);
244 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
248 void race_SendRankings(float pos, float prevpos, float del, float msg)
250 WriteHeader(msg, TE_CSQC_RACE);
251 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
252 WriteShort(msg, pos);
253 WriteShort(msg, prevpos);
254 WriteShort(msg, del);
255 WriteString(msg, race_readName(GetMapname(), pos));
256 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
259 void race_SendStatus(float id, entity e)
261 if(!IS_REAL_CLIENT(e))
270 WRITESPECTATABLE_MSG_ONE(msg_entity, {
271 WriteHeader(msg, TE_CSQC_RACE);
272 WriteByte(msg, RACE_NET_SERVER_STATUS);
274 WriteString(msg, e.netname);
278 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
280 // netname only used TEMPORARILY for printing
281 int newpos = race_readPos(map, t);
283 int player_prevpos = 0;
284 for(int i = 1; i <= RANKINGS_CNT; ++i)
286 if(race_readUID(map, i) == myuid)
291 string oldrec_holder;
292 if (player_prevpos && (player_prevpos < newpos || !newpos))
294 oldrec = race_readTime(GetMapname(), player_prevpos);
295 race_SendStatus(0, e); // "fail"
297 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
302 // no ranking, time worse than the worst ranked
303 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
304 race_SendStatus(0, e); // "fail"
306 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
310 // if we didn't hit a return yet, we have a new record!
312 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
316 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
320 if(uid2name(myuid) == "^1Unregistered Player")
323 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
327 oldrec = race_readTime(GetMapname(), newpos);
328 oldrec_holder = race_readName(GetMapname(), newpos);
331 race_writeTime(GetMapname(), t, myuid);
333 if (newpos == 1 && showmessage)
335 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
336 race_send_recordtime(MSG_ALL);
339 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
340 strcpy(rankings_reply, getrankings());
342 if(newpos == player_prevpos)
345 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
346 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
347 else { race_SendStatus(1, e); } // "new time"
352 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
353 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
354 else { race_SendStatus(2, e); } // "new rank"
359 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
360 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
361 else { race_SendStatus(2, e); } // "new rank"
365 void race_deleteTime(string map, float pos)
367 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
369 for(int i = pos; i <= RANKINGS_CNT; ++i)
371 string therank = ftos(i);
372 if (i == RANKINGS_CNT)
374 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
375 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
379 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
380 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
384 race_SendRankings(pos, 0, 1, MSG_ALL);
386 race_send_recordtime(MSG_ALL);
388 strcpy(rankings_reply, getrankings());
391 void race_SendTime(entity e, float cp, float t, float tvalid)
395 if(g_race_qualifying)
396 t += e.race_penalty_accumulator;
398 t = TIME_ENCODE(t); // make integer
401 if(cp == race_timed_checkpoint) // finish line
402 if (!CS(e).race_completed)
405 if(g_race_qualifying)
407 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
409 GameRules_scoring_add(e, RACE_FASTEST, t - s);
413 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
415 GameRules_scoring_add(e, RACE_FASTEST, t - s);
417 s = GameRules_scoring_add(e, RACE_TIME, 0);
418 snew = TIME_ENCODE(time - game_starttime);
419 GameRules_scoring_add(e, RACE_TIME, snew - s);
420 l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
422 if(autocvar_fraglimit)
423 if(l >= autocvar_fraglimit)
424 race_StartCompleting();
428 CS(e).race_completed = 1;
429 MAKE_INDEPENDENT_PLAYER(e);
430 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
436 if(g_race_qualifying)
443 recordtime = race_checkpoint_records[cp];
444 float myrecordtime = e.race_checkpoint_record[cp];
445 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
446 if(recordholder == e.netname)
451 if(cp == race_timed_checkpoint)
453 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
454 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
456 if(t < myrecordtime || myrecordtime == 0)
457 e.race_checkpoint_record[cp] = t; // resending done below
459 if(t < recordtime || recordtime == 0)
461 race_checkpoint_records[cp] = t;
462 strcpy(race_checkpoint_recordholders[cp], e.netname);
463 if(g_race_qualifying)
464 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
477 if(IS_REAL_CLIENT(e))
479 if(g_race_qualifying)
481 FOREACH_CLIENT(IS_REAL_CLIENT(it),
483 if(it == e || (IS_SPEC(it) && it.enemy == e))
486 WriteHeader(MSG_ONE, TE_CSQC_RACE);
487 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
488 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
489 WriteInt24_t(MSG_ONE, t); // time to that intermediate
490 WriteInt24_t(MSG_ONE, recordtime); // previously best time
491 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
492 WriteString(MSG_ONE, recordholder); // record holder
498 else // RACE! Not Qualifying
500 float mylaps, lother, othtime;
501 entity oth = race_checkpoint_lastplayers[cp];
504 mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
505 lother = race_checkpoint_lastlaps[cp];
506 othtime = race_checkpoint_lasttimes[cp];
509 mylaps = lother = othtime = 0;
511 if(IS_REAL_CLIENT(e))
514 WRITESPECTATABLE_MSG_ONE(msg_entity, {
515 WriteHeader(MSG_ONE, TE_CSQC_RACE);
516 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
517 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
520 WriteInt24_t(MSG_ONE, 0);
521 WriteByte(MSG_ONE, 0);
522 WriteByte(MSG_ONE, 0);
526 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
527 WriteByte(MSG_ONE, mylaps - lother);
528 WriteByte(MSG_ONE, etof(oth)); // record holder
533 race_checkpoint_lastplayers[cp] = e;
534 race_checkpoint_lasttimes[cp] = time;
535 race_checkpoint_lastlaps[cp] = mylaps;
537 if(IS_REAL_CLIENT(oth))
540 WRITESPECTATABLE_MSG_ONE(msg_entity, {
541 WriteHeader(MSG_ONE, TE_CSQC_RACE);
542 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
543 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
546 WriteInt24_t(MSG_ONE, 0);
547 WriteByte(MSG_ONE, 0);
548 WriteByte(MSG_ONE, 0);
552 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
553 WriteByte(MSG_ONE, lother - mylaps);
554 WriteByte(MSG_ONE, etof(e) - 1); // record holder
561 void race_ClearTime(entity e)
563 e.race_checkpoint = 0;
565 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
566 e.race_penalty_accumulator = 0;
567 e.race_lastpenalty = NULL;
569 if(!IS_REAL_CLIENT(e))
573 WRITESPECTATABLE_MSG_ONE(msg_entity, {
574 WriteHeader(MSG_ONE, TE_CSQC_RACE);
575 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
579 void checkpoint_passed(entity this, entity player)
581 if(IS_VEHICLE(player) && player.owner)
582 player = player.owner;
584 if(player.personal && autocvar_g_allow_checkpoints)
585 return; // practice mode!
587 if(player.classname == "porto")
589 // do not allow portalling through checkpoints
590 trace_plane_normal = normalize(-1 * player.velocity);
591 W_Porto_Fail(player, 0);
595 string oldmsg; // used twice
600 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
602 oldmsg = this.message;
604 SUB_UseTargets(this, player, player);
605 this.message = oldmsg;
608 if (!IS_PLAYER(player))
612 * Remove unauthorized equipment
614 Portal_ClearAll(player);
616 player.porto_forbidden = 2; // decreased by 1 each StartFrame
620 if(this.race_checkpoint == -2)
622 this.race_checkpoint = player.race_checkpoint;
625 int cp_amount = 0, largest_cp_id = 0;
626 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
629 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
633 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
635 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
640 largest_cp_id = it.race_checkpoint;
641 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
643 it.race_checkpoint = largest_cp_id + 1; // finish line
645 race_highest_checkpoint = largest_cp_id + 1;
646 race_timed_checkpoint = largest_cp_id + 1;
652 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
654 it.race_checkpoint = 1;
656 race_highest_checkpoint = 1;
657 race_timed_checkpoint = 1;
661 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
663 if(this.race_penalty)
665 if(player.race_lastpenalty != this)
667 player.race_lastpenalty = this;
668 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
672 if(player.race_penalty)
678 if(this.spawnflags & 2)
680 oldmsg = this.message;
682 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
683 this.message = oldmsg;
686 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
687 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
688 player.race_respawn_checkpoint = this.race_checkpoint;
689 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
690 player.race_started = 1;
692 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
694 if(!this.race_checkpoint) // start line
696 player.race_laptime = time;
697 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
698 player.race_penalty_accumulator = 0;
699 player.race_lastpenalty = NULL;
702 if(g_race_qualifying)
703 race_SendNextCheckpoint(player, 0);
705 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
708 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
711 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
713 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
719 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
725 if(this.spawnflags & 4)
726 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
730 void checkpoint_touch(entity this, entity toucher)
732 EXACTTRIGGER_TOUCH(this, toucher);
733 checkpoint_passed(this, toucher);
736 void checkpoint_use(entity this, entity actor, entity trigger)
738 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
741 checkpoint_passed(this, actor);
744 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
746 entity own = this.owner;
748 own = this.realowner; // target support
750 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
752 else if(view.race_checkpoint == own.race_checkpoint)
758 void trigger_race_checkpoint_verify(entity this)
760 static bool have_verified;
761 if (have_verified) return;
762 have_verified = true;
764 bool qual = g_race_qualifying;
766 int pl_race_checkpoint = 0;
767 int pl_race_place = 0;
770 for (int i = 0; i <= race_highest_checkpoint; ++i) {
771 pl_race_checkpoint = race_NextCheckpoint(i);
773 // race only (middle of the race)
774 g_race_qualifying = false;
776 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
777 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
782 g_race_qualifying = 1;
783 pl_race_place = race_lowest_place_spawn;
784 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
785 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
788 // race only (initial spawn)
789 g_race_qualifying = 0;
790 for (int p = 1; p <= race_highest_place_spawn; ++p) {
792 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
793 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
798 } else if (!defrag_ents) {
800 pl_race_checkpoint = race_NextCheckpoint(0);
801 g_race_qualifying = 1;
802 pl_race_place = race_lowest_place_spawn;
803 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
804 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
807 pl_race_checkpoint = race_NextCheckpoint(0);
808 g_race_qualifying = 1;
809 pl_race_place = 0; // there's only one spawn on defrag maps
811 // check if a defragcp file already exists, then read it and apply the checkpoint order
816 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
818 while ((l = fgets(fh))) {
819 len = tokenize_console(l);
821 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
824 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
825 if (argv(0) == cp.targetname) {
826 cp.race_checkpoint = stof(argv(1));
834 g_race_qualifying = qual;
836 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
838 if(it.targetname == "" || !it.targetname) // somehow this is a case...
841 FOREACH_ENTITY_STRING(target, cpt.targetname,
843 vector org = (it.absmin + it.absmax) * 0.5;
844 if(cpt.race_checkpoint == 0)
845 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
847 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
849 it.sprite.realowner = cpt;
850 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
854 if (race_timed_checkpoint) {
856 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
859 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
860 if(it.targetname == "" || !it.targetname) // somehow this is a case...
862 FOREACH_ENTITY_STRING(target, cpt.targetname, {
864 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
867 if(it.classname == "target_checkpoint") {
868 if(it.race_checkpoint == -2)
869 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
872 if (defragcpexists != -1) {
873 float largest_cp_id = 0;
874 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
875 if (cp.race_checkpoint > largest_cp_id) {
876 largest_cp_id = cp.race_checkpoint;
879 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
880 cp.race_checkpoint = largest_cp_id + 1; // finish line
882 race_highest_checkpoint = largest_cp_id + 1;
883 race_timed_checkpoint = largest_cp_id + 1;
885 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
886 cp.race_checkpoint = 255; // finish line
888 race_highest_checkpoint = 255;
889 race_timed_checkpoint = 255;
892 IL_EACH(g_racecheckpoints, it.sprite,
894 if (it.race_checkpoint == 0) {
895 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
896 } else if (it.race_checkpoint == race_timed_checkpoint) {
897 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
904 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
905 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
906 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
912 // These just make the game crash on some maps with oddly shaped triggers.
913 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
914 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
915 // (happened on item triggers too)
920 //setsize(targ, trigger.mins, trigger.maxs);
921 //setorigin(targ, trigger.origin);
929 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
931 if(g_race_qualifying)
934 if(this.race_checkpoint != 0)
936 if(spot.race_place != race_lowest_place_spawn)
941 if(this.race_checkpoint != player.race_respawn_checkpoint)
943 // try reusing the previous spawn
944 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
945 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
946 if(this.race_checkpoint == 0)
948 int pl = player.race_place;
949 if(pl > race_highest_place_spawn)
951 if(pl == 0 && !player.race_started)
952 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
953 if(spot.race_place != pl)
960 spawnfunc(trigger_race_checkpoint)
963 if(!g_race && !g_cts) { delete(this); return; }
967 this.use = checkpoint_use;
968 if (!(this.spawnflags & 1))
969 settouch(this, checkpoint_touch);
971 o = (this.absmin + this.absmax) * 0.5;
972 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
973 waypoint_spawnforitem_force(this, trace_endpos);
974 this.nearestwaypointtimeout = -1;
976 if(this.message == "")
977 this.message = "went backwards";
978 if (this.message2 == "")
979 this.message2 = "was pushed backwards by";
980 if (this.race_penalty_reason == "")
981 this.race_penalty_reason = "missing a checkpoint";
983 this.race_checkpoint = this.cnt;
985 if(this.race_checkpoint > race_highest_checkpoint)
987 race_highest_checkpoint = this.race_checkpoint;
988 if(this.spawnflags & 8)
989 race_timed_checkpoint = this.race_checkpoint;
991 race_timed_checkpoint = 0;
994 if(!this.race_penalty)
996 if(this.race_checkpoint)
997 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
999 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
1002 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1003 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1005 IL_PUSH(g_racecheckpoints, this);
1007 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1010 spawnfunc(target_checkpoint) // defrag entity
1012 if(!g_race && !g_cts) { delete(this); return; }
1015 // if this is targeted, then it probably isn't a trigger
1016 bool is_trigger = this.targetname == "";
1021 this.use = checkpoint_use;
1022 if (is_trigger && !(this.spawnflags & 1))
1023 settouch(this, checkpoint_touch);
1025 vector org = this.origin;
1027 // bots should only pathfind to this if it is a valid touchable trigger
1030 org = (this.absmin + this.absmax) * 0.5;
1031 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1032 waypoint_spawnforitem_force(this, trace_endpos);
1033 this.nearestwaypointtimeout = -1;
1036 if(this.message == "")
1037 this.message = "went backwards";
1038 if (this.message2 == "")
1039 this.message2 = "was pushed backwards by";
1040 if (this.race_penalty_reason == "")
1041 this.race_penalty_reason = "missing a checkpoint";
1043 if(this.classname == "target_startTimer")
1044 this.race_checkpoint = 0;
1046 this.race_checkpoint = -2;
1048 race_timed_checkpoint = 1;
1050 IL_PUSH(g_race_targets, this);
1052 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1055 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1056 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1058 void race_AbandonRaceCheck(entity p)
1060 if(race_completing && !CS(p).race_completed)
1062 CS(p).race_completed = 1;
1063 MAKE_INDEPENDENT_PLAYER(p);
1064 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1065 ClientData_Touch(p);
1069 void race_StartCompleting()
1071 race_completing = 1;
1072 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1075 void race_PreparePlayer(entity this)
1077 race_ClearTime(this);
1078 this.race_place = 0;
1079 this.race_started = 0;
1080 this.race_respawn_checkpoint = 0;
1081 this.race_respawn_spotref = NULL;
1084 void race_RetractPlayer(entity this)
1086 if(!g_race && !g_cts)
1088 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1089 race_ClearTime(this);
1090 this.race_checkpoint = this.race_respawn_checkpoint;
1093 spawnfunc(info_player_race)
1095 if(!g_race && !g_cts) { delete(this); return; }
1097 spawnfunc_info_player_deathmatch(this);
1099 if(this.race_place > race_highest_place_spawn)
1100 race_highest_place_spawn = this.race_place;
1101 if(this.race_place < race_lowest_place_spawn)
1102 race_lowest_place_spawn = this.race_place;
1105 void race_ClearRecords()
1107 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1109 race_checkpoint_records[j] = 0;
1110 strfree(race_checkpoint_recordholders[j]);
1113 FOREACH_CLIENT(true, {
1114 float p = it.race_place;
1115 race_PreparePlayer(it);
1120 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1122 if(g_race_qualifying)
1124 pl.race_penalty_accumulator += penalty;
1125 if(IS_REAL_CLIENT(pl))
1128 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1129 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1130 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1131 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1132 WriteString(MSG_ONE, reason);
1138 pl.race_penalty = time + penalty;
1139 if(IS_REAL_CLIENT(pl))
1142 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1143 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1144 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1145 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1146 WriteString(MSG_ONE, reason);
1152 void penalty_touch(entity this, entity toucher)
1154 EXACTTRIGGER_TOUCH(this, toucher);
1155 if(toucher.race_lastpenalty != this)
1157 toucher.race_lastpenalty = this;
1158 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1162 void penalty_use(entity this, entity actor, entity trigger)
1164 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1167 spawnfunc(trigger_race_penalty)
1169 // TODO: find out why this wasnt done:
1170 //if(!g_cts && !g_race) { remove(this); return; }
1174 this.use = penalty_use;
1175 if (!(this.spawnflags & 1))
1176 settouch(this, penalty_touch);
1178 if (this.race_penalty_reason == "")
1179 this.race_penalty_reason = "missing a checkpoint";
1180 if (!this.race_penalty)
1181 this.race_penalty = 5;
1184 float race_GetFractionalLapCount(entity e)
1186 // interesting metrics (idea by KrimZon) to maybe sort players in the
1187 // scoreboard, immediately updates when overtaking
1189 // requires the track to be built so you never get farther away from the
1190 // next checkpoint, though, and current Xonotic race maps are not built that
1193 // also, this code is slow and would need optimization (i.e. "next CP"
1194 // links on CP entities)
1197 l = GameRules_scoring_add(e, RACE_LAPS, 0);
1198 if(CS(e).race_completed)
1199 return l; // not fractional
1202 float bestfraction, fraction;
1204 float nextcpindex, lastcpindex;
1206 nextcpindex = max(e.race_checkpoint, 0);
1207 lastcpindex = e.race_respawn_checkpoint;
1208 lastcp = e.race_respawn_spotref;
1210 if(nextcpindex == lastcpindex)
1214 IL_EACH(g_racecheckpoints, true,
1216 if(it.race_checkpoint != lastcpindex)
1221 o0 = (it.absmin + it.absmax) * 0.5;
1222 IL_EACH(g_racecheckpoints, true,
1224 if(it.race_checkpoint != nextcpindex)
1226 o1 = (it.absmin + it.absmax) * 0.5;
1229 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1230 if(fraction < bestfraction)
1231 bestfraction = fraction;
1235 // we are at CP "nextcpindex - bestfraction"
1236 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1237 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1239 nc = race_highest_checkpoint + 1;
1240 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;