Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@2:06c63d567719, 2018-11-30 (annotated)
- Committer:
- Sterofin
- Date:
- Fri Nov 30 02:53:05 2018 +0000
- Revision:
- 2:06c63d567719
- Parent:
- 0:35660d7952f7
- Child:
- 3:664c79e2ceb5
doot
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| rconnorlawson | 0:35660d7952f7 | 1 | #include "map.h" | 
| Sterofin | 2:06c63d567719 | 2 | #include "hash_table.h" | 
| 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 | */ | 
| Sterofin | 2:06c63d567719 | 20 | static Map map[2]; | 
| Sterofin | 2:06c63d567719 | 21 | //Map 2 is cyberspace | 
| rconnorlawson | 0:35660d7952f7 | 22 | static int active_map; | 
| rconnorlawson | 0:35660d7952f7 | 23 | |
| rconnorlawson | 0:35660d7952f7 | 24 | /** | 
| rconnorlawson | 0:35660d7952f7 | 25 | * The first step in HashTable access for the map is turning the two-dimensional | 
| rconnorlawson | 0:35660d7952f7 | 26 | * key information (x, y) into a one-dimensional unsigned integer. | 
| rconnorlawson | 0:35660d7952f7 | 27 | * This function should uniquely map (x,y) onto the space of unsigned integers. | 
| rconnorlawson | 0:35660d7952f7 | 28 | */ | 
| Sterofin | 2:06c63d567719 | 29 | unsigned XY_KEY(int X, int Y) { | 
| Sterofin | 2:06c63d567719 | 30 | unsigned key = X*50+Y;//0.5*(X+Y)*(X+Y+1)+Y; | 
| Sterofin | 2:06c63d567719 | 31 | return key; | 
| rconnorlawson | 0:35660d7952f7 | 32 | } | 
| rconnorlawson | 0:35660d7952f7 | 33 | |
| rconnorlawson | 0:35660d7952f7 | 34 | /** | 
| rconnorlawson | 0:35660d7952f7 | 35 | * This is the hash function actually passed into createHashTable. It takes an | 
| rconnorlawson | 0:35660d7952f7 | 36 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some | 
| rconnorlawson | 0:35660d7952f7 | 37 | * small non-negative integer). | 
| rconnorlawson | 0:35660d7952f7 | 38 | */ | 
| rconnorlawson | 0:35660d7952f7 | 39 | unsigned map_hash(unsigned key) | 
| rconnorlawson | 0:35660d7952f7 | 40 | { | 
| Sterofin | 2:06c63d567719 | 41 | unsigned hsh = key%8; | 
| Sterofin | 2:06c63d567719 | 42 | return hsh; | 
| rconnorlawson | 0:35660d7952f7 | 43 | } | 
| rconnorlawson | 0:35660d7952f7 | 44 | |
| rconnorlawson | 0:35660d7952f7 | 45 | void maps_init() | 
| rconnorlawson | 0:35660d7952f7 | 46 | { | 
| rconnorlawson | 0:35660d7952f7 | 47 | // TODO: Implement! | 
| rconnorlawson | 0:35660d7952f7 | 48 | // Initialize hash table | 
| Sterofin | 2:06c63d567719 | 49 | map[0].items = createHashTable(map_hash,8); | 
| Sterofin | 2:06c63d567719 | 50 | map[1].items = createHashTable(map_hash,8); | 
| rconnorlawson | 0:35660d7952f7 | 51 | // Set width & height | 
| Sterofin | 2:06c63d567719 | 52 | map[0].w = 50; | 
| Sterofin | 2:06c63d567719 | 53 | map[0].h = 50; | 
| Sterofin | 2:06c63d567719 | 54 | map[1].w = 11; | 
| Sterofin | 2:06c63d567719 | 55 | map[1].h = 11; | 
| rconnorlawson | 0:35660d7952f7 | 56 | } | 
| rconnorlawson | 0:35660d7952f7 | 57 | |
| rconnorlawson | 0:35660d7952f7 | 58 | Map* get_active_map() | 
| rconnorlawson | 0:35660d7952f7 | 59 | { | 
| rconnorlawson | 0:35660d7952f7 | 60 | // There's only one map | 
| Sterofin | 2:06c63d567719 | 61 | return &map[active_map]; | 
| rconnorlawson | 0:35660d7952f7 | 62 | } | 
| rconnorlawson | 0:35660d7952f7 | 63 | |
| rconnorlawson | 0:35660d7952f7 | 64 | Map* set_active_map(int m) | 
| rconnorlawson | 0:35660d7952f7 | 65 | { | 
| rconnorlawson | 0:35660d7952f7 | 66 | active_map = m; | 
| Sterofin | 2:06c63d567719 | 67 | return &map[active_map]; | 
| rconnorlawson | 0:35660d7952f7 | 68 | } | 
| rconnorlawson | 0:35660d7952f7 | 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. | 
| Sterofin | 2:06c63d567719 | 73 | char lookup[] = {'W', 'P', 'N', 'T','H'}; | 
| 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 | { | 
| Sterofin | 2:06c63d567719 | 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 | { | 
| Sterofin | 2:06c63d567719 | 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 | { | 
| Sterofin | 2:06c63d567719 | 98 | return map_width()*map_height(); | 
| rconnorlawson | 0:35660d7952f7 | 99 | } | 
| rconnorlawson | 0:35660d7952f7 | 100 | |
| rconnorlawson | 0:35660d7952f7 | 101 | MapItem* get_north(int x, int y) | 
| rconnorlawson | 0:35660d7952f7 | 102 | { | 
| Sterofin | 2:06c63d567719 | 103 | Map *m1 = get_active_map(); | 
| Sterofin | 2:06c63d567719 | 104 | MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x,y-1)); | 
| Sterofin | 2:06c63d567719 | 105 | return item; | 
| rconnorlawson | 0:35660d7952f7 | 106 | } | 
| rconnorlawson | 0:35660d7952f7 | 107 | |
| rconnorlawson | 0:35660d7952f7 | 108 | MapItem* get_south(int x, int y) | 
| rconnorlawson | 0:35660d7952f7 | 109 | { | 
| Sterofin | 2:06c63d567719 | 110 | Map *m1 = get_active_map(); | 
| Sterofin | 2:06c63d567719 | 111 | MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x,y+1)); | 
| Sterofin | 2:06c63d567719 | 112 | return item; | 
| rconnorlawson | 0:35660d7952f7 | 113 | } | 
| rconnorlawson | 0:35660d7952f7 | 114 | |
| rconnorlawson | 0:35660d7952f7 | 115 | MapItem* get_east(int x, int y) | 
| rconnorlawson | 0:35660d7952f7 | 116 | { | 
| Sterofin | 2:06c63d567719 | 117 | Map *m1 = get_active_map(); | 
| Sterofin | 2:06c63d567719 | 118 | MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x+1,y)); | 
| Sterofin | 2:06c63d567719 | 119 | return item; | 
| rconnorlawson | 0:35660d7952f7 | 120 | } | 
| rconnorlawson | 0:35660d7952f7 | 121 | |
| rconnorlawson | 0:35660d7952f7 | 122 | MapItem* get_west(int x, int y) | 
| rconnorlawson | 0:35660d7952f7 | 123 | { | 
| Sterofin | 2:06c63d567719 | 124 | Map *m1 = get_active_map(); | 
| Sterofin | 2:06c63d567719 | 125 | MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x-1,y)); | 
| Sterofin | 2:06c63d567719 | 126 | return item; | 
| rconnorlawson | 0:35660d7952f7 | 127 | } | 
| rconnorlawson | 0:35660d7952f7 | 128 | |
| rconnorlawson | 0:35660d7952f7 | 129 | MapItem* get_here(int x, int y) | 
| rconnorlawson | 0:35660d7952f7 | 130 | { | 
| Sterofin | 2:06c63d567719 | 131 | Map *m1 = get_active_map(); | 
| Sterofin | 2:06c63d567719 | 132 | MapItem* here = (MapItem*)getItem(m1->items,XY_KEY(x,y)); | 
| Sterofin | 2:06c63d567719 | 133 | return here; | 
| rconnorlawson | 0:35660d7952f7 | 134 | } | 
| rconnorlawson | 0:35660d7952f7 | 135 | |
| rconnorlawson | 0:35660d7952f7 | 136 | |
| rconnorlawson | 0:35660d7952f7 | 137 | void map_erase(int x, int y) | 
| rconnorlawson | 0:35660d7952f7 | 138 | { | 
| Sterofin | 2:06c63d567719 | 139 | deleteItem(map[0].items,XY_KEY(x,y)); | 
| Sterofin | 2:06c63d567719 | 140 | deleteItem(map[1].items,XY_KEY(x,y)); | 
| rconnorlawson | 0:35660d7952f7 | 141 | } | 
| rconnorlawson | 0:35660d7952f7 | 142 | |
| rconnorlawson | 0:35660d7952f7 | 143 | void add_wall(int x, int y, int dir, int len) | 
| rconnorlawson | 0:35660d7952f7 | 144 | { | 
| rconnorlawson | 0:35660d7952f7 | 145 | for(int i = 0; i < len; i++) | 
| rconnorlawson | 0:35660d7952f7 | 146 | { | 
| rconnorlawson | 0:35660d7952f7 | 147 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| rconnorlawson | 0:35660d7952f7 | 148 | w1->type = WALL; | 
| rconnorlawson | 0:35660d7952f7 | 149 | w1->draw = draw_wall; | 
| rconnorlawson | 0:35660d7952f7 | 150 | w1->walkable = false; | 
| rconnorlawson | 0:35660d7952f7 | 151 | w1->data = NULL; | 
| rconnorlawson | 0:35660d7952f7 | 152 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); | 
| rconnorlawson | 0:35660d7952f7 | 153 | void* val = insertItem(get_active_map()->items, key, w1); | 
| rconnorlawson | 0:35660d7952f7 | 154 | if (val) free(val); // If something is already there, free it | 
| rconnorlawson | 0:35660d7952f7 | 155 | } | 
| rconnorlawson | 0:35660d7952f7 | 156 | } | 
| rconnorlawson | 0:35660d7952f7 | 157 | |
| Sterofin | 2:06c63d567719 | 158 | void add_netPortal(int x, int y) | 
| Sterofin | 2:06c63d567719 | 159 | { | 
| Sterofin | 2:06c63d567719 | 160 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| Sterofin | 2:06c63d567719 | 161 | w1->type = NPORTAL; | 
| Sterofin | 2:06c63d567719 | 162 | w1->draw = draw_netPortal; | 
| Sterofin | 2:06c63d567719 | 163 | w1->walkable = false; | 
| Sterofin | 2:06c63d567719 | 164 | w1->data = NULL; | 
| Sterofin | 2:06c63d567719 | 165 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| Sterofin | 2:06c63d567719 | 166 | if (val) free(val); // If something is already there, free it | 
| Sterofin | 2:06c63d567719 | 167 | } | 
| Sterofin | 2:06c63d567719 | 168 | |
| Sterofin | 2:06c63d567719 | 169 | void add_terminal(int x, int y) | 
| Sterofin | 2:06c63d567719 | 170 | { | 
| Sterofin | 2:06c63d567719 | 171 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| Sterofin | 2:06c63d567719 | 172 | w1->type = TERMINAL; | 
| Sterofin | 2:06c63d567719 | 173 | w1->draw = draw_terminal; | 
| Sterofin | 2:06c63d567719 | 174 | w1->walkable = false; | 
| Sterofin | 2:06c63d567719 | 175 | w1->data = NULL; | 
| Sterofin | 2:06c63d567719 | 176 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| Sterofin | 2:06c63d567719 | 177 | if (val) free(val); // If something is already there, free it | 
| Sterofin | 2:06c63d567719 | 178 | } | 
| Sterofin | 2:06c63d567719 | 179 | |
| Sterofin | 2:06c63d567719 | 180 | void add_hacked_terminal(int x, int y) | 
| Sterofin | 2:06c63d567719 | 181 | { | 
| Sterofin | 2:06c63d567719 | 182 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| Sterofin | 2:06c63d567719 | 183 | w1->type = HACKED_TERMINAL; | 
| Sterofin | 2:06c63d567719 | 184 | w1->draw = draw_hacked_terminal; | 
| Sterofin | 2:06c63d567719 | 185 | w1->walkable = false; | 
| Sterofin | 2:06c63d567719 | 186 | w1->data = NULL; | 
| Sterofin | 2:06c63d567719 | 187 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| Sterofin | 2:06c63d567719 | 188 | if (val) free(val); // If something is already there, free it | 
| Sterofin | 2:06c63d567719 | 189 | } | 
| Sterofin | 2:06c63d567719 | 190 | |
| Sterofin | 2:06c63d567719 | 191 | void add_NPC(int x, int y) | 
| Sterofin | 2:06c63d567719 | 192 | { | 
| Sterofin | 2:06c63d567719 | 193 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| Sterofin | 2:06c63d567719 | 194 | w1->type = NPC; | 
| Sterofin | 2:06c63d567719 | 195 | w1->draw = draw_NPC; | 
| Sterofin | 2:06c63d567719 | 196 | w1->walkable = false; | 
| Sterofin | 2:06c63d567719 | 197 | w1->data = NULL; | 
| Sterofin | 2:06c63d567719 | 198 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| Sterofin | 2:06c63d567719 | 199 | if (val) free(val); // If something is already there, free it | 
| Sterofin | 2:06c63d567719 | 200 | } | 
| Sterofin | 2:06c63d567719 | 201 | |
| rconnorlawson | 0:35660d7952f7 | 202 | void add_plant(int x, int y) | 
| rconnorlawson | 0:35660d7952f7 | 203 | { | 
| rconnorlawson | 0:35660d7952f7 | 204 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| rconnorlawson | 0:35660d7952f7 | 205 | w1->type = PLANT; | 
| rconnorlawson | 0:35660d7952f7 | 206 | w1->draw = draw_plant; | 
| rconnorlawson | 0:35660d7952f7 | 207 | w1->walkable = true; | 
| rconnorlawson | 0:35660d7952f7 | 208 | w1->data = NULL; | 
| rconnorlawson | 0:35660d7952f7 | 209 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| rconnorlawson | 0:35660d7952f7 | 210 | if (val) free(val); // If something is already there, free it | 
| Sterofin | 2:06c63d567719 | 211 | } | 
| Sterofin | 2:06c63d567719 | 212 | |
| Sterofin | 2:06c63d567719 | 213 | void add_door(int x, int y,bool walk) | 
| Sterofin | 2:06c63d567719 | 214 | { | 
| Sterofin | 2:06c63d567719 | 215 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| Sterofin | 2:06c63d567719 | 216 | w1->type = DOOR; | 
| Sterofin | 2:06c63d567719 | 217 | w1->draw = draw_door; | 
| Sterofin | 2:06c63d567719 | 218 | w1->walkable = walk; | 
| Sterofin | 2:06c63d567719 | 219 | w1->data = NULL; | 
| Sterofin | 2:06c63d567719 | 220 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| Sterofin | 2:06c63d567719 | 221 | if (val) free(val); // If something is already there, free it | 
| Sterofin | 2:06c63d567719 | 222 | } | 
| Sterofin | 2:06c63d567719 | 223 | |
| Sterofin | 2:06c63d567719 | 224 | void add_boots(int x, int y){ | 
| Sterofin | 2:06c63d567719 | 225 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| Sterofin | 2:06c63d567719 | 226 | w1->type = BOOTS; | 
| Sterofin | 2:06c63d567719 | 227 | w1->draw = draw_boots; | 
| Sterofin | 2:06c63d567719 | 228 | w1->walkable = false; | 
| Sterofin | 2:06c63d567719 | 229 | w1->data = NULL; | 
| Sterofin | 2:06c63d567719 | 230 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| Sterofin | 2:06c63d567719 | 231 | if (val) free(val); // If something is already there, free it | 
| Sterofin | 2:06c63d567719 | 232 | } | 
| Sterofin | 2:06c63d567719 | 233 | void add_kill(int x, int y){ | 
| Sterofin | 2:06c63d567719 | 234 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| Sterofin | 2:06c63d567719 | 235 | w1->type = KILL; | 
| Sterofin | 2:06c63d567719 | 236 | w1->draw = draw_gun; | 
| Sterofin | 2:06c63d567719 | 237 | w1->walkable = false; | 
| Sterofin | 2:06c63d567719 | 238 | w1->data = NULL; | 
| Sterofin | 2:06c63d567719 | 239 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| Sterofin | 2:06c63d567719 | 240 | if (val) free(val); // If something is already there, free it | 
| Sterofin | 2:06c63d567719 | 241 | } | 
| Sterofin | 2:06c63d567719 | 242 | void add_orgprint(int x, int y){ | 
| Sterofin | 2:06c63d567719 | 243 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| Sterofin | 2:06c63d567719 | 244 | w1->type = PRINTER; | 
| Sterofin | 2:06c63d567719 | 245 | w1->draw = draw_printer; | 
| Sterofin | 2:06c63d567719 | 246 | w1->walkable = false; | 
| Sterofin | 2:06c63d567719 | 247 | w1->data = NULL; | 
| Sterofin | 2:06c63d567719 | 248 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| Sterofin | 2:06c63d567719 | 249 | if (val) free(val); // If something is already there, free it | 
| Sterofin | 2:06c63d567719 | 250 | } | 
| Sterofin | 2:06c63d567719 | 251 | void add_teleport(int x, int y){ | 
| Sterofin | 2:06c63d567719 | 252 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); | 
| Sterofin | 2:06c63d567719 | 253 | w1->type = TELEPORT; | 
| Sterofin | 2:06c63d567719 | 254 | w1->draw = draw_teleport; | 
| Sterofin | 2:06c63d567719 | 255 | w1->walkable = false; | 
| Sterofin | 2:06c63d567719 | 256 | w1->data = NULL; | 
| Sterofin | 2:06c63d567719 | 257 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); | 
| Sterofin | 2:06c63d567719 | 258 | if (val) free(val); // If something is already there, free it | 
| rconnorlawson | 0:35660d7952f7 | 259 | } |