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