]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/mapvoting.qc
Map vote: capture and ignore right mouse click; it prevents zooming while holding...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / mapvoting.qc
1 #include "mapvoting.qh"
2
3 #include <client/draw.qh>
4 #include <client/hud/_mod.qh>
5 #include <client/hud/panel/scoreboard.qh>
6 #include <common/mapinfo.qh>
7 #include <common/util.qh>
8
9 // MapVote (#21)
10
11 void MapVote_Draw_Export(int fh)
12 {
13         // allow saving cvars that aesthetically change the panel into hud skin files
14         HUD_Write_Cvar("hud_panel_mapvote_highlight_border");
15 }
16
17 int mv_num_maps;
18
19 string mv_maps[MAPVOTE_COUNT];
20 string mv_pics[MAPVOTE_COUNT];
21 string mv_pk3[MAPVOTE_COUNT]; // map pk3 name or gametype human readable name
22 string mv_desc[MAPVOTE_COUNT];
23 float mv_preview[MAPVOTE_COUNT];
24 float mv_votes[MAPVOTE_COUNT];
25 float mv_flags[MAPVOTE_COUNT];
26 float mv_flags_start[MAPVOTE_COUNT];
27 entity mv_pk3list;
28 float mv_abstain;
29 float mv_ownvote;
30 float mv_detail;
31 float mv_timeout;
32 float mv_top2_time;
33 float mv_top2_alpha;
34
35 int mv_selection;
36 int mv_columns;
37 int mv_mouse_selection;
38 int mv_selection_keyboard;
39
40 float gametypevote;
41 string mapvote_chosenmap;
42 vector gtv_text_size;
43 vector gtv_text_size_small;
44
45 const int NUM_SSDIRS = 4;
46 string ssdirs[NUM_SSDIRS];
47 int n_ssdirs;
48
49 bool PreviewExists(string name)
50 {
51         if(autocvar_cl_readpicture_force)
52                 return false;
53
54         if (fexists(strcat(name, ".tga"))) return true;
55         if (fexists(strcat(name, ".png"))) return true;
56         if (fexists(strcat(name, ".jpg"))) return true;
57         if (fexists(strcat(name, ".pcx"))) return true;
58
59         return false;
60 }
61
62 string MapVote_FormatMapItem(int id, string map, float _count, float maxwidth, vector fontsize)
63 {
64         TC(int, id);
65         string pre, post;
66         pre = sprintf("%d. ", id+1);
67         if(mv_detail)
68         {
69                 if(_count == 1)
70                         post = _(" (1 vote)");
71                 else if(_count >= 0 && (mv_flags[id] & GTV_AVAILABLE))
72                         post = sprintf(_(" (%d votes)"), _count);
73                 else
74                         post = "";
75         }
76         else
77                 post = "";
78         maxwidth -= stringwidth(pre, false, fontsize) + stringwidth(post, false, fontsize);
79         map = textShortenToWidth(map, maxwidth, fontsize, stringwidth_nocolors);
80         return strcat(pre, map, post);
81 }
82
83 vector MapVote_RGB(int id)
84 {
85         TC(int, id);
86         if(!(mv_flags[id] & GTV_AVAILABLE))
87                 return '1 1 1';
88         if(id == mv_ownvote)
89                 return '0 1 0';
90         else if (id == mv_selection)
91                 return '1 1 0';
92         else
93                 return '1 1 1';
94 }
95
96 void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, float _count, int id)
97 {
98         TC(int, id);
99         // Find the correct alpha
100         float alpha;
101         if(!(mv_flags_start[id] & GTV_AVAILABLE))
102                 alpha = 0.2; // The gametype isn't supported by the map
103         else if ( !(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
104                 alpha = mv_top2_alpha; // Fade away if not one of the top 2 choice
105         else
106                 alpha = 1; // Normal, full alpha
107         alpha *= panel_fg_alpha;
108
109         // Bounding box details
110         float rect_margin = hud_fontsize.y / 2;
111
112         pos.x += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
113         pos.y += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
114         maxh -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
115         tsize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
116
117         vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
118         vector rect_size = '1 1 0';
119         rect_size.x = tsize + rect_margin;
120         rect_size.y = maxh + rect_margin;
121
122         // Highlight selected item
123         if(id == mv_selection && (mv_flags[id] & GTV_AVAILABLE))
124         {
125                 drawfill(rect_pos, rect_size, '1 1 1', 0.1 * panel_fg_alpha, DRAWFLAG_NORMAL);
126         }
127
128         // Highlight current vote
129         vector rgb = MapVote_RGB(id);
130         if(id == mv_ownvote)
131         {
132                 drawfill(rect_pos, rect_size, rgb, 0.1 * alpha, DRAWFLAG_NORMAL);
133                 drawborderlines(autocvar_hud_panel_mapvote_highlight_border, rect_pos, rect_size, rgb, alpha, DRAWFLAG_NORMAL);
134         }
135
136         vector offset = pos;
137
138         float title_gap = gtv_text_size.y * 1.4; // distance between the title and the description
139         pos.y += title_gap;
140         maxh -= title_gap;
141
142         // Evaluate the image size
143         vector image_size = '1 1 0' * gtv_text_size.x * 3;
144         if ( maxh < image_size.y )
145                 image_size = '1 1 0' * maxh;
146         image_size *= 0.8;
147         float desc_padding = gtv_text_size.x * 0.6;
148         pos.x += image_size.x + desc_padding;
149         tsize -= image_size.x + desc_padding;
150
151         // Split the description into lines
152         entity title;
153         title = spawn();
154         title.message = MapVote_FormatMapItem(id, mv_pk3[id], _count, tsize, gtv_text_size);
155
156         string thelabel = mv_desc[id], ts;
157         entity last = title;
158         entity next = NULL;
159         float nlines = 0;
160         if( thelabel != "")
161         {
162                 float i,n = tokenizebyseparator(thelabel, "\n");
163                 for(i = 0; i < n && maxh > (nlines+1)*gtv_text_size_small.y; ++i)
164                 {
165                         getWrappedLine_remaining = argv(i);
166                         while(getWrappedLine_remaining && maxh > (nlines+1)*gtv_text_size_small.y)
167                         {
168                                 ts = getWrappedLine(tsize, gtv_text_size_small, stringwidth_colors);
169                                 if (ts != "")
170                                 {
171                                         next = spawn();
172                                         next.message = ts;
173                                         next.origin = pos-offset;
174                                         last.chain = next;
175                                         last = next;
176                                         pos.y += gtv_text_size_small.y;
177                                         nlines++;
178                                 }
179                         }
180                 }
181         }
182
183         // Center the contents in the bounding box
184         maxh -= max(nlines*gtv_text_size_small.y,image_size.y);
185         if ( maxh > 0 )
186                 offset.y += maxh/2;
187
188         // Draw the title
189         drawstring(offset, title.message, gtv_text_size, rgb, alpha, DRAWFLAG_NORMAL);
190
191         // Draw the icon
192         if(pic != "")
193                 drawpic('0 1 0'*title_gap+'0.5 0 0'*desc_padding+offset, pic, image_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
194
195         // Draw the description
196         for ( last = title.chain; last ; )
197         {
198                 drawstring(last.origin+offset, last.message, gtv_text_size_small, '1 1 1', alpha, DRAWFLAG_NORMAL);
199                 next = last;
200                 last = last.chain;
201                 delete(next);
202         }
203
204         // Cleanup
205         delete(title);
206 }
207
208 void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float _count, int id)
209 {
210         TC(int, id);
211         vector img_size = '0 0 0';
212         string label;
213         float text_size;
214
215         float rect_margin = hud_fontsize.y / 2;
216
217         pos.x += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
218         pos.y += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
219         isize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
220         tsize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
221
222         vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
223         vector rect_size = '1 1 0';
224         rect_size.x = tsize + rect_margin;
225         rect_size.y = isize + rect_margin;
226
227         float img_ar = 4/3;
228         img_size.x = min(tsize, isize * img_ar);
229         img_size.y = img_size.x / img_ar;
230         img_size.y -= hud_fontsize.y;
231         img_size.x = img_size.y * img_ar;
232
233         pos.y += (isize - img_size.y - hud_fontsize.y) / 2;
234
235         label = MapVote_FormatMapItem(id, map, _count, tsize, hud_fontsize);
236
237         text_size = stringwidth(label, false, hud_fontsize);
238
239         float save_rect_sizex = rect_size.x;
240         rect_size.x = max(img_size.x, text_size) + rect_margin;
241         rect_pos.x += (save_rect_sizex - rect_size.x) / 2;
242
243         vector text_pos = '0 0 0';
244         text_pos.x = pos.x + (tsize - text_size) / 2;
245         text_pos.y = pos.y + img_size.y;
246
247         pos.x += (tsize - img_size.x) / 2;
248
249         float theAlpha;
250         if (!(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
251                 theAlpha = mv_top2_alpha;
252         else
253                 theAlpha = 1;
254         theAlpha *= panel_fg_alpha;
255
256         // Highlight selected item
257         if(id == mv_selection && (mv_flags[id] & GTV_AVAILABLE))
258                 drawfill(rect_pos, rect_size, '1 1 1', 0.1 * panel_fg_alpha, DRAWFLAG_NORMAL);
259
260         // Highlight current vote
261         vector rgb = MapVote_RGB(id);
262         if(id == mv_ownvote)
263         {
264                 drawfill(rect_pos, rect_size, rgb, 0.1 * theAlpha, DRAWFLAG_NORMAL);
265                 drawborderlines(autocvar_hud_panel_mapvote_highlight_border, rect_pos, rect_size, rgb, theAlpha, DRAWFLAG_NORMAL);
266         }
267
268         drawstring(text_pos, label, hud_fontsize, rgb, theAlpha, DRAWFLAG_NORMAL);
269
270         if(pic == "")
271         {
272                 drawfill(pos, img_size, '.5 .5 .5', .7 * theAlpha, DRAWFLAG_NORMAL);
273         }
274         else
275         {
276                 if(drawgetimagesize(pic) == '0 0 0')
277                         drawpic(pos, draw_UseSkinFor("nopreview_map"), img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
278                 else
279                         drawpic(pos, pic, img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
280         }
281 }
282
283 void MapVote_DrawAbstain(vector pos, float isize, float tsize, float _count, int id)
284 {
285         TC(int, id);
286         vector rgb;
287         float text_size;
288         string label;
289
290         rgb = MapVote_RGB(id);
291
292         label = MapVote_FormatMapItem(id, _("Don't care"), _count, tsize, hud_fontsize);
293
294         text_size = stringwidth(label, false, hud_fontsize);
295
296         pos.x -= text_size*0.5;
297         drawstring(pos, label, hud_fontsize, rgb, panel_fg_alpha, DRAWFLAG_NORMAL);
298 }
299
300 vector MapVote_GridVec(vector gridspec, int i, int m)
301 {
302         TC(int, i); TC(int, m);
303         int r = i % m;
304         return
305                 '1 0 0' * (gridspec.x * r)
306                 +
307                 '0 1 0' * (gridspec.y * (i - r) / m);
308 }
309
310 float MapVote_Selection(vector topleft, vector cellsize, float rows, float columns)
311 {
312
313         float c, r;
314
315         mv_mouse_selection = -1;
316
317         for (r = 0; r < rows; ++r)
318                 for (c = 0; c < columns; ++c)
319                 {
320                         if (mousepos.x >= topleft.x + cellsize.x *  c &&
321                                 mousepos.x <= topleft.x + cellsize.x * (c + 1) &&
322                                 mousepos.y >= topleft.y + cellsize.y *  r &&
323                                 mousepos.y <= topleft.y + cellsize.y * (r + 1))
324                         {
325                                 mv_mouse_selection = r * columns + c;
326                                 break;
327                         }
328                 }
329
330         if (mv_mouse_selection >= mv_num_maps)
331                 mv_mouse_selection = -1;
332
333         if (mv_abstain && mv_mouse_selection < 0)
334                 mv_mouse_selection = mv_num_maps;
335
336         if ( mv_selection_keyboard )
337                 return mv_selection;
338
339         return mv_mouse_selection;
340 }
341
342 vector prev_mousepos;
343 // draws map vote or gametype vote
344 void MapVote_Draw()
345 {
346         string map;
347         int i;
348         float tmp;
349         vector pos;
350         float center;
351         float rows;
352         vector dist = '0 0 0';
353
354         //if(intermission != 2) return;
355         if(!mv_active)
356                 return;
357
358         HUD_Panel_LoadCvars();
359
360         if (!autocvar_hud_cursormode)
361         {
362                 if (mousepos.x != prev_mousepos.x || mousepos.y != prev_mousepos.y)
363                 {
364                         mv_selection_keyboard = 0;
365                         prev_mousepos = mousepos;
366                 }
367         }
368
369         center = (vid_conwidth - 1)/2;
370         xmin = vid_conwidth * 0.08;
371         xmax = vid_conwidth - xmin;
372         ymin = 20;
373         ymax = vid_conheight - ymin;
374
375         if(chat_posy + chat_sizey / 2 < vid_conheight / 2)
376                 ymin += chat_sizey;
377         else
378                 ymax -= chat_sizey;
379
380         hud_fontsize = HUD_GetFontsize("hud_fontsize");
381         if (gametypevote)
382         {
383                 gtv_text_size = hud_fontsize * 1.4;
384                 gtv_text_size_small = hud_fontsize * 1.1;
385         }
386
387         pos.y = ymin;
388         pos.z = 0;
389
390         HUD_Scale_Disable();
391         draw_beginBoldFont();
392
393         map = ((gametypevote) ? _("Decide the gametype") : _("Vote for a map"));
394         pos.x = center - stringwidth(map, false, hud_fontsize * 2) * 0.5;
395         drawstring(pos, map, hud_fontsize * 2, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
396         pos.y += hud_fontsize.y * 2;
397
398         if( mapvote_chosenmap != "" )
399         {
400                 pos.y += hud_fontsize.y * 0.25;
401                 pos.x = center - stringwidth(mapvote_chosenmap, false, hud_fontsize * 1.5) * 0.5;
402                 drawstring(pos, mapvote_chosenmap, hud_fontsize * 1.5, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
403                 pos.y += hud_fontsize.y * 1.5;
404         }
405         pos.y += hud_fontsize.y * 0.5;
406
407         draw_endBoldFont();
408
409         i = ceil(max(0, mv_timeout - time));
410         map = sprintf(_("%d seconds left"), i);
411         pos.x = center - stringwidth(map, false, hud_fontsize * 1.5) * 0.5;
412         drawstring(pos, map, hud_fontsize * 1.5, '0 1 0', panel_fg_alpha, DRAWFLAG_NORMAL);
413         pos.y += hud_fontsize.y * 1.5;
414         pos.y += hud_fontsize.y * 0.5;
415
416         // base for multi-column stuff...
417         pos.y += hud_fontsize.y;
418         pos.x = xmin;
419         ymin = pos.y;
420         float abstain_spacing = panel_bg_border + hud_fontsize.y;
421         if(mv_abstain)
422         {
423                 mv_num_maps -= 1;
424                 ymax -= abstain_spacing;
425         }
426
427         // higher than the image itself ratio for mapvote items to reserve space for long map names
428         int item_aspect = (gametypevote) ? 3/1 : 5/3;
429         vector table_size = HUD_GetTableSize_BestItemAR(mv_num_maps, vec2(xmax - xmin, ymax - ymin), item_aspect);
430         mv_columns = table_size.x;
431         rows = table_size.y;
432
433         dist.x = (xmax - xmin) / mv_columns;
434         dist.y = (ymax - pos.y) / rows;
435
436         // reduce size of too wide items
437         tmp = vid_conwidth / 3; // max width
438         if(dist.x > tmp)
439         {
440                 dist.x = tmp;
441                 dist.y = min(dist.y, dist.x / item_aspect);
442         }
443         tmp = vid_conheight / 3; // max height
444         if(dist.y > tmp)
445         {
446                 dist.y = tmp;
447                 dist.x = min(dist.x, dist.y * item_aspect);
448         }
449
450         // reduce size to fix aspect ratio
451         if(dist.x / dist.y > item_aspect)
452                 dist.x = dist.y * item_aspect;
453         else
454                 dist.y = dist.x / item_aspect;
455
456         // adjust table pos and size according to the new size
457         float offset;
458         offset = ((xmax - pos.x) - dist.x * mv_columns) / 2;
459         xmin = pos.x += offset;
460         xmax -= offset;
461         offset = ((ymax - pos.y) - dist.y * rows) / 2;
462         ymax -= 2 * offset;
463
464         // override panel_pos and panel_size
465         panel_pos.x = pos.x;
466         panel_pos.y = pos.y;
467         panel_size.x = xmax - xmin;
468         panel_size.y = ymax - ymin;
469         HUD_Panel_DrawBg();
470
471         if(panel_bg_padding)
472         {
473                 // FIXME item AR gets slightly changed here...
474                 // it's rather hard to avoid it at this point
475                 dist.x -= 2 * panel_bg_padding / mv_columns;
476                 dist.y -= 2 * panel_bg_padding / rows;
477                 xmin = pos.x += panel_bg_padding;
478                 ymin = pos.y += panel_bg_padding;
479                 xmax -= 2 * panel_bg_padding;
480                 ymax -= 2 * panel_bg_padding;
481         }
482
483         mv_selection = MapVote_Selection(pos, dist, rows, mv_columns);
484
485         if (mv_top2_time)
486                 mv_top2_alpha = max(0.2, 1 - (time - mv_top2_time) ** 2);
487
488         void (vector, float, float, string, string, float, float) DrawItem;
489
490         if(gametypevote)
491                 DrawItem = GameTypeVote_DrawGameTypeItem;
492         else
493                 DrawItem = MapVote_DrawMapItem;
494
495         for(i = 0; i < mv_num_maps; ++i)
496         {
497                 tmp = mv_votes[i]; // FTEQCC bug: too many array accesses in the function call screw it up
498                 map = mv_maps[i];
499                 if(mv_preview[i])
500                         DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), dist.y, dist.x, map, mv_pics[i], tmp, i);
501                 else
502                         DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), dist.y, dist.x, map, "", tmp, i);
503         }
504
505         if(mv_abstain)
506                 ++mv_num_maps;
507
508         if(mv_abstain && i < mv_num_maps) {
509                 tmp = mv_votes[i];
510                 pos.y = ymax + abstain_spacing;
511                 pos.x = (xmax+xmin)*0.5;
512                 MapVote_DrawAbstain(pos, dist.x, xmax - xmin, tmp, i);
513         }
514 }
515
516 void Cmd_MapVote_MapDownload(int argc)
517 {
518         TC(int, argc);
519         entity pak;
520
521         if(argc != 2 || !mv_pk3list)
522         {
523                 LOG_INFO(_("mv_mapdownload: ^3You're not supposed to use this command on your own!"));
524                 return;
525         }
526
527         int id = stof(argv(1));
528         for(pak = mv_pk3list; pak; pak = pak.chain)
529                 if(pak.sv_entnum == id)
530                         break;
531
532         if(!pak || pak.sv_entnum != id) {
533                 LOG_INFO(_("^1Error:^7 Couldn't find pak index."));
534                 return;
535         }
536
537         if(PreviewExists(pak.message))
538         {
539                 mv_preview[id] = true;
540                 return;
541         } else {
542                 LOG_INFO(_("Requesting preview..."));
543                 localcmd(strcat("\ncmd mv_getpicture ", ftos(id), "\n"));
544         }
545 }
546
547 void MapVote_CheckPK3(string pic, string pk3, int id)
548 {
549         TC(int, id);
550         entity pak;
551         pak = spawn();
552         pak.netname = pk3;
553         pak.message = pic;
554         pak.sv_entnum = id;
555
556         pak.chain = mv_pk3list;
557         mv_pk3list = pak;
558
559         if(pk3 != "")
560         {
561                 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
562         }
563         else
564         {
565                 Cmd_MapVote_MapDownload(tokenize_console(strcat("mv_download ", ftos(id))));
566         }
567 }
568
569 void MapVote_CheckPic(string pic, string pk3, int id)
570 {
571         TC(int, id);
572         // never try to retrieve a pic for the "don't care" 'map'
573         if(mv_abstain && id == mv_num_maps - 1)
574                 return;
575
576         if(PreviewExists(pic))
577         {
578                 mv_preview[id] = true;
579                 return;
580         }
581         MapVote_CheckPK3(pic, pk3, id);
582 }
583
584 void MapVote_ReadMask()
585 {
586         int i;
587         if ( mv_num_maps < 24 )
588         {
589                 int mask;
590                 if(mv_num_maps < 8)
591                         mask = ReadByte();
592                 else if(mv_num_maps < 16)
593                         mask = ReadShort();
594                 else
595                         mask = ReadLong();
596
597                 for(i = 0; i < mv_num_maps; ++i)
598                 {
599                         if (mask & BIT(i))
600                                 mv_flags[i] |= GTV_AVAILABLE;
601                         else
602                                 mv_flags[i] &= ~GTV_AVAILABLE;
603                 }
604         }
605         else
606         {
607                 for(i = 0; i < mv_num_maps; ++i )
608                         mv_flags[i] = ReadByte();
609         }
610 }
611
612 void MapVote_ReadOption(int i)
613 {
614         TC(int, i);
615         string map = strzone(ReadString());
616         string pk3 = strzone(ReadString());
617         int j = bound(0, ReadByte(), n_ssdirs - 1);
618
619         mv_maps[i] = map;
620         mv_pk3[i] = pk3;
621         mv_flags[i] = GTV_AVAILABLE;
622
623         string pic = strzone(strcat(ssdirs[j], "/", map));
624         mv_pics[i] = pic;
625         mv_preview[i] = false;
626         MapVote_CheckPic(pic, pk3, i);
627 }
628
629 void GameTypeVote_ReadOption(int i)
630 {
631         TC(int, i);
632         string gt = strzone(ReadString());
633
634         mv_maps[i] = gt;
635         mv_flags[i] = ReadByte();
636
637         string basetype = "";
638
639         if ( mv_flags[i] & GTV_CUSTOM )
640         {
641                 string name = ReadString();
642                 if ( strlen(name) < 1 )
643                         name = gt;
644                 mv_pk3[i] = strzone(name);
645                 mv_desc[i] = strzone(ReadString());
646                 basetype = strzone(ReadString());
647         }
648         else
649         {
650                 Gametype type = MapInfo_Type_FromString(gt, false);
651                 mv_pk3[i] = strzone(MapInfo_Type_ToText(type));
652                 mv_desc[i] = MapInfo_Type_Description(type);
653         }
654
655         string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, gt);
656         if(precache_pic(mv_picpath) == "")
657         {
658                 mv_picpath = strcat("gfx/menu/default/gametype_", gt);
659                 if(precache_pic(mv_picpath) == "")
660                 {
661                         mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, basetype);
662                         if(precache_pic(mv_picpath) == "")
663                         {
664                                 mv_picpath = strcat("gfx/menu/default/gametype_", basetype);
665                         }
666                 }
667         }
668         string pic = strzone(mv_picpath);
669         mv_pics[i] = pic;
670         mv_preview[i] = PreviewExists(pic);
671 }
672
673 void MapVote_Init()
674 {
675         mv_active = 1;
676         if(!autocvar_hud_cursormode) mousepos = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
677         mv_selection = -1;
678         mv_selection_keyboard = 0;
679
680         string s;
681         for(n_ssdirs = 0; ; ++n_ssdirs)
682         {
683                 s = ReadString();
684                 if(s == "")
685                         break;
686                 if(n_ssdirs < NUM_SSDIRS)
687                         ssdirs[n_ssdirs] = s;
688         }
689         n_ssdirs = min(n_ssdirs, NUM_SSDIRS);
690
691         mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
692         mv_abstain = ReadByte();
693         if(mv_abstain)
694                 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
695         mv_detail = ReadByte();
696
697         mv_ownvote = -1;
698         mv_timeout = ReadCoord();
699
700         gametypevote = ReadByte();
701
702         if(gametypevote)
703         {
704                 mapvote_chosenmap = strzone(ReadString());
705                 if ( gametypevote == 2 )
706                         gametypevote = 0;
707         }
708
709         MapVote_ReadMask();
710         int i;
711         for(i = 0; i < mv_num_maps; ++i )
712                 mv_flags_start[i] = mv_flags[i];
713
714         // Assume mv_pk3list is NULL, there should only be 1 mapvote per round
715         mv_pk3list = NULL; // I'm still paranoid!
716
717         for(i = 0; i < mv_num_maps; ++i)
718         {
719                 mv_votes[i] = 0;
720
721                 if ( gametypevote )
722                         GameTypeVote_ReadOption(i);
723                 else
724                         MapVote_ReadOption(i);
725         }
726
727         for(i = 0; i < n_ssdirs; ++i)
728                 ssdirs[n_ssdirs] = string_null;
729         n_ssdirs = 0;
730 }
731
732 void MapVote_SendChoice(int index)
733 {
734         TC(int, index);
735         localcmd(strcat("\nimpulse ", ftos(index+1), "\n"));
736 }
737
738 int MapVote_MoveLeft(int pos)
739 {
740         TC(int, pos);
741         int imp;
742         if ( pos < 0 )
743                 imp = mv_num_maps - 1;
744         else
745                 imp = pos < 1 ? mv_num_maps - 1 : pos - 1;
746         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
747                 imp = MapVote_MoveLeft(imp);
748         return imp;
749 }
750 int MapVote_MoveRight(int pos)
751 {
752         TC(int, pos);
753         int imp;
754         if ( pos < 0 )
755                 imp = 0;
756         else
757                 imp = pos >= mv_num_maps - 1 ? 0 : pos + 1;
758         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
759                 imp = MapVote_MoveRight(imp);
760         return imp;
761 }
762 int MapVote_MoveUp(int pos)
763 {
764         TC(int, pos);
765         int imp;
766         if ( pos < 0 )
767                 imp = mv_num_maps - 1;
768         else
769         {
770                 imp = pos - mv_columns;
771                 if ( imp < 0 )
772                 {
773                         imp = floor(mv_num_maps/mv_columns)*mv_columns + pos % mv_columns;
774                         if ( imp >= mv_num_maps )
775                                 imp -= mv_columns;
776                 }
777         }
778         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
779                 imp = MapVote_MoveUp(imp);
780         return imp;
781 }
782 int MapVote_MoveDown(int pos)
783 {
784         TC(int, pos);
785         int imp;
786         if ( pos < 0 )
787                 imp = 0;
788         else
789         {
790                 imp = pos + mv_columns;
791                 if ( imp >= mv_num_maps )
792                         imp = imp % mv_columns;
793         }
794         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
795                 imp = MapVote_MoveDown(imp);
796         return imp;
797 }
798
799 float MapVote_InputEvent(int bInputType, float nPrimary, float nSecondary)
800 {
801         TC(int, bInputType);
802
803         static int first_digit = 0;
804         if (!mv_active)
805                 return false;
806
807         if(bInputType == 3)
808         {
809                 mousepos.x = nPrimary;
810                 mousepos.y = nSecondary;
811                 mv_selection_keyboard = 0;
812                 return true;
813         }
814
815         if (bInputType == 2)
816                 return false;
817
818         // at this point bInputType can be 0 or 1 (key pressed or released)
819         bool key_pressed = (bInputType == 0);
820
821         if (key_pressed)
822         {
823                 if (nPrimary == K_ALT) hudShiftState |= S_ALT;
824                 if (nPrimary == K_CTRL) hudShiftState |= S_CTRL;
825                 if (nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
826         }
827         else
828         {
829                 if (nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
830                 if (nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
831                 if (nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
832
833                 if (nPrimary == K_CTRL)
834                         first_digit = 0;
835         }
836
837         int imp = 0;
838         switch(nPrimary)
839         {
840                 case K_RIGHTARROW:
841                         if (!key_pressed) return true;
842                         mv_selection_keyboard = 1;
843                         mv_selection = MapVote_MoveRight(mv_selection);
844                         return true;
845                 case K_LEFTARROW:
846                         if (!key_pressed) return true;
847                         mv_selection_keyboard = 1;
848                         mv_selection = MapVote_MoveLeft(mv_selection);
849                         return true;
850                 case K_DOWNARROW:
851                         if (!key_pressed) return true;
852                         mv_selection_keyboard = 1;
853                         mv_selection = MapVote_MoveDown(mv_selection);
854                         return true;
855                 case K_UPARROW:
856                         if (!key_pressed) return true;
857                         mv_selection_keyboard = 1;
858                         mv_selection = MapVote_MoveUp(mv_selection);
859                         return true;
860                 case K_KP_ENTER:
861                 case K_ENTER:
862                 case K_SPACE:
863                         if (!key_pressed) return true;
864                         if ( mv_selection_keyboard )
865                                 MapVote_SendChoice(mv_selection);
866                         return true;
867                 case '1': case K_KP_1: imp = 1; break;
868                 case '2': case K_KP_2: imp = 2; break;
869                 case '3': case K_KP_3: imp = 3; break;
870                 case '4': case K_KP_4: imp = 4; break;
871                 case '5': case K_KP_5: imp = 5; break;
872                 case '6': case K_KP_6: imp = 6; break;
873                 case '7': case K_KP_7: imp = 7; break;
874                 case '8': case K_KP_8: imp = 8; break;
875                 case '9': case K_KP_9: imp = 9; break;
876                 case '0': case K_KP_0: imp = 10; break;
877         }
878
879         if (imp && hudShiftState & S_CTRL)
880         {
881                 if (!first_digit)
882                 {
883                         if (!key_pressed)
884                                 return true;
885                         first_digit = imp % 10;
886                         return true;
887                 }
888                 else
889                         imp = first_digit * 10 + (imp % 10);
890         }
891
892         if (nPrimary == K_MOUSE1)
893         {
894                 if (!key_pressed)
895                         return true;
896                 mv_selection_keyboard = 0;
897                 mv_selection = mv_mouse_selection;
898                 if (mv_selection >= 0)
899                         imp = min(mv_selection + 1, mv_num_maps);
900         }
901
902         if (nPrimary == K_MOUSE2)
903                 return true; // do nothing
904
905         if (imp)
906         {
907                 if (!key_pressed)
908                         return true;
909                 if (imp <= mv_num_maps)
910                         localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
911                 return true;
912         }
913
914         return false;
915 }
916
917 void MapVote_UpdateMask()
918 {
919         MapVote_ReadMask();
920         mv_top2_time = time;
921 }
922
923 void MapVote_UpdateVotes()
924 {
925         int i;
926         for(i = 0; i < mv_num_maps; ++i)
927         {
928                 if(mv_flags[i] & GTV_AVAILABLE)
929                 {
930                         if(mv_detail)
931                                 mv_votes[i] = ReadByte();
932                         else
933                                 mv_votes[i] = 0;
934                 }
935                 else
936                         mv_votes[i] = -1;
937         }
938
939         mv_ownvote = ReadByte()-1;
940 }
941
942 NET_HANDLE(ENT_CLIENT_MAPVOTE, bool isnew)
943 {
944         make_pure(this);
945         int sf = ReadByte();
946         return = true;
947
948         if(sf & 1)
949                 MapVote_Init();
950
951         if(sf & 2)
952                 MapVote_UpdateMask();
953
954         if(sf & 4)
955                 MapVote_UpdateVotes();
956 }
957
958 NET_HANDLE(TE_CSQC_PICTURE, bool isNew)
959 {
960         Net_MapVote_Picture();
961         return true;
962 }
963
964 void Net_MapVote_Picture()
965 {
966         int type = ReadByte();
967         mv_preview[type] = true;
968         mv_pics[type] = strzone(ReadPicture());
969 }