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