1 /*
2 * Hedgewars, a free turn based strategy game
3 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #include "map.h"
21
22 #include "../util/inihelper.h"
23 #include "../util/util.h"
24 #include "../util/logging.h"
25
26 #include <stdlib.h>
27
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)) {
30 return NULL;
31 }
32 flib_map newmap = {0};
33 newmap.mapgen = MAPGEN_REGULAR;
34 newmap.name = "+rnd+";
35 newmap.seed = (char*)seed;
36 newmap.theme = (char*)theme;
37 newmap.templateFilter = templateFilter;
38 return flib_map_copy(&newmap);
39 }
40
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)) {
43 return NULL;
44 }
45 flib_map newmap = {0};
46 newmap.mapgen = MAPGEN_MAZE;
47 newmap.name = "+maze+";
48 newmap.seed = (char*)seed;
49 newmap.theme = (char*)theme;
50 newmap.mazeSize = mazeSize;
51 return flib_map_copy(&newmap);
52 }
53
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)) {
56 return NULL;
57 }
58 flib_map newmap = {0};
59 newmap.mapgen = MAPGEN_NAMED;
60 newmap.name = (char*)name;
61 newmap.seed = (char*)seed;
62 return flib_map_copy(&newmap);
63 }
64
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)) {
67 return NULL;
68 }
69 flib_map newmap = {0};
70 newmap.mapgen = MAPGEN_DRAWN;
71 newmap.name = "+drawn+";
72 newmap.seed = (char*)seed;
73 newmap.theme = (char*)theme;
74 newmap.drawData = (uint8_t*) drawData;
75 newmap.drawDataSize = drawDataSize;
76 return flib_map_copy(&newmap);
77 }
78
flib_map_copy(const flib_map * map)79 flib_map *flib_map_copy(const flib_map *map) {
80 flib_map *result = NULL;
81 if(map) {
82 flib_map *newmap = flib_calloc(1, sizeof(flib_map));
83 if(newmap) {
84 newmap->mapgen = map->mapgen;
85 newmap->drawDataSize = map->drawDataSize;
86 newmap->drawData = flib_bufdupnull(map->drawData, map->drawDataSize);
87 newmap->mazeSize = map->mazeSize;
88 newmap->name = flib_strdupnull(map->name);
89 newmap->seed = flib_strdupnull(map->seed);
90 newmap->templateFilter = map->templateFilter;
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)) {
93 result = newmap;
94 newmap = NULL;
95 }
96 }
97 flib_map_destroy(newmap);
98 }
99 return result;
100 }
101
flib_map_destroy(flib_map * map)102 void flib_map_destroy(flib_map *map) {
103 if(map) {
104 free(map->seed);
105 free(map->drawData);
106 free(map->name);
107 free(map->theme);
108 free(map);
109 }
110 }
111