Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Entity.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_SIM2_ENTITY
19 #define INCLUDED_SIM2_ENTITY
20 // (can't call it INCLUDED_ENTITY because that conflicts with simulation/Entity.h)
21 
22 #include "lib/types.h"
23 
24 class IComponent;
25 
26 /**
27  * Entity ID type.
28  * ID numbers are never reused within a simulation run.
29  */
30 typedef u32 entity_id_t;
31 
32 /**
33  * Invalid entity ID. Used as an error return value by some functions.
34  * No valid entity will have this ID.
35  */
37 
38 /**
39  * Entity ID for singleton 'system' components.
40  * Use with QueryInterface to get the component instance.
41  * (This allows those systems to make convenient use of the common infrastructure
42  * for message-passing, scripting, serialisation, etc.)
43  */
45 
46 // Entities are split into two kinds:
47 // "Normal" (for most entities)
48 // "Local" (for entities that only exist on the local machine, aren't synchronised across the network,
49 // aren't retained in saved games, etc)
50 // The distinction is encoded in the entity ID, so that they're easily distinguished.
51 //
52 // We want all entity_id_ts to fit in jsval ints, i.e. 1-2^30 .. 2^30-1 (inclusive)
53 // We want them to be unsigned ints (actually it shouldn't matter but unsigned seems simpler)
54 // We want 1 tag bit
55 // So we have 1 JS-reserved bit, 1 unused sign bit, 1 local tag bit, 29 counter bits
56 // (0.5B entities should be plenty)
57 
58 #define ENTITY_TAGMASK (1 << 29)
59 #define ENTITY_IS_NORMAL(id) (((id) & ENTITY_TAGMASK) == 0)
60 #define ENTITY_IS_LOCAL(id) (((id) & ENTITY_TAGMASK) == ENTITY_TAGMASK)
62 
64 {
65  size_t numInterfaces;
66  IComponent* interfaces[1]; /* variable length array */
67 };
68 
69 /**
70  * Object wrapping an entity_id_t, with a SEntityComponentCache to support fast
71  * QueryInterface() / CmpPtr<>() calls.
72  *
73  * Components can use IComponent::GetSystemEntity() and IComponent::GetEntityHandle()
74  * to get handles to SYSTEM_ENTITY and themselves, and then pass those handles around.
75  *
76  * Be careful not to store a CEntityHandle for longer than the lifetime of the entity
77  * (listen to MT_Destroy messages to know when to release it) - otherwise the handle will
78  * contain a dangling pointer and will probably crash.
79  */
81 {
82 public:
85  : m_Id(id), m_ComponentCache(componentCache)
86  {
87  }
88 
89  entity_id_t GetId() const { return m_Id; }
91 
92 private:
95 };
96 
97 #endif // INCLUDED_SIM2_ENTITY
IComponent * interfaces[1]
Definition: Entity.h:66
size_t numInterfaces
Definition: Entity.h:65
Object wrapping an entity_id_t, with a SEntityComponentCache to support fast QueryInterface() / CmpPt...
Definition: Entity.h:80
SEntityComponentCache * m_ComponentCache
Definition: Entity.h:94
#define ENTITY_TAGMASK
Definition: Entity.h:58
const entity_id_t SYSTEM_ENTITY
Entity ID for singleton &#39;system&#39; components.
Definition: Entity.h:44
const entity_id_t FIRST_LOCAL_ENTITY
Definition: Entity.h:61
entity_id_t m_Id
Definition: Entity.h:93
entity_id_t GetId() const
Definition: Entity.h:89
#define u32
Definition: types.h:41
CEntityHandle(entity_id_t id, SEntityComponentCache *componentCache)
Definition: Entity.h:84
const entity_id_t INVALID_ENTITY
Invalid entity ID.
Definition: Entity.h:36
u32 entity_id_t
Entity ID type.
Definition: Entity.h:24
CEntityHandle()
Definition: Entity.h:83
SEntityComponentCache * GetComponentCache() const
Definition: Entity.h:90