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