2035 project 2
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@9:f3c1d9f78c44, 2021-11-19 (annotated)
- Committer:
- ranroun3
- Date:
- Fri Nov 19 04:41:20 2021 +0000
- Revision:
- 9:f3c1d9f78c44
- Parent:
- 8:89dee5f187ba
- Child:
- 11:8f55aebf67a4
added spells
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" |
ranroun3 | 6:291aef457c4e | 4 | #include "hash_table.h" |
rconnorlawson | 0:35660d7952f7 | 5 | #include "graphics.h" |
rconnorlawson | 0:35660d7952f7 | 6 | |
rconnorlawson | 0:35660d7952f7 | 7 | /** |
rconnorlawson | 0:35660d7952f7 | 8 | * The Map structure. This holds a HashTable for all the MapItems, along with |
rconnorlawson | 0:35660d7952f7 | 9 | * values for the width and height of the Map. |
lballard9 | 4:37d3935365f8 | 10 | * In this file you need to define how the map will be structured. IE how will |
lballard9 | 4:37d3935365f8 | 11 | * you put values into the map, pull them from the map. Remember a "Map" variable |
ranroun3 | 8:89dee5f187ba | 12 | * is a hashtable plus two ints (see below) |
lballard9 | 4:37d3935365f8 | 13 | * You will have more than one map variable, one will be the main map with it's own hashtable. |
lballard9 | 4:37d3935365f8 | 14 | * Then you'll have a second map with another hashtable |
lballard9 | 4:37d3935365f8 | 15 | * You should store objects into the hashtable with different properties (spells |
lballard9 | 4:37d3935365f8 | 16 | * etc) |
rconnorlawson | 0:35660d7952f7 | 17 | */ |
rconnorlawson | 0:35660d7952f7 | 18 | struct Map { |
rconnorlawson | 0:35660d7952f7 | 19 | HashTable* items; |
rconnorlawson | 0:35660d7952f7 | 20 | int w, h; |
rconnorlawson | 0:35660d7952f7 | 21 | }; |
rconnorlawson | 0:35660d7952f7 | 22 | |
rconnorlawson | 0:35660d7952f7 | 23 | /** |
rconnorlawson | 0:35660d7952f7 | 24 | * Storage area for the maps. |
rconnorlawson | 0:35660d7952f7 | 25 | * This is a global variable, but can only be access from this file because it |
rconnorlawson | 0:35660d7952f7 | 26 | * is static. |
rconnorlawson | 0:35660d7952f7 | 27 | */ |
ranroun3 | 6:291aef457c4e | 28 | static Map map[3]; |
rconnorlawson | 0:35660d7952f7 | 29 | static int active_map; |
rconnorlawson | 0:35660d7952f7 | 30 | |
ranroun3 | 6:291aef457c4e | 31 | |
rconnorlawson | 0:35660d7952f7 | 32 | /** |
rconnorlawson | 0:35660d7952f7 | 33 | * The first step in HashTable access for the map is turning the two-dimensional |
rconnorlawson | 0:35660d7952f7 | 34 | * key information (x, y) into a one-dimensional unsigned integer. |
rconnorlawson | 0:35660d7952f7 | 35 | * This function should uniquely map (x,y) onto the space of unsigned integers. |
rconnorlawson | 0:35660d7952f7 | 36 | */ |
ranroun3 | 8:89dee5f187ba | 37 | static unsigned XY_KEY(int X, int Y) |
ranroun3 | 8:89dee5f187ba | 38 | { |
ranroun3 | 6:291aef457c4e | 39 | return (Y * map_width()) + X; |
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 | { |
ranroun3 | 6:291aef457c4e | 49 | return key % NUM_BUCKETS; |
rconnorlawson | 0:35660d7952f7 | 50 | } |
rconnorlawson | 0:35660d7952f7 | 51 | |
rconnorlawson | 0:35660d7952f7 | 52 | void maps_init() |
rconnorlawson | 0:35660d7952f7 | 53 | { |
ranroun3 | 8:89dee5f187ba | 54 | |
ranroun3 | 6:291aef457c4e | 55 | |
ranroun3 | 6:291aef457c4e | 56 | map[0].items = createHashTable(map_hash, NUM_BUCKETS); |
ranroun3 | 6:291aef457c4e | 57 | map[0].w = mainMapWidth; |
ranroun3 | 6:291aef457c4e | 58 | map[0].h = mainMapHeight; |
ranroun3 | 6:291aef457c4e | 59 | map[1].items = createHashTable(map_hash, NUM_BUCKETS); |
ranroun3 | 6:291aef457c4e | 60 | map[1].w = bossMapWidth; |
ranroun3 | 6:291aef457c4e | 61 | map[1].h = bossMapHeight; |
ranroun3 | 6:291aef457c4e | 62 | map[2].items = createHashTable(map_hash, NUM_BUCKETS); |
ranroun3 | 6:291aef457c4e | 63 | map[2].w = map3Width; |
ranroun3 | 6:291aef457c4e | 64 | map[2].h = map3Height; |
ranroun3 | 8:89dee5f187ba | 65 | |
ranroun3 | 8:89dee5f187ba | 66 | // TODO: Implement! |
rconnorlawson | 0:35660d7952f7 | 67 | // Initialize hash table |
rconnorlawson | 0:35660d7952f7 | 68 | // Set width & height |
rconnorlawson | 0:35660d7952f7 | 69 | } |
rconnorlawson | 0:35660d7952f7 | 70 | |
rconnorlawson | 0:35660d7952f7 | 71 | Map* get_active_map() |
rconnorlawson | 0:35660d7952f7 | 72 | { |
ranroun3 | 8:89dee5f187ba | 73 | |
ranroun3 | 6:291aef457c4e | 74 | return &map[active_map]; |
rconnorlawson | 0:35660d7952f7 | 75 | } |
rconnorlawson | 0:35660d7952f7 | 76 | |
rconnorlawson | 0:35660d7952f7 | 77 | Map* set_active_map(int m) |
rconnorlawson | 0:35660d7952f7 | 78 | { |
rconnorlawson | 0:35660d7952f7 | 79 | active_map = m; |
ranroun3 | 6:291aef457c4e | 80 | return &map[active_map]; |
rconnorlawson | 0:35660d7952f7 | 81 | } |
rconnorlawson | 0:35660d7952f7 | 82 | |
rconnorlawson | 0:35660d7952f7 | 83 | void print_map() |
rconnorlawson | 0:35660d7952f7 | 84 | { |
rconnorlawson | 0:35660d7952f7 | 85 | // As you add more types, you'll need to add more items to this array. |
rconnorlawson | 0:35660d7952f7 | 86 | char lookup[] = {'W', 'P'}; |
ranroun3 | 8:89dee5f187ba | 87 | for(int y = 0; y < map_height(); y++) { |
ranroun3 | 8:89dee5f187ba | 88 | for (int x = 0; x < map_width(); x++) { |
rconnorlawson | 0:35660d7952f7 | 89 | MapItem* item = get_here(x,y); |
rconnorlawson | 0:35660d7952f7 | 90 | if (item) pc.printf("%c", lookup[item->type]); |
rconnorlawson | 0:35660d7952f7 | 91 | else pc.printf(" "); |
rconnorlawson | 0:35660d7952f7 | 92 | } |
rconnorlawson | 0:35660d7952f7 | 93 | pc.printf("\r\n"); |
rconnorlawson | 0:35660d7952f7 | 94 | } |
rconnorlawson | 0:35660d7952f7 | 95 | } |
rconnorlawson | 0:35660d7952f7 | 96 | |
rconnorlawson | 0:35660d7952f7 | 97 | int map_width() |
rconnorlawson | 0:35660d7952f7 | 98 | { |
ranroun3 | 6:291aef457c4e | 99 | Map* currMap = get_active_map(); |
ranroun3 | 6:291aef457c4e | 100 | return currMap->w; |
rconnorlawson | 0:35660d7952f7 | 101 | } |
rconnorlawson | 0:35660d7952f7 | 102 | |
rconnorlawson | 0:35660d7952f7 | 103 | int map_height() |
rconnorlawson | 0:35660d7952f7 | 104 | { |
ranroun3 | 6:291aef457c4e | 105 | Map* currMap = get_active_map(); |
ranroun3 | 6:291aef457c4e | 106 | return currMap->h; |
rconnorlawson | 0:35660d7952f7 | 107 | } |
rconnorlawson | 0:35660d7952f7 | 108 | |
rconnorlawson | 0:35660d7952f7 | 109 | int map_area() |
rconnorlawson | 0:35660d7952f7 | 110 | { |
ranroun3 | 6:291aef457c4e | 111 | Map* currMap = get_active_map(); |
ranroun3 | 6:291aef457c4e | 112 | int area = (currMap-> h) * currMap-> w; |
ranroun3 | 6:291aef457c4e | 113 | return area; |
rconnorlawson | 0:35660d7952f7 | 114 | } |
rconnorlawson | 0:35660d7952f7 | 115 | |
rconnorlawson | 0:35660d7952f7 | 116 | MapItem* get_north(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 117 | { |
ranroun3 | 6:291aef457c4e | 118 | MapItem* currItem = get_here(x, y - 1); |
ranroun3 | 6:291aef457c4e | 119 | return currItem; |
rconnorlawson | 0:35660d7952f7 | 120 | } |
rconnorlawson | 0:35660d7952f7 | 121 | |
rconnorlawson | 0:35660d7952f7 | 122 | MapItem* get_south(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 123 | { |
ranroun3 | 6:291aef457c4e | 124 | MapItem* currItem = get_here(x, y + 1); |
ranroun3 | 6:291aef457c4e | 125 | return currItem; |
rconnorlawson | 0:35660d7952f7 | 126 | } |
rconnorlawson | 0:35660d7952f7 | 127 | |
rconnorlawson | 0:35660d7952f7 | 128 | MapItem* get_east(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 129 | { |
ranroun3 | 6:291aef457c4e | 130 | MapItem* currItem = get_here(x + 1, y); |
ranroun3 | 6:291aef457c4e | 131 | return currItem; |
rconnorlawson | 0:35660d7952f7 | 132 | } |
rconnorlawson | 0:35660d7952f7 | 133 | |
rconnorlawson | 0:35660d7952f7 | 134 | MapItem* get_west(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 135 | { |
ranroun3 | 6:291aef457c4e | 136 | MapItem* currItem = get_here(x - 1, y); |
ranroun3 | 6:291aef457c4e | 137 | return currItem; |
rconnorlawson | 0:35660d7952f7 | 138 | } |
rconnorlawson | 0:35660d7952f7 | 139 | |
rconnorlawson | 0:35660d7952f7 | 140 | MapItem* get_here(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 141 | { |
ranroun3 | 6:291aef457c4e | 142 | Map* currMap = get_active_map(); |
ranroun3 | 6:291aef457c4e | 143 | unsigned key = XY_KEY(x, y); |
ranroun3 | 6:291aef457c4e | 144 | MapItem* currItem = (MapItem*)getItem(currMap->items, key); |
ranroun3 | 6:291aef457c4e | 145 | return currItem; |
rconnorlawson | 0:35660d7952f7 | 146 | } |
rconnorlawson | 0:35660d7952f7 | 147 | |
rconnorlawson | 0:35660d7952f7 | 148 | |
rconnorlawson | 0:35660d7952f7 | 149 | void map_erase(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 150 | { |
ranroun3 | 6:291aef457c4e | 151 | Map* curr = get_active_map(); |
ranroun3 | 6:291aef457c4e | 152 | unsigned key = XY_KEY(x, y); |
ranroun3 | 6:291aef457c4e | 153 | MapItem* currItem = (MapItem*) getItem(curr->items, key); |
ranroun3 | 8:89dee5f187ba | 154 | if(currItem) { |
ranroun3 | 6:291aef457c4e | 155 | deleteItem(curr->items, key); |
ranroun3 | 6:291aef457c4e | 156 | } |
ranroun3 | 8:89dee5f187ba | 157 | |
rconnorlawson | 0:35660d7952f7 | 158 | } |
rconnorlawson | 0:35660d7952f7 | 159 | |
rconnorlawson | 0:35660d7952f7 | 160 | void add_wall(int x, int y, int dir, int len) |
rconnorlawson | 0:35660d7952f7 | 161 | { |
ranroun3 | 8:89dee5f187ba | 162 | for(int i = 0; i < len; i++) { |
rconnorlawson | 0:35660d7952f7 | 163 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 164 | w1->type = WALL; |
rconnorlawson | 0:35660d7952f7 | 165 | w1->draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 166 | w1->walkable = false; |
rconnorlawson | 0:35660d7952f7 | 167 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 168 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
rconnorlawson | 0:35660d7952f7 | 169 | void* val = insertItem(get_active_map()->items, key, w1); |
rconnorlawson | 0:35660d7952f7 | 170 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 171 | } |
rconnorlawson | 0:35660d7952f7 | 172 | } |
rconnorlawson | 0:35660d7952f7 | 173 | |
rconnorlawson | 0:35660d7952f7 | 174 | void add_plant(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 175 | { |
rconnorlawson | 0:35660d7952f7 | 176 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 177 | w1->type = PLANT; |
rconnorlawson | 0:35660d7952f7 | 178 | w1->draw = draw_plant; |
rconnorlawson | 0:35660d7952f7 | 179 | w1->walkable = true; |
rconnorlawson | 0:35660d7952f7 | 180 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 181 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
rconnorlawson | 0:35660d7952f7 | 182 | if (val) free(val); // If something is already there, free it |
ranroun3 | 6:291aef457c4e | 183 | } |
ranroun3 | 6:291aef457c4e | 184 | |
ranroun3 | 6:291aef457c4e | 185 | void add_stairs(int x, int y, int newX, int newY, int newMap) |
ranroun3 | 6:291aef457c4e | 186 | { |
ranroun3 | 8:89dee5f187ba | 187 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ranroun3 | 6:291aef457c4e | 188 | w1->type = STAIRS; |
ranroun3 | 6:291aef457c4e | 189 | w1->draw = draw_stairs; |
ranroun3 | 6:291aef457c4e | 190 | w1->walkable = true; |
ranroun3 | 6:291aef457c4e | 191 | StairsData *s1 = (StairsData*) malloc(sizeof(StairsData)); |
ranroun3 | 6:291aef457c4e | 192 | s1 -> tx = newX; |
ranroun3 | 6:291aef457c4e | 193 | s1 -> ty = newY; |
ranroun3 | 6:291aef457c4e | 194 | s1 -> tm = newMap; |
ranroun3 | 6:291aef457c4e | 195 | s1 -> isLocked = 0; |
ranroun3 | 6:291aef457c4e | 196 | w1->data = s1; |
ranroun3 | 6:291aef457c4e | 197 | //stairs struct goes here |
ranroun3 | 6:291aef457c4e | 198 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ranroun3 | 8:89dee5f187ba | 199 | if (val) free(val); // If something is already there, free it |
ranroun3 | 8:89dee5f187ba | 200 | |
ranroun3 | 8:89dee5f187ba | 201 | } |
ranroun3 | 8:89dee5f187ba | 202 | |
ranroun3 | 8:89dee5f187ba | 203 | void add_npc(int x, int y) |
ranroun3 | 8:89dee5f187ba | 204 | { |
ranroun3 | 8:89dee5f187ba | 205 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ranroun3 | 8:89dee5f187ba | 206 | w1->type = NPC; |
ranroun3 | 8:89dee5f187ba | 207 | w1->draw = draw_npc; |
ranroun3 | 8:89dee5f187ba | 208 | w1->walkable = false; |
ranroun3 | 8:89dee5f187ba | 209 | newNPC *npc1 = (newNPC*) malloc(sizeof(newNPC)); |
ranroun3 | 8:89dee5f187ba | 210 | npc1 -> x = x; |
ranroun3 | 8:89dee5f187ba | 211 | npc1 -> y = y; |
ranroun3 | 8:89dee5f187ba | 212 | npc1 -> state = 0; |
ranroun3 | 8:89dee5f187ba | 213 | w1->data = npc1; |
ranroun3 | 8:89dee5f187ba | 214 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ranroun3 | 8:89dee5f187ba | 215 | if (val) free(val); // If something is already there, free it |
ranroun3 | 8:89dee5f187ba | 216 | } |
ranroun3 | 8:89dee5f187ba | 217 | |
ranroun3 | 8:89dee5f187ba | 218 | |
ranroun3 | 8:89dee5f187ba | 219 | void add_boss(int x, int y, int corrSpell) |
ranroun3 | 8:89dee5f187ba | 220 | { |
ranroun3 | 8:89dee5f187ba | 221 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ranroun3 | 8:89dee5f187ba | 222 | w1->type = BOSS; |
ranroun3 | 8:89dee5f187ba | 223 | w1->draw = draw_boss; |
ranroun3 | 8:89dee5f187ba | 224 | w1->walkable = false; |
ranroun3 | 9:f3c1d9f78c44 | 225 | bossChar *b1 = (bossChar*) malloc(sizeof(bossChar)); |
ranroun3 | 9:f3c1d9f78c44 | 226 | b1 -> x = x; |
ranroun3 | 9:f3c1d9f78c44 | 227 | b1 -> y = y; |
ranroun3 | 9:f3c1d9f78c44 | 228 | b1 -> state = 0; |
ranroun3 | 9:f3c1d9f78c44 | 229 | b1 -> correctSpell = corrSpell; |
ranroun3 | 9:f3c1d9f78c44 | 230 | w1->data = b1; |
ranroun3 | 9:f3c1d9f78c44 | 231 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ranroun3 | 9:f3c1d9f78c44 | 232 | if (val) free(val); // If something is already there, free it |
ranroun3 | 9:f3c1d9f78c44 | 233 | } |
ranroun3 | 9:f3c1d9f78c44 | 234 | |
ranroun3 | 9:f3c1d9f78c44 | 235 | void add_spell(int x, int y, int spellType) |
ranroun3 | 9:f3c1d9f78c44 | 236 | { |
ranroun3 | 9:f3c1d9f78c44 | 237 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ranroun3 | 9:f3c1d9f78c44 | 238 | w1->type = SPELL; |
ranroun3 | 9:f3c1d9f78c44 | 239 | |
ranroun3 | 9:f3c1d9f78c44 | 240 | w1->walkable = true; |
ranroun3 | 9:f3c1d9f78c44 | 241 | Spell *s1 = (Spell*) malloc(sizeof(Spell)); |
ranroun3 | 9:f3c1d9f78c44 | 242 | s1 -> x = x; |
ranroun3 | 9:f3c1d9f78c44 | 243 | s1 -> y = y; |
ranroun3 | 9:f3c1d9f78c44 | 244 | s1 -> spell_type = spellType; |
ranroun3 | 9:f3c1d9f78c44 | 245 | if(spellType == FIRE) { |
ranroun3 | 9:f3c1d9f78c44 | 246 | w1->draw = draw_fire; |
ranroun3 | 9:f3c1d9f78c44 | 247 | } |
ranroun3 | 9:f3c1d9f78c44 | 248 | if(spellType == WATER) { |
ranroun3 | 9:f3c1d9f78c44 | 249 | w1->draw = draw_water; |
ranroun3 | 9:f3c1d9f78c44 | 250 | } |
ranroun3 | 9:f3c1d9f78c44 | 251 | if(spellType == TACO) { |
ranroun3 | 9:f3c1d9f78c44 | 252 | w1->draw = draw_taco; |
ranroun3 | 9:f3c1d9f78c44 | 253 | } |
ranroun3 | 9:f3c1d9f78c44 | 254 | w1->data = s1; |
ranroun3 | 8:89dee5f187ba | 255 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ranroun3 | 8:89dee5f187ba | 256 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 257 | } |