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