You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1837 lines
51 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "lua_api/l_mapgen.h"
  17. #include "lua_api/l_internal.h"
  18. #include "lua_api/l_vmanip.h"
  19. #include "common/c_converter.h"
  20. #include "common/c_content.h"
  21. #include "cpp_api/s_security.h"
  22. #include "util/serialize.h"
  23. #include "server.h"
  24. #include "environment.h"
  25. #include "emerge.h"
  26. #include "mg_biome.h"
  27. #include "mg_ore.h"
  28. #include "mg_decoration.h"
  29. #include "mg_schematic.h"
  30. #include "mapgen_v5.h"
  31. #include "mapgen_v7.h"
  32. #include "filesys.h"
  33. #include "settings.h"
  34. #include "log.h"
  35. struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
  36. {
  37. {BIOMETYPE_NORMAL, "normal"},
  38. {0, NULL},
  39. };
  40. struct EnumString ModApiMapgen::es_DecorationType[] =
  41. {
  42. {DECO_SIMPLE, "simple"},
  43. {DECO_SCHEMATIC, "schematic"},
  44. {DECO_LSYSTEM, "lsystem"},
  45. {0, NULL},
  46. };
  47. struct EnumString ModApiMapgen::es_MapgenObject[] =
  48. {
  49. {MGOBJ_VMANIP, "voxelmanip"},
  50. {MGOBJ_HEIGHTMAP, "heightmap"},
  51. {MGOBJ_BIOMEMAP, "biomemap"},
  52. {MGOBJ_HEATMAP, "heatmap"},
  53. {MGOBJ_HUMIDMAP, "humiditymap"},
  54. {MGOBJ_GENNOTIFY, "gennotify"},
  55. {0, NULL},
  56. };
  57. struct EnumString ModApiMapgen::es_OreType[] =
  58. {
  59. {ORE_SCATTER, "scatter"},
  60. {ORE_SHEET, "sheet"},
  61. {ORE_PUFF, "puff"},
  62. {ORE_BLOB, "blob"},
  63. {ORE_VEIN, "vein"},
  64. {0, NULL},
  65. };
  66. struct EnumString ModApiMapgen::es_Rotation[] =
  67. {
  68. {ROTATE_0, "0"},
  69. {ROTATE_90, "90"},
  70. {ROTATE_180, "180"},
  71. {ROTATE_270, "270"},
  72. {ROTATE_RAND, "random"},
  73. {0, NULL},
  74. };
  75. struct EnumString ModApiMapgen::es_SchematicFormatType[] =
  76. {
  77. {SCHEM_FMT_HANDLE, "handle"},
  78. {SCHEM_FMT_MTS, "mts"},
  79. {SCHEM_FMT_LUA, "lua"},
  80. {0, NULL},
  81. };
  82. ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
  83. Biome *get_or_load_biome(lua_State *L, int index,
  84. BiomeManager *biomemgr);
  85. Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
  86. size_t get_biome_list(lua_State *L, int index,
  87. BiomeManager *biomemgr, std::unordered_set<u8> *biome_id_list);
  88. Schematic *get_or_load_schematic(lua_State *L, int index,
  89. SchematicManager *schemmgr, StringMap *replace_names);
  90. Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
  91. StringMap *replace_names);
  92. Schematic *load_schematic_from_def(lua_State *L, int index,
  93. INodeDefManager *ndef, StringMap *replace_names);
  94. bool read_schematic_def(lua_State *L, int index,
  95. Schematic *schem, std::vector<std::string> *names);
  96. bool read_deco_simple(lua_State *L, DecoSimple *deco);
  97. bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
  98. ///////////////////////////////////////////////////////////////////////////////
  99. ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
  100. {
  101. if (index < 0)
  102. index = lua_gettop(L) + 1 + index;
  103. // If a number, assume this is a handle to an object def
  104. if (lua_isnumber(L, index))
  105. return objmgr->get(lua_tointeger(L, index));
  106. // If a string, assume a name is given instead
  107. if (lua_isstring(L, index))
  108. return objmgr->getByName(lua_tostring(L, index));
  109. return NULL;
  110. }
  111. ///////////////////////////////////////////////////////////////////////////////
  112. Schematic *get_or_load_schematic(lua_State *L, int index,
  113. SchematicManager *schemmgr, StringMap *replace_names)
  114. {
  115. if (index < 0)
  116. index = lua_gettop(L) + 1 + index;
  117. Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
  118. if (schem)
  119. return schem;
  120. schem = load_schematic(L, index, schemmgr->getNodeDef(),
  121. replace_names);
  122. if (!schem)
  123. return NULL;
  124. if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
  125. delete schem;
  126. return NULL;
  127. }
  128. return schem;
  129. }
  130. Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
  131. StringMap *replace_names)
  132. {
  133. if (index < 0)
  134. index = lua_gettop(L) + 1 + index;
  135. Schematic *schem = NULL;
  136. if (lua_istable(L, index)) {
  137. schem = load_schematic_from_def(L, index, ndef,
  138. replace_names);
  139. if (!schem) {
  140. delete schem;
  141. return NULL;
  142. }
  143. } else if (lua_isnumber(L, index)) {
  144. return NULL;
  145. } else if (lua_isstring(L, index)) {
  146. schem = SchematicManager::create(SCHEMATIC_NORMAL);
  147. std::string filepath = lua_tostring(L, index);
  148. if (!fs::IsPathAbsolute(filepath))
  149. filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
  150. if (!schem->loadSchematicFromFile(filepath, ndef,
  151. replace_names)) {
  152. delete schem;
  153. return NULL;
  154. }
  155. }
  156. return schem;
  157. }
  158. Schematic *load_schematic_from_def(lua_State *L, int index,
  159. INodeDefManager *ndef, StringMap *replace_names)
  160. {
  161. Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
  162. if (!read_schematic_def(L, index, schem, &schem->m_nodenames)) {
  163. delete schem;
  164. return NULL;
  165. }
  166. size_t num_nodes = schem->m_nodenames.size();
  167. schem->m_nnlistsizes.push_back(num_nodes);
  168. if (replace_names) {
  169. for (size_t i = 0; i != num_nodes; i++) {
  170. StringMap::iterator it = replace_names->find(schem->m_nodenames[i]);
  171. if (it != replace_names->end())
  172. schem->m_nodenames[i] = it->second;
  173. }
  174. }
  175. if (ndef)
  176. ndef->pendNodeResolve(schem);
  177. return schem;
  178. }
  179. bool read_schematic_def(lua_State *L, int index,
  180. Schematic *schem, std::vector<std::string> *names)
  181. {
  182. if (!lua_istable(L, index))
  183. return false;
  184. //// Get schematic size
  185. lua_getfield(L, index, "size");
  186. v3s16 size = check_v3s16(L, -1);
  187. lua_pop(L, 1);
  188. schem->size = size;
  189. //// Get schematic data
  190. lua_getfield(L, index, "data");
  191. luaL_checktype(L, -1, LUA_TTABLE);
  192. u32 numnodes = size.X * size.Y * size.Z;
  193. schem->schemdata = new MapNode[numnodes];
  194. size_t names_base = names->size();
  195. std::unordered_map<std::string, content_t> name_id_map;
  196. u32 i = 0;
  197. for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) {
  198. if (i >= numnodes)
  199. continue;
  200. //// Read name
  201. std::string name;
  202. if (!getstringfield(L, -1, "name", name))
  203. throw LuaError("Schematic data definition with missing name field");
  204. //// Read param1/prob
  205. u8 param1;
  206. if (!getintfield(L, -1, "param1", param1) &&
  207. !getintfield(L, -1, "prob", param1))
  208. param1 = MTSCHEM_PROB_ALWAYS_OLD;
  209. //// Read param2
  210. u8 param2 = getintfield_default(L, -1, "param2", 0);
  211. //// Find or add new nodename-to-ID mapping
  212. std::unordered_map<std::string, content_t>::iterator it = name_id_map.find(name);
  213. content_t name_index;
  214. if (it != name_id_map.end()) {
  215. name_index = it->second;
  216. } else {
  217. name_index = names->size() - names_base;
  218. name_id_map[name] = name_index;
  219. names->push_back(name);
  220. }
  221. //// Perform probability/force_place fixup on param1
  222. param1 >>= 1;
  223. if (getboolfield_default(L, -1, "force_place", false))
  224. param1 |= MTSCHEM_FORCE_PLACE;
  225. //// Actually set the node in the schematic
  226. schem->schemdata[i] = MapNode(name_index, param1, param2);
  227. }
  228. if (i != numnodes) {
  229. errorstream << "read_schematic_def: incorrect number of "
  230. "nodes provided in raw schematic data (got " << i <<
  231. ", expected " << numnodes << ")." << std::endl;
  232. return false;
  233. }
  234. //// Get Y-slice probability values (if present)
  235. schem->slice_probs = new u8[size.Y];
  236. for (i = 0; i != (u32) size.Y; i++)
  237. schem->slice_probs[i] = MTSCHEM_PROB_ALWAYS;
  238. lua_getfield(L, index, "yslice_prob");
  239. if (lua_istable(L, -1)) {
  240. for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
  241. u16 ypos;
  242. if (!getintfield(L, -1, "ypos", ypos) || (ypos >= size.Y) ||
  243. !getintfield(L, -1, "prob", schem->slice_probs[ypos]))
  244. continue;
  245. schem->slice_probs[ypos] >>= 1;
  246. }
  247. }
  248. return true;
  249. }
  250. void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
  251. {
  252. if (index < 0)
  253. index = lua_gettop(L) + 1 + index;
  254. lua_pushnil(L);
  255. while (lua_next(L, index)) {
  256. std::string replace_from;
  257. std::string replace_to;
  258. if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
  259. lua_rawgeti(L, -1, 1);
  260. if (!lua_isstring(L, -1))
  261. throw LuaError("schematics: replace_from field is not a string");
  262. replace_from = lua_tostring(L, -1);
  263. lua_pop(L, 1);
  264. lua_rawgeti(L, -1, 2);
  265. if (!lua_isstring(L, -1))
  266. throw LuaError("schematics: replace_to field is not a string");
  267. replace_to = lua_tostring(L, -1);
  268. lua_pop(L, 1);
  269. } else { // New {x = "y", ...} format
  270. if (!lua_isstring(L, -2))
  271. throw LuaError("schematics: replace_from field is not a string");
  272. replace_from = lua_tostring(L, -2);
  273. if (!lua_isstring(L, -1))
  274. throw LuaError("schematics: replace_to field is not a string");
  275. replace_to = lua_tostring(L, -1);
  276. }
  277. replace_names->insert(std::make_pair(replace_from, replace_to));
  278. lua_pop(L, 1);
  279. }
  280. }
  281. ///////////////////////////////////////////////////////////////////////////////
  282. Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
  283. {
  284. if (index < 0)
  285. index = lua_gettop(L) + 1 + index;
  286. Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
  287. if (biome)
  288. return biome;
  289. biome = read_biome_def(L, index, biomemgr->getNodeDef());
  290. if (!biome)
  291. return NULL;
  292. if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
  293. delete biome;
  294. return NULL;
  295. }
  296. return biome;
  297. }
  298. Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
  299. {
  300. if (!lua_istable(L, index))
  301. return NULL;
  302. BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
  303. ModApiMapgen::es_BiomeTerrainType, BIOMETYPE_NORMAL);
  304. Biome *b = BiomeManager::create(biometype);
  305. b->name = getstringfield_default(L, index, "name", "");
  306. b->depth_top = getintfield_default(L, index, "depth_top", 0);
  307. b->depth_filler = getintfield_default(L, index, "depth_filler", -31000);
  308. b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
  309. b->depth_riverbed = getintfield_default(L, index, "depth_riverbed", 0);
  310. b->y_min = getintfield_default(L, index, "y_min", -31000);
  311. b->y_max = getintfield_default(L, index, "y_max", 31000);
  312. b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
  313. b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
  314. b->vertical_blend = getintfield_default(L, index, "vertical_blend", 0);
  315. b->flags = 0; //reserved
  316. std::vector<std::string> &nn = b->m_nodenames;
  317. nn.push_back(getstringfield_default(L, index, "node_top", ""));
  318. nn.push_back(getstringfield_default(L, index, "node_filler", ""));
  319. nn.push_back(getstringfield_default(L, index, "node_stone", ""));
  320. nn.push_back(getstringfield_default(L, index, "node_water_top", ""));
  321. nn.push_back(getstringfield_default(L, index, "node_water", ""));
  322. nn.push_back(getstringfield_default(L, index, "node_river_water", ""));
  323. nn.push_back(getstringfield_default(L, index, "node_riverbed", ""));
  324. nn.push_back(getstringfield_default(L, index, "node_dust", ""));
  325. ndef->pendNodeResolve(b);
  326. return b;
  327. }
  328. size_t get_biome_list(lua_State *L, int index,
  329. BiomeManager *biomemgr, std::unordered_set<u8> *biome_id_list)
  330. {
  331. if (index < 0)
  332. index = lua_gettop(L) + 1 + index;
  333. if (lua_isnil(L, index))
  334. return 0;
  335. bool is_single = true;
  336. if (lua_istable(L, index)) {
  337. lua_getfield(L, index, "name");
  338. is_single = !lua_isnil(L, -1);
  339. lua_pop(L, 1);
  340. }
  341. if (is_single) {
  342. Biome *biome = get_or_load_biome(L, index, biomemgr);
  343. if (!biome) {
  344. infostream << "get_biome_list: failed to get biome '"
  345. << (lua_isstring(L, index) ? lua_tostring(L, index) : "")
  346. << "'." << std::endl;
  347. return 1;
  348. }
  349. biome_id_list->insert(biome->index);
  350. return 0;
  351. }
  352. // returns number of failed resolutions
  353. size_t fail_count = 0;
  354. size_t count = 0;
  355. for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
  356. count++;
  357. Biome *biome = get_or_load_biome(L, -1, biomemgr);
  358. if (!biome) {
  359. fail_count++;
  360. infostream << "get_biome_list: failed to get biome '"
  361. << (lua_isstring(L, -1) ? lua_tostring(L, -1) : "")
  362. << "'" << std::endl;
  363. continue;
  364. }
  365. biome_id_list->insert(biome->index);
  366. }
  367. return fail_count;
  368. }
  369. ///////////////////////////////////////////////////////////////////////////////
  370. // get_biome_id(biomename)
  371. // returns the biome id as used in biomemap and returned by 'get_biome_data()'
  372. int ModApiMapgen::l_get_biome_id(lua_State *L)
  373. {
  374. NO_MAP_LOCK_REQUIRED;
  375. const char *biome_str = lua_tostring(L, 1);
  376. if (!biome_str)
  377. return 0;
  378. BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
  379. if (!bmgr)
  380. return 0;
  381. Biome *biome = (Biome *)bmgr->getByName(biome_str);
  382. if (!biome || biome->index == OBJDEF_INVALID_INDEX)
  383. return 0;
  384. lua_pushinteger(L, biome->index);
  385. return 1;
  386. }
  387. // get_biome_name(biome_id)
  388. // returns the biome name string
  389. int ModApiMapgen::l_get_biome_name(lua_State *L)
  390. {
  391. NO_MAP_LOCK_REQUIRED;
  392. int biome_id = luaL_checkinteger(L, 1);
  393. BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
  394. if (!bmgr)
  395. return 0;
  396. Biome *b = (Biome *)bmgr->getRaw(biome_id);
  397. lua_pushstring(L, b->name.c_str());
  398. return 1;
  399. }
  400. // get_heat(pos)
  401. // returns the heat at the position
  402. int ModApiMapgen::l_get_heat(lua_State *L)
  403. {
  404. NO_MAP_LOCK_REQUIRED;
  405. v3s16 pos = read_v3s16(L, 1);
  406. NoiseParams np_heat;
  407. NoiseParams np_heat_blend;
  408. MapSettingsManager *settingsmgr =
  409. getServer(L)->getEmergeManager()->map_settings_mgr;
  410. if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat",
  411. &np_heat) ||
  412. !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend",
  413. &np_heat_blend))
  414. return 0;
  415. std::string value;
  416. if (!settingsmgr->getMapSetting("seed", &value))
  417. return 0;
  418. std::istringstream ss(value);
  419. u64 seed;
  420. ss >> seed;
  421. BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
  422. if (!bmgr)
  423. return 0;
  424. float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed);
  425. if (!heat)
  426. return 0;
  427. lua_pushnumber(L, heat);
  428. return 1;
  429. }
  430. // get_humidity(pos)
  431. // returns the humidity at the position
  432. int ModApiMapgen::l_get_humidity(lua_State *L)
  433. {
  434. NO_MAP_LOCK_REQUIRED;
  435. v3s16 pos = read_v3s16(L, 1);
  436. NoiseParams np_humidity;
  437. NoiseParams np_humidity_blend;
  438. MapSettingsManager *settingsmgr =
  439. getServer(L)->getEmergeManager()->map_settings_mgr;
  440. if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity",
  441. &np_humidity) ||
  442. !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend",
  443. &np_humidity_blend))
  444. return 0;
  445. std::string value;
  446. if (!settingsmgr->getMapSetting("seed", &value))
  447. return 0;
  448. std::istringstream ss(value);
  449. u64 seed;
  450. ss >> seed;
  451. BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
  452. if (!bmgr)
  453. return 0;
  454. float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity,
  455. np_humidity_blend, seed);
  456. if (!humidity)
  457. return 0;
  458. lua_pushnumber(L, humidity);
  459. return 1;
  460. }
  461. // get_biome_data(pos)
  462. // returns a table containing the biome id, heat and humidity at the position
  463. int ModApiMapgen::l_get_biome_data(lua_State *L)
  464. {
  465. NO_MAP_LOCK_REQUIRED;
  466. v3s16 pos = read_v3s16(L, 1);
  467. NoiseParams np_heat;
  468. NoiseParams np_heat_blend;
  469. NoiseParams np_humidity;
  470. NoiseParams np_humidity_blend;
  471. MapSettingsManager *settingsmgr =
  472. getServer(L)->getEmergeManager()->map_settings_mgr;
  473. if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat",
  474. &np_heat) ||
  475. !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend",
  476. &np_heat_blend) ||
  477. !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity",
  478. &np_humidity) ||
  479. !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend",
  480. &np_humidity_blend))
  481. return 0;
  482. std::string value;
  483. if (!settingsmgr->getMapSetting("seed", &value))
  484. return 0;
  485. std::istringstream ss(value);
  486. u64 seed;
  487. ss >> seed;
  488. BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
  489. if (!bmgr)
  490. return 0;
  491. float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed);
  492. if (!heat)
  493. return 0;
  494. float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity,
  495. np_humidity_blend, seed);
  496. if (!humidity)
  497. return 0;
  498. Biome *biome = (Biome *)bmgr->getBiomeFromNoiseOriginal(heat, humidity, pos.Y);
  499. if (!biome || biome->index == OBJDEF_INVALID_INDEX)
  500. return 0;
  501. lua_newtable(L);
  502. lua_pushinteger(L, biome->index);
  503. lua_setfield(L, -2, "biome");
  504. lua_pushnumber(L, heat);
  505. lua_setfield(L, -2, "heat");
  506. lua_pushnumber(L, humidity);
  507. lua_setfield(L, -2, "humidity");
  508. return 1;
  509. }
  510. // get_mapgen_object(objectname)
  511. // returns the requested object used during map generation
  512. int ModApiMapgen::l_get_mapgen_object(lua_State *L)
  513. {
  514. NO_MAP_LOCK_REQUIRED;
  515. const char *mgobjstr = lua_tostring(L, 1);
  516. int mgobjint;
  517. if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
  518. return 0;
  519. enum MapgenObject mgobj = (MapgenObject)mgobjint;
  520. EmergeManager *emerge = getServer(L)->getEmergeManager();
  521. Mapgen *mg = emerge->getCurrentMapgen();
  522. if (!mg)
  523. throw LuaError("Must only be called in a mapgen thread!");
  524. size_t maplen = mg->csize.X * mg->csize.Z;
  525. switch (mgobj) {
  526. case MGOBJ_VMANIP: {
  527. MMVManip *vm = mg->vm;
  528. // VoxelManip object
  529. LuaVoxelManip *o = new LuaVoxelManip(vm, true);
  530. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  531. luaL_getmetatable(L, "VoxelManip");
  532. lua_setmetatable(L, -2);
  533. // emerged min pos
  534. push_v3s16(L, vm->m_area.MinEdge);
  535. // emerged max pos
  536. push_v3s16(L, vm->m_area.MaxEdge);
  537. return 3;
  538. }
  539. case MGOBJ_HEIGHTMAP: {
  540. if (!mg->heightmap)
  541. return 0;
  542. lua_newtable(L);
  543. for (size_t i = 0; i != maplen; i++) {
  544. lua_pushinteger(L, mg->heightmap[i]);
  545. lua_rawseti(L, -2, i + 1);
  546. }
  547. return 1;
  548. }
  549. case MGOBJ_BIOMEMAP: {
  550. if (!mg->biomegen)
  551. return 0;
  552. lua_newtable(L);
  553. for (size_t i = 0; i != maplen; i++) {
  554. lua_pushinteger(L, mg->biomegen->biomemap[i]);
  555. lua_rawseti(L, -2, i + 1);
  556. }
  557. return 1;
  558. }
  559. case MGOBJ_HEATMAP: {
  560. if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL)
  561. return 0;
  562. BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
  563. lua_newtable(L);
  564. for (size_t i = 0; i != maplen; i++) {
  565. lua_pushnumber(L, bg->heatmap[i]);
  566. lua_rawseti(L, -2, i + 1);
  567. }
  568. return 1;
  569. }
  570. case MGOBJ_HUMIDMAP: {
  571. if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL)
  572. return 0;
  573. BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
  574. lua_newtable(L);
  575. for (size_t i = 0; i != maplen; i++) {
  576. lua_pushnumber(L, bg->humidmap[i]);
  577. lua_rawseti(L, -2, i + 1);
  578. }
  579. return 1;
  580. }
  581. case MGOBJ_GENNOTIFY: {
  582. std::map<std::string, std::vector<v3s16> >event_map;
  583. std::map<std::string, std::vector<v3s16> >::iterator it;
  584. mg->gennotify.getEvents(event_map);
  585. lua_newtable(L);
  586. for (it = event_map.begin(); it != event_map.end(); ++it) {
  587. lua_newtable(L);
  588. for (size_t j = 0; j != it->second.size(); j++) {
  589. push_v3s16(L, it->second[j]);
  590. lua_rawseti(L, -2, j + 1);
  591. }
  592. lua_setfield(L, -2, it->first.c_str());
  593. }
  594. return 1;
  595. }
  596. }
  597. return 0;
  598. }
  599. // get_spawn_level(x = num, z = num)
  600. int ModApiMapgen::l_get_spawn_level(lua_State *L)
  601. {
  602. NO_MAP_LOCK_REQUIRED;
  603. s16 x = luaL_checkinteger(L, 1);
  604. s16 z = luaL_checkinteger(L, 2);
  605. EmergeManager *emerge = getServer(L)->getEmergeManager();
  606. int spawn_level = emerge->getSpawnLevelAtPoint(v2s16(x, z));
  607. // Unsuitable spawn point
  608. if (spawn_level == MAX_MAP_GENERATION_LIMIT)
  609. return 0;
  610. // 'findSpawnPos()' in server.cpp adds at least 1
  611. lua_pushinteger(L, spawn_level + 1);
  612. return 1;
  613. }
  614. int ModApiMapgen::l_get_mapgen_params(lua_State *L)
  615. {
  616. NO_MAP_LOCK_REQUIRED;
  617. log_deprecated(L, "get_mapgen_params is deprecated; "
  618. "use get_mapgen_setting instead");
  619. std::string value;
  620. MapSettingsManager *settingsmgr =
  621. getServer(L)->getEmergeManager()->map_settings_mgr;
  622. lua_newtable(L);
  623. settingsmgr->getMapSetting("mg_name", &value);
  624. lua_pushstring(L, value.c_str());
  625. lua_setfield(L, -2, "mgname");
  626. settingsmgr->getMapSetting("seed", &value);
  627. std::istringstream ss(value);
  628. u64 seed;
  629. ss >> seed;
  630. lua_pushinteger(L, seed);
  631. lua_setfield(L, -2, "seed");
  632. settingsmgr->getMapSetting("water_level", &value);
  633. lua_pushinteger(L, stoi(value, -32768, 32767));
  634. lua_setfield(L, -2, "water_level");
  635. settingsmgr->getMapSetting("chunksize", &value);
  636. lua_pushinteger(L, stoi(value, -32768, 32767));
  637. lua_setfield(L, -2, "chunksize");
  638. settingsmgr->getMapSetting("mg_flags", &value);
  639. lua_pushstring(L, value.c_str());
  640. lua_setfield(L, -2, "flags");
  641. return 1;
  642. }
  643. // set_mapgen_params(params)
  644. // set mapgen parameters
  645. int ModApiMapgen::l_set_mapgen_params(lua_State *L)
  646. {
  647. NO_MAP_LOCK_REQUIRED;
  648. log_deprecated(L, "set_mapgen_params is deprecated; "
  649. "use set_mapgen_setting instead");
  650. if (!lua_istable(L, 1))
  651. return 0;
  652. MapSettingsManager *settingsmgr =
  653. getServer(L)->getEmergeManager()->map_settings_mgr;
  654. lua_getfield(L, 1, "mgname");
  655. if (lua_isstring(L, -1))
  656. settingsmgr->setMapSetting("mg_name", readParam<std::string>(L, -1), true);
  657. lua_getfield(L, 1, "seed");
  658. if (lua_isnumber(L, -1))
  659. settingsmgr->setMapSetting("seed", readParam<std::string>(L, -1), true);
  660. lua_getfield(L, 1, "water_level");
  661. if (lua_isnumber(L, -1))
  662. settingsmgr->setMapSetting("water_level", readParam<std::string>(L, -1), true);
  663. lua_getfield(L, 1, "chunksize");
  664. if (lua_isnumber(L, -1))
  665. settingsmgr->setMapSetting("chunksize", readParam<std::string>(L, -1), true);
  666. warn_if_field_exists(L, 1, "flagmask",
  667. "Deprecated: flags field now includes unset flags.");
  668. lua_getfield(L, 1, "flags");
  669. if (lua_isstring(L, -1))
  670. settingsmgr->setMapSetting("mg_flags", readParam<std::string>(L, -1), true);
  671. return 0;
  672. }
  673. // get_mapgen_setting(name)
  674. int ModApiMapgen::l_get_mapgen_setting(lua_State *L)
  675. {
  676. NO_MAP_LOCK_REQUIRED;
  677. std::string value;
  678. MapSettingsManager *settingsmgr =
  679. getServer(L)->getEmergeManager()->map_settings_mgr;
  680. const char *name = luaL_checkstring(L, 1);
  681. if (!settingsmgr->getMapSetting(name, &value))
  682. return 0;
  683. lua_pushstring(L, value.c_str());
  684. return 1;
  685. }
  686. // get_mapgen_setting_noiseparams(name)
  687. int ModApiMapgen::l_get_mapgen_setting_noiseparams(lua_State *L)
  688. {
  689. NO_MAP_LOCK_REQUIRED;
  690. NoiseParams np;
  691. MapSettingsManager *settingsmgr =
  692. getServer(L)->getEmergeManager()->map_settings_mgr;
  693. const char *name = luaL_checkstring(L, 1);
  694. if (!settingsmgr->getMapSettingNoiseParams(name, &np))
  695. return 0;
  696. push_noiseparams(L, &np);
  697. return 1;
  698. }
  699. // set_mapgen_setting(name, value, override_meta)
  700. // set mapgen config values
  701. int ModApiMapgen::l_set_mapgen_setting(lua_State *L)
  702. {
  703. NO_MAP_LOCK_REQUIRED;
  704. MapSettingsManager *settingsmgr =
  705. getServer(L)->getEmergeManager()->map_settings_mgr;
  706. const char *name = luaL_checkstring(L, 1);
  707. const char *value = luaL_checkstring(L, 2);
  708. bool override_meta = readParam<bool>(L, 3, false);
  709. if (!settingsmgr->setMapSetting(name, value, override_meta)) {
  710. errorstream << "set_mapgen_setting: cannot set '"
  711. << name << "' after initialization" << std::endl;
  712. }
  713. return 0;
  714. }
  715. // set_mapgen_setting_noiseparams(name, noiseparams, set_default)
  716. // set mapgen config values for noise parameters
  717. int ModApiMapgen::l_set_mapgen_setting_noiseparams(lua_State *L)
  718. {
  719. NO_MAP_LOCK_REQUIRED;
  720. MapSettingsManager *settingsmgr =
  721. getServer(L)->getEmergeManager()->map_settings_mgr;
  722. const char *name = luaL_checkstring(L, 1);
  723. NoiseParams np;
  724. if (!read_noiseparams(L, 2, &np)) {
  725. errorstream << "set_mapgen_setting_noiseparams: cannot set '" << name
  726. << "'; invalid noiseparams table" << std::endl;
  727. return 0;
  728. }
  729. bool override_meta = readParam<bool>(L, 3, false);
  730. if (!settingsmgr->setMapSettingNoiseParams(name, &np, override_meta)) {
  731. errorstream << "set_mapgen_setting_noiseparams: cannot set '"
  732. << name << "' after initialization" << std::endl;
  733. }
  734. return 0;
  735. }
  736. // set_noiseparams(name, noiseparams, set_default)
  737. // set global config values for noise parameters
  738. int ModApiMapgen::l_set_noiseparams(lua_State *L)
  739. {
  740. NO_MAP_LOCK_REQUIRED;
  741. const char *name = luaL_checkstring(L, 1);
  742. NoiseParams np;
  743. if (!read_noiseparams(L, 2, &np)) {
  744. errorstream << "set_noiseparams: cannot set '" << name
  745. << "'; invalid noiseparams table" << std::endl;
  746. return 0;
  747. }
  748. bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true;
  749. g_settings->setNoiseParams(name, np, set_default);
  750. return 0;
  751. }
  752. // get_noiseparams(name)
  753. int ModApiMapgen::l_get_noiseparams(lua_State *L)
  754. {
  755. NO_MAP_LOCK_REQUIRED;
  756. std::string name = luaL_checkstring(L, 1);
  757. NoiseParams np;
  758. if (!g_settings->getNoiseParams(name, np))
  759. return 0;
  760. push_noiseparams(L, &np);
  761. return 1;
  762. }
  763. // set_gen_notify(flags, {deco_id_table})
  764. int ModApiMapgen::l_set_gen_notify(lua_State *L)
  765. {
  766. NO_MAP_LOCK_REQUIRED;
  767. u32 flags = 0, flagmask = 0;
  768. EmergeManager *emerge = getServer(L)->getEmergeManager();
  769. if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
  770. emerge->gen_notify_on &= ~flagmask;
  771. emerge->gen_notify_on |= flags;
  772. }
  773. if (lua_istable(L, 2)) {
  774. lua_pushnil(L);
  775. while (lua_next(L, 2)) {
  776. if (lua_isnumber(L, -1))
  777. emerge->gen_notify_on_deco_ids.insert((u32)lua_tonumber(L, -1));
  778. lua_pop(L, 1);
  779. }
  780. }
  781. return 0;
  782. }
  783. // get_gen_notify()
  784. int ModApiMapgen::l_get_gen_notify(lua_State *L)
  785. {
  786. NO_MAP_LOCK_REQUIRED;
  787. EmergeManager *emerge = getServer(L)->getEmergeManager();
  788. push_flags_string(L, flagdesc_gennotify, emerge->gen_notify_on,
  789. emerge->gen_notify_on);
  790. lua_newtable(L);
  791. int i = 1;
  792. for (std::set<u32>::iterator it = emerge->gen_notify_on_deco_ids.begin();
  793. it != emerge->gen_notify_on_deco_ids.end(); ++it) {
  794. lua_pushnumber(L, *it);
  795. lua_rawseti(L, -2, i);
  796. i++;
  797. }
  798. return 2;
  799. }
  800. // get_decoration_id(decoration_name)
  801. // returns the decoration ID as used in gennotify
  802. int ModApiMapgen::l_get_decoration_id(lua_State *L)
  803. {
  804. NO_MAP_LOCK_REQUIRED;
  805. const char *deco_str = luaL_checkstring(L, 1);
  806. if (!deco_str)
  807. return 0;
  808. DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
  809. if (!dmgr)
  810. return 0;
  811. Decoration *deco = (Decoration *)dmgr->getByName(deco_str);
  812. if (!deco)
  813. return 0;
  814. lua_pushinteger(L, deco->index);
  815. return 1;
  816. }
  817. // register_biome({lots of stuff})
  818. int ModApiMapgen::l_register_biome(lua_State *L)
  819. {
  820. NO_MAP_LOCK_REQUIRED;
  821. int index = 1;
  822. luaL_checktype(L, index, LUA_TTABLE);
  823. INodeDefManager *ndef = getServer(L)->getNodeDefManager();
  824. BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
  825. Biome *biome = read_biome_def(L, index, ndef);
  826. if (!biome)
  827. return 0;
  828. ObjDefHandle handle = bmgr->add(biome);
  829. if (handle == OBJDEF_INVALID_HANDLE) {
  830. delete biome;
  831. return 0;
  832. }
  833. lua_pushinteger(L, handle);
  834. return 1;
  835. }
  836. // register_decoration({lots of stuff})
  837. int ModApiMapgen::l_register_decoration(lua_State *L)
  838. {
  839. NO_MAP_LOCK_REQUIRED;
  840. int index = 1;
  841. luaL_checktype(L, index, LUA_TTABLE);
  842. INodeDefManager *ndef = getServer(L)->getNodeDefManager();
  843. DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
  844. BiomeManager *biomemgr = getServer(L)->getEmergeManager()->biomemgr;
  845. SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
  846. enum DecorationType decotype = (DecorationType)getenumfield(L, index,
  847. "deco_type", es_DecorationType, -1);
  848. Decoration *deco = decomgr->create(decotype);
  849. if (!deco) {
  850. errorstream << "register_decoration: decoration placement type "
  851. << decotype << " not implemented" << std::endl;
  852. return 0;
  853. }
  854. deco->name = getstringfield_default(L, index, "name", "");
  855. deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
  856. deco->y_min = getintfield_default(L, index, "y_min", -31000);
  857. deco->y_max = getintfield_default(L, index, "y_max", 31000);
  858. deco->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
  859. deco->place_offset_y = getintfield_default(L, index, "place_offset_y", 0);
  860. deco->sidelen = getintfield_default(L, index, "sidelen", 8);
  861. if (deco->sidelen <= 0) {
  862. errorstream << "register_decoration: sidelen must be "
  863. "greater than 0" << std::endl;
  864. delete deco;
  865. return 0;
  866. }
  867. //// Get node name(s) to place decoration on
  868. size_t nread = getstringlistfield(L, index, "place_on", &deco->m_nodenames);
  869. deco->m_nnlistsizes.push_back(nread);
  870. //// Get decoration flags
  871. getflagsfield(L, index, "flags", flagdesc_deco, &deco->flags, NULL);
  872. //// Get NoiseParams to define how decoration is placed
  873. lua_getfield(L, index, "noise_params");
  874. if (read_noiseparams(L, -1, &deco->np))
  875. deco->flags |= DECO_USE_NOISE;
  876. lua_pop(L, 1);
  877. //// Get biomes associated with this decoration (if any)
  878. lua_getfield(L, index, "biomes");
  879. if (get_biome_list(L, -1, biomemgr, &deco->biomes))
  880. infostream << "register_decoration: couldn't get all biomes " << std::endl;
  881. lua_pop(L, 1);
  882. //// Get node name(s) to 'spawn by'
  883. size_t nnames = getstringlistfield(L, index, "spawn_by", &deco->m_nodenames);
  884. deco->m_nnlistsizes.push_back(nnames);
  885. if (nnames == 0 && deco->nspawnby != -1) {
  886. errorstream << "register_decoration: no spawn_by nodes defined,"
  887. " but num_spawn_by specified" << std::endl;
  888. }
  889. //// Handle decoration type-specific parameters
  890. bool success = false;
  891. switch (decotype) {
  892. case DECO_SIMPLE:
  893. success = read_deco_simple(L, (DecoSimple *)deco);
  894. break;
  895. case DECO_SCHEMATIC:
  896. success = read_deco_schematic(L, schemmgr, (DecoSchematic *)deco);
  897. break;
  898. case DECO_LSYSTEM:
  899. break;
  900. }
  901. if (!success) {
  902. delete deco;
  903. return 0;
  904. }
  905. ndef->pendNodeResolve(deco);
  906. ObjDefHandle handle = decomgr->add(deco);
  907. if (handle == OBJDEF_INVALID_HANDLE) {
  908. delete deco;
  909. return 0;
  910. }
  911. lua_pushinteger(L, handle);
  912. return 1;
  913. }
  914. bool read_deco_simple(lua_State *L, DecoSimple *deco)
  915. {
  916. int index = 1;
  917. int param2;
  918. int param2_max;
  919. deco->deco_height = getintfield_default(L, index, "height", 1);
  920. deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
  921. if (deco->deco_height <= 0) {
  922. errorstream << "register_decoration: simple decoration height"
  923. " must be greater than 0" << std::endl;
  924. return false;
  925. }
  926. size_t nnames = getstringlistfield(L, index, "decoration", &deco->m_nodenames);
  927. deco->m_nnlistsizes.push_back(nnames);
  928. if (nnames == 0) {
  929. errorstream << "register_decoration: no decoration nodes "
  930. "defined" << std::endl;
  931. return false;
  932. }
  933. param2 = getintfield_default(L, index, "param2", 0);
  934. param2_max = getintfield_default(L, index, "param2_max", 0);
  935. if (param2 < 0 || param2 > 255 || param2_max < 0 || param2_max > 255) {
  936. errorstream << "register_decoration: param2 or param2_max out of bounds (0-255)"
  937. << std::endl;
  938. return false;
  939. }
  940. deco->deco_param2 = (u8)param2;
  941. return true;
  942. }
  943. bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco)
  944. {
  945. int index = 1;
  946. deco->rotation = (Rotation)getenumfield(L, index, "rotation",
  947. ModApiMapgen::es_Rotation, ROTATE_0);
  948. StringMap replace_names;
  949. lua_getfield(L, index, "replacements");
  950. if (lua_istable(L, -1))
  951. read_schematic_replacements(L, -1, &replace_names);
  952. lua_pop(L, 1);
  953. lua_getfield(L, index, "schematic");
  954. Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
  955. lua_pop(L, 1);
  956. deco->schematic = schem;
  957. return schem != NULL;
  958. }
  959. // register_ore({lots of stuff})
  960. int ModApiMapgen::l_register_ore(lua_State *L)
  961. {
  962. NO_MAP_LOCK_REQUIRED;
  963. int index = 1;
  964. luaL_checktype(L, index, LUA_TTABLE);
  965. INodeDefManager *ndef = getServer(L)->getNodeDefManager();
  966. BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
  967. OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr;
  968. enum OreType oretype = (OreType)getenumfield(L, index,
  969. "ore_type", es_OreType, ORE_SCATTER);
  970. Ore *ore = oremgr->create(oretype);
  971. if (!ore) {
  972. errorstream << "register_ore: ore_type " << oretype << " not implemented\n";
  973. return 0;
  974. }
  975. ore->name = getstringfield_default(L, index, "name", "");
  976. ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
  977. ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
  978. ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
  979. ore->clust_size = getintfield_default(L, index, "clust_size", 0);
  980. ore->noise = NULL;
  981. ore->flags = 0;
  982. //// Get noise_threshold
  983. warn_if_field_exists(L, index, "noise_threshhold",
  984. "Deprecated: new name is \"noise_threshold\".");
  985. float nthresh;
  986. if (!getfloatfield(L, index, "noise_threshold", nthresh) &&
  987. !getfloatfield(L, index, "noise_threshhold", nthresh))
  988. nthresh = 0;
  989. ore->nthresh = nthresh;
  990. //// Get y_min/y_max
  991. warn_if_field_exists(L, index, "height_min",
  992. "Deprecated: new name is \"y_min\".");
  993. warn_if_field_exists(L, index, "height_max",
  994. "Deprecated: new name is \"y_max\".");
  995. int ymin, ymax;
  996. if (!getintfield(L, index, "y_min", ymin) &&
  997. !getintfield(L, index, "height_min", ymin))
  998. ymin = -31000;
  999. if (!getintfield(L, index, "y_max", ymax) &&
  1000. !getintfield(L, index, "height_max", ymax))
  1001. ymax = 31000;
  1002. ore->y_min = ymin;
  1003. ore->y_max = ymax;
  1004. if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
  1005. errorstream << "register_ore: clust_scarcity and clust_num_ores"
  1006. "must be greater than 0" << std::endl;
  1007. delete ore;
  1008. return 0;
  1009. }
  1010. //// Get flags
  1011. getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
  1012. //// Get biomes associated with this decoration (if any)
  1013. lua_getfield(L, index, "biomes");
  1014. if (get_biome_list(L, -1, bmgr, &ore->biomes))
  1015. infostream << "register_ore: couldn't get all biomes " << std::endl;
  1016. lua_pop(L, 1);
  1017. //// Get noise parameters if needed
  1018. lua_getfield(L, index, "noise_params");
  1019. if (read_noiseparams(L, -1, &ore->np)) {
  1020. ore->flags |= OREFLAG_USE_NOISE;
  1021. } else if (ore->NEEDS_NOISE) {
  1022. errorstream << "register_ore: specified ore type requires valid "
  1023. "noise parameters" << std::endl;
  1024. delete ore;
  1025. return 0;
  1026. }
  1027. lua_pop(L, 1);
  1028. //// Get type-specific parameters
  1029. switch (oretype) {
  1030. case ORE_SHEET: {
  1031. OreSheet *oresheet = (OreSheet *)ore;
  1032. oresheet->column_height_min = getintfield_default(L, index,
  1033. "column_height_min", 1);
  1034. oresheet->column_height_max = getintfield_default(L, index,
  1035. "column_height_max", ore->clust_size);
  1036. oresheet->column_midpoint_factor = getfloatfield_default(L, index,
  1037. "column_midpoint_factor", 0.5f);
  1038. break;
  1039. }
  1040. case ORE_PUFF: {
  1041. OrePuff *orepuff = (OrePuff *)ore;
  1042. lua_getfield(L, index, "np_puff_top");
  1043. read_noiseparams(L, -1, &orepuff->np_puff_top);
  1044. lua_pop(L, 1);
  1045. lua_getfield(L, index, "np_puff_bottom");
  1046. read_noiseparams(L, -1, &orepuff->np_puff_bottom);
  1047. lua_pop(L, 1);
  1048. break;
  1049. }
  1050. case ORE_VEIN: {
  1051. OreVein *orevein = (OreVein *)ore;
  1052. orevein->random_factor = getfloatfield_default(L, index,
  1053. "random_factor", 1.f);
  1054. break;
  1055. }
  1056. default:
  1057. break;
  1058. }
  1059. ObjDefHandle handle = oremgr->add(ore);
  1060. if (handle == OBJDEF_INVALID_HANDLE) {
  1061. delete ore;
  1062. return 0;
  1063. }
  1064. ore->m_nodenames.push_back(getstringfield_default(L, index, "ore", ""));
  1065. size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames);
  1066. ore->m_nnlistsizes.push_back(nnames);
  1067. ndef->pendNodeResolve(ore);
  1068. lua_pushinteger(L, handle);
  1069. return 1;
  1070. }
  1071. // register_schematic({schematic}, replacements={})
  1072. int ModApiMapgen::l_register_schematic(lua_State *L)
  1073. {
  1074. NO_MAP_LOCK_REQUIRED;
  1075. SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
  1076. StringMap replace_names;
  1077. if (lua_istable(L, 2))
  1078. read_schematic_replacements(L, 2, &replace_names);
  1079. Schematic *schem = load_schematic(L, 1, schemmgr->getNodeDef(),
  1080. &replace_names);
  1081. if (!schem)
  1082. return 0;
  1083. ObjDefHandle handle = schemmgr->add(schem);
  1084. if (handle == OBJDEF_INVALID_HANDLE) {
  1085. delete schem;
  1086. return 0;
  1087. }
  1088. lua_pushinteger(L, handle);
  1089. return 1;
  1090. }
  1091. // clear_registered_biomes()
  1092. int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
  1093. {
  1094. NO_MAP_LOCK_REQUIRED;
  1095. BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
  1096. bmgr->clear();
  1097. return 0;
  1098. }
  1099. // clear_registered_decorations()
  1100. int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
  1101. {
  1102. NO_MAP_LOCK_REQUIRED;
  1103. DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
  1104. dmgr->clear();
  1105. return 0;
  1106. }
  1107. // clear_registered_ores()
  1108. int ModApiMapgen::l_clear_registered_ores(lua_State *L)
  1109. {
  1110. NO_MAP_LOCK_REQUIRED;
  1111. OreManager *omgr = getServer(L)->getEmergeManager()->oremgr;
  1112. omgr->clear();
  1113. return 0;
  1114. }
  1115. // clear_registered_schematics()
  1116. int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
  1117. {
  1118. NO_MAP_LOCK_REQUIRED;
  1119. SchematicManager *smgr = getServer(L)->getEmergeManager()->schemmgr;
  1120. smgr->clear();
  1121. return 0;
  1122. }
  1123. // generate_ores(vm, p1, p2, [ore_id])
  1124. int ModApiMapgen::l_generate_ores(lua_State *L)
  1125. {
  1126. NO_MAP_LOCK_REQUIRED;
  1127. EmergeManager *emerge = getServer(L)->getEmergeManager();
  1128. Mapgen mg;
  1129. mg.seed = emerge->mgparams->seed;
  1130. mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
  1131. mg.ndef = getServer(L)->getNodeDefManager();
  1132. v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
  1133. mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
  1134. v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
  1135. mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
  1136. sortBoxVerticies(pmin, pmax);
  1137. u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
  1138. emerge->oremgr->placeAllOres(&mg, blockseed, pmin, pmax);
  1139. return 0;
  1140. }
  1141. // generate_decorations(vm, p1, p2, [deco_id])
  1142. int ModApiMapgen::l_generate_decorations(lua_State *L)
  1143. {
  1144. NO_MAP_LOCK_REQUIRED;
  1145. EmergeManager *emerge = getServer(L)->getEmergeManager();
  1146. Mapgen mg;
  1147. mg.seed = emerge->mgparams->seed;
  1148. mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
  1149. mg.ndef = getServer(L)->getNodeDefManager();
  1150. v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
  1151. mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
  1152. v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
  1153. mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
  1154. sortBoxVerticies(pmin, pmax);
  1155. u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
  1156. emerge->decomgr->placeAllDecos(&mg, blockseed, pmin, pmax);
  1157. return 0;
  1158. }
  1159. // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
  1160. int ModApiMapgen::l_create_schematic(lua_State *L)
  1161. {
  1162. MAP_LOCK_REQUIRED;
  1163. INodeDefManager *ndef = getServer(L)->getNodeDefManager();
  1164. const char *filename = luaL_checkstring(L, 4);
  1165. CHECK_SECURE_PATH(L, filename, true);
  1166. Map *map = &(getEnv(L)->getMap());
  1167. Schematic schem;
  1168. v3s16 p1 = check_v3s16(L, 1);
  1169. v3s16 p2 = check_v3s16(L, 2);
  1170. sortBoxVerticies(p1, p2);
  1171. std::vector<std::pair<v3s16, u8> > prob_list;
  1172. if (lua_istable(L, 3)) {
  1173. lua_pushnil(L);
  1174. while (lua_next(L, 3)) {
  1175. if (lua_istable(L, -1)) {
  1176. lua_getfield(L, -1, "pos");
  1177. v3s16 pos = check_v3s16(L, -1);
  1178. lua_pop(L, 1);
  1179. u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
  1180. prob_list.push_back(std::make_pair(pos, prob));
  1181. }
  1182. lua_pop(L, 1);
  1183. }
  1184. }
  1185. std::vector<std::pair<s16, u8> > slice_prob_list;
  1186. if (lua_istable(L, 5)) {
  1187. lua_pushnil(L);
  1188. while (lua_next(L, 5)) {
  1189. if (lua_istable(L, -1)) {
  1190. s16 ypos = getintfield_default(L, -1, "ypos", 0);
  1191. u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
  1192. slice_prob_list.push_back(std::make_pair(ypos, prob));
  1193. }
  1194. lua_pop(L, 1);
  1195. }
  1196. }
  1197. if (!schem.getSchematicFromMap(map, p1, p2)) {
  1198. errorstream << "create_schematic: failed to get schematic "
  1199. "from map" << std::endl;
  1200. return 0;
  1201. }
  1202. schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
  1203. schem.saveSchematicToFile(filename, ndef);
  1204. actionstream << "create_schematic: saved schematic file '"
  1205. << filename << "'." << std::endl;
  1206. lua_pushboolean(L, true);
  1207. return 1;
  1208. }
  1209. // place_schematic(p, schematic, rotation,
  1210. // replacements, force_placement, flagstring)
  1211. int ModApiMapgen::l_place_schematic(lua_State *L)
  1212. {
  1213. MAP_LOCK_REQUIRED;
  1214. Map *map = &(getEnv(L)->getMap());
  1215. SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
  1216. //// Read position
  1217. v3s16 p = check_v3s16(L, 1);
  1218. //// Read rotation
  1219. int rot = ROTATE_0;
  1220. std::string enumstr = readParam<std::string>(L, 3, "");
  1221. if (!enumstr.empty())
  1222. string_to_enum(es_Rotation, rot, enumstr);
  1223. //// Read force placement
  1224. bool force_placement = true;
  1225. if (lua_isboolean(L, 5))
  1226. force_placement = readParam<bool>(L, 5);
  1227. //// Read node replacements
  1228. StringMap replace_names;
  1229. if (lua_istable(L, 4))
  1230. read_schematic_replacements(L, 4, &replace_names);
  1231. //// Read schematic
  1232. Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
  1233. if (!schem) {
  1234. errorstream << "place_schematic: failed to get schematic" << std::endl;
  1235. return 0;
  1236. }
  1237. //// Read flags
  1238. u32 flags = 0;
  1239. read_flags(L, 6, flagdesc_deco, &flags, NULL);
  1240. schem->placeOnMap(map, p, flags, (Rotation)rot, force_placement);
  1241. lua_pushboolean(L, true);
  1242. return 1;
  1243. }
  1244. // place_schematic_on_vmanip(vm, p, schematic, rotation,
  1245. // replacements, force_placement, flagstring)
  1246. int ModApiMapgen::l_place_schematic_on_vmanip(lua_State *L)
  1247. {
  1248. NO_MAP_LOCK_REQUIRED;
  1249. SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
  1250. //// Read VoxelManip object
  1251. MMVManip *vm = LuaVoxelManip::checkobject(L, 1)->vm;
  1252. //// Read position
  1253. v3s16 p = check_v3s16(L, 2);
  1254. //// Read rotation
  1255. int rot = ROTATE_0;
  1256. std::string enumstr = readParam<std::string>(L, 4, "");
  1257. if (!enumstr.empty())
  1258. string_to_enum(es_Rotation, rot, std::string(enumstr));
  1259. //// Read force placement
  1260. bool force_placement = true;
  1261. if (lua_isboolean(L, 6))
  1262. force_placement = readParam<bool>(L, 6);
  1263. //// Read node replacements
  1264. StringMap replace_names;
  1265. if (lua_istable(L, 5))
  1266. read_schematic_replacements(L, 5, &replace_names);
  1267. //// Read schematic
  1268. Schematic *schem = get_or_load_schematic(L, 3, schemmgr, &replace_names);
  1269. if (!schem) {
  1270. errorstream << "place_schematic: failed to get schematic" << std::endl;
  1271. return 0;
  1272. }
  1273. //// Read flags
  1274. u32 flags = 0;
  1275. read_flags(L, 7, flagdesc_deco, &flags, NULL);
  1276. bool schematic_did_fit = schem->placeOnVManip(
  1277. vm, p, flags, (Rotation)rot, force_placement);
  1278. lua_pushboolean(L, schematic_did_fit);
  1279. return 1;
  1280. }
  1281. // serialize_schematic(schematic, format, options={...})
  1282. int ModApiMapgen::l_serialize_schematic(lua_State *L)
  1283. {
  1284. NO_MAP_LOCK_REQUIRED;
  1285. SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
  1286. //// Read options
  1287. bool use_comments = getboolfield_default(L, 3, "lua_use_comments", false);
  1288. u32 indent_spaces = getintfield_default(L, 3, "lua_num_indent_spaces", 0);
  1289. //// Get schematic
  1290. bool was_loaded = false;
  1291. Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
  1292. if (!schem) {
  1293. schem = load_schematic(L, 1, NULL, NULL);
  1294. was_loaded = true;
  1295. }
  1296. if (!schem) {
  1297. errorstream << "serialize_schematic: failed to get schematic" << std::endl;
  1298. return 0;
  1299. }
  1300. //// Read format of definition to save as
  1301. int schem_format = SCHEM_FMT_MTS;
  1302. std::string enumstr = readParam<std::string>(L, 2, "");
  1303. if (!enumstr.empty())
  1304. string_to_enum(es_SchematicFormatType, schem_format, enumstr);
  1305. //// Serialize to binary string
  1306. std::ostringstream os(std::ios_base::binary);
  1307. switch (schem_format) {
  1308. case SCHEM_FMT_MTS:
  1309. schem->serializeToMts(&os, schem->m_nodenames);
  1310. break;
  1311. case SCHEM_FMT_LUA:
  1312. schem->serializeToLua(&os, schem->m_nodenames,
  1313. use_comments, indent_spaces);
  1314. break;
  1315. default:
  1316. return 0;
  1317. }
  1318. if (was_loaded)
  1319. delete schem;
  1320. std::string ser = os.str();
  1321. lua_pushlstring(L, ser.c_str(), ser.length());
  1322. return 1;
  1323. }
  1324. // read_schematic(schematic, options={...})
  1325. int ModApiMapgen::l_read_schematic(lua_State *L)
  1326. {
  1327. NO_MAP_LOCK_REQUIRED;
  1328. SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
  1329. //// Read options
  1330. std::string write_yslice = getstringfield_default(L, 2, "write_yslice_prob", "all");
  1331. //// Get schematic
  1332. bool was_loaded = false;
  1333. Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
  1334. if (!schem) {
  1335. schem = load_schematic(L, 1, NULL, NULL);
  1336. was_loaded = true;
  1337. }
  1338. if (!schem) {
  1339. errorstream << "read_schematic: failed to get schematic" << std::endl;
  1340. return 0;
  1341. }
  1342. lua_pop(L, 2);
  1343. //// Create the Lua table
  1344. u32 numnodes = schem->size.X * schem->size.Y * schem->size.Z;
  1345. const std::vector<std::string> &names = schem->m_nodenames;
  1346. lua_createtable(L, 0, (write_yslice == "none") ? 2 : 3);
  1347. // Create the size field
  1348. push_v3s16(L, schem->size);
  1349. lua_setfield(L, 1, "size");
  1350. // Create the yslice_prob field
  1351. if (write_yslice != "none") {
  1352. lua_createtable(L, schem->size.Y, 0);
  1353. for (u16 y = 0; y != schem->size.Y; ++y) {
  1354. u8 probability = schem->slice_probs[y] & MTSCHEM_PROB_MASK;
  1355. if (probability < MTSCHEM_PROB_ALWAYS || write_yslice != "low") {
  1356. lua_createtable(L, 0, 2);
  1357. lua_pushinteger(L, y);
  1358. lua_setfield(L, 3, "ypos");
  1359. lua_pushinteger(L, probability * 2);
  1360. lua_setfield(L, 3, "prob");
  1361. lua_rawseti(L, 2, y + 1);
  1362. }
  1363. }
  1364. lua_setfield(L, 1, "yslice_prob");
  1365. }
  1366. // Create the data field
  1367. lua_createtable(L, numnodes, 0); // data table
  1368. for (u32 i = 0; i < numnodes; ++i) {
  1369. MapNode node = schem->schemdata[i];
  1370. u8 probability = node.param1 & MTSCHEM_PROB_MASK;
  1371. bool force_place = node.param1 & MTSCHEM_FORCE_PLACE;
  1372. lua_createtable(L, 0, force_place ? 4 : 3);
  1373. lua_pushstring(L, names[schem->schemdata[i].getContent()].c_str());
  1374. lua_setfield(L, 3, "name");
  1375. lua_pushinteger(L, probability * 2);
  1376. lua_setfield(L, 3, "prob");
  1377. lua_pushinteger(L, node.param2);
  1378. lua_setfield(L, 3, "param2");
  1379. if (force_place) {
  1380. lua_pushboolean(L, 1);
  1381. lua_setfield(L, 3, "force_place");
  1382. }
  1383. lua_rawseti(L, 2, i + 1);
  1384. }
  1385. lua_setfield(L, 1, "data");
  1386. if (was_loaded)
  1387. delete schem;
  1388. return 1;
  1389. }
  1390. void ModApiMapgen::Initialize(lua_State *L, int top)
  1391. {
  1392. API_FCT(get_biome_id);
  1393. API_FCT(get_biome_name);
  1394. API_FCT(get_heat);
  1395. API_FCT(get_humidity);
  1396. API_FCT(get_biome_data);
  1397. API_FCT(get_mapgen_object);
  1398. API_FCT(get_spawn_level);
  1399. API_FCT(get_mapgen_params);
  1400. API_FCT(set_mapgen_params);
  1401. API_FCT(get_mapgen_setting);
  1402. API_FCT(set_mapgen_setting);
  1403. API_FCT(get_mapgen_setting_noiseparams);
  1404. API_FCT(set_mapgen_setting_noiseparams);
  1405. API_FCT(set_noiseparams);
  1406. API_FCT(get_noiseparams);
  1407. API_FCT(set_gen_notify);
  1408. API_FCT(get_gen_notify);
  1409. API_FCT(get_decoration_id);
  1410. API_FCT(register_biome);
  1411. API_FCT(register_decoration);
  1412. API_FCT(register_ore);
  1413. API_FCT(register_schematic);
  1414. API_FCT(clear_registered_biomes);
  1415. API_FCT(clear_registered_decorations);
  1416. API_FCT(clear_registered_ores);
  1417. API_FCT(clear_registered_schematics);
  1418. API_FCT(generate_ores);
  1419. API_FCT(generate_decorations);
  1420. API_FCT(create_schematic);
  1421. API_FCT(place_schematic);
  1422. API_FCT(place_schematic_on_vmanip);
  1423. API_FCT(serialize_schematic);
  1424. API_FCT(read_schematic);
  1425. }