3 #include <common/weapons/_all.qh>
4 #include <common/stats.qh>
5 #include <server/damage.qh>
6 #include <server/world.qh>
7 #include <server/miscfunctions.qh>
8 #include <server/weapons/common.qh>
13 #include "spawnpoints.qh"
15 #include "command/getreplies.qh"
16 #include "../common/deathtypes/all.qh"
17 #include "../common/notifications/all.qh"
18 #include <common/gamemodes/_mod.qh>
19 #include <common/gamemodes/rules.qh>
20 #include <common/net_linked.qh>
21 #include <common/state.qh>
22 #include <common/weapons/weapon/porto.qh>
23 #include "../common/mapobjects/subs.qh"
24 #include <common/mapobjects/triggers.qh>
25 #include "../lib/warpzone/util_server.qh"
26 #include "../lib/warpzone/common.qh"
27 #include <common/vehicles/sv_vehicles.qh>
28 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
30 IntrusiveList g_race_targets;
31 IntrusiveList g_racecheckpoints;
34 g_race_targets = IL_NEW();
35 g_racecheckpoints = IL_NEW();
38 void race_InitSpectator()
41 if(msg_entity.enemy.race_laptime)
42 race_SendNextCheckpoint(msg_entity.enemy, 1);
45 float race_readTime(string map, float pos)
47 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
49 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
52 string race_readUID(string map, float pos)
54 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
56 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
59 float race_readPos(string map, float t)
61 for(int i = 1; i <= RANKINGS_CNT; ++i)
63 int mytime = race_readTime(map, i);
64 if(!mytime || mytime > t)
68 return 0; // pos is zero if unranked
71 void race_writeTime(string map, float t, string myuid)
73 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
76 newpos = race_readPos(map, t);
79 for(i = 1; i <= RANKINGS_CNT; ++i)
81 if(race_readUID(map, i) == myuid)
86 // player improved his existing record, only have to iterate on ranks between new and old recs
87 for (i = prevpos; i > newpos; --i)
89 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
90 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
95 // player has no ranked record yet
96 for (i = RANKINGS_CNT; i > newpos; --i)
98 float other_time = race_readTime(map, i - 1);
100 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(other_time));
101 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
106 // store new time itself
107 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
108 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
111 string race_readName(string map, float pos)
113 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
115 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
119 const float MAX_CHECKPOINTS = 255;
122 .float race_penalty_accumulator;
123 .string race_penalty_reason;
124 .float race_checkpoint; // player: next checkpoint that has to be reached
125 .entity race_lastpenalty;
129 float race_checkpoint_records[MAX_CHECKPOINTS];
130 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
131 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
132 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
133 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
135 .float race_checkpoint_record[MAX_CHECKPOINTS];
137 float race_highest_checkpoint;
138 float race_timed_checkpoint;
141 float defragcpexists;
143 float race_NextCheckpoint(float f)
145 if(f >= race_highest_checkpoint)
151 float race_PreviousCheckpoint(float f)
156 return race_highest_checkpoint;
162 // 0 = common start/finish
165 float race_CheckpointNetworkID(float f)
167 if(race_timed_checkpoint)
171 else if(f == race_timed_checkpoint)
172 return 255; // finish
177 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
182 int cp = e.race_checkpoint;
183 float recordtime = race_checkpoint_records[cp];
184 float myrecordtime = e.race_checkpoint_record[cp];
185 string recordholder = race_checkpoint_recordholders[cp];
186 if(recordholder == e.netname)
189 if(!IS_REAL_CLIENT(e))
194 WRITESPECTATABLE_MSG_ONE(msg_entity, {
195 WriteHeader(MSG_ONE, TE_CSQC_RACE);
198 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
199 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
200 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
203 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
204 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
205 WriteInt24_t(MSG_ONE, recordtime);
207 WriteInt24_t(MSG_ONE, myrecordtime);
208 WriteString(MSG_ONE, recordholder);
212 void race_send_recordtime(float msg)
214 // send the server best time
215 WriteHeader(msg, TE_CSQC_RACE);
216 WriteByte(msg, RACE_NET_SERVER_RECORD);
217 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
221 void race_send_speedaward(float msg)
223 // send the best speed of the round
224 WriteHeader(msg, TE_CSQC_RACE);
225 WriteByte(msg, RACE_NET_SPEED_AWARD);
226 WriteInt24_t(msg, floor(speedaward_speed+0.5));
227 WriteString(msg, speedaward_holder);
230 void race_send_speedaward_alltimebest(float msg)
232 // send the best speed
233 WriteHeader(msg, TE_CSQC_RACE);
234 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
235 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
236 WriteString(msg, speedaward_alltimebest_holder);
239 void race_send_rankings_cnt(float msg)
241 WriteHeader(msg, TE_CSQC_RACE);
242 WriteByte(msg, RACE_NET_RANKINGS_CNT);
243 int m = min(RANKINGS_CNT, autocvar_g_cts_send_rankings_cnt);
247 void race_SendRankings(float pos, float prevpos, float del, float msg)
249 WriteHeader(msg, TE_CSQC_RACE);
250 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
251 WriteShort(msg, pos);
252 WriteShort(msg, prevpos);
253 WriteShort(msg, del);
254 WriteString(msg, race_readName(GetMapname(), pos));
255 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
258 void race_SendStatus(float id, entity e)
260 if(!IS_REAL_CLIENT(e))
269 WRITESPECTATABLE_MSG_ONE(msg_entity, {
270 WriteHeader(msg, TE_CSQC_RACE);
271 WriteByte(msg, RACE_NET_SERVER_STATUS);
273 WriteString(msg, e.netname);
277 void race_setTime(string map, float t, string myuid, string mynetname, entity e, bool showmessage)
279 // netname only used TEMPORARILY for printing
280 int newpos = race_readPos(map, t);
282 int player_prevpos = 0;
283 for(int i = 1; i <= RANKINGS_CNT; ++i)
285 if(race_readUID(map, i) == myuid)
290 string oldrec_holder;
291 if (player_prevpos && (player_prevpos < newpos || !newpos))
293 oldrec = race_readTime(GetMapname(), player_prevpos);
294 race_SendStatus(0, e); // "fail"
296 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
301 // no ranking, time worse than the worst ranked
302 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
303 race_SendStatus(0, e); // "fail"
305 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
309 // if we didn't hit a return yet, we have a new record!
311 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
315 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
319 if(uid2name(myuid) == "^1Unregistered Player")
322 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_MISSING_NAME, mynetname, t);
326 oldrec = race_readTime(GetMapname(), newpos);
327 oldrec_holder = race_readName(GetMapname(), newpos);
330 race_writeTime(GetMapname(), t, myuid);
332 if (newpos == 1 && showmessage)
334 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
335 race_send_recordtime(MSG_ALL);
338 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
339 strcpy(rankings_reply, getrankings());
341 if(newpos == player_prevpos)
344 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
345 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
346 else { race_SendStatus(1, e); } // "new time"
351 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
352 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
353 else { race_SendStatus(2, e); } // "new rank"
358 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
359 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
360 else { race_SendStatus(2, e); } // "new rank"
364 void race_deleteTime(string map, float pos)
366 string rr = ((g_cts) ? CTS_RECORD : ((g_ctf) ? CTF_RECORD : RACE_RECORD));
368 for(int i = pos; i <= RANKINGS_CNT; ++i)
370 string therank = ftos(i);
371 if (i == RANKINGS_CNT)
373 db_remove(ServerProgsDB, strcat(map, rr, "time", therank));
374 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank));
378 db_put(ServerProgsDB, strcat(map, rr, "time", therank), ftos(race_readTime(GetMapname(), i+1)));
379 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", therank), race_readUID(GetMapname(), i+1));
383 race_SendRankings(pos, 0, 1, MSG_ALL);
385 race_send_recordtime(MSG_ALL);
387 strcpy(rankings_reply, getrankings());
390 void race_SendTime(entity e, float cp, float t, float tvalid)
394 if(g_race_qualifying)
395 t += e.race_penalty_accumulator;
397 t = TIME_ENCODE(t); // make integer
400 if(cp == race_timed_checkpoint) // finish line
401 if (!CS(e).race_completed)
404 if(g_race_qualifying)
406 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
408 GameRules_scoring_add(e, RACE_FASTEST, t - s);
412 s = GameRules_scoring_add(e, RACE_FASTEST, 0);
414 GameRules_scoring_add(e, RACE_FASTEST, t - s);
416 s = GameRules_scoring_add(e, RACE_TIME, 0);
417 snew = TIME_ENCODE(time - game_starttime);
418 GameRules_scoring_add(e, RACE_TIME, snew - s);
419 l = GameRules_scoring_add_team(e, RACE_LAPS, 1);
421 if(autocvar_fraglimit)
422 if(l >= autocvar_fraglimit)
423 race_StartCompleting();
427 CS(e).race_completed = 1;
428 MAKE_INDEPENDENT_PLAYER(e);
429 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_FINISHED, e.netname);
435 if(g_race_qualifying)
442 recordtime = race_checkpoint_records[cp];
443 float myrecordtime = e.race_checkpoint_record[cp];
444 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
445 if(recordholder == e.netname)
450 if(cp == race_timed_checkpoint)
452 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e, true);
453 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
455 if(t < myrecordtime || myrecordtime == 0)
456 e.race_checkpoint_record[cp] = t; // resending done below
458 if(t < recordtime || recordtime == 0)
460 race_checkpoint_records[cp] = t;
461 strcpy(race_checkpoint_recordholders[cp], e.netname);
462 if(g_race_qualifying)
463 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, { race_SendNextCheckpoint(it, 0); });
476 if(IS_REAL_CLIENT(e))
478 if(g_race_qualifying)
480 FOREACH_CLIENT(IS_REAL_CLIENT(it),
482 if(it == e || (IS_SPEC(it) && it.enemy == e))
485 WriteHeader(MSG_ONE, TE_CSQC_RACE);
486 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
487 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
488 WriteInt24_t(MSG_ONE, t); // time to that intermediate
489 WriteInt24_t(MSG_ONE, recordtime); // previously best time
490 WriteInt24_t(MSG_ONE, ((tvalid) ? it.race_checkpoint_record[cp] : 0)); // previously best time
491 WriteString(MSG_ONE, recordholder); // record holder
497 else // RACE! Not Qualifying
499 float mylaps, lother, othtime;
500 entity oth = race_checkpoint_lastplayers[cp];
503 mylaps = GameRules_scoring_add(e, RACE_LAPS, 0);
504 lother = race_checkpoint_lastlaps[cp];
505 othtime = race_checkpoint_lasttimes[cp];
508 mylaps = lother = othtime = 0;
510 if(IS_REAL_CLIENT(e))
513 WRITESPECTATABLE_MSG_ONE(msg_entity, {
514 WriteHeader(MSG_ONE, TE_CSQC_RACE);
515 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
516 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
519 WriteInt24_t(MSG_ONE, 0);
520 WriteByte(MSG_ONE, 0);
521 WriteByte(MSG_ONE, 0);
525 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
526 WriteByte(MSG_ONE, mylaps - lother);
527 WriteByte(MSG_ONE, etof(oth)); // record holder
532 race_checkpoint_lastplayers[cp] = e;
533 race_checkpoint_lasttimes[cp] = time;
534 race_checkpoint_lastlaps[cp] = mylaps;
536 if(IS_REAL_CLIENT(oth))
539 WRITESPECTATABLE_MSG_ONE(msg_entity, {
540 WriteHeader(MSG_ONE, TE_CSQC_RACE);
541 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
542 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
545 WriteInt24_t(MSG_ONE, 0);
546 WriteByte(MSG_ONE, 0);
547 WriteByte(MSG_ONE, 0);
551 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
552 WriteByte(MSG_ONE, lother - mylaps);
553 WriteByte(MSG_ONE, etof(e) - 1); // record holder
560 void race_ClearTime(entity e)
562 e.race_checkpoint = 0;
564 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
565 e.race_penalty_accumulator = 0;
566 e.race_lastpenalty = NULL;
568 if(!IS_REAL_CLIENT(e))
572 WRITESPECTATABLE_MSG_ONE(msg_entity, {
573 WriteHeader(MSG_ONE, TE_CSQC_RACE);
574 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
578 void checkpoint_passed(entity this, entity player)
580 if(IS_VEHICLE(player) && player.owner)
581 player = player.owner;
583 if(player.personal && autocvar_g_allow_checkpoints)
584 return; // practice mode!
586 if(player.classname == "porto")
588 // do not allow portalling through checkpoints
589 trace_plane_normal = normalize(-1 * player.velocity);
590 W_Porto_Fail(player, 0);
594 string oldmsg; // used twice
599 if (!((this.spawnflags & 2) && (IS_PLAYER(player))))
601 oldmsg = this.message;
603 SUB_UseTargets(this, player, player);
604 this.message = oldmsg;
607 if (!IS_PLAYER(player))
611 * Remove unauthorized equipment
613 Portal_ClearAll(player);
615 player.porto_forbidden = 2; // decreased by 1 each StartFrame
619 if(this.race_checkpoint == -2)
621 this.race_checkpoint = player.race_checkpoint;
624 int cp_amount = 0, largest_cp_id = 0;
625 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
628 if(it.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
632 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
634 if(it.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
639 largest_cp_id = it.race_checkpoint;
640 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
642 it.race_checkpoint = largest_cp_id + 1; // finish line
644 race_highest_checkpoint = largest_cp_id + 1;
645 race_timed_checkpoint = largest_cp_id + 1;
651 IL_EACH(g_race_targets, it.classname == "target_stopTimer",
653 it.race_checkpoint = 1;
655 race_highest_checkpoint = 1;
656 race_timed_checkpoint = 1;
660 if((player.race_checkpoint == -1 && this.race_checkpoint == 0) || (player.race_checkpoint == this.race_checkpoint))
662 if(this.race_penalty)
664 if(player.race_lastpenalty != this)
666 player.race_lastpenalty = this;
667 race_ImposePenaltyTime(player, this.race_penalty, this.race_penalty_reason);
671 if(player.race_penalty)
677 if(this.spawnflags & 2)
679 oldmsg = this.message;
681 SUB_UseTargets(this, player, player); // TODO: should we be using other for the trigger here?
682 this.message = oldmsg;
685 if(player.race_respawn_checkpoint != this.race_checkpoint || !player.race_started)
686 player.race_respawn_spotref = this; // this is not a spot but a CP, but spawnpoint selection will deal with that
687 player.race_respawn_checkpoint = this.race_checkpoint;
688 player.race_checkpoint = race_NextCheckpoint(this.race_checkpoint);
689 player.race_started = 1;
691 race_SendTime(player, this.race_checkpoint, player.race_movetime, boolean(player.race_laptime));
693 if(!this.race_checkpoint) // start line
695 player.race_laptime = time;
696 player.race_movetime = player.race_movetime_frac = player.race_movetime_count = 0;
697 player.race_penalty_accumulator = 0;
698 player.race_lastpenalty = NULL;
701 if(g_race_qualifying)
702 race_SendNextCheckpoint(player, 0);
704 if(defrag_ents && defragcpexists < 0 && this.classname == "target_stopTimer")
707 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
710 IL_EACH(g_race_targets, it.classname == "target_checkpoint",
712 fputs(fh, strcat(it.targetname, " ", ftos(it.race_checkpoint), "\n"));
718 else if(player.race_checkpoint == race_NextCheckpoint(this.race_checkpoint))
724 if(this.spawnflags & 4)
725 Damage (player, this, this, 10000, DEATH_HURTTRIGGER.m_id, DMG_NOWEP, player.origin, '0 0 0');
729 void checkpoint_touch(entity this, entity toucher)
731 EXACTTRIGGER_TOUCH(this, toucher);
732 checkpoint_passed(this, toucher);
735 void checkpoint_use(entity this, entity actor, entity trigger)
737 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
740 checkpoint_passed(this, actor);
743 bool race_waypointsprite_visible_for_player(entity this, entity player, entity view)
745 entity own = this.owner;
747 own = this.realowner; // target support
749 if(view.race_checkpoint == -1 || own.race_checkpoint == -2)
751 else if(view.race_checkpoint == own.race_checkpoint)
757 void trigger_race_checkpoint_verify(entity this)
759 static bool have_verified;
760 if (have_verified) return;
761 have_verified = true;
763 bool qual = g_race_qualifying;
765 int pl_race_checkpoint = 0;
766 int pl_race_place = 0;
769 for (int i = 0; i <= race_highest_checkpoint; ++i) {
770 pl_race_checkpoint = race_NextCheckpoint(i);
772 // race only (middle of the race)
773 g_race_qualifying = false;
775 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
776 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for respawning in race) - bailing out"));
781 g_race_qualifying = 1;
782 pl_race_place = race_lowest_place_spawn;
783 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
784 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
787 // race only (initial spawn)
788 g_race_qualifying = 0;
789 for (int p = 1; p <= race_highest_place_spawn; ++p) {
791 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
792 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for initially spawning in race) - bailing out"));
797 } else if (!defrag_ents) {
799 pl_race_checkpoint = race_NextCheckpoint(0);
800 g_race_qualifying = 1;
801 pl_race_place = race_lowest_place_spawn;
802 if (!Spawn_FilterOutBadSpots(this, findchain(classname, "info_player_deathmatch"), 0, false, true)) {
803 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(pl_race_place), " (used for qualifying) - bailing out"));
806 pl_race_checkpoint = race_NextCheckpoint(0);
807 g_race_qualifying = 1;
808 pl_race_place = 0; // there's only one spawn on defrag maps
810 // check if a defragcp file already exists, then read it and apply the checkpoint order
815 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
817 while ((l = fgets(fh))) {
818 len = tokenize_console(l);
820 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
823 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
824 if (argv(0) == cp.targetname) {
825 cp.race_checkpoint = stof(argv(1));
833 g_race_qualifying = qual;
835 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
837 if(it.targetname == "" || !it.targetname) // somehow this is a case...
840 FOREACH_ENTITY_STRING(target, cpt.targetname,
842 vector org = (it.absmin + it.absmax) * 0.5;
843 if(cpt.race_checkpoint == 0)
844 WaypointSprite_SpawnFixed(WP_RaceStart, org, it, sprite, RADARICON_NONE);
846 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, org, it, sprite, RADARICON_NONE);
848 it.sprite.realowner = cpt;
849 it.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
853 if (race_timed_checkpoint) {
855 IL_EACH(g_race_targets, it.classname == "target_checkpoint" || it.classname == "target_startTimer" || it.classname == "target_stopTimer",
858 if(it.classname == "target_startTimer" || it.classname == "target_stopTimer") {
859 if(it.targetname == "" || !it.targetname) // somehow this is a case...
861 FOREACH_ENTITY_STRING(target, cpt.targetname, {
863 WaypointSprite_UpdateSprites(it.sprite, ((cpt.classname == "target_startTimer") ? WP_RaceStart : WP_RaceFinish), WP_Null, WP_Null);
866 if(it.classname == "target_checkpoint") {
867 if(it.race_checkpoint == -2)
868 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
871 if (defragcpexists != -1) {
872 float largest_cp_id = 0;
873 for (entity cp = NULL; (cp = find(cp, classname, "target_checkpoint"));) {
874 if (cp.race_checkpoint > largest_cp_id) {
875 largest_cp_id = cp.race_checkpoint;
878 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
879 cp.race_checkpoint = largest_cp_id + 1; // finish line
881 race_highest_checkpoint = largest_cp_id + 1;
882 race_timed_checkpoint = largest_cp_id + 1;
884 for (entity cp = NULL; (cp = find(cp, classname, "target_stopTimer"));) {
885 cp.race_checkpoint = 255; // finish line
887 race_highest_checkpoint = 255;
888 race_timed_checkpoint = 255;
891 IL_EACH(g_racecheckpoints, it.sprite,
893 if (it.race_checkpoint == 0) {
894 WaypointSprite_UpdateSprites(it.sprite, WP_RaceStart, WP_Null, WP_Null);
895 } else if (it.race_checkpoint == race_timed_checkpoint) {
896 WaypointSprite_UpdateSprites(it.sprite, WP_RaceFinish, WP_Null, WP_Null);
903 for (entity trigger = NULL; (trigger = find(trigger, classname, "trigger_multiple")); ) {
904 for (entity targ = NULL; (targ = find(targ, targetname, trigger.target)); ) {
905 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
911 // These just make the game crash on some maps with oddly shaped triggers.
912 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
913 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
914 // (happened on item triggers too)
919 //setsize(targ, trigger.mins, trigger.maxs);
920 //setorigin(targ, trigger.origin);
928 vector trigger_race_checkpoint_spawn_evalfunc(entity this, entity player, entity spot, vector current)
930 if(g_race_qualifying)
933 if(this.race_checkpoint != 0)
935 if(spot.race_place != race_lowest_place_spawn)
940 if(this.race_checkpoint != player.race_respawn_checkpoint)
942 // try reusing the previous spawn
943 if(this == player.race_respawn_spotref || spot == player.race_respawn_spotref)
944 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
945 if(this.race_checkpoint == 0)
947 int pl = player.race_place;
948 if(pl > race_highest_place_spawn)
950 if(pl == 0 && !player.race_started)
951 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
952 if(spot.race_place != pl)
959 spawnfunc(trigger_race_checkpoint)
962 if(!g_race && !g_cts) { delete(this); return; }
966 this.use = checkpoint_use;
967 if (!(this.spawnflags & 1))
968 settouch(this, checkpoint_touch);
970 o = (this.absmin + this.absmax) * 0.5;
971 tracebox(o, PL_MIN_CONST, PL_MAX_CONST, o - '0 0 1' * (o.z - this.absmin.z), MOVE_NORMAL, this);
972 waypoint_spawnforitem_force(this, trace_endpos);
973 this.nearestwaypointtimeout = -1;
975 if(this.message == "")
976 this.message = "went backwards";
977 if (this.message2 == "")
978 this.message2 = "was pushed backwards by";
979 if (this.race_penalty_reason == "")
980 this.race_penalty_reason = "missing a checkpoint";
982 this.race_checkpoint = this.cnt;
984 if(this.race_checkpoint > race_highest_checkpoint)
986 race_highest_checkpoint = this.race_checkpoint;
987 if(this.spawnflags & 8)
988 race_timed_checkpoint = this.race_checkpoint;
990 race_timed_checkpoint = 0;
993 if(!this.race_penalty)
995 if(this.race_checkpoint)
996 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, this, sprite, RADARICON_NONE);
998 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, this, sprite, RADARICON_NONE);
1001 this.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1002 this.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
1004 IL_PUSH(g_racecheckpoints, this);
1006 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1009 spawnfunc(target_checkpoint) // defrag entity
1011 if(!g_race && !g_cts) { delete(this); return; }
1014 // if this is targeted, then it probably isn't a trigger
1015 bool is_trigger = this.targetname == "";
1020 this.use = checkpoint_use;
1021 if (is_trigger && !(this.spawnflags & 1))
1022 settouch(this, checkpoint_touch);
1024 vector org = this.origin;
1026 // bots should only pathfind to this if it is a valid touchable trigger
1029 org = (this.absmin + this.absmax) * 0.5;
1030 tracebox(org, PL_MIN_CONST, PL_MAX_CONST, org - '0 0 1' * (org.z - this.absmin.z), MOVE_NORMAL, this);
1031 waypoint_spawnforitem_force(this, trace_endpos);
1032 this.nearestwaypointtimeout = -1;
1035 if(this.message == "")
1036 this.message = "went backwards";
1037 if (this.message2 == "")
1038 this.message2 = "was pushed backwards by";
1039 if (this.race_penalty_reason == "")
1040 this.race_penalty_reason = "missing a checkpoint";
1042 if(this.classname == "target_startTimer")
1043 this.race_checkpoint = 0;
1045 this.race_checkpoint = -2;
1047 race_timed_checkpoint = 1;
1049 IL_PUSH(g_race_targets, this);
1051 InitializeEntity(this, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1054 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1055 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1057 void race_AbandonRaceCheck(entity p)
1059 if(race_completing && !CS(p).race_completed)
1061 CS(p).race_completed = 1;
1062 MAKE_INDEPENDENT_PLAYER(p);
1063 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1064 ClientData_Touch(p);
1068 void race_StartCompleting()
1070 race_completing = 1;
1071 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), { race_AbandonRaceCheck(it); });
1074 void race_PreparePlayer(entity this)
1076 race_ClearTime(this);
1077 this.race_place = 0;
1078 this.race_started = 0;
1079 this.race_respawn_checkpoint = 0;
1080 this.race_respawn_spotref = NULL;
1083 void race_RetractPlayer(entity this)
1085 if(!g_race && !g_cts)
1087 if(this.race_respawn_checkpoint == 0 || this.race_respawn_checkpoint == race_timed_checkpoint)
1088 race_ClearTime(this);
1089 this.race_checkpoint = this.race_respawn_checkpoint;
1092 spawnfunc(info_player_race)
1094 if(!g_race && !g_cts) { delete(this); return; }
1096 spawnfunc_info_player_deathmatch(this);
1098 if(this.race_place > race_highest_place_spawn)
1099 race_highest_place_spawn = this.race_place;
1100 if(this.race_place < race_lowest_place_spawn)
1101 race_lowest_place_spawn = this.race_place;
1104 void race_ClearRecords()
1106 for(int j = 0; j < MAX_CHECKPOINTS; ++j)
1108 race_checkpoint_records[j] = 0;
1109 strfree(race_checkpoint_recordholders[j]);
1112 FOREACH_CLIENT(true, {
1113 float p = it.race_place;
1114 race_PreparePlayer(it);
1119 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1121 if(g_race_qualifying)
1123 pl.race_penalty_accumulator += penalty;
1124 if(IS_REAL_CLIENT(pl))
1127 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1128 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1129 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1130 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1131 WriteString(MSG_ONE, reason);
1137 pl.race_penalty = time + penalty;
1138 if(IS_REAL_CLIENT(pl))
1141 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1142 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1143 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1144 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1145 WriteString(MSG_ONE, reason);
1151 void penalty_touch(entity this, entity toucher)
1153 EXACTTRIGGER_TOUCH(this, toucher);
1154 if(toucher.race_lastpenalty != this)
1156 toucher.race_lastpenalty = this;
1157 race_ImposePenaltyTime(toucher, this.race_penalty, this.race_penalty_reason);
1161 void penalty_use(entity this, entity actor, entity trigger)
1163 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1166 spawnfunc(trigger_race_penalty)
1168 // TODO: find out why this wasnt done:
1169 //if(!g_cts && !g_race) { remove(this); return; }
1173 this.use = penalty_use;
1174 if (!(this.spawnflags & 1))
1175 settouch(this, penalty_touch);
1177 if (this.race_penalty_reason == "")
1178 this.race_penalty_reason = "missing a checkpoint";
1179 if (!this.race_penalty)
1180 this.race_penalty = 5;
1183 float race_GetFractionalLapCount(entity e)
1185 // interesting metrics (idea by KrimZon) to maybe sort players in the
1186 // scoreboard, immediately updates when overtaking
1188 // requires the track to be built so you never get farther away from the
1189 // next checkpoint, though, and current Xonotic race maps are not built that
1192 // also, this code is slow and would need optimization (i.e. "next CP"
1193 // links on CP entities)
1196 l = GameRules_scoring_add(e, RACE_LAPS, 0);
1197 if(CS(e).race_completed)
1198 return l; // not fractional
1201 float bestfraction, fraction;
1203 float nextcpindex, lastcpindex;
1205 nextcpindex = max(e.race_checkpoint, 0);
1206 lastcpindex = e.race_respawn_checkpoint;
1207 lastcp = e.race_respawn_spotref;
1209 if(nextcpindex == lastcpindex)
1213 IL_EACH(g_racecheckpoints, true,
1215 if(it.race_checkpoint != lastcpindex)
1220 o0 = (it.absmin + it.absmax) * 0.5;
1221 IL_EACH(g_racecheckpoints, true,
1223 if(it.race_checkpoint != nextcpindex)
1225 o1 = (it.absmin + it.absmax) * 0.5;
1228 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1229 if(fraction < bestfraction)
1230 bestfraction = fraction;
1234 // we are at CP "nextcpindex - bestfraction"
1235 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1236 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1238 nc = race_highest_checkpoint + 1;
1239 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;