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