1 #define MAX_CHECKPOINTS 255
3 void spawnfunc_target_checkpoint();
6 .float race_penalty_accumulator;
7 .string race_penalty_reason;
8 .float race_checkpoint; // player: next checkpoint that has to be reached
10 .entity race_lastpenalty;
14 float race_checkpoint_records[MAX_CHECKPOINTS];
15 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
16 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
17 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
18 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
20 float race_highest_checkpoint;
21 float race_timed_checkpoint;
26 float race_NextCheckpoint(float f)
28 if(f >= race_highest_checkpoint)
34 float race_PreviousCheckpoint(float f)
39 return race_highest_checkpoint;
45 // 0 = common start/finish
48 float race_CheckpointNetworkID(float f)
50 if(race_timed_checkpoint)
54 else if(f == race_timed_checkpoint)
60 void race_SendNextCheckpoint(entity e, float spec) // qualifying only
69 cp = e.race_checkpoint;
70 recordtime = race_checkpoint_records[cp];
71 recordholder = race_checkpoint_recordholders[cp];
72 if(recordholder == e.netname)
77 WRITESPECTATABLE_MSG_ONE({
78 WriteByte(MSG_ONE, SVC_TEMPENTITY);
79 WriteByte(MSG_ONE, TE_CSQC_RACE);
82 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
83 //WriteCoord(MSG_ONE, e.race_laptime - e.race_penalty_accumulator);
84 WriteCoord(MSG_ONE, time - e.race_movetime - e.race_penalty_accumulator);
87 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
88 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player will be at next
89 WriteInt24_t(MSG_ONE, recordtime);
90 WriteString(MSG_ONE, recordholder);
94 void race_InitSpectator()
97 if(msg_entity.enemy.race_laptime)
98 race_SendNextCheckpoint(msg_entity.enemy, 1);
102 float grecordtime[RANKINGS_CNT];
103 string grecordholder[RANKINGS_CNT];
105 string grecorduid[RANKINGS_CNT];
107 float worst_time; // last ranked time
108 float have_recs; // have we already read the records from the database before?
109 float race_GetTime(float pos) {
115 if (!have_recs) { // I guess this is better than checking if the first array item is empty, rumor has it that arrays are slow
117 for(i=0;i<RANKINGS_CNT;++i) {
118 grecordtime[i] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i))));
119 grecordholder[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i))));
121 grecorduid[i] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i))));
124 grecordtime[0] = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));
125 grecordholder[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "netname")));
127 grecorduid[0] = strzone(db_get(ServerProgsDB, strcat(GetMapname(), rr, "uid")));
129 worst_time = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, strcat("time", ftos(RANKINGS_CNT-1)))));
133 return grecordtime[pos-1];
136 string race_GetName(float pos) { // these other functions assume that race_GetTime has been called >= once before
137 return grecordholder[pos-1];
141 float race_CheckUID(string uid, string netname) { // return existing UID or player name ranking pos, else 0
143 for (i=RANKINGS_CNT-1;i>=0;--i)
144 if(grecorduid[i] == uid)
146 for (i=RANKINGS_CNT-1;i>=0;--i)
147 if(grecordholder[i] == netname)
153 float race_CheckName(string netname) { // Does the name already exist in rankings? In that case, where? (otherwise 0)
155 for (i=RANKINGS_CNT-1;i>=0;--i)
156 if(grecordholder[i] == netname)
161 float race_GetPos(float t) {
165 for (i=0;i<RANKINGS_CNT;++i)
166 if (grecordtime[i] == 0 || grecordtime[i] > t)
169 for (i=0;i<RANKINGS_CNT;++i)
170 if (grecordtime[i] > t)
175 void race_send_recordtime(float msg)
177 // send the server best time
178 WriteByte(msg, SVC_TEMPENTITY);
179 WriteByte(msg, TE_CSQC_RACE);
180 WriteByte(msg, RACE_NET_SERVER_RECORD);
181 WriteInt24_t(msg, race_GetTime(1));
184 void race_SendRankings(float pos, float prevpos, float del, float msg)
186 WriteByte(msg, SVC_TEMPENTITY);
187 WriteByte(msg, TE_CSQC_RACE);
188 WriteByte(msg, RACE_NET_SERVER_RANKINGS);
189 WriteShort(msg, pos);
190 WriteShort(msg, prevpos);
191 WriteShort(msg, del);
192 WriteString(msg, race_GetName(pos));
193 WriteInt24_t(msg, race_GetTime(pos));
196 string race_PlaceName(float pos) {
204 return strcat(ftos(pos), "th");
207 void race_SetTime(entity e, float t) {
208 if (worst_time && t > worst_time)
214 pos = race_GetPos(t);
216 float prevpos = race_CheckName(e.netname);
218 float prevpos = race_CheckUID(e.uid, e.netname);
220 if (prevpos && (prevpos < pos || !pos))
222 oldrec = race_GetTime(prevpos);
223 string recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - oldrec), "]");
224 bprint(e.netname, "^1 couldn't break their ", race_PlaceName(prevpos), " place record of ", TIME_ENCODED_TOSTRING(oldrec), recorddifference, "\n");
227 } else if (!pos) { // no ranking, time worse than the worst ranked
228 string recorddifference = strcat(" ^1[+", TIME_ENCODED_TOSTRING(t - grecordtime[RANKINGS_CNT-1]), "]");
229 bprint(e.netname, "^1 couldn't break the ", race_PlaceName(RANKINGS_CNT), " place record of ", TIME_ENCODED_TOSTRING(grecordtime[RANKINGS_CNT-1]), recorddifference, "\n");
234 // move other rankings out of the way
236 if (prevpos) { // player improved his existing record
238 oldrec = grecordtime[pos-1];
240 for (i=prevpos-1;i>pos-1;--i) {
241 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
242 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
244 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i-1]);
247 oldrec = grecordtime[i];
248 oldname = grecordholder[i];
250 grecordtime[i] = grecordtime[i-1];
252 if (grecordholder[i])
253 strunzone(grecordholder[i]);
254 grecordholder[i] = strzone(grecordholder[i-1]);
257 strunzone(grecorduid[i]);
258 grecorduid[i] = strzone(grecorduid[i-1]);
261 } else { // player has no ranked record yet
262 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
263 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i-1]));
264 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i-1]);
266 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(i)), grecorduid[i-1]);
268 grecordtime[i] = grecordtime[i-1];
270 if (grecordholder[i])
271 strunzone(grecordholder[i]);
272 grecordholder[i] = strzone(grecordholder[i-1]);
275 strunzone(grecorduid[i]);
276 grecorduid[i] = strzone(grecorduid[i-1]);
283 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(t));
284 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), e.netname);
286 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid"), e.uid);
291 if (grecordholder[0])
292 strunzone(grecordholder[0]);
293 grecordholder[0] = strzone(e.netname);
296 strunzone(grecorduid[0]);
297 grecorduid[0] = strzone(e.uid);
299 write_recordmarker(e, time - TIME_DECODE(t), TIME_DECODE(t));
300 race_send_recordtime(MSG_ALL);
302 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(pos-1)), ftos(t));
303 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(pos-1)), e.netname);
305 db_put(ServerProgsDB, strcat(GetMapname(), rr, "uid", ftos(pos-1)), e.uid);
308 grecordtime[pos-1] = t;
310 if (grecordholder[pos-1])
311 strunzone(grecordholder[pos-1]);
312 grecordholder[pos-1] = strzone(e.netname);
314 if (grecorduid[pos-1])
315 strunzone(grecorduid[pos-1]);
316 grecorduid[pos-1] = strzone(e.uid);
320 if (pos == RANKINGS_CNT)
323 race_SendRankings(pos, prevpos, 0, MSG_ALL);
325 strunzone(rankings_reply);
326 rankings_reply = strzone(getrankings());
328 string recorddifference = strcat(" ^3[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
329 bprint(e.netname, "^3 improved their ", race_PlaceName(pos), " ^3place record with ", TIME_ENCODED_TOSTRING(t), recorddifference, "\n");
331 else if(oldname == "")
332 bprint(e.netname, "^2 set the ", race_PlaceName(pos), " ^2place record with ", TIME_ENCODED_TOSTRING(t), "\n");
334 string recorddifference = strcat(" ^2[-", TIME_ENCODED_TOSTRING(oldrec - t), "]");
335 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"));
339 void race_DeleteTime(float pos) {
342 for (i = pos-1; i <= RANKINGS_CNT-1; ++i) {
344 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time"), ftos(grecordtime[1]));
345 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname"), grecordholder[1]);
346 grecordtime[0] = grecordtime[1];
347 if (grecordholder[i])
348 strunzone(grecordholder[0]);
349 grecordholder[0] = strzone(grecordholder[1]);
351 else if (i == RANKINGS_CNT-1) {
352 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), string_null);
353 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), string_null);
355 if (grecordholder[i])
356 strunzone(grecordholder[i]);
357 grecordholder[i] = string_null;
360 db_put(ServerProgsDB, strcat(GetMapname(), rr, "time", ftos(i)), ftos(grecordtime[i+1]));
361 db_put(ServerProgsDB, strcat(GetMapname(), rr, "netname", ftos(i)), grecordholder[i+1]);
362 grecordtime[i] = grecordtime[i+1];
363 if (grecordholder[i])
364 strunzone(grecordholder[i]);
365 grecordholder[i] = strzone(grecordholder[i+1]);
369 race_SendRankings(pos, 0, 1, MSG_ALL);
371 race_send_recordtime(MSG_ALL);
374 strunzone(rankings_reply);
375 rankings_reply = strzone(getrankings());
380 void race_SendTime(entity e, float cp, float t, float tvalid)
385 if(g_race_qualifying)
386 t += e.race_penalty_accumulator;
388 t = TIME_ENCODE(t); // make integer
389 // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
392 if(cp == race_timed_checkpoint) // finish line
393 if not(e.race_completed)
396 if(g_race_qualifying)
398 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
400 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
404 s = PlayerScore_Add(e, SP_RACE_TIME, 0);
405 snew = TIME_ENCODE(time - game_starttime);
406 PlayerScore_Add(e, SP_RACE_TIME, snew - s);
407 l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
409 if(cvar("fraglimit"))
410 if(l >= cvar("fraglimit"))
411 race_StartCompleting();
415 e.race_completed = 1;
416 MAKE_INDEPENDENT_PLAYER(e);
417 bprint(e.netname, "^7 has finished the race.\n");
425 if(g_race_qualifying)
429 recordtime = race_checkpoint_records[cp];
430 recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
431 if(recordholder == e.netname)
435 if(t < worst_time || worst_time == 0)
437 if(cp == race_timed_checkpoint)
442 if(t < recordtime || recordtime == 0)
444 race_checkpoint_records[cp] = t;
445 if(race_checkpoint_recordholders[cp])
446 strunzone(race_checkpoint_recordholders[cp]);
447 race_checkpoint_recordholders[cp] = strzone(e.netname);
448 if(g_race_qualifying)
450 FOR_EACH_REALPLAYER(p)
451 if(p.race_checkpoint == cp)
452 race_SendNextCheckpoint(p, 0);
466 if(g_race_qualifying)
468 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
469 WriteByte(MSG_ONE, SVC_TEMPENTITY);
470 WriteByte(MSG_ONE, TE_CSQC_RACE);
471 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
472 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
473 WriteInt24_t(MSG_ONE, t); // time to that intermediate
474 WriteInt24_t(MSG_ONE, recordtime); // previously best time
475 WriteString(MSG_ONE, recordholder); // record holder
479 else // RACE! Not Qualifying
481 float lself, lother, othtime;
483 oth = race_checkpoint_lastplayers[cp];
486 lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
487 lother = race_checkpoint_lastlaps[cp];
488 othtime = race_checkpoint_lasttimes[cp];
491 lself = lother = othtime = 0;
494 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
495 WriteByte(MSG_ONE, SVC_TEMPENTITY);
496 WriteByte(MSG_ONE, TE_CSQC_RACE);
497 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
498 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
501 WriteInt24_t(MSG_ONE, 0);
502 WriteByte(MSG_ONE, 0);
503 WriteString(MSG_ONE, "");
507 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - race_checkpoint_lasttimes[cp]));
508 WriteByte(MSG_ONE, lself - lother);
509 WriteString(MSG_ONE, oth.netname); // record holder
513 race_checkpoint_lastplayers[cp] = e;
514 race_checkpoint_lasttimes[cp] = time;
515 race_checkpoint_lastlaps[cp] = lself;
518 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
519 WriteByte(MSG_ONE, SVC_TEMPENTITY);
520 WriteByte(MSG_ONE, TE_CSQC_RACE);
521 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
522 WriteByte(MSG_ONE, race_CheckpointNetworkID(cp)); // checkpoint the player now is at
525 WriteInt24_t(MSG_ONE, 0);
526 WriteByte(MSG_ONE, 0);
527 WriteString(MSG_ONE, "");
531 WriteInt24_t(MSG_ONE, TIME_ENCODE(time - othtime));
532 WriteByte(MSG_ONE, lother - lself);
533 WriteString(MSG_ONE, e.netname); // record holder
539 void race_ClearTime(entity e)
541 e.race_checkpoint = 0;
543 e.race_movetime = e.race_movetime_frac = e.race_movetime_count = 0;
544 e.race_penalty_accumulator = 0;
545 e.race_lastpenalty = world;
548 WRITESPECTATABLE_MSG_ONE({
549 WriteByte(MSG_ONE, SVC_TEMPENTITY);
550 WriteByte(MSG_ONE, TE_CSQC_RACE);
551 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
555 void dumpsurface(entity e)
559 print("Surfaces of ", etos(e), ":\n");
561 print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
565 n = getsurfacenumpoints(e, si);
568 print(" Surface ", ftos(si), ":\n");
569 norm = getsurfacenormal(e, si);
570 print(" Normal = ", vtos(norm), "\n");
571 for(ni = 0; ni < n; ++ni)
573 vec = getsurfacepoint(e, si, ni);
574 print(" Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
579 void checkpoint_passed()
584 if(other.classname == "porto")
586 // do not allow portalling through checkpoints
587 trace_plane_normal = normalize(-1 * other.velocity);
596 if not((self.spawnflags & 2) && (other.classname == "player"))
599 oldmsg = self.message;
602 self.message = oldmsg;
605 if(other.classname != "player")
609 * Remove unauthorized equipment
611 Portal_ClearAll(other);
613 other.porto_forbidden = 2; // decreased by 1 each StartFrame
616 if(self.race_checkpoint == -2)
618 self.race_checkpoint = other.race_checkpoint;
623 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
625 if(cp.race_checkpoint > largest_cp_id) // update the finish id if someone hit a new checkpoint
627 largest_cp_id = cp.race_checkpoint;
628 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
629 cp.race_checkpoint = largest_cp_id + 1; // finish line
630 race_highest_checkpoint = largest_cp_id + 1;
631 race_timed_checkpoint = largest_cp_id + 1;
633 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
634 if(cp.race_checkpoint == -2) // set defragcpexists to -1 so that the cp id file will be rewritten when someone finishes
640 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
641 cp.race_checkpoint = 1;
642 race_highest_checkpoint = 1;
643 race_timed_checkpoint = 1;
647 if((other.race_checkpoint == -1 && self.race_checkpoint == 0) || (other.race_checkpoint == self.race_checkpoint))
649 if(self.race_penalty)
651 if(other.race_lastpenalty != self)
653 other.race_lastpenalty = self;
654 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
658 if(other.race_penalty)
664 if(self.spawnflags & 2)
667 oldmsg = self.message;
670 self.message = oldmsg;
673 if(other.race_respawn_checkpoint != self.race_checkpoint || !other.race_started)
674 other.race_respawn_spotref = self; // this is not a spot but a CP, but spawnpoint selection will deal with that
675 other.race_respawn_checkpoint = self.race_checkpoint;
676 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
677 other.race_started = 1;
679 race_SendTime(other, self.race_checkpoint, other.race_movetime, !!other.race_laptime);
681 if(!self.race_checkpoint) // start line
683 other.race_laptime = time;
684 other.race_movetime = other.race_movetime_frac = other.race_movetime_count = 0;
685 other.race_penalty_accumulator = 0;
686 other.race_lastpenalty = world;
689 if(g_race_qualifying)
690 race_SendNextCheckpoint(other, 0);
692 if(defrag_ents && defragcpexists < 0 && self.classname == "target_stopTimer")
695 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_WRITE);
698 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
699 fputs(fh, strcat(cp.targetname, " ", ftos(cp.race_checkpoint), "\n"));
704 else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
710 if(self.spawnflags & 4)
711 Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
715 void checkpoint_touch()
721 void checkpoint_use()
723 if(other.classname == "info_player_deathmatch") // a spawn, a spawn
730 float race_waypointsprite_visible_for_player(entity e)
732 if(e.race_checkpoint == -1 || self.owner.race_checkpoint == -2)
734 else if(e.race_checkpoint == self.owner.race_checkpoint)
741 void trigger_race_checkpoint_verify()
751 qual = g_race_qualifying;
755 self.classname = "player";
759 for(i = 0; i <= race_highest_checkpoint; ++i)
761 self.race_checkpoint = race_NextCheckpoint(i);
763 // race only (middle of the race)
764 g_race_qualifying = 0;
766 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
767 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
772 g_race_qualifying = 1;
773 self.race_place = race_lowest_place_spawn;
774 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
775 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
777 // race only (initial spawn)
778 g_race_qualifying = 0;
779 for(p = 1; p <= race_highest_place_spawn; ++p)
782 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
783 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
788 else if(!defrag_ents)
791 self.race_checkpoint = race_NextCheckpoint(0);
792 g_race_qualifying = 1;
793 self.race_place = race_lowest_place_spawn;
794 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE, FALSE))
795 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
799 self.race_checkpoint = race_NextCheckpoint(0);
800 g_race_qualifying = 1;
801 self.race_place = 0; // there's only one spawn on defrag maps
803 // check if a defragcp file already exists, then read it and apply the checkpoint order
808 defragcpexists = fh = fopen(strcat("maps/", GetMapname(), ".defragcp"), FILE_READ);
811 while((l = fgets(fh)))
813 len = tokenize_console(l);
815 defragcpexists = -1; // something's wrong in the defrag cp file, set defragcpexists to -1 so that it will be rewritten when someone finishes
818 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
819 if(argv(0) == cp.targetname)
820 cp.race_checkpoint = stof(argv(1));
826 g_race_qualifying = qual;
828 if(race_timed_checkpoint) {
830 for(cp = world; (cp = find(cp, classname, "target_startTimer"));)
831 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
832 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
833 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
835 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));) {
836 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
840 if(defragcpexists != -1){
842 for(cp = world; (cp = find(cp, classname, "target_checkpoint"));)
843 if(cp.race_checkpoint > largest_cp_id)
844 largest_cp_id = cp.race_checkpoint;
845 for(cp = world; (cp = find(cp, classname, "target_stopTimer"));)
846 cp.race_checkpoint = largest_cp_id + 1; // finish line
847 race_highest_checkpoint = largest_cp_id + 1;
848 race_timed_checkpoint = largest_cp_id + 1;
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;
857 for(cp = world; (cp = find(cp, classname, "trigger_race_checkpoint")); )
860 if(cp.race_checkpoint == 0)
861 WaypointSprite_UpdateSprites(cp.sprite, "race-start", "", "");
862 else if(cp.race_checkpoint == race_timed_checkpoint)
863 WaypointSprite_UpdateSprites(cp.sprite, "race-finish", "", "");
869 entity trigger, targ;
870 for(trigger = world; (trigger = find(trigger, classname, "trigger_multiple")); )
871 for(targ = world; (targ = find(targ, targetname, trigger.target)); )
872 if (targ.classname == "target_checkpoint" || targ.classname == "target_startTimer" || targ.classname == "target_stopTimer") {
876 setsize(targ, trigger.mins, trigger.maxs);
877 setorigin(targ, trigger.origin);
885 void spawnfunc_trigger_race_checkpoint()
888 if(!g_race && !g_cts)
896 self.use = checkpoint_use;
897 if not(self.spawnflags & 1)
898 self.touch = checkpoint_touch;
900 o = (self.absmin + self.absmax) * 0.5;
901 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
902 waypoint_spawnforitem_force(self, trace_endpos);
903 self.nearestwaypointtimeout = time + 1000000000;
906 self.message = "went backwards";
908 self.message2 = "was pushed backwards by";
909 if (!self.race_penalty_reason)
910 self.race_penalty_reason = "missing a checkpoint";
912 self.race_checkpoint = self.cnt;
914 if(self.race_checkpoint > race_highest_checkpoint)
916 race_highest_checkpoint = self.race_checkpoint;
917 if(self.spawnflags & 8)
918 race_timed_checkpoint = self.race_checkpoint;
920 race_timed_checkpoint = 0;
923 if(!self.race_penalty)
925 if(self.race_checkpoint)
926 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
928 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
931 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
933 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
936 void spawnfunc_target_checkpoint() // defrag entity
939 if(!g_race && !g_cts)
948 self.use = checkpoint_use;
949 if not(self.spawnflags & 1)
950 self.touch = checkpoint_touch;
952 o = (self.absmin + self.absmax) * 0.5;
953 tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
954 waypoint_spawnforitem_force(self, trace_endpos);
955 self.nearestwaypointtimeout = time + 1000000000;
958 self.message = "went backwards";
960 self.message2 = "was pushed backwards by";
961 if (!self.race_penalty_reason)
962 self.race_penalty_reason = "missing a checkpoint";
964 if(self.classname == "target_startTimer")
965 self.race_checkpoint = 0;
967 self.race_checkpoint = -2;
969 race_timed_checkpoint = 1;
971 if(self.race_checkpoint == 0)
972 WaypointSprite_SpawnFixed("race-start", o, self, sprite);
974 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
976 self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
978 InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
981 void spawnfunc_target_startTimer() { spawnfunc_target_checkpoint(); }
982 void spawnfunc_target_stopTimer() { spawnfunc_target_checkpoint(); }
984 void race_AbandonRaceCheck(entity p)
986 if(race_completing && !p.race_completed)
988 p.race_completed = 1;
989 MAKE_INDEPENDENT_PLAYER(p);
990 bprint(p.netname, "^7 has abandoned the race.\n");
995 void race_StartCompleting()
1000 if(p.deadflag != DEAD_NO)
1001 race_AbandonRaceCheck(p);
1004 void race_PreparePlayer()
1006 race_ClearTime(self);
1007 self.race_place = 0;
1008 self.race_started = 0;
1009 self.race_respawn_checkpoint = 0;
1010 self.race_respawn_spotref = world;
1013 void race_RetractPlayer()
1015 if(!g_race && !g_cts)
1017 if(self.race_respawn_checkpoint == 0 || self.race_respawn_checkpoint == race_timed_checkpoint)
1018 race_ClearTime(self);
1019 self.race_checkpoint = self.race_respawn_checkpoint;
1024 if(!g_race && !g_cts)
1027 race_AbandonRaceCheck(self);
1030 void race_PreSpawn()
1032 if(!g_race && !g_cts)
1034 if(self.killcount == -666 /* initial spawn */ || g_race_qualifying) // spawn
1035 race_PreparePlayer();
1037 race_RetractPlayer();
1039 race_AbandonRaceCheck(self);
1042 void race_PostSpawn(entity spot)
1044 if(!g_race && !g_cts)
1047 if(spot.target == "")
1048 // Emergency: this wasn't a real spawnpoint. Can this ever happen?
1049 race_PreparePlayer();
1051 // if we need to respawn, do it right
1052 self.race_respawn_checkpoint = self.race_checkpoint;
1053 self.race_respawn_spotref = spot;
1055 self.race_place = 0;
1058 void race_PreSpawnObserver()
1060 if(!g_race && !g_cts)
1062 race_PreparePlayer();
1063 self.race_checkpoint = -1;
1066 void spawnfunc_info_player_race (void)
1068 if(!g_race && !g_cts)
1074 spawnfunc_info_player_deathmatch();
1076 if(self.race_place > race_highest_place_spawn)
1077 race_highest_place_spawn = self.race_place;
1078 if(self.race_place < race_lowest_place_spawn)
1079 race_lowest_place_spawn = self.race_place;
1082 void race_ClearRecords()
1087 for(i = 0; i < MAX_CHECKPOINTS; ++i)
1089 race_checkpoint_records[i] = 0;
1090 if(race_checkpoint_recordholders[i])
1091 strunzone(race_checkpoint_recordholders[i]);
1092 race_checkpoint_recordholders[i] = string_null;
1096 FOR_EACH_CLIENT(self)
1099 p = self.race_place;
1100 race_PreparePlayer();
1101 self.race_place = p;
1106 void race_ReadyRestart()
1110 Score_NicePrint(world);
1112 race_ClearRecords();
1113 PlayerScore_Sort(race_place);
1120 s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
1124 print(e.netname, " = ", ftos(e.race_place), "\n");
1127 if(g_race_qualifying == 2)
1129 g_race_qualifying = 0;
1130 independent_players = 0;
1131 cvar_set("fraglimit", ftos(race_fraglimit));
1132 cvar_set("leadlimit", ftos(race_leadlimit));
1133 cvar_set("timelimit", ftos(race_timelimit));
1138 void race_ImposePenaltyTime(entity pl, float penalty, string reason)
1140 if(g_race_qualifying)
1142 pl.race_penalty_accumulator += penalty;
1144 WRITESPECTATABLE_MSG_ONE({
1145 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1146 WriteByte(MSG_ONE, TE_CSQC_RACE);
1147 WriteByte(MSG_ONE, RACE_NET_PENALTY_QUALIFYING);
1148 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1149 WriteString(MSG_ONE, reason);
1154 pl.race_penalty = time + penalty;
1156 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy, {
1157 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1158 WriteByte(MSG_ONE, TE_CSQC_RACE);
1159 WriteByte(MSG_ONE, RACE_NET_PENALTY_RACE);
1160 WriteShort(MSG_ONE, TIME_ENCODE(penalty));
1161 WriteString(MSG_ONE, reason);
1166 void penalty_touch()
1169 if(other.race_lastpenalty != self)
1171 other.race_lastpenalty = self;
1172 race_ImposePenaltyTime(other, self.race_penalty, self.race_penalty_reason);
1178 race_ImposePenaltyTime(activator, self.race_penalty, self.race_penalty_reason);
1181 void spawnfunc_trigger_race_penalty()
1185 self.use = penalty_use;
1186 if not(self.spawnflags & 1)
1187 self.touch = penalty_touch;
1189 if (!self.race_penalty_reason)
1190 self.race_penalty_reason = "missing a checkpoint";
1191 if (!self.race_penalty)
1192 self.race_penalty = 5;