project for 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
kblake9
Date:
Tue Nov 24 11:10:42 2020 +0000
Revision:
6:492048165bba
Parent:
5:cd2bdbfedd21
Child:
7:1912b4a2e4fe
forgot to make snakey boy oop

Who changed what in which revision?

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