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