3 #include <common/deathtypes/all.qh>
4 #include <common/gamemodes/_mod.qh>
5 #include <common/gamemodes/rules.qh>
6 #include <common/mapobjects/subs.qh>
7 #include <common/mapobjects/triggers.qh>
8 #include <common/mutators/mutator/waypoints/waypointsprites.qh>
9 #include <common/net_linked.qh>
10 #include <common/notifications/all.qh>
11 #include <common/state.qh>
12 #include <common/stats.qh>
13 #include <common/vehicles/sv_vehicles.qh>
14 #include <common/weapons/_all.qh>
15 #include <common/weapons/weapon/porto.qh>
16 #include <lib/warpzone/common.qh>
17 #include <lib/warpzone/util_server.qh>
18 #include <server/bot/api.qh>
19 #include <server/cheats.qh>
20 #include <server/client.qh>
21 #include <server/command/getreplies.qh>
22 #include <server/damage.qh>
23 #include <server/gamelog.qh>
24 #include <server/intermission.qh>
25 #include <server/main.qh>
26 #include <server/mutators/_mod.qh>
27 #include <server/portals.qh>
28 #include <server/scores.qh>
29 #include <server/spawnpoints.qh>
30 #include <server/weapons/common.qh>
31 #include <server/world.qh>
33 string uid2name(string myuid)
35 string s = db_get(ServerProgsDB, strcat("/uid2name/", myuid));
37 // FIXME remove this later after 0.6 release
38 // convert old style broken records to correct style
41 s = db_get(ServerProgsDB, strcat("uid2name", myuid));
44 db_put(ServerProgsDB, strcat("/uid2name/", myuid), s);
45 db_remove(ServerProgsDB, strcat("uid2name", myuid));
50 s = "^1Unregistered Player";
54 void write_recordmarker(entity pl, float tstart, float dt)
56 GameLogEcho(strcat(":recordset:", ftos(pl.playerid), ":", ftos(dt)));
58 // also write a marker into demo files for demotc-race-record-extractor to find
61 strcat("//", strconv(2, 0, 0, GetGametype()), " RECORD SET ", TIME_ENCODED_TOSTRING(TIME_ENCODE(dt))),
62 " ", ftos(tstart), " ", ftos(dt), "\n"));
65 IntrusiveList g_race_targets;
66 IntrusiveList g_racecheckpoints;
69 g_race_targets = IL_NEW();
70 g_racecheckpoints = IL_NEW();
73 void race_InitSpectator()
76 if(msg_entity.enemy.race_laptime)
77 race_SendNextCheckpoint(msg_entity.enemy, 1);
80 float race_readTime(string map, float pos)
82 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
84 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
87 string race_readUID(string map, float pos)
89 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
91 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
94 float race_readPos(string map, float t)
96 for(int i = 1; i <= RANKINGS_CNT; ++i)
98 int mytime = race_readTime(map, i);
99 if(!mytime || mytime > t)
103 return 0; // pos is zero if unranked
106 void race_writeTime(string map, float t, string myuid)
108 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
111 newpos = race_readPos(map, t);
113 float i, prevpos = 0;
114 for(i = 1; i <= RANKINGS_CNT; ++i)
116 if(race_readUID(map, i) == myuid)
121 // player improved his existing record, only have to iterate on ranks between new and old recs
122 for (i = prevpos; i > newpos; --i)
124 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
125 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
130 // player has no ranked record yet
131 for (i = RANKINGS_CNT; i > newpos; --i)
133 float other_time = race_readTime(map, i - 1);
135 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(other_time));
136 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
141 // store new time itself
142 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
143 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
146 string race_readName(string map, float pos)
148 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
150 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
154 const float MAX_CHECKPOINTS = 255;
157 .float race_penalty_accumulator;
158 .string race_penalty_reason;
159 .float race_checkpoint; // player: next checkpoint that has to be reached
160 .entity race_lastpenalty;
164 float race_checkpoint_records[MAX_CHECKPOINTS];
165 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
166 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
167 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
168 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
170 .float race_checkpoint_record[MAX_CHECKPOINTS];
172 float race_highest_checkpoint;
173 float race_timed_checkpoint;
176 float defragcpexists;
178 float race_NextCheckpoint(float f)
180 if(f >= race_highest_checkpoint)
186 float race_PreviousCheckpoint(float f)
191 return race_highest_checkpoint;
197 // 0 = common start/finish
200 float race_CheckpointNetworkID(float f)
202 if(race_timed_checkpoint)
206 else if(f == race_timed_checkpoint)
207 return 255; // finish
212 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
217 int cp = e.race_checkpoint;
218 float recordtime = race_checkpoint_records[cp];
219 float myrecordtime = e.race_checkpoint_record[cp];
220 string recordholder = race_checkpoint_recordholders[cp];
221 if(recordholder == e.netname)
224 if(!IS_REAL_CLIENT(e))
229 WRITESPECTATABLE_MSG_ONE(msg_entity, {
230 WriteHeader(MSG_ONE, TE_CSQC_RACE);
233 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
234 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
235 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
238 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
239 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
240 WriteInt24_t(MSG_ONE, recordtime);
242 WriteInt24_t(MSG_ONE, myrecordtime);
243 WriteString(MSG_ONE, recordholder);
247 void race_send_recordtime(float msg)
249 // send the server best time
250 WriteHeader(msg, TE_CSQC_RACE);
251 WriteByte(msg, RACE_NET_SERVER_RECORD);
252 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
256 void race_send_speedaward(float msg)
258 // send the best speed of the round
259 WriteHeader(msg, TE_CSQC_RACE);
260 WriteByte(msg, RACE_NET_SPEED_AWARD);
261 WriteInt24_t(msg, floor(speedaward_speed+0.5));
262 WriteString(msg, speedaward_holder);
265 void race_send_speedaward_alltimebest(float msg)
267 // send the best speed
268 WriteHeader(msg, TE_CSQC_RACE);
269 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
270 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
271 WriteString(msg, speedaward_alltimebest_holder);
274 void race_send_rankings_cnt(float msg)
276 WriteHeader(msg, TE_CSQC_RACE);
277 WriteByte(msg, RACE_NET_RANKINGS_CNT);
278 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
282 void race_SendRankings(float pos, float prevpos, float del, float msg)
284 WriteHeader(msg, TE_CSQC_RACE);
285 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
286 WriteShort(msg, pos);
287 WriteShort(msg, prevpos);
288 WriteShort(msg, del);
289 WriteString(msg, race_readName(GetMapname(), pos));
290 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
293 void race_SendStatus(float id, entity e)
295 if(!IS_REAL_CLIENT(e))
304 WRITESPECTATABLE_MSG_ONE(msg_entity, {
305 WriteHeader(msg, TE_CSQC_RACE);
306 WriteByte(msg, RACE_NET_SERVER_STATUS);
308 WriteString(msg, e.netname);
312 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
314 // netname only used TEMPORARILY for printing
315 int newpos = race_readPos(map, t);
317 int player_prevpos = 0;
318 for(int i = 1; i <= RANKINGS_CNT; ++i)
320 if(race_readUID(map, i) == myuid)
325 string oldrec_holder;
326 if (player_prevpos && (player_prevpos < newpos || !newpos))
328 oldrec = race_readTime(GetMapname(), player_prevpos);
329 race_SendStatus(0, e); // "fail"
331 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
336 // no ranking, time worse than the worst ranked
337 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
338 race_SendStatus(0, e); // "fail"
340 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
344 // if we didn't hit a return yet, we have a new record!
346 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
350 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
354 if(uid2name(myuid) == "^1Unregistered Player")
357 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
361 oldrec = race_readTime(GetMapname(), newpos);
362 oldrec_holder = race_readName(GetMapname(), newpos);
365 race_writeTime(GetMapname(), t, myuid);
367 if (newpos == 1 && showmessage)
369 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
370 race_send_recordtime(MSG_ALL);
373 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
374 strcpy(rankings_reply, getrankings());
376 if(newpos == player_prevpos)
379 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
380 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
381 else { race_SendStatus(1, e); } // "new time"
386 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
387 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
388 else { race_SendStatus(2, e); } // "new rank"
393 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
394 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
395 else { race_SendStatus(2, e); } // "new rank"
399 void race_deleteTime(string map, float pos)
401 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
403 for(int i = pos; i <= RANKINGS_CNT; ++i)
405 string therank = ftos(i);
406 if (i == RANKINGS_CNT)
408 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
409 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
413 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
414 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
418 race_SendRankings(pos, 0, 1, MSG_ALL);
420 race_send_recordtime(MSG_ALL);
422 strcpy(rankings_reply, getrankings());
425 void race_SendTime(entity e, float cp, float t, float tvalid)
427 if(g_race_qualifying)
428 t += e.race_penalty_accumulator;
430 t = TIME_ENCODE(t); // make integer
433 if(cp == race_timed_checkpoint) // finish line
434 if (!CS(e).race_completed)
437 if(g_race_qualifying)
439 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
441 GameRules_scoring_add(e, RACE_FASTEST, t - s);
445 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
447 GameRules_scoring_add(e, RACE_FASTEST, t - s);
449 s = GameRules_scoring_add(e, RACE_TIME, 0);
450 float snew = TIME_ENCODE(time - game_starttime);
451 GameRules_scoring_add(e, RACE_TIME, snew - s);
452 float l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
454 if(autocvar_fraglimit)
455 if(l >= autocvar_fraglimit)
456 race_StartCompleting();
460 CS(e).race_completed = 1;
461 MAKE_INDEPENDENT_PLAYER(e);
462 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
468 if(g_race_qualifying)
475 recordtime = race_checkpoint_records[cp];
476 float myrecordtime = e.race_checkpoint_record[cp];
477 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
478 if(recordholder == e.netname)
483 if(cp == race_timed_checkpoint)
485 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
486 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
488 if(t < myrecordtime || myrecordtime == 0)
489 e.race_checkpoint_record[cp] = t; // resending done below
491 if(t < recordtime || recordtime == 0)
493 race_checkpoint_records[cp] = t;
494 strcpy(race_checkpoint_recordholders[cp], e.netname);
495 if(g_race_qualifying)
496 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
509 if(IS_REAL_CLIENT(e))
511 if(g_race_qualifying)
513 FOREACH_CLIENT(IS_REAL_CLIENT(it),
515 if(it == e || (IS_SPEC(it) && it.enemy == e))
518 WriteHeader(MSG_ONE, TE_CSQC_RACE);
519 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
520 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
521 WriteInt24_t(MSG_ONE, t); // time to that intermediate
522 WriteInt24_t(MSG_ONE, recordtime); // previously best time
523 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
524 WriteString(MSG_ONE, recordholder); // record holder
530 else // RACE! Not Qualifying
532 float mylaps, lother, othtime;
533 entity oth = race_checkpoint_lastplayers[cp];
536 mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
537 lother = race_checkpoint_lastlaps[cp];
538 othtime = race_checkpoint_lasttimes[cp];
541 mylaps = lother = othtime = 0;
543 if(IS_REAL_CLIENT(e))
546 WRITESPECTATABLE_MSG_ONE(msg_entity, {
547 WriteHeader(MSG_ONE, TE_CSQC_RACE);
548 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
549 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
552 WriteInt24_t(MSG_ONE, 0);
553 WriteByte(MSG_ONE, 0);
554 WriteByte(MSG_ONE, 0);
558 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
559 WriteByte(MSG_ONE, mylaps - lother);
560 WriteByte(MSG_ONE, etof(oth)); // record holder
565 race_checkpoint_lastplayers[cp] = e;
566 race_checkpoint_lasttimes[cp] = time;
567 race_checkpoint_lastlaps[cp] = mylaps;
569 if(IS_REAL_CLIENT(oth))
572 WRITESPECTATABLE_MSG_ONE(msg_entity, {
573 WriteHeader(MSG_ONE, TE_CSQC_RACE);
574 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
575 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
578 WriteInt24_t(MSG_ONE, 0);
579 WriteByte(MSG_ONE, 0);
580 WriteByte(MSG_ONE, 0);
584 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
585 WriteByte(MSG_ONE, lother - mylaps);
586 WriteByte(MSG_ONE, etof(e) - 1); // record holder
593 void race_ClearTime(entity e)
595 e.race_checkpoint = 0;
597 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
598 e.race_penalty_accumulator = 0;
599 e.race_lastpenalty = NULL;
601 if(!IS_REAL_CLIENT(e))
605 WRITESPECTATABLE_MSG_ONE(msg_entity, {
606 WriteHeader(MSG_ONE, TE_CSQC_RACE);
607 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
611 void checkpoint_passed(entity this, entity player)
613 if(IS_VEHICLE(player) && player.owner)
614 player = player.owner;
616 if(player.personal && autocvar_g_allow_checkpoints)
617 return; // practice mode!
619 if(player.classname == "porto")
621 // do not allow portalling through checkpoints
622 trace_plane_normal = normalize(-1 * player.velocity);
623 W_Porto_Fail(player, 0);
627 string oldmsg; // used twice
632 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
634 oldmsg = this.message;
636 SUB_UseTargets(this, player, player);
637 this.message = oldmsg;
640 if (!IS_PLAYER(player))
644 * Remove unauthorized equipment
646 Portal_ClearAll(player);
648 player.porto_forbidden = 2; // decreased by 1 each StartFrame
652 if(this.race_checkpoint == -2)
654 this.race_checkpoint = player.race_checkpoint;
657 int cp_amount = 0, largest_cp_id = 0;
658 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
661 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
665 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
667 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
672 largest_cp_id = it.race_checkpoint;
673 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
675 it.race_checkpoint = largest_cp_id + 1; // finish line
677 race_highest_checkpoint = largest_cp_id + 1;
678 race_timed_checkpoint = largest_cp_id + 1;
684 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
686 it.race_checkpoint = 1;
688 race_highest_checkpoint = 1;
689 race_timed_checkpoint = 1;
693 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
695 if(this.race_penalty)
697 if(player.race_lastpenalty != this)
699 player.race_lastpenalty = this;
700 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
704 if(player.race_penalty)
710 if(this.spawnflags & 2)
712 oldmsg = this.message;
714 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
715 this.message = oldmsg;
718 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
719 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
720 player.race_respawn_checkpoint = this.race_checkpoint;
721 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
722 player.race_started = 1;
724 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
726 if(!this.race_checkpoint) // start line
728 player.race_laptime = time;
729 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
730 player.race_penalty_accumulator = 0;
731 player.race_lastpenalty = NULL;
734 if(g_race_qualifying)
735 race_SendNextCheckpoint(player, 0);
737 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
740 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
743 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
745 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
751 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
757 if(this.spawnflags & 4)
758 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
762 void checkpoint_touch(entity this, entity toucher)
764 EXACTTRIGGER_TOUCH(this, toucher);
765 checkpoint_passed(this, toucher);
768 void checkpoint_use(entity this, entity actor, entity trigger)
770 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
773 checkpoint_passed(this, actor);
776 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
778 entity own = this.owner;
780 own = this.realowner; // target support
782 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
784 else if(view.race_checkpoint == own.race_checkpoint)
790 void trigger_race_checkpoint_verify(entity this)
792 static bool have_verified;
793 if (have_verified) return;
794 have_verified = true;
796 bool qual = g_race_qualifying;
798 int pl_race_checkpoint = 0;
799 int pl_race_place = 0;
802 for (int i = 0; i <= race_highest_checkpoint; ++i) {
803 pl_race_checkpoint = race_NextCheckpoint(i);
805 // race only (middle of the race)
806 g_race_qualifying = false;
808 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
809 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
814 g_race_qualifying = 1;
815 pl_race_place = race_lowest_place_spawn;
816 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
817 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
820 // race only (initial spawn)
821 g_race_qualifying = 0;
822 for (int p = 1; p <= race_highest_place_spawn; ++p) {
824 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
825 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
830 } else if (!defrag_ents) {
832 pl_race_checkpoint = race_NextCheckpoint(0);
833 g_race_qualifying = 1;
834 pl_race_place = race_lowest_place_spawn;
835 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
836 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
839 pl_race_checkpoint = race_NextCheckpoint(0);
840 g_race_qualifying = 1;
841 pl_race_place = 0; // there's only one spawn on defrag maps
843 // check if a defragcp file already exists, then read it and apply the checkpoint order
848 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
850 while ((l = fgets(fh))) {
851 len = tokenize_console(l);
853 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
856 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
857 if (argv(0) == cp.targetname) {
858 cp.race_checkpoint = stof(argv(1));
866 g_race_qualifying = qual;
868 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
870 if(it.targetname == "" || !it.targetname) // somehow this is a case...
873 FOREACH_ENTITY_STRING(target, cpt.targetname,
875 vector org = (it.absmin + it.absmax) * 0.5;
876 if(cpt.race_checkpoint == 0)
877 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
879 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
881 it.sprite.realowner = cpt;
882 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
886 if (race_timed_checkpoint) {
888 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
891 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
892 if(it.targetname == "" || !it.targetname) // somehow this is a case...
894 FOREACH_ENTITY_STRING(target, cpt.targetname, {
896 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
899 if(it.classname == "target_checkpoint") {
900 if(it.race_checkpoint == -2)
901 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
904 if (defragcpexists != -1) {
905 float largest_cp_id = 0;
906 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
907 if (cp.race_checkpoint > largest_cp_id) {
908 largest_cp_id = cp.race_checkpoint;
911 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
912 cp.race_checkpoint = largest_cp_id + 1; // finish line
914 race_highest_checkpoint = largest_cp_id + 1;
915 race_timed_checkpoint = largest_cp_id + 1;
917 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
918 cp.race_checkpoint = 255; // finish line
920 race_highest_checkpoint = 255;
921 race_timed_checkpoint = 255;
924 IL_EACH(g_racecheckpoints, it.sprite,
926 if (it.race_checkpoint == 0) {
927 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
928 } else if (it.race_checkpoint == race_timed_checkpoint) {
929 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
936 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
937 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
938 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
944 // These just make the game crash on some maps with oddly shaped triggers.
945 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
946 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
947 // (happened on item triggers too)
952 //setsize(targ, trigger.mins, trigger.maxs);
953 //setorigin(targ, trigger.origin);
961 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
963 if(g_race_qualifying)
966 if(this.race_checkpoint != 0)
968 if(spot.race_place != race_lowest_place_spawn)
973 if(this.race_checkpoint != player.race_respawn_checkpoint)
975 // try reusing the previous spawn
976 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
977 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
978 if(this.race_checkpoint == 0)
980 int pl = player.race_place;
981 if(pl > race_highest_place_spawn)
983 if(pl == 0 && !player.race_started)
984 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
985 if(spot.race_place != pl)
992 spawnfunc(trigger_race_checkpoint)
995 if(!g_race && !g_cts) { delete(this); return; }
999 this.use = checkpoint_use;
1000 if (!(this.spawnflags & 1))
1001 settouch(this, checkpoint_touch);
1003 o = (this.absmin + this.absmax) * 0.5;
1004 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
1005 waypoint_spawnforitem_force(this, trace_endpos);
1006 this.nearestwaypointtimeout = -1;
1008 if(this.message == "")
1009 this.message = "went backwards";
1010 if (this.message2 == "")
1011 this.message2 = "was pushed backwards by";
1012 if (this.race_penalty_reason == "")
1013 this.race_penalty_reason = "missing a checkpoint";
1015 this.race_checkpoint = this.cnt;
1017 if(this.race_checkpoint > race_highest_checkpoint)
1019 race_highest_checkpoint = this.race_checkpoint;
1020 if(this.spawnflags & 8)
1021 race_timed_checkpoint = this.race_checkpoint;
1023 race_timed_checkpoint = 0;
1026 if(!this.race_penalty)
1028 if(this.race_checkpoint)
1029 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
1031 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
1034 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1035 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1037 IL_PUSH(g_racecheckpoints, this);
1039 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1042 void target_checkpoint_setup(entity this)
1044 if(!g_race && !g_cts) { delete(this); return; }
1047 // if this is targeted, then it probably isn't a trigger
1048 bool is_trigger = this.targetname == "";
1053 this.use = checkpoint_use;
1054 if (is_trigger && !(this.spawnflags & 1))
1055 settouch(this, checkpoint_touch);
1057 vector org = this.origin;
1059 // bots should only pathfind to this if it is a valid touchable trigger
1062 org = (this.absmin + this.absmax) * 0.5;
1063 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1064 waypoint_spawnforitem_force(this, trace_endpos);
1065 this.nearestwaypointtimeout = -1;
1068 if(this.message == "")
1069 this.message = "went backwards";
1070 if (this.message2 == "")
1071 this.message2 = "was pushed backwards by";
1072 if (this.race_penalty_reason == "")
1073 this.race_penalty_reason = "missing a checkpoint";
1075 if(this.classname == "target_startTimer")
1076 this.race_checkpoint = 0;
1078 this.race_checkpoint = -2;
1080 race_timed_checkpoint = 1;
1082 IL_PUSH(g_race_targets, this);
1084 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1087 spawnfunc(target_checkpoint)
1089 // xonotic defrag entity
1090 target_checkpoint_setup(this);
1093 // compatibility entity names
1094 spawnfunc(target_startTimer) { target_checkpoint_setup(this); }
1095 spawnfunc(target_stopTimer) { target_checkpoint_setup(this); }
1097 void race_AbandonRaceCheck(entity p)
1099 if(race_completing && !CS(p).race_completed)
1101 CS(p).race_completed = 1;
1102 MAKE_INDEPENDENT_PLAYER(p);
1103 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1104 ClientData_Touch(p);
1108 void race_StartCompleting()
1110 race_completing = 1;
1111 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1114 void race_PreparePlayer(entity this)
1116 race_ClearTime(this);
1117 this.race_place = 0;
1118 this.race_started = 0;
1119 this.race_respawn_checkpoint = 0;
1120 this.race_respawn_spotref = NULL;
1123 void race_RetractPlayer(entity this)
1125 if(!g_race && !g_cts)
1127 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1128 race_ClearTime(this);
1129 this.race_checkpoint = this.race_respawn_checkpoint;
1132 spawnfunc(info_player_race)
1134 if(!g_race && !g_cts) { delete(this); return; }
1136 spawnfunc_info_player_deathmatch(this);
1138 if(this.race_place > race_highest_place_spawn)
1139 race_highest_place_spawn = this.race_place;
1140 if(this.race_place < race_lowest_place_spawn)
1141 race_lowest_place_spawn = this.race_place;
1144 void race_ClearRecords()
1146 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1148 race_checkpoint_records[j] = 0;
1149 strfree(race_checkpoint_recordholders[j]);
1152 FOREACH_CLIENT(true, {
1153 float p = it.race_place;
1154 race_PreparePlayer(it);
1159 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1161 if(g_race_qualifying)
1163 pl.race_penalty_accumulator += penalty;
1164 if(IS_REAL_CLIENT(pl))
1167 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1168 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1169 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1170 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1171 WriteString(MSG_ONE, reason);
1177 pl.race_penalty = time + penalty;
1178 if(IS_REAL_CLIENT(pl))
1181 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1182 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1183 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1184 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1185 WriteString(MSG_ONE, reason);
1191 void penalty_touch(entity this, entity toucher)
1193 EXACTTRIGGER_TOUCH(this, toucher);
1194 if(toucher.race_lastpenalty != this)
1196 toucher.race_lastpenalty = this;
1197 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1201 void penalty_use(entity this, entity actor, entity trigger)
1203 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1206 spawnfunc(trigger_race_penalty)
1208 // TODO: find out why this wasnt done:
1209 //if(!g_cts && !g_race) { remove(this); return; }
1213 this.use = penalty_use;
1214 if (!(this.spawnflags & 1))
1215 settouch(this, penalty_touch);
1217 if (this.race_penalty_reason == "")
1218 this.race_penalty_reason = "missing a checkpoint";
1219 if (!this.race_penalty)
1220 this.race_penalty = 5;
1223 float race_GetFractionalLapCount(entity e)
1225 // interesting metrics (idea by KrimZon) to maybe sort players in the
1226 // scoreboard, immediately updates when overtaking
1228 // requires the track to be built so you never get farther away from the
1229 // next checkpoint, though, and current Xonotic race maps are not built that
1232 // also, this code is slow and would need optimization (i.e. "next CP"
1233 // links on CP entities)
1236 l = GameRules_scoring_add(e, RACE_LAPS, 0);
1237 if(CS(e).race_completed)
1238 return l; // not fractional
1241 float bestfraction, fraction;
1243 float nextcpindex, lastcpindex;
1245 nextcpindex = max(e.race_checkpoint, 0);
1246 lastcpindex = e.race_respawn_checkpoint;
1247 lastcp = e.race_respawn_spotref;
1249 if(nextcpindex == lastcpindex)
1253 IL_EACH(g_racecheckpoints, true,
1255 if(it.race_checkpoint != lastcpindex)
1260 o0 = (it.absmin + it.absmax) * 0.5;
1261 IL_EACH(g_racecheckpoints, true,
1263 if(it.race_checkpoint != nextcpindex)
1265 o1 = (it.absmin + it.absmax) * 0.5;
1268 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1269 if(fraction < bestfraction)
1270 bestfraction = fraction;
1274 // we are at CP "nextcpindex - bestfraction"
1275 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1276 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1278 nc = race_highest_checkpoint + 1;
1279 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;