preliminary lua scripting framework for objects
[oweals/minetest.git] / src / clientobject.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef CLIENTOBJECT_HEADER
21 #define CLIENTOBJECT_HEADER
22
23 #include "common_irrlicht.h"
24 #include "activeobject.h"
25
26 /*
27
28 Some planning
29 -------------
30
31 * Client receives a network packet with information of added objects
32   in it
33 * Client supplies the information to its ClientEnvironment
34 * The environment adds the specified objects to itself
35
36 */
37
38 class ClientActiveObject : public ActiveObject
39 {
40 public:
41         ClientActiveObject(u16 id);
42         virtual ~ClientActiveObject();
43
44         virtual void addToScene(scene::ISceneManager *smgr){}
45         virtual void removeFromScene(){}
46         // 0 <= light_at_pos <= LIGHT_SUN
47         virtual void updateLight(u8 light_at_pos){}
48         virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
49         
50         // Step object in time
51         virtual void step(float dtime){}
52         
53         // Process a message sent by the server side object
54         virtual void processMessage(const std::string &data){}
55
56         /*
57                 This takes the return value of getClientInitializationData
58                 TODO: Usage of this
59         */
60         virtual void initialize(const std::string &data){}
61         
62         // Create a certain type of ClientActiveObject
63         static ClientActiveObject* create(u8 type);
64
65 protected:
66 };
67
68 class TestCAO : public ClientActiveObject
69 {
70 public:
71         TestCAO(u16 id);
72         virtual ~TestCAO();
73         
74         u8 getType() const
75         {
76                 return ACTIVEOBJECT_TYPE_TEST;
77         }
78         
79         void addToScene(scene::ISceneManager *smgr);
80         void removeFromScene();
81         void updateLight(u8 light_at_pos);
82         v3s16 getLightPosition();
83         void updateNodePos();
84
85         void step(float dtime);
86
87         void processMessage(const std::string &data);
88
89 private:
90         scene::IMeshSceneNode *m_node;
91         v3f m_position;
92 };
93
94 extern "C"{
95 #include "lua.h"
96 #include "lualib.h"
97 #include "lauxlib.h"
98 }
99
100 class LuaCAO : public ClientActiveObject
101 {
102 public:
103         LuaCAO(u16 id);
104         virtual ~LuaCAO();
105         
106         u8 getType() const
107         {
108                 return ACTIVEOBJECT_TYPE_LUA;
109         }
110         
111         void step(float dtime);
112
113         void processMessage(const std::string &data);
114
115         void initialize(const std::string &data);
116
117         void loadScript(const std::string script);
118
119         void addToScene(scene::ISceneManager *smgr);
120         void removeFromScene();
121         void updateLight(u8 light_at_pos);
122         v3s16 getLightPosition();
123         void updateNodePos();
124
125         void setPosition(v3f pos);
126         v3f getPosition();
127
128         void setRotation(v3f rot);
129         v3f getRotation();
130         
131         // image: eg. "rat.png"
132         // corners: v3f corners[4]
133         void addToMesh(const char *image, v3f *corners, bool backface_culling);
134         void clearMesh();
135
136 private:
137         lua_State* L;
138         
139         scene::ISceneManager *m_smgr;
140         scene::IMeshSceneNode *m_node;
141         scene::SMesh *m_mesh;
142
143         v3f m_position;
144         v3f m_rotation;
145 };
146
147 #endif
148