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