project_files/frontlib/model/map.c
changeset 7177 bf6cf4dd847a
child 7179 f84805e6df03
equal deleted inserted replaced
7175:038e3415100a 7177:bf6cf4dd847a
       
     1 #include "map.h"
       
     2 
       
     3 #include "../ini/inihelper.h"
       
     4 #include "../util.h"
       
     5 #include "../logging.h"
       
     6 
       
     7 #include <stdlib.h>
       
     8 
       
     9 flib_map *flib_map_create_regular(const char *theme, int templateFilter) {
       
    10 	flib_map *result = NULL;
       
    11 	if(!theme) {
       
    12 		flib_log_e("null parameter in flib_map_create_regular");
       
    13 	} else {
       
    14 		flib_map *newmap = calloc(1, sizeof(flib_map));
       
    15 		if(newmap) {
       
    16 			newmap->mapgen = MAPGEN_REGULAR;
       
    17 			newmap->templateFilter = templateFilter;
       
    18 			newmap->theme = flib_strdupnull(theme);
       
    19 			if(newmap->theme) {
       
    20 				result = newmap;
       
    21 				newmap = NULL;
       
    22 			}
       
    23 		}
       
    24 		flib_map_destroy(newmap);
       
    25 	}
       
    26 	return result;
       
    27 }
       
    28 
       
    29 flib_map *flib_map_create_maze(const char *theme, int mazeSize) {
       
    30 	flib_map *result = NULL;
       
    31 	if(!theme) {
       
    32 		flib_log_e("null parameter in flib_map_create_maze");
       
    33 	} else {
       
    34 		flib_map *newmap = calloc(1, sizeof(flib_map));
       
    35 		if(newmap) {
       
    36 			newmap->mapgen = MAPGEN_MAZE;
       
    37 			newmap->mazeSize = mazeSize;
       
    38 			newmap->theme = flib_strdupnull(theme);
       
    39 			if(newmap->theme) {
       
    40 				result = newmap;
       
    41 				newmap = NULL;
       
    42 			}
       
    43 		}
       
    44 		flib_map_destroy(newmap);
       
    45 	}
       
    46 	return result;
       
    47 }
       
    48 
       
    49 flib_map *flib_map_create_named(const char *name) {
       
    50 	flib_map *result = NULL;
       
    51 	if(!name) {
       
    52 		flib_log_e("null parameter in flib_map_create_named");
       
    53 	} else {
       
    54 		flib_map *newmap = calloc(1, sizeof(flib_map));
       
    55 		if(newmap) {
       
    56 			newmap->mapgen = MAPGEN_NAMED;
       
    57 			newmap->name = flib_strdupnull(name);
       
    58 			if(newmap->name) {
       
    59 				result = newmap;
       
    60 				newmap = NULL;
       
    61 			}
       
    62 		}
       
    63 		flib_map_destroy(newmap);
       
    64 	}
       
    65 	return result;
       
    66 }
       
    67 
       
    68 flib_map *flib_map_create_drawn(const char *theme, const uint8_t *drawData, int drawDataSize) {
       
    69 	flib_map *result = NULL;
       
    70 	if(!theme || !drawData) {
       
    71 		flib_log_e("null parameter in flib_map_create_named");
       
    72 	} else {
       
    73 		flib_map *newmap = calloc(1, sizeof(flib_map));
       
    74 		if(newmap) {
       
    75 			newmap->mapgen = MAPGEN_DRAWN;
       
    76 			newmap->drawData = flib_bufdupnull(drawData, drawDataSize);
       
    77 			newmap->drawDataSize = drawDataSize;
       
    78 			if(newmap->drawData) {
       
    79 				result = newmap;
       
    80 				newmap = NULL;
       
    81 			}
       
    82 		}
       
    83 		flib_map_destroy(newmap);
       
    84 	}
       
    85 	return result;
       
    86 }
       
    87 
       
    88 void flib_map_destroy(flib_map *map) {
       
    89 	if(map) {
       
    90 		free(map->drawData);
       
    91 		free(map->name);
       
    92 		free(map->theme);
       
    93 		free(map);
       
    94 	}
       
    95 }