/[LeafOK_CVS]/pvpgn-1.7.4/src/bnetd/anongame_maplists.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/bnetd/anongame_maplists.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

1 sysadm 1.1 /*
2     * Copyright (C) 2004 CreepLord (creeplord@pvpgn.org)
3     *
4     * This program is free software; you can redistribute it and/or
5     * modify it under the terms of the GNU General Public License
6     * as published by the Free Software Foundation; either version 2
7     * of the License, or (at your option) any later version.
8     *
9     * This program is distributed in the hope that it will be useful,
10     * but WITHOUT ANY WARRANTY; without even the implied warranty of
11     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     * GNU General Public License for more details.
13     *
14     * You should have received a copy of the GNU General Public License
15     * along with this program; if not, write to the Free Software
16     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17     */
18    
19     #include "common/setup_before.h"
20     #include <stdio.h>
21    
22     /* amadeo */
23     #ifdef WIN32_GUI
24     #include <win32/winmain.h>
25     #endif
26    
27     #ifdef HAVE_STRING_H
28     # include <string.h>
29     #else
30     # ifdef HAVE_STRINGS_H
31     # include <strings.h>
32     # endif
33     #endif
34     #ifdef STDC_HEADERS
35     # include <stdlib.h>
36     #else
37     # ifdef HAVE_MALLOC_H
38     # include <malloc.h>
39     # endif
40     #endif
41    
42     #include "common/packet.h"
43     #include "common/tag.h"
44     #include "common/eventlog.h"
45     #include "common/xalloc.h"
46     #include "prefs.h"
47     #include "anongame_maplists.h"
48     #include "common/setup_after.h"
49    
50     #define MAXMAPS 100
51     #define MAXMAPS_PER_QUEUE 32 /* cannot be changed (map_prefs only supports 32 maps) */
52    
53     /**********************************************************************************/
54     static char * maplist_war3[MAXMAPS];
55     static char * maplist_w3xp[MAXMAPS];
56     static int number_maps_war3 = 0;
57     static int number_maps_w3xp = 0;
58     static char maplists_war3[ANONGAME_TYPES][MAXMAPS_PER_QUEUE+1];
59     static char maplists_w3xp[ANONGAME_TYPES][MAXMAPS_PER_QUEUE+1];
60    
61     static int _maplists_type_get_queue(const char * type);
62     static char * _maplists_queue_get_type(int queue);
63    
64     static void _maplists_add_map(t_clienttag clienttag, char * mapname, int queue);
65    
66     /*****/
67     static char * queue_names[ANONGAME_TYPES] = {
68     "1v1","2v2","3v3","4v4","sffa","at2v2","tffa","at3v3","at4v4",
69     "TY",
70     "5v5","6v6","2v2v2","3v3v3","4v4v4","2v2v2v2","3v3v3v3",
71     "at2v2v2"
72     };
73     /**********************************************************************************/
74     static int _maplists_type_get_queue(const char * type)
75     {
76     int i;
77    
78     for (i = 0; i < ANONGAME_TYPES; i++)
79     if (strcmp(type, queue_names[i]) == 0)
80     return i;
81    
82     return -1;
83     }
84    
85    
86     static char * _maplists_queue_get_type(int queue)
87     {
88     return queue_names[queue];
89     }
90    
91     static void _maplists_add_map(t_clienttag clienttag, char * mapname, int queue)
92     {
93     /* this function does two things
94     * 1. adds the mapname to maplist for MAPS section
95     * maplist[mapnumber] = mapname
96     * number_maps = total maps held in maplist[]
97     * 2. adds the mapnumber to maplists for TYPE section
98     * maplists[queue][0] = number of maps for queue
99     * maplists[queue][1..32] = mapnumber
100     * ie. maplist[maplists[queue][1..32]] = mapname
101     */
102     int in_list = 0;
103     int j;
104     char clienttag_str[5];
105    
106     if (clienttag==CLIENTTAG_WARCRAFT3_UINT) {
107     for (j = 0; j < number_maps_war3; j++) {
108     if (strcmp(maplist_war3[j], mapname) == 0) { /* already in list */
109     in_list = 1;
110     break;
111     }
112     }
113    
114     if (!in_list)
115     maplist_war3[number_maps_war3++] = xstrdup(mapname);
116    
117     if (maplists_war3[queue][0] < MAXMAPS_PER_QUEUE) {
118     maplists_war3[queue][0]++;
119     maplists_war3[queue][(int)maplists_war3[queue][0]] = j;
120     } else {
121     eventlog(eventlog_level_error,__FUNCTION__,
122     "cannot add map \"%s\" for gametype: %s (maxmaps per qametype: %d)",
123     mapname, _maplists_queue_get_type(queue), MAXMAPS_PER_QUEUE);
124     }
125     }
126    
127     else if (clienttag==CLIENTTAG_WAR3XP_UINT) {
128     for (j = 0; j < number_maps_w3xp; j++) {
129     if (strcmp(maplist_w3xp[j], mapname) == 0) { /* already in list */
130     in_list = 1;
131     break;
132     }
133     }
134    
135     if (!in_list)
136     maplist_w3xp[number_maps_w3xp++] = xstrdup(mapname);
137    
138     if (maplists_w3xp[queue][0] < MAXMAPS_PER_QUEUE) {
139     maplists_w3xp[queue][0]++;
140     maplists_w3xp[queue][(int)maplists_w3xp[queue][0]] = j;
141     } else {
142     eventlog(eventlog_level_error,__FUNCTION__,
143     "cannot add map \"%s\" for gametype: %s (maxmaps per qametype: %d)",
144     mapname, _maplists_queue_get_type(queue), MAXMAPS_PER_QUEUE);
145     }
146     }
147    
148     else eventlog(eventlog_level_error,__FUNCTION__,"invalid clienttag: %s",tag_uint_to_str(clienttag_str,clienttag));
149     }
150    
151     /**********************************************************************************/
152     extern int anongame_maplists_create(void)
153     {
154     FILE *mapfd;
155     char buffer[256];
156     int len, i, queue;
157     char *p, *q, *r, *u;
158    
159     if (prefs_get_mapsfile() == NULL) {
160     eventlog(eventlog_level_error, "anongame_maplists_create","invalid mapsfile, check your config");
161     return -1;
162     }
163    
164     if ((mapfd = fopen(prefs_get_mapsfile(), "rt")) == NULL) {
165     eventlog(eventlog_level_error, "anongame_maplists_create", "could not open mapsfile : \"%s\"", prefs_get_mapsfile());
166     return -1;
167     }
168    
169     /* init the maps, they say static vars are 0-ed anyway but u never know :) */
170     for(i=0; i < ANONGAME_TYPES; i++) {
171     maplists_war3[i][0] = 0;
172     maplists_w3xp[i][0] = 0;
173     }
174    
175     while(fgets(buffer, 256, mapfd)) {
176     len = strlen(buffer);
177     if (len < 1) continue;
178     if (buffer[len-1] == '\n') {
179     buffer[len-1] = '\0';
180     }
181    
182     /* search for comments and comment them out */
183     for(p = buffer; *p ; p++)
184     if (*p == '#') {
185     *p = '\0';
186     break;
187     }
188    
189     /* skip spaces and/or tabs */
190     for(p = buffer; *p && ( *p == ' ' || *p == '\t' ); p++); /* p = clienttag */
191     if (*p == '\0') continue;
192    
193     /* find next delimiter */
194     for(q = p; *q && *q != ' ' && *q != '\t'; q++);
195     if (*q == '\0' || q - p != 4) continue; /* clienttag needs to have 4 chars */
196    
197     *q = '\0'; /* end of clienttag */
198    
199     /* skip spaces and/or tabs */
200     for (q++ ; *q && ( *q == ' ' || *q == '\t'); q++); /* q = type */
201     if (*q == '\0') continue;
202    
203     /* find next delimiter */
204     for (r = q+1; *r && *r != ' ' && *r != '\t'; r++);
205    
206     *r = '\0'; /* end of type */
207    
208     /* skip spaces and/or tabs */
209     for (r++ ; *r && ( *r == ' ' || *r == '\t'); r++); /* r = mapname */
210     if (*r == '\0') continue;
211    
212     if (*r!='\"') /* mapname without quotes */
213     /* find next delimiter */
214     for (u = r+1; *u && *u != ' ' && *u != '\t'; u++);
215     else /* mapname with quotes */
216     {
217     r++; /* skip quote */
218     for (u = r+1; *u && *u != '\"'; u++); /* find end quote or end of buffer */
219     if (*u!='\"')
220     {
221     eventlog(eventlog_level_error,__FUNCTION__,"missing \" at the end of the map name, presume it's ok anyway");
222     }
223     }
224     *u = '\0'; /* end of mapname */
225    
226     if ((queue = _maplists_type_get_queue(q)) < 0) continue; /* invalid queue */
227    
228     _maplists_add_map(tag_case_str_to_uint(p), r, queue);
229     }
230     fclose(mapfd);
231     return 0;
232     }
233    
234     /* used by the MAPS section */
235     extern int maplists_get_totalmaps(t_clienttag clienttag)
236     {
237     if (clienttag==CLIENTTAG_WARCRAFT3_UINT)
238     return number_maps_war3;
239    
240     if (clienttag==CLIENTTAG_WAR3XP_UINT)
241     return number_maps_w3xp;
242    
243     return 0;
244     }
245    
246     extern void maplists_add_maps_to_packet(t_packet * packet, t_clienttag clienttag)
247     {
248     int i;
249    
250     if (clienttag==CLIENTTAG_WARCRAFT3_UINT)
251     for (i = 0; i < number_maps_war3; i++)
252     packet_append_string(packet, maplist_war3[i]);
253    
254     if (clienttag==CLIENTTAG_WAR3XP_UINT)
255     for (i = 0; i < number_maps_w3xp; i++)
256     packet_append_string(packet, maplist_w3xp[i]);
257     }
258    
259     /* used by TYPE section */
260     extern int maplists_get_totalmaps_by_queue(t_clienttag clienttag, int queue)
261     {
262     if (clienttag==CLIENTTAG_WARCRAFT3_UINT)
263     return maplists_war3[queue][0];
264    
265     if (clienttag==CLIENTTAG_WAR3XP_UINT)
266     return maplists_w3xp[queue][0];
267    
268     return 0;
269     }
270    
271     extern void maplists_add_map_info_to_packet(t_packet * rpacket, t_clienttag clienttag, int queue)
272     {
273     int i;
274    
275     if (clienttag==CLIENTTAG_WARCRAFT3_UINT) {
276     for (i = 0; i < maplists_war3[queue][0] + 1; i++)
277     packet_append_data(rpacket, &maplists_war3[queue][i], 1);
278     }
279     if (clienttag==CLIENTTAG_WAR3XP_UINT) {
280     for (i = 0; i < maplists_w3xp[queue][0] + 1; i++)
281     packet_append_data(rpacket, &maplists_w3xp[queue][i], 1);
282     }
283     }
284    
285     /* used by _get_map_from_prefs() */
286     extern char * maplists_get_map(int queue, t_clienttag clienttag, int mapnumber)
287     {
288     if (clienttag==CLIENTTAG_WARCRAFT3_UINT)
289     return maplist_war3[(int)maplists_war3[queue][mapnumber]];
290     if (clienttag==CLIENTTAG_WAR3XP_UINT)
291     return maplist_w3xp[(int)maplists_w3xp[queue][mapnumber]];
292    
293     return NULL;
294     }
295    
296     extern void anongame_maplists_destroy()
297     {
298     int i;
299    
300     for (i = 0; i < MAXMAPS; i++) {
301     if (maplist_war3[i])
302     xfree((void *)maplist_war3[i]);
303     if (maplist_w3xp[i])
304     xfree((void *)maplist_w3xp[i]);
305     }
306     }
307    
308     extern int anongame_add_tournament_map(t_clienttag clienttag, char * mapname)
309     {
310     _maplists_add_map(clienttag, mapname, ANONGAME_TYPE_TY);
311     return 0;
312     }
313    
314     extern void anongame_tournament_maplists_destroy(void)
315     {
316     return; /* nothing to destroy */
317     }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1