summaryrefslogtreecommitdiff
path: root/src/sky.h
blob: 4af6be0246bbfaeb5f1d3b847d398915e47a8948 (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
/*
Minetest
Copyright (C) 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.
*/

#include "irrlichttypes_extrabloated.h"
#include <ISceneNode.h>
#include "camera.h"

#ifndef SKY_HEADER
#define SKY_HEADER

#define SKY_MATERIAL_COUNT 5
#define SKY_STAR_COUNT 200

// Skybox, rendered with zbuffer turned off, before all other nodes.
class Sky : public scene::ISceneNode
{
public:
	//! constructor
	Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id);

	virtual void OnRegisterSceneNode();

	//! renders the node.
	virtual void render();

	virtual const core::aabbox3d<f32>& getBoundingBox() const;

	// Used by Irrlicht for optimizing rendering
	virtual video::SMaterial& getMaterial(u32 i)
	{ return m_materials[i]; }

	// Used by Irrlicht for optimizing rendering
	virtual u32 getMaterialCount() const
	{ return SKY_MATERIAL_COUNT; }

	void update(float m_time_of_day, float time_brightness,
			float direct_brightness, bool sunlight_seen, CameraMode cam_mode,
			float yaw, float pitch);
	
	float getBrightness(){ return m_brightness; }

	video::SColor getBgColor(){
		return m_visible ? m_bgcolor : m_fallback_bg_color;
	}
	video::SColor getSkyColor(){
		return m_visible ? m_skycolor : m_fallback_bg_color;
	}
	
	bool getCloudsVisible(){ return m_clouds_visible && m_visible; }
	video::SColorf getCloudColor(){ return m_cloudcolor_f; }

	void setVisible(bool visible){ m_visible = visible; }
	void setFallbackBgColor(const video::SColor &fallback_bg_color){
		m_fallback_bg_color = fallback_bg_color;
	}

private:
	core::aabbox3d<f32> Box;
	video::SMaterial m_materials[SKY_MATERIAL_COUNT];

	// How much sun & moon transition should affect horizon color
	float m_horizon_blend()
	{
		if (!m_sunlight_seen)
			return 0;
		float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2 : m_time_of_day * 2;

		if (x <= 0.3)
			return 0;
		if (x <= 0.4) // when the sun and moon are aligned
			return (x - 0.3) * 10;
		if (x <= 0.5)
			return (0.5 - x) * 10;
		return 0;
	}

	// Mix two colors by a given amount
	video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
	{
		video::SColor result = video::SColor(
			col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
			col1.getRed() * (1 - factor) + col2.getRed() * factor,
			col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
			col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
		return result;
	}
	video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
	{
		video::SColorf result = video::SColorf(
			col1.r * (1 - factor) + col2.r * factor,
			col1.g * (1 - factor) + col2.g * factor,
			col1.b * (1 - factor) + col2.b * factor,
			col1.a * (1 - factor) + col2.a * factor);
		return result;
	}

	bool m_visible;
	video::SColor m_fallback_bg_color; // Used when m_visible=false
	bool m_first_update;
	float m_time_of_day;
	float m_time_brightness;
	bool m_sunlight_seen;
	float m_brightness;
	float m_cloud_brightness;
	bool m_clouds_visible;
	bool m_directional_colored_fog;
	video::SColorf m_bgcolor_bright_f;
	video::SColorf m_skycolor_bright_f;
	video::SColorf m_cloudcolor_bright_f;
	video::SColor m_bgcolor;
	video::SColor m_skycolor;
	video::SColorf m_cloudcolor_f;
	v3f m_stars[SKY_STAR_COUNT];
	video::S3DVertex m_star_vertices[SKY_STAR_COUNT*4];
	video::ITexture* m_sun_texture;
	video::ITexture* m_moon_texture;
	video::ITexture* m_sun_tonemap;
	video::ITexture* m_moon_tonemap;
};

#endif

608'>608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
/*
Minetest-c55
Copyright (C) 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 "scriptapi.h"

#include <iostream>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

#include "log.h"
#include "server.h"
#include "porting.h"
#include "filesys.h"
#include "serverobject.h"
#include "script.h"
//#include "luna.h"
#include "luaentity_common.h"
#include "content_sao.h" // For LuaEntitySAO
#include "tooldef.h"

/*
TODO:
- Node definition
- Random node triggers (like grass growth)
- Deterministic node triggers (like falling sand)
- Object visual client-side stuff
	- Blink effect
	- Spritesheets and animation
- Named node types and dynamic id allocation per MapBlock
- LuaNodeMetadata
	blockdef.has_metadata = true/false
	- Stores an inventory and stuff in a Settings object
	meta.inventory_add_list("main")
	blockdef.on_inventory_modified
	meta.set("owner", playername)
	meta.get("owner")
- Item definition (actually, only CraftItem)
*/

static void stackDump(lua_State *L, std::ostream &o)
{
  int i;
  int top = lua_gettop(L);
  for (i = 1; i <= top; i++) {  /* repeat for each level */
	int t = lua_type(L, i);
	switch (t) {

	  case LUA_TSTRING:  /* strings */
	  	o<<"\""<<lua_tostring(L, i)<<"\"";
		break;

	  case LUA_TBOOLEAN:  /* booleans */
		o<<(lua_toboolean(L, i) ? "true" : "false");
		break;

	  case LUA_TNUMBER:  /* numbers */ {
	  	char buf[10];
		snprintf(buf, 10, "%g", lua_tonumber(L, i));
		o<<buf;
		break; }

	  default:  /* other values */
		o<<lua_typename(L, t);
		break;

	}
	o<<" ";
  }
  o<<std::endl;
}

static void realitycheck(lua_State *L)
{
	int top = lua_gettop(L);
	if(top >= 30){
		dstream<<"Stack is over 30:"<<std::endl;
		stackDump(L, dstream);
		script_error(L, "Stack is over 30 (reality check)");
	}
}

class StackUnroller
{
private:
	lua_State *m_lua;
	int m_original_top;
public:
	StackUnroller(lua_State *L):
		m_lua(L),
		m_original_top(-1)
	{
		m_original_top = lua_gettop(m_lua); // store stack height
	}
	~StackUnroller()
	{
		lua_settop(m_lua, m_original_top); // restore stack height
	}
};

v3f readFloatPos(lua_State *L, int index)
{
	v3f pos;
	lua_pushvalue(L, index); // Push pos
	luaL_checktype(L, -1, LUA_TTABLE);
	lua_getfield(L, -1, "x");
	pos.X = lua_tonumber(L, -1);
	lua_pop(L, 1);
	lua_getfield(L, -1, "y");
	pos.Y = lua_tonumber(L, -1);
	lua_pop(L, 1);
	lua_getfield(L, -1, "z");
	pos.Z = lua_tonumber(L, -1);
	lua_pop(L, 1);
	lua_pop(L, 1); // Pop pos
	pos *= BS; // Scale to internal format
	return pos;
}

/*
	Global functions
*/

// Register new object prototype
// register_entity(name, prototype)
static int l_register_entity(lua_State *L)
{
	const char *name = luaL_checkstring(L, 1);
	infostream<<"register_entity: "<<name<<std::endl;
	luaL_checktype(L, 2, LUA_TTABLE);

	// Get minetest.registered_entities
	lua_getglobal(L, "minetest");
	lua_getfield(L, -1, "registered_entities");
	luaL_checktype(L, -1, LUA_TTABLE);
	int registered_entities = lua_gettop(L);
	lua_pushvalue(L, 2); // Object = param 2 -> stack top
	// registered_entities[name] = object
	lua_setfield(L, registered_entities, name);
	
	// Get registered object to top of stack
	lua_pushvalue(L, 2);
	
	// Set __index to point to itself
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "__index");

	// Set metatable.__index = metatable
	luaL_getmetatable(L, "minetest.entity");
	lua_pushvalue(L, -1); // duplicate metatable
	lua_setfield(L, -2, "__index");
	// Set object metatable
	lua_setmetatable(L, -2);

	return 0; /* number of results */
}

// Register a global step function
// register_globalstep(function)
static int l_register_globalstep(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TFUNCTION);
	infostream<<"register_globalstep"<<std::endl;

	lua_getglobal(L, "table");
	lua_getfield(L, -1, "insert");
	int table_insert = lua_gettop(L);
	// Get minetest.registered_globalsteps
	lua_getglobal(L, "minetest");
	lua_getfield(L, -1, "registered_globalsteps");
	luaL_checktype(L, -1, LUA_TTABLE);
	int registered_globalsteps = lua_gettop(L);
	// table.insert(registered_globalsteps, func)
	lua_pushvalue(L, table_insert);
	lua_pushvalue(L, registered_globalsteps);
	lua_pushvalue(L, 1); // push function from argument 1
	// Call insert
	if(lua_pcall(L, 2, 0, 0))
		script_error(L, "error: %s\n", lua_tostring(L, -1));

	return 0; /* number of results */
}

#if 0
// Clear all registered tools
// deregister_tools()
static int l_deregister_tools(lua_State *L)
{
	infostream<<"deregister_tools"<<std::endl;

	// Get server from registry
	lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
	Server *server = (Server*)lua_touserdata(L, -1);
	// And get the writable tool definition manager from the server
	IWritableToolDefManager *tooldef =
			server->getWritableToolDefManager();
	
	tooldef->clear();

	return 0; /* number of results */
}
#endif

// register_tool(name, {lots of stuff})
static int l_register_tool(lua_State *L)
{
	const char *name = luaL_checkstring(L, 1);
	infostream<<"register_tool: "<<name<<std::endl;
	luaL_checktype(L, 2, LUA_TTABLE);

	// Get server from registry
	lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
	Server *server = (Server*)lua_touserdata(L, -1);
	// And get the writable tool definition manager from the server
	IWritableToolDefManager *tooldef =
			server->getWritableToolDefManager();
	
	int table = 2;
	
	ToolDefinition def;

	lua_getfield(L, table, "image");
	if(lua_isstring(L, -1))
		def.imagename = lua_tostring(L, -1);
	lua_pop(L, 1);
	
	lua_getfield(L, table, "basetime");
	def.properties.basetime = lua_tonumber(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, table, "dt_weight");
	def.properties.dt_weight = lua_tonumber(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, table, "dt_crackiness");
	def.properties.dt_crackiness = lua_tonumber(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, table, "dt_crumbliness");
	def.properties.dt_crumbliness = lua_tonumber(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, table, "dt_cuttability");
	def.properties.dt_cuttability = lua_tonumber(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, table, "basedurability");
	def.properties.basedurability = lua_tonumber(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, table, "dd_weight");
	def.properties.dd_weight = lua_tonumber(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, table, "dd_crackiness");
	def.properties.dd_crackiness = lua_tonumber(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, table, "dd_crumbliness");
	def.properties.dd_crumbliness = lua_tonumber(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, table, "dd_cuttability");
	def.properties.dd_cuttability = lua_tonumber(L, -1);
	lua_pop(L, 1);

	tooldef->registerTool(name, def);

	return 0; /* number of results */
}

static const struct luaL_Reg minetest_f [] = {
	{"register_entity", l_register_entity},
	{"register_globalstep", l_register_globalstep},
	//{"deregister_tools", l_deregister_tools},
	{"register_tool", l_register_tool},
	{NULL, NULL}
};

/*
	LuaEntity functions
*/

static const struct luaL_Reg minetest_entity_m [] = {
	{NULL, NULL}
};

/*
	Getters for stuff in main tables
*/

static void objectref_get(lua_State *L, u16 id)
{
	// Get minetest.object_refs[i]
	lua_getglobal(L, "minetest");
	lua_getfield(L, -1, "object_refs");
	luaL_checktype(L, -1, LUA_TTABLE);
	lua_pushnumber(L, id);
	lua_gettable(L, -2);
	lua_remove(L, -2); // object_refs
	lua_remove(L, -2); // minetest
}

static void luaentity_get(lua_State *L, u16 id)
{
	// Get minetest.luaentities[i]
	lua_getglobal(L, "minetest");
	lua_getfield(L, -1, "luaentities");
	luaL_checktype(L, -1, LUA_TTABLE);
	lua_pushnumber(L, id);
	lua_gettable(L, -2);
	lua_remove(L, -2); // luaentities
	lua_remove(L, -2); // minetest
}

/*
	Reference objects
*/
#define method(class, name) {#name, class::l_##name}

class EnvRef
{
private:
	ServerEnvironment *m_env;

	static const char className[];
	static const luaL_reg methods[];

	static EnvRef *checkobject(lua_State *L, int narg)
	{
		luaL_checktype(L, narg, LUA_TUSERDATA);
		void *ud = luaL_checkudata(L, narg, className);
		if(!ud) luaL_typerror(L, narg, className);
		return *(EnvRef**)ud;  // unbox pointer
	}
	
	// Exported functions

	// EnvRef:add_node(pos, content)
	// pos = {x=num, y=num, z=num}
	// content = number
	static int l_add_node(lua_State *L)
	{
		infostream<<"EnvRef::l_add_node()"<<std::endl;
		EnvRef *o = checkobject(L, 1);
		ServerEnvironment *env = o->m_env;
		if(env == NULL) return 0;
		// pos
		v3s16 pos;
		lua_pushvalue(L, 2); // Push pos
		luaL_checktype(L, -1, LUA_TTABLE);
		lua_getfield(L, -1, "x");
		pos.X = lua_tonumber(L, -1);
		lua_pop(L, 1);
		lua_getfield(L, -1, "y");
		pos.Y = lua_tonumber(L, -1);
		lua_pop(L, 1);
		lua_getfield(L, -1, "z");
		pos.Z = lua_tonumber(L, -1);
		lua_pop(L, 1);
		lua_pop(L, 1); // Pop pos
		// content
		u16 content = 0;
		lua_pushvalue(L, 3); // Push content
		content = lua_tonumber(L, -1);
		lua_pop(L, 1); // Pop content
		// Do it
		env->getMap().addNodeWithEvent(pos, MapNode(content));
		return 0;
	}

	static int gc_object(lua_State *L) {
		EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
		delete o;
		return 0;
	}

public:
	EnvRef(ServerEnvironment *env):
		m_env(env)
	{
		infostream<<"EnvRef created"<<std::endl;
	}

	~EnvRef()
	{
		infostream<<"EnvRef destructing"<<std::endl;
	}

	// Creates an EnvRef and leaves it on top of stack
	// Not callable from Lua; all references are created on the C side.
	static void create(lua_State *L, ServerEnvironment *env)
	{
		EnvRef *o = new EnvRef(env);
		//infostream<<"EnvRef::create: o="<<o<<std::endl;
		*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
		luaL_getmetatable(L, className);
		lua_setmetatable(L, -2);
	}

	static void set_null(lua_State *L)
	{
		EnvRef *o = checkobject(L, -1);
		o->m_env = NULL;
	}
	
	static void Register(lua_State *L)
	{
		lua_newtable(L);
		int methodtable = lua_gettop(L);
		luaL_newmetatable(L, className);
		int metatable = lua_gettop(L);

		lua_pushliteral(L, "__metatable");
		lua_pushvalue(L, methodtable);
		lua_settable(L, metatable);  // hide metatable from Lua getmetatable()

		lua_pushliteral(L, "__index");
		lua_pushvalue(L, methodtable);
		lua_settable(L, metatable);

		lua_pushliteral(L, "__gc");
		lua_pushcfunction(L, gc_object);
		lua_settable(L, metatable);

		lua_pop(L, 1);  // drop metatable

		luaL_openlib(L, 0, methods, 0);  // fill methodtable
		lua_pop(L, 1);  // drop methodtable

		// Cannot be created from Lua
		//lua_register(L, className, create_object);
	}
};
const char EnvRef::className[] = "EnvRef";
const luaL_reg EnvRef::methods[] = {