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/main.qh>
9 #include <server/mutators/_mod.qh>
10 #include <server/world.qh>
11 #include <server/weapons/common.qh>
16 #include "spawnpoints.qh"
18 #include "command/getreplies.qh"
19 #include "../common/deathtypes/all.qh"
20 #include "../common/notifications/all.qh"
21 #include <common/gamemodes/_mod.qh>
22 #include <common/gamemodes/rules.qh>
23 #include <common/net_linked.qh>
24 #include <common/state.qh>
25 #include <common/weapons/weapon/porto.qh>
26 #include "../common/mapobjects/subs.qh"
27 #include <common/mapobjects/triggers.qh>
28 #include "../lib/warpzone/util_server.qh"
29 #include "../lib/warpzone/common.qh"
30 #include <common/vehicles/sv_vehicles.qh>
31 #include "../common/mutators/mutator/waypoints/waypointsprites.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)
429 if(g_race_qualifying)
430 t += e.race_penalty_accumulator;
432 t = TIME_ENCODE(t); // make integer
435 if(cp == race_timed_checkpoint) // finish line
436 if (!CS(e).race_completed)
439 if(g_race_qualifying)
441 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
443 GameRules_scoring_add(e, RACE_FASTEST, t - s);
447 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
449 GameRules_scoring_add(e, RACE_FASTEST, t - s);
451 s = GameRules_scoring_add(e, RACE_TIME, 0);
452 snew = TIME_ENCODE(time - game_starttime);
453 GameRules_scoring_add(e, RACE_TIME, snew - s);
454 l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
456 if(autocvar_fraglimit)
457 if(l >= autocvar_fraglimit)
458 race_StartCompleting();
462 CS(e).race_completed = 1;
463 MAKE_INDEPENDENT_PLAYER(e);
464 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
470 if(g_race_qualifying)
477 recordtime = race_checkpoint_records[cp];
478 float myrecordtime = e.race_checkpoint_record[cp];
479 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
480 if(recordholder == e.netname)
485 if(cp == race_timed_checkpoint)
487 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
488 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
490 if(t < myrecordtime || myrecordtime == 0)
491 e.race_checkpoint_record[cp] = t; // resending done below
493 if(t < recordtime || recordtime == 0)
495 race_checkpoint_records[cp] = t;
496 strcpy(race_checkpoint_recordholders[cp], e.netname);
497 if(g_race_qualifying)
498 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
511 if(IS_REAL_CLIENT(e))
513 if(g_race_qualifying)
515 FOREACH_CLIENT(IS_REAL_CLIENT(it),
517 if(it == e || (IS_SPEC(it) && it.enemy == e))
520 WriteHeader(MSG_ONE, TE_CSQC_RACE);
521 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
522 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
523 WriteInt24_t(MSG_ONE, t); // time to that intermediate
524 WriteInt24_t(MSG_ONE, recordtime); // previously best time
525 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
526 WriteString(MSG_ONE, recordholder); // record holder
532 else // RACE! Not Qualifying
534 float mylaps, lother, othtime;
535 entity oth = race_checkpoint_lastplayers[cp];
538 mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
539 lother = race_checkpoint_lastlaps[cp];
540 othtime = race_checkpoint_lasttimes[cp];
543 mylaps = lother = othtime = 0;
545 if(IS_REAL_CLIENT(e))
548 WRITESPECTATABLE_MSG_ONE(msg_entity, {
549 WriteHeader(MSG_ONE, TE_CSQC_RACE);
550 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
551 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
554 WriteInt24_t(MSG_ONE, 0);
555 WriteByte(MSG_ONE, 0);
556 WriteByte(MSG_ONE, 0);
560 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
561 WriteByte(MSG_ONE, mylaps - lother);
562 WriteByte(MSG_ONE, etof(oth)); // record holder
567 race_checkpoint_lastplayers[cp] = e;
568 race_checkpoint_lasttimes[cp] = time;
569 race_checkpoint_lastlaps[cp] = mylaps;
571 if(IS_REAL_CLIENT(oth))
574 WRITESPECTATABLE_MSG_ONE(msg_entity, {
575 WriteHeader(MSG_ONE, TE_CSQC_RACE);
576 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
577 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
580 WriteInt24_t(MSG_ONE, 0);
581 WriteByte(MSG_ONE, 0);
582 WriteByte(MSG_ONE, 0);
586 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
587 WriteByte(MSG_ONE, lother - mylaps);
588 WriteByte(MSG_ONE, etof(e) - 1); // record holder
595 void race_ClearTime(entity e)
597 e.race_checkpoint = 0;
599 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
600 e.race_penalty_accumulator = 0;
601 e.race_lastpenalty = NULL;
603 if(!IS_REAL_CLIENT(e))
607 WRITESPECTATABLE_MSG_ONE(msg_entity, {
608 WriteHeader(MSG_ONE, TE_CSQC_RACE);
609 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
613 void checkpoint_passed(entity this, entity player)
615 if(IS_VEHICLE(player) && player.owner)
616 player = player.owner;
618 if(player.personal && autocvar_g_allow_checkpoints)
619 return; // practice mode!
621 if(player.classname == "porto")
623 // do not allow portalling through checkpoints
624 trace_plane_normal = normalize(-1 * player.velocity);
625 W_Porto_Fail(player, 0);
629 string oldmsg; // used twice
634 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
636 oldmsg = this.message;
638 SUB_UseTargets(this, player, player);
639 this.message = oldmsg;
642 if (!IS_PLAYER(player))
646 * Remove unauthorized equipment
648 Portal_ClearAll(player);
650 player.porto_forbidden = 2; // decreased by 1 each StartFrame
654 if(this.race_checkpoint == -2)
656 this.race_checkpoint = player.race_checkpoint;
659 int cp_amount = 0, largest_cp_id = 0;
660 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
663 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
667 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
669 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
674 largest_cp_id = it.race_checkpoint;
675 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
677 it.race_checkpoint = largest_cp_id + 1; // finish line
679 race_highest_checkpoint = largest_cp_id + 1;
680 race_timed_checkpoint = largest_cp_id + 1;
686 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
688 it.race_checkpoint = 1;
690 race_highest_checkpoint = 1;
691 race_timed_checkpoint = 1;
695 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
697 if(this.race_penalty)
699 if(player.race_lastpenalty != this)
701 player.race_lastpenalty = this;
702 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
706 if(player.race_penalty)
712 if(this.spawnflags & 2)
714 oldmsg = this.message;
716 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
717 this.message = oldmsg;
720 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
721 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
722 player.race_respawn_checkpoint = this.race_checkpoint;
723 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
724 player.race_started = 1;
726 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
728 if(!this.race_checkpoint) // start line
730 player.race_laptime = time;
731 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
732 player.race_penalty_accumulator = 0;
733 player.race_lastpenalty = NULL;
736 if(g_race_qualifying)
737 race_SendNextCheckpoint(player, 0);
739 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
742 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
745 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
747 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
753 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
759 if(this.spawnflags & 4)
760 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
764 void checkpoint_touch(entity this, entity toucher)
766 EXACTTRIGGER_TOUCH(this, toucher);
767 checkpoint_passed(this, toucher);
770 void checkpoint_use(entity this, entity actor, entity trigger)
772 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
775 checkpoint_passed(this, actor);
778 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
780 entity own = this.owner;
782 own = this.realowner; // target support
784 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
786 else if(view.race_checkpoint == own.race_checkpoint)
792 void trigger_race_checkpoint_verify(entity this)
794 static bool have_verified;
795 if (have_verified) return;
796 have_verified = true;
798 bool qual = g_race_qualifying;
800 int pl_race_checkpoint = 0;
801 int pl_race_place = 0;
804 for (int i = 0; i <= race_highest_checkpoint; ++i) {
805 pl_race_checkpoint = race_NextCheckpoint(i);
807 // race only (middle of the race)
808 g_race_qualifying = false;
810 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
811 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
816 g_race_qualifying = 1;
817 pl_race_place = race_lowest_place_spawn;
818 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
819 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
822 // race only (initial spawn)
823 g_race_qualifying = 0;
824 for (int p = 1; p <= race_highest_place_spawn; ++p) {
826 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
827 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
832 } else if (!defrag_ents) {
834 pl_race_checkpoint = race_NextCheckpoint(0);
835 g_race_qualifying = 1;
836 pl_race_place = race_lowest_place_spawn;
837 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
838 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
841 pl_race_checkpoint = race_NextCheckpoint(0);
842 g_race_qualifying = 1;
843 pl_race_place = 0; // there's only one spawn on defrag maps
845 // check if a defragcp file already exists, then read it and apply the checkpoint order
850 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
852 while ((l = fgets(fh))) {
853 len = tokenize_console(l);
855 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
858 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
859 if (argv(0) == cp.targetname) {
860 cp.race_checkpoint = stof(argv(1));
868 g_race_qualifying = qual;
870 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
872 if(it.targetname == "" || !it.targetname) // somehow this is a case...
875 FOREACH_ENTITY_STRING(target, cpt.targetname,
877 vector org = (it.absmin + it.absmax) * 0.5;
878 if(cpt.race_checkpoint == 0)
879 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
881 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
883 it.sprite.realowner = cpt;
884 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
888 if (race_timed_checkpoint) {
890 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
893 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
894 if(it.targetname == "" || !it.targetname) // somehow this is a case...
896 FOREACH_ENTITY_STRING(target, cpt.targetname, {
898 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
901 if(it.classname == "target_checkpoint") {
902 if(it.race_checkpoint == -2)
903 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
906 if (defragcpexists != -1) {
907 float largest_cp_id = 0;
908 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
909 if (cp.race_checkpoint > largest_cp_id) {
910 largest_cp_id = cp.race_checkpoint;
913 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
914 cp.race_checkpoint = largest_cp_id + 1; // finish line
916 race_highest_checkpoint = largest_cp_id + 1;
917 race_timed_checkpoint = largest_cp_id + 1;
919 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
920 cp.race_checkpoint = 255; // finish line
922 race_highest_checkpoint = 255;
923 race_timed_checkpoint = 255;
926 IL_EACH(g_racecheckpoints, it.sprite,
928 if (it.race_checkpoint == 0) {
929 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
930 } else if (it.race_checkpoint == race_timed_checkpoint) {
931 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
938 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
939 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
940 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
946 // These just make the game crash on some maps with oddly shaped triggers.
947 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
948 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
949 // (happened on item triggers too)
954 //setsize(targ, trigger.mins, trigger.maxs);
955 //setorigin(targ, trigger.origin);
963 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
965 if(g_race_qualifying)
968 if(this.race_checkpoint != 0)
970 if(spot.race_place != race_lowest_place_spawn)
975 if(this.race_checkpoint != player.race_respawn_checkpoint)
977 // try reusing the previous spawn
978 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
979 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
980 if(this.race_checkpoint == 0)
982 int pl = player.race_place;
983 if(pl > race_highest_place_spawn)
985 if(pl == 0 && !player.race_started)
986 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
987 if(spot.race_place != pl)
994 spawnfunc(trigger_race_checkpoint)
997 if(!g_race && !g_cts) { delete(this); return; }
1001 this.use = checkpoint_use;
1002 if (!(this.spawnflags & 1))
1003 settouch(this, checkpoint_touch);
1005 o = (this.absmin + this.absmax) * 0.5;
1006 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
1007 waypoint_spawnforitem_force(this, trace_endpos);
1008 this.nearestwaypointtimeout = -1;
1010 if(this.message == "")
1011 this.message = "went backwards";
1012 if (this.message2 == "")
1013 this.message2 = "was pushed backwards by";
1014 if (this.race_penalty_reason == "")
1015 this.race_penalty_reason = "missing a checkpoint";
1017 this.race_checkpoint = this.cnt;
1019 if(this.race_checkpoint > race_highest_checkpoint)
1021 race_highest_checkpoint = this.race_checkpoint;
1022 if(this.spawnflags & 8)
1023 race_timed_checkpoint = this.race_checkpoint;
1025 race_timed_checkpoint = 0;
1028 if(!this.race_penalty)
1030 if(this.race_checkpoint)
1031 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
1033 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
1036 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1037 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1039 IL_PUSH(g_racecheckpoints, this);
1041 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1044 spawnfunc(target_checkpoint) // defrag entity
1046 if(!g_race && !g_cts) { delete(this); return; }
1049 // if this is targeted, then it probably isn't a trigger
1050 bool is_trigger = this.targetname == "";
1055 this.use = checkpoint_use;
1056 if (is_trigger && !(this.spawnflags & 1))
1057 settouch(this, checkpoint_touch);
1059 vector org = this.origin;
1061 // bots should only pathfind to this if it is a valid touchable trigger
1064 org = (this.absmin + this.absmax) * 0.5;
1065 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1066 waypoint_spawnforitem_force(this, trace_endpos);
1067 this.nearestwaypointtimeout = -1;
1070 if(this.message == "")
1071 this.message = "went backwards";
1072 if (this.message2 == "")
1073 this.message2 = "was pushed backwards by";
1074 if (this.race_penalty_reason == "")
1075 this.race_penalty_reason = "missing a checkpoint";
1077 if(this.classname == "target_startTimer")
1078 this.race_checkpoint = 0;
1080 this.race_checkpoint = -2;
1082 race_timed_checkpoint = 1;
1084 IL_PUSH(g_race_targets, this);
1086 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1089 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1090 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1092 void race_AbandonRaceCheck(entity p)
1094 if(race_completing && !CS(p).race_completed)
1096 CS(p).race_completed = 1;
1097 MAKE_INDEPENDENT_PLAYER(p);
1098 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1099 ClientData_Touch(p);
1103 void race_StartCompleting()
1105 race_completing = 1;
1106 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1109 void race_PreparePlayer(entity this)
1111 race_ClearTime(this);
1112 this.race_place = 0;
1113 this.race_started = 0;
1114 this.race_respawn_checkpoint = 0;
1115 this.race_respawn_spotref = NULL;
1118 void race_RetractPlayer(entity this)
1120 if(!g_race && !g_cts)
1122 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1123 race_ClearTime(this);
1124 this.race_checkpoint = this.race_respawn_checkpoint;
1127 spawnfunc(info_player_race)
1129 if(!g_race && !g_cts) { delete(this); return; }
1131 spawnfunc_info_player_deathmatch(this);
1133 if(this.race_place > race_highest_place_spawn)
1134 race_highest_place_spawn = this.race_place;
1135 if(this.race_place < race_lowest_place_spawn)
1136 race_lowest_place_spawn = this.race_place;
1139 void race_ClearRecords()
1141 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1143 race_checkpoint_records[j] = 0;
1144 strfree(race_checkpoint_recordholders[j]);
1147 FOREACH_CLIENT(true, {
1148 float p = it.race_place;
1149 race_PreparePlayer(it);
1154 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1156 if(g_race_qualifying)
1158 pl.race_penalty_accumulator += penalty;
1159 if(IS_REAL_CLIENT(pl))
1162 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1163 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1164 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1165 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1166 WriteString(MSG_ONE, reason);
1172 pl.race_penalty = time + penalty;
1173 if(IS_REAL_CLIENT(pl))
1176 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1177 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1178 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1179 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1180 WriteString(MSG_ONE, reason);
1186 void penalty_touch(entity this, entity toucher)
1188 EXACTTRIGGER_TOUCH(this, toucher);
1189 if(toucher.race_lastpenalty != this)
1191 toucher.race_lastpenalty = this;
1192 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1196 void penalty_use(entity this, entity actor, entity trigger)
1198 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1201 spawnfunc(trigger_race_penalty)
1203 // TODO: find out why this wasnt done:
1204 //if(!g_cts && !g_race) { remove(this); return; }
1208 this.use = penalty_use;
1209 if (!(this.spawnflags & 1))
1210 settouch(this, penalty_touch);
1212 if (this.race_penalty_reason == "")
1213 this.race_penalty_reason = "missing a checkpoint";
1214 if (!this.race_penalty)
1215 this.race_penalty = 5;
1218 float race_GetFractionalLapCount(entity e)
1220 // interesting metrics (idea by KrimZon) to maybe sort players in the
1221 // scoreboard, immediately updates when overtaking
1223 // requires the track to be built so you never get farther away from the
1224 // next checkpoint, though, and current Xonotic race maps are not built that
1227 // also, this code is slow and would need optimization (i.e. "next CP"
1228 // links on CP entities)
1231 l = GameRules_scoring_add(e, RACE_LAPS, 0);
1232 if(CS(e).race_completed)
1233 return l; // not fractional
1236 float bestfraction, fraction;
1238 float nextcpindex, lastcpindex;
1240 nextcpindex = max(e.race_checkpoint, 0);
1241 lastcpindex = e.race_respawn_checkpoint;
1242 lastcp = e.race_respawn_spotref;
1244 if(nextcpindex == lastcpindex)
1248 IL_EACH(g_racecheckpoints, true,
1250 if(it.race_checkpoint != lastcpindex)
1255 o0 = (it.absmin + it.absmax) * 0.5;
1256 IL_EACH(g_racecheckpoints, true,
1258 if(it.race_checkpoint != nextcpindex)
1260 o1 = (it.absmin + it.absmax) * 0.5;
1263 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1264 if(fraction < bestfraction)
1265 bestfraction = fraction;
1269 // we are at CP "nextcpindex - bestfraction"
1270 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1271 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1273 nc = race_highest_checkpoint + 1;
1274 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;