]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
fix timelimit display after ready-restart (no longer 20.166666667)
[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.5 + 10 * t); // make integer
84
85         if(tvalid)
86         if(cp == 0) // finish line
87         if not(e.race_completed)
88         {
89                 float s;
90                 if(g_race_qualifying)
91                 {
92                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
93                         if(!s || t < s)
94                                 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
95                 }
96                 else
97                 {
98                         s = PlayerScore_Add(e, SP_RACE_TIME, 0);
99                         snew = floor(0.5 + 10 * (time - game_starttime));
100                         PlayerScore_Add(e, SP_RACE_TIME, snew - s);
101                         l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
102
103                         if(cvar("fraglimit"))
104                                 if(l >= cvar("fraglimit"))
105                                 {
106                                         race_completing = 1;
107                                         FOR_EACH_PLAYER(p)
108                                                 if(p.deadflag != DEAD_NO)
109                                                         race_AbandonRaceCheck(p);
110                                 }
111
112                         if(race_completing)
113                         {
114                                 e.race_completed = 1;
115                                 MAKE_INDEPENDENT_PLAYER(e);
116                                 bprint(e.netname, "^7 has finished the race.\n");
117                                 // TODO support spectators with this (e.g. set a flag for forced sbar)
118                                 if(clienttype(e) == CLIENTTYPE_REAL)
119                                 {
120                                         msg_entity = e;
121                                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
122                                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
123                                         WriteByte(MSG_ONE, 1);
124                                         // he can still move, but will see the scoreboard now
125                                 }
126                         }
127                 }
128         }
129
130         float recordtime;
131         string recordholder;
132
133         if(g_race_qualifying)
134         {
135                 if(tvalid)
136                 {
137                         recordtime = race_checkpoint_records[cp];
138                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
139                         if(recordholder == e.netname)
140                                 recordholder = "";
141
142                         if(t < recordtime || recordtime == 0)
143                         {
144                                 race_checkpoint_records[cp] = t;
145                                 if(race_checkpoint_recordholders[cp])
146                                         strunzone(race_checkpoint_recordholders[cp]);
147                                 race_checkpoint_recordholders[cp] = strzone(e.netname);
148                                 if(cp == 0)
149                                 {
150                                         float grecordtime;
151                                         string grecordholder;
152                                         grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
153                                         grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
154                                         if(grecordholder == e.netname)
155                                                 grecordholder = "";
156                                         if(grecordtime == 0)
157                                         {
158                                                 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
159                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
160                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
161                                                 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
162                                         }
163                                         else if(t < grecordtime)
164                                         {
165                                                 if(grecordholder == "")
166                                                         bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
167                                                 else
168                                                         bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
169                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
170                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
171                                                 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
172                                         }
173                                         else
174                                         {
175                                                 if(grecordholder == "")
176                                                         bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
177                                                 else
178                                                         bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
179                                         }
180                                 }
181
182                                 if(g_race_qualifying)
183                                 {
184                                         FOR_EACH_REALPLAYER(p)
185                                                 if(p.race_checkpoint == cp)
186                                                         race_SendNextCheckpoint(p, 0);
187                                 }
188                         }
189                 }
190                 else
191                 {
192                         // dummies
193                         t = 0;
194                         recordtime = 0;
195                         recordholder = "";
196                 }
197
198                 msg_entity = e;
199                 if(g_race_qualifying)
200                 {
201                         WRITESPECTATABLE_MSG_ONE_VARNAME(dummy1, {
202                                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
203                                 WriteByte(MSG_ONE, TE_CSQC_RACE);
204                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
205                                 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
206                                 WriteShort(MSG_ONE, t); // time to that intermediate
207                                 WriteShort(MSG_ONE, recordtime); // previously best time
208                                 WriteString(MSG_ONE, recordholder); // record holder
209                         });
210                 }
211         }
212         else // RACE! Not Qualifying
213         {
214                 float lself, lother, othtime;
215                 entity oth;
216                 oth = race_checkpoint_lastplayers[cp];
217                 if(oth)
218                 {
219                         lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
220                         lother = race_checkpoint_lastlaps[cp];
221                         othtime = race_checkpoint_lasttimes[cp];
222                 }
223                 else
224                         lself = lother = othtime = 0;
225
226                 msg_entity = e;
227                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy2, {
228                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
229                         WriteByte(MSG_ONE, TE_CSQC_RACE);
230                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
231                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
232                         if(e == oth)
233                         {
234                                 WriteShort(MSG_ONE, 0);
235                                 WriteByte(MSG_ONE, 0);
236                                 WriteString(MSG_ONE, "");
237                         }
238                         else
239                         {
240                                 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
241                                 WriteByte(MSG_ONE, lself - lother);
242                                 WriteString(MSG_ONE, oth.netname); // record holder
243                         }
244                 });
245
246                 race_checkpoint_lastplayers[cp] = e;
247                 race_checkpoint_lasttimes[cp] = time;
248                 race_checkpoint_lastlaps[cp] = lself;
249
250                 msg_entity = oth;
251                 WRITESPECTATABLE_MSG_ONE_VARNAME(dummy3, {
252                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
253                         WriteByte(MSG_ONE, TE_CSQC_RACE);
254                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
255                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
256                         if(e == oth)
257                         {
258                                 WriteShort(MSG_ONE, 0);
259                                 WriteByte(MSG_ONE, 0);
260                                 WriteString(MSG_ONE, "");
261                         }
262                         else
263                         {
264                                 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
265                                 WriteByte(MSG_ONE, lother - lself);
266                                 WriteString(MSG_ONE, e.netname); // record holder
267                         }
268                 });
269         }
270 }
271
272 void race_ClearTime(entity e)
273 {
274         e.race_checkpoint = -1;
275         e.race_laptime = 0;
276
277         msg_entity = e;
278         WRITESPECTATABLE_MSG_ONE({
279                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
280                 WriteByte(MSG_ONE, TE_CSQC_RACE);
281                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
282         });
283 }
284
285 void dumpsurface(entity e)
286 {
287         float n, si, ni;
288         vector norm, vec;
289         print("Surfaces of ", etos(e), ":\n");
290
291         print("TEST = ", ftos(getsurfacenearpoint(e, '0 0 0')), "\n");
292
293         for(si = 0; ; ++si)
294         {
295                 n = getsurfacenumpoints(e, si);
296                 if(n <= 0)
297                         break;
298                 print("  Surface ", ftos(si), ":\n");
299                 norm = getsurfacenormal(e, si);
300                 print("    Normal = ", vtos(norm), "\n");
301                 for(ni = 0; ni < n; ++ni)
302                 {
303                         vec = getsurfacepoint(e, si, ni);
304                         print("    Point ", ftos(ni), " = ", vtos(vec), " (", ftos(norm * vec), ")\n");
305                 }
306         }
307 }
308
309 void checkpoint_passed()
310 {
311         string oldmsg;
312         entity oldself;
313
314         if(other.classname == "porto")
315         {
316                 // do not allow portalling through checkpoints
317                 trace_plane_normal = normalize(-1 * other.velocity);
318                 self = other;
319                 W_Porto_Fail(0);
320                 return;
321         }
322
323         /*
324          * Trigger targets
325          */
326         if not(self.spawnflags & 2)
327         {
328                 activator = other;
329                 oldmsg = self.message;
330                 self.message = "";
331                 SUB_UseTargets();
332                 self.message = oldmsg;
333         }
334
335         if(other.classname != "player")
336                 return;
337
338         /*
339          * Remove unauthorized equipment
340          */
341         Portal_ClearAll(other);
342         if(other.porto_current)
343         {
344                 oldself = self;
345                 self = other.porto_current;
346                 W_Porto_Fail(1);
347                 self = oldself;
348         }
349
350         other.porto_forbidden = 2; // decreased by 1 each StartFrame
351
352         if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
353         {
354                 /*
355                  * Trigger targets
356                  */
357                 if(self.spawnflags & 2)
358                 {
359                         activator = other;
360                         oldmsg = self.message;
361                         self.message = "";
362                         SUB_UseTargets();
363                         self.message = oldmsg;
364                 }
365
366                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
367
368                 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
369
370                 if(!self.race_checkpoint) // finish line
371                         other.race_laptime = time;
372
373                 if(g_race_qualifying)
374                         race_SendNextCheckpoint(other, 0);
375         }
376         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
377         {
378                 // ignored
379         }
380         else
381         {
382                 if(self.spawnflags & 4)
383                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
384         }
385 }
386
387 void checkpoint_touch()
388 {
389         EXACTTRIGGER_TOUCH;
390         checkpoint_passed();
391 }
392
393 void checkpoint_use()
394 {
395         other = activator;
396         checkpoint_passed();
397 }
398
399 float race_waypointsprite_visible_for_player(entity e)
400 {
401         if(e.race_checkpoint == -1)
402                 return TRUE;
403         else if(e.race_checkpoint == self.owner.race_checkpoint)
404                 return TRUE;
405         else
406                 return FALSE;
407 }
408
409 void spawnfunc_trigger_race_checkpoint()
410 {
411         vector o;
412         if(!g_race)
413         {
414                 remove(self);
415                 return;
416         }
417
418         EXACTTRIGGER_INIT;
419
420         self.use = checkpoint_use;
421         if not(self.spawnflags & 1)
422                 self.touch = checkpoint_touch;
423
424         o = (self.absmin + self.absmax) * 0.5;
425         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
426         self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
427         self.nearestwaypointtimeout = time + 1000000000;
428
429         if(!self.message)
430                 self.message = "went backwards";
431         
432         self.race_checkpoint = self.cnt;
433
434         if(self.race_checkpoint > race_highest_checkpoint)
435                 race_highest_checkpoint = self.race_checkpoint;
436
437         if(self.race_checkpoint)
438         {
439                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
440         }
441         else
442         {
443                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
444         }
445         self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
446 }
447
448 void race_AbandonRaceCheck(entity p)
449 {
450         if(race_completing && !p.race_completed)
451         {
452                 p.race_completed = 1;
453                 MAKE_INDEPENDENT_PLAYER(p);
454                 bprint(p.netname, "^7 has abandoned the race.\n");
455                 // TODO support spectators with this (e.g. set a flag for forced sbar)
456                 if(clienttype(p) == CLIENTTYPE_REAL)
457                 {
458                         msg_entity = p;
459                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
460                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
461                         WriteByte(MSG_ONE, 1);
462                         // he can still move, but will see the scoreboard now
463                 }
464         }
465 }
466
467 void race_PreparePlayer()
468 {
469         race_ClearTime(self);
470         self.race_place = 0;
471 }
472
473 void race_RetractPlayer()
474 {
475         if(!g_race)
476                 return;
477         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
478         if(self.race_checkpoint == 0)
479         {
480                 race_ClearTime(self);
481                 self.race_checkpoint = 0;
482         }
483 }
484
485 void race_PreDie()
486 {
487         if(!g_race)
488                 return;
489
490         race_AbandonRaceCheck(self);
491 }
492
493 void race_PreSpawn()
494 {
495         if(!g_race)
496                 return;
497         if(self.killcount == -666 || g_race_qualifying)
498                 race_PreparePlayer();
499
500         race_AbandonRaceCheck(self);
501 }
502
503 void race_PostSpawn(entity spot)
504 {
505         if(!g_race)
506                 return;
507         if(self.killcount != -666 && !g_race_qualifying)
508         {
509                 if(spot.target == "")
510                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
511                         race_PreparePlayer();
512                 else
513                         race_RetractPlayer();
514         }
515
516         if(spot.target != "" && self.race_checkpoint == -1)
517                 self.race_checkpoint = 0;
518
519         self.race_place = 0;
520 }
521
522 void race_PreSpawnObserver()
523 {
524         if(!g_race)
525                 return;
526         race_PreparePlayer();
527 }
528
529 void spawnfunc_info_player_race (void)
530 {
531         if(!g_race)
532         {
533                 remove(self);
534                 return;
535         }
536         ++race_spawns;
537         spawnfunc_info_player_deathmatch();
538
539         if(self.race_place > race_highest_place_spawn)
540                 race_highest_place_spawn = self.race_place;
541         if(self.race_place < race_lowest_place_spawn)
542                 race_lowest_place_spawn = self.race_place;
543 }
544
545 void race_ClearRecords()
546 {
547         float i;
548         entity e;
549
550         for(i = 0; i < MAX_CHECKPOINTS; ++i)
551         {
552                 race_checkpoint_records[i] = 0;
553                 if(race_checkpoint_recordholders[i])
554                         strunzone(race_checkpoint_recordholders[i]);
555                 race_checkpoint_recordholders[i] = string_null;
556         }
557
558         FOR_EACH_CLIENT(e)
559                 race_ClearTime(e);
560 }
561
562 void race_ReadyRestart()
563 {
564         race_ClearRecords();
565
566         if(g_race_qualifying == 2)
567         {
568                 g_race_qualifying = 0;
569                 independent_players = 0;
570                 cvar_set("fraglimit", ftos(race_fraglimit));
571                 ScoreRules_race();
572         }
573
574         PlayerScore_Sort(race_place);
575 }