3 #include "cl_client.qh"
6 #include "spawnpoints.qh"
7 #include "bot/waypoints.qh"
8 #include "bot/navigation.qh"
9 #include "command/getreplies.qh"
10 #include "../common/deathtypes/all.qh"
11 #include "../common/notifications/all.qh"
12 #include "../common/mapinfo.qh"
13 #include "../common/triggers/subs.qh"
14 #include "../lib/warpzone/util_server.qh"
15 #include "../lib/warpzone/common.qh"
16 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
18 void race_InitSpectator()
21 if(msg_entity.enemy.race_laptime)
22 race_SendNextCheckpoint(msg_entity.enemy, 1);
25 void W_Porto_Fail(float failhard);
27 float race_readTime(string map, float pos)
29 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
31 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
34 string race_readUID(string map, float pos)
36 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
38 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
41 float race_readPos(string map, float t)
44 for (i = 1; i <= RANKINGS_CNT; ++i)
45 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
48 return 0; // pos is zero if unranked
51 void race_writeTime(string map, float t, string myuid)
53 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
56 newpos = race_readPos(map, t);
59 for(i = 1; i <= RANKINGS_CNT; ++i)
61 if(race_readUID(map, i) == myuid)
66 // player improved his existing record, only have to iterate on ranks between new and old recs
67 for (i = prevpos; i > newpos; --i)
69 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
70 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
75 // player has no ranked record yet
76 for (i = RANKINGS_CNT; i > newpos; --i)
78 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
79 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
83 // store new time itself
84 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
85 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
88 string race_readName(string map, float pos)
90 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
92 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
96 const float MAX_CHECKPOINTS = 255;
98 spawnfunc(target_checkpoint);
101 .float race_penalty_accumulator;
102 .string race_penalty_reason;
103 .float race_checkpoint; // player: next checkpoint that has to be reached
104 .entity race_lastpenalty;
108 float race_checkpoint_records[MAX_CHECKPOINTS];
109 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
110 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
111 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
112 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
114 float race_highest_checkpoint;
115 float race_timed_checkpoint;
118 float defragcpexists;
120 float race_NextCheckpoint(float f)
122 if(f >= race_highest_checkpoint)
128 float race_PreviousCheckpoint(float f)
133 return race_highest_checkpoint;
139 // 0 = common start/finish
142 float race_CheckpointNetworkID(float f)
144 if(race_timed_checkpoint)
148 else if(f == race_timed_checkpoint)
149 return 255; // finish
154 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
163 cp = e.race_checkpoint;
164 recordtime = race_checkpoint_records[cp];
165 recordholder = race_checkpoint_recordholders[cp];
166 if(recordholder == e.netname)
169 if(!IS_REAL_CLIENT(e))
174 WRITESPECTATABLE_MSG_ONE(msg_entity, {
175 WriteHeader(MSG_ONE, TE_CSQC_RACE);
178 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
179 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
180 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
183 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
184 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
185 WriteInt24_t(MSG_ONE, recordtime);
186 WriteString(MSG_ONE, recordholder);
190 void race_send_recordtime(float msg)
192 // send the server best time
193 WriteHeader(msg, TE_CSQC_RACE);
194 WriteByte(msg, RACE_NET_SERVER_RECORD);
195 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
199 void race_send_speedaward(float msg)
201 // send the best speed of the round
202 WriteHeader(msg, TE_CSQC_RACE);
203 WriteByte(msg, RACE_NET_SPEED_AWARD);
204 WriteInt24_t(msg, floor(speedaward_speed+0.5));
205 WriteString(msg, speedaward_holder);
208 void race_send_speedaward_alltimebest(float msg)
210 // send the best speed
211 WriteHeader(msg, TE_CSQC_RACE);
212 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
213 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
214 WriteString(msg, speedaward_alltimebest_holder);
217 void race_SendRankings(float pos, float prevpos, float del, float msg)
219 WriteHeader(msg, TE_CSQC_RACE);
220 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
221 WriteShort(msg, pos);
222 WriteShort(msg, prevpos);
223 WriteShort(msg, del);
224 WriteString(msg, race_readName(GetMapname(), pos));
225 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
228 void race_SendStatus(float id, entity e)
230 if(!IS_REAL_CLIENT(e))
239 WRITESPECTATABLE_MSG_ONE(msg_entity, {
240 WriteHeader(msg, TE_CSQC_RACE);
241 WriteByte(msg, RACE_NET_SERVER_STATUS);
243 WriteString(msg, e.netname);
247 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
249 // netname only used TEMPORARILY for printing
250 float newpos, player_prevpos;
251 newpos = race_readPos(map, t);
255 for(i = 1; i <= RANKINGS_CNT; ++i)
257 if(race_readUID(map, i) == myuid)
262 string oldrec_holder;
263 if (player_prevpos && (player_prevpos < newpos || !newpos))
265 oldrec = race_readTime(GetMapname(), player_prevpos);
266 race_SendStatus(0, e); // "fail"
267 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
272 // no ranking, time worse than the worst ranked
273 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
274 race_SendStatus(0, e); // "fail"
275 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
279 // if we didn't hit a return yet, we have a new record!
281 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
284 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
288 oldrec = race_readTime(GetMapname(), newpos);
289 oldrec_holder = race_readName(GetMapname(), newpos);
292 race_writeTime(GetMapname(), t, myuid);
296 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
297 race_send_recordtime(MSG_ALL);
300 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
302 strunzone(rankings_reply);
303 rankings_reply = strzone(getrankings());
305 if(newpos == player_prevpos)
307 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
308 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
309 else { race_SendStatus(1, e); } // "new time"
313 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
314 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
315 else { race_SendStatus(2, e); } // "new rank"
319 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
320 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
321 else { race_SendStatus(2, e); } // "new rank"
325 void race_deleteTime(string map, float pos)
334 for (i = pos; i <= RANKINGS_CNT; ++i)
336 if (i == RANKINGS_CNT)
338 db_remove(ServerProgsDB, strcat(map, rr, "time", ftos(i)));
339 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)));
343 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
344 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
348 race_SendRankings(pos, 0, 1, MSG_ALL);
350 race_send_recordtime(MSG_ALL);
353 strunzone(rankings_reply);
354 rankings_reply = strzone(getrankings());
357 void race_SendTime(entity e, float cp, float t, float tvalid)
361 if(g_race_qualifying)
362 t += e.race_penalty_accumulator;
364 t = TIME_ENCODE(t); // make integer
365 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
368 if(cp == race_timed_checkpoint) // finish line
369 if (!e.race_completed)
372 if(g_race_qualifying)
374 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
376 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
380 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
382 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
384 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
385 snew = TIME_ENCODE(time - game_starttime);
386 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
387 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
389 if(autocvar_fraglimit)
390 if(l >= autocvar_fraglimit)
391 race_StartCompleting();
395 e.race_completed = 1;
396 MAKE_INDEPENDENT_PLAYER(e);
397 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
405 if(g_race_qualifying)
409 recordtime = race_checkpoint_records[cp];
410 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
411 if(recordholder == e.netname)
416 if(cp == race_timed_checkpoint)
418 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
419 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
421 if(t < recordtime || recordtime == 0)
423 race_checkpoint_records[cp] = t;
424 if(race_checkpoint_recordholders[cp])
425 strunzone(race_checkpoint_recordholders[cp]);
426 race_checkpoint_recordholders[cp] = strzone(e.netname);
427 if(g_race_qualifying)
429 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.race_checkpoint == cp, LAMBDA(race_SendNextCheckpoint(it, 0)));
442 if(IS_REAL_CLIENT(e))
445 if(g_race_qualifying)
447 WRITESPECTATABLE_MSG_ONE(msg_entity, {
448 WriteHeader(MSG_ONE, TE_CSQC_RACE);
449 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
450 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
451 WriteInt24_t(MSG_ONE, t); // time to that intermediate
452 WriteInt24_t(MSG_ONE, recordtime); // previously best time
453 WriteString(MSG_ONE, recordholder); // record holder
458 else // RACE! Not Qualifying
460 float lself, lother, othtime;
462 oth = race_checkpoint_lastplayers[cp];
465 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
466 lother = race_checkpoint_lastlaps[cp];
467 othtime = race_checkpoint_lasttimes[cp];
470 lself = lother = othtime = 0;
472 if(IS_REAL_CLIENT(e))
475 WRITESPECTATABLE_MSG_ONE(msg_entity, {
476 WriteHeader(MSG_ONE, TE_CSQC_RACE);
477 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
478 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
481 WriteInt24_t(MSG_ONE, 0);
482 WriteByte(MSG_ONE, 0);
483 WriteString(MSG_ONE, "");
487 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
488 WriteByte(MSG_ONE, lself - lother);
489 WriteString(MSG_ONE, oth.netname); // record holder
494 race_checkpoint_lastplayers[cp] = e;
495 race_checkpoint_lasttimes[cp] = time;
496 race_checkpoint_lastlaps[cp] = lself;
498 if(IS_REAL_CLIENT(oth))
501 WRITESPECTATABLE_MSG_ONE(msg_entity, {
502 WriteHeader(MSG_ONE, TE_CSQC_RACE);
503 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
504 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
507 WriteInt24_t(MSG_ONE, 0);
508 WriteByte(MSG_ONE, 0);
509 WriteString(MSG_ONE, "");
513 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
514 WriteByte(MSG_ONE, lother - lself);
515 WriteString(MSG_ONE, e.netname); // record holder
522 void race_ClearTime(entity e)
524 e.race_checkpoint = 0;
526 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
527 e.race_penalty_accumulator = 0;
528 e.race_lastpenalty = world;
530 if(!IS_REAL_CLIENT(e))
534 WRITESPECTATABLE_MSG_ONE(msg_entity, {
535 WriteHeader(MSG_ONE, TE_CSQC_RACE);
536 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
540 void dumpsurface(entity e)
544 LOG_INFO("Surfaces of ", etos(e), ":\n");
546 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
550 n = getsurfacenumpoints(e, si);
553 LOG_INFO(" Surface ", ftos(si), ":\n");
554 norm = getsurfacenormal(e, si);
555 LOG_INFO(" Normal = ", vtos(norm), "\n");
556 for(ni = 0; ni < n; ++ni)
558 vec = getsurfacepoint(e, si, ni);
559 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
564 void checkpoint_passed()
569 if(other.classname == "porto")
571 // do not allow portalling through checkpoints
572 trace_plane_normal = normalize(-1 * other.velocity);
581 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
583 oldmsg = self.message;
585 SUB_UseTargets(self, other, other); // TODO: should we be using other for the trigger here?
586 self.message = oldmsg;
589 if (!IS_PLAYER(other))
593 * Remove unauthorized equipment
595 Portal_ClearAll(other);
597 other.porto_forbidden = 2; // decreased by 1 each StartFrame
601 if(self.race_checkpoint == -2)
603 self.race_checkpoint = other.race_checkpoint;
606 float largest_cp_id = 0;
608 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
611 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
613 largest_cp_id = cp.race_checkpoint;
614 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
615 cp.race_checkpoint = largest_cp_id + 1; // finish line
616 race_highest_checkpoint = largest_cp_id + 1;
617 race_timed_checkpoint = largest_cp_id + 1;
619 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
621 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
628 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
629 cp.race_checkpoint = 1;
630 race_highest_checkpoint = 1;
631 race_timed_checkpoint = 1;
635 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
637 if(self.race_penalty)
639 if(other.race_lastpenalty != self)
641 other.race_lastpenalty = self;
642 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
646 if(other.race_penalty)
652 if(self.spawnflags & 2)
654 oldmsg = self.message;
656 SUB_UseTargets(self, other, other); // TODO: should we be using other for the trigger here?
657 self.message = oldmsg;
660 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
661 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
662 other.race_respawn_checkpoint = self.race_checkpoint;
663 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
664 other.race_started = 1;
666 race_SendTime(other, self.race_checkpoint, other.race_movetime, boolean(other.race_laptime));
668 if(!self.race_checkpoint) // start line
670 other.race_laptime = time;
671 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
672 other.race_penalty_accumulator = 0;
673 other.race_lastpenalty = world;
676 if(g_race_qualifying)
677 race_SendNextCheckpoint(other, 0);
679 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
682 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
685 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
686 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
691 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
697 if(self.spawnflags & 4)
698 Damage (other, self, self, 10000, DEATH_HURTTRIGGER.m_id, other.origin, '0 0 0');
702 void checkpoint_touch()
708 void checkpoint_use(entity this, entity actor, entity trigger)
710 if(trigger.classname == "info_player_deathmatch") // a spawn, a spawn
717 float race_waypointsprite_visible_for_player(entity e)
719 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
721 else if(e.race_checkpoint == self.owner.race_checkpoint)
728 void trigger_race_checkpoint_verify()
738 qual = g_race_qualifying;
741 TRANSMUTE(Player, self);
745 for(i = 0; i <= race_highest_checkpoint; ++i)
747 self.race_checkpoint = race_NextCheckpoint(i);
749 // race only (middle of the race)
750 g_race_qualifying = 0;
752 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
753 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
758 g_race_qualifying = 1;
759 self.race_place = race_lowest_place_spawn;
760 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
761 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
763 // race only (initial spawn)
764 g_race_qualifying = 0;
765 for(p = 1; p <= race_highest_place_spawn; ++p)
768 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
769 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
774 else if(!defrag_ents)
777 self.race_checkpoint = race_NextCheckpoint(0);
778 g_race_qualifying = 1;
779 self.race_place = race_lowest_place_spawn;
780 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
781 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
785 self.race_checkpoint = race_NextCheckpoint(0);
786 g_race_qualifying = 1;
787 self.race_place = 0; // there's only one spawn on defrag maps
789 // check if a defragcp file already exists, then read it and apply the checkpoint order
794 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
797 while((l = fgets(fh)))
799 len = tokenize_console(l);
802 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
805 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
806 if(argv(0) == cp.targetname)
807 cp.race_checkpoint = stof(argv(1));
813 g_race_qualifying = qual;
815 if(race_timed_checkpoint)
819 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
820 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
821 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
822 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
824 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
826 if(cp.race_checkpoint == -2) // 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
830 if(defragcpexists != -1)
832 float largest_cp_id = 0;
833 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
834 if(cp.race_checkpoint > largest_cp_id)
835 largest_cp_id = cp.race_checkpoint;
836 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
837 cp.race_checkpoint = largest_cp_id + 1; // finish line
838 race_highest_checkpoint = largest_cp_id + 1;
839 race_timed_checkpoint = largest_cp_id + 1;
843 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
844 cp.race_checkpoint = 255; // finish line
845 race_highest_checkpoint = 255;
846 race_timed_checkpoint = 255;
851 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
854 if(cp.race_checkpoint == 0)
855 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
856 else if(cp.race_checkpoint == race_timed_checkpoint)
857 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
864 entity trigger, targ;
865 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
866 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
867 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
874 // These just make the game crash on some maps with oddly shaped triggers.
875 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
876 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
877 // (happened on item triggers too)
882 //setsize(targ, trigger.mins, trigger.maxs);
883 //setorigin(targ, trigger.origin);
891 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
893 if(g_race_qualifying)
896 if(self.race_checkpoint != 0)
898 if(spot.race_place != race_lowest_place_spawn)
903 if(self.race_checkpoint != player.race_respawn_checkpoint)
905 // try reusing the previous spawn
906 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
907 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
908 if(self.race_checkpoint == 0)
911 pl = player.race_place;
912 if(pl > race_highest_place_spawn)
914 if(pl == 0 && !player.race_started)
915 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
916 if(spot.race_place != pl)
923 spawnfunc(trigger_race_checkpoint)
926 if(!g_race && !g_cts) { remove(self); return; }
930 self.use1 = checkpoint_use;
931 if (!(self.spawnflags & 1))
932 self.touch = checkpoint_touch;
934 o = (self.absmin + self.absmax) * 0.5;
935 tracebox(o, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
936 waypoint_spawnforitem_force(self, trace_endpos);
937 self.nearestwaypointtimeout = time + 1000000000;
939 if(self.message == "")
940 self.message = "went backwards";
941 if (self.message2 == "")
942 self.message2 = "was pushed backwards by";
943 if (self.race_penalty_reason == "")
944 self.race_penalty_reason = "missing a checkpoint";
946 self.race_checkpoint = self.cnt;
948 if(self.race_checkpoint > race_highest_checkpoint)
950 race_highest_checkpoint = self.race_checkpoint;
951 if(self.spawnflags & 8)
952 race_timed_checkpoint = self.race_checkpoint;
954 race_timed_checkpoint = 0;
957 if(!self.race_penalty)
959 if(self.race_checkpoint)
960 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
962 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, self, sprite, RADARICON_NONE);
965 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
966 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
968 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
971 spawnfunc(target_checkpoint) // defrag entity
974 if(!g_race && !g_cts) { remove(self); return; }
979 self.use1 = checkpoint_use;
980 if (!(self.spawnflags & 1))
981 self.touch = checkpoint_touch;
983 o = (self.absmin + self.absmax) * 0.5;
984 tracebox(o, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
985 waypoint_spawnforitem_force(self, trace_endpos);
986 self.nearestwaypointtimeout = time + 1000000000;
988 if(self.message == "")
989 self.message = "went backwards";
990 if (self.message2 == "")
991 self.message2 = "was pushed backwards by";
992 if (self.race_penalty_reason == "")
993 self.race_penalty_reason = "missing a checkpoint";
995 if(self.classname == "target_startTimer")
996 self.race_checkpoint = 0;
998 self.race_checkpoint = -2;
1000 race_timed_checkpoint = 1;
1002 if(self.race_checkpoint == 0)
1003 WaypointSprite_SpawnFixed(WP_RaceStart, o, self, sprite, RADARICON_NONE);
1005 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
1007 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1009 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1012 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1013 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1015 void race_AbandonRaceCheck(entity p)
1017 if(race_completing && !p.race_completed)
1019 p.race_completed = 1;
1020 MAKE_INDEPENDENT_PLAYER(p);
1021 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1022 ClientData_Touch(p);
1026 void race_StartCompleting()
1028 race_completing = 1;
1029 FOREACH_CLIENT(IS_PLAYER(it) && IS_DEAD(it), LAMBDA(race_AbandonRaceCheck(it)));
1032 void race_PreparePlayer()
1034 race_ClearTime(self);
1035 self.race_place = 0;
1036 self.race_started = 0;
1037 self.race_respawn_checkpoint = 0;
1038 self.race_respawn_spotref = world;
1041 void race_RetractPlayer()
1043 if(!g_race && !g_cts)
1045 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1046 race_ClearTime(self);
1047 self.race_checkpoint = self.race_respawn_checkpoint;
1050 spawnfunc(info_player_race)
1052 if(!g_race && !g_cts) { remove(self); return; }
1054 spawnfunc_info_player_deathmatch(this);
1056 if(self.race_place > race_highest_place_spawn)
1057 race_highest_place_spawn = self.race_place;
1058 if(self.race_place < race_lowest_place_spawn)
1059 race_lowest_place_spawn = self.race_place;
1062 void race_ClearRecords()
1066 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1068 race_checkpoint_records[i] = 0;
1069 if(race_checkpoint_recordholders[i])
1070 strunzone(race_checkpoint_recordholders[i]);
1071 race_checkpoint_recordholders[i] = string_null;
1074 FOREACH_CLIENT(true, LAMBDA(
1075 float p = it.race_place;
1076 WITHSELF(it, race_PreparePlayer());
1081 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1083 if(g_race_qualifying)
1085 pl.race_penalty_accumulator += penalty;
1086 if(IS_REAL_CLIENT(pl))
1089 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1090 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1091 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1092 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1093 WriteString(MSG_ONE, reason);
1099 pl.race_penalty = time + penalty;
1100 if(IS_REAL_CLIENT(pl))
1103 WRITESPECTATABLE_MSG_ONE(msg_entity, {
1104 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1105 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1106 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1107 WriteString(MSG_ONE, reason);
1113 void penalty_touch()
1116 if(other.race_lastpenalty != self)
1118 other.race_lastpenalty = self;
1119 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1123 void penalty_use(entity this, entity actor, entity trigger)
1125 race_ImposePenaltyTime(actor, this.race_penalty, this.race_penalty_reason);
1128 spawnfunc(trigger_race_penalty)
1130 // TODO: find out why this wasnt done:
1131 //if(!g_cts && !g_race) { remove(self); return; }
1135 self.use1 = penalty_use;
1136 if (!(self.spawnflags & 1))
1137 self.touch = penalty_touch;
1139 if (self.race_penalty_reason == "")
1140 self.race_penalty_reason = "missing a checkpoint";
1141 if (!self.race_penalty)
1142 self.race_penalty = 5;
1145 float race_GetFractionalLapCount(entity e)
1147 // interesting metrics (idea by KrimZon) to maybe sort players in the
1148 // scoreboard, immediately updates when overtaking
1150 // requires the track to be built so you never get farther away from the
1151 // next checkpoint, though, and current Xonotic race maps are not built that
1154 // also, this code is slow and would need optimization (i.e. "next CP"
1155 // links on CP entities)
1158 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1159 if(e.race_completed)
1160 return l; // not fractional
1163 float bestfraction, fraction;
1164 entity lastcp, cp0, cp1;
1165 float nextcpindex, lastcpindex;
1167 nextcpindex = max(e.race_checkpoint, 0);
1168 lastcpindex = e.race_respawn_checkpoint;
1169 lastcp = e.race_respawn_spotref;
1171 if(nextcpindex == lastcpindex)
1175 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1177 if(cp0.race_checkpoint != lastcpindex)
1182 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1183 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1185 if(cp1.race_checkpoint != nextcpindex)
1187 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1190 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1191 if(fraction < bestfraction)
1192 bestfraction = fraction;
1196 // we are at CP "nextcpindex - bestfraction"
1197 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1198 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1200 nc = race_highest_checkpoint + 1;
1201 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;