We should follow the HandBook
[tuxanci.git] / src / modules / modWall.c
blobd9906dbe6ded7ab0f7e76e700716f7fd3ac10eb4
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
6 #include "main.h"
7 #include "modules.h"
8 #include "tux.h"
9 #include "shot.h"
10 #include "list.h"
11 #include "gun.h"
12 #include "space.h"
14 #ifndef PUBLIC_SERVER
15 #include "interface.h"
16 #include "image.h"
17 #else
18 #include "publicServer.h"
19 #endif
21 typedef struct wall_struct {
22 int id;
24 /* position of the teleport */
25 int x;
26 int y;
28 /* size of the teleport */
29 int w;
30 int h;
32 int img_x;
33 int img_y;
35 /* layer in the arena where the teleport lies */
36 int layer;
38 #ifndef PUBLIC_SERVER
39 /* its image */
40 image_t *img;
41 #endif
42 } wall_t;
44 static export_fce_t *export_fce;
46 static space_t *spaceWall;
48 #ifndef PUBLIC_SERVER
49 static space_t *spaceImgWall;
50 #endif
52 static list_t *listWall;
54 #ifndef PUBLIC_SERVER
55 static wall_t *newWall(int x, int y, int w, int h, int img_x, int img_y, int layer, image_t *img)
56 #else
57 static wall_t *newWall(int x, int y, int w, int h, int img_x, int img_y, int layer)
58 #endif
60 static int last_id = 0;
61 wall_t *new;
63 #ifndef PUBLIC_SERVER
64 assert(img != NULL);
65 #endif
66 new = malloc(sizeof(wall_t));
67 assert(new != NULL);
69 new->id = ++last_id;
70 new->x = x;
71 new->y = y;
72 new->w = w;
73 new->h = h;
74 new->img_x = img_x;
75 new->img_y = img_y;
76 new->layer = layer;
77 #ifndef PUBLIC_SERVER
78 new->img = img;
79 #endif
81 return new;
84 #ifndef PUBLIC_SERVER
86 static void drawWall(wall_t *p)
88 assert(p != NULL);
90 export_fce->fce_addLayer(p->img,
91 p->img_x, p->img_y,
92 0, 0,
93 p->img->w, p->img->h,
94 p->layer);
97 static void drawListWall(list_t *list)
99 wall_t *thisWall;
100 int i;
102 assert(list != NULL);
104 for (i = 0; i < list->count; i++) {
105 thisWall = (wall_t *) list->list[i];
106 assert(thisWall != NULL);
107 drawWall(thisWall);
111 #endif
113 static void destroyWall(wall_t *p)
115 assert(p != NULL);
116 free(p);
119 static void getStatusWall(void *p, int *id, int *x, int *y, int *w, int *h)
121 wall_t *wall;
123 wall = p;
125 *id = wall->id;
126 *x = wall->x;
127 *y = wall->y;
128 *w = wall->w;
129 *h = wall->h;
132 static void setStatusWall(void *p, int x, int y, int w, int h)
134 wall_t *wall;
136 wall = p;
138 wall->x = x;
139 wall->y = y;
140 wall->w = w;
141 wall->h = h;
144 #ifndef PUBLIC_SERVER
145 static void getStatusImgWall(void *p, int *id, int *x, int *y, int *w, int *h)
147 wall_t *wall;
149 wall = p;
151 *id = wall->id;
152 *x = wall->img_x;
153 *y = wall->img_y;
154 *w = wall->img->w;
155 *h = wall->img->h;
158 static void setStatusImgWall(void *p, int x, int y, int w, int h)
160 UNUSED(p);
161 UNUSED(x);
162 UNUSED(y);
163 UNUSED(w);
164 UNUSED(h);
166 #endif
168 static void cmd_wall(char *line)
170 char str_x[STR_NUM_SIZE];
171 char str_y[STR_NUM_SIZE];
172 char str_img_x[STR_NUM_SIZE];
173 char str_img_y[STR_NUM_SIZE];
174 char str_w[STR_NUM_SIZE];
175 char str_h[STR_NUM_SIZE];
176 char str_layer[STR_NUM_SIZE];
177 char str_image[STR_SIZE];
178 char str_rel[STR_SIZE];
179 int x, y, w, h, img_x, img_y, layer;
180 int rel;
181 wall_t *new;
183 rel = 0;
185 if (export_fce->fce_getValue(line, "rel", str_rel, STR_NUM_SIZE) != 0)
186 strcpy(str_rel, "0");
187 if (export_fce->fce_getValue(line, "x", str_x, STR_NUM_SIZE) != 0)
188 return;
189 if (export_fce->fce_getValue(line, "y", str_y, STR_NUM_SIZE) != 0)
190 return;
191 if (export_fce->fce_getValue(line, "w", str_w, STR_NUM_SIZE) != 0)
192 return;
193 if (export_fce->fce_getValue(line, "h", str_h, STR_NUM_SIZE) != 0)
194 return;
195 if (export_fce->fce_getValue(line, "img_x", str_img_x, STR_NUM_SIZE) != 0)
196 return;
197 if (export_fce->fce_getValue(line, "img_y", str_img_y, STR_NUM_SIZE) != 0)
198 return;
199 if (export_fce->fce_getValue(line, "layer", str_layer, STR_NUM_SIZE) != 0)
200 return;
201 if (export_fce->fce_getValue(line, "image", str_image, STR_SIZE) != 0)
202 return;
204 rel = atoi(str_rel);
205 x = atoi(str_x);
206 y = atoi(str_y);
207 w = atoi(str_w);
208 h = atoi(str_h);
209 img_x = atoi(str_img_x);
210 img_y = atoi(str_img_y);
211 layer = atoi(str_layer);
213 if (rel == 1) {
214 img_x += x;
215 img_y += y;
217 #ifndef PUBLIC_SERVER
218 new = newWall(x, y, w, h, img_x, img_y, layer, export_fce->fce_image_get(IMAGE_GROUP_USER, str_image));
219 #else
220 new = newWall(x, y, w, h, img_x, img_y, layer);
221 #endif
223 if (spaceWall == NULL) {
224 spaceWall = space_new(export_fce->fce_arena_get_current()->w,
225 export_fce->fce_arena_get_current()->h,
226 320, 240,
227 getStatusWall, setStatusWall);
230 #ifndef PUBLIC_SERVER
231 if (spaceImgWall == NULL) {
232 spaceImgWall = space_new(export_fce->fce_arena_get_current()->w,
233 export_fce->fce_arena_get_current()->h,
234 320, 240,
235 getStatusImgWall, setStatusImgWall);
237 #endif
239 space_add(spaceWall, new);
241 #ifndef PUBLIC_SERVER
242 space_add(spaceImgWall, new);
243 #endif
246 static int init(export_fce_t *p)
248 export_fce = p;
249 listWall = list_new();
251 return 0;
254 #ifndef PUBLIC_SERVER
255 static void action_drawwall(space_t *space, wall_t *wall, void *p)
257 UNUSED(space);
258 UNUSED(p);
260 drawWall(wall);
263 static int draw(int x, int y, int w, int h)
265 if (spaceWall == NULL) {
266 return 0;
269 space_action_from_location(spaceImgWall, action_drawwall, NULL, x, y, w, h);
271 /*space_print(spaceWall);*/
273 return 0;
275 #endif
277 static void action_eventwall(space_t *space, wall_t *wall, shot_t *shot)
279 arena_t *arena;
280 tux_t *author;
282 UNUSED(space);
283 UNUSED(wall);
285 arena = export_fce->fce_arena_get_current();
287 author = space_get_object_id(arena->spaceTux, shot->author_id);
289 if (author != NULL && author->bonus == BONUS_GHOST && author->bonus_time > 0) {
290 return;
293 if (shot->gun == GUN_BOMBBALL) {
294 if (export_fce->fce_net_multiplayer_get_game_type() != NET_GAME_TYPE_CLIENT) {
295 export_fce->fce_shot_bound_bombBall(shot);
298 return;
301 /*space_del_with_item(arena->spaceShot, shot, export_fce->fce_shot_destroy);*/
302 shot->del = TRUE;
305 static void action_eventshot(space_t *space, shot_t *shot, space_t *p_spaceWall)
307 space_action_from_location(p_spaceWall, action_eventwall,
308 shot, shot->x, shot->y,
309 shot->w, shot->h);
311 if (shot->del == TRUE) {
312 space_del_with_item(space, shot, export_fce->fce_shot_destroy);
316 static int event()
318 if (spaceWall == NULL) {
319 return 0;
322 space_action(export_fce->fce_arena_get_current()->spaceShot,
323 action_eventshot, spaceWall);
325 return 0;
328 static int isConflict(int x, int y, int w, int h)
330 if (spaceWall == NULL) {
331 return 0;
334 return space_is_conflict_with_object(spaceWall, x, y, w, h);
337 static void cmdArena(char *line)
339 if (strncmp(line, "wall", 4) == 0) {
340 cmd_wall(line);
344 static void recvMsg(char *msg)
346 UNUSED(msg);
349 static int destroy()
351 space_destroy_with_item(spaceWall, destroyWall);
352 #ifndef PUBLIC_SERVER
353 space_destroy(spaceImgWall);
354 spaceImgWall = NULL;
355 #endif
356 list_destroy(listWall);
358 listWall = NULL;
359 spaceWall = NULL;
361 return 0;
364 mod_sym_t modwall_sym = { &init,
365 #ifndef PUBLIC_SERVER
366 &draw,
367 #else
369 #endif
370 &event,
371 &isConflict,
372 &cmdArena,
373 &recvMsg,
374 &destroy };