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