project for 2035
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@22:33f1a0dff46c, 2020-11-25 (annotated)
- Committer:
- kblake9
- Date:
- Wed Nov 25 04:48:22 2020 +0000
- Revision:
- 22:33f1a0dff46c
- Parent:
- 18:92431a28b46b
final
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 |
DCchico | 1:10330bce85cb | 22 | static Map maps[NUM_MAPS]; |
DCchico | 1:10330bce85cb | 23 | static int active_map; |
DCchico | 1:10330bce85cb | 24 | |
DCchico | 1:10330bce85cb | 25 | static const MapItem CLEAR_SENTINEL = { |
DCchico | 1:10330bce85cb | 26 | .type = CLEAR, |
DCchico | 1:10330bce85cb | 27 | .draw = draw_nothing |
DCchico | 1:10330bce85cb | 28 | }; |
DCchico | 1:10330bce85cb | 29 | |
DCchico | 1:10330bce85cb | 30 | /** |
DCchico | 1:10330bce85cb | 31 | * The first step in HashTable access for the map is turning the two-dimensional |
DCchico | 1:10330bce85cb | 32 | * key information (x, y) into a one-dimensional unsigned integer. |
DCchico | 1:10330bce85cb | 33 | * This function should uniquely map (x,y) onto the space of unsigned integers. |
DCchico | 1:10330bce85cb | 34 | */ |
DCchico | 1:10330bce85cb | 35 | static unsigned XY_KEY(int X, int Y) { |
kblake9 | 16:ffec120dd665 | 36 | //implement pairing function for unique number |
kblake9 | 17:0549f0894d9b | 37 | return .5*(X+Y)*(X+Y+1)+Y; |
DCchico | 1:10330bce85cb | 38 | } |
DCchico | 1:10330bce85cb | 39 | |
DCchico | 1:10330bce85cb | 40 | /** |
DCchico | 1:10330bce85cb | 41 | * This is the hash function actually passed into createHashTable. It takes an |
DCchico | 1:10330bce85cb | 42 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some |
DCchico | 1:10330bce85cb | 43 | * small non-negative integer). |
DCchico | 1:10330bce85cb | 44 | */ |
DCchico | 1:10330bce85cb | 45 | unsigned map_hash(unsigned key) |
DCchico | 1:10330bce85cb | 46 | { |
kblake9 | 16:ffec120dd665 | 47 | return key%5; |
DCchico | 1:10330bce85cb | 48 | } |
DCchico | 1:10330bce85cb | 49 | |
DCchico | 1:10330bce85cb | 50 | void maps_init() |
kblake9 | 16:ffec120dd665 | 51 | { |
DCchico | 1:10330bce85cb | 52 | // Initialize hash table |
kblake9 | 17:0549f0894d9b | 53 | maps[0].items = createHashTable(map_hash, 5); |
DCchico | 1:10330bce85cb | 54 | // Set width & height |
kblake9 | 18:92431a28b46b | 55 | maps[0].w = 55; |
kblake9 | 18:92431a28b46b | 56 | maps[0].h = 55; |
DCchico | 1:10330bce85cb | 57 | } |
DCchico | 1:10330bce85cb | 58 | |
DCchico | 1:10330bce85cb | 59 | Map* get_active_map() |
DCchico | 1:10330bce85cb | 60 | { |
DCchico | 1:10330bce85cb | 61 | return &maps[active_map]; |
DCchico | 1:10330bce85cb | 62 | } |
DCchico | 1:10330bce85cb | 63 | |
DCchico | 1:10330bce85cb | 64 | Map* set_active_map(int m) |
DCchico | 1:10330bce85cb | 65 | { |
DCchico | 1:10330bce85cb | 66 | active_map = m; |
DCchico | 1:10330bce85cb | 67 | return &maps[active_map]; |
DCchico | 1:10330bce85cb | 68 | } |
DCchico | 1:10330bce85cb | 69 | |
DCchico | 1:10330bce85cb | 70 | void print_map() |
DCchico | 1:10330bce85cb | 71 | { |
DCchico | 1:10330bce85cb | 72 | char lookup[] = {'W', 'D', 'P', 'A', 'K', 'C', 'N',' ','S'}; |
DCchico | 1:10330bce85cb | 73 | Map* map = get_active_map(); |
DCchico | 1:10330bce85cb | 74 | for(int j = 0; j < map->h; j++) |
DCchico | 1:10330bce85cb | 75 | { |
DCchico | 1:10330bce85cb | 76 | for (int i = 0; i < map->w; i++) |
DCchico | 1:10330bce85cb | 77 | { |
DCchico | 1:10330bce85cb | 78 | MapItem* item = (MapItem*)getItem(map->items, XY_KEY(i, j)); |
DCchico | 1:10330bce85cb | 79 | if (item) pc.printf("%c", lookup[item->type]); |
DCchico | 1:10330bce85cb | 80 | else pc.printf(" "); |
DCchico | 1:10330bce85cb | 81 | } |
DCchico | 1:10330bce85cb | 82 | pc.printf("\r\n"); |
DCchico | 1:10330bce85cb | 83 | } |
DCchico | 1:10330bce85cb | 84 | } |
DCchico | 1:10330bce85cb | 85 | |
DCchico | 1:10330bce85cb | 86 | int map_width() |
DCchico | 1:10330bce85cb | 87 | { |
kblake9 | 16:ffec120dd665 | 88 | return get_active_map()->w; |
DCchico | 1:10330bce85cb | 89 | } |
DCchico | 1:10330bce85cb | 90 | |
DCchico | 1:10330bce85cb | 91 | int map_height() |
DCchico | 1:10330bce85cb | 92 | { |
kblake9 | 16:ffec120dd665 | 93 | return get_active_map()->h; |
DCchico | 1:10330bce85cb | 94 | } |
DCchico | 1:10330bce85cb | 95 | |
DCchico | 1:10330bce85cb | 96 | int map_area() |
DCchico | 1:10330bce85cb | 97 | { |
kblake9 | 18:92431a28b46b | 98 | pc.printf("got here!\r\n"); |
kblake9 | 16:ffec120dd665 | 99 | return map_height()*map_width(); |
DCchico | 1:10330bce85cb | 100 | } |
DCchico | 1:10330bce85cb | 101 | MapItem* get_current(int x, int y) |
DCchico | 1:10330bce85cb | 102 | { |
kblake9 | 17:0549f0894d9b | 103 | return (MapItem*)getItem(get_active_map()->items, XY_KEY(x,y)); |
DCchico | 1:10330bce85cb | 104 | } |
DCchico | 1:10330bce85cb | 105 | MapItem* get_north(int x, int y) |
DCchico | 1:10330bce85cb | 106 | { |
kblake9 | 17:0549f0894d9b | 107 | return (MapItem*)getItem(get_active_map()->items, XY_KEY(x,y+1)); |
DCchico | 1:10330bce85cb | 108 | } |
DCchico | 1:10330bce85cb | 109 | MapItem* get_south(int x, int y) |
DCchico | 1:10330bce85cb | 110 | { |
kblake9 | 17:0549f0894d9b | 111 | return (MapItem*)getItem(get_active_map()->items, XY_KEY(x,y-1)); |
DCchico | 1:10330bce85cb | 112 | } |
DCchico | 1:10330bce85cb | 113 | |
DCchico | 1:10330bce85cb | 114 | MapItem* get_east(int x, int y) |
DCchico | 1:10330bce85cb | 115 | { |
kblake9 | 17:0549f0894d9b | 116 | return (MapItem*)getItem(get_active_map()->items, XY_KEY(x+1,y)); |
DCchico | 1:10330bce85cb | 117 | } |
DCchico | 1:10330bce85cb | 118 | |
DCchico | 1:10330bce85cb | 119 | MapItem* get_west(int x, int y) |
DCchico | 1:10330bce85cb | 120 | { |
kblake9 | 17:0549f0894d9b | 121 | return (MapItem*)getItem(get_active_map()->items, XY_KEY(x-1,y)); |
DCchico | 1:10330bce85cb | 122 | } |
DCchico | 1:10330bce85cb | 123 | |
DCchico | 1:10330bce85cb | 124 | MapItem* get_here(int x, int y) |
DCchico | 1:10330bce85cb | 125 | { |
kblake9 | 17:0549f0894d9b | 126 | return (MapItem*)getItem(get_active_map()->items, XY_KEY(x,y)); |
DCchico | 1:10330bce85cb | 127 | } |
DCchico | 1:10330bce85cb | 128 | |
DCchico | 1:10330bce85cb | 129 | void map_erase(int x, int y) |
DCchico | 1:10330bce85cb | 130 | { |
kblake9 | 16:ffec120dd665 | 131 | deleteItem(get_active_map()->items, XY_KEY(x,y)); |
DCchico | 1:10330bce85cb | 132 | } |
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 |
kblake9 | 16:ffec120dd665 | 213 | } |