File: Entity.h

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (97 lines) | stat: -rw-r--r-- 3,378 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Copyright (C) 2010 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_SIM2_ENTITY
#define INCLUDED_SIM2_ENTITY
// (can't call it INCLUDED_ENTITY because that conflicts with simulation/Entity.h)

#include "lib/types.h"

class IComponent;

/**
 * Entity ID type.
 * ID numbers are never reused within a simulation run.
 */
typedef u32 entity_id_t;

/**
 * Invalid entity ID. Used as an error return value by some functions.
 * No valid entity will have this ID.
 */
const entity_id_t INVALID_ENTITY = 0;

/**
 * Entity ID for singleton 'system' components.
 * Use with QueryInterface to get the component instance.
 * (This allows those systems to make convenient use of the common infrastructure
 * for message-passing, scripting, serialisation, etc.)
 */
const entity_id_t SYSTEM_ENTITY = 1;

// Entities are split into two kinds:
// "Normal" (for most entities)
// "Local" (for entities that only exist on the local machine, aren't synchronised across the network,
// aren't retained in saved games, etc)
// The distinction is encoded in the entity ID, so that they're easily distinguished.
//
// We want all entity_id_ts to fit in jsval ints, i.e. 1-2^30 .. 2^30-1 (inclusive)
// We want them to be unsigned ints (actually it shouldn't matter but unsigned seems simpler)
// We want 1 tag bit
// So we have 1 JS-reserved bit, 1 unused sign bit, 1 local tag bit, 29 counter bits
// (0.5B entities should be plenty)

#define ENTITY_TAGMASK (1 << 29)
#define ENTITY_IS_NORMAL(id) (((id) & ENTITY_TAGMASK) == 0)
#define ENTITY_IS_LOCAL(id) (((id) & ENTITY_TAGMASK) == ENTITY_TAGMASK)
const entity_id_t FIRST_LOCAL_ENTITY = ENTITY_TAGMASK;

struct SEntityComponentCache
{
	size_t numInterfaces;
	IComponent* interfaces[1]; /* variable length array */
};

/**
 * Object wrapping an entity_id_t, with a SEntityComponentCache to support fast
 * QueryInterface() / CmpPtr<>() calls.
 *
 * Components can use IComponent::GetSystemEntity() and IComponent::GetEntityHandle()
 * to get handles to SYSTEM_ENTITY and themselves, and then pass those handles around.
 *
 * Be careful not to store a CEntityHandle for longer than the lifetime of the entity
 * (listen to MT_Destroy messages to know when to release it) - otherwise the handle will
 * contain a dangling pointer and will probably crash.
 */
class CEntityHandle
{
public:
	CEntityHandle() : m_Id(0), m_ComponentCache(NULL) { }
	CEntityHandle(entity_id_t id, SEntityComponentCache* componentCache)
		: m_Id(id), m_ComponentCache(componentCache)
	{
	}

	entity_id_t GetId() const { return m_Id; }
	SEntityComponentCache* GetComponentCache() const { return m_ComponentCache; }

private:
	entity_id_t m_Id;
	SEntityComponentCache* m_ComponentCache;
};

#endif // INCLUDED_SIM2_ENTITY