]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/mapvoting.qc
Merge branch 'drjaska/mayhem' 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 float gametypevote;
43 string mapvote_chosenmap;
44 vector gtv_text_size;
45 vector gtv_text_size_small;
46
47 const int NUM_SSDIRS = 4;
48 string ssdirs[NUM_SSDIRS];
49 int n_ssdirs;
50
51 bool PreviewExists(string name)
52 {
53         if(autocvar_cl_readpicture_force)
54                 return false;
55
56         if (fexists(strcat(name, ".tga"))) return true;
57         if (fexists(strcat(name, ".png"))) return true;
58         if (fexists(strcat(name, ".jpg"))) return true;
59         if (fexists(strcat(name, ".pcx"))) return true;
60
61         return false;
62 }
63
64 string MapVote_FormatMapItem(int id, string map, float _count, float maxwidth, vector fontsize)
65 {
66         TC(int, id);
67         string pre, post;
68         pre = sprintf("%d. ", id+1);
69         if(mv_detail)
70         {
71                 if(_count == 1)
72                         post = _(" (1 vote)");
73                 else if(_count >= 0 && (mv_flags[id] & GTV_AVAILABLE))
74                         post = sprintf(_(" (%d votes)"), _count);
75                 else
76                         post = "";
77         }
78         else
79                 post = "";
80         maxwidth -= stringwidth(pre, false, fontsize) + stringwidth(post, false, fontsize);
81         map = textShortenToWidth(map, maxwidth, fontsize, stringwidth_nocolors);
82         return strcat(pre, map, post);
83 }
84
85 vector MapVote_RGB(int id)
86 {
87         TC(int, id);
88         if(!(mv_flags[id] & GTV_AVAILABLE))
89                 return '1 1 1';
90         if(id == mv_ownvote)
91                 return '0 1 0';
92         else if (id == mv_selection)
93                 return '1 1 0';
94         else
95                 return '1 1 1';
96 }
97
98 void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, float _count, int id)
99 {
100         TC(int, id);
101         // Find the correct alpha
102         float alpha;
103         if(!(mv_flags_start[id] & GTV_AVAILABLE))
104                 alpha = 0.2; // The gametype isn't supported by the map
105         else if ( !(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
106                 alpha = mv_top2_alpha; // Fade away if not one of the top 2 choice
107         else
108                 alpha = 1; // Normal, full alpha
109         alpha *= panel_fg_alpha;
110
111         // Bounding box details
112         float rect_margin = hud_fontsize.y / 2;
113
114         pos.x += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
115         pos.y += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
116         maxh -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
117         tsize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
118
119         vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
120         vector rect_size = '1 1 0';
121         rect_size.x = tsize + rect_margin;
122         rect_size.y = maxh + rect_margin;
123
124         // Highlight selected item
125         if(id == mv_selection && (mv_flags[id] & GTV_AVAILABLE))
126         {
127                 drawfill(rect_pos, rect_size, '1 1 1', 0.1 * panel_fg_alpha, DRAWFLAG_NORMAL);
128         }
129
130         // Highlight current vote
131         vector rgb = MapVote_RGB(id);
132         if(id == mv_ownvote)
133         {
134                 drawfill(rect_pos, rect_size, rgb, 0.1 * alpha, DRAWFLAG_NORMAL);
135                 drawborderlines(autocvar_hud_panel_mapvote_highlight_border, rect_pos, rect_size, rgb, alpha, DRAWFLAG_NORMAL);
136         }
137
138         vector offset = pos;
139
140         float title_gap = gtv_text_size.y * 1.4; // distance between the title and the description
141         pos.y += title_gap;
142         maxh -= title_gap;
143
144         // Evaluate the image size
145         vector image_size = '1 1 0' * gtv_text_size.x * 3;
146         if ( maxh < image_size.y )
147                 image_size = '1 1 0' * maxh;
148         image_size *= 0.8;
149         float desc_padding = gtv_text_size.x * 0.6;
150         pos.x += image_size.x + desc_padding;
151         tsize -= image_size.x + desc_padding;
152
153         // Split the description into lines
154         entity title;
155         title = spawn();
156         title.message = MapVote_FormatMapItem(id, mv_pk3[id], _count, tsize, gtv_text_size);
157
158         string thelabel = mv_desc[id], ts;
159         entity last = title;
160         entity next = NULL;
161         float nlines = 0;
162         if( thelabel != "")
163         {
164                 float i,n = tokenizebyseparator(thelabel, "\n");
165                 for(i = 0; i < n && maxh > (nlines+1)*gtv_text_size_small.y; ++i)
166                 {
167                         getWrappedLine_remaining = argv(i);
168                         while(getWrappedLine_remaining && maxh > (nlines+1)*gtv_text_size_small.y)
169                         {
170                                 ts = getWrappedLine(tsize, gtv_text_size_small, stringwidth_colors);
171                                 if (ts != "")
172                                 {
173                                         next = spawn();
174                                         next.message = ts;
175                                         next.origin = pos-offset;
176                                         last.chain = next;
177                                         last = next;
178                                         pos.y += gtv_text_size_small.y;
179                                         nlines++;
180                                 }
181                         }
182                 }
183         }
184
185         // Center the contents in the bounding box
186         maxh -= max(nlines*gtv_text_size_small.y,image_size.y);
187         if ( maxh > 0 )
188                 offset.y += maxh/2;
189
190         // Draw the title
191         drawstring(offset, title.message, gtv_text_size, rgb, alpha, DRAWFLAG_NORMAL);
192
193         // Draw the icon
194         if(pic != "")
195                 drawpic('0 1 0'*title_gap+'0.5 0 0'*desc_padding+offset, pic, image_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
196
197         // Draw the description
198         for ( last = title.chain; last ; )
199         {
200                 drawstring(last.origin+offset, last.message, gtv_text_size_small, '1 1 1', alpha, DRAWFLAG_NORMAL);
201                 next = last;
202                 last = last.chain;
203                 delete(next);
204         }
205
206         // Cleanup
207         delete(title);
208 }
209
210 void MapVote_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         if (mv_winner)
516         {
517                 // expand winner map image
518                 vector startsize = '0 0 0';
519                 startsize.y = vid_conheight * 0.1;
520                 startsize.x = startsize.y * 4/3;
521                 vector startpos = panel_pos + (panel_size - startsize) / 2;
522
523                 vector endsize = '0 0 0';
524                 endsize.y = vid_conheight * 0.5;
525                 endsize.x = endsize.y * 4/3;
526                 vector endpos = '0 0 0';
527                 endpos.y = panel_pos.y;
528                 endpos.x = (vid_conwidth - endsize.x) * 0.5;
529
530                 float f = bound(0, (time - mv_winner_time) * 2, 1);
531                 f = sqrt(f);
532                 float theAlpha = f;
533                 f = min(0.1 + f, 1);
534                 vector img_pos = endpos * f + startpos * (1 - f);
535                 vector img_size = endsize * f + startsize * (1 - f);
536
537                 MapVote_DrawMapPicture(mv_pics[mv_winner - 1], img_pos, img_size, theAlpha);
538         }
539
540         if(mv_abstain)
541                 ++mv_num_maps;
542
543         if(mv_abstain && i < mv_num_maps) {
544                 tmp = mv_votes[i];
545                 pos.y = ymax + abstain_spacing;
546                 pos.x = (xmax+xmin)*0.5;
547                 MapVote_DrawAbstain(pos, dist.x, xmax - xmin, tmp, i);
548         }
549 }
550
551 void Cmd_MapVote_MapDownload(int argc)
552 {
553         TC(int, argc);
554         entity pak;
555
556         if(argc != 2 || !mv_pk3list)
557         {
558                 LOG_INFO(_("mv_mapdownload: ^3You're not supposed to use this command on your own!"));
559                 return;
560         }
561
562         int id = stof(argv(1));
563         for(pak = mv_pk3list; pak; pak = pak.chain)
564                 if(pak.sv_entnum == id)
565                         break;
566
567         if(!pak || pak.sv_entnum != id) {
568                 LOG_INFO(_("^1Error:^7 Couldn't find pak index."));
569                 return;
570         }
571
572         if(PreviewExists(pak.message))
573         {
574                 mv_preview[id] = true;
575                 return;
576         } else {
577                 LOG_INFO(_("Requesting preview..."));
578                 localcmd(strcat("\ncmd mv_getpicture ", ftos(id), "\n"));
579         }
580 }
581
582 void MapVote_CheckPK3(string pic, string pk3, int id)
583 {
584         TC(int, id);
585         entity pak;
586         pak = spawn();
587         pak.netname = pk3;
588         pak.message = pic;
589         pak.sv_entnum = id;
590
591         pak.chain = mv_pk3list;
592         mv_pk3list = pak;
593
594         if(pk3 != "")
595         {
596                 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
597         }
598         else
599         {
600                 Cmd_MapVote_MapDownload(tokenize_console(strcat("mv_download ", ftos(id))));
601         }
602 }
603
604 void MapVote_CheckPic(string pic, string pk3, int id)
605 {
606         TC(int, id);
607         // never try to retrieve a pic for the "don't care" 'map'
608         if(mv_abstain && id == mv_num_maps - 1)
609                 return;
610
611         if(PreviewExists(pic))
612         {
613                 mv_preview[id] = true;
614                 return;
615         }
616         MapVote_CheckPK3(pic, pk3, id);
617 }
618
619 void MapVote_ReadMask()
620 {
621         int i;
622         if ( mv_num_maps < 24 )
623         {
624                 int mask;
625                 if(mv_num_maps < 8)
626                         mask = ReadByte();
627                 else if(mv_num_maps < 16)
628                         mask = ReadShort();
629                 else
630                         mask = ReadLong();
631
632                 for(i = 0; i < mv_num_maps; ++i)
633                 {
634                         if (mask & BIT(i))
635                                 mv_flags[i] |= GTV_AVAILABLE;
636                         else
637                                 mv_flags[i] &= ~GTV_AVAILABLE;
638                 }
639         }
640         else
641         {
642                 for(i = 0; i < mv_num_maps; ++i )
643                         mv_flags[i] = ReadByte();
644         }
645 }
646
647 void MapVote_ReadOption(int i)
648 {
649         TC(int, i);
650         string map = strzone(ReadString());
651         string pk3 = strzone(ReadString());
652         int j = bound(0, ReadByte(), n_ssdirs - 1);
653
654         mv_maps[i] = map;
655         mv_pk3[i] = pk3;
656         mv_flags[i] = GTV_AVAILABLE;
657
658         string pic = strzone(strcat(ssdirs[j], "/", map));
659         mv_pics[i] = pic;
660         mv_preview[i] = false;
661         MapVote_CheckPic(pic, pk3, i);
662 }
663
664 void GameTypeVote_ReadOption(int i)
665 {
666         TC(int, i);
667         string gt = strzone(ReadString());
668
669         mv_maps[i] = gt;
670         mv_flags[i] = ReadByte();
671
672         string basetype = "";
673
674         if ( mv_flags[i] & GTV_CUSTOM )
675         {
676                 string name = ReadString();
677                 if ( strlen(name) < 1 )
678                         name = gt;
679                 mv_pk3[i] = strzone(name);
680                 mv_desc[i] = strzone(ReadString());
681                 basetype = strzone(ReadString());
682         }
683         else
684         {
685                 Gametype type = MapInfo_Type_FromString(gt, false, false);
686                 mv_pk3[i] = strzone(MapInfo_Type_ToText(type));
687                 mv_desc[i] = MapInfo_Type_Description(type);
688         }
689
690         string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, gt);
691         if(precache_pic(mv_picpath) == "")
692         {
693                 mv_picpath = strcat("gfx/menu/default/gametype_", gt);
694                 if(precache_pic(mv_picpath) == "")
695                 {
696                         mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, basetype);
697                         if(precache_pic(mv_picpath) == "")
698                         {
699                                 mv_picpath = strcat("gfx/menu/default/gametype_", basetype);
700                         }
701                 }
702         }
703         string pic = strzone(mv_picpath);
704         mv_pics[i] = pic;
705         mv_preview[i] = PreviewExists(pic);
706 }
707
708 void MapVote_Init()
709 {
710         mv_active = 1;
711         if(!autocvar_hud_cursormode) mousepos = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
712         mv_selection = -1;
713         mv_selection_keyboard = 0;
714
715         string s;
716         for(n_ssdirs = 0; ; ++n_ssdirs)
717         {
718                 s = ReadString();
719                 if(s == "")
720                         break;
721                 if(n_ssdirs < NUM_SSDIRS)
722                         ssdirs[n_ssdirs] = s;
723         }
724         n_ssdirs = min(n_ssdirs, NUM_SSDIRS);
725
726         mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
727         mv_abstain = ReadByte();
728         if(mv_abstain)
729                 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
730         mv_detail = ReadByte();
731
732         mv_ownvote = -1;
733         mv_timeout = ReadCoord();
734
735         gametypevote = ReadByte();
736
737         if(gametypevote)
738         {
739                 mapvote_chosenmap = strzone(ReadString());
740                 if ( gametypevote == 2 )
741                         gametypevote = 0;
742         }
743
744         MapVote_ReadMask();
745         int i;
746         for(i = 0; i < mv_num_maps; ++i )
747                 mv_flags_start[i] = mv_flags[i];
748
749         // Assume mv_pk3list is NULL, there should only be 1 mapvote per round
750         mv_pk3list = NULL; // I'm still paranoid!
751
752         for(i = 0; i < mv_num_maps; ++i)
753         {
754                 mv_votes[i] = 0;
755
756                 if ( gametypevote )
757                         GameTypeVote_ReadOption(i);
758                 else
759                         MapVote_ReadOption(i);
760         }
761
762         for(i = 0; i < n_ssdirs; ++i)
763                 ssdirs[n_ssdirs] = string_null;
764         n_ssdirs = 0;
765 }
766
767 void MapVote_SendChoice(int index)
768 {
769         TC(int, index);
770         localcmd(strcat("\nimpulse ", ftos(index+1), "\n"));
771 }
772
773 int MapVote_MoveLeft(int pos)
774 {
775         TC(int, pos);
776         int imp;
777         if ( pos < 0 )
778                 imp = mv_num_maps - 1;
779         else
780                 imp = pos < 1 ? mv_num_maps - 1 : pos - 1;
781         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
782                 imp = MapVote_MoveLeft(imp);
783         return imp;
784 }
785 int MapVote_MoveRight(int pos)
786 {
787         TC(int, pos);
788         int imp;
789         if ( pos < 0 )
790                 imp = 0;
791         else
792                 imp = pos >= mv_num_maps - 1 ? 0 : pos + 1;
793         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
794                 imp = MapVote_MoveRight(imp);
795         return imp;
796 }
797 int MapVote_MoveUp(int pos)
798 {
799         TC(int, pos);
800         int imp;
801         if ( pos < 0 )
802                 imp = mv_num_maps - 1;
803         else
804         {
805                 imp = pos - mv_columns;
806                 if ( imp < 0 )
807                 {
808                         int mv_rows = ceil(mv_num_maps / mv_columns);
809                         if (imp == -mv_columns) // pos == 0
810                                 imp = mv_columns * mv_rows - 1;
811                         else
812                                 imp = imp + mv_columns * mv_rows - 1;
813                 }
814         }
815         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
816                 imp = MapVote_MoveUp(imp);
817         return imp;
818 }
819 int MapVote_MoveDown(int pos)
820 {
821         TC(int, pos);
822         int imp;
823         if ( pos < 0 )
824                 imp = 0;
825         else
826         {
827                 imp = pos + mv_columns;
828                 if ( imp >= mv_num_maps )
829                 {
830                         if ((imp % mv_columns) == mv_columns - 1)
831                                 imp = 0;
832                         else
833                                 imp = imp % mv_columns + 1;
834                 }
835         }
836         if ( !(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote )
837                 imp = MapVote_MoveDown(imp);
838         return imp;
839 }
840
841 float MapVote_InputEvent(int bInputType, float nPrimary, float nSecondary)
842 {
843         TC(int, bInputType);
844
845         static int first_digit = 0;
846         if (!mv_active || isdemo())
847                 return false;
848
849         if(bInputType == 3)
850         {
851                 mousepos.x = nPrimary;
852                 mousepos.y = nSecondary;
853                 mv_selection_keyboard = 0;
854                 return true;
855         }
856
857         if (bInputType == 2)
858         {
859                 mv_selection_keyboard = 0;
860                 return false;
861         }
862
863         // at this point bInputType can only be 0 or 1 (key pressed or released)
864         bool key_pressed = (bInputType == 0);
865
866         if (key_pressed)
867         {
868                 if (nPrimary == K_ALT) hudShiftState |= S_ALT;
869                 if (nPrimary == K_CTRL) hudShiftState |= S_CTRL;
870                 if (nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
871         }
872         else
873         {
874                 if (nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
875                 if (nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
876                 if (nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
877
878                 if (nPrimary == K_CTRL)
879                         first_digit = 0;
880         }
881
882         // Key release events must be handled by the engine otherwise the on-press command such as +jump
883         // executed by pressing SPACE before entering the map voting screen won't be followed by the
884         // on-release command (-jump) on key release once entered the map voting screen, causing +jump
885         // to stay active even on the next map and automatically forcing the player to join
886         if (!key_pressed) return false;
887
888         int imp = 0;
889         switch(nPrimary)
890         {
891                 case K_RIGHTARROW:
892                         if (mv_winner)
893                                 return true;
894                         mv_selection_keyboard = 1;
895                         mv_selection = MapVote_MoveRight(mv_selection);
896                         return true;
897                 case K_LEFTARROW:
898                         if (mv_winner)
899                                 return true;
900                         mv_selection_keyboard = 1;
901                         mv_selection = MapVote_MoveLeft(mv_selection);
902                         return true;
903                 case K_DOWNARROW:
904                         if (mv_winner)
905                                 return true;
906                         mv_selection_keyboard = 1;
907                         mv_selection = MapVote_MoveDown(mv_selection);
908                         return true;
909                 case K_UPARROW:
910                         if (mv_winner)
911                                 return true;
912                         mv_selection_keyboard = 1;
913                         mv_selection = MapVote_MoveUp(mv_selection);
914                         return true;
915                 case K_KP_ENTER:
916                 case K_ENTER:
917                 case K_SPACE:
918                         if (mv_winner)
919                                 return true;
920                         if ( mv_selection_keyboard )
921                                 MapVote_SendChoice(mv_selection);
922                         return true;
923                 case '1': case K_KP_1: imp = 1; break;
924                 case '2': case K_KP_2: imp = 2; break;
925                 case '3': case K_KP_3: imp = 3; break;
926                 case '4': case K_KP_4: imp = 4; break;
927                 case '5': case K_KP_5: imp = 5; break;
928                 case '6': case K_KP_6: imp = 6; break;
929                 case '7': case K_KP_7: imp = 7; break;
930                 case '8': case K_KP_8: imp = 8; break;
931                 case '9': case K_KP_9: imp = 9; break;
932                 case '0': case K_KP_0: imp = 10; break;
933         }
934
935         if (imp && hudShiftState & S_CTRL)
936         {
937                 if (!first_digit)
938                 {
939                         first_digit = imp % 10;
940                         return true;
941                 }
942                 else
943                         imp = first_digit * 10 + (imp % 10);
944         }
945
946         if (nPrimary == K_MOUSE1)
947         {
948                 mv_selection_keyboard = 0;
949                 mv_selection = mv_mouse_selection;
950                 if (mv_selection >= 0)
951                         imp = min(mv_selection + 1, mv_num_maps);
952         }
953
954         if (nPrimary == K_MOUSE2)
955                 return true; // do nothing
956
957         if (imp)
958         {
959                 if (!mv_winner && imp <= mv_num_maps)
960                         localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
961                 return true;
962         }
963
964         return false;
965 }
966
967 void MapVote_UpdateMask()
968 {
969         MapVote_ReadMask();
970         mv_top2_time = time;
971 }
972
973 void MapVote_UpdateVotes()
974 {
975         int i;
976         for(i = 0; i < mv_num_maps; ++i)
977         {
978                 if(mv_flags[i] & GTV_AVAILABLE)
979                 {
980                         if(mv_detail)
981                                 mv_votes[i] = ReadByte();
982                         else
983                                 mv_votes[i] = 0;
984                 }
985                 else
986                         mv_votes[i] = -1;
987         }
988
989         mv_ownvote = ReadByte()-1;
990 }
991
992 NET_HANDLE(ENT_CLIENT_MAPVOTE, bool isnew)
993 {
994         make_pure(this);
995         int sf = ReadByte();
996         return = true;
997
998         if(sf & 1)
999                 MapVote_Init();
1000
1001         if(sf & 2)
1002                 MapVote_UpdateMask();
1003
1004         if(sf & 4)
1005                 MapVote_UpdateVotes();
1006
1007         if(sf & 8)
1008         {
1009                 mv_winner = ReadByte();
1010                 mv_winner_time = time;
1011         }
1012 }
1013
1014 NET_HANDLE(TE_CSQC_PICTURE, bool isNew)
1015 {
1016         Net_MapVote_Picture();
1017         return true;
1018 }
1019
1020 void Net_MapVote_Picture()
1021 {
1022         int type = ReadByte();
1023         mv_preview[type] = true;
1024         mv_pics[type] = strzone(ReadPicture());
1025 }