-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
276 lines (245 loc) · 6.8 KB
/
map.cpp
File metadata and controls
276 lines (245 loc) · 6.8 KB
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
#include "map.h"
#include "globals.h"
#include "graphics.h"
#define MAP_WIDTH 50
#define MAP_HEIGHT 50
#define NUM_TILES MAP_WIDTH*MAP_HEIGHT
/**
* The Map structure. This holds a HashTable for all the MapItems, along with
* values for the width and height of the Map.
*/
struct Map {
HashTable* items;
int w, h;
};
/**
* Storage area for the maps.
* This is a global variable, but can only be access from this file because it
* is static.
*/
static Map map;
static Map ruins;
static int active_map;
/**
* The first step in HashTable access for the map is turning the two-dimensional
* key information (x, y) into a one-dimensional unsigned integer.
* This function should uniquely map (x,y) onto the space of unsigned integers.
*/
static unsigned XY_KEY(int X, int Y) {
// Map it to the number as if each tile of the map were numbered
// going across from the top left and then down
unsigned mapped = Y * map_width() + X;
return mapped;
}
/**
* This is the hash function actually passed into createHashTable. It takes an
* unsigned key (the output of XY_KEY) and turns it into a hash value (some
* small non-negative integer).
*/
unsigned map_hash(unsigned key)
{
unsigned hash_value = key % (map_width());
return hash_value;
}
void maps_init()
{
// Initialize hash table
map.items = createHashTable(map_hash, MAP_HEIGHT);
ruins.items = createHashTable(map_hash, 30);
// Set width & height
map.w = MAP_WIDTH;
map.h = MAP_HEIGHT;
ruins.w = 15;
ruins.h = 30;
}
Map* get_active_map()
{
if(active_map == 0)
return ↦
else if(active_map == 1)
return &ruins;
else
return ↦ //default to map
}
Map* set_active_map(int m)
{
active_map = m;
if(active_map == 0)
return ↦
else if(active_map == 1)
return &ruins;
else
return NULL;
}
Map* get_map(int m)
{
if(m == 0)
return ↦
else if(m == 1)
return &ruins;
else
return ↦ //default to map
}
void print_map()
{
// As you add more types, you'll need to add more items to this array.
char lookup[] = {'W', 'P', 'N', 'K', 'D', 'S', 'I'};
for(int y = 0; y < map_height(); y++)
{
for (int x = 0; x < map_width(); x++)
{
MapItem* item = get_here(x,y);
if (item) pc.printf("%c", lookup[item->type]);
else pc.printf(" ");
}
pc.printf("\r\n");
}
}
int map_width()
{
return get_active_map()->w;
}
int map_height()
{
return get_active_map()->h;
}
int map_area()
{
return get_active_map()->w * get_active_map()->h;
}
MapItem* get_north(int x, int y)
{
// Check if there is no northern tile
if(y <= 0)
return NULL;
return (MapItem*) getItem(get_active_map()->items, XY_KEY(x, y - 1));
}
MapItem* get_south(int x, int y)
{
// Check if there is no southern tile
if(y >= get_active_map()->h - 1)
return NULL;
return (MapItem*) getItem(get_active_map()->items, XY_KEY(x, y + 1));
}
MapItem* get_east(int x, int y)
{
// Check if there is no eastern tile
if(x >= get_active_map()->w - 1)
return NULL;
return (MapItem*) getItem(get_active_map()->items, XY_KEY(x + 1, y));
}
MapItem* get_west(int x, int y)
{
// Check if there is no western tile
if(x <= 0)
return NULL;
return (MapItem*) getItem(get_active_map()->items, XY_KEY(x - 1, y));
}
MapItem* get_here(int x, int y)
{
// Check if tile is on map
if(x > -1 && x < get_active_map()->w && y > -1 && y < get_active_map()->h)
return (MapItem*) getItem(get_active_map()->items, XY_KEY(x, y));
else
return NULL;
}
void map_erase(int x, int y)
{
deleteItem(get_active_map()->items, XY_KEY(x, y));
}
void* map_remove(int x, int y)
{
return removeItem(get_active_map()->items, XY_KEY(x,y));
}
void add_wall(int x, int y, int dir, int len)
{
for(int i = 0; i < len; i++)
{
MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
w1->type = WALL;
w1->draw = draw_wall;
w1->walkable = false;
w1->data = NULL;
unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i);
void* val = insertItem(get_active_map()->items, key, w1);
if (val) free(val); // If something is already there, free it
}
}
void add_plant(int x, int y)
{
MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
w1->type = PLANT;
w1->draw = draw_plant;
w1->walkable = true;
w1->data = NULL;
void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
if (val) free(val); // If something is already there, free it
}
void add_NPC(int x, int y, int* state)
{
MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
w1->type = NPC;
w1->draw = draw_NPC;
w1->walkable = false;
w1->data = state;
pc.printf("NPC created with data %u\r\n", *((int*)w1->data));
void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
if (val) free(val); // If something is already there, free it
}
void add_key(int x, int y)
{
MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
w1->type = KEY;
w1->draw = draw_key;
w1->walkable = true;
w1->data = NULL;
void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
if (val) free(val); // If something is already there, free it
}
void add_door(int x, int y, int open)
{
MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
w1->type = DOOR;
w1->draw = (open) ? draw_door_open : draw_door_closed;
w1->walkable = (open) ? true : false; // if the door is open, you can walk through it
w1->data = NULL;
void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
if (val) free(val); // If something is already there, free it
}
void add_stairs(int x, int y, int* map)
{
MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
w1->type = STAIRS;
w1->draw = draw_stairs;
w1->walkable = true;
w1->data = map; //data points to the map the stairs lead to
pc.printf("NPC created with data %u\r\n", *((int*)w1->data));
void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
if (val) free(val); // If something is already there, free it
}
void add_win_item(int x, int y)
{
MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
w1->type = WIN_ITEM;
w1->draw = draw_win_item;
w1->walkable = true;
w1->data = NULL;
void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
if (val) free(val); // If something is already there, free it
}
void add_maze(int x, int y, const char* maze)
{
for(int i = 0; i < 11*11; i++)
{
if(maze[i] == 'w')
add_wall(x + i % 11, y + i / 11, HORIZONTAL, 1);
}
}
void remove_maze(int x, int y, const char* maze)
{
for(int i = 0; i < 11*11; i++)
{
if(maze[i] == 'w')
map_erase(x + i % 11, y + i / 11);
}
}