File: InvisibleEntityCache.cpp

package info (click to toggle)
eris 1.2.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,360 kB
  • ctags: 1,348
  • sloc: sh: 8,289; cpp: 7,576; perl: 287; ansic: 172; makefile: 143
file content (48 lines) | stat: -rw-r--r-- 1,455 bytes parent folder | download
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
#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include <Eris/InvisibleEntityCache.h>

#include <Eris/World.h>
#include <Eris/Log.h>

using namespace Time;

namespace Eris {

void InvisibleEntityCache::add(Entity *e)
{
	assert(e);
	/* if the first addition to the bucket (it's stamp) was longer ago that this time, we create a new
	bucket, instead of extending the current one. This keeps the buckets reasonably localised in
	time. */
	Time::Stamp lastUpdateBound = Time::Stamp::now() - _bucketWidthMsec;
	
	if (_buckets.empty() || (_buckets.front().stamp < lastUpdateBound)) {
		/* keep the width (times spanned by a single bucket under control : typical values would
		be a few seconds. Anything above 30 seconds is too much, if the 'view' is changing
		fast, since the buckets will get enormous (hundreds of entities) */
		
		Eris::log(LOG_DEBUG, "adding new IEC bucket, previous bucket has size %i",
			_buckets.front().contents.size());
		_buckets.push_front(_Bucket());
	}
	_buckets.front().add(e);
}	
	
void InvisibleEntityCache::flush()
{
	Time::Stamp expiry(Time::Stamp::now() - _bucketLifetimeMsec);
	while (!_buckets.empty() && (_buckets.back().stamp < expiry)) {
		for (EntitySet::iterator E=_buckets.back().contents.begin(); 
				E!=_buckets.back().contents.end();++E) {
			(*E)->getWorld()->flush(*E);
			delete *E;
		}
		Eris::log(LOG_VERBOSE, "IEC flushed %i entities", _buckets.back().contents.size());
		_buckets.pop_back();
	}
}
	
}