summaryrefslogtreecommitdiff
path: root/src/arenaFile.c
blob: 5c89476b0e98c2b0872b3a3f1d2a38d32b15da68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "main.h"
#include "list.h"
#include "textFile.h"
#include "configFile.h"
#include "arenaFile.h"
#include "arena.h"
#include "tux.h"
#include "shot.h"
#include "wall.h"
#include "pipe.h"
#include "teleport.h"
#include "item.h"
#include "director.h"
#include "path.h"
#include "string_length.h"

#ifndef PUBLIC_SERVER
#include "image.h"
#include "music.h"
#endif

static list_t *listArenaFile;

static bool_t isArenaFileInit = FALSE;

bool_t isAreaFileInicialized()
{
	return isArenaFileInit;
}

#ifndef PUBLIC_SERVER

static void cmd_loadImage(char *line)
{
	char str_file[STR_SIZE];
	char str_name[STR_SIZE];
	char str_alpha[STR_SIZE];

	if( getValue(line, "file", str_file, STR_SIZE) != 0 )return;
	if( getValue(line, "name", str_name, STR_SIZE) != 0 )return;
	if( getValue(line, "alpha", str_alpha, STR_SIZE) != 0 )return;

	addImageData(str_file, isYesOrNO(str_alpha), str_name, IMAGE_GROUP_USER);
}

static void cmd_loadMusic(char *line)
{
	char str_file[STR_SIZE];
	char str_name[STR_SIZE];

	if( getValue(line, "file", str_file, STR_SIZE) != 0 )return;
	if( getValue(line, "name", str_name, STR_SIZE) != 0 )return;

	addMusic(str_file, str_name, MUSIC_GROUP_USER);
}

static void cmd_background(arena_t *arena, char *line)
{
	char str_image[STR_SIZE];

	if( getValue(line, "image", str_image, STR_SIZE) != 0 )return;

	arena->background = getImage(IMAGE_GROUP_USER, str_image);
}

static void cmd_playMusic(arena_t *arena, char *line)
{
	char str_music[STR_SIZE];

	if( getValue(line, "music", str_music, STR_SIZE) != 0 )return;

	strcpy(arena->music, str_music);
}

#endif

static void cmd_wall(arena_t *arena, char *line)
{
	char str_x[STR_NUM_SIZE];
	char str_y[STR_NUM_SIZE];
	char str_img_x[STR_NUM_SIZE];
	char str_img_y[STR_NUM_SIZE];
	char str_w[STR_NUM_SIZE];
	char str_h[STR_NUM_SIZE];
	char str_layer[STR_NUM_SIZE];
	char str_image[STR_SIZE];
	wall_t *new;

	if( getValue(line, "x", str_x, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "y", str_y, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "w", str_w, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "h", str_h, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "img_x", str_img_x, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "img_y", str_img_y, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "image", str_image, STR_SIZE) != 0 )return;

#ifndef PUBLIC_SERVER
	new = newWall(atoi(str_x), atoi(str_y),
			atoi(str_w), atoi(str_h),
			atoi(str_img_x), atoi(str_img_y),
			atoi(str_layer), getImage(IMAGE_GROUP_USER, str_image) );
#endif

#ifdef PUBLIC_SERVER
	new = newWall(atoi(str_x), atoi(str_y),
			atoi(str_w), atoi(str_h),
			atoi(str_img_x), atoi(str_img_y),
			atoi(str_layer) );
#endif

	addList(arena->listWall, new);
}

static void cmd_teleport(arena_t *arena, char *line)
{
	char str_x[STR_NUM_SIZE];
	char str_y[STR_NUM_SIZE];
	char str_w[STR_NUM_SIZE];
	char str_h[STR_NUM_SIZE];
	char str_layer[STR_NUM_SIZE];
	char str_image[STR_SIZE];
	teleport_t *new;

	if( getValue(line, "x", str_x, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "y", str_y, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "w", str_w, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "h", str_h, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "image", str_image, STR_SIZE) != 0 )return;

#ifndef PUBLIC_SERVER
	new = newTeleport(atoi(str_x), atoi(str_y),
			atoi(str_w), atoi(str_h),
			atoi(str_layer), getImage(IMAGE_GROUP_USER, str_image) );
#endif

#ifdef PUBLIC_SERVER
	new = newTeleport(atoi(str_x), atoi(str_y),
			atoi(str_w), atoi(str_h),
			atoi(str_layer) );
#endif

	addList(arena->listTeleport, new);
}

static void cmd_pipe(arena_t *arena, char *line)
{
	char str_x[STR_NUM_SIZE];
	char str_y[STR_NUM_SIZE];
	char str_w[STR_NUM_SIZE];
	char str_h[STR_NUM_SIZE];
	char str_id[STR_NUM_SIZE];
	char str_id_out[STR_NUM_SIZE];
	char str_position[STR_NUM_SIZE];
	char str_layer[STR_NUM_SIZE];
	char str_image[STR_SIZE];
	pipe_t *new;

	if( getValue(line, "x", str_x, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "y", str_y, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "w", str_w, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "h", str_h, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "id", str_id, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "id_out", str_id_out, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "position", str_position, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0 )return;
	if( getValue(line, "image", str_image, STR_SIZE) != 0 )return;

#ifndef PUBLIC_SERVER
	new = newPipe(atoi(str_x), atoi(str_y),
			atoi(str_w), atoi(str_h),
			atoi(str_layer), atoi(str_id), atoi(str_id_out), atoi(str_position),
			getImage(IMAGE_GROUP_USER, str_image) );
#endif

#ifdef PUBLIC_SERVER
	new = newPipe(atoi(str_x), atoi(str_y),
			atoi(str_w), atoi(str_h),
			atoi(str_layer), atoi(str_id), atoi(str_id_out), atoi(str_position) );
#endif

	addList(arena->listPipe, new);
}

int getArenaCount()
{
	return listArenaFile->count;
}

static char *getArenaStatus(int id, char *s)
{
	textFile_t *ts;
	int len;
	int i;

	ts = (textFile_t *) listArenaFile->list[id];
	len = strlen(s);

	for( i = 0 ; i < ts->text->count ; i++ )
	{
		char *line;
		line = (char *) (ts->text->list[i]);

		if( strncmp(line, s, len) == 0 )
		{
			return line+len+1;
		}
	}

	return NULL;
}

char *getArenaName(int id)
{
	char *ret;

	ret = getArenaStatus(id, "name");

	return ( ret != NULL ? ret : "arena_no_name" );
}

char *getArenaNetName(int id)
{
	char *ret;

	ret = getArenaStatus(id, "netName");

	return ( ret != NULL ? ret : "arena_no_net_name" );
}

int getArenaIdFormNetName(char *s)
{
	int i;

	for( i = 0 ; i < listArenaFile->count ; i++ )
	{
		if( strcmp(getArenaNetName(i), s) == 0 )
		{
			return i;
		}
	}

	return -1;
}

char *getArenaImage(int id)
{
	return getArenaStatus(id, "screen");
}

arena_t* getArena(int id)
{
	textFile_t *ts;
	arena_t *arena;
	int i;

	ts = (textFile_t *) listArenaFile->list[id];
	arena = newArena();

	for( i = 0 ; i < ts->text->count ; i++ )
	{
		char *line;
		line = (char *) (ts->text->list[i]);

#ifndef PUBLIC_SERVER
		if( strncmp(line, "loadImage", 9) == 0 )cmd_loadImage(line);
		if( strncmp(line, "loadMusic", 9) == 0 )cmd_loadMusic(line);
		if( strncmp(line, "background", 10) == 0 )cmd_background(arena, line);
		if( strncmp(line, "playMusic", 9) == 0 )cmd_playMusic(arena, line);
#endif

		if( strncmp(line, "wall", 4) == 0 )cmd_wall(arena, line);
		if( strncmp(line, "teleport", 8) == 0 )cmd_teleport(arena, line);
		if( strncmp(line, "pipe", 4) == 0 )cmd_pipe(arena, line);
	}

	return arena;
}

void initArenaFile()
{
	director_t *p;
	int i;

#ifndef PUBLIC_SERVER
	assert( isImageInicialized() == TRUE );
#endif

	isArenaFileInit = TRUE;
	listArenaFile  = newList();

#ifndef PUBLIC_SERVER
	p = loadDirector(PATH_ARENA);
#endif

#ifdef PUBLIC_SERVER
	p = loadDirector(PUBLIC_SERVER_PATH_ARENA);
#endif

	for( i = 0 ; i < p->list->count ; i++ )
	{
		char *line;

		line = (char *)( p->list->list[i] );

		if( strstr(line, ".map") != NULL && strstr(line, "~") == NULL )
		{
			char path[STR_PATH_SIZE];

#ifndef PUBLIC_SERVER
			sprintf(path, PATH_ARENA "%s", line);
#endif
#ifdef PUBLIC_SERVER
			sprintf(path, PUBLIC_SERVER_PATH_ARENA "%s", line);
#endif
			printf("load map %s\n", line);
			addList(listArenaFile, loadTextFile(path));
		}
	}

	destroyDirector(p);
}

void quitArenaFile()
{
	isArenaFileInit = FALSE;
	destroyListItem(listArenaFile, destroyTextFile);
}