Game For ECE 2035
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@10:e18685911e84, 2021-12-01 (annotated)
- Committer:
- nasiromar
- Date:
- Wed Dec 01 22:07:24 2021 +0000
- Revision:
- 10:e18685911e84
- Parent:
- 9:cbb9cfb1f6c5
- Child:
- 11:6cd02a8539d1
Walk;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rconnorlawson | 0:35660d7952f7 | 1 | #include "map.h" |
rconnorlawson | 0:35660d7952f7 | 2 | |
rconnorlawson | 0:35660d7952f7 | 3 | #include "globals.h" |
rconnorlawson | 0:35660d7952f7 | 4 | #include "graphics.h" |
rconnorlawson | 0:35660d7952f7 | 5 | |
nasiromar | 6:c9695079521d | 6 | #define NumBuckets 5 |
nasiromar | 6:c9695079521d | 7 | #define NumMaps 2 |
nasiromar | 6:c9695079521d | 8 | #define HEIGHT 50 |
nasiromar | 6:c9695079521d | 9 | #define WIDTH 50 |
nasiromar | 6:c9695079521d | 10 | |
rconnorlawson | 0:35660d7952f7 | 11 | /** |
rconnorlawson | 0:35660d7952f7 | 12 | * The Map structure. This holds a HashTable for all the MapItems, along with |
rconnorlawson | 0:35660d7952f7 | 13 | * values for the width and height of the Map. |
lballard9 | 4:37d3935365f8 | 14 | * In this file you need to define how the map will be structured. IE how will |
lballard9 | 4:37d3935365f8 | 15 | * you put values into the map, pull them from the map. Remember a "Map" variable |
nasiromar | 6:c9695079521d | 16 | * is a hashtable plus two ints (see below) |
lballard9 | 4:37d3935365f8 | 17 | * You will have more than one map variable, one will be the main map with it's own hashtable. |
lballard9 | 4:37d3935365f8 | 18 | * Then you'll have a second map with another hashtable |
lballard9 | 4:37d3935365f8 | 19 | * You should store objects into the hashtable with different properties (spells |
lballard9 | 4:37d3935365f8 | 20 | * etc) |
rconnorlawson | 0:35660d7952f7 | 21 | */ |
rconnorlawson | 0:35660d7952f7 | 22 | struct Map { |
rconnorlawson | 0:35660d7952f7 | 23 | HashTable* items; |
rconnorlawson | 0:35660d7952f7 | 24 | int w, h; |
rconnorlawson | 0:35660d7952f7 | 25 | }; |
rconnorlawson | 0:35660d7952f7 | 26 | |
rconnorlawson | 0:35660d7952f7 | 27 | /** |
rconnorlawson | 0:35660d7952f7 | 28 | * Storage area for the maps. |
rconnorlawson | 0:35660d7952f7 | 29 | * This is a global variable, but can only be access from this file because it |
rconnorlawson | 0:35660d7952f7 | 30 | * is static. |
rconnorlawson | 0:35660d7952f7 | 31 | */ |
nasiromar | 6:c9695079521d | 32 | static Map map[NumMaps]; |
rconnorlawson | 0:35660d7952f7 | 33 | static int active_map; |
rconnorlawson | 0:35660d7952f7 | 34 | |
rconnorlawson | 0:35660d7952f7 | 35 | /** |
rconnorlawson | 0:35660d7952f7 | 36 | * The first step in HashTable access for the map is turning the two-dimensional |
rconnorlawson | 0:35660d7952f7 | 37 | * key information (x, y) into a one-dimensional unsigned integer. |
rconnorlawson | 0:35660d7952f7 | 38 | * This function should uniquely map (x,y) onto the space of unsigned integers. |
rconnorlawson | 0:35660d7952f7 | 39 | */ |
nasiromar | 6:c9695079521d | 40 | static unsigned XY_KEY(int X, int Y) |
nasiromar | 6:c9695079521d | 41 | { |
rconnorlawson | 0:35660d7952f7 | 42 | // TODO: Fix me! |
nasiromar | 6:c9695079521d | 43 | unsigned int key = WIDTH*X+Y; |
nasiromar | 6:c9695079521d | 44 | return key; |
rconnorlawson | 0:35660d7952f7 | 45 | } |
rconnorlawson | 0:35660d7952f7 | 46 | |
rconnorlawson | 0:35660d7952f7 | 47 | /** |
rconnorlawson | 0:35660d7952f7 | 48 | * This is the hash function actually passed into createHashTable. It takes an |
rconnorlawson | 0:35660d7952f7 | 49 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some |
rconnorlawson | 0:35660d7952f7 | 50 | * small non-negative integer). |
rconnorlawson | 0:35660d7952f7 | 51 | */ |
rconnorlawson | 0:35660d7952f7 | 52 | unsigned map_hash(unsigned key) |
rconnorlawson | 0:35660d7952f7 | 53 | { |
rconnorlawson | 0:35660d7952f7 | 54 | // TODO: Fix me! |
nasiromar | 6:c9695079521d | 55 | int hash_value = key%NumBuckets; |
nasiromar | 6:c9695079521d | 56 | return hash_value; |
rconnorlawson | 0:35660d7952f7 | 57 | } |
rconnorlawson | 0:35660d7952f7 | 58 | |
rconnorlawson | 0:35660d7952f7 | 59 | void maps_init() |
rconnorlawson | 0:35660d7952f7 | 60 | { |
nasiromar | 6:c9695079521d | 61 | // TODO: Implement! |
rconnorlawson | 0:35660d7952f7 | 62 | // Initialize hash table |
rconnorlawson | 0:35660d7952f7 | 63 | // Set width & height |
nasiromar | 6:c9695079521d | 64 | |
nasiromar | 6:c9695079521d | 65 | map[0].items = createHashTable(map_hash,NumBuckets); |
nasiromar | 6:c9695079521d | 66 | map[0].w = WIDTH; |
nasiromar | 6:c9695079521d | 67 | map[0].h = HEIGHT; |
nasiromar | 6:c9695079521d | 68 | |
nasiromar | 6:c9695079521d | 69 | map[1].items = createHashTable(map_hash,NumBuckets); |
nasiromar | 6:c9695079521d | 70 | map[1].w = WIDTH-25; |
nasiromar | 6:c9695079521d | 71 | map[1].h = HEIGHT-25; |
nasiromar | 6:c9695079521d | 72 | |
nasiromar | 6:c9695079521d | 73 | |
rconnorlawson | 0:35660d7952f7 | 74 | } |
rconnorlawson | 0:35660d7952f7 | 75 | |
rconnorlawson | 0:35660d7952f7 | 76 | Map* get_active_map() |
rconnorlawson | 0:35660d7952f7 | 77 | { |
rconnorlawson | 0:35660d7952f7 | 78 | // There's only one map |
nasiromar | 6:c9695079521d | 79 | return &map[active_map]; |
rconnorlawson | 0:35660d7952f7 | 80 | } |
rconnorlawson | 0:35660d7952f7 | 81 | |
nasiromar | 10:e18685911e84 | 82 | int get_current_map(){ |
nasiromar | 10:e18685911e84 | 83 | |
nasiromar | 10:e18685911e84 | 84 | return active_map; |
nasiromar | 10:e18685911e84 | 85 | |
nasiromar | 10:e18685911e84 | 86 | } |
nasiromar | 10:e18685911e84 | 87 | |
rconnorlawson | 0:35660d7952f7 | 88 | Map* set_active_map(int m) |
rconnorlawson | 0:35660d7952f7 | 89 | { |
rconnorlawson | 0:35660d7952f7 | 90 | active_map = m; |
nasiromar | 6:c9695079521d | 91 | return &map[active_map]; |
rconnorlawson | 0:35660d7952f7 | 92 | } |
rconnorlawson | 0:35660d7952f7 | 93 | |
rconnorlawson | 0:35660d7952f7 | 94 | void print_map() |
rconnorlawson | 0:35660d7952f7 | 95 | { |
rconnorlawson | 0:35660d7952f7 | 96 | // As you add more types, you'll need to add more items to this array. |
rconnorlawson | 0:35660d7952f7 | 97 | char lookup[] = {'W', 'P'}; |
nasiromar | 6:c9695079521d | 98 | for(int y = 0; y < map_height(); y++) { |
nasiromar | 6:c9695079521d | 99 | for (int x = 0; x < map_width(); x++) { |
rconnorlawson | 0:35660d7952f7 | 100 | MapItem* item = get_here(x,y); |
rconnorlawson | 0:35660d7952f7 | 101 | if (item) pc.printf("%c", lookup[item->type]); |
rconnorlawson | 0:35660d7952f7 | 102 | else pc.printf(" "); |
rconnorlawson | 0:35660d7952f7 | 103 | } |
rconnorlawson | 0:35660d7952f7 | 104 | pc.printf("\r\n"); |
rconnorlawson | 0:35660d7952f7 | 105 | } |
rconnorlawson | 0:35660d7952f7 | 106 | } |
rconnorlawson | 0:35660d7952f7 | 107 | |
rconnorlawson | 0:35660d7952f7 | 108 | int map_width() |
rconnorlawson | 0:35660d7952f7 | 109 | { |
nasiromar | 6:c9695079521d | 110 | return get_active_map()->w; |
rconnorlawson | 0:35660d7952f7 | 111 | } |
rconnorlawson | 0:35660d7952f7 | 112 | |
rconnorlawson | 0:35660d7952f7 | 113 | int map_height() |
rconnorlawson | 0:35660d7952f7 | 114 | { |
nasiromar | 6:c9695079521d | 115 | return get_active_map()->h; |
rconnorlawson | 0:35660d7952f7 | 116 | } |
rconnorlawson | 0:35660d7952f7 | 117 | |
rconnorlawson | 0:35660d7952f7 | 118 | int map_area() |
rconnorlawson | 0:35660d7952f7 | 119 | { |
nasiromar | 6:c9695079521d | 120 | int area = map_height() * map_width(); |
nasiromar | 6:c9695079521d | 121 | return area; |
rconnorlawson | 0:35660d7952f7 | 122 | } |
rconnorlawson | 0:35660d7952f7 | 123 | |
rconnorlawson | 0:35660d7952f7 | 124 | MapItem* get_north(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 125 | { |
nasiromar | 6:c9695079521d | 126 | unsigned int key = XY_KEY(x,y-1); |
nasiromar | 6:c9695079521d | 127 | MapItem* up = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 128 | return up; |
nasiromar | 6:c9695079521d | 129 | |
rconnorlawson | 0:35660d7952f7 | 130 | } |
rconnorlawson | 0:35660d7952f7 | 131 | |
rconnorlawson | 0:35660d7952f7 | 132 | MapItem* get_south(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 133 | { |
nasiromar | 6:c9695079521d | 134 | unsigned int key = XY_KEY(x,y+1); |
nasiromar | 6:c9695079521d | 135 | MapItem* down = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 136 | return down; |
nasiromar | 6:c9695079521d | 137 | |
rconnorlawson | 0:35660d7952f7 | 138 | } |
rconnorlawson | 0:35660d7952f7 | 139 | |
rconnorlawson | 0:35660d7952f7 | 140 | MapItem* get_east(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 141 | { |
nasiromar | 6:c9695079521d | 142 | unsigned int key = XY_KEY(x+1,y); |
nasiromar | 6:c9695079521d | 143 | MapItem* right = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 144 | return right; |
nasiromar | 6:c9695079521d | 145 | |
rconnorlawson | 0:35660d7952f7 | 146 | } |
rconnorlawson | 0:35660d7952f7 | 147 | |
rconnorlawson | 0:35660d7952f7 | 148 | MapItem* get_west(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 149 | { |
nasiromar | 6:c9695079521d | 150 | unsigned int key = XY_KEY(x-1,y); |
nasiromar | 6:c9695079521d | 151 | MapItem* left = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 152 | return left; |
nasiromar | 6:c9695079521d | 153 | |
rconnorlawson | 0:35660d7952f7 | 154 | } |
rconnorlawson | 0:35660d7952f7 | 155 | |
rconnorlawson | 0:35660d7952f7 | 156 | MapItem* get_here(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 157 | { |
nasiromar | 6:c9695079521d | 158 | unsigned int key = XY_KEY(x,y); |
nasiromar | 6:c9695079521d | 159 | MapItem* curr = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 160 | return curr; |
rconnorlawson | 0:35660d7952f7 | 161 | } |
rconnorlawson | 0:35660d7952f7 | 162 | |
rconnorlawson | 0:35660d7952f7 | 163 | |
rconnorlawson | 0:35660d7952f7 | 164 | void map_erase(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 165 | { |
nasiromar | 6:c9695079521d | 166 | unsigned int key = XY_KEY(x,y); |
nasiromar | 6:c9695079521d | 167 | |
nasiromar | 10:e18685911e84 | 168 | removeItem(get_active_map()->items,key); |
rconnorlawson | 0:35660d7952f7 | 169 | } |
rconnorlawson | 0:35660d7952f7 | 170 | |
rconnorlawson | 0:35660d7952f7 | 171 | void add_wall(int x, int y, int dir, int len) |
rconnorlawson | 0:35660d7952f7 | 172 | { |
nasiromar | 6:c9695079521d | 173 | for(int i = 0; i < len; i++) { |
rconnorlawson | 0:35660d7952f7 | 174 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 175 | w1->type = WALL; |
rconnorlawson | 0:35660d7952f7 | 176 | w1->draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 177 | w1->walkable = false; |
rconnorlawson | 0:35660d7952f7 | 178 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 179 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
rconnorlawson | 0:35660d7952f7 | 180 | void* val = insertItem(get_active_map()->items, key, w1); |
rconnorlawson | 0:35660d7952f7 | 181 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 182 | } |
rconnorlawson | 0:35660d7952f7 | 183 | } |
rconnorlawson | 0:35660d7952f7 | 184 | |
nasiromar | 7:862062ffca62 | 185 | void add_castle(int x, int y, int dir, int len) |
nasiromar | 6:c9695079521d | 186 | { |
nasiromar | 7:862062ffca62 | 187 | for(int i = 0; i < len; i++) { |
nasiromar | 6:c9695079521d | 188 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 6:c9695079521d | 189 | w1->type = CASTL; |
nasiromar | 6:c9695079521d | 190 | w1->draw = draw_castle; |
nasiromar | 6:c9695079521d | 191 | w1->walkable = false; |
nasiromar | 6:c9695079521d | 192 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 193 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
nasiromar | 7:862062ffca62 | 194 | void* val = insertItem(get_active_map()->items, key, w1); |
nasiromar | 6:c9695079521d | 195 | if (val) free(val); // If something is already there, free it |
nasiromar | 7:862062ffca62 | 196 | } |
nasiromar | 6:c9695079521d | 197 | } |
nasiromar | 6:c9695079521d | 198 | |
nasiromar | 9:cbb9cfb1f6c5 | 199 | MapItem*npc = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 9:cbb9cfb1f6c5 | 200 | |
nasiromar | 6:c9695079521d | 201 | |
nasiromar | 6:c9695079521d | 202 | void add_npc(int x, int y) |
nasiromar | 6:c9695079521d | 203 | { |
nasiromar | 9:cbb9cfb1f6c5 | 204 | |
nasiromar | 6:c9695079521d | 205 | npc->type = NPC; |
nasiromar | 6:c9695079521d | 206 | npc->draw = draw_npc; |
nasiromar | 6:c9695079521d | 207 | npc->walkable = false; |
nasiromar | 6:c9695079521d | 208 | npc->data = NULL; |
nasiromar | 6:c9695079521d | 209 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),npc); |
nasiromar | 6:c9695079521d | 210 | if (val) free(val); |
nasiromar | 6:c9695079521d | 211 | } |
nasiromar | 6:c9695079521d | 212 | |
nasiromar | 9:cbb9cfb1f6c5 | 213 | void delete_npc(int x, int y) |
nasiromar | 9:cbb9cfb1f6c5 | 214 | { |
nasiromar | 10:e18685911e84 | 215 | map_erase(x,y); |
nasiromar | 10:e18685911e84 | 216 | } |
nasiromar | 9:cbb9cfb1f6c5 | 217 | |
nasiromar | 6:c9695079521d | 218 | void add_portal(int x, int y) |
nasiromar | 6:c9695079521d | 219 | { |
nasiromar | 6:c9695079521d | 220 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 6:c9695079521d | 221 | w1->type = PORTAL; |
nasiromar | 6:c9695079521d | 222 | w1->draw = draw_portal; |
nasiromar | 6:c9695079521d | 223 | w1->walkable = true; |
nasiromar | 6:c9695079521d | 224 | w1->data = NULL; |
nasiromar | 6:c9695079521d | 225 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 6:c9695079521d | 226 | if (val) free(val); |
nasiromar | 6:c9695079521d | 227 | } |
nasiromar | 6:c9695079521d | 228 | |
nasiromar | 7:862062ffca62 | 229 | void add_kindom(int x, int y) |
nasiromar | 7:862062ffca62 | 230 | { |
nasiromar | 7:862062ffca62 | 231 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 7:862062ffca62 | 232 | w1->type = KINDOM; |
nasiromar | 7:862062ffca62 | 233 | w1->draw = draw_kindom; |
nasiromar | 7:862062ffca62 | 234 | w1->walkable = false; |
nasiromar | 7:862062ffca62 | 235 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 236 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 7:862062ffca62 | 237 | if (val) free(val); |
nasiromar | 7:862062ffca62 | 238 | } |
nasiromar | 7:862062ffca62 | 239 | |
nasiromar | 7:862062ffca62 | 240 | |
nasiromar | 7:862062ffca62 | 241 | void add_portal2(int x, int y) |
nasiromar | 7:862062ffca62 | 242 | { |
nasiromar | 7:862062ffca62 | 243 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 7:862062ffca62 | 244 | w1->type = PORTAl; |
nasiromar | 7:862062ffca62 | 245 | w1->draw = draw_portal2; |
nasiromar | 7:862062ffca62 | 246 | w1->walkable = true; |
nasiromar | 7:862062ffca62 | 247 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 248 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 7:862062ffca62 | 249 | if (val) free(val); |
nasiromar | 7:862062ffca62 | 250 | } |
nasiromar | 7:862062ffca62 | 251 | |
nasiromar | 7:862062ffca62 | 252 | |
nasiromar | 6:c9695079521d | 253 | void add_chest(int x, int y) |
nasiromar | 6:c9695079521d | 254 | { |
nasiromar | 6:c9695079521d | 255 | MapItem*chest = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 6:c9695079521d | 256 | chest->type = CHEST; |
nasiromar | 6:c9695079521d | 257 | chest->draw = draw_chest; |
nasiromar | 6:c9695079521d | 258 | chest->walkable = false; |
nasiromar | 6:c9695079521d | 259 | chest->data = NULL; |
nasiromar | 6:c9695079521d | 260 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),chest); |
nasiromar | 6:c9695079521d | 261 | if (val) free(val); |
nasiromar | 6:c9695079521d | 262 | } |
nasiromar | 6:c9695079521d | 263 | |
nasiromar | 7:862062ffca62 | 264 | void add_door(int x, int y) |
nasiromar | 7:862062ffca62 | 265 | { |
nasiromar | 7:862062ffca62 | 266 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 7:862062ffca62 | 267 | w1->type = DOOR; |
nasiromar | 7:862062ffca62 | 268 | w1->draw = draw_door; |
nasiromar | 7:862062ffca62 | 269 | w1->walkable = false; |
nasiromar | 7:862062ffca62 | 270 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 271 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 7:862062ffca62 | 272 | if (val) free(val); |
nasiromar | 7:862062ffca62 | 273 | } |
nasiromar | 7:862062ffca62 | 274 | |
nasiromar | 7:862062ffca62 | 275 | void add_dragon(int x, int y) |
nasiromar | 7:862062ffca62 | 276 | { |
nasiromar | 7:862062ffca62 | 277 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 7:862062ffca62 | 278 | w1->type = DRAGON; |
nasiromar | 7:862062ffca62 | 279 | w1->draw = draw_dragon; |
nasiromar | 7:862062ffca62 | 280 | w1->walkable = false; |
nasiromar | 7:862062ffca62 | 281 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 282 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 7:862062ffca62 | 283 | if (val) free(val); |
nasiromar | 7:862062ffca62 | 284 | } |
nasiromar | 6:c9695079521d | 285 | |
nasiromar | 6:c9695079521d | 286 | |
nasiromar | 6:c9695079521d | 287 | |
nasiromar | 6:c9695079521d | 288 | |
rconnorlawson | 0:35660d7952f7 | 289 | void add_plant(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 290 | { |
rconnorlawson | 0:35660d7952f7 | 291 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 292 | w1->type = PLANT; |
rconnorlawson | 0:35660d7952f7 | 293 | w1->draw = draw_plant; |
rconnorlawson | 0:35660d7952f7 | 294 | w1->walkable = true; |
nasiromar | 9:cbb9cfb1f6c5 | 295 | w1->data = FRUIT; |
rconnorlawson | 0:35660d7952f7 | 296 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
rconnorlawson | 0:35660d7952f7 | 297 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 298 | } |