Randall Kliman / Mbed 2 deprecated NEOPUNK

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers map.cpp Source File

map.cpp

00001 #include "map.h"
00002 #include "hash_table.h"
00003 #include "globals.h"
00004 #include "graphics.h"
00005 
00006 /**
00007  * The Map structure. This holds a HashTable for all the MapItems, along with
00008  * values for the width and height of the Map.
00009  */
00010 struct Map {
00011     HashTable* items;
00012     int w, h;
00013 };
00014 
00015 /**
00016  * Storage area for the maps.
00017  * This is a global variable, but can only be access from this file because it
00018  * is static.
00019  */
00020 static Map map[2];
00021 //Map 2 is cyberspace
00022 static int active_map;
00023 
00024 /**
00025  * The first step in HashTable access for the map is turning the two-dimensional
00026  * key information (x, y) into a one-dimensional unsigned integer.
00027  * This function should uniquely map (x,y) onto the space of unsigned integers.
00028  */
00029 static unsigned XY_KEY(int X, int Y) {
00030     unsigned key = X*50+Y;//0.5*(X+Y)*(X+Y+1)+Y;
00031     return key;
00032 }
00033 
00034 /**
00035  * This is the hash function actually passed into createHashTable. It takes an
00036  * unsigned key (the output of XY_KEY) and turns it into a hash value (some
00037  * small non-negative integer).
00038  */
00039 unsigned map_hash(unsigned key)
00040 {
00041     unsigned hsh = key%8;
00042     return hsh;
00043 }
00044 
00045 void maps_init()
00046 {
00047     // TODO: Implement!    
00048     // Initialize hash table
00049     map[0].items = createHashTable(map_hash,8);
00050     map[1].items = createHashTable(map_hash,8);
00051     // Set width & height
00052     map[0].w = 50;
00053     map[0].h = 50;
00054     map[1].w = 11;
00055     map[1].h = 11;
00056 }
00057 
00058 Map* get_active_map()
00059 {
00060     // There's only one map
00061     return &map[active_map];
00062 }
00063 
00064 Map* set_active_map(int m)
00065 {
00066     active_map = m;
00067     return &map[active_map];
00068 }
00069 
00070 void print_map()
00071 {
00072     // As you add more types, you'll need to add more items to this array.
00073     char lookup[] = {'W', 'P', 'N', 'T','H'};
00074     for(int y = 0; y < map_height(); y++)
00075     {
00076         for (int x = 0; x < map_width(); x++)
00077         {
00078             MapItem* item = get_here(x,y);
00079             if (item) pc.printf("%c", lookup[item->type]);
00080             else pc.printf(" ");
00081         }
00082         pc.printf("\r\n");
00083     }
00084 }
00085 
00086 int map_width()
00087 {
00088     return map[active_map].w;
00089 }
00090 
00091 int map_height()
00092 {
00093     return map[active_map].h;
00094 }
00095 
00096 int map_area()
00097 {
00098     return map_width()*map_height();
00099 }
00100 
00101 MapItem* get_north(int x, int y)
00102 {
00103     Map *m1 = get_active_map();
00104     MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x,y-1));
00105     return item;
00106 }
00107 
00108 MapItem* get_south(int x, int y)
00109 {
00110     Map *m1 = get_active_map();
00111     MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x,y+1));
00112     return item;
00113 }
00114 
00115 MapItem* get_east(int x, int y)
00116 {
00117     Map *m1 = get_active_map();
00118     MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x+1,y));
00119     return item;
00120 }
00121 
00122 MapItem* get_west(int x, int y)
00123 {
00124     Map *m1 = get_active_map();
00125     MapItem* item = (MapItem*)getItem(m1->items,XY_KEY(x-1,y));
00126     return item;
00127 }
00128 
00129 MapItem* get_here(int x, int y)
00130 {
00131     Map *m1 = get_active_map();
00132     MapItem* here = (MapItem*)getItem(m1->items,XY_KEY(x,y));
00133     return here;
00134 }
00135 
00136 
00137 void map_erase(int x, int y)
00138 {
00139     free((MapItem*)removeItem(map[active_map].items,XY_KEY(x,y)));
00140 }
00141 
00142 void add_wall(int x, int y, int dir, int len)
00143 {
00144     for(int i = 0; i < len; i++)
00145     {
00146         MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00147         w1->type = WALL;
00148         w1->draw = draw_wall;
00149         w1->walkable = false;
00150         w1->data = NULL;
00151         w1->x = x;
00152         w1->y = y;
00153         unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i);
00154         void* val = insertItem(get_active_map()->items, key, w1);
00155         if (val) free(val); // If something is already there, free it
00156     }
00157 }
00158 
00159 void add_netPortal(int x, int y)
00160 {
00161     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00162     w1->type = NPORTAL;
00163     w1->draw = draw_netPortal;
00164     w1->walkable = false;
00165     w1->data = NULL;
00166     w1->x = x;
00167     w1->y = y;
00168     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00169     if (val) free(val); // If something is already there, free it
00170 }
00171 
00172 void add_terminal(int x, int y)
00173 {
00174     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00175     w1->type = TERMINAL;
00176     w1->draw = draw_terminal;
00177     w1->walkable = false;
00178     w1->data = NULL;
00179     w1->x = x;
00180     w1->y = y;
00181     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00182     if (val) free(val); // If something is already there, free it
00183 }
00184 
00185 void add_hacked_terminal(int x, int y)
00186 {
00187     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00188     w1->type = HACKED_TERMINAL;
00189     w1->draw = draw_hacked_terminal;
00190     w1->walkable = false;
00191     w1->data = NULL;
00192     w1->x = x;
00193     w1->y = y;
00194     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00195     if (val) free(val); // If something is already there, free it
00196 }
00197 
00198 void add_NPC(int x, int y)
00199 {
00200     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00201     w1->type = NPC;
00202     w1->draw = draw_NPC;
00203     w1->walkable = false;
00204     w1->data = NULL;
00205     w1->x = x;
00206     w1->y = y;
00207     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00208     if (val) free(val); // If something is already there, free it
00209 }
00210 
00211 void add_plant(int x, int y)
00212 {
00213     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00214     w1->type = PLANT;
00215     w1->draw = draw_plant;
00216     w1->walkable = true;
00217     w1->data = NULL;
00218     w1->x = x;
00219     w1->y = y;
00220     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00221     if (val) free(val); // If something is already there, free it
00222 }
00223 
00224 void add_door(int x, int y,bool walk)
00225 {
00226     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00227     w1->type = DOOR;
00228     w1->draw = draw_door;
00229     w1->walkable = walk;
00230     w1->data = NULL;
00231     w1->x = x;
00232     w1->y = y;
00233     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00234     if (val) free(val); // If something is already there, free it
00235 }
00236 
00237 void add_boots(int x, int y){
00238   MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00239   w1->type = BOOTS;
00240   w1->draw = draw_boots;
00241   w1->walkable = false;
00242   w1->data = NULL;
00243   w1->x = x;
00244   w1->y = y;
00245   void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00246   if (val) free(val); // If something is already there, free it
00247 }
00248 void add_kill(int x, int y){
00249   MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00250   w1->type = KILL;
00251   w1->draw = draw_gun;
00252   w1->walkable = false;
00253   w1->data = NULL;
00254   w1->x = x;
00255   w1->y = y;
00256   void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00257   if (val) free(val); // If something is already there, free it
00258 }
00259 void add_orgprint(int x, int y){
00260   MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00261   w1->type = PRINTER;
00262   w1->draw = draw_printer;
00263   w1->walkable = false;
00264   w1->data = NULL;
00265   w1->x = x;
00266   w1->y = y;
00267   void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00268   if (val) free(val); // If something is already there, free it
00269 }
00270 void add_teleport(int x, int y){
00271   MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00272   w1->type = TELEPORT;
00273   w1->draw = draw_teleport;
00274   w1->walkable = false;
00275   w1->data = NULL;
00276   w1->x = x;
00277   w1->y = y;
00278   void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00279   if (val) free(val); // If something is already there, free it
00280 }