4 #include "cl_client.qh"
7 #include "spawnpoints.qh"
8 #include "bot/waypoints.qh"
9 #include "bot/navigation.qh"
10 #include "command/getreplies.qh"
11 #include "../common/deathtypes.qh"
12 #include "../common/notifications.qh"
13 #include "../common/mapinfo.qh"
14 #include "../warpzonelib/util_server.qh"
16 void W_Porto_Fail(float failhard);
18 float race_readTime(string map, float pos)
20 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
22 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
25 string race_readUID(string map, float pos)
27 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
29 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
32 float race_readPos(string map, float t)
35 for (i = 1; i <= RANKINGS_CNT; ++i)
36 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
39 return 0; // pos is zero if unranked
42 void race_writeTime(string map, float t, string myuid)
44 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
47 newpos = race_readPos(map, t);
50 for(i = 1; i <= RANKINGS_CNT; ++i)
52 if(race_readUID(map, i) == myuid)
57 // player improved his existing record, only have to iterate on ranks between new and old recs
58 for (i = prevpos; i > newpos; --i)
60 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
61 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
66 // player has no ranked record yet
67 for (i = RANKINGS_CNT; 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));
74 // store new time itself
75 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
76 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
79 string race_readName(string map, float pos)
81 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
83 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
87 const float MAX_CHECKPOINTS = 255;
89 void spawnfunc_target_checkpoint();
92 .float race_penalty_accumulator;
93 .string race_penalty_reason;
94 .float race_checkpoint; // player: next checkpoint that has to be reached
95 .entity race_lastpenalty;
99 float race_checkpoint_records[MAX_CHECKPOINTS];
100 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
101 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
102 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
103 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
105 float race_highest_checkpoint;
106 float race_timed_checkpoint;
109 float defragcpexists;
111 float race_NextCheckpoint(float f)
113 if(f >= race_highest_checkpoint)
119 float race_PreviousCheckpoint(float f)
124 return race_highest_checkpoint;
130 // 0 = common start/finish
133 float race_CheckpointNetworkID(float f)
135 if(race_timed_checkpoint)
139 else if(f == race_timed_checkpoint)
140 return 255; // finish
145 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
154 cp = e.race_checkpoint;
155 recordtime = race_checkpoint_records[cp];
156 recordholder = race_checkpoint_recordholders[cp];
157 if(recordholder == e.netname)
160 if(!IS_REAL_CLIENT(e))
165 WRITESPECTATABLE_MSG_ONE({
166 WriteByte(MSG_ONE, SVC_TEMPENTITY);
167 WriteByte(MSG_ONE, TE_CSQC_RACE);
170 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
171 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
172 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
175 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
176 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
177 WriteInt24_t(MSG_ONE, recordtime);
178 WriteString(MSG_ONE, recordholder);
182 void race_send_recordtime(float msg)
184 // send the server best time
185 WriteByte(msg, SVC_TEMPENTITY);
186 WriteByte(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 WriteByte(msg, SVC_TEMPENTITY);
196 WriteByte(msg, TE_CSQC_RACE);
197 WriteByte(msg, RACE_NET_SPEED_AWARD);
198 WriteInt24_t(msg, floor(speedaward_speed+0.5));
199 WriteString(msg, speedaward_holder);
202 void race_send_speedaward_alltimebest(float msg)
204 // send the best speed
205 WriteByte(msg, SVC_TEMPENTITY);
206 WriteByte(msg, TE_CSQC_RACE);
207 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
208 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
209 WriteString(msg, speedaward_alltimebest_holder);
212 void race_SendRankings(float pos, float prevpos, float del, float msg)
214 WriteByte(msg, SVC_TEMPENTITY);
215 WriteByte(msg, TE_CSQC_RACE);
216 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
217 WriteShort(msg, pos);
218 WriteShort(msg, prevpos);
219 WriteShort(msg, del);
220 WriteString(msg, race_readName(GetMapname(), pos));
221 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
224 void race_SendStatus(float id, entity e)
226 if(!IS_REAL_CLIENT(e))
235 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
236 WriteByte(msg, SVC_TEMPENTITY);
237 WriteByte(msg, TE_CSQC_RACE);
238 WriteByte(msg, RACE_NET_SERVER_STATUS);
240 WriteString(msg, e.netname);
244 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
246 // netname only used TEMPORARILY for printing
247 float newpos, player_prevpos;
248 newpos = race_readPos(map, t);
252 for(i = 1; i <= RANKINGS_CNT; ++i)
254 if(race_readUID(map, i) == myuid)
259 string oldrec_holder;
260 if (player_prevpos && (player_prevpos < newpos || !newpos))
262 oldrec = race_readTime(GetMapname(), player_prevpos);
263 race_SendStatus(0, e); // "fail"
264 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
269 // no ranking, time worse than the worst ranked
270 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
271 race_SendStatus(0, e); // "fail"
272 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
276 // if we didn't hit a return yet, we have a new record!
278 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
281 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
285 oldrec = race_readTime(GetMapname(), newpos);
286 oldrec_holder = race_readName(GetMapname(), newpos);
289 race_writeTime(GetMapname(), t, myuid);
293 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
294 race_send_recordtime(MSG_ALL);
297 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
299 strunzone(rankings_reply);
300 rankings_reply = strzone(getrankings());
302 if(newpos == player_prevpos)
304 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
305 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
306 else { race_SendStatus(1, e); } // "new time"
310 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
311 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
312 else { race_SendStatus(2, e); } // "new rank"
316 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
317 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
318 else { race_SendStatus(2, e); } // "new rank"
322 void race_deleteTime(string map, float pos)
331 for (i = pos; i <= RANKINGS_CNT; ++i)
333 if (i == RANKINGS_CNT)
335 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
336 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
340 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
341 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
345 race_SendRankings(pos, 0, 1, MSG_ALL);
347 race_send_recordtime(MSG_ALL);
350 strunzone(rankings_reply);
351 rankings_reply = strzone(getrankings());
354 void race_SendTime(entity e, float cp, float t, float tvalid)
359 if(g_race_qualifying)
360 t += e.race_penalty_accumulator;
362 t = TIME_ENCODE(t); // make integer
363 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
366 if(cp == race_timed_checkpoint) // finish line
367 if (!e.race_completed)
370 if(g_race_qualifying)
372 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
374 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
378 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
380 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
382 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
383 snew = TIME_ENCODE(time - game_starttime);
384 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
385 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
387 if(autocvar_fraglimit)
388 if(l >= autocvar_fraglimit)
389 race_StartCompleting();
393 e.race_completed = 1;
394 MAKE_INDEPENDENT_PLAYER(e);
395 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
403 if(g_race_qualifying)
407 recordtime = race_checkpoint_records[cp];
408 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
409 if(recordholder == e.netname)
414 if(cp == race_timed_checkpoint)
416 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
417 if(g_cts && autocvar_g_cts_finish_kill_delay)
422 if(t < recordtime || recordtime == 0)
424 race_checkpoint_records[cp] = t;
425 if(race_checkpoint_recordholders[cp])
426 strunzone(race_checkpoint_recordholders[cp]);
427 race_checkpoint_recordholders[cp] = strzone(e.netname);
428 if(g_race_qualifying)
430 FOR_EACH_REALPLAYER(p)
431 if(p.race_checkpoint == cp)
432 race_SendNextCheckpoint(p, 0);
445 if(IS_REAL_CLIENT(e))
448 if(g_race_qualifying)
450 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
451 WriteByte(MSG_ONE, SVC_TEMPENTITY);
452 WriteByte(MSG_ONE, TE_CSQC_RACE);
453 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
454 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
455 WriteInt24_t(MSG_ONE, t); // time to that intermediate
456 WriteInt24_t(MSG_ONE, recordtime); // previously best time
457 WriteString(MSG_ONE, recordholder); // record holder
462 else // RACE! Not Qualifying
464 float lself, lother, othtime;
466 oth = race_checkpoint_lastplayers[cp];
469 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
470 lother = race_checkpoint_lastlaps[cp];
471 othtime = race_checkpoint_lasttimes[cp];
474 lself = lother = othtime = 0;
476 if(IS_REAL_CLIENT(e))
479 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
480 WriteByte(MSG_ONE, SVC_TEMPENTITY);
481 WriteByte(MSG_ONE, TE_CSQC_RACE);
482 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
483 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
486 WriteInt24_t(MSG_ONE, 0);
487 WriteByte(MSG_ONE, 0);
488 WriteString(MSG_ONE, "");
492 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
493 WriteByte(MSG_ONE, lself - lother);
494 WriteString(MSG_ONE, oth.netname); // record holder
499 race_checkpoint_lastplayers[cp] = e;
500 race_checkpoint_lasttimes[cp] = time;
501 race_checkpoint_lastlaps[cp] = lself;
503 if(IS_REAL_CLIENT(oth))
506 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
507 WriteByte(MSG_ONE, SVC_TEMPENTITY);
508 WriteByte(MSG_ONE, TE_CSQC_RACE);
509 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
510 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
513 WriteInt24_t(MSG_ONE, 0);
514 WriteByte(MSG_ONE, 0);
515 WriteString(MSG_ONE, "");
519 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
520 WriteByte(MSG_ONE, lother - lself);
521 WriteString(MSG_ONE, e.netname); // record holder
528 void race_ClearTime(entity e)
530 e.race_checkpoint = 0;
532 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
533 e.race_penalty_accumulator = 0;
534 e.race_lastpenalty = world;
536 if(!IS_REAL_CLIENT(e))
540 WRITESPECTATABLE_MSG_ONE({
541 WriteByte(MSG_ONE, SVC_TEMPENTITY);
542 WriteByte(MSG_ONE, TE_CSQC_RACE);
543 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
547 void dumpsurface(entity e)
551 LOG_INFO("Surfaces of ", etos(e), ":\n");
553 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
557 n = getsurfacenumpoints(e, si);
560 LOG_INFO(" Surface ", ftos(si), ":\n");
561 norm = getsurfacenormal(e, si);
562 LOG_INFO(" Normal = ", vtos(norm), "\n");
563 for(ni = 0; ni < n; ++ni)
565 vec = getsurfacepoint(e, si, ni);
566 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
571 void checkpoint_passed()
576 if(other.classname == "porto")
578 // do not allow portalling through checkpoints
579 trace_plane_normal = normalize(-1 * other.velocity);
588 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
591 oldmsg = self.message;
594 self.message = oldmsg;
597 if (!IS_PLAYER(other))
601 * Remove unauthorized equipment
603 Portal_ClearAll(other);
605 other.porto_forbidden = 2; // decreased by 1 each StartFrame
609 if(self.race_checkpoint == -2)
611 self.race_checkpoint = other.race_checkpoint;
614 float largest_cp_id = 0;
616 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
619 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
621 largest_cp_id = cp.race_checkpoint;
622 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
623 cp.race_checkpoint = largest_cp_id + 1; // finish line
624 race_highest_checkpoint = largest_cp_id + 1;
625 race_timed_checkpoint = largest_cp_id + 1;
627 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
629 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
636 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
637 cp.race_checkpoint = 1;
638 race_highest_checkpoint = 1;
639 race_timed_checkpoint = 1;
643 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
645 if(self.race_penalty)
647 if(other.race_lastpenalty != self)
649 other.race_lastpenalty = self;
650 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
654 if(other.race_penalty)
660 if(self.spawnflags & 2)
663 oldmsg = self.message;
666 self.message = oldmsg;
669 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
670 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
671 other.race_respawn_checkpoint = self.race_checkpoint;
672 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
673 other.race_started = 1;
675 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
677 if(!self.race_checkpoint) // start line
679 other.race_laptime = time;
680 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
681 other.race_penalty_accumulator = 0;
682 other.race_lastpenalty = world;
685 if(g_race_qualifying)
686 race_SendNextCheckpoint(other, 0);
688 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
691 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
694 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
695 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
700 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
706 if(self.spawnflags & 4)
707 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
711 void checkpoint_touch()
717 void checkpoint_use()
719 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
726 float race_waypointsprite_visible_for_player(entity e)
728 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
730 else if(e.race_checkpoint == self.owner.race_checkpoint)
737 void trigger_race_checkpoint_verify()
747 qual = g_race_qualifying;
750 self.classname = "player";
754 for(i = 0; i <= race_highest_checkpoint; ++i)
756 self.race_checkpoint = race_NextCheckpoint(i);
758 // race only (middle of the race)
759 g_race_qualifying = 0;
761 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
762 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
767 g_race_qualifying = 1;
768 self.race_place = race_lowest_place_spawn;
769 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
770 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
772 // race only (initial spawn)
773 g_race_qualifying = 0;
774 for(p = 1; p <= race_highest_place_spawn; ++p)
777 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
778 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
783 else if(!defrag_ents)
786 self.race_checkpoint = race_NextCheckpoint(0);
787 g_race_qualifying = 1;
788 self.race_place = race_lowest_place_spawn;
789 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
790 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
794 self.race_checkpoint = race_NextCheckpoint(0);
795 g_race_qualifying = 1;
796 self.race_place = 0; // there's only one spawn on defrag maps
798 // check if a defragcp file already exists, then read it and apply the checkpoint order
803 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
806 while((l = fgets(fh)))
808 len = tokenize_console(l);
811 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
814 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
815 if(argv(0) == cp.targetname)
816 cp.race_checkpoint = stof(argv(1));
822 g_race_qualifying = qual;
824 if(race_timed_checkpoint)
828 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
829 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
830 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
831 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
833 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
835 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
839 if(defragcpexists != -1)
841 float largest_cp_id = 0;
842 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
843 if(cp.race_checkpoint > largest_cp_id)
844 largest_cp_id = cp.race_checkpoint;
845 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
846 cp.race_checkpoint = largest_cp_id + 1; // finish line
847 race_highest_checkpoint = largest_cp_id + 1;
848 race_timed_checkpoint = largest_cp_id + 1;
852 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
853 cp.race_checkpoint = 255; // finish line
854 race_highest_checkpoint = 255;
855 race_timed_checkpoint = 255;
860 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
863 if(cp.race_checkpoint == 0)
864 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
865 else if(cp.race_checkpoint == race_timed_checkpoint)
866 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
873 entity trigger, targ;
874 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
875 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
876 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
883 // These just make the game crash on some maps with oddly shaped triggers.
884 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
885 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
886 // (happened on item triggers too)
891 //setsize(targ, trigger.mins, trigger.maxs);
892 //setorigin(targ, trigger.origin);
900 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
902 if(g_race_qualifying)
905 if(self.race_checkpoint != 0)
907 if(spot.race_place != race_lowest_place_spawn)
912 if(self.race_checkpoint != player.race_respawn_checkpoint)
914 // try reusing the previous spawn
915 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
916 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
917 if(self.race_checkpoint == 0)
920 pl = player.race_place;
921 if(pl > race_highest_place_spawn)
923 if(pl == 0 && !player.race_started)
924 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
925 if(spot.race_place != pl)
932 void spawnfunc_trigger_race_checkpoint()
935 if(!g_race && !g_cts) { remove(self); return; }
939 self.use = checkpoint_use;
940 if (!(self.spawnflags & 1))
941 self.touch = checkpoint_touch;
943 o = (self.absmin + self.absmax) * 0.5;
944 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
945 waypoint_spawnforitem_force(self, trace_endpos);
946 self.nearestwaypointtimeout = time + 1000000000;
948 if(self.message == "")
949 self.message = "went backwards";
950 if (self.message2 == "")
951 self.message2 = "was pushed backwards by";
952 if (self.race_penalty_reason == "")
953 self.race_penalty_reason = "missing a checkpoint";
955 self.race_checkpoint = self.cnt;
957 if(self.race_checkpoint > race_highest_checkpoint)
959 race_highest_checkpoint = self.race_checkpoint;
960 if(self.spawnflags & 8)
961 race_timed_checkpoint = self.race_checkpoint;
963 race_timed_checkpoint = 0;
966 if(!self.race_penalty)
968 if(self.race_checkpoint)
969 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
971 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, self, sprite, RADARICON_NONE);
974 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
975 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
977 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
980 void spawnfunc_target_checkpoint() // defrag entity
983 if(!g_race && !g_cts) { remove(self); return; }
988 self.use = checkpoint_use;
989 if (!(self.spawnflags & 1))
990 self.touch = checkpoint_touch;
992 o = (self.absmin + self.absmax) * 0.5;
993 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
994 waypoint_spawnforitem_force(self, trace_endpos);
995 self.nearestwaypointtimeout = time + 1000000000;
997 if(self.message == "")
998 self.message = "went backwards";
999 if (self.message2 == "")
1000 self.message2 = "was pushed backwards by";
1001 if (self.race_penalty_reason == "")
1002 self.race_penalty_reason = "missing a checkpoint";
1004 if(self.classname == "target_startTimer")
1005 self.race_checkpoint = 0;
1007 self.race_checkpoint = -2;
1009 race_timed_checkpoint = 1;
1011 if(self.race_checkpoint == 0)
1012 WaypointSprite_SpawnFixed(WP_RaceStart, o, self, sprite, RADARICON_NONE);
1014 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
1016 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1018 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1021 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
1022 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
1024 void race_AbandonRaceCheck(entity p)
1026 if(race_completing && !p.race_completed)
1028 p.race_completed = 1;
1029 MAKE_INDEPENDENT_PLAYER(p);
1030 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1031 ClientData_Touch(p);
1035 void race_StartCompleting()
1038 race_completing = 1;
1040 if(p.deadflag != DEAD_NO)
1041 race_AbandonRaceCheck(p);
1044 void race_PreparePlayer()
1046 race_ClearTime(self);
1047 self.race_place = 0;
1048 self.race_started = 0;
1049 self.race_respawn_checkpoint = 0;
1050 self.race_respawn_spotref = world;
1053 void race_RetractPlayer()
1055 if(!g_race && !g_cts)
1057 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1058 race_ClearTime(self);
1059 self.race_checkpoint = self.race_respawn_checkpoint;
1062 void spawnfunc_info_player_race (void)
1064 if(!g_race && !g_cts) { remove(self); return; }
1066 spawnfunc_info_player_deathmatch();
1068 if(self.race_place > race_highest_place_spawn)
1069 race_highest_place_spawn = self.race_place;
1070 if(self.race_place < race_lowest_place_spawn)
1071 race_lowest_place_spawn = self.race_place;
1074 void race_ClearRecords()
1078 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1080 race_checkpoint_records[i] = 0;
1081 if(race_checkpoint_recordholders[i])
1082 strunzone(race_checkpoint_recordholders[i]);
1083 race_checkpoint_recordholders[i] = string_null;
1089 float p = e.race_place;
1090 WITH(entity, self, e, race_PreparePlayer());
1095 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1097 if(g_race_qualifying)
1099 pl.race_penalty_accumulator += penalty;
1100 if(IS_REAL_CLIENT(pl))
1103 WRITESPECTATABLE_MSG_ONE({
1104 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1105 WriteByte(MSG_ONE, TE_CSQC_RACE);
1106 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1107 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1108 WriteString(MSG_ONE, reason);
1114 pl.race_penalty = time + penalty;
1115 if(IS_REAL_CLIENT(pl))
1118 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1119 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1120 WriteByte(MSG_ONE, TE_CSQC_RACE);
1121 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1122 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1123 WriteString(MSG_ONE, reason);
1129 void penalty_touch()
1132 if(other.race_lastpenalty != self)
1134 other.race_lastpenalty = self;
1135 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1141 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1144 void spawnfunc_trigger_race_penalty()
1148 self.use = penalty_use;
1149 if (!(self.spawnflags & 1))
1150 self.touch = penalty_touch;
1152 if (self.race_penalty_reason == "")
1153 self.race_penalty_reason = "missing a checkpoint";
1154 if (!self.race_penalty)
1155 self.race_penalty = 5;
1158 float race_GetFractionalLapCount(entity e)
1160 // interesting metrics (idea by KrimZon) to maybe sort players in the
1161 // scoreboard, immediately updates when overtaking
1163 // requires the track to be built so you never get farther away from the
1164 // next checkpoint, though, and current Xonotic race maps are not built that
1167 // also, this code is slow and would need optimization (i.e. "next CP"
1168 // links on CP entities)
1171 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1172 if(e.race_completed)
1173 return l; // not fractional
1176 float bestfraction, fraction;
1177 entity lastcp, cp0, cp1;
1178 float nextcpindex, lastcpindex;
1180 nextcpindex = max(e.race_checkpoint, 0);
1181 lastcpindex = e.race_respawn_checkpoint;
1182 lastcp = e.race_respawn_spotref;
1184 if(nextcpindex == lastcpindex)
1188 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1190 if(cp0.race_checkpoint != lastcpindex)
1195 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1196 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1198 if(cp1.race_checkpoint != nextcpindex)
1200 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1203 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1204 if(fraction < bestfraction)
1205 bestfraction = fraction;
1209 // we are at CP "nextcpindex - bestfraction"
1210 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1211 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1213 nc = race_highest_checkpoint + 1;
1214 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;