]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/mapvoting.qc
2b416cd67c31c89ba595ef0f32cb0b1508f7efe0
[divverent/nexuiz.git] / data / 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
16 string MapVote_FormatMapItem(float id, string map, float count, float maxwidth, vector fontsize)
17 {
18         string pre, post;
19         pre = strcat(ftos(id+1), ". ");
20         if(mv_detail)
21         {
22                 if(count == 1)
23                         post = strcat(" (1 vote)");
24                 else
25                         post = strcat(" (", ftos(count), " votes)");
26         }
27         else
28                 post = "";
29         maxwidth -= stringwidth(pre, FALSE, fontsize) + stringwidth(post, FALSE, fontsize);
30         map = textShortenToWidth(map, maxwidth, fontsize, stringwidth_nocolors);
31         return strcat(pre, map, post);
32 }
33
34 vector MapVote_RGB(float id)
35 {
36         if(id == mv_ownvote)
37                 return '1 1 0';
38         else
39                 return '1 1 1';
40 }
41
42 void MapVote_DrawMapItem(vector pos, float isize, float tsize, string map, string pic, float count, float id)
43 {
44         vector img_size;
45         vector rgb;
46         string label;
47         float text_size;
48         
49         isize -= sbar_fontsize_y; // respect the text when calculating the image size
50
51         rgb = MapVote_RGB(id);
52         
53         img_size_y = isize;
54         img_size_x = isize / 0.75; // 4:3 x can be stretched easily, height is defined in isize
55
56         drawfont = sbar_font;
57         pos_y = pos_y + img_size_y;
58         
59         label = MapVote_FormatMapItem(id, map, count, tsize, sbar_fontsize);
60
61         text_size = stringwidth(label, false, sbar_fontsize);
62         
63         pos_x -= text_size*0.5;
64         drawstring(pos, label, sbar_fontsize, rgb, 1, DRAWFLAG_NORMAL);
65         
66         pos_x = pos_x + text_size*0.5 - img_size_x*0.5;
67         pos_y = pos_y - img_size_y;
68
69         pos += sbar_border_thickness * '1 1 0';
70         img_size -= (sbar_border_thickness * 2) * '1 1 0';
71         if(pic == "")
72         {
73                 drawfill(pos, img_size, '.5 .5 .5', .7, DRAWFLAG_NORMAL);
74         }
75         else
76         {
77                 drawpic(pos, pic, img_size, '1 1 1', 1, DRAWFLAG_NORMAL);
78         }
79         
80         drawpic(pos + '1 0 0', strcat("gfx/hud/num_", ftos(id+1)), (img_size_y / 5) * '1 1 0', '1 1 1', 0.6, DRAWFLAG_NORMAL);
81
82         if(id == mv_ownvote || pic == "")
83         {
84                 drawborderlines(sbar_border_thickness, pos, img_size, rgb, 1, DRAWFLAG_NORMAL);
85                 drawpic(pos + '1 0 0', strcat("gfx/hud/num_", ftos(id+1)), (img_size_y / 5) * '1 1 0', rgb, 0.6, DRAWFLAG_NORMAL);
86         }
87         else
88         {
89                 drawborderlines(sbar_border_thickness, pos, img_size, '0 0 0', 1, DRAWFLAG_NORMAL);
90                 drawpic(pos + '1 0 0', strcat("gfx/hud/num_", ftos(id+1)), (img_size_y / 5) * '1 1 0', '1 1 1', 0.6, DRAWFLAG_NORMAL);
91         }
92 }
93
94 void MapVote_DrawAbstain(vector pos, float isize, float tsize, float count, float id)
95 {
96         vector rgb;
97         float text_size;
98         string label;
99         
100         rgb = MapVote_RGB(id);
101
102         drawfont = sbar_font;
103         pos_y = pos_y + sbar_fontsize_y;
104         
105         label = MapVote_FormatMapItem(id, "Don't care", count, tsize, sbar_fontsize);
106
107         text_size = stringwidth(label, false, sbar_fontsize);
108         
109         pos_x -= text_size*0.5;
110         drawstring(pos, label, sbar_fontsize, rgb, 1, DRAWFLAG_NORMAL);
111 }
112
113 vector MapVote_GridVec(vector gridspec, float i, float m)
114 {
115         float r;
116         r = mod(i, m);
117         return
118                 '1 0 0' * (gridspec_x * r)
119                 +
120                 '0 1 0' * (gridspec_y * (i - r) / m);
121 }
122
123 void MapVote_Draw()
124 {
125         string map;
126         float i, tmp;
127         vector pos;
128         float isize;
129         float center;
130         float columns, rows;
131         float tsize;
132         vector dist;
133
134         if(!mv_active)
135                 return;
136         
137         center = (vid_conwidth - 1)/2;
138         xmin = vid_conwidth*0.05; // 5% border must suffice
139         xmax = vid_conwidth - xmin;
140         ymin = 20;
141         i = cvar("con_chatpos"); //*cvar("con_chatsize");
142         if(i < 0)
143                 ymax = vid_conheight + (i - cvar("con_chat")) * cvar("con_chatsize");
144         if(i >= 0 || ymax < (vid_conheight*0.5))
145                 ymax = vid_conheight - ymin;
146
147         drawfont = sbar_bigfont;
148         sbar_fontsize = Sbar_GetFontsize("sbar_fontsize");
149
150         pos_y = ymin;
151         pos_z = 0;
152         //pos_x = center - stringwidth("Vote for a map", false) * 0.5 * 24;
153         pos_x = center - stringwidth("Vote for a map", false, '12 0 0');
154         drawstring(pos, "Vote for a map", '24 24 0', '1 1 1', 1, DRAWFLAG_NORMAL);
155         pos_y += 26;
156
157         i = ceil(max(0, mv_timeout - time));
158         map = strcat(ftos(i), " seconds left");
159         //pos_x = center - stringwidth(map, false) * 0.5 * 16;
160         pos_x = center - stringwidth(map, false, '8 0 0');
161         drawstring(pos, map, '16 16 0', '0 1 0', 1, DRAWFLAG_NORMAL);
162         pos_y += 22;
163         pos_x = xmin;
164
165         drawfont = sbar_font;
166         
167         // base for multi-column stuff...
168         ymin = pos_y;
169         if(mv_abstain)
170                 mv_num_maps -= 1;
171         
172         if(mv_num_maps > 3)
173         {
174                 columns = 3;
175         } else {
176                 columns = mv_num_maps;
177         }
178         rows = ceil(mv_num_maps / columns);
179
180         dist_x = (xmax - xmin) / columns;
181         dist_y = (ymax - pos_y) / rows;
182         tsize = dist_x - 10;
183         isize = min(dist_y - 10, 0.75 * tsize);
184
185         pos_x += (xmax - xmin) / (2 * columns);
186         pos_y += (dist_y - isize) / 2;
187         ymax -= isize;
188         for(i = 0; i < mv_num_maps; ++i)
189         {
190                 tmp = mv_votes[i]; // FTEQCC bug: too many array accesses in the function call screw it up
191                 if(tmp < 0)
192                         continue;
193                 map = mv_maps[i];
194                 if(mv_preview[i])
195                         MapVote_DrawMapItem(pos + MapVote_GridVec(dist, i, columns), isize, tsize, map, mv_pics[i], tmp, i);
196                 else
197                         MapVote_DrawMapItem(pos + MapVote_GridVec(dist, i, columns), isize, tsize, map, "", tmp, i);
198         }
199
200         if(mv_abstain)
201                 ++mv_num_maps;
202         
203         if(mv_abstain && i < mv_num_maps) {
204                 tmp = mv_votes[i];
205                 pos_y = ymax + isize - sbar_fontsize_y;
206                 pos_x = (xmax+xmin)*0.5;
207                 MapVote_DrawAbstain(pos, isize, xmax - xmin, tmp, i);
208         }
209 }
210
211 void Cmd_MapVote_MapDownload(float argc)
212 {
213         float id;
214         entity pak;
215
216         if(argc != 2 || !mv_pk3list)
217         {
218                 print("mv_mapdownload: ^3You're not supposed to use this command on your own!\n");
219                 return;
220         }
221         
222         id = stof(argv(1));
223         for(pak = mv_pk3list; pak; pak = pak.chain)
224                 if(pak.sv_entnum == id)
225                         break;
226         
227         if(!pak || pak.sv_entnum != id) {
228                 print("^1Error:^7 Couldn't find pak index.\n");
229                 return;
230         }
231
232         //print(strcat("^3Adding: ", ftos(id), " - ", pak.message, " - "));
233         
234         if(PreviewExists(pak.message))
235         {
236                 mv_preview[id] = true;
237                 //print("^2Found...\n");
238                 return;
239         } else {
240                 print("Requesting preview...\n");
241                 localcmd(strcat("\ncmd mv_getpic ", ftos(id), "\n"));
242         }
243 }
244
245 void MapVote_CheckPK3(string pic, string pk3, float id)
246 {
247         entity pak;
248         pak = spawn();
249         pak.netname = pk3;
250         pak.message = pic;
251         pak.sv_entnum = id;
252         
253         pak.chain = mv_pk3list;
254         mv_pk3list = pak;
255         
256         if(pk3 != "")
257         {
258                 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
259         }
260         else
261         {
262                 Cmd_MapVote_MapDownload(tokenize_console(strcat("mv_download ", ftos(id))));
263         }
264 }
265
266 void MapVote_CheckPic(string pic, string pk3, float id)
267 {
268         // never try to retrieve a pic for the "don't care" 'map'
269         if(mv_abstain && id == mv_num_maps - 1)
270                 return;
271
272         if(PreviewExists(pic))
273         {
274                 mv_preview[id] = true;
275                 return;
276         }
277         MapVote_CheckPK3(pic, pk3, id);
278 }
279
280 #define NUM_SSDIRS 4
281 string ssdirs[NUM_SSDIRS];
282 float n_ssdirs;
283 void MapVote_Init()
284 {
285         float i, j, power;
286         string map, pk3, s;
287
288         precache_sound ("misc/invshot.wav");
289
290         registercmd("+showscores");
291         registercmd("-showscores");
292
293         mv_active = 1;
294
295         for(n_ssdirs = 0; ; ++n_ssdirs)
296         {
297                 s = ReadString();
298                 if(s == "")
299                         break;
300                 if(n_ssdirs < NUM_SSDIRS)
301                         ssdirs[n_ssdirs] = s;
302         }
303         n_ssdirs = min(n_ssdirs, NUM_SSDIRS);
304
305         mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
306         mv_abstain = ReadByte();
307         if(mv_abstain)
308                 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
309         mv_detail = ReadByte();
310
311         mv_ownvote = -1;
312         mv_timeout = ReadCoord();
313
314         if(mv_num_maps <= 8)
315                 mv_maps_mask = ReadByte();
316         else
317                 mv_maps_mask = ReadShort();
318         
319         // Assume mv_pk3list is NULL, there should only be 1 mapvote per round
320         mv_pk3list = NULL; // I'm still paranoid!
321         
322         for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
323         {
324                 mv_votes[i] = 0;
325
326                 if(mv_maps_mask & power)
327                 {
328                         map = strzone(ReadString());
329                         pk3 = strzone(ReadString());
330                         j = bound(0, ReadByte(), n_ssdirs - 1);
331         
332                         mv_maps[i] = map;
333                         mv_pk3[i] = pk3;
334                         map = strzone(strcat(ssdirs[j], "/", map));
335                         mv_pics[i] = map;
336
337                         mv_preview[i] = false;
338
339                         //print(strcat("RECV: ", map, " in ", pk3, "\n"));
340                         MapVote_CheckPic(map, pk3, i);
341                 }
342                 else
343                 {
344                         mv_maps[i] = strzone("if-you-see-this-the-code-is-broken");
345                         mv_pk3[i] = strzone("if-you-see-this-the-code-is-broken");
346                         mv_pics[i] = strzone("if-you-see-this-the-code-is-broken");
347                         mv_preview[i] = false;
348                 }
349         }
350
351         for(i = 0; i < n_ssdirs; ++i)
352                 ssdirs[n_ssdirs] = string_null;
353         n_ssdirs = 0;
354
355         // do we NEED this, or can we handle key presses directly in CSQC?
356         localcmd("\nin_bind 7 1 \"impulse 1\"; in_bind 7 2 \"impulse 2\"; in_bind 7 3 \"impulse 3\"; in_bind 7 4 \"impulse 4\"; in_bind 7 5 \"impulse 5\"; in_bind 7 6 \"impulse 6\"; in_bind 7 7 \"impulse 7\"; in_bind 7 8 \"impulse 8\"; in_bind 7 9 \"impulse 9\"; in_bind 7 0 \"impulse 10\"; in_bind 7 KP_1 \"impulse 1\"; in_bind 7 KP_2 \"impulse 2\"; in_bind 7 KP_3 \"impulse 3\"; in_bind 7 KP_4 \"impulse 4\"; in_bind 7 KP_5 \"impulse 5\"; in_bind 7 KP_6 \"impulse 6\"; in_bind 7 KP_7 \"impulse 7\"; in_bind 7 KP_8 \"impulse 8\"; in_bind 7 KP_9 \"impulse 9\"; in_bind 7 KP_0 \"impulse 10\"; in_bindmap 7 0\n");
357 }
358
359 void MapVote_UpdateMask()
360 {
361         float i, power;
362         float oldmask;
363
364         oldmask = mv_maps_mask;
365         if(mv_num_maps <= 8)
366                 mv_maps_mask = ReadByte();
367         else
368                 mv_maps_mask = ReadShort();
369
370         if(oldmask & mv_maps_mask != oldmask)
371                 if(oldmask & mv_maps_mask == mv_maps_mask)
372                          sound(world, CHAN_AUTO, "misc_invshot.wav", VOL_BASE, ATTN_NONE);
373
374         // remove votes that no longer apply
375         for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
376                 if not(mv_maps_mask & power)
377                         mv_votes[i] = -1;
378 }
379
380 void MapVote_UpdateVotes()
381 {
382         float i, power;
383         for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
384         {
385                 if(mv_maps_mask & power)
386                 {
387                         if(mv_detail)
388                                 mv_votes[i] = ReadByte();
389                         else
390                                 mv_votes[i] = 0;
391                 }
392                 else
393                         mv_votes[i] = -1;
394         }
395
396         mv_ownvote = ReadByte()-1;
397 }
398
399 void Ent_MapVote()
400 {
401         float sf;
402
403         sf = ReadByte();
404
405         if(sf & 1)
406                 MapVote_Init();
407
408         if(sf & 2)
409                 MapVote_UpdateMask();
410
411         if(sf & 4)
412                 MapVote_UpdateVotes();
413 }
414
415 void Net_MapVote_Picture()
416 {
417         float type;
418         type = ReadByte();
419         mv_preview[type] = true;
420         mv_pics[type] = strzone(ReadPicture());
421 }