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