1 #include "miscfunctions.qh"
3 #include "../common/urllib.qh"
5 #include "../common/command/generic.qh"
13 oself.think = SUB_Remove;
14 oself.nextthink = time;
20 Execute func() after time + fdelay.
21 self when func is executed = self when defer is called
23 void defer(float fdelay, void() func)
28 e.classname = "defer";
31 e.think = defer_think;
32 e.nextthink = time + fdelay;
35 void defer_clear(entity ent)
38 for(e = world; (e = find(e, classname, "defer")); )
50 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
52 if(prev != e.sort_prev)
53 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
57 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
59 if(prev != e.sort_prev)
60 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
65 float RegisterPlayer(entity player)
69 for(pl = players.sort_next; pl; pl = pl.sort_next)
71 error("Player already registered!");
72 player.sort_next = players.sort_next;
73 player.sort_prev = players;
75 players.sort_next.sort_prev = player;
76 players.sort_next = player;
81 void RemovePlayer(entity player)
86 for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
91 error("Trying to remove a player which is not in the playerlist!");
94 parent.sort_next = player.sort_next;
96 player.sort_next.sort_prev = parent;
100 void MoveToLast(entity e)
112 float RegisterTeam(entity Team)
116 for(tm = teams.sort_next; tm; tm = tm.sort_next)
118 error("Team already registered!");
119 Team.sort_next = teams.sort_next;
120 Team.sort_prev = teams;
122 teams.sort_next.sort_prev = Team;
123 teams.sort_next = Team;
124 if(Team.team && Team.team != NUM_SPECTATOR)
130 void RemoveTeam(entity Team)
135 for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
140 print(_("Trying to remove a team which is not in the teamlist!"));
143 parent.sort_next = Team.sort_next;
145 Team.sort_next.sort_prev = parent;
146 if(Team.team && Team.team != NUM_SPECTATOR)
151 entity GetTeam(int Team, bool add)
153 int num = (Team == NUM_SPECTATOR) ? 16 : Team;
155 return teamslots[num];
165 vector HUD_GetFontsize(string cvarname)
168 v = stov(cvar_string(cvarname));
177 float PreviewExists(string name)
179 if(autocvar_cl_readpicture_force)
182 if (fexists(strcat(name, ".tga"))) return true;
183 if (fexists(strcat(name, ".png"))) return true;
184 if (fexists(strcat(name, ".jpg"))) return true;
185 if (fexists(strcat(name, ".pcx"))) return true;
190 vector rotate(vector v, float a)
193 // FTEQCC SUCKS AGAIN
194 w.x = v.x * cos(a) + v.y * sin(a);
195 w.y = -1 * v.x * sin(a) + v.y * cos(a);
199 string ColorTranslateRGB(string s)
201 if(ColorTranslateMode & 1)
202 return strdecolorize(s);
207 // decolorizes and team colors the player name when needed
208 string playername(string thename, float teamid)
213 t = Team_ColorCode(teamid);
214 return strcat(t, strdecolorize(thename));
217 return strdecolorize(thename);
220 float cvar_or(string cv, float v)
230 vector project_3d_to_2d(vector vec)
232 vec = cs_project(vec);
233 if(cs_project_is_b0rked > 0)
235 vec.x *= vid_conwidth / vid_width;
236 vec.y *= vid_conheight / vid_height;
241 void dummyfunction(float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
245 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
247 return 1.2 / (1.2 - fadelerp);
250 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
252 boxsize.x *= boxxsizefactor; // easier interface for text
253 return boxsize * (0.5 * (1 - sz));
256 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
258 vector line_dim = '0 0 0';
260 // left and right lines
262 line_dim.x = thickness;
264 drawfill(pos, line_dim, color, theAlpha, drawflag);
265 drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
267 // upper and lower lines
269 line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
270 line_dim.y = thickness;
271 drawfill(pos, line_dim, color, theAlpha, drawflag);
272 drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
275 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
277 vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
278 end_pos = pos + area;
280 current_pos.y = pos.y;
281 while (current_pos.y < end_pos.y)
283 current_pos.x = pos.x;
284 while (current_pos.x < end_pos.x)
286 new_size.x = min(sz.x, end_pos.x - current_pos.x);
287 new_size.y = min(sz.y, end_pos.y - current_pos.y);
288 ratio.x = new_size.x / sz.x;
289 ratio.y = new_size.y / sz.y;
290 drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
291 current_pos.x += sz.x;
293 current_pos.y += sz.y;
297 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
300 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
302 drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
305 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
307 drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
308 drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
311 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
312 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
313 SET_POS_AND_SZ_Y_ASPECT(false);
314 drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
317 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
318 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
319 SET_POS_AND_SZ_Y_ASPECT(true);
320 drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
323 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
326 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
328 drawfontscale = sz * '1 1 0';
329 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
330 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);
332 // (scale_x * sz / drawfontscale_x) * drawfontscale_x * SIZE1 / (scale_x * sz)
334 drawfontscale = '1 1 0';
337 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
338 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
339 SET_POS_AND_SZ_Y_ASPECT(false);
340 drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
343 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
346 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
348 drawfontscale = sz * '1 1 0';
349 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
350 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);
351 drawfontscale = '1 1 0';
354 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
355 SET_POS_AND_SZ_Y_ASPECT(true);
356 drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
359 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
360 float PolyDrawModelSurface(entity e, float i_s)
366 tex = getsurfacetexture(e, i_s);
368 return 0; // this is beyond the last one
369 n_t = getsurfacenumtriangles(e, i_s);
370 for(i_t = 0; i_t < n_t; ++i_t)
372 tri = getsurfacetriangle(e, i_s, i_t);
373 R_BeginPolygon(tex, 0);
374 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
375 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
376 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
381 void PolyDrawModel(entity e)
384 for(i_s = 0; ; ++i_s)
385 if(!PolyDrawModelSurface(e, i_s))
389 void DrawCircleClippedPic(vector centre, float radius, string pic, float f, vector rgb, float a, float drawflag)
392 vector ringsize, v, t;
393 ringsize = radius * '1 1 0';
395 x = cos(f * 2 * M_PI);
396 y = sin(f * 2 * M_PI);
397 q = fabs(x) + fabs(y);
403 // draw full rectangle
404 R_BeginPolygon(pic, drawflag);
405 v = centre; t = '0.5 0.5 0';
406 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
407 R_PolygonVertex(v, t, rgb, a);
409 v = centre; t = '0.5 0.5 0';
410 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
411 R_PolygonVertex(v, t, rgb, a);
413 v = centre; t = '0.5 0.5 0';
414 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
415 R_PolygonVertex(v, t, rgb, a);
417 v = centre; t = '0.5 0.5 0';
418 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
419 R_PolygonVertex(v, t, rgb, a);
425 R_BeginPolygon(pic, drawflag);
426 v = centre; t = '0.5 0.5 0';
427 R_PolygonVertex(v, t, rgb, a);
429 v = centre; t = '0.5 0.5 0';
430 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
431 R_PolygonVertex(v, t, rgb, a);
436 // draw upper and first triangle
437 R_BeginPolygon(pic, drawflag);
438 v = centre; t = '0.5 0.5 0';
439 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
440 R_PolygonVertex(v, t, rgb, a);
442 v = centre; t = '0.5 0.5 0';
443 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
444 R_PolygonVertex(v, t, rgb, a);
446 v = centre; t = '0.5 0.5 0';
447 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
448 R_PolygonVertex(v, t, rgb, a);
450 R_BeginPolygon(pic, drawflag);
451 v = centre; t = '0.5 0.5 0';
452 R_PolygonVertex(v, t, rgb, a);
454 v = centre; t = '0.5 0.5 0';
455 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
456 R_PolygonVertex(v, t, rgb, a);
458 v = centre; t = '0.5 0.5 0';
459 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
460 R_PolygonVertex(v, t, rgb, a);
468 // draw upper triangle
469 R_BeginPolygon(pic, drawflag);
470 v = centre; t = '0.5 0.5 0';
471 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
472 R_PolygonVertex(v, t, rgb, a);
474 v = centre; t = '0.5 0.5 0';
475 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
476 R_PolygonVertex(v, t, rgb, a);
478 v = centre; t = '0.5 0.5 0';
479 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
480 R_PolygonVertex(v, t, rgb, a);
486 R_BeginPolygon(pic, drawflag);
487 v = centre; t = '0.5 0.5 0';
488 R_PolygonVertex(v, t, rgb, a);
490 v = centre; t = '0.5 0.5 0';
491 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
492 R_PolygonVertex(v, t, rgb, a);
497 // draw first triangle
498 R_BeginPolygon(pic, drawflag);
499 v = centre; t = '0.5 0.5 0';
500 R_PolygonVertex(v, t, rgb, a);
502 v = centre; t = '0.5 0.5 0';
503 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
504 R_PolygonVertex(v, t, rgb, a);
506 v = centre; t = '0.5 0.5 0';
507 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
508 R_PolygonVertex(v, t, rgb, a);
519 R_BeginPolygon(pic, drawflag);
520 v = centre; t = '0.5 0.5 0';
521 R_PolygonVertex(v, t, rgb, a);
523 v = centre; t = '0.5 0.5 0';
524 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
525 R_PolygonVertex(v, t, rgb, a);
531 v = centre; t = '0.5 0.5 0';
532 v.x += x * 0.5 * ringsize.x; t += x * '0.5 0.5 0';
533 v.y += y * 0.5 * ringsize.y; t += y * '0.5 -0.5 0';
534 R_PolygonVertex(v, t, rgb, a);
539 vector getplayerorigin(int pl)
543 e = CSQCModel_server2csqc(pl + 1);
547 e = entcs_receiver[pl];
551 return GETPLAYERORIGIN_ERROR;
554 float getplayeralpha(float pl)
558 e = CSQCModel_server2csqc(pl + 1);
565 vector getcsqcplayercolor(float pl)
569 e = CSQCModel_server2csqc(pl);
573 return colormapPaletteColor(((e.colormap >= 1024) ? e.colormap : stof(getplayerkeyvalue(e.colormap - 1, "colors"))) & 0x0F, true);
579 float getplayerisdead(float pl)
583 e = CSQCModel_server2csqc(pl + 1);
585 return e.csqcmodel_isdead;
590 void URI_Get_Callback(int id, float status, string data)
592 if(url_URI_Get_Callback(id, status, data))
596 else if (id == URI_GET_DISCARD)
600 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
603 Curl_URI_Get_Callback(id, status, data);
607 printf("Received HTTP request data for an invalid id %d.\n", id);
611 void draw_beginBoldFont()
613 drawfont = FONT_USER+2;
616 void draw_endBoldFont()
618 drawfont = FONT_USER+1;
621 void Accuracy_LoadLevels()
623 if(autocvar_accuracy_color_levels != acc_color_levels)
626 strunzone(acc_color_levels);
627 acc_color_levels = strzone(autocvar_accuracy_color_levels);
628 acc_levels = tokenize_console(acc_color_levels);
629 if(acc_levels > MAX_ACCURACY_LEVELS)
630 acc_levels = MAX_ACCURACY_LEVELS;
632 print("Warning: accuracy_color_levels must contain at least 2 values\n");
635 for(i = 0; i < acc_levels; ++i)
636 acc_lev[i] = stof(argv(i)) / 100.0;
640 void Accuracy_LoadColors()
642 if(time > acc_col_loadtime)
646 for(i = 0; i < acc_levels; ++i)
647 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
648 acc_col_loadtime = time + 2;
652 vector Accuracy_GetColor(float accuracy)
657 return '0 0 0'; // return black, can't determine the right color
659 // find the max level lower than acc
660 int j = acc_levels-1;
661 while(j && accuracy < acc_lev[j])
664 // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
665 factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
667 color = color + factor * (acc_col[j+1] - color);