Cong Vu / Mbed 2 deprecated Project2_cvu31

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 // Copyright 2020 Georgia Tech.  All rights reserved.
00002 // The materials provided by the instructor in this course are for
00003 // the use of the students currently enrolled in the course.
00004 // Copyrighted course materials may not be further disseminated.
00005 // This file must not be made publicly available anywhere.
00006 
00007 #include "map.h"
00008 
00009 #include "globals.h"
00010 #include "graphics.h"
00011 
00012 /**
00013  * The Map structure. This holds a HashTable for all the MapItems, along with
00014  * values for the width and height of the Map.
00015  */
00016 struct Map {
00017     HashTable* items;
00018     int w, h;
00019 };
00020 
00021 #define NUM_MAPS 2
00022 #define MAP_WIDTH   50
00023 #define MAP_HEIGHT  50
00024 static Map maps[NUM_MAPS];
00025 static int active_map;
00026 
00027 static const MapItem CLEAR_SENTINEL = {
00028     .type = CLEAR,
00029     .draw = draw_nothing
00030 };
00031 
00032 /**
00033  * The first step in HashTable access for the map is turning the two-dimensional
00034  * key information (x, y) into a one-dimensional unsigned integer.
00035  * This function should uniquely map (x,y) onto the space of unsigned integers.
00036  */
00037 static unsigned XY_KEY(int X, int Y) {
00038      
00039      return X*maps[0].h + Y;
00040      
00041 }
00042 
00043 /**
00044  * This is the hash function actually passed into createHashTable. It takes an
00045  * unsigned key (the output of XY_KEY) and turns it into a hash value (some
00046  * small non-negative integer).
00047  */
00048 unsigned map_hash(unsigned key)
00049 {
00050     return key%(NUM_MAPS);
00051 }
00052 
00053 void maps_init()
00054 {
00055     maps[0].items = createHashTable(map_hash, NUM_MAPS);
00056     maps[0].w = MAP_WIDTH;
00057     maps[0].h = MAP_HEIGHT;
00058 }
00059 
00060 Map* get_active_map()
00061 {
00062     return &maps[active_map];
00063 }
00064 
00065 Map* set_active_map(int m)
00066 {
00067     active_map = m;
00068     return &maps[active_map];
00069 }
00070 
00071 void print_map()
00072 {
00073     char lookup[] = {'W', 'D', 'P', 'A', 'K', 'C', 'N',' ','S'};
00074     Map* map = get_active_map();
00075     for(int j = 0; j < map->h; j++)
00076     {
00077         for (int i = 0; i < map->w; i++)
00078         {
00079             MapItem* item = (MapItem*)getItem(map->items, XY_KEY(i, j));
00080             if (item) pc.printf("%c", lookup[item->type]);
00081             else pc.printf(" ");
00082         }
00083         pc.printf("\r\n");
00084     }
00085 }
00086 
00087 int map_width()
00088 {
00089     return get_active_map()->w;
00090 }
00091 
00092 int map_height()
00093 {
00094     return get_active_map()->h;
00095 }
00096 
00097 int map_area()
00098 {
00099     return map_width() * map_height();
00100 }
00101 
00102 MapItem* get_north(int x, int y)
00103 {
00104     return get_here(x, y-1);
00105 }
00106 
00107 MapItem* get_south(int x, int y)
00108 {
00109     return get_here(x, y+1);
00110 }
00111 
00112 MapItem* get_east(int x, int y)
00113 {
00114     return get_here(x+1, y);
00115 }
00116 
00117 MapItem* get_west(int x, int y)
00118 {
00119     return get_here(x-1, y);
00120 }
00121 
00122 MapItem* get_here(int x, int y)
00123 {
00124     return (MapItem*) getItem(get_active_map()->items, XY_KEY(x, y));
00125 }
00126 
00127 void map_erase(int x, int y)
00128 {
00129     MapItem* item = get_here(x, y);
00130     if (item && item->data)
00131         free(item->data);
00132     deleteItem(get_active_map()->items, XY_KEY(x, y));
00133 }
00134 void add_wall(int x, int y, int dir, int len)
00135 {
00136     for(int i = 0; i < len; i++)
00137     {
00138         MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00139         w1->type = WALL;
00140         w1->draw = draw_wall;
00141         w1->walkable = false;
00142         w1->data = NULL;
00143         unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i);
00144         void* val = insertItem(get_active_map()->items, key, w1);
00145         if (val) free(val); // If something is already there, free it
00146     }
00147 }
00148 
00149 void add_plant(int x, int y)
00150 {
00151     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00152     w1->type = PLANT;
00153     w1->draw = draw_plant;
00154     w1->walkable = false;
00155     w1->data = NULL;
00156     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00157     if (val) free(val); // If something is already there, free it
00158 }
00159 
00160 void add_goodie(int x, int y)
00161 {
00162     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00163     w1->type = GOODIE;
00164     w1->draw = draw_goodie;
00165     w1->walkable = true;
00166     w1->data = NULL;
00167     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00168     if (val) free(val); // If something is already there, free it
00169 }
00170 
00171 void add_inclength(int x, int y)
00172 {
00173     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00174     w1->type = INCLENGTH;
00175     w1->draw = draw_inclength;
00176     w1->walkable = true;
00177     w1->data = NULL;
00178     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00179     if (val) free(val); // If something is already there, free it
00180 }
00181 
00182 void remove_goodie(int x, int y) // 
00183 {
00184     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00185     w1->type = PLANT;
00186     w1->draw = draw_plant;
00187     w1->walkable = true;
00188     w1->data = NULL;
00189     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00190     if (val) free(val); // If something is already there, free it
00191 }
00192 
00193 void add_snake_body(int x, int y)
00194 {
00195     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00196     w1->type = SNAKE_BODY;
00197     w1->draw = draw_snake_body;
00198     w1->walkable = false;
00199     w1->data = NULL;
00200     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00201     if (val) free(val); // If something is already there, free it
00202 }
00203 
00204 void add_snake_head(int x, int y)
00205 {
00206     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00207     w1->type = SNAKE_HEAD;
00208     w1->draw = draw_snake_head;
00209     w1->walkable = false;
00210     w1->data = NULL;
00211     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00212     if (val) free(val); // If something is already there, free it
00213 }
00214 
00215 void add_snake_tail(int x, int y)
00216 {
00217     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00218     w1->type = SNAKE_BODY;
00219     w1->draw = draw_snake_tail;
00220     w1->walkable = false;
00221     w1->data = NULL;
00222     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00223     if (val) free(val); // If something is already there, free it
00224 }
00225 
00226 void add_poison(int x, int y)
00227 {
00228     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00229     w1->type = POISON;
00230     w1->draw = draw_poison;
00231     w1->walkable = true;
00232     w1->data = NULL;
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_speedup(int x, int y) 
00238 {
00239     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00240     w1->type = SPEEDUP;
00241     w1->draw = draw_speedup;
00242     w1->walkable = true;
00243     w1->data = NULL;
00244     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00245     if (val) free(val); // If something is already there, free it
00246     
00247 }
00248 
00249 void add_slowdown(int x, int y) 
00250 {
00251     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00252     w1->type = SLOWDOWN;
00253     w1->draw = draw_slowdown;
00254     w1->walkable = true;
00255     w1->data = NULL;
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 }
00260 
00261 void add_decrease_length(int x, int y) 
00262 {
00263     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00264     w1->type = DECLENGTH;
00265     w1->draw = draw_decrease_length;
00266     w1->walkable = true;
00267     w1->data = NULL;
00268     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00269     if (val) free(val); // If something is already there, free it
00270     
00271 }
00272 
00273 void add_random(int x, int y, int d) 
00274 {
00275     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00276     w1->type = RANDOM;
00277     w1->draw = draw_random;
00278     w1->walkable = true;
00279     d = d % 4;
00280     w1->data = (int*)(d + 13);
00281     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00282     if (val) free(val); // If something is already there, free it
00283 }
00284 
00285 void add_moving(int x, int y) {
00286     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00287     w1->type = MOVING;
00288     w1->draw = draw_moving;
00289     w1->walkable = true;
00290     w1->data = NULL;
00291     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00292     if (val) free(val); // If something is already there, free it
00293 }
00294 
00295 void add_invinc(int x, int y) {
00296     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00297     w1->type = INVINC;
00298     w1->draw = draw_invinc;
00299     w1->walkable = true;
00300     w1->data = NULL;
00301     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
00302     if (val) free(val); // If something is already there, free it
00303 }
00304 
00305 void add_nothing(int x, int y)
00306 {
00307     MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
00308     w1->type = CLEAR;
00309     w1->draw = draw_nothing;
00310     w1->walkable = true;
00311     w1->data = NULL;
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 }