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
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Eris/Avatar.h>
#include <Eris/Entity.h>
#include <Eris/Connection.h>
#include <Eris/Log.h>
#include <Eris/View.h>
#include <Eris/IGRouter.h>
#include <Eris/Account.h>
#include <Eris/Exceptions.h>
#include <Eris/TypeService.h>
#include <Eris/Operations.h>
#include <wfmath/atlasconv.h>
#include <sigc++/slot.h>
#include <Atlas/Objects/Operation.h>
#include <Atlas/Objects/Entity.h>
#include <Atlas/Objects/Anonymous.h>
using namespace Atlas::Objects::Operation;
using Atlas::Objects::Root;
using Atlas::Objects::Entity::Anonymous;
using WFMath::TimeStamp;
using namespace Atlas::Message;
namespace Eris
{
Avatar::Avatar(Account* pl, const std::string& entId) :
m_account(pl),
m_entityId(entId),
m_entity(NULL),
m_stampAtLastOp(TimeStamp::now()),
m_lastOpTime(0.0)
{
m_view = new View(this);
m_entityAppearanceCon = m_view->Appearance.connect(sigc::mem_fun(this, &Avatar::onEntityAppear));
m_router = new IGRouter(this);
m_view->getEntityFromServer("");
m_view->getEntity(m_entityId);
m_wielded.Changed.connect(sigc::mem_fun(this, &Avatar::onWieldedChanged));
}
Avatar::~Avatar()
{
m_account->deactivateCharacter(this);
delete m_router;
delete m_view;
}
#pragma mark -
void Avatar::drop(Entity* e, const WFMath::Point<3>& pos, const std::string& loc)
{
if(e->getLocation() != m_entity)
{
error() << "Can't drop an Entity which is not held by the character";
return;
}
Move moveOp;
moveOp->setFrom(m_entityId);
Anonymous what;
what->setLoc(loc);
Atlas::Message::Element apos(pos.toAtlas());
what->setPosAsList(apos.asList());
what->setId(e->getId());
moveOp->setArgs1(what);
getConnection()->send(moveOp);
}
void Avatar::drop(Entity* e, const WFMath::Vector<3>& offset)
{
drop(e, m_entity->getPosition() + offset, m_entity->getLocation()->getId());
}
void Avatar::take(Entity* e)
{
Move moveOp;
moveOp->setFrom(m_entityId);
Anonymous what;
what->setLoc(m_entityId);
std::vector<double> p(3, 0.0);
what->setPos(p); // cyphesis is rejecting move ops with no pos set
what->setId(e->getId());
moveOp->setArgs1(what);
getConnection()->send(moveOp);
}
void Avatar::touch(Entity* e)
{
Touch touchOp;
touchOp->setFrom(m_entityId);
Anonymous what;
what->setId(e->getId());
touchOp->setArgs1(what);
getConnection()->send(touchOp);
}
void Avatar::say(const std::string& msg)
{
Talk t;
Anonymous what;
what->setAttr("say", msg);
t->setArgs1(what);
t->setFrom(m_entityId);
getConnection()->send(t);
}
void Avatar::emote(const std::string &em)
{
Imaginary im;
Anonymous emote;
emote->setId("emote");
emote->setAttr("description", em);
im->setArgs1(emote);
im->setFrom(m_entityId);
im->setSerialno(getNewSerialno());
getConnection()->send(im);
}
void Avatar::moveToPoint(const WFMath::Point<3>& pos)
{
Anonymous what;
what->setLoc(m_entity->getLocation()->getId());
what->setId(m_entityId);
what->setAttr("pos", pos.toAtlas());
Move moveOp;
moveOp->setFrom(m_entityId);
moveOp->setArgs1(what);
getConnection()->send(moveOp);
}
void Avatar::moveInDirection(const WFMath::Vector<3>& vel)
{
const double MIN_VELOCITY = 1e-3;
Anonymous arg;
//arg->setAttr("location", m_entity->getLocation()->getId());
arg->setId(m_entityId);
arg->setAttr("velocity", vel.toAtlas());
WFMath::CoordType sqr_mag = vel.sqrMag();
if(sqr_mag > MIN_VELOCITY) { // don't set orientation for zero velocity
WFMath::Quaternion q;
WFMath::CoordType z_squared = vel.z() * vel.z();
WFMath::CoordType plane_sqr_mag = sqr_mag - z_squared;
if(plane_sqr_mag < WFMATH_EPSILON * z_squared) {
// it's on the z axis
q.rotation(1, vel[2] > 0 ? -WFMath::Pi/2 : WFMath::Pi/2);
} else {
// rotate in the plane first
q.rotation(2, atan2(vel[1], vel[0]));
// then get the angle away from the plane
q = WFMath::Quaternion(1, -asin(vel[2] / sqrt(plane_sqr_mag))) * q;
}
arg->setAttr("orientation", q.toAtlas());
}
Move moveOp;
moveOp->setFrom(m_entityId);
moveOp->setArgs1(arg);
getConnection()->send(moveOp);
}
void Avatar::moveInDirection(const WFMath::Vector<3>& vel, const WFMath::Quaternion& orient)
{
Anonymous arg;
// arg->setAttr("location", m_entity->getLocation()->getId());
arg->setAttr("velocity", vel.toAtlas());
arg->setAttr("orientation", orient.toAtlas());
arg->setId(m_entityId);
Move moveOp;
moveOp->setFrom(m_entityId);
moveOp->setArgs1(arg);
getConnection()->send(moveOp);
}
void Avatar::place(Entity* e, Entity* container, const WFMath::Point<3>& pos)
{
Anonymous what;
what->setLoc(container->getId());
what->setAttr("pos", pos.toAtlas());
// what->setVelocityAsList( .... zero ... );
what->setId(e->getId());
Move moveOp;
moveOp->setFrom(m_entityId);
moveOp->setArgs1(what);
getConnection()->send(moveOp);
}
void Avatar::wield(Entity * entity)
{
if(entity->getLocation() != m_entity)
{
error() << "Can't wield an Entity which is not located in the avatar.";
return;
}
Anonymous arguments;
arguments->setId(entity->getId());
Wield wield;
wield->setFrom(m_entityId);
wield->setArgs1(arguments);
getConnection()->send(wield);
}
void Avatar::useOn(Entity * entity, const WFMath::Point< 3 > & position, const std::string& opType)
{
Anonymous arguments;
arguments->setId(entity->getId());
arguments->setObjtype("obj");
if (position.isValid()) arguments->setAttr("pos", position.toAtlas());
Use use;
use->setFrom(m_entityId);
if (opType.empty())
{
use->setArgs1(arguments);
} else {
RootOperation op;
StringList prs;
prs.push_back(opType);
op->setParents(prs);
op->setArgs1(arguments);
op->setFrom(m_entityId);
use->setArgs1(op);
}
getConnection()->send(use);
}
void Avatar::attack(Entity* entity)
{
assert(entity);
Attack attackOp;
attackOp->setFrom(m_entityId);
Anonymous what;
what->setId(entity->getId());
attackOp->setArgs1(what);
getConnection()->send(attackOp);
}
#pragma mark -
void Avatar::onEntityAppear(Entity* ent)
{
if (ent->getId() == m_entityId) {
assert(m_entity == NULL);
m_entity = ent;
ent->ChildAdded.connect(sigc::mem_fun(this, &Avatar::onCharacterChildAdded));
ent->ChildRemoved.connect(sigc::mem_fun(this, &Avatar::onCharacterChildRemoved));
ent->observe("right_hand_wield", sigc::mem_fun(this, &Avatar::onCharacterWield));
GotCharacterEntity.emit(ent);
m_entityAppearanceCon.disconnect(); // stop listenting to View::Appearance
}
}
void Avatar::onCharacterChildAdded(Entity* child)
{
InvAdded.emit(child);
}
void Avatar::onCharacterChildRemoved(Entity* child)
{
InvRemoved.emit(child);
}
void Avatar::onCharacterWield(const std::string& s, const Atlas::Message::Element& val)
{
if (s != "right_hand_wield") {
warning() << "got wield for strange slot";
return;
}
if (!val.isString()) {
warning() << "got malformed wield value";
return;
}
m_wielded = EntityRef(m_view, val.asString());
}
Connection* Avatar::getConnection() const
{
return m_account->getConnection();
}
double Avatar::getWorldTime()
{
WFMath::TimeDiff deltaT = TimeStamp::now() - m_stampAtLastOp;
return m_lastOpTime + (deltaT.milliseconds() / 1000.0);
}
void Avatar::updateWorldTime(double seconds)
{
m_stampAtLastOp = TimeStamp::now();
m_lastOpTime = seconds;
}
void Avatar::onWieldedChanged()
{
m_useOps.clear();
if (!m_wielded) return;
if (!m_wielded->hasAttr("operations")) return;
const Element& ops = m_wielded->valueOfAttr("operations");
if (!ops.isList()) {
warning() << "entity " << m_wielded->getId() <<
" has operations attr which is not a list";
return;
}
const ListType& opsl(ops.asList());
m_useOps.reserve(opsl.size());
TypeService* ts = getConnection()->getTypeService();
for (ListType::const_iterator i=opsl.begin(); i!=opsl.end(); ++i)
{
if (!i->isString())
{
warning() << "ignoring malformed operations list item";
continue;
}
m_useOps.push_back(ts->getTypeByName(i->asString()));
}
}
} // of namespace Eris
|