1 #include "miscfunctions.qh"
5 #include "../common/command/generic.qh"
7 #include "../common/teams.qh"
9 #include "../lib/csqcmodel/cl_model.qh"
18 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
20 if(prev != e.sort_prev)
21 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
25 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
27 if(prev != e.sort_prev)
28 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
33 float RegisterPlayer(entity player)
37 for(pl = players.sort_next; pl; pl = pl.sort_next)
39 error("Player already registered!");
40 player.sort_next = players.sort_next;
41 player.sort_prev = players;
43 players.sort_next.sort_prev = player;
44 players.sort_next = player;
49 void RemovePlayer(entity player)
54 for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
59 error("Trying to remove a player which is not in the playerlist!");
62 parent.sort_next = player.sort_next;
64 player.sort_next.sort_prev = parent;
68 void MoveToLast(entity e)
80 float RegisterTeam(entity Team)
84 for(tm = teams.sort_next; tm; tm = tm.sort_next)
86 error("Team already registered!");
87 Team.sort_next = teams.sort_next;
88 Team.sort_prev = teams;
90 teams.sort_next.sort_prev = Team;
91 teams.sort_next = Team;
92 if(Team.team && Team.team != NUM_SPECTATOR)
98 void RemoveTeam(entity Team)
103 for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
108 LOG_INFO(_("Trying to remove a team which is not in the teamlist!"));
111 parent.sort_next = Team.sort_next;
113 Team.sort_next.sort_prev = parent;
114 if(Team.team && Team.team != NUM_SPECTATOR)
119 entity GetTeam(int Team, bool add)
121 int num = (Team == NUM_SPECTATOR) ? 16 : Team;
123 return teamslots[num];
126 entity tm = new(team);
134 vector HUD_GetFontsize(string cvarname)
137 v = stov(cvar_string(cvarname));
146 float PreviewExists(string name)
148 if(autocvar_cl_readpicture_force)
151 if (fexists(strcat(name, ".tga"))) return true;
152 if (fexists(strcat(name, ".png"))) return true;
153 if (fexists(strcat(name, ".jpg"))) return true;
154 if (fexists(strcat(name, ".pcx"))) return true;
159 // decolorizes and team colors the player name when needed
160 string playername(string thename, float teamid)
165 t = Team_ColorCode(teamid);
166 return strcat(t, strdecolorize(thename));
169 return strdecolorize(thename);
172 float cvar_or(string cv, float v)
182 vector project_3d_to_2d(vector vec)
184 vec = cs_project(vec);
185 if(cs_project_is_b0rked > 0)
187 vec.x *= vid_conwidth / vid_width;
188 vec.y *= vid_conheight / vid_height;
193 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
195 return 1.2 / (1.2 - fadelerp);
198 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
200 boxsize.x *= boxxsizefactor; // easier interface for text
201 return boxsize * (0.5 * (1 - sz));
204 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
206 vector line_dim = '0 0 0';
208 // left and right lines
210 line_dim.x = thickness;
212 drawfill(pos, line_dim, color, theAlpha, drawflag);
213 drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
215 // upper and lower lines
217 line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
218 line_dim.y = thickness;
219 drawfill(pos, line_dim, color, theAlpha, drawflag);
220 drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
223 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
225 vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
226 end_pos = pos + area;
228 current_pos.y = pos.y;
229 while (current_pos.y < end_pos.y)
231 current_pos.x = pos.x;
232 while (current_pos.x < end_pos.x)
234 new_size.x = min(sz.x, end_pos.x - current_pos.x);
235 new_size.y = min(sz.y, end_pos.y - current_pos.y);
236 ratio.x = new_size.x / sz.x;
237 ratio.y = new_size.y / sz.y;
238 drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
239 current_pos.x += sz.x;
241 current_pos.y += sz.y;
245 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
248 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
250 drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
253 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
255 drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
256 drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
259 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
260 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
261 SET_POS_AND_SZ_Y_ASPECT(false);
262 drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
265 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
266 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
267 SET_POS_AND_SZ_Y_ASPECT(true);
268 drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
271 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
274 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
276 drawfontscale = sz * '1 1 0';
277 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);
279 // (scale_x * sz / drawfontscale_x) * drawfontscale_x * SIZE1 / (scale_x * sz)
281 drawfontscale = '1 1 0';
284 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
285 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
286 SET_POS_AND_SZ_Y_ASPECT(false);
287 drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
290 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
293 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
295 drawfontscale = sz * '1 1 0';
296 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);
297 drawfontscale = '1 1 0';
300 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
301 SET_POS_AND_SZ_Y_ASPECT(true);
302 drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
305 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
306 float PolyDrawModelSurface(entity e, float i_s)
312 tex = getsurfacetexture(e, i_s);
314 return 0; // this is beyond the last one
315 n_t = getsurfacenumtriangles(e, i_s);
316 for(i_t = 0; i_t < n_t; ++i_t)
318 tri = getsurfacetriangle(e, i_s, i_t);
319 R_BeginPolygon(tex, 0);
320 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
321 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
322 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
327 void PolyDrawModel(entity e)
330 for(i_s = 0; ; ++i_s)
331 if(!PolyDrawModelSurface(e, i_s))
335 void DrawCircleClippedPic(vector centre, float radi, string pic, float f, vector rgb, float a, float drawflag)
338 vector ringsize, v, t;
339 ringsize = radi * '1 1 0';
341 x = cos(f * 2 * M_PI);
342 y = sin(f * 2 * M_PI);
343 q = fabs(x) + fabs(y);
349 // draw full rectangle
350 R_BeginPolygon(pic, drawflag);
351 v = centre; t = '0.5 0.5 0';
352 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
353 R_PolygonVertex(v, t, rgb, a);
355 v = centre; t = '0.5 0.5 0';
356 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
357 R_PolygonVertex(v, t, rgb, a);
359 v = centre; t = '0.5 0.5 0';
360 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
361 R_PolygonVertex(v, t, rgb, a);
363 v = centre; t = '0.5 0.5 0';
364 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
365 R_PolygonVertex(v, t, rgb, a);
371 R_BeginPolygon(pic, drawflag);
372 v = centre; t = '0.5 0.5 0';
373 R_PolygonVertex(v, t, rgb, a);
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);
382 // draw upper and first triangle
383 R_BeginPolygon(pic, drawflag);
384 v = centre; t = '0.5 0.5 0';
385 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
386 R_PolygonVertex(v, t, rgb, a);
388 v = centre; t = '0.5 0.5 0';
389 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
390 R_PolygonVertex(v, t, rgb, a);
392 v = centre; t = '0.5 0.5 0';
393 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
394 R_PolygonVertex(v, t, rgb, a);
396 R_BeginPolygon(pic, drawflag);
397 v = centre; t = '0.5 0.5 0';
398 R_PolygonVertex(v, t, rgb, a);
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);
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);
414 // draw upper triangle
415 R_BeginPolygon(pic, drawflag);
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);
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);
424 v = centre; t = '0.5 0.5 0';
425 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
426 R_PolygonVertex(v, t, rgb, a);
432 R_BeginPolygon(pic, drawflag);
433 v = centre; t = '0.5 0.5 0';
434 R_PolygonVertex(v, t, rgb, a);
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);
443 // draw first triangle
444 R_BeginPolygon(pic, drawflag);
445 v = centre; t = '0.5 0.5 0';
446 R_PolygonVertex(v, t, rgb, a);
448 v = centre; t = '0.5 0.5 0';
449 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
450 R_PolygonVertex(v, t, rgb, a);
452 v = centre; t = '0.5 0.5 0';
453 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
454 R_PolygonVertex(v, t, rgb, a);
465 R_BeginPolygon(pic, drawflag);
466 v = centre; t = '0.5 0.5 0';
467 R_PolygonVertex(v, t, rgb, a);
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);
477 v = centre; t = '0.5 0.5 0';
478 v.x += x * 0.5 * ringsize.x; t += x * '0.5 0.5 0';
479 v.y += y * 0.5 * ringsize.y; t += y * '0.5 -0.5 0';
480 R_PolygonVertex(v, t, rgb, a);
485 vector getplayerorigin(int pl)
489 e = CSQCModel_server2csqc(pl + 1);
493 e = entcs_receiver[pl];
497 return GETPLAYERORIGIN_ERROR;
500 float getplayeralpha(float pl)
504 e = CSQCModel_server2csqc(pl + 1);
511 vector getcsqcplayercolor(float pl)
515 e = CSQCModel_server2csqc(pl);
519 return colormapPaletteColor(((e.colormap >= 1024) ? e.colormap : stof(getplayerkeyvalue(e.colormap - 1, "colors"))) & 0x0F, true);
525 float getplayerisdead(float pl)
529 e = CSQCModel_server2csqc(pl + 1);
531 return e.csqcmodel_isdead;
536 /** engine callback */
537 void URI_Get_Callback(int id, float status, string data)
539 if(url_URI_Get_Callback(id, status, data))
543 else if (id == URI_GET_DISCARD)
547 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
550 Curl_URI_Get_Callback(id, status, data);
554 LOG_INFOF("Received HTTP request data for an invalid id %d.\n", id);
558 void Accuracy_LoadLevels()
560 if(autocvar_accuracy_color_levels != acc_color_levels)
563 strunzone(acc_color_levels);
564 acc_color_levels = strzone(autocvar_accuracy_color_levels);
565 acc_levels = tokenize_console(acc_color_levels);
566 if(acc_levels > MAX_ACCURACY_LEVELS)
567 acc_levels = MAX_ACCURACY_LEVELS;
569 LOG_INFO("Warning: accuracy_color_levels must contain at least 2 values\n");
572 for(i = 0; i < acc_levels; ++i)
573 acc_lev[i] = stof(argv(i)) / 100.0;
577 void Accuracy_LoadColors()
579 if(time > acc_col_loadtime)
583 for(i = 0; i < acc_levels; ++i)
584 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
585 acc_col_loadtime = time + 2;
589 vector Accuracy_GetColor(float accuracy)
594 return '0 0 0'; // return black, can't determine the right color
596 // find the max level lower than acc
597 int j = acc_levels-1;
598 while(j && accuracy < acc_lev[j])
601 // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
602 factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
604 color = color + factor * (acc_col[j+1] - color);