P2-2 Harris Barton

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Revision:
3:e2fb359d6545
Parent:
2:4947d6a82971
--- a/map.cpp	Fri Oct 23 16:30:18 2020 -0400
+++ b/map.cpp	Wed Nov 25 01:17:39 2020 +0000
@@ -19,6 +19,8 @@
 };
 
 #define NUM_MAPS 1
+#define MAP_WIDTH   50
+#define MAP_HEIGHT  50
 static Map maps[NUM_MAPS];
 static int active_map;
 
@@ -33,7 +35,9 @@
  * This function should uniquely map (x,y) onto the space of unsigned integers.
  */
 static unsigned XY_KEY(int X, int Y) {
-     // TODO: Fix me!
+     
+     return X*maps[0].h + Y;
+     
 }
 
 /**
@@ -43,14 +47,14 @@
  */
 unsigned map_hash(unsigned key)
 {
-    // TODO: Fix me!
+    return key%(NUM_MAPS);
 }
 
 void maps_init()
 {
-    // TODO: Implement!    
-    // Initialize hash table
-    // Set width & height
+    maps[0].items = createHashTable(map_hash, NUM_MAPS);
+    maps[0].w = MAP_WIDTH;
+    maps[0].h = MAP_HEIGHT;
 }
 
 Map* get_active_map()
@@ -82,51 +86,51 @@
 
 int map_width()
 {
-
+    return get_active_map()->w;
 }
 
 int map_height()
 {
-
+    return get_active_map()->h;
 }
 
 int map_area()
 {
-
+    return map_width() * map_height();
 }
-MapItem* get_current(int x, int y)
-{
-    
-}
+
 MapItem* get_north(int x, int y)
 {
-    
+    return get_here(x, y-1);
 }
+
 MapItem* get_south(int x, int y)
 {
-    
+    return get_here(x, y+1);
 }
 
 MapItem* get_east(int x, int y)
 {
-    
+    return get_here(x+1, y);
 }
 
 MapItem* get_west(int x, int y)
 {
-
+    return get_here(x-1, y);
 }
 
 MapItem* get_here(int x, int y)
 {
-
+    return (MapItem*) getItem(get_active_map()->items, XY_KEY(x, y));
 }
 
 void map_erase(int x, int y)
 {
-
+    MapItem* item = get_here(x, y);
+    if (item && item->data)
+        free(item->data);
+    deleteItem(get_active_map()->items, XY_KEY(x, y));
 }
-
 void add_wall(int x, int y, int dir, int len)
 {
     for(int i = 0; i < len; i++)
@@ -189,7 +193,7 @@
 void add_snake_head(int x, int y)
 {
     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
-    w1->type = SNAKE_BODY;
+    w1->type = SNAKE_HEAD;
     w1->draw = draw_snake_head;
     w1->walkable = false;
     w1->data = NULL;
@@ -207,3 +211,103 @@
     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
     if (val) free(val); // If something is already there, free it
 }
+
+void add_poison(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = POISON;
+    w1->draw = draw_poison;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_speedup(int x, int y) 
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = SPEEDUP;
+    w1->draw = draw_speedup;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+    
+}
+
+void add_slowdown(int x, int y) 
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = SLOWDOWN;
+    w1->draw = draw_slowdown;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+    
+}
+
+void add_decrease_length(int x, int y) 
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = DECLENGTH;
+    w1->draw = draw_decrease_length;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+    
+}
+
+void add_random(int x, int y, int d) 
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = RANDOM;
+    w1->draw = draw_random;
+    w1->walkable = true;
+    d = d % 4;
+    w1->data = (int*)(d + 13);
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_moving(int x, int y) {
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = MOVING;
+    w1->draw = draw_moving;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_invinc(int x, int y) {
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = INVINC;
+    w1->draw = draw_invinc;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_megaGoodie(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = GOODIE;
+    w1->draw = draw_megaGoodie;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+void add_nothing(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = CLEAR;
+    w1->draw = draw_nothing;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
\ No newline at end of file