aboutsummaryrefslogtreecommitdiff
path: root/src/nodedef.h
blob: 4f07565d1ad2073ded8fd09d44f3ec1e115a4ac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef NODEDEF_HEADER
#define NODEDEF_HEADER

#include "irrlichttypes_bloated.h"
#include <string>
#include <iostream>
#include <map>
#include "mapnode.h"
#ifndef SERVER
#include "tile.h"
#endif
#include "itemgroup.h"
#include "sound.h" // SimpleSoundSpec
#include "constants.h" // BS

class IItemDefManager;
class ITextureSource;
class IGameDef;

enum ContentParamType
{
	CPT_NONE,
	CPT_LIGHT,
};

enum ContentParamType2
{
	CPT2_NONE,
	// Need 8-bit param2
	CPT2_FULL,
	// Flowing liquid properties
	CPT2_FLOWINGLIQUID,
	// Direction for chests and furnaces and such
	CPT2_FACEDIR,
	// Direction for signs, torches and such
	CPT2_WALLMOUNTED,
};

enum LiquidType
{
	LIQUID_NONE,
	LIQUID_FLOWING,
	LIQUID_SOURCE
};

enum NodeBoxType
{
	NODEBOX_REGULAR, // Regular block; allows buildable_to
	NODEBOX_FIXED, // Static separately defined box(es)
	NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
};

struct NodeBox
{
	enum NodeBoxType type;
	// NODEBOX_REGULAR (no parameters)
	// NODEBOX_FIXED
	std::vector<aabb3f> fixed;
	// NODEBOX_WALLMOUNTED
	aabb3f wall_top;
	aabb3f wall_bottom;
	aabb3f wall_side; // being at the -X side

	NodeBox()
	{ reset(); }

	void reset();
	void serialize(std::ostream &os) const;
	void deSerialize(std::istream &is);
};

struct MapNode;
class NodeMetadata;

/*
	Stand-alone definition of a TileSpec (basically a server-side TileSpec)
*/
enum TileAnimationType{
	TAT_NONE=0,
	TAT_VERTICAL_FRAMES=1,
};
struct TileDef
{
	std::string name;
	bool backface_culling; // Takes effect only in special cases
	struct{
		enum TileAnimationType type;
		int aspect_w; // width for aspect ratio
		int aspect_h; // height for aspect ratio
		float length; // seconds
	} animation;

	TileDef()
	{
		name = "";
		backface_culling = true;
		animation.type = TAT_NONE;
		animation.aspect_w = 1;
		animation.aspect_h = 1;
		animation.length = 1.0;
	}

	void serialize(std::ostream &os, u16 protocol_version) const;
	void deSerialize(std::istream &is);
};

enum NodeDrawType
{
	NDT_NORMAL, // A basic solid block
	NDT_AIRLIKE, // Nothing is drawn
	NDT_LIQUID, // Do not draw face towards same kind of flowing/source liquid
	NDT_FLOWINGLIQUID, // A very special kind of thing
	NDT_GLASSLIKE, // Glass-like, don't draw faces towards other glass
	NDT_ALLFACES, // Leaves-like, draw all faces no matter what
	NDT_ALLFACES_OPTIONAL, // Fancy -> allfaces, fast -> normal
	NDT_TORCHLIKE,
	NDT_SIGNLIKE,
	NDT_PLANTLIKE,
	NDT_FENCELIKE,
	NDT_RAILLIKE,
	NDT_NODEBOX,
};

#define CF_SPECIAL_COUNT 2

struct ContentFeatures
{
	/*
		Cached stuff
	*/
#ifndef SERVER
	// 0     1     2     3     4     5
	// up    down  right left  back  front 
	TileSpec tiles[6];
	// Special tiles
	// - Currently used for flowing liquids
	TileSpec special_tiles[CF_SPECIAL_COUNT];
	u8 solidness; // Used when choosing which face is drawn
	u8 visual_solidness; // When solidness=0, this tells how it looks like
	bool backface_culling;
#endif

	// Server-side cached callback existence for fast skipping
	bool has_on_construct;
	bool has_on_destruct;
	bool has_after_destruct;

	/*
		Actual data
	*/

	std::string name; // "" = undefined node
	ItemGroupList groups; // Same as in itemdef

	// Visual definition
	enum NodeDrawType drawtype;
	float visual_scale; // Misc. scale parameter
	TileDef tiledef[6];
	TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
	u8 alpha;

	// Post effect color, drawn when the camera is inside the node.
	video::SColor post_effect_color;
	// Type of MapNode::param1
	ContentParamType param_type;
	// Type of MapNode::param2
	ContentParamType2 param_type_2;
	// True for all ground-like things like stone and mud, false for eg. trees
	bool is_ground_content;
	bool light_propagates;
	bool sunlight_propagates;
	// This is used for collision detection.
	// Also for general solidness queries.
	bool walkable;
	// Player can point to these
	bool pointable;
	// Player can dig these
	bool diggable;
	// Player can climb these
	bool climbable;
	// Player can build on these
	bool buildable_to;
	// Player cannot build to these (placement prediction disabled)
	bool rightclickable;
	// Whether the node is non-liquid, source liquid or flowing liquid
	enum LiquidType liquid_type;
	// If the content is liquid, this is the flowing version of the liquid.
	std::string liquid_alternative_flowing;
	// If the content is liquid, this is the source version of the liquid.
	std::string liquid_alternative_source;
	// Viscosity for fluid flow, ranging from 1 to 7, with
	// 1 giving almost instantaneous propagation and 7 being
	// the slowest possible
	u8 liquid_viscosity;
	// Is liquid renewable (new liquid source will be created between 2 existing)
	bool liquid_renewable;
	// Amount of light the node emits
	u8 light_source;
	u32 damage_per_second;
	NodeBox node_box;
	NodeBox selection_box;
	// Compatibility with old maps
	// Set to true if paramtype used to be 'facedir_simple'
	bool legacy_facedir_simple;
	// Set to true if wall_mounted used to be set to true
	bool legacy_wallmounted;

	// Sound properties
	SimpleSoundSpec sound_footstep;
	SimpleSoundSpec sound_dig;
	SimpleSoundSpec sound_dug;

	/*
		Methods
	*/
	
	ContentFeatures();
	~ContentFeatures();
	void reset();
	void serialize(std::ostream &os, u16 protocol_version);
	void deSerialize(std::istream &is);
	void serializeOld(std::ostream &os, u16 protocol_version);
	void deSerializeOld(std::istream &is, int version);

	/*
		Some handy methods
	*/
	bool isLiquid() const{
		return (liquid_type != LIQUID_NONE);
	}
	bool sameLiquid(const ContentFeatures &f) const{
		if(!isLiquid() || !f.isLiquid()) return false;
		return (liquid_alternative_flowing == f.liquid_alternative_flowing);
	}
};

class INodeDefManager
{
public:
	INodeDefManager(){}
	virtual ~INodeDefManager(){}
	// Get node definition
	virtual const ContentFeatures& get(content_t c) const=0;
	virtual const ContentFeatures& get(const MapNode &n) const=0;
	virtual bool getId(const std::string &name, content_t &result) const=0;
	virtual content_t getId(const std::string &name) const=0;
	// Allows "group:name" in addition to regular node names
	virtual void getIds(const std::string &name, std::set<content_t> &result)
			const=0;
	virtual const ContentFeatures& get(const std::string &name) const=0;
	
	virtual void serialize(std::ostream &os, u16 protocol_version)=0;
};

class IWritableNodeDefManager : public INodeDefManager
{
public:
	IWritableNodeDefManager(){}
	virtual ~IWritableNodeDefManager(){}
	virtual IWritableNodeDefManager* clone()=0;
	// Get node definition
	virtual const ContentFeatures& get(content_t c) const=0;
	virtual const ContentFeatures& get(const MapNode &n) const=0;
	virtual bool getId(const std::string &name, content_t &result) const=0;
	virtual content_t getId(const std::string &name) const=0;
	// Allows "group:name" in addition to regular node names
	virtual void getIds(const std::string &name, std::set<content_t> &result)
			const=0;
	// If not found, returns the features of CONTENT_IGNORE
	virtual const ContentFeatures& get(const std::string &name) const=0;

	// Register node definition
	virtual void set(content_t c, const ContentFeatures &def)=0;
	// Register node definition by name (allocate an id)
	// If returns CONTENT_IGNORE, could not allocate id
	virtual content_t set(const std::string &name,
			const ContentFeatures &def)=0;
	// If returns CONTENT_IGNORE, could not allocate id
	virtual content_t allocateDummy(const std::string &name)=0;

	/*
		Update item alias mapping.
		Call after updating item definitions.
	*/
	virtual void updateAliases(IItemDefManager *idef)=0;

	/*
		Update tile textures to latest return values of TextueSource.
		Call after updating the texture atlas of a TextureSource.
	*/
	virtual void updateTextures(ITextureSource *tsrc)=0;

	virtual void serialize(std::ostream &os, u16 protocol_version)=0;
	virtual void deSerialize(std::istream &is)=0;
};

IWritableNodeDefManager* createNodeDefManager();

#endif

6' href='#n1176'>1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
/*
Minetest-c55
Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "mapgen.h"
#include "voxel.h"
#include "content_mapnode.h"
#include "noise.h"
#include "mapblock.h"
#include "map.h"
#include "mineral.h"
//#include "serverobject.h"
#include "content_sao.h"

namespace mapgen
{

/*
	Some helper functions for the map generator
*/

#if 0
static s16 find_ground_level(VoxelManipulator &vmanip, v2s16 p2d)
{
	v3s16 em = vmanip.m_area.getExtent();
	s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
	s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
	u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
	s16 y;
	for(y=y_nodes_max; y>=y_nodes_min; y--)
	{
		MapNode &n = vmanip.m_data[i];
		if(content_walkable(n.d))
			break;

		vmanip.m_area.add_y(em, i, -1);
	}
	if(y >= y_nodes_min)
		return y;
	else
		return y_nodes_min;
}

static s16 find_ground_level_clever(VoxelManipulator &vmanip, v2s16 p2d)
{
	v3s16 em = vmanip.m_area.getExtent();
	s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
	s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
	u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
	s16 y;
	for(y=y_nodes_max; y>=y_nodes_min; y--)
	{
		MapNode &n = vmanip.m_data[i];
		if(content_walkable(n.d)
				&& n.d != CONTENT_TREE
				&& n.d != CONTENT_LEAVES)
			break;

		vmanip.m_area.add_y(em, i, -1);
	}
	if(y >= y_nodes_min)
		return y;
	else
		return y_nodes_min;
}
#endif

static void make_tree(VoxelManipulator &vmanip, v3s16 p0)
{
	MapNode treenode(CONTENT_TREE);
	MapNode leavesnode(CONTENT_LEAVES);

	s16 trunk_h = myrand_range(4, 5);
	v3s16 p1 = p0;
	for(s16 ii=0; ii<trunk_h; ii++)
	{
		if(vmanip.m_area.contains(p1))
			vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
		p1.Y++;
	}

	// p1 is now the last piece of the trunk
	p1.Y -= 1;

	VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
	//SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
	Buffer<u8> leaves_d(leaves_a.getVolume());
	for(s32 i=0; i<leaves_a.getVolume(); i++)
		leaves_d[i] = 0;

	// Force leaves at near the end of the trunk
	{
		s16 d = 1;
		for(s16 z=-d; z<=d; z++)
		for(s16 y=-d; y<=d; y++)
		for(s16 x=-d; x<=d; x++)
		{
			leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
		}
	}

	// Add leaves randomly
	for(u32 iii=0; iii<7; iii++)
	{
		s16 d = 1;

		v3s16 p(
			myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
			myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
			myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
		);

		for(s16 z=0; z<=d; z++)
		for(s16 y=0; y<=d; y++)
		for(s16 x=0; x<=d; x++)
		{
			leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
		}
	}

	// Blit leaves to vmanip
	for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
	for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
	for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
	{
		v3s16 p(x,y,z);
		p += p1;
		if(vmanip.m_area.contains(p) == false)
			continue;
		u32 vi = vmanip.m_area.index(p);
		if(vmanip.m_data[vi].d != CONTENT_AIR
				&& vmanip.m_data[vi].d != CONTENT_IGNORE)
			continue;
		u32 i = leaves_a.index(x,y,z);
		if(leaves_d[i] == 1)
			vmanip.m_data[vi] = leavesnode;
	}
}

void make_papyrus(VoxelManipulator &vmanip, v3s16 p0)
{
	MapNode papyrusnode(CONTENT_PAPYRUS);

	s16 trunk_h = myrand_range(2, 3);
	v3s16 p1 = p0;
	for(s16 ii=0; ii<trunk_h; ii++)
	{
		if(vmanip.m_area.contains(p1))
			vmanip.m_data[vmanip.m_area.index(p1)] = papyrusnode;
		p1.Y++;
	}
}

void make_cactus(VoxelManipulator &vmanip, v3s16 p0)
{
	MapNode cactusnode(CONTENT_CACTUS);

	s16 trunk_h = 3;
	v3s16 p1 = p0;
	for(s16 ii=0; ii<trunk_h; ii++)
	{
		if(vmanip.m_area.contains(p1))
			vmanip.m_data[vmanip.m_area.index(p1)] = cactusnode;
		p1.Y++;
	}
}

#if 0
static void make_randomstone(VoxelManipulator &vmanip, v3s16 p0)
{
	MapNode stonenode(CONTENT_STONE);

	s16 size = myrand_range(3, 6);
	
	VoxelArea stone_a(v3s16(-2,0,-2), v3s16(2,size,2));
	Buffer<u8> stone_d(stone_a.getVolume());
	for(s32 i=0; i<stone_a.getVolume(); i++)
		stone_d[i] = 0;

	// Force stone at bottom to make it usually touch the ground
	{
		for(s16 z=0; z<=0; z++)
		for(s16 y=0; y<=0; y++)
		for(s16 x=0; x<=0; x++)
		{
			stone_d[stone_a.index(v3s16(x,y,z))] = 1;
		}
	}

	// Generate from perlin noise
	for(s16 z=stone_a.MinEdge.Z; z<=stone_a.MaxEdge.Z; z++)
	for(s16 y=stone_a.MinEdge.Y; y<=stone_a.MaxEdge.Y; y++)
	for(s16 x=stone_a.MinEdge.X; x<=stone_a.MaxEdge.X; x++)
	{
		double d = noise3d_perlin((float)x/3.,(float)z/3.,(float)y/3.,
				p0.Z*4243+p0.Y*34+p0.X, 2, 0.5);
		if(z == stone_a.MinEdge.Z || z == stone_a.MaxEdge.Z)
			d -= 0.3;
		if(/*y == stone_a.MinEdge.Y ||*/ y == stone_a.MaxEdge.Y)
			d -= 0.3;
		if(x == stone_a.MinEdge.X || x == stone_a.MaxEdge.X)
			d -= 0.3;
		if(d > 0.0)
		{
			u32 vi = stone_a.index(v3s16(x,y,z));
			stone_d[vi] = 1;
		}
	}

	/*// Add stone randomly
	for(u32 iii=0; iii<7; iii++)
	{
		s16 d = 1;

		v3s16 p(
			myrand_range(stone_a.MinEdge.X, stone_a.MaxEdge.X-d),
			myrand_range(stone_a.MinEdge.Y, stone_a.MaxEdge.Y-d),
			myrand_range(stone_a.MinEdge.Z, stone_a.MaxEdge.Z-d)
		);

		for(s16 z=0; z<=d; z++)
		for(s16 y=0; y<=d; y++)
		for(s16 x=0; x<=d; x++)
		{
			stone_d[stone_a.index(p+v3s16(x,y,z))] = 1;
		}
	}*/

	// Blit stone to vmanip
	for(s16 z=stone_a.MinEdge.Z; z<=stone_a.MaxEdge.Z; z++)
	for(s16 y=stone_a.MinEdge.Y; y<=stone_a.MaxEdge.Y; y++)
	for(s16 x=stone_a.MinEdge.X; x<=stone_a.MaxEdge.X; x++)
	{
		v3s16 p(x,y,z);
		p += p0;
		if(vmanip.m_area.contains(p) == false)
			continue;
		u32 vi = vmanip.m_area.index(p);
		if(vmanip.m_data[vi].d != CONTENT_AIR
				&& vmanip.m_data[vi].d != CONTENT_IGNORE)
			continue;
		u32 i = stone_a.index(x,y,z);
		if(stone_d[i] == 1)
			vmanip.m_data[vi] = stonenode;
	}
}
#endif

#if 0
static void make_largestone(VoxelManipulator &vmanip, v3s16 p0)
{
	MapNode stonenode(CONTENT_STONE);

	s16 size = myrand_range(8, 16);
	
	VoxelArea stone_a(v3s16(-size/2,0,-size/2), v3s16(size/2,size,size/2));
	Buffer<u8> stone_d(stone_a.getVolume());
	for(s32 i=0; i<stone_a.getVolume(); i++)
		stone_d[i] = 0;

	// Force stone at bottom to make it usually touch the ground
	{
		for(s16 z=0; z<=0; z++)
		for(s16 y=0; y<=0; y++)
		for(s16 x=0; x<=0; x++)
		{
			stone_d[stone_a.index(v3s16(x,y,z))] = 1;
		}
	}

	// Generate from perlin noise
	for(s16 z=stone_a.MinEdge.Z; z<=stone_a.MaxEdge.Z; z++)
	for(s16 y=stone_a.MinEdge.Y; y<=stone_a.MaxEdge.Y; y++)
	for(s16 x=stone_a.MinEdge.X; x<=stone_a.MaxEdge.X; x++)
	{
		double d = 1.0;
		d += noise3d_perlin((float)x/10.,(float)z/10.,(float)y/10.,
				p0.Z*5123+p0.Y*2439+p0.X, 2, 0.5);
		double mid_z = (stone_a.MaxEdge.Z+stone_a.MinEdge.Z)/2;
		double mid_x = (stone_a.MaxEdge.X+stone_a.MinEdge.X)/2;
		double mid_y = (stone_a.MaxEdge.Y+stone_a.MinEdge.Y)/2;
		double dz = (double)z-mid_z;
		double dx = (double)x-mid_x;
		double dy = MYMAX(0, (double)y-mid_y);
		double r = sqrt(dz*dz+dx*dx+dy*dy);
		d /= (2*r/size)*2 + 0.01;
		if(d > 1.0)
		{
			u32 vi = stone_a.index(v3s16(x,y,z));
			stone_d[vi] = 1;
		}
	}

	/*// Add stone randomly
	for(u32 iii=0; iii<7; iii++)
	{
		s16 d = 1;

		v3s16 p(
			myrand_range(stone_a.MinEdge.X, stone_a.MaxEdge.X-d),
			myrand_range(stone_a.MinEdge.Y, stone_a.MaxEdge.Y-d),
			myrand_range(stone_a.MinEdge.Z, stone_a.MaxEdge.Z-d)
		);

		for(s16 z=0; z<=d; z++)
		for(s16 y=0; y<=d; y++)
		for(s16 x=0; x<=d; x++)
		{
			stone_d[stone_a.index(p+v3s16(x,y,z))] = 1;
		}
	}*/

	// Blit stone to vmanip
	for(s16 z=stone_a.MinEdge.Z; z<=stone_a.MaxEdge.Z; z++)
	for(s16 y=stone_a.MinEdge.Y; y<=stone_a.MaxEdge.Y; y++)
	for(s16 x=stone_a.MinEdge.X; x<=stone_a.MaxEdge.X; x++)
	{
		v3s16 p(x,y,z);
		p += p0;
		if(vmanip.m_area.contains(p) == false)
			continue;
		u32 vi = vmanip.m_area.index(p);
		/*if(vmanip.m_data[vi].d != CONTENT_AIR
				&& vmanip.m_data[vi].d != CONTENT_IGNORE)
			continue;*/
		u32 i = stone_a.index(x,y,z);
		if(stone_d[i] == 1)
			vmanip.m_data[vi] = stonenode;
	}
}
#endif

/*
	Dungeon making routines
*/

#define VMANIP_FLAG_DUNGEON_INSIDE VOXELFLAG_CHECKED1
#define VMANIP_FLAG_DUNGEON_PRESERVE VOXELFLAG_CHECKED2
#define VMANIP_FLAG_DUNGEON_UNTOUCHABLE (\
		VMANIP_FLAG_DUNGEON_INSIDE|VMANIP_FLAG_DUNGEON_PRESERVE)

static void make_room1(VoxelManipulator &vmanip, v3s16 roomsize, v3s16 roomplace)
{
	// Make +-X walls
	for(s16 z=0; z<roomsize.Z; z++)
	for(s16 y=0; y<roomsize.Y; y++)
	{
		{
			v3s16 p = roomplace + v3s16(0,y,z);
			if(vmanip.m_area.contains(p) == false)
				continue;
			u32 vi = vmanip.m_area.index(p);
			if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
				continue;
			vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
		}
		{
			v3s16 p = roomplace + v3s16(roomsize.X-1,y,z);
			if(vmanip.m_area.contains(p) == false)
				continue;
			u32 vi = vmanip.m_area.index(p);
			if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
				continue;
			vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
		}
	}
	
	// Make +-Z walls
	for(s16 x=0; x<roomsize.X; x++)
	for(s16 y=0; y<roomsize.Y; y++)
	{
		{
			v3s16 p = roomplace + v3s16(x,y,0);
			if(vmanip.m_area.contains(p) == false)
				continue;
			u32 vi = vmanip.m_area.index(p);
			if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
				continue;
			vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
		}
		{
			v3s16 p = roomplace + v3s16(x,y,roomsize.Z-1);
			if(vmanip.m_area.contains(p) == false)
				continue;
			u32 vi = vmanip.m_area.index(p);
			if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
				continue;
			vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
		}
	}
	
	// Make +-Y walls (floor and ceiling)
	for(s16 z=0; z<roomsize.Z; z++)
	for(s16 x=0; x<roomsize.X; x++)
	{
		{
			v3s16 p = roomplace + v3s16(x,0,z);
			if(vmanip.m_area.contains(p) == false)
				continue;
			u32 vi = vmanip.m_area.index(p);
			if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
				continue;
			vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
		}
		{
			v3s16 p = roomplace + v3s16(x,roomsize.Y-1,z);
			if(vmanip.m_area.contains(p) == false)
				continue;
			u32 vi = vmanip.m_area.index(p);
			if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
				continue;
			vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
		}
	}
	
	// Fill with air
	for(s16 z=1; z<roomsize.Z-1; z++)
	for(s16 y=1; y<roomsize.Y-1; y++)
	for(s16 x=1; x<roomsize.X-1; x++)
	{
		v3s16 p = roomplace + v3s16(x,y,z);
		if(vmanip.m_area.contains(p) == false)
			continue;
		u32 vi = vmanip.m_area.index(p);
		vmanip.m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
		vmanip.m_data[vi] = MapNode(CONTENT_AIR);
	}
}

static void make_fill(VoxelManipulator &vmanip, v3s16 place, v3s16 size,
		u8 avoid_flags, MapNode n, u8 or_flags)
{
	for(s16 z=0; z<size.Z; z++)
	for(s16 y=0; y<size.Y; y++)
	for(s16 x=0; x<size.X; x++)
	{
		v3s16 p = place + v3s16(x,y,z);
		if(vmanip.m_area.contains(p) == false)
			continue;
		u32 vi = vmanip.m_area.index(p);
		if(vmanip.m_flags[vi] & avoid_flags)
			continue;
		vmanip.m_flags[vi] |= or_flags;
		vmanip.m_data[vi] = n;
	}
}

static void make_hole1(VoxelManipulator &vmanip, v3s16 place)
{
	make_fill(vmanip, place, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
			VMANIP_FLAG_DUNGEON_INSIDE);
}

static void make_door1(VoxelManipulator &vmanip, v3s16 doorplace, v3s16 doordir)
{
	make_hole1(vmanip, doorplace);
	// Place torch (for testing)
	//vmanip.m_data[vmanip.m_area.index(doorplace)] = MapNode(CONTENT_TORCH);
}

static v3s16 rand_ortho_dir(PseudoRandom &random)
{
	if(random.next()%2==0)
		return random.next()%2 ? v3s16(-1,0,0) : v3s16(1,0,0);
	else
		return random.next()%2 ? v3s16(0,0,-1) : v3s16(0,0,1);
}

static v3s16 turn_xz(v3s16 olddir, int t)
{
	v3s16 dir;
	if(t == 0)
	{
		// Turn right
		dir.X = olddir.Z;
		dir.Z = -olddir.X;
		dir.Y = olddir.Y;
	}
	else
	{
		// Turn left
		dir.X = -olddir.Z;
		dir.Z = olddir.X;
		dir.Y = olddir.Y;
	}
	return dir;
}

static v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
{
	int turn = random.range(0,2);
	v3s16 dir;
	if(turn == 0)
	{
		// Go straight
		dir = olddir;
	}
	else if(turn == 1)
		// Turn right
		dir = turn_xz(olddir, 0);
	else
		// Turn left
		dir = turn_xz(olddir, 1);
	return dir;
}

static void make_corridor(VoxelManipulator &vmanip, v3s16 doorplace,
		v3s16 doordir, v3s16 &result_place, v3s16 &result_dir,
		PseudoRandom &random)
{
	make_hole1(vmanip, doorplace);
	v3s16 p0 = doorplace;
	v3s16 dir = doordir;
	u32 length;
	if(random.next()%2)
		length = random.range(1,13);
	else
		length = random.range(1,6);
	length = random.range(1,13);
	u32 partlength = random.range(1,13);
	u32 partcount = 0;
	s16 make_stairs = 0;
	if(random.next()%2 == 0 && partlength >= 3)
		make_stairs = random.next()%2 ? 1 : -1;
	for(u32 i=0; i<length; i++)
	{
		v3s16 p = p0 + dir;
		if(partcount != 0)
			p.Y += make_stairs;

		/*// If already empty
		if(vmanip.getNodeNoExNoEmerge(p).d
				== CONTENT_AIR
		&& vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).d
				== CONTENT_AIR)
		{
		}*/

		if(vmanip.m_area.contains(p) == true
				&& vmanip.m_area.contains(p+v3s16(0,1,0)) == true)
		{
			if(make_stairs)
			{
				make_fill(vmanip, p+v3s16(-1,-1,-1), v3s16(3,5,3),
						VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(CONTENT_COBBLE), 0);
				make_fill(vmanip, p, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
						VMANIP_FLAG_DUNGEON_INSIDE);
				make_fill(vmanip, p-dir, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
						VMANIP_FLAG_DUNGEON_INSIDE);
			}
			else
			{
				make_fill(vmanip, p+v3s16(-1,-1,-1), v3s16(3,4,3),
						VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(CONTENT_COBBLE), 0);
				make_hole1(vmanip, p);
				/*make_fill(vmanip, p, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
						VMANIP_FLAG_DUNGEON_INSIDE);*/
			}

			p0 = p;
		}
		else
		{
			// Can't go here, turn away
			dir = turn_xz(dir, random.range(0,1));
			make_stairs = -make_stairs;
			partcount = 0;
			partlength = random.range(1,length);
			continue;
		}

		partcount++;
		if(partcount >= partlength)
		{
			partcount = 0;
			
			dir = random_turn(random, dir);
			
			partlength = random.range(1,length);

			make_stairs = 0;
			if(random.next()%2 == 0 && partlength >= 3)
				make_stairs = random.next()%2 ? 1 : -1;
		}
	}
	result_place = p0;
	result_dir = dir;
}

class RoomWalker
{
public:

	RoomWalker(VoxelManipulator &vmanip_, v3s16 pos, PseudoRandom &random):
			vmanip(vmanip_),
			m_pos(pos),
			m_random(random)
	{
		randomizeDir();
	}

	void randomizeDir()
	{
		m_dir = rand_ortho_dir(m_random);
	}

	void setPos(v3s16 pos)
	{
		m_pos = pos;
	}

	void setDir(v3s16 dir)
	{
		m_dir = dir;
	}
	
	bool findPlaceForDoor(v3s16 &result_place, v3s16 &result_dir)
	{
		for(u32 i=0; i<100; i++)
		{
			v3s16 p = m_pos + m_dir;
			v3s16 p1 = p + v3s16(0,1,0);
			if(vmanip.m_area.contains(p) == false
					|| vmanip.m_area.contains(p1) == false
					|| i % 4 == 0)
			{
				randomizeDir();
				continue;
			}
			if(vmanip.getNodeNoExNoEmerge(p).d
					== CONTENT_COBBLE
			&& vmanip.getNodeNoExNoEmerge(p1).d
					== CONTENT_COBBLE)
			{
				// Found wall, this is a good place!
				result_place = p;
				result_dir = m_dir;
				// Randomize next direction
				randomizeDir();
				return true;
			}
			/*
				Determine where to move next
			*/
			// Jump one up if the actual space is there
			if(vmanip.getNodeNoExNoEmerge(p+v3s16(0,0,0)).d
					== CONTENT_COBBLE
			&& vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).d
					== CONTENT_AIR
			&& vmanip.getNodeNoExNoEmerge(p+v3s16(0,2,0)).d
					== CONTENT_AIR)
				p += v3s16(0,1,0);
			// Jump one down if the actual space is there
			if(vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).d
					== CONTENT_COBBLE
			&& vmanip.getNodeNoExNoEmerge(p+v3s16(0,0,0)).d
					== CONTENT_AIR
			&& vmanip.getNodeNoExNoEmerge(p+v3s16(0,-1,0)).d
					== CONTENT_AIR)
				p += v3s16(0,-1,0);
			// Check if walking is now possible
			if(vmanip.getNodeNoExNoEmerge(p).d
					!= CONTENT_AIR
			|| vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).d
					!= CONTENT_AIR)
			{
				// Cannot continue walking here
				randomizeDir();
				continue;
			}
			// Move there
			m_pos = p;
		}
		return false;
	}

	bool findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
			v3s16 &result_doordir, v3s16 &result_roomplace)
	{
		for(s16 trycount=0; trycount<30; trycount++)
		{
			v3s16 doorplace;
			v3s16 doordir;
			bool r = findPlaceForDoor(doorplace, doordir);
			if(r == false)
				continue;
			v3s16 roomplace;
			// X east, Z north, Y up
#if 1
			if(doordir == v3s16(1,0,0)) // X+
				roomplace = doorplace +
						v3s16(0,-1,m_random.range(-roomsize.Z+2,-2));
			if(doordir == v3s16(-1,0,0)) // X-
				roomplace = doorplace +
						v3s16(-roomsize.X+1,-1,m_random.range(-roomsize.Z+2,-2));
			if(doordir == v3s16(0,0,1)) // Z+
				roomplace = doorplace +
						v3s16(m_random.range(-roomsize.X+2,-2),-1,0);
			if(doordir == v3s16(0,0,-1)) // Z-
				roomplace = doorplace +
						v3s16(m_random.range(-roomsize.X+2,-2),-1,-roomsize.Z+1);
#endif
#if 0
			if(doordir == v3s16(1,0,0)) // X+
				roomplace = doorplace + v3s16(0,-1,-roomsize.Z/2);
			if(doordir == v3s16(-1,0,0)) // X-
				roomplace = doorplace + v3s16(-roomsize.X+1,-1,-roomsize.Z/2);
			if(doordir == v3s16(0,0,1)) // Z+
				roomplace = doorplace + v3s16(-roomsize.X/2,-1,0);
			if(doordir == v3s16(0,0,-1)) // Z-
				roomplace = doorplace + v3s16(-roomsize.X/2,-1,-roomsize.Z+1);
#endif
			
			// Check fit
			bool fits = true;
			for(s16 z=1; z<roomsize.Z-1; z++)
			for(s16 y=1; y<roomsize.Y-1; y++)
			for(s16 x=1; x<roomsize.X-1; x++)
			{
				v3s16 p = roomplace + v3s16(x,y,z);
				if(vmanip.m_area.contains(p) == false)
				{
					fits = false;
					break;
				}
				if(vmanip.m_flags[vmanip.m_area.index(p)]
						& VMANIP_FLAG_DUNGEON_INSIDE)
				{
					fits = false;
					break;
				}
			}
			if(fits == false)
			{
				// Find new place
				continue;
			}
			result_doorplace = doorplace;
			result_doordir = doordir;
			result_roomplace = roomplace;
			return true;
		}
		return false;
	}

private:
	VoxelManipulator &vmanip;
	v3s16 m_pos;
	v3s16 m_dir;
	PseudoRandom &m_random;
};

static void make_dungeon1(VoxelManipulator &vmanip, PseudoRandom &random)
{
	v3s16 areasize = vmanip.m_area.getExtent();
	v3s16 roomsize;
	v3s16 roomplace;
	
	/*
		Find place for first room
	*/
	bool fits = false;
	for(u32 i=0; i<100; i++)
	{
		roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
		roomplace = vmanip.m_area.MinEdge + v3s16(
				random.range(0,areasize.X-roomsize.X-1),
				random.range(0,areasize.Y-roomsize.Y-1),
				random.range(0,areasize.Z-roomsize.Z-1));
		/*
			Check that we're not putting the room to an unknown place,
			otherwise it might end up floating in the air
		*/
		fits = true;
		for(s16 z=1; z<roomsize.Z-1; z++)
		for(s16 y=1; y<roomsize.Y-1; y++)
		for(s16 x=1; x<roomsize.X-1; x++)
		{
			v3s16 p = roomplace + v3s16(x,y,z);
			u32 vi = vmanip.m_area.index(p);
			if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_INSIDE)
			{
				fits = false;
				break;
			}
			if(vmanip.m_data[vi].d == CONTENT_IGNORE)
			{
				fits = false;
				break;
			}
		}
		if(fits)
			break;
	}
	// No place found
	if(fits == false)
		return;
	
	/*
		Stores the center position of the last room made, so that
		a new corridor can be started from the last room instead of
		the new room, if chosen so.
	*/
	v3s16 last_room_center = roomplace+v3s16(roomsize.X/2,1,roomsize.Z/2);
	
	u32 room_count = random.range(2,7);
	for(u32 i=0; i<room_count; i++)
	{
		// Make a room to the determined place
		make_room1(vmanip, roomsize, roomplace);
		
		v3s16 room_center = roomplace + v3s16(roomsize.X/2,1,roomsize.Z/2);

		// Place torch at room center (for testing)
		//vmanip.m_data[vmanip.m_area.index(room_center)] = MapNode(CONTENT_TORCH);

		// Quit if last room
		if(i == room_count-1)
			break;
		
		// Determine walker start position

		bool start_in_last_room = (random.range(0,2)!=0);
		//bool start_in_last_room = true;

		v3s16 walker_start_place;

		if(start_in_last_room)
		{
			walker_start_place = last_room_center;
		}
		else
		{
			walker_start_place = room_center;
			// Store center of current room as the last one
			last_room_center = room_center;
		}
		
		// Create walker and find a place for a door
		RoomWalker walker(vmanip, walker_start_place, random);
		v3s16 doorplace;
		v3s16 doordir;
		bool r = walker.findPlaceForDoor(doorplace, doordir);
		if(r == false)
			return;
		
		if(random.range(0,1)==0)
			// Make the door
			make_door1(vmanip, doorplace, doordir);
		else
			// Don't actually make a door
			doorplace -= doordir;
		
		// Make a random corridor starting from the door
		v3s16 corridor_end;
		v3s16 corridor_end_dir;
		make_corridor(vmanip, doorplace, doordir, corridor_end,
				corridor_end_dir, random);
		
		// Find a place for a random sized room
		roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
		walker.setPos(corridor_end);
		walker.setDir(corridor_end_dir);
		r = walker.findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace);
		if(r == false)
			return;

		if(random.range(0,1)==0)
			// Make the door
			make_door1(vmanip, doorplace, doordir);
		else
			// Don't actually make a door
			roomplace -= doordir;
		
	}
}

/*
	Noise functions. Make sure seed is mangled differently in each one.
*/

/*
	Scaling the output of the noise function affects the overdrive of the
	contour function, which affects the shape of the output considerably.
*/
#define CAVE_NOISE_SCALE 12.0
//#define CAVE_NOISE_SCALE 10.0
//#define CAVE_NOISE_SCALE 7.5
//#define CAVE_NOISE_SCALE 5.0
//#define CAVE_NOISE_SCALE 1.0

//#define CAVE_NOISE_THRESHOLD (2.5/CAVE_NOISE_SCALE)
#define CAVE_NOISE_THRESHOLD (1.5/CAVE_NOISE_SCALE)

NoiseParams get_cave_noise1_params(u64 seed)
{
	/*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.7,
			200, CAVE_NOISE_SCALE);*/
	/*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 4, 0.7,
			100, CAVE_NOISE_SCALE);*/
	/*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.6,
			100, CAVE_NOISE_SCALE);*/
	/*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.3,
			100, CAVE_NOISE_SCALE);*/
	return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 4, 0.5,
			50, CAVE_NOISE_SCALE);
	//return NoiseParams(NOISE_CONSTANT_ONE);
}

NoiseParams get_cave_noise2_params(u64 seed)
{
	/*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.7,
			200, CAVE_NOISE_SCALE);*/
	/*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 4, 0.7,
			100, CAVE_NOISE_SCALE);*/
	/*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.3,
			100, CAVE_NOISE_SCALE);*/
	return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 4, 0.5,
			50, CAVE_NOISE_SCALE);
	//return NoiseParams(NOISE_CONSTANT_ONE);
}

NoiseParams get_ground_noise1_params(u64 seed)
{
	return NoiseParams(NOISE_PERLIN, seed+983240, 4,
			0.55, 80.0, 40.0);
}

NoiseParams get_ground_crumbleness_params(u64 seed)
{
	return NoiseParams(NOISE_PERLIN, seed+34413, 3,
			1.3, 20.0, 1.0);
}

NoiseParams get_ground_wetness_params(u64 seed)
{
	return NoiseParams(NOISE_PERLIN, seed+32474, 4,
			1.1, 40.0, 1.0);
}

bool is_cave(u64 seed, v3s16 p)
{
	double d1 = noise3d_param(get_cave_noise1_params(seed), p.X,p.Y,p.Z);
	double d2 = noise3d_param(get_cave_noise2_params(seed), p.X,p.Y,p.Z);
	return d1*d2 > CAVE_NOISE_THRESHOLD;
}

/*
	Ground density noise shall be interpreted by using this.

	TODO: No perlin noises here, they should be outsourced
	      and buffered
		  NOTE: The speed of these actually isn't terrible
*/
bool val_is_ground(double ground_noise1_val, v3s16 p, u64 seed)
{
	//return ((double)p.Y < ground_noise1_val);

	double f = 0.55 + noise2d_perlin(
			0.5+(float)p.X/250, 0.5+(float)p.Z/250,
			seed+920381, 3, 0.45);
	if(f < 0.01)
		f = 0.01;
	else if(f >= 1.0)
		f *= 1.6;
	double h = WATER_LEVEL + 10 * noise2d_perlin(
			0.5+(float)p.X/250, 0.5+(float)p.Z/250,
			seed+84174, 4, 0.5);
	/*double f = 1;
	double h = 0;*/
	return ((double)p.Y - h < ground_noise1_val * f);
}

/*
	Queries whether a position is ground or not.
*/
bool is_ground(u64 seed, v3s16 p)
{
	double val1 = noise3d_param(get_ground_noise1_params(seed), p.X,p.Y,p.Z);
	return val_is_ground(val1, p, seed);
}

// Amount of trees per area in nodes
double tree_amount_2d(u64 seed, v2s16 p)
{
	/*double noise = noise2d_perlin(
			0.5+(float)p.X/250, 0.5+(float)p.Y/250,
			seed+2, 5, 0.66);*/
	double noise = noise2d_perlin(
			0.5+(float)p.X/125, 0.5+(float)p.Y/125,
			seed+2, 4, 0.66);
	double zeroval = -0.35;
	if(noise < zeroval)
		return 0;
	else
		return 0.04 * (noise-zeroval) / (1.0-zeroval);
}

#if 0
double randomstone_amount_2d(u64 seed, v2s16 p)
{
	double noise = noise2d_perlin(
			0.5+(float)p.X/250, 0.5+(float)p.Y/250,
			seed+3829434, 5, 0.66);
	double zeroval = 0.1;
	if(noise < zeroval)
		return 0;
	else
		return 0.01 * (noise-zeroval) / (1.0-zeroval);
}
#endif

double largestone_amount_2d(u64 seed, v2s16 p)
{
	double noise = noise2d_perlin(
			0.5+(float)p.X/250, 0.5+(float)p.Y/250,
			seed+14143242, 5, 0.66);
	double zeroval = 0.3;
	if(noise < zeroval)
		return 0;
	else
		return 0.005 * (noise-zeroval) / (1.0-zeroval);
}

/*
	Incrementally find ground level from 3d noise
*/
s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
{
	// Start a bit fuzzy to make averaging lower precision values
	// more useful
	s16 level = myrand_range(-precision/2, precision/2);
	s16 dec[] = {31000, 100, 20, 4, 1, 0};
	s16 i;
	for(i = 1; dec[i] != 0 && precision <= dec[i]; i++)
	{
		// First find non-ground by going upwards
		// Don't stop in caves.
		{
			s16 max = level+dec[i-1]*2;
			v3s16 p(p2d.X, level, p2d.Y);
			for(; p.Y < max; p.Y += dec[i])
			{
				if(!is_ground(seed, p))
				{
					level = p.Y;
					break;
				}
			}
		}
		// Then find ground by going downwards from there.
		// Go in caves, too, when precision is 1.
		{
			s16 min = level-dec[i-1]*2;
			v3s16 p(p2d.X, level, p2d.Y);