]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/mapvoting.qc
get rid of the v_flipped toggle
[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 if(csqc_flags & CSQC_FLAG_READPICTURE) {
231                 print("Requesting preview...\n");
232                 localcmd(strcat("\ncmd mv_getpic ", ftos(id), "\n"));
233         } else {
234                 print("^3Missing map preview - Update to a newer build to be able to see them.\n");
235         }
236 }
237
238 void MapVote_CheckPK3(string pic, string pk3, float id)
239 {
240         entity pak;
241         pak = spawn();
242         pak.netname = pk3;
243         pak.message = pic;
244         pak.sv_entnum = id;
245         
246         pak.chain = mv_pk3list;
247         mv_pk3list = pak;
248         
249         if(pk3 != "")
250         {
251                 localcmd(strcat("\ncurl --pak ", pk3, "; wait; cl_cmd mv_download ", ftos(id), "\n"));
252         }
253         else
254         {
255                 Cmd_MapVote_MapDownload(tokenize_sane(strcat("mv_download ", ftos(id))));
256         }
257 }
258
259 void MapVote_CheckPic(string pic, string pk3, float id)
260 {
261         // never try to retrieve a pic for the "don't care" 'map'
262         if(mv_abstain && id == mv_num_maps - 1)
263                 return;
264
265         if(PreviewExists(pic))
266         {
267                 mv_preview[id] = true;
268                 return;
269         }
270         MapVote_CheckPK3(pic, pk3, id);
271 }
272
273 void MapVote_Init()
274 {
275         float i, power;
276         string map, pk3, ssdir;
277
278         precache_sound ("misc/invshot.wav");
279
280         registercmd("+showscores");
281         registercmd("-showscores");
282
283         mv_active = 1;
284
285         ssdir = ReadString();
286         
287         mv_num_maps = min(MAPVOTE_COUNT, ReadByte());
288         mv_abstain = ReadByte();
289         if(mv_abstain)
290                 mv_abstain = 1; // must be 1 for bool-true, makes stuff easier
291         mv_detail = ReadByte();
292
293         mv_ownvote = -1;
294         mv_timeout = ReadCoord();
295
296         if(mv_num_maps <= 8)
297                 mv_maps_mask = ReadByte();
298         else
299                 mv_maps_mask = ReadShort();
300         
301         // Assume mv_pk3list is NULL, there should only be 1 mapvote per round
302         mv_pk3list = NULL; // I'm still paranoid!
303         
304         for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
305         {
306                 mv_votes[i] = 0;
307
308                 if(mv_maps_mask & power)
309                 {
310                         map = strzone(ReadString());
311                         pk3 = strzone(ReadString());
312                         mv_maps[i] = map;
313                         mv_pk3[i] = pk3;
314                         map = strzone(strcat(ssdir, "/", map));
315                         mv_pics[i] = map;
316
317                         mv_preview[i] = false;
318
319                         //print(strcat("RECV: ", map, " in ", pk3, "\n"));
320                         MapVote_CheckPic(map, pk3, i);
321                 }
322                 else
323                 {
324                         mv_maps[i] = strzone("if-you-see-this-the-code-is-broken");
325                         mv_pk3[i] = strzone("if-you-see-this-the-code-is-broken");
326                         mv_pics[i] = strzone("if-you-see-this-the-code-is-broken");
327                         mv_preview[i] = false;
328                 }
329         }
330
331         // do we NEED this, or can we handle key presses directly in CSQC?
332         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");
333 }
334
335 void MapVote_UpdateMask()
336 {
337         float i, power;
338         float oldmask;
339
340         oldmask = mv_maps_mask;
341         if(mv_num_maps <= 8)
342                 mv_maps_mask = ReadByte();
343         else
344                 mv_maps_mask = ReadShort();
345
346         if(oldmask & mv_maps_mask != oldmask)
347                 if(oldmask & mv_maps_mask == mv_maps_mask)
348                          sound(world, CHAN_AUTO, "misc_invshot.wav", VOL_BASE, ATTN_NONE);
349
350         // remove votes that no longer apply
351         for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
352                 if not(mv_maps_mask & power)
353                         mv_votes[i] = -1;
354 }
355
356 void MapVote_UpdateVotes()
357 {
358         float i, power;
359         for(i = 0, power = 1; i < mv_num_maps; ++i, power *= 2)
360         {
361                 if(mv_maps_mask & power)
362                 {
363                         if(mv_detail)
364                                 mv_votes[i] = ReadByte();
365                         else
366                                 mv_votes[i] = 0;
367                 }
368                 else
369                         mv_votes[i] = -1;
370         }
371
372         mv_ownvote = ReadByte()-1;
373 }
374
375 void Ent_MapVote()
376 {
377         float sf;
378
379         sf = ReadByte();
380
381         if(sf & 1)
382                 MapVote_Init();
383
384         if(sf & 2)
385                 MapVote_UpdateMask();
386
387         if(sf & 4)
388                 MapVote_UpdateVotes();
389 }
390
391 string Net_ReadPicture();
392 void Net_MapVote_Picture()
393 {
394         float type;
395         type = ReadByte();
396         mv_preview[type] = true;
397         mv_pics[type] = strzone(Net_ReadPicture());
398 }