1 #define MAX_CHECKPOINTS 255
4 .float race_penalty_accumulator;
5 .string race_penalty_reason;
6 .float race_checkpoint; // player: next checkpoint that has to be reached
8 .entity race_lastpenalty;
12 float race_checkpoint_records[MAX_CHECKPOINTS];
13 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
14 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
15 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
16 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
18 float race_highest_checkpoint;
19 float race_timed_checkpoint;
24 float race_NextCheckpoint(float f)
26 if(f >= race_highest_checkpoint)
32 float race_PreviousCheckpoint(float f)
37 return race_highest_checkpoint;
43 // 0 = common start/finish
46 float race_CheckpointNetworkID(float f)
48 if(race_timed_checkpoint)
52 else if(f == race_timed_checkpoint)
58 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
67 cp = e.race_checkpoint;
68 recordtime = race_checkpoint_records[cp];
69 recordholder = race_checkpoint_recordholders[cp];
70 if(recordholder == e.netname)
75 WRITESPECTATABLE_MSG_ONE({
76 WriteByte(MSG_ONE, SVC_TEMPENTITY);
77 WriteByte(MSG_ONE, TE_CSQC_RACE);
80 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
81 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
82 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
85 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
86 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
87 WriteInt24_t(MSG_ONE, recordtime);
88 WriteString(MSG_ONE, recordholder);
92 void race_InitSpectator()
95 if(msg_entity.enemy.race_laptime)
96 race_SendNextCheckpoint(msg_entity.enemy, 1);
100 float grecordtime[RANKINGS_CNT];
101 string grecordholder[RANKINGS_CNT];
102 float worst_time; // last ranked time
103 float have_recs; // have we already read the records from the database before?
104 float race_GetTime(float pos) {
110 if (!have_recs) { // I guess this is better than checking if the first array item is empty, rumor has it that arrays are slow
112 for(i=0;i<RANKINGS_CNT;++i) {
113 grecordtime[i] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i))));
114 grecordholder[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i))));
116 grecordtime[0] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
117 grecordholder[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname")));
118 worst_time = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, strcat("time", ftos(RANKINGS_CNT-1)))));
122 return grecordtime[pos-1];
125 string race_GetName(float pos) { // these other functions assume that race_GetTime has been called >= once before
126 return grecordholder[pos-1];
129 float race_CheckName(string netname) { // Does the name already exist in rankings? In that case, where? (otherwise 0)
131 for (i=RANKINGS_CNT-1;i>=0;--i)
132 if(grecordholder[i] == netname)
137 float race_GetPos(float t) {
141 for (i=0;i<RANKINGS_CNT;++i)
142 if (grecordtime[i] == 0 || grecordtime[i] > t)
145 for (i=0;i<RANKINGS_CNT;++i)
146 if (grecordtime[i] > t)
151 void race_send_recordtime(float msg)
153 // send the server best time
154 WriteByte(msg, SVC_TEMPENTITY);
155 WriteByte(msg, TE_CSQC_RACE);
156 WriteByte(msg, RACE_NET_SERVER_RECORD);
157 WriteInt24_t(msg, race_GetTime(1));
160 void race_SendRankings(float pos, float prevpos, float del, float msg)
162 WriteByte(msg, SVC_TEMPENTITY);
163 WriteByte(msg, TE_CSQC_RACE);
164 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
165 WriteShort(msg, pos);
166 WriteShort(msg, prevpos);
167 WriteShort(msg, del);
168 WriteString(msg, race_GetName(pos));
169 WriteInt24_t(msg, race_GetTime(pos));
172 string race_PlaceName(float pos) {
180 return strcat(ftos(pos), "th");
183 void race_SetTime(entity e, float t) {
184 if (worst_time && t > worst_time)
190 pos = race_GetPos(t);
192 float prevpos = race_CheckName(e.netname);
193 if (prevpos && (prevpos < pos || !pos))
195 oldrec = race_GetTime(prevpos);
196 string recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
197 bprint(e.netname, "^1 couldn't break their ", race_PlaceName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
200 } else if (!pos) { // no ranking, time worse than the worst ranked
201 string recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - grecordtime[RANKINGS_CNT-1]), "]");
202 bprint(e.netname, "^1 couldn't break the ", race_PlaceName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
207 // move other rankings out of the way
209 if (prevpos) { // player improved his existing record
211 oldrec = grecordtime[pos-1];
213 for (i=prevpos-1;i>pos-1;--i) {
214 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
215 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
217 oldrec = grecordtime[i];
218 oldname = grecordholder[i];
220 grecordtime[i] = grecordtime[i-1];
221 if (grecordholder[i])
222 strunzone(grecordholder[i]);
223 grecordholder[i] = strzone(grecordholder[i-1]);
225 } else { // player has no ranked record yet
226 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
227 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
228 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
229 grecordtime[i] = grecordtime[i-1];
230 if (grecordholder[i])
231 strunzone(grecordholder[i]);
232 grecordholder[i] = strzone(grecordholder[i-1]);
238 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
239 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
241 if (grecordholder[0])
242 strunzone(grecordholder[0]);
243 grecordholder[0] = strzone(e.netname);
244 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
245 race_send_recordtime(MSG_ALL);
247 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(pos-1)), ftos(t));
248 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(pos-1)), e.netname);
249 grecordtime[pos-1] = t;
250 if (grecordholder[pos-1])
251 strunzone(grecordholder[pos-1]);
252 grecordholder[pos-1] = strzone(e.netname);
255 if (pos == RANKINGS_CNT)
258 race_SendRankings(pos, prevpos, 0, MSG_ALL);
260 strunzone(rankings_reply);
261 rankings_reply = strzone(getrankings());
263 string recorddifference = strcat(" ^3[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
264 bprint(e.netname, "^3 improved their ", race_PlaceName(pos), " ^3place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
266 else if(oldname == "")
267 bprint(e.netname, "^2 set the ", race_PlaceName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
269 string recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
270 bprint(e.netname, "^2 broke ", oldname, "^2's ", race_PlaceName(pos), " ^2place record ", strcat(TIME_ENCODED_TOSTRING(oldrec), " ^2with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n"));
274 void race_DeleteTime(float pos) {
277 for (i = pos-1; i <= RANKINGS_CNT-1; ++i) {
279 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(grecordtime[1]));
280 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), grecordholder[1]);
281 grecordtime[0] = grecordtime[1];
282 if (grecordholder[i])
283 strunzone(grecordholder[0]);
284 grecordholder[0] = strzone(grecordholder[1]);
286 else if (i == RANKINGS_CNT-1) {
287 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), string_null);
288 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), string_null);
290 if (grecordholder[i])
291 strunzone(grecordholder[i]);
292 grecordholder[i] = string_null;
295 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i+1]));
296 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i+1]);
297 grecordtime[i] = grecordtime[i+1];
298 if (grecordholder[i])
299 strunzone(grecordholder[i]);
300 grecordholder[i] = strzone(grecordholder[i+1]);
304 race_SendRankings(pos, 0, 1, MSG_ALL);
306 race_send_recordtime(MSG_ALL);
309 strunzone(rankings_reply);
310 rankings_reply = strzone(getrankings());
315 void race_SendTime(entity e, float cp, float t, float tvalid)
320 if(g_race_qualifying)
321 t += e.race_penalty_accumulator;
323 t = TIME_ENCODE(t); // make integer
324 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
327 if(cp == race_timed_checkpoint) // finish line
328 if not(e.race_completed)
331 if(g_race_qualifying)
333 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
335 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
339 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
340 snew = TIME_ENCODE(time - game_starttime);
341 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
342 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
344 if(cvar("fraglimit"))
345 if(l >= cvar("fraglimit"))
346 race_StartCompleting();
350 e.race_completed = 1;
351 MAKE_INDEPENDENT_PLAYER(e);
352 bprint(e.netname, "^7 has finished the race.\n");
360 if(g_race_qualifying)
364 recordtime = race_checkpoint_records[cp];
365 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
366 if(recordholder == e.netname)
370 if(t < worst_time || worst_time == 0)
372 if(cp == race_timed_checkpoint)
377 if(t < recordtime || recordtime == 0)
379 race_checkpoint_records[cp] = t;
380 if(race_checkpoint_recordholders[cp])
381 strunzone(race_checkpoint_recordholders[cp]);
382 race_checkpoint_recordholders[cp] = strzone(e.netname);
383 if(g_race_qualifying)
385 FOR_EACH_REALPLAYER(p)
386 if(p.race_checkpoint == cp)
387 race_SendNextCheckpoint(p, 0);
401 if(g_race_qualifying)
403 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
404 WriteByte(MSG_ONE, SVC_TEMPENTITY);
405 WriteByte(MSG_ONE, TE_CSQC_RACE);
406 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
407 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
408 WriteInt24_t(MSG_ONE, t); // time to that intermediate
409 WriteInt24_t(MSG_ONE, recordtime); // previously best time
410 WriteString(MSG_ONE, recordholder); // record holder
414 else // RACE! Not Qualifying
416 float lself, lother, othtime;
418 oth = race_checkpoint_lastplayers[cp];
421 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
422 lother = race_checkpoint_lastlaps[cp];
423 othtime = race_checkpoint_lasttimes[cp];
426 lself = lother = othtime = 0;
429 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
430 WriteByte(MSG_ONE, SVC_TEMPENTITY);
431 WriteByte(MSG_ONE, TE_CSQC_RACE);
432 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
433 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
436 WriteInt24_t(MSG_ONE, 0);
437 WriteByte(MSG_ONE, 0);
438 WriteString(MSG_ONE, "");
442 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
443 WriteByte(MSG_ONE, lself - lother);
444 WriteString(MSG_ONE, oth.netname); // record holder
448 race_checkpoint_lastplayers[cp] = e;
449 race_checkpoint_lasttimes[cp] = time;
450 race_checkpoint_lastlaps[cp] = lself;
453 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
454 WriteByte(MSG_ONE, SVC_TEMPENTITY);
455 WriteByte(MSG_ONE, TE_CSQC_RACE);
456 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
457 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
460 WriteInt24_t(MSG_ONE, 0);
461 WriteByte(MSG_ONE, 0);
462 WriteString(MSG_ONE, "");
466 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
467 WriteByte(MSG_ONE, lother - lself);
468 WriteString(MSG_ONE, e.netname); // record holder
474 void race_ClearTime(entity e)
476 e.race_checkpoint = 0;
478 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
479 e.race_penalty_accumulator = 0;
480 e.race_lastpenalty = world;
483 WRITESPECTATABLE_MSG_ONE({
484 WriteByte(MSG_ONE, SVC_TEMPENTITY);
485 WriteByte(MSG_ONE, TE_CSQC_RACE);
486 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
490 void dumpsurface(entity e)
494 print("Surfaces of ", etos(e), ":\n");
496 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
500 n = getsurfacenumpoints(e, si);
503 print(" Surface ", ftos(si), ":\n");
504 norm = getsurfacenormal(e, si);
505 print(" Normal = ", vtos(norm), "\n");
506 for(ni = 0; ni < n; ++ni)
508 vec = getsurfacepoint(e, si, ni);
509 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
514 void checkpoint_passed()
519 if(other.classname == "porto")
521 // do not allow portalling through checkpoints
522 trace_plane_normal = normalize(-1 * other.velocity);
531 if not((self.spawnflags & 2) && (other.classname == "player"))
534 oldmsg = self.message;
537 self.message = oldmsg;
540 if(other.classname != "player")
544 * Remove unauthorized equipment
546 Portal_ClearAll(other);
548 other.porto_forbidden = 2; // decreased by 1 each StartFrame
551 if(self.race_checkpoint == -2)
553 self.race_checkpoint = other.race_checkpoint;
558 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
560 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
562 largest_cp_id = cp.race_checkpoint;
563 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
564 cp.race_checkpoint = largest_cp_id + 1; // finish line
565 race_highest_checkpoint = largest_cp_id + 1;
566 race_timed_checkpoint = largest_cp_id + 1;
568 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
569 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
575 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
576 cp.race_checkpoint = 1;
577 race_highest_checkpoint = 1;
578 race_timed_checkpoint = 1;
582 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
584 if(self.race_penalty)
586 if(other.race_lastpenalty != self)
588 other.race_lastpenalty = self;
589 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
593 if(other.race_penalty)
599 if(self.spawnflags & 2)
602 oldmsg = self.message;
605 self.message = oldmsg;
608 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
609 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
610 other.race_respawn_checkpoint = self.race_checkpoint;
611 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
612 other.race_started = 1;
614 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
616 if(!self.race_checkpoint) // start line
618 other.race_laptime = time;
619 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
620 other.race_penalty_accumulator = 0;
621 other.race_lastpenalty = world;
624 if(g_race_qualifying)
625 race_SendNextCheckpoint(other, 0);
627 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
630 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
633 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
634 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
639 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
645 if(self.spawnflags & 4)
646 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
650 void checkpoint_touch()
656 void checkpoint_use()
658 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
665 float race_waypointsprite_visible_for_player(entity e)
667 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
669 else if(e.race_checkpoint == self.owner.race_checkpoint)
676 void trigger_race_checkpoint_verify()
686 qual = g_race_qualifying;
690 self.classname = "player";
694 for(i = 0; i <= race_highest_checkpoint; ++i)
696 self.race_checkpoint = race_NextCheckpoint(i);
698 // race only (middle of the race)
699 g_race_qualifying = 0;
701 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
702 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
707 g_race_qualifying = 1;
708 self.race_place = race_lowest_place_spawn;
709 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
710 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
712 // race only (initial spawn)
713 g_race_qualifying = 0;
714 for(p = 1; p <= race_highest_place_spawn; ++p)
717 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
718 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
723 else if(!defrag_ents)
726 self.race_checkpoint = race_NextCheckpoint(0);
727 g_race_qualifying = 1;
728 self.race_place = race_lowest_place_spawn;
729 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
730 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
734 self.race_checkpoint = race_NextCheckpoint(0);
735 g_race_qualifying = 1;
736 self.race_place = 0; // there's only one spawn on defrag maps
738 // check if a defragcp file already exists, then read it and apply the checkpoint order
743 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
746 while((l = fgets(fh)))
748 len = tokenize_console(l);
750 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
753 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
754 if(argv(0) == cp.targetname)
755 cp.race_checkpoint = stof(argv(1));
761 g_race_qualifying = qual;
763 if(race_timed_checkpoint) {
765 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
766 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
767 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
768 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
770 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
771 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
775 if(defragcpexists != -1){
777 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
778 if(cp.race_checkpoint > largest_cp_id)
779 largest_cp_id = cp.race_checkpoint;
780 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
781 cp.race_checkpoint = largest_cp_id + 1; // finish line
782 race_highest_checkpoint = largest_cp_id + 1;
783 race_timed_checkpoint = largest_cp_id + 1;
785 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
786 cp.race_checkpoint = 255; // finish line
787 race_highest_checkpoint = 255;
788 race_timed_checkpoint = 255;
791 for(targ = world; (targ = find(targ, target, self.targetname)); )
792 if (targ.classname == "trigger_multiple") {
793 targ.wait = -2; // still needs investigating, why cant two players pass a (defrag) checkpoint at the same time?
798 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
801 if(cp.race_checkpoint == 0)
802 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
803 else if(cp.race_checkpoint == race_timed_checkpoint)
804 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
813 void spawnfunc_trigger_race_checkpoint()
816 if(!g_race && !g_cts)
824 self.use = checkpoint_use;
825 if not(self.spawnflags & 1)
826 self.touch = checkpoint_touch;
828 o = (self.absmin + self.absmax) * 0.5;
829 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
830 waypoint_spawnforitem_force(self, trace_endpos);
831 self.nearestwaypointtimeout = time + 1000000000;
834 self.message = "went backwards";
836 self.message2 = "was pushed backwards by";
837 if (!self.race_penalty_reason)
838 self.race_penalty_reason = "missing a checkpoint";
840 self.race_checkpoint = self.cnt;
842 if(self.race_checkpoint > race_highest_checkpoint)
844 race_highest_checkpoint = self.race_checkpoint;
845 if(self.spawnflags & 8)
846 race_timed_checkpoint = self.race_checkpoint;
848 race_timed_checkpoint = 0;
851 if(!self.race_penalty)
853 if(self.race_checkpoint)
854 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
856 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
859 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
861 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
864 void spawnfunc_target_checkpoint() // defrag entity
867 if(!g_race && !g_cts)
876 self.use = checkpoint_use;
877 //if not(self.spawnflags & 1)
878 // self.touch = checkpoint_touch;
880 o = (self.absmin + self.absmax) * 0.5;
881 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
882 waypoint_spawnforitem_force(self, trace_endpos);
883 self.nearestwaypointtimeout = time + 1000000000;
886 self.message = "went backwards";
888 self.message2 = "was pushed backwards by";
889 if (!self.race_penalty_reason)
890 self.race_penalty_reason = "missing a checkpoint";
892 if(self.classname == "target_startTimer")
893 self.race_checkpoint = 0;
895 self.race_checkpoint = -2;
897 race_timed_checkpoint = 1;
899 if(self.race_checkpoint == 0)
900 WaypointSprite_SpawnFixed("race-start", o, self, sprite);
902 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
904 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
906 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
909 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
910 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
912 void race_AbandonRaceCheck(entity p)
914 if(race_completing && !p.race_completed)
916 p.race_completed = 1;
917 MAKE_INDEPENDENT_PLAYER(p);
918 bprint(p.netname, "^7 has abandoned the race.\n");
923 void race_StartCompleting()
928 if(p.deadflag != DEAD_NO)
929 race_AbandonRaceCheck(p);
932 void race_PreparePlayer()
934 race_ClearTime(self);
936 self.race_started = 0;
937 self.race_respawn_checkpoint = 0;
938 self.race_respawn_spotref = world;
941 void race_RetractPlayer()
943 if(!g_race && !g_cts)
945 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
946 race_ClearTime(self);
947 self.race_checkpoint = self.race_respawn_checkpoint;
952 if(!g_race && !g_cts)
955 race_AbandonRaceCheck(self);
960 if(!g_race && !g_cts)
962 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
963 race_PreparePlayer();
965 race_RetractPlayer();
967 race_AbandonRaceCheck(self);
970 void race_PostSpawn(entity spot)
972 if(!g_race && !g_cts)
975 if(spot.target == "")
976 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
977 race_PreparePlayer();
979 // if we need to respawn, do it right
980 self.race_respawn_checkpoint = self.race_checkpoint;
981 self.race_respawn_spotref = spot;
986 void race_PreSpawnObserver()
988 if(!g_race && !g_cts)
990 race_PreparePlayer();
991 self.race_checkpoint = -1;
994 void spawnfunc_info_player_race (void)
996 if(!g_race && !g_cts)
1002 spawnfunc_info_player_deathmatch();
1004 if(self.race_place > race_highest_place_spawn)
1005 race_highest_place_spawn = self.race_place;
1006 if(self.race_place < race_lowest_place_spawn)
1007 race_lowest_place_spawn = self.race_place;
1010 void race_ClearRecords()
1015 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1017 race_checkpoint_records[i] = 0;
1018 if(race_checkpoint_recordholders[i])
1019 strunzone(race_checkpoint_recordholders[i]);
1020 race_checkpoint_recordholders[i] = string_null;
1024 FOR_EACH_CLIENT(self)
1027 p = self.race_place;
1028 race_PreparePlayer();
1029 self.race_place = p;
1034 void race_ReadyRestart()
1038 Score_NicePrint(world);
1040 race_ClearRecords();
1041 PlayerScore_Sort(race_place);
1048 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1052 print(e.netname, " = ", ftos(e.race_place), "\n");
1055 if(g_race_qualifying == 2)
1057 g_race_qualifying = 0;
1058 independent_players = 0;
1059 cvar_set("fraglimit", ftos(race_fraglimit));
1060 cvar_set("leadlimit", ftos(race_leadlimit));
1061 cvar_set("timelimit", ftos(race_timelimit));
1066 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1068 if(g_race_qualifying)
1070 pl.race_penalty_accumulator += penalty;
1072 WRITESPECTATABLE_MSG_ONE({
1073 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1074 WriteByte(MSG_ONE, TE_CSQC_RACE);
1075 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1076 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1077 WriteString(MSG_ONE, reason);
1082 pl.race_penalty = time + penalty;
1084 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1085 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1086 WriteByte(MSG_ONE, TE_CSQC_RACE);
1087 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1088 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1089 WriteString(MSG_ONE, reason);
1094 void penalty_touch()
1097 if(other.race_lastpenalty != self)
1099 other.race_lastpenalty = self;
1100 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1106 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1109 void spawnfunc_trigger_race_penalty()
1113 self.use = penalty_use;
1114 if not(self.spawnflags & 1)
1115 self.touch = penalty_touch;
1117 if (!self.race_penalty_reason)
1118 self.race_penalty_reason = "missing a checkpoint";
1119 if (!self.race_penalty)
1120 self.race_penalty = 5;