Finished V1
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@4:2297a714936f, 2019-11-19 (annotated)
- Committer:
- trich9
- Date:
- Tue Nov 19 16:53:47 2019 +0000
- Revision:
- 4:2297a714936f
- Parent:
- 0:35660d7952f7
- Child:
- 5:2fb023cdc666
11/19/2019;
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 | |
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 | */ |
rconnorlawson | 0:35660d7952f7 | 20 | static Map map; |
rconnorlawson | 0:35660d7952f7 | 21 | static int active_map; |
rconnorlawson | 0:35660d7952f7 | 22 | |
trich9 | 4:2297a714936f | 23 | //MYCODE |
trich9 | 4:2297a714936f | 24 | static Map map2; |
trich9 | 4:2297a714936f | 25 | |
trich9 | 4:2297a714936f | 26 | //array of maps |
trich9 | 4:2297a714936f | 27 | static Map maps[2]; |
trich9 | 4:2297a714936f | 28 | |
trich9 | 4:2297a714936f | 29 | |
rconnorlawson | 0:35660d7952f7 | 30 | /** |
rconnorlawson | 0:35660d7952f7 | 31 | * The first step in HashTable access for the map is turning the two-dimensional |
rconnorlawson | 0:35660d7952f7 | 32 | * key information (x, y) into a one-dimensional unsigned integer. |
rconnorlawson | 0:35660d7952f7 | 33 | * This function should uniquely map (x,y) onto the space of unsigned integers. |
rconnorlawson | 0:35660d7952f7 | 34 | */ |
rconnorlawson | 0:35660d7952f7 | 35 | static unsigned XY_KEY(int X, int Y) { |
rconnorlawson | 0:35660d7952f7 | 36 | // TODO: Fix me! |
trich9 | 4:2297a714936f | 37 | |
trich9 | 4:2297a714936f | 38 | //MYCODE |
trich9 | 4:2297a714936f | 39 | return ((X*map.h) + Y); //multiplies X by height of map to get the row, then adds y for the column |
rconnorlawson | 0:35660d7952f7 | 40 | } |
rconnorlawson | 0:35660d7952f7 | 41 | |
rconnorlawson | 0:35660d7952f7 | 42 | /** |
rconnorlawson | 0:35660d7952f7 | 43 | * This is the hash function actually passed into createHashTable. It takes an |
rconnorlawson | 0:35660d7952f7 | 44 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some |
rconnorlawson | 0:35660d7952f7 | 45 | * small non-negative integer). |
rconnorlawson | 0:35660d7952f7 | 46 | */ |
rconnorlawson | 0:35660d7952f7 | 47 | unsigned map_hash(unsigned key) |
rconnorlawson | 0:35660d7952f7 | 48 | { |
rconnorlawson | 0:35660d7952f7 | 49 | // TODO: Fix me! |
trich9 | 4:2297a714936f | 50 | |
trich9 | 4:2297a714936f | 51 | //MYCODE |
trich9 | 4:2297a714936f | 52 | return key%NUMBUCKETS; //NB = 75 |
rconnorlawson | 0:35660d7952f7 | 53 | } |
rconnorlawson | 0:35660d7952f7 | 54 | |
rconnorlawson | 0:35660d7952f7 | 55 | void maps_init() |
rconnorlawson | 0:35660d7952f7 | 56 | { |
rconnorlawson | 0:35660d7952f7 | 57 | // TODO: Implement! |
rconnorlawson | 0:35660d7952f7 | 58 | // Initialize hash table |
rconnorlawson | 0:35660d7952f7 | 59 | // Set width & height |
trich9 | 4:2297a714936f | 60 | |
trich9 | 4:2297a714936f | 61 | //MYCODE |
trich9 | 4:2297a714936f | 62 | map.items = createHashTable(map_hash, NUMBUCKETS);//NB = 75 |
trich9 | 4:2297a714936f | 63 | map.w = MAPWIDTH; //75 |
trich9 | 4:2297a714936f | 64 | map.h = MAPHEIGHT; //75 |
trich9 | 4:2297a714936f | 65 | maps[0] = map; |
trich9 | 4:2297a714936f | 66 | |
trich9 | 4:2297a714936f | 67 | map2.items = createHashTable(map_hash, NUMBUCKETS); |
trich9 | 4:2297a714936f | 68 | map2.w = MAP2WIDTH; //20 |
trich9 | 4:2297a714936f | 69 | map2.h = MAP2HEIGHT; //10 |
trich9 | 4:2297a714936f | 70 | maps[1] = map2; |
rconnorlawson | 0:35660d7952f7 | 71 | } |
rconnorlawson | 0:35660d7952f7 | 72 | |
rconnorlawson | 0:35660d7952f7 | 73 | Map* get_active_map() |
rconnorlawson | 0:35660d7952f7 | 74 | { |
trich9 | 4:2297a714936f | 75 | //OGCODE |
rconnorlawson | 0:35660d7952f7 | 76 | // There's only one map |
trich9 | 4:2297a714936f | 77 | //return ↦ |
trich9 | 4:2297a714936f | 78 | |
trich9 | 4:2297a714936f | 79 | return &(maps[active_map]); |
trich9 | 4:2297a714936f | 80 | |
trich9 | 4:2297a714936f | 81 | |
trich9 | 4:2297a714936f | 82 | |
rconnorlawson | 0:35660d7952f7 | 83 | } |
rconnorlawson | 0:35660d7952f7 | 84 | |
rconnorlawson | 0:35660d7952f7 | 85 | Map* set_active_map(int m) |
rconnorlawson | 0:35660d7952f7 | 86 | { |
trich9 | 4:2297a714936f | 87 | //OGCODE |
trich9 | 4:2297a714936f | 88 | //active_map = m; |
trich9 | 4:2297a714936f | 89 | //return ↦ |
trich9 | 4:2297a714936f | 90 | |
rconnorlawson | 0:35660d7952f7 | 91 | active_map = m; |
trich9 | 4:2297a714936f | 92 | return &(maps[m]); |
trich9 | 4:2297a714936f | 93 | |
rconnorlawson | 0:35660d7952f7 | 94 | } |
rconnorlawson | 0:35660d7952f7 | 95 | |
rconnorlawson | 0:35660d7952f7 | 96 | void print_map() |
rconnorlawson | 0:35660d7952f7 | 97 | { |
rconnorlawson | 0:35660d7952f7 | 98 | // As you add more types, you'll need to add more items to this array. |
rconnorlawson | 0:35660d7952f7 | 99 | char lookup[] = {'W', 'P'}; |
rconnorlawson | 0:35660d7952f7 | 100 | for(int y = 0; y < map_height(); y++) |
rconnorlawson | 0:35660d7952f7 | 101 | { |
rconnorlawson | 0:35660d7952f7 | 102 | for (int x = 0; x < map_width(); x++) |
rconnorlawson | 0:35660d7952f7 | 103 | { |
rconnorlawson | 0:35660d7952f7 | 104 | MapItem* item = get_here(x,y); |
rconnorlawson | 0:35660d7952f7 | 105 | if (item) pc.printf("%c", lookup[item->type]); |
rconnorlawson | 0:35660d7952f7 | 106 | else pc.printf(" "); |
rconnorlawson | 0:35660d7952f7 | 107 | } |
rconnorlawson | 0:35660d7952f7 | 108 | pc.printf("\r\n"); |
rconnorlawson | 0:35660d7952f7 | 109 | } |
rconnorlawson | 0:35660d7952f7 | 110 | } |
rconnorlawson | 0:35660d7952f7 | 111 | |
trich9 | 4:2297a714936f | 112 | |
trich9 | 4:2297a714936f | 113 | //MYCODE |
rconnorlawson | 0:35660d7952f7 | 114 | int map_width() |
rconnorlawson | 0:35660d7952f7 | 115 | { |
trich9 | 4:2297a714936f | 116 | //need to get active map |
trich9 | 4:2297a714936f | 117 | Map *map = get_active_map(); |
trich9 | 4:2297a714936f | 118 | return map->w; |
rconnorlawson | 0:35660d7952f7 | 119 | } |
rconnorlawson | 0:35660d7952f7 | 120 | |
trich9 | 4:2297a714936f | 121 | //MYCODE |
rconnorlawson | 0:35660d7952f7 | 122 | int map_height() |
rconnorlawson | 0:35660d7952f7 | 123 | { |
trich9 | 4:2297a714936f | 124 | Map *map = get_active_map(); |
trich9 | 4:2297a714936f | 125 | return map->h; |
rconnorlawson | 0:35660d7952f7 | 126 | } |
rconnorlawson | 0:35660d7952f7 | 127 | |
trich9 | 4:2297a714936f | 128 | //MYCODE |
rconnorlawson | 0:35660d7952f7 | 129 | int map_area() |
rconnorlawson | 0:35660d7952f7 | 130 | { |
trich9 | 4:2297a714936f | 131 | |
trich9 | 4:2297a714936f | 132 | Map *map = get_active_map(); |
trich9 | 4:2297a714936f | 133 | return (map->h * map->w); |
rconnorlawson | 0:35660d7952f7 | 134 | } |
rconnorlawson | 0:35660d7952f7 | 135 | |
trich9 | 4:2297a714936f | 136 | //MYCODE |
rconnorlawson | 0:35660d7952f7 | 137 | MapItem* get_north(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 138 | { |
trich9 | 4:2297a714936f | 139 | Map *thisMap = get_active_map(); |
trich9 | 4:2297a714936f | 140 | int thisKey = XY_KEY(x, y-1); |
trich9 | 4:2297a714936f | 141 | return (MapItem*) getItem(thisMap->items,thisKey); |
rconnorlawson | 0:35660d7952f7 | 142 | } |
rconnorlawson | 0:35660d7952f7 | 143 | |
rconnorlawson | 0:35660d7952f7 | 144 | MapItem* get_south(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 145 | { |
trich9 | 4:2297a714936f | 146 | Map *thisMap = get_active_map(); |
trich9 | 4:2297a714936f | 147 | int thisKey = XY_KEY(x, y+1); |
trich9 | 4:2297a714936f | 148 | return (MapItem*) getItem(thisMap->items,thisKey); |
rconnorlawson | 0:35660d7952f7 | 149 | } |
rconnorlawson | 0:35660d7952f7 | 150 | |
rconnorlawson | 0:35660d7952f7 | 151 | MapItem* get_east(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 152 | { |
trich9 | 4:2297a714936f | 153 | Map *thisMap = get_active_map(); |
trich9 | 4:2297a714936f | 154 | int thisKey = XY_KEY(x+1, y); |
trich9 | 4:2297a714936f | 155 | return (MapItem*) getItem(thisMap->items,thisKey); |
rconnorlawson | 0:35660d7952f7 | 156 | } |
rconnorlawson | 0:35660d7952f7 | 157 | |
rconnorlawson | 0:35660d7952f7 | 158 | MapItem* get_west(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 159 | { |
trich9 | 4:2297a714936f | 160 | Map *thisMap = get_active_map(); |
trich9 | 4:2297a714936f | 161 | int thisKey = XY_KEY(x-1, y); |
trich9 | 4:2297a714936f | 162 | return (MapItem*) getItem(thisMap->items,thisKey); |
rconnorlawson | 0:35660d7952f7 | 163 | } |
rconnorlawson | 0:35660d7952f7 | 164 | |
rconnorlawson | 0:35660d7952f7 | 165 | MapItem* get_here(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 166 | { |
trich9 | 4:2297a714936f | 167 | Map *thisMap = get_active_map(); |
trich9 | 4:2297a714936f | 168 | int thisKey = XY_KEY(x, y); |
trich9 | 4:2297a714936f | 169 | return (MapItem*) getItem(thisMap->items,thisKey); |
rconnorlawson | 0:35660d7952f7 | 170 | } |
rconnorlawson | 0:35660d7952f7 | 171 | |
rconnorlawson | 0:35660d7952f7 | 172 | |
rconnorlawson | 0:35660d7952f7 | 173 | void map_erase(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 174 | { |
trich9 | 4:2297a714936f | 175 | Map *thisMap = get_active_map(); |
trich9 | 4:2297a714936f | 176 | int thisKey = XY_KEY(x, y); |
trich9 | 4:2297a714936f | 177 | deleteItem(thisMap->items,thisKey); |
rconnorlawson | 0:35660d7952f7 | 178 | } |
rconnorlawson | 0:35660d7952f7 | 179 | |
rconnorlawson | 0:35660d7952f7 | 180 | void add_wall(int x, int y, int dir, int len) |
rconnorlawson | 0:35660d7952f7 | 181 | { |
rconnorlawson | 0:35660d7952f7 | 182 | for(int i = 0; i < len; i++) |
rconnorlawson | 0:35660d7952f7 | 183 | { |
rconnorlawson | 0:35660d7952f7 | 184 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 185 | w1->type = WALL; |
rconnorlawson | 0:35660d7952f7 | 186 | w1->draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 187 | w1->walkable = false; |
rconnorlawson | 0:35660d7952f7 | 188 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 189 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
rconnorlawson | 0:35660d7952f7 | 190 | void* val = insertItem(get_active_map()->items, key, w1); |
rconnorlawson | 0:35660d7952f7 | 191 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 192 | } |
rconnorlawson | 0:35660d7952f7 | 193 | } |
rconnorlawson | 0:35660d7952f7 | 194 | |
rconnorlawson | 0:35660d7952f7 | 195 | void add_plant(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 196 | { |
rconnorlawson | 0:35660d7952f7 | 197 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 198 | w1->type = PLANT; |
rconnorlawson | 0:35660d7952f7 | 199 | w1->draw = draw_plant; |
rconnorlawson | 0:35660d7952f7 | 200 | w1->walkable = true; |
rconnorlawson | 0:35660d7952f7 | 201 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 202 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
rconnorlawson | 0:35660d7952f7 | 203 | if (val) free(val); // If something is already there, free it |
trich9 | 4:2297a714936f | 204 | } |
trich9 | 4:2297a714936f | 205 | |
trich9 | 4:2297a714936f | 206 | //MYCODE |
trich9 | 4:2297a714936f | 207 | void add_NPC(int x, int y) |
trich9 | 4:2297a714936f | 208 | { |
trich9 | 4:2297a714936f | 209 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 4:2297a714936f | 210 | w1->type = NPC; |
trich9 | 4:2297a714936f | 211 | w1->draw = draw_NPC; |
trich9 | 4:2297a714936f | 212 | w1->walkable = false; |
trich9 | 4:2297a714936f | 213 | w1->data = NULL; |
trich9 | 4:2297a714936f | 214 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 4:2297a714936f | 215 | if (val) free(val); // If something is already there, free it |
trich9 | 4:2297a714936f | 216 | } |
trich9 | 4:2297a714936f | 217 | |
trich9 | 4:2297a714936f | 218 | void add_ladder(int x, int y){ |
trich9 | 4:2297a714936f | 219 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 4:2297a714936f | 220 | w1->type = ladder; |
trich9 | 4:2297a714936f | 221 | w1->draw = draw_ladder; |
trich9 | 4:2297a714936f | 222 | w1->walkable = true; |
trich9 | 4:2297a714936f | 223 | w1->data = NULL; |
trich9 | 4:2297a714936f | 224 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 4:2297a714936f | 225 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 226 | } |