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