1 #include "mapvoting.qh"
2 #include "scoreboard.qh"
4 string MapVote_FormatMapItem(int id, string map, float _count, float maxwidth, vector fontsize)
7 pre = sprintf("%d. ", id+1);
11 post = _(" (1 vote)");
12 else if(_count >= 0 && mv_avail[id] == GTV_AVAILABLE)
13 post = sprintf(_(" (%d votes)"), _count);
19 maxwidth -= stringwidth(pre, false, fontsize) + stringwidth(post, false, fontsize);
20 map = textShortenToWidth(map, maxwidth, fontsize, stringwidth_nocolors);
21 return strcat(pre, map, post);
24 string GameTypeVote_DescriptionByID(float id)
26 return MapInfo_Type_Description(MapInfo_Type_FromString(mv_maps[id]));
29 vector MapVote_RGB(int id)
31 if(mv_avail[id] != GTV_AVAILABLE)
35 else if (id == mv_selection)
41 void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, float _count, int id)
44 float desc_padding = gtv_text_size.x * 3;
45 float rect_margin = hud_fontsize.y / 2;
46 vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
47 vector rect_size = '1 1 0';
48 rect_size_x = tsize + rect_margin;
49 rect_size_y = maxh + rect_margin;
50 vector rgb = MapVote_RGB(id);
54 if(mv_avail_start[id] != GTV_AVAILABLE)
56 else if ( mv_avail[id] != GTV_AVAILABLE && mv_top2_alpha)
57 alpha = mv_top2_alpha;
61 if(id == mv_selection && mv_avail[id] == GTV_AVAILABLE)
63 drawfill(rect_pos, rect_size, '1 1 1', 0.1, DRAWFLAG_NORMAL);
67 drawfill(rect_pos, rect_size, rgb, 0.1*alpha, DRAWFLAG_NORMAL);
68 drawborderlines(autocvar_scoreboard_border_thickness, rect_pos, rect_size, rgb, alpha, DRAWFLAG_NORMAL);
73 title.message = MapVote_FormatMapItem(id, MapInfo_Type_ToText(MapInfo_Type_FromString(gtype)),
74 _count, tsize, gtv_text_size);
75 title.origin = pos-offset;
77 pos.y += gtv_text_size_small.y;
78 pos.y += gtv_text_size.y/2;
80 maxh -= gtv_text_size.y;
82 entity picent = spawn();
83 picent.origin = pos-offset;
84 picent.maxs = '1 1 0 ' * min(maxh, desc_padding) * 0.8;
86 pos.x += desc_padding;
87 tsize -= desc_padding;
89 string thelabel = GameTypeVote_DescriptionByID(id), ts;
94 float i,n = tokenizebyseparator(thelabel, "\n");
95 for(i = 0; i < n && maxh > (nlines+1)*gtv_text_size_small.y; ++i)
97 getWrappedLine_remaining = argv(i);
98 while(getWrappedLine_remaining && maxh > (nlines+1)*gtv_text_size_small.y)
100 ts = getWrappedLine(tsize, gtv_text_size_small, stringwidth_colors);
105 next.origin = pos-offset;
108 pos.y += gtv_text_size_small.y;
115 maxh -= max(nlines*gtv_text_size_small.y,picent.maxs.y);
118 drawstring(title.origin+offset, title.message, gtv_text_size, rgb, alpha, DRAWFLAG_NORMAL);
121 drawpic(picent.origin+offset, pic, picent.maxs, '1 1 1', alpha, DRAWFLAG_NORMAL);
123 for ( last = title.chain; last ; )
125 drawstring(last.origin+offset, last.message, gtv_text_size_small, '1 1 1', alpha, DRAWFLAG_NORMAL);
135 void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float _count, int id)
137 vector img_size = '0 0 0';
142 isize -= hud_fontsize.y; // respect the text when calculating the image size
144 rgb = MapVote_RGB(id);
147 img_size_x = isize / 0.75; // 4:3 x can be stretched easily, height is defined in isize
149 pos_y = pos.y + img_size.y;
151 label = MapVote_FormatMapItem(id, map, _count, tsize, hud_fontsize);
153 text_size = stringwidth(label, false, hud_fontsize);
156 if (mv_avail[id] != GTV_AVAILABLE && mv_top2_alpha)
157 theAlpha = mv_top2_alpha;
161 pos.x -= text_size*0.5;
162 drawstring(pos, label, hud_fontsize, rgb, theAlpha, DRAWFLAG_NORMAL);
164 pos_x = pos.x + text_size*0.5 - img_size.x*0.5;
165 pos_y = pos.y - img_size.y;
167 pos += autocvar_scoreboard_border_thickness * '1 1 0';
168 img_size -= (autocvar_scoreboard_border_thickness * 2) * '1 1 0';
171 drawfill(pos, img_size, '.5 .5 .5', .7 * theAlpha, DRAWFLAG_NORMAL);
175 if(drawgetimagesize(pic) == '0 0 0')
176 drawpic(pos, draw_UseSkinFor("nopreview_map"), img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
178 drawpic(pos, pic, img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
182 drawborderlines(autocvar_scoreboard_border_thickness, pos, img_size, rgb, theAlpha, DRAWFLAG_NORMAL);
184 drawborderlines(autocvar_scoreboard_border_thickness, pos, img_size, '0 0 0', theAlpha, DRAWFLAG_NORMAL);
186 if(id == mv_selection && mv_avail[id] == GTV_AVAILABLE)
187 drawfill(pos, img_size, '1 1 1', 0.1, DRAWFLAG_NORMAL);
190 void MapVote_DrawAbstain(vector pos, float isize, float tsize, float _count, int id)
196 rgb = MapVote_RGB(id);
198 pos_y = pos.y + hud_fontsize.y;
200 label = MapVote_FormatMapItem(id, _("Don't care"), _count, tsize, hud_fontsize);
202 text_size = stringwidth(label, false, hud_fontsize);
204 pos.x -= text_size*0.5;
205 drawstring(pos, label, hud_fontsize, rgb, 1, DRAWFLAG_NORMAL);
208 vector MapVote_GridVec(vector gridspec, int i, int m)
212 '1 0 0' * (gridspec.x * r)
214 '0 1 0' * (gridspec.y * (i - r) / m);
217 float MapVote_Selection(vector topleft, vector cellsize, float rows, float columns)
222 mv_mouse_selection = -1;
224 for (r = 0; r < rows; ++r)
225 for (c = 0; c < columns; ++c)
227 if (mv_mousepos.x >= topleft.x + cellsize.x * c &&
228 mv_mousepos.x <= topleft.x + cellsize.x * (c + 1) &&
229 mv_mousepos.y >= topleft.y + cellsize.y * r &&
230 mv_mousepos.y <= topleft.y + cellsize.y * (r + 1))
232 mv_mouse_selection = r * columns + c;
237 if (mv_mouse_selection >= mv_num_maps)
238 mv_mouse_selection = -1;
240 if (mv_abstain && mv_mouse_selection < 0)
241 mv_mouse_selection = mv_num_maps;
243 if ( mv_selection_keyboard )
246 return mv_mouse_selection;
259 vector dist = '0 0 0';
264 if (!autocvar_hud_cursormode)
266 vector mpos = mv_mousepos + getmousepos();
267 mpos_x = bound(0, mpos.x, vid_conwidth);
268 mpos_y = bound(0, mpos.y, vid_conheight);
270 if ( mpos.x != mv_mousepos.x || mpos.y != mv_mousepos.y )
271 mv_selection_keyboard = 0;
276 center = (vid_conwidth - 1)/2;
277 xmin = vid_conwidth*0.05; // 5% border must suffice
278 xmax = vid_conwidth - xmin;
280 i = autocvar_con_chatpos; //*autocvar_con_chatsize;
282 ymax = vid_conheight + (i - autocvar_con_chat) * autocvar_con_chatsize;
283 if(i >= 0 || ymax < (vid_conheight*0.5))
284 ymax = vid_conheight - ymin;
286 hud_fontsize = HUD_GetFontsize("hud_fontsize");
291 draw_beginBoldFont();
292 map = ((gametypevote) ? _("Decide the gametype") : _("Vote for a map"));
293 pos_x = center - stringwidth(map, false, '12 0 0');
294 drawstring(pos, map, '24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
297 if( mapvote_chosenmap != "" )
299 pos_x = center - stringwidth(mapvote_chosenmap, false, hud_fontsize*1.5/2);
300 drawstring(pos, mapvote_chosenmap, hud_fontsize*1.5, '1 1 1', 1, DRAWFLAG_NORMAL);
301 pos.y += hud_fontsize.y*2;
304 i = ceil(max(0, mv_timeout - time));
305 map = sprintf(_("%d seconds left"), i);
306 pos_x = center - stringwidth(map, false, '8 0 0');
307 drawstring(pos, map, '16 16 0', '0 1 0', 1, DRAWFLAG_NORMAL);
312 // base for multi-column stuff...
317 rows = ceil(mv_num_maps / mv_columns);
319 dist_x = (xmax - xmin) / mv_columns;
320 dist_y = (ymax - pos.y) / rows;
324 tsize = dist.x - hud_fontsize.y;
326 float maxheight = (ymax - pos.y) / 3;
327 if ( isize > maxheight )
329 pos.x += (isize - maxheight)/2;
333 dist.y += hud_fontsize.y;
334 pos_x = ( vid_conwidth - dist.x * mv_columns ) / 2;
339 isize = min(dist.y - 10, 0.75 * tsize);
342 mv_selection = MapVote_Selection(pos, dist, rows, mv_columns);
346 pos.y += (dist.y - isize) / 2;
350 mv_top2_alpha = max(0.2, 1 - (time - mv_top2_time)*(time - mv_top2_time));
352 void (vector, float, float, string, string, float, float) DrawItem;
355 DrawItem = GameTypeVote_DrawGameTypeItem;
357 DrawItem = MapVote_DrawMapItem;
359 for(i = 0; i < mv_num_maps; ++i)
361 tmp = mv_votes[i]; // FTEQCC bug: too many array accesses in the function call screw it up
364 DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), isize, tsize, map, mv_pics[i], tmp, i);
366 DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), isize, tsize, map, "", tmp, i);
372 if(mv_abstain && i < mv_num_maps) {
374 pos_y = ymax + isize - hud_fontsize.y;
375 pos_x = (xmax+xmin)*0.5;
376 MapVote_DrawAbstain(pos, isize, xmax - xmin, tmp, i);
379 drawpic(mv_mousepos, strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"), '32 32 0', '1 1 1', 1 - autocvar__menu_alpha, DRAWFLAG_NORMAL);
382 void Cmd_MapVote_MapDownload(float argc)
387 if(argc != 2 || !mv_pk3list)
389 print(_("mv_mapdownload: ^3You're not supposed to use this command on your own!\n"));
394 for(pak = mv_pk3list; pak; pak = pak.chain)
395 if(pak.sv_entnum == id)
398 if(!pak || pak.sv_entnum != id) {
399 print(_("^1Error:^7 Couldn't find pak index.\n"));
403 if(PreviewExists(pak.message))
405 mv_preview[id] = true;
408 print(_("Requesting preview...\n"));
409 localcmd(strcat("\ncmd mv_getpicture ", ftos(id), "\n"));
413 void MapVote_CheckPK3(string pic, string pk3, int id)
421 pak.chain = mv_pk3list;
426 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
430 Cmd_MapVote_MapDownload(tokenize_console(strcat("mv_download ", ftos(id))));
434 void MapVote_CheckPic(string pic, string pk3, int id)
436 // never try to retrieve a pic for the "don't care" 'map'
437 if(mv_abstain && id == mv_num_maps - 1)
440 if(PreviewExists(pic))
442 mv_preview[id] = true;
445 MapVote_CheckPK3(pic, pk3, id);
448 void MapVote_ReadMask()
451 if ( mv_num_maps < 24 )
456 else if(mv_num_maps < 16)
461 for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
462 mv_avail[i] = (mask & power) ? GTV_AVAILABLE : GTV_FORBIDDEN;
466 for(i = 0; i < mv_num_maps; ++i )
467 mv_avail[i] = ReadByte();
476 precache_sound ("misc/invshot.wav");
479 if(autocvar_hud_cursormode) { setcursormode(1); }
480 else { mv_mousepos = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight; }
482 mv_selection_keyboard = 0;
484 for(n_ssdirs = 0; ; ++n_ssdirs)
489 if(n_ssdirs < NUM_SSDIRS)
490 ssdirs[n_ssdirs] = s;
492 n_ssdirs = min(n_ssdirs, NUM_SSDIRS);
494 mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
495 mv_abstain = ReadByte();
497 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
498 mv_detail = ReadByte();
501 mv_timeout = ReadCoord();
503 gametypevote = ReadByte();
505 float mv_real_num_maps = mv_num_maps - mv_abstain;
509 mapvote_chosenmap = strzone(ReadString());
510 if ( gametypevote == 2 )
513 gtv_text_size = hud_fontsize*1.4;
514 gtv_text_size_small = hud_fontsize*1.1;
516 if (mv_real_num_maps > 8 )
519 mv_columns = min(2, mv_real_num_maps);
523 if (mv_real_num_maps > 16)
525 else if (mv_real_num_maps > 9)
527 else if(mv_real_num_maps > 3)
530 mv_columns = mv_real_num_maps;
534 for(i = 0; i < mv_num_maps; ++i )
535 mv_avail_start[i] = mv_avail[i];
537 // Assume mv_pk3list is world, there should only be 1 mapvote per round
538 mv_pk3list = world; // I'm still paranoid!
540 for(i = 0; i < mv_num_maps; ++i)
544 map = strzone(ReadString());
545 pk3 = strzone(ReadString());
546 j = bound(0, ReadByte(), n_ssdirs - 1);
550 mv_avail[i] = ReadByte();
554 //map = strzone(strcat("gfx/menu/default/gametype_", map));
555 //map = strzone(sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, map));
556 string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, map);
557 if(precache_pic(mv_picpath) == "")
558 mv_picpath = strcat("gfx/menu/default/gametype_", map);
559 map = strzone(mv_picpath);
561 mv_preview[i] = PreviewExists(map);
565 map = strzone(strcat(ssdirs[j], "/", map));
567 mv_preview[i] = false;
568 MapVote_CheckPic(map, pk3, i);
572 for(i = 0; i < n_ssdirs; ++i)
573 ssdirs[n_ssdirs] = string_null;
577 void MapVote_SendChoice(float index)
579 localcmd(strcat("\nimpulse ", ftos(index+1), "\n"));
582 int MapVote_MoveLeft(int pos)
586 imp = mv_num_maps - 1;
588 imp = pos < 1 ? mv_num_maps - 1 : pos - 1;
589 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
590 imp = MapVote_MoveLeft(imp);
593 int MapVote_MoveRight(int pos)
599 imp = pos >= mv_num_maps - 1 ? 0 : pos + 1;
600 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
601 imp = MapVote_MoveRight(imp);
604 int MapVote_MoveUp(int pos)
608 imp = mv_num_maps - 1;
611 imp = pos - mv_columns;
614 imp = floor(mv_num_maps/mv_columns)*mv_columns + pos % mv_columns;
615 if ( imp >= mv_num_maps )
619 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
620 imp = MapVote_MoveUp(imp);
623 int MapVote_MoveDown(int pos)
630 imp = pos + mv_columns;
631 if ( imp >= mv_num_maps )
632 imp = imp % mv_columns;
634 if ( mv_avail[imp] != GTV_AVAILABLE && imp != mv_ownvote )
635 imp = MapVote_MoveDown(imp);
639 float MapVote_InputEvent(float bInputType, float nPrimary, float nSecondary)
648 mv_mousepos_x = nPrimary;
649 mv_mousepos_y = nSecondary;
650 mv_selection_keyboard = 0;
657 if ('0' <= nPrimary && nPrimary <= '9')
659 imp = nPrimary - '0';
660 if (imp == 0) imp = 10;
661 localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
666 case K_KP_1: localcmd("\nimpulse 1\n"); return true;
667 case K_KP_2: localcmd("\nimpulse 2\n"); return true;
668 case K_KP_3: localcmd("\nimpulse 3\n"); return true;
669 case K_KP_4: localcmd("\nimpulse 4\n"); return true;
670 case K_KP_5: localcmd("\nimpulse 5\n"); return true;
671 case K_KP_6: localcmd("\nimpulse 6\n"); return true;
672 case K_KP_7: localcmd("\nimpulse 7\n"); return true;
673 case K_KP_8: localcmd("\nimpulse 8\n"); return true;
674 case K_KP_9: localcmd("\nimpulse 9\n"); return true;
675 case K_KP_0: localcmd("\nimpulse 10\n"); return true;
678 mv_selection_keyboard = 1;
679 mv_selection = MapVote_MoveRight(mv_selection);
682 mv_selection_keyboard = 1;
683 mv_selection = MapVote_MoveLeft(mv_selection);
686 mv_selection_keyboard = 1;
687 mv_selection = MapVote_MoveDown(mv_selection);
690 mv_selection_keyboard = 1;
691 mv_selection = MapVote_MoveUp(mv_selection);
696 if ( mv_selection_keyboard )
697 MapVote_SendChoice(mv_selection);
701 if (nPrimary == K_MOUSE1)
703 mv_selection_keyboard = 0;
704 mv_selection = mv_mouse_selection;
705 if (mv_selection >= 0)
707 imp = min(mv_selection + 1, mv_num_maps);
708 localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
716 void MapVote_UpdateMask()
722 void MapVote_UpdateVotes()
725 for(i = 0; i < mv_num_maps; ++i)
727 if(mv_avail[i] == GTV_AVAILABLE)
730 mv_votes[i] = ReadByte();
738 mv_ownvote = ReadByte()-1;
749 MapVote_UpdateMask();
752 MapVote_UpdateVotes();
755 void Net_MapVote_Picture()
757 int type = ReadByte();
758 mv_preview[type] = true;
759 mv_pics[type] = strzone(ReadPicture());