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