File: Entity.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 (474 lines) | stat: -rw-r--r-- 11,903 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include <Eris/Entity.h>
#include <Eris/World.h>
#include <Eris/Connection.h>
#include <Eris/Utils.h>
#include <Eris/Wait.h>
#include <Eris/Property.h>
#include <Eris/TypeInfo.h>
#include <Eris/OpDispatcher.h>
#include <Eris/IdDispatcher.h>
#include <Eris/Log.h>
#include <Eris/Exceptions.h>

#include <Atlas/Objects/Entity/GameEntity.h>
#include <Atlas/Objects/Operation/Move.h>
#include <Atlas/Objects/Operation/Talk.h>
#include <Atlas/Objects/Operation/Sight.h>

#include <wfmath/atlasconv.h>

#include <algorithm>
#include <set> 
#include <cmath>

#include <cassert>

using namespace Atlas::Message;

namespace Eris {

Entity::Entity(const Atlas::Objects::Entity::GameEntity &ge, World *world) :
	_id(ge.getId()),
	_stamp(-1.0),
	_visible(true),
	_container(NULL),
	_position(ge.getPos()),
	_velocity(ge.getVelocity()),
	_orientation(1.0, 0., 0., 0.),
	_inUpdate(false),
	_hasBBox(false),
	_world(world)
{	
	_parents = getParentsAsSet(ge);
	recvSight(ge);	
}

Entity::Entity(const std::string &id, World *world) :
	_id(id),
	_stamp(-1.0),
	_visible(true),
	_container(NULL),
	_position(0., 0., 0.),
	_velocity(0., 0., 0.),
	_orientation(1.0, 0., 0., 0.),
	_inUpdate(false),
	_world(world)
{
	;
}

Entity::~Entity()
{
	Connection *con = _world->getConnection();
	
	while (!_localDispatchers.empty()) {
		con->removeDispatcherByPath("op:sight:op",_localDispatchers.front());
		_localDispatchers.pop_front();
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Entity::setContainer(Entity *cnt)
{
	Recontainered.emit(_container, cnt);
	_container = cnt;
}

void Entity::addMember(Entity *e)
{
    Eris::log(LOG_DEBUG, "adding entity '%s' as member of '%s'",
	e->getName().c_str(), getName().c_str());
    
    assert(e != this);
    
    _members.push_back(e);
    AddedMember.emit(e);
    e->setContainer(this);
}

void Entity::rmvMember(Entity *e)
{
    if (!e)
	throw InvalidOperation("passed NULL pointer to Entity::rmvMember");
    EntityArray::iterator ei = std::find(_members.begin(), _members.end(), e);
    if (ei == _members.end())
	    throw InvalidOperation("Unknown member " + e->getName() + 
		" to remove from " + getName());
    _members.erase(ei);
    RemovedMember.emit(e);
}

Entity* Entity::getMember(unsigned int i)
{
	if (i >= _members.size())
		throw InvalidOperation("Illegal member index");
	return _members[i];
}

bool Entity::hasProperty(const std::string &p) const
{
    PropertyMap::const_iterator pi =
	_properties.find(p);
    return (pi != _properties.end());
}

const Atlas::Message::Element& Entity::getProperty(const std::string &p) const
{
    PropertyMap::const_iterator pi = _properties.find(p);
    if (pi == _properties.end())
	throw InvalidOperation("Unknown property " + p);

    return pi->second->getValue();
}

void Entity::observeProperty(const std::string &nm, 
    const SigC::Slot1<void, const Atlas::Message::Element&> slot)
{
    PropertyMap::iterator pi = _properties.find(nm);
    if (pi == _properties.end())
	throw InvalidOperation("Unknown property " + nm);
    
    pi->second->Set.connect(slot);
}

WFMath::Point<3> Entity::getPosition() const
{
	return _position;
}

WFMath::Vector<3> Entity::getVelocity() const
{
	return _velocity;
}

WFMath::AxisBox<3> Entity::getBBox() const
{
	return _bbox;
}

WFMath::Quaternion Entity::getOrientation() const
{
    return _orientation;
}

TypeInfo* Entity::getType() const
{
    assert(!_parents.empty());
	TypeService *ts = _world->getConnection()->getTypeService();
    return ts->getTypeByName(*_parents.begin());
}

void Entity::setVisible(bool vis)
{
	bool wasVisible = _visible;
	_visible = vis;
  
	// recurse onz children
	for (EntityArray::iterator E=_members.begin();E!=_members.end();++E)
		(*E)->setVisible(vis);
	
	if (!wasVisible && _visible) {
		//move back to actice
		_world->markVisible(this);
	}
	
	if (wasVisible && !_visible) {
		// move to the cache ... keep around for 'a while'
		_world->markInvisible(this);
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Entity::recvSight(const Atlas::Objects::Entity::GameEntity &ge)
{    
    beginUpdate();
    
    Atlas::Message::Element::MapType amp = ge.asObject().asMap();
    for (Atlas::Message::Element::MapType::iterator A = amp.begin(); A!=amp.end(); ++A) {
	if (A->first == "id") continue;
	setProperty(A->first, A->second);
    }

    endUpdate();
}

void Entity::recvMove(const Atlas::Objects::Operation::Move &mv)
{	
    beginUpdate();
    
    const Atlas::Message::Element::MapType &args = 
	mv.getArgs().front().asMap();
    for (Atlas::Message::Element::MapType::const_iterator A = args.begin(); 
	    A != args.end(); ++A) {
	setProperty(A->first, A->second);
    }

    endUpdate();	// emit 'Changed' too
    handleMove();
}

void Entity::recvSet(const Atlas::Objects::Operation::Set &st)
{
    const Atlas::Message::Element::MapType &attrs = st.getArgs().front().asMap();
    beginUpdate();
    // blast through the map, setting each property
    for (Atlas::Message::Element::MapType::const_iterator ai = attrs.begin(); ai != attrs.end(); ++ai) {
	if (ai->first=="id") continue;	// important
	setProperty(ai->first, ai->second);
    }
    endUpdate();
}

void Entity::recvSound(const Atlas::Objects::Operation::Sound &/*snd*/)
{
	// FIXME - decide upon a clever way to handle these. 
}

void Entity::recvTalk(const Atlas::Objects::Operation::Talk &tk)
{
	const Atlas::Message::Element &obj = getArg(tk, 0);
	Atlas::Message::Element::MapType::const_iterator m = obj.asMap().find("say");
	if (m == obj.asMap().end())
		throw IllegalObject(tk, "No sound object in arg 0");
	
	handleTalk(m->second.asString());
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// default implementions of handle simple emit the corresponding signal

void Entity::handleTalk(const std::string &msg)
{
	Say.emit(msg);
}

void Entity::handleMove()
{
	Moved.emit(_position);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// FIXME
// this really needs a global switch from strings to integer Atoms to be effective;
// once that is done the if ( == "") tests can be replaced with a nice efficent switch

void Entity::setProperty(const std::string &s, const Atlas::Message::Element &val)
{
    /* we allow mapping of attributes to different internal values; use this with
    caution */
    std::string mapped(s);
	
    if (s == "name")
	_name = val.asString();
    else if (s == "stamp")
	_stamp = val.asFloat();
    else if (s == "loc") {
	std::string loc = val.asString();
	setContainerById(loc);
    } else if (s == "pos") {
	WFMath::Point<3> pos;
	pos.fromAtlas(val);
	setPosition(pos);
    } else if (s == "velocity")
	_velocity.fromAtlas(val);
    else if (s == "orientation")
	_orientation.fromAtlas(val);
    else if (s == "face") {
	mapped = "orientation";
	WFMath::Point<3> face(val); // Should this be Point<3> or Vector<3>?
	// build the quaternion from rotation about z axis
	_orientation.rotation(2, atan2(face[1], face[0]));	
    } else if (s == "description")
	_description = val.asString();
    else if (s == "bbox") {
	_bbox.fromAtlas(val);
        _hasBBox = true;
    } else if (s == "contains") {
	setContents(val.asList());
    }
    
    PropertyMap::iterator P=_properties.find(mapped);
    if (P == _properties.end()) {
	P = _properties.insert(P, 
	    PropertyMap::value_type(mapped, new Property())
	);
    }
    
    P->second->setValue(val);
    
    if (_inUpdate) {
	// add to modified set
	_modified.insert(mapped);
    } else {
	// generate a Changed single with a temporary set
	StringSet ms;
	ms.insert(mapped);
	Changed.emit(ms);
    }
}


void Entity::beginUpdate()
{
    if (_inUpdate)
	throw InvalidOperation("Entity::beingUpdate called inside of property update");
    assert(_modified.empty());
    _inUpdate = true;
}

void Entity::endUpdate()
{
    if (!_inUpdate)
	throw InvalidOperation("Entity::endUpdate called outside of property update");
    _inUpdate = false;
    Changed.emit(_modified);
    _modified.clear();
}

void Entity::innerOpFromSlot(Dispatcher *s)
{
	// build the unique dispathcer name
	std::string efnm = "from." + _id;
	
	// check if the dispatcher already exists
	Connection *con = _world->getConnection(); 
	Dispatcher *ds = con->getDispatcherByPath("op:sight:op"),
		*efd = ds->getSubdispatch(efnm);
	
	if (!efd) {
		efd = new OpFromDispatcher(efnm, _id);
		_localDispatchers.push_back(efnm);
		ds->addSubdispatch(efd);
	}
	
	// attach the operation dispatcher
	efd->addSubdispatch(s);
}

void Entity::innerOpToSlot(Dispatcher *s)
{
	// build the unique dispathcer name
	std::string etnm = "to." + _id;
	
	// check if the dispatcher already exists
	Connection *con = _world->getConnection(); 
	Dispatcher *ds = con->getDispatcherByPath("op:sight:op"),
		*etd = ds->getSubdispatch(etnm);
	
	if (!etd) {
		etd = new OpToDispatcher(etnm, _id);
		_localDispatchers.push_back(etnm);
		ds->addSubdispatch(etd);
	}
	
	// attach the operation dispatcher
	etd->addSubdispatch(s);
}

void Entity::setPosition(const WFMath::Point<3>& pt)
{
    _position = pt;
}

void Entity::setContents(const Atlas::Message::Element::ListType &contents)
{
    for (Atlas::Message::Element::ListType::const_iterator
	    C=contents.begin(); C!=contents.end();++C) {
	// check we have it; if yes, ensure it's installed. if not, it will be attached when
	// it arrives.
	Entity *con = _world->lookup(C->asString());
	if (con) {
	    Eris::log(LOG_DEBUG, 
		"already have entity '%s', not setting container",
		con->getName().c_str()
	    );
	}
    }
}

void Entity::setContainerById(const std::string &id)
{
    if ( !_container || (id != _container->getID()) ) {
		
	if (_container) {
	    Eris::log(LOG_DEBUG, "Entity::setContainerById: setting container to NULL");
	    _container->rmvMember(this);
	    _container = NULL;
	}
		
	// have to check this, becuase changes in what the client can see
	// may cause the client's root to change
	if (!id.empty()) {
		Entity *ncr = _world->lookup(id);
		if (ncr) {
			ncr->addMember(this);
			setContainer(ncr);
		} else {
			// setup a redispatch once we have the container
			// sythesises a set, with just the container.
			Atlas::Objects::Operation::Set setc;
				
			Atlas::Message::Element::MapType args;
			args["loc"] = id;
			setc.setArgs(Atlas::Message::Element::ListType(1,args));
			setc.setTo(_id);
			
			Atlas::Objects::Operation::Sight ssc;
			ssc.setArgs(Atlas::Message::Element::ListType(1, setc.asObject()));
			ssc.setTo(_world->getFocusedEntityID());
			ssc.setSerialno(getNewSerialno());
			
			// if we received sets/creates/sights in rapid sucession, this can happen, and is
			// very, very bad
			std::string setid("set_container_" + _id);
			std::string dispatch_id = "op:" + _world->getDispatcherID()
				+ ":sight:entity";

			_world->getConnection()->removeIfDispatcherByPath(
				dispatch_id, setid);
			
			new WaitForDispatch(ssc, dispatch_id, 
				new IdDispatcher(setid, id), _world->getConnection());
		}
	} // of new container is valid
    }	
	
    if (id.empty()) {	// id of "" implies local root
	Eris::log(LOG_DEBUG, "got entity with empty container, assuming it's the world");
	_world->setRootEntity(this);	
    }
}

#ifdef HAVE_CPPUNIT
class EntityTest : public TestCase
{
public:
    
    void runTest()
    {
	Entity testA("foo");	
	
	Atlas::Objects::Operation::Move mv;
	mv.SetLocation("foo");
	Coord v1(2.0, 3.0, -4.0);
	mv.SetVelocity(v1.asObject());
	mv.setAttr("face", 120.0);
	
	testA.recvMove(mv);
	
	CPPUNIT_ASSERT("foo" == test.getContainer());
	CPPUNIT_ASSERT(120.0 == test.getOrientation().asEuler().z);
    }

private:
};
#endif

} // of namespace