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