Performance fix + SAO factorization
[oweals/minetest.git] / src / clientobject.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 #include <map>
26 #include "util/cpp11_container.h"
27
28 class ClientEnvironment;
29 class ITextureSource;
30 class Client;
31 class IGameDef;
32 class LocalPlayer;
33 struct ItemStack;
34 class WieldMeshSceneNode;
35
36 class ClientActiveObject : public ActiveObject
37 {
38 public:
39         ClientActiveObject(u16 id, Client *client, ClientEnvironment *env);
40         virtual ~ClientActiveObject();
41
42         virtual void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
43                         IrrlichtDevice *irr){}
44         virtual void removeFromScene(bool permanent){}
45         // 0 <= light_at_pos <= LIGHT_SUN
46         virtual void updateLight(u8 light_at_pos){}
47         virtual void updateLightNoCheck(u8 light_at_pos){}
48         virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
49         virtual aabb3f *getSelectionBox() { return NULL; }
50         virtual bool getCollisionBox(aabb3f *toset) const { return false; }
51         virtual bool collideWithObjects() const { return false; }
52         virtual v3f getPosition(){return v3f(0,0,0);}
53         virtual float getYaw() const {return 0;}
54         virtual scene::ISceneNode *getSceneNode(){return NULL;}
55         virtual scene::IMeshSceneNode *getMeshSceneNode(){return NULL;}
56         virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode(){return NULL;}
57         virtual WieldMeshSceneNode *getWieldMeshSceneNode(){return NULL;}
58         virtual scene::IBillboardSceneNode *getSpriteSceneNode(){return NULL;}
59         virtual bool isPlayer() const {return false;}
60         virtual bool isLocalPlayer() const {return false;}
61         virtual void setAttachments(){}
62         virtual bool doShowSelectionBox(){return true;}
63         virtual void updateCameraOffset(v3s16 camera_offset){};
64
65         // Step object in time
66         virtual void step(float dtime, ClientEnvironment *env){}
67
68         // Process a message sent by the server side object
69         virtual void processMessage(const std::string &data){}
70
71         virtual std::string infoText() {return "";}
72         virtual std::string debugInfoText() {return "";}
73
74         /*
75                 This takes the return value of
76                 ServerActiveObject::getClientInitializationData
77         */
78         virtual void initialize(const std::string &data){}
79
80         // Create a certain type of ClientActiveObject
81         static ClientActiveObject* create(ActiveObjectType type, Client *client,
82                         ClientEnvironment *env);
83
84         // If returns true, punch will not be sent to the server
85         virtual bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
86                         float time_from_last_punch=1000000)
87         { return false; }
88
89 protected:
90         // Used for creating objects based on type
91         typedef ClientActiveObject* (*Factory)(Client *client, ClientEnvironment *env);
92         static void registerType(u16 type, Factory f);
93         Client *m_client;
94         ClientEnvironment *m_env;
95 private:
96         // Used for creating objects based on type
97         static UNORDERED_MAP<u16, Factory> m_types;
98 };
99
100 struct DistanceSortedActiveObject
101 {
102         ClientActiveObject *obj;
103         f32 d;
104
105         DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
106         {
107                 obj = a_obj;
108                 d = a_d;
109         }
110
111         bool operator < (const DistanceSortedActiveObject &other) const
112         {
113                 return d < other.d;
114         }
115 };
116
117 #endif