1 #include "mapvoting.qh"
3 #include <client/autocvars.qh>
4 #include <client/draw.qh>
5 #include <client/hud/_mod.qh>
6 #include <client/hud/panel/scoreboard.qh>
7 #include <common/mapinfo.qh>
8 #include <common/util.qh>
12 void MapVote_Draw_Export(int fh)
14 // allow saving cvars that aesthetically change the panel into hud skin files
15 HUD_Write_Cvar("hud_panel_mapvote_highlight_border");
20 string mv_maps[MAPVOTE_COUNT];
21 string mv_pics[MAPVOTE_COUNT];
22 string mv_pk3[MAPVOTE_COUNT]; // map pk3 name or gametype human readable name
23 string mv_desc[MAPVOTE_COUNT];
24 float mv_preview[MAPVOTE_COUNT];
25 float mv_votes[MAPVOTE_COUNT];
26 float mv_flags[MAPVOTE_COUNT];
27 float mv_flags_start[MAPVOTE_COUNT];
38 int mv_mouse_selection;
39 int mv_selection_keyboard;
42 string mapvote_chosenmap;
44 vector gtv_text_size_small;
46 const int NUM_SSDIRS = 4;
47 string ssdirs[NUM_SSDIRS];
50 bool PreviewExists(string name)
52 if(autocvar_cl_readpicture_force)
55 if (fexists(strcat(name, ".tga"))) return true;
56 if (fexists(strcat(name, ".png"))) return true;
57 if (fexists(strcat(name, ".jpg"))) return true;
58 if (fexists(strcat(name, ".pcx"))) return true;
63 string MapVote_FormatMapItem(int id, string map, float _count, float maxwidth, vector fontsize)
67 pre = sprintf("%d. ", id+1);
71 post = _(" (1 vote)");
72 else if(_count >= 0 && (mv_flags[id] & GTV_AVAILABLE))
73 post = sprintf(_(" (%d votes)"), _count);
79 maxwidth -= stringwidth(pre, false, fontsize) + stringwidth(post, false, fontsize);
80 map = textShortenToWidth(map, maxwidth, fontsize, stringwidth_nocolors);
81 return strcat(pre, map, post);
84 vector MapVote_RGB(int id)
87 if(!(mv_flags[id] & GTV_AVAILABLE))
91 else if (id == mv_selection)
97 void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, float _count, int id)
100 // Find the correct alpha
102 if(!(mv_flags_start[id] & GTV_AVAILABLE))
103 alpha = 0.2; // The gametype isn't supported by the map
104 else if ( !(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
105 alpha = mv_top2_alpha; // Fade away if not one of the top 2 choice
107 alpha = 1; // Normal, full alpha
108 alpha *= panel_fg_alpha;
110 // Bounding box details
111 float rect_margin = hud_fontsize.y / 2;
113 pos.x += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
114 pos.y += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
115 maxh -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
116 tsize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
118 vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
119 vector rect_size = '1 1 0';
120 rect_size.x = tsize + rect_margin;
121 rect_size.y = maxh + rect_margin;
123 // Highlight selected item
124 if(id == mv_selection && (mv_flags[id] & GTV_AVAILABLE))
126 drawfill(rect_pos, rect_size, '1 1 1', 0.1 * panel_fg_alpha, DRAWFLAG_NORMAL);
129 // Highlight current vote
130 vector rgb = MapVote_RGB(id);
133 drawfill(rect_pos, rect_size, rgb, 0.1 * alpha, DRAWFLAG_NORMAL);
134 drawborderlines(autocvar_hud_panel_mapvote_highlight_border, rect_pos, rect_size, rgb, alpha, DRAWFLAG_NORMAL);
139 float title_gap = gtv_text_size.y * 1.4; // distance between the title and the description
143 // Evaluate the image size
144 vector image_size = '1 1 0' * gtv_text_size.x * 3;
145 if ( maxh < image_size.y )
146 image_size = '1 1 0' * maxh;
148 float desc_padding = gtv_text_size.x * 0.6;
149 pos.x += image_size.x + desc_padding;
150 tsize -= image_size.x + desc_padding;
152 // Split the description into lines
155 title.message = MapVote_FormatMapItem(id, mv_pk3[id], _count, tsize, gtv_text_size);
157 string thelabel = mv_desc[id], ts;
163 float i,n = tokenizebyseparator(thelabel, "\n");
164 for(i = 0; i < n && maxh > (nlines+1)*gtv_text_size_small.y; ++i)
166 getWrappedLine_remaining = argv(i);
167 while(getWrappedLine_remaining && maxh > (nlines+1)*gtv_text_size_small.y)
169 ts = getWrappedLine(tsize, gtv_text_size_small, stringwidth_colors);
174 next.origin = pos-offset;
177 pos.y += gtv_text_size_small.y;
184 // Center the contents in the bounding box
185 maxh -= max(nlines*gtv_text_size_small.y,image_size.y);
190 drawstring(offset, title.message, gtv_text_size, rgb, alpha, DRAWFLAG_NORMAL);
194 drawpic('0 1 0'*title_gap+'0.5 0 0'*desc_padding+offset, pic, image_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
196 // Draw the description
197 for ( last = title.chain; last ; )
199 drawstring(last.origin+offset, last.message, gtv_text_size_small, '1 1 1', alpha, DRAWFLAG_NORMAL);
209 void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float _count, int id)
212 vector img_size = '0 0 0';
216 float rect_margin = hud_fontsize.y / 2;
218 pos.x += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
219 pos.y += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
220 isize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
221 tsize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
223 vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
224 vector rect_size = '1 1 0';
225 rect_size.x = tsize + rect_margin;
226 rect_size.y = isize + rect_margin;
229 img_size.x = min(tsize, isize * img_ar);
230 img_size.y = img_size.x / img_ar;
231 img_size.y -= hud_fontsize.y;
232 img_size.x = img_size.y * img_ar;
234 pos.y += (isize - img_size.y - hud_fontsize.y) / 2;
236 label = MapVote_FormatMapItem(id, map, _count, tsize, hud_fontsize);
238 text_size = stringwidth(label, false, hud_fontsize);
240 float save_rect_sizex = rect_size.x;
241 rect_size.x = max(img_size.x, text_size) + rect_margin;
242 rect_pos.x += (save_rect_sizex - rect_size.x) / 2;
244 vector text_pos = '0 0 0';
245 text_pos.x = pos.x + (tsize - text_size) / 2;
246 text_pos.y = pos.y + img_size.y;
248 pos.x += (tsize - img_size.x) / 2;
251 if (!(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
252 theAlpha = mv_top2_alpha;
255 theAlpha *= panel_fg_alpha;
257 // Highlight selected item
258 if(id == mv_selection && (mv_flags[id] & GTV_AVAILABLE))
259 drawfill(rect_pos, rect_size, '1 1 1', 0.1 * panel_fg_alpha, DRAWFLAG_NORMAL);
261 // Highlight current vote
262 vector rgb = MapVote_RGB(id);
265 drawfill(rect_pos, rect_size, rgb, 0.1 * theAlpha, DRAWFLAG_NORMAL);
266 drawborderlines(autocvar_hud_panel_mapvote_highlight_border, rect_pos, rect_size, rgb, theAlpha, DRAWFLAG_NORMAL);
269 drawstring(text_pos, label, hud_fontsize, rgb, theAlpha, DRAWFLAG_NORMAL);
273 drawfill(pos, img_size, '.5 .5 .5', .7 * theAlpha, DRAWFLAG_NORMAL);
277 if(drawgetimagesize(pic) == '0 0 0')
278 drawpic(pos, draw_UseSkinFor("nopreview_map"), img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
280 drawpic(pos, pic, img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
284 void MapVote_DrawAbstain(vector pos, float isize, float tsize, float _count, int id)
291 rgb = MapVote_RGB(id);
293 label = MapVote_FormatMapItem(id, _("Don't care"), _count, tsize, hud_fontsize);
295 text_size = stringwidth(label, false, hud_fontsize);
297 pos.x -= text_size*0.5;
298 drawstring(pos, label, hud_fontsize, rgb, panel_fg_alpha, DRAWFLAG_NORMAL);
301 vector MapVote_GridVec(vector gridspec, int i, int m)
303 TC(int, i); TC(int, m);
306 '1 0 0' * (gridspec.x * r)
308 '0 1 0' * (gridspec.y * (i - r) / m);
311 float MapVote_Selection(vector topleft, vector cellsize, float rows, float columns)
316 mv_mouse_selection = -1;
318 for (r = 0; r < rows; ++r)
319 for (c = 0; c < columns; ++c)
321 if (mousepos.x >= topleft.x + cellsize.x * c &&
322 mousepos.x <= topleft.x + cellsize.x * (c + 1) &&
323 mousepos.y >= topleft.y + cellsize.y * r &&
324 mousepos.y <= topleft.y + cellsize.y * (r + 1))
326 mv_mouse_selection = r * columns + c;
331 if (mv_mouse_selection >= mv_num_maps)
332 mv_mouse_selection = -1;
334 if (mv_abstain && mv_mouse_selection < 0)
335 mv_mouse_selection = mv_num_maps;
337 if ( mv_selection_keyboard )
340 return mv_mouse_selection;
343 vector prev_mousepos;
344 // draws map vote or gametype vote
353 vector dist = '0 0 0';
355 //if(intermission != 2) return;
359 HUD_Panel_LoadCvars();
361 if (!autocvar_hud_cursormode)
363 if (mousepos.x != prev_mousepos.x || mousepos.y != prev_mousepos.y)
365 mv_selection_keyboard = 0;
366 prev_mousepos = mousepos;
370 center = (vid_conwidth - 1)/2;
371 xmin = vid_conwidth * 0.08;
372 xmax = vid_conwidth - xmin;
374 ymax = vid_conheight - ymin;
376 if(chat_posy + chat_sizey / 2 < vid_conheight / 2)
381 hud_fontsize = HUD_GetFontsize("hud_fontsize");
384 gtv_text_size = hud_fontsize * 1.4;
385 gtv_text_size_small = hud_fontsize * 1.1;
392 draw_beginBoldFont();
394 map = ((gametypevote) ? _("Decide the gametype") : _("Vote for a map"));
395 pos.x = center - stringwidth(map, false, hud_fontsize * 2) * 0.5;
396 drawstring(pos, map, hud_fontsize * 2, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
397 pos.y += hud_fontsize.y * 2;
399 if( mapvote_chosenmap != "" )
401 pos.y += hud_fontsize.y * 0.25;
402 pos.x = center - stringwidth(mapvote_chosenmap, false, hud_fontsize * 1.5) * 0.5;
403 drawstring(pos, mapvote_chosenmap, hud_fontsize * 1.5, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
404 pos.y += hud_fontsize.y * 1.5;
406 pos.y += hud_fontsize.y * 0.5;
410 i = ceil(max(0, mv_timeout - time));
411 map = sprintf(_("%d seconds left"), i);
412 pos.x = center - stringwidth(map, false, hud_fontsize * 1.5) * 0.5;
413 drawstring(pos, map, hud_fontsize * 1.5, '0 1 0', panel_fg_alpha, DRAWFLAG_NORMAL);
414 pos.y += hud_fontsize.y * 1.5;
415 pos.y += hud_fontsize.y * 0.5;
417 // base for multi-column stuff...
418 pos.y += hud_fontsize.y;
421 float abstain_spacing = panel_bg_border + hud_fontsize.y;
425 ymax -= abstain_spacing;
428 // higher than the image itself ratio for mapvote items to reserve space for long map names
429 int item_aspect = (gametypevote) ? 3/1 : 5/3;
430 vector table_size = HUD_GetTableSize_BestItemAR(mv_num_maps, vec2(xmax - xmin, ymax - ymin), item_aspect);
431 mv_columns = table_size.x;
434 dist.x = (xmax - xmin) / mv_columns;
435 dist.y = (ymax - pos.y) / rows;
437 // reduce size of too wide items
438 tmp = vid_conwidth / 3; // max width
442 dist.y = min(dist.y, dist.x / item_aspect);
444 tmp = vid_conheight / 3; // max height
448 dist.x = min(dist.x, dist.y * item_aspect);
451 // reduce size to fix aspect ratio
452 if(dist.x / dist.y > item_aspect)
453 dist.x = dist.y * item_aspect;
455 dist.y = dist.x / item_aspect;
457 // adjust table pos and size according to the new size
459 offset = ((xmax - pos.x) - dist.x * mv_columns) / 2;
460 xmin = pos.x += offset;
462 offset = ((ymax - pos.y) - dist.y * rows) / 2;
465 // override panel_pos and panel_size
468 panel_size.x = xmax - xmin;
469 panel_size.y = ymax - ymin;
474 // FIXME item AR gets slightly changed here...
475 // it's rather hard to avoid it at this point
476 dist.x -= 2 * panel_bg_padding / mv_columns;
477 dist.y -= 2 * panel_bg_padding / rows;
478 xmin = pos.x += panel_bg_padding;
479 ymin = pos.y += panel_bg_padding;
480 xmax -= 2 * panel_bg_padding;
481 ymax -= 2 * panel_bg_padding;
484 mv_selection = MapVote_Selection(pos, dist, rows, mv_columns);
487 mv_top2_alpha = max(0.2, 1 - (time - mv_top2_time) ** 2);
489 void (vector, float, float, string, string, float, float) DrawItem;
492 DrawItem = GameTypeVote_DrawGameTypeItem;
494 DrawItem = MapVote_DrawMapItem;
496 for(i = 0; i < mv_num_maps; ++i)
498 tmp = mv_votes[i]; // FTEQCC bug: too many array accesses in the function call screw it up
501 DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), dist.y, dist.x, map, mv_pics[i], tmp, i);
503 DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), dist.y, dist.x, map, "", tmp, i);
509 if(mv_abstain && i < mv_num_maps) {
511 pos.y = ymax + abstain_spacing;
512 pos.x = (xmax+xmin)*0.5;
513 MapVote_DrawAbstain(pos, dist.x, xmax - xmin, tmp, i);
517 void Cmd_MapVote_MapDownload(int argc)
522 if(argc != 2 || !mv_pk3list)
524 LOG_INFO(_("mv_mapdownload: ^3You're not supposed to use this command on your own!"));
528 int id = stof(argv(1));
529 for(pak = mv_pk3list; pak; pak = pak.chain)
530 if(pak.sv_entnum == id)
533 if(!pak || pak.sv_entnum != id) {
534 LOG_INFO(_("^1Error:^7 Couldn't find pak index."));
538 if(PreviewExists(pak.message))
540 mv_preview[id] = true;
543 LOG_INFO(_("Requesting preview..."));
544 localcmd(strcat("\ncmd mv_getpicture ", ftos(id), "\n"));
548 void MapVote_CheckPK3(string pic, string pk3, int id)
557 pak.chain = mv_pk3list;
562 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
566 Cmd_MapVote_MapDownload(tokenize_console(strcat("mv_download ", ftos(id))));
570 void MapVote_CheckPic(string pic, string pk3, int id)
573 // never try to retrieve a pic for the "don't care" 'map'
574 if(mv_abstain && id == mv_num_maps - 1)
577 if(PreviewExists(pic))
579 mv_preview[id] = true;
582 MapVote_CheckPK3(pic, pk3, id);
585 void MapVote_ReadMask()
588 if ( mv_num_maps < 24 )
593 else if(mv_num_maps < 16)
598 for(i = 0; i < mv_num_maps; ++i)
601 mv_flags[i] |= GTV_AVAILABLE;
603 mv_flags[i] &= ~GTV_AVAILABLE;
608 for(i = 0; i < mv_num_maps; ++i )
609 mv_flags[i] = ReadByte();
613 void MapVote_ReadOption(int i)
616 string map = strzone(ReadString());
617 string pk3 = strzone(ReadString());
618 int j = bound(0, ReadByte(), n_ssdirs - 1);
622 mv_flags[i] = GTV_AVAILABLE;
624 string pic = strzone(strcat(ssdirs[j], "/", map));
626 mv_preview[i] = false;
627 MapVote_CheckPic(pic, pk3, i);
630 void GameTypeVote_ReadOption(int i)
633 string gt = strzone(ReadString());
636 mv_flags[i] = ReadByte();
638 string basetype = "";
640 if ( mv_flags[i] & GTV_CUSTOM )
642 string name = ReadString();
643 if ( strlen(name) < 1 )
645 mv_pk3[i] = strzone(name);
646 mv_desc[i] = strzone(ReadString());
647 basetype = strzone(ReadString());
651 Gametype type = MapInfo_Type_FromString(gt, false);
652 mv_pk3[i] = strzone(MapInfo_Type_ToText(type));
653 mv_desc[i] = MapInfo_Type_Description(type);
656 string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, gt);
657 if(precache_pic(mv_picpath) == "")
659 mv_picpath = strcat("gfx/menu/default/gametype_", gt);
660 if(precache_pic(mv_picpath) == "")
662 mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, basetype);
663 if(precache_pic(mv_picpath) == "")
665 mv_picpath = strcat("gfx/menu/default/gametype_", basetype);
669 string pic = strzone(mv_picpath);
671 mv_preview[i] = PreviewExists(pic);
677 if(!autocvar_hud_cursormode) mousepos = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
679 mv_selection_keyboard = 0;
682 for(n_ssdirs = 0; ; ++n_ssdirs)
687 if(n_ssdirs < NUM_SSDIRS)
688 ssdirs[n_ssdirs] = s;
690 n_ssdirs = min(n_ssdirs, NUM_SSDIRS);
692 mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
693 mv_abstain = ReadByte();
695 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
696 mv_detail = ReadByte();
699 mv_timeout = ReadCoord();
701 gametypevote = ReadByte();
705 mapvote_chosenmap = strzone(ReadString());
706 if ( gametypevote == 2 )
712 for(i = 0; i < mv_num_maps; ++i )
713 mv_flags_start[i] = mv_flags[i];
715 // Assume mv_pk3list is NULL, there should only be 1 mapvote per round
716 mv_pk3list = NULL; // I'm still paranoid!
718 for(i = 0; i < mv_num_maps; ++i)
723 GameTypeVote_ReadOption(i);
725 MapVote_ReadOption(i);
728 for(i = 0; i < n_ssdirs; ++i)
729 ssdirs[n_ssdirs] = string_null;
733 void MapVote_SendChoice(int index)
736 localcmd(strcat("\nimpulse ", ftos(index+1), "\n"));
739 int MapVote_MoveLeft(int pos)
744 imp = mv_num_maps - 1;
746 imp = pos < 1 ? mv_num_maps - 1 : pos - 1;
747 if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
748 imp = MapVote_MoveLeft(imp);
751 int MapVote_MoveRight(int pos)
758 imp = pos >= mv_num_maps - 1 ? 0 : pos + 1;
759 if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
760 imp = MapVote_MoveRight(imp);
763 int MapVote_MoveUp(int pos)
768 imp = mv_num_maps - 1;
771 imp = pos - mv_columns;
774 imp = floor(mv_num_maps/mv_columns)*mv_columns + pos % mv_columns;
775 if ( imp >= mv_num_maps )
779 if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
780 imp = MapVote_MoveUp(imp);
783 int MapVote_MoveDown(int pos)
791 imp = pos + mv_columns;
792 if ( imp >= mv_num_maps )
793 imp = imp % mv_columns;
795 if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
796 imp = MapVote_MoveDown(imp);
800 float MapVote_InputEvent(int bInputType, float nPrimary, float nSecondary)
804 static int first_digit = 0;
810 mousepos.x = nPrimary;
811 mousepos.y = nSecondary;
812 mv_selection_keyboard = 0;
818 if (nPrimary == K_ALT) hudShiftState |= S_ALT;
819 if (nPrimary == K_CTRL) hudShiftState |= S_CTRL;
820 if (nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
822 else if (bInputType == 1)
824 if (nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
825 if (nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
826 if (nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
828 if (nPrimary == K_CTRL)
839 mv_selection_keyboard = 1;
840 mv_selection = MapVote_MoveRight(mv_selection);
843 mv_selection_keyboard = 1;
844 mv_selection = MapVote_MoveLeft(mv_selection);
847 mv_selection_keyboard = 1;
848 mv_selection = MapVote_MoveDown(mv_selection);
851 mv_selection_keyboard = 1;
852 mv_selection = MapVote_MoveUp(mv_selection);
857 if ( mv_selection_keyboard )
858 MapVote_SendChoice(mv_selection);
860 case '1': case K_KP_1: imp = 1; break;
861 case '2': case K_KP_2: imp = 2; break;
862 case '3': case K_KP_3: imp = 3; break;
863 case '4': case K_KP_4: imp = 4; break;
864 case '5': case K_KP_5: imp = 5; break;
865 case '6': case K_KP_6: imp = 6; break;
866 case '7': case K_KP_7: imp = 7; break;
867 case '8': case K_KP_8: imp = 8; break;
868 case '9': case K_KP_9: imp = 9; break;
869 case '0': case K_KP_0: imp = 10; break;
872 if (imp && hudShiftState & S_CTRL)
876 first_digit = imp % 10;
880 imp = first_digit * 10 + (imp % 10);
883 if (nPrimary == K_MOUSE1)
885 mv_selection_keyboard = 0;
886 mv_selection = mv_mouse_selection;
887 if (mv_selection >= 0)
888 imp = min(mv_selection + 1, mv_num_maps);
893 if (imp <= mv_num_maps)
894 localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
901 void MapVote_UpdateMask()
907 void MapVote_UpdateVotes()
910 for(i = 0; i < mv_num_maps; ++i)
912 if(mv_flags[i] & GTV_AVAILABLE)
915 mv_votes[i] = ReadByte();
923 mv_ownvote = ReadByte()-1;
926 NET_HANDLE(ENT_CLIENT_MAPVOTE, bool isnew)
936 MapVote_UpdateMask();
939 MapVote_UpdateVotes();
942 NET_HANDLE(TE_CSQC_PICTURE, bool isNew)
944 Net_MapVote_Picture();
948 void Net_MapVote_Picture()
950 int type = ReadByte();
951 mv_preview[type] = true;
952 mv_pics[type] = strzone(ReadPicture());