demose: Remove tralling whitespaces too.
[gfxprim/pasky.git] / demos / bogoman / bogoman_map.c
blobb911744131cfa5e238f333d0fe1fe21a03c276ed
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
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. *
8 * *
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. *
13 * *
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 *
18 * *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <stdio.h>
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:
36 printf("-");
37 break;
38 case BOGOMAN_WALL_UP:
39 case BOGOMAN_WALL_DOWN:
40 case BOGOMAN_WALL_UP | BOGOMAN_WALL_DOWN:
41 printf("|");
42 break;
43 case BOGOMAN_WALL_UP | BOGOMAN_WALL_RIGHT:
44 case BOGOMAN_WALL_DOWN | BOGOMAN_WALL_LEFT:
45 printf("\\");
46 break;
47 case BOGOMAN_WALL_UP | BOGOMAN_WALL_LEFT:
48 case BOGOMAN_WALL_DOWN | BOGOMAN_WALL_RIGHT:
49 printf("/");
50 break;
51 default:
52 printf("+");
53 break;
57 void bogoman_map_dump(struct bogoman_map *map)
59 unsigned int x, y;
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);
67 switch (elem->id) {
68 case BOGOMAN_NONE:
69 printf(" ");
70 break;
71 case BOGOMAN_PLAYER:
72 printf("@");
73 break;
74 case BOGOMAN_DIAMOND:
75 printf("$");
76 break;
77 case BOGOMAN_MOVEABLE:
78 printf("M");
79 break;
80 case BOGOMAN_EDIBLE:
81 printf("E");
82 break;
83 case BOGOMAN_WALL:
84 print_wall(elem);
85 break;
89 printf("\n");
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,
97 int dst_x, int dst_y,
98 int src_x, int src_y)
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);
105 *dst = *src;
107 dst->dirty = 1;
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;
118 block->dirty = 1;
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))
150 return 0;
152 copy_block(map, new_x, new_y, x, y);
153 } else {
154 DEBUG(1, "Block moved out of screen %ux%u -> %ix%i",
155 x, y, new_x, new_y);
158 clear_block(map, x, y);
159 return 1;
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");
166 return;
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",
175 new_x, new_y);
176 return;
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)) {
192 case BOGOMAN_NONE:
193 break;
194 case BOGOMAN_DIAMOND:
195 move_get_diamond(map, px, py);
196 break;
197 case BOGOMAN_MOVEABLE:
198 if (!try_move_block(map, px, py, dx, dy))
199 goto finish_move;
200 break;
201 case BOGOMAN_EDIBLE:
202 clear_block(map, px, py);
203 break;
204 default:
205 goto finish_move;
208 player_x = px;
209 player_y = py;
212 /* Update the map */
213 struct bogoman_map_elem *player, *space;
215 finish_move:
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 */
228 player->dirty = 1;
229 space->dirty = 1;
232 void bogoman_map_timer_tick(struct bogoman_map *map)
234 unsigned int x, y;
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);
242 //TODO