]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
support for announcer/male/impressive and announcer/male/headshot ;)
[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
313         if(other.classname == "porto")
314         {
315                 // do not allow portalling through checkpoints
316                 trace_plane_normal = normalize(-1 * other.velocity);
317                 self = other;
318                 W_Porto_Fail(0);
319                 return;
320         }
321
322         /*
323          * Trigger targets
324          */
325         if not(self.spawnflags & 2)
326         {
327                 activator = other;
328                 oldmsg = self.message;
329                 self.message = "";
330                 SUB_UseTargets();
331                 self.message = oldmsg;
332         }
333
334         if(other.classname != "player")
335                 return;
336
337         /*
338          * Remove unauthorized equipment
339          */
340         Portal_ClearAll(other);
341
342         other.porto_forbidden = 2; // decreased by 1 each StartFrame
343
344         if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
345         {
346                 /*
347                  * Trigger targets
348                  */
349                 if(self.spawnflags & 2)
350                 {
351                         activator = other;
352                         oldmsg = self.message;
353                         self.message = "";
354                         SUB_UseTargets();
355                         self.message = oldmsg;
356                 }
357
358                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
359
360                 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
361
362                 if(!self.race_checkpoint) // finish line
363                         other.race_laptime = time;
364
365                 if(g_race_qualifying)
366                         race_SendNextCheckpoint(other, 0);
367         }
368         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
369         {
370                 // ignored
371         }
372         else
373         {
374                 if(self.spawnflags & 4)
375                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
376         }
377 }
378
379 void checkpoint_touch()
380 {
381         EXACTTRIGGER_TOUCH;
382         checkpoint_passed();
383 }
384
385 void checkpoint_use()
386 {
387         other = activator;
388         checkpoint_passed();
389 }
390
391 float race_waypointsprite_visible_for_player(entity e)
392 {
393         if(e.race_checkpoint == -1)
394                 return TRUE;
395         else if(e.race_checkpoint == self.owner.race_checkpoint)
396                 return TRUE;
397         else
398                 return FALSE;
399 }
400
401 void spawnfunc_trigger_race_checkpoint()
402 {
403         vector o;
404         if(!g_race)
405         {
406                 remove(self);
407                 return;
408         }
409
410         EXACTTRIGGER_INIT;
411
412         self.use = checkpoint_use;
413         if not(self.spawnflags & 1)
414                 self.touch = checkpoint_touch;
415
416         o = (self.absmin + self.absmax) * 0.5;
417         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
418         self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
419         self.nearestwaypointtimeout = time + 1000000000;
420
421         if(!self.message)
422                 self.message = "went backwards";
423         if (!self.message2)
424                 self.message2 = "was pushed backwards by";
425         
426         self.race_checkpoint = self.cnt;
427
428         if(self.race_checkpoint > race_highest_checkpoint)
429                 race_highest_checkpoint = self.race_checkpoint;
430
431         if(self.race_checkpoint)
432         {
433                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
434         }
435         else
436         {
437                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
438         }
439         self.sprite.waypointsprite_visible_for_player = race_waypointsprite_visible_for_player;
440 }
441
442 void race_AbandonRaceCheck(entity p)
443 {
444         if(race_completing && !p.race_completed)
445         {
446                 p.race_completed = 1;
447                 MAKE_INDEPENDENT_PLAYER(p);
448                 bprint(p.netname, "^7 has abandoned the race.\n");
449                 // TODO support spectators with this (e.g. set a flag for forced sbar)
450                 if(clienttype(p) == CLIENTTYPE_REAL)
451                 {
452                         msg_entity = p;
453                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
454                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
455                         WriteByte(MSG_ONE, 1);
456                         // he can still move, but will see the scoreboard now
457                 }
458         }
459 }
460
461 void race_PreparePlayer()
462 {
463         race_ClearTime(self);
464         self.race_place = 0;
465 }
466
467 void race_RetractPlayer()
468 {
469         if(!g_race)
470                 return;
471         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
472         if(self.race_checkpoint == 0)
473         {
474                 race_ClearTime(self);
475                 self.race_checkpoint = 0;
476         }
477 }
478
479 void race_PreDie()
480 {
481         if(!g_race)
482                 return;
483
484         race_AbandonRaceCheck(self);
485 }
486
487 void race_PreSpawn()
488 {
489         if(!g_race)
490                 return;
491         if(self.killcount == -666 || g_race_qualifying)
492                 race_PreparePlayer();
493
494         race_AbandonRaceCheck(self);
495 }
496
497 void race_PostSpawn(entity spot)
498 {
499         if(!g_race)
500                 return;
501         if(self.killcount != -666 && !g_race_qualifying)
502         {
503                 if(spot.target == "")
504                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
505                         race_PreparePlayer();
506                 else
507                         race_RetractPlayer();
508         }
509
510         if(spot.target != "" && self.race_checkpoint == -1)
511                 self.race_checkpoint = 0;
512
513         self.race_place = 0;
514 }
515
516 void race_PreSpawnObserver()
517 {
518         if(!g_race)
519                 return;
520         race_PreparePlayer();
521 }
522
523 void spawnfunc_info_player_race (void)
524 {
525         if(!g_race)
526         {
527                 remove(self);
528                 return;
529         }
530         ++race_spawns;
531         spawnfunc_info_player_deathmatch();
532
533         if(self.race_place > race_highest_place_spawn)
534                 race_highest_place_spawn = self.race_place;
535         if(self.race_place < race_lowest_place_spawn)
536                 race_lowest_place_spawn = self.race_place;
537 }
538
539 void race_ClearRecords()
540 {
541         float i;
542         entity e;
543
544         for(i = 0; i < MAX_CHECKPOINTS; ++i)
545         {
546                 race_checkpoint_records[i] = 0;
547                 if(race_checkpoint_recordholders[i])
548                         strunzone(race_checkpoint_recordholders[i]);
549                 race_checkpoint_recordholders[i] = string_null;
550         }
551
552         FOR_EACH_CLIENT(e)
553                 race_ClearTime(e);
554 }
555
556 void race_ReadyRestart()
557 {
558         race_ClearRecords();
559
560         if(g_race_qualifying == 2)
561         {
562                 g_race_qualifying = 0;
563                 independent_players = 0;
564                 cvar_set("fraglimit", ftos(race_fraglimit));
565                 ScoreRules_race();
566         }
567
568         PlayerScore_Sort(race_place);
569 }