Game For ECE 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
nasiromar
Date:
Fri Nov 19 22:03:25 2021 +0000
Revision:
6:c9695079521d
Parent:
4:37d3935365f8
Child:
7:862062ffca62
Basics

Who changed what in which revision?

UserRevisionLine numberNew 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
nasiromar 6:c9695079521d 6 #define NumBuckets 5
nasiromar 6:c9695079521d 7 #define NumMaps 2
nasiromar 6:c9695079521d 8 #define HEIGHT 50
nasiromar 6:c9695079521d 9 #define WIDTH 50
nasiromar 6:c9695079521d 10
rconnorlawson 0:35660d7952f7 11 /**
rconnorlawson 0:35660d7952f7 12 * The Map structure. This holds a HashTable for all the MapItems, along with
rconnorlawson 0:35660d7952f7 13 * values for the width and height of the Map.
lballard9 4:37d3935365f8 14 * In this file you need to define how the map will be structured. IE how will
lballard9 4:37d3935365f8 15 * you put values into the map, pull them from the map. Remember a "Map" variable
nasiromar 6:c9695079521d 16 * is a hashtable plus two ints (see below)
lballard9 4:37d3935365f8 17 * You will have more than one map variable, one will be the main map with it's own hashtable.
lballard9 4:37d3935365f8 18 * Then you'll have a second map with another hashtable
lballard9 4:37d3935365f8 19 * You should store objects into the hashtable with different properties (spells
lballard9 4:37d3935365f8 20 * etc)
rconnorlawson 0:35660d7952f7 21 */
rconnorlawson 0:35660d7952f7 22 struct Map {
rconnorlawson 0:35660d7952f7 23 HashTable* items;
rconnorlawson 0:35660d7952f7 24 int w, h;
rconnorlawson 0:35660d7952f7 25 };
rconnorlawson 0:35660d7952f7 26
rconnorlawson 0:35660d7952f7 27 /**
rconnorlawson 0:35660d7952f7 28 * Storage area for the maps.
rconnorlawson 0:35660d7952f7 29 * This is a global variable, but can only be access from this file because it
rconnorlawson 0:35660d7952f7 30 * is static.
rconnorlawson 0:35660d7952f7 31 */
nasiromar 6:c9695079521d 32 static Map map[NumMaps];
rconnorlawson 0:35660d7952f7 33 static int active_map;
rconnorlawson 0:35660d7952f7 34
rconnorlawson 0:35660d7952f7 35 /**
rconnorlawson 0:35660d7952f7 36 * The first step in HashTable access for the map is turning the two-dimensional
rconnorlawson 0:35660d7952f7 37 * key information (x, y) into a one-dimensional unsigned integer.
rconnorlawson 0:35660d7952f7 38 * This function should uniquely map (x,y) onto the space of unsigned integers.
rconnorlawson 0:35660d7952f7 39 */
nasiromar 6:c9695079521d 40 static unsigned XY_KEY(int X, int Y)
nasiromar 6:c9695079521d 41 {
rconnorlawson 0:35660d7952f7 42 // TODO: Fix me!
nasiromar 6:c9695079521d 43 unsigned int key = WIDTH*X+Y;
nasiromar 6:c9695079521d 44 return key;
rconnorlawson 0:35660d7952f7 45 }
rconnorlawson 0:35660d7952f7 46
rconnorlawson 0:35660d7952f7 47 /**
rconnorlawson 0:35660d7952f7 48 * This is the hash function actually passed into createHashTable. It takes an
rconnorlawson 0:35660d7952f7 49 * unsigned key (the output of XY_KEY) and turns it into a hash value (some
rconnorlawson 0:35660d7952f7 50 * small non-negative integer).
rconnorlawson 0:35660d7952f7 51 */
rconnorlawson 0:35660d7952f7 52 unsigned map_hash(unsigned key)
rconnorlawson 0:35660d7952f7 53 {
rconnorlawson 0:35660d7952f7 54 // TODO: Fix me!
nasiromar 6:c9695079521d 55 int hash_value = key%NumBuckets;
nasiromar 6:c9695079521d 56 return hash_value;
rconnorlawson 0:35660d7952f7 57 }
rconnorlawson 0:35660d7952f7 58
rconnorlawson 0:35660d7952f7 59 void maps_init()
rconnorlawson 0:35660d7952f7 60 {
nasiromar 6:c9695079521d 61 // TODO: Implement!
rconnorlawson 0:35660d7952f7 62 // Initialize hash table
rconnorlawson 0:35660d7952f7 63 // Set width & height
nasiromar 6:c9695079521d 64
nasiromar 6:c9695079521d 65 map[0].items = createHashTable(map_hash,NumBuckets);
nasiromar 6:c9695079521d 66 map[0].w = WIDTH;
nasiromar 6:c9695079521d 67 map[0].h = HEIGHT;
nasiromar 6:c9695079521d 68
nasiromar 6:c9695079521d 69 map[1].items = createHashTable(map_hash,NumBuckets);
nasiromar 6:c9695079521d 70 map[1].w = WIDTH-25;
nasiromar 6:c9695079521d 71 map[1].h = HEIGHT-25;
nasiromar 6:c9695079521d 72
nasiromar 6:c9695079521d 73
rconnorlawson 0:35660d7952f7 74 }
rconnorlawson 0:35660d7952f7 75
rconnorlawson 0:35660d7952f7 76 Map* get_active_map()
rconnorlawson 0:35660d7952f7 77 {
rconnorlawson 0:35660d7952f7 78 // There's only one map
nasiromar 6:c9695079521d 79 return &map[active_map];
rconnorlawson 0:35660d7952f7 80 }
rconnorlawson 0:35660d7952f7 81
rconnorlawson 0:35660d7952f7 82 Map* set_active_map(int m)
rconnorlawson 0:35660d7952f7 83 {
rconnorlawson 0:35660d7952f7 84 active_map = m;
nasiromar 6:c9695079521d 85 return &map[active_map];
rconnorlawson 0:35660d7952f7 86 }
rconnorlawson 0:35660d7952f7 87
rconnorlawson 0:35660d7952f7 88 void print_map()
rconnorlawson 0:35660d7952f7 89 {
rconnorlawson 0:35660d7952f7 90 // As you add more types, you'll need to add more items to this array.
rconnorlawson 0:35660d7952f7 91 char lookup[] = {'W', 'P'};
nasiromar 6:c9695079521d 92 for(int y = 0; y < map_height(); y++) {
nasiromar 6:c9695079521d 93 for (int x = 0; x < map_width(); x++) {
rconnorlawson 0:35660d7952f7 94 MapItem* item = get_here(x,y);
rconnorlawson 0:35660d7952f7 95 if (item) pc.printf("%c", lookup[item->type]);
rconnorlawson 0:35660d7952f7 96 else pc.printf(" ");
rconnorlawson 0:35660d7952f7 97 }
rconnorlawson 0:35660d7952f7 98 pc.printf("\r\n");
rconnorlawson 0:35660d7952f7 99 }
rconnorlawson 0:35660d7952f7 100 }
rconnorlawson 0:35660d7952f7 101
rconnorlawson 0:35660d7952f7 102 int map_width()
rconnorlawson 0:35660d7952f7 103 {
nasiromar 6:c9695079521d 104 return get_active_map()->w;
rconnorlawson 0:35660d7952f7 105 }
rconnorlawson 0:35660d7952f7 106
rconnorlawson 0:35660d7952f7 107 int map_height()
rconnorlawson 0:35660d7952f7 108 {
nasiromar 6:c9695079521d 109 return get_active_map()->h;
rconnorlawson 0:35660d7952f7 110 }
rconnorlawson 0:35660d7952f7 111
rconnorlawson 0:35660d7952f7 112 int map_area()
rconnorlawson 0:35660d7952f7 113 {
nasiromar 6:c9695079521d 114 int area = map_height() * map_width();
nasiromar 6:c9695079521d 115 return area;
rconnorlawson 0:35660d7952f7 116 }
rconnorlawson 0:35660d7952f7 117
rconnorlawson 0:35660d7952f7 118 MapItem* get_north(int x, int y)
rconnorlawson 0:35660d7952f7 119 {
nasiromar 6:c9695079521d 120 unsigned int key = XY_KEY(x,y-1);
nasiromar 6:c9695079521d 121 MapItem* up = (MapItem*)getItem(get_active_map()->items,key);
nasiromar 6:c9695079521d 122 return up;
nasiromar 6:c9695079521d 123
rconnorlawson 0:35660d7952f7 124 }
rconnorlawson 0:35660d7952f7 125
rconnorlawson 0:35660d7952f7 126 MapItem* get_south(int x, int y)
rconnorlawson 0:35660d7952f7 127 {
nasiromar 6:c9695079521d 128 unsigned int key = XY_KEY(x,y+1);
nasiromar 6:c9695079521d 129 MapItem* down = (MapItem*)getItem(get_active_map()->items,key);
nasiromar 6:c9695079521d 130 return down;
nasiromar 6:c9695079521d 131
rconnorlawson 0:35660d7952f7 132 }
rconnorlawson 0:35660d7952f7 133
rconnorlawson 0:35660d7952f7 134 MapItem* get_east(int x, int y)
rconnorlawson 0:35660d7952f7 135 {
nasiromar 6:c9695079521d 136 unsigned int key = XY_KEY(x+1,y);
nasiromar 6:c9695079521d 137 MapItem* right = (MapItem*)getItem(get_active_map()->items,key);
nasiromar 6:c9695079521d 138 return right;
nasiromar 6:c9695079521d 139
rconnorlawson 0:35660d7952f7 140 }
rconnorlawson 0:35660d7952f7 141
rconnorlawson 0:35660d7952f7 142 MapItem* get_west(int x, int y)
rconnorlawson 0:35660d7952f7 143 {
nasiromar 6:c9695079521d 144 unsigned int key = XY_KEY(x-1,y);
nasiromar 6:c9695079521d 145 MapItem* left = (MapItem*)getItem(get_active_map()->items,key);
nasiromar 6:c9695079521d 146 return left;
nasiromar 6:c9695079521d 147
rconnorlawson 0:35660d7952f7 148 }
rconnorlawson 0:35660d7952f7 149
rconnorlawson 0:35660d7952f7 150 MapItem* get_here(int x, int y)
rconnorlawson 0:35660d7952f7 151 {
nasiromar 6:c9695079521d 152 unsigned int key = XY_KEY(x,y);
nasiromar 6:c9695079521d 153 MapItem* curr = (MapItem*)getItem(get_active_map()->items,key);
nasiromar 6:c9695079521d 154 return curr;
rconnorlawson 0:35660d7952f7 155 }
rconnorlawson 0:35660d7952f7 156
rconnorlawson 0:35660d7952f7 157
rconnorlawson 0:35660d7952f7 158 void map_erase(int x, int y)
rconnorlawson 0:35660d7952f7 159 {
nasiromar 6:c9695079521d 160 unsigned int key = XY_KEY(x,y);
nasiromar 6:c9695079521d 161
nasiromar 6:c9695079521d 162 destroyHashTable(get_active_map()->items);
rconnorlawson 0:35660d7952f7 163 }
rconnorlawson 0:35660d7952f7 164
rconnorlawson 0:35660d7952f7 165 void add_wall(int x, int y, int dir, int len)
rconnorlawson 0:35660d7952f7 166 {
nasiromar 6:c9695079521d 167 for(int i = 0; i < len; i++) {
rconnorlawson 0:35660d7952f7 168 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
rconnorlawson 0:35660d7952f7 169 w1->type = WALL;
rconnorlawson 0:35660d7952f7 170 w1->draw = draw_wall;
rconnorlawson 0:35660d7952f7 171 w1->walkable = false;
rconnorlawson 0:35660d7952f7 172 w1->data = NULL;
rconnorlawson 0:35660d7952f7 173 unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i);
rconnorlawson 0:35660d7952f7 174 void* val = insertItem(get_active_map()->items, key, w1);
rconnorlawson 0:35660d7952f7 175 if (val) free(val); // If something is already there, free it
rconnorlawson 0:35660d7952f7 176 }
rconnorlawson 0:35660d7952f7 177 }
rconnorlawson 0:35660d7952f7 178
nasiromar 6:c9695079521d 179 void add_castle(int x, int y)
nasiromar 6:c9695079521d 180 {
nasiromar 6:c9695079521d 181 //for(int i = 0; i < len; i++) {
nasiromar 6:c9695079521d 182 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
nasiromar 6:c9695079521d 183 w1->type = CASTL;
nasiromar 6:c9695079521d 184 w1->draw = draw_castle;
nasiromar 6:c9695079521d 185 w1->walkable = false;
nasiromar 6:c9695079521d 186 w1->data = NULL;
nasiromar 6:c9695079521d 187 //unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i);
nasiromar 6:c9695079521d 188 void* val = insertItem(get_active_map()->items, XY_KEY(x,y), w1);
nasiromar 6:c9695079521d 189 if (val) free(val); // If something is already there, free it
nasiromar 6:c9695079521d 190
nasiromar 6:c9695079521d 191 }
nasiromar 6:c9695079521d 192
nasiromar 6:c9695079521d 193
nasiromar 6:c9695079521d 194 void add_npc(int x, int y)
nasiromar 6:c9695079521d 195 {
nasiromar 6:c9695079521d 196 MapItem*npc = (MapItem*) malloc(sizeof(MapItem));
nasiromar 6:c9695079521d 197 npc->type = NPC;
nasiromar 6:c9695079521d 198 npc->draw = draw_npc;
nasiromar 6:c9695079521d 199 npc->walkable = false;
nasiromar 6:c9695079521d 200 npc->data = NULL;
nasiromar 6:c9695079521d 201 void* val = insertItem(get_active_map()->items,XY_KEY(x,y),npc);
nasiromar 6:c9695079521d 202 if (val) free(val);
nasiromar 6:c9695079521d 203 }
nasiromar 6:c9695079521d 204
nasiromar 6:c9695079521d 205 void add_portal(int x, int y)
nasiromar 6:c9695079521d 206 {
nasiromar 6:c9695079521d 207 MapItem*w1 = (MapItem*) malloc(sizeof(MapItem));
nasiromar 6:c9695079521d 208 w1->type = PORTAL;
nasiromar 6:c9695079521d 209 w1->draw = draw_portal;
nasiromar 6:c9695079521d 210 w1->walkable = true;
nasiromar 6:c9695079521d 211 w1->data = NULL;
nasiromar 6:c9695079521d 212 void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1);
nasiromar 6:c9695079521d 213 if (val) free(val);
nasiromar 6:c9695079521d 214 }
nasiromar 6:c9695079521d 215
nasiromar 6:c9695079521d 216 void add_chest(int x, int y)
nasiromar 6:c9695079521d 217 {
nasiromar 6:c9695079521d 218 MapItem*chest = (MapItem*) malloc(sizeof(MapItem));
nasiromar 6:c9695079521d 219 chest->type = CHEST;
nasiromar 6:c9695079521d 220 chest->draw = draw_chest;
nasiromar 6:c9695079521d 221 chest->walkable = false;
nasiromar 6:c9695079521d 222 chest->data = NULL;
nasiromar 6:c9695079521d 223 void* val = insertItem(get_active_map()->items,XY_KEY(x,y),chest);
nasiromar 6:c9695079521d 224 if (val) free(val);
nasiromar 6:c9695079521d 225 }
nasiromar 6:c9695079521d 226
nasiromar 6:c9695079521d 227
nasiromar 6:c9695079521d 228
nasiromar 6:c9695079521d 229
nasiromar 6:c9695079521d 230
rconnorlawson 0:35660d7952f7 231 void add_plant(int x, int y)
rconnorlawson 0:35660d7952f7 232 {
rconnorlawson 0:35660d7952f7 233 MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
rconnorlawson 0:35660d7952f7 234 w1->type = PLANT;
rconnorlawson 0:35660d7952f7 235 w1->draw = draw_plant;
rconnorlawson 0:35660d7952f7 236 w1->walkable = true;
rconnorlawson 0:35660d7952f7 237 w1->data = NULL;
rconnorlawson 0:35660d7952f7 238 void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
rconnorlawson 0:35660d7952f7 239 if (val) free(val); // If something is already there, free it
rconnorlawson 0:35660d7952f7 240 }