2 #include "../dpdefs/csprogsdefs.qc"
4 #include "../common/constants.qh"
5 #include "../warpzonelib/mathlib.qh"
6 #include "../common/teams.qh"
7 #include "../common/util.qh"
8 #include "../common/command/generic.qh"
9 #include "../common/urllib.qh"
10 #include "autocvars.qh"
13 #include "../csqcmodellib/cl_model.qh"
21 float team_count; // real teams
29 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
31 if(prev != e.sort_prev)
32 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
36 for(e = prev.sort_next; e; prev = e, e = e.sort_next)
38 if(prev != e.sort_prev)
39 error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
44 float RegisterPlayer(entity player)
48 for(pl = players.sort_next; pl; pl = pl.sort_next)
50 error("Player already registered!");
51 player.sort_next = players.sort_next;
52 player.sort_prev = players;
54 players.sort_next.sort_prev = player;
55 players.sort_next = player;
60 void RemovePlayer(entity player)
65 for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
70 error("Trying to remove a player which is not in the playerlist!");
73 parent.sort_next = player.sort_next;
75 player.sort_next.sort_prev = parent;
79 void MoveToLast(entity e)
91 float RegisterTeam(entity Team)
95 for(tm = teams.sort_next; tm; tm = tm.sort_next)
97 error("Team already registered!");
98 Team.sort_next = teams.sort_next;
99 Team.sort_prev = teams;
101 teams.sort_next.sort_prev = Team;
102 teams.sort_next = Team;
103 if(Team.team && Team.team != NUM_SPECTATOR)
109 void RemoveTeam(entity Team)
114 for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
119 print(_("Trying to remove a team which is not in the teamlist!"));
122 parent.sort_next = Team.sort_next;
124 Team.sort_next.sort_prev = parent;
125 if(Team.team && Team.team != NUM_SPECTATOR)
130 entity GetTeam(int Team, bool add)
132 int num = (Team == NUM_SPECTATOR) ? 16 : Team;
134 return teamslots[num];
144 vector HUD_GetFontsize(string cvarname)
147 v = stov(cvar_string(cvarname));
156 float PreviewExists(string name)
158 if(autocvar_cl_readpicture_force)
161 if (fexists(strcat(name, ".tga"))) return true;
162 if (fexists(strcat(name, ".png"))) return true;
163 if (fexists(strcat(name, ".jpg"))) return true;
164 if (fexists(strcat(name, ".pcx"))) return true;
169 vector rotate(vector v, float a)
172 // FTEQCC SUCKS AGAIN
173 w_x = v.x * cos(a) + v.y * sin(a);
174 w_y = -1 * v.x * sin(a) + v.y * cos(a);
178 int ColorTranslateMode;
180 string ColorTranslateRGB(string s)
182 if(ColorTranslateMode & 1)
183 return strdecolorize(s);
188 // decolorizes and team colors the player name when needed
189 string playername(string thename, float teamid)
194 t = Team_ColorCode(teamid);
195 return strcat(t, strdecolorize(thename));
198 return strdecolorize(thename);
201 float cvar_or(string cv, float v)
211 vector project_3d_to_2d(vector vec)
213 vec = cs_project(vec);
214 if(cs_project_is_b0rked > 0)
216 vec.x *= vid_conwidth / vid_width;
217 vec.y *= vid_conheight / vid_height;
222 void dummyfunction(float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
226 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
228 return 1.2 / (1.2 - fadelerp);
231 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
233 boxsize.x *= boxxsizefactor; // easier interface for text
234 return boxsize * (0.5 * (1 - sz));
237 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
239 vector line_dim = '0 0 0';
241 // left and right lines
243 line_dim_x = thickness;
245 drawfill(pos, line_dim, color, theAlpha, drawflag);
246 drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
248 // upper and lower lines
250 line_dim_x = dim.x + thickness * 2; // make upper and lower lines longer
251 line_dim_y = thickness;
252 drawfill(pos, line_dim, color, theAlpha, drawflag);
253 drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
256 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
258 vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
259 end_pos = pos + area;
261 current_pos_y = pos.y;
262 while (current_pos.y < end_pos.y)
264 current_pos_x = pos.x;
265 while (current_pos.x < end_pos.x)
267 new_size_x = min(sz.x, end_pos.x - current_pos.x);
268 new_size_y = min(sz.y, end_pos.y - current_pos.y);
269 ratio_x = new_size.x / sz.x;
270 ratio_y = new_size.y / sz.y;
271 drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
272 current_pos.x += sz.x;
274 current_pos.y += sz.y;
278 // drawpic wrapper to draw an image as large as possible with preserved aspect ratio into a box
279 float _drawpic_imgaspect;
280 vector _drawpic_imgsize;
282 float _drawpic_oldsz;
283 string _drawpic_picpath;
284 #define drawpic_aspect(pos,pic,mySize,color,theAlpha,drawflag)\
286 _drawpic_imgsize = draw_getimagesize(pic);\
287 if(_drawpic_imgsize != '0 0 0') {\
288 _drawpic_imgaspect = _drawpic_imgsize.x/_drawpic_imgsize.y;\
289 _drawpic_sz = mySize;\
290 if(_drawpic_sz.x/_drawpic_sz.y > _drawpic_imgaspect) {\
291 _drawpic_oldsz = _drawpic_sz.x;\
292 _drawpic_sz_x = _drawpic_sz.y * _drawpic_imgaspect;\
294 drawpic(pos + eX * (_drawpic_oldsz - _drawpic_sz.x) * 0.5, pic, _drawpic_sz, color, theAlpha, drawflag);\
296 _drawpic_oldsz = _drawpic_sz.y;\
297 _drawpic_sz_y = _drawpic_sz.x / _drawpic_imgaspect;\
299 drawpic(pos + eY * (_drawpic_oldsz - _drawpic_sz.y) * 0.5, pic, _drawpic_sz, color, theAlpha, drawflag);\
304 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
305 #define drawpic_aspect_skin(pos,pic,sz,color,theAlpha,drawflag)\
307 _drawpic_picpath = strcat(hud_skin_path, "/", pic);\
308 if(precache_pic(_drawpic_picpath) == "") {\
309 _drawpic_picpath = strcat("gfx/hud/default/", pic);\
311 drawpic_aspect(pos, _drawpic_picpath, sz, color, theAlpha, drawflag);\
312 _drawpic_picpath = string_null;\
315 // draw HUD element with image from gfx/hud/hud_skin/foo.tga if it exists, otherwise gfx/hud/default/foo.tga
316 #define drawpic_skin(pos,pic,sz,color,theAlpha,drawflag)\
318 _drawpic_picpath = strcat(hud_skin_path, "/", pic);\
319 if(precache_pic(_drawpic_picpath) == "") {\
320 _drawpic_picpath = strcat("gfx/hud/default/", pic);\
322 drawpic(pos, _drawpic_picpath, sz, color, theAlpha, drawflag);\
323 _drawpic_picpath = string_null;\
326 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
329 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
331 drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
334 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
336 drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
337 drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
339 #define SET_POS_AND_SZ_Y_ASPECT(allow_colors) do { \
340 float textaspect, oldsz; \
341 textaspect = stringwidth(text, allow_colors, '1 1 1' * sz.y) / sz.y; \
342 if(sz.x/sz.y > textaspect) { \
344 sz_x = sz.y * textaspect; \
345 pos.x += (oldsz - sz.x) * 0.5; \
348 sz_y = sz.x / textaspect; \
349 pos.y += (oldsz - sz.y) * 0.5; \
353 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
354 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
355 SET_POS_AND_SZ_Y_ASPECT(false);
356 drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
359 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
360 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
361 SET_POS_AND_SZ_Y_ASPECT(true);
362 drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
365 vector drawfontscale;
366 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
369 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
371 drawfontscale = sz * '1 1 0';
372 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
373 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);
375 // (scale_x * sz / drawfontscale_x) * drawfontscale_x * SIZE1 / (scale_x * sz)
377 drawfontscale = '1 1 0';
380 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
381 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
382 SET_POS_AND_SZ_Y_ASPECT(false);
383 drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
386 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
389 sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
391 drawfontscale = sz * '1 1 0';
392 dummyfunction(0, 0, 0, 0, 0, 0, 0, 0);
393 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);
394 drawfontscale = '1 1 0';
397 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
398 SET_POS_AND_SZ_Y_ASPECT(true);
399 drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
402 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
403 float PolyDrawModelSurface(entity e, float i_s)
409 tex = getsurfacetexture(e, i_s);
411 return 0; // this is beyond the last one
412 n_t = getsurfacenumtriangles(e, i_s);
413 for(i_t = 0; i_t < n_t; ++i_t)
415 tri = getsurfacetriangle(e, i_s, i_t);
416 R_BeginPolygon(tex, 0);
417 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
418 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
419 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
424 void PolyDrawModel(entity e)
427 for(i_s = 0; ; ++i_s)
428 if(!PolyDrawModelSurface(e, i_s))
432 void DrawCircleClippedPic(vector centre, float radius, string pic, float f, vector rgb, float a, float drawflag)
435 vector ringsize, v, t;
436 ringsize = radius * '1 1 0';
438 x = cos(f * 2 * M_PI);
439 y = sin(f * 2 * M_PI);
440 q = fabs(x) + fabs(y);
446 // draw full rectangle
447 R_BeginPolygon(pic, drawflag);
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);
456 v = centre; t = '0.5 0.5 0';
457 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
458 R_PolygonVertex(v, t, rgb, a);
460 v = centre; t = '0.5 0.5 0';
461 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
462 R_PolygonVertex(v, t, rgb, a);
468 R_BeginPolygon(pic, drawflag);
469 v = centre; t = '0.5 0.5 0';
470 R_PolygonVertex(v, t, rgb, a);
472 v = centre; t = '0.5 0.5 0';
473 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
474 R_PolygonVertex(v, t, rgb, a);
479 // draw upper and first triangle
480 R_BeginPolygon(pic, drawflag);
481 v = centre; t = '0.5 0.5 0';
482 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
483 R_PolygonVertex(v, t, rgb, a);
485 v = centre; t = '0.5 0.5 0';
486 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
487 R_PolygonVertex(v, t, rgb, a);
489 v = centre; t = '0.5 0.5 0';
490 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
491 R_PolygonVertex(v, t, rgb, a);
493 R_BeginPolygon(pic, drawflag);
494 v = centre; t = '0.5 0.5 0';
495 R_PolygonVertex(v, t, rgb, a);
497 v = centre; t = '0.5 0.5 0';
498 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
499 R_PolygonVertex(v, t, rgb, a);
501 v = centre; t = '0.5 0.5 0';
502 v.y -= 0.5 * ringsize.y; t -= '0.5 -0.5 0';
503 R_PolygonVertex(v, t, rgb, a);
511 // draw upper triangle
512 R_BeginPolygon(pic, drawflag);
513 v = centre; t = '0.5 0.5 0';
514 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
515 R_PolygonVertex(v, t, rgb, a);
517 v = centre; t = '0.5 0.5 0';
518 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
519 R_PolygonVertex(v, t, rgb, a);
521 v = centre; t = '0.5 0.5 0';
522 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
523 R_PolygonVertex(v, t, rgb, a);
529 R_BeginPolygon(pic, drawflag);
530 v = centre; t = '0.5 0.5 0';
531 R_PolygonVertex(v, t, rgb, a);
533 v = centre; t = '0.5 0.5 0';
534 v.x -= 0.5 * ringsize.x; t -= '0.5 0.5 0';
535 R_PolygonVertex(v, t, rgb, a);
540 // draw first triangle
541 R_BeginPolygon(pic, drawflag);
542 v = centre; t = '0.5 0.5 0';
543 R_PolygonVertex(v, t, rgb, a);
545 v = centre; t = '0.5 0.5 0';
546 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
547 R_PolygonVertex(v, t, rgb, a);
549 v = centre; t = '0.5 0.5 0';
550 v.y += 0.5 * ringsize.y; t += '0.5 -0.5 0';
551 R_PolygonVertex(v, t, rgb, a);
562 R_BeginPolygon(pic, drawflag);
563 v = centre; t = '0.5 0.5 0';
564 R_PolygonVertex(v, t, rgb, a);
566 v = centre; t = '0.5 0.5 0';
567 v.x += 0.5 * ringsize.x; t += '0.5 0.5 0';
568 R_PolygonVertex(v, t, rgb, a);
574 v = centre; t = '0.5 0.5 0';
575 v.x += x * 0.5 * ringsize.x; t += x * '0.5 0.5 0';
576 v.y += y * 0.5 * ringsize.y; t += y * '0.5 -0.5 0';
577 R_PolygonVertex(v, t, rgb, a);
582 const vector GETPLAYERORIGIN_ERROR = '1123581321 2357111317 3141592653'; // way out of bounds for anything on the map
583 vector getplayerorigin(int pl)
587 e = CSQCModel_server2csqc(pl + 1);
591 e = entcs_receiver[pl];
595 return GETPLAYERORIGIN_ERROR;
598 float getplayeralpha(float pl)
602 e = CSQCModel_server2csqc(pl + 1);
609 vector getcsqcplayercolor(float pl)
613 e = CSQCModel_server2csqc(pl);
617 return colormapPaletteColor(((e.colormap >= 1024) ? e.colormap : stof(getplayerkeyvalue(e.colormap - 1, "colors"))) & 0x0F, true);
623 float getplayerisdead(float pl)
627 e = CSQCModel_server2csqc(pl + 1);
629 return e.csqcmodel_isdead;
634 void URI_Get_Callback(int id, float status, string data)
636 if(url_URI_Get_Callback(id, status, data))
640 else if (id == URI_GET_DISCARD)
644 else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
647 Curl_URI_Get_Callback(id, status, data);
651 printf("Received HTTP request data for an invalid id %d.\n", id);
655 void draw_beginBoldFont()
657 drawfont = FONT_USER+2;
660 void draw_endBoldFont()
662 drawfont = FONT_USER+1;
666 const int MAX_ACCURACY_LEVELS = 10;
667 float acc_lev[MAX_ACCURACY_LEVELS];
668 vector acc_col[MAX_ACCURACY_LEVELS];
669 float acc_col_loadtime;
671 string acc_color_levels;
672 void Accuracy_LoadLevels()
674 if(autocvar_accuracy_color_levels != acc_color_levels)
677 strunzone(acc_color_levels);
678 acc_color_levels = strzone(autocvar_accuracy_color_levels);
679 acc_levels = tokenize_console(acc_color_levels);
680 if(acc_levels > MAX_ACCURACY_LEVELS)
681 acc_levels = MAX_ACCURACY_LEVELS;
683 print("Warning: accuracy_color_levels must contain at least 2 values\n");
686 for(i = 0; i < acc_levels; ++i)
687 acc_lev[i] = stof(argv(i)) / 100.0;
691 void Accuracy_LoadColors()
693 if(time > acc_col_loadtime)
697 for(i = 0; i < acc_levels; ++i)
698 acc_col[i] = stov(cvar_string(strcat("accuracy_color", ftos(i))));
699 acc_col_loadtime = time + 2;
703 vector Accuracy_GetColor(float accuracy)
708 return '0 0 0'; // return black, can't determine the right color
710 // find the max level lower than acc
711 int j = acc_levels-1;
712 while(j && accuracy < acc_lev[j])
715 // inject color j+1 in color j, how much depending on how much accuracy is higher than level j
716 factor = (accuracy - acc_lev[j]) / (acc_lev[j+1] - acc_lev[j]);
718 color = color + factor * (acc_col[j+1] - color);