Add MapSettingsManager and new mapgen setting script API functions
[oweals/minetest.git] / src / unittest / test_map_settings_manager.cpp
1  /*
2 Minetest
3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "test.h"
21
22 #include "noise.h"
23 #include "settings.h"
24 #include "mapgen_v5.h"
25 #include "util/sha1.h"
26 #include "map_settings_manager.h"
27
28 class TestMapSettingsManager : public TestBase {
29 public:
30         TestMapSettingsManager() { TestManager::registerTestModule(this); }
31         const char *getName() { return "TestMapSettingsManager"; }
32
33         void makeUserConfig(Settings *conf);
34         std::string makeMetaFile(bool make_corrupt);
35
36         void runTests(IGameDef *gamedef);
37
38         void testMapSettingsManager();
39         void testMapMetaSaveLoad();
40         void testMapMetaFailures();
41 };
42
43 static TestMapSettingsManager g_test_instance;
44
45 void TestMapSettingsManager::runTests(IGameDef *gamedef)
46 {
47         TEST(testMapSettingsManager);
48         TEST(testMapMetaSaveLoad);
49         TEST(testMapMetaFailures);
50 }
51
52 ////////////////////////////////////////////////////////////////////////////////
53
54
55 void check_noise_params(const NoiseParams *np1, const NoiseParams *np2)
56 {
57         UASSERTEQ(float, np1->offset, np2->offset);
58         UASSERTEQ(float, np1->scale, np2->scale);
59         UASSERT(np1->spread == np2->spread);
60         UASSERTEQ(s32, np1->seed, np2->seed);
61         UASSERTEQ(u16, np1->octaves, np2->octaves);
62         UASSERTEQ(float, np1->persist, np2->persist);
63         UASSERTEQ(float, np1->lacunarity, np2->lacunarity);
64         UASSERTEQ(u32, np1->flags, np2->flags);
65 }
66
67
68 std::string read_file_to_string(const std::string &filepath)
69 {
70         std::string buf;
71         FILE *f = fopen(filepath.c_str(), "rb");
72         if (!f)
73                 return "";
74
75         fseek(f, 0, SEEK_END);
76
77         long filesize = ftell(f);
78         if (filesize == -1)
79                 return "";
80         rewind(f);
81
82         buf.resize(filesize);
83
84         fread(&buf[0], 1, filesize, f);
85
86         fclose(f);
87         return buf;
88 }
89
90
91 void TestMapSettingsManager::makeUserConfig(Settings *conf)
92 {
93         conf->set("mg_name", "v7");
94         conf->set("seed", "5678");
95         conf->set("water_level", "20");
96         conf->set("mgv5_np_factor", "0, 12,  (500, 250, 500), 920382, 5, 0.45, 3.0");
97         conf->set("mgv5_np_height", "0, 15, (500, 250, 500), 841746,  5, 0.5,  3.0");
98         conf->set("mgv5_np_filler_depth", "20, 1, (150, 150, 150), 261, 4, 0.7,  1.0");
99         conf->set("mgv5_np_ground", "-43, 40, (80,  80,  80),  983240, 4, 0.55, 2.0");
100 }
101
102
103 std::string TestMapSettingsManager::makeMetaFile(bool make_corrupt)
104 {
105         std::string metafile = getTestTempFile();
106
107         const char *metafile_contents =
108                 "mg_name = v5\n"
109                 "seed = 1234\n"
110                 "mg_flags = light\n"
111                 "mgv5_np_filler_depth = 20, 1, (150, 150, 150), 261, 4, 0.7,  1.0\n"
112                 "mgv5_np_height = 20, 10, (250, 250, 250), 84174,  4, 0.5,  1.0\n";
113
114         FILE *f = fopen(metafile.c_str(), "wb");
115         UASSERT(f != NULL);
116
117         fputs(metafile_contents, f);
118         if (!make_corrupt)
119                 fputs("[end_of_params]\n", f);
120
121         fclose(f);
122
123         return metafile;
124 }
125
126
127 void TestMapSettingsManager::testMapSettingsManager()
128 {
129         Settings user_settings;
130         makeUserConfig(&user_settings);
131
132         std::string test_mapmeta_path = makeMetaFile(false);
133
134         MapSettingsManager mgr(&user_settings, test_mapmeta_path);
135         std::string value;
136
137         UASSERT(mgr.getMapSetting("mg_name", &value));
138         UASSERT(value == "v7");
139
140         // Pretend we're initializing the ServerMap
141         UASSERT(mgr.loadMapMeta());
142
143         // Pretend some scripts are requesting mapgen params
144         UASSERT(mgr.getMapSetting("mg_name", &value));
145         UASSERT(value == "v5");
146         UASSERT(mgr.getMapSetting("seed", &value));
147         UASSERT(value == "1234");
148         UASSERT(mgr.getMapSetting("water_level", &value));
149         UASSERT(value == "20");
150
151     // Pretend we have some mapgen settings configured from the scripting
152         UASSERT(mgr.setMapSetting("water_level", "15"));
153         UASSERT(mgr.setMapSetting("seed", "02468"));
154         UASSERT(mgr.setMapSetting("mg_flags", "nolight", true));
155
156         NoiseParams script_np_filler_depth(0, 100, v3f(200, 100, 200), 261, 4, 0.7, 2.0);
157         NoiseParams script_np_factor(0, 100, v3f(50, 50, 50), 920381, 3, 0.45, 2.0);
158         NoiseParams script_np_height(0, 100, v3f(450, 450, 450), 84174, 4, 0.5, 2.0);
159         NoiseParams meta_np_height(20, 10, v3f(250, 250, 250), 84174,  4, 0.5,  1.0);
160         NoiseParams user_np_ground(-43, 40, v3f(80,  80,  80),  983240, 4, 0.55, 2.0, NOISE_FLAG_EASED);
161
162         mgr.setMapSettingNoiseParams("mgv5_np_filler_depth", &script_np_filler_depth, true);
163         mgr.setMapSettingNoiseParams("mgv5_np_height", &script_np_height);
164         mgr.setMapSettingNoiseParams("mgv5_np_factor", &script_np_factor);
165
166         // Now make our Params and see if the values are correctly sourced
167         MapgenParams *params = mgr.makeMapgenParams();
168         UASSERT(params->mgtype == MAPGEN_V5);
169         UASSERT(params->chunksize == 5);
170         UASSERT(params->water_level == 15);
171         UASSERT(params->seed == 1234);
172         UASSERT((params->flags & MG_LIGHT) == 0);
173
174         MapgenV5Params *v5params = (MapgenV5Params *)params;
175
176         check_noise_params(&v5params->np_filler_depth, &script_np_filler_depth);
177         check_noise_params(&v5params->np_factor, &script_np_factor);
178         check_noise_params(&v5params->np_height, &meta_np_height);
179         check_noise_params(&v5params->np_ground, &user_np_ground);
180
181         UASSERT(mgr.setMapSetting("foobar", "25") == false);
182
183         // Pretend the ServerMap is shutting down
184         UASSERT(mgr.saveMapMeta());
185
186         // Make sure our interface expectations are met
187         UASSERT(mgr.mapgen_params == params);
188         UASSERT(mgr.makeMapgenParams() == params);
189
190         // Load the resulting map_meta.txt and make sure it contains what we expect
191         unsigned char expected_contents_hash[20] = {
192                 0xf6, 0x44, 0x90, 0xb7, 0xab, 0xd8, 0x91, 0xf4, 0x08, 0x96,
193                 0xfc, 0x7e, 0xed, 0x01, 0xc5, 0x9a, 0xfd, 0x2f, 0x2d, 0x79
194         };
195
196         SHA1 ctx;
197         std::string metafile_contents = read_file_to_string(test_mapmeta_path);
198         ctx.addBytes(&metafile_contents[0], metafile_contents.size());
199         unsigned char *sha1_result = ctx.getDigest();
200         int resultdiff = memcmp(sha1_result, expected_contents_hash, 20);
201         free(sha1_result);
202
203         UASSERT(!resultdiff);
204 }
205
206
207 void TestMapSettingsManager::testMapMetaSaveLoad()
208 {
209         Settings conf;
210         std::string path = getTestTempDirectory()
211                 + DIR_DELIM + "foobar" + DIR_DELIM + "map_meta.txt";
212
213         // Create a set of mapgen params and save them to map meta
214         conf.set("seed", "12345");
215         conf.set("water_level", "5");
216         MapSettingsManager mgr1(&conf, path);
217         MapgenParams *params1 = mgr1.makeMapgenParams();
218         UASSERT(params1);
219         UASSERT(mgr1.saveMapMeta());
220
221         // Now try loading the map meta to mapgen params
222         conf.set("seed", "67890");
223         conf.set("water_level", "32");
224         MapSettingsManager mgr2(&conf, path);
225         UASSERT(mgr2.loadMapMeta());
226         MapgenParams *params2 = mgr2.makeMapgenParams();
227         UASSERT(params2);
228
229         // Check that both results are correct
230         UASSERTEQ(u64, params1->seed, 12345);
231         UASSERTEQ(s16, params1->water_level, 5);
232         UASSERTEQ(u64, params2->seed, 12345);
233         UASSERTEQ(s16, params2->water_level, 5);
234 }
235
236
237 void TestMapSettingsManager::testMapMetaFailures()
238 {
239         std::string test_mapmeta_path;
240         Settings conf;
241
242         // Check to see if it'll fail on a non-existent map meta file
243         test_mapmeta_path = "woobawooba/fgdfg/map_meta.txt";
244         UASSERT(!fs::PathExists(test_mapmeta_path));
245
246         MapSettingsManager mgr1(&conf, test_mapmeta_path);
247         UASSERT(!mgr1.loadMapMeta());
248
249         // Check to see if it'll fail on a corrupt map meta file
250         test_mapmeta_path = makeMetaFile(true);
251         UASSERT(fs::PathExists(test_mapmeta_path));
252
253         MapSettingsManager mgr2(&conf, test_mapmeta_path);
254         UASSERT(!mgr2.loadMapMeta());
255 }