Finished V1
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@5:2fb023cdc666, 2019-11-24 (annotated)
- Committer:
- trich9
- Date:
- Sun Nov 24 03:22:35 2019 +0000
- Revision:
- 5:2fb023cdc666
- Parent:
- 4:2297a714936f
finished V1
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 |
trich9 | 5:2fb023cdc666 | 226 | } |
trich9 | 5:2fb023cdc666 | 227 | |
trich9 | 5:2fb023cdc666 | 228 | void add_key(int x, int y){ |
trich9 | 5:2fb023cdc666 | 229 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 230 | w1->type = keyItem; |
trich9 | 5:2fb023cdc666 | 231 | w1->draw = draw_key; |
trich9 | 5:2fb023cdc666 | 232 | w1->walkable = true; |
trich9 | 5:2fb023cdc666 | 233 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 234 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 5:2fb023cdc666 | 235 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 236 | } |
trich9 | 5:2fb023cdc666 | 237 | |
trich9 | 5:2fb023cdc666 | 238 | void add_sonar(int x, int y){ |
trich9 | 5:2fb023cdc666 | 239 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 240 | w1->type = sonar; |
trich9 | 5:2fb023cdc666 | 241 | w1->draw = draw_sonar; |
trich9 | 5:2fb023cdc666 | 242 | w1->walkable = false; |
trich9 | 5:2fb023cdc666 | 243 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 244 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 5:2fb023cdc666 | 245 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 246 | } |
trich9 | 5:2fb023cdc666 | 247 | |
trich9 | 5:2fb023cdc666 | 248 | void add_treasure(int x, int y){ |
trich9 | 5:2fb023cdc666 | 249 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 250 | w1->type = treasure; |
trich9 | 5:2fb023cdc666 | 251 | w1->draw = draw_treasure; |
trich9 | 5:2fb023cdc666 | 252 | w1->walkable = false; |
trich9 | 5:2fb023cdc666 | 253 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 254 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 5:2fb023cdc666 | 255 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 256 | } |
trich9 | 5:2fb023cdc666 | 257 | |
trich9 | 5:2fb023cdc666 | 258 | |
trich9 | 5:2fb023cdc666 | 259 | void add_gate(int x, int y){ |
trich9 | 5:2fb023cdc666 | 260 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 261 | w1->type = gate; |
trich9 | 5:2fb023cdc666 | 262 | w1->draw = draw_gate; |
trich9 | 5:2fb023cdc666 | 263 | w1->walkable = false; |
trich9 | 5:2fb023cdc666 | 264 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 265 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 5:2fb023cdc666 | 266 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 267 | } |
trich9 | 5:2fb023cdc666 | 268 | |
trich9 | 5:2fb023cdc666 | 269 | void add_prisoner(int x, int y){ |
trich9 | 5:2fb023cdc666 | 270 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 271 | w1->type = prisoner; |
trich9 | 5:2fb023cdc666 | 272 | w1->draw = draw_prisoner; |
trich9 | 5:2fb023cdc666 | 273 | w1->walkable = false; |
trich9 | 5:2fb023cdc666 | 274 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 275 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 5:2fb023cdc666 | 276 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 277 | } |
trich9 | 5:2fb023cdc666 | 278 | |
trich9 | 5:2fb023cdc666 | 279 | void add_expPnt(int x, int y){ |
trich9 | 5:2fb023cdc666 | 280 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 281 | w1->type = expPnt; |
trich9 | 5:2fb023cdc666 | 282 | w1->draw = draw_expPnt; |
trich9 | 5:2fb023cdc666 | 283 | w1->walkable = true; |
trich9 | 5:2fb023cdc666 | 284 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 285 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 5:2fb023cdc666 | 286 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 287 | } |
trich9 | 5:2fb023cdc666 | 288 | |
trich9 | 5:2fb023cdc666 | 289 | void add_water(int x, int y, int dir, int len) |
trich9 | 5:2fb023cdc666 | 290 | { |
trich9 | 5:2fb023cdc666 | 291 | for(int i = 0; i < len; i++) |
trich9 | 5:2fb023cdc666 | 292 | { |
trich9 | 5:2fb023cdc666 | 293 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 294 | w1->type = water; |
trich9 | 5:2fb023cdc666 | 295 | w1->draw = draw_water; |
trich9 | 5:2fb023cdc666 | 296 | w1->walkable = false; |
trich9 | 5:2fb023cdc666 | 297 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 298 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
trich9 | 5:2fb023cdc666 | 299 | void* val = insertItem(get_active_map()->items, key, w1); |
trich9 | 5:2fb023cdc666 | 300 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 301 | } |
trich9 | 5:2fb023cdc666 | 302 | } |
trich9 | 5:2fb023cdc666 | 303 | |
trich9 | 5:2fb023cdc666 | 304 | void add_rock(int x, int y, int dir, int len) |
trich9 | 5:2fb023cdc666 | 305 | { |
trich9 | 5:2fb023cdc666 | 306 | for(int i = 0; i < len; i++) |
trich9 | 5:2fb023cdc666 | 307 | { |
trich9 | 5:2fb023cdc666 | 308 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 309 | w1->type = rock; |
trich9 | 5:2fb023cdc666 | 310 | w1->draw = draw_rock; |
trich9 | 5:2fb023cdc666 | 311 | w1->walkable = false; |
trich9 | 5:2fb023cdc666 | 312 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 313 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
trich9 | 5:2fb023cdc666 | 314 | void* val = insertItem(get_active_map()->items, key, w1); |
trich9 | 5:2fb023cdc666 | 315 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 316 | } |
trich9 | 5:2fb023cdc666 | 317 | } |
trich9 | 5:2fb023cdc666 | 318 | |
trich9 | 5:2fb023cdc666 | 319 | void add_wood(int x, int y, int dir, int len) |
trich9 | 5:2fb023cdc666 | 320 | { |
trich9 | 5:2fb023cdc666 | 321 | for(int i = 0; i < len; i++) |
trich9 | 5:2fb023cdc666 | 322 | { |
trich9 | 5:2fb023cdc666 | 323 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 324 | w1->type = wood; |
trich9 | 5:2fb023cdc666 | 325 | w1->draw = draw_wood; |
trich9 | 5:2fb023cdc666 | 326 | w1->walkable = false; |
trich9 | 5:2fb023cdc666 | 327 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 328 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
trich9 | 5:2fb023cdc666 | 329 | void* val = insertItem(get_active_map()->items, key, w1); |
trich9 | 5:2fb023cdc666 | 330 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 331 | } |
trich9 | 5:2fb023cdc666 | 332 | } |
trich9 | 5:2fb023cdc666 | 333 | |
trich9 | 5:2fb023cdc666 | 334 | |
trich9 | 5:2fb023cdc666 | 335 | void add_waterS1(int x, int y){ |
trich9 | 5:2fb023cdc666 | 336 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 337 | w1->type = waterS1; |
trich9 | 5:2fb023cdc666 | 338 | w1->draw = draw_waterS1; |
trich9 | 5:2fb023cdc666 | 339 | w1->walkable = true; |
trich9 | 5:2fb023cdc666 | 340 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 341 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 5:2fb023cdc666 | 342 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 343 | } |
trich9 | 5:2fb023cdc666 | 344 | |
trich9 | 5:2fb023cdc666 | 345 | |
trich9 | 5:2fb023cdc666 | 346 | void add_waterS2(int x, int y){ |
trich9 | 5:2fb023cdc666 | 347 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
trich9 | 5:2fb023cdc666 | 348 | w1->type = waterS2; |
trich9 | 5:2fb023cdc666 | 349 | w1->draw = draw_waterS2; |
trich9 | 5:2fb023cdc666 | 350 | w1->walkable = true; |
trich9 | 5:2fb023cdc666 | 351 | w1->data = NULL; |
trich9 | 5:2fb023cdc666 | 352 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
trich9 | 5:2fb023cdc666 | 353 | if (val) free(val); // If something is already there, free it |
trich9 | 5:2fb023cdc666 | 354 | } |