tao lao

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Revision:
3:33bf11645fe1
Parent:
2:4947d6a82971
--- a/map.cpp	Fri Oct 23 16:30:18 2020 -0400
+++ b/map.cpp	Tue Nov 24 05:05:10 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++)
@@ -207,3 +211,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_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
+}