tao lao
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@3:33bf11645fe1, 2020-11-24 (annotated)
- Committer:
- hnguyen403
- Date:
- Tue Nov 24 05:05:10 2020 +0000
- Revision:
- 3:33bf11645fe1
- Parent:
- 2:4947d6a82971
tao lao;
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 |
hnguyen403 | 3:33bf11645fe1 | 22 | #define MAP_WIDTH 50 |
hnguyen403 | 3:33bf11645fe1 | 23 | #define MAP_HEIGHT 50 |
DCchico | 1:10330bce85cb | 24 | static Map maps[NUM_MAPS]; |
DCchico | 1:10330bce85cb | 25 | static int active_map; |
DCchico | 1:10330bce85cb | 26 | |
DCchico | 1:10330bce85cb | 27 | static const MapItem CLEAR_SENTINEL = { |
DCchico | 1:10330bce85cb | 28 | .type = CLEAR, |
DCchico | 1:10330bce85cb | 29 | .draw = draw_nothing |
DCchico | 1:10330bce85cb | 30 | }; |
DCchico | 1:10330bce85cb | 31 | |
DCchico | 1:10330bce85cb | 32 | /** |
DCchico | 1:10330bce85cb | 33 | * The first step in HashTable access for the map is turning the two-dimensional |
DCchico | 1:10330bce85cb | 34 | * key information (x, y) into a one-dimensional unsigned integer. |
DCchico | 1:10330bce85cb | 35 | * This function should uniquely map (x,y) onto the space of unsigned integers. |
DCchico | 1:10330bce85cb | 36 | */ |
DCchico | 1:10330bce85cb | 37 | static unsigned XY_KEY(int X, int Y) { |
hnguyen403 | 3:33bf11645fe1 | 38 | |
hnguyen403 | 3:33bf11645fe1 | 39 | return X*maps[0].h + Y; |
hnguyen403 | 3:33bf11645fe1 | 40 | |
DCchico | 1:10330bce85cb | 41 | } |
DCchico | 1:10330bce85cb | 42 | |
DCchico | 1:10330bce85cb | 43 | /** |
DCchico | 1:10330bce85cb | 44 | * This is the hash function actually passed into createHashTable. It takes an |
DCchico | 1:10330bce85cb | 45 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some |
DCchico | 1:10330bce85cb | 46 | * small non-negative integer). |
DCchico | 1:10330bce85cb | 47 | */ |
DCchico | 1:10330bce85cb | 48 | unsigned map_hash(unsigned key) |
DCchico | 1:10330bce85cb | 49 | { |
hnguyen403 | 3:33bf11645fe1 | 50 | return key%(NUM_MAPS); |
DCchico | 1:10330bce85cb | 51 | } |
DCchico | 1:10330bce85cb | 52 | |
DCchico | 1:10330bce85cb | 53 | void maps_init() |
DCchico | 1:10330bce85cb | 54 | { |
hnguyen403 | 3:33bf11645fe1 | 55 | maps[0].items = createHashTable(map_hash, NUM_MAPS); |
hnguyen403 | 3:33bf11645fe1 | 56 | maps[0].w = MAP_WIDTH; |
hnguyen403 | 3:33bf11645fe1 | 57 | maps[0].h = MAP_HEIGHT; |
DCchico | 1:10330bce85cb | 58 | } |
DCchico | 1:10330bce85cb | 59 | |
DCchico | 1:10330bce85cb | 60 | Map* get_active_map() |
DCchico | 1:10330bce85cb | 61 | { |
DCchico | 1:10330bce85cb | 62 | return &maps[active_map]; |
DCchico | 1:10330bce85cb | 63 | } |
DCchico | 1:10330bce85cb | 64 | |
DCchico | 1:10330bce85cb | 65 | Map* set_active_map(int m) |
DCchico | 1:10330bce85cb | 66 | { |
DCchico | 1:10330bce85cb | 67 | active_map = m; |
DCchico | 1:10330bce85cb | 68 | return &maps[active_map]; |
DCchico | 1:10330bce85cb | 69 | } |
DCchico | 1:10330bce85cb | 70 | |
DCchico | 1:10330bce85cb | 71 | void print_map() |
DCchico | 1:10330bce85cb | 72 | { |
DCchico | 1:10330bce85cb | 73 | char lookup[] = {'W', 'D', 'P', 'A', 'K', 'C', 'N',' ','S'}; |
DCchico | 1:10330bce85cb | 74 | Map* map = get_active_map(); |
DCchico | 1:10330bce85cb | 75 | for(int j = 0; j < map->h; j++) |
DCchico | 1:10330bce85cb | 76 | { |
DCchico | 1:10330bce85cb | 77 | for (int i = 0; i < map->w; i++) |
DCchico | 1:10330bce85cb | 78 | { |
DCchico | 1:10330bce85cb | 79 | MapItem* item = (MapItem*)getItem(map->items, XY_KEY(i, j)); |
DCchico | 1:10330bce85cb | 80 | if (item) pc.printf("%c", lookup[item->type]); |
DCchico | 1:10330bce85cb | 81 | else pc.printf(" "); |
DCchico | 1:10330bce85cb | 82 | } |
DCchico | 1:10330bce85cb | 83 | pc.printf("\r\n"); |
DCchico | 1:10330bce85cb | 84 | } |
DCchico | 1:10330bce85cb | 85 | } |
DCchico | 1:10330bce85cb | 86 | |
DCchico | 1:10330bce85cb | 87 | int map_width() |
DCchico | 1:10330bce85cb | 88 | { |
hnguyen403 | 3:33bf11645fe1 | 89 | return get_active_map()->w; |
DCchico | 1:10330bce85cb | 90 | } |
DCchico | 1:10330bce85cb | 91 | |
DCchico | 1:10330bce85cb | 92 | int map_height() |
DCchico | 1:10330bce85cb | 93 | { |
hnguyen403 | 3:33bf11645fe1 | 94 | return get_active_map()->h; |
DCchico | 1:10330bce85cb | 95 | } |
DCchico | 1:10330bce85cb | 96 | |
DCchico | 1:10330bce85cb | 97 | int map_area() |
DCchico | 1:10330bce85cb | 98 | { |
hnguyen403 | 3:33bf11645fe1 | 99 | return map_width() * map_height(); |
DCchico | 1:10330bce85cb | 100 | } |
hnguyen403 | 3:33bf11645fe1 | 101 | |
DCchico | 1:10330bce85cb | 102 | MapItem* get_north(int x, int y) |
DCchico | 1:10330bce85cb | 103 | { |
hnguyen403 | 3:33bf11645fe1 | 104 | return get_here(x, y-1); |
DCchico | 1:10330bce85cb | 105 | } |
hnguyen403 | 3:33bf11645fe1 | 106 | |
DCchico | 1:10330bce85cb | 107 | MapItem* get_south(int x, int y) |
DCchico | 1:10330bce85cb | 108 | { |
hnguyen403 | 3:33bf11645fe1 | 109 | return get_here(x, y+1); |
DCchico | 1:10330bce85cb | 110 | } |
DCchico | 1:10330bce85cb | 111 | |
DCchico | 1:10330bce85cb | 112 | MapItem* get_east(int x, int y) |
DCchico | 1:10330bce85cb | 113 | { |
hnguyen403 | 3:33bf11645fe1 | 114 | return get_here(x+1, y); |
DCchico | 1:10330bce85cb | 115 | } |
DCchico | 1:10330bce85cb | 116 | |
DCchico | 1:10330bce85cb | 117 | MapItem* get_west(int x, int y) |
DCchico | 1:10330bce85cb | 118 | { |
hnguyen403 | 3:33bf11645fe1 | 119 | return get_here(x-1, y); |
DCchico | 1:10330bce85cb | 120 | } |
DCchico | 1:10330bce85cb | 121 | |
DCchico | 1:10330bce85cb | 122 | MapItem* get_here(int x, int y) |
DCchico | 1:10330bce85cb | 123 | { |
hnguyen403 | 3:33bf11645fe1 | 124 | return (MapItem*) getItem(get_active_map()->items, XY_KEY(x, y)); |
DCchico | 1:10330bce85cb | 125 | } |
DCchico | 1:10330bce85cb | 126 | |
DCchico | 1:10330bce85cb | 127 | void map_erase(int x, int y) |
DCchico | 1:10330bce85cb | 128 | { |
hnguyen403 | 3:33bf11645fe1 | 129 | MapItem* item = get_here(x, y); |
hnguyen403 | 3:33bf11645fe1 | 130 | if (item && item->data) |
hnguyen403 | 3:33bf11645fe1 | 131 | free(item->data); |
hnguyen403 | 3:33bf11645fe1 | 132 | deleteItem(get_active_map()->items, XY_KEY(x, y)); |
DCchico | 1:10330bce85cb | 133 | } |
DCchico | 1:10330bce85cb | 134 | void add_wall(int x, int y, int dir, int len) |
DCchico | 1:10330bce85cb | 135 | { |
DCchico | 1:10330bce85cb | 136 | for(int i = 0; i < len; i++) |
DCchico | 1:10330bce85cb | 137 | { |
DCchico | 1:10330bce85cb | 138 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 139 | w1->type = WALL; |
DCchico | 1:10330bce85cb | 140 | w1->draw = draw_wall; |
DCchico | 1:10330bce85cb | 141 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 142 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 143 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
DCchico | 1:10330bce85cb | 144 | void* val = insertItem(get_active_map()->items, key, w1); |
DCchico | 1:10330bce85cb | 145 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 146 | } |
DCchico | 1:10330bce85cb | 147 | } |
DCchico | 1:10330bce85cb | 148 | |
DCchico | 1:10330bce85cb | 149 | void add_plant(int x, int y) |
DCchico | 1:10330bce85cb | 150 | { |
DCchico | 1:10330bce85cb | 151 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 152 | w1->type = PLANT; |
DCchico | 1:10330bce85cb | 153 | w1->draw = draw_plant; |
DCchico | 1:10330bce85cb | 154 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 155 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 156 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 157 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 158 | } |
DCchico | 1:10330bce85cb | 159 | |
DCchico | 1:10330bce85cb | 160 | void add_goodie(int x, int y) |
DCchico | 1:10330bce85cb | 161 | { |
DCchico | 1:10330bce85cb | 162 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 163 | w1->type = GOODIE; |
DCchico | 1:10330bce85cb | 164 | w1->draw = draw_goodie; |
DCchico | 1:10330bce85cb | 165 | w1->walkable = true; |
DCchico | 1:10330bce85cb | 166 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 167 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 168 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 169 | } |
DCchico | 1:10330bce85cb | 170 | |
DCchico | 1:10330bce85cb | 171 | void remove_goodie(int x, int y) // I'm lazy so overwrite it with a plant |
DCchico | 1:10330bce85cb | 172 | { |
DCchico | 1:10330bce85cb | 173 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 174 | w1->type = PLANT; |
DCchico | 1:10330bce85cb | 175 | w1->draw = draw_plant; |
DCchico | 1:10330bce85cb | 176 | w1->walkable = true; |
DCchico | 1:10330bce85cb | 177 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 178 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 179 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 180 | } |
DCchico | 1:10330bce85cb | 181 | |
DCchico | 1:10330bce85cb | 182 | void add_snake_body(int x, int y) |
DCchico | 1:10330bce85cb | 183 | { |
DCchico | 1:10330bce85cb | 184 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 185 | w1->type = SNAKE_BODY; |
DCchico | 1:10330bce85cb | 186 | w1->draw = draw_snake_body; |
DCchico | 1:10330bce85cb | 187 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 188 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 189 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 190 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 191 | } |
DCchico | 1:10330bce85cb | 192 | |
DCchico | 1:10330bce85cb | 193 | void add_snake_head(int x, int y) |
DCchico | 1:10330bce85cb | 194 | { |
DCchico | 1:10330bce85cb | 195 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 196 | w1->type = SNAKE_BODY; |
DCchico | 1:10330bce85cb | 197 | w1->draw = draw_snake_head; |
DCchico | 1:10330bce85cb | 198 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 199 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 200 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 201 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 202 | } |
DCchico | 1:10330bce85cb | 203 | |
DCchico | 1:10330bce85cb | 204 | void add_snake_tail(int x, int y) |
DCchico | 1:10330bce85cb | 205 | { |
DCchico | 1:10330bce85cb | 206 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
DCchico | 1:10330bce85cb | 207 | w1->type = SNAKE_BODY; |
DCchico | 1:10330bce85cb | 208 | w1->draw = draw_snake_tail; |
DCchico | 1:10330bce85cb | 209 | w1->walkable = false; |
DCchico | 1:10330bce85cb | 210 | w1->data = NULL; |
DCchico | 1:10330bce85cb | 211 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
DCchico | 1:10330bce85cb | 212 | if (val) free(val); // If something is already there, free it |
DCchico | 1:10330bce85cb | 213 | } |
hnguyen403 | 3:33bf11645fe1 | 214 | void add_nothing(int x, int y) |
hnguyen403 | 3:33bf11645fe1 | 215 | { |
hnguyen403 | 3:33bf11645fe1 | 216 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
hnguyen403 | 3:33bf11645fe1 | 217 | w1->type = CLEAR; |
hnguyen403 | 3:33bf11645fe1 | 218 | w1->draw = draw_nothing; |
hnguyen403 | 3:33bf11645fe1 | 219 | w1->walkable = true; |
hnguyen403 | 3:33bf11645fe1 | 220 | w1->data = NULL; |
hnguyen403 | 3:33bf11645fe1 | 221 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
hnguyen403 | 3:33bf11645fe1 | 222 | if (val) free(val); // If something is already there, free it |
hnguyen403 | 3:33bf11645fe1 | 223 | } |