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