
hgftf
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@9:d09b96b6c39c, 2020-11-20 (annotated)
- Committer:
- ajorgih3
- Date:
- Fri Nov 20 21:58:45 2020 +0000
- Revision:
- 9:d09b96b6c39c
- Parent:
- 7:7ab2f4b09196
- Child:
- 10:0b2f37cef9b9
yes;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DCchico | 2:4947d6a82971 | 1 | // Copyright 2020 Georgia Tech. All rights reserved. |
DCchico | 2:4947d6a82971 | 2 | // The materials provided by the instructor in this course are for |
DCchico | 2:4947d6a82971 | 3 | // the use of the students currently enrolled in the course. |
DCchico | 2:4947d6a82971 | 4 | // Copyrighted course materials may not be further disseminated. |
DCchico | 2:4947d6a82971 | 5 | // This file must not be made publicly available anywhere. |
DCchico | 2:4947d6a82971 | 6 | |
DCchico | 1:10330bce85cb | 7 | #include "map.h" |
DCchico | 1:10330bce85cb | 8 | |
DCchico | 1:10330bce85cb | 9 | #include "globals.h" |
DCchico | 1:10330bce85cb | 10 | #include "graphics.h" |
DCchico | 1:10330bce85cb | 11 | |
DCchico | 1:10330bce85cb | 12 | /** |
DCchico | 1:10330bce85cb | 13 | * The Map structure. This holds a HashTable for all the MapItems, along with |
DCchico | 1:10330bce85cb | 14 | * values for the width and height of the Map. |
DCchico | 1:10330bce85cb | 15 | */ |
DCchico | 1:10330bce85cb | 16 | struct Map { |
DCchico | 1:10330bce85cb | 17 | HashTable* items; |
DCchico | 1:10330bce85cb | 18 | int w, h; |
DCchico | 1:10330bce85cb | 19 | }; |
DCchico | 1:10330bce85cb | 20 | |
DCchico | 1:10330bce85cb | 21 | #define NUM_MAPS 1 |
ajorgih3 | 3:bb6f73642f01 | 22 | #define MAPS_WIDTH 50 |
ajorgih3 | 3:bb6f73642f01 | 23 | #define MAPS_HEIGHT 50 |
DCchico | 1:10330bce85cb | 24 | static Map maps[NUM_MAPS]; |
DCchico | 1:10330bce85cb | 25 | static int active_map; |
ajorgih3 | 4:697e1120f821 | 26 | unsigned int NUMBUCKETS = 100; |
ajorgih3 | 3:bb6f73642f01 | 27 | |
ajorgih3 | 4:697e1120f821 | 28 | // this is important |
DCchico | 1:10330bce85cb | 29 | static const MapItem CLEAR_SENTINEL = { |
DCchico | 1:10330bce85cb | 30 | .type = CLEAR, |
DCchico | 1:10330bce85cb | 31 | .draw = draw_nothing |
DCchico | 1:10330bce85cb | 32 | }; |
DCchico | 1:10330bce85cb | 33 | |
DCchico | 1:10330bce85cb | 34 | /** |
DCchico | 1:10330bce85cb | 35 | * The first step in HashTable access for the map is turning the two-dimensional |
DCchico | 1:10330bce85cb | 36 | * key information (x, y) into a one-dimensional unsigned integer. |
DCchico | 1:10330bce85cb | 37 | * This function should uniquely map (x,y) onto the space of unsigned integers. |
DCchico | 1:10330bce85cb | 38 | */ |
DCchico | 1:10330bce85cb | 39 | static unsigned XY_KEY(int X, int Y) { |
ajorgih3 | 3:bb6f73642f01 | 40 | // multiply the map height by Y and adding it to X |
ajorgih3 | 3:bb6f73642f01 | 41 | unsigned XYKey = Y * map_width() + X; |
ajorgih3 | 3:bb6f73642f01 | 42 | // return the unique key |
ajorgih3 | 3:bb6f73642f01 | 43 | return XYKey; |
DCchico | 1:10330bce85cb | 44 | } |
DCchico | 1:10330bce85cb | 45 | |
DCchico | 1:10330bce85cb | 46 | /** |
DCchico | 1:10330bce85cb | 47 | * This is the hash function actually passed into createHashTable. It takes an |
DCchico | 1:10330bce85cb | 48 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some |
DCchico | 1:10330bce85cb | 49 | * small non-negative integer). |
DCchico | 1:10330bce85cb | 50 | */ |
DCchico | 1:10330bce85cb | 51 | unsigned map_hash(unsigned key) |
DCchico | 1:10330bce85cb | 52 | { |
ajorgih3 | 3:bb6f73642f01 | 53 | // modulo map hashing into something unique |
ajorgih3 | 3:bb6f73642f01 | 54 | return key % NUMBUCKETS; |
DCchico | 1:10330bce85cb | 55 | } |
DCchico | 1:10330bce85cb | 56 | |
ajorgih3 | 3:bb6f73642f01 | 57 | /** |
ajorgih3 | 3:bb6f73642f01 | 58 | * Initializes the internal structures for all maps. This does not populate |
ajorgih3 | 3:bb6f73642f01 | 59 | * the map with items, but allocates space for them, initializes the hash tables, |
ajorgih3 | 3:bb6f73642f01 | 60 | * and sets the width and height. |
ajorgih3 | 3:bb6f73642f01 | 61 | */ |
ajorgih3 | 3:bb6f73642f01 | 62 | |
DCchico | 1:10330bce85cb | 63 | void maps_init() |
DCchico | 1:10330bce85cb | 64 | { |
DCchico | 1:10330bce85cb | 65 | // TODO: Implement! |
DCchico | 1:10330bce85cb | 66 | // Initialize hash table |
DCchico | 1:10330bce85cb | 67 | // Set width & height |
ajorgih3 | 3:bb6f73642f01 | 68 | set_active_map(0)->items = createHashTable(map_hash, NUMBUCKETS); // num buckets |
ajorgih3 | 3:bb6f73642f01 | 69 | get_active_map()->w = MAPS_WIDTH; |
ajorgih3 | 3:bb6f73642f01 | 70 | get_active_map()->h = MAPS_HEIGHT; |
ajorgih3 | 3:bb6f73642f01 | 71 | return; |
DCchico | 1:10330bce85cb | 72 | } |
DCchico | 1:10330bce85cb | 73 | |
DCchico | 1:10330bce85cb | 74 | Map* get_active_map() |
DCchico | 1:10330bce85cb | 75 | { |
DCchico | 1:10330bce85cb | 76 | return &maps[active_map]; |
DCchico | 1:10330bce85cb | 77 | } |
DCchico | 1:10330bce85cb | 78 | |
DCchico | 1:10330bce85cb | 79 | Map* set_active_map(int m) |
DCchico | 1:10330bce85cb | 80 | { |
DCchico | 1:10330bce85cb | 81 | active_map = m; |
DCchico | 1:10330bce85cb | 82 | return &maps[active_map]; |
DCchico | 1:10330bce85cb | 83 | } |
DCchico | 1:10330bce85cb | 84 | |
DCchico | 1:10330bce85cb | 85 | void print_map() |
DCchico | 1:10330bce85cb | 86 | { |
DCchico | 1:10330bce85cb | 87 | char lookup[] = {'W', 'D', 'P', 'A', 'K', 'C', 'N',' ','S'}; |
DCchico | 1:10330bce85cb | 88 | Map* map = get_active_map(); |
DCchico | 1:10330bce85cb | 89 | for(int j = 0; j < map->h; j++) |
DCchico | 1:10330bce85cb | 90 | { |
DCchico | 1:10330bce85cb | 91 | for (int i = 0; i < map->w; i++) |
DCchico | 1:10330bce85cb | 92 | { |
DCchico | 1:10330bce85cb | 93 | MapItem* item = (MapItem*)getItem(map->items, XY_KEY(i, j)); |
DCchico | 1:10330bce85cb | 94 | if (item) pc.printf("%c", lookup[item->type]); |
DCchico | 1:10330bce85cb | 95 | else pc.printf(" "); |
DCchico | 1:10330bce85cb | 96 | } |
DCchico | 1:10330bce85cb | 97 | pc.printf("\r\n"); |
DCchico | 1:10330bce85cb | 98 | } |
DCchico | 1:10330bce85cb | 99 | } |
DCchico | 1:10330bce85cb | 100 | |
ajorgih3 | 3:bb6f73642f01 | 101 | /** |
ajorgih3 | 3:bb6f73642f01 | 102 | * Returns the width of the active map. |
ajorgih3 | 3:bb6f73642f01 | 103 | */ |
DCchico | 1:10330bce85cb | 104 | int map_width() |
DCchico | 1:10330bce85cb | 105 | { |
ajorgih3 | 3:bb6f73642f01 | 106 | // get the pointer of the active map |
ajorgih3 | 3:bb6f73642f01 | 107 | // and the pointer to the variable w |
ajorgih3 | 3:bb6f73642f01 | 108 | Map* map = get_active_map(); |
ajorgih3 | 3:bb6f73642f01 | 109 | return map->w; |
DCchico | 1:10330bce85cb | 110 | } |
DCchico | 1:10330bce85cb | 111 | |
ajorgih3 | 3:bb6f73642f01 | 112 | /** |
ajorgih3 | 3:bb6f73642f01 | 113 | * Returns the heigh of the active map. |
ajorgih3 | 3:bb6f73642f01 | 114 | */ |
DCchico | 1:10330bce85cb | 115 | int map_height() |
DCchico | 1:10330bce85cb | 116 | { |
ajorgih3 | 3:bb6f73642f01 | 117 | // get the pointer of the active map |
ajorgih3 | 3:bb6f73642f01 | 118 | // and the pointer to the variable h |
ajorgih3 | 3:bb6f73642f01 | 119 | Map* map = get_active_map(); |
ajorgih3 | 3:bb6f73642f01 | 120 | return map->w; |
DCchico | 1:10330bce85cb | 121 | } |
DCchico | 1:10330bce85cb | 122 | |
ajorgih3 | 3:bb6f73642f01 | 123 | /** |
ajorgih3 | 3:bb6f73642f01 | 124 | * Returns the total number of cells in the active map. |
ajorgih3 | 3:bb6f73642f01 | 125 | */ |
DCchico | 1:10330bce85cb | 126 | int map_area() |
DCchico | 1:10330bce85cb | 127 | { |
ajorgih3 | 3:bb6f73642f01 | 128 | // returns the product of height and width |
ajorgih3 | 3:bb6f73642f01 | 129 | return map_height() * map_width(); |
ajorgih3 | 3:bb6f73642f01 | 130 | } |
DCchico | 1:10330bce85cb | 131 | |
DCchico | 1:10330bce85cb | 132 | MapItem* get_current(int x, int y) |
DCchico | 1:10330bce85cb | 133 | { |
ajorgih3 | 3:bb6f73642f01 | 134 | // return the current item in that specific map |
ajorgih3 | 3:bb6f73642f01 | 135 | return (MapItem*) get_active_map()->items; |
DCchico | 1:10330bce85cb | 136 | } |
ajorgih3 | 3:bb6f73642f01 | 137 | |
ajorgih3 | 3:bb6f73642f01 | 138 | /** |
ajorgih3 | 3:bb6f73642f01 | 139 | * Returns the MapItem immediately above the given location. |
ajorgih3 | 3:bb6f73642f01 | 140 | */ |
DCchico | 1:10330bce85cb | 141 | MapItem* get_north(int x, int y) |
DCchico | 1:10330bce85cb | 142 | { |
ajorgih3 | 3:bb6f73642f01 | 143 | // coordinate to the one block north of the current block |
ajorgih3 | 3:bb6f73642f01 | 144 | int northCoordinate = y - 1; |
ajorgih3 | 3:bb6f73642f01 | 145 | // get the items in the active map |
ajorgih3 | 3:bb6f73642f01 | 146 | HashTable* mapItem = get_active_map()->items; |
ajorgih3 | 3:bb6f73642f01 | 147 | // get the keys to the respective coordinate, placed in a specific bucket |
ajorgih3 | 3:bb6f73642f01 | 148 | unsigned keys = XY_KEY(x, northCoordinate); |
ajorgih3 | 3:bb6f73642f01 | 149 | // we need to cast it because it is not type MapItem, but type HashTable |
ajorgih3 | 3:bb6f73642f01 | 150 | return (MapItem*) getItem(mapItem, keys); |
DCchico | 1:10330bce85cb | 151 | } |
ajorgih3 | 3:bb6f73642f01 | 152 | |
ajorgih3 | 3:bb6f73642f01 | 153 | /** |
ajorgih3 | 3:bb6f73642f01 | 154 | * Returns the MapItem immediately below the given location. |
ajorgih3 | 3:bb6f73642f01 | 155 | */ |
DCchico | 1:10330bce85cb | 156 | MapItem* get_south(int x, int y) |
DCchico | 1:10330bce85cb | 157 | { |
ajorgih3 | 3:bb6f73642f01 | 158 | // coordinate to the one block south of the current block |
ajorgih3 | 3:bb6f73642f01 | 159 | int southCoordinate = y + 1; |
ajorgih3 | 3:bb6f73642f01 | 160 | // get the items in the active map |
ajorgih3 | 3:bb6f73642f01 | 161 | HashTable* mapItem = get_active_map()->items; |
ajorgih3 | 3:bb6f73642f01 | 162 | // get the keys to the respective coordinate, placed in a specific bucket |
ajorgih3 | 3:bb6f73642f01 | 163 | unsigned keys = XY_KEY(x, southCoordinate); |
ajorgih3 | 3:bb6f73642f01 | 164 | // we need to cast it because it is not type MapItem, but type HashTable |
ajorgih3 | 3:bb6f73642f01 | 165 | return (MapItem*) getItem(mapItem, keys); |
DCchico | 1:10330bce85cb | 166 | } |
DCchico | 1:10330bce85cb | 167 | |
ajorgih3 | 3:bb6f73642f01 | 168 | |
ajorgih3 | 3:bb6f73642f01 | 169 | /** |
ajorgih3 | 3:bb6f73642f01 | 170 | * Returns the MapItem immediately to the right of the given location. |
ajorgih3 | 3:bb6f73642f01 | 171 | */ |
ajorgih3 | 3:bb6f73642f01 | 172 | MapItem* get_east(int x, int y) |
ajorgih3 | 3:bb6f73642f01 | 173 | { |
ajorgih3 | 3:bb6f73642f01 | 174 | // coordinate to the one block east of the current block |
ajorgih3 | 3:bb6f73642f01 | 175 | int eastCoordinate = x + 1; |
ajorgih3 | 3:bb6f73642f01 | 176 | // get the items in the active map |
ajorgih3 | 3:bb6f73642f01 | 177 | HashTable* mapItem = get_active_map()->items; |
ajorgih3 | 3:bb6f73642f01 | 178 | // get the keys to the respective coordinate, placed in a specific bucket |
ajorgih3 | 3:bb6f73642f01 | 179 | unsigned keys = XY_KEY(eastCoordinate, y); |
ajorgih3 | 3:bb6f73642f01 | 180 | // we need to cast it because it is not type MapItem, but type HashTable |
ajorgih3 | 3:bb6f73642f01 | 181 | return (MapItem*) getItem(mapItem, keys); |
ajorgih3 | 3:bb6f73642f01 | 182 | } |
ajorgih3 | 3:bb6f73642f01 | 183 | |
ajorgih3 | 3:bb6f73642f01 | 184 | |
ajorgih3 | 3:bb6f73642f01 | 185 | /** |
ajorgih3 | 3:bb6f73642f01 | 186 | * Returns the MapItem immediately to the left of the given location. |
ajorgih3 | 3:bb6f73642f01 | 187 | */ |
DCchico | 1:10330bce85cb | 188 | MapItem* get_west(int x, int y) |
DCchico | 1:10330bce85cb | 189 | { |
ajorgih3 | 3:bb6f73642f01 | 190 | // coordinate to the one block west of the current block |
ajorgih3 | 3:bb6f73642f01 | 191 | int westCoordinate = x - 1; |
ajorgih3 | 3:bb6f73642f01 | 192 | // get the items in the active map |
ajorgih3 | 3:bb6f73642f01 | 193 | HashTable* mapItem = get_active_map()->items; |
ajorgih3 | 3:bb6f73642f01 | 194 | // get the keys to the respective coordinate, placed in a specific bucket |
ajorgih3 | 3:bb6f73642f01 | 195 | unsigned keys = XY_KEY(westCoordinate, y); |
ajorgih3 | 3:bb6f73642f01 | 196 | // we need to cast it because it is not type MapItem, but type HashTable |
ajorgih3 | 3:bb6f73642f01 | 197 | return (MapItem*) getItem(mapItem, keys); |
DCchico | 1:10330bce85cb | 198 | } |
DCchico | 1:10330bce85cb | 199 | |
ajorgih3 | 3:bb6f73642f01 | 200 | /** |
ajorgih3 | 3:bb6f73642f01 | 201 | * Returns the MapItem at the given location. |
ajorgih3 | 3:bb6f73642f01 | 202 | */ |
DCchico | 1:10330bce85cb | 203 | MapItem* get_here(int x, int y) |
DCchico | 1:10330bce85cb | 204 | { |
ajorgih3 | 3:bb6f73642f01 | 205 | // get the items in the active map |
ajorgih3 | 3:bb6f73642f01 | 206 | HashTable* mapItem = get_active_map()->items; |
ajorgih3 | 3:bb6f73642f01 | 207 | // get the keys to the respective coordinate, placed in a specific bucket |
ajorgih3 | 3:bb6f73642f01 | 208 | unsigned keys = XY_KEY(x, y); |
ajorgih3 | 3:bb6f73642f01 | 209 | // we need to cast it because it is not type MapItem, but type HashTable |
ajorgih3 | 3:bb6f73642f01 | 210 | return (MapItem*) getItem(mapItem, keys); |
DCchico | 1:10330bce85cb | 211 | } |
DCchico | 1:10330bce85cb | 212 | |
ajorgih3 | 3:bb6f73642f01 | 213 | |
ajorgih3 | 3:bb6f73642f01 | 214 | /** |
ajorgih3 | 3:bb6f73642f01 | 215 | * If there is a MapItem at (x,y), remove it from the map. |
ajorgih3 | 3:bb6f73642f01 | 216 | */ |
DCchico | 1:10330bce85cb | 217 | void map_erase(int x, int y) |
DCchico | 1:10330bce85cb | 218 | { |
ajorgih3 | 3:bb6f73642f01 | 219 | if (get_here(x, y) != NULL) { |
ajorgih3 | 3:bb6f73642f01 | 220 | return deleteItem(get_active_map()->items, XY_KEY(x, y)); |
ajorgih3 | 3:bb6f73642f01 | 221 | } |
ajorgih3 | 3:bb6f73642f01 | 222 | return; |
DCchico | 1:10330bce85cb | 223 | } |
DCchico | 1:10330bce85cb | 224 | |
DCchico | 1:10330bce85cb | 225 | void add_wall(int x, int y, int dir, int len) |
DCchico | 1:10330bce85cb | 226 | { |
DCchico | 1:10330bce85cb | 227 | for(int i = 0; i < len; i++) |
DCchico | 1:10330bce85cb | 228 | { |
DCchico | 1:10330bce85cb | 229 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 230 | w1->type = WALL; |
DCchico | 1:10330bce85cb | 231 | w1->draw = draw_wall; |
DCchico | 1:10330bce85cb | 232 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 233 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 234 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
DCchico | 1:10330bce85cb | 235 | void* val = insertItem(get_active_map()->items, key, w1); |
DCchico | 1:10330bce85cb | 236 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 237 | } |
DCchico | 1:10330bce85cb | 238 | } |
DCchico | 1:10330bce85cb | 239 | |
DCchico | 1:10330bce85cb | 240 | void add_plant(int x, int y) |
DCchico | 1:10330bce85cb | 241 | { |
DCchico | 1:10330bce85cb | 242 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 243 | w1->type = PLANT; |
DCchico | 1:10330bce85cb | 244 | w1->draw = draw_plant; |
DCchico | 1:10330bce85cb | 245 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 246 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 247 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 248 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 249 | } |
DCchico | 1:10330bce85cb | 250 | |
DCchico | 1:10330bce85cb | 251 | void add_goodie(int x, int y) |
DCchico | 1:10330bce85cb | 252 | { |
DCchico | 1:10330bce85cb | 253 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 254 | w1->type = GOODIE; |
DCchico | 1:10330bce85cb | 255 | w1->draw = draw_goodie; |
DCchico | 1:10330bce85cb | 256 | w1->walkable = true; |
DCchico | 1:10330bce85cb | 257 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 258 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 259 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 260 | } |
DCchico | 1:10330bce85cb | 261 | |
DCchico | 1:10330bce85cb | 262 | void remove_goodie(int x, int y) // I'm lazy so overwrite it with a plant |
DCchico | 1:10330bce85cb | 263 | { |
DCchico | 1:10330bce85cb | 264 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 265 | w1->type = PLANT; |
DCchico | 1:10330bce85cb | 266 | w1->draw = draw_plant; |
DCchico | 1:10330bce85cb | 267 | w1->walkable = true; |
DCchico | 1:10330bce85cb | 268 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 269 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 270 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 271 | } |
DCchico | 1:10330bce85cb | 272 | |
DCchico | 1:10330bce85cb | 273 | void add_snake_body(int x, int y) |
DCchico | 1:10330bce85cb | 274 | { |
DCchico | 1:10330bce85cb | 275 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 276 | w1->type = SNAKE_BODY; |
DCchico | 1:10330bce85cb | 277 | w1->draw = draw_snake_body; |
DCchico | 1:10330bce85cb | 278 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 279 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 280 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 281 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 282 | } |
DCchico | 1:10330bce85cb | 283 | |
DCchico | 1:10330bce85cb | 284 | void add_snake_head(int x, int y) |
DCchico | 1:10330bce85cb | 285 | { |
DCchico | 1:10330bce85cb | 286 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 287 | w1->type = SNAKE_BODY; |
DCchico | 1:10330bce85cb | 288 | w1->draw = draw_snake_head; |
DCchico | 1:10330bce85cb | 289 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 290 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 291 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 292 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 293 | } |
DCchico | 1:10330bce85cb | 294 | |
DCchico | 1:10330bce85cb | 295 | void add_snake_tail(int x, int y) |
DCchico | 1:10330bce85cb | 296 | { |
DCchico | 1:10330bce85cb | 297 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 298 | w1->type = SNAKE_BODY; |
DCchico | 1:10330bce85cb | 299 | w1->draw = draw_snake_tail; |
DCchico | 1:10330bce85cb | 300 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 301 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 302 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 303 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 304 | } |
ajorgih3 | 3:bb6f73642f01 | 305 | |
ajorgih3 | 3:bb6f73642f01 | 306 | void add_shield(int x, int y) |
ajorgih3 | 3:bb6f73642f01 | 307 | { |
ajorgih3 | 3:bb6f73642f01 | 308 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 3:bb6f73642f01 | 309 | w1->type = SHIELD; |
ajorgih3 | 3:bb6f73642f01 | 310 | w1->draw = draw_shield; |
ajorgih3 | 3:bb6f73642f01 | 311 | w1->walkable = true; |
ajorgih3 | 3:bb6f73642f01 | 312 | w1->data = NULL; |
ajorgih3 | 3:bb6f73642f01 | 313 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 3:bb6f73642f01 | 314 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 3:bb6f73642f01 | 315 | } |
ajorgih3 | 3:bb6f73642f01 | 316 | |
ajorgih3 | 3:bb6f73642f01 | 317 | void add_life(int x, int y) |
ajorgih3 | 3:bb6f73642f01 | 318 | { |
ajorgih3 | 3:bb6f73642f01 | 319 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 3:bb6f73642f01 | 320 | w1->type = LIFE; |
ajorgih3 | 3:bb6f73642f01 | 321 | w1->draw = draw_life; |
ajorgih3 | 3:bb6f73642f01 | 322 | w1->walkable = true; |
ajorgih3 | 3:bb6f73642f01 | 323 | w1->data = NULL; |
ajorgih3 | 3:bb6f73642f01 | 324 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 3:bb6f73642f01 | 325 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 3:bb6f73642f01 | 326 | } |
ajorgih3 | 3:bb6f73642f01 | 327 | |
ajorgih3 | 3:bb6f73642f01 | 328 | void add_snowflake(int x, int y) |
ajorgih3 | 3:bb6f73642f01 | 329 | { |
ajorgih3 | 3:bb6f73642f01 | 330 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 3:bb6f73642f01 | 331 | w1->type = SNOWFLAKE; |
ajorgih3 | 3:bb6f73642f01 | 332 | w1->draw = draw_snowflake; |
ajorgih3 | 3:bb6f73642f01 | 333 | w1->walkable = true; |
ajorgih3 | 3:bb6f73642f01 | 334 | w1->data = NULL; |
ajorgih3 | 3:bb6f73642f01 | 335 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 3:bb6f73642f01 | 336 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 3:bb6f73642f01 | 337 | } |
ajorgih3 | 3:bb6f73642f01 | 338 | |
ajorgih3 | 3:bb6f73642f01 | 339 | void add_sword(int x, int y) |
ajorgih3 | 3:bb6f73642f01 | 340 | { |
ajorgih3 | 3:bb6f73642f01 | 341 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 3:bb6f73642f01 | 342 | w1->type = SWORD; |
ajorgih3 | 3:bb6f73642f01 | 343 | w1->draw = draw_sword; |
ajorgih3 | 6:fbaee888e5ea | 344 | w1->walkable = true; |
ajorgih3 | 3:bb6f73642f01 | 345 | w1->data = NULL; |
ajorgih3 | 3:bb6f73642f01 | 346 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 3:bb6f73642f01 | 347 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 3:bb6f73642f01 | 348 | } |
ajorgih3 | 3:bb6f73642f01 | 349 | |
ajorgih3 | 3:bb6f73642f01 | 350 | void add_nothing(int x, int y) |
ajorgih3 | 3:bb6f73642f01 | 351 | { |
ajorgih3 | 3:bb6f73642f01 | 352 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 3:bb6f73642f01 | 353 | w1->type = NOTHING; |
ajorgih3 | 3:bb6f73642f01 | 354 | w1->draw = draw_nothing; |
ajorgih3 | 3:bb6f73642f01 | 355 | w1->walkable = true; |
ajorgih3 | 3:bb6f73642f01 | 356 | w1->data = NULL; |
ajorgih3 | 3:bb6f73642f01 | 357 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 3:bb6f73642f01 | 358 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 3:bb6f73642f01 | 359 | } |
ajorgih3 | 4:697e1120f821 | 360 | |
ajorgih3 | 4:697e1120f821 | 361 | void add_arrow_up(int x, int y) |
ajorgih3 | 4:697e1120f821 | 362 | { |
ajorgih3 | 4:697e1120f821 | 363 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 4:697e1120f821 | 364 | w1->type = ARROWUP; |
ajorgih3 | 4:697e1120f821 | 365 | w1->draw = draw_arrow_up; |
ajorgih3 | 4:697e1120f821 | 366 | w1->walkable = true; |
ajorgih3 | 4:697e1120f821 | 367 | w1->data = NULL; |
ajorgih3 | 4:697e1120f821 | 368 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 4:697e1120f821 | 369 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 4:697e1120f821 | 370 | } |
ajorgih3 | 4:697e1120f821 | 371 | |
ajorgih3 | 4:697e1120f821 | 372 | void add_arrow_down(int x, int y) |
ajorgih3 | 4:697e1120f821 | 373 | { |
ajorgih3 | 4:697e1120f821 | 374 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 4:697e1120f821 | 375 | w1->type = ARROWDOWN; |
ajorgih3 | 4:697e1120f821 | 376 | w1->draw = draw_arrow_down; |
ajorgih3 | 4:697e1120f821 | 377 | w1->walkable = true; |
ajorgih3 | 4:697e1120f821 | 378 | w1->data = NULL; |
ajorgih3 | 4:697e1120f821 | 379 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 4:697e1120f821 | 380 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 4:697e1120f821 | 381 | } |
ajorgih3 | 4:697e1120f821 | 382 | |
ajorgih3 | 4:697e1120f821 | 383 | void add_arrow_right(int x, int y) |
ajorgih3 | 4:697e1120f821 | 384 | { |
ajorgih3 | 4:697e1120f821 | 385 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 4:697e1120f821 | 386 | w1->type = ARROWRIGHT; |
ajorgih3 | 4:697e1120f821 | 387 | w1->draw = draw_arrow_right; |
ajorgih3 | 4:697e1120f821 | 388 | w1->walkable = true; |
ajorgih3 | 4:697e1120f821 | 389 | w1->data = NULL; |
ajorgih3 | 4:697e1120f821 | 390 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 4:697e1120f821 | 391 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 4:697e1120f821 | 392 | } |
ajorgih3 | 4:697e1120f821 | 393 | |
ajorgih3 | 4:697e1120f821 | 394 | void add_arrow_left(int x, int y) |
ajorgih3 | 4:697e1120f821 | 395 | { |
ajorgih3 | 4:697e1120f821 | 396 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 4:697e1120f821 | 397 | w1->type = ARROWLEFT; |
ajorgih3 | 4:697e1120f821 | 398 | w1->draw = draw_arrow_left; |
ajorgih3 | 4:697e1120f821 | 399 | w1->walkable = true; |
ajorgih3 | 4:697e1120f821 | 400 | w1->data = NULL; |
ajorgih3 | 4:697e1120f821 | 401 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 4:697e1120f821 | 402 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 4:697e1120f821 | 403 | } |
ajorgih3 | 4:697e1120f821 | 404 | |
ajorgih3 | 5:5953ca12205d | 405 | void add_lightspeed(int x, int y) |
ajorgih3 | 5:5953ca12205d | 406 | { |
ajorgih3 | 5:5953ca12205d | 407 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 5:5953ca12205d | 408 | w1->type = LIGHTSPEED; |
ajorgih3 | 5:5953ca12205d | 409 | w1->draw = draw_lightspeed; |
ajorgih3 | 5:5953ca12205d | 410 | w1->walkable = true; |
ajorgih3 | 5:5953ca12205d | 411 | w1->data = NULL; |
ajorgih3 | 5:5953ca12205d | 412 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 5:5953ca12205d | 413 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 5:5953ca12205d | 414 | } |
ajorgih3 | 5:5953ca12205d | 415 | |
ajorgih3 | 5:5953ca12205d | 416 | void add_blue_wall(int x, int y, int dir, int len) |
ajorgih3 | 5:5953ca12205d | 417 | { |
ajorgih3 | 5:5953ca12205d | 418 | for(int i = 0; i < len; i++) |
ajorgih3 | 5:5953ca12205d | 419 | { |
ajorgih3 | 5:5953ca12205d | 420 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 5:5953ca12205d | 421 | w1->type = BLUEWALL; |
ajorgih3 | 5:5953ca12205d | 422 | w1->draw = draw_blue_wall; |
ajorgih3 | 5:5953ca12205d | 423 | w1->walkable = false; |
ajorgih3 | 5:5953ca12205d | 424 | w1->data = NULL; |
ajorgih3 | 5:5953ca12205d | 425 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
ajorgih3 | 5:5953ca12205d | 426 | void* val = insertItem(get_active_map()->items, key, w1); |
ajorgih3 | 5:5953ca12205d | 427 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 5:5953ca12205d | 428 | } |
ajorgih3 | 5:5953ca12205d | 429 | } |
ajorgih3 | 5:5953ca12205d | 430 | |
ajorgih3 | 6:fbaee888e5ea | 431 | void add_reverse(int x, int y) |
ajorgih3 | 6:fbaee888e5ea | 432 | { |
ajorgih3 | 6:fbaee888e5ea | 433 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 6:fbaee888e5ea | 434 | w1->type = REVERSE; |
ajorgih3 | 6:fbaee888e5ea | 435 | w1->draw = draw_reverse; |
ajorgih3 | 6:fbaee888e5ea | 436 | w1->walkable = true; |
ajorgih3 | 6:fbaee888e5ea | 437 | w1->data = NULL; |
ajorgih3 | 6:fbaee888e5ea | 438 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 6:fbaee888e5ea | 439 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 6:fbaee888e5ea | 440 | } |
ajorgih3 | 7:7ab2f4b09196 | 441 | |
ajorgih3 | 7:7ab2f4b09196 | 442 | void add_blue_potion(int x, int y) |
ajorgih3 | 7:7ab2f4b09196 | 443 | { |
ajorgih3 | 7:7ab2f4b09196 | 444 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 7:7ab2f4b09196 | 445 | w1->type = BLUEPOTION; |
ajorgih3 | 7:7ab2f4b09196 | 446 | w1->draw = draw_blue_potion; |
ajorgih3 | 7:7ab2f4b09196 | 447 | w1->walkable = true; |
ajorgih3 | 7:7ab2f4b09196 | 448 | w1->data = NULL; |
ajorgih3 | 7:7ab2f4b09196 | 449 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 7:7ab2f4b09196 | 450 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 7:7ab2f4b09196 | 451 | } |
ajorgih3 | 7:7ab2f4b09196 | 452 | |
ajorgih3 | 7:7ab2f4b09196 | 453 | void add_red_potion(int x, int y) |
ajorgih3 | 7:7ab2f4b09196 | 454 | { |
ajorgih3 | 7:7ab2f4b09196 | 455 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 7:7ab2f4b09196 | 456 | w1->type = REDPOTION; |
ajorgih3 | 7:7ab2f4b09196 | 457 | w1->draw = draw_red_potion; |
ajorgih3 | 7:7ab2f4b09196 | 458 | w1->walkable = true; |
ajorgih3 | 7:7ab2f4b09196 | 459 | w1->data = NULL; |
ajorgih3 | 7:7ab2f4b09196 | 460 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 7:7ab2f4b09196 | 461 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 7:7ab2f4b09196 | 462 | } |
ajorgih3 | 9:d09b96b6c39c | 463 | |
ajorgih3 | 9:d09b96b6c39c | 464 | void add_portal(int x, int y) |
ajorgih3 | 9:d09b96b6c39c | 465 | { |
ajorgih3 | 9:d09b96b6c39c | 466 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
ajorgih3 | 9:d09b96b6c39c | 467 | w1->type = PORTAL; |
ajorgih3 | 9:d09b96b6c39c | 468 | w1->draw = draw_portal; |
ajorgih3 | 9:d09b96b6c39c | 469 | w1->walkable = true; |
ajorgih3 | 9:d09b96b6c39c | 470 | w1->data = NULL; |
ajorgih3 | 9:d09b96b6c39c | 471 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
ajorgih3 | 9:d09b96b6c39c | 472 | if (val) free(val); // If something is already there, free it |
ajorgih3 | 9:d09b96b6c39c | 473 | } |