aboutsummaryrefslogtreecommitdiff
path: root/src/mapgen/mg_schematic.h
blob: 9189bb3a79738b70165d141a19c53af376f8dca6 (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
/*
Minetest
Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
Copyright (C) 2015-2018 paramat

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.
*/

#pragma once

#include <map>
#include "mg_decoration.h"
#include "util/string.h"

class Map;
class ServerMap;
class Mapgen;
class MMVManip;
class PseudoRandom;
class NodeResolver;
class Server;

/*
	Minetest Schematic File Format

	All values are stored in big-endian byte order.
	[u32] signature: 'MTSM'
	[u16] version: 4
	[u16] size X
	[u16] size Y
	[u16] size Z
	For each Y:
		[u8] slice probability value
	[Name-ID table] Name ID Mapping Table
		[u16] name-id count
		For each name-id mapping:
			[u16] name length
			[u8[]] name
	ZLib deflated {
	For each node in schematic:  (for z, y, x)
		[u16] content
	For each node in schematic:
		[u8] param1
		  bit 0-6: probability
		  bit 7:   specific node force placement
	For each node in schematic:
		[u8] param2
	}

	Version changes:
	1 - Initial version
	2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
	3 - Added y-slice probabilities; this allows for variable height structures
	4 - Compressed range of node occurence prob., added per-node force placement bit
*/

//// Schematic constants
#define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
#define MTSCHEM_FILE_VER_HIGHEST_READ  4
#define MTSCHEM_FILE_VER_HIGHEST_WRITE 4
#define MTSCHEM_MAPNODE_SER_FMT_VER    28 // Fixed serialization version for schematics since these still need to use Zlib

#define MTSCHEM_PROB_MASK       0x7F

#define MTSCHEM_PROB_NEVER      0x00
#define MTSCHEM_PROB_ALWAYS     0x7F
#define MTSCHEM_PROB_ALWAYS_OLD 0xFF

#define MTSCHEM_FORCE_PLACE     0x80

enum SchematicType
{
	SCHEMATIC_NORMAL,
};

enum SchematicFormatType {
	SCHEM_FMT_HANDLE,
	SCHEM_FMT_MTS,
	SCHEM_FMT_LUA,
};

class Schematic : public ObjDef, public NodeResolver {
public:
	Schematic() = default;
	virtual ~Schematic();

	ObjDef *clone() const;

	virtual void resolveNodeNames();

	bool loadSchematicFromFile(const std::string &filename,
		const NodeDefManager *ndef, StringMap *replace_names = NULL);
	bool saveSchematicToFile(const std::string &filename,
		const NodeDefManager *ndef);
	bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);

	bool deserializeFromMts(std::istream *is);
	bool serializeToMts(std::ostream *os) const;
	bool serializeToLua(std::ostream *os, bool use_comments, u32 indent_spaces) const;

	void blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place);
	bool placeOnVManip(MMVManip *vm, v3s16 p, u32 flags, Rotation rot, bool force_place);
	void placeOnMap(ServerMap *map, v3s16 p, u32 flags, Rotation rot, bool force_place);

	void applyProbabilities(v3s16 p0,
		std::vector<std::pair<v3s16, u8> > *plist,
		std::vector<std::pair<s16, u8> > *splist);

	std::vector<content_t> c_nodes;
	u32 flags = 0;
	v3s16 size;
	MapNode *schemdata = nullptr;
	u8 *slice_probs = nullptr;

private:
	// Counterpart to the node resolver: Condense content_t to a sequential "m_nodenames" list
	void condenseContentIds();
};

class SchematicManager : public ObjDefManager {
public:
	SchematicManager(Server *server);
	virtual ~SchematicManager() = default;

	SchematicManager *clone() const;

	virtual void clear();

	const char *getObjectTitle() const
	{
		return "schematic";
	}

	static Schematic *create(SchematicType type)
	{
		return new Schematic;
	}

private:
	SchematicManager() {};

	Server *m_server;
};

umeric.h" // MYMIN //! constructor Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id): scene::ISceneNode(parent, mgr, id), m_first_update(true), m_brightness(0.5), m_cloud_brightness(0.5), m_bgcolor_bright_f(1,1,1,1), m_skycolor_bright_f(1,1,1,1), m_cloudcolor_bright_f(1,1,1,1) { setAutomaticCulling(scene::EAC_OFF); Box.MaxEdge.set(0,0,0); Box.MinEdge.set(0,0,0); // create material video::SMaterial mat; mat.Lighting = false; mat.ZBuffer = video::ECFN_NEVER; mat.ZWriteEnable = false; mat.AntiAliasing=0; mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; mat.BackfaceCulling = false; m_materials[0] = mat; m_materials[1] = mat; //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA; m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; m_materials[2] = mat; m_materials[2].setTexture(0, mgr->getVideoDriver()->getTexture( getTexturePath("sunrisebg.png").c_str())); m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR; for(u32 i=0; i<SKY_STAR_COUNT; i++){ m_stars[i] = v3f( myrand_range(-10000,10000), myrand_range(-10000,10000), myrand_range(-10000,10000) ); m_stars[i].normalize(); } } void Sky::OnRegisterSceneNode() { if (IsVisible) SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX); scene::ISceneNode::OnRegisterSceneNode(); } const core::aabbox3d<f32>& Sky::getBoundingBox() const { return Box; } //! renders the node. void Sky::render() { video::IVideoDriver* driver = SceneManager->getVideoDriver(); scene::ICameraSceneNode* camera = SceneManager->getActiveCamera(); if (!camera || !driver) return; ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG); // draw perspective skybox core::matrix4 translate(AbsoluteTransformation); translate.setTranslation(camera->getAbsolutePosition()); // Draw the sky box between the near and far clip plane const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f; core::matrix4 scale; scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance)); driver->setTransform(video::ETS_WORLD, translate * scale); if(m_sunlight_seen) { float sunsize = 0.07; video::SColorf suncolor_f(1, 1, 0, 1); suncolor_f.r = 1; suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7+m_time_brightness*(0.5))); suncolor_f.b = MYMAX(0.0, m_brightness*0.95); video::SColorf suncolor2_f(1, 1, 1, 1); suncolor_f.r = 1; suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85+m_time_brightness*(0.5))); suncolor_f.b = MYMAX(0.0, m_brightness); float moonsize = 0.04; video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1); video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1); float nightlength = 0.415; float wn = nightlength / 2; float wicked_time_of_day = 0; if(m_time_of_day > wn && m_time_of_day < 1.0 - wn) wicked_time_of_day = (m_time_of_day - wn)/(1.0-wn*2)*0.5 + 0.25; else if(m_time_of_day < 0.5) wicked_time_of_day = m_time_of_day / wn * 0.25; else wicked_time_of_day = 1.0 - ((1.0-m_time_of_day) / wn * 0.25); /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> " <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/ video::SColor suncolor = suncolor_f.toSColor(); video::SColor suncolor2 = suncolor2_f.toSColor(); video::SColor mooncolor = mooncolor_f.toSColor(); video::SColor mooncolor2 = mooncolor2_f.toSColor(); const f32 t = 1.0f; const f32 o = 0.0f; static const u16 indices[4] = {0,1,2,3}; video::S3DVertex vertices[4]; driver->setMaterial(m_materials[1]); //video::SColor cloudyfogcolor(255,255,255,255); video::SColor cloudyfogcolor = m_bgcolor; //video::SColor cloudyfogcolor = m_bgcolor.getInterpolated(m_skycolor, 0.5); // Draw far cloudy fog thing for(u32 j=0; j<4; j++) { video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45); vertices[0] = video::S3DVertex(-1, 0.08,-1, 0,0,1, c, t, t); vertices[1] = video::S3DVertex( 1, 0.08,-1, 0,0,1, c, o, t); vertices[2] = video::S3DVertex( 1, 0.12,-1, 0,0,1, c, o, o); vertices[3] = video::S3DVertex(-1, 0.12,-1, 0,0,1, c, t, o); for(u32 i=0; i<4; i++){ if(j==0) // Don't switch {} else if(j==1) // Switch from -Z (south) to +X (east) vertices[i].Pos.rotateXZBy(90); else if(j==2) // Switch from -Z (south) to -X (west) vertices[i].Pos.rotateXZBy(-90); else // Switch from -Z (south) to -Z (north) vertices[i].Pos.rotateXZBy(-180); } driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2); } for(u32 j=0; j<4; j++) { video::SColor c = cloudyfogcolor; vertices[0] = video::S3DVertex(-1,-1.0,-1, 0,0,1, c, t, t); vertices[1] = video::S3DVertex( 1,-1.0,-1, 0,0,1, c, o, t); vertices[2] = video::S3DVertex( 1, 0.08,-1, 0,0,1, c, o, o); vertices[3] = video::S3DVertex(-1, 0.08,-1, 0,0,1, c, t, o); for(u32 i=0; i<4; i++){ if(j==0) // Don't switch {} else if(j==1) // Switch from -Z (south) to +X (east) vertices[i].Pos.rotateXZBy(90); else if(j==2) // Switch from -Z (south) to -X (west) vertices[i].Pos.rotateXZBy(-90); else // Switch from -Z (south) to -Z (north) vertices[i].Pos.rotateXZBy(-180); } driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2); } driver->setMaterial(m_materials[2]); { float mid1 = 0.25; float mid = (wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1)); float a_ = 1.0 - fabs(wicked_time_of_day - mid) * 35.0; float a = easeCurve(MYMAX(0, MYMIN(1, a_))); //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl; video::SColor c(255,255,255,255); float y = -(1.0 - a) * 0.2; vertices[0] = video::S3DVertex(-1,-0.05+y,-1, 0,0,1, c, t, t); vertices[1] = video::S3DVertex( 1,-0.05+y,-1, 0,0,1, c, o, t); vertices[2] = video::S3DVertex( 1, 0.2+y,-1, 0,0,1, c, o, o); vertices[3] = video::S3DVertex(-1, 0.2+y,-1, 0,0,1, c, t, o); for(u32 i=0; i<4; i++){ if(wicked_time_of_day < 0.5) // Switch from -Z (south) to +X (east) vertices[i].Pos.rotateXZBy(90); else // Switch from -Z (south) to -X (west) vertices[i].Pos.rotateXZBy(-90); } driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2); } driver->setMaterial(m_materials[1]); // Draw sun if(wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85) { float d = sunsize * 1.7; video::SColor c = suncolor; c.setAlpha(0.05*255); vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t); vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t); vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o); vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o); for(u32 i=0; i<4; i++){ // Switch from -Z (south) to +X (east) vertices[i].Pos.rotateXZBy(90); vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90); } driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2); d = sunsize * 1.2; c = suncolor; c.setAlpha(0.15*255); vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t); vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t); vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o); vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o); for(u32 i=0; i<4; i++){ // Switch from -Z (south) to +X (east) vertices[i].Pos.rotateXZBy(90); vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90); } driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2); d = sunsize; vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor, t, t); vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor, o, t); vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor, o, o); vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor, t, o); for(u32 i=0; i<4; i++){ // Switch from -Z (south) to +X (east) vertices[i].Pos.rotateXZBy(90); vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90); } driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2); d = sunsize * 0.7; vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor2, t, t); vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor2, o, t); vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor2, o, o); vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor2, t, o); for(u32 i=0; i<4; i++){ // Switch from -Z (south) to +X (east) vertices[i].Pos.rotateXZBy(90);