]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/mapvoting.qc
Fade out discarded maps in the map voting screen and keep them a bit visible, it...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / mapvoting.qc
1 float mv_num_maps;
2
3 float mv_active;
4 string mv_maps[MAPVOTE_COUNT];
5 string mv_pics[MAPVOTE_COUNT];
6 string mv_pk3[MAPVOTE_COUNT];
7 float mv_preview[MAPVOTE_COUNT];
8 float mv_votes[MAPVOTE_COUNT];
9 entity mv_pk3list;
10 float mv_abstain;
11 float mv_ownvote;
12 float mv_detail;
13 float mv_timeout;
14 float mv_maps_mask;
15 float mv_top2_time;
16 float mv_top2_alpha;
17
18 vector mv_mousepos;
19 float mv_selection;
20
21 string MapVote_FormatMapItem(float id, string map, float count, float maxwidth, vector fontsize)
22 {
23         string pre, post;
24         pre = strcat(ftos(id+1), ". ");
25         if(mv_detail)
26         {
27                 if(count == 1)
28                         post = strcat(" (1 vote)");
29                 else
30                         post = strcat(" (", ftos(count), " votes)");
31         }
32         else
33                 post = "";
34         maxwidth -= stringwidth(pre, FALSE, fontsize) + stringwidth(post, FALSE, fontsize);
35         map = textShortenToWidth(map, maxwidth, fontsize, stringwidth_nocolors);
36         return strcat(pre, map, post);
37 }
38
39 vector MapVote_RGB(float id, float count)
40 {
41         if(count < 0)
42                 return '1 1 1';
43         if(id == mv_ownvote)
44                 return '0 1 0';
45         else if (id == mv_selection)
46                 return '1 1 0';
47         else
48                 return '1 1 1';
49 }
50
51 void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float count, float id)
52 {
53         vector img_size;
54         vector rgb;
55         string label;
56         float text_size;
57         
58         isize -= hud_fontsize_y; // respect the text when calculating the image size
59
60         rgb = MapVote_RGB(id, count);
61         
62         img_size_y = isize;
63         img_size_x = isize / 0.75; // 4:3 x can be stretched easily, height is defined in isize
64
65         drawfont = hud_font;
66         pos_y = pos_y + img_size_y;
67         
68         label = MapVote_FormatMapItem(id, map, count, tsize, hud_fontsize);
69
70         text_size = stringwidth(label, false, hud_fontsize);
71
72         float alpha;
73         if (count < 0 && mv_top2_alpha)
74                 alpha = mv_top2_alpha;
75         else
76                 alpha = 1;
77
78         pos_x -= text_size*0.5;
79         drawstring(pos, label, hud_fontsize, rgb, alpha, DRAWFLAG_NORMAL);
80         
81         pos_x = pos_x + text_size*0.5 - img_size_x*0.5;
82         pos_y = pos_y - img_size_y;
83
84         pos += autocvar_scoreboard_border_thickness * '1 1 0';
85         img_size -= (autocvar_scoreboard_border_thickness * 2) * '1 1 0';
86         if(pic == "")
87         {
88                 drawfill(pos, img_size, '.5 .5 .5', .7 * alpha, DRAWFLAG_NORMAL);
89         }
90         else
91         {
92                 drawpic(pos, pic, img_size, '1 1 1', alpha, DRAWFLAG_NORMAL);
93         }
94
95         if(id == mv_ownvote)
96                 drawborderlines(autocvar_scoreboard_border_thickness, pos, img_size, rgb, alpha, DRAWFLAG_NORMAL);
97         else
98                 drawborderlines(autocvar_scoreboard_border_thickness, pos, img_size, '0 0 0', alpha, DRAWFLAG_NORMAL);
99
100         if(id == mv_selection && count >= 0)
101                 drawfill(pos, img_size, '1 1 1', 0.1, DRAWFLAG_NORMAL);
102 }
103
104 void MapVote_DrawAbstain(vector pos, float isize, float tsize, float count, float id)
105 {
106         vector rgb;
107         float text_size;
108         string label;
109         
110         rgb = MapVote_RGB(id, count);
111
112         drawfont = hud_font;
113         pos_y = pos_y + hud_fontsize_y;
114         
115         label = MapVote_FormatMapItem(id, "Don't care", count, tsize, hud_fontsize);
116
117         text_size = stringwidth(label, false, hud_fontsize);
118         
119         pos_x -= text_size*0.5;
120         drawstring(pos, label, hud_fontsize, rgb, 1, DRAWFLAG_NORMAL);
121 }
122
123 vector MapVote_GridVec(vector gridspec, float i, float m)
124 {
125         float r;
126         r = mod(i, m);
127         return
128                 '1 0 0' * (gridspec_x * r)
129                 +
130                 '0 1 0' * (gridspec_y * (i - r) / m);
131 }
132
133 float MapVote_Selection(vector topleft, vector cellsize, float rows, float columns)
134 {
135         float cell;
136         float c, r;
137
138         cell = -1;
139
140         for (r = 0; r < rows; ++r)
141                 for (c = 0; c < columns; ++c)
142                 {
143                         if (mv_mousepos_x >= topleft_x + cellsize_x *  c &&
144                                 mv_mousepos_x <= topleft_x + cellsize_x * (c + 1) &&
145                                 mv_mousepos_y >= topleft_y + cellsize_y *  r &&
146                                 mv_mousepos_y <= topleft_y + cellsize_y * (r + 1))
147                         {
148                                 cell = r * columns + c;
149                                 break;
150                         }
151                 }
152
153         if (cell >= mv_num_maps)
154                 cell = -1;
155
156         if (mv_abstain && cell < 0)
157                 return mv_num_maps;
158
159         return cell;
160 }
161
162 void MapVote_Draw()
163 {
164         string map;
165         float i, tmp;
166         vector pos;
167         float isize;
168         float center;
169         float columns, rows;
170         float tsize;
171         vector dist;
172
173         if(!mv_active)
174                 return;
175         
176         mv_mousepos = mv_mousepos + getmousepos();
177
178         mv_mousepos_x = bound(0, mv_mousepos_x, vid_conwidth);
179         mv_mousepos_y = bound(0, mv_mousepos_y, vid_conheight);
180
181         center = (vid_conwidth - 1)/2;
182         xmin = vid_conwidth*0.05; // 5% border must suffice
183         xmax = vid_conwidth - xmin;
184         ymin = 20;
185         i = autocvar_con_chatpos; //*autocvar_con_chatsize;
186         if(i < 0)
187                 ymax = vid_conheight + (i - autocvar_con_chat) * autocvar_con_chatsize;
188         if(i >= 0 || ymax < (vid_conheight*0.5))
189                 ymax = vid_conheight - ymin;
190
191         drawfont = hud_bigfont;
192         hud_fontsize = HUD_GetFontsize("hud_fontsize");
193
194         pos_y = ymin;
195         pos_z = 0;
196         //pos_x = center - stringwidth("Vote for a map", false) * 0.5 * 24;
197         pos_x = center - stringwidth("Vote for a map", false, '12 0 0');
198         drawstring(pos, "Vote for a map", '24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
199         pos_y += 26;
200
201         i = ceil(max(0, mv_timeout - time));
202         map = strcat(ftos(i), " seconds left");
203         //pos_x = center - stringwidth(map, false) * 0.5 * 16;
204         pos_x = center - stringwidth(map, false, '8 0 0');
205         drawstring(pos, map, '16 16 0', '0 1 0', 1, DRAWFLAG_NORMAL);
206         pos_y += 22;
207         pos_x = xmin;
208
209         drawfont = hud_font;
210         
211         // base for multi-column stuff...
212         ymin = pos_y;
213         if(mv_abstain)
214                 mv_num_maps -= 1;
215         
216         if(mv_num_maps > 3)
217         {
218                 columns = 3;
219         } else {
220                 columns = mv_num_maps;
221         }
222         rows = ceil(mv_num_maps / columns);
223
224         dist_x = (xmax - xmin) / columns;
225         dist_y = (ymax - pos_y) / rows;
226         tsize = dist_x - 10;
227         isize = min(dist_y - 10, 0.75 * tsize);
228
229         mv_selection = MapVote_Selection(pos, dist, rows, columns);
230
231         pos_x += (xmax - xmin) / (2 * columns);
232         pos_y += (dist_y - isize) / 2;
233         ymax -= isize;
234
235         if (mv_top2_time)
236                 mv_top2_alpha = max(0.2, 1 - (time - mv_top2_time)*(time - mv_top2_time));
237
238         for(i = 0; i < mv_num_maps; ++i)
239         {
240                 tmp = mv_votes[i]; // FTEQCC bug: too many array accesses in the function call screw it up
241                 map = mv_maps[i];
242                 if(mv_preview[i])
243                         MapVote_DrawMapItem(pos + MapVote_GridVec(dist, i, columns), isize, tsize, map, mv_pics[i], tmp, i);
244                 else
245                         MapVote_DrawMapItem(pos + MapVote_GridVec(dist, i, columns), isize, tsize, map, "", tmp, i);
246         }
247
248         if(mv_abstain)
249                 ++mv_num_maps;
250         
251         if(mv_abstain && i < mv_num_maps) {
252                 tmp = mv_votes[i];
253                 pos_y = ymax + isize - hud_fontsize_y;
254                 pos_x = (xmax+xmin)*0.5;
255                 MapVote_DrawAbstain(pos, isize, xmax - xmin, tmp, i);
256         }
257
258         drawpic(mv_mousepos, strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"), '32 32 0', '1 1 1', autocvar_hud_panel_fg_alpha, DRAWFLAG_NORMAL);
259 }
260
261 void Cmd_MapVote_MapDownload(float argc)
262 {
263         float id;
264         entity pak;
265
266         if(argc != 2 || !mv_pk3list)
267         {
268                 print("mv_mapdownload: ^3You're not supposed to use this command on your own!\n");
269                 return;
270         }
271         
272         id = stof(argv(1));
273         for(pak = mv_pk3list; pak; pak = pak.chain)
274                 if(pak.sv_entnum == id)
275                         break;
276         
277         if(!pak || pak.sv_entnum != id) {
278                 print("^1Error:^7 Couldn't find pak index.\n");
279                 return;
280         }
281
282         //print(strcat("^3Adding: ", ftos(id), " - ", pak.message, " - "));
283         
284         if(PreviewExists(pak.message))
285         {
286                 mv_preview[id] = true;
287                 //print("^2Found...\n");
288                 return;
289         } else {
290                 print("Requesting preview...\n");
291                 localcmd(strcat("\ncmd mv_getpic ", ftos(id), "\n"));
292         }
293 }
294
295 void MapVote_CheckPK3(string pic, string pk3, float id)
296 {
297         entity pak;
298         pak = spawn();
299         pak.netname = pk3;
300         pak.message = pic;
301         pak.sv_entnum = id;
302         
303         pak.chain = mv_pk3list;
304         mv_pk3list = pak;
305         
306         if(pk3 != "")
307         {
308                 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
309         }
310         else
311         {
312                 Cmd_MapVote_MapDownload(tokenize_console(strcat("mv_download ", ftos(id))));
313         }
314 }
315
316 void MapVote_CheckPic(string pic, string pk3, float id)
317 {
318         // never try to retrieve a pic for the "don't care" 'map'
319         if(mv_abstain && id == mv_num_maps - 1)
320                 return;
321
322         if(PreviewExists(pic))
323         {
324                 mv_preview[id] = true;
325                 return;
326         }
327         MapVote_CheckPK3(pic, pk3, id);
328 }
329
330 #define NUM_SSDIRS 4
331 string ssdirs[NUM_SSDIRS];
332 float n_ssdirs;
333 void MapVote_Init()
334 {
335         float i, j, power;
336         string map, pk3, s;
337
338         precache_sound ("misc/invshot.wav");
339
340         mv_active = 1;
341
342         mv_mousepos = '0.5 0 0' * vid_conwidth + '0 0.5 0' * vid_conheight;
343         mv_selection = -1;
344
345         for(n_ssdirs = 0; ; ++n_ssdirs)
346         {
347                 s = ReadString();
348                 if(s == "")
349                         break;
350                 if(n_ssdirs < NUM_SSDIRS)
351                         ssdirs[n_ssdirs] = s;
352         }
353         n_ssdirs = min(n_ssdirs, NUM_SSDIRS);
354
355         mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
356         mv_abstain = ReadByte();
357         if(mv_abstain)
358                 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
359         mv_detail = ReadByte();
360
361         mv_ownvote = -1;
362         mv_timeout = ReadCoord();
363
364         if(mv_num_maps <= 8)
365                 mv_maps_mask = ReadByte();
366         else
367                 mv_maps_mask = ReadShort();
368         
369         // Assume mv_pk3list is NULL, there should only be 1 mapvote per round
370         mv_pk3list = NULL; // I'm still paranoid!
371         
372         for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
373         {
374                 mv_votes[i] = 0;
375
376                 if(mv_maps_mask & power)
377                 {
378                         map = strzone(ReadString());
379                         pk3 = strzone(ReadString());
380                         j = bound(0, ReadByte(), n_ssdirs - 1);
381         
382                         mv_maps[i] = map;
383                         mv_pk3[i] = pk3;
384                         map = strzone(strcat(ssdirs[j], "/", map));
385                         mv_pics[i] = map;
386
387                         mv_preview[i] = false;
388
389                         //print(strcat("RECV: ", map, " in ", pk3, "\n"));
390                         MapVote_CheckPic(map, pk3, i);
391                 }
392                 else
393                 {
394                         mv_maps[i] = strzone("if-you-see-this-the-code-is-broken");
395                         mv_pk3[i] = strzone("if-you-see-this-the-code-is-broken");
396                         mv_pics[i] = strzone("if-you-see-this-the-code-is-broken");
397                         mv_preview[i] = false;
398                 }
399         }
400
401         for(i = 0; i < n_ssdirs; ++i)
402                 ssdirs[n_ssdirs] = string_null;
403         n_ssdirs = 0;
404 }
405
406 float MapVote_InputEvent(float bInputType, float nPrimary, float nSecondary)
407 {
408         float imp;
409
410         if (!mv_active)
411                 return false;
412
413         if (bInputType != 0)
414                 return false;
415
416         if ('0' <= nPrimary && nPrimary <= '9')
417         {
418                 imp = nPrimary - '0';
419                 if (imp == 0) imp = 10;
420                 localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
421                 return true;
422         }
423
424         if (nPrimary == K_MOUSE1)
425                 if (mv_selection >= 0)
426                 {
427                         imp = min(mv_selection + 1, mv_num_maps);
428                         localcmd(strcat("\nimpulse ", ftos(imp), "\n"));
429                         return true;
430                 }
431
432         return false;
433 }
434
435 void MapVote_UpdateMask()
436 {
437         float i, power;
438         float oldmask;
439
440         oldmask = mv_maps_mask;
441         if(mv_num_maps <= 8)
442                 mv_maps_mask = ReadByte();
443         else
444                 mv_maps_mask = ReadShort();
445
446         if(oldmask & mv_maps_mask != oldmask)
447                 if(oldmask & mv_maps_mask == mv_maps_mask)
448                          sound(world, CHAN_AUTO, "misc_invshot.wav", VOL_BASE, ATTN_NONE);
449
450         // remove votes that no longer apply
451         for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
452                 if not(mv_maps_mask & power)
453                         mv_votes[i] = -1;
454
455         mv_top2_time = time;
456 }
457
458 void MapVote_UpdateVotes()
459 {
460         float i, power;
461         for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
462         {
463                 if(mv_maps_mask & power)
464                 {
465                         if(mv_detail)
466                                 mv_votes[i] = ReadByte();
467                         else
468                                 mv_votes[i] = 0;
469                 }
470                 else
471                         mv_votes[i] = -1;
472         }
473
474         mv_ownvote = ReadByte()-1;
475 }
476
477 void Ent_MapVote()
478 {
479         float sf;
480
481         sf = ReadByte();
482
483         if(sf & 1)
484                 MapVote_Init();
485
486         if(sf & 2)
487                 MapVote_UpdateMask();
488
489         if(sf & 4)
490                 MapVote_UpdateVotes();
491 }
492
493 void Net_MapVote_Picture()
494 {
495         float type;
496         type = ReadByte();
497         mv_preview[type] = true;
498         mv_pics[type] = strzone(ReadPicture());
499 }