3 #include <common/weapons/_all.qh>
4 #include <common/stats.qh>
5 #include <server/damage.qh>
6 #include <server/world.qh>
7 #include <server/miscfunctions.qh>
8 #include <server/weapons/common.qh>
13 #include "spawnpoints.qh"
15 #include "command/getreplies.qh"
16 #include "../common/deathtypes/all.qh"
17 #include "../common/notifications/all.qh"
18 #include <common/gamemodes/_mod.qh>
19 #include <common/gamemodes/rules.qh>
20 #include <common/net_linked.qh>
21 #include <common/state.qh>
22 #include <common/weapons/weapon/porto.qh>
23 #include "../common/mapobjects/subs.qh"
24 #include <common/mapobjects/triggers.qh>
25 #include "../lib/warpzone/util_server.qh"
26 #include "../lib/warpzone/common.qh"
27 #include <common/vehicles/sv_vehicles.qh>
28 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
30 IntrusiveList g_race_targets;
31 IntrusiveList g_racecheckpoints;
34 g_race_targets = IL_NEW();
35 g_racecheckpoints = IL_NEW();
38 void race_InitSpectator()
41 if(msg_entity.enemy.race_laptime)
42 race_SendNextCheckpoint(msg_entity.enemy, 1);
45 float race_readTime(string map, float pos)
47 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
49 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
52 string race_readUID(string map, float pos)
54 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
56 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
59 float race_readPos(string map, float t)
61 for(int i = 1; i <= RANKINGS_CNT; ++i)
63 int mytime = race_readTime(map, i);
64 if(!mytime || mytime > t)
68 return 0; // pos is zero if unranked
71 void race_writeTime(string map, float t, string myuid)
73 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
76 newpos = race_readPos(map, t);
79 for(i = 1; i <= RANKINGS_CNT; ++i)
81 if(race_readUID(map, i) == myuid)
86 // player improved his existing record, only have to iterate on ranks between new and old recs
87 for (i = prevpos; i > newpos; --i)
89 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
90 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
95 // player has no ranked record yet
96 for (i = RANKINGS_CNT; i > newpos; --i)
98 float other_time = race_readTime(map, i - 1);
100 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(other_time));
101 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
106 // store new time itself
107 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
108 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
111 string race_readName(string map, float pos)
113 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
115 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
119 const float MAX_CHECKPOINTS = 255;
122 .float race_penalty_accumulator;
123 .string race_penalty_reason;
124 .float race_checkpoint; // player: next checkpoint that has to be reached
125 .entity race_lastpenalty;
129 float race_checkpoint_records[MAX_CHECKPOINTS];
130 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
131 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
132 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
133 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
135 .float race_checkpoint_record[MAX_CHECKPOINTS];
137 float race_highest_checkpoint;
138 float race_timed_checkpoint;
141 float defragcpexists;
143 float race_NextCheckpoint(float f)
145 if(f >= race_highest_checkpoint)
151 float race_PreviousCheckpoint(float f)
156 return race_highest_checkpoint;
162 // 0 = common start/finish
165 float race_CheckpointNetworkID(float f)
167 if(race_timed_checkpoint)
171 else if(f == race_timed_checkpoint)
172 return 255; // finish
177 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
182 int cp = e.race_checkpoint;
183 float recordtime = race_checkpoint_records[cp];
184 float myrecordtime = e.race_checkpoint_record[cp];
185 string recordholder = race_checkpoint_recordholders[cp];
186 if(recordholder == e.netname)
189 if(!IS_REAL_CLIENT(e))
194 WRITESPECTATABLE_MSG_ONE(msg_entity, {
195 WriteHeader(MSG_ONE, TE_CSQC_RACE);
198 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
199 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
200 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
203 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
204 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
205 WriteInt24_t(MSG_ONE, recordtime);
207 WriteInt24_t(MSG_ONE, myrecordtime);
208 WriteString(MSG_ONE, recordholder);
212 void race_send_recordtime(float msg)
214 // send the server best time
215 WriteHeader(msg, TE_CSQC_RACE);
216 WriteByte(msg, RACE_NET_SERVER_RECORD);
217 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
221 void race_send_speedaward(float msg)
223 // send the best speed of the round
224 WriteHeader(msg, TE_CSQC_RACE);
225 WriteByte(msg, RACE_NET_SPEED_AWARD);
226 WriteInt24_t(msg, floor(speedaward_speed+0.5));
227 WriteString(msg, speedaward_holder);
230 void race_send_speedaward_alltimebest(float msg)
232 // send the best speed
233 WriteHeader(msg, TE_CSQC_RACE);
234 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
235 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
236 WriteString(msg, speedaward_alltimebest_holder);
239 void race_send_rankings_cnt(float msg)
241 WriteHeader(msg, TE_CSQC_RACE);
242 WriteByte(msg, RACE_NET_RANKINGS_CNT);
243 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
247 void race_SendRankings(float pos, float prevpos, float del, float msg)
249 WriteHeader(msg, TE_CSQC_RACE);
250 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
251 WriteShort(msg, pos);
252 WriteShort(msg, prevpos);
253 WriteShort(msg, del);
254 WriteString(msg, race_readName(GetMapname(), pos));
255 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
258 void race_SendStatus(float id, entity e)
260 if(!IS_REAL_CLIENT(e))
269 WRITESPECTATABLE_MSG_ONE(msg_entity, {
270 WriteHeader(msg, TE_CSQC_RACE);
271 WriteByte(msg, RACE_NET_SERVER_STATUS);
273 WriteString(msg, e.netname);
277 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
279 // netname only used TEMPORARILY for printing
280 int newpos = race_readPos(map, t);
282 int player_prevpos = 0;
283 for(int i = 1; i <= RANKINGS_CNT; ++i)
285 if(race_readUID(map, i) == myuid)
290 string oldrec_holder;
291 if (player_prevpos && (player_prevpos < newpos || !newpos))
293 oldrec = race_readTime(GetMapname(), player_prevpos);
294 race_SendStatus(0, e); // "fail"
296 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
301 // no ranking, time worse than the worst ranked
302 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
303 race_SendStatus(0, e); // "fail"
305 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
309 // if we didn't hit a return yet, we have a new record!
311 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
315 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
319 if(uid2name(myuid) == "^1Unregistered Player")
322 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
326 oldrec = race_readTime(GetMapname(), newpos);
327 oldrec_holder = race_readName(GetMapname(), newpos);
330 race_writeTime(GetMapname(), t, myuid);
332 if (newpos == 1 && showmessage)
334 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
335 race_send_recordtime(MSG_ALL);
338 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
339 strcpy(rankings_reply, getrankings());
341 if(newpos == player_prevpos)
344 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
345 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
346 else { race_SendStatus(1, e); } // "new time"
351 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
352 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
353 else { race_SendStatus(2, e); } // "new rank"
358 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
359 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
360 else { race_SendStatus(2, e); } // "new rank"
364 void race_deleteTime(string map, float pos)
366 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
368 for(int i = pos; i <= RANKINGS_CNT; ++i)
370 string therank = ftos(i);
371 if (i == RANKINGS_CNT)
373 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
374 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
378 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
379 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
383 race_SendRankings(pos, 0, 1, MSG_ALL);
385 race_send_recordtime(MSG_ALL);
387 strcpy(rankings_reply, getrankings());
390 void race_SendTime(entity e, float cp, float t, float tvalid)
394 if(g_race_qualifying)
395 t += e.race_penalty_accumulator;
397 t = TIME_ENCODE(t); // make integer
400 if(cp == race_timed_checkpoint) // finish line
401 if (!CS(e).race_completed)
404 if(g_race_qualifying)
406 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
408 GameRules_scoring_add(e, RACE_FASTEST, t - s);
412 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
414 GameRules_scoring_add(e, RACE_FASTEST, t - s);
416 s = GameRules_scoring_add(e, RACE_TIME, 0);
417 snew = TIME_ENCODE(time - game_starttime);
418 GameRules_scoring_add(e, RACE_TIME, snew - s);
419 l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
421 if(autocvar_fraglimit)
422 if(l >= autocvar_fraglimit)
423 race_StartCompleting();
427 CS(e).race_completed = 1;
428 MAKE_INDEPENDENT_PLAYER(e);
429 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
435 if(g_race_qualifying)
442 recordtime = race_checkpoint_records[cp];
443 float myrecordtime = e.race_checkpoint_record[cp];
444 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
445 if(recordholder == e.netname)
450 if(cp == race_timed_checkpoint)
452 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
453 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
455 if(t < myrecordtime || myrecordtime == 0)
456 e.race_checkpoint_record[cp] = t; // resending done below
458 if(t < recordtime || recordtime == 0)
460 race_checkpoint_records[cp] = t;
461 strcpy(race_checkpoint_recordholders[cp], e.netname);
462 if(g_race_qualifying)
463 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
476 if(IS_REAL_CLIENT(e))
478 if(g_race_qualifying)
480 FOREACH_CLIENT(IS_REAL_CLIENT(it),
482 if(it == e || (IS_SPEC(it) && it.enemy == e))
485 WriteHeader(MSG_ONE, TE_CSQC_RACE);
486 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
487 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
488 WriteInt24_t(MSG_ONE, t); // time to that intermediate
489 WriteInt24_t(MSG_ONE, recordtime); // previously best time
490 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
491 WriteString(MSG_ONE, recordholder); // record holder
497 else // RACE! Not Qualifying
499 float mylaps, lother, othtime;
500 entity oth = race_checkpoint_lastplayers[cp];
503 mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
504 lother = race_checkpoint_lastlaps[cp];
505 othtime = race_checkpoint_lasttimes[cp];
508 mylaps = lother = othtime = 0;
510 if(IS_REAL_CLIENT(e))
513 WRITESPECTATABLE_MSG_ONE(msg_entity, {
514 WriteHeader(MSG_ONE, TE_CSQC_RACE);
515 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
516 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
519 WriteInt24_t(MSG_ONE, 0);
520 WriteByte(MSG_ONE, 0);
521 WriteByte(MSG_ONE, 0);
525 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
526 WriteByte(MSG_ONE, mylaps - lother);
527 WriteByte(MSG_ONE, etof(oth)); // record holder
532 race_checkpoint_lastplayers[cp] = e;
533 race_checkpoint_lasttimes[cp] = time;
534 race_checkpoint_lastlaps[cp] = mylaps;
536 if(IS_REAL_CLIENT(oth))
539 WRITESPECTATABLE_MSG_ONE(msg_entity, {
540 WriteHeader(MSG_ONE, TE_CSQC_RACE);
541 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
542 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
545 WriteInt24_t(MSG_ONE, 0);
546 WriteByte(MSG_ONE, 0);
547 WriteByte(MSG_ONE, 0);
551 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
552 WriteByte(MSG_ONE, lother - mylaps);
553 WriteByte(MSG_ONE, etof(e) - 1); // record holder
560 void race_ClearTime(entity e)
562 e.race_checkpoint = 0;
564 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
565 e.race_penalty_accumulator = 0;
566 e.race_lastpenalty = NULL;
568 if(!IS_REAL_CLIENT(e))
572 WRITESPECTATABLE_MSG_ONE(msg_entity, {
573 WriteHeader(MSG_ONE, TE_CSQC_RACE);
574 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
578 void checkpoint_passed(entity this, entity player)
580 if(IS_VEHICLE(player) && player.owner)
581 player = player.owner;
583 if(player.personal && autocvar_g_allow_checkpoints)
584 return; // practice mode!
586 if(player.classname == "porto")
588 // do not allow portalling through checkpoints
589 trace_plane_normal = normalize(-1 * player.velocity);
590 W_Porto_Fail(player, 0);
594 string oldmsg; // used twice
599 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
601 oldmsg = this.message;
603 SUB_UseTargets(this, player, player);
604 this.message = oldmsg;
607 if (!IS_PLAYER(player))
611 * Remove unauthorized equipment
613 Portal_ClearAll(player);
615 player.porto_forbidden = 2; // decreased by 1 each StartFrame
619 if(this.race_checkpoint == -2)
621 this.race_checkpoint = player.race_checkpoint;
624 int cp_amount = 0, largest_cp_id = 0;
625 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
628 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
632 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
634 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
639 largest_cp_id = it.race_checkpoint;
640 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
642 it.race_checkpoint = largest_cp_id + 1; // finish line
644 race_highest_checkpoint = largest_cp_id + 1;
645 race_timed_checkpoint = largest_cp_id + 1;
651 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
653 it.race_checkpoint = 1;
655 race_highest_checkpoint = 1;
656 race_timed_checkpoint = 1;
660 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
662 if(this.race_penalty)
664 if(player.race_lastpenalty != this)
666 player.race_lastpenalty = this;
667 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
671 if(player.race_penalty)
677 if(this.spawnflags & 2)
679 oldmsg = this.message;
681 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
682 this.message = oldmsg;
685 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
686 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
687 player.race_respawn_checkpoint = this.race_checkpoint;
688 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
689 player.race_started = 1;
691 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
693 if(!this.race_checkpoint) // start line
695 player.race_laptime = time;
696 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
697 player.race_penalty_accumulator = 0;
698 player.race_lastpenalty = NULL;
701 if(g_race_qualifying)
702 race_SendNextCheckpoint(player, 0);
704 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
707 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
710 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
712 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
718 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
724 if(this.spawnflags & 4)
725 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
729 void checkpoint_touch(entity this, entity toucher)
731 EXACTTRIGGER_TOUCH(this, toucher);
732 checkpoint_passed(this, toucher);
735 void checkpoint_use(entity this, entity actor, entity trigger)
737 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
740 checkpoint_passed(this, actor);
743 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
745 entity own = this.owner;
747 own = this.realowner; // target support
749 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
751 else if(view.race_checkpoint == own.race_checkpoint)
757 void defrag_waypointsprites(entity targeted, entity checkpoint)
759 for(entity t = findchain(target, targeted.targetname); t; t = t.chain)
763 entity s = WP_RaceStart;
765 if(checkpoint.classname == "target_checkpoint")
766 s = WP_RaceCheckpoint;
767 else if(checkpoint.classname == "target_stopTimer")
770 vector o = (t.absmin + t.absmax) * 0.5;
772 WaypointSprite_SpawnFixed(s, o, t, sprite, RADARICON_NONE);
774 t.sprite.realowner = checkpoint;
775 t.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
779 defrag_waypointsprites(t, checkpoint);
783 void trigger_race_checkpoint_verify(entity this)
785 static bool have_verified;
786 if (have_verified) return;
787 have_verified = true;
789 bool qual = g_race_qualifying;
791 int pl_race_checkpoint = 0;
792 int pl_race_place = 0;
795 for (int i = 0; i <= race_highest_checkpoint; ++i) {
796 pl_race_checkpoint = race_NextCheckpoint(i);
798 // race only (middle of the race)
799 g_race_qualifying = false;
801 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
802 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
807 g_race_qualifying = 1;
808 pl_race_place = race_lowest_place_spawn;
809 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
810 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
813 // race only (initial spawn)
814 g_race_qualifying = 0;
815 for (int p = 1; p <= race_highest_place_spawn; ++p) {
817 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
818 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
823 } else if (!defrag_ents) {
825 pl_race_checkpoint = race_NextCheckpoint(0);
826 g_race_qualifying = 1;
827 pl_race_place = race_lowest_place_spawn;
828 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
829 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
832 pl_race_checkpoint = race_NextCheckpoint(0);
833 g_race_qualifying = 1;
834 pl_race_place = 0; // there's only one spawn on defrag maps
836 // check if a defragcp file already exists, then read it and apply the checkpoint order
841 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
843 while ((l = fgets(fh))) {
844 len = tokenize_console(l);
846 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
849 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
850 if (argv(0) == cp.targetname) {
851 cp.race_checkpoint = stof(argv(1));
859 g_race_qualifying = qual;
861 if (race_timed_checkpoint) {
863 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
865 defrag_waypointsprites(it, it);
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);
903 if (defrag_ents) { /* The following hack shall be removed when per-player trigger_multiple.wait is implemented for cts */
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;