4 string mv_maps[MAPVOTE_COUNT];
5 string mv_pics[MAPVOTE_COUNT];
6 string mv_pk3[MAPVOTE_COUNT];
7 float mv_preview[MAPVOTE_COUNT];
8 float mv_votes[MAPVOTE_COUNT];
9 float mv_avail[MAPVOTE_COUNT];
10 float mv_avail_start[MAPVOTE_COUNT];
22 int mv_mouse_selection;
23 int mv_selection_keyboard;
26 string mapvote_chosenmap;
28 vector gtv_text_size_small;
30 string MapVote_FormatMapItem(int id, string map, float count, float maxwidth, vector fontsize)
33 pre = sprintf("%d. ", id+1);
37 post = _(" (1 vote)");
38 else if(count >= 0 && mv_avail[id] == GTV_AVAILABLE)
39 post = sprintf(_(" (%d votes)"), count);
45 maxwidth -= stringwidth(pre, false, fontsize) + stringwidth(post, false, fontsize);
46 map = textShortenToWidth(map, maxwidth, fontsize, stringwidth_nocolors);
47 return strcat(pre, map, post);
50 string GameTypeVote_DescriptionByID(float id)
52 return MapInfo_Type_Description(MapInfo_Type_FromString(mv_maps[id]));
55 vector MapVote_RGB(int id)
57 if(mv_avail[id] != GTV_AVAILABLE)
61 else if (id == mv_selection)
67 void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, float count, int id)
70 float desc_padding = gtv_text_size.x * 3;
71 float rect_margin = hud_fontsize.y / 2;
72 vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
73 vector rect_size = '1 1 0';
74 rect_size_x = tsize + rect_margin;
75 rect_size_y = maxh + rect_margin;
76 vector rgb = MapVote_RGB(id);
80 if(mv_avail_start[id] != GTV_AVAILABLE)
82 else if ( mv_avail[id] != GTV_AVAILABLE && mv_top2_alpha)
83 alpha = mv_top2_alpha;
87 if(id == mv_selection && mv_avail[id] == GTV_AVAILABLE)
89 drawfill(rect_pos, rect_size, '1 1 1', 0.1, DRAWFLAG_NORMAL);
93 drawfill(rect_pos, rect_size, rgb, 0.1*alpha, DRAWFLAG_NORMAL);
94 drawborderlines(autocvar_scoreboard_border_thickness, rect_pos, rect_size, rgb, alpha, DRAWFLAG_NORMAL);
99 title.message = MapVote_FormatMapItem(id, MapInfo_Type_ToText(MapInfo_Type_FromString(gtype)),
100 count, tsize, gtv_text_size);
101 title.origin = pos-offset;
103 pos.y += gtv_text_size_small.y;
104 pos.y += gtv_text_size.y/2;
106 maxh -= gtv_text_size.y;
108 entity picent = spawn();
109 picent.origin = pos-offset;
110 picent.maxs = '1 1 0 ' * min(maxh, desc_padding) * 0.8;
112 pos.x += desc_padding;
113 tsize -= desc_padding;
115 string thelabel = GameTypeVote_DescriptionByID(id), ts;
120 float i,n = tokenizebyseparator(thelabel, "\n");
121 for(i = 0; i < n && maxh > (nlines+1)*gtv_text_size_small.y; ++i)
123 getWrappedLine_remaining = argv(i);
124 while(getWrappedLine_remaining && maxh > (nlines+1)*gtv_text_size_small.y)
126 ts = getWrappedLine(tsize, gtv_text_size_small, stringwidth_colors);
131 next.origin = pos-offset;
134 pos.y += gtv_text_size_small.y;
141 maxh -= max(nlines*gtv_text_size_small.y,picent.maxs.y);
144 drawstring(title.origin+offset, title.message, gtv_text_size, rgb, alpha, DRAWFLAG_NORMAL);
147 drawpic(picent.origin+offset, pic, picent.maxs, '1 1 1', alpha, DRAWFLAG_NORMAL);
149 for ( last = title.chain; last ; )
151 drawstring(last.origin+offset, last.message, gtv_text_size_small, '1 1 1', alpha, DRAWFLAG_NORMAL);
161 void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float count, int id)
163 vector img_size = '0 0 0';
168 isize -= hud_fontsize.y; // respect the text when calculating the image size
170 rgb = MapVote_RGB(id);
173 img_size_x = isize / 0.75; // 4:3 x can be stretched easily, height is defined in isize
175 pos_y = pos.y + img_size.y;
177 label = MapVote_FormatMapItem(id, map, count, tsize, hud_fontsize);
179 text_size = stringwidth(label, false, hud_fontsize);
182 if (mv_avail[id] != GTV_AVAILABLE && mv_top2_alpha)
183 theAlpha = mv_top2_alpha;
187 pos.x -= text_size*0.5;
188 drawstring(pos, label, hud_fontsize, rgb, theAlpha, DRAWFLAG_NORMAL);
190 pos_x = pos.x + text_size*0.5 - img_size.x*0.5;
191 pos_y = pos.y - img_size.y;
193 pos += autocvar_scoreboard_border_thickness * '1 1 0';
194 img_size -= (autocvar_scoreboard_border_thickness * 2) * '1 1 0';
197 drawfill(pos, img_size, '.5 .5 .5', .7 * theAlpha, DRAWFLAG_NORMAL);
201 if(drawgetimagesize(pic) == '0 0 0')
202 drawpic(pos, draw_UseSkinFor("nopreview_map"), img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
204 drawpic(pos, pic, img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
208 drawborderlines(autocvar_scoreboard_border_thickness, pos, img_size, rgb, theAlpha, DRAWFLAG_NORMAL);
210 drawborderlines(autocvar_scoreboard_border_thickness, pos, img_size, '0 0 0', theAlpha, DRAWFLAG_NORMAL);
212 if(id == mv_selection && mv_avail[id] == GTV_AVAILABLE)
213 drawfill(pos, img_size, '1 1 1', 0.1, DRAWFLAG_NORMAL);
216 void MapVote_DrawAbstain(vector pos, float isize, float tsize, float count, int id)
222 rgb = MapVote_RGB(id);
224 pos_y = pos.y + hud_fontsize.y;
226 label = MapVote_FormatMapItem(id, _("Don't care"), count, tsize, hud_fontsize);
228 text_size = stringwidth(label, false, hud_fontsize);
230 pos.x -= text_size*0.5;
231 drawstring(pos, label, hud_fontsize, rgb, 1, DRAWFLAG_NORMAL);
234 vector MapVote_GridVec(vector gridspec, int i, int m)
238 '1 0 0' * (gridspec.x * r)
240 '0 1 0' * (gridspec.y * (i - r) / m);
243 float MapVote_Selection(vector topleft, vector cellsize, float rows, float columns)
248 mv_mouse_selection = -1;
250 for (r = 0; r < rows; ++r)
251 for (c = 0; c < columns; ++c)
253 if (mv_mousepos.x >= topleft.x + cellsize.x * c &&
254 mv_mousepos.x <= topleft.x + cellsize.x * (c + 1) &&
255 mv_mousepos.y >= topleft.y + cellsize.y * r &&
256 mv_mousepos.y <= topleft.y + cellsize.y * (r + 1))
258 mv_mouse_selection = r * columns + c;
263 if (mv_mouse_selection >= mv_num_maps)
264 mv_mouse_selection = -1;
266 if (mv_abstain && mv_mouse_selection < 0)
267 mv_mouse_selection = mv_num_maps;
269 if ( mv_selection_keyboard )
272 return mv_mouse_selection;
285 vector dist = '0 0 0';
290 if (!autocvar_hud_cursormode)
292 vector mpos = mv_mousepos + getmousepos();
293 mpos_x = bound(0, mpos.x, vid_conwidth);
294 mpos_y = bound(0, mpos.y, vid_conheight);
296 if ( mpos.x != mv_mousepos.x || mpos.y != mv_mousepos.y )
297 mv_selection_keyboard = 0;
302 center = (vid_conwidth - 1)/2;
303 xmin = vid_conwidth*0.05; // 5% border must suffice
304 xmax = vid_conwidth - xmin;
306 i = autocvar_con_chatpos; //*autocvar_con_chatsize;
308 ymax = vid_conheight + (i - autocvar_con_chat) * autocvar_con_chatsize;
309 if(i >= 0 || ymax < (vid_conheight*0.5))
310 ymax = vid_conheight - ymin;
312 hud_fontsize = HUD_GetFontsize("hud_fontsize");
317 draw_beginBoldFont();
318 map = ((gametypevote) ? _("Decide the gametype") : _("Vote for a map"));
319 pos_x = center - stringwidth(map, false, '12 0 0');
320 drawstring(pos, map, '24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
323 if( mapvote_chosenmap != "" )
325 pos_x = center - stringwidth(mapvote_chosenmap, false, hud_fontsize*1.5/2);
326 drawstring(pos, mapvote_chosenmap, hud_fontsize*1.5, '1 1 1', 1, DRAWFLAG_NORMAL);
327 pos.y += hud_fontsize.y*2;
330 i = ceil(max(0, mv_timeout - time));
331 map = sprintf(_("%d seconds left"), i);
332 pos_x = center - stringwidth(map, false, '8 0 0');
333 drawstring(pos, map, '16 16 0', '0 1 0', 1, DRAWFLAG_NORMAL);
338 // base for multi-column stuff...
343 rows = ceil(mv_num_maps / mv_columns);
345 dist_x = (xmax - xmin) / mv_columns;
346 dist_y = (ymax - pos.y) / rows;
350 tsize = dist.x - hud_fontsize.y;
352 float maxheight = (ymax - pos.y) / 3;
353 if ( isize > maxheight )
355 pos.x += (isize - maxheight)/2;
359 dist.y += hud_fontsize.y;
360 pos_x = ( vid_conwidth - dist.x * mv_columns ) / 2;
365 isize = min(dist.y - 10, 0.75 * tsize);
368 mv_selection = MapVote_Selection(pos, dist, rows, mv_columns);
372 pos.y += (dist.y - isize) / 2;
376 mv_top2_alpha = max(0.2, 1 - (time - mv_top2_time)*(time - mv_top2_time));
378 void (vector, float, float, string, string, float, float) DrawItem;
381 DrawItem = GameTypeVote_DrawGameTypeItem;
383 DrawItem = MapVote_DrawMapItem;
385 for(i = 0; i < mv_num_maps; ++i)
387 tmp = mv_votes[i]; // FTEQCC bug: too many array accesses in the function call screw it up
390 DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), isize, tsize, map, mv_pics[i], tmp, i);
392 DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), isize, tsize, map, "", tmp, i);
398 if(mv_abstain && i < mv_num_maps) {
400 pos_y = ymax + isize - hud_fontsize.y;
401 pos_x = (xmax+xmin)*0.5;
402 MapVote_DrawAbstain(pos, isize, xmax - xmin, tmp, i);
405 drawpic(mv_mousepos, strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"), '32 32 0', '1 1 1', 1 - autocvar__menu_alpha, DRAWFLAG_NORMAL);
408 void Cmd_MapVote_MapDownload(float argc)
413 if(argc != 2 || !mv_pk3list)
415 print(_("mv_mapdownload: ^3You're not supposed to use this command on your own!\n"));
420 for(pak = mv_pk3list; pak; pak = pak.chain)
421 if(pak.sv_entnum == id)
424 if(!pak || pak.sv_entnum != id) {
425 print(_("^1Error:^7 Couldn't find pak index.\n"));
429 if(PreviewExists(pak.message))
431 mv_preview[id] = true;
434 print(_("Requesting preview...\n"));
435 localcmd(strcat("\ncmd mv_getpicture ", ftos(id), "\n"));
439 void MapVote_CheckPK3(string pic, string pk3, int id)
447 pak.chain = mv_pk3list;
452 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
456 Cmd_MapVote_MapDownload(tokenize_console(strcat("mv_download ", ftos(id))));
460 void MapVote_CheckPic(string pic, string pk3, int id)
462 // never try to retrieve a pic for the "don't care" 'map'
463 if(mv_abstain && id == mv_num_maps - 1)
466 if(PreviewExists(pic))
468 mv_preview[id] = true;
471 MapVote_CheckPK3(pic, pk3, id);
474 void MapVote_ReadMask()
477 if ( mv_num_maps < 24 )
482 else if(mv_num_maps < 16)
487 for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
488 mv_avail[i] = (mask & power) ? GTV_AVAILABLE : GTV_FORBIDDEN;
492 for(i = 0; i < mv_num_maps; ++i )
493 mv_avail[i] = ReadByte();
497 const float NUM_SSDIRS = 4;
498 string ssdirs[NUM_SSDIRS];
505 precache_sound ("misc/invshot.wav");
508 if(autocvar_hud_cursormode) { setcursormode(1); }
509 else { mv_mousepos = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight; }
511 mv_selection_keyboard = 0;
513 for(n_ssdirs = 0; ; ++n_ssdirs)
518 if(n_ssdirs < NUM_SSDIRS)
519 ssdirs[n_ssdirs] = s;
521 n_ssdirs = min(n_ssdirs, NUM_SSDIRS);
523 mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
524 mv_abstain = ReadByte();
526 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
527 mv_detail = ReadByte();
530 mv_timeout = ReadCoord();
532 gametypevote = ReadByte();
534 float mv_real_num_maps = mv_num_maps - mv_abstain;
538 mapvote_chosenmap = strzone(ReadString());
539 if ( gametypevote == 2 )
542 gtv_text_size = hud_fontsize*1.4;
543 gtv_text_size_small = hud_fontsize*1.1;
545 if (mv_real_num_maps > 8 )
548 mv_columns = min(2, mv_real_num_maps);
552 if (mv_real_num_maps > 16)
554 else if (mv_real_num_maps > 9)
556 else if(mv_real_num_maps > 3)
559 mv_columns = mv_real_num_maps;
563 for(i = 0; i < mv_num_maps; ++i )
564 mv_avail_start[i] = mv_avail[i];
566 // Assume mv_pk3list is world, there should only be 1 mapvote per round
567 mv_pk3list = world; // I'm still paranoid!
569 for(i = 0; i < mv_num_maps; ++i)
573 map = strzone(ReadString());
574 pk3 = strzone(ReadString());
575 j = bound(0, ReadByte(), n_ssdirs - 1);
579 mv_avail[i] = ReadByte();
583 //map = strzone(strcat("gfx/menu/default/gametype_", map));
584 //map = strzone(sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, map));
585 string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, map);
586 if(precache_pic(mv_picpath) == "")
587 mv_picpath = strcat("gfx/menu/default/gametype_", map);
588 map = strzone(mv_picpath);
590 mv_preview[i] = PreviewExists(map);
594 map = strzone(strcat(ssdirs[j], "/", map));
596 mv_preview[i] = false;
597 MapVote_CheckPic(map, pk3, i);
601 for(i = 0; i < n_ssdirs; ++i)
602 ssdirs[n_ssdirs] = string_null;
606 void MapVote_SendChoice(float index)
608 localcmd(strcat("\nimpulse ", ftos(index+1), "\n"));
611 int MapVote_MoveLeft(int pos)
615 imp = mv_num_maps - 1;
617 imp = pos < 1 ? mv_num_maps - 1 : pos - 1;
618 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
619 imp = MapVote_MoveLeft(imp);
622 int MapVote_MoveRight(int pos)
628 imp = pos >= mv_num_maps - 1 ? 0 : pos + 1;
629 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
630 imp = MapVote_MoveRight(imp);
633 int MapVote_MoveUp(int pos)
637 imp = mv_num_maps - 1;
640 imp = pos - mv_columns;
643 imp = floor(mv_num_maps/mv_columns)*mv_columns + pos % mv_columns;
644 if ( imp >= mv_num_maps )
648 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
649 imp = MapVote_MoveUp(imp);
652 int MapVote_MoveDown(int pos)
659 imp = pos + mv_columns;
660 if ( imp >= mv_num_maps )
661 imp = imp % mv_columns;
663 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
664 imp = MapVote_MoveDown(imp);
668 float MapVote_InputEvent(float bInputType, float nPrimary, float nSecondary)
677 mv_mousepos_x = nPrimary;
678 mv_mousepos_y = nSecondary;
679 mv_selection_keyboard = 0;
686 if ('0' <= nPrimary && nPrimary <= '9')
688 imp = nPrimary - '0';
689 if (imp == 0) imp = 10;
690 localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
695 case K_KP_1: localcmd("\nimpulse 1\n"); return true;
696 case K_KP_2: localcmd("\nimpulse 2\n"); return true;
697 case K_KP_3: localcmd("\nimpulse 3\n"); return true;
698 case K_KP_4: localcmd("\nimpulse 4\n"); return true;
699 case K_KP_5: localcmd("\nimpulse 5\n"); return true;
700 case K_KP_6: localcmd("\nimpulse 6\n"); return true;
701 case K_KP_7: localcmd("\nimpulse 7\n"); return true;
702 case K_KP_8: localcmd("\nimpulse 8\n"); return true;
703 case K_KP_9: localcmd("\nimpulse 9\n"); return true;
704 case K_KP_0: localcmd("\nimpulse 10\n"); return true;
707 mv_selection_keyboard = 1;
708 mv_selection = MapVote_MoveRight(mv_selection);
711 mv_selection_keyboard = 1;
712 mv_selection = MapVote_MoveLeft(mv_selection);
715 mv_selection_keyboard = 1;
716 mv_selection = MapVote_MoveDown(mv_selection);
719 mv_selection_keyboard = 1;
720 mv_selection = MapVote_MoveUp(mv_selection);
725 if ( mv_selection_keyboard )
726 MapVote_SendChoice(mv_selection);
730 if (nPrimary == K_MOUSE1)
732 mv_selection_keyboard = 0;
733 mv_selection = mv_mouse_selection;
734 if (mv_selection >= 0)
736 imp = min(mv_selection + 1, mv_num_maps);
737 localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
745 void MapVote_UpdateMask()
751 void MapVote_UpdateVotes()
754 for(i = 0; i < mv_num_maps; ++i)
756 if(mv_avail[i] == GTV_AVAILABLE)
759 mv_votes[i] = ReadByte();
767 mv_ownvote = ReadByte()-1;
778 MapVote_UpdateMask();
781 MapVote_UpdateVotes();
784 void Net_MapVote_Picture()
786 int type = ReadByte();
787 mv_preview[type] = true;
788 mv_pics[type] = strzone(ReadPicture());