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
00001 #include "map.h" 00002 00003 #include "globals.h" 00004 #include "graphics.h" 00005 00006 /** 00007 * The Map structure. This holds a HashTable for all the MapItems, along with 00008 * values for the width and height of the Map. 00009 */ 00010 struct Map { 00011 HashTable* items; 00012 int w, h; 00013 }; 00014 00015 /** 00016 * Storage area for the maps. 00017 * This is a global variable, but can only be access from this file because it 00018 * is static. 00019 */ 00020 static Map map; 00021 static int active_map; 00022 00023 /** 00024 * The first step in HashTable access for the map is turning the two-dimensional 00025 * key information (x, y) into a one-dimensional unsigned integer. 00026 * This function should uniquely map (x,y) onto the space of unsigned integers. 00027 */ 00028 static unsigned XY_KEY(int X, int Y) { 00029 return X*(map.h)+Y; 00030 } 00031 00032 /** 00033 * This is the hash function actually passed into createHashTable. It takes an 00034 * unsigned key (the output of XY_KEY) and turns it into a hash value (some 00035 * small non-negative integer). 00036 */ 00037 unsigned map_hash(unsigned key) 00038 { 00039 return key%NUMBUCKETS; 00040 } 00041 00042 void maps_init() 00043 { 00044 00045 map.items = createHashTable(map_hash, NUMBUCKETS); 00046 map.w = WIDTH; 00047 map.h = HEIGHT; 00048 } 00049 00050 Map* get_active_map() 00051 { 00052 return ↦ 00053 } 00054 00055 Map* set_active_map(int m) 00056 { 00057 active_map = m; 00058 return ↦ 00059 } 00060 00061 void print_map() 00062 { 00063 // As you add more types, you'll need to add more items to this array. 00064 char lookup[] = {'W', 'P'}; 00065 for(int y = 0; y < map_height(); y++) 00066 { 00067 for (int x = 0; x < map_width(); x++) 00068 { 00069 MapItem* item = get_here(x,y); 00070 if (item) pc.printf("%c", lookup[item->type]); 00071 else pc.printf(" "); 00072 } 00073 pc.printf("\r\n"); 00074 } 00075 } 00076 00077 int map_width() 00078 { 00079 Map *map = get_active_map(); 00080 return map->w; 00081 } 00082 00083 int map_height() 00084 { 00085 Map *map = get_active_map(); 00086 return map->h; 00087 } 00088 00089 int map_area() 00090 { 00091 Map* map = get_active_map(); 00092 return (map->h * map->w); 00093 } 00094 00095 MapItem* get_north(int x, int y) 00096 { 00097 Map *map = get_active_map(); 00098 int key = XY_KEY(x, y-1); 00099 return (MapItem*)getItem(map->items, key); 00100 } 00101 00102 MapItem* get_south(int x, int y) 00103 { 00104 Map *map = get_active_map(); 00105 int key = XY_KEY(x, y+1); 00106 return (MapItem*)getItem(map->items, key); 00107 } 00108 00109 MapItem* get_east(int x, int y) 00110 { 00111 Map *map = get_active_map(); 00112 int key = XY_KEY(x+1, y); 00113 return (MapItem*)getItem(map->items, key); 00114 } 00115 00116 MapItem* get_west(int x, int y) 00117 { 00118 Map *map = get_active_map(); 00119 int key = XY_KEY(x-1, y); 00120 return (MapItem*)getItem(map->items, key); 00121 } 00122 00123 MapItem* get_here(int x, int y) 00124 { 00125 Map *map = get_active_map(); 00126 int key = XY_KEY(x, y); 00127 return (MapItem*)getItem(map->items, key); 00128 } 00129 00130 00131 void map_erase(int x, int y) 00132 { 00133 Map *map = get_active_map(); 00134 int key = XY_KEY(x, y); 00135 deleteItem(map->items, key); 00136 } 00137 00138 void add_wall(int x, int y, int dir, int len) 00139 { 00140 for(int i = 0; i < len; i++) 00141 { 00142 00143 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00144 w1->type = WALL; 00145 w1->draw = draw_wall; 00146 w1->walkable = false; 00147 w1->data = NULL; 00148 unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); 00149 void* val = insertItem(get_active_map()->items, key, w1); 00150 if (val)free(val); // If something is already there, free it 00151 } 00152 00153 } 00154 00155 void add_plant(int x, int y) 00156 { 00157 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00158 w1->type = FLOWWINK; 00159 w1->draw = draw_flowey_wink; 00160 w1->walkable = false; 00161 w1->data = NULL; 00162 00163 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00164 if (val) free(val); // If something is already there, free it 00165 } 00166 00167 void add_sign(int x, int y) { 00168 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00169 w1->type = SIGN; 00170 w1->draw = draw_sign; 00171 w1->walkable = false; 00172 w1->data = NULL; 00173 00174 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00175 if (val) free(val); 00176 } 00177 00178 void add_door_locked(int x, int y) { 00179 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00180 w1->type = DOORLOCKED; 00181 w1->draw = draw_door_locked; 00182 w1->walkable = false; 00183 w1->data = NULL; 00184 00185 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00186 if (val) free(val); 00187 } 00188 00189 void add_door_unlocked(int x, int y) { 00190 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00191 w1->type = DOORUNLOCKED; 00192 w1->draw = draw_door_unlocked; 00193 w1->walkable = false; 00194 w1->data = NULL; 00195 00196 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00197 if (val) free(val); 00198 } 00199 00200 00201 void add_flowey_friendly(int x, int y) { 00202 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00203 w1->type = FLOWFRIEND; 00204 w1->draw = draw_flowey_friendly; 00205 w1->walkable = false; 00206 w1->data = NULL; 00207 00208 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00209 if (val) free(val); 00210 } 00211 00212 void add_flowey_wink(int x, int y) { 00213 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00214 w1->type = FLOWWINK; 00215 w1->draw = draw_flowey_wink; 00216 w1->walkable = false; 00217 w1->data = NULL; 00218 00219 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00220 if (val) free(val); 00221 } 00222 00223 void add_flowey_creepy(int x, int y) { 00224 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00225 w1->type = FLOWCREEPY; 00226 w1->draw = draw_flowey_creepy; 00227 w1->walkable = false; 00228 w1->data = NULL; 00229 00230 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00231 if (val) free(val); 00232 } 00233 00234 void add_toby(int x, int y) { 00235 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00236 w1->type = TOBY; 00237 w1->draw = draw_toby; 00238 w1->walkable = false; 00239 w1->data = NULL; 00240 00241 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00242 if (val) free(val); 00243 } 00244 00245 void add_toby_bone(int x, int y) { 00246 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00247 w1->type = TOBYBONE; 00248 w1->draw = draw_toby_bone; 00249 w1->walkable = false; 00250 w1->data = NULL; 00251 00252 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00253 if (val) free(val); 00254 } 00255 00256 void add_red_button(int x, int y) { 00257 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00258 w1->type = REDBUTTON; 00259 w1->draw = draw_red_button; 00260 w1->walkable = false; 00261 w1->data = NULL; 00262 00263 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00264 if (val) free(val); 00265 } 00266 00267 void add_green_button(int x, int y) { 00268 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00269 w1->type = GREENBUTTON; 00270 w1->draw = draw_green_button; 00271 w1->walkable = false; 00272 w1->data = NULL; 00273 00274 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00275 if (val) free(val); 00276 } 00277 00278 void add_blue_button(int x, int y) { 00279 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00280 w1->type = BLUEBUTTON; 00281 w1->draw = draw_blue_button; 00282 w1->walkable = false; 00283 w1->data = NULL; 00284 00285 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00286 if (val) free(val); 00287 } 00288 00289 void add_river_man(int x, int y) { 00290 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00291 w1->type = RIVERMAN; 00292 w1->draw = draw_river_man; 00293 w1->walkable = false; 00294 w1->data = NULL; 00295 00296 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00297 if (val) free(val); 00298 } 00299 00300 void add_chest(int x, int y) { 00301 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00302 w1->type = CHEST; 00303 w1->draw = draw_chest; 00304 w1->walkable = false; 00305 w1->data = NULL; 00306 00307 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00308 if (val) free(val); 00309 } 00310 void add_leaf(int x, int y) { 00311 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00312 w1->type = LEAF; 00313 w1->draw = draw_leaf; 00314 w1->walkable = true; 00315 w1->data = NULL; 00316 00317 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00318 if (val) free(val); 00319 } 00320 void add_wood(int x, int y) { 00321 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); 00322 w1->type = WOOD; 00323 w1->draw = draw_wood; 00324 w1->walkable = false; 00325 w1->data = NULL; 00326 00327 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); 00328 if (val) free(val); 00329 } 00330 // add toby, tobybone, riverman, doorlocked, doorunlocked,
Generated on Thu Jul 14 2022 01:43:17 by
1.7.2