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