project_files/frontlib/model/map.c
changeset 7271 5608ac657362
parent 7230 240620f46dd7
child 7314 6171f0bad318
--- a/project_files/frontlib/model/map.c	Thu Jun 21 21:32:12 2012 +0200
+++ b/project_files/frontlib/model/map.c	Mon Jun 25 00:42:07 2012 +0200
@@ -9,6 +9,7 @@
 
 static void flib_map_destroy(flib_map *map) {
 	if(map) {
+		free(map->seed);
 		free(map->drawData);
 		free(map->name);
 		free(map->theme);
@@ -16,85 +17,88 @@
 	}
 }
 
-flib_map *flib_map_create_regular(const char *theme, int templateFilter) {
+flib_map *flib_map_create_regular(const char *seed, const char *theme, int templateFilter) {
 	flib_map *result = NULL;
-	if(!theme) {
+	if(!seed || !theme) {
 		flib_log_e("null parameter in flib_map_create_regular");
 	} else {
-		flib_map *newmap = flib_calloc(1, sizeof(flib_map));
-		if(newmap) {
-			newmap->_referenceCount = 1;
-			newmap->mapgen = MAPGEN_REGULAR;
-			newmap->templateFilter = templateFilter;
-			newmap->theme = flib_strdupnull(theme);
-			if(newmap->theme) {
-				result = newmap;
-				newmap = NULL;
-			}
-		}
-		flib_map_destroy(newmap);
+		flib_map newmap = {0};
+		newmap.mapgen = MAPGEN_REGULAR;
+		newmap.name = "+rnd+";
+		newmap.seed = (char*)seed;
+		newmap.theme = (char*)theme;
+		newmap.templateFilter = templateFilter;
+		result = flib_map_copy(&newmap);
+	}
+	return result;
+}
+
+flib_map *flib_map_create_maze(const char *seed, const char *theme, int mazeSize) {
+	flib_map *result = NULL;
+	if(!seed || !theme) {
+		flib_log_e("null parameter in flib_map_create_maze");
+	} else {
+		flib_map newmap = {0};
+		newmap.mapgen = MAPGEN_MAZE;
+		newmap.name = "+maze+";
+		newmap.seed = (char*)seed;
+		newmap.theme = (char*)theme;
+		newmap.mazeSize = mazeSize;
+		result = flib_map_copy(&newmap);
 	}
 	return result;
 }
 
-flib_map *flib_map_create_maze(const char *theme, int mazeSize) {
+flib_map *flib_map_create_named(const char *seed, const char *name) {
 	flib_map *result = NULL;
-	if(!theme) {
-		flib_log_e("null parameter in flib_map_create_maze");
+	if(!seed || !name) {
+		flib_log_e("null parameter in flib_map_create_named");
+	} else {
+		flib_map newmap = {0};
+		newmap.mapgen = MAPGEN_NAMED;
+		newmap.name = (char*)name;
+		newmap.seed = (char*)seed;
+		result = flib_map_copy(&newmap);
+	}
+	return result;
+}
+
+flib_map *flib_map_create_drawn(const char *seed, const char *theme, const uint8_t *drawData, int drawDataSize) {
+	flib_map *result = NULL;
+	if(!seed || !theme || (!drawData && drawDataSize)) {
+		flib_log_e("null parameter in flib_map_create_drawn");
 	} else {
-		flib_map *newmap = flib_calloc(1, sizeof(flib_map));
+		flib_map newmap = {0};
+		newmap.mapgen = MAPGEN_DRAWN;
+		newmap.name = "+drawn+";
+		newmap.seed = (char*)seed;
+		newmap.theme = (char*)theme;
+		newmap.drawData = (uint8_t*) drawData;
+		newmap.drawDataSize = drawDataSize;
+		result = flib_map_copy(&newmap);
+	}
+	return result;
+}
+
+flib_map *flib_map_copy(const flib_map *map) {
+	flib_map *result = NULL;
+	if(map) {
+		flib_map *newmap = flib_map_retain(flib_calloc(1, sizeof(flib_map)));
 		if(newmap) {
-			newmap->_referenceCount = 1;
-			newmap->mapgen = MAPGEN_MAZE;
-			newmap->mazeSize = mazeSize;
-			newmap->theme = flib_strdupnull(theme);
-			if(newmap->theme) {
+			newmap->mapgen = map->mapgen;
+			newmap->drawDataSize = map->drawDataSize;
+			newmap->drawData = flib_bufdupnull(map->drawData, map->drawDataSize);
+			newmap->mazeSize = map->mazeSize;
+			newmap->name = flib_strdupnull(map->name);
+			newmap->seed = flib_strdupnull(map->seed);
+			newmap->templateFilter = map->templateFilter;
+			newmap->theme = flib_strdupnull(map->theme);
+			if((newmap->drawData || !map->drawData) && (newmap->name || !map->name) && (newmap->seed || !map->seed) && (newmap->theme || !map->theme)) {
 				result = newmap;
 				newmap = NULL;
 			}
 		}
-		flib_map_destroy(newmap);
-	}
-	return result;
-}
-
-flib_map *flib_map_create_named(const char *name) {
-	flib_map *result = NULL;
-	if(!name) {
-		flib_log_e("null parameter in flib_map_create_named");
-	} else {
-		flib_map *newmap = flib_calloc(1, sizeof(flib_map));
-		if(newmap) {
-			newmap->_referenceCount = 1;
-			newmap->mapgen = MAPGEN_NAMED;
-			newmap->name = flib_strdupnull(name);
-			if(newmap->name) {
-				result = newmap;
-				newmap = NULL;
-			}
-		}
-		flib_map_destroy(newmap);
-	}
-	return result;
-}
-
-flib_map *flib_map_create_drawn(const char *theme, const uint8_t *drawData, int drawDataSize) {
-	flib_map *result = NULL;
-	if(!theme || !drawData) {
-		flib_log_e("null parameter in flib_map_create_named");
-	} else {
-		flib_map *newmap = flib_calloc(1, sizeof(flib_map));
-		if(newmap) {
-			newmap->_referenceCount = 1;
-			newmap->mapgen = MAPGEN_DRAWN;
-			newmap->drawData = flib_bufdupnull(drawData, drawDataSize);
-			newmap->drawDataSize = drawDataSize;
-			if(newmap->drawData) {
-				result = newmap;
-				newmap = NULL;
-			}
-		}
-		flib_map_destroy(newmap);
+		flib_map_release(newmap);
 	}
 	return result;
 }