- Partly implemented client side prediction (still unstable).
[peakengine.git] / engine / include / core / Entity.h
blob243f658b81bc485e523ae4ae5c26e76324c0b2eb
1 /*
2 Copyright (C) 2009 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef _ENTITY_H_
23 #define _ENTITY_H_
25 #include <string>
26 #include <list>
27 #include <vector>
29 #include "core/Buffer.h"
31 namespace peak
33 class Buffer;
34 class Model;
36 enum VariableType
38 EVT_Int32,
39 EVT_Int16,
40 EVT_Int8,
41 EVT_Float,
42 EVT_Double,
43 EVT_Object3D
46 struct EntityVariable
48 EntityVariable(VariableType type, void *pointer)
50 this->type = type;
51 ptr = pointer;
53 void write(Buffer *buffer);
54 void read(Buffer *buffer);
56 VariableType type;
57 void *ptr;
60 struct EntityState
62 void create(std::vector<EntityVariable> &typeinfo, int time);
63 int timestamp;
64 Buffer data;
67 struct EntityCommand
69 unsigned int timestamp;
70 unsigned short id;
71 Buffer data;
74 class Entity
76 public:
77 Entity();
78 ~Entity();
80 virtual bool init(std::string type);
81 virtual bool destroy(void);
83 bool saveState(EntityState *state);
84 bool restoreState(EntityState *state);
85 bool sendDeltaUpdate(Buffer *buffer, EntityState *from, EntityState *to);
86 bool sendFullUpdate(Buffer *buffer);
87 bool sendDeltaUpdate(Buffer *buffer, int time1, int time2);
88 bool applyDeltaUpdate(Buffer *buffer, EntityState *from, EntityState *to);
89 //bool applyDeltaUpdate(Buffer *buffer, EntityState *state);
90 EntityState *getState(int time);
91 bool checkState(EntityState *state, Buffer *updatebuffer);
93 void discardStates(bool future, bool past);
95 bool save(void);
96 bool reset(int time, bool deletefuture = false);
97 bool rewind(void);
98 bool update(Buffer *buffer);
99 bool injectUpdate(int time, Buffer *buffer, bool apply = false);
100 //void void resetStates(void);
102 void sendCommand(EntityCommand *command);
103 virtual void applyCommand(EntityCommand *command);
104 void applyNextCommand(void);
105 void applyCommand(int time);
107 void setID(int id);
108 int getID(void);
110 virtual void update(float msecs);
112 std::string getType(void);
114 unsigned short getLastCommandID(void)
116 return lastcommandid;
118 unsigned int getLastCommandTime(void)
120 return lastcommandtime;
122 EntityCommand *getCommand(int id)
124 std::list<EntityCommand*>::iterator it = lastcommands.begin();
125 while (it != lastcommands.end())
127 if ((*it)->id == id) return *it;
128 it++;
130 return 0;
133 void finalize(void);
134 protected:
135 void add(Model *model, unsigned int flags = 0);
136 void add(int *variable, unsigned int flags = 0);
137 void add(float *variable, unsigned int flags = 0);
138 void add(unsigned char *variable, unsigned int flags = 0);
139 private:
140 std::list<EntityState*> past;
141 std::list<EntityState*> future;
143 std::vector<EntityVariable> variables;
145 std::list<EntityCommand*> lastcommands;
146 unsigned int lastcommandtime;
147 unsigned short lastcommandid;
149 int id;
150 static unsigned int maxstates;
151 std::string type;
155 #endif