SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Entity.h
Go to the documentation of this file.
1 /*
2  * SEGS - Super Entity Game Server
3  * http://www.segs.io/
4  * Copyright (c) 2006 - 2018 SEGS Team (see Authors.txt)
5  * This software is licensed! (See License.txt for details)
6  */
7 
8 #pragma once
9 #include "CommonNetStructures.h"
10 #include "Powers.h"
11 #include "Costume.h"
12 #include "Team.h"
13 #include "FixedPointValue.h"
16 
17 #include <glm/gtx/quaternion.hpp>
18 #include <glm/gtc/constants.hpp>
19 
20 #include <array>
21 #include <memory>
22 
23 struct MapClientSession;
24 class Team;
25 class Character;
26 struct PlayerData;
27 using Parse_AllKeyProfiles = std::vector<struct Keybind_Profiles>;
28 
29 struct AngleRadians // TODO: Is this intended to be used?
30 {
31  static AngleRadians fromDeg(float deg) { return AngleRadians(deg*float(M_PI)/180.0f);}
32  float toDeg() { return AngleRadians((v*180.0f)/ float(M_PI)).v;}
33  explicit AngleRadians(float x=0.0f) : v(x) {}
35  {
36  AngleRadians result(v);
37  return result-=ot;
38  }
40  return AngleRadians(-v);
41  }
42  float operator/(AngleRadians &other) const {
43  return v/other.v;
44  }
46  {
47  AngleRadians result(v);
48  result+=ot;
49  result.fixup();
50  return result;
51  }
52  AngleRadians operator*(float scale) const
53  {
54  return AngleRadians(v*scale);
55  }
56  AngleRadians &operator*=(float scale)
57  {
58  v*=scale;
59  return *this;
60  }
62  {
63  v += ot.v;
64  fixup();
65  return *this;
66  }
68  {
69  v -= ot.v;
70  fixup();
71  return *this;
72  }
73  bool operator==(float other) const { return v == other; }
74  bool operator==(const AngleRadians &other) const { return v == other.v; }
75  bool operator!=(const AngleRadians &other) const { return v != other.v; }
77  if ( v > glm::pi<float>())
78  v -= glm::two_pi<float>();
79  if ( v <= -glm::pi<float>())
80  v += glm::two_pi<float>();
81  return *this;
82  }
83  bool operator<( const AngleRadians &o) const {
84  return v<o.v;
85  }
86  bool operator>( const AngleRadians &o) const {
87  return v>o.v;
88  }
89  AngleRadians lerp(AngleRadians towards,float factor) const {
90 
91  float v3(towards.v - v);
92  if ( v3 > glm::pi<float>())
93  v3 = v3 - glm::two_pi<float>();
94  if ( v3 <= -glm::pi<float>())
95  v3 = v3 + glm::two_pi<float>();
96  return AngleRadians(v3 * factor + v);
97 
98  }
99  // operator float()
100  // { return v;}
101  float v;
102  int toIntegerForm() const
103  {
104  return int((v + glm::pi<float>()) * 2048.0f / (glm::two_pi<float>()));
105  }
106  float fromIntegerForm(/*int v*/) const
107  {
108  return (float(v)/2048.0f)*(2*M_PI) - M_PI;
109  }
110  explicit operator float() const {
111  return v;
112  }
113 };
114 
116 {
117 public:
118  glm::vec3 posvec;
120  //glm::quat quat;
122 };
123 
125 {
126 public:
128  {
129  for(int i=0; i<3; ++i)
130  {
131  pos_delta_valid[i]=false;
132  pyr_valid[i]=false;
133  }
134  }
135 
136  uint8_t m_csc_deltabits = 0;
137  bool m_send_deltas = 0;
138  uint16_t controlBits = 0;
139  uint16_t send_id = 0;
140  void *current_state_P = nullptr;
141  glm::vec3 camera_pyr;
142  glm::vec3 m_orientation_pyr; // Stored in Radians
143  glm::quat m_direction;
144  int m_time_diff1 = 0;
145  int m_time_diff2 = 0;
146  uint8_t input_vel_scale = 0; // TODO: Should be float?
148  bool m_no_coll = false;
150  bool pos_delta_valid[3] = {};
151  bool pyr_valid[3] = {};
152  glm::vec3 pos_delta;
153  bool m_controls_disabled = false;
154 
156  void processDirectionControl(int dir, int prev_time, int press_release);
157 };
158 
159 enum class FadeDirection
160 {
161  In,
162  Out
163 };
164 
165 // returned by getEntityFromDB()
167 {
168  QString name;
171  float hitpoints;
172  float endurance;
173  uint32_t sg_id;
174  uint32_t m_db_id;
175 };
176 
177 enum class EntType : uint8_t
178 {
179  Invalid = 0,
180  NPC = 1,
181  PLAYER = 2,
182  HERO = 3,
183  CRITTER = 4,
184  CAR = 5,
185  DELIVERYTARGET = 6,
186  MOBILEGEOMETRY = 7,
187  MISSION_ITEM = 8,
188  MAPXFERDOOR = 9,
189  DOOR = 10,
190  COUNT = 11
191 };
192 
193 enum class AppearanceType : uint8_t
194 {
195  None = 0,
196  WholeCostume = 1,
197  NpcCostume = 2,
198  VillainIndex = 3,
199  SequencerName = 4
200 };
201 
203 {
204  int m_SG_id = {0};
205  QString m_SG_name = "Supergroup"; // 64 chars max
206  //QString m_SG_motto;
207  //QString m_SG_costume; // 128 chars max -> hash table key from the CostumeString_HTable
208  uint32_t m_SG_color1 = 0; // supergroup color 1
209  uint32_t m_SG_color2 = 0; // supergroup color 2
210  int m_SG_rank = 1;
211 };
212 
213 struct NPCData
214 {
215  bool m_is_owned = false;
216  const struct Parse_NPC *src_data;
217  int npc_idx=0;
219 };
220 
221 class Entity
222 {
223  // only EntityStore can create instances of this class
224  friend class EntityStore;
225  friend std::array<Entity,10240>;
226  using CharacterPtr = std::unique_ptr<Character>;
227  using PlayerPtr = std::unique_ptr<PlayerData>;
228  using NPCPtr = std::unique_ptr<NPCData>;
229 private:
230  Entity();
231 virtual ~Entity();
232 public:
234  {
235  glm::vec3 pos;
236  glm::vec3 pyr; //TODO: convert to quat
237  };
239  // Some entities might not have a character data ( doors, cars )
240  // Making it an unique_ptr<Character> makes it clear that Entity 'owns'
241  // and takes care of this data, at the same time it can be missing
242  CharacterPtr m_char;
243  // And not all entities are players
244  PlayerPtr m_player;
245  NPCPtr m_npc;
246 
247 
248  bool m_has_supergroup = true;
249  SuperGroup m_supergroup; // client has this in entity class, but maybe move to Character class?
250  bool m_has_team = false;
251  Team * m_team = nullptr; // we might want t move this to Character class, but maybe Baddies use teams?
253 
254  uint32_t m_idx = {0};
255  uint32_t m_db_id = {0};
257  glm::quat m_direction;
258  glm::vec3 m_spd = {1,1,1};
259  uint32_t m_target_idx = 0;
260  uint32_t m_assist_target_idx = 0;
261 
262  int m_randSeed = 0; // Sequencer uses this as a seed for random bone scale
263  int m_num_fx = 0;
264  bool m_is_logging_out = false;
265  int m_time_till_logout = 0; // time in miliseconds untill given entity should be marked as logged out.
266  std::vector<uint8_t> m_fx1;
267  std::vector<uint32_t> m_fx2;
268  std::vector<uint8_t> m_fx3;
270  int m_state_mode = 0;
271  bool m_state_mode_send = false;
272  bool m_odd_send = false;
273  bool m_seq_update = false;
274  bool m_is_hero = false;
275  bool m_is_villian = false;
276  bool m_contact = false;
277  int m_seq_upd_num1 = 0;
278  int m_seq_upd_num2 = 0;
279  bool m_is_flying = false;
280  bool m_is_stunned = false;
281  bool m_is_jumping = false;
282  bool m_is_sliding = false;
283  bool m_is_falling = false;
284  bool m_has_jumppack = false;
285  bool m_controls_disabled = false;
286  float m_backup_spd = 1.0f;
287  float m_jump_height = 2.0f;
288  uint8_t m_update_id = 1;
289  bool m_update_part_1 = true; // EntityResponse sendServerControlState
290  bool m_force_pos_and_cam = true; // EntityResponse sendServerControlState
291  bool m_full_update = true; // EntityReponse sendServerPhysicsPositions
292  bool m_has_control_id = true; // EntityReponse sendServerPhysicsPositions
293 
294  int u3 = 0;
295 
297  size_t m_update_idx = 0;
298  std::vector<PosUpdate> interpResults;
299  bool entReceiveAlwaysCon = false;
301  int pkt_id_QrotUpdateVal[3] = {0};
302  glm::vec3 vel;
303  uint32_t prev_pos[3] = {0};
305  bool m_pchar_things = false;
306  bool might_have_rare = false;
307  bool m_hasname = false;
308  bool m_classname_override = false;
309  bool m_hasRagdoll = false;
310  bool m_has_owner = false;
311  bool m_create_player = false;
312  bool m_rare_bits = false;
315  uint32_t m_input_ack = {0};
316  bool player_type = false;
317  bool m_destroyed = false;
318  uint32_t ownerEntityId = 0;
319  uint32_t creatorEntityId = 0;
320  float translucency = 1.f;
321  bool m_is_fading = true;
324  uint32_t m_db_store_flags = 0;
325 
326  void dump();
327  void addPosUpdate(const PosUpdate &p);
328  void addInterp(const PosUpdate &p);
329 
330 static void sendAllyID(BitStream &bs);
331 static void sendPvP(BitStream &bs);
332 
333  bool update_rot(int axis) const; // returns true if given axis needs updating;
334 
335  void InsertUpdate(PosUpdate pup);
336  const QString & name() const;
337  void fillFromCharacter();
338  void beginLogout(uint16_t time_till_logout=10); // Default logout time is 10 s
339 };
340 
341 enum class DbStoreFlags : uint32_t
342 {
343  Gui = 1,
344  Options = 2,
345  Keybinds = 4,
346  PlayerData = 7,
347  Full = ~0U,
348 };
349 
352 void initializeNewNpcEntity(Entity &e, const Parse_NPC *src, int idx, int variant);
353 void fillEntityFromNewCharData(Entity &e, BitStream &src, const ColorAndPartPacker *packer, const Parse_AllKeyProfiles &default_profiles);
354 extern void abortLogout(Entity *e);
void InsertUpdate(PosUpdate pup)
Definition: Entity.cpp:90
The ColorAndPartPacker class is responsible for packing/unpacking colors and part names from BitStrea...
Definition: CommonNetStructures.h:34
Definition: Entity.h:29
AngleRadians operator+(const AngleRadians &ot) const
Definition: Entity.h:45
bool m_is_logging_out
Definition: Entity.h:264
void addInterp(const PosUpdate &p)
Definition: Entity.cpp:138
static void sendPvP(BitStream &bs)
Definition: Entity.cpp:37
int m_seq_upd_num2
Definition: Entity.h:278
NPCPtr m_npc
Definition: Entity.h:245
AngleRadians operator-(const AngleRadians &ot) const
Definition: Entity.h:34
bool m_send_deltas
Definition: Entity.h:137
Definition: MapClientSession.h:32
bool has_input_commit_guess
Definition: Entity.h:149
bool m_contact
Definition: Entity.h:276
bool m_is_owned
Definition: Entity.h:215
void initializeNewNpcEntity(Entity &e, const Parse_NPC *src, int idx, int variant)
Definition: Entity.cpp:178
Definition: Entity.h:115
int m_state_mode
Definition: Entity.h:270
bool m_has_team
Definition: Entity.h:250
MapClientSession * m_client
Definition: Entity.h:322
bool m_update_part_1
Definition: Entity.h:289
uint32_t m_target_idx
Definition: Entity.h:259
bool m_controls_disabled
Definition: Entity.h:153
uint32_t m_assist_target_idx
Definition: Entity.h:260
AngleRadians operator-() const
Definition: Entity.h:39
Definition: Character.h:55
bool m_classname_override
Definition: Entity.h:308
AngleRadians operator*(float scale) const
Definition: Entity.h:52
bool m_is_falling
Definition: Entity.h:283
int m_randSeed
Definition: Entity.h:262
bool m_destroyed
Definition: Entity.h:317
bool m_odd_send
Definition: Entity.h:272
bool operator>(const AngleRadians &o) const
Definition: Entity.h:86
Definition: Entity.h:202
bool operator!=(const AngleRadians &other) const
Definition: Entity.h:75
static AngleRadians fromDeg(float deg)
Definition: Entity.h:31
Definition: Team.h:13
uint32_t m_idx
Definition: Entity.h:254
void processDirectionControl(int dir, int prev_time, int press_release)
Definition: InputState.cpp:81
bool update_rot(int axis) const
SuperGroup m_supergroup
Definition: Entity.h:249
int npc_idx
Definition: Entity.h:217
Definition: Entity.h:213
bool m_is_villian
Definition: Entity.h:275
bool m_is_flying
Definition: Entity.h:279
int pkt_id_QrotUpdateVal[3]
Definition: Entity.h:301
std::vector< uint8_t > m_fx3
Definition: Entity.h:268
Definition: BitStream.h:30
Definition: entitydata_definitions.h:14
Definition: playerdata_definitions.h:17
bool m_has_control_id
Definition: Entity.h:292
uint32_t creatorEntityId
Definition: Entity.h:319
bool m_state_mode_send
Definition: Entity.h:271
QString m_override_name
Definition: Entity.h:314
std::vector< uint8_t > m_fx1
Definition: Entity.h:266
bool pos_delta_valid[3]
Definition: Entity.h:150
Vector3_FPV fixedpoint_pos
Definition: Entity.h:304
uint32_t m_db_id
Definition: Entity.h:174
uint16_t controlBits
Definition: Entity.h:138
void dump()
Definition: Entity.cpp:101
int m_num_fx
Definition: Entity.h:263
InputStateStorage & operator=(const InputStateStorage &other)
Definition: InputState.cpp:43
int m_SG_rank
Definition: Entity.h:210
void addPosUpdate(const PosUpdate &p)
Definition: Entity.cpp:133
float m_backup_spd
Definition: Entity.h:286
AngleRadians & operator*=(float scale)
Definition: Entity.h:56
uint8_t input_vel_scale
Definition: Entity.h:146
uint16_t send_id
Definition: Entity.h:139
int m_SG_id
Definition: Entity.h:204
uint32_t m_SG_color2
Definition: Entity.h:209
DbStoreFlags
Definition: Entity.h:341
size_t m_update_idx
Definition: Entity.h:297
bool m_is_stunned
Definition: Entity.h:280
glm::vec3 m_orientation_pyr
Definition: Entity.h:142
float endurance
Definition: Entity.h:172
bool operator<(const AngleRadians &o) const
Definition: Entity.h:83
uint32_t m_db_id
Definition: Entity.h:255
void fillEntityFromNewCharData(Entity &e, BitStream &src, const ColorAndPartPacker *packer, const Parse_AllKeyProfiles &default_profiles)
Definition: Entity.cpp:65
QString m_SG_name
Definition: Entity.h:205
uint8_t m_update_id
Definition: Entity.h:288
Definition: Entity.h:166
float v
Definition: Entity.h:101
int m_timestamp
Definition: Entity.h:121
bool m_has_owner
Definition: Entity.h:310
bool m_force_pos_and_cam
Definition: Entity.h:290
PlayerPtr m_player
Definition: Entity.h:244
void beginLogout(uint16_t time_till_logout=10)
Definition: Entity.cpp:57
float translucency
Definition: Entity.h:320
float hitpoints
Definition: Entity.h:171
CharacterPtr m_char
Definition: Entity.h:242
bool player_type
Definition: Entity.h:316
void * current_state_P
Definition: Entity.h:140
AngleRadians & operator+=(const AngleRadians &ot)
Definition: Entity.h:61
bool entReceiveSeeThroughWalls
Definition: Entity.h:300
CharacterData char_data
Definition: Entity.h:170
bool entReceiveAlwaysCon
Definition: Entity.h:299
bool m_no_coll
Definition: Entity.h:148
int u3
Definition: Entity.h:294
int m_seq_upd_num1
Definition: Entity.h:277
bool pyr_valid[3]
Definition: Entity.h:151
uint8_t m_received_server_update_id
Definition: Entity.h:147
bool operator==(const AngleRadians &other) const
Definition: Entity.h:74
AppearanceType
Definition: Entity.h:193
EntityData m_entity_data
Definition: Entity.h:252
Team * m_team
Definition: Entity.h:251
float toDeg()
Definition: Entity.h:32
Definition: npc_definitions.h:57
float operator/(AngleRadians &other) const
Definition: Entity.h:42
Definition: Entity.h:221
int costume_variant
Definition: Entity.h:218
uint32_t m_SG_color1
Definition: Entity.h:208
AngleRadians & fixup()
Definition: Entity.h:76
FadeDirection m_fading_direction
Definition: Entity.h:323
bool m_has_jumppack
Definition: Entity.h:284
Definition: FixedPointValue.h:28
bool m_create_player
Definition: Entity.h:311
bool might_have_rare
Definition: Entity.h:306
std::vector< Keybind_Profiles > Parse_AllKeyProfiles
Definition: keybind_definitions.h:472
bool operator==(float other) const
Definition: Entity.h:73
uint32_t prev_pos[3]
Definition: Entity.h:303
struct Parse_NPC * src_data
Definition: Entity.h:216
void abortLogout(Entity *e)
Definition: Entity.cpp:151
InputStateStorage inp_state
Definition: Entity.h:238
uint32_t sg_id
Definition: Entity.h:173
int m_time_diff2
Definition: Entity.h:145
AngleRadians lerp(AngleRadians towards, float factor) const
Definition: Entity.h:89
FadeDirection
Definition: Entity.h:159
bool m_is_fading
Definition: Entity.h:321
AngleRadians(float x=0.0f)
Definition: Entity.h:33
int current_client_packet_id
Definition: Entity.h:313
uint32_t ownerEntityId
Definition: Entity.h:318
EntType
Definition: Entity.h:177
bool m_hasRagdoll
Definition: Entity.h:309
EntType m_type
Definition: Entity.h:256
glm::quat m_direction
Definition: Entity.h:143
Definition: Entity.h:124
uint32_t m_input_ack
Definition: Entity.h:315
glm::vec3 pos
Definition: Entity.h:235
bool m_is_sliding
Definition: Entity.h:282
int toIntegerForm() const
Definition: Entity.h:102
glm::quat m_direction
Definition: Entity.h:257
bool m_hasname
Definition: Entity.h:307
glm::vec3 vel
Definition: Entity.h:302
bool m_is_hero
Definition: Entity.h:274
bool m_full_update
Definition: Entity.h:291
EntityData entity_data
Definition: Entity.h:169
float fromIntegerForm() const
Definition: Entity.h:106
AppearanceType m_costume_type
Definition: Entity.h:269
InputStateStorage()
Definition: Entity.h:127
Definition: EntityStorage.h:24
glm::vec3 pos_delta
Definition: Entity.h:152
std::vector< PosUpdate > interpResults
Definition: Entity.h:298
QString name
Definition: Entity.h:168
uint8_t m_csc_deltabits
Definition: Entity.h:136
glm::vec3 camera_pyr
Definition: Entity.h:141
glm::vec3 posvec
Definition: Entity.h:118
glm::vec3 m_spd
Definition: Entity.h:258
float m_jump_height
Definition: Entity.h:287
void initializeNewPlayerEntity(Entity &e)
Definition: Entity.cpp:157
std::vector< uint32_t > m_fx2
Definition: Entity.h:267
uint32_t m_db_store_flags
Definition: Entity.h:324
void fillFromCharacter()
Definition: Entity.cpp:45
bool m_is_jumping
Definition: Entity.h:281
AngleRadians & operator-=(const AngleRadians &ot)
Definition: Entity.h:67
const QString & name() const
Definition: Entity.cpp:97
Definition: Entity.h:233
int m_time_till_logout
Definition: Entity.h:265
bool m_controls_disabled
Definition: Entity.h:285
PosUpdate m_pos_updates[64]
Definition: Entity.h:296
bool m_seq_update
Definition: Entity.h:273
bool m_pchar_things
Definition: Entity.h:305
bool m_rare_bits
Definition: Entity.h:312
AngleRadians PitchYawRoll[3]
Definition: Entity.h:119
bool m_has_supergroup
Definition: Entity.h:248
Definition: chardata_definitions.h:29
int m_time_diff1
Definition: Entity.h:144
void markEntityForDbStore(Entity *e, DbStoreFlags f)
Definition: Entity.cpp:199
glm::vec3 pyr
Definition: Entity.h:236
static void sendAllyID(BitStream &bs)
Definition: Entity.cpp:31