]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/mapvoting.qc
Merge branch 'terencehill/minigame_flood_control' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / mapvoting.qc
1 #include "mapvoting.qh"
2
3 #include <client/draw.qh>
4 #include <client/hud/_mod.qh>
5 #include <client/hud/panel/scoreboard.qh>
6 #include <common/mapinfo.qh>
7 #include <common/util.qh>
8
9 // MapVote (#21)
10
11 void MapVote_Draw_Export(int fh)
12 {
13         // allow saving cvars that aesthetically change the panel into hud skin files
14         HUD_Write_Cvar("hud_panel_mapvote_highlight_border");
15 }
16
17 int mv_num_maps;
18
19 string mv_maps[MAPVOTE_COUNT];
20 string mv_pics[MAPVOTE_COUNT];
21 string mv_pk3[MAPVOTE_COUNT]; // map pk3 name or gametype human readable name
22 string mv_desc[MAPVOTE_COUNT];
23 float mv_preview[MAPVOTE_COUNT];
24 float mv_votes[MAPVOTE_COUNT];
25 float mv_flags[MAPVOTE_COUNT];
26 float mv_flags_start[MAPVOTE_COUNT];
27 entity mv_pk3list;
28 float mv_abstain;
29 float mv_ownvote;
30 float mv_detail;
31 float mv_timeout;
32 float mv_top2_time;
33 float mv_top2_alpha;
34 float mv_winner_time;
35 float mv_winner_alpha;
36
37 int mv_selection;
38 int mv_columns;
39 int mv_mouse_selection;
40 int mv_selection_keyboard;
41
42 bool 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_DrawMapPicture(string pic, vector pos, vector img_size, float theAlpha)
211 {
212         if(pic == "")
213                 drawfill(pos, img_size, '.5 .5 .5', .7 * theAlpha, DRAWFLAG_NORMAL);
214         else
215         {
216                 if(drawgetimagesize(pic) == '0 0 0')
217                         drawpic(pos, draw_UseSkinFor("nopreview_map"), img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
218                 else
219                         drawpic(pos, pic, img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
220         }
221 }
222
223 void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float _count, int id)
224 {
225         TC(int, id);
226         vector img_size = '0 0 0';
227         string label;
228         float text_size;
229
230         float rect_margin = hud_fontsize.y / 2;
231
232         pos.x += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
233         pos.y += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
234         isize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
235         tsize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
236
237         vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
238         vector rect_size = '1 1 0';
239         rect_size.x = tsize + rect_margin;
240         rect_size.y = isize + rect_margin;
241
242         float img_ar = 4/3;
243         img_size.x = min(tsize, isize * img_ar);
244         img_size.y = img_size.x / img_ar;
245         img_size.y -= hud_fontsize.y;
246         img_size.x = img_size.y * img_ar;
247
248         pos.y += (isize - img_size.y - hud_fontsize.y) / 2;
249
250         label = MapVote_FormatMapItem(id, map, _count, tsize, hud_fontsize);
251
252         text_size = stringwidth(label, false, hud_fontsize);
253
254         float save_rect_sizex = rect_size.x;
255         rect_size.x = max(img_size.x, text_size) + rect_margin;
256         rect_pos.x += (save_rect_sizex - rect_size.x) / 2;
257
258         vector text_pos = '0 0 0';
259         text_pos.x = pos.x + (tsize - text_size) / 2;
260         text_pos.y = pos.y + img_size.y;
261
262         pos.x += (tsize - img_size.x) / 2;
263
264         float theAlpha;
265         if (!(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
266                 theAlpha = mv_top2_alpha;
267         else if (mv_winner && mv_winner_alpha)
268                 theAlpha = mv_winner_alpha;
269         else
270                 theAlpha = 1;
271         theAlpha *= panel_fg_alpha;
272
273         vector rgb = MapVote_RGB(id);
274
275         // Highlight selected item
276         if (!mv_winner)
277         {
278                 if(id == mv_selection && (mv_flags[id] & GTV_AVAILABLE))
279                         drawfill(rect_pos, rect_size, '1 1 1', 0.1 * panel_fg_alpha, DRAWFLAG_NORMAL);
280
281                 // Highlight current vote
282                 if(id == mv_ownvote)
283                 {
284                         drawfill(rect_pos, rect_size, rgb, 0.1 * theAlpha, DRAWFLAG_NORMAL);
285                         drawborderlines(autocvar_hud_panel_mapvote_highlight_border, rect_pos, rect_size, rgb, theAlpha, DRAWFLAG_NORMAL);
286                 }
287         }
288
289         drawstring(text_pos, label, hud_fontsize, rgb, theAlpha, DRAWFLAG_NORMAL);
290
291         MapVote_DrawMapPicture(pic, pos, img_size, theAlpha);
292 }
293
294 void MapVote_DrawAbstain(vector pos, float isize, float tsize, float _count, int id)
295 {
296         TC(int, id);
297         vector rgb;
298         float text_size;
299         string label;
300
301         rgb = MapVote_RGB(id);
302
303         label = MapVote_FormatMapItem(id, _("Don't care"), _count, tsize, hud_fontsize);
304
305         text_size = stringwidth(label, false, hud_fontsize);
306
307         pos.x -= text_size*0.5;
308         drawstring(pos, label, hud_fontsize, rgb, panel_fg_alpha, DRAWFLAG_NORMAL);
309 }
310
311 vector MapVote_GridVec(vector gridspec, int i, int m)
312 {
313         TC(int, i); TC(int, m);
314         int r = i % m;
315         return
316                 '1 0 0' * (gridspec.x * r)
317                 +
318                 '0 1 0' * (gridspec.y * (i - r) / m);
319 }
320
321 float MapVote_Selection(vector topleft, vector cellsize, float rows, float columns)
322 {
323         float c, r;
324         if (mv_winner)
325                 return -1;
326
327         mv_mouse_selection = -1;
328
329         for (r = 0; r < rows; ++r)
330                 for (c = 0; c < columns; ++c)
331                 {
332                         if (mousepos.x >= topleft.x + cellsize.x *  c &&
333                                 mousepos.x <= topleft.x + cellsize.x * (c + 1) &&
334                                 mousepos.y >= topleft.y + cellsize.y *  r &&
335                                 mousepos.y <= topleft.y + cellsize.y * (r + 1))
336                         {
337                                 mv_mouse_selection = r * columns + c;
338                                 break;
339                         }
340                 }
341
342         if (mv_mouse_selection >= mv_num_maps)
343                 mv_mouse_selection = -1;
344
345         if (mv_abstain && mv_mouse_selection < 0)
346                 mv_mouse_selection = mv_num_maps;
347
348         if ( mv_selection_keyboard )
349                 return mv_selection;
350
351         return mv_mouse_selection;
352 }
353
354 // draws map vote or gametype vote
355 void MapVote_Draw()
356 {
357         string map;
358         int i;
359         float tmp;
360         vector pos;
361         float center;
362         float rows;
363         vector dist = '0 0 0';
364
365         //if(intermission != 2) return;
366         if(!mv_active)
367                 return;
368
369         HUD_Panel_LoadCvars();
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         if (!mv_winner)
397         {
398                 pos.x = center - stringwidth(map, false, hud_fontsize * 2) * 0.5;
399                 drawstring(pos, map, hud_fontsize * 2, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
400         }
401         pos.y += hud_fontsize.y * 2;
402
403         if( mapvote_chosenmap != "" )
404         {
405                 pos.y += hud_fontsize.y * 0.25;
406                 pos.x = center - stringwidth(mapvote_chosenmap, false, hud_fontsize * 1.5) * 0.5;
407                 drawstring(pos, mapvote_chosenmap, hud_fontsize * 1.5, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
408                 pos.y += hud_fontsize.y * 1.5;
409         }
410         pos.y += hud_fontsize.y * 0.5;
411
412         draw_endBoldFont();
413
414         i = ceil(max(1, mv_timeout - time)); // make sure 0 seconds left never shows up, not even for a frame
415         if (mv_winner)
416                 map = mv_maps[mv_winner - 1];
417         else
418                 map = sprintf(_("%d seconds left"), i);
419         pos.x = center - stringwidth(map, false, hud_fontsize * 1.5) * 0.5;
420         drawstring(pos, map, hud_fontsize * 1.5, '0 1 0', panel_fg_alpha, DRAWFLAG_NORMAL);
421         pos.y += hud_fontsize.y * 1.5;
422         pos.y += hud_fontsize.y * 0.5;
423
424         // base for multi-column stuff...
425         pos.y += hud_fontsize.y;
426         pos.x = xmin;
427         ymin = pos.y;
428         float abstain_spacing = panel_bg_border + hud_fontsize.y;
429         if(mv_abstain)
430         {
431                 mv_num_maps -= 1;
432                 ymax -= abstain_spacing;
433         }
434
435         // higher than the image itself ratio for mapvote items to reserve space for long map names
436         int item_aspect = (gametypevote) ? 3/1 : 5/3;
437         vector table_size = HUD_GetTableSize_BestItemAR(mv_num_maps, vec2(xmax - xmin, ymax - ymin), item_aspect);
438         mv_columns = table_size.x;
439         rows = table_size.y;
440
441         dist.x = (xmax - xmin) / mv_columns;
442         dist.y = (ymax - pos.y) / rows;
443
444         // reduce size of too wide items
445         tmp = vid_conwidth / 3; // max width
446         if(dist.x > tmp)
447         {
448                 dist.x = tmp;
449                 dist.y = min(dist.y, dist.x / item_aspect);
450         }
451         tmp = vid_conheight / 3; // max height
452         if(dist.y > tmp)
453         {
454                 dist.y = tmp;
455                 dist.x = min(dist.x, dist.y * item_aspect);
456         }
457
458         // reduce size to fix aspect ratio
459         if(dist.x / dist.y > item_aspect)
460                 dist.x = dist.y * item_aspect;
461         else
462                 dist.y = dist.x / item_aspect;
463
464         // adjust table pos and size according to the new size
465         float offset;
466         offset = ((xmax - pos.x) - dist.x * mv_columns) / 2;
467         xmin = pos.x += offset;
468         xmax -= offset;
469         offset = ((ymax - pos.y) - dist.y * rows) / 2;
470         ymax -= 2 * offset;
471
472         // override panel_pos and panel_size
473         panel_pos.x = pos.x;
474         panel_pos.y = pos.y;
475         panel_size.x = xmax - xmin;
476         panel_size.y = ymax - ymin;
477         HUD_Panel_DrawBg();
478
479         if(panel_bg_padding)
480         {
481                 // FIXME item AR gets slightly changed here...
482                 // it's rather hard to avoid it at this point
483                 dist.x -= 2 * panel_bg_padding / mv_columns;
484                 dist.y -= 2 * panel_bg_padding / rows;
485                 xmin = pos.x += panel_bg_padding;
486                 ymin = pos.y += panel_bg_padding;
487                 xmax -= 2 * panel_bg_padding;
488                 ymax -= 2 * panel_bg_padding;
489         }
490
491         mv_selection = MapVote_Selection(pos, dist, rows, mv_columns);
492
493         if (mv_top2_time)
494                 mv_top2_alpha = max(0.2, 1 - (time - mv_top2_time) ** 2);
495
496         if (mv_winner_time)
497                 mv_winner_alpha = max(0.2, 1 - sqrt(max(0, time - mv_winner_time)));
498
499         void (vector, float, float, string, string, float, float) DrawItem;
500
501         if(gametypevote)
502                 DrawItem = GameTypeVote_DrawGameTypeItem;
503         else
504                 DrawItem = MapVote_DrawMapItem;
505
506         for(i = 0; i < mv_num_maps; ++i)
507         {
508                 tmp = mv_votes[i]; // FTEQCC bug: too many array accesses in the function call screw it up
509                 map = mv_maps[i];
510                 if(mv_preview[i])
511                         DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), dist.y, dist.x, map, mv_pics[i], tmp, i);
512                 else
513                         DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), dist.y, dist.x, map, "", tmp, i);
514         }
515
516         if(mv_abstain)
517                 ++mv_num_maps;
518
519         if(mv_abstain && i < mv_num_maps) {
520                 tmp = mv_votes[i];
521                 pos.y = ymax + abstain_spacing;
522                 pos.x = (xmax + xmin) * 0.5;
523                 MapVote_DrawAbstain(pos, dist.x, xmax - xmin, tmp, i);
524         }
525
526         if (mv_winner)
527         {
528                 // expand winner map image
529                 vector startsize = '0 0 0';
530                 startsize.y = vid_conheight * 0.1;
531                 startsize.x = startsize.y * 4/3;
532                 vector startpos = panel_pos + (panel_size - startsize) / 2;
533
534                 vector endsize = '0 0 0';
535                 endsize.y = vid_conheight * 0.5;
536                 endsize.x = endsize.y * 4/3;
537                 vector endpos = '0 0 0';
538                 endpos.y = panel_pos.y;
539                 endpos.x = (vid_conwidth - endsize.x) * 0.5;
540
541                 float f = bound(0, (time - mv_winner_time) * 2, 1);
542                 f = sqrt(f);
543                 float theAlpha = f;
544                 f = min(0.1 + f, 1);
545                 vector img_pos = endpos * f + startpos * (1 - f);
546                 vector img_size = endsize * f + startsize * (1 - f);
547
548                 MapVote_DrawMapPicture(mv_pics[mv_winner - 1], img_pos, img_size, theAlpha);
549         }
550 }
551
552 void Cmd_MapVote_MapDownload(int argc)
553 {
554         TC(int, argc);
555         entity pak;
556
557         if(argc != 2 || !mv_pk3list)
558         {
559                 LOG_INFO(_("mv_mapdownload: ^3You're not supposed to use this command on your own!"));
560                 return;
561         }
562
563         int id = stof(argv(1));
564         for(pak = mv_pk3list; pak; pak = pak.chain)
565                 if(pak.sv_entnum == id)
566                         break;
567
568         if(!pak || pak.sv_entnum != id) {
569                 LOG_INFO(_("^1Error:^7 Couldn't find pak index."));
570                 return;
571         }
572
573         if(PreviewExists(pak.message))
574         {
575                 mv_preview[id] = true;
576                 return;
577         } else {
578                 LOG_INFO(_("Requesting preview..."));
579                 localcmd(strcat("\ncmd mv_getpicture ", ftos(id), "\n"));
580         }
581 }
582
583 void MapVote_CheckPK3(string pic, string pk3, int id)
584 {
585         TC(int, id);
586         entity pak;
587         pak = spawn();
588         pak.netname = pk3;
589         pak.message = pic;
590         pak.sv_entnum = id;
591
592         pak.chain = mv_pk3list;
593         mv_pk3list = pak;
594
595         if(pk3 != "")
596         {
597                 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
598         }
599         else
600         {
601                 Cmd_MapVote_MapDownload(tokenize_console(strcat("mv_download ", ftos(id))));
602         }
603 }
604
605 void MapVote_CheckPic(string pic, string pk3, int id)
606 {
607         TC(int, id);
608         // never try to retrieve a pic for the "don't care" 'map'
609         if(mv_abstain && id == mv_num_maps - 1)
610                 return;
611
612         if(PreviewExists(pic))
613         {
614                 mv_preview[id] = true;
615                 return;
616         }
617         MapVote_CheckPK3(pic, pk3, id);
618 }
619
620 void MapVote_ReadMask()
621 {
622         int i;
623         if ( mv_num_maps < 24 )
624         {
625                 int mask;
626                 if(mv_num_maps < 8)
627                         mask = ReadByte();
628                 else if(mv_num_maps < 16)
629                         mask = ReadShort();
630                 else
631                         mask = ReadLong();
632
633                 for(i = 0; i < mv_num_maps; ++i)
634                 {
635                         if (mask & BIT(i))
636                                 mv_flags[i] |= GTV_AVAILABLE;
637                         else
638                                 mv_flags[i] &= ~GTV_AVAILABLE;
639                 }
640         }
641         else
642         {
643                 for(i = 0; i < mv_num_maps; ++i )
644                         mv_flags[i] = ReadByte();
645         }
646 }
647
648 void MapVote_ReadOption(int i)
649 {
650         TC(int, i);
651         string map = strzone(ReadString());
652         string pk3 = strzone(ReadString());
653         int j = bound(0, ReadByte(), n_ssdirs - 1);
654
655         mv_maps[i] = map;
656         mv_pk3[i] = pk3;
657         mv_flags[i] = GTV_AVAILABLE;
658
659         string pic = strzone(strcat(ssdirs[j], "/", map));
660         mv_pics[i] = pic;
661         mv_preview[i] = false;
662         MapVote_CheckPic(pic, pk3, i);
663 }
664
665 void GameTypeVote_ReadOption(int i)
666 {
667         TC(int, i);
668         string gt = strzone(ReadString());
669
670         mv_maps[i] = gt;
671         mv_flags[i] = ReadByte();
672
673         string basetype = "";
674
675         if ( mv_flags[i] & GTV_CUSTOM )
676         {
677                 string name = ReadString();
678                 if ( strlen(name) < 1 )
679                         name = gt;
680                 mv_pk3[i] = strzone(name);
681                 mv_desc[i] = strzone(ReadString());
682                 basetype = strzone(ReadString());
683         }
684         else
685         {
686                 Gametype type = MapInfo_Type_FromString(gt, false, false);
687                 mv_pk3[i] = strzone(MapInfo_Type_ToText(type));
688                 mv_desc[i] = MapInfo_Type_Description(type);
689         }
690
691         string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, gt);
692         if(precache_pic(mv_picpath) == "")
693         {
694                 mv_picpath = strcat("gfx/menu/default/gametype_", gt);
695                 if(precache_pic(mv_picpath) == "")
696                 {
697                         mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, basetype);
698                         if(precache_pic(mv_picpath) == "")
699                         {
700                                 mv_picpath = strcat("gfx/menu/default/gametype_", basetype);
701                         }
702                 }
703         }
704         string pic = strzone(mv_picpath);
705         mv_pics[i] = pic;
706         mv_preview[i] = PreviewExists(pic);
707 }
708
709 void MapVote_Init()
710 {
711         mv_active = 1;
712         if(!autocvar_hud_cursormode) mousepos = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
713         mv_selection = -1;
714         mv_selection_keyboard = 0;
715
716         string s;
717         for(n_ssdirs = 0; ; ++n_ssdirs)
718         {
719                 s = ReadString();
720                 if(s == "")
721                         break;
722                 if(n_ssdirs < NUM_SSDIRS)
723                         ssdirs[n_ssdirs] = s;
724         }
725         n_ssdirs = min(n_ssdirs, NUM_SSDIRS);
726
727         mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
728         mv_abstain = ReadByte();
729         if(mv_abstain)
730                 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
731         mv_detail = ReadByte();
732
733         mv_ownvote = -1;
734         mv_timeout = ReadCoord();
735
736         int gametypevote_flags = ReadByte();
737         gametypevote = boolean(gametypevote_flags & BIT(0));
738         if(gametypevote_flags)
739                 mapvote_chosenmap = strzone(ReadString());
740
741         MapVote_ReadMask();
742         int i;
743         for(i = 0; i < mv_num_maps; ++i )
744                 mv_flags_start[i] = mv_flags[i];
745
746         // Assume mv_pk3list is NULL, there should only be 1 mapvote per round
747         mv_pk3list = NULL; // I'm still paranoid!
748
749         for(i = 0; i < mv_num_maps; ++i)
750         {
751                 mv_votes[i] = 0;
752
753                 if ( gametypevote )
754                         GameTypeVote_ReadOption(i);
755                 else
756                         MapVote_ReadOption(i);
757         }
758
759         for(i = 0; i < n_ssdirs; ++i)
760                 ssdirs[n_ssdirs] = string_null;
761         n_ssdirs = 0;
762 }
763
764 void MapVote_SendChoice(int index)
765 {
766         TC(int, index);
767         localcmd(strcat("\nimpulse ", ftos(index+1), "\n"));
768 }
769
770 int MapVote_MoveLeft(int pos)
771 {
772         TC(int, pos);
773         int imp;
774         if ( pos < 0 )
775                 imp = mv_num_maps - 1;
776         else
777                 imp = pos < 1 ? mv_num_maps - 1 : pos - 1;
778         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
779                 imp = MapVote_MoveLeft(imp);
780         return imp;
781 }
782 int MapVote_MoveRight(int pos)
783 {
784         TC(int, pos);
785         int imp;
786         if ( pos < 0 )
787                 imp = 0;
788         else
789                 imp = pos >= mv_num_maps - 1 ? 0 : pos + 1;
790         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
791                 imp = MapVote_MoveRight(imp);
792         return imp;
793 }
794 int MapVote_MoveUp(int pos)
795 {
796         TC(int, pos);
797         int imp;
798         if ( pos < 0 )
799                 imp = mv_num_maps - 1;
800         else
801         {
802                 imp = pos - mv_columns;
803                 if ( imp < 0 )
804                 {
805                         int mv_rows = ceil(mv_num_maps / mv_columns);
806                         if (imp == -mv_columns) // pos == 0
807                                 imp = mv_columns * mv_rows - 1;
808                         else
809                                 imp = imp + mv_columns * mv_rows - 1;
810                 }
811         }
812         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
813                 imp = MapVote_MoveUp(imp);
814         return imp;
815 }
816 int MapVote_MoveDown(int pos)
817 {
818         TC(int, pos);
819         int imp;
820         if ( pos < 0 )
821                 imp = 0;
822         else
823         {
824                 imp = pos + mv_columns;
825                 if ( imp >= mv_num_maps )
826                 {
827                         if ((imp % mv_columns) == mv_columns - 1)
828                                 imp = 0;
829                         else
830                                 imp = imp % mv_columns + 1;
831                 }
832         }
833         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
834                 imp = MapVote_MoveDown(imp);
835         return imp;
836 }
837
838 float MapVote_InputEvent(int bInputType, float nPrimary, float nSecondary)
839 {
840         TC(int, bInputType);
841
842         static int first_digit = 0;
843         if (!mv_active || isdemo())
844                 return false;
845
846         if(bInputType == 3)
847         {
848                 mousepos.x = nPrimary;
849                 mousepos.y = nSecondary;
850                 mv_selection_keyboard = 0;
851                 return true;
852         }
853
854         if (bInputType == 2)
855         {
856                 mv_selection_keyboard = 0;
857                 return false;
858         }
859
860         // at this point bInputType can only be 0 or 1 (key pressed or released)
861         bool key_pressed = (bInputType == 0);
862
863         if (key_pressed)
864         {
865                 if (nPrimary == K_ALT) hudShiftState |= S_ALT;
866                 if (nPrimary == K_CTRL) hudShiftState |= S_CTRL;
867                 if (nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
868         }
869         else
870         {
871                 if (nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
872                 if (nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
873                 if (nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
874
875                 if (nPrimary == K_CTRL)
876                         first_digit = 0;
877         }
878
879         // Key release events must be handled by the engine otherwise the on-press command such as +jump
880         // executed by pressing SPACE before entering the map voting screen won't be followed by the
881         // on-release command (-jump) on key release once entered the map voting screen, causing +jump
882         // to stay active even on the next map and automatically forcing the player to join
883         if (!key_pressed) return false;
884
885         int imp = 0;
886         switch(nPrimary)
887         {
888                 case K_RIGHTARROW:
889                         if (mv_winner)
890                                 return true;
891                         mv_selection_keyboard = 1;
892                         mv_selection = MapVote_MoveRight(mv_selection);
893                         return true;
894                 case K_LEFTARROW:
895                         if (mv_winner)
896                                 return true;
897                         mv_selection_keyboard = 1;
898                         mv_selection = MapVote_MoveLeft(mv_selection);
899                         return true;
900                 case K_DOWNARROW:
901                         if (mv_winner)
902                                 return true;
903                         mv_selection_keyboard = 1;
904                         mv_selection = MapVote_MoveDown(mv_selection);
905                         return true;
906                 case K_UPARROW:
907                         if (mv_winner)
908                                 return true;
909                         mv_selection_keyboard = 1;
910                         mv_selection = MapVote_MoveUp(mv_selection);
911                         return true;
912                 case K_KP_ENTER:
913                 case K_ENTER:
914                 case K_SPACE:
915                         if (mv_winner)
916                                 return true;
917                         if ( mv_selection_keyboard )
918                                 MapVote_SendChoice(mv_selection);
919                         return true;
920                 case '1': case K_KP_1: imp = 1; break;
921                 case '2': case K_KP_2: imp = 2; break;
922                 case '3': case K_KP_3: imp = 3; break;
923                 case '4': case K_KP_4: imp = 4; break;
924                 case '5': case K_KP_5: imp = 5; break;
925                 case '6': case K_KP_6: imp = 6; break;
926                 case '7': case K_KP_7: imp = 7; break;
927                 case '8': case K_KP_8: imp = 8; break;
928                 case '9': case K_KP_9: imp = 9; break;
929                 case '0': case K_KP_0: imp = 10; break;
930         }
931
932         if (imp && hudShiftState & S_CTRL)
933         {
934                 if (!first_digit)
935                 {
936                         first_digit = imp % 10;
937                         return true;
938                 }
939                 else
940                         imp = first_digit * 10 + (imp % 10);
941         }
942
943         if (nPrimary == K_MOUSE1)
944         {
945                 mv_selection_keyboard = 0;
946                 mv_selection = mv_mouse_selection;
947                 if (mv_selection >= 0)
948                         imp = min(mv_selection + 1, mv_num_maps);
949         }
950
951         if (nPrimary == K_MOUSE2)
952                 return true; // do nothing
953
954         if (imp)
955         {
956                 if (!mv_winner && imp <= mv_num_maps)
957                         localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
958                 return true;
959         }
960
961         return false;
962 }
963
964 void MapVote_UpdateMask()
965 {
966         MapVote_ReadMask();
967         mv_top2_time = time;
968 }
969
970 void MapVote_UpdateVotes()
971 {
972         int i;
973         for(i = 0; i < mv_num_maps; ++i)
974         {
975                 if(mv_flags[i] & GTV_AVAILABLE)
976                 {
977                         if(mv_detail)
978                                 mv_votes[i] = ReadByte();
979                         else
980                                 mv_votes[i] = 0;
981                 }
982                 else
983                         mv_votes[i] = -1;
984         }
985
986         mv_ownvote = ReadByte()-1;
987 }
988
989 NET_HANDLE(ENT_CLIENT_MAPVOTE, bool isnew)
990 {
991         make_pure(this);
992         int sf = ReadByte();
993         return = true;
994
995         if(sf & 1)
996                 MapVote_Init();
997
998         if(sf & 2)
999                 MapVote_UpdateMask();
1000
1001         if(sf & 4)
1002                 MapVote_UpdateVotes();
1003
1004         if(sf & 8)
1005         {
1006                 mv_winner = ReadByte();
1007                 mv_winner_time = time;
1008         }
1009 }
1010
1011 NET_HANDLE(TE_CSQC_PICTURE, bool isNew)
1012 {
1013         Net_MapVote_Picture();
1014         return true;
1015 }
1016
1017 void Net_MapVote_Picture()
1018 {
1019         int type = ReadByte();
1020         mv_preview[type] = true;
1021         mv_pics[type] = strzone(ReadPicture());
1022 }