4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
6 #include "../warpzonelib/util_server.qh"
7 #include "../common/constants.qh"
8 #include "../common/util.qh"
9 #include "autocvars.qh"
11 #include "../common/notifications.qh"
12 #include "../common/deathtypes.qh"
13 #include "mutators/mutators_include.qh"
14 #include "../common/mapinfo.qh"
15 #include "command/sv_cmd.qh"
18 #include "spawnpoints.qh"
22 float race_readTime(string map, float pos)
24 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
26 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
29 string race_readUID(string map, float pos)
31 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
33 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
36 float race_readPos(string map, float t)
39 for (i = 1; i <= RANKINGS_CNT; ++i)
40 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
43 return 0; // pos is zero if unranked
46 void race_writeTime(string map, float t, string myuid)
48 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
51 newpos = race_readPos(map, t);
54 for(i = 1; i <= RANKINGS_CNT; ++i)
56 if(race_readUID(map, i) == myuid)
61 // player improved his existing record, only have to iterate on ranks between new and old recs
62 for (i = prevpos; i > newpos; --i)
64 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
65 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
70 // player has no ranked record yet
71 for (i = RANKINGS_CNT; i > newpos; --i)
73 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
74 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
78 // store new time itself
79 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
80 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
83 string race_readName(string map, float pos)
85 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
87 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
91 const float MAX_CHECKPOINTS = 255;
93 void spawnfunc_target_checkpoint();
96 .float race_penalty_accumulator;
97 .string race_penalty_reason;
98 .float race_checkpoint; // player: next checkpoint that has to be reached
100 .entity race_lastpenalty;
104 float race_checkpoint_records[MAX_CHECKPOINTS];
105 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
106 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
107 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
108 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
110 float race_highest_checkpoint;
111 float race_timed_checkpoint;
114 float defragcpexists;
116 float race_NextCheckpoint(float f)
118 if(f >= race_highest_checkpoint)
124 float race_PreviousCheckpoint(float f)
129 return race_highest_checkpoint;
135 // 0 = common start/finish
138 float race_CheckpointNetworkID(float f)
140 if(race_timed_checkpoint)
144 else if(f == race_timed_checkpoint)
145 return 255; // finish
150 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
159 cp = e.race_checkpoint;
160 recordtime = race_checkpoint_records[cp];
161 recordholder = race_checkpoint_recordholders[cp];
162 if(recordholder == e.netname)
165 if(!IS_REAL_CLIENT(e))
170 WRITESPECTATABLE_MSG_ONE({
171 WriteByte(MSG_ONE, SVC_TEMPENTITY);
172 WriteByte(MSG_ONE, TE_CSQC_RACE);
175 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
176 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
177 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
180 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
181 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
182 WriteInt24_t(MSG_ONE, recordtime);
183 WriteString(MSG_ONE, recordholder);
187 void race_send_recordtime(float msg)
189 // send the server best time
190 WriteByte(msg, SVC_TEMPENTITY);
191 WriteByte(msg, TE_CSQC_RACE);
192 WriteByte(msg, RACE_NET_SERVER_RECORD);
193 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
196 void race_SendRankings(float pos, float prevpos, float del, float msg)
198 WriteByte(msg, SVC_TEMPENTITY);
199 WriteByte(msg, TE_CSQC_RACE);
200 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
201 WriteShort(msg, pos);
202 WriteShort(msg, prevpos);
203 WriteShort(msg, del);
204 WriteString(msg, race_readName(GetMapname(), pos));
205 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
208 void race_SendStatus(float id, entity e)
210 if(!IS_REAL_CLIENT(e))
219 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
220 WriteByte(msg, SVC_TEMPENTITY);
221 WriteByte(msg, TE_CSQC_RACE);
222 WriteByte(msg, RACE_NET_SERVER_STATUS);
224 WriteString(msg, e.netname);
228 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
230 // netname only used TEMPORARILY for printing
231 float newpos, player_prevpos;
232 newpos = race_readPos(map, t);
236 for(i = 1; i <= RANKINGS_CNT; ++i)
238 if(race_readUID(map, i) == myuid)
243 string oldrec_holder;
244 if (player_prevpos && (player_prevpos < newpos || !newpos))
246 oldrec = race_readTime(GetMapname(), player_prevpos);
247 race_SendStatus(0, e); // "fail"
248 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
253 // no ranking, time worse than the worst ranked
254 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
255 race_SendStatus(0, e); // "fail"
256 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
260 // if we didn't hit a return yet, we have a new record!
262 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
265 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
269 oldrec = race_readTime(GetMapname(), newpos);
270 oldrec_holder = race_readName(GetMapname(), newpos);
273 race_writeTime(GetMapname(), t, myuid);
277 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
278 race_send_recordtime(MSG_ALL);
281 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
283 strunzone(rankings_reply);
284 rankings_reply = strzone(getrankings());
286 if(newpos == player_prevpos)
288 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
289 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
290 else { race_SendStatus(1, e); } // "new time"
294 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
295 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
296 else { race_SendStatus(2, e); } // "new rank"
300 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
301 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
302 else { race_SendStatus(2, e); } // "new rank"
306 void race_deleteTime(string map, float pos)
315 for (i = pos; i <= RANKINGS_CNT; ++i)
317 if (i == RANKINGS_CNT)
319 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
320 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
324 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
325 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
329 race_SendRankings(pos, 0, 1, MSG_ALL);
331 race_send_recordtime(MSG_ALL);
334 strunzone(rankings_reply);
335 rankings_reply = strzone(getrankings());
338 void race_SendTime(entity e, float cp, float t, float tvalid)
343 if(g_race_qualifying)
344 t += e.race_penalty_accumulator;
346 t = TIME_ENCODE(t); // make integer
347 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
350 if(cp == race_timed_checkpoint) // finish line
351 if (!e.race_completed)
354 if(g_race_qualifying)
356 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
358 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
362 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
364 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
366 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
367 snew = TIME_ENCODE(time - game_starttime);
368 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
369 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
371 if(autocvar_fraglimit)
372 if(l >= autocvar_fraglimit)
373 race_StartCompleting();
377 e.race_completed = 1;
378 MAKE_INDEPENDENT_PLAYER(e);
379 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
387 if(g_race_qualifying)
391 recordtime = race_checkpoint_records[cp];
392 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
393 if(recordholder == e.netname)
398 if(cp == race_timed_checkpoint)
400 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
401 if(g_cts && autocvar_g_cts_finish_kill_delay)
406 if(t < recordtime || recordtime == 0)
408 race_checkpoint_records[cp] = t;
409 if(race_checkpoint_recordholders[cp])
410 strunzone(race_checkpoint_recordholders[cp]);
411 race_checkpoint_recordholders[cp] = strzone(e.netname);
412 if(g_race_qualifying)
414 FOR_EACH_REALPLAYER(p)
415 if(p.race_checkpoint == cp)
416 race_SendNextCheckpoint(p, 0);
429 if(IS_REAL_CLIENT(e))
432 if(g_race_qualifying)
434 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
435 WriteByte(MSG_ONE, SVC_TEMPENTITY);
436 WriteByte(MSG_ONE, TE_CSQC_RACE);
437 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
438 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
439 WriteInt24_t(MSG_ONE, t); // time to that intermediate
440 WriteInt24_t(MSG_ONE, recordtime); // previously best time
441 WriteString(MSG_ONE, recordholder); // record holder
446 else // RACE! Not Qualifying
448 float lself, lother, othtime;
450 oth = race_checkpoint_lastplayers[cp];
453 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
454 lother = race_checkpoint_lastlaps[cp];
455 othtime = race_checkpoint_lasttimes[cp];
458 lself = lother = othtime = 0;
460 if(IS_REAL_CLIENT(e))
463 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
464 WriteByte(MSG_ONE, SVC_TEMPENTITY);
465 WriteByte(MSG_ONE, TE_CSQC_RACE);
466 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
467 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
470 WriteInt24_t(MSG_ONE, 0);
471 WriteByte(MSG_ONE, 0);
472 WriteString(MSG_ONE, "");
476 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
477 WriteByte(MSG_ONE, lself - lother);
478 WriteString(MSG_ONE, oth.netname); // record holder
483 race_checkpoint_lastplayers[cp] = e;
484 race_checkpoint_lasttimes[cp] = time;
485 race_checkpoint_lastlaps[cp] = lself;
487 if(IS_REAL_CLIENT(oth))
490 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
491 WriteByte(MSG_ONE, SVC_TEMPENTITY);
492 WriteByte(MSG_ONE, TE_CSQC_RACE);
493 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
494 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
497 WriteInt24_t(MSG_ONE, 0);
498 WriteByte(MSG_ONE, 0);
499 WriteString(MSG_ONE, "");
503 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
504 WriteByte(MSG_ONE, lother - lself);
505 WriteString(MSG_ONE, e.netname); // record holder
512 void race_ClearTime(entity e)
514 e.race_checkpoint = 0;
516 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
517 e.race_penalty_accumulator = 0;
518 e.race_lastpenalty = world;
520 if(!IS_REAL_CLIENT(e))
524 WRITESPECTATABLE_MSG_ONE({
525 WriteByte(MSG_ONE, SVC_TEMPENTITY);
526 WriteByte(MSG_ONE, TE_CSQC_RACE);
527 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
531 void dumpsurface(entity e)
535 print("Surfaces of ", etos(e), ":\n");
537 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
541 n = getsurfacenumpoints(e, si);
544 print(" Surface ", ftos(si), ":\n");
545 norm = getsurfacenormal(e, si);
546 print(" Normal = ", vtos(norm), "\n");
547 for(ni = 0; ni < n; ++ni)
549 vec = getsurfacepoint(e, si, ni);
550 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
555 void checkpoint_passed()
560 if(other.classname == "porto")
562 // do not allow portalling through checkpoints
563 trace_plane_normal = normalize(-1 * other.velocity);
572 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
575 oldmsg = self.message;
578 self.message = oldmsg;
581 if (!IS_PLAYER(other))
585 * Remove unauthorized equipment
587 Portal_ClearAll(other);
589 other.porto_forbidden = 2; // decreased by 1 each StartFrame
593 if(self.race_checkpoint == -2)
595 self.race_checkpoint = other.race_checkpoint;
598 float largest_cp_id = 0;
600 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
603 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
605 largest_cp_id = cp.race_checkpoint;
606 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
607 cp.race_checkpoint = largest_cp_id + 1; // finish line
608 race_highest_checkpoint = largest_cp_id + 1;
609 race_timed_checkpoint = largest_cp_id + 1;
611 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
613 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
620 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
621 cp.race_checkpoint = 1;
622 race_highest_checkpoint = 1;
623 race_timed_checkpoint = 1;
627 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
629 if(self.race_penalty)
631 if(other.race_lastpenalty != self)
633 other.race_lastpenalty = self;
634 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
638 if(other.race_penalty)
644 if(self.spawnflags & 2)
647 oldmsg = self.message;
650 self.message = oldmsg;
653 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
654 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
655 other.race_respawn_checkpoint = self.race_checkpoint;
656 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
657 other.race_started = 1;
659 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
661 if(!self.race_checkpoint) // start line
663 other.race_laptime = time;
664 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
665 other.race_penalty_accumulator = 0;
666 other.race_lastpenalty = world;
669 if(g_race_qualifying)
670 race_SendNextCheckpoint(other, 0);
672 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
675 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
678 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
679 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
684 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
690 if(self.spawnflags & 4)
691 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
695 void checkpoint_touch()
701 void checkpoint_use()
703 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
710 float race_waypointsprite_visible_for_player(entity e)
712 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
714 else if(e.race_checkpoint == self.owner.race_checkpoint)
721 void trigger_race_checkpoint_verify()
731 qual = g_race_qualifying;
735 self.classname = "player";
739 for(i = 0; i <= race_highest_checkpoint; ++i)
741 self.race_checkpoint = race_NextCheckpoint(i);
743 // race only (middle of the race)
744 g_race_qualifying = 0;
746 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
747 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
752 g_race_qualifying = 1;
753 self.race_place = race_lowest_place_spawn;
754 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
755 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
757 // race only (initial spawn)
758 g_race_qualifying = 0;
759 for(p = 1; p <= race_highest_place_spawn; ++p)
762 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
763 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
768 else if(!defrag_ents)
771 self.race_checkpoint = race_NextCheckpoint(0);
772 g_race_qualifying = 1;
773 self.race_place = race_lowest_place_spawn;
774 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
775 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
779 self.race_checkpoint = race_NextCheckpoint(0);
780 g_race_qualifying = 1;
781 self.race_place = 0; // there's only one spawn on defrag maps
783 // check if a defragcp file already exists, then read it and apply the checkpoint order
788 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
791 while((l = fgets(fh)))
793 len = tokenize_console(l);
796 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
799 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
800 if(argv(0) == cp.targetname)
801 cp.race_checkpoint = stof(argv(1));
807 g_race_qualifying = qual;
809 if(race_timed_checkpoint)
813 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
814 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
815 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
816 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
818 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
820 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
824 if(defragcpexists != -1)
826 float largest_cp_id = 0;
827 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
828 if(cp.race_checkpoint > largest_cp_id)
829 largest_cp_id = cp.race_checkpoint;
830 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
831 cp.race_checkpoint = largest_cp_id + 1; // finish line
832 race_highest_checkpoint = largest_cp_id + 1;
833 race_timed_checkpoint = largest_cp_id + 1;
837 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
838 cp.race_checkpoint = 255; // finish line
839 race_highest_checkpoint = 255;
840 race_timed_checkpoint = 255;
845 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
848 if(cp.race_checkpoint == 0)
849 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
850 else if(cp.race_checkpoint == race_timed_checkpoint)
851 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
858 entity trigger, targ;
859 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
860 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
861 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
868 // These just make the game crash on some maps with oddly shaped triggers.
869 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
870 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
871 // (happened on item triggers too)
876 //setsize(targ, trigger.mins, trigger.maxs);
877 //setorigin(targ, trigger.origin);
885 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
887 if(g_race_qualifying)
890 if(self.race_checkpoint != 0)
892 if(spot.race_place != race_lowest_place_spawn)
897 if(self.race_checkpoint != player.race_respawn_checkpoint)
899 // try reusing the previous spawn
900 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
901 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
902 if(self.race_checkpoint == 0)
905 pl = player.race_place;
906 if(pl > race_highest_place_spawn)
908 if(pl == 0 && !player.race_started)
909 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
910 if(spot.race_place != pl)
917 void spawnfunc_trigger_race_checkpoint()
920 if(!g_race && !g_cts) { remove(self); return; }
924 self.use = checkpoint_use;
925 if (!(self.spawnflags & 1))
926 self.touch = checkpoint_touch;
928 o = (self.absmin + self.absmax) * 0.5;
929 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
930 waypoint_spawnforitem_force(self, trace_endpos);
931 self.nearestwaypointtimeout = time + 1000000000;
933 if(self.message == "")
934 self.message = "went backwards";
935 if (self.message2 == "")
936 self.message2 = "was pushed backwards by";
937 if (self.race_penalty_reason == "")
938 self.race_penalty_reason = "missing a checkpoint";
940 self.race_checkpoint = self.cnt;
942 if(self.race_checkpoint > race_highest_checkpoint)
944 race_highest_checkpoint = self.race_checkpoint;
945 if(self.spawnflags & 8)
946 race_timed_checkpoint = self.race_checkpoint;
948 race_timed_checkpoint = 0;
951 if(!self.race_penalty)
953 if(self.race_checkpoint)
954 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
956 WaypointSprite_SpawnFixed("race-start-finish", o, self, sprite, RADARICON_NONE, '1 0.5 0');
959 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
960 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
962 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
965 void spawnfunc_target_checkpoint() // defrag entity
968 if(!g_race && !g_cts) { remove(self); return; }
973 self.use = checkpoint_use;
974 if (!(self.spawnflags & 1))
975 self.touch = checkpoint_touch;
977 o = (self.absmin + self.absmax) * 0.5;
978 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
979 waypoint_spawnforitem_force(self, trace_endpos);
980 self.nearestwaypointtimeout = time + 1000000000;
982 if(self.message == "")
983 self.message = "went backwards";
984 if (self.message2 == "")
985 self.message2 = "was pushed backwards by";
986 if (self.race_penalty_reason == "")
987 self.race_penalty_reason = "missing a checkpoint";
989 if(self.classname == "target_startTimer")
990 self.race_checkpoint = 0;
992 self.race_checkpoint = -2;
994 race_timed_checkpoint = 1;
996 if(self.race_checkpoint == 0)
997 WaypointSprite_SpawnFixed("race-start", o, self, sprite, RADARICON_NONE, '1 0.5 0');
999 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
1001 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1003 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1006 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
1007 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
1009 void race_AbandonRaceCheck(entity p)
1011 if(race_completing && !p.race_completed)
1013 p.race_completed = 1;
1014 MAKE_INDEPENDENT_PLAYER(p);
1015 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1016 ClientData_Touch(p);
1020 void race_StartCompleting()
1023 race_completing = 1;
1025 if(p.deadflag != DEAD_NO)
1026 race_AbandonRaceCheck(p);
1029 void race_PreparePlayer()
1031 race_ClearTime(self);
1032 self.race_place = 0;
1033 self.race_started = 0;
1034 self.race_respawn_checkpoint = 0;
1035 self.race_respawn_spotref = world;
1038 void race_RetractPlayer()
1040 if(!g_race && !g_cts)
1042 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1043 race_ClearTime(self);
1044 self.race_checkpoint = self.race_respawn_checkpoint;
1047 void spawnfunc_info_player_race (void)
1049 if(!g_race && !g_cts) { remove(self); return; }
1051 spawnfunc_info_player_deathmatch();
1053 if(self.race_place > race_highest_place_spawn)
1054 race_highest_place_spawn = self.race_place;
1055 if(self.race_place < race_lowest_place_spawn)
1056 race_lowest_place_spawn = self.race_place;
1059 void race_ClearRecords()
1064 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1066 race_checkpoint_records[i] = 0;
1067 if(race_checkpoint_recordholders[i])
1068 strunzone(race_checkpoint_recordholders[i]);
1069 race_checkpoint_recordholders[i] = string_null;
1073 FOR_EACH_CLIENT(self)
1076 p = self.race_place;
1077 race_PreparePlayer();
1078 self.race_place = p;
1083 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1085 if(g_race_qualifying)
1087 pl.race_penalty_accumulator += penalty;
1088 if(IS_REAL_CLIENT(pl))
1091 WRITESPECTATABLE_MSG_ONE({
1092 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1093 WriteByte(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 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1108 WriteByte(MSG_ONE, TE_CSQC_RACE);
1109 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1110 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1111 WriteString(MSG_ONE, reason);
1117 void penalty_touch()
1120 if(other.race_lastpenalty != self)
1122 other.race_lastpenalty = self;
1123 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1129 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1132 void spawnfunc_trigger_race_penalty()
1136 self.use = penalty_use;
1137 if (!(self.spawnflags & 1))
1138 self.touch = penalty_touch;
1140 if (self.race_penalty_reason == "")
1141 self.race_penalty_reason = "missing a checkpoint";
1142 if (!self.race_penalty)
1143 self.race_penalty = 5;
1146 float race_GetFractionalLapCount(entity e)
1148 // interesting metrics (idea by KrimZon) to maybe sort players in the
1149 // scoreboard, immediately updates when overtaking
1151 // requires the track to be built so you never get farther away from the
1152 // next checkpoint, though, and current Xonotic race maps are not built that
1155 // also, this code is slow and would need optimization (i.e. "next CP"
1156 // links on CP entities)
1159 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1160 if(e.race_completed)
1161 return l; // not fractional
1164 float bestfraction, fraction;
1165 entity lastcp, cp0, cp1;
1166 float nextcpindex, lastcpindex;
1168 nextcpindex = max(e.race_checkpoint, 0);
1169 lastcpindex = e.race_respawn_checkpoint;
1170 lastcp = e.race_respawn_spotref;
1172 if(nextcpindex == lastcpindex)
1176 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1178 if(cp0.race_checkpoint != lastcpindex)
1183 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1184 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1186 if(cp1.race_checkpoint != nextcpindex)
1188 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1191 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1192 if(fraction < bestfraction)
1193 bestfraction = fraction;
1197 // we are at CP "nextcpindex - bestfraction"
1198 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1199 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1201 nc = race_highest_checkpoint + 1;
1202 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;