Still won't work
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@5:142e66c8a7fa, 2019-04-12 (annotated)
- Committer:
- jtrux
- Date:
- Fri Apr 12 18:42:43 2019 +0000
- Revision:
- 5:142e66c8a7fa
- Parent:
- 4:237c791cc5c0
Still won't work;
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 | |
rconnorlawson | 0:35660d7952f7 | 23 | /** |
rconnorlawson | 0:35660d7952f7 | 24 | * The first step in HashTable access for the map is turning the two-dimensional |
rconnorlawson | 0:35660d7952f7 | 25 | * key information (x, y) into a one-dimensional unsigned integer. |
rconnorlawson | 0:35660d7952f7 | 26 | * This function should uniquely map (x,y) onto the space of unsigned integers. |
rconnorlawson | 0:35660d7952f7 | 27 | */ |
jtrux | 5:142e66c8a7fa | 28 | static unsigned XY_KEY(int X, int Y) |
jtrux | 5:142e66c8a7fa | 29 | { |
rconnorlawson | 0:35660d7952f7 | 30 | // TODO: Fix me! |
jtrux | 3:44a69b76507d | 31 | return X + (Y*map_height()); |
rconnorlawson | 0:35660d7952f7 | 32 | } |
rconnorlawson | 0:35660d7952f7 | 33 | |
rconnorlawson | 0:35660d7952f7 | 34 | /** |
rconnorlawson | 0:35660d7952f7 | 35 | * This is the hash function actually passed into createHashTable. It takes an |
rconnorlawson | 0:35660d7952f7 | 36 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some |
rconnorlawson | 0:35660d7952f7 | 37 | * small non-negative integer). |
rconnorlawson | 0:35660d7952f7 | 38 | */ |
rconnorlawson | 0:35660d7952f7 | 39 | unsigned map_hash(unsigned key) |
rconnorlawson | 0:35660d7952f7 | 40 | { |
rconnorlawson | 0:35660d7952f7 | 41 | // TODO: Fix me! |
jtrux | 3:44a69b76507d | 42 | return key%50; |
rconnorlawson | 0:35660d7952f7 | 43 | } |
rconnorlawson | 0:35660d7952f7 | 44 | |
rconnorlawson | 0:35660d7952f7 | 45 | void maps_init() |
rconnorlawson | 0:35660d7952f7 | 46 | { |
jtrux | 5:142e66c8a7fa | 47 | // TODO: Implement! |
rconnorlawson | 0:35660d7952f7 | 48 | // Initialize hash table |
rconnorlawson | 0:35660d7952f7 | 49 | // Set width & height |
jtrux | 5:142e66c8a7fa | 50 | HashTable* ht = createHashTable(map_hash, 50); |
jtrux | 3:44a69b76507d | 51 | map.items = ht; |
jtrux | 4:237c791cc5c0 | 52 | map.w = map_width(); |
jtrux | 4:237c791cc5c0 | 53 | map.h = map_height(); |
rconnorlawson | 0:35660d7952f7 | 54 | } |
rconnorlawson | 0:35660d7952f7 | 55 | |
rconnorlawson | 0:35660d7952f7 | 56 | Map* get_active_map() |
rconnorlawson | 0:35660d7952f7 | 57 | { |
rconnorlawson | 0:35660d7952f7 | 58 | // There's only one map |
rconnorlawson | 0:35660d7952f7 | 59 | return ↦ |
rconnorlawson | 0:35660d7952f7 | 60 | } |
rconnorlawson | 0:35660d7952f7 | 61 | |
rconnorlawson | 0:35660d7952f7 | 62 | Map* set_active_map(int m) |
rconnorlawson | 0:35660d7952f7 | 63 | { |
rconnorlawson | 0:35660d7952f7 | 64 | active_map = m; |
rconnorlawson | 0:35660d7952f7 | 65 | return ↦ |
rconnorlawson | 0:35660d7952f7 | 66 | } |
rconnorlawson | 0:35660d7952f7 | 67 | |
rconnorlawson | 0:35660d7952f7 | 68 | void print_map() |
rconnorlawson | 0:35660d7952f7 | 69 | { |
rconnorlawson | 0:35660d7952f7 | 70 | // As you add more types, you'll need to add more items to this array. |
rconnorlawson | 0:35660d7952f7 | 71 | char lookup[] = {'W', 'P'}; |
jtrux | 5:142e66c8a7fa | 72 | for(int y = 0; y < map_height(); y++) { |
jtrux | 5:142e66c8a7fa | 73 | for (int x = 0; x < map_width(); x++) { |
rconnorlawson | 0:35660d7952f7 | 74 | MapItem* item = get_here(x,y); |
rconnorlawson | 0:35660d7952f7 | 75 | if (item) pc.printf("%c", lookup[item->type]); |
rconnorlawson | 0:35660d7952f7 | 76 | else pc.printf(" "); |
rconnorlawson | 0:35660d7952f7 | 77 | } |
rconnorlawson | 0:35660d7952f7 | 78 | pc.printf("\r\n"); |
rconnorlawson | 0:35660d7952f7 | 79 | } |
rconnorlawson | 0:35660d7952f7 | 80 | } |
rconnorlawson | 0:35660d7952f7 | 81 | |
rconnorlawson | 0:35660d7952f7 | 82 | int map_width() |
rconnorlawson | 0:35660d7952f7 | 83 | { |
jtrux | 3:44a69b76507d | 84 | return 50; |
rconnorlawson | 0:35660d7952f7 | 85 | } |
rconnorlawson | 0:35660d7952f7 | 86 | |
rconnorlawson | 0:35660d7952f7 | 87 | int map_height() |
rconnorlawson | 0:35660d7952f7 | 88 | { |
jtrux | 3:44a69b76507d | 89 | return 50; |
rconnorlawson | 0:35660d7952f7 | 90 | } |
rconnorlawson | 0:35660d7952f7 | 91 | |
rconnorlawson | 0:35660d7952f7 | 92 | int map_area() |
rconnorlawson | 0:35660d7952f7 | 93 | { |
jtrux | 4:237c791cc5c0 | 94 | return map_width()*map_height(); |
rconnorlawson | 0:35660d7952f7 | 95 | } |
rconnorlawson | 0:35660d7952f7 | 96 | |
rconnorlawson | 0:35660d7952f7 | 97 | MapItem* get_north(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 98 | { |
jtrux | 5:142e66c8a7fa | 99 | unsigned int ynew = y-1; |
jtrux | 3:44a69b76507d | 100 | return (MapItem*)getItem(map.items,XY_KEY(x,ynew)); |
rconnorlawson | 0:35660d7952f7 | 101 | } |
rconnorlawson | 0:35660d7952f7 | 102 | |
rconnorlawson | 0:35660d7952f7 | 103 | MapItem* get_south(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 104 | { |
jtrux | 5:142e66c8a7fa | 105 | unsigned int ynew = y+1; |
jtrux | 3:44a69b76507d | 106 | return (MapItem*)getItem(map.items,XY_KEY(x,ynew)); |
rconnorlawson | 0:35660d7952f7 | 107 | } |
rconnorlawson | 0:35660d7952f7 | 108 | |
rconnorlawson | 0:35660d7952f7 | 109 | MapItem* get_east(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 110 | { |
jtrux | 5:142e66c8a7fa | 111 | unsigned int xnew = x+1; |
jtrux | 3:44a69b76507d | 112 | return (MapItem*)getItem(map.items,XY_KEY(xnew,y)); |
rconnorlawson | 0:35660d7952f7 | 113 | } |
rconnorlawson | 0:35660d7952f7 | 114 | |
rconnorlawson | 0:35660d7952f7 | 115 | MapItem* get_west(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 116 | { |
jtrux | 5:142e66c8a7fa | 117 | unsigned int xnew = x-1; |
jtrux | 3:44a69b76507d | 118 | return (MapItem*)getItem(map.items,XY_KEY(xnew,y)); |
rconnorlawson | 0:35660d7952f7 | 119 | } |
rconnorlawson | 0:35660d7952f7 | 120 | |
rconnorlawson | 0:35660d7952f7 | 121 | MapItem* get_here(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 122 | { |
jtrux | 3:44a69b76507d | 123 | return (MapItem*)getItem(map.items,XY_KEY(x,y)); |
rconnorlawson | 0:35660d7952f7 | 124 | } |
rconnorlawson | 0:35660d7952f7 | 125 | |
rconnorlawson | 0:35660d7952f7 | 126 | |
rconnorlawson | 0:35660d7952f7 | 127 | void map_erase(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 128 | { |
jtrux | 5:142e66c8a7fa | 129 | |
rconnorlawson | 0:35660d7952f7 | 130 | } |
rconnorlawson | 0:35660d7952f7 | 131 | |
rconnorlawson | 0:35660d7952f7 | 132 | void add_wall(int x, int y, int dir, int len) |
rconnorlawson | 0:35660d7952f7 | 133 | { |
jtrux | 5:142e66c8a7fa | 134 | for(int i = 0; i < len; i++) { |
rconnorlawson | 0:35660d7952f7 | 135 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 136 | w1->type = WALL; |
rconnorlawson | 0:35660d7952f7 | 137 | w1->draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 138 | w1->walkable = false; |
rconnorlawson | 0:35660d7952f7 | 139 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 140 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
rconnorlawson | 0:35660d7952f7 | 141 | void* val = insertItem(get_active_map()->items, key, w1); |
rconnorlawson | 0:35660d7952f7 | 142 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 143 | } |
rconnorlawson | 0:35660d7952f7 | 144 | } |
rconnorlawson | 0:35660d7952f7 | 145 | |
rconnorlawson | 0:35660d7952f7 | 146 | void add_plant(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 147 | { |
rconnorlawson | 0:35660d7952f7 | 148 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 149 | w1->type = PLANT; |
rconnorlawson | 0:35660d7952f7 | 150 | w1->draw = draw_plant; |
rconnorlawson | 0:35660d7952f7 | 151 | w1->walkable = true; |
rconnorlawson | 0:35660d7952f7 | 152 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 153 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
rconnorlawson | 0:35660d7952f7 | 154 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 155 | } |