]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/main.qc
Minigames: fix fatal error
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / main.qc
1 #include "main.qh"
2
3 #include "../common/effects/qc/all.qh"
4 #include "hook.qh"
5 #include "hud/all.qh"
6 #include "mapvoting.qh"
7 #include "mutators/events.qh"
8 #include "quickmenu.qh"
9 #include "scoreboard.qh"
10 #include "shownames.qh"
11 #include "tuba.qh"
12 #include "t_items.qh"
13 #include "wall.qh"
14 #include "weapons/projectile.qh"
15 #include "../common/deathtypes/all.qh"
16 #include "../common/items/all.qh"
17 #include "../common/mapinfo.qh"
18 #include "../common/minigames/cl_minigames.qh"
19 #include "../common/minigames/cl_minigames_hud.qh"
20 #include "../common/net_notice.qh"
21 #include "../common/triggers/include.qh"
22 #include "../common/vehicles/all.qh"
23 #include "../lib/csqcmodel/cl_model.qh"
24 #include "../lib/csqcmodel/interpolate.qh"
25 #include "../lib/warpzone/client.qh"
26
27 // --------------------------------------------------------------------------
28 // BEGIN REQUIRED CSQC FUNCTIONS
29 //include "main.qh"
30
31 #define DP_CSQC_ENTITY_REMOVE_IS_B0RKED
32
33 // CSQC_Init : Called every time the CSQC code is initialized (essentially at map load)
34 // Useful for precaching things
35
36 void ConsoleCommand_macro_init();
37 void CSQC_Init()
38 {
39         prvm_language = strzone(cvar_string("prvm_language"));
40
41 #ifdef WATERMARK
42         LOG_INFOF("^4CSQC Build information: ^1%s\n", WATERMARK);
43 #endif
44
45         binddb = db_create();
46         tempdb = db_create();
47         ClientProgsDB = db_load("client.db");
48         compressShortVector_init();
49
50         draw_endBoldFont();
51
52         {
53                 int i = 0;
54                 for ( ; i < 255; ++i)
55                         if (getplayerkeyvalue(i, "viewentity") == "")
56                                 break;
57                 maxclients = i;
58         }
59
60         //registercommand("hud_configure");
61         //registercommand("hud_save");
62         //registercommand("menu_action");
63
64         ConsoleCommand_macro_init();
65
66         registercvar("hud_usecsqc", "1");
67         registercvar("scoreboard_columns", "default");
68
69         registercvar("cl_nade_type", "3");
70         registercvar("cl_pokenade_type", "zombie");
71
72         registercvar("cl_jumpspeedcap_min", "");
73         registercvar("cl_jumpspeedcap_max", "");
74
75         registercvar("cl_multijump", "0");
76
77         registercvar("cl_spawn_near_teammate", "1");
78
79         gametype = 0;
80
81         // hud_fields uses strunzone on the titles!
82         for(int i = 0; i < MAX_HUD_FIELDS; ++i)
83                 hud_title[i] = strzone("(null)");
84
85         Cmd_HUD_SetFields(0);
86
87         postinit = false;
88
89         calledhooks = 0;
90
91         teams = Sort_Spawn();
92         players = Sort_Spawn();
93
94         GetTeam(NUM_SPECTATOR, true); // add specs first
95
96         // needs to be done so early because of the constants they create
97         static_init();
98         static_init_late();
99         static_init_precache();
100
101         // precaches
102
103         if(autocvar_cl_reticle)
104         {
105                 precache_pic("gfx/reticle_normal");
106                 // weapon reticles are precached in weapon files
107         }
108
109         {
110                 get_mi_min_max_texcoords(1); // try the CLEVER way first
111                 minimapname = strcat("gfx/", mi_shortname, "_radar.tga");
112                 shortmapname = mi_shortname;
113
114                 if (precache_pic(minimapname) == "")
115                 {
116                         // but maybe we have a non-clever minimap
117                         minimapname = strcat("gfx/", mi_shortname, "_mini.tga");
118                         if (precache_pic(minimapname) == "")
119                                 minimapname = ""; // FAIL
120                         else
121                                 get_mi_min_max_texcoords(0); // load new texcoords
122                 }
123
124                 mi_center = (mi_min + mi_max) * 0.5;
125                 mi_scale = mi_max - mi_min;
126                 minimapname = strzone(minimapname);
127         }
128
129         hud_skin_path = strzone(strcat("gfx/hud/", autocvar_hud_skin));
130         draw_currentSkin = strzone(strcat("gfx/menu/", cvar_string("menu_skin")));
131 }
132
133 // CSQC_Shutdown : Called every time the CSQC code is shutdown (changing maps, quitting, etc)
134 void Shutdown()
135 {
136         WarpZone_Shutdown();
137
138         remove(teams);
139         remove(players);
140         db_close(binddb);
141         db_close(tempdb);
142         if(autocvar_cl_db_saveasdump)
143                 db_dump(ClientProgsDB, "client.db");
144         else
145                 db_save(ClientProgsDB, "client.db");
146         db_close(ClientProgsDB);
147
148         if(camera_active)
149                 cvar_set("chase_active",ftos(chase_active_backup));
150
151         // unset the event chasecam's chase_active
152         if(autocvar_chase_active < 0)
153                 cvar_set("chase_active", "0");
154
155         if (!isdemo())
156         {
157                 if (!(calledhooks & HOOK_START))
158                         localcmd("\n_cl_hook_gamestart nop\n");
159                 if (!(calledhooks & HOOK_END))
160                         localcmd("\ncl_hook_gameend\n");
161         }
162
163         deactivate_minigame();
164         HUD_MinigameMenu_Close();
165 }
166
167 .float has_team;
168 float SetTeam(entity o, int Team)
169 {
170         entity tm;
171         if(teamplay)
172         {
173                 switch(Team)
174                 {
175                         case -1:
176                         case NUM_TEAM_1:
177                         case NUM_TEAM_2:
178                         case NUM_TEAM_3:
179                         case NUM_TEAM_4:
180                                 break;
181                         default:
182                                 if(GetTeam(Team, false) == world)
183                                 {
184                                         LOG_TRACEF("trying to switch to unsupported team %d\n", Team);
185                                         Team = NUM_SPECTATOR;
186                                 }
187                                 break;
188                 }
189         }
190         else
191         {
192                 switch(Team)
193                 {
194                         case -1:
195                         case 0:
196                                 break;
197                         default:
198                                 if(GetTeam(Team, false) == world)
199                                 {
200                                         LOG_TRACEF("trying to switch to unsupported team %d\n", Team);
201                                         Team = NUM_SPECTATOR;
202                                 }
203                                 break;
204                 }
205         }
206         if(Team == -1) // leave
207         {
208                 if(o.has_team)
209                 {
210                         tm = GetTeam(o.team, false);
211                         tm.team_size -= 1;
212                         o.has_team = 0;
213                         return true;
214                 }
215         }
216         else
217         {
218                 if (!o.has_team)
219                 {
220                         o.team = Team;
221                         tm = GetTeam(Team, true);
222                         tm.team_size += 1;
223                         o.has_team = 1;
224                         return true;
225                 }
226                 else if(Team != o.team)
227                 {
228                         tm = GetTeam(o.team, false);
229                         tm.team_size -= 1;
230                         o.team = Team;
231                         tm = GetTeam(Team, true);
232                         tm.team_size += 1;
233                         return true;
234                 }
235         }
236         return false;
237 }
238
239 void Playerchecker_Think()
240 {
241         SELFPARAM();
242     int i;
243         entity e;
244         for(i = 0; i < maxclients; ++i)
245         {
246                 e = playerslots[i];
247                 if(GetPlayerName(i) == "")
248                 {
249                         if(e.sort_prev)
250                         {
251                                 // player disconnected
252                                 SetTeam(e, -1);
253                                 RemovePlayer(e);
254                                 e.sort_prev = world;
255                                 //e.gotscores = 0;
256                         }
257                 }
258                 else
259                 {
260                         if (!e.sort_prev)
261                         {
262                                 // player connected
263                                 if (!e)
264                                 {
265                                         playerslots[i] = e = new(playerslot);
266                                         make_pure(e);
267                                 }
268                                 e.sv_entnum = i;
269                                 e.ping = 0;
270                                 e.ping_packetloss = 0;
271                                 e.ping_movementloss = 0;
272                                 //e.gotscores = 0; // we might already have the scores...
273                                 SetTeam(e, GetPlayerColor(i)); // will not hurt; later updates come with HUD_UpdatePlayerTeams
274                                 RegisterPlayer(e);
275                                 HUD_UpdatePlayerPos(e);
276                         }
277                 }
278         }
279         this.nextthink = time + 0.2;
280 }
281
282 void Porto_Init();
283 void TrueAim_Init();
284 void PostInit()
285 {
286         entity playerchecker = new(playerchecker);
287         make_pure(playerchecker);
288         playerchecker.think = Playerchecker_Think;
289         playerchecker.nextthink = time + 0.2;
290
291         Porto_Init();
292         TrueAim_Init();
293
294         postinit = true;
295 }
296
297 // CSQC_InputEvent : Used to perform actions based on any key pressed, key released and mouse on the client.
298 // Return value should be 1 if CSQC handled the input, otherwise return 0 to have the input passed to the engine.
299 // All keys are in ascii.
300 // bInputType = 0 is key pressed, 1 is key released, 2 and 3 are mouse input.
301 // In the case of keyboard input, nPrimary is the ascii code, and nSecondary is 0.
302 // In the case of mouse input, nPrimary is xdelta, nSecondary is ydelta.
303 // In the case of mouse input after a setcursormode(1) call, nPrimary is xpos, nSecondary is ypos.
304 float CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)
305 {
306         if (HUD_Panel_InputEvent(bInputType, nPrimary, nSecondary))
307                 return true;
308
309         if (QuickMenu_InputEvent(bInputType, nPrimary, nSecondary))
310                 return true;
311
312         if (HUD_Radar_InputEvent(bInputType, nPrimary, nSecondary))
313                 return true;
314
315         if (MapVote_InputEvent(bInputType, nPrimary, nSecondary))
316                 return true;
317
318         if (HUD_Minigame_InputEvent(bInputType, nPrimary, nSecondary))
319                 return true;
320
321         return false;
322 }
323
324 // END REQUIRED CSQC FUNCTIONS
325 // --------------------------------------------------------------------------
326
327 // --------------------------------------------------------------------------
328 // BEGIN OPTIONAL CSQC FUNCTIONS
329
330 void Ent_RemoveEntCS()
331 {
332         SELFPARAM();
333         entcs_receiver[this.sv_entnum] = NULL;
334 }
335
336 NET_HANDLE(ENT_CLIENT_ENTCS, bool isnew)
337 {
338         make_pure(this);
339         this.classname = "entcs_receiver";
340         InterpolateOrigin_Undo();
341         int sf = ReadByte();
342
343         if(sf & BIT(0))
344                 this.sv_entnum = ReadByte();
345         if (sf & BIT(1))
346         {
347                 this.origin_x = ReadShort();
348                 this.origin_y = ReadShort();
349                 this.origin_z = ReadShort();
350                 setorigin(this, this.origin);
351         }
352         if (sf & BIT(2))
353         {
354                 this.angles_y = ReadByte() * 360.0 / 256;
355                 this.angles_x = this.angles_z = 0;
356         }
357         if (sf & BIT(3))
358                 this.healthvalue = ReadByte() * 10;
359         if (sf & BIT(4))
360                 this.armorvalue = ReadByte() * 10;
361
362         return = true;
363
364         entcs_receiver[this.sv_entnum] = this;
365         this.entremove = Ent_RemoveEntCS;
366         this.iflags |= IFLAG_ORIGIN;
367
368         InterpolateOrigin_Note();
369 }
370
371 void Ent_Remove();
372
373 void Ent_RemovePlayerScore()
374 {
375         SELFPARAM();
376         if(this.owner) {
377                 SetTeam(this.owner, -1);
378                 this.owner.gotscores = 0;
379                 for(int i = 0; i < MAX_SCORE; ++i) {
380                         this.owner.(scores[i]) = 0; // clear all scores
381                 }
382         }
383 }
384
385 NET_HANDLE(ENT_CLIENT_SCORES, bool isnew)
386 {
387         make_pure(this);
388         int i, n;
389         bool isNew;
390         entity o;
391
392         // damnit -.- don't want to go change every single .sv_entnum in hud.qc AGAIN
393         // (no I've never heard of M-x replace-string, sed, or anything like that)
394         isNew = !this.owner; // workaround for DP bug
395         n = ReadByte()-1;
396
397 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
398         if(!isNew && n != this.sv_entnum)
399         {
400                 //print("A CSQC entity changed its owner!\n");
401                 LOG_INFOF("A CSQC entity changed its owner! (edict: %d, classname: %s)\n", num_for_edict(this), this.classname);
402                 isNew = true;
403                 Ent_Remove();
404         }
405 #endif
406
407         this.sv_entnum = n;
408
409         o = playerslots[this.sv_entnum];
410         if (!o)
411         {
412                 o = playerslots[this.sv_entnum] = new(playerslot);
413                 make_pure(o);
414         }
415         this.owner = o;
416         o.sv_entnum = this.sv_entnum;
417         o.gotscores = 1;
418
419         //if (!o.sort_prev)
420         //      RegisterPlayer(o);
421         //playerchecker will do this for us later, if it has not already done so
422
423     int sf, lf;
424 #if MAX_SCORE <= 8
425         sf = ReadByte();
426         lf = ReadByte();
427 #else
428         sf = ReadShort();
429         lf = ReadShort();
430 #endif
431     int p;
432         for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
433                 if(sf & p)
434                 {
435                         if(lf & p)
436                                 o.(scores[i]) = ReadInt24_t();
437                         else
438                                 o.(scores[i]) = ReadChar();
439                 }
440
441         return = true;
442
443         if(o.sort_prev)
444                 HUD_UpdatePlayerPos(o); // if not registered, we cannot do this yet!
445
446         this.entremove = Ent_RemovePlayerScore;
447 }
448
449 NET_HANDLE(ENT_CLIENT_TEAMSCORES, bool isnew)
450 {
451         make_pure(this);
452         int i;
453         entity o;
454
455         this.team = ReadByte();
456         o = this.owner = GetTeam(this.team, true); // these team numbers can always be trusted
457
458     int sf, lf;
459 #if MAX_TEAMSCORE <= 8
460         sf = ReadByte();
461         lf = ReadByte();
462 #else
463         sf = ReadShort();
464         lf = ReadShort();
465 #endif
466         int p;
467         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
468                 if(sf & p)
469                 {
470                         if(lf & p)
471                                 o.(teamscores[i]) = ReadInt24_t();
472                         else
473                                 o.(teamscores[i]) = ReadChar();
474                 }
475
476         return = true;
477
478         HUD_UpdateTeamPos(o);
479 }
480
481 NET_HANDLE(ENT_CLIENT_CLIENTDATA, bool isnew)
482 {
483         make_pure(this);
484         float newspectatee_status;
485
486     int f = ReadByte();
487
488         scoreboard_showscores_force = (f & 1);
489
490         if(f & 2)
491         {
492                 newspectatee_status = ReadByte();
493                 if(newspectatee_status == player_localnum + 1)
494                         newspectatee_status = -1; // observing
495         }
496         else
497                 newspectatee_status = 0;
498
499         spectatorbutton_zoom = (f & 4);
500
501         if(f & 8)
502         {
503                 angles_held_status = 1;
504                 angles_held.x = ReadAngle();
505                 angles_held.y = ReadAngle();
506                 angles_held.z = 0;
507         }
508         else
509                 angles_held_status = 0;
510
511         return = true;
512
513         if(newspectatee_status != spectatee_status)
514         {
515                 // clear race stuff
516                 race_laptime = 0;
517                 race_checkpointtime = 0;
518         }
519         if (autocvar_hud_panel_healtharmor_progressbar_gfx)
520         {
521                 if ( (spectatee_status == -1 && newspectatee_status > 0) //before observing, now spectating
522                   || (spectatee_status > 0 && newspectatee_status > 0 && spectatee_status != newspectatee_status) //changed spectated player
523                 )
524                         prev_p_health = -1;
525                 else if(spectatee_status && !newspectatee_status) //before observing/spectating, now playing
526                         prev_health = -1;
527         }
528         spectatee_status = newspectatee_status;
529
530         // we could get rid of spectatee_status, and derive it from player_localentnum and player_localnum
531 }
532
533 NET_HANDLE(ENT_CLIENT_NAGGER, bool isnew)
534 {
535         make_pure(this);
536     int i, j, b, f;
537
538     int nags = ReadByte(); // NAGS NAGS NAGS NAGS NAGS NAGS NADZ NAGS NAGS NAGS
539
540         if(!(nags & BIT(2)))
541         {
542                 if(vote_called_vote)
543                         strunzone(vote_called_vote);
544                 vote_called_vote = string_null;
545                 vote_active = 0;
546         }
547         else
548         {
549                 vote_active = 1;
550         }
551
552         if(nags & BIT(6))
553         {
554                 vote_yescount = ReadByte();
555                 vote_nocount = ReadByte();
556                 vote_needed = ReadByte();
557                 vote_highlighted = ReadChar();
558         }
559
560         if(nags & BIT(7))
561         {
562                 if(vote_called_vote)
563                         strunzone(vote_called_vote);
564                 vote_called_vote = strzone(ColorTranslateRGB(ReadString()));
565         }
566
567         if(nags & 1)
568         {
569                 for(j = 0; j < maxclients; ++j)
570                         if(playerslots[j])
571                                 playerslots[j].ready = 1;
572                 for(i = 1; i <= maxclients; i += 8)
573                 {
574                         f = ReadByte();
575                         for(j = i-1, b = 1; b < 256; b *= 2, ++j)
576                                 if (!(f & b))
577                                         if(playerslots[j])
578                                                 playerslots[j].ready = 0;
579                 }
580         }
581
582         return = true;
583
584         ready_waiting = (nags & BIT(0));
585         ready_waiting_for_me = (nags & BIT(1));
586         vote_waiting = (nags & BIT(2));
587         vote_waiting_for_me = (nags & BIT(3));
588         warmup_stage = (nags & BIT(4));
589 }
590
591 NET_HANDLE(ENT_CLIENT_ELIMINATEDPLAYERS, bool isnew)
592 {
593         make_pure(this);
594     int i, j, b, f;
595
596     int sf = ReadByte();
597         if(sf & 1)
598         {
599                 for(j = 0; j < maxclients; ++j)
600                         if(playerslots[j])
601                                 playerslots[j].eliminated = 1;
602                 for(i = 1; i <= maxclients; i += 8)
603                 {
604                         f = ReadByte();
605                         for(j = i-1, b = 1; b < 256; b *= 2, ++j)
606                                 if (!(f & b))
607                                         if(playerslots[j])
608                                                 playerslots[j].eliminated = 0;
609                 }
610         }
611         return true;
612 }
613
614 NET_HANDLE(ENT_CLIENT_RANDOMSEED, bool isnew)
615 {
616         make_pure(this);
617         prandom_debug();
618         float s = ReadShort();
619         psrandom(s);
620         return true;
621 }
622
623 NET_HANDLE(ENT_CLIENT_ACCURACY, bool isnew)
624 {
625         make_pure(this);
626     int sf = ReadInt24_t();
627         if (sf == 0) {
628                 for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w)
629                         weapon_accuracy[w] = -1;
630                 return true;
631         }
632
633         int f = 1;
634         for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
635                 if (sf & f) {
636             int b = ReadByte();
637                         if (b == 0)
638                                 weapon_accuracy[w] = -1;
639                         else if (b == 255)
640                                 weapon_accuracy[w] = 1.0; // no better error handling yet, sorry
641                         else
642                                 weapon_accuracy[w] = (b - 1.0) / 100.0;
643                 }
644                 f = (f == 0x800000) ? 1 : f * 2;
645         }
646         return true;
647 }
648
649 void Spawn_Draw(entity this)
650 {
651         __pointparticles(this.cnt, this.origin + '0 0 28', '0 0 2', bound(0, frametime, 0.1));
652 }
653
654 NET_HANDLE(ENT_CLIENT_SPAWNPOINT, bool is_new)
655 {
656         float teamnum = (ReadByte() - 1);
657         vector spn_origin;
658         spn_origin.x = ReadShort();
659         spn_origin.y = ReadShort();
660         spn_origin.z = ReadShort();
661
662         //if(is_new)
663         //{
664                 this.origin = spn_origin;
665                 setsize(this, PL_MIN_CONST, PL_MAX_CONST);
666                 //droptofloor();
667
668                 /*if(autocvar_cl_spawn_point_model) // needs a model first
669                 {
670                         this.mdl = "models/spawnpoint.md3";
671                         this.colormod = Team_ColorRGB(teamnum);
672                         precache_model(this.mdl);
673                         setmodel(this, this.mdl);
674                         this.drawmask = MASK_NORMAL;
675                         //this.movetype = MOVETYPE_NOCLIP;
676                         //this.draw = Spawn_Draw;
677                 }*/
678                 if(autocvar_cl_spawn_point_particles)
679                 {
680                         if((serverflags & SERVERFLAG_TEAMPLAY))
681                         {
682                                 switch(teamnum)
683                                 {
684                                         case NUM_TEAM_1: this.cnt = particleeffectnum(EFFECT_SPAWNPOINT_RED); break;
685                                         case NUM_TEAM_2: this.cnt = particleeffectnum(EFFECT_SPAWNPOINT_BLUE); break;
686                                         case NUM_TEAM_3: this.cnt = particleeffectnum(EFFECT_SPAWNPOINT_YELLOW); break;
687                                         case NUM_TEAM_4: this.cnt = particleeffectnum(EFFECT_SPAWNPOINT_PINK); break;
688                                         default: this.cnt = particleeffectnum(EFFECT_SPAWNPOINT_NEUTRAL); break;
689                                 }
690                         }
691                         else { this.cnt = particleeffectnum(EFFECT_SPAWNPOINT_NEUTRAL); }
692
693                         this.draw = Spawn_Draw;
694                 }
695         //}
696
697         //printf("Ent_ReadSpawnPoint(is_new = %d); origin = %s, team = %d, effect = %d\n", is_new, vtos(this.origin), teamnum, this.cnt);
698         return true;
699 }
700
701 NET_HANDLE(ENT_CLIENT_SPAWNEVENT, bool is_new)
702 {
703         // If entnum is 0, ONLY do the local spawn actions
704         // this way the server can disable the sending of
705         // spawn origin or such to clients if wanted.
706         float entnum = ReadByte();
707
708         if(entnum)
709         {
710                 this.origin_x = ReadShort();
711                 this.origin_y = ReadShort();
712                 this.origin_z = ReadShort();
713
714                 if(is_new)
715                 {
716                         float teamnum = GetPlayerColor(entnum - 1);
717
718                         if(autocvar_cl_spawn_event_particles)
719                         {
720                                 switch(teamnum)
721                                 {
722                                         case NUM_TEAM_1: pointparticles(EFFECT_SPAWN_RED, this.origin, '0 0 0', 1); break;
723                                         case NUM_TEAM_2: pointparticles(EFFECT_SPAWN_BLUE, this.origin, '0 0 0', 1); break;
724                                         case NUM_TEAM_3: pointparticles(EFFECT_SPAWN_YELLOW, this.origin, '0 0 0', 1); break;
725                                         case NUM_TEAM_4: pointparticles(EFFECT_SPAWN_PINK, this.origin, '0 0 0', 1); break;
726                                         default: pointparticles(EFFECT_SPAWN_NEUTRAL, this.origin, '0 0 0', 1); break;
727                                 }
728                         }
729                         if(autocvar_cl_spawn_event_sound)
730                         {
731                                 sound(this, CH_TRIGGER, SND_SPAWN, VOL_BASE, ATTEN_NORM);
732                         }
733                 }
734         }
735         return = true;
736
737         // local spawn actions
738         if(is_new && (!entnum || (entnum == player_localentnum)))
739         {
740                 zoomin_effect = 1;
741                 current_viewzoom = (1 / bound(1, autocvar_cl_spawnzoom_factor, 16));
742
743                 if(autocvar_cl_unpress_zoom_on_spawn)
744                 {
745                         localcmd("-zoom\n");
746                         button_zoom = false;
747                 }
748         }
749         HUD_Radar_Hide_Maximized();
750         //printf("Ent_ReadSpawnEvent(is_new = %d); origin = %s, entnum = %d, localentnum = %d\n", is_new, vtos(this.origin), entnum, player_localentnum);
751 }
752
753 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.
754 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.
755 void CSQC_Ent_Update(bool isnew)
756 {
757         SELFPARAM();
758         this.sourceLocLine = __LINE__;
759         this.sourceLocFile = __FILE__;
760         int t = ReadByte();
761
762         // set up the "time" global for received entities to be correct for interpolation purposes
763         float savetime = time;
764         if(servertime)
765         {
766                 time = servertime;
767         }
768         else
769         {
770                 serverprevtime = time;
771                 serverdeltatime = getstatf(STAT_MOVEVARS_TICRATE) * getstatf(STAT_MOVEVARS_TIMESCALE);
772                 time = serverprevtime + serverdeltatime;
773         }
774
775 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
776         if (this.enttype)
777         {
778                 if (t != this.enttype || isnew)
779                 {
780                         LOG_INFOF("A CSQC entity changed its type! (edict: %d, server: %d, type: %d -> %d)\n", num_for_edict(this), this.entnum, this.enttype, t);
781                         Ent_Remove();
782                         clearentity(this);
783                         isnew = true;
784                 }
785         }
786         else
787         {
788                 if (!isnew)
789                 {
790                         LOG_INFOF("A CSQC entity appeared out of nowhere! (edict: %d, server: %d, type: %d)\n", num_for_edict(this), this.entnum, t);
791                         isnew = true;
792                 }
793         }
794 #endif
795         this.enttype = t;
796         bool done = false;
797         FOREACH(LinkedEntities, it.m_id == t, LAMBDA(
798                 if (isnew) this.classname = it.netname;
799                 if (autocvar_developer_csqcentities)
800             LOG_INFOF("CSQC_Ent_Update(%d) at %f with this=%i {.entnum=%d, .enttype=%d} t=%s (%d)\n", isnew, savetime, this, this.entnum, this.enttype, this.classname, t);
801                 done = it.m_read(this, isnew);
802                 break;
803         ));
804         time = savetime;
805         if (!done)
806         {
807                 LOG_FATALF("CSQC_Ent_Update(%d) at %f with this=%i {.entnum=%d, .enttype=%d} t=%s (%d)\n", isnew, savetime, this, this.entnum, this.enttype, this.classname, t);
808         }
809 }
810
811 // Destructor, but does NOT deallocate the entity by calling remove(). Also
812 // used when an entity changes its type. For an entity that someone interacts
813 // with others, make sure it can no longer do so.
814 void Ent_Remove()
815 {
816         SELFPARAM();
817         if(this.entremove) this.entremove();
818
819         if(this.skeletonindex)
820         {
821                 skel_delete(this.skeletonindex);
822                 this.skeletonindex = 0;
823         }
824
825         if(this.snd_looping > 0)
826         {
827                 sound(this, this.snd_looping, SND_Null, VOL_BASE, autocvar_g_jetpack_attenuation);
828                 this.snd_looping = 0;
829         }
830
831         this.enttype = 0;
832         this.classname = "";
833         this.draw = func_null;
834         this.entremove = func_null;
835         // TODO possibly set more stuff to defaults
836 }
837 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(this) as well.
838 void CSQC_Ent_Remove()
839 {
840         SELFPARAM();
841         if (autocvar_developer_csqcentities) LOG_INFOF("CSQC_Ent_Remove() with this=%i {.entnum=%d, .enttype=%d}\n", this, this.entnum, this.enttype);
842         if (wasfreed(this))
843         {
844                 LOG_WARNING("CSQC_Ent_Remove called for already removed entity. Packet loss?\n");
845                 return;
846         }
847         if (this.enttype) Ent_Remove();
848         remove(this);
849 }
850
851 void Gamemode_Init()
852 {
853         if (!isdemo())
854         {
855                 if(!(calledhooks & HOOK_START))
856                         localcmd("\n_cl_hook_gamestart ", MapInfo_Type_ToString(gametype), "\n");
857                 calledhooks |= HOOK_START;
858         }
859 }
860 // CSQC_Parse_StuffCmd : Provides the stuffcmd string in the first parameter that the server provided.  To execute standard behavior, simply execute localcmd with the string.
861 void CSQC_Parse_StuffCmd(string strMessage)
862 {
863         if (autocvar_developer_csqcentities) LOG_INFOF("CSQC_Parse_StuffCmd(\"%s\")\n", strMessage);
864         localcmd(strMessage);
865 }
866 // CSQC_Parse_Print : Provides the print string in the first parameter that the server provided.  To execute standard behavior, simply execute print with the string.
867 void CSQC_Parse_Print(string strMessage)
868 {
869         if (autocvar_developer_csqcentities) LOG_INFOF("CSQC_Parse_Print(\"%s\")\n", strMessage);
870         print(ColorTranslateRGB(strMessage));
871 }
872
873 // CSQC_Parse_CenterPrint : Provides the centerprint_hud string in the first parameter that the server provided.
874 void CSQC_Parse_CenterPrint(string strMessage)
875 {
876         if (autocvar_developer_csqcentities) LOG_INFOF("CSQC_Parse_CenterPrint(\"%s\")\n", strMessage);
877         centerprint_hud(strMessage);
878 }
879
880 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.
881 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.
882 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.
883 bool CSQC_Parse_TempEntity()
884 {
885         // Acquire TE ID
886         int nTEID = ReadByte();
887
888         FOREACH(TempEntities, it.m_id == nTEID, LAMBDA(
889                 if (autocvar_developer_csqcentities)
890                         LOG_INFOF("CSQC_Parse_TempEntity() nTEID=%s (%d)\n", it.netname, nTEID);
891                 return it.m_read(NULL, true);
892         ));
893
894         if (autocvar_developer_csqcentities)
895                 LOG_INFOF("CSQC_Parse_TempEntity() with nTEID=%d\n", nTEID);
896
897         // No special logic for this temporary entity; return 0 so the engine can handle it
898         return false;
899 }
900
901 /** TODO somehow thwart prvm_globalset client ... */
902 string forcefog;
903 void Fog_Force()
904 {
905         if (autocvar_cl_orthoview && autocvar_cl_orthoview_nofog)
906                 localcmd("\nr_drawfog 0\n");
907         else if (forcefog != "")
908                 localcmd(sprintf("\nfog %s\nr_fog_exp2 0\nr_drawfog 1\n", forcefog));
909 }
910
911 void Gamemode_Init();
912 NET_HANDLE(ENT_CLIENT_SCORES_INFO, bool isnew)
913 {
914         make_pure(this);
915         gametype = ReadInt24_t();
916         HUD_ModIcons_SetFunc();
917         for (int i = 0; i < MAX_SCORE; ++i)
918         {
919                 if (scores_label[i]) strunzone(scores_label[i]);
920                 scores_label[i] = strzone(ReadString());
921                 scores_flags[i] = ReadByte();
922         }
923         for (int i = 0; i < MAX_TEAMSCORE; ++i)
924         {
925                 if (teamscores_label[i]) strunzone(teamscores_label[i]);
926                 teamscores_label[i] = strzone(ReadString());
927                 teamscores_flags[i] = ReadByte();
928         }
929         return = true;
930         HUD_InitScores();
931         Gamemode_Init();
932 }
933
934 NET_HANDLE(ENT_CLIENT_INIT, bool isnew)
935 {
936         nb_pb_period = ReadByte() / 32; //Accuracy of 1/32th
937
938         hook_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
939         hook_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
940         hook_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
941         hook_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
942         arc_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
943         arc_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
944         arc_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
945         arc_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
946
947         if (forcefog) strunzone(forcefog);
948         forcefog = strzone(ReadString());
949
950         armorblockpercent = ReadByte() / 255.0;
951
952         g_balance_mortar_bouncefactor = ReadCoord();
953         g_balance_mortar_bouncestop = ReadCoord();
954         g_balance_electro_secondary_bouncefactor = ReadCoord();
955         g_balance_electro_secondary_bouncestop = ReadCoord();
956
957         vortex_scope = !ReadByte();
958         rifle_scope = !ReadByte();
959
960         serverflags = ReadByte();
961
962         minelayer_maxmines = ReadByte();
963
964         hagar_maxrockets = ReadByte();
965
966         g_trueaim_minrange = ReadCoord();
967         g_balance_porto_secondary = ReadByte();
968         return = true;
969
970         MUTATOR_CALLHOOK(Ent_Init);
971
972         if (!postinit) PostInit();
973 }
974
975 NET_HANDLE(TE_CSQC_RACE, bool isNew)
976 {
977         int b = ReadByte();
978
979         switch (b)
980         {
981                 case RACE_NET_CHECKPOINT_HIT_QUALIFYING:
982                         race_checkpoint = ReadByte();
983                         race_time = ReadInt24_t();
984                         race_previousbesttime = ReadInt24_t();
985                         if(race_previousbestname)
986                                 strunzone(race_previousbestname);
987                         race_previousbestname = strzone(ColorTranslateRGB(ReadString()));
988
989                         race_checkpointtime = time;
990
991                         if(race_checkpoint == 0 || race_checkpoint == 254)
992                         {
993                                 race_penaltyaccumulator = 0;
994                                 race_laptime = time; // valid
995                         }
996
997                         break;
998
999                 case RACE_NET_CHECKPOINT_CLEAR:
1000                         race_laptime = 0;
1001                         race_checkpointtime = 0;
1002                         break;
1003
1004                 case RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING:
1005                         race_laptime = ReadCoord();
1006                         race_checkpointtime = -99999;
1007                         // fall through
1008                 case RACE_NET_CHECKPOINT_NEXT_QUALIFYING:
1009                         race_nextcheckpoint = ReadByte();
1010
1011                         race_nextbesttime = ReadInt24_t();
1012                         if(race_nextbestname)
1013                                 strunzone(race_nextbestname);
1014                         race_nextbestname = strzone(ColorTranslateRGB(ReadString()));
1015                         break;
1016
1017                 case RACE_NET_CHECKPOINT_HIT_RACE:
1018                         race_mycheckpoint = ReadByte();
1019                         race_mycheckpointtime = time;
1020                         race_mycheckpointdelta = ReadInt24_t();
1021                         race_mycheckpointlapsdelta = ReadByte();
1022                         if(race_mycheckpointlapsdelta >= 128)
1023                                 race_mycheckpointlapsdelta -= 256;
1024                         if(race_mycheckpointenemy)
1025                                 strunzone(race_mycheckpointenemy);
1026                         race_mycheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
1027                         break;
1028
1029                 case RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT:
1030                         race_othercheckpoint = ReadByte();
1031                         race_othercheckpointtime = time;
1032                         race_othercheckpointdelta = ReadInt24_t();
1033                         race_othercheckpointlapsdelta = ReadByte();
1034                         if(race_othercheckpointlapsdelta >= 128)
1035                                 race_othercheckpointlapsdelta -= 256;
1036                         if(race_othercheckpointenemy)
1037                                 strunzone(race_othercheckpointenemy);
1038                         race_othercheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
1039                         break;
1040
1041                 case RACE_NET_PENALTY_RACE:
1042                         race_penaltyeventtime = time;
1043                         race_penaltytime = ReadShort();
1044                         //race_penaltyaccumulator += race_penaltytime;
1045                         if(race_penaltyreason)
1046                                 strunzone(race_penaltyreason);
1047                         race_penaltyreason = strzone(ReadString());
1048                         break;
1049
1050                 case RACE_NET_PENALTY_QUALIFYING:
1051                         race_penaltyeventtime = time;
1052                         race_penaltytime = ReadShort();
1053                         race_penaltyaccumulator += race_penaltytime;
1054                         if(race_penaltyreason)
1055                                 strunzone(race_penaltyreason);
1056                         race_penaltyreason = strzone(ReadString());
1057                         break;
1058
1059                 case RACE_NET_SERVER_RECORD:
1060                         race_server_record = ReadInt24_t();
1061                         break;
1062                 case RACE_NET_SPEED_AWARD:
1063                         race_speedaward = ReadInt24_t();
1064                         if(race_speedaward_holder)
1065                                 strunzone(race_speedaward_holder);
1066                         race_speedaward_holder = strzone(ReadString());
1067                         break;
1068                 case RACE_NET_SPEED_AWARD_BEST:
1069                         race_speedaward_alltimebest = ReadInt24_t();
1070                         if(race_speedaward_alltimebest_holder)
1071                                 strunzone(race_speedaward_alltimebest_holder);
1072                         race_speedaward_alltimebest_holder = strzone(ReadString());
1073                         break;
1074                 case RACE_NET_SERVER_RANKINGS:
1075                         float prevpos, del;
1076             int pos = ReadShort();
1077                         prevpos = ReadShort();
1078                         del = ReadShort();
1079
1080                         // move other rankings out of the way
1081             int i;
1082                         if (prevpos) {
1083                                 for (i=prevpos-1;i>pos-1;--i) {
1084                                         grecordtime[i] = grecordtime[i-1];
1085                                         if(grecordholder[i])
1086                                                 strunzone(grecordholder[i]);
1087                                         grecordholder[i] = strzone(grecordholder[i-1]);
1088                                 }
1089                         } else if (del) { // a record has been deleted by the admin
1090                                 for (i=pos-1; i<= RANKINGS_CNT-1; ++i) {
1091                                         if (i == RANKINGS_CNT-1) { // clear out last record
1092                                                 grecordtime[i] = 0;
1093                                                 if (grecordholder[i])
1094                                                         strunzone(grecordholder[i]);
1095                                                 grecordholder[i] = string_null;
1096                                         }
1097                                         else {
1098                                                 grecordtime[i] = grecordtime[i+1];
1099                                                 if (grecordholder[i])
1100                                                         strunzone(grecordholder[i]);
1101                                                 grecordholder[i] = strzone(grecordholder[i+1]);
1102                                         }
1103                                 }
1104                         } else { // player has no ranked record yet
1105                                 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
1106                                         grecordtime[i] = grecordtime[i-1];
1107                                         if(grecordholder[i])
1108                                                 strunzone(grecordholder[i]);
1109                                         grecordholder[i] = strzone(grecordholder[i-1]);
1110                                 }
1111                         }
1112
1113                         // store new ranking
1114                         if(grecordholder[pos-1] != "")
1115                                 strunzone(grecordholder[pos-1]);
1116                         grecordholder[pos-1] = strzone(ReadString());
1117                         grecordtime[pos-1] = ReadInt24_t();
1118                         if(grecordholder[pos-1] == GetPlayerName(player_localnum))
1119                                 race_myrank = pos;
1120                         break;
1121                 case RACE_NET_SERVER_STATUS:
1122                         race_status = ReadShort();
1123                         if(race_status_name)
1124                                 strunzone(race_status_name);
1125                         race_status_name = strzone(ReadString());
1126         }
1127         return true;
1128 }
1129
1130 NET_HANDLE(TE_CSQC_TEAMNAGGER, bool isNew)
1131 {
1132         teamnagger = 1;
1133         return true;
1134 }
1135
1136 NET_HANDLE(TE_CSQC_PINGPLREPORT, bool isNew)
1137 {
1138         int i = ReadByte();
1139         int pi = ReadShort();
1140         int pl = ReadByte();
1141         int ml = ReadByte();
1142         return = true;
1143         entity e = playerslots[i];
1144         if (!e) return;
1145         e.ping = pi;
1146         e.ping_packetloss = pl / 255.0;
1147         e.ping_movementloss = ml / 255.0;
1148 }
1149
1150 NET_HANDLE(TE_CSQC_WEAPONCOMPLAIN, bool isNew)
1151 {
1152         complain_weapon = ReadByte();
1153         if (complain_weapon_name) strunzone(complain_weapon_name);
1154         complain_weapon_name = strzone(WEP_NAME(complain_weapon));
1155         complain_weapon_type = ReadByte();
1156         return = true;
1157
1158         complain_weapon_time = time;
1159         weapontime = time; // ping the weapon panel
1160
1161         switch(complain_weapon_type)
1162         {
1163                 case 0: Local_Notification(MSG_MULTI, ITEM_WEAPON_NOAMMO, complain_weapon); break;
1164                 case 1: Local_Notification(MSG_MULTI, ITEM_WEAPON_DONTHAVE, complain_weapon); break;
1165                 default: Local_Notification(MSG_MULTI, ITEM_WEAPON_UNAVAILABLE, complain_weapon); break;
1166         }
1167 }
1168
1169 string getcommandkey(string text, string command)
1170 {
1171         string keys;
1172         float n, j, k, l = 0;
1173
1174         if (!autocvar_hud_showbinds)
1175                 return text;
1176
1177         keys = db_get(binddb, command);
1178         if (keys == "")
1179         {
1180                 n = tokenize(findkeysforcommand(command, 0)); // uses '...' strings
1181                 for(j = 0; j < n; ++j)
1182                 {
1183                         k = stof(argv(j));
1184                         if(k != -1)
1185                         {
1186                                 if ("" == keys)
1187                                         keys = keynumtostring(k);
1188                                 else
1189                                         keys = strcat(keys, ", ", keynumtostring(k));
1190
1191                                 ++l;
1192                                 if (autocvar_hud_showbinds_limit > 0 && autocvar_hud_showbinds_limit <= l)
1193                                         break;
1194                         }
1195
1196                 }
1197                 if (keys == "")
1198                         keys = "NO_KEY";
1199                 db_put(binddb, command, keys);
1200         }
1201
1202         if (keys == "NO_KEY") {
1203                 if (autocvar_hud_showbinds > 1)
1204                         return sprintf(_("%s (not bound)"), text);
1205                 else
1206                         return text;
1207         }
1208         else if (autocvar_hud_showbinds > 1)
1209                 return sprintf("%s (%s)", text, keys);
1210         else
1211                 return keys;
1212 }