1 #include "miscfunctions.qh"
3 #include "../common/urllib.qh"
5 #include "../common/command/generic.qh"
7 void InitializeEntity(entity e, void(void) func, float order)
11 if (!e || e.initialize_entity)
13 // make a proxy initializer entity
17 e.classname = "initialize_entity";
21 e.initialize_entity = func;
22 e.initialize_entity_order = order;
24 cur = initialize_entity_first;
28 if (!cur || cur.initialize_entity_order > order)
30 // insert between prev and cur
32 prev.initialize_entity_next = e;
34 initialize_entity_first = e;
35 e.initialize_entity_next = cur;
39 cur = cur.initialize_entity_next;
42 void InitializeEntitiesRun()
45 startoflist = initialize_entity_first;
46 initialize_entity_first = world;
47 for (self = startoflist; self; self = self.initialize_entity_next)
49 //self.remove_except_protected_forbidden = 1;
51 for (self = startoflist; self; )
55 e = self.initialize_entity_next;
56 func = self.initialize_entity;
57 self.initialize_entity_order = 0;
58 self.initialize_entity = func_null;
59 self.initialize_entity_next = world;
60 //self.remove_except_protected_forbidden = 0;
61 if (self.classname == "initialize_entity")
68 //dprint("Delayed initialization: ", self.classname, "\n");
74 backtrace(strcat("Null function in: ", self.classname, "\n"));
86 oself.think = SUB_Remove;
87 oself.nextthink = time;
93 Execute func() after time + fdelay.
94 self when func is executed = self when defer is called
96 void defer(float fdelay, void() func)
101 e.classname = "defer";
104 e.think = defer_think;
105 e.nextthink = time + fdelay;
108 void defer_clear(entity ent)
111 for(e = world; (e = find(e, classname, "defer")); )
123 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
125 if(prev != e.sort_prev)
126 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
130 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
132 if(prev != e.sort_prev)
133 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
138 float RegisterPlayer(entity player)
142 for(pl = players.sort_next; pl; pl = pl.sort_next)
144 error("Player already registered!");
145 player.sort_next = players.sort_next;
146 player.sort_prev = players;
147 if(players.sort_next)
148 players.sort_next.sort_prev = player;
149 players.sort_next = player;
154 void RemovePlayer(entity player)
159 for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
164 error("Trying to remove a player which is not in the playerlist!");
167 parent.sort_next = player.sort_next;
169 player.sort_next.sort_prev = parent;
173 void MoveToLast(entity e)
185 float RegisterTeam(entity Team)
189 for(tm = teams.sort_next; tm; tm = tm.sort_next)
191 error("Team already registered!");
192 Team.sort_next = teams.sort_next;
193 Team.sort_prev = teams;
195 teams.sort_next.sort_prev = Team;
196 teams.sort_next = Team;
197 if(Team.team && Team.team != NUM_SPECTATOR)
203 void RemoveTeam(entity Team)
208 for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
213 print(_("Trying to remove a team which is not in the teamlist!"));
216 parent.sort_next = Team.sort_next;
218 Team.sort_next.sort_prev = parent;
219 if(Team.team && Team.team != NUM_SPECTATOR)
224 entity GetTeam(int Team, bool add)
226 int num = (Team == NUM_SPECTATOR) ? 16 : Team;
228 return teamslots[num];
238 vector HUD_GetFontsize(string cvarname)
241 v = stov(cvar_string(cvarname));
250 float PreviewExists(string name)
252 if(autocvar_cl_readpicture_force)
255 if (fexists(strcat(name, ".tga"))) return true;
256 if (fexists(strcat(name, ".png"))) return true;
257 if (fexists(strcat(name, ".jpg"))) return true;
258 if (fexists(strcat(name, ".pcx"))) return true;
263 vector rotate(vector v, float a)
266 // FTEQCC SUCKS AGAIN
267 w.x = v.x * cos(a) + v.y * sin(a);
268 w.y = -1 * v.x * sin(a) + v.y * cos(a);
272 string ColorTranslateRGB(string s)
274 if(ColorTranslateMode & 1)
275 return strdecolorize(s);
280 // decolorizes and team colors the player name when needed
281 string playername(string thename, float teamid)
286 t = Team_ColorCode(teamid);
287 return strcat(t, strdecolorize(thename));
290 return strdecolorize(thename);
293 float cvar_or(string cv, float v)
303 vector project_3d_to_2d(vector vec)
305 vec = cs_project(vec);
306 if(cs_project_is_b0rked > 0)
308 vec.x *= vid_conwidth / vid_width;
309 vec.y *= vid_conheight / vid_height;
314 void dummyfunction(float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
318 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
320 return 1.2 / (1.2 - fadelerp);
323 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
325 boxsize.x *= boxxsizefactor; // easier interface for text
326 return boxsize * (0.5 * (1 - sz));
329 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
331 vector line_dim = '0 0 0';
333 // left and right lines
335 line_dim.x = thickness;
337 drawfill(pos, line_dim, color, theAlpha, drawflag);
338 drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
340 // upper and lower lines
342 line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
343 line_dim.y = thickness;
344 drawfill(pos, line_dim, color, theAlpha, drawflag);
345 drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
348 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
350 vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
351 end_pos = pos + area;
353 current_pos.y = pos.y;
354 while (current_pos.y < end_pos.y)
356 current_pos.x = pos.x;
357 while (current_pos.x < end_pos.x)
359 new_size.x = min(sz.x, end_pos.x - current_pos.x);
360 new_size.y = min(sz.y, end_pos.y - current_pos.y);
361 ratio.x = new_size.x / sz.x;
362 ratio.y = new_size.y / sz.y;
363 drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
364 current_pos.x += sz.x;
366 current_pos.y += sz.y;
370 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
373 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
375 drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
378 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
380 drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
381 drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
384 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
385 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
386 SET_POS_AND_SZ_Y_ASPECT(false);
387 drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
390 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
391 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
392 SET_POS_AND_SZ_Y_ASPECT(true);
393 drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
396 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
399 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
401 drawfontscale = sz * '1 1 0';
402 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
403 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);
405 // (scale_x * sz / drawfontscale_x) * drawfontscale_x * SIZE1 / (scale_x * sz)
407 drawfontscale = '1 1 0';
410 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
411 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
412 SET_POS_AND_SZ_Y_ASPECT(false);
413 drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
416 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
419 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
421 drawfontscale = sz * '1 1 0';
422 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
423 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);
424 drawfontscale = '1 1 0';
427 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
428 SET_POS_AND_SZ_Y_ASPECT(true);
429 drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
432 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
433 float PolyDrawModelSurface(entity e, float i_s)
439 tex = getsurfacetexture(e, i_s);
441 return 0; // this is beyond the last one
442 n_t = getsurfacenumtriangles(e, i_s);
443 for(i_t = 0; i_t < n_t; ++i_t)
445 tri = getsurfacetriangle(e, i_s, i_t);
446 R_BeginPolygon(tex, 0);
447 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
448 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
449 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
454 void PolyDrawModel(entity e)
457 for(i_s = 0; ; ++i_s)
458 if(!PolyDrawModelSurface(e, i_s))
462 void DrawCircleClippedPic(vector centre, float radi, string pic, float f, vector rgb, float a, float drawflag)
465 vector ringsize, v, t;
466 ringsize = radi * '1 1 0';
468 x = cos(f * 2 * M_PI);
469 y = sin(f * 2 * M_PI);
470 q = fabs(x) + fabs(y);
476 // draw full rectangle
477 R_BeginPolygon(pic, drawflag);
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);
482 v = centre; t = '0.5 0.5 0';
483 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
484 R_PolygonVertex(v, t, rgb, a);
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);
490 v = centre; t = '0.5 0.5 0';
491 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
492 R_PolygonVertex(v, t, rgb, a);
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);
509 // draw upper and first triangle
510 R_BeginPolygon(pic, drawflag);
511 v = centre; t = '0.5 0.5 0';
512 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
513 R_PolygonVertex(v, t, rgb, a);
515 v = centre; t = '0.5 0.5 0';
516 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
517 R_PolygonVertex(v, t, rgb, a);
519 v = centre; t = '0.5 0.5 0';
520 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
521 R_PolygonVertex(v, t, rgb, a);
523 R_BeginPolygon(pic, drawflag);
524 v = centre; t = '0.5 0.5 0';
525 R_PolygonVertex(v, t, rgb, a);
527 v = centre; t = '0.5 0.5 0';
528 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
529 R_PolygonVertex(v, t, rgb, a);
531 v = centre; t = '0.5 0.5 0';
532 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
533 R_PolygonVertex(v, t, rgb, a);
541 // draw upper triangle
542 R_BeginPolygon(pic, drawflag);
543 v = centre; t = '0.5 0.5 0';
544 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
545 R_PolygonVertex(v, t, rgb, a);
547 v = centre; t = '0.5 0.5 0';
548 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
549 R_PolygonVertex(v, t, rgb, a);
551 v = centre; t = '0.5 0.5 0';
552 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
553 R_PolygonVertex(v, t, rgb, a);
559 R_BeginPolygon(pic, drawflag);
560 v = centre; t = '0.5 0.5 0';
561 R_PolygonVertex(v, t, rgb, a);
563 v = centre; t = '0.5 0.5 0';
564 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
565 R_PolygonVertex(v, t, rgb, a);
570 // draw first triangle
571 R_BeginPolygon(pic, drawflag);
572 v = centre; t = '0.5 0.5 0';
573 R_PolygonVertex(v, t, rgb, a);
575 v = centre; t = '0.5 0.5 0';
576 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
577 R_PolygonVertex(v, t, rgb, a);
579 v = centre; t = '0.5 0.5 0';
580 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
581 R_PolygonVertex(v, t, rgb, a);
592 R_BeginPolygon(pic, drawflag);
593 v = centre; t = '0.5 0.5 0';
594 R_PolygonVertex(v, t, rgb, a);
596 v = centre; t = '0.5 0.5 0';
597 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
598 R_PolygonVertex(v, t, rgb, a);
604 v = centre; t = '0.5 0.5 0';
605 v.x += x * 0.5 * ringsize.x; t += x * '0.5 0.5 0';
606 v.y += y * 0.5 * ringsize.y; t += y * '0.5 -0.5 0';
607 R_PolygonVertex(v, t, rgb, a);
612 vector getplayerorigin(int pl)
616 e = CSQCModel_server2csqc(pl + 1);
620 e = entcs_receiver[pl];
624 return GETPLAYERORIGIN_ERROR;
627 float getplayeralpha(float pl)
631 e = CSQCModel_server2csqc(pl + 1);
638 vector getcsqcplayercolor(float pl)
642 e = CSQCModel_server2csqc(pl);
646 return colormapPaletteColor(((e.colormap >= 1024) ? e.colormap : stof(getplayerkeyvalue(e.colormap - 1, "colors"))) & 0x0F, true);
652 float getplayerisdead(float pl)
656 e = CSQCModel_server2csqc(pl + 1);
658 return e.csqcmodel_isdead;
663 void URI_Get_Callback(int id, float status, string data)
665 if(url_URI_Get_Callback(id, status, data))
669 else if (id == URI_GET_DISCARD)
673 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
676 Curl_URI_Get_Callback(id, status, data);
680 printf("Received HTTP request data for an invalid id %d.\n", id);
684 void draw_beginBoldFont()
686 drawfont = FONT_USER+2;
689 void draw_endBoldFont()
691 drawfont = FONT_USER+1;
694 void Accuracy_LoadLevels()
696 if(autocvar_accuracy_color_levels != acc_color_levels)
699 strunzone(acc_color_levels);
700 acc_color_levels = strzone(autocvar_accuracy_color_levels);
701 acc_levels = tokenize_console(acc_color_levels);
702 if(acc_levels > MAX_ACCURACY_LEVELS)
703 acc_levels = MAX_ACCURACY_LEVELS;
705 print("Warning: accuracy_color_levels must contain at least 2 values\n");
708 for(i = 0; i < acc_levels; ++i)
709 acc_lev[i] = stof(argv(i)) / 100.0;
713 void Accuracy_LoadColors()
715 if(time > acc_col_loadtime)
719 for(i = 0; i < acc_levels; ++i)
720 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
721 acc_col_loadtime = time + 2;
725 vector Accuracy_GetColor(float accuracy)
730 return '0 0 0'; // return black, can't determine the right color
732 // find the max level lower than acc
733 int j = acc_levels-1;
734 while(j && accuracy < acc_lev[j])
737 // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
738 factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
740 color = color + factor * (acc_col[j+1] - color);