4 #include "cl_client.qh"
7 #include "spawnpoints.qh"
8 #include "command/getreplies.qh"
9 #include "../common/deathtypes.qh"
10 #include "../common/notifications.qh"
11 #include "../common/mapinfo.qh"
12 #include "../warpzonelib/util_server.qh"
14 void W_Porto_Fail(float failhard);
16 float race_readTime(string map, float pos)
18 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
20 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
23 string race_readUID(string map, float pos)
25 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
27 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
30 float race_readPos(string map, float t)
33 for (i = 1; i <= RANKINGS_CNT; ++i)
34 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
37 return 0; // pos is zero if unranked
40 void race_writeTime(string map, float t, string myuid)
42 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
45 newpos = race_readPos(map, t);
48 for(i = 1; i <= RANKINGS_CNT; ++i)
50 if(race_readUID(map, i) == myuid)
55 // player improved his existing record, only have to iterate on ranks between new and old recs
56 for (i = prevpos; i > newpos; --i)
58 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
59 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
64 // player has no ranked record yet
65 for (i = RANKINGS_CNT; i > newpos; --i)
67 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
68 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
72 // store new time itself
73 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
74 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
77 string race_readName(string map, float pos)
79 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
81 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
85 const float MAX_CHECKPOINTS = 255;
87 spawnfunc(target_checkpoint);
90 .float race_penalty_accumulator;
91 .string race_penalty_reason;
92 .float race_checkpoint; // player: next checkpoint that has to be reached
93 .entity race_lastpenalty;
97 float race_checkpoint_records[MAX_CHECKPOINTS];
98 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
99 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
100 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
101 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
103 float race_highest_checkpoint;
104 float race_timed_checkpoint;
107 float defragcpexists;
109 float race_NextCheckpoint(float f)
111 if(f >= race_highest_checkpoint)
117 float race_PreviousCheckpoint(float f)
122 return race_highest_checkpoint;
128 // 0 = common start/finish
131 float race_CheckpointNetworkID(float f)
133 if(race_timed_checkpoint)
137 else if(f == race_timed_checkpoint)
138 return 255; // finish
143 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
152 cp = e.race_checkpoint;
153 recordtime = race_checkpoint_records[cp];
154 recordholder = race_checkpoint_recordholders[cp];
155 if(recordholder == e.netname)
158 if(!IS_REAL_CLIENT(e))
163 WRITESPECTATABLE_MSG_ONE({
164 WriteByte(MSG_ONE, SVC_TEMPENTITY);
165 WriteByte(MSG_ONE, TE_CSQC_RACE);
168 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
169 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
170 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
173 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
174 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
175 WriteInt24_t(MSG_ONE, recordtime);
176 WriteString(MSG_ONE, recordholder);
180 void race_send_recordtime(float msg)
182 // send the server best time
183 WriteByte(msg, SVC_TEMPENTITY);
184 WriteByte(msg, TE_CSQC_RACE);
185 WriteByte(msg, RACE_NET_SERVER_RECORD);
186 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
190 void race_send_speedaward(float msg)
192 // send the best speed of the round
193 WriteByte(msg, SVC_TEMPENTITY);
194 WriteByte(msg, TE_CSQC_RACE);
195 WriteByte(msg, RACE_NET_SPEED_AWARD);
196 WriteInt24_t(msg, floor(speedaward_speed+0.5));
197 WriteString(msg, speedaward_holder);
200 void race_send_speedaward_alltimebest(float msg)
202 // send the best speed
203 WriteByte(msg, SVC_TEMPENTITY);
204 WriteByte(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 WriteByte(msg, SVC_TEMPENTITY);
213 WriteByte(msg, TE_CSQC_RACE);
214 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
215 WriteShort(msg, pos);
216 WriteShort(msg, prevpos);
217 WriteShort(msg, del);
218 WriteString(msg, race_readName(GetMapname(), pos));
219 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
222 void race_SendStatus(float id, entity e)
224 if(!IS_REAL_CLIENT(e))
233 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
234 WriteByte(msg, SVC_TEMPENTITY);
235 WriteByte(msg, TE_CSQC_RACE);
236 WriteByte(msg, RACE_NET_SERVER_STATUS);
238 WriteString(msg, e.netname);
242 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
244 // netname only used TEMPORARILY for printing
245 float newpos, player_prevpos;
246 newpos = race_readPos(map, t);
250 for(i = 1; i <= RANKINGS_CNT; ++i)
252 if(race_readUID(map, i) == myuid)
257 string oldrec_holder;
258 if (player_prevpos && (player_prevpos < newpos || !newpos))
260 oldrec = race_readTime(GetMapname(), player_prevpos);
261 race_SendStatus(0, e); // "fail"
262 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
267 // no ranking, time worse than the worst ranked
268 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
269 race_SendStatus(0, e); // "fail"
270 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
274 // if we didn't hit a return yet, we have a new record!
276 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
279 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
283 oldrec = race_readTime(GetMapname(), newpos);
284 oldrec_holder = race_readName(GetMapname(), newpos);
287 race_writeTime(GetMapname(), t, myuid);
291 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
292 race_send_recordtime(MSG_ALL);
295 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
297 strunzone(rankings_reply);
298 rankings_reply = strzone(getrankings());
300 if(newpos == player_prevpos)
302 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
303 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
304 else { race_SendStatus(1, e); } // "new time"
308 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
309 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
310 else { race_SendStatus(2, e); } // "new rank"
314 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
315 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
316 else { race_SendStatus(2, e); } // "new rank"
320 void race_deleteTime(string map, float pos)
329 for (i = pos; i <= RANKINGS_CNT; ++i)
331 if (i == RANKINGS_CNT)
333 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
334 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
338 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
339 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
343 race_SendRankings(pos, 0, 1, MSG_ALL);
345 race_send_recordtime(MSG_ALL);
348 strunzone(rankings_reply);
349 rankings_reply = strzone(getrankings());
352 void race_SendTime(entity e, float cp, float t, float tvalid)
357 if(g_race_qualifying)
358 t += e.race_penalty_accumulator;
360 t = TIME_ENCODE(t); // make integer
361 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
364 if(cp == race_timed_checkpoint) // finish line
365 if (!e.race_completed)
368 if(g_race_qualifying)
370 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
372 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
376 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
378 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
380 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
381 snew = TIME_ENCODE(time - game_starttime);
382 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
383 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
385 if(autocvar_fraglimit)
386 if(l >= autocvar_fraglimit)
387 race_StartCompleting();
391 e.race_completed = 1;
392 MAKE_INDEPENDENT_PLAYER(e);
393 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
401 if(g_race_qualifying)
405 recordtime = race_checkpoint_records[cp];
406 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
407 if(recordholder == e.netname)
412 if(cp == race_timed_checkpoint)
414 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
415 if(g_cts && autocvar_g_cts_finish_kill_delay)
420 if(t < recordtime || recordtime == 0)
422 race_checkpoint_records[cp] = t;
423 if(race_checkpoint_recordholders[cp])
424 strunzone(race_checkpoint_recordholders[cp]);
425 race_checkpoint_recordholders[cp] = strzone(e.netname);
426 if(g_race_qualifying)
428 FOR_EACH_REALPLAYER(p)
429 if(p.race_checkpoint == cp)
430 race_SendNextCheckpoint(p, 0);
443 if(IS_REAL_CLIENT(e))
446 if(g_race_qualifying)
448 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
449 WriteByte(MSG_ONE, SVC_TEMPENTITY);
450 WriteByte(MSG_ONE, TE_CSQC_RACE);
451 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
452 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
453 WriteInt24_t(MSG_ONE, t); // time to that intermediate
454 WriteInt24_t(MSG_ONE, recordtime); // previously best time
455 WriteString(MSG_ONE, recordholder); // record holder
460 else // RACE! Not Qualifying
462 float lself, lother, othtime;
464 oth = race_checkpoint_lastplayers[cp];
467 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
468 lother = race_checkpoint_lastlaps[cp];
469 othtime = race_checkpoint_lasttimes[cp];
472 lself = lother = othtime = 0;
474 if(IS_REAL_CLIENT(e))
477 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
478 WriteByte(MSG_ONE, SVC_TEMPENTITY);
479 WriteByte(MSG_ONE, TE_CSQC_RACE);
480 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
481 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
484 WriteInt24_t(MSG_ONE, 0);
485 WriteByte(MSG_ONE, 0);
486 WriteString(MSG_ONE, "");
490 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
491 WriteByte(MSG_ONE, lself - lother);
492 WriteString(MSG_ONE, oth.netname); // record holder
497 race_checkpoint_lastplayers[cp] = e;
498 race_checkpoint_lasttimes[cp] = time;
499 race_checkpoint_lastlaps[cp] = lself;
501 if(IS_REAL_CLIENT(oth))
504 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
505 WriteByte(MSG_ONE, SVC_TEMPENTITY);
506 WriteByte(MSG_ONE, TE_CSQC_RACE);
507 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
508 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
511 WriteInt24_t(MSG_ONE, 0);
512 WriteByte(MSG_ONE, 0);
513 WriteString(MSG_ONE, "");
517 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
518 WriteByte(MSG_ONE, lother - lself);
519 WriteString(MSG_ONE, e.netname); // record holder
526 void race_ClearTime(entity e)
528 e.race_checkpoint = 0;
530 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
531 e.race_penalty_accumulator = 0;
532 e.race_lastpenalty = world;
534 if(!IS_REAL_CLIENT(e))
538 WRITESPECTATABLE_MSG_ONE({
539 WriteByte(MSG_ONE, SVC_TEMPENTITY);
540 WriteByte(MSG_ONE, TE_CSQC_RACE);
541 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
545 void dumpsurface(entity e)
549 LOG_INFO("Surfaces of ", etos(e), ":\n");
551 LOG_INFO("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
555 n = getsurfacenumpoints(e, si);
558 LOG_INFO(" Surface ", ftos(si), ":\n");
559 norm = getsurfacenormal(e, si);
560 LOG_INFO(" Normal = ", vtos(norm), "\n");
561 for(ni = 0; ni < n; ++ni)
563 vec = getsurfacepoint(e, si, ni);
564 LOG_INFO(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
569 void checkpoint_passed()
574 if(other.classname == "porto")
576 // do not allow portalling through checkpoints
577 trace_plane_normal = normalize(-1 * other.velocity);
586 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
589 oldmsg = self.message;
592 self.message = oldmsg;
595 if (!IS_PLAYER(other))
599 * Remove unauthorized equipment
601 Portal_ClearAll(other);
603 other.porto_forbidden = 2; // decreased by 1 each StartFrame
607 if(self.race_checkpoint == -2)
609 self.race_checkpoint = other.race_checkpoint;
612 float largest_cp_id = 0;
614 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
617 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
619 largest_cp_id = cp.race_checkpoint;
620 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
621 cp.race_checkpoint = largest_cp_id + 1; // finish line
622 race_highest_checkpoint = largest_cp_id + 1;
623 race_timed_checkpoint = largest_cp_id + 1;
625 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
627 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
634 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
635 cp.race_checkpoint = 1;
636 race_highest_checkpoint = 1;
637 race_timed_checkpoint = 1;
641 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
643 if(self.race_penalty)
645 if(other.race_lastpenalty != self)
647 other.race_lastpenalty = self;
648 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
652 if(other.race_penalty)
658 if(self.spawnflags & 2)
661 oldmsg = self.message;
664 self.message = oldmsg;
667 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
668 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
669 other.race_respawn_checkpoint = self.race_checkpoint;
670 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
671 other.race_started = 1;
673 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
675 if(!self.race_checkpoint) // start line
677 other.race_laptime = time;
678 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
679 other.race_penalty_accumulator = 0;
680 other.race_lastpenalty = world;
683 if(g_race_qualifying)
684 race_SendNextCheckpoint(other, 0);
686 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
689 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
692 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
693 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
698 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
704 if(self.spawnflags & 4)
705 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
709 void checkpoint_touch()
715 void checkpoint_use()
717 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
724 float race_waypointsprite_visible_for_player(entity e)
726 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
728 else if(e.race_checkpoint == self.owner.race_checkpoint)
735 void trigger_race_checkpoint_verify()
745 qual = g_race_qualifying;
748 self.classname = "player";
752 for(i = 0; i <= race_highest_checkpoint; ++i)
754 self.race_checkpoint = race_NextCheckpoint(i);
756 // race only (middle of the race)
757 g_race_qualifying = 0;
759 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
760 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
765 g_race_qualifying = 1;
766 self.race_place = race_lowest_place_spawn;
767 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
768 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
770 // race only (initial spawn)
771 g_race_qualifying = 0;
772 for(p = 1; p <= race_highest_place_spawn; ++p)
775 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
776 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
781 else if(!defrag_ents)
784 self.race_checkpoint = race_NextCheckpoint(0);
785 g_race_qualifying = 1;
786 self.race_place = race_lowest_place_spawn;
787 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
788 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
792 self.race_checkpoint = race_NextCheckpoint(0);
793 g_race_qualifying = 1;
794 self.race_place = 0; // there's only one spawn on defrag maps
796 // check if a defragcp file already exists, then read it and apply the checkpoint order
801 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
804 while((l = fgets(fh)))
806 len = tokenize_console(l);
809 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
812 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
813 if(argv(0) == cp.targetname)
814 cp.race_checkpoint = stof(argv(1));
820 g_race_qualifying = qual;
822 if(race_timed_checkpoint)
826 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
827 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
828 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
829 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
831 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
833 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
837 if(defragcpexists != -1)
839 float largest_cp_id = 0;
840 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
841 if(cp.race_checkpoint > largest_cp_id)
842 largest_cp_id = cp.race_checkpoint;
843 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
844 cp.race_checkpoint = largest_cp_id + 1; // finish line
845 race_highest_checkpoint = largest_cp_id + 1;
846 race_timed_checkpoint = largest_cp_id + 1;
850 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
851 cp.race_checkpoint = 255; // finish line
852 race_highest_checkpoint = 255;
853 race_timed_checkpoint = 255;
858 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
861 if(cp.race_checkpoint == 0)
862 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceStart, WP_Null, WP_Null);
863 else if(cp.race_checkpoint == race_timed_checkpoint)
864 WaypointSprite_UpdateSprites(cp.sprite, WP_RaceFinish, WP_Null, WP_Null);
871 entity trigger, targ;
872 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
873 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
874 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
881 // These just make the game crash on some maps with oddly shaped triggers.
882 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
883 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
884 // (happened on item triggers too)
889 //setsize(targ, trigger.mins, trigger.maxs);
890 //setorigin(targ, trigger.origin);
898 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
900 if(g_race_qualifying)
903 if(self.race_checkpoint != 0)
905 if(spot.race_place != race_lowest_place_spawn)
910 if(self.race_checkpoint != player.race_respawn_checkpoint)
912 // try reusing the previous spawn
913 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
914 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
915 if(self.race_checkpoint == 0)
918 pl = player.race_place;
919 if(pl > race_highest_place_spawn)
921 if(pl == 0 && !player.race_started)
922 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
923 if(spot.race_place != pl)
930 spawnfunc(trigger_race_checkpoint)
933 if(!g_race && !g_cts) { remove(self); return; }
937 self.use = checkpoint_use;
938 if (!(self.spawnflags & 1))
939 self.touch = checkpoint_touch;
941 o = (self.absmin + self.absmax) * 0.5;
942 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
943 waypoint_spawnforitem_force(self, trace_endpos);
944 self.nearestwaypointtimeout = time + 1000000000;
946 if(self.message == "")
947 self.message = "went backwards";
948 if (self.message2 == "")
949 self.message2 = "was pushed backwards by";
950 if (self.race_penalty_reason == "")
951 self.race_penalty_reason = "missing a checkpoint";
953 self.race_checkpoint = self.cnt;
955 if(self.race_checkpoint > race_highest_checkpoint)
957 race_highest_checkpoint = self.race_checkpoint;
958 if(self.spawnflags & 8)
959 race_timed_checkpoint = self.race_checkpoint;
961 race_timed_checkpoint = 0;
964 if(!self.race_penalty)
966 if(self.race_checkpoint)
967 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
969 WaypointSprite_SpawnFixed(WP_RaceStartFinish, o, self, sprite, RADARICON_NONE);
972 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
973 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
975 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
978 spawnfunc(target_checkpoint) // defrag entity
981 if(!g_race && !g_cts) { remove(self); return; }
986 self.use = checkpoint_use;
987 if (!(self.spawnflags & 1))
988 self.touch = checkpoint_touch;
990 o = (self.absmin + self.absmax) * 0.5;
991 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
992 waypoint_spawnforitem_force(self, trace_endpos);
993 self.nearestwaypointtimeout = time + 1000000000;
995 if(self.message == "")
996 self.message = "went backwards";
997 if (self.message2 == "")
998 self.message2 = "was pushed backwards by";
999 if (self.race_penalty_reason == "")
1000 self.race_penalty_reason = "missing a checkpoint";
1002 if(self.classname == "target_startTimer")
1003 self.race_checkpoint = 0;
1005 self.race_checkpoint = -2;
1007 race_timed_checkpoint = 1;
1009 if(self.race_checkpoint == 0)
1010 WaypointSprite_SpawnFixed(WP_RaceStart, o, self, sprite, RADARICON_NONE);
1012 WaypointSprite_SpawnFixed(WP_RaceCheckpoint, o, self, sprite, RADARICON_NONE);
1014 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1016 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1019 spawnfunc(target_startTimer) { spawnfunc_target_checkpoint(this); }
1020 spawnfunc(target_stopTimer) { spawnfunc_target_checkpoint(this); }
1022 void race_AbandonRaceCheck(entity p)
1024 if(race_completing && !p.race_completed)
1026 p.race_completed = 1;
1027 MAKE_INDEPENDENT_PLAYER(p);
1028 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1029 ClientData_Touch(p);
1033 void race_StartCompleting()
1036 race_completing = 1;
1038 if(p.deadflag != DEAD_NO)
1039 race_AbandonRaceCheck(p);
1042 void race_PreparePlayer()
1044 race_ClearTime(self);
1045 self.race_place = 0;
1046 self.race_started = 0;
1047 self.race_respawn_checkpoint = 0;
1048 self.race_respawn_spotref = world;
1051 void race_RetractPlayer()
1053 if(!g_race && !g_cts)
1055 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1056 race_ClearTime(self);
1057 self.race_checkpoint = self.race_respawn_checkpoint;
1060 spawnfunc(info_player_race)
1062 if(!g_race && !g_cts) { remove(self); return; }
1064 spawnfunc_info_player_deathmatch(this);
1066 if(self.race_place > race_highest_place_spawn)
1067 race_highest_place_spawn = self.race_place;
1068 if(self.race_place < race_lowest_place_spawn)
1069 race_lowest_place_spawn = self.race_place;
1072 void race_ClearRecords()
1076 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1078 race_checkpoint_records[i] = 0;
1079 if(race_checkpoint_recordholders[i])
1080 strunzone(race_checkpoint_recordholders[i]);
1081 race_checkpoint_recordholders[i] = string_null;
1087 float p = e.race_place;
1088 WITH(entity, self, e, race_PreparePlayer());
1093 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1095 if(g_race_qualifying)
1097 pl.race_penalty_accumulator += penalty;
1098 if(IS_REAL_CLIENT(pl))
1101 WRITESPECTATABLE_MSG_ONE({
1102 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1103 WriteByte(MSG_ONE, TE_CSQC_RACE);
1104 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1105 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1106 WriteString(MSG_ONE, reason);
1112 pl.race_penalty = time + penalty;
1113 if(IS_REAL_CLIENT(pl))
1116 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1117 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1118 WriteByte(MSG_ONE, TE_CSQC_RACE);
1119 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1120 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1121 WriteString(MSG_ONE, reason);
1127 void penalty_touch()
1130 if(other.race_lastpenalty != self)
1132 other.race_lastpenalty = self;
1133 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1139 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1142 spawnfunc(trigger_race_penalty)
1146 self.use = penalty_use;
1147 if (!(self.spawnflags & 1))
1148 self.touch = penalty_touch;
1150 if (self.race_penalty_reason == "")
1151 self.race_penalty_reason = "missing a checkpoint";
1152 if (!self.race_penalty)
1153 self.race_penalty = 5;
1156 float race_GetFractionalLapCount(entity e)
1158 // interesting metrics (idea by KrimZon) to maybe sort players in the
1159 // scoreboard, immediately updates when overtaking
1161 // requires the track to be built so you never get farther away from the
1162 // next checkpoint, though, and current Xonotic race maps are not built that
1165 // also, this code is slow and would need optimization (i.e. "next CP"
1166 // links on CP entities)
1169 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1170 if(e.race_completed)
1171 return l; // not fractional
1174 float bestfraction, fraction;
1175 entity lastcp, cp0, cp1;
1176 float nextcpindex, lastcpindex;
1178 nextcpindex = max(e.race_checkpoint, 0);
1179 lastcpindex = e.race_respawn_checkpoint;
1180 lastcp = e.race_respawn_spotref;
1182 if(nextcpindex == lastcpindex)
1186 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1188 if(cp0.race_checkpoint != lastcpindex)
1193 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1194 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1196 if(cp1.race_checkpoint != nextcpindex)
1198 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1201 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1202 if(fraction < bestfraction)
1203 bestfraction = fraction;
1207 // we are at CP "nextcpindex - bestfraction"
1208 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1209 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1211 nc = race_highest_checkpoint + 1;
1212 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;