ECE 2035 final project

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
npatel387
Date:
Mon Apr 15 12:25:08 2019 +0000
Revision:
2:22d36e7740f1
Parent:
0:35660d7952f7
final;

Who changed what in which revision?

UserRevisionLine numberNew 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
rconnorlawson 0:35660d7952f7 6 /**
rconnorlawson 0:35660d7952f7 7 * The Map structure. This holds a HashTable for all the MapItems, along with
rconnorlawson 0:35660d7952f7 8 * values for the width and height of the Map.
rconnorlawson 0:35660d7952f7 9 */
rconnorlawson 0:35660d7952f7 10 struct Map {
rconnorlawson 0:35660d7952f7 11 HashTable* items;
rconnorlawson 0:35660d7952f7 12 int w, h;
rconnorlawson 0:35660d7952f7 13 };
rconnorlawson 0:35660d7952f7 14
rconnorlawson 0:35660d7952f7 15 /**
rconnorlawson 0:35660d7952f7 16 * Storage area for the maps.
rconnorlawson 0:35660d7952f7 17 * This is a global variable, but can only be access from this file because it
rconnorlawson 0:35660d7952f7 18 * is static.
rconnorlawson 0:35660d7952f7 19 */
npatel387 2:22d36e7740f1 20 static Map map[NUM_MAPS];
rconnorlawson 0:35660d7952f7 21 static int active_map;
rconnorlawson 0:35660d7952f7 22
rconnorlawson 0:35660d7952f7 23 /**
rconnorlawson 0:35660d7952f7 24 * The first step in HashTable access for the map is turning the two-dimensional
rconnorlawson 0:35660d7952f7 25 * key information (x, y) into a one-dimensional unsigned integer.
rconnorlawson 0:35660d7952f7 26 * This function should uniquely map (x,y) onto the space of unsigned integers.
rconnorlawson 0:35660d7952f7 27 */
npatel387 2:22d36e7740f1 28 static unsigned XY_KEY(int X, int Y, int size) {
npatel387 2:22d36e7740f1 29 return (X+size*Y); //linearize a 2d index
rconnorlawson 0:35660d7952f7 30 }
rconnorlawson 0:35660d7952f7 31
rconnorlawson 0:35660d7952f7 32 /**
rconnorlawson 0:35660d7952f7 33 * This is the hash function actually passed into createHashTable. It takes an
rconnorlawson 0:35660d7952f7 34 * unsigned key (the output of XY_KEY) and turns it into a hash value (some
rconnorlawson 0:35660d7952f7 35 * small non-negative integer).
rconnorlawson 0:35660d7952f7 36 */
npatel387 2:22d36e7740f1 37 unsigned map_hash1(unsigned key)
rconnorlawson 0:35660d7952f7 38 {
npatel387 2:22d36e7740f1 39 return key%NUM_BUCKETS1;
npatel387 2:22d36e7740f1 40 }
npatel387 2:22d36e7740f1 41
npatel387 2:22d36e7740f1 42 unsigned map_hash2(unsigned key)
npatel387 2:22d36e7740f1 43 {
npatel387 2:22d36e7740f1 44 return key%NUM_BUCKETS2;
rconnorlawson 0:35660d7952f7 45 }
rconnorlawson 0:35660d7952f7 46
rconnorlawson 0:35660d7952f7 47 void maps_init()
rconnorlawson 0:35660d7952f7 48 {
npatel387 2:22d36e7740f1 49 map[0].items = createHashTable(map_hash1, NUM_BUCKETS1);
npatel387 2:22d36e7740f1 50 map[0].h = 50;
npatel387 2:22d36e7740f1 51 map[0].w = 50;
npatel387 2:22d36e7740f1 52 map[1].items = createHashTable(map_hash2, NUM_BUCKETS2);
npatel387 2:22d36e7740f1 53 map[1].h = 20;
npatel387 2:22d36e7740f1 54 map[1].w = 20;
rconnorlawson 0:35660d7952f7 55 }
rconnorlawson 0:35660d7952f7 56
rconnorlawson 0:35660d7952f7 57 Map* get_active_map()
rconnorlawson 0:35660d7952f7 58 {
rconnorlawson 0:35660d7952f7 59 // There's only one map
npatel387 2:22d36e7740f1 60 return &map[active_map];
rconnorlawson 0:35660d7952f7 61 }
rconnorlawson 0:35660d7952f7 62
rconnorlawson 0:35660d7952f7 63 Map* set_active_map(int m)
rconnorlawson 0:35660d7952f7 64 {
rconnorlawson 0:35660d7952f7 65 active_map = m;
npatel387 2:22d36e7740f1 66 return &map[active_map];
rconnorlawson 0:35660d7952f7 67 }
rconnorlawson 0:35660d7952f7 68
npatel387 2:22d36e7740f1 69
rconnorlawson 0:35660d7952f7 70 void print_map()
rconnorlawson 0:35660d7952f7 71 {
rconnorlawson 0:35660d7952f7 72 // As you add more types, you'll need to add more items to this array.
npatel387 2:22d36e7740f1 73 char lookup[] = {'W', 'P', 'N'};
rconnorlawson 0:35660d7952f7 74 for(int y = 0; y < map_height(); y++)
rconnorlawson 0:35660d7952f7 75 {
rconnorlawson 0:35660d7952f7 76 for (int x = 0; x < map_width(); x++)
rconnorlawson 0:35660d7952f7 77 {
rconnorlawson 0:35660d7952f7 78 MapItem* item = get_here(x,y);
rconnorlawson 0:35660d7952f7 79 if (item) pc.printf("%c", lookup[item->type]);
rconnorlawson 0:35660d7952f7 80 else pc.printf(" ");
rconnorlawson 0:35660d7952f7 81 }
rconnorlawson 0:35660d7952f7 82 pc.printf("\r\n");
rconnorlawson 0:35660d7952f7 83 }
rconnorlawson 0:35660d7952f7 84 }
rconnorlawson 0:35660d7952f7 85
rconnorlawson 0:35660d7952f7 86 int map_width()
rconnorlawson 0:35660d7952f7 87 {
npatel387 2:22d36e7740f1 88 return map[active_map].w;
rconnorlawson 0:35660d7952f7 89 }
rconnorlawson 0:35660d7952f7 90
rconnorlawson 0:35660d7952f7 91 int map_height()
rconnorlawson 0:35660d7952f7 92 {
npatel387 2:22d36e7740f1 93 return map[active_map].h;
rconnorlawson 0:35660d7952f7 94 }
rconnorlawson 0:35660d7952f7 95
rconnorlawson 0:35660d7952f7 96 int map_area()
rconnorlawson 0:35660d7952f7 97 {
npatel387 2:22d36e7740f1 98 return map[active_map].h * map[active_map].w;
rconnorlawson 0:35660d7952f7 99 }
rconnorlawson 0:35660d7952f7 100
rconnorlawson 0:35660d7952f7 101 MapItem* get_north(int x, int y)
rconnorlawson 0:35660d7952f7 102 {
npatel387 2:22d36e7740f1 103 return (MapItem*)getItem(map[active_map].items, XY_KEY(x,y-1,map_height()));
rconnorlawson 0:35660d7952f7 104 }
rconnorlawson 0:35660d7952f7 105
rconnorlawson 0:35660d7952f7 106 MapItem* get_south(int x, int y)
rconnorlawson 0:35660d7952f7 107 {
npatel387 2:22d36e7740f1 108 return (MapItem*)getItem(map[active_map].items, XY_KEY(x,y+1,map_height()));
rconnorlawson 0:35660d7952f7 109 }
rconnorlawson 0:35660d7952f7 110
rconnorlawson 0:35660d7952f7 111 MapItem* get_east(int x, int y)
rconnorlawson 0:35660d7952f7 112 {
npatel387 2:22d36e7740f1 113 return (MapItem*)getItem(map[active_map].items, XY_KEY(x+1,y,map_height()));
rconnorlawson 0:35660d7952f7 114 }
rconnorlawson 0:35660d7952f7 115
rconnorlawson 0:35660d7952f7 116 MapItem* get_west(int x, int y)
rconnorlawson 0:35660d7952f7 117 {
npatel387 2:22d36e7740f1 118 return (MapItem*)getItem(map[active_map].items, XY_KEY(x-1,y,map_height()));
rconnorlawson 0:35660d7952f7 119 }
rconnorlawson 0:35660d7952f7 120
rconnorlawson 0:35660d7952f7 121 MapItem* get_here(int x, int y)
rconnorlawson 0:35660d7952f7 122 {
npatel387 2:22d36e7740f1 123 return (MapItem*)getItem(map[active_map].items, XY_KEY(x,y, map_height()));
rconnorlawson 0:35660d7952f7 124 }
rconnorlawson 0:35660d7952f7 125
rconnorlawson 0:35660d7952f7 126
rconnorlawson 0:35660d7952f7 127 void map_erase(int x, int y)
rconnorlawson 0:35660d7952f7 128 {
npatel387 2:22d36e7740f1 129 deleteItem(map[active_map].items, XY_KEY(x,y,map_height()));
rconnorlawson 0:35660d7952f7 130 }
rconnorlawson 0:35660d7952f7 131
rconnorlawson 0:35660d7952f7 132 void add_wall(int x, int y, int dir, int len)
rconnorlawson 0:35660d7952f7 133 {
rconnorlawson 0:35660d7952f7 134 for(int i = 0; i < len; i++)
rconnorlawson 0:35660d7952f7 135 {
rconnorlawson 0:35660d7952f7 136 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
rconnorlawson 0:35660d7952f7 137 w1->type = WALL;
rconnorlawson 0:35660d7952f7 138 w1->draw = draw_wall;
rconnorlawson 0:35660d7952f7 139 w1->walkable = false;
rconnorlawson 0:35660d7952f7 140 w1->data = NULL;
npatel387 2:22d36e7740f1 141 unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y, map_height()) : XY_KEY(x, y+i, map_height());
rconnorlawson 0:35660d7952f7 142 void* val = insertItem(get_active_map()->items, key, w1);
rconnorlawson 0:35660d7952f7 143 if (val) free(val); // If something is already there, free it
rconnorlawson 0:35660d7952f7 144 }
rconnorlawson 0:35660d7952f7 145 }
rconnorlawson 0:35660d7952f7 146
rconnorlawson 0:35660d7952f7 147 void add_plant(int x, int y)
rconnorlawson 0:35660d7952f7 148 {
rconnorlawson 0:35660d7952f7 149 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
rconnorlawson 0:35660d7952f7 150 w1->type = PLANT;
rconnorlawson 0:35660d7952f7 151 w1->draw = draw_plant;
rconnorlawson 0:35660d7952f7 152 w1->walkable = true;
rconnorlawson 0:35660d7952f7 153 w1->data = NULL;
npatel387 2:22d36e7740f1 154 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
npatel387 2:22d36e7740f1 155 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 156 }
npatel387 2:22d36e7740f1 157
npatel387 2:22d36e7740f1 158 void add_plant2(int x, int y)
npatel387 2:22d36e7740f1 159 {
npatel387 2:22d36e7740f1 160 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 161 w1->type = PLANT2;
npatel387 2:22d36e7740f1 162 w1->draw = draw_plant;
npatel387 2:22d36e7740f1 163 w1->walkable = true;
npatel387 2:22d36e7740f1 164 w1->data = NULL;
npatel387 2:22d36e7740f1 165 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
npatel387 2:22d36e7740f1 166 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 167 }
npatel387 2:22d36e7740f1 168
npatel387 2:22d36e7740f1 169 void add_startNPC(int x, int y)
npatel387 2:22d36e7740f1 170 {
npatel387 2:22d36e7740f1 171 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 172 w1->type = STARTNPC;
npatel387 2:22d36e7740f1 173 w1->draw = draw_startNPC;
npatel387 2:22d36e7740f1 174 w1->walkable = false;
npatel387 2:22d36e7740f1 175 w1->data = NULL;
npatel387 2:22d36e7740f1 176 void* val = insertItem(get_active_map()->items, XY_KEY(x, y,map_height()), w1);
npatel387 2:22d36e7740f1 177 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 178 }
npatel387 2:22d36e7740f1 179
npatel387 2:22d36e7740f1 180 void add_swordInStone(int x, int y)
npatel387 2:22d36e7740f1 181 {
npatel387 2:22d36e7740f1 182 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 183 w1->type = SWORDINSTONE;
npatel387 2:22d36e7740f1 184 w1->draw = draw_swordInStone;
npatel387 2:22d36e7740f1 185 w1->walkable = false;
npatel387 2:22d36e7740f1 186 w1->data = NULL;
npatel387 2:22d36e7740f1 187 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
npatel387 2:22d36e7740f1 188 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 189 }
npatel387 2:22d36e7740f1 190
npatel387 2:22d36e7740f1 191 void add_elvarg(int x, int y)
npatel387 2:22d36e7740f1 192 {
npatel387 2:22d36e7740f1 193 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 194 w1->type = ELVARG;
npatel387 2:22d36e7740f1 195 w1->draw = draw_elvarg;
npatel387 2:22d36e7740f1 196 w1->walkable = false;
npatel387 2:22d36e7740f1 197 w1->data = NULL;
npatel387 2:22d36e7740f1 198 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
npatel387 2:22d36e7740f1 199 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 200 }
npatel387 2:22d36e7740f1 201
npatel387 2:22d36e7740f1 202 void add_cave(int x, int y)
npatel387 2:22d36e7740f1 203 {
npatel387 2:22d36e7740f1 204 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 205 w1->type = CAVE;
npatel387 2:22d36e7740f1 206 w1->draw = draw_cave;
npatel387 2:22d36e7740f1 207 w1->walkable = false;
npatel387 2:22d36e7740f1 208 w1->data = NULL;
npatel387 2:22d36e7740f1 209 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
npatel387 2:22d36e7740f1 210 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 211 }
npatel387 2:22d36e7740f1 212
npatel387 2:22d36e7740f1 213 void add_gate(int x, int y)
npatel387 2:22d36e7740f1 214 {
npatel387 2:22d36e7740f1 215 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 216 w1->type = GATE;
npatel387 2:22d36e7740f1 217 w1->draw = draw_gate;
npatel387 2:22d36e7740f1 218 w1->walkable = false;
npatel387 2:22d36e7740f1 219 w1->data = NULL;
npatel387 2:22d36e7740f1 220 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
npatel387 2:22d36e7740f1 221 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 222 }
npatel387 2:22d36e7740f1 223
npatel387 2:22d36e7740f1 224 void add_treasure(int x, int y)
npatel387 2:22d36e7740f1 225 {
npatel387 2:22d36e7740f1 226 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 227 w1->type = TREASURE;
npatel387 2:22d36e7740f1 228 w1->draw = draw_treasure;
npatel387 2:22d36e7740f1 229 w1->walkable = false;
npatel387 2:22d36e7740f1 230 w1->data = NULL;
npatel387 2:22d36e7740f1 231 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
npatel387 2:22d36e7740f1 232 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 233 }
npatel387 2:22d36e7740f1 234
npatel387 2:22d36e7740f1 235 void add_boulder(int x, int y)
npatel387 2:22d36e7740f1 236 {
npatel387 2:22d36e7740f1 237 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 238 w1->type = BOULDER;
npatel387 2:22d36e7740f1 239 w1->draw = draw_boulder;
npatel387 2:22d36e7740f1 240 w1->walkable = 2;
npatel387 2:22d36e7740f1 241 w1->data = NULL;
npatel387 2:22d36e7740f1 242 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
npatel387 2:22d36e7740f1 243 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 244 }
npatel387 2:22d36e7740f1 245
npatel387 2:22d36e7740f1 246 void add_phat(int x, int y)
npatel387 2:22d36e7740f1 247 {
npatel387 2:22d36e7740f1 248 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 249 w1->type = PHAT;
npatel387 2:22d36e7740f1 250 w1->draw = draw_phat;
npatel387 2:22d36e7740f1 251 w1->walkable = false;
npatel387 2:22d36e7740f1 252 w1->data = NULL;
npatel387 2:22d36e7740f1 253 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
npatel387 2:22d36e7740f1 254 if (val) free(val); // If something is already there, free it
npatel387 2:22d36e7740f1 255 }
npatel387 2:22d36e7740f1 256
npatel387 2:22d36e7740f1 257 void add_rolling(int x, int y)
npatel387 2:22d36e7740f1 258 {
npatel387 2:22d36e7740f1 259 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
npatel387 2:22d36e7740f1 260 w1->type = ROLLING;
npatel387 2:22d36e7740f1 261 w1->draw = draw_rolling;
npatel387 2:22d36e7740f1 262 w1->walkable = false;
npatel387 2:22d36e7740f1 263 w1->data = NULL;
npatel387 2:22d36e7740f1 264 void* val = insertItem(get_active_map()->items, XY_KEY(x, y, map_height()), w1);
rconnorlawson 0:35660d7952f7 265 if (val) free(val); // If something is already there, free it
rconnorlawson 0:35660d7952f7 266 }