fix for https://github.com/Attnam/ivan/issues/119
[k8-i-v-a-n.git] / src / game / entity.cpp
blob943f1d0a034d9cfbd068362f860b11086687773d
1 /*
3 * Iter Vehemens ad Necem (IVAN)
4 * Copyright (C) Timo Kiviluoto
5 * Released under the GNU General
6 * Public License
8 * See LICENSING which should be included
9 * along with this file for more details
12 #include <stdio.h>
13 /* Compiled through coreset.cpp */
16 //#define xlogf(...) do { fprintf(stderr, __VA_ARGS__); fflush(stderr); } while (0)
17 #define xlogf(...)
20 ////////////////////////////////////////////////////////////////////////////////
21 void entity::DumpDeadSet () {
23 fprintf(stderr, "=== dead set ===\n");
24 for (VoidSet::const_iterator i = mDeadSet.begin(); i != mDeadSet.end(); ++i) fprintf(stderr, "%p\n", *i);
29 ////////////////////////////////////////////////////////////////////////////////
30 entity::entity (const entity &Entity) : Emitation(Entity.Emitation), Flags(Entity.Flags) {
31 xlogf("entity::entity0: %p\n", this);
32 pool::RemoveFromHell(this);
33 if (Flags & HAS_BE) {
34 pool::Add(this);
35 } else {
36 // just in case
37 pool::Remove(this);
39 mOnEvents = Entity.mOnEvents;
43 entity::entity (int Flags) : Emitation(0), Flags(Flags|EXISTS) {
44 xlogf("entity::entity1: %p\n", this);
45 pool::RemoveFromHell(this);
46 if (Flags & HAS_BE) {
47 pool::Add(this);
48 } else {
49 // just in case
50 pool::Remove(this);
55 entity::~entity () {
56 xlogf("entity::~entity: %p\n", this);
57 pool::Remove(this);
58 pool::RemoveFromHell(this);
62 /* Calling SendToHell() marks the entity dead,
63 * so that it will be removed by hell::Burn() at the end of the tick.
64 * In general, you should never delete an entity instead of calling this,
65 * because pool::Be() will crash if the entity currently Be()ing is deleted. */
66 void entity::SendToHell () {
67 if (Flags&EXISTS) {
68 if (Flags&HAS_BE) {
69 pool::Remove(this);
70 Flags ^= HAS_BE;
72 pool::AddToHell(this);
73 Flags ^= EXISTS;
78 /* These functions tell the poolsystem whether the Be() function of the entity
79 * ought to be called during each tick, thus allowing it to alter itself independently.
80 * If the entity is stabile, use Disable(), since it speeds up the game. */
81 void entity::Enable () {
82 if (!(Flags&HAS_BE)) {
83 pool::Add(this);
84 Flags |= HAS_BE;
89 void entity::Disable () {
90 if (Flags&HAS_BE) {
91 pool::Remove(this);
92 Flags ^= HAS_BE;
97 int entity::GetUniqueMemoryMark (entity *e) {
98 if (e) {
99 int *mp = (int *)e;
101 return mp[-1];
103 return -1;
107 void *entity::operator new (size_t size) {
108 void *p;
109 static int mark = 0;
111 if (size == 0) {
112 fprintf(stderr, "FATAL: ALLOCATING ENTITY OF ZERO SIZE!\n");
113 abort();
115 p = calloc(1, size+sizeof(int));
117 int *mp = (int *)p;
119 *mp = ++mark;
120 p = mp+1;
122 return p;
126 void entity::operator delete (void *p) {
127 if (p) {
128 int *mp = (int *)p;
130 xlogf("delete(%d): %p\n", mp[-1], p);
131 --mp;
132 *mp = -1;
133 free(mp);
138 #undef xlogf