]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/race.qc
Race: send stuff to people spectating someone too (incomplete)
[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 float race_highest_place_spawn;
16
17 float race_NextCheckpoint(float f)
18 {
19         if(f >= race_highest_checkpoint)
20                 return 0;
21         else
22                 return f + 1;
23 }
24
25 float race_PreviousCheckpoint(float f)
26 {
27         if(f == -1)
28                 return 0;
29         else if(f == 0)
30                 return race_highest_checkpoint;
31         else
32                 return f - 1;
33 }
34
35 void race_SendNextCheckpoint(entity e)
36 {
37         float recordtime;
38         string recordholder;
39         float cp;
40
41         if(!e.race_laptime)
42                 return;
43
44         cp = e.race_checkpoint;
45         recordtime = race_checkpoint_records[cp];
46         recordholder = race_checkpoint_recordholders[cp];
47         /*
48         recordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/time")));
49         recordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/", ftos(cp), "/netname"));
50         */
51         if(recordholder == e.netname)
52                 recordholder = "";
53
54         msg_entity = e;
55         WRITESPECTATABLE_MSG_ONE({
56                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
57                 WriteByte(MSG_ONE, TE_CSQC_RACE);
58                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_NEXT_QUALIFYING);
59                 WriteByte(MSG_ONE, cp); // checkpoint the player will be at next
60                 WriteShort(MSG_ONE, recordtime);
61                 WriteString(MSG_ONE, recordholder);
62         });
63 }
64
65 void race_SendTime(entity e, float cp, float t, float tvalid)
66 {
67         float snew, l;
68         t = floor(0.5 + 10 * t); // make integer
69
70         if(tvalid)
71         if(cp == 0) // finish line
72         if not(e.race_completed)
73         {
74                 float s;
75                 if(g_race_qualifying)
76                 {
77                         s = PlayerScore_Add(e, SP_RACE_FASTEST, 0);
78                         if(!s || t < s)
79                                 PlayerScore_Add(e, SP_RACE_FASTEST, t - s);
80                 }
81                 else
82                 {
83                         s = PlayerScore_Add(e, SP_RACE_TIME, 0);
84                         snew = floor(0.5 + 10 * (time - restart_countdown));
85                         PlayerScore_Add(e, SP_RACE_TIME, snew - s);
86                         l = PlayerTeamScore_Add(e, SP_RACE_LAPS, ST_RACE_LAPS, 1);
87
88                         if(cvar("fraglimit"))
89                                 if(l >= cvar("fraglimit"))
90                                         race_completing = 1;
91
92                         if(race_completing)
93                         {
94                                 e.race_completed = 1;
95                                 MAKE_INDEPENDENT_PLAYER(e);
96                                 bprint(e.netname, "^7 has finished the race.\n");
97                                 // TODO support spectators with this (e.g. set a flag for forced sbar)
98                                 if(clienttype(e) == CLIENTTYPE_REAL)
99                                 {
100                                         msg_entity = e;
101                                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
102                                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
103                                         WriteByte(MSG_ONE, 1);
104                                         // he can still move, but will see the scoreboard now
105                                 }
106                         }
107                 }
108         }
109
110         float recordtime;
111         string recordholder;
112
113         if(g_race_qualifying)
114         {
115                 if(tvalid)
116                 {
117                         recordtime = race_checkpoint_records[cp];
118                         recordholder = strcat1(race_checkpoint_recordholders[cp]); // make a tempstring copy, as we'll possibly strunzone it!
119                         if(recordholder == e.netname)
120                                 recordholder = "";
121
122                         if(t < recordtime || recordtime == 0)
123                         {
124                                 race_checkpoint_records[cp] = t;
125                                 if(race_checkpoint_recordholders[cp])
126                                         strunzone(race_checkpoint_recordholders[cp]);
127                                 race_checkpoint_recordholders[cp] = strzone(e.netname);
128                                 if(cp == 0)
129                                 {
130                                         float grecordtime;
131                                         string grecordholder;
132                                         grecordtime = stof(db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/time")));
133                                         grecordholder = db_get(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"));
134                                         if(grecordholder == e.netname)
135                                                 grecordholder = "";
136                                         if(grecordtime == 0)
137                                         {
138                                                 bprint(e.netname, "^7 set the all-time fastest lap record with ", mmsss(t), "\n");
139                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
140                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
141                                         }
142                                         else if(t < grecordtime)
143                                         {
144                                                 if(grecordholder == "")
145                                                         bprint(e.netname, "^7 broke his all-time fastest lap record with ", mmsss(t), "\n");
146                                                 else
147                                                         bprint(e.netname, "^7 broke ", grecordholder, "^7's all-time fastest lap record with ", mmsss(t), "\n");
148                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/time"), ftos(t));
149                                                 db_put(ServerProgsDB, strcat(GetMapname(), "/racerecord/netname"), e.netname);
150                                         }
151                                         else
152                                         {
153                                                 if(grecordholder == "")
154                                                         bprint(e.netname, "^7's new fastest lap could not break his all-time fastest lap record of ", mmsss(grecordtime), "\n");
155                                                 else
156                                                         bprint(e.netname, "^7's new fastest lap could not break ", grecordholder, "^7's all-time fastest lap record of ", mmsss(grecordtime), "\n");
157                                         }
158                                 }
159
160                                 if(g_race_qualifying)
161                                 {
162                                         entity p;
163                                         FOR_EACH_REALPLAYER(p)
164                                                 if(p.race_checkpoint == cp)
165                                                         race_SendNextCheckpoint(p);
166                                 }
167                         }
168                 }
169                 else
170                 {
171                         // dummies
172                         t = 0;
173                         recordtime = 0;
174                         recordholder = "";
175                 }
176
177                 msg_entity = e;
178                 if(g_race_qualifying)
179                 {
180                         WRITESPECTATABLE_MSG_ONE({
181                                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
182                                 WriteByte(MSG_ONE, TE_CSQC_RACE);
183                                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_QUALIFYING);
184                                 WriteByte(MSG_ONE, cp); // checkpoint the player now is at
185                                 WriteShort(MSG_ONE, t); // time to that intermediate
186                                 WriteShort(MSG_ONE, recordtime); // previously best time
187                                 WriteString(MSG_ONE, recordholder); // record holder
188                         });
189                 }
190         }
191         else // RACE! Not Qualifying
192         {
193                 float lself, lother, othtime;
194                 entity oth;
195                 oth = race_checkpoint_lastplayers[cp];
196                 if(oth)
197                 {
198                         lself = PlayerScore_Add(e, SP_RACE_LAPS, 0);
199                         lother = race_checkpoint_lastlaps[cp];
200                         othtime = race_checkpoint_lasttimes[cp];
201                 }
202                 else
203                         lself = lother = othtime = 0;
204
205                 msg_entity = e;
206                 WRITESPECTATABLE_MSG_ONE({
207                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
208                         WriteByte(MSG_ONE, TE_CSQC_RACE);
209                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE);
210                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
211                         if(e == oth)
212                         {
213                                 WriteShort(MSG_ONE, 0);
214                                 WriteByte(MSG_ONE, 0);
215                                 WriteString(MSG_ONE, "");
216                         }
217                         else
218                         {
219                                 WriteShort(MSG_ONE, floor(10 * (time - race_checkpoint_lasttimes[cp]) + 0.5));
220                                 WriteByte(MSG_ONE, lself - lother);
221                                 WriteString(MSG_ONE, oth.netname); // record holder
222                         }
223                 });
224
225                 race_checkpoint_lastplayers[cp] = e;
226                 race_checkpoint_lasttimes[cp] = time;
227                 race_checkpoint_lastlaps[cp] = lself;
228
229                 msg_entity = oth;
230                 WRITESPECTATABLE_MSG_ONE({
231                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
232                         WriteByte(MSG_ONE, TE_CSQC_RACE);
233                         WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT);
234                         WriteByte(MSG_ONE, cp); // checkpoint the player now is at
235                         if(e == oth)
236                         {
237                                 WriteShort(MSG_ONE, 0);
238                                 WriteByte(MSG_ONE, 0);
239                                 WriteString(MSG_ONE, "");
240                         }
241                         else
242                         {
243                                 WriteShort(MSG_ONE, floor(10 * (time - othtime) + 0.5));
244                                 WriteByte(MSG_ONE, lother - lself);
245                                 WriteString(MSG_ONE, e.netname); // record holder
246                         }
247                 });
248         }
249 }
250
251 void race_ClearTime(entity e)
252 {
253         e.race_checkpoint = -1;
254         e.race_laptime = 0;
255
256         msg_entity = e;
257         WRITESPECTATABLE_MSG_ONE({
258                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
259                 WriteByte(MSG_ONE, TE_CSQC_RACE);
260                 WriteByte(MSG_ONE, RACE_NET_CHECKPOINT_CLEAR); // next
261         });
262 }
263
264 void checkpoint_touch()
265 {
266         if(other.classname != "player")
267                 return;
268
269         if(other.race_checkpoint == -1 || other.race_checkpoint == self.race_checkpoint)
270         {
271                 other.race_checkpoint = race_NextCheckpoint(self.race_checkpoint);
272
273                 race_SendTime(other, self.race_checkpoint, time - other.race_laptime, !!other.race_laptime);
274
275                 if(!self.race_checkpoint) // finish line
276                         other.race_laptime = time;
277
278                 if(g_race_qualifying)
279                         race_SendNextCheckpoint(other);
280         }
281         else if(other.race_checkpoint == race_NextCheckpoint(self.race_checkpoint))
282         {
283                 // ignored
284         }
285         else
286         {
287                 if(self.spawnflags & 4)
288                         Damage (other, self, self, 10000, DEATH_HURTTRIGGER, other.origin, '0 0 0');
289         }
290 }
291
292 void checkpoint_use()
293 {
294         other = activator;
295         checkpoint_touch();
296 }
297
298 float race_waypointsprite_for_player(entity e)
299 {
300         if(e.race_checkpoint == -1)
301                 return self.modelindex;
302         else if(e.race_checkpoint == self.owner.race_checkpoint)
303                 return self.modelindex;
304         else
305                 return FALSE;
306 }
307
308 void spawnfunc_trigger_race_checkpoint()
309 {
310         vector o;
311         if(!g_race)
312         {
313                 remove(self);
314                 return;
315         }
316         InitTrigger();
317         self.use = checkpoint_use;
318         if not(self.spawnflags & 1)
319                 self.touch = checkpoint_touch;
320
321         o = (self.absmin + self.absmax) * 0.5;
322         tracebox(o, PL_MIN, PL_MAX, o - '0 0 1' * (o_z - self.absmin_z), MOVE_NORMAL, self);
323         self.nearestwaypoint = waypoint_spawn(trace_endpos, trace_endpos, WAYPOINTFLAG_GENERATED);
324         self.nearestwaypointtimeout = time + 1000000000;
325
326         if(!self.message)
327                 self.message = "went backwards";
328         
329         self.race_checkpoint = self.cnt;
330
331         if(self.race_checkpoint > race_highest_checkpoint)
332                 race_highest_checkpoint = self.race_checkpoint;
333
334         if(self.race_checkpoint)
335         {
336                 precache_model("models/sprites/race-checkpoint.sp2");
337                 WaypointSprite_SpawnFixed("race-checkpoint", o, self, sprite);
338         }
339         else
340         {
341                 precache_model("models/sprites/race-finish.sp2");
342                 WaypointSprite_SpawnFixed("race-finish", o, self, sprite);
343         }
344         self.sprite.waypointsprite_for_player = race_waypointsprite_for_player;
345 }
346
347 void race_PreparePlayer()
348 {
349         race_ClearTime(self);
350         self.race_place = 0;
351 }
352
353 void race_RetractPlayer()
354 {
355         if(!g_race)
356                 return;
357         self.race_checkpoint = race_PreviousCheckpoint(self.race_checkpoint);
358         if(self.race_checkpoint == 0)
359         {
360                 race_ClearTime(self);
361                 self.race_checkpoint = 0;
362         }
363 }
364
365 void race_PreSpawn()
366 {
367         if(!g_race)
368                 return;
369         if(self.killcount == -666 || g_race_qualifying)
370                 race_PreparePlayer();
371
372         if(race_completing)
373         {
374                 self.race_completed = 1;
375                 MAKE_INDEPENDENT_PLAYER(self);
376                 bprint(self.netname, "^7 has abandoned the race.\n");
377                 // TODO support spectators with this (e.g. set a flag for forced sbar)
378                 if(clienttype(self) == CLIENTTYPE_REAL)
379                 {
380                         msg_entity = self;
381                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
382                         WriteByte(MSG_ONE, TE_CSQC_FORCESCOREBOARD);
383                         WriteByte(MSG_ONE, 1);
384                         // he can still move, but will see the scoreboard now
385                 }
386         }
387 }
388
389 void race_PostSpawn(entity spot)
390 {
391         if(!g_race)
392                 return;
393         if(self.killcount != -666 && !g_race_qualifying)
394         {
395                 if(spot.target == "")
396                         // let the player run without timing, if he did not spawn at a targetting spawnpoint
397                         race_PreparePlayer();
398                 else
399                         race_RetractPlayer();
400         }
401
402         if(spot.target != "" && self.race_checkpoint == -1)
403                 self.race_checkpoint = 0;
404
405         self.race_place = 0;
406 }
407
408 void race_PreSpawnObserver()
409 {
410         if(!g_race)
411                 return;
412         race_PreparePlayer();
413 }
414
415 void spawnfunc_info_player_race (void)
416 {
417         if(!g_race)
418         {
419                 remove(self);
420                 return;
421         }
422         ++race_spawns;
423         spawnfunc_info_player_deathmatch();
424
425         if(self.race_place > race_highest_place_spawn)
426                 race_highest_place_spawn = self.race_place;
427 }
428
429 void race_ClearRecords()
430 {
431         float i;
432         entity e;
433
434         for(i = 0; i < MAX_CHECKPOINTS; ++i)
435         {
436                 race_checkpoint_records[i] = 0;
437                 if(race_checkpoint_recordholders[i])
438                         strunzone(race_checkpoint_recordholders[i]);
439                 race_checkpoint_recordholders[i] = string_null;
440         }
441
442         FOR_EACH_CLIENT(e)
443                 race_ClearTime(e);
444 }
445
446 void race_ReadyRestart()
447 {
448         race_ClearRecords();
449
450         if(g_race_qualifying == 2)
451         {
452                 g_race_qualifying = 0;
453                 independent_players = 0;
454                 cvar_set("fraglimit", ftos(race_fraglimit));
455                 ScoreRules_race();
456         }
457
458         PlayerScore_Sort(race_place);
459 }