Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
mapvoting.qc
Go to the documentation of this file.
1#include "mapvoting.qh"
2
3#include <client/draw.qh>
4#include <client/hud/_mod.qh>
6#include <common/mapinfo.qh>
7#include <common/util.qh>
8
9// MapVote (#21)
10
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
18
19// These are mostly the same as server/mapvoting.qc, or otherwise intuitive
20string mv_entries[MAPVOTE_COUNT]; // map name or gametype name
22string mv_data[MAPVOTE_COUNT]; // map pk3 name or gametype human readable name
42
47
52
53const float MV_FADETIME = 0.2;
54
55const int NUM_SSDIRS = 4;
58
59bool PreviewExists(string name)
60{
62 return false;
63
64 if (fexists(strcat(name, ".tga"))) return true;
65 if (fexists(strcat(name, ".png"))) return true;
66 if (fexists(strcat(name, ".jpg"))) return true;
67 if (fexists(strcat(name, ".pcx"))) return true;
68
69 return false;
70}
71
72string MapVote_FormatMapItem(int id, string map, int _count, float maxwidth, vector fontsize, int most_votes)
73{
74 TC(int, id);
75 string post;
76 string pre = sprintf("%d. ", id + 1);
77 if (mv_detail)
78 {
79 if (_count == 1)
80 post = _(" (1 vote)");
81 else if (_count >= 0 && (mv_flags[id] & GTV_AVAILABLE))
82 post = sprintf(_(" (%d votes)"), _count);
83 else
84 post = "";
85 if (post != "" && (mv_flags[id] & GTV_AVAILABLE))
86 if (mv_tie_winner == id || (mv_tie_winner == -2 && _count == most_votes))
87 post = strcat("^5", post);
88 }
89 else
90 post = "";
91 maxwidth -= stringwidth(pre, false, fontsize) + stringwidth(post, true, fontsize);
92 map = textShortenToWidth(map, maxwidth, fontsize, stringwidth_nocolors);
93 return strcat(pre, map, post);
94}
95
97{
98 TC(int, id);
99
100 vector rgb;
101 if (!(mv_flags[id] & GTV_AVAILABLE))
102 rgb = '1 1 1';
103 else if (id == mv_ownvote)
104 rgb = '0 1 0';
105 else if (id == mv_selection)
106 rgb = '1 1 0';
107 else
108 {
109 const float time_frac = (time - mv_select_lasttime[id]) / MV_FADETIME;
110 rgb = '1 1 0' + eZ * (time_frac >= 1 ? 1 : sqrt(time_frac));
111 }
112 return rgb;
113}
114
115void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, int _count, int id, int most_votes)
116{
117 TC(int, id);
118 // gtype is unused
119
120 // Find the correct alpha
121 float alpha;
122 if (!(mv_flags_start[id] & GTV_AVAILABLE))
123 alpha = 0.2; // The gametype isn't supported by the map
124 else if (!(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
125 alpha = mv_top2_alpha; // Fade away if not one of the top 2 choice
126 else
127 alpha = 1; // Normal, full alpha
129
130 // Bounding box details
131 const float rect_margin = hud_fontsize.y * 0.5;
132
133 pos.x += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
134 pos.y += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
135 maxh -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
136 tsize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
137
138 vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
139 vector rect_size = '1 1 0';
140 rect_size.x = tsize + rect_margin;
141 rect_size.y = maxh + rect_margin;
142
143 const vector rgb = MapVote_RGB(id);
144 float time_frac;
145 if (id == mv_ownvote)
146 {
147 // Highlight current vote
148 time_frac = 0;
149 mv_select_lasttime[id] = time; // pretend it's selected, to also enlarge icon
150 drawfill(rect_pos, rect_size, rgb, 0.1 * alpha, DRAWFLAG_NORMAL);
152 }
153 else if (mv_flags[id] & GTV_AVAILABLE)
154 {
155 // Highlight selected item
156 if (id == mv_selection)
157 time_frac = 0;
158 else
159 time_frac = (time - mv_select_lasttime[id]) / MV_FADETIME;
160 if (time_frac < 1)
161 drawfill(rect_pos, rect_size, '1 1 1', 0.1 * (1 - sqrt(time_frac)) * panel_fg_alpha, DRAWFLAG_NORMAL);
162 }
163 else
164 time_frac = 1;
165
166 vector offset = pos;
167
168 const float title_gap = gtv_text_size.y * 1.4; // distance between the title and the description
169 pos.y += title_gap;
170 maxh -= title_gap;
171
172 // Evaluate the image size
173 vector image_size = '3 3 0' * gtv_text_size.x;
174 if (maxh < image_size.y)
175 image_size = '1 1 0' * maxh;
176 image_size *= 0.8;
177 const float desc_padding = gtv_text_size.x * 0.6;
178 pos.x += image_size.x + desc_padding;
179
180 // Split the description into lines
181 entity title = spawn();
182 title.message = strcat(rgb_to_hexcolor(rgb), MapVote_FormatMapItem(id, mv_data[id], _count, tsize, gtv_text_size, most_votes));
183 tsize -= image_size.x + desc_padding;
184
185 string thelabel = mv_desc[id], ts;
186 entity last = title;
187 entity next = NULL;
188 int nlines = 0;
189 if (thelabel != "")
190 {
191 const int n = tokenizebyseparator(thelabel, "\n");
192 for (int i = 0; i < n && maxh > (nlines + 1) * gtv_text_size_small.y; ++i)
193 {
195 while (getWrappedLine_remaining && maxh > (nlines + 1) * gtv_text_size_small.y)
196 {
198 if (ts != "")
199 {
200 next = spawn();
201 next.message = ts;
202 next.origin = pos - offset;
203 last.chain = next;
204 last = next;
205 pos.y += gtv_text_size_small.y;
206 ++nlines;
207 }
208 }
209 }
210 }
211
212 // Center the contents in the bounding box
213 maxh -= max(nlines * gtv_text_size_small.y, image_size.y);
214 if (maxh > 0)
215 offset.y += maxh * 0.5;
216
217 // Draw the title
219
220 // Draw the icon
221 if (pic != "")
222 {
223 vector offset_padding;
224 if (time_frac >= 0 && time_frac < 1)
225 {
226 const float extra_padding = min(desc_padding, title_gap);
227 const float transition_progress = (1 - sqrt(time_frac));
228 image_size *= transition_progress * (extra_padding / image_size.x) + 1;
229 offset_padding = transition_progress * extra_padding * '-0.5 -0.5 0';
230 }
231 else
232 offset_padding = '0 0 0';
233 drawpic(eY * title_gap + eX * 0.5 * desc_padding + offset + offset_padding, pic, image_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
234 }
235
236 // Draw the description
237 last = title.chain;
238 while (last)
239 {
240 drawstring(last.origin + offset, last.message, gtv_text_size_small, '1 1 1', alpha, DRAWFLAG_NORMAL);
241 next = last;
242 last = last.chain;
243 delete(next);
244 }
245
246 // Cleanup
247 delete(title);
248}
249
250void MapVote_DrawMapPicture(string pic, vector pos, vector img_size, float theAlpha)
251{
252 if (pic == "")
253 drawfill(pos, img_size, '0.5 0.5 0.5', 0.7 * theAlpha, DRAWFLAG_NORMAL);
254 else
255 {
256 if (drawgetimagesize(pic) == '0 0 0')
257 drawpic(pos, draw_UseSkinFor("nopreview_map"), img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
258 else
259 drawpic(pos, pic, img_size, '1 1 1', theAlpha, DRAWFLAG_NORMAL);
260 }
261}
262
263void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, int _count, int id, int most_votes)
264{
265 TC(int, id);
266
267 const float rect_margin = hud_fontsize.y * 0.5;
268 pos.x += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
269 pos.y += rect_margin + autocvar_hud_panel_mapvote_highlight_border;
270 isize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
271 tsize -= 2 * (rect_margin + autocvar_hud_panel_mapvote_highlight_border);
272
273 vector rect_pos = pos - '0.5 0.5 0' * rect_margin;
274 vector rect_size = '1 1 0';
275 rect_size.x = tsize + rect_margin;
276 rect_size.y = isize + rect_margin;
277
278 const float img_ar = 4/3;
279 vector img_size = '0 0 0';
280 img_size.x = min(tsize, isize * img_ar);
281 img_size.y = img_size.x / img_ar;
282 img_size.y -= hud_fontsize.y;
283 img_size.x = img_size.y * img_ar;
284
285 pos.y += (isize - img_size.y - hud_fontsize.y) * 0.5;
286
287 const vector rgb = MapVote_RGB(id);
288 const string label = strcat(rgb_to_hexcolor(rgb), MapVote_FormatMapItem(id, map, _count, tsize, hud_fontsize, most_votes));
289
290 const float text_size = stringwidth(label, true, hud_fontsize);
291
292 const float save_rect_sizex = rect_size.x;
293 rect_size.x = max(img_size.x, text_size) + rect_margin;
294 rect_pos.x += (save_rect_sizex - rect_size.x) * 0.5;
295
296 vector text_pos;
297 text_pos.x = pos.x + (tsize - text_size) * 0.5;
298 text_pos.y = pos.y + img_size.y;
299 text_pos.z = 0;
300
301 pos.x += (tsize - img_size.x) * 0.5;
302
303 float theAlpha;
304 if (!(mv_flags[id] & GTV_AVAILABLE) && mv_top2_alpha)
305 theAlpha = mv_top2_alpha;
306 else if (mv_winner && mv_winner_alpha)
307 theAlpha = mv_winner_alpha;
308 else
309 theAlpha = 1;
310 theAlpha *= panel_fg_alpha;
311
312 if (!mv_winner)
313 {
314 if (id == mv_ownvote)
315 {
316 // Highlight current vote
317 drawfill(rect_pos, rect_size, rgb, 0.1 * theAlpha, DRAWFLAG_NORMAL);
319 }
320 else if (mv_flags[id] & GTV_AVAILABLE)
321 {
322 // Highlight selected item
323 float time_frac;
324 if (id == mv_selection)
325 time_frac = 0;
326 else
327 time_frac = (time - mv_select_lasttime[id]) / MV_FADETIME;
328 if (time_frac < 1)
329 drawfill(rect_pos, rect_size, '1 1 1', 0.1 * (1 - sqrt(time_frac)) * panel_fg_alpha, DRAWFLAG_NORMAL);
330 }
331 }
332
333 drawcolorcodedstring(text_pos, label, hud_fontsize, theAlpha, DRAWFLAG_NORMAL);
334
335 MapVote_DrawMapPicture(pic, pos, img_size, theAlpha);
336}
337
338void MapVote_DrawAbstain(vector pos, float tsize, int _count, int id)
339{
340 const string label = MapVote_FormatMapItem(id, _("Don't care"), _count, tsize, hud_fontsize, -1);
341
342 float theAlpha = (mv_winner && mv_winner_alpha)
344 : 1;
345 theAlpha *= panel_fg_alpha;
346 pos.x -= 0.5 * stringwidth(label, false, hud_fontsize);
347 drawstring(pos, label, hud_fontsize, MapVote_RGB(id), theAlpha, DRAWFLAG_NORMAL);
348}
349
351{
352 const int id = mv_winner ? mv_winner - 1 : mv_selection;
353
354 float theAlpha = 0;
355 if (id != -1 && (mv_flags[id] & GTV_AVAILABLE)) // if the map is valid, update cache and time
356 {
357 if (mv_suggester_cache == "" && mv_suggester[id] == "")
358 return;
359 if (mv_suggester[id] != "") // update cache to current, set time
360 {
363 theAlpha = 1;
364 }
365 }
366 if (mv_suggester_cache != "" && theAlpha != 1) // figure out alpha for cache if it's partly-faded
367 {
368 const float time_frac = (time - mv_suggester_cachetime) / MV_FADETIME;
369 if (time_frac >= 1) // finished fading out, delete cache
370 {
372 return;
373 }
374 theAlpha = (1 - sqrt(time_frac));
375 }
376 if (!theAlpha)
377 return;
378
379 const string label = sprintf(_("Suggested by: %s"), ColorTranslateRGB(mv_suggester_cache));
380
381 theAlpha *= panel_fg_alpha;
382 pos.x -= 0.5 * stringwidth(label, true, hud_fontsize);
383 drawcolorcodedstring(pos, label, hud_fontsize, theAlpha, DRAWFLAG_NORMAL);
384}
385
386vector MapVote_GridVec(vector gridspec, int i, int m)
387{
388 TC(int, i); TC(int, m);
389 const int r = i % m;
390 return
391 eX * (gridspec.x * r)
392 +
393 eY * (gridspec.y * (i - r) / m);
394}
395
396float MapVote_Selection(vector topleft, vector cellsize, float rows, float columns)
397{
398 if (mv_winner)
399 return -1;
400
402
403 for (float c, r = 0; r < rows; ++r)
404 for (c = 0; c < columns; ++c)
405 if (mousepos.x >= topleft.x + cellsize.x * c
406 && mousepos.x <= topleft.x + cellsize.x * (c + 1)
407 && mousepos.y >= topleft.y + cellsize.y * r
408 && mousepos.y <= topleft.y + cellsize.y * (r + 1))
409 {
410 mv_mouse_selection = r * columns + c;
411 break;
412 }
413
418
420 return mv_selection;
421 return mv_mouse_selection;
422}
423
424// draws map vote or gametype vote
425void MapVote_Draw(bool should_draw)
426{
427 if (!should_draw)
428 return;
429 //if (intermission != 2) return;
430 if (!mv_active)
431 return;
432
434
435 const float center = (vid_conwidth - 1) * 0.5;
436 xmin = vid_conwidth * (gametypevote ? 0.08 : 0.1);
438 ymin = 20;
440
441 if (chat_posy + chat_sizey * 0.5 < vid_conheight * 0.5)
442 ymin += chat_sizey;
443 else
444 ymax -= chat_sizey;
445
446 hud_fontsize = HUD_GetFontsize("hud_fontsize");
447 if (gametypevote)
448 {
451 }
452
453 vector pos;
454 pos.y = ymin;
455 pos.z = 0;
456
459
460 string map = (gametypevote) ? _("Decide the gametype") : _("Vote for a map");
461 if (!mv_winner)
462 {
463 pos.x = center - stringwidth(map, false, hud_fontsize * 2) * 0.5;
464 drawstring(pos, map, hud_fontsize * 2, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
465 }
466 pos.y += hud_fontsize.y * 2;
467
468 if (mapvote_chosenmap != "")
469 {
470 pos.y += hud_fontsize.y * 0.25;
471 pos.x = center - stringwidth(mapvote_chosenmap, false, hud_fontsize * 1.5) * 0.5;
473 pos.y += hud_fontsize.y * 1.5;
474 }
475 pos.y += hud_fontsize.y * 0.5;
476
478
479 int i;
480 if (mv_winner)
481 map = mv_entries[mv_winner - 1];
482 else
483 {
484 i = ceil(max(1, mv_timeout - time)); // make sure 0 seconds left never shows up, not even for a frame
485 map = count_seconds(i);
486 }
487 pos.x = center - stringwidth(map, false, hud_fontsize * 1.5) * 0.5;
488 drawstring(pos, map, hud_fontsize * 1.5, '0 1 0', panel_fg_alpha, DRAWFLAG_NORMAL);
489 pos.y += hud_fontsize.y * 1.5;
490 pos.y += hud_fontsize.y * 0.5;
491
492 // base for multi-column stuff...
493 pos.y += hud_fontsize.y;
494 pos.x = xmin;
495 ymin = pos.y;
496 const float abstain_spacing = panel_bg_border + hud_fontsize.y;
497 const float suggester_spacing = hud_fontsize.y * 1.5;
498 if (mv_abstain)
499 {
500 --mv_num_maps;
501 ymax -= abstain_spacing;
502 }
503 const bool were_suggesters = (mv_suggester[0] != ""); // suggestions are placed at the start
504 if (were_suggesters)
505 ymax -= suggester_spacing;
506
507 // higher than the image itself ratio for mapvote items to reserve space for long map names
508 const float item_aspect = (gametypevote) ? 3/1 : 5/3;
509 const vector table_size = HUD_GetTableSize_BestItemAR(mv_num_maps, vec2(xmax - xmin, ymax - ymin), item_aspect);
510 mv_columns = table_size.x;
511 const float rows = table_size.y;
512
513 vector dist;
514 dist.x = (xmax - xmin) / mv_columns;
515 dist.y = (ymax - pos.y) / rows;
516 dist.z = 0;
517
518 // reduce size of too wide items
519 float tmp = vid_conwidth / 3; // max width
520 if (dist.x > tmp)
521 {
522 dist.x = tmp;
523 dist.y = min(dist.y, dist.x / item_aspect);
524 }
525 tmp = vid_conheight / 3; // max height
526 if (dist.y > tmp)
527 {
528 dist.y = tmp;
529 dist.x = min(dist.x, dist.y * item_aspect);
530 }
531
532 // reduce size to fix aspect ratio
533 if (dist.x / dist.y > item_aspect)
534 dist.x = dist.y * item_aspect;
535 else
536 dist.y = dist.x / item_aspect;
537
538 // adjust table pos and size according to the new size
539 float offset = ((xmax - pos.x) - dist.x * mv_columns) * 0.5;
540 xmin = pos.x += offset;
541 xmax -= offset;
542 offset = ((ymax - pos.y) - dist.y * rows) * 0.5;
543 ymax -= 2 * offset;
544
545 // override panel_pos and panel_size
546 panel_pos.x = pos.x;
547 panel_pos.y = pos.y;
548 panel_size.x = xmax - xmin;
549 panel_size.y = ymax - ymin;
551
553 {
554 // FIXME item AR gets slightly changed here...
555 // it's rather hard to avoid it at this point
556 dist.x -= 2 * panel_bg_padding / mv_columns;
557 dist.y -= 2 * panel_bg_padding / rows;
558 xmin = pos.x += panel_bg_padding;
559 ymin = pos.y += panel_bg_padding;
560 xmax -= 2 * panel_bg_padding;
561 ymax -= 2 * panel_bg_padding;
562 }
563
564 mv_selection = MapVote_Selection(pos, dist, rows, mv_columns);
565 if (mv_selection != -1)
567
568 if (mv_top2_time)
569 mv_top2_alpha = max(0.2, 1 - (time - mv_top2_time) ** 2);
570
571 if (mv_winner_time)
572 mv_winner_alpha = max(0.2, 1 - sqrt(max(0, time - mv_winner_time)));
573
574 int most_votes = -1;
575 if (mv_tie_winner == -2)
576 for (i = 0; i < mv_num_maps; ++i)
577 if (mv_votes[i] > most_votes)
578 most_votes = mv_votes[i];
579
580 void(vector, float, float, string, string, float, int, int) DrawItem;
581 if (gametypevote)
583 else
584 DrawItem = MapVote_DrawMapItem;
585
586 for (i = 0; i < mv_num_maps; ++i)
587 {
588 tmp = mv_votes[i]; // FTEQCC bug: too many array accesses in the function call screw it up
589 map = mv_entries[i];
590 const string pic = mv_preview[i] ? mv_pics[i] : "";
591 DrawItem(pos + MapVote_GridVec(dist, i, mv_columns), dist.y, dist.x, map, pic, tmp, i, most_votes);
592 }
593
594 if (mv_abstain)
595 ++mv_num_maps;
596
597 pos.y = ymax + abstain_spacing;
598 if (mv_abstain && i < mv_num_maps)
599 {
600 tmp = mv_votes[i];
601 pos.x = center;
602 MapVote_DrawAbstain(pos, xmax - xmin, tmp, i);
603 pos.y += suggester_spacing;
604 }
605
606 if (mv_winner)
607 {
608 // expand winner map image
609 vector startsize;
610 startsize.z = 0;
611 startsize.y = vid_conheight * 0.1;
612 startsize.x = startsize.y * 4/3;
613 const vector startpos = panel_pos + (panel_size - startsize) * 0.5;
614
615 vector endsize;
616 endsize.z = 0;
617 endsize.y = vid_conheight * 0.5;
618 endsize.x = endsize.y * 4/3;
619 vector endpos;
620 endpos.z = 0;
621 endpos.y = panel_pos.y;
622 endpos.x = (vid_conwidth - endsize.x) * 0.5;
623
624 float f = bound(0, sqrt((time - mv_winner_time) * 2), 1);
625 const float theAlpha = f;
626 f = min(0.1 + f, 1);
627 vector img_size = endsize * f + startsize * (1 - f);
628 vector img_pos = endpos * f + startpos * (1 - f);
629
630 MapVote_DrawMapPicture(mv_pics[mv_winner - 1], img_pos, img_size, theAlpha);
631
632 if (were_suggesters)
633 {
634 const float suggester_startposy = pos.y;
635 const float suggester_endposy = panel_pos.y + endsize.y + hud_fontsize.y;
636 pos.y = suggester_endposy * f + suggester_startposy * (1 - f);
637 }
638 }
639
640 if (were_suggesters)
641 {
642 pos.x = center;
644 }
645}
646
648{
649 TC(int, argc);
650
651 if (argc != 2 || !mv_pk3list)
652 {
653 LOG_INFO(_("mv_mapdownload: ^3You're not supposed to use this command on your own!"));
654 return;
655 }
656
657 entity pak;
658 const int id = stof(argv(1));
659 for (pak = mv_pk3list; pak; pak = pak.chain)
660 if (pak.sv_entnum == id)
661 break;
662
663 if (!pak || pak.sv_entnum != id)
664 {
665 LOG_INFO(_("^1Error:^7 Couldn't find pak index."));
666 return;
667 }
668
669 if (PreviewExists(pak.message))
670 {
671 mv_preview[id] = true;
672 return;
673 }
674 else
675 {
676 LOG_INFO(_("Requesting preview..."));
677 localcmd("\ncmd mv_getpicture ", ftos(id), "\n");
678 }
679}
680
681void MapVote_CheckPK3(string pic, string pk3, int id)
682{
683 TC(int, id);
684 entity pak = spawn();
685 pak.message = pic;
686 pak.netname = pk3;
687 pak.sv_entnum = id;
688
689 pak.chain = mv_pk3list;
690 mv_pk3list = pak;
691
692 if (pk3 != "")
693 localcmd("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", itos(id), "\n");
694 else
696}
697
698void MapVote_CheckPic(string pic, string pk3, int id)
699{
700 TC(int, id);
701 // never try to retrieve a pic for the "don't care" 'map'
702 if (mv_abstain && id == mv_num_maps - 1)
703 return;
704
705 if (PreviewExists(pic))
706 {
707 mv_preview[id] = true;
708 return;
709 }
710 MapVote_CheckPK3(pic, pk3, id);
711}
712
714{
715 int i;
716 if (mv_num_maps < 24)
717 {
718 int mask;
719 if (mv_num_maps < 8)
720 mask = ReadByte();
721 else if (mv_num_maps < 16)
722 mask = ReadShort();
723 else
724 mask = ReadLong();
725
726 for (i = 0; i < mv_num_maps; ++i)
727 {
728 if (mask & BIT(i))
730 else
732 }
733 }
734 else
735 {
736 for (i = 0; i < mv_num_maps; ++i)
737 mv_flags[i] = ReadByte();
738 }
739}
740
742{
743 TC(int, i);
744
746 mv_data[i] = strzone(ReadString());
749 const int j = bound(0, ReadByte(), n_ssdirs - 1);
750
751 mv_pics[i] = strzone(strcat(ssdirs[j], "/", mv_entries[i]));
752 mv_preview[i] = false;
754}
755
757{
758 TC(int, i);
759
761 mv_flags[i] = ReadByte();
762 mv_suggester[i] = "";
763
764 string basetype = "";
765
766 if (mv_flags[i] & GTV_CUSTOM)
767 {
768 string name = ReadString();
769 if (strlen(name) < 1)
770 name = mv_entries[i];
771 mv_data[i] = strzone(name);
772 mv_desc[i] = strzone(ReadString());
773 basetype = strzone(ReadString());
774 }
775 else
776 {
777 const Gametype type = MapInfo_Type_FromString(mv_entries[i], false, false);
780 }
781
782 string mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, mv_entries[i]);
783 if (precache_pic(mv_picpath) == "")
784 {
785 mv_picpath = strcat("gfx/menu/wickedx/gametype_", mv_entries[i]);
786 if (precache_pic(mv_picpath) == "")
787 {
788 mv_picpath = sprintf("gfx/menu/%s/gametype_%s", autocvar_menu_skin, basetype);
789 if (precache_pic(mv_picpath) == "")
790 mv_picpath = strcat("gfx/menu/wickedx/gametype_", basetype);
791 }
792 }
793 mv_pics[i] = strzone(mv_picpath);
795}
796
798{
799 mv_active = true;
801 mousepos = (eX * vid_conwidth + eY * vid_conheight) * 0.5;
802 mv_selection = -1;
803 mv_selection_keyboard = false;
804
805 string s;
806 for (n_ssdirs = 0; ; ++n_ssdirs)
807 {
808 s = ReadString();
809 if (s == "")
810 break;
811 if (n_ssdirs < NUM_SSDIRS)
812 ssdirs[n_ssdirs] = s;
813 }
815
819
822
823 mv_ownvote = -1;
824 mv_timeout = ReadCoord();
825
826 const int gametypevote_flags = ReadByte();
827 gametypevote = boolean(gametypevote_flags & BIT(0));
828 if (gametypevote_flags)
830
832 int i;
833 for (i = 0; i < mv_num_maps; ++i)
834 mv_flags_start[i] = mv_flags[i];
835
836 // Assume mv_pk3list is NULL, there should only be 1 mapvote per round
837 mv_pk3list = NULL; // I'm still paranoid!
838
839 for (i = 0; i < mv_num_maps; ++i)
840 {
841 mv_votes[i] = 0;
842 mv_select_lasttime[i] = 0;
843
844 if (gametypevote)
846 else
848 }
849
850 for (i = 0; i < n_ssdirs; ++i)
852 n_ssdirs = 0;
853}
854
855void MapVote_SendChoice(int index)
856{
857 TC(int, index);
858 localcmd("\nimpulse ", ftos(index + 1), "\n");
859}
860
862{
863 TC(int, pos);
864 int imp;
865 if (pos < 0)
866 imp = mv_num_maps - 1;
867 else
868 imp = pos < 1 ? mv_num_maps - 1 : pos - 1;
869 if (!(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote)
871 return imp;
872}
874{
875 TC(int, pos);
876 int imp;
877 if (pos < 0)
878 imp = 0;
879 else
880 imp = pos >= mv_num_maps - 1 ? 0 : pos + 1;
881 if (!(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote)
883 return imp;
884}
885int MapVote_MoveUp(int pos)
886{
887 TC(int, pos);
888 int imp;
889 if (pos < 0)
890 imp = mv_num_maps - 1;
891 else
892 {
893 imp = pos - mv_columns;
894 if (imp < 0)
895 {
896 const int mv_rows = ceil(mv_num_maps / mv_columns);
897 if (imp == -mv_columns) // pos == 0
898 imp = mv_columns * mv_rows - 1;
899 else
900 imp += mv_columns * mv_rows - 1;
901 }
902 }
903 if (!(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote)
905 return imp;
906}
908{
909 TC(int, pos);
910 int imp;
911 if (pos < 0)
912 imp = 0;
913 else
914 {
915 imp = pos + mv_columns;
916 if (imp >= mv_num_maps)
917 {
918 const int col = imp % mv_columns;
919 if (col == mv_columns - 1)
920 imp = 0;
921 else
922 imp = col + 1;
923 }
924 }
925 if (!(mv_flags[imp] & GTV_AVAILABLE) && imp != mv_ownvote)
927 return imp;
928}
929
930float MapVote_InputEvent(int bInputType, float nPrimary, float nSecondary)
931{
932 TC(int, bInputType);
933
934 static int first_digit = 0;
935 if (!mv_active || isdemo())
936 return false;
937
938 if (bInputType == 3)
939 {
940 mousepos.x = nPrimary;
941 mousepos.y = nSecondary;
942 mv_selection_keyboard = false;
943 return true;
944 }
945
946 if (bInputType == 2)
947 {
948 mv_selection_keyboard = false;
949 return false;
950 }
951
952 // at this point bInputType can only be 0 or 1 (key pressed or released)
953 const bool key_pressed = (bInputType == 0);
954
955 if (key_pressed)
956 {
957 if (nPrimary == K_ALT) hudShiftState |= S_ALT;
958 if (nPrimary == K_CTRL) hudShiftState |= S_CTRL;
959 if (nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
960 }
961 else
962 {
963 if (nPrimary == K_ALT) hudShiftState &= ~S_ALT;
964 if (nPrimary == K_CTRL) hudShiftState &= ~S_CTRL;
965 if (nPrimary == K_SHIFT) hudShiftState &= ~S_SHIFT;
966
967 if (nPrimary == K_CTRL)
968 first_digit = 0;
969 }
970
971 // Key release events must be handled by the engine otherwise the on-press command such as +jump
972 // executed by pressing SPACE before entering the map voting screen won't be followed by the
973 // on-release command (-jump) on key release once entered the map voting screen, causing +jump
974 // to stay active even on the next map and automatically forcing the player to join
975 if (!key_pressed) return false;
976
977 int imp = 0;
978 switch (nPrimary)
979 {
980 case K_RIGHTARROW:
981 if (mv_winner)
982 return true;
985 return true;
986 case K_LEFTARROW:
987 if (mv_winner)
988 return true;
991 return true;
992 case K_DOWNARROW:
993 if (mv_winner)
994 return true;
997 return true;
998 case K_UPARROW:
999 if (mv_winner)
1000 return true;
1001 mv_selection_keyboard = true;
1003 return true;
1004 case K_KP_ENTER:
1005 case K_ENTER:
1006 case K_SPACE:
1007 if (mv_winner)
1008 return true;
1011 return true;
1012 case '1': case K_KP_1: imp = 1; break;
1013 case '2': case K_KP_2: imp = 2; break;
1014 case '3': case K_KP_3: imp = 3; break;
1015 case '4': case K_KP_4: imp = 4; break;
1016 case '5': case K_KP_5: imp = 5; break;
1017 case '6': case K_KP_6: imp = 6; break;
1018 case '7': case K_KP_7: imp = 7; break;
1019 case '8': case K_KP_8: imp = 8; break;
1020 case '9': case K_KP_9: imp = 9; break;
1021 case '0': case K_KP_0: imp = 10; break;
1022 }
1023
1024 if (imp && hudShiftState & S_CTRL)
1025 {
1026 if (!first_digit)
1027 {
1028 first_digit = imp % 10;
1029 return true;
1030 }
1031 else
1032 imp = first_digit * 10 + (imp % 10);
1033 }
1034
1035 if (nPrimary == K_MOUSE1)
1036 {
1037 mv_selection_keyboard = false;
1039 if (mv_selection >= 0)
1041 }
1042
1043 if (nPrimary == K_MOUSE2)
1044 return true; // do nothing
1045
1046 if (imp)
1047 {
1048 if (!mv_winner && imp <= mv_num_maps)
1049 localcmd("\nimpulse ", ftos(imp), "\n");
1050 return true;
1051 }
1052
1053 return false;
1054}
1055
1057{
1060}
1061
1063{
1064 for (int i = 0; i < mv_num_maps; ++i)
1065 {
1066 if (mv_flags[i] & GTV_AVAILABLE)
1067 {
1068 if (mv_detail)
1069 mv_votes[i] = ReadByte();
1070 else
1071 mv_votes[i] = 0;
1072 }
1073 else
1074 mv_votes[i] = -1;
1075 }
1076 if (mv_detail)
1077 mv_tie_winner = ReadChar();
1078
1079 mv_ownvote = ReadByte() - 1;
1080}
1081
1082NET_HANDLE(ENT_CLIENT_MAPVOTE, bool isnew)
1083{
1084 make_pure(this);
1085 const int sf = ReadByte();
1086 return = true;
1087
1088 if (sf & BIT(0))
1089 MapVote_Init();
1090
1091 if (sf & BIT(1))
1093
1094 if (sf & BIT(2))
1096
1097 if (sf & BIT(3))
1098 {
1099 mv_winner = ReadByte();
1101 }
1102}
1103
1104NET_HANDLE(TE_CSQC_PICTURE, bool isNew)
1105{
1107 return true;
1108}
1109
1111{
1112 const int type = ReadByte();
1113 mv_preview[type] = true;
1114 mv_pics[type] = strzone(ReadPicture());
1115}
#define BIT(n)
Only ever assign into the first 24 bits in QC (so max is BIT(23)).
Definition bits.qh:8
#define boolean(value)
Definition bool.qh:9
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
#define ReadString
void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
Definition draw.qc:5
#define drawcolorcodedstring(position, text, scale, alpha, flag)
Definition draw.qh:30
#define drawstring(position, text, scale, rgb, alpha, flag)
Definition draw.qh:27
#define drawpic(position, pic, size, rgb, alpha, flag)
Definition draw.qh:21
#define draw_beginBoldFont()
Definition draw.qh:4
#define drawfill(position, size, rgb, alpha, flag)
Definition draw.qh:36
#define draw_endBoldFont()
Definition draw.qh:5
float alpha
Definition items.qc:13
vector hud_fontsize
Definition main.qh:77
int mv_columns
Definition mapvoting.qc:44
bool mv_abstain
Definition mapvoting.qc:31
void Cmd_MapVote_MapDownload(int argc)
Definition mapvoting.qc:647
bool mv_selection_keyboard
Definition mapvoting.qc:46
string mapvote_chosenmap
Definition mapvoting.qc:49
int mv_tie_winner
Definition mapvoting.qc:34
int n_ssdirs
Definition mapvoting.qc:57
void MapVote_Draw_Export(int fh)
Definition mapvoting.qc:11
int mv_selection
Definition mapvoting.qc:43
void GameTypeVote_DrawGameTypeItem(vector pos, float maxh, float tsize, string gtype, string pic, int _count, int id, int most_votes)
Definition mapvoting.qc:115
float mv_select_lasttime[MAPVOTE_COUNT]
Definition mapvoting.qc:29
vector gtv_text_size_small
Definition mapvoting.qc:51
void GameTypeVote_ReadOption(int i)
Definition mapvoting.qc:756
void MapVote_UpdateMask()
int mv_flags[MAPVOTE_COUNT]
Definition mapvoting.qc:27
int mv_flags_start[MAPVOTE_COUNT]
Definition mapvoting.qc:28
string ssdirs[NUM_SSDIRS]
Definition mapvoting.qc:56
void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, int _count, int id, int most_votes)
Definition mapvoting.qc:263
int MapVote_MoveRight(int pos)
Definition mapvoting.qc:873
float mv_top2_time
Definition mapvoting.qc:36
vector MapVote_GridVec(vector gridspec, int i, int m)
Definition mapvoting.qc:386
void MapVote_CheckPic(string pic, string pk3, int id)
Definition mapvoting.qc:698
string mv_entries[MAPVOTE_COUNT]
Definition mapvoting.qc:20
void MapVote_DrawMapPicture(string pic, vector pos, vector img_size, float theAlpha)
Definition mapvoting.qc:250
void MapVote_CheckPK3(string pic, string pk3, int id)
Definition mapvoting.qc:681
int mv_detail
Definition mapvoting.qc:32
void MapVote_ReadOption(int i)
Definition mapvoting.qc:741
bool PreviewExists(string name)
Definition mapvoting.qc:59
int mv_mouse_selection
Definition mapvoting.qc:45
float mv_winner_time
Definition mapvoting.qc:38
void MapVote_SendChoice(int index)
Definition mapvoting.qc:855
void MapVote_UpdateVotes()
void MapVote_Init()
Definition mapvoting.qc:797
void Net_MapVote_Picture()
int MapVote_MoveLeft(int pos)
Definition mapvoting.qc:861
float MapVote_InputEvent(int bInputType, float nPrimary, float nSecondary)
Definition mapvoting.qc:930
float mv_suggester_cachetime
Definition mapvoting.qc:41
string mv_suggester_cache
Definition mapvoting.qc:40
string mv_desc[MAPVOTE_COUNT]
Definition mapvoting.qc:24
float mv_top2_alpha
Definition mapvoting.qc:37
entity mv_pk3list
Definition mapvoting.qc:30
vector gtv_text_size
Definition mapvoting.qc:50
const int NUM_SSDIRS
Definition mapvoting.qc:55
int mv_ownvote
Definition mapvoting.qc:33
string mv_data[MAPVOTE_COUNT]
Definition mapvoting.qc:22
bool gametypevote
Definition mapvoting.qc:48
vector MapVote_RGB(int id)
Definition mapvoting.qc:96
void MapVote_DrawSuggester(vector pos)
Definition mapvoting.qc:350
int mv_votes[MAPVOTE_COUNT]
Definition mapvoting.qc:26
string MapVote_FormatMapItem(int id, string map, int _count, float maxwidth, vector fontsize, int most_votes)
Definition mapvoting.qc:72
int MapVote_MoveUp(int pos)
Definition mapvoting.qc:885
void MapVote_DrawAbstain(vector pos, float tsize, int _count, int id)
Definition mapvoting.qc:338
float mv_winner_alpha
Definition mapvoting.qc:39
int MapVote_MoveDown(int pos)
Definition mapvoting.qc:907
const float MV_FADETIME
Definition mapvoting.qc:53
string mv_suggester[MAPVOTE_COUNT]
Definition mapvoting.qc:23
float mv_preview[MAPVOTE_COUNT]
Definition mapvoting.qc:25
void MapVote_Draw(bool should_draw)
Definition mapvoting.qc:425
void MapVote_ReadMask()
Definition mapvoting.qc:713
float MapVote_Selection(vector topleft, vector cellsize, float rows, float columns)
Definition mapvoting.qc:396
string mv_pics[MAPVOTE_COUNT]
Definition mapvoting.qc:21
int mv_num_maps
Definition mapvoting.qc:17
float mv_timeout
Definition mapvoting.qc:35
float xmin
Definition mapvoting.qh:19
float ymin
Definition mapvoting.qh:19
bool autocvar_cl_readpicture_force
Definition mapvoting.qh:5
bool mv_active
Definition mapvoting.qh:17
float xmax
Definition mapvoting.qh:19
int mv_winner
Definition mapvoting.qh:18
float autocvar_hud_panel_mapvote_highlight_border
Definition mapvoting.qh:6
string autocvar_menu_skin
Definition mapvoting.qh:7
float ymax
Definition mapvoting.qh:19
ERASEABLE string rgb_to_hexcolor(vector rgb)
Definition color.qh:183
string textShortenToWidth(string theText, float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
Definition util.qc:1171
string getWrappedLine(float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
Definition util.qc:1131
string getWrappedLine_remaining
Definition util.qh:147
const int GTV_CUSTOM
Definition constants.qh:65
const int MAPVOTE_COUNT
Definition constants.qh:52
const int GTV_AVAILABLE
Definition constants.qh:64
#define count_seconds(time)
Definition counting.qh:56
const float DRAWFLAG_NORMAL
vector drawgetimagesize(string pic)
float time
#define stringwidth
#define spawn
#define strlen
#define tokenize_console
#define tokenizebyseparator
ERASEABLE bool fexists(string f)
Definition file.qh:4
void HUD_Panel_LoadCvars()
Definition hud.qc:215
vector HUD_GetFontsize(string cvarname)
Definition hud.qc:112
vector HUD_GetTableSize_BestItemAR(int item_count, vector psize, float item_aspect)
Definition hud.qc:172
void HUD_Scale_Disable()
Definition hud.qc:84
vector panel_size
Definition hud.qh:163
float panel_fg_alpha
Definition hud.qh:169
const int S_SHIFT
Definition hud.qh:129
int hudShiftState
Definition hud.qh:128
const int S_ALT
Definition hud.qh:131
float panel_bg_padding
Definition hud.qh:174
float panel_bg_border
Definition hud.qh:172
float chat_sizey
Definition hud.qh:183
#define HUD_Panel_DrawBg()
Definition hud.qh:55
bool autocvar_hud_cursormode
Definition hud.qh:191
vector panel_pos
Definition hud.qh:162
float chat_posy
Definition hud.qh:182
vector mousepos
Definition hud.qh:103
const int S_CTRL
Definition hud.qh:130
#define HUD_Write_Cvar(cvar)
Definition hud_config.qh:40
next
Definition all.qh:95
#define itos(i)
Definition int.qh:6
float K_KP_9
Definition keycodes.qc:65
float K_SHIFT
Definition keycodes.qc:22
float K_KP_8
Definition keycodes.qc:63
float K_UPARROW
Definition keycodes.qc:15
float K_KP_3
Definition keycodes.qc:54
float K_KP_0
Definition keycodes.qc:48
float K_DOWNARROW
Definition keycodes.qc:16
float K_KP_7
Definition keycodes.qc:61
float K_MOUSE1
Definition keycodes.qc:129
float K_KP_4
Definition keycodes.qc:56
float K_CTRL
Definition keycodes.qc:21
float K_RIGHTARROW
Definition keycodes.qc:18
float K_SPACE
Definition keycodes.qc:10
float K_ALT
Definition keycodes.qc:20
float K_ENTER
Definition keycodes.qc:8
float K_KP_5
Definition keycodes.qc:58
float K_KP_2
Definition keycodes.qc:52
float K_LEFTARROW
Definition keycodes.qc:17
float K_MOUSE2
Definition keycodes.qc:130
float K_KP_ENTER
Definition keycodes.qc:74
float K_KP_1
Definition keycodes.qc:50
float K_KP_6
Definition keycodes.qc:59
#define TC(T, sym)
Definition _all.inc:82
noref float vid_conwidth
Definition draw.qh:8
noref float vid_conheight
Definition draw.qh:9
#define NET_HANDLE(id, param)
Definition net.qh:15
int ReadByte()
#define LOG_INFO(...)
Definition log.qh:65
string MapInfo_Type_Description(Gametype t)
Definition mapinfo.qc:650
string MapInfo_Type_ToText(Gametype t)
Definition mapinfo.qc:660
Gametype MapInfo_Type_FromString(string gtype, bool dowarn, bool is_q3compat)
Definition mapinfo.qc:620
string name
Definition menu.qh:30
void localcmd(string command,...)
float isdemo()
float ceil(float f)
float stof(string val,...)
float bound(float min, float value, float max)
string precache_pic(string name,...)
float sqrt(float f)
float min(float f,...)
string ftos(float f)
string strzone(string s)
string argv(float n)
float max(float f,...)
string string_null
Definition nil.qh:9
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
#define make_pure(e)
direct use is
Definition oo.qh:13
#define NULL
Definition post.qh:14
vector
Definition self.qh:92
void
Definition self.qh:72
int int int imp
Definition impulse.qc:90
ERASEABLE string ColorTranslateRGB(string s)
Definition string.qh:196
float stringwidth_nocolors(string s, vector theSize)
Definition string.qh:35
float stringwidth_colors(string s, vector theSize)
Definition string.qh:30
const vector eY
Definition vector.qh:45
const vector eZ
Definition vector.qh:46
const vector eX
Definition vector.qh:44
#define vec2(...)
Definition vector.qh:90