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.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 W_Porto_Fail(float failhard);
20 float race_readTime(string map, float pos)
22 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
24 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
27 string race_readUID(string map, float pos)
29 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
31 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
34 float race_readPos(string map, float t)
37 for (i = 1; i <= RANKINGS_CNT; ++i)
38 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
41 return 0; // pos is zero if unranked
44 void race_writeTime(string map, float t, string myuid)
46 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
49 newpos = race_readPos(map, t);
52 for(i = 1; i <= RANKINGS_CNT; ++i)
54 if(race_readUID(map, i) == myuid)
59 // player improved his existing record, only have to iterate on ranks between new and old recs
60 for (i = prevpos; i > newpos; --i)
62 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
63 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
68 // player has no ranked record yet
69 for (i = RANKINGS_CNT; i > newpos; --i)
71 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
72 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
76 // store new time itself
77 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
78 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
81 string race_readName(string map, float pos)
83 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
85 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
89 const float MAX_CHECKPOINTS = 255;
91 spawnfunc(target_checkpoint);
94 .float race_penalty_accumulator;
95 .string race_penalty_reason;
96 .float race_checkpoint; // player: next checkpoint that has to be reached
97 .entity race_lastpenalty;
101 float race_checkpoint_records[MAX_CHECKPOINTS];
102 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
103 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
104 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
105 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
107 float race_highest_checkpoint;
108 float race_timed_checkpoint;
111 float defragcpexists;
113 float race_NextCheckpoint(float f)
115 if(f >= race_highest_checkpoint)
121 float race_PreviousCheckpoint(float f)
126 return race_highest_checkpoint;
132 // 0 = common start/finish
135 float race_CheckpointNetworkID(float f)
137 if(race_timed_checkpoint)
141 else if(f == race_timed_checkpoint)
142 return 255; // finish
147 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
156 cp = e.race_checkpoint;
157 recordtime = race_checkpoint_records[cp];
158 recordholder = race_checkpoint_recordholders[cp];
159 if(recordholder == e.netname)
162 if(!IS_REAL_CLIENT(e))
167 WRITESPECTATABLE_MSG_ONE({
168 WriteHeader(MSG_ONE, TE_CSQC_RACE);
171 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
172 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
173 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
176 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
177 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
178 WriteInt24_t(MSG_ONE, recordtime);
179 WriteString(MSG_ONE, recordholder);
183 void race_send_recordtime(float msg)
185 // send the server best time
186 WriteHeader(msg, TE_CSQC_RACE);
187 WriteByte(msg, RACE_NET_SERVER_RECORD);
188 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
192 void race_send_speedaward(float msg)
194 // send the best speed of the round
195 WriteHeader(msg, TE_CSQC_RACE);
196 WriteByte(msg, RACE_NET_SPEED_AWARD);
197 WriteInt24_t(msg, floor(speedaward_speed+0.5));
198 WriteString(msg, speedaward_holder);
201 void race_send_speedaward_alltimebest(float msg)
203 // send the best speed
204 WriteHeader(msg, TE_CSQC_RACE);
205 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
206 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
207 WriteString(msg, speedaward_alltimebest_holder);
210 void race_SendRankings(float pos, float prevpos, float del, float msg)
212 WriteHeader(msg, TE_CSQC_RACE);
213 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
214 WriteShort(msg, pos);
215 WriteShort(msg, prevpos);
216 WriteShort(msg, del);
217 WriteString(msg, race_readName(GetMapname(), pos));
218 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
221 void race_SendStatus(float id, entity e)
223 if(!IS_REAL_CLIENT(e))
232 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
233 WriteHeader(msg, TE_CSQC_RACE);
234 WriteByte(msg, RACE_NET_SERVER_STATUS);
236 WriteString(msg, e.netname);
240 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
242 // netname only used TEMPORARILY for printing
243 float newpos, player_prevpos;
244 newpos = race_readPos(map, t);
248 for(i = 1; i <= RANKINGS_CNT; ++i)
250 if(race_readUID(map, i) == myuid)
255 string oldrec_holder;
256 if (player_prevpos && (player_prevpos < newpos || !newpos))
258 oldrec = race_readTime(GetMapname(), player_prevpos);
259 race_SendStatus(0, e); // "fail"
260 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
265 // no ranking, time worse than the worst ranked
266 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
267 race_SendStatus(0, e); // "fail"
268 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
272 // if we didn't hit a return yet, we have a new record!
274 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
277 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
281 oldrec = race_readTime(GetMapname(), newpos);
282 oldrec_holder = race_readName(GetMapname(), newpos);
285 race_writeTime(GetMapname(), t, myuid);
289 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
290 race_send_recordtime(MSG_ALL);
293 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
295 strunzone(rankings_reply);
296 rankings_reply = strzone(getrankings());
298 if(newpos == player_prevpos)
300 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
301 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
302 else { race_SendStatus(1, e); } // "new time"
306 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
307 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
308 else { race_SendStatus(2, e); } // "new rank"
312 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
313 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
314 else { race_SendStatus(2, e); } // "new rank"
318 void race_deleteTime(string map, float pos)
327 for (i = pos; i <= RANKINGS_CNT; ++i)
329 if (i == RANKINGS_CNT)
331 db_remove(ServerProgsDB, strcat(map, rr, "time", ftos(i)));
332 db_remove(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)));
336 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
337 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
341 race_SendRankings(pos, 0, 1, MSG_ALL);
343 race_send_recordtime(MSG_ALL);
346 strunzone(rankings_reply);
347 rankings_reply = strzone(getrankings());
350 void race_SendTime(entity e, float cp, float t, float tvalid)
355 if(g_race_qualifying)
356 t += e.race_penalty_accumulator;
358 t = TIME_ENCODE(t); // make integer
359 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
362 if(cp == race_timed_checkpoint) // finish line
363 if (!e.race_completed)
366 if(g_race_qualifying)
368 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
370 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
374 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
376 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
378 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
379 snew = TIME_ENCODE(time - game_starttime);
380 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
381 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
383 if(autocvar_fraglimit)
384 if(l >= autocvar_fraglimit)
385 race_StartCompleting();
389 e.race_completed = 1;
390 MAKE_INDEPENDENT_PLAYER(e);
391 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
399 if(g_race_qualifying)
403 recordtime = race_checkpoint_records[cp];
404 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
405 if(recordholder == e.netname)
410 if(cp == race_timed_checkpoint)
412 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
413 MUTATOR_CALLHOOK(Race_FinalCheckpoint, e);
415 if(t < recordtime || recordtime == 0)
417 race_checkpoint_records[cp] = t;
418 if(race_checkpoint_recordholders[cp])
419 strunzone(race_checkpoint_recordholders[cp]);
420 race_checkpoint_recordholders[cp] = strzone(e.netname);
421 if(g_race_qualifying)
423 FOR_EACH_REALPLAYER(p)
424 if(p.race_checkpoint == cp)
425 race_SendNextCheckpoint(p, 0);
438 if(IS_REAL_CLIENT(e))
441 if(g_race_qualifying)
443 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
444 WriteHeader(MSG_ONE, TE_CSQC_RACE);
445 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
446 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
447 WriteInt24_t(MSG_ONE, t); // time to that intermediate
448 WriteInt24_t(MSG_ONE, recordtime); // previously best time
449 WriteString(MSG_ONE, recordholder); // record holder
454 else // RACE! Not Qualifying
456 float lself, lother, othtime;
458 oth = race_checkpoint_lastplayers[cp];
461 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
462 lother = race_checkpoint_lastlaps[cp];
463 othtime = race_checkpoint_lasttimes[cp];
466 lself = lother = othtime = 0;
468 if(IS_REAL_CLIENT(e))
471 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
472 WriteHeader(MSG_ONE, TE_CSQC_RACE);
473 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
474 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
477 WriteInt24_t(MSG_ONE, 0);
478 WriteByte(MSG_ONE, 0);
479 WriteString(MSG_ONE, "");
483 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
484 WriteByte(MSG_ONE, lself - lother);
485 WriteString(MSG_ONE, oth.netname); // record holder
490 race_checkpoint_lastplayers[cp] = e;
491 race_checkpoint_lasttimes[cp] = time;
492 race_checkpoint_lastlaps[cp] = lself;
494 if(IS_REAL_CLIENT(oth))
497 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
498 WriteHeader(MSG_ONE, TE_CSQC_RACE);
499 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
500 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
503 WriteInt24_t(MSG_ONE, 0);
504 WriteByte(MSG_ONE, 0);
505 WriteString(MSG_ONE, "");
509 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
510 WriteByte(MSG_ONE, lother - lself);
511 WriteString(MSG_ONE, e.netname); // record holder
518 void race_ClearTime(entity e)
520 e.race_checkpoint = 0;
522 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
523 e.race_penalty_accumulator = 0;
524 e.race_lastpenalty = world;
526 if(!IS_REAL_CLIENT(e))
530 WRITESPECTATABLE_MSG_ONE({
531 WriteHeader(MSG_ONE, TE_CSQC_RACE);
532 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
536 void dumpsurface(entity e)
540 LOG_INFO("Surfaces of ", etos(e), ":\n");
542 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
546 n = getsurfacenumpoints(e, si);
549 LOG_INFO(" Surface ", ftos(si), ":\n");
550 norm = getsurfacenormal(e, si);
551 LOG_INFO(" Normal = ", vtos(norm), "\n");
552 for(ni = 0; ni < n; ++ni)
554 vec = getsurfacepoint(e, si, ni);
555 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
560 void checkpoint_passed()
565 if(other.classname == "porto")
567 // do not allow portalling through checkpoints
568 trace_plane_normal = normalize(-1 * other.velocity);
577 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
580 oldmsg = self.message;
583 self.message = oldmsg;
586 if (!IS_PLAYER(other))
590 * Remove unauthorized equipment
592 Portal_ClearAll(other);
594 other.porto_forbidden = 2; // decreased by 1 each StartFrame
598 if(self.race_checkpoint == -2)
600 self.race_checkpoint = other.race_checkpoint;
603 float largest_cp_id = 0;
605 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
608 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
610 largest_cp_id = cp.race_checkpoint;
611 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
612 cp.race_checkpoint = largest_cp_id + 1; // finish line
613 race_highest_checkpoint = largest_cp_id + 1;
614 race_timed_checkpoint = largest_cp_id + 1;
616 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
618 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
625 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
626 cp.race_checkpoint = 1;
627 race_highest_checkpoint = 1;
628 race_timed_checkpoint = 1;
632 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
634 if(self.race_penalty)
636 if(other.race_lastpenalty != self)
638 other.race_lastpenalty = self;
639 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
643 if(other.race_penalty)
649 if(self.spawnflags & 2)
652 oldmsg = self.message;
655 self.message = oldmsg;
658 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
659 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
660 other.race_respawn_checkpoint = self.race_checkpoint;
661 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
662 other.race_started = 1;
664 race_SendTime(other, self.race_checkpoint, other.race_movetime, boolean(other.race_laptime));
666 if(!self.race_checkpoint) // start line
668 other.race_laptime = time;
669 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
670 other.race_penalty_accumulator = 0;
671 other.race_lastpenalty = world;
674 if(g_race_qualifying)
675 race_SendNextCheckpoint(other, 0);
677 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
680 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
683 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
684 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
689 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
695 if(self.spawnflags & 4)
696 Damage (other, self, self, 10000, DEATH_HURTTRIGGER.m_id, other.origin, '0 0 0');
700 void checkpoint_touch()
706 void checkpoint_use()
708 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
715 float race_waypointsprite_visible_for_player(entity e)
717 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
719 else if(e.race_checkpoint == self.owner.race_checkpoint)
726 void trigger_race_checkpoint_verify()
736 qual = g_race_qualifying;
739 self.classname = STR_PLAYER;
743 for(i = 0; i <= race_highest_checkpoint; ++i)
745 self.race_checkpoint = race_NextCheckpoint(i);
747 // race only (middle of the race)
748 g_race_qualifying = 0;
750 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
751 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
756 g_race_qualifying = 1;
757 self.race_place = race_lowest_place_spawn;
758 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
759 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
761 // race only (initial spawn)
762 g_race_qualifying = 0;
763 for(p = 1; p <= race_highest_place_spawn; ++p)
766 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
767 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
772 else if(!defrag_ents)
775 self.race_checkpoint = race_NextCheckpoint(0);
776 g_race_qualifying = 1;
777 self.race_place = race_lowest_place_spawn;
778 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
779 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
783 self.race_checkpoint = race_NextCheckpoint(0);
784 g_race_qualifying = 1;
785 self.race_place = 0; // there's only one spawn on defrag maps
787 // check if a defragcp file already exists, then read it and apply the checkpoint order
792 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
795 while((l = fgets(fh)))
797 len = tokenize_console(l);
800 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
803 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
804 if(argv(0) == cp.targetname)
805 cp.race_checkpoint = stof(argv(1));
811 g_race_qualifying = qual;
813 if(race_timed_checkpoint)
817 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
818 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
819 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
820 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
822 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
824 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
828 if(defragcpexists != -1)
830 float largest_cp_id = 0;
831 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
832 if(cp.race_checkpoint > largest_cp_id)
833 largest_cp_id = cp.race_checkpoint;
834 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
835 cp.race_checkpoint = largest_cp_id + 1; // finish line
836 race_highest_checkpoint = largest_cp_id + 1;
837 race_timed_checkpoint = largest_cp_id + 1;
841 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
842 cp.race_checkpoint = 255; // finish line
843 race_highest_checkpoint = 255;
844 race_timed_checkpoint = 255;
849 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
852 if(cp.race_checkpoint == 0)
853 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
854 else if(cp.race_checkpoint == race_timed_checkpoint)
855 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
862 entity trigger, targ;
863 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
864 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
865 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
872 // These just make the game crash on some maps with oddly shaped triggers.
873 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
874 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
875 // (happened on item triggers too)
880 //setsize(targ, trigger.mins, trigger.maxs);
881 //setorigin(targ, trigger.origin);
889 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
891 if(g_race_qualifying)
894 if(self.race_checkpoint != 0)
896 if(spot.race_place != race_lowest_place_spawn)
901 if(self.race_checkpoint != player.race_respawn_checkpoint)
903 // try reusing the previous spawn
904 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
905 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
906 if(self.race_checkpoint == 0)
909 pl = player.race_place;
910 if(pl > race_highest_place_spawn)
912 if(pl == 0 && !player.race_started)
913 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
914 if(spot.race_place != pl)
921 spawnfunc(trigger_race_checkpoint)
924 if(!g_race && !g_cts) { remove(self); return; }
928 self.use = checkpoint_use;
929 if (!(self.spawnflags & 1))
930 self.touch = checkpoint_touch;
932 o = (self.absmin + self.absmax) * 0.5;
933 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
934 waypoint_spawnforitem_force(self, trace_endpos);
935 self.nearestwaypointtimeout = time + 1000000000;
937 if(self.message == "")
938 self.message = "went backwards";
939 if (self.message2 == "")
940 self.message2 = "was pushed backwards by";
941 if (self.race_penalty_reason == "")
942 self.race_penalty_reason = "missing a checkpoint";
944 self.race_checkpoint = self.cnt;
946 if(self.race_checkpoint > race_highest_checkpoint)
948 race_highest_checkpoint = self.race_checkpoint;
949 if(self.spawnflags & 8)
950 race_timed_checkpoint = self.race_checkpoint;
952 race_timed_checkpoint = 0;
955 if(!self.race_penalty)
957 if(self.race_checkpoint)
958 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
960 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, self, sprite, RADARICON_NONE);
963 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
964 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
966 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
969 spawnfunc(target_checkpoint) // defrag entity
972 if(!g_race && !g_cts) { remove(self); return; }
977 self.use = checkpoint_use;
978 if (!(self.spawnflags & 1))
979 self.touch = checkpoint_touch;
981 o = (self.absmin + self.absmax) * 0.5;
982 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
983 waypoint_spawnforitem_force(self, trace_endpos);
984 self.nearestwaypointtimeout = time + 1000000000;
986 if(self.message == "")
987 self.message = "went backwards";
988 if (self.message2 == "")
989 self.message2 = "was pushed backwards by";
990 if (self.race_penalty_reason == "")
991 self.race_penalty_reason = "missing a checkpoint";
993 if(self.classname == "target_startTimer")
994 self.race_checkpoint = 0;
996 self.race_checkpoint = -2;
998 race_timed_checkpoint = 1;
1000 if(self.race_checkpoint == 0)
1001 WaypointSprite_SpawnFixed(WP_RaceStart, o, self, sprite, RADARICON_NONE);
1003 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
1005 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1007 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1010 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1011 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1013 void race_AbandonRaceCheck(entity p)
1015 if(race_completing && !p.race_completed)
1017 p.race_completed = 1;
1018 MAKE_INDEPENDENT_PLAYER(p);
1019 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1020 ClientData_Touch(p);
1024 void race_StartCompleting()
1027 race_completing = 1;
1029 if(p.deadflag != DEAD_NO)
1030 race_AbandonRaceCheck(p);
1033 void race_PreparePlayer()
1035 race_ClearTime(self);
1036 self.race_place = 0;
1037 self.race_started = 0;
1038 self.race_respawn_checkpoint = 0;
1039 self.race_respawn_spotref = world;
1042 void race_RetractPlayer()
1044 if(!g_race && !g_cts)
1046 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1047 race_ClearTime(self);
1048 self.race_checkpoint = self.race_respawn_checkpoint;
1051 spawnfunc(info_player_race)
1053 if(!g_race && !g_cts) { remove(self); return; }
1055 spawnfunc_info_player_deathmatch(this);
1057 if(self.race_place > race_highest_place_spawn)
1058 race_highest_place_spawn = self.race_place;
1059 if(self.race_place < race_lowest_place_spawn)
1060 race_lowest_place_spawn = self.race_place;
1063 void race_ClearRecords()
1067 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1069 race_checkpoint_records[i] = 0;
1070 if(race_checkpoint_recordholders[i])
1071 strunzone(race_checkpoint_recordholders[i]);
1072 race_checkpoint_recordholders[i] = string_null;
1078 float p = e.race_place;
1079 WITH(entity, self, e, race_PreparePlayer());
1084 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1086 if(g_race_qualifying)
1088 pl.race_penalty_accumulator += penalty;
1089 if(IS_REAL_CLIENT(pl))
1092 WRITESPECTATABLE_MSG_ONE({
1093 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1094 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1095 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1096 WriteString(MSG_ONE, reason);
1102 pl.race_penalty = time + penalty;
1103 if(IS_REAL_CLIENT(pl))
1106 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1107 WriteHeader(MSG_ONE, TE_CSQC_RACE);
1108 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1109 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1110 WriteString(MSG_ONE, reason);
1116 void penalty_touch()
1119 if(other.race_lastpenalty != self)
1121 other.race_lastpenalty = self;
1122 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1128 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1131 spawnfunc(trigger_race_penalty)
1133 // TODO: find out why this wasnt done:
1134 //if(!g_cts && !g_race) { remove(self); return; }
1138 self.use = penalty_use;
1139 if (!(self.spawnflags & 1))
1140 self.touch = penalty_touch;
1142 if (self.race_penalty_reason == "")
1143 self.race_penalty_reason = "missing a checkpoint";
1144 if (!self.race_penalty)
1145 self.race_penalty = 5;
1148 float race_GetFractionalLapCount(entity e)
1150 // interesting metrics (idea by KrimZon) to maybe sort players in the
1151 // scoreboard, immediately updates when overtaking
1153 // requires the track to be built so you never get farther away from the
1154 // next checkpoint, though, and current Xonotic race maps are not built that
1157 // also, this code is slow and would need optimization (i.e. "next CP"
1158 // links on CP entities)
1161 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1162 if(e.race_completed)
1163 return l; // not fractional
1166 float bestfraction, fraction;
1167 entity lastcp, cp0, cp1;
1168 float nextcpindex, lastcpindex;
1170 nextcpindex = max(e.race_checkpoint, 0);
1171 lastcpindex = e.race_respawn_checkpoint;
1172 lastcp = e.race_respawn_spotref;
1174 if(nextcpindex == lastcpindex)
1178 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1180 if(cp0.race_checkpoint != lastcpindex)
1185 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1186 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1188 if(cp1.race_checkpoint != nextcpindex)
1190 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1193 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1194 if(fraction < bestfraction)
1195 bestfraction = fraction;
1199 // we are at CP "nextcpindex - bestfraction"
1200 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1201 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1203 nc = race_highest_checkpoint + 1;
1204 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;