project_files/frontlib/model/map.c
changeset 10017 de822cd3df3a
parent 7497 7e1d72fc03c7
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    24 #include "../util/logging.h"
    24 #include "../util/logging.h"
    25 
    25 
    26 #include <stdlib.h>
    26 #include <stdlib.h>
    27 
    27 
    28 flib_map *flib_map_create_regular(const char *seed, const char *theme, int templateFilter) {
    28 flib_map *flib_map_create_regular(const char *seed, const char *theme, int templateFilter) {
    29 	if(log_badargs_if2(seed==NULL, theme==NULL)) {
    29     if(log_badargs_if2(seed==NULL, theme==NULL)) {
    30 		return NULL;
    30         return NULL;
    31 	}
    31     }
    32 	flib_map newmap = {0};
    32     flib_map newmap = {0};
    33 	newmap.mapgen = MAPGEN_REGULAR;
    33     newmap.mapgen = MAPGEN_REGULAR;
    34 	newmap.name = "+rnd+";
    34     newmap.name = "+rnd+";
    35 	newmap.seed = (char*)seed;
    35     newmap.seed = (char*)seed;
    36 	newmap.theme = (char*)theme;
    36     newmap.theme = (char*)theme;
    37 	newmap.templateFilter = templateFilter;
    37     newmap.templateFilter = templateFilter;
    38 	return flib_map_copy(&newmap);
    38     return flib_map_copy(&newmap);
    39 }
    39 }
    40 
    40 
    41 flib_map *flib_map_create_maze(const char *seed, const char *theme, int mazeSize) {
    41 flib_map *flib_map_create_maze(const char *seed, const char *theme, int mazeSize) {
    42 	if(log_badargs_if2(seed==NULL, theme==NULL)) {
    42     if(log_badargs_if2(seed==NULL, theme==NULL)) {
    43 		return NULL;
    43         return NULL;
    44 	}
    44     }
    45 	flib_map newmap = {0};
    45     flib_map newmap = {0};
    46 	newmap.mapgen = MAPGEN_MAZE;
    46     newmap.mapgen = MAPGEN_MAZE;
    47 	newmap.name = "+maze+";
    47     newmap.name = "+maze+";
    48 	newmap.seed = (char*)seed;
    48     newmap.seed = (char*)seed;
    49 	newmap.theme = (char*)theme;
    49     newmap.theme = (char*)theme;
    50 	newmap.mazeSize = mazeSize;
    50     newmap.mazeSize = mazeSize;
    51 	return flib_map_copy(&newmap);
    51     return flib_map_copy(&newmap);
    52 }
    52 }
    53 
    53 
    54 flib_map *flib_map_create_named(const char *seed, const char *name) {
    54 flib_map *flib_map_create_named(const char *seed, const char *name) {
    55 	if(log_badargs_if2(seed==NULL, name==NULL)) {
    55     if(log_badargs_if2(seed==NULL, name==NULL)) {
    56 		return NULL;
    56         return NULL;
    57 	}
    57     }
    58 	flib_map newmap = {0};
    58     flib_map newmap = {0};
    59 	newmap.mapgen = MAPGEN_NAMED;
    59     newmap.mapgen = MAPGEN_NAMED;
    60 	newmap.name = (char*)name;
    60     newmap.name = (char*)name;
    61 	newmap.seed = (char*)seed;
    61     newmap.seed = (char*)seed;
    62 	return flib_map_copy(&newmap);
    62     return flib_map_copy(&newmap);
    63 }
    63 }
    64 
    64 
    65 flib_map *flib_map_create_drawn(const char *seed, const char *theme, const uint8_t *drawData, size_t drawDataSize) {
    65 flib_map *flib_map_create_drawn(const char *seed, const char *theme, const uint8_t *drawData, size_t drawDataSize) {
    66 	if(log_badargs_if3(seed==NULL, theme==NULL, drawData==NULL)) {
    66     if(log_badargs_if3(seed==NULL, theme==NULL, drawData==NULL)) {
    67 		return NULL;
    67         return NULL;
    68 	}
    68     }
    69 	flib_map newmap = {0};
    69     flib_map newmap = {0};
    70 	newmap.mapgen = MAPGEN_DRAWN;
    70     newmap.mapgen = MAPGEN_DRAWN;
    71 	newmap.name = "+drawn+";
    71     newmap.name = "+drawn+";
    72 	newmap.seed = (char*)seed;
    72     newmap.seed = (char*)seed;
    73 	newmap.theme = (char*)theme;
    73     newmap.theme = (char*)theme;
    74 	newmap.drawData = (uint8_t*) drawData;
    74     newmap.drawData = (uint8_t*) drawData;
    75 	newmap.drawDataSize = drawDataSize;
    75     newmap.drawDataSize = drawDataSize;
    76 	return flib_map_copy(&newmap);
    76     return flib_map_copy(&newmap);
    77 }
    77 }
    78 
    78 
    79 flib_map *flib_map_copy(const flib_map *map) {
    79 flib_map *flib_map_copy(const flib_map *map) {
    80 	flib_map *result = NULL;
    80     flib_map *result = NULL;
    81 	if(map) {
    81     if(map) {
    82 		flib_map *newmap = flib_calloc(1, sizeof(flib_map));
    82         flib_map *newmap = flib_calloc(1, sizeof(flib_map));
    83 		if(newmap) {
    83         if(newmap) {
    84 			newmap->mapgen = map->mapgen;
    84             newmap->mapgen = map->mapgen;
    85 			newmap->drawDataSize = map->drawDataSize;
    85             newmap->drawDataSize = map->drawDataSize;
    86 			newmap->drawData = flib_bufdupnull(map->drawData, map->drawDataSize);
    86             newmap->drawData = flib_bufdupnull(map->drawData, map->drawDataSize);
    87 			newmap->mazeSize = map->mazeSize;
    87             newmap->mazeSize = map->mazeSize;
    88 			newmap->name = flib_strdupnull(map->name);
    88             newmap->name = flib_strdupnull(map->name);
    89 			newmap->seed = flib_strdupnull(map->seed);
    89             newmap->seed = flib_strdupnull(map->seed);
    90 			newmap->templateFilter = map->templateFilter;
    90             newmap->templateFilter = map->templateFilter;
    91 			newmap->theme = flib_strdupnull(map->theme);
    91             newmap->theme = flib_strdupnull(map->theme);
    92 			if((newmap->drawData || !map->drawData) && (newmap->name || !map->name) && (newmap->seed || !map->seed) && (newmap->theme || !map->theme)) {
    92             if((newmap->drawData || !map->drawData) && (newmap->name || !map->name) && (newmap->seed || !map->seed) && (newmap->theme || !map->theme)) {
    93 				result = newmap;
    93                 result = newmap;
    94 				newmap = NULL;
    94                 newmap = NULL;
    95 			}
    95             }
    96 		}
    96         }
    97 		flib_map_destroy(newmap);
    97         flib_map_destroy(newmap);
    98 	}
    98     }
    99 	return result;
    99     return result;
   100 }
   100 }
   101 
   101 
   102 void flib_map_destroy(flib_map *map) {
   102 void flib_map_destroy(flib_map *map) {
   103 	if(map) {
   103     if(map) {
   104 		free(map->seed);
   104         free(map->seed);
   105 		free(map->drawData);
   105         free(map->drawData);
   106 		free(map->name);
   106         free(map->name);
   107 		free(map->theme);
   107         free(map->theme);
   108 		free(map);
   108         free(map);
   109 	}
   109     }
   110 }
   110 }