]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/miscfunctions.qc
Remove direct server #includes from client
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / miscfunctions.qc
1 #include "miscfunctions.qh"
2
3 #include "autocvars.qh"
4 #include "defs.qh"
5 #include "hud.qh"
6 #include "main.qh"
7 #include "sortlist.qh"
8
9 #include "../common/command/generic.qh"
10
11 #include "../common/teams.qh"
12 #include "../common/urllib.qh"
13 #include "../common/util.qh"
14
15 #include "../csqcmodellib/cl_model.qh"
16
17 #include "../warpzonelib/mathlib.qh"
18
19 void AuditLists()
20 {
21         entity e;
22         entity prev;
23
24         prev = players;
25         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
26         {
27                 if(prev != e.sort_prev)
28                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
29         }
30
31         prev = teams;
32         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
33         {
34                 if(prev != e.sort_prev)
35                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
36         }
37 }
38
39
40 float RegisterPlayer(entity player)
41 {
42         entity pl;
43         AuditLists();
44         for(pl = players.sort_next; pl; pl = pl.sort_next)
45                 if(pl == player)
46                         error("Player already registered!");
47         player.sort_next = players.sort_next;
48         player.sort_prev = players;
49         if(players.sort_next)
50                 players.sort_next.sort_prev = player;
51         players.sort_next = player;
52         AuditLists();
53         return true;
54 }
55
56 void RemovePlayer(entity player)
57 {
58         entity pl, parent;
59         AuditLists();
60         parent = players;
61         for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
62                 parent = pl;
63
64         if(!pl)
65         {
66                 error("Trying to remove a player which is not in the playerlist!");
67                 return;
68         }
69         parent.sort_next = player.sort_next;
70         if(player.sort_next)
71                 player.sort_next.sort_prev = parent;
72         AuditLists();
73 }
74
75 void MoveToLast(entity e)
76 {
77         AuditLists();
78         other = e.sort_next;
79         while(other)
80         {
81                 SORT_SWAP(other, e);
82                 other = e.sort_next;
83         }
84         AuditLists();
85 }
86
87 float RegisterTeam(entity Team)
88 {
89         entity tm;
90         AuditLists();
91         for(tm = teams.sort_next; tm; tm = tm.sort_next)
92                 if(tm == Team)
93                         error("Team already registered!");
94         Team.sort_next = teams.sort_next;
95         Team.sort_prev = teams;
96         if(teams.sort_next)
97                 teams.sort_next.sort_prev = Team;
98         teams.sort_next = Team;
99         if(Team.team && Team.team != NUM_SPECTATOR)
100                 ++team_count;
101         AuditLists();
102         return true;
103 }
104
105 void RemoveTeam(entity Team)
106 {
107         entity tm, parent;
108         AuditLists();
109         parent = teams;
110         for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
111                 parent = tm;
112
113         if(!tm)
114         {
115                 print(_("Trying to remove a team which is not in the teamlist!"));
116                 return;
117         }
118         parent.sort_next = Team.sort_next;
119         if(Team.sort_next)
120                 Team.sort_next.sort_prev = parent;
121         if(Team.team && Team.team != NUM_SPECTATOR)
122                 --team_count;
123         AuditLists();
124 }
125
126 entity GetTeam(int Team, bool add)
127 {
128         int num = (Team == NUM_SPECTATOR) ? 16 : Team;
129         if(teamslots[num])
130                 return teamslots[num];
131         if (!add)
132                 return world;
133         entity tm = spawn();
134         tm.team = Team;
135         teamslots[num] = tm;
136         RegisterTeam(tm);
137         return tm;
138 }
139
140 vector HUD_GetFontsize(string cvarname)
141 {
142         vector v;
143         v = stov(cvar_string(cvarname));
144         if(v.x == 0)
145                 v = '8 8 0';
146         if(v.y == 0)
147                 v.y = v.x;
148         v.z = 0;
149         return v;
150 }
151
152 float PreviewExists(string name)
153 {
154         if(autocvar_cl_readpicture_force)
155                 return false;
156
157         if (fexists(strcat(name, ".tga"))) return true;
158         if (fexists(strcat(name, ".png"))) return true;
159         if (fexists(strcat(name, ".jpg"))) return true;
160         if (fexists(strcat(name, ".pcx"))) return true;
161
162         return false;
163 }
164
165 vector rotate(vector v, float a)
166 {
167         vector w = '0 0 0';
168         // FTEQCC SUCKS AGAIN
169         w.x =      v.x * cos(a) + v.y * sin(a);
170         w.y = -1 * v.x * sin(a) + v.y * cos(a);
171         return w;
172 }
173
174 string ColorTranslateRGB(string s)
175 {
176         if(ColorTranslateMode & 1)
177                 return strdecolorize(s);
178         else
179                 return s;
180 }
181
182 // decolorizes and team colors the player name when needed
183 string playername(string thename, float teamid)
184 {
185     string t;
186     if (teamplay)
187     {
188         t = Team_ColorCode(teamid);
189         return strcat(t, strdecolorize(thename));
190     }
191     else
192         return strdecolorize(thename);
193 }
194
195 float cvar_or(string cv, float v)
196 {
197         string s;
198         s = cvar_string(cv);
199         if(s == "")
200                 return v;
201         else
202                 return stof(s);
203 }
204
205 vector project_3d_to_2d(vector vec)
206 {
207         vec = cs_project(vec);
208         if(cs_project_is_b0rked > 0)
209         {
210                 vec.x *= vid_conwidth / vid_width;
211                 vec.y *= vid_conheight / vid_height;
212         }
213         return vec;
214 }
215
216 void dummyfunction(float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
217 {
218 }
219
220 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
221 {
222         return 1.2 / (1.2 - fadelerp);
223 }
224
225 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
226 {
227         boxsize.x *= boxxsizefactor; // easier interface for text
228         return boxsize * (0.5 * (1 - sz));
229 }
230
231 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
232 {
233         vector line_dim = '0 0 0';
234
235         // left and right lines
236         pos.x -= thickness;
237         line_dim.x = thickness;
238         line_dim.y = dim.y;
239         drawfill(pos, line_dim, color, theAlpha, drawflag);
240         drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
241
242         // upper and lower lines
243         pos.y -= thickness;
244         line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
245         line_dim.y = thickness;
246         drawfill(pos, line_dim, color, theAlpha, drawflag);
247         drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
248 }
249
250 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
251 {
252         vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
253         end_pos = pos + area;
254
255         current_pos.y = pos.y;
256         while (current_pos.y < end_pos.y)
257         {
258                 current_pos.x = pos.x;
259                 while (current_pos.x < end_pos.x)
260                 {
261                         new_size.x = min(sz.x, end_pos.x - current_pos.x);
262                         new_size.y = min(sz.y, end_pos.y - current_pos.y);
263                         ratio.x = new_size.x / sz.x;
264                         ratio.y = new_size.y / sz.y;
265                         drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
266                         current_pos.x += sz.x;
267                 }
268                 current_pos.y += sz.y;
269         }
270 }
271
272 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
273 {
274         float sz;
275         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
276
277         drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
278 }
279
280 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
281 {
282         drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
283         drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
284 }
285
286 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
287 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
288         SET_POS_AND_SZ_Y_ASPECT(false);
289         drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
290 }
291
292 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
293 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
294         SET_POS_AND_SZ_Y_ASPECT(true);
295         drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
296 }
297
298 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
299 {
300         float sz;
301         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
302
303         drawfontscale = sz * '1 1 0';
304         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
305         drawstring(position + expandingbox_resize_centered_box_offset(sz, theScale, stringwidth(text, false, theScale * (sz / drawfontscale.x)) / (theScale.x * sz)), text, theScale * (sz / drawfontscale.x), rgb, theAlpha * (1 - fadelerp), flag);
306         // width parameter:
307         //    (scale_x * sz / drawfontscale_x) * drawfontscale_x * SIZE1 / (scale_x * sz)
308         //    SIZE1
309         drawfontscale = '1 1 0';
310 }
311
312 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
313 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
314         SET_POS_AND_SZ_Y_ASPECT(false);
315         drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
316 }
317
318 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
319 {
320         float sz;
321         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
322
323         drawfontscale = sz * '1 1 0';
324         dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
325         drawcolorcodedstring(position + expandingbox_resize_centered_box_offset(sz, theScale, stringwidth(text, true, theScale * (sz / drawfontscale.x)) / (theScale.x * sz)), text, theScale * (sz / drawfontscale.x), theAlpha * (1 - fadelerp), flag);
326         drawfontscale = '1 1 0';
327 }
328
329 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
330         SET_POS_AND_SZ_Y_ASPECT(true);
331         drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
332 }
333
334 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
335 float PolyDrawModelSurface(entity e, float i_s)
336 {
337         float i_t;
338         float n_t;
339         vector tri;
340         string tex;
341         tex = getsurfacetexture(e, i_s);
342         if (!tex)
343                 return 0; // this is beyond the last one
344         n_t = getsurfacenumtriangles(e, i_s);
345         for(i_t = 0; i_t < n_t; ++i_t)
346         {
347                 tri = getsurfacetriangle(e, i_s, i_t);
348                 R_BeginPolygon(tex, 0);
349                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
350                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
351                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
352                 R_EndPolygon();
353         }
354         return 1;
355 }
356 void PolyDrawModel(entity e)
357 {
358         float i_s;
359         for(i_s = 0; ; ++i_s)
360                 if(!PolyDrawModelSurface(e, i_s))
361                         break;
362 }
363
364 void DrawCircleClippedPic(vector centre, float radius, string pic, float f, vector rgb, float a, float drawflag)
365 {
366         float x, y, q, d;
367         vector ringsize, v, t;
368         ringsize = radius * '1 1 0';
369
370         x = cos(f * 2 * M_PI);
371         y = sin(f * 2 * M_PI);
372         q = fabs(x) + fabs(y);
373         x /= q;
374         y /= q;
375
376         if(f >= 1)
377         {
378                 // draw full rectangle
379                 R_BeginPolygon(pic, drawflag);
380                         v = centre;                     t = '0.5 0.5 0';
381                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
382                         R_PolygonVertex(v, t, rgb, a);
383
384                         v = centre;                     t = '0.5 0.5 0';
385                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
386                         R_PolygonVertex(v, t, rgb, a);
387
388                         v = centre;                     t = '0.5 0.5 0';
389                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
390                         R_PolygonVertex(v, t, rgb, a);
391
392                         v = centre;                     t = '0.5 0.5 0';
393                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
394                         R_PolygonVertex(v, t, rgb, a);
395                 R_EndPolygon();
396
397                 d = q - 1;
398                 if(d > 0)
399                 {
400                         R_BeginPolygon(pic, drawflag);
401                                 v = centre;                     t = '0.5 0.5 0';
402                                 R_PolygonVertex(v, t, rgb, a);
403
404                                 v = centre;                     t = '0.5 0.5 0';
405                                 v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
406                                 R_PolygonVertex(v, t, rgb, a);
407                 }
408         }
409         else if(f > 0.75)
410         {
411                 // draw upper and first triangle
412                 R_BeginPolygon(pic, drawflag);
413                         v = centre;                     t = '0.5 0.5 0';
414                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
415                         R_PolygonVertex(v, t, rgb, a);
416
417                         v = centre;                     t = '0.5 0.5 0';
418                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
419                         R_PolygonVertex(v, t, rgb, a);
420
421                         v = centre;                     t = '0.5 0.5 0';
422                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
423                         R_PolygonVertex(v, t, rgb, a);
424                 R_EndPolygon();
425                 R_BeginPolygon(pic, drawflag);
426                         v = centre;                     t = '0.5 0.5 0';
427                         R_PolygonVertex(v, t, rgb, a);
428
429                         v = centre;                     t = '0.5 0.5 0';
430                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
431                         R_PolygonVertex(v, t, rgb, a);
432
433                         v = centre;                     t = '0.5 0.5 0';
434                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
435                         R_PolygonVertex(v, t, rgb, a);
436
437                 d = q - 0.75;
438                 if(d <= 0)
439                         R_EndPolygon();
440         }
441         else if(f > 0.5)
442         {
443                 // draw upper triangle
444                 R_BeginPolygon(pic, drawflag);
445                         v = centre;                     t = '0.5 0.5 0';
446                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
447                         R_PolygonVertex(v, t, rgb, a);
448
449                         v = centre;                     t = '0.5 0.5 0';
450                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
451                         R_PolygonVertex(v, t, rgb, a);
452
453                         v = centre;                     t = '0.5 0.5 0';
454                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
455                         R_PolygonVertex(v, t, rgb, a);
456                 R_EndPolygon();
457
458                 d = q - 0.5;
459                 if(d > 0)
460                 {
461                         R_BeginPolygon(pic, drawflag);
462                                 v = centre;                     t = '0.5 0.5 0';
463                                 R_PolygonVertex(v, t, rgb, a);
464
465                                 v = centre;                     t = '0.5 0.5 0';
466                                 v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
467                                 R_PolygonVertex(v, t, rgb, a);
468                 }
469         }
470         else if(f > 0.25)
471         {
472                 // draw first triangle
473                 R_BeginPolygon(pic, drawflag);
474                         v = centre;                     t = '0.5 0.5 0';
475                         R_PolygonVertex(v, t, rgb, a);
476
477                         v = centre;                     t = '0.5 0.5 0';
478                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
479                         R_PolygonVertex(v, t, rgb, a);
480
481                         v = centre;                     t = '0.5 0.5 0';
482                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
483                         R_PolygonVertex(v, t, rgb, a);
484
485                 d = q - 0.25;
486                 if(d <= 0)
487                         R_EndPolygon();
488         }
489         else
490         {
491                 d = q;
492                 if(d > 0)
493                 {
494                         R_BeginPolygon(pic, drawflag);
495                                 v = centre;                     t = '0.5 0.5 0';
496                                 R_PolygonVertex(v, t, rgb, a);
497
498                                 v = centre;                     t = '0.5 0.5 0';
499                                 v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
500                                 R_PolygonVertex(v, t, rgb, a);
501                 }
502         }
503
504         if(d > 0)
505         {
506                         v = centre;                     t = '0.5 0.5 0';
507                         v.x += x * 0.5 * ringsize.x;    t += x * '0.5 0.5 0';
508                         v.y += y * 0.5 * ringsize.y;    t += y * '0.5 -0.5 0';
509                         R_PolygonVertex(v, t, rgb, a);
510                 R_EndPolygon();
511         }
512 }
513
514 vector getplayerorigin(int pl)
515 {
516         entity e;
517
518         e = CSQCModel_server2csqc(pl + 1);
519         if(e)
520                 return e.origin;
521
522         e = entcs_receiver[pl];
523         if(e)
524                 return e.origin;
525
526         return GETPLAYERORIGIN_ERROR;
527 }
528
529 float getplayeralpha(float pl)
530 {
531         entity e;
532
533         e = CSQCModel_server2csqc(pl + 1);
534         if(e)
535                 return e.alpha;
536
537         return 1;
538 }
539
540 vector getcsqcplayercolor(float pl)
541 {
542         entity e;
543
544         e = CSQCModel_server2csqc(pl);
545         if(e)
546         {
547                 if(e.colormap > 0)
548                         return colormapPaletteColor(((e.colormap >= 1024) ? e.colormap : stof(getplayerkeyvalue(e.colormap - 1, "colors"))) & 0x0F, true);
549         }
550
551         return '1 1 1';
552 }
553
554 float getplayerisdead(float pl)
555 {
556         entity e;
557
558         e = CSQCModel_server2csqc(pl + 1);
559         if(e)
560                 return e.csqcmodel_isdead;
561
562         return false;
563 }
564
565 void URI_Get_Callback(int id, float status, string data)
566 {
567         if(url_URI_Get_Callback(id, status, data))
568         {
569                 // handled
570         }
571         else if (id == URI_GET_DISCARD)
572         {
573                 // discard
574         }
575         else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
576         {
577                 // sv_cmd curl
578                 Curl_URI_Get_Callback(id, status, data);
579         }
580         else
581         {
582                 printf("Received HTTP request data for an invalid id %d.\n", id);
583         }
584 }
585
586 void draw_beginBoldFont()
587 {
588         drawfont = FONT_USER+2;
589 }
590
591 void draw_endBoldFont()
592 {
593         drawfont = FONT_USER+1;
594 }
595
596 void Accuracy_LoadLevels()
597 {
598         if(autocvar_accuracy_color_levels != acc_color_levels)
599         {
600                 if(acc_color_levels)
601                         strunzone(acc_color_levels);
602                 acc_color_levels = strzone(autocvar_accuracy_color_levels);
603                 acc_levels = tokenize_console(acc_color_levels);
604                 if(acc_levels > MAX_ACCURACY_LEVELS)
605                         acc_levels = MAX_ACCURACY_LEVELS;
606                 if(acc_levels < 2)
607                         print("Warning: accuracy_color_levels must contain at least 2 values\n");
608
609                 int i;
610                 for(i = 0; i < acc_levels; ++i)
611                         acc_lev[i] = stof(argv(i)) / 100.0;
612         }
613 }
614
615 void Accuracy_LoadColors()
616 {
617         if(time > acc_col_loadtime)
618         if(acc_levels >= 2)
619         {
620                 int i;
621                 for(i = 0; i < acc_levels; ++i)
622                         acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
623                 acc_col_loadtime = time + 2;
624         }
625 }
626
627 vector Accuracy_GetColor(float accuracy)
628 {
629         float factor;
630         vector color;
631         if(acc_levels < 2)
632                 return '0 0 0'; // return black, can't determine the right color
633
634         // find the max level lower than acc
635         int j = acc_levels-1;
636         while(j && accuracy < acc_lev[j])
637                 --j;
638
639         // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
640         factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
641         color = acc_col[j];
642         color = color + factor * (acc_col[j+1] - color);
643         return color;
644 }
645