]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
support race_place -1 to mark a qualifying-only finish spawn point
[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         t = floor(0.5 + 10 * t); // make integer
82
83         if(tvalid)
84         if(cp == 0) // finish line
85         if not(e.race_completed)
86         {
87                 float s;
88                 if(g_race_qualifying)
89                 {
90                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
91                         if(!s || t < s)
92                                 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
93                 }
94                 else
95                 {
96                         s = PlayerScore_Add(e, SP_RACE_TIME, 0);
97                         snew = floor(0.5 + 10 * (time - restart_countdown));
98                         PlayerScore_Add(e, SP_RACE_TIME, snew - s);
99                         l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
100
101                         if(cvar("fraglimit"))
102                                 if(l >= cvar("fraglimit"))
103                                         race_completing = 1;
104
105                         if(race_completing)
106                         {
107                                 e.race_completed = 1;
108                                 MAKE_INDEPENDENT_PLAYER(e);
109                                 bprint(e.netname, "^7 has finished the race.\n");
110                                 // TODO support spectators with this (e.g. set a flag for forced sbar)
111                                 if(clienttype(e) == CLIENTTYPE_REAL)
112                                 {
113                                         msg_entity = e;
114                                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
115                                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
116                                         WriteByte(MSG_ONE, 1);
117                                         // he can still move, but will see the scoreboard now
118                                 }
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 < recordtime || recordtime == 0)
136                         {
137                                 race_checkpoint_records[cp] = t;
138                                 if(race_checkpoint_recordholders[cp])
139                                         strunzone(race_checkpoint_recordholders[cp]);
140                                 race_checkpoint_recordholders[cp] = strzone(e.netname);
141                                 if(cp == 0)
142                                 {
143                                         float grecordtime;
144                                         string grecordholder;
145                                         grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
146                                         grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
147                                         if(grecordholder == e.netname)
148                                                 grecordholder = "";
149                                         if(grecordtime == 0)
150                                         {
151                                                 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
152                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
153                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
154                                                 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
155                                         }
156                                         else if(t < grecordtime)
157                                         {
158                                                 if(grecordholder == "")
159                                                         bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
160                                                 else
161                                                         bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
162                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
163                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
164                                                 GameLogEcho(strcat(":recordset:", ftos(e.playerid), ":", ftos(t / 10)));
165                                         }
166                                         else
167                                         {
168                                                 if(grecordholder == "")
169                                                         bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
170                                                 else
171                                                         bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
172                                         }
173                                 }
174
175                                 if(g_race_qualifying)
176                                 {
177                                         entity p;
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({
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({
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({
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         entity oldself;
307
308         if(other.classname == "porto")
309         {
310                 // do not allow portalling through checkpoints
311                 trace_plane_normal = normalize(-1 * other.velocity);
312                 self = other;
313                 W_Porto_Fail(0);
314                 return;
315         }
316
317         /*
318          * Trigger targets
319          */
320         if not(self.spawnflags & 2)
321         {
322                 activator = other;
323                 oldmsg = self.message;
324                 self.message = "";
325                 SUB_UseTargets();
326                 self.message = oldmsg;
327         }
328
329         if(other.classname != "player")
330                 return;
331
332         /*
333          * Remove unauthorized equipment
334          */
335         Portal_ClearAll(other);
336         if(other.porto_current)
337         {
338                 oldself = self;
339                 self = other.porto_current;
340                 W_Porto_Fail(1);
341                 self = oldself;
342         }
343
344         other.porto_forbidden = 2; // decreased by 1 each StartFrame
345
346         if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
347         {
348                 /*
349                  * Trigger targets
350                  */
351                 if(self.spawnflags & 2)
352                 {
353                         activator = other;
354                         oldmsg = self.message;
355                         self.message = "";
356                         SUB_UseTargets();
357                         self.message = oldmsg;
358                 }
359
360                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
361
362                 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
363
364                 if(!self.race_checkpoint) // finish line
365                         other.race_laptime = time;
366
367                 if(g_race_qualifying)
368                         race_SendNextCheckpoint(other, 0);
369         }
370         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
371         {
372                 // ignored
373         }
374         else
375         {
376                 if(self.spawnflags & 4)
377                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
378         }
379 }
380
381 void checkpoint_touch()
382 {
383         EXACTTRIGGER_TOUCH;
384         checkpoint_passed();
385 }
386
387 void checkpoint_use()
388 {
389         other = activator;
390         checkpoint_passed();
391 }
392
393 float race_waypointsprite_for_player(entity e)
394 {
395         if(e.race_checkpoint == -1)
396                 return self.modelindex;
397         else if(e.race_checkpoint == self.owner.race_checkpoint)
398                 return self.modelindex;
399         else
400                 return FALSE;
401 }
402
403 void spawnfunc_trigger_race_checkpoint()
404 {
405         vector o;
406         if(!g_race)
407         {
408                 remove(self);
409                 return;
410         }
411
412         EXACTTRIGGER_INIT;
413
414         self.use = checkpoint_use;
415         if not(self.spawnflags & 1)
416                 self.touch = checkpoint_touch;
417
418         o = (self.absmin + self.absmax) * 0.5;
419         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
420         self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
421         self.nearestwaypointtimeout = time + 1000000000;
422
423         if(!self.message)
424                 self.message = "went backwards";
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                 precache_model("models/sprites/race-checkpoint.sp2");
434                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
435         }
436         else
437         {
438                 precache_model("models/sprites/race-finish.sp2");
439                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
440         }
441         self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
442 }
443
444 void race_PreparePlayer()
445 {
446         race_ClearTime(self);
447         self.race_place = 0;
448 }
449
450 void race_RetractPlayer()
451 {
452         if(!g_race)
453                 return;
454         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
455         if(self.race_checkpoint == 0)
456         {
457                 race_ClearTime(self);
458                 self.race_checkpoint = 0;
459         }
460 }
461
462 void race_PreSpawn()
463 {
464         if(!g_race)
465                 return;
466         if(self.killcount == -666 || g_race_qualifying)
467                 race_PreparePlayer();
468
469         if(race_completing)
470         {
471                 self.race_completed = 1;
472                 MAKE_INDEPENDENT_PLAYER(self);
473                 bprint(self.netname, "^7 has abandoned the race.\n");
474                 // TODO support spectators with this (e.g. set a flag for forced sbar)
475                 if(clienttype(self) == CLIENTTYPE_REAL)
476                 {
477                         msg_entity = self;
478                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
479                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
480                         WriteByte(MSG_ONE, 1);
481                         // he can still move, but will see the scoreboard now
482                 }
483         }
484 }
485
486 void race_PostSpawn(entity spot)
487 {
488         if(!g_race)
489                 return;
490         if(self.killcount != -666 && !g_race_qualifying)
491         {
492                 if(spot.target == "")
493                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
494                         race_PreparePlayer();
495                 else
496                         race_RetractPlayer();
497         }
498
499         if(spot.target != "" && self.race_checkpoint == -1)
500                 self.race_checkpoint = 0;
501
502         self.race_place = 0;
503 }
504
505 void race_PreSpawnObserver()
506 {
507         if(!g_race)
508                 return;
509         race_PreparePlayer();
510 }
511
512 void spawnfunc_info_player_race (void)
513 {
514         if(!g_race)
515         {
516                 remove(self);
517                 return;
518         }
519         ++race_spawns;
520         spawnfunc_info_player_deathmatch();
521
522         if(self.race_place > race_highest_place_spawn)
523                 race_highest_place_spawn = self.race_place;
524         if(self.race_place < race_lowest_place_spawn)
525                 race_lowest_place_spawn = self.race_place;
526 }
527
528 void race_ClearRecords()
529 {
530         float i;
531         entity e;
532
533         for(i = 0; i < MAX_CHECKPOINTS; ++i)
534         {
535                 race_checkpoint_records[i] = 0;
536                 if(race_checkpoint_recordholders[i])
537                         strunzone(race_checkpoint_recordholders[i]);
538                 race_checkpoint_recordholders[i] = string_null;
539         }
540
541         FOR_EACH_CLIENT(e)
542                 race_ClearTime(e);
543 }
544
545 void race_ReadyRestart()
546 {
547         race_ClearRecords();
548
549         if(g_race_qualifying == 2)
550         {
551                 g_race_qualifying = 0;
552                 independent_players = 0;
553                 cvar_set("fraglimit", ftos(race_fraglimit));
554                 ScoreRules_race();
555         }
556
557         PlayerScore_Sort(race_place);
558 }