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