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