game for 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
Sterofin
Date:
Fri Nov 30 08:29:52 2018 +0000
Revision:
5:a16af8f3fea9
Parent:
4:af9d6e3b8a29
bigger doot;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rconnorlawson 0:35660d7952f7 1 #include "map.h"
Sterofin 2:06c63d567719 2 #include "hash_table.h"
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 */
Sterofin 2:06c63d567719 20 static Map map[2];
Sterofin 2:06c63d567719 21 //Map 2 is cyberspace
rconnorlawson 0:35660d7952f7 22 static int active_map;
rconnorlawson 0:35660d7952f7 23
rconnorlawson 0:35660d7952f7 24 /**
rconnorlawson 0:35660d7952f7 25 * The first step in HashTable access for the map is turning the two-dimensional
rconnorlawson 0:35660d7952f7 26 * key information (x, y) into a one-dimensional unsigned integer.
rconnorlawson 0:35660d7952f7 27 * This function should uniquely map (x,y) onto the space of unsigned integers.
rconnorlawson 0:35660d7952f7 28 */
Sterofin 3:664c79e2ceb5 29 static unsigned XY_KEY(int X, int Y) {
Sterofin 2:06c63d567719 30 unsigned key = X*50+Y;//0.5*(X+Y)*(X+Y+1)+Y;
Sterofin 2:06c63d567719 31 return key;
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 {
Sterofin 2:06c63d567719 41 unsigned hsh = key%8;
Sterofin 2:06c63d567719 42 return hsh;
rconnorlawson 0:35660d7952f7 43 }
rconnorlawson 0:35660d7952f7 44
rconnorlawson 0:35660d7952f7 45 void maps_init()
rconnorlawson 0:35660d7952f7 46 {
rconnorlawson 0:35660d7952f7 47 // TODO: Implement!
rconnorlawson 0:35660d7952f7 48 // Initialize hash table
Sterofin 2:06c63d567719 49 map[0].items = createHashTable(map_hash,8);
Sterofin 2:06c63d567719 50 map[1].items = createHashTable(map_hash,8);
rconnorlawson 0:35660d7952f7 51 // Set width & height
Sterofin 2:06c63d567719 52 map[0].w = 50;
Sterofin 2:06c63d567719 53 map[0].h = 50;
Sterofin 2:06c63d567719 54 map[1].w = 11;
Sterofin 2:06c63d567719 55 map[1].h = 11;
rconnorlawson 0:35660d7952f7 56 }
rconnorlawson 0:35660d7952f7 57
rconnorlawson 0:35660d7952f7 58 Map* get_active_map()
rconnorlawson 0:35660d7952f7 59 {
rconnorlawson 0:35660d7952f7 60 // There's only one map
Sterofin 2:06c63d567719 61 return &map[active_map];
rconnorlawson 0:35660d7952f7 62 }
rconnorlawson 0:35660d7952f7 63
rconnorlawson 0:35660d7952f7 64 Map* set_active_map(int m)
rconnorlawson 0:35660d7952f7 65 {
rconnorlawson 0:35660d7952f7 66 active_map = m;
Sterofin 2:06c63d567719 67 return &map[active_map];
rconnorlawson 0:35660d7952f7 68 }
rconnorlawson 0:35660d7952f7 69
rconnorlawson 0:35660d7952f7 70 void print_map()
rconnorlawson 0:35660d7952f7 71 {
rconnorlawson 0:35660d7952f7 72 // As you add more types, you'll need to add more items to this array.
Sterofin 2:06c63d567719 73 char lookup[] = {'W', 'P', 'N', 'T','H'};
rconnorlawson 0:35660d7952f7 74 for(int y = 0; y < map_height(); y++)
rconnorlawson 0:35660d7952f7 75 {
rconnorlawson 0:35660d7952f7 76 for (int x = 0; x < map_width(); x++)
rconnorlawson 0:35660d7952f7 77 {
rconnorlawson 0:35660d7952f7 78 MapItem* item = get_here(x,y);
rconnorlawson 0:35660d7952f7 79 if (item) pc.printf("%c", lookup[item->type]);
rconnorlawson 0:35660d7952f7 80 else pc.printf(" ");
rconnorlawson 0:35660d7952f7 81 }
rconnorlawson 0:35660d7952f7 82 pc.printf("\r\n");
rconnorlawson 0:35660d7952f7 83 }
rconnorlawson 0:35660d7952f7 84 }
rconnorlawson 0:35660d7952f7 85
rconnorlawson 0:35660d7952f7 86 int map_width()
rconnorlawson 0:35660d7952f7 87 {
Sterofin 2:06c63d567719 88 return map[active_map].w;
rconnorlawson 0:35660d7952f7 89 }
rconnorlawson 0:35660d7952f7 90
rconnorlawson 0:35660d7952f7 91 int map_height()
rconnorlawson 0:35660d7952f7 92 {
Sterofin 2:06c63d567719 93 return map[active_map].h;
rconnorlawson 0:35660d7952f7 94 }
rconnorlawson 0:35660d7952f7 95
rconnorlawson 0:35660d7952f7 96 int map_area()
rconnorlawson 0:35660d7952f7 97 {
Sterofin 2:06c63d567719 98 return map_width()*map_height();
rconnorlawson 0:35660d7952f7 99 }
rconnorlawson 0:35660d7952f7 100
rconnorlawson 0:35660d7952f7 101 MapItem* get_north(int x, int y)
rconnorlawson 0:35660d7952f7 102 {
Sterofin 2:06c63d567719 103 Map *m1 = get_active_map();
Sterofin 2:06c63d567719 104 MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x,y-1));
Sterofin 2:06c63d567719 105 return item;
rconnorlawson 0:35660d7952f7 106 }
rconnorlawson 0:35660d7952f7 107
rconnorlawson 0:35660d7952f7 108 MapItem* get_south(int x, int y)
rconnorlawson 0:35660d7952f7 109 {
Sterofin 2:06c63d567719 110 Map *m1 = get_active_map();
Sterofin 2:06c63d567719 111 MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x,y+1));
Sterofin 2:06c63d567719 112 return item;
rconnorlawson 0:35660d7952f7 113 }
rconnorlawson 0:35660d7952f7 114
rconnorlawson 0:35660d7952f7 115 MapItem* get_east(int x, int y)
rconnorlawson 0:35660d7952f7 116 {
Sterofin 2:06c63d567719 117 Map *m1 = get_active_map();
Sterofin 2:06c63d567719 118 MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x+1,y));
Sterofin 2:06c63d567719 119 return item;
rconnorlawson 0:35660d7952f7 120 }
rconnorlawson 0:35660d7952f7 121
rconnorlawson 0:35660d7952f7 122 MapItem* get_west(int x, int y)
rconnorlawson 0:35660d7952f7 123 {
Sterofin 2:06c63d567719 124 Map *m1 = get_active_map();
Sterofin 2:06c63d567719 125 MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x-1,y));
Sterofin 2:06c63d567719 126 return item;
rconnorlawson 0:35660d7952f7 127 }
rconnorlawson 0:35660d7952f7 128
rconnorlawson 0:35660d7952f7 129 MapItem* get_here(int x, int y)
rconnorlawson 0:35660d7952f7 130 {
Sterofin 2:06c63d567719 131 Map *m1 = get_active_map();
Sterofin 2:06c63d567719 132 MapItem* here = (MapItem*)getItem(m1->items,XY_KEY(x,y));
Sterofin 2:06c63d567719 133 return here;
rconnorlawson 0:35660d7952f7 134 }
rconnorlawson 0:35660d7952f7 135
rconnorlawson 0:35660d7952f7 136
rconnorlawson 0:35660d7952f7 137 void map_erase(int x, int y)
rconnorlawson 0:35660d7952f7 138 {
Sterofin 4:af9d6e3b8a29 139 free((MapItem*)removeItem(map[active_map].items,XY_KEY(x,y)));
rconnorlawson 0:35660d7952f7 140 }
rconnorlawson 0:35660d7952f7 141
rconnorlawson 0:35660d7952f7 142 void add_wall(int x, int y, int dir, int len)
rconnorlawson 0:35660d7952f7 143 {
rconnorlawson 0:35660d7952f7 144 for(int i = 0; i < len; i++)
rconnorlawson 0:35660d7952f7 145 {
rconnorlawson 0:35660d7952f7 146 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
rconnorlawson 0:35660d7952f7 147 w1->type = WALL;
rconnorlawson 0:35660d7952f7 148 w1->draw = draw_wall;
rconnorlawson 0:35660d7952f7 149 w1->walkable = false;
rconnorlawson 0:35660d7952f7 150 w1->data = NULL;
Sterofin 5:a16af8f3fea9 151 w1->x = x;
Sterofin 5:a16af8f3fea9 152 w1->y = y;
rconnorlawson 0:35660d7952f7 153 unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i);
rconnorlawson 0:35660d7952f7 154 void* val = insertItem(get_active_map()->items, key, w1);
rconnorlawson 0:35660d7952f7 155 if (val) free(val); // If something is already there, free it
rconnorlawson 0:35660d7952f7 156 }
rconnorlawson 0:35660d7952f7 157 }
rconnorlawson 0:35660d7952f7 158
Sterofin 2:06c63d567719 159 void add_netPortal(int x, int y)
Sterofin 2:06c63d567719 160 {
Sterofin 2:06c63d567719 161 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
Sterofin 2:06c63d567719 162 w1->type = NPORTAL;
Sterofin 2:06c63d567719 163 w1->draw = draw_netPortal;
Sterofin 2:06c63d567719 164 w1->walkable = false;
Sterofin 2:06c63d567719 165 w1->data = NULL;
Sterofin 5:a16af8f3fea9 166 w1->x = x;
Sterofin 5:a16af8f3fea9 167 w1->y = y;
Sterofin 2:06c63d567719 168 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
Sterofin 2:06c63d567719 169 if (val) free(val); // If something is already there, free it
Sterofin 2:06c63d567719 170 }
Sterofin 2:06c63d567719 171
Sterofin 2:06c63d567719 172 void add_terminal(int x, int y)
Sterofin 2:06c63d567719 173 {
Sterofin 2:06c63d567719 174 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
Sterofin 2:06c63d567719 175 w1->type = TERMINAL;
Sterofin 2:06c63d567719 176 w1->draw = draw_terminal;
Sterofin 2:06c63d567719 177 w1->walkable = false;
Sterofin 2:06c63d567719 178 w1->data = NULL;
Sterofin 5:a16af8f3fea9 179 w1->x = x;
Sterofin 5:a16af8f3fea9 180 w1->y = y;
Sterofin 2:06c63d567719 181 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
Sterofin 2:06c63d567719 182 if (val) free(val); // If something is already there, free it
Sterofin 2:06c63d567719 183 }
Sterofin 2:06c63d567719 184
Sterofin 2:06c63d567719 185 void add_hacked_terminal(int x, int y)
Sterofin 2:06c63d567719 186 {
Sterofin 2:06c63d567719 187 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
Sterofin 2:06c63d567719 188 w1->type = HACKED_TERMINAL;
Sterofin 2:06c63d567719 189 w1->draw = draw_hacked_terminal;
Sterofin 2:06c63d567719 190 w1->walkable = false;
Sterofin 2:06c63d567719 191 w1->data = NULL;
Sterofin 5:a16af8f3fea9 192 w1->x = x;
Sterofin 5:a16af8f3fea9 193 w1->y = y;
Sterofin 2:06c63d567719 194 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
Sterofin 2:06c63d567719 195 if (val) free(val); // If something is already there, free it
Sterofin 2:06c63d567719 196 }
Sterofin 2:06c63d567719 197
Sterofin 2:06c63d567719 198 void add_NPC(int x, int y)
Sterofin 2:06c63d567719 199 {
Sterofin 2:06c63d567719 200 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
Sterofin 2:06c63d567719 201 w1->type = NPC;
Sterofin 2:06c63d567719 202 w1->draw = draw_NPC;
Sterofin 2:06c63d567719 203 w1->walkable = false;
Sterofin 2:06c63d567719 204 w1->data = NULL;
Sterofin 5:a16af8f3fea9 205 w1->x = x;
Sterofin 5:a16af8f3fea9 206 w1->y = y;
Sterofin 2:06c63d567719 207 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
Sterofin 2:06c63d567719 208 if (val) free(val); // If something is already there, free it
Sterofin 2:06c63d567719 209 }
Sterofin 2:06c63d567719 210
rconnorlawson 0:35660d7952f7 211 void add_plant(int x, int y)
rconnorlawson 0:35660d7952f7 212 {
rconnorlawson 0:35660d7952f7 213 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
rconnorlawson 0:35660d7952f7 214 w1->type = PLANT;
rconnorlawson 0:35660d7952f7 215 w1->draw = draw_plant;
rconnorlawson 0:35660d7952f7 216 w1->walkable = true;
rconnorlawson 0:35660d7952f7 217 w1->data = NULL;
Sterofin 5:a16af8f3fea9 218 w1->x = x;
Sterofin 5:a16af8f3fea9 219 w1->y = y;
rconnorlawson 0:35660d7952f7 220 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
rconnorlawson 0:35660d7952f7 221 if (val) free(val); // If something is already there, free it
Sterofin 2:06c63d567719 222 }
Sterofin 2:06c63d567719 223
Sterofin 2:06c63d567719 224 void add_door(int x, int y,bool walk)
Sterofin 2:06c63d567719 225 {
Sterofin 2:06c63d567719 226 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
Sterofin 2:06c63d567719 227 w1->type = DOOR;
Sterofin 2:06c63d567719 228 w1->draw = draw_door;
Sterofin 2:06c63d567719 229 w1->walkable = walk;
Sterofin 2:06c63d567719 230 w1->data = NULL;
Sterofin 5:a16af8f3fea9 231 w1->x = x;
Sterofin 5:a16af8f3fea9 232 w1->y = y;
Sterofin 2:06c63d567719 233 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
Sterofin 2:06c63d567719 234 if (val) free(val); // If something is already there, free it
Sterofin 2:06c63d567719 235 }
Sterofin 2:06c63d567719 236
Sterofin 2:06c63d567719 237 void add_boots(int x, int y){
Sterofin 2:06c63d567719 238 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
Sterofin 2:06c63d567719 239 w1->type = BOOTS;
Sterofin 2:06c63d567719 240 w1->draw = draw_boots;
Sterofin 2:06c63d567719 241 w1->walkable = false;
Sterofin 2:06c63d567719 242 w1->data = NULL;
Sterofin 5:a16af8f3fea9 243 w1->x = x;
Sterofin 5:a16af8f3fea9 244 w1->y = y;
Sterofin 2:06c63d567719 245 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
Sterofin 2:06c63d567719 246 if (val) free(val); // If something is already there, free it
Sterofin 2:06c63d567719 247 }
Sterofin 2:06c63d567719 248 void add_kill(int x, int y){
Sterofin 2:06c63d567719 249 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
Sterofin 2:06c63d567719 250 w1->type = KILL;
Sterofin 2:06c63d567719 251 w1->draw = draw_gun;
Sterofin 2:06c63d567719 252 w1->walkable = false;
Sterofin 2:06c63d567719 253 w1->data = NULL;
Sterofin 5:a16af8f3fea9 254 w1->x = x;
Sterofin 5:a16af8f3fea9 255 w1->y = y;
Sterofin 2:06c63d567719 256 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
Sterofin 2:06c63d567719 257 if (val) free(val); // If something is already there, free it
Sterofin 2:06c63d567719 258 }
Sterofin 2:06c63d567719 259 void add_orgprint(int x, int y){
Sterofin 2:06c63d567719 260 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
Sterofin 2:06c63d567719 261 w1->type = PRINTER;
Sterofin 2:06c63d567719 262 w1->draw = draw_printer;
Sterofin 2:06c63d567719 263 w1->walkable = false;
Sterofin 2:06c63d567719 264 w1->data = NULL;
Sterofin 5:a16af8f3fea9 265 w1->x = x;
Sterofin 5:a16af8f3fea9 266 w1->y = y;
Sterofin 2:06c63d567719 267 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
Sterofin 2:06c63d567719 268 if (val) free(val); // If something is already there, free it
Sterofin 2:06c63d567719 269 }
Sterofin 2:06c63d567719 270 void add_teleport(int x, int y){
Sterofin 2:06c63d567719 271 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
Sterofin 2:06c63d567719 272 w1->type = TELEPORT;
Sterofin 2:06c63d567719 273 w1->draw = draw_teleport;
Sterofin 2:06c63d567719 274 w1->walkable = false;
Sterofin 2:06c63d567719 275 w1->data = NULL;
Sterofin 5:a16af8f3fea9 276 w1->x = x;
Sterofin 5:a16af8f3fea9 277 w1->y = y;
Sterofin 2:06c63d567719 278 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
Sterofin 2:06c63d567719 279 if (val) free(val); // If something is already there, free it
rconnorlawson 0:35660d7952f7 280 }