]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
also write a record marker for CTF (so one can extract record runs from demos)
[divverent/nexuiz.git] / data / qcsrc / server / race.qc
1 #define MAX_CHECKPOINTS 255
2
3 .float race_checkpoint; // player: next checkpoint that has to be reached
4 .float race_laptime;
5
6 .entity sprite;
7
8 float race_checkpoint_records[MAX_CHECKPOINTS];
9 string race_checkpoint_recordholders[MAX_CHECKPOINTS];
10 float race_checkpoint_lasttimes[MAX_CHECKPOINTS];
11 float race_checkpoint_lastlaps[MAX_CHECKPOINTS];
12 entity race_checkpoint_lastplayers[MAX_CHECKPOINTS];
13
14 float race_highest_checkpoint;
15
16 float race_NextCheckpoint(float f)
17 {
18         if(f >= race_highest_checkpoint)
19                 return 0;
20         else
21                 return f + 1;
22 }
23
24 float race_PreviousCheckpoint(float f)
25 {
26         if(f == -1)
27                 return 0;
28         else if(f == 0)
29                 return race_highest_checkpoint;
30         else
31                 return f - 1;
32 }
33
34 void race_SendNextCheckpoint(entity e, float spec)
35 {
36         float recordtime;
37         string recordholder;
38         float cp;
39
40         if(!e.race_laptime)
41                 return;
42
43         cp = e.race_checkpoint;
44         recordtime = race_checkpoint_records[cp];
45         recordholder = race_checkpoint_recordholders[cp];
46         /*
47         recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
48         recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
49         */
50         if(recordholder == e.netname)
51                 recordholder = "";
52
53         if(!spec)
54                 msg_entity = e;
55         WRITESPECTATABLE_MSG_ONE({
56                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
57                 WriteByte(MSG_ONE, TE_CSQC_RACE);
58                 if(spec)
59                 {
60                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING);
61                         WriteCoord(MSG_ONE, e.race_laptime);
62                 }
63                 else
64                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
65                 WriteByte(MSG_ONE, cp); // checkpoint the player will be at next
66                 WriteShort(MSG_ONE, recordtime);
67                 WriteString(MSG_ONE, recordholder);
68         });
69 }
70
71 void race_InitSpectator()
72 {
73         if(g_race_qualifying)
74                 if(msg_entity.enemy.race_laptime)
75                         race_SendNextCheckpoint(msg_entity.enemy, 1);
76 }
77
78 void race_SendTime(entity e, float cp, float t, float tvalid)
79 {
80         float snew, l;
81         entity p;
82
83         t = floor(0.4 + 10 * t); // make integer
84         // adding just 0.4 so it rounds down in the .5 case (matching the timer display)
85
86         if(tvalid)
87         if(cp == 0) // finish line
88         if not(e.race_completed)
89         {
90                 float s;
91                 if(g_race_qualifying)
92                 {
93                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
94                         if(!s || t < s)
95                                 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
96                 }
97                 else
98                 {
99                         s = PlayerScore_Add(e, SP_RACE_TIME, 0);
100                         snew = floor(0.5 + 10 * (time - game_starttime));
101                         PlayerScore_Add(e, SP_RACE_TIME, snew - s);
102                         l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
103
104                         if(cvar("fraglimit"))
105                                 if(l >= cvar("fraglimit"))
106                                 {
107                                         race_completing = 1;
108                                         FOR_EACH_PLAYER(p)
109                                                 if(p.deadflag != DEAD_NO)
110                                                         race_AbandonRaceCheck(p);
111                                 }
112
113                         if(race_completing)
114                         {
115                                 e.race_completed = 1;
116                                 MAKE_INDEPENDENT_PLAYER(e);
117                                 bprint(e.netname, "^7 has finished the race.\n");
118                                 ClientData_Touch(e);
119                         }
120                 }
121         }
122
123         float recordtime;
124         string recordholder;
125
126         if(g_race_qualifying)
127         {
128                 if(tvalid)
129                 {
130                         recordtime = race_checkpoint_records[cp];
131                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
132                         if(recordholder == e.netname)
133                                 recordholder = "";
134
135                         if(t != 0)
136                         if(t < recordtime || recordtime == 0)
137                         {
138                                 race_checkpoint_records[cp] = t;
139                                 if(race_checkpoint_recordholders[cp])
140                                         strunzone(race_checkpoint_recordholders[cp]);
141                                 race_checkpoint_recordholders[cp] = strzone(e.netname);
142                                 if(cp == 0)
143                                 {
144                                         float grecordtime;
145                                         string grecordholder;
146                                         grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
147                                         grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
148                                         if(grecordholder == e.netname)
149                                                 grecordholder = "";
150                                         if(grecordtime == 0)
151                                         {
152                                                 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
153                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
154                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
155                                                 write_recordmarker(e, time - t/10, t/10);
156                                         }
157                                         else if(t < grecordtime)
158                                         {
159                                                 if(grecordholder == "")
160                                                         bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
161                                                 else
162                                                         bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
163                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
164                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
165                                                 write_recordmarker(e, time - t/10, t/10);
166                                         }
167                                         else
168                                         {
169                                                 if(grecordholder == "")
170                                                         bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
171                                                 else
172                                                         bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
173                                         }
174                                 }
175
176                                 if(g_race_qualifying)
177                                 {
178                                         FOR_EACH_REALPLAYER(p)
179                                                 if(p.race_checkpoint == cp)
180                                                         race_SendNextCheckpoint(p, 0);
181                                 }
182                         }
183                 }
184                 else
185                 {
186                         // dummies
187                         t = 0;
188                         recordtime = 0;
189                         recordholder = "";
190                 }
191
192                 msg_entity = e;
193                 if(g_race_qualifying)
194                 {
195                         WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
196                                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
197                                 WriteByte(MSG_ONE, TE_CSQC_RACE);
198                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
199                                 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
200                                 WriteShort(MSG_ONE, t); // time to that intermediate
201                                 WriteShort(MSG_ONE, recordtime); // previously best time
202                                 WriteString(MSG_ONE, recordholder); // record holder
203                         });
204                 }
205         }
206         else // RACE! Not Qualifying
207         {
208                 float lself, lother, othtime;
209                 entity oth;
210                 oth = race_checkpoint_lastplayers[cp];
211                 if(oth)
212                 {
213                         lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
214                         lother = race_checkpoint_lastlaps[cp];
215                         othtime = race_checkpoint_lasttimes[cp];
216                 }
217                 else
218                         lself = lother = othtime = 0;
219
220                 msg_entity = e;
221                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
222                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
223                         WriteByte(MSG_ONE, TE_CSQC_RACE);
224                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
225                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
226                         if(e == oth)
227                         {
228                                 WriteShort(MSG_ONE, 0);
229                                 WriteByte(MSG_ONE, 0);
230                                 WriteString(MSG_ONE, "");
231                         }
232                         else
233                         {
234                                 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
235                                 WriteByte(MSG_ONE, lself - lother);
236                                 WriteString(MSG_ONE, oth.netname); // record holder
237                         }
238                 });
239
240                 race_checkpoint_lastplayers[cp] = e;
241                 race_checkpoint_lasttimes[cp] = time;
242                 race_checkpoint_lastlaps[cp] = lself;
243
244                 msg_entity = oth;
245                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
246                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
247                         WriteByte(MSG_ONE, TE_CSQC_RACE);
248                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
249                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
250                         if(e == oth)
251                         {
252                                 WriteShort(MSG_ONE, 0);
253                                 WriteByte(MSG_ONE, 0);
254                                 WriteString(MSG_ONE, "");
255                         }
256                         else
257                         {
258                                 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
259                                 WriteByte(MSG_ONE, lother - lself);
260                                 WriteString(MSG_ONE, e.netname); // record holder
261                         }
262                 });
263         }
264 }
265
266 void race_ClearTime(entity e)
267 {
268         e.race_checkpoint = -1;
269         e.race_laptime = 0;
270
271         msg_entity = e;
272         WRITESPECTATABLE_MSG_ONE({
273                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
274                 WriteByte(MSG_ONE, TE_CSQC_RACE);
275                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
276         });
277 }
278
279 void dumpsurface(entity e)
280 {
281         float n, si, ni;
282         vector norm, vec;
283         print("Surfaces of ", etos(e), ":\n");
284
285         print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
286
287         for(si = 0; ; ++si)
288         {
289                 n = getsurfacenumpoints(e, si);
290                 if(n <= 0)
291                         break;
292                 print("  Surface ", ftos(si), ":\n");
293                 norm = getsurfacenormal(e, si);
294                 print("    Normal = ", vtos(norm), "\n");
295                 for(ni = 0; ni < n; ++ni)
296                 {
297                         vec = getsurfacepoint(e, si, ni);
298                         print("    Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
299                 }
300         }
301 }
302
303 void checkpoint_passed()
304 {
305         string oldmsg;
306
307         if(other.classname == "porto")
308         {
309                 // do not allow portalling through checkpoints
310                 trace_plane_normal = normalize(-1 * other.velocity);
311                 self = other;
312                 W_Porto_Fail(0);
313                 return;
314         }
315
316         /*
317          * Trigger targets
318          */
319         if not(self.spawnflags & 2)
320         {
321                 activator = other;
322                 oldmsg = self.message;
323                 self.message = "";
324                 SUB_UseTargets();
325                 self.message = oldmsg;
326         }
327
328         if(other.classname != "player")
329                 return;
330
331         /*
332          * Remove unauthorized equipment
333          */
334         Portal_ClearAll(other);
335
336         other.porto_forbidden = 2; // decreased by 1 each StartFrame
337
338         if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
339         {
340                 /*
341                  * Trigger targets
342                  */
343                 if(self.spawnflags & 2)
344                 {
345                         activator = other;
346                         oldmsg = self.message;
347                         self.message = "";
348                         SUB_UseTargets();
349                         self.message = oldmsg;
350                 }
351
352                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
353
354                 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
355
356                 if(!self.race_checkpoint) // finish line
357                         other.race_laptime = time;
358
359                 if(g_race_qualifying)
360                         race_SendNextCheckpoint(other, 0);
361         }
362         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
363         {
364                 // ignored
365         }
366         else
367         {
368                 if(self.spawnflags & 4)
369                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
370         }
371 }
372
373 void checkpoint_touch()
374 {
375         EXACTTRIGGER_TOUCH;
376         checkpoint_passed();
377 }
378
379 void checkpoint_use()
380 {
381         if(other.classname == "info_player_deathmatch") // a spawn, a spawn
382                 return;
383
384         other = activator;
385         checkpoint_passed();
386 }
387
388 float race_waypointsprite_visible_for_player(entity e)
389 {
390         if(e.race_checkpoint == -1)
391                 return TRUE;
392         else if(e.race_checkpoint == self.owner.race_checkpoint)
393                 return TRUE;
394         else
395                 return FALSE;
396 }
397
398 float have_verified;
399 void trigger_race_checkpoint_verify()
400 {
401         entity oldself;
402         float i, p;
403         float qual;
404
405         if(have_verified)
406                 return;
407         have_verified = 1;
408         
409         qual = g_race_qualifying;
410
411         oldself = self;
412         self = spawn();
413         self.classname = "player";
414
415         for(i = 0; i <= race_highest_checkpoint; ++i)
416         {
417                 self.race_checkpoint = race_NextCheckpoint(i);
418
419                 // race only (middle of the race)
420                 g_race_qualifying = 0;
421                 self.race_place = 0;
422                 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
423                         error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for respawning in race) - bailing out"));
424
425                 if(i == 0)
426                 {
427                         // qualifying only
428                         g_race_qualifying = 1;
429                         self.race_place = race_lowest_place_spawn;
430                         if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
431                                 error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for qualifying) - bailing out"));
432                         
433                         // race only (initial spawn)
434                         g_race_qualifying = 0;
435                         for(p = 1; p <= race_highest_place_spawn; ++p)
436                         {
437                                 self.race_place = p;
438                                 if(!Spawn_FilterOutBadSpots(findchain(classname, "info_player_deathmatch"), world, 0, FALSE))
439                                         error(strcat("Checkpoint ", ftos(i), " misses a spawnpoint with race_place==", ftos(self.race_place), " (used for initially spawning in race) - bailing out"));
440                         }
441                 }
442         }
443
444         g_race_qualifying = qual;
445
446         remove(self);
447         self = oldself;
448 }
449
450 void spawnfunc_trigger_race_checkpoint()
451 {
452         vector o;
453         if(!g_race)
454         {
455                 remove(self);
456                 return;
457         }
458
459         EXACTTRIGGER_INIT;
460
461         self.use = checkpoint_use;
462         if not(self.spawnflags & 1)
463                 self.touch = checkpoint_touch;
464
465         o = (self.absmin + self.absmax) * 0.5;
466         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
467         self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
468         self.nearestwaypointtimeout = time + 1000000000;
469
470         if(!self.message)
471                 self.message = "went backwards";
472         if (!self.message2)
473                 self.message2 = "was pushed backwards by";
474         
475         self.race_checkpoint = self.cnt;
476
477         if(self.race_checkpoint > race_highest_checkpoint)
478                 race_highest_checkpoint = self.race_checkpoint;
479
480         if(self.race_checkpoint)
481         {
482                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
483         }
484         else
485         {
486                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
487         }
488         self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
489
490         InitializeEntity(self, trigger_race_checkpoint_verify, INITPRIO_FINDTARGET);
491 }
492
493 void race_AbandonRaceCheck(entity p)
494 {
495         if(race_completing && !p.race_completed)
496         {
497                 p.race_completed = 1;
498                 MAKE_INDEPENDENT_PLAYER(p);
499                 bprint(p.netname, "^7 has abandoned the race.\n");
500                 ClientData_Touch(p);
501         }
502 }
503
504 void race_PreparePlayer()
505 {
506         race_ClearTime(self);
507         self.race_place = 0;
508 }
509
510 void race_RetractPlayer()
511 {
512         if(!g_race)
513                 return;
514         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
515         if(self.race_checkpoint == 0)
516         {
517                 race_ClearTime(self);
518                 self.race_checkpoint = 0;
519         }
520 }
521
522 void race_PreDie()
523 {
524         if(!g_race)
525                 return;
526
527         race_AbandonRaceCheck(self);
528 }
529
530 void race_PreSpawn()
531 {
532         if(!g_race)
533                 return;
534         if(self.killcount == -666 || g_race_qualifying)
535                 race_PreparePlayer();
536
537         race_AbandonRaceCheck(self);
538 }
539
540 void race_PostSpawn(entity spot)
541 {
542         if(!g_race)
543                 return;
544         if(self.killcount != -666 && !g_race_qualifying)
545         {
546                 if(spot.target == "")
547                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
548                         race_PreparePlayer();
549                 else
550                         race_RetractPlayer();
551         }
552
553         if(spot.target != "" && self.race_checkpoint == -1)
554                 self.race_checkpoint = 0;
555
556         self.race_place = 0;
557 }
558
559 void race_PreSpawnObserver()
560 {
561         if(!g_race)
562                 return;
563         race_PreparePlayer();
564 }
565
566 void spawnfunc_info_player_race (void)
567 {
568         if(!g_race)
569         {
570                 remove(self);
571                 return;
572         }
573         ++race_spawns;
574         spawnfunc_info_player_deathmatch();
575
576         if(self.race_place > race_highest_place_spawn)
577                 race_highest_place_spawn = self.race_place;
578         if(self.race_place < race_lowest_place_spawn)
579                 race_lowest_place_spawn = self.race_place;
580 }
581
582 void race_ClearRecords()
583 {
584         float i;
585         entity e;
586
587         for(i = 0; i < MAX_CHECKPOINTS; ++i)
588         {
589                 race_checkpoint_records[i] = 0;
590                 if(race_checkpoint_recordholders[i])
591                         strunzone(race_checkpoint_recordholders[i]);
592                 race_checkpoint_recordholders[i] = string_null;
593         }
594
595         FOR_EACH_CLIENT(e)
596                 race_ClearTime(e);
597 }
598
599 void race_ReadyRestart()
600 {
601         race_ClearRecords();
602
603         PlayerScore_Sort(race_place);
604
605         if(g_race_qualifying == 2)
606         {
607                 g_race_qualifying = 0;
608                 independent_players = 0;
609                 cvar_set("fraglimit", ftos(race_fraglimit));
610                 ScoreRules_race();
611         }
612 }