summaryrefslogtreecommitdiff
path: root/src/serverobject.h
blob: 557d9b6186e13a870b89f4c44d59731f102d015b (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
/*
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.
*/

#ifndef SERVEROBJECT_HEADER
#define SERVEROBJECT_HEADER

#include "common_irrlicht.h"
#include "activeobject.h"
#include "utility.h"

/*

Some planning
-------------

* Server environment adds an active object, which gets the id 1
* The active object list is scanned for each client once in a while,
  and it finds out what objects have been added that are not known
  by the client yet. This scan is initiated by the server and the
  result ends up directly to the server.
* A network packet is created with the info and sent to the client.

*/

class ServerEnvironment;

class ServerActiveObject : public ActiveObject
{
public:
	ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos=v3f(0,0,0));
	virtual ~ServerActiveObject();

	v3f getBasePosition()
	{
		return m_base_position;
	}
	
	void setBasePosition(v3f pos)
	{
		m_base_position = pos;
	}

	ServerEnvironment* getEnv()
	{
		return m_env;
	}
	
	/*
		Step object in time.
		Messages added to messages are sent to client over network.
	*/
	virtual void step(float dtime, Queue<ActiveObjectMessage> &messages){}
	
	/*
		The return value of this is passed to the client-side object
		when it is created
	*/
	virtual std::string getClientInitializationData(){return "";}
	
	/*
		The return value of this is passed to the server-side object
		when it is loaded from disk or from a static object
	*/
	virtual std::string getServerInitializationData(){return "";}
	
	/*
		This takes the return value of getServerInitializationData
	*/
	virtual void initialize(const std::string &data){}
	
	// Number of players which know about this object
	u16 m_known_by_count;
	/*
		Whether this object is to be removed when nobody knows about
		it anymore.
		Removal is delayed to preserve the id for the time during which
		it could be confused to some other object by some client.
	*/
	bool m_removed;
	
protected:
	ServerEnvironment *m_env;
	v3f m_base_position;
};

class TestSAO : public ServerActiveObject
{
public:
	TestSAO(ServerEnvironment *env, u16 id, v3f pos);
	u8 getType() const
	{
		return ACTIVEOBJECT_TYPE_TEST;
	}
	void step(float dtime, Queue<ActiveObjectMessage> &messages);
private:
	float m_timer1;
	float m_age;
};

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

class LuaSAO : public ServerActiveObject
{
public:
	LuaSAO(ServerEnvironment *env, u16 id, v3f pos);
	virtual ~LuaSAO();

	u8 getType() const
	{
		return ACTIVEOBJECT_TYPE_LUA;
	}

	virtual std::string getClientInitializationData();
	virtual std::string getServerInitializationData();
	
	void initializeFromNothing(const std::string &script_name);
	void initializeFromSave(const std::string &data);
	
	void loadScripts(const std::string &script_name);

	void step(float dtime, Queue<ActiveObjectMessage> &messages);

	/*
		Stuff available for usage for the lua callbacks
	*/
	// This is moved onwards at the end of step()
	Queue<ActiveObjectMessage> m_message_queue;

private:
	lua_State* L;
	std::string m_script_name;
};

#endif