1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim 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 GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
25 #include "bogoman_common.h"
26 #include "bogoman_debug.h"
28 #include "bogoman_map.h"
30 static void print_wall(struct bogoman_map_elem
*elem
)
32 switch (elem
->flags
) {
33 case BOGOMAN_WALL_LEFT
:
34 case BOGOMAN_WALL_RIGHT
:
35 case BOGOMAN_WALL_LEFT
| BOGOMAN_WALL_RIGHT
:
39 case BOGOMAN_WALL_DOWN
:
40 case BOGOMAN_WALL_UP
| BOGOMAN_WALL_DOWN
:
43 case BOGOMAN_WALL_UP
| BOGOMAN_WALL_RIGHT
:
44 case BOGOMAN_WALL_DOWN
| BOGOMAN_WALL_LEFT
:
47 case BOGOMAN_WALL_UP
| BOGOMAN_WALL_LEFT
:
48 case BOGOMAN_WALL_DOWN
| BOGOMAN_WALL_RIGHT
:
57 void bogoman_map_dump(struct bogoman_map
*map
)
61 for (y
= 0; y
< map
->h
; y
++) {
62 for (x
= 0; x
< map
->w
; x
++) {
63 struct bogoman_map_elem
*elem
;
65 elem
= bogoman_get_map_elem(map
, x
, y
);
77 case BOGOMAN_MOVEABLE
:
92 printf("Player at %ux%u diamonds total %u\n",
93 map
->player_x
, map
->player_y
, map
->diamonds_total
);
96 static void copy_block(struct bogoman_map
*map
,
100 struct bogoman_map_elem
*src
, *dst
;
102 src
= bogoman_get_map_elem(map
, src_x
, src_y
);
103 dst
= bogoman_get_map_elem(map
, dst_x
, dst_y
);
110 static void clear_block(struct bogoman_map
*map
,
111 unsigned int x
, unsigned int y
)
113 struct bogoman_map_elem
*block
;
115 block
= bogoman_get_map_elem(map
, x
, y
);
117 block
->id
= BOGOMAN_NONE
;
122 * Removes diamond when player has stepped on it.
124 static void move_get_diamond(struct bogoman_map
*map
,
125 unsigned int x
, unsigned int y
)
127 clear_block(map
, x
, y
);
129 map
->player_diamonds
++;
131 DEBUG(1, "Diamond taken, yet to collect %u\n",
132 map
->diamonds_total
- map
->player_diamonds
);
136 * Moves block to empty space, only one of dx or dy can be set
137 * to nonzero value and the value could only be 1 or -1.
139 static int try_move_block(struct bogoman_map
*map
,
140 int x
, int y
, int dx
, int dy
)
142 struct bogoman_map_elem
*elem
= bogoman_get_map_elem(map
, x
, y
);
144 int new_x
= (int)x
+ dx
;
145 int new_y
= (int)y
+ dy
;
147 if (bogoman_coord_in_map(map
, new_x
, new_y
)) {
149 if (!bogoman_is_empty(map
, new_x
, new_y
))
152 copy_block(map
, new_x
, new_y
, x
, y
);
154 DEBUG(1, "Block moved out of screen %ux%u -> %ix%i",
158 clear_block(map
, x
, y
);
162 void bogoman_map_player_move(struct bogoman_map
*map
, int x
, int y
)
164 if (x
!= 0 && y
!= 0) {
165 WARN("Player can move only in one direction\n");
169 int new_x
= map
->player_x
+ x
;
170 int new_y
= map
->player_y
+ y
;
172 if (new_x
< 0 || new_x
>= (int)map
->w
||
173 new_y
< 0 || new_y
>= (int)map
->h
) {
174 WARN("Player can move only in screen got %ix%i\n",
179 int player_x
= map
->player_x
;
180 int player_y
= map
->player_y
;
182 DEBUG(1, "Player %ix%i -> %ix%i\n", player_x
, player_y
, new_x
, new_y
);
184 while (player_x
!= new_x
|| player_y
!= new_y
) {
185 int dx
= SIGN(new_x
- player_x
);
186 int dy
= SIGN(new_y
- player_y
);
188 int px
= map
->player_x
+ dx
;
189 int py
= map
->player_y
+ dy
;
191 switch (bogoman_get_map_elem_id(map
, px
, py
)) {
194 case BOGOMAN_DIAMOND
:
195 move_get_diamond(map
, px
, py
);
197 case BOGOMAN_MOVEABLE
:
198 if (!try_move_block(map
, px
, py
, dx
, dy
))
202 clear_block(map
, px
, py
);
213 struct bogoman_map_elem
*player
, *space
;
217 player
= bogoman_get_map_elem(map
, map
->player_x
, map
->player_y
);
218 space
= bogoman_get_map_elem(map
, player_x
, player_y
);
220 *player
= map
->under_player
;
221 map
->under_player
= *space
;
222 space
->id
= BOGOMAN_PLAYER
;
224 map
->player_x
= player_x
;
225 map
->player_y
= player_y
;
227 /* turn on dirty flags */
232 void bogoman_map_timer_tick(struct bogoman_map
*map
)
236 for (y
= 0; y
< map
->h
; y
++) {
237 for (x
= 0; x
< map
->w
; x
++) {
238 struct bogoman_map_elem
*elem
;
240 elem
= bogoman_get_map_elem(map
, x
, y
);