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