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