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