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