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