3 #include <common/weapons/_all.qh>
4 #include <common/stats.qh>
5 #include <server/damage.qh>
6 #include <server/gamelog.qh>
7 #include <server/intermission.qh>
8 #include <server/world.qh>
9 #include <server/miscfunctions.qh>
10 #include <server/weapons/common.qh>
15 #include "spawnpoints.qh"
17 #include "command/getreplies.qh"
18 #include "../common/deathtypes/all.qh"
19 #include "../common/notifications/all.qh"
20 #include <common/gamemodes/_mod.qh>
21 #include <common/gamemodes/rules.qh>
22 #include <common/net_linked.qh>
23 #include <common/state.qh>
24 #include <common/weapons/weapon/porto.qh>
25 #include "../common/mapobjects/subs.qh"
26 #include <common/mapobjects/triggers.qh>
27 #include "../lib/warpzone/util_server.qh"
28 #include "../lib/warpzone/common.qh"
29 #include <common/vehicles/sv_vehicles.qh>
30 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
32 void write_recordmarker(entity pl, float tstart, float dt)
34 GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
36 // also write a marker into demo files for demotc-race-record-extractor to find
39 strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
40 " ", ftos(tstart), " ", ftos(dt), "\n"));
43 IntrusiveList g_race_targets;
44 IntrusiveList g_racecheckpoints;
47 g_race_targets = IL_NEW();
48 g_racecheckpoints = IL_NEW();
51 void race_InitSpectator()
54 if(msg_entity.enemy.race_laptime)
55 race_SendNextCheckpoint(msg_entity.enemy, 1);
58 float race_readTime(string map, float pos)
60 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
62 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
65 string race_readUID(string map, float pos)
67 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
69 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
72 float race_readPos(string map, float t)
74 for(int i = 1; i <= RANKINGS_CNT; ++i)
76 int mytime = race_readTime(map, i);
77 if(!mytime || mytime > t)
81 return 0; // pos is zero if unranked
84 void race_writeTime(string map, float t, string myuid)
86 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
89 newpos = race_readPos(map, t);
92 for(i = 1; i <= RANKINGS_CNT; ++i)
94 if(race_readUID(map, i) == myuid)
99 // player improved his existing record, only have to iterate on ranks between new and old recs
100 for (i = prevpos; i > newpos; --i)
102 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
103 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
108 // player has no ranked record yet
109 for (i = RANKINGS_CNT; i > newpos; --i)
111 float other_time = race_readTime(map, i - 1);
113 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(other_time));
114 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
119 // store new time itself
120 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
121 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
124 string race_readName(string map, float pos)
126 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
128 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
132 const float MAX_CHECKPOINTS = 255;
135 .float race_penalty_accumulator;
136 .string race_penalty_reason;
137 .float race_checkpoint; // player: next checkpoint that has to be reached
138 .entity race_lastpenalty;
142 float race_checkpoint_records[MAX_CHECKPOINTS];
143 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
144 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
145 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
146 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
148 .float race_checkpoint_record[MAX_CHECKPOINTS];
150 float race_highest_checkpoint;
151 float race_timed_checkpoint;
154 float defragcpexists;
156 float race_NextCheckpoint(float f)
158 if(f >= race_highest_checkpoint)
164 float race_PreviousCheckpoint(float f)
169 return race_highest_checkpoint;
175 // 0 = common start/finish
178 float race_CheckpointNetworkID(float f)
180 if(race_timed_checkpoint)
184 else if(f == race_timed_checkpoint)
185 return 255; // finish
190 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
195 int cp = e.race_checkpoint;
196 float recordtime = race_checkpoint_records[cp];
197 float myrecordtime = e.race_checkpoint_record[cp];
198 string recordholder = race_checkpoint_recordholders[cp];
199 if(recordholder == e.netname)
202 if(!IS_REAL_CLIENT(e))
207 WRITESPECTATABLE_MSG_ONE(msg_entity, {
208 WriteHeader(MSG_ONE, TE_CSQC_RACE);
211 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
212 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
213 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
216 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
217 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
218 WriteInt24_t(MSG_ONE, recordtime);
220 WriteInt24_t(MSG_ONE, myrecordtime);
221 WriteString(MSG_ONE, recordholder);
225 void race_send_recordtime(float msg)
227 // send the server best time
228 WriteHeader(msg, TE_CSQC_RACE);
229 WriteByte(msg, RACE_NET_SERVER_RECORD);
230 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
234 void race_send_speedaward(float msg)
236 // send the best speed of the round
237 WriteHeader(msg, TE_CSQC_RACE);
238 WriteByte(msg, RACE_NET_SPEED_AWARD);
239 WriteInt24_t(msg, floor(speedaward_speed+0.5));
240 WriteString(msg, speedaward_holder);
243 void race_send_speedaward_alltimebest(float msg)
245 // send the best speed
246 WriteHeader(msg, TE_CSQC_RACE);
247 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
248 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
249 WriteString(msg, speedaward_alltimebest_holder);
252 void race_send_rankings_cnt(float msg)
254 WriteHeader(msg, TE_CSQC_RACE);
255 WriteByte(msg, RACE_NET_RANKINGS_CNT);
256 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
260 void race_SendRankings(float pos, float prevpos, float del, float msg)
262 WriteHeader(msg, TE_CSQC_RACE);
263 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
264 WriteShort(msg, pos);
265 WriteShort(msg, prevpos);
266 WriteShort(msg, del);
267 WriteString(msg, race_readName(GetMapname(), pos));
268 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
271 void race_SendStatus(float id, entity e)
273 if(!IS_REAL_CLIENT(e))
282 WRITESPECTATABLE_MSG_ONE(msg_entity, {
283 WriteHeader(msg, TE_CSQC_RACE);
284 WriteByte(msg, RACE_NET_SERVER_STATUS);
286 WriteString(msg, e.netname);
290 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
292 // netname only used TEMPORARILY for printing
293 int newpos = race_readPos(map, t);
295 int player_prevpos = 0;
296 for(int i = 1; i <= RANKINGS_CNT; ++i)
298 if(race_readUID(map, i) == myuid)
303 string oldrec_holder;
304 if (player_prevpos && (player_prevpos < newpos || !newpos))
306 oldrec = race_readTime(GetMapname(), player_prevpos);
307 race_SendStatus(0, e); // "fail"
309 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
314 // no ranking, time worse than the worst ranked
315 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
316 race_SendStatus(0, e); // "fail"
318 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
322 // if we didn't hit a return yet, we have a new record!
324 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
328 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
332 if(uid2name(myuid) == "^1Unregistered Player")
335 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
339 oldrec = race_readTime(GetMapname(), newpos);
340 oldrec_holder = race_readName(GetMapname(), newpos);
343 race_writeTime(GetMapname(), t, myuid);
345 if (newpos == 1 && showmessage)
347 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
348 race_send_recordtime(MSG_ALL);
351 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
352 strcpy(rankings_reply, getrankings());
354 if(newpos == player_prevpos)
357 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
358 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
359 else { race_SendStatus(1, e); } // "new time"
364 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
365 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
366 else { race_SendStatus(2, e); } // "new rank"
371 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
372 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
373 else { race_SendStatus(2, e); } // "new rank"
377 void race_deleteTime(string map, float pos)
379 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
381 for(int i = pos; i <= RANKINGS_CNT; ++i)
383 string therank = ftos(i);
384 if (i == RANKINGS_CNT)
386 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
387 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
391 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
392 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
396 race_SendRankings(pos, 0, 1, MSG_ALL);
398 race_send_recordtime(MSG_ALL);
400 strcpy(rankings_reply, getrankings());
403 void race_SendTime(entity e, float cp, float t, float tvalid)
407 if(g_race_qualifying)
408 t += e.race_penalty_accumulator;
410 t = TIME_ENCODE(t); // make integer
413 if(cp == race_timed_checkpoint) // finish line
414 if (!CS(e).race_completed)
417 if(g_race_qualifying)
419 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
421 GameRules_scoring_add(e, RACE_FASTEST, t - s);
425 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
427 GameRules_scoring_add(e, RACE_FASTEST, t - s);
429 s = GameRules_scoring_add(e, RACE_TIME, 0);
430 snew = TIME_ENCODE(time - game_starttime);
431 GameRules_scoring_add(e, RACE_TIME, snew - s);
432 l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
434 if(autocvar_fraglimit)
435 if(l >= autocvar_fraglimit)
436 race_StartCompleting();
440 CS(e).race_completed = 1;
441 MAKE_INDEPENDENT_PLAYER(e);
442 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
448 if(g_race_qualifying)
455 recordtime = race_checkpoint_records[cp];
456 float myrecordtime = e.race_checkpoint_record[cp];
457 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
458 if(recordholder == e.netname)
463 if(cp == race_timed_checkpoint)
465 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
466 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
468 if(t < myrecordtime || myrecordtime == 0)
469 e.race_checkpoint_record[cp] = t; // resending done below
471 if(t < recordtime || recordtime == 0)
473 race_checkpoint_records[cp] = t;
474 strcpy(race_checkpoint_recordholders[cp], e.netname);
475 if(g_race_qualifying)
476 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
489 if(IS_REAL_CLIENT(e))
491 if(g_race_qualifying)
493 FOREACH_CLIENT(IS_REAL_CLIENT(it),
495 if(it == e || (IS_SPEC(it) && it.enemy == e))
498 WriteHeader(MSG_ONE, TE_CSQC_RACE);
499 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
500 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
501 WriteInt24_t(MSG_ONE, t); // time to that intermediate
502 WriteInt24_t(MSG_ONE, recordtime); // previously best time
503 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
504 WriteString(MSG_ONE, recordholder); // record holder
510 else // RACE! Not Qualifying
512 float mylaps, lother, othtime;
513 entity oth = race_checkpoint_lastplayers[cp];
516 mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
517 lother = race_checkpoint_lastlaps[cp];
518 othtime = race_checkpoint_lasttimes[cp];
521 mylaps = lother = othtime = 0;
523 if(IS_REAL_CLIENT(e))
526 WRITESPECTATABLE_MSG_ONE(msg_entity, {
527 WriteHeader(MSG_ONE, TE_CSQC_RACE);
528 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
529 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
532 WriteInt24_t(MSG_ONE, 0);
533 WriteByte(MSG_ONE, 0);
534 WriteByte(MSG_ONE, 0);
538 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
539 WriteByte(MSG_ONE, mylaps - lother);
540 WriteByte(MSG_ONE, etof(oth)); // record holder
545 race_checkpoint_lastplayers[cp] = e;
546 race_checkpoint_lasttimes[cp] = time;
547 race_checkpoint_lastlaps[cp] = mylaps;
549 if(IS_REAL_CLIENT(oth))
552 WRITESPECTATABLE_MSG_ONE(msg_entity, {
553 WriteHeader(MSG_ONE, TE_CSQC_RACE);
554 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
555 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
558 WriteInt24_t(MSG_ONE, 0);
559 WriteByte(MSG_ONE, 0);
560 WriteByte(MSG_ONE, 0);
564 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
565 WriteByte(MSG_ONE, lother - mylaps);
566 WriteByte(MSG_ONE, etof(e) - 1); // record holder
573 void race_ClearTime(entity e)
575 e.race_checkpoint = 0;
577 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
578 e.race_penalty_accumulator = 0;
579 e.race_lastpenalty = NULL;
581 if(!IS_REAL_CLIENT(e))
585 WRITESPECTATABLE_MSG_ONE(msg_entity, {
586 WriteHeader(MSG_ONE, TE_CSQC_RACE);
587 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
591 void checkpoint_passed(entity this, entity player)
593 if(IS_VEHICLE(player) && player.owner)
594 player = player.owner;
596 if(player.personal && autocvar_g_allow_checkpoints)
597 return; // practice mode!
599 if(player.classname == "porto")
601 // do not allow portalling through checkpoints
602 trace_plane_normal = normalize(-1 * player.velocity);
603 W_Porto_Fail(player, 0);
607 string oldmsg; // used twice
612 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
614 oldmsg = this.message;
616 SUB_UseTargets(this, player, player);
617 this.message = oldmsg;
620 if (!IS_PLAYER(player))
624 * Remove unauthorized equipment
626 Portal_ClearAll(player);
628 player.porto_forbidden = 2; // decreased by 1 each StartFrame
632 if(this.race_checkpoint == -2)
634 this.race_checkpoint = player.race_checkpoint;
637 int cp_amount = 0, largest_cp_id = 0;
638 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
641 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
645 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
647 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
652 largest_cp_id = it.race_checkpoint;
653 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
655 it.race_checkpoint = largest_cp_id + 1; // finish line
657 race_highest_checkpoint = largest_cp_id + 1;
658 race_timed_checkpoint = largest_cp_id + 1;
664 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
666 it.race_checkpoint = 1;
668 race_highest_checkpoint = 1;
669 race_timed_checkpoint = 1;
673 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
675 if(this.race_penalty)
677 if(player.race_lastpenalty != this)
679 player.race_lastpenalty = this;
680 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
684 if(player.race_penalty)
690 if(this.spawnflags & 2)
692 oldmsg = this.message;
694 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
695 this.message = oldmsg;
698 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
699 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
700 player.race_respawn_checkpoint = this.race_checkpoint;
701 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
702 player.race_started = 1;
704 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
706 if(!this.race_checkpoint) // start line
708 player.race_laptime = time;
709 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
710 player.race_penalty_accumulator = 0;
711 player.race_lastpenalty = NULL;
714 if(g_race_qualifying)
715 race_SendNextCheckpoint(player, 0);
717 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
720 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
723 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
725 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
731 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
737 if(this.spawnflags & 4)
738 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
742 void checkpoint_touch(entity this, entity toucher)
744 EXACTTRIGGER_TOUCH(this, toucher);
745 checkpoint_passed(this, toucher);
748 void checkpoint_use(entity this, entity actor, entity trigger)
750 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
753 checkpoint_passed(this, actor);
756 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
758 entity own = this.owner;
760 own = this.realowner; // target support
762 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
764 else if(view.race_checkpoint == own.race_checkpoint)
770 void defrag_waypointsprites(entity targeted, entity checkpoint)
772 for(entity t = findchain(target, targeted.targetname); t; t = t.chain)
776 entity s = WP_RaceStart;
778 if(checkpoint.classname == "target_checkpoint")
779 s = WP_RaceCheckpoint;
780 else if(checkpoint.classname == "target_stopTimer")
783 vector o = (t.absmin + t.absmax) * 0.5;
785 WaypointSprite_SpawnFixed(s, o, t, sprite, RADARICON_NONE);
787 t.sprite.realowner = checkpoint;
788 t.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
792 defrag_waypointsprites(t, checkpoint);
796 void trigger_race_checkpoint_verify(entity this)
798 static bool have_verified;
799 if (have_verified) return;
800 have_verified = true;
802 bool qual = g_race_qualifying;
804 int pl_race_checkpoint = 0;
805 int pl_race_place = 0;
808 for (int i = 0; i <= race_highest_checkpoint; ++i) {
809 pl_race_checkpoint = race_NextCheckpoint(i);
811 // race only (middle of the race)
812 g_race_qualifying = false;
814 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
815 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
820 g_race_qualifying = 1;
821 pl_race_place = race_lowest_place_spawn;
822 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
823 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
826 // race only (initial spawn)
827 g_race_qualifying = 0;
828 for (int p = 1; p <= race_highest_place_spawn; ++p) {
830 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
831 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
836 } else if (!defrag_ents) {
838 pl_race_checkpoint = race_NextCheckpoint(0);
839 g_race_qualifying = 1;
840 pl_race_place = race_lowest_place_spawn;
841 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
842 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
845 pl_race_checkpoint = race_NextCheckpoint(0);
846 g_race_qualifying = 1;
847 pl_race_place = 0; // there's only one spawn on defrag maps
849 // check if a defragcp file already exists, then read it and apply the checkpoint order
854 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
856 while ((l = fgets(fh))) {
857 len = tokenize_console(l);
859 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
862 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
863 if (argv(0) == cp.targetname) {
864 cp.race_checkpoint = stof(argv(1));
872 g_race_qualifying = qual;
874 if (race_timed_checkpoint) {
876 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
878 defrag_waypointsprites(it, it);
880 if(it.classname == "target_checkpoint") {
881 if(it.race_checkpoint == -2)
882 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
885 if (defragcpexists != -1) {
886 float largest_cp_id = 0;
887 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
888 if (cp.race_checkpoint > largest_cp_id) {
889 largest_cp_id = cp.race_checkpoint;
892 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
893 cp.race_checkpoint = largest_cp_id + 1; // finish line
895 race_highest_checkpoint = largest_cp_id + 1;
896 race_timed_checkpoint = largest_cp_id + 1;
898 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
899 cp.race_checkpoint = 255; // finish line
901 race_highest_checkpoint = 255;
902 race_timed_checkpoint = 255;
905 IL_EACH(g_racecheckpoints, it.sprite,
907 if (it.race_checkpoint == 0) {
908 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
909 } else if (it.race_checkpoint == race_timed_checkpoint) {
910 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
916 if (defrag_ents) { /* The following hack shall be removed when per-player trigger_multiple.wait is implemented for cts */
917 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
918 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
919 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
925 // These just make the game crash on some maps with oddly shaped triggers.
926 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
927 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
928 // (happened on item triggers too)
933 //setsize(targ, trigger.mins, trigger.maxs);
934 //setorigin(targ, trigger.origin);
942 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
944 if(g_race_qualifying)
947 if(this.race_checkpoint != 0)
949 if(spot.race_place != race_lowest_place_spawn)
954 if(this.race_checkpoint != player.race_respawn_checkpoint)
956 // try reusing the previous spawn
957 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
958 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
959 if(this.race_checkpoint == 0)
961 int pl = player.race_place;
962 if(pl > race_highest_place_spawn)
964 if(pl == 0 && !player.race_started)
965 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
966 if(spot.race_place != pl)
973 spawnfunc(trigger_race_checkpoint)
976 if(!g_race && !g_cts) { delete(this); return; }
980 this.use = checkpoint_use;
981 if (!(this.spawnflags & 1))
982 settouch(this, checkpoint_touch);
984 o = (this.absmin + this.absmax) * 0.5;
985 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
986 waypoint_spawnforitem_force(this, trace_endpos);
987 this.nearestwaypointtimeout = -1;
989 if(this.message == "")
990 this.message = "went backwards";
991 if (this.message2 == "")
992 this.message2 = "was pushed backwards by";
993 if (this.race_penalty_reason == "")
994 this.race_penalty_reason = "missing a checkpoint";
996 this.race_checkpoint = this.cnt;
998 if(this.race_checkpoint > race_highest_checkpoint)
1000 race_highest_checkpoint = this.race_checkpoint;
1001 if(this.spawnflags & 8)
1002 race_timed_checkpoint = this.race_checkpoint;
1004 race_timed_checkpoint = 0;
1007 if(!this.race_penalty)
1009 if(this.race_checkpoint)
1010 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
1012 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
1015 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1016 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1018 IL_PUSH(g_racecheckpoints, this);
1020 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1023 spawnfunc(target_checkpoint) // defrag entity
1025 if(!g_race && !g_cts) { delete(this); return; }
1028 // if this is targeted, then it probably isn't a trigger
1029 bool is_trigger = this.targetname == "";
1034 this.use = checkpoint_use;
1035 if (is_trigger && !(this.spawnflags & 1))
1036 settouch(this, checkpoint_touch);
1038 vector org = this.origin;
1040 // bots should only pathfind to this if it is a valid touchable trigger
1043 org = (this.absmin + this.absmax) * 0.5;
1044 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1045 waypoint_spawnforitem_force(this, trace_endpos);
1046 this.nearestwaypointtimeout = -1;
1049 if(this.message == "")
1050 this.message = "went backwards";
1051 if (this.message2 == "")
1052 this.message2 = "was pushed backwards by";
1053 if (this.race_penalty_reason == "")
1054 this.race_penalty_reason = "missing a checkpoint";
1056 if(this.classname == "target_startTimer")
1057 this.race_checkpoint = 0;
1059 this.race_checkpoint = -2;
1061 race_timed_checkpoint = 1;
1063 IL_PUSH(g_race_targets, this);
1065 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1068 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1069 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1071 void race_AbandonRaceCheck(entity p)
1073 if(race_completing && !CS(p).race_completed)
1075 CS(p).race_completed = 1;
1076 MAKE_INDEPENDENT_PLAYER(p);
1077 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1078 ClientData_Touch(p);
1082 void race_StartCompleting()
1084 race_completing = 1;
1085 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1088 void race_PreparePlayer(entity this)
1090 race_ClearTime(this);
1091 this.race_place = 0;
1092 this.race_started = 0;
1093 this.race_respawn_checkpoint = 0;
1094 this.race_respawn_spotref = NULL;
1097 void race_RetractPlayer(entity this)
1099 if(!g_race && !g_cts)
1101 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1102 race_ClearTime(this);
1103 this.race_checkpoint = this.race_respawn_checkpoint;
1106 spawnfunc(info_player_race)
1108 if(!g_race && !g_cts) { delete(this); return; }
1110 spawnfunc_info_player_deathmatch(this);
1112 if(this.race_place > race_highest_place_spawn)
1113 race_highest_place_spawn = this.race_place;
1114 if(this.race_place < race_lowest_place_spawn)
1115 race_lowest_place_spawn = this.race_place;
1118 void race_ClearRecords()
1120 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1122 race_checkpoint_records[j] = 0;
1123 strfree(race_checkpoint_recordholders[j]);
1126 FOREACH_CLIENT(true, {
1127 float p = it.race_place;
1128 race_PreparePlayer(it);
1133 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1135 if(g_race_qualifying)
1137 pl.race_penalty_accumulator += penalty;
1138 if(IS_REAL_CLIENT(pl))
1141 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1142 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1143 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1144 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1145 WriteString(MSG_ONE, reason);
1151 pl.race_penalty = time + penalty;
1152 if(IS_REAL_CLIENT(pl))
1155 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1156 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1157 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1158 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1159 WriteString(MSG_ONE, reason);
1165 void penalty_touch(entity this, entity toucher)
1167 EXACTTRIGGER_TOUCH(this, toucher);
1168 if(toucher.race_lastpenalty != this)
1170 toucher.race_lastpenalty = this;
1171 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1175 void penalty_use(entity this, entity actor, entity trigger)
1177 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1180 spawnfunc(trigger_race_penalty)
1182 // TODO: find out why this wasnt done:
1183 //if(!g_cts && !g_race) { remove(this); return; }
1187 this.use = penalty_use;
1188 if (!(this.spawnflags & 1))
1189 settouch(this, penalty_touch);
1191 if (this.race_penalty_reason == "")
1192 this.race_penalty_reason = "missing a checkpoint";
1193 if (!this.race_penalty)
1194 this.race_penalty = 5;
1197 float race_GetFractionalLapCount(entity e)
1199 // interesting metrics (idea by KrimZon) to maybe sort players in the
1200 // scoreboard, immediately updates when overtaking
1202 // requires the track to be built so you never get farther away from the
1203 // next checkpoint, though, and current Xonotic race maps are not built that
1206 // also, this code is slow and would need optimization (i.e. "next CP"
1207 // links on CP entities)
1210 l = GameRules_scoring_add(e, RACE_LAPS, 0);
1211 if(CS(e).race_completed)
1212 return l; // not fractional
1215 float bestfraction, fraction;
1217 float nextcpindex, lastcpindex;
1219 nextcpindex = max(e.race_checkpoint, 0);
1220 lastcpindex = e.race_respawn_checkpoint;
1221 lastcp = e.race_respawn_spotref;
1223 if(nextcpindex == lastcpindex)
1227 IL_EACH(g_racecheckpoints, true,
1229 if(it.race_checkpoint != lastcpindex)
1234 o0 = (it.absmin + it.absmax) * 0.5;
1235 IL_EACH(g_racecheckpoints, true,
1237 if(it.race_checkpoint != nextcpindex)
1239 o1 = (it.absmin + it.absmax) * 0.5;
1242 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1243 if(fraction < bestfraction)
1244 bestfraction = fraction;
1248 // we are at CP "nextcpindex - bestfraction"
1249 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1250 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1252 nc = race_highest_checkpoint + 1;
1253 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;