Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Thinkers|World: Added Thinkers utility method for counting active thi…
…nkers
  • Loading branch information
danij-deng committed Jan 19, 2014
1 parent 1a396ec commit 071e9be
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions doomsday/client/include/world/thinkers.h
Expand Up @@ -95,6 +95,15 @@ class Thinkers
*/
void setMobjId(thid_t id, bool inUse = true);

/**
* Returns the total number of thinkers (of any type) in the collection.
*
* @param numInStasis If not @c NULL, the number of thinkers in stasis will
* be added to the current value (caller must ensure to
* initialize this).
*/
int count(int *numInStasis = 0) const;

private:
DENG2_PRIVATE(d)
};
Expand Down
34 changes: 34 additions & 0 deletions doomsday/client/src/world/thinkers.cpp
Expand Up @@ -86,6 +86,26 @@ struct ThinkerList
sentinel.prev = &th;
}

int count(int *numInStasis) const
{
int num = 0;
thinker_t *th = sentinel.next;
while(th != &sentinel && th)
{
#ifdef LIBDENG_FAKE_MEMORY_ZONE
DENG_ASSERT(th->next != 0);
DENG_ASSERT(th->prev != 0);
#endif
num += 1;
if(numInStasis && th->inStasis)
{
(*numInStasis) += 1;
}
th = th->next;
}
return num;
}

int iterate(int (*callback) (thinker_t *, void *), void *parameters = 0)
{
int result = false;
Expand Down Expand Up @@ -337,6 +357,20 @@ int Thinkers::iterate(thinkfunc_t func, byte flags,
return result;
}

int Thinkers::count(int *numInStasis) const
{
int total = 0;
if(isInited())
{
for(int i = 0; i < d->lists.count(); ++i)
{
ThinkerList *list = d->lists[i];
total += list->count(numInStasis);
}
}
return total;
}

void unlinkThinkerFromList(thinker_t *th)
{
th->next->prev = th->prev;
Expand Down

0 comments on commit 071e9be

Please sign in to comment.