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 trigger_race_checkpoint_verify(entity this)
772 static bool have_verified;
773 if (have_verified) return;
774 have_verified = true;
776 bool qual = g_race_qualifying;
778 int pl_race_checkpoint = 0;
779 int pl_race_place = 0;
782 for (int i = 0; i <= race_highest_checkpoint; ++i) {
783 pl_race_checkpoint = race_NextCheckpoint(i);
785 // race only (middle of the race)
786 g_race_qualifying = false;
788 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
789 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
794 g_race_qualifying = 1;
795 pl_race_place = race_lowest_place_spawn;
796 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
797 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
800 // race only (initial spawn)
801 g_race_qualifying = 0;
802 for (int p = 1; p <= race_highest_place_spawn; ++p) {
804 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
805 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
810 } else if (!defrag_ents) {
812 pl_race_checkpoint = race_NextCheckpoint(0);
813 g_race_qualifying = 1;
814 pl_race_place = race_lowest_place_spawn;
815 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
816 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
819 pl_race_checkpoint = race_NextCheckpoint(0);
820 g_race_qualifying = 1;
821 pl_race_place = 0; // there's only one spawn on defrag maps
823 // check if a defragcp file already exists, then read it and apply the checkpoint order
828 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
830 while ((l = fgets(fh))) {
831 len = tokenize_console(l);
833 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
836 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
837 if (argv(0) == cp.targetname) {
838 cp.race_checkpoint = stof(argv(1));
846 g_race_qualifying = qual;
848 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
850 if(it.targetname == "" || !it.targetname) // somehow this is a case...
853 FOREACH_ENTITY_STRING(target, cpt.targetname,
855 vector org = (it.absmin + it.absmax) * 0.5;
856 if(cpt.race_checkpoint == 0)
857 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
859 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
861 it.sprite.realowner = cpt;
862 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
866 if (race_timed_checkpoint) {
868 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
871 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
872 if(it.targetname == "" || !it.targetname) // somehow this is a case...
874 FOREACH_ENTITY_STRING(target, cpt.targetname, {
876 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
879 if(it.classname == "target_checkpoint") {
880 if(it.race_checkpoint == -2)
881 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
884 if (defragcpexists != -1) {
885 float largest_cp_id = 0;
886 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
887 if (cp.race_checkpoint > largest_cp_id) {
888 largest_cp_id = cp.race_checkpoint;
891 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
892 cp.race_checkpoint = largest_cp_id + 1; // finish line
894 race_highest_checkpoint = largest_cp_id + 1;
895 race_timed_checkpoint = largest_cp_id + 1;
897 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
898 cp.race_checkpoint = 255; // finish line
900 race_highest_checkpoint = 255;
901 race_timed_checkpoint = 255;
904 IL_EACH(g_racecheckpoints, it.sprite,
906 if (it.race_checkpoint == 0) {
907 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
908 } else if (it.race_checkpoint == race_timed_checkpoint) {
909 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
916 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
917 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
918 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
924 // These just make the game crash on some maps with oddly shaped triggers.
925 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
926 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
927 // (happened on item triggers too)
932 //setsize(targ, trigger.mins, trigger.maxs);
933 //setorigin(targ, trigger.origin);
941 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
943 if(g_race_qualifying)
946 if(this.race_checkpoint != 0)
948 if(spot.race_place != race_lowest_place_spawn)
953 if(this.race_checkpoint != player.race_respawn_checkpoint)
955 // try reusing the previous spawn
956 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
957 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
958 if(this.race_checkpoint == 0)
960 int pl = player.race_place;
961 if(pl > race_highest_place_spawn)
963 if(pl == 0 && !player.race_started)
964 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
965 if(spot.race_place != pl)
972 spawnfunc(trigger_race_checkpoint)
975 if(!g_race && !g_cts) { delete(this); return; }
979 this.use = checkpoint_use;
980 if (!(this.spawnflags & 1))
981 settouch(this, checkpoint_touch);
983 o = (this.absmin + this.absmax) * 0.5;
984 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
985 waypoint_spawnforitem_force(this, trace_endpos);
986 this.nearestwaypointtimeout = -1;
988 if(this.message == "")
989 this.message = "went backwards";
990 if (this.message2 == "")
991 this.message2 = "was pushed backwards by";
992 if (this.race_penalty_reason == "")
993 this.race_penalty_reason = "missing a checkpoint";
995 this.race_checkpoint = this.cnt;
997 if(this.race_checkpoint > race_highest_checkpoint)
999 race_highest_checkpoint = this.race_checkpoint;
1000 if(this.spawnflags & 8)
1001 race_timed_checkpoint = this.race_checkpoint;
1003 race_timed_checkpoint = 0;
1006 if(!this.race_penalty)
1008 if(this.race_checkpoint)
1009 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
1011 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
1014 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1015 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1017 IL_PUSH(g_racecheckpoints, this);
1019 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1022 spawnfunc(target_checkpoint) // defrag entity
1024 if(!g_race && !g_cts) { delete(this); return; }
1027 // if this is targeted, then it probably isn't a trigger
1028 bool is_trigger = this.targetname == "";
1033 this.use = checkpoint_use;
1034 if (is_trigger && !(this.spawnflags & 1))
1035 settouch(this, checkpoint_touch);
1037 vector org = this.origin;
1039 // bots should only pathfind to this if it is a valid touchable trigger
1042 org = (this.absmin + this.absmax) * 0.5;
1043 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1044 waypoint_spawnforitem_force(this, trace_endpos);
1045 this.nearestwaypointtimeout = -1;
1048 if(this.message == "")
1049 this.message = "went backwards";
1050 if (this.message2 == "")
1051 this.message2 = "was pushed backwards by";
1052 if (this.race_penalty_reason == "")
1053 this.race_penalty_reason = "missing a checkpoint";
1055 if(this.classname == "target_startTimer")
1056 this.race_checkpoint = 0;
1058 this.race_checkpoint = -2;
1060 race_timed_checkpoint = 1;
1062 IL_PUSH(g_race_targets, this);
1064 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1067 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1068 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1070 void race_AbandonRaceCheck(entity p)
1072 if(race_completing && !CS(p).race_completed)
1074 CS(p).race_completed = 1;
1075 MAKE_INDEPENDENT_PLAYER(p);
1076 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1077 ClientData_Touch(p);
1081 void race_StartCompleting()
1083 race_completing = 1;
1084 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1087 void race_PreparePlayer(entity this)
1089 race_ClearTime(this);
1090 this.race_place = 0;
1091 this.race_started = 0;
1092 this.race_respawn_checkpoint = 0;
1093 this.race_respawn_spotref = NULL;
1096 void race_RetractPlayer(entity this)
1098 if(!g_race && !g_cts)
1100 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1101 race_ClearTime(this);
1102 this.race_checkpoint = this.race_respawn_checkpoint;
1105 spawnfunc(info_player_race)
1107 if(!g_race && !g_cts) { delete(this); return; }
1109 spawnfunc_info_player_deathmatch(this);
1111 if(this.race_place > race_highest_place_spawn)
1112 race_highest_place_spawn = this.race_place;
1113 if(this.race_place < race_lowest_place_spawn)
1114 race_lowest_place_spawn = this.race_place;
1117 void race_ClearRecords()
1119 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1121 race_checkpoint_records[j] = 0;
1122 strfree(race_checkpoint_recordholders[j]);
1125 FOREACH_CLIENT(true, {
1126 float p = it.race_place;
1127 race_PreparePlayer(it);
1132 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1134 if(g_race_qualifying)
1136 pl.race_penalty_accumulator += penalty;
1137 if(IS_REAL_CLIENT(pl))
1140 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1141 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1142 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1143 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1144 WriteString(MSG_ONE, reason);
1150 pl.race_penalty = time + penalty;
1151 if(IS_REAL_CLIENT(pl))
1154 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1155 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1156 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1157 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1158 WriteString(MSG_ONE, reason);
1164 void penalty_touch(entity this, entity toucher)
1166 EXACTTRIGGER_TOUCH(this, toucher);
1167 if(toucher.race_lastpenalty != this)
1169 toucher.race_lastpenalty = this;
1170 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1174 void penalty_use(entity this, entity actor, entity trigger)
1176 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1179 spawnfunc(trigger_race_penalty)
1181 // TODO: find out why this wasnt done:
1182 //if(!g_cts && !g_race) { remove(this); return; }
1186 this.use = penalty_use;
1187 if (!(this.spawnflags & 1))
1188 settouch(this, penalty_touch);
1190 if (this.race_penalty_reason == "")
1191 this.race_penalty_reason = "missing a checkpoint";
1192 if (!this.race_penalty)
1193 this.race_penalty = 5;
1196 float race_GetFractionalLapCount(entity e)
1198 // interesting metrics (idea by KrimZon) to maybe sort players in the
1199 // scoreboard, immediately updates when overtaking
1201 // requires the track to be built so you never get farther away from the
1202 // next checkpoint, though, and current Xonotic race maps are not built that
1205 // also, this code is slow and would need optimization (i.e. "next CP"
1206 // links on CP entities)
1209 l = GameRules_scoring_add(e, RACE_LAPS, 0);
1210 if(CS(e).race_completed)
1211 return l; // not fractional
1214 float bestfraction, fraction;
1216 float nextcpindex, lastcpindex;
1218 nextcpindex = max(e.race_checkpoint, 0);
1219 lastcpindex = e.race_respawn_checkpoint;
1220 lastcp = e.race_respawn_spotref;
1222 if(nextcpindex == lastcpindex)
1226 IL_EACH(g_racecheckpoints, true,
1228 if(it.race_checkpoint != lastcpindex)
1233 o0 = (it.absmin + it.absmax) * 0.5;
1234 IL_EACH(g_racecheckpoints, true,
1236 if(it.race_checkpoint != nextcpindex)
1238 o1 = (it.absmin + it.absmax) * 0.5;
1241 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1242 if(fraction < bestfraction)
1243 bestfraction = fraction;
1247 // we are at CP "nextcpindex - bestfraction"
1248 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1249 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1251 nc = race_highest_checkpoint + 1;
1252 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;