1 #include "mapvoting.qh"
5 #include "scoreboard.qh"
7 #include "../common/mapinfo.qh"
8 #include "../common/util.qh"
10 #include "../dpdefs/keycodes.qh"
16 string mv_maps[MAPVOTE_COUNT];
17 string mv_pics[MAPVOTE_COUNT];
18 string mv_pk3[MAPVOTE_COUNT];
19 float mv_preview[MAPVOTE_COUNT];
20 float mv_votes[MAPVOTE_COUNT];
21 float mv_avail[MAPVOTE_COUNT];
22 float mv_avail_start[MAPVOTE_COUNT];
34 int mv_mouse_selection;
35 int mv_selection_keyboard;
38 string mapvote_chosenmap;
40 vector gtv_text_size_small;
42 const int NUM_SSDIRS = 4;
43 string ssdirs[NUM_SSDIRS];
46 string MapVote_FormatMapItem(int id, string map, float _count, float maxwidth, vector fontsize)
49 pre = sprintf("%d. ", id+1);
53 post = _(" (1 vote)");
54 else if(_count >= 0 && mv_avail[id] == GTV_AVAILABLE)
55 post = sprintf(_(" (%d votes)"), _count);
61 maxwidth -= stringwidth(pre, false, fontsize) + stringwidth(post, false, fontsize);
62 map = textShortenToWidth(map, maxwidth, fontsize, stringwidth_nocolors);
63 return strcat(pre, map, post);
66 string GameTypeVote_DescriptionByID(int id)
68 return MapInfo_Type_Description(MapInfo_Type_FromString(mv_maps[id]));
71 vector MapVote_RGB(int id)
73 if(mv_avail[id] != GTV_AVAILABLE)
77 else if (id == mv_selection)
83 void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, float _count, int id)
86 float desc_padding = gtv_text_size.x * 3;
87 float rect_margin = hud_fontsize.y / 2;
88 vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
89 vector rect_size = '1 1 0';
90 rect_size.x = tsize + rect_margin;
91 rect_size.y = maxh + rect_margin;
92 vector rgb = MapVote_RGB(id);
96 if(mv_avail_start[id] != GTV_AVAILABLE)
98 else if ( mv_avail[id] != GTV_AVAILABLE && mv_top2_alpha)
99 alpha = mv_top2_alpha;
103 if(id == mv_selection && mv_avail[id] == GTV_AVAILABLE)
105 drawfill(rect_pos, rect_size, '1 1 1', 0.1, DRAWFLAG_NORMAL);
109 drawfill(rect_pos, rect_size, rgb, 0.1*alpha, DRAWFLAG_NORMAL);
110 drawborderlines(autocvar_scoreboard_border_thickness, rect_pos, rect_size, rgb, alpha, DRAWFLAG_NORMAL);
115 title.message = MapVote_FormatMapItem(id, MapInfo_Type_ToText(MapInfo_Type_FromString(gtype)),
116 _count, tsize, gtv_text_size);
117 title.origin = pos-offset;
119 pos.y += gtv_text_size_small.y;
120 pos.y += gtv_text_size.y/2;
122 maxh -= gtv_text_size.y;
124 entity picent = spawn();
125 picent.origin = pos-offset;
126 picent.maxs = '1 1 0 ' * min(maxh, desc_padding) * 0.8;
128 pos.x += desc_padding;
129 tsize -= desc_padding;
131 string thelabel = GameTypeVote_DescriptionByID(id), ts;
136 float i,n = tokenizebyseparator(thelabel, "\n");
137 for(i = 0; i < n && maxh > (nlines+1)*gtv_text_size_small.y; ++i)
139 getWrappedLine_remaining = argv(i);
140 while(getWrappedLine_remaining && maxh > (nlines+1)*gtv_text_size_small.y)
142 ts = getWrappedLine(tsize, gtv_text_size_small, stringwidth_colors);
147 next.origin = pos-offset;
150 pos.y += gtv_text_size_small.y;
157 maxh -= max(nlines*gtv_text_size_small.y,picent.maxs.y);
160 drawstring(title.origin+offset, title.message, gtv_text_size, rgb, alpha, DRAWFLAG_NORMAL);
163 drawpic(picent.origin+offset, pic, picent.maxs, '1 1 1', alpha, DRAWFLAG_NORMAL);
165 for ( last = title.chain; last ; )
167 drawstring(last.origin+offset, last.message, gtv_text_size_small, '1 1 1', alpha, DRAWFLAG_NORMAL);
177 void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float _count, int id)
179 vector img_size = '0 0 0';
184 isize -= hud_fontsize.y; // respect the text when calculating the image size
186 rgb = MapVote_RGB(id);
189 img_size.x = isize / 0.75; // 4:3 x can be stretched easily, height is defined in isize
191 pos.y = pos.y + img_size.y;
193 label = MapVote_FormatMapItem(id, map, _count, tsize, hud_fontsize);
195 text_size = stringwidth(label, false, hud_fontsize);
198 if (mv_avail[id] != GTV_AVAILABLE && mv_top2_alpha)
199 theAlpha = mv_top2_alpha;
203 pos.x -= text_size*0.5;
204 drawstring(pos, label, hud_fontsize, rgb, theAlpha, DRAWFLAG_NORMAL);
206 pos.x = pos.x + text_size*0.5 - img_size.x*0.5;
207 pos.y = pos.y - img_size.y;
209 pos += autocvar_scoreboard_border_thickness * '1 1 0';
210 img_size -= (autocvar_scoreboard_border_thickness * 2) * '1 1 0';
213 drawfill(pos, img_size, '.5 .5 .5', .7 * theAlpha, DRAWFLAG_NORMAL);
217 if(drawgetimagesize(pic) == '0 0 0')
218 drawpic(pos, draw_UseSkinFor("nopreview_map"), img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
220 drawpic(pos, pic, img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
224 drawborderlines(autocvar_scoreboard_border_thickness, pos, img_size, rgb, theAlpha, DRAWFLAG_NORMAL);
226 drawborderlines(autocvar_scoreboard_border_thickness, pos, img_size, '0 0 0', theAlpha, DRAWFLAG_NORMAL);
228 if(id == mv_selection && mv_avail[id] == GTV_AVAILABLE)
229 drawfill(pos, img_size, '1 1 1', 0.1, DRAWFLAG_NORMAL);
232 void MapVote_DrawAbstain(vector pos, float isize, float tsize, float _count, int id)
238 rgb = MapVote_RGB(id);
240 pos.y = pos.y + hud_fontsize.y;
242 label = MapVote_FormatMapItem(id, _("Don't care"), _count, tsize, hud_fontsize);
244 text_size = stringwidth(label, false, hud_fontsize);
246 pos.x -= text_size*0.5;
247 drawstring(pos, label, hud_fontsize, rgb, 1, DRAWFLAG_NORMAL);
250 vector MapVote_GridVec(vector gridspec, int i, int m)
254 '1 0 0' * (gridspec.x * r)
256 '0 1 0' * (gridspec.y * (i - r) / m);
259 float MapVote_Selection(vector topleft, vector cellsize, float rows, float columns)
264 mv_mouse_selection = -1;
266 for (r = 0; r < rows; ++r)
267 for (c = 0; c < columns; ++c)
269 if (mv_mousepos.x >= topleft.x + cellsize.x * c &&
270 mv_mousepos.x <= topleft.x + cellsize.x * (c + 1) &&
271 mv_mousepos.y >= topleft.y + cellsize.y * r &&
272 mv_mousepos.y <= topleft.y + cellsize.y * (r + 1))
274 mv_mouse_selection = r * columns + c;
279 if (mv_mouse_selection >= mv_num_maps)
280 mv_mouse_selection = -1;
282 if (mv_abstain && mv_mouse_selection < 0)
283 mv_mouse_selection = mv_num_maps;
285 if ( mv_selection_keyboard )
288 return mv_mouse_selection;
301 vector dist = '0 0 0';
306 if (!autocvar_hud_cursormode)
308 vector mpos = mv_mousepos + getmousepos();
309 mpos.x = bound(0, mpos.x, vid_conwidth);
310 mpos.y = bound(0, mpos.y, vid_conheight);
312 if ( mpos.x != mv_mousepos.x || mpos.y != mv_mousepos.y )
313 mv_selection_keyboard = 0;
318 center = (vid_conwidth - 1)/2;
319 xmin = vid_conwidth*0.05; // 5% border must suffice
320 xmax = vid_conwidth - xmin;
322 i = autocvar_con_chatpos; //*autocvar_con_chatsize;
324 ymax = vid_conheight + (i - autocvar_con_chat) * autocvar_con_chatsize;
325 if(i >= 0 || ymax < (vid_conheight*0.5))
326 ymax = vid_conheight - ymin;
328 hud_fontsize = HUD_GetFontsize("hud_fontsize");
333 draw_beginBoldFont();
334 map = ((gametypevote) ? _("Decide the gametype") : _("Vote for a map"));
335 pos.x = center - stringwidth(map, false, '12 0 0');
336 drawstring(pos, map, '24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
339 if( mapvote_chosenmap != "" )
341 pos.x = center - stringwidth(mapvote_chosenmap, false, hud_fontsize*1.5/2);
342 drawstring(pos, mapvote_chosenmap, hud_fontsize*1.5, '1 1 1', 1, DRAWFLAG_NORMAL);
343 pos.y += hud_fontsize.y*2;
346 i = ceil(max(0, mv_timeout - time));
347 map = sprintf(_("%d seconds left"), i);
348 pos.x = center - stringwidth(map, false, '8 0 0');
349 drawstring(pos, map, '16 16 0', '0 1 0', 1, DRAWFLAG_NORMAL);
354 // base for multi-column stuff...
359 rows = ceil(mv_num_maps / mv_columns);
361 dist.x = (xmax - xmin) / mv_columns;
362 dist.y = (ymax - pos.y) / rows;
366 tsize = dist.x - hud_fontsize.y;
368 float maxheight = (ymax - pos.y) / 3;
369 if ( isize > maxheight )
371 pos.x += (isize - maxheight)/2;
375 dist.y += hud_fontsize.y;
376 pos.x = ( vid_conwidth - dist.x * mv_columns ) / 2;
381 isize = min(dist.y - 10, 0.75 * tsize);
384 mv_selection = MapVote_Selection(pos, dist, rows, mv_columns);
388 pos.y += (dist.y - isize) / 2;
392 mv_top2_alpha = max(0.2, 1 - (time - mv_top2_time)*(time - mv_top2_time));
394 void (vector, float, float, string, string, float, float) DrawItem;
397 DrawItem = GameTypeVote_DrawGameTypeItem;
399 DrawItem = MapVote_DrawMapItem;
401 for(i = 0; i < mv_num_maps; ++i)
403 tmp = mv_votes[i]; // FTEQCC bug: too many array accesses in the function call screw it up
406 DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), isize, tsize, map, mv_pics[i], tmp, i);
408 DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), isize, tsize, map, "", tmp, i);
414 if(mv_abstain && i < mv_num_maps) {
416 pos.y = ymax + isize - hud_fontsize.y;
417 pos.x = (xmax+xmin)*0.5;
418 MapVote_DrawAbstain(pos, isize, xmax - xmin, tmp, i);
421 drawpic(mv_mousepos, strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"), '32 32 0', '1 1 1', 1 - autocvar__menu_alpha, DRAWFLAG_NORMAL);
424 void Cmd_MapVote_MapDownload(float argc)
428 if(argc != 2 || !mv_pk3list)
430 print(_("mv_mapdownload: ^3You're not supposed to use this command on your own!\n"));
434 int id = stof(argv(1));
435 for(pak = mv_pk3list; pak; pak = pak.chain)
436 if(pak.sv_entnum == id)
439 if(!pak || pak.sv_entnum != id) {
440 print(_("^1Error:^7 Couldn't find pak index.\n"));
444 if(PreviewExists(pak.message))
446 mv_preview[id] = true;
449 print(_("Requesting preview...\n"));
450 localcmd(strcat("\ncmd mv_getpicture ", ftos(id), "\n"));
454 void MapVote_CheckPK3(string pic, string pk3, int id)
462 pak.chain = mv_pk3list;
467 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
471 Cmd_MapVote_MapDownload(tokenize_console(strcat("mv_download ", ftos(id))));
475 void MapVote_CheckPic(string pic, string pk3, int id)
477 // never try to retrieve a pic for the "don't care" 'map'
478 if(mv_abstain && id == mv_num_maps - 1)
481 if(PreviewExists(pic))
483 mv_preview[id] = true;
486 MapVote_CheckPK3(pic, pk3, id);
489 void MapVote_ReadMask()
492 if ( mv_num_maps < 24 )
497 else if(mv_num_maps < 16)
502 for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
503 mv_avail[i] = (mask & power) ? GTV_AVAILABLE : GTV_FORBIDDEN;
507 for(i = 0; i < mv_num_maps; ++i )
508 mv_avail[i] = ReadByte();
517 precache_sound ("misc/invshot.wav");
520 if(autocvar_hud_cursormode) { setcursormode(1); }
521 else { mv_mousepos = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight; }
523 mv_selection_keyboard = 0;
525 for(n_ssdirs = 0; ; ++n_ssdirs)
530 if(n_ssdirs < NUM_SSDIRS)
531 ssdirs[n_ssdirs] = s;
533 n_ssdirs = min(n_ssdirs, NUM_SSDIRS);
535 mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
536 mv_abstain = ReadByte();
538 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
539 mv_detail = ReadByte();
542 mv_timeout = ReadCoord();
544 gametypevote = ReadByte();
546 float mv_real_num_maps = mv_num_maps - mv_abstain;
550 mapvote_chosenmap = strzone(ReadString());
551 if ( gametypevote == 2 )
554 gtv_text_size = hud_fontsize*1.4;
555 gtv_text_size_small = hud_fontsize*1.1;
557 if (mv_real_num_maps > 8 )
560 mv_columns = min(2, mv_real_num_maps);
564 if (mv_real_num_maps > 16)
566 else if (mv_real_num_maps > 9)
568 else if(mv_real_num_maps > 3)
571 mv_columns = mv_real_num_maps;
575 for(i = 0; i < mv_num_maps; ++i )
576 mv_avail_start[i] = mv_avail[i];
578 // Assume mv_pk3list is world, there should only be 1 mapvote per round
579 mv_pk3list = world; // I'm still paranoid!
581 for(i = 0; i < mv_num_maps; ++i)
585 map = strzone(ReadString());
586 pk3 = strzone(ReadString());
587 j = bound(0, ReadByte(), n_ssdirs - 1);
591 mv_avail[i] = ReadByte();
595 //map = strzone(strcat("gfx/menu/default/gametype_", map));
596 //map = strzone(sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, map));
597 string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, map);
598 if(precache_pic(mv_picpath) == "")
599 mv_picpath = strcat("gfx/menu/default/gametype_", map);
600 map = strzone(mv_picpath);
602 mv_preview[i] = PreviewExists(map);
606 map = strzone(strcat(ssdirs[j], "/", map));
608 mv_preview[i] = false;
609 MapVote_CheckPic(map, pk3, i);
613 for(i = 0; i < n_ssdirs; ++i)
614 ssdirs[n_ssdirs] = string_null;
618 void MapVote_SendChoice(float index)
620 localcmd(strcat("\nimpulse ", ftos(index+1), "\n"));
623 int MapVote_MoveLeft(int pos)
627 imp = mv_num_maps - 1;
629 imp = pos < 1 ? mv_num_maps - 1 : pos - 1;
630 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
631 imp = MapVote_MoveLeft(imp);
634 int MapVote_MoveRight(int pos)
640 imp = pos >= mv_num_maps - 1 ? 0 : pos + 1;
641 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
642 imp = MapVote_MoveRight(imp);
645 int MapVote_MoveUp(int pos)
649 imp = mv_num_maps - 1;
652 imp = pos - mv_columns;
655 imp = floor(mv_num_maps/mv_columns)*mv_columns + pos % mv_columns;
656 if ( imp >= mv_num_maps )
660 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
661 imp = MapVote_MoveUp(imp);
664 int MapVote_MoveDown(int pos)
671 imp = pos + mv_columns;
672 if ( imp >= mv_num_maps )
673 imp = imp % mv_columns;
675 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
676 imp = MapVote_MoveDown(imp);
680 float MapVote_InputEvent(float bInputType, float nPrimary, float nSecondary)
689 mv_mousepos.x = nPrimary;
690 mv_mousepos.y = nSecondary;
691 mv_selection_keyboard = 0;
698 if ('0' <= nPrimary && nPrimary <= '9')
700 imp = nPrimary - '0';
701 if (imp == 0) imp = 10;
702 localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
707 case K_KP_1: localcmd("\nimpulse 1\n"); return true;
708 case K_KP_2: localcmd("\nimpulse 2\n"); return true;
709 case K_KP_3: localcmd("\nimpulse 3\n"); return true;
710 case K_KP_4: localcmd("\nimpulse 4\n"); return true;
711 case K_KP_5: localcmd("\nimpulse 5\n"); return true;
712 case K_KP_6: localcmd("\nimpulse 6\n"); return true;
713 case K_KP_7: localcmd("\nimpulse 7\n"); return true;
714 case K_KP_8: localcmd("\nimpulse 8\n"); return true;
715 case K_KP_9: localcmd("\nimpulse 9\n"); return true;
716 case K_KP_0: localcmd("\nimpulse 10\n"); return true;
719 mv_selection_keyboard = 1;
720 mv_selection = MapVote_MoveRight(mv_selection);
723 mv_selection_keyboard = 1;
724 mv_selection = MapVote_MoveLeft(mv_selection);
727 mv_selection_keyboard = 1;
728 mv_selection = MapVote_MoveDown(mv_selection);
731 mv_selection_keyboard = 1;
732 mv_selection = MapVote_MoveUp(mv_selection);
737 if ( mv_selection_keyboard )
738 MapVote_SendChoice(mv_selection);
742 if (nPrimary == K_MOUSE1)
744 mv_selection_keyboard = 0;
745 mv_selection = mv_mouse_selection;
746 if (mv_selection >= 0)
748 imp = min(mv_selection + 1, mv_num_maps);
749 localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
757 void MapVote_UpdateMask()
763 void MapVote_UpdateVotes()
766 for(i = 0; i < mv_num_maps; ++i)
768 if(mv_avail[i] == GTV_AVAILABLE)
771 mv_votes[i] = ReadByte();
779 mv_ownvote = ReadByte()-1;
790 MapVote_UpdateMask();
793 MapVote_UpdateVotes();
796 void Net_MapVote_Picture()
798 int type = ReadByte();
799 mv_preview[type] = true;
800 mv_pics[type] = strzone(ReadPicture());