4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
7 #include "../warpzonelib/util_server.qh"
8 #include "../common/constants.qh"
9 #include "../common/util.qh"
10 #include "autocvars.qh"
12 #include "../common/notifications.qh"
13 #include "../common/deathtypes.qh"
14 #include "mutators/mutators_include.qh"
15 #include "../common/mapinfo.qh"
16 #include "command/sv_cmd.qh"
19 #include "spawnpoints.qh"
23 float race_readTime(string map, float pos)
25 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
27 return stof(db_get(ServerProgsDB, strcat(map, rr, "time", ftos(pos))));
30 string race_readUID(string map, float pos)
32 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
34 return db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos)));
37 float race_readPos(string map, float t)
40 for (i = 1; i <= RANKINGS_CNT; ++i)
41 if (race_readTime(map, i) == 0 || race_readTime(map, i) > t)
44 return 0; // pos is zero if unranked
47 void race_writeTime(string map, float t, string myuid)
49 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
52 newpos = race_readPos(map, t);
55 for(i = 1; i <= RANKINGS_CNT; ++i)
57 if(race_readUID(map, i) == myuid)
62 // player improved his existing record, only have to iterate on ranks between new and old recs
63 for (i = prevpos; i > newpos; --i)
65 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
66 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
71 // player has no ranked record yet
72 for (i = RANKINGS_CNT; i > newpos; --i)
74 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(map, i - 1)));
75 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(map, i - 1));
79 // store new time itself
80 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(newpos)), ftos(t));
81 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(newpos)), myuid);
84 string race_readName(string map, float pos)
86 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
88 return uid2name(db_get(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(pos))));
92 const float MAX_CHECKPOINTS = 255;
94 void spawnfunc_target_checkpoint();
97 .float race_penalty_accumulator;
98 .string race_penalty_reason;
99 .float race_checkpoint; // player: next checkpoint that has to be reached
101 .entity race_lastpenalty;
105 float race_checkpoint_records[MAX_CHECKPOINTS];
106 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
107 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
108 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
109 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
111 float race_highest_checkpoint;
112 float race_timed_checkpoint;
115 float defragcpexists;
117 float race_NextCheckpoint(float f)
119 if(f >= race_highest_checkpoint)
125 float race_PreviousCheckpoint(float f)
130 return race_highest_checkpoint;
136 // 0 = common start/finish
139 float race_CheckpointNetworkID(float f)
141 if(race_timed_checkpoint)
145 else if(f == race_timed_checkpoint)
146 return 255; // finish
151 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
160 cp = e.race_checkpoint;
161 recordtime = race_checkpoint_records[cp];
162 recordholder = race_checkpoint_recordholders[cp];
163 if(recordholder == e.netname)
166 if(!IS_REAL_CLIENT(e))
171 WRITESPECTATABLE_MSG_ONE({
172 WriteByte(MSG_ONE, SVC_TEMPENTITY);
173 WriteByte(MSG_ONE, TE_CSQC_RACE);
176 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
177 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
178 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
181 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
182 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
183 WriteInt24_t(MSG_ONE, recordtime);
184 WriteString(MSG_ONE, recordholder);
188 void race_send_recordtime(float msg)
190 // send the server best time
191 WriteByte(msg, SVC_TEMPENTITY);
192 WriteByte(msg, TE_CSQC_RACE);
193 WriteByte(msg, RACE_NET_SERVER_RECORD);
194 WriteInt24_t(msg, race_readTime(GetMapname(), 1));
197 void race_SendRankings(float pos, float prevpos, float del, float msg)
199 WriteByte(msg, SVC_TEMPENTITY);
200 WriteByte(msg, TE_CSQC_RACE);
201 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
202 WriteShort(msg, pos);
203 WriteShort(msg, prevpos);
204 WriteShort(msg, del);
205 WriteString(msg, race_readName(GetMapname(), pos));
206 WriteInt24_t(msg, race_readTime(GetMapname(), pos));
209 void race_SendStatus(float id, entity e)
211 if(!IS_REAL_CLIENT(e))
220 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
221 WriteByte(msg, SVC_TEMPENTITY);
222 WriteByte(msg, TE_CSQC_RACE);
223 WriteByte(msg, RACE_NET_SERVER_STATUS);
225 WriteString(msg, e.netname);
229 void race_setTime(string map, float t, string myuid, string mynetname, entity e)
231 // netname only used TEMPORARILY for printing
232 float newpos, player_prevpos;
233 newpos = race_readPos(map, t);
237 for(i = 1; i <= RANKINGS_CNT; ++i)
239 if(race_readUID(map, i) == myuid)
244 string oldrec_holder;
245 if (player_prevpos && (player_prevpos < newpos || !newpos))
247 oldrec = race_readTime(GetMapname(), player_prevpos);
248 race_SendStatus(0, e); // "fail"
249 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_RANKED, mynetname, player_prevpos, t, oldrec);
254 // no ranking, time worse than the worst ranked
255 oldrec = race_readTime(GetMapname(), RANKINGS_CNT);
256 race_SendStatus(0, e); // "fail"
257 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FAIL_UNRANKED, mynetname, RANKINGS_CNT, t, oldrec);
261 // if we didn't hit a return yet, we have a new record!
263 // if the player does not have a UID we can unfortunately not store the record, as the rankings system relies on UIDs
266 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_MISSING_UID, mynetname, t);
270 oldrec = race_readTime(GetMapname(), newpos);
271 oldrec_holder = race_readName(GetMapname(), newpos);
274 race_writeTime(GetMapname(), t, myuid);
278 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
279 race_send_recordtime(MSG_ALL);
282 race_SendRankings(newpos, player_prevpos, 0, MSG_ALL);
284 strunzone(rankings_reply);
285 rankings_reply = strzone(getrankings());
287 if(newpos == player_prevpos)
289 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_IMPROVED, mynetname, newpos, t, oldrec);
290 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
291 else { race_SendStatus(1, e); } // "new time"
295 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_SET, mynetname, newpos, t);
296 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
297 else { race_SendStatus(2, e); } // "new rank"
301 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_NEW_BROKEN, mynetname, oldrec_holder, newpos, t, oldrec);
302 if(newpos == 1) { race_SendStatus(3, e); } // "new server record"
303 else { race_SendStatus(2, e); } // "new rank"
307 void race_deleteTime(string map, float pos)
316 for (i = pos; i <= RANKINGS_CNT; ++i)
318 if (i == RANKINGS_CNT)
320 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), string_null);
321 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), string_null);
325 db_put(ServerProgsDB, strcat(map, rr, "time", ftos(i)), ftos(race_readTime(GetMapname(), i+1)));
326 db_put(ServerProgsDB, strcat(map, rr, "crypto_idfp", ftos(i)), race_readUID(GetMapname(), i+1));
330 race_SendRankings(pos, 0, 1, MSG_ALL);
332 race_send_recordtime(MSG_ALL);
335 strunzone(rankings_reply);
336 rankings_reply = strzone(getrankings());
339 void race_SendTime(entity e, float cp, float t, float tvalid)
344 if(g_race_qualifying)
345 t += e.race_penalty_accumulator;
347 t = TIME_ENCODE(t); // make integer
348 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
351 if(cp == race_timed_checkpoint) // finish line
352 if (!e.race_completed)
355 if(g_race_qualifying)
357 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
359 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
363 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
365 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
367 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
368 snew = TIME_ENCODE(time - game_starttime);
369 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
370 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
372 if(autocvar_fraglimit)
373 if(l >= autocvar_fraglimit)
374 race_StartCompleting();
378 e.race_completed = 1;
379 MAKE_INDEPENDENT_PLAYER(e);
380 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_FINISHED, e.netname);
388 if(g_race_qualifying)
392 recordtime = race_checkpoint_records[cp];
393 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
394 if(recordholder == e.netname)
399 if(cp == race_timed_checkpoint)
401 race_setTime(GetMapname(), t, e.crypto_idfp, e.netname, e);
402 if(g_cts && autocvar_g_cts_finish_kill_delay)
407 if(t < recordtime || recordtime == 0)
409 race_checkpoint_records[cp] = t;
410 if(race_checkpoint_recordholders[cp])
411 strunzone(race_checkpoint_recordholders[cp]);
412 race_checkpoint_recordholders[cp] = strzone(e.netname);
413 if(g_race_qualifying)
415 FOR_EACH_REALPLAYER(p)
416 if(p.race_checkpoint == cp)
417 race_SendNextCheckpoint(p, 0);
430 if(IS_REAL_CLIENT(e))
433 if(g_race_qualifying)
435 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
436 WriteByte(MSG_ONE, SVC_TEMPENTITY);
437 WriteByte(MSG_ONE, TE_CSQC_RACE);
438 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
439 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
440 WriteInt24_t(MSG_ONE, t); // time to that intermediate
441 WriteInt24_t(MSG_ONE, recordtime); // previously best time
442 WriteString(MSG_ONE, recordholder); // record holder
447 else // RACE! Not Qualifying
449 float lself, lother, othtime;
451 oth = race_checkpoint_lastplayers[cp];
454 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
455 lother = race_checkpoint_lastlaps[cp];
456 othtime = race_checkpoint_lasttimes[cp];
459 lself = lother = othtime = 0;
461 if(IS_REAL_CLIENT(e))
464 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
465 WriteByte(MSG_ONE, SVC_TEMPENTITY);
466 WriteByte(MSG_ONE, TE_CSQC_RACE);
467 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
468 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
471 WriteInt24_t(MSG_ONE, 0);
472 WriteByte(MSG_ONE, 0);
473 WriteString(MSG_ONE, "");
477 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
478 WriteByte(MSG_ONE, lself - lother);
479 WriteString(MSG_ONE, oth.netname); // record holder
484 race_checkpoint_lastplayers[cp] = e;
485 race_checkpoint_lasttimes[cp] = time;
486 race_checkpoint_lastlaps[cp] = lself;
488 if(IS_REAL_CLIENT(oth))
491 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
492 WriteByte(MSG_ONE, SVC_TEMPENTITY);
493 WriteByte(MSG_ONE, TE_CSQC_RACE);
494 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
495 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
498 WriteInt24_t(MSG_ONE, 0);
499 WriteByte(MSG_ONE, 0);
500 WriteString(MSG_ONE, "");
504 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
505 WriteByte(MSG_ONE, lother - lself);
506 WriteString(MSG_ONE, e.netname); // record holder
513 void race_ClearTime(entity e)
515 e.race_checkpoint = 0;
517 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
518 e.race_penalty_accumulator = 0;
519 e.race_lastpenalty = world;
521 if(!IS_REAL_CLIENT(e))
525 WRITESPECTATABLE_MSG_ONE({
526 WriteByte(MSG_ONE, SVC_TEMPENTITY);
527 WriteByte(MSG_ONE, TE_CSQC_RACE);
528 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
532 void dumpsurface(entity e)
536 print("Surfaces of ", etos(e), ":\n");
538 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
542 n = getsurfacenumpoints(e, si);
545 print(" Surface ", ftos(si), ":\n");
546 norm = getsurfacenormal(e, si);
547 print(" Normal = ", vtos(norm), "\n");
548 for(ni = 0; ni < n; ++ni)
550 vec = getsurfacepoint(e, si, ni);
551 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
556 void checkpoint_passed()
561 if(other.classname == "porto")
563 // do not allow portalling through checkpoints
564 trace_plane_normal = normalize(-1 * other.velocity);
573 if (!((self.spawnflags & 2) && (IS_PLAYER(other))))
576 oldmsg = self.message;
579 self.message = oldmsg;
582 if (!IS_PLAYER(other))
586 * Remove unauthorized equipment
588 Portal_ClearAll(other);
590 other.porto_forbidden = 2; // decreased by 1 each StartFrame
594 if(self.race_checkpoint == -2)
596 self.race_checkpoint = other.race_checkpoint;
599 float largest_cp_id = 0;
601 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
604 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
606 largest_cp_id = cp.race_checkpoint;
607 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
608 cp.race_checkpoint = largest_cp_id + 1; // finish line
609 race_highest_checkpoint = largest_cp_id + 1;
610 race_timed_checkpoint = largest_cp_id + 1;
612 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
614 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
621 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
622 cp.race_checkpoint = 1;
623 race_highest_checkpoint = 1;
624 race_timed_checkpoint = 1;
628 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
630 if(self.race_penalty)
632 if(other.race_lastpenalty != self)
634 other.race_lastpenalty = self;
635 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
639 if(other.race_penalty)
645 if(self.spawnflags & 2)
648 oldmsg = self.message;
651 self.message = oldmsg;
654 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
655 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
656 other.race_respawn_checkpoint = self.race_checkpoint;
657 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
658 other.race_started = 1;
660 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
662 if(!self.race_checkpoint) // start line
664 other.race_laptime = time;
665 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
666 other.race_penalty_accumulator = 0;
667 other.race_lastpenalty = world;
670 if(g_race_qualifying)
671 race_SendNextCheckpoint(other, 0);
673 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
676 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
679 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
680 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
685 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
691 if(self.spawnflags & 4)
692 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
696 void checkpoint_touch()
702 void checkpoint_use()
704 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
711 float race_waypointsprite_visible_for_player(entity e)
713 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
715 else if(e.race_checkpoint == self.owner.race_checkpoint)
722 void trigger_race_checkpoint_verify()
732 qual = g_race_qualifying;
736 self.classname = "player";
740 for(i = 0; i <= race_highest_checkpoint; ++i)
742 self.race_checkpoint = race_NextCheckpoint(i);
744 // race only (middle of the race)
745 g_race_qualifying = 0;
747 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
748 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
753 g_race_qualifying = 1;
754 self.race_place = race_lowest_place_spawn;
755 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
756 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
758 // race only (initial spawn)
759 g_race_qualifying = 0;
760 for(p = 1; p <= race_highest_place_spawn; ++p)
763 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
764 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
769 else if(!defrag_ents)
772 self.race_checkpoint = race_NextCheckpoint(0);
773 g_race_qualifying = 1;
774 self.race_place = race_lowest_place_spawn;
775 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), 0, false))
776 error(strcat("Checkpoint 0 misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
780 self.race_checkpoint = race_NextCheckpoint(0);
781 g_race_qualifying = 1;
782 self.race_place = 0; // there's only one spawn on defrag maps
784 // check if a defragcp file already exists, then read it and apply the checkpoint order
789 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
792 while((l = fgets(fh)))
794 len = tokenize_console(l);
797 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
800 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
801 if(argv(0) == cp.targetname)
802 cp.race_checkpoint = stof(argv(1));
808 g_race_qualifying = qual;
810 if(race_timed_checkpoint)
814 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
815 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
816 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
817 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
819 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
821 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
825 if(defragcpexists != -1)
827 float largest_cp_id = 0;
828 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
829 if(cp.race_checkpoint > largest_cp_id)
830 largest_cp_id = cp.race_checkpoint;
831 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
832 cp.race_checkpoint = largest_cp_id + 1; // finish line
833 race_highest_checkpoint = largest_cp_id + 1;
834 race_timed_checkpoint = largest_cp_id + 1;
838 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
839 cp.race_checkpoint = 255; // finish line
840 race_highest_checkpoint = 255;
841 race_timed_checkpoint = 255;
846 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
849 if(cp.race_checkpoint == 0)
850 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
851 else if(cp.race_checkpoint == race_timed_checkpoint)
852 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
859 entity trigger, targ;
860 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
861 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
862 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer")
869 // These just make the game crash on some maps with oddly shaped triggers.
870 // (on the other hand they used to fix the case when two players ran through a checkpoint at once,
871 // and often one of them just passed through without being registered. Hope it's fixed in a better way now.
872 // (happened on item triggers too)
877 //setsize(targ, trigger.mins, trigger.maxs);
878 //setorigin(targ, trigger.origin);
886 vector trigger_race_checkpoint_spawn_evalfunc(entity player, entity spot, vector current)
888 if(g_race_qualifying)
891 if(self.race_checkpoint != 0)
893 if(spot.race_place != race_lowest_place_spawn)
898 if(self.race_checkpoint != player.race_respawn_checkpoint)
900 // try reusing the previous spawn
901 if(self == player.race_respawn_spotref || spot == player.race_respawn_spotref)
902 current.x += SPAWN_PRIO_RACE_PREVIOUS_SPAWN;
903 if(self.race_checkpoint == 0)
906 pl = player.race_place;
907 if(pl > race_highest_place_spawn)
909 if(pl == 0 && !player.race_started)
910 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet
911 if(spot.race_place != pl)
918 void spawnfunc_trigger_race_checkpoint()
921 if(!g_race && !g_cts) { remove(self); return; }
925 self.use = checkpoint_use;
926 if (!(self.spawnflags & 1))
927 self.touch = checkpoint_touch;
929 o = (self.absmin + self.absmax) * 0.5;
930 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
931 waypoint_spawnforitem_force(self, trace_endpos);
932 self.nearestwaypointtimeout = time + 1000000000;
934 if(self.message == "")
935 self.message = "went backwards";
936 if (self.message2 == "")
937 self.message2 = "was pushed backwards by";
938 if (self.race_penalty_reason == "")
939 self.race_penalty_reason = "missing a checkpoint";
941 self.race_checkpoint = self.cnt;
943 if(self.race_checkpoint > race_highest_checkpoint)
945 race_highest_checkpoint = self.race_checkpoint;
946 if(self.spawnflags & 8)
947 race_timed_checkpoint = self.race_checkpoint;
949 race_timed_checkpoint = 0;
952 if(!self.race_penalty)
954 if(self.race_checkpoint)
955 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
957 WaypointSprite_SpawnFixed("race-start-finish", o, self, sprite, RADARICON_NONE, '1 0.5 0');
960 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
961 self.spawn_evalfunc = trigger_race_checkpoint_spawn_evalfunc;
963 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
966 void spawnfunc_target_checkpoint() // defrag entity
969 if(!g_race && !g_cts) { remove(self); return; }
974 self.use = checkpoint_use;
975 if (!(self.spawnflags & 1))
976 self.touch = checkpoint_touch;
978 o = (self.absmin + self.absmax) * 0.5;
979 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o.z - self.absmin.z), MOVE_NORMAL, self);
980 waypoint_spawnforitem_force(self, trace_endpos);
981 self.nearestwaypointtimeout = time + 1000000000;
983 if(self.message == "")
984 self.message = "went backwards";
985 if (self.message2 == "")
986 self.message2 = "was pushed backwards by";
987 if (self.race_penalty_reason == "")
988 self.race_penalty_reason = "missing a checkpoint";
990 if(self.classname == "target_startTimer")
991 self.race_checkpoint = 0;
993 self.race_checkpoint = -2;
995 race_timed_checkpoint = 1;
997 if(self.race_checkpoint == 0)
998 WaypointSprite_SpawnFixed("race-start", o, self, sprite, RADARICON_NONE, '1 0.5 0');
1000 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite, RADARICON_NONE, '1 0.5 0');
1002 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
1004 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
1007 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
1008 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
1010 void race_AbandonRaceCheck(entity p)
1012 if(race_completing && !p.race_completed)
1014 p.race_completed = 1;
1015 MAKE_INDEPENDENT_PLAYER(p);
1016 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_RACE_ABANDONED, p.netname);
1017 ClientData_Touch(p);
1021 void race_StartCompleting()
1024 race_completing = 1;
1026 if(p.deadflag != DEAD_NO)
1027 race_AbandonRaceCheck(p);
1030 void race_PreparePlayer()
1032 race_ClearTime(self);
1033 self.race_place = 0;
1034 self.race_started = 0;
1035 self.race_respawn_checkpoint = 0;
1036 self.race_respawn_spotref = world;
1039 void race_RetractPlayer()
1041 if(!g_race && !g_cts)
1043 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1044 race_ClearTime(self);
1045 self.race_checkpoint = self.race_respawn_checkpoint;
1048 void spawnfunc_info_player_race (void)
1050 if(!g_race && !g_cts) { remove(self); return; }
1052 spawnfunc_info_player_deathmatch();
1054 if(self.race_place > race_highest_place_spawn)
1055 race_highest_place_spawn = self.race_place;
1056 if(self.race_place < race_lowest_place_spawn)
1057 race_lowest_place_spawn = self.race_place;
1060 void race_ClearRecords()
1065 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1067 race_checkpoint_records[i] = 0;
1068 if(race_checkpoint_recordholders[i])
1069 strunzone(race_checkpoint_recordholders[i]);
1070 race_checkpoint_recordholders[i] = string_null;
1074 FOR_EACH_CLIENT(self)
1077 p = self.race_place;
1078 race_PreparePlayer();
1079 self.race_place = p;
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 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1094 WriteByte(MSG_ONE, TE_CSQC_RACE);
1095 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1096 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1097 WriteString(MSG_ONE, reason);
1103 pl.race_penalty = time + penalty;
1104 if(IS_REAL_CLIENT(pl))
1107 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1108 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1109 WriteByte(MSG_ONE, TE_CSQC_RACE);
1110 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1111 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1112 WriteString(MSG_ONE, reason);
1118 void penalty_touch()
1121 if(other.race_lastpenalty != self)
1123 other.race_lastpenalty = self;
1124 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1130 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1133 void spawnfunc_trigger_race_penalty()
1137 self.use = penalty_use;
1138 if (!(self.spawnflags & 1))
1139 self.touch = penalty_touch;
1141 if (self.race_penalty_reason == "")
1142 self.race_penalty_reason = "missing a checkpoint";
1143 if (!self.race_penalty)
1144 self.race_penalty = 5;
1147 float race_GetFractionalLapCount(entity e)
1149 // interesting metrics (idea by KrimZon) to maybe sort players in the
1150 // scoreboard, immediately updates when overtaking
1152 // requires the track to be built so you never get farther away from the
1153 // next checkpoint, though, and current Xonotic race maps are not built that
1156 // also, this code is slow and would need optimization (i.e. "next CP"
1157 // links on CP entities)
1160 l = PlayerScore_Add(e, SP_RACE_LAPS, 0);
1161 if(e.race_completed)
1162 return l; // not fractional
1165 float bestfraction, fraction;
1166 entity lastcp, cp0, cp1;
1167 float nextcpindex, lastcpindex;
1169 nextcpindex = max(e.race_checkpoint, 0);
1170 lastcpindex = e.race_respawn_checkpoint;
1171 lastcp = e.race_respawn_spotref;
1173 if(nextcpindex == lastcpindex)
1177 for(cp0 = world; (cp0 = find(cp0, classname, "trigger_race_checkpoint")); )
1179 if(cp0.race_checkpoint != lastcpindex)
1184 o0 = (cp0.absmin + cp0.absmax) * 0.5;
1185 for(cp1 = world; (cp1 = find(cp1, classname, "trigger_race_checkpoint")); )
1187 if(cp1.race_checkpoint != nextcpindex)
1189 o1 = (cp1.absmin + cp1.absmax) * 0.5;
1192 fraction = bound(0.0001, vlen(e.origin - o1) / vlen(o0 - o1), 1);
1193 if(fraction < bestfraction)
1194 bestfraction = fraction;
1198 // we are at CP "nextcpindex - bestfraction"
1199 // race_timed_checkpoint == 4: then nextcp==4 means 0.9999x, nextcp==0 means 0.0000x
1200 // race_timed_checkpoint == 0: then nextcp==0 means 0.9999x
1202 nc = race_highest_checkpoint + 1;
1203 c = ((nextcpindex - race_timed_checkpoint + nc + nc - 1) % nc) + 1 - bestfraction;