Carter Montgomery / Mbed 2 deprecated 2035_Final_Project

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 
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 
00011 
00012 /**
00013  * Storage area for the maps.
00014  * This is a global variable, but can only be access from this file because it
00015  * is static.
00016  */
00017 static Map maps[3];
00018 static int active_map;
00019 
00020 /**
00021  * The first step in HashTable access for the map is turning the two-dimensional
00022  * key information (x, y) into a one-dimensional unsigned integer.
00023  * This function should uniquely map (x,y) onto the space of unsigned integers.
00024  */
00025 static unsigned XY_KEY(int X, int Y) {
00026     Map * m = get_active_map();
00027     return (m->w)*Y + X;
00028 }
00029 
00030 /**
00031  * This is the hash function actually passed into createHashTable. It takes an
00032  * unsigned key (the output of XY_KEY) and turns it into a hash value (some
00033  * small non-negative integer).
00034  */
00035 unsigned map_hash(unsigned key)
00036 {
00037     // TODO: Fix me!
00038     return key%5;
00039 }
00040 
00041 void maps_init(int h, int w, int buckets)
00042 {
00043     Map * m = get_active_map();
00044     m->items = createHashTable(map_hash, buckets);
00045     m->w = w;
00046     m->h = h;
00047 }
00048 
00049 Map* get_active_map()
00050 {
00051     
00052     return &(maps[active_map]);
00053 }
00054 
00055 Map* set_active_map(int m)
00056 {
00057     active_map = m;
00058     return &(maps[active_map]);
00059 }
00060 
00061 void print_map()
00062 {
00063     // As you add more types, you'll need to add more items to this array.
00064     char lookup[] = {'W', 'P'};
00065     for(int y = 0; y < map_height(); y++)
00066     {
00067         for (int x = 0; x < map_width(); x++)
00068         {
00069             MapItem* item = get_here(x,y);
00070             if (item) pc.printf("%c", lookup[item->type]);
00071             else pc.printf(" ");
00072         }
00073         pc.printf("\r\n");
00074     }
00075 }
00076 
00077 int map_width()
00078 {
00079     Map * m = get_active_map();
00080     return m->w;
00081 }
00082 
00083 int map_height()
00084 {
00085     Map * m = get_active_map();
00086     return m->h;
00087 }
00088 
00089 int map_area()
00090 {
00091     Map * m = get_active_map();
00092     return (m->h)*(m->w);
00093 }
00094 
00095 MapItem* get_north(int x, int y)
00096 {
00097     Map* map = get_active_map();
00098     //get key of item north of the location
00099     int index = XY_KEY(x, y-1);
00100     return (MapItem*)getItem(map->items, index);
00101 }
00102 
00103 MapItem* get_south(int x, int y)
00104 {
00105     Map* map = get_active_map();
00106     int index = XY_KEY(x, y+1);
00107     return (MapItem*)getItem(map->items, index);
00108 }
00109 
00110 MapItem* get_east(int x, int y)
00111 {
00112     Map* map = get_active_map();
00113     int index = XY_KEY(x-1, y); //wtf why does this work boi 
00114     return (MapItem*)getItem(map->items, index);
00115 }
00116 
00117 MapItem* get_west(int x, int y)
00118 {
00119     Map* map = get_active_map();
00120     int index = XY_KEY(x+1, y); //shouldn't this be minus?
00121     return (MapItem*)getItem(map->items, index);
00122 }
00123 
00124 MapItem* get_here(int x, int y)
00125 {
00126     Map* map = get_active_map();
00127     int index = XY_KEY(x, y);
00128     return (MapItem*)getItem(map->items, index);
00129 }
00130 
00131 
00132 void map_erase(int x, int y)
00133 {
00134     Map* map = get_active_map();
00135     int index = XY_KEY(x, y);
00136     deleteItem(map->items, index);
00137 }
00138 
00139 
00140 void add_wall(int x, int y, int dir, int len)
00141 {
00142     for(int i = 0; i < len; i++)
00143     {
00144         MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00145         w1->type = WALL;
00146         w1->draw = draw_wall;
00147         w1->walkable = false;
00148         w1->data = NULL;
00149         unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i);
00150         void* val = insertItem(get_active_map()->items, key, w1);
00151         if (val) free(val); // If something is already there, free it
00152     }
00153 }
00154 
00155 /*void add_road(int x, int y, int dir, int len)
00156 {
00157     for(int i = 0; i < len; i++)
00158     {
00159         MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00160         w1->type = ROAD;
00161         w1->draw = draw_wall;
00162         w1->walkable = true;
00163         w1->data = NULL;
00164         unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i);
00165         void* val = insertItem(get_active_map()->items, key, w1);
00166         if (val) free(val); // If something is already there, free it
00167     }
00168 }*/
00169 
00170 void add_lbush(int x, int y, int key){
00171     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00172     w1->type = LBUSH;
00173     w1->draw = draw_lbush;
00174     w1->walkable = true;
00175     MapItem* w2 = (MapItem*) malloc(sizeof(MapItem));
00176     w2->type = LBUSH;
00177     w2->draw = draw_lbush;
00178     w2->walkable = true;
00179     MapItem* w3 = (MapItem*) malloc(sizeof(MapItem));
00180     w3->type = LBUSH;
00181     w3->draw = draw_lbush;
00182     w3->walkable = true;
00183     MapItem* w4 = (MapItem*) malloc(sizeof(MapItem));
00184     w4->type = LBUSH;
00185     w4->draw = draw_lbush;
00186     w4->walkable = true;
00187     
00188     Lbush* b1 = (Lbush*)malloc(sizeof(Lbush));
00189     b1 -> key = key;
00190     
00191     w1 -> data = b1;
00192     w2 -> data = b1;
00193     w3 -> data = b1;
00194     w4 -> data = b1;
00195     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00196     if (val) free(val); // If something is already there, free it
00197     void* val1 = insertItem(get_active_map()->items, XY_KEY(x+1, y), w2);
00198     if (val1) free(val); // If something is already there, free it
00199     void* val2 = insertItem(get_active_map()->items, XY_KEY(x, y+1), w3);
00200     if (val2) free(val); // If something is already there, free it
00201     void* val3 = insertItem(get_active_map()->items, XY_KEY(x+1, y+1), w4);
00202     if (val3) free(val); // If something is already there, free it
00203 }
00204 
00205 void add_bush_rect(int a, int b, int c, int d){
00206     
00207     for(int i = a; i < c; i++){
00208         for(int j = b; j < d; j++){
00209             MapItem* b = (MapItem*) malloc(sizeof(MapItem));
00210             b->type = LBUSH;
00211             b->draw = draw_lbush;
00212             b->walkable = true;
00213     
00214             Lbush* bush = (Lbush*)malloc(sizeof(Lbush));
00215             bush -> key = XY_KEY(i,j);
00216             b -> data = bush;
00217             void* val = insertItem(get_active_map()->items, XY_KEY(i, j), b);
00218             if (val) free(val); // If something is already there, free it
00219         }
00220     }
00221 }
00222 
00223 void add_plant(int x, int y)
00224 {
00225     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00226     w1->type = PLANT;
00227     w1->draw = draw_plant;
00228     w1->walkable = true;
00229     w1->data = NULL;
00230     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00231     if (val) free(val); // If something is already there, free it
00232 }
00233 
00234 void add_rock(int x, int y)
00235 {
00236     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00237     w1->type = ROCK;
00238     w1->draw = draw_rock;
00239     w1->walkable = false;
00240     Rock* r = (Rock*) malloc(sizeof(Rock));
00241     //r -> is_pushed = false;
00242     //r -> wall_touch = false;
00243     //r -> x = x;
00244     //r -> y = y;
00245     w1->data = r;
00246     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00247     if (val) free(val); // If something is already there, free it
00248 }
00249 
00250 void add_npc(int x, int y)
00251 {
00252     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00253     w1->type = NPC;
00254     w1->draw = draw_npc;
00255     w1->walkable = false;
00256     NonPlayer* npc = (NonPlayer*) malloc(sizeof(NonPlayer));
00257     npc->quest_requested = false;
00258     npc->quest_complete = false;
00259     npc->has_key = true;
00260     w1->data = npc;
00261     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00262     if (val) free(val); // If something is already there, free it
00263 }
00264 
00265 void add_extra(int x, int y)
00266 {
00267     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00268     w1->type = EXTRA;
00269     w1->draw = draw_npc;
00270     w1->walkable = false;
00271     Extra* npc = (Extra*) malloc(sizeof(Extra));
00272     npc -> x = x;
00273     npc -> y = y;
00274     npc -> e = false;
00275     w1->data = npc;
00276     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00277     if (val) free(val); // If something is already there, free it
00278 }
00279 
00280 void add_door(int x, int y)
00281 {
00282     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00283     w1->type = DOOR;
00284     w1->draw = draw_door;
00285     w1->walkable = false;
00286     int o = false;
00287     w1->data = &o;
00288     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00289     if (val) free(val); // If something is already there, free it
00290 }
00291 
00292 void add_gdoor(int x, int y)
00293 {
00294     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00295     w1->type = GDOOR;
00296     w1->draw = draw_door;
00297     w1->walkable = false;
00298     int o = false;
00299     w1->data = &o;
00300     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00301     if (val) free(val); // If something is already there, free it
00302 }
00303 
00304 void add_goal(int x, int y)
00305 {
00306     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00307     w1->type = GOAL;
00308     w1->draw = draw_goal;
00309     w1->walkable = false;
00310     int o = false;
00311     w1->data = &o;
00312     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00313     if (val) free(val); // If something is already there, free it
00314 }
00315 
00316 void add_spike(int x, int y)
00317 {
00318     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00319     w1->type = SPIKE;
00320     w1->draw = draw_spike;
00321     w1->walkable = true;
00322     int o = false;
00323     w1->data = &o;
00324     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00325     if (val) free(val); // If something is already there, free it
00326 }
00327 
00328 void add_gem(int x, int y, int color)
00329 {
00330     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00331     w1->type = GEM;
00332     int* c = &color;
00333     if (color == 1){
00334         w1->draw = draw_gem1;
00335         
00336     } else if (color == 2){
00337         w1->draw = draw_gem2;
00338     } else{
00339         w1->draw = draw_gem3;
00340     }
00341     w1->walkable = true;
00342     w1->data = c;
00343     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00344     if (val) free(val); // If something is already there, free it
00345 }
00346 
00347 void add_redhouse(int x, int y, int key){
00348     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00349     w1->type = HOUSE;
00350     w1->draw = draw_rhouse1;
00351     w1->walkable = false;
00352     MapItem* w2 = (MapItem*) malloc(sizeof(MapItem));
00353     w2->type = HOUSE;
00354     w2->draw = draw_rhouse2;
00355     w2->walkable = false;
00356     MapItem* w3 = (MapItem*) malloc(sizeof(MapItem));
00357     w3->type = HOUSE;
00358     w3->draw = draw_rhouse2;
00359     w3->walkable = false;
00360     MapItem* w4 = (MapItem*) malloc(sizeof(MapItem));
00361     w4->type = HOUSE;
00362     w4->draw = draw_rhouse1;
00363     w4->walkable = false;
00364     MapItem* w5 = (MapItem*) malloc(sizeof(MapItem));
00365     w5->type = HOUSE;
00366     w5->draw = draw_rhouse2;
00367     w5->walkable = false;
00368     
00369     House* r = (House*)malloc(sizeof(House));
00370     r -> key = key;
00371     
00372     w1 -> data = r;
00373     w2 -> data = r;
00374     w3 -> data = r;
00375     w4 -> data = r;
00376     w5 -> data = r;
00377     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00378     if (val) free(val); // If something is already there, free it
00379     void* val1 = insertItem(get_active_map()->items, XY_KEY(x+1, y), w2);
00380     if (val1) free(val); // If something is already there, free it
00381     void* val2 = insertItem(get_active_map()->items, XY_KEY(x+1, y+1), w3);
00382     if (val2) free(val); // If something is already there, free it
00383     void* val3 = insertItem(get_active_map()->items, XY_KEY(x, y+2), w4);
00384     if (val3) free(val); // If something is already there, free it
00385     void* val4 = insertItem(get_active_map()->items, XY_KEY(x+1, y+2), w5);
00386     if (val4) free(val); // If something is already there, free it
00387     add_door(x, y+1);
00388 }
00389 
00390 
00391 void add_bluehouse(int x, int y, int key){
00392     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00393     w1->type = HOUSE;
00394     w1->draw = draw_bhouse1;
00395     w1->walkable = false;
00396     MapItem* w2 = (MapItem*) malloc(sizeof(MapItem));
00397     w2->type = HOUSE;
00398     w2->draw = draw_bhouse2;
00399     w2->walkable = false;
00400     MapItem* w3 = (MapItem*) malloc(sizeof(MapItem));
00401     w3->type = HOUSE;
00402     w3->draw = draw_bhouse2;
00403     w3->walkable = false;
00404     MapItem* w4 = (MapItem*) malloc(sizeof(MapItem));
00405     w4->type = HOUSE;
00406     w4->draw = draw_bhouse1;
00407     w4->walkable = false;
00408     MapItem* w5 = (MapItem*) malloc(sizeof(MapItem));
00409     w5->type = HOUSE;
00410     w5->draw = draw_bhouse2;
00411     w5->walkable = false;
00412     
00413     House* r = (House*)malloc(sizeof(House));
00414     r -> key = key;
00415     
00416     w1 -> data = r;
00417     w2 -> data = r;
00418     w3 -> data = r;
00419     w4 -> data = r;
00420     w5 -> data = r;
00421     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00422     if (val) free(val); // If something is already there, free it
00423     void* val1 = insertItem(get_active_map()->items, XY_KEY(x+1, y), w2);
00424     if (val1) free(val); // If something is already there, free it
00425     void* val2 = insertItem(get_active_map()->items, XY_KEY(x+1, y+1), w3);
00426     if (val2) free(val); // If something is already there, free it
00427     void* val3 = insertItem(get_active_map()->items, XY_KEY(x, y+2), w4);
00428     if (val3) free(val); // If something is already there, free it
00429     void* val4 = insertItem(get_active_map()->items, XY_KEY(x+1, y+2), w5);
00430     if (val4) free(val); // If something is already there, free it
00431     add_door(x, y+1);
00432 }