]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
cleanups, change nothing
[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 - restart_countdown));
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({
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({
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({
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_for_player(entity e)
400 {
401         if(e.race_checkpoint == -1)
402                 return self.modelindex;
403         else if(e.race_checkpoint == self.owner.race_checkpoint)
404                 return self.modelindex;
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                 precache_model("models/sprites/race-checkpoint.sp2");
440                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
441         }
442         else
443         {
444                 precache_model("models/sprites/race-finish.sp2");
445                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
446         }
447         self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
448 }
449
450 void race_AbandonRaceCheck(entity p)
451 {
452         if(race_completing && !p.race_completed)
453         {
454                 p.race_completed = 1;
455                 MAKE_INDEPENDENT_PLAYER(p);
456                 bprint(p.netname, "^7 has abandoned the race.\n");
457                 // TODO support spectators with this (e.g. set a flag for forced sbar)
458                 if(clienttype(p) == CLIENTTYPE_REAL)
459                 {
460                         msg_entity = p;
461                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
462                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
463                         WriteByte(MSG_ONE, 1);
464                         // he can still move, but will see the scoreboard now
465                 }
466         }
467 }
468
469 void race_PreparePlayer()
470 {
471         race_ClearTime(self);
472         self.race_place = 0;
473 }
474
475 void race_RetractPlayer()
476 {
477         if(!g_race)
478                 return;
479         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
480         if(self.race_checkpoint == 0)
481         {
482                 race_ClearTime(self);
483                 self.race_checkpoint = 0;
484         }
485 }
486
487 void race_PreDie()
488 {
489         if(!g_race)
490                 return;
491
492         race_AbandonRaceCheck(self);
493 }
494
495 void race_PreSpawn()
496 {
497         if(!g_race)
498                 return;
499         if(self.killcount == -666 || g_race_qualifying)
500                 race_PreparePlayer();
501
502         race_AbandonRaceCheck(self);
503 }
504
505 void race_PostSpawn(entity spot)
506 {
507         if(!g_race)
508                 return;
509         if(self.killcount != -666 && !g_race_qualifying)
510         {
511                 if(spot.target == "")
512                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
513                         race_PreparePlayer();
514                 else
515                         race_RetractPlayer();
516         }
517
518         if(spot.target != "" && self.race_checkpoint == -1)
519                 self.race_checkpoint = 0;
520
521         self.race_place = 0;
522 }
523
524 void race_PreSpawnObserver()
525 {
526         if(!g_race)
527                 return;
528         race_PreparePlayer();
529 }
530
531 void spawnfunc_info_player_race (void)
532 {
533         if(!g_race)
534         {
535                 remove(self);
536                 return;
537         }
538         ++race_spawns;
539         spawnfunc_info_player_deathmatch();
540
541         if(self.race_place > race_highest_place_spawn)
542                 race_highest_place_spawn = self.race_place;
543         if(self.race_place < race_lowest_place_spawn)
544                 race_lowest_place_spawn = self.race_place;
545 }
546
547 void race_ClearRecords()
548 {
549         float i;
550         entity e;
551
552         for(i = 0; i < MAX_CHECKPOINTS; ++i)
553         {
554                 race_checkpoint_records[i] = 0;
555                 if(race_checkpoint_recordholders[i])
556                         strunzone(race_checkpoint_recordholders[i]);
557                 race_checkpoint_recordholders[i] = string_null;
558         }
559
560         FOR_EACH_CLIENT(e)
561                 race_ClearTime(e);
562 }
563
564 void race_ReadyRestart()
565 {
566         race_ClearRecords();
567
568         if(g_race_qualifying == 2)
569         {
570                 g_race_qualifying = 0;
571                 independent_players = 0;
572                 cvar_set("fraglimit", ftos(race_fraglimit));
573                 ScoreRules_race();
574         }
575
576         PlayerScore_Sort(race_place);
577 }