this is lame
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@5:37ed7d5744a6, 2019-04-11 (annotated)
- Committer:
- korib
- Date:
- Thu Apr 11 19:13:49 2019 -0400
- Revision:
- 5:37ed7d5744a6
- Parent:
- 2:c5848a443855
I like to commit. This is a bad commit message
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 | */ |
kalejandro3 | 2:c5848a443855 | 20 | static Map map[2]; |
kalejandro3 | 2:c5848a443855 | 21 | static int active_map=0; |
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 | */ |
rconnorlawson | 0:35660d7952f7 | 28 | static unsigned XY_KEY(int X, int Y) { |
rconnorlawson | 0:35660d7952f7 | 29 | // TODO: Fix me! |
kalejandro3 | 2:c5848a443855 | 30 | return X* map[active_map].h + Y; |
rconnorlawson | 0:35660d7952f7 | 31 | } |
rconnorlawson | 0:35660d7952f7 | 32 | |
rconnorlawson | 0:35660d7952f7 | 33 | /** |
rconnorlawson | 0:35660d7952f7 | 34 | * This is the hash function actually passed into createHashTable. It takes an |
rconnorlawson | 0:35660d7952f7 | 35 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some |
rconnorlawson | 0:35660d7952f7 | 36 | * small non-negative integer). |
rconnorlawson | 0:35660d7952f7 | 37 | */ |
rconnorlawson | 0:35660d7952f7 | 38 | unsigned map_hash(unsigned key) |
rconnorlawson | 0:35660d7952f7 | 39 | { |
kalejandro3 | 2:c5848a443855 | 40 | |
kalejandro3 | 2:c5848a443855 | 41 | return key % NUMBUCK;// key mod number of buckets- returns the right bucket number that the key is in |
rconnorlawson | 0:35660d7952f7 | 42 | } |
rconnorlawson | 0:35660d7952f7 | 43 | |
rconnorlawson | 0:35660d7952f7 | 44 | void maps_init() |
rconnorlawson | 0:35660d7952f7 | 45 | { |
kalejandro3 | 2:c5848a443855 | 46 | map[0].items =createHashTable(map_hash, NUMBUCK); //initialize hash table w numbuck from globals.h |
kalejandro3 | 2:c5848a443855 | 47 | map[0].w= 50; // set width- in globals.h |
kalejandro3 | 2:c5848a443855 | 48 | map[0].h= 50; //set height- in global map[0].items =createHashTable(map_hash, NUMBUCK); //initialize hash table w numbuck from globals.h |
kalejandro3 | 2:c5848a443855 | 49 | map[1].items =createHashTable(map_hash, NUMBUCK); |
kalejandro3 | 2:c5848a443855 | 50 | map[1].w= 50; // set width- in globals.h |
kalejandro3 | 2:c5848a443855 | 51 | map[1].h= 50; //set height- in globals.hs.h |
kalejandro3 | 2:c5848a443855 | 52 | |
kalejandro3 | 2:c5848a443855 | 53 | |
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 |
kalejandro3 | 2:c5848a443855 | 59 | return & map[active_map]; |
rconnorlawson | 0:35660d7952f7 | 60 | } |
rconnorlawson | 0:35660d7952f7 | 61 | |
rconnorlawson | 0:35660d7952f7 | 62 | Map* set_active_map(int m) |
rconnorlawson | 0:35660d7952f7 | 63 | { |
kalejandro3 | 2:c5848a443855 | 64 | active_map = m; |
kalejandro3 | 2:c5848a443855 | 65 | return & map[active_map]; |
rconnorlawson | 0:35660d7952f7 | 66 | } |
rconnorlawson | 0:35660d7952f7 | 67 | |
kalejandro3 | 2:c5848a443855 | 68 | #define WALL 0 |
kalejandro3 | 2:c5848a443855 | 69 | #define PLANT 1 |
kalejandro3 | 2:c5848a443855 | 70 | #define GATE 2 |
kalejandro3 | 2:c5848a443855 | 71 | #define NPC 3 |
kalejandro3 | 2:c5848a443855 | 72 | |
kalejandro3 | 2:c5848a443855 | 73 | |
rconnorlawson | 0:35660d7952f7 | 74 | void print_map() |
rconnorlawson | 0:35660d7952f7 | 75 | { |
rconnorlawson | 0:35660d7952f7 | 76 | // As you add more types, you'll need to add more items to this array. |
rconnorlawson | 0:35660d7952f7 | 77 | char lookup[] = {'W', 'P'}; |
rconnorlawson | 0:35660d7952f7 | 78 | for(int y = 0; y < map_height(); y++) |
rconnorlawson | 0:35660d7952f7 | 79 | { |
rconnorlawson | 0:35660d7952f7 | 80 | for (int x = 0; x < map_width(); x++) |
rconnorlawson | 0:35660d7952f7 | 81 | { |
rconnorlawson | 0:35660d7952f7 | 82 | MapItem* item = get_here(x,y); |
rconnorlawson | 0:35660d7952f7 | 83 | if (item) pc.printf("%c", lookup[item->type]); |
rconnorlawson | 0:35660d7952f7 | 84 | else pc.printf(" "); |
rconnorlawson | 0:35660d7952f7 | 85 | } |
rconnorlawson | 0:35660d7952f7 | 86 | pc.printf("\r\n"); |
rconnorlawson | 0:35660d7952f7 | 87 | } |
rconnorlawson | 0:35660d7952f7 | 88 | } |
rconnorlawson | 0:35660d7952f7 | 89 | |
rconnorlawson | 0:35660d7952f7 | 90 | int map_width() |
rconnorlawson | 0:35660d7952f7 | 91 | { |
kalejandro3 | 2:c5848a443855 | 92 | Map* m= get_active_map();//get the active map |
kalejandro3 | 2:c5848a443855 | 93 | return m -> w; //returns the active map's width |
rconnorlawson | 0:35660d7952f7 | 94 | } |
rconnorlawson | 0:35660d7952f7 | 95 | |
rconnorlawson | 0:35660d7952f7 | 96 | int map_height() |
rconnorlawson | 0:35660d7952f7 | 97 | { |
kalejandro3 | 2:c5848a443855 | 98 | Map* m= get_active_map(); //get the active map |
kalejandro3 | 2:c5848a443855 | 99 | return m -> h; // returns the active map's height |
rconnorlawson | 0:35660d7952f7 | 100 | } |
rconnorlawson | 0:35660d7952f7 | 101 | |
rconnorlawson | 0:35660d7952f7 | 102 | int map_area() |
rconnorlawson | 0:35660d7952f7 | 103 | { |
kalejandro3 | 2:c5848a443855 | 104 | Map * m= get_active_map(); |
kalejandro3 | 2:c5848a443855 | 105 | return (m-> h * m-> w); |
rconnorlawson | 0:35660d7952f7 | 106 | } |
rconnorlawson | 0:35660d7952f7 | 107 | |
rconnorlawson | 0:35660d7952f7 | 108 | MapItem* get_north(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 109 | { |
kalejandro3 | 2:c5848a443855 | 110 | Map *m= get_active_map(); |
kalejandro3 | 2:c5848a443855 | 111 | int key= XY_KEY(x,y+1);//north is y+1 |
kalejandro3 | 2:c5848a443855 | 112 | return (MapItem *) getItem(map-> items, key); //gets the item corresponding to that key- uses hash_table.c function |
rconnorlawson | 0:35660d7952f7 | 113 | } |
rconnorlawson | 0:35660d7952f7 | 114 | |
rconnorlawson | 0:35660d7952f7 | 115 | MapItem* get_south(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 116 | { |
kalejandro3 | 2:c5848a443855 | 117 | Map *m= get_active_map(); |
kalejandro3 | 2:c5848a443855 | 118 | int key= XY_KEY(x,y-1);//south is y-1 |
kalejandro3 | 2:c5848a443855 | 119 | return (MapItem *) getItem(map-> items, key); //gets the item corresponding to that key- uses hash_table.c function |
rconnorlawson | 0:35660d7952f7 | 120 | } |
rconnorlawson | 0:35660d7952f7 | 121 | |
rconnorlawson | 0:35660d7952f7 | 122 | MapItem* get_east(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 123 | { |
kalejandro3 | 2:c5848a443855 | 124 | Map *m= get_active_map(); |
kalejandro3 | 2:c5848a443855 | 125 | int key= XY_KEY(x+1,y);//east is x+1 |
kalejandro3 | 2:c5848a443855 | 126 | return (MapItem *) getItem(map-> items, key); //gets the item corresponding to that key- uses hash_table.c function |
rconnorlawson | 0:35660d7952f7 | 127 | } |
rconnorlawson | 0:35660d7952f7 | 128 | |
rconnorlawson | 0:35660d7952f7 | 129 | MapItem* get_west(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 130 | { |
kalejandro3 | 2:c5848a443855 | 131 | Map *m= get_active_map(); |
kalejandro3 | 2:c5848a443855 | 132 | int key= XY_KEY(x-1,y);//west is x-1 |
kalejandro3 | 2:c5848a443855 | 133 | return (MapItem *) getItem(map-> items, key); //gets the item corresponding to that key- uses hash_table.c function |
rconnorlawson | 0:35660d7952f7 | 134 | } |
rconnorlawson | 0:35660d7952f7 | 135 | |
rconnorlawson | 0:35660d7952f7 | 136 | MapItem* get_here(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 137 | { |
kalejandro3 | 2:c5848a443855 | 138 | Map *m= get_active_map(); |
kalejandro3 | 2:c5848a443855 | 139 | int key= XY_KEY(x,y); |
kalejandro3 | 2:c5848a443855 | 140 | return (MapItem *) getItem(map-> items, key); //gets the item corresponding to that key- uses hash_table.c function |
rconnorlawson | 0:35660d7952f7 | 141 | } |
rconnorlawson | 0:35660d7952f7 | 142 | |
kalejandro3 | 2:c5848a443855 | 143 | int check_type(int x, int y) |
kalejandro3 | 2:c5848a443855 | 144 | { |
kalejandro3 | 2:c5848a443855 | 145 | |
kalejandro3 | 2:c5848a443855 | 146 | } |
rconnorlawson | 0:35660d7952f7 | 147 | void map_erase(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 148 | { |
kalejandro3 | 2:c5848a443855 | 149 | Map *map= get_active_map();//gets current map |
kalejandro3 | 2:c5848a443855 | 150 | int key= XY_KEY(x,y);//gets current key |
kalejandro3 | 2:c5848a443855 | 151 | MapItem* item = (MapItem *) getItem(map-> items, key);//current item |
kalejandro3 | 2:c5848a443855 | 152 | //condition for erase//// if key found??? |
kalejandro3 | 2:c5848a443855 | 153 | if (item -> type == GATE) deleteItem(map->items, key); // if gate erase previous map- |
kalejandro3 | 2:c5848a443855 | 154 | set_active_map(active_map +1); // set the active map to be the next map |
rconnorlawson | 0:35660d7952f7 | 155 | } |
rconnorlawson | 0:35660d7952f7 | 156 | |
rconnorlawson | 0:35660d7952f7 | 157 | void add_wall(int x, int y, int dir, int len) |
rconnorlawson | 0:35660d7952f7 | 158 | { |
rconnorlawson | 0:35660d7952f7 | 159 | for(int i = 0; i < len; i++) |
rconnorlawson | 0:35660d7952f7 | 160 | { |
rconnorlawson | 0:35660d7952f7 | 161 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 162 | w1->type = WALL; |
rconnorlawson | 0:35660d7952f7 | 163 | w1->draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 164 | w1->walkable = false; |
rconnorlawson | 0:35660d7952f7 | 165 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 166 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
rconnorlawson | 0:35660d7952f7 | 167 | void* val = insertItem(get_active_map()->items, key, w1); |
rconnorlawson | 0:35660d7952f7 | 168 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 169 | } |
rconnorlawson | 0:35660d7952f7 | 170 | } |
rconnorlawson | 0:35660d7952f7 | 171 | |
rconnorlawson | 0:35660d7952f7 | 172 | void add_plant(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 173 | { |
rconnorlawson | 0:35660d7952f7 | 174 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 175 | w1->type = PLANT; |
rconnorlawson | 0:35660d7952f7 | 176 | w1->draw = draw_plant; |
rconnorlawson | 0:35660d7952f7 | 177 | w1->walkable = true; |
rconnorlawson | 0:35660d7952f7 | 178 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 179 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
rconnorlawson | 0:35660d7952f7 | 180 | if (val) free(val); // If something is already there, free it |
kalejandro3 | 2:c5848a443855 | 181 | } |
kalejandro3 | 2:c5848a443855 | 182 | |
kalejandro3 | 2:c5848a443855 | 183 | //add other things |