3 #include <server/defs.qh>
4 #include <server/g_damage.qh>
5 #include <server/g_world.qh>
6 #include <server/miscfunctions.qh>
10 #include "spawnpoints.qh"
12 #include "command/getreplies.qh"
13 #include "../common/deathtypes/all.qh"
14 #include "../common/notifications/all.qh"
15 #include <common/gamemodes/_mod.qh>
16 #include <common/gamemodes/rules.qh>
17 #include <common/net_linked.qh>
18 #include <common/state.qh>
19 #include <common/weapons/weapon/porto.qh>
20 #include "../common/mapobjects/subs.qh"
21 #include <common/mapobjects/triggers.qh>
22 #include "../lib/warpzone/util_server.qh"
23 #include "../lib/warpzone/common.qh"
24 #include <common/vehicles/sv_vehicles.qh>
25 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
27 IntrusiveList g_race_targets;
28 IntrusiveList g_racecheckpoints;
31 g_race_targets = IL_NEW();
32 g_racecheckpoints = IL_NEW();
35 void race_InitSpectator()
38 if(msg_entity.enemy.race_laptime)
39 race_SendNextCheckpoint(msg_entity.enemy, 1);
42 float race_readTime(string map, float pos)
44 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
46 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
49 string race_readUID(string map, float pos)
51 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
53 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
56 float race_readPos(string map, float t)
58 for(int i = 1; i <= RANKINGS_CNT; ++i)
60 int mytime = race_readTime(map, i);
61 if(!mytime || mytime > t)
65 return 0; // pos is zero if unranked
68 void race_writeTime(string map, float t, string myuid)
70 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
73 newpos = race_readPos(map, t);
76 for(i = 1; i <= RANKINGS_CNT; ++i)
78 if(race_readUID(map, i) == myuid)
83 // player improved his existing record, only have to iterate on ranks between new and old recs
84 for (i = prevpos; i > newpos; --i)
86 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
87 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
92 // player has no ranked record yet
93 for (i = RANKINGS_CNT; i > newpos; --i)
95 float other_time = race_readTime(map, i - 1);
97 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(other_time));
98 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
103 // store new time itself
104 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
105 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
108 string race_readName(string map, float pos)
110 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
112 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
116 const float MAX_CHECKPOINTS = 255;
119 .float race_penalty_accumulator;
120 .string race_penalty_reason;
121 .float race_checkpoint; // player: next checkpoint that has to be reached
122 .entity race_lastpenalty;
126 float race_checkpoint_records[MAX_CHECKPOINTS];
127 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
128 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
129 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
130 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
132 .float race_checkpoint_record[MAX_CHECKPOINTS];
134 float race_highest_checkpoint;
135 float race_timed_checkpoint;
138 float defragcpexists;
140 float race_NextCheckpoint(float f)
142 if(f >= race_highest_checkpoint)
148 float race_PreviousCheckpoint(float f)
153 return race_highest_checkpoint;
159 // 0 = common start/finish
162 float race_CheckpointNetworkID(float f)
164 if(race_timed_checkpoint)
168 else if(f == race_timed_checkpoint)
169 return 255; // finish
174 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
179 int cp = e.race_checkpoint;
180 float recordtime = race_checkpoint_records[cp];
181 float myrecordtime = e.race_checkpoint_record[cp];
182 string recordholder = race_checkpoint_recordholders[cp];
183 if(recordholder == e.netname)
186 if(!IS_REAL_CLIENT(e))
191 WRITESPECTATABLE_MSG_ONE(msg_entity, {
192 WriteHeader(MSG_ONE, TE_CSQC_RACE);
195 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
196 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
197 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
200 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
201 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
202 WriteInt24_t(MSG_ONE, recordtime);
204 WriteInt24_t(MSG_ONE, myrecordtime);
205 WriteString(MSG_ONE, recordholder);
209 void race_send_recordtime(float msg)
211 // send the server best time
212 WriteHeader(msg, TE_CSQC_RACE);
213 WriteByte(msg, RACE_NET_SERVER_RECORD);
214 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
218 void race_send_speedaward(float msg)
220 // send the best speed of the round
221 WriteHeader(msg, TE_CSQC_RACE);
222 WriteByte(msg, RACE_NET_SPEED_AWARD);
223 WriteInt24_t(msg, floor(speedaward_speed+0.5));
224 WriteString(msg, speedaward_holder);
227 void race_send_speedaward_alltimebest(float msg)
229 // send the best speed
230 WriteHeader(msg, TE_CSQC_RACE);
231 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
232 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
233 WriteString(msg, speedaward_alltimebest_holder);
236 void race_send_rankings_cnt(float msg)
238 WriteHeader(msg, TE_CSQC_RACE);
239 WriteByte(msg, RACE_NET_RANKINGS_CNT);
240 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
244 void race_SendRankings(float pos, float prevpos, float del, float msg)
246 WriteHeader(msg, TE_CSQC_RACE);
247 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
248 WriteShort(msg, pos);
249 WriteShort(msg, prevpos);
250 WriteShort(msg, del);
251 WriteString(msg, race_readName(GetMapname(), pos));
252 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
255 void race_SendStatus(float id, entity e)
257 if(!IS_REAL_CLIENT(e))
266 WRITESPECTATABLE_MSG_ONE(msg_entity, {
267 WriteHeader(msg, TE_CSQC_RACE);
268 WriteByte(msg, RACE_NET_SERVER_STATUS);
270 WriteString(msg, e.netname);
274 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
276 // netname only used TEMPORARILY for printing
277 int newpos = race_readPos(map, t);
279 int player_prevpos = 0;
280 for(int i = 1; i <= RANKINGS_CNT; ++i)
282 if(race_readUID(map, i) == myuid)
287 string oldrec_holder;
288 if (player_prevpos && (player_prevpos < newpos || !newpos))
290 oldrec = race_readTime(GetMapname(), player_prevpos);
291 race_SendStatus(0, e); // "fail"
293 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
298 // no ranking, time worse than the worst ranked
299 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
300 race_SendStatus(0, e); // "fail"
302 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
306 // if we didn't hit a return yet, we have a new record!
308 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
312 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
316 if(uid2name(myuid) == "^1Unregistered Player")
319 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
323 oldrec = race_readTime(GetMapname(), newpos);
324 oldrec_holder = race_readName(GetMapname(), newpos);
327 race_writeTime(GetMapname(), t, myuid);
329 if (newpos == 1 && showmessage)
331 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
332 race_send_recordtime(MSG_ALL);
335 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
336 strcpy(rankings_reply, getrankings());
338 if(newpos == player_prevpos)
341 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
342 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
343 else { race_SendStatus(1, e); } // "new time"
348 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
349 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
350 else { race_SendStatus(2, e); } // "new rank"
355 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
356 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
357 else { race_SendStatus(2, e); } // "new rank"
361 void race_deleteTime(string map, float pos)
363 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
365 for(int i = pos; i <= RANKINGS_CNT; ++i)
367 string therank = ftos(i);
368 if (i == RANKINGS_CNT)
370 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
371 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
375 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
376 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
380 race_SendRankings(pos, 0, 1, MSG_ALL);
382 race_send_recordtime(MSG_ALL);
384 strcpy(rankings_reply, getrankings());
387 void race_SendTime(entity e, float cp, float t, float tvalid)
391 if(g_race_qualifying)
392 t += e.race_penalty_accumulator;
394 t = TIME_ENCODE(t); // make integer
397 if(cp == race_timed_checkpoint) // finish line
398 if (!CS(e).race_completed)
401 if(g_race_qualifying)
403 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
405 GameRules_scoring_add(e, RACE_FASTEST, t - s);
409 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
411 GameRules_scoring_add(e, RACE_FASTEST, t - s);
413 s = GameRules_scoring_add(e, RACE_TIME, 0);
414 snew = TIME_ENCODE(time - game_starttime);
415 GameRules_scoring_add(e, RACE_TIME, snew - s);
416 l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
418 if(autocvar_fraglimit)
419 if(l >= autocvar_fraglimit)
420 race_StartCompleting();
424 CS(e).race_completed = 1;
425 MAKE_INDEPENDENT_PLAYER(e);
426 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
432 if(g_race_qualifying)
439 recordtime = race_checkpoint_records[cp];
440 float myrecordtime = e.race_checkpoint_record[cp];
441 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
442 if(recordholder == e.netname)
447 if(cp == race_timed_checkpoint)
449 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
450 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
452 if(t < myrecordtime || myrecordtime == 0)
453 e.race_checkpoint_record[cp] = t; // resending done below
455 if(t < recordtime || recordtime == 0)
457 race_checkpoint_records[cp] = t;
458 strcpy(race_checkpoint_recordholders[cp], e.netname);
459 if(g_race_qualifying)
460 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
473 if(IS_REAL_CLIENT(e))
475 if(g_race_qualifying)
477 FOREACH_CLIENT(IS_REAL_CLIENT(it),
479 if(it == e || (IS_SPEC(it) && it.enemy == e))
482 WriteHeader(MSG_ONE, TE_CSQC_RACE);
483 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
484 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
485 WriteInt24_t(MSG_ONE, t); // time to that intermediate
486 WriteInt24_t(MSG_ONE, recordtime); // previously best time
487 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
488 WriteString(MSG_ONE, recordholder); // record holder
494 else // RACE! Not Qualifying
496 float mylaps, lother, othtime;
497 entity oth = race_checkpoint_lastplayers[cp];
500 mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
501 lother = race_checkpoint_lastlaps[cp];
502 othtime = race_checkpoint_lasttimes[cp];
505 mylaps = lother = othtime = 0;
507 if(IS_REAL_CLIENT(e))
510 WRITESPECTATABLE_MSG_ONE(msg_entity, {
511 WriteHeader(MSG_ONE, TE_CSQC_RACE);
512 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
513 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
516 WriteInt24_t(MSG_ONE, 0);
517 WriteByte(MSG_ONE, 0);
518 WriteByte(MSG_ONE, 0);
522 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
523 WriteByte(MSG_ONE, mylaps - lother);
524 WriteByte(MSG_ONE, etof(oth)); // record holder
529 race_checkpoint_lastplayers[cp] = e;
530 race_checkpoint_lasttimes[cp] = time;
531 race_checkpoint_lastlaps[cp] = mylaps;
533 if(IS_REAL_CLIENT(oth))
536 WRITESPECTATABLE_MSG_ONE(msg_entity, {
537 WriteHeader(MSG_ONE, TE_CSQC_RACE);
538 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
539 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
542 WriteInt24_t(MSG_ONE, 0);
543 WriteByte(MSG_ONE, 0);
544 WriteByte(MSG_ONE, 0);
548 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
549 WriteByte(MSG_ONE, lother - mylaps);
550 WriteByte(MSG_ONE, etof(e) - 1); // record holder
557 void race_ClearTime(entity e)
559 e.race_checkpoint = 0;
561 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
562 e.race_penalty_accumulator = 0;
563 e.race_lastpenalty = NULL;
565 if(!IS_REAL_CLIENT(e))
569 WRITESPECTATABLE_MSG_ONE(msg_entity, {
570 WriteHeader(MSG_ONE, TE_CSQC_RACE);
571 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
575 void checkpoint_passed(entity this, entity player)
577 if(IS_VEHICLE(player) && player.owner)
578 player = player.owner;
580 if(player.personal && autocvar_g_allow_checkpoints)
581 return; // practice mode!
583 if(player.classname == "porto")
585 // do not allow portalling through checkpoints
586 trace_plane_normal = normalize(-1 * player.velocity);
587 W_Porto_Fail(player, 0);
591 string oldmsg; // used twice
596 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
598 oldmsg = this.message;
600 SUB_UseTargets(this, player, player);
601 this.message = oldmsg;
604 if (!IS_PLAYER(player))
608 * Remove unauthorized equipment
610 Portal_ClearAll(player);
612 player.porto_forbidden = 2; // decreased by 1 each StartFrame
616 if(this.race_checkpoint == -2)
618 this.race_checkpoint = player.race_checkpoint;
621 int cp_amount = 0, largest_cp_id = 0;
622 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
625 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
629 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
631 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
636 largest_cp_id = it.race_checkpoint;
637 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
639 it.race_checkpoint = largest_cp_id + 1; // finish line
641 race_highest_checkpoint = largest_cp_id + 1;
642 race_timed_checkpoint = largest_cp_id + 1;
648 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
650 it.race_checkpoint = 1;
652 race_highest_checkpoint = 1;
653 race_timed_checkpoint = 1;
657 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
659 if(this.race_penalty)
661 if(player.race_lastpenalty != this)
663 player.race_lastpenalty = this;
664 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
668 if(player.race_penalty)
674 if(this.spawnflags & 2)
676 oldmsg = this.message;
678 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
679 this.message = oldmsg;
682 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
683 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
684 player.race_respawn_checkpoint = this.race_checkpoint;
685 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
686 player.race_started = 1;
688 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
690 if(!this.race_checkpoint) // start line
692 player.race_laptime = time;
693 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
694 player.race_penalty_accumulator = 0;
695 player.race_lastpenalty = NULL;
698 if(g_race_qualifying)
699 race_SendNextCheckpoint(player, 0);
701 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
704 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
707 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
709 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
715 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
721 if(this.spawnflags & 4)
722 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
726 void checkpoint_touch(entity this, entity toucher)
728 EXACTTRIGGER_TOUCH(this, toucher);
729 checkpoint_passed(this, toucher);
732 void checkpoint_use(entity this, entity actor, entity trigger)
734 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
737 checkpoint_passed(this, actor);
740 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
742 entity own = this.owner;
744 own = this.realowner; // target support
746 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
748 else if(view.race_checkpoint == own.race_checkpoint)
754 void trigger_race_checkpoint_verify(entity this)
756 static bool have_verified;
757 if (have_verified) return;
758 have_verified = true;
760 bool qual = g_race_qualifying;
762 int pl_race_checkpoint = 0;
763 int pl_race_place = 0;
766 for (int i = 0; i <= race_highest_checkpoint; ++i) {
767 pl_race_checkpoint = race_NextCheckpoint(i);
769 // race only (middle of the race)
770 g_race_qualifying = false;
772 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
773 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
778 g_race_qualifying = 1;
779 pl_race_place = race_lowest_place_spawn;
780 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
781 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
784 // race only (initial spawn)
785 g_race_qualifying = 0;
786 for (int p = 1; p <= race_highest_place_spawn; ++p) {
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 initially spawning in race) - bailing out"));
794 } else if (!defrag_ents) {
796 pl_race_checkpoint = race_NextCheckpoint(0);
797 g_race_qualifying = 1;
798 pl_race_place = race_lowest_place_spawn;
799 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
800 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
803 pl_race_checkpoint = race_NextCheckpoint(0);
804 g_race_qualifying = 1;
805 pl_race_place = 0; // there's only one spawn on defrag maps
807 // check if a defragcp file already exists, then read it and apply the checkpoint order
812 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
814 while ((l = fgets(fh))) {
815 len = tokenize_console(l);
817 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
820 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
821 if (argv(0) == cp.targetname) {
822 cp.race_checkpoint = stof(argv(1));
830 g_race_qualifying = qual;
832 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
834 if(it.targetname == "" || !it.targetname) // somehow this is a case...
837 FOREACH_ENTITY_STRING(target, cpt.targetname,
839 vector org = (it.absmin + it.absmax) * 0.5;
840 if(cpt.race_checkpoint == 0)
841 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
843 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
845 it.sprite.realowner = cpt;
846 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
850 if (race_timed_checkpoint) {
852 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
855 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
856 if(it.targetname == "" || !it.targetname) // somehow this is a case...
858 FOREACH_ENTITY_STRING(target, cpt.targetname, {
860 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
863 if(it.classname == "target_checkpoint") {
864 if(it.race_checkpoint == -2)
865 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
868 if (defragcpexists != -1) {
869 float largest_cp_id = 0;
870 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
871 if (cp.race_checkpoint > largest_cp_id) {
872 largest_cp_id = cp.race_checkpoint;
875 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
876 cp.race_checkpoint = largest_cp_id + 1; // finish line
878 race_highest_checkpoint = largest_cp_id + 1;
879 race_timed_checkpoint = largest_cp_id + 1;
881 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
882 cp.race_checkpoint = 255; // finish line
884 race_highest_checkpoint = 255;
885 race_timed_checkpoint = 255;
888 IL_EACH(g_racecheckpoints, it.sprite,
890 if (it.race_checkpoint == 0) {
891 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
892 } else if (it.race_checkpoint == race_timed_checkpoint) {
893 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
900 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
901 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
902 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
908 // These just make the game crash on some maps with oddly shaped triggers.
909 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
910 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
911 // (happened on item triggers too)
916 //setsize(targ, trigger.mins, trigger.maxs);
917 //setorigin(targ, trigger.origin);
925 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
927 if(g_race_qualifying)
930 if(this.race_checkpoint != 0)
932 if(spot.race_place != race_lowest_place_spawn)
937 if(this.race_checkpoint != player.race_respawn_checkpoint)
939 // try reusing the previous spawn
940 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
941 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
942 if(this.race_checkpoint == 0)
944 int pl = player.race_place;
945 if(pl > race_highest_place_spawn)
947 if(pl == 0 && !player.race_started)
948 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
949 if(spot.race_place != pl)
956 spawnfunc(trigger_race_checkpoint)
959 if(!g_race && !g_cts) { delete(this); return; }
963 this.use = checkpoint_use;
964 if (!(this.spawnflags & 1))
965 settouch(this, checkpoint_touch);
967 o = (this.absmin + this.absmax) * 0.5;
968 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
969 waypoint_spawnforitem_force(this, trace_endpos);
970 this.nearestwaypointtimeout = -1;
972 if(this.message == "")
973 this.message = "went backwards";
974 if (this.message2 == "")
975 this.message2 = "was pushed backwards by";
976 if (this.race_penalty_reason == "")
977 this.race_penalty_reason = "missing a checkpoint";
979 this.race_checkpoint = this.cnt;
981 if(this.race_checkpoint > race_highest_checkpoint)
983 race_highest_checkpoint = this.race_checkpoint;
984 if(this.spawnflags & 8)
985 race_timed_checkpoint = this.race_checkpoint;
987 race_timed_checkpoint = 0;
990 if(!this.race_penalty)
992 if(this.race_checkpoint)
993 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
995 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
998 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
999 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1001 IL_PUSH(g_racecheckpoints, this);
1003 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1006 spawnfunc(target_checkpoint) // defrag entity
1008 if(!g_race && !g_cts) { delete(this); return; }
1011 // if this is targeted, then it probably isn't a trigger
1012 bool is_trigger = this.targetname == "";
1017 this.use = checkpoint_use;
1018 if (is_trigger && !(this.spawnflags & 1))
1019 settouch(this, checkpoint_touch);
1021 vector org = this.origin;
1023 // bots should only pathfind to this if it is a valid touchable trigger
1026 org = (this.absmin + this.absmax) * 0.5;
1027 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1028 waypoint_spawnforitem_force(this, trace_endpos);
1029 this.nearestwaypointtimeout = -1;
1032 if(this.message == "")
1033 this.message = "went backwards";
1034 if (this.message2 == "")
1035 this.message2 = "was pushed backwards by";
1036 if (this.race_penalty_reason == "")
1037 this.race_penalty_reason = "missing a checkpoint";
1039 if(this.classname == "target_startTimer")
1040 this.race_checkpoint = 0;
1042 this.race_checkpoint = -2;
1044 race_timed_checkpoint = 1;
1046 IL_PUSH(g_race_targets, this);
1048 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1051 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1052 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1054 void race_AbandonRaceCheck(entity p)
1056 if(race_completing && !CS(p).race_completed)
1058 CS(p).race_completed = 1;
1059 MAKE_INDEPENDENT_PLAYER(p);
1060 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1061 ClientData_Touch(p);
1065 void race_StartCompleting()
1067 race_completing = 1;
1068 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1071 void race_PreparePlayer(entity this)
1073 race_ClearTime(this);
1074 this.race_place = 0;
1075 this.race_started = 0;
1076 this.race_respawn_checkpoint = 0;
1077 this.race_respawn_spotref = NULL;
1080 void race_RetractPlayer(entity this)
1082 if(!g_race && !g_cts)
1084 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1085 race_ClearTime(this);
1086 this.race_checkpoint = this.race_respawn_checkpoint;
1089 spawnfunc(info_player_race)
1091 if(!g_race && !g_cts) { delete(this); return; }
1093 spawnfunc_info_player_deathmatch(this);
1095 if(this.race_place > race_highest_place_spawn)
1096 race_highest_place_spawn = this.race_place;
1097 if(this.race_place < race_lowest_place_spawn)
1098 race_lowest_place_spawn = this.race_place;
1101 void race_ClearRecords()
1103 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1105 race_checkpoint_records[j] = 0;
1106 strfree(race_checkpoint_recordholders[j]);
1109 FOREACH_CLIENT(true, {
1110 float p = it.race_place;
1111 race_PreparePlayer(it);
1116 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1118 if(g_race_qualifying)
1120 pl.race_penalty_accumulator += penalty;
1121 if(IS_REAL_CLIENT(pl))
1124 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1125 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1126 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1127 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1128 WriteString(MSG_ONE, reason);
1134 pl.race_penalty = time + penalty;
1135 if(IS_REAL_CLIENT(pl))
1138 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1139 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1140 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1141 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1142 WriteString(MSG_ONE, reason);
1148 void penalty_touch(entity this, entity toucher)
1150 EXACTTRIGGER_TOUCH(this, toucher);
1151 if(toucher.race_lastpenalty != this)
1153 toucher.race_lastpenalty = this;
1154 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1158 void penalty_use(entity this, entity actor, entity trigger)
1160 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1163 spawnfunc(trigger_race_penalty)
1165 // TODO: find out why this wasnt done:
1166 //if(!g_cts && !g_race) { remove(this); return; }
1170 this.use = penalty_use;
1171 if (!(this.spawnflags & 1))
1172 settouch(this, penalty_touch);
1174 if (this.race_penalty_reason == "")
1175 this.race_penalty_reason = "missing a checkpoint";
1176 if (!this.race_penalty)
1177 this.race_penalty = 5;
1180 float race_GetFractionalLapCount(entity e)
1182 // interesting metrics (idea by KrimZon) to maybe sort players in the
1183 // scoreboard, immediately updates when overtaking
1185 // requires the track to be built so you never get farther away from the
1186 // next checkpoint, though, and current Xonotic race maps are not built that
1189 // also, this code is slow and would need optimization (i.e. "next CP"
1190 // links on CP entities)
1193 l = GameRules_scoring_add(e, RACE_LAPS, 0);
1194 if(CS(e).race_completed)
1195 return l; // not fractional
1198 float bestfraction, fraction;
1200 float nextcpindex, lastcpindex;
1202 nextcpindex = max(e.race_checkpoint, 0);
1203 lastcpindex = e.race_respawn_checkpoint;
1204 lastcp = e.race_respawn_spotref;
1206 if(nextcpindex == lastcpindex)
1210 IL_EACH(g_racecheckpoints, true,
1212 if(it.race_checkpoint != lastcpindex)
1217 o0 = (it.absmin + it.absmax) * 0.5;
1218 IL_EACH(g_racecheckpoints, true,
1220 if(it.race_checkpoint != nextcpindex)
1222 o1 = (it.absmin + it.absmax) * 0.5;
1225 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1226 if(fraction < bestfraction)
1227 bestfraction = fraction;
1231 // we are at CP "nextcpindex - bestfraction"
1232 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1233 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1235 nc = race_highest_checkpoint + 1;
1236 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;