Enable client-side attachments, add detachment code
[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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 "irrlichttypes_extrabloated.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 ClientEnvironment;
39 class ITextureSource;
40 class IGameDef;
41 class LocalPlayer;
42 struct ItemStack;
43
44 class ClientActiveObject : public ActiveObject
45 {
46 public:
47         ClientActiveObject(u16 id, IGameDef *gamedef, ClientEnvironment *env);
48         virtual ~ClientActiveObject();
49
50         virtual void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
51                         IrrlichtDevice *irr){}
52         virtual void removeFromScene(){}
53         // 0 <= light_at_pos <= LIGHT_SUN
54         virtual void updateLight(u8 light_at_pos){}
55         virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
56         virtual core::aabbox3d<f32>* getSelectionBox(){return NULL;}
57         virtual core::aabbox3d<f32>* getCollisionBox(){return NULL;}
58         virtual v3f getPosition(){return v3f(0,0,0);}
59         virtual scene::IMeshSceneNode *getMeshSceneNode(){return NULL;}
60         virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode(){return NULL;}
61         virtual scene::IBillboardSceneNode *getSpriteSceneNode(){return NULL;}
62         virtual bool isPlayer(){return false;}
63         virtual bool isLocalPlayer(){return false;}
64         virtual bool doShowSelectionBox(){return true;}
65         
66         // Step object in time
67         virtual void step(float dtime, ClientEnvironment *env){}
68         
69         // Process a message sent by the server side object
70         virtual void processMessage(const std::string &data){}
71
72         virtual std::string infoText() {return "";}
73         virtual std::string debugInfoText() {return "";}
74         
75         /*
76                 This takes the return value of
77                 ServerActiveObject::getClientInitializationData
78         */
79         virtual void initialize(const std::string &data){}
80         
81         // Create a certain type of ClientActiveObject
82         static ClientActiveObject* create(u8 type, IGameDef *gamedef,
83                         ClientEnvironment *env);
84
85         // If returns true, punch will not be sent to the server
86         virtual bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
87                         float time_from_last_punch=1000000)
88         { return false; }
89
90 protected:
91         // Used for creating objects based on type
92         typedef ClientActiveObject* (*Factory)(IGameDef *gamedef, ClientEnvironment *env);
93         static void registerType(u16 type, Factory f);
94         IGameDef *m_gamedef;
95         ClientEnvironment *m_env;
96 private:
97         // Used for creating objects based on type
98         static core::map<u16, Factory> m_types;
99 };
100
101 struct DistanceSortedActiveObject
102 {
103         ClientActiveObject *obj;
104         f32 d;
105
106         DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
107         {
108                 obj = a_obj;
109                 d = a_d;
110         }
111
112         bool operator < (DistanceSortedActiveObject &other)
113         {
114                 return d < other.d;
115         }
116 };
117
118 #endif
119