2015-09-20 20:03:13 +02:00
|
|
|
/*
|
|
|
|
|
Copyright (C) 2015 erik
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-09-26 20:37:31 +02:00
|
|
|
#include <rulesets/mind/AwareMind.h>
|
|
|
|
|
#include <rulesets/mind/AwarenessStore.h>
|
|
|
|
|
#include <rulesets/mind/AwarenessStoreProvider.h>
|
|
|
|
|
#include <rulesets/mind/SharedTerrain.h>
|
2016-03-31 21:08:12 +02:00
|
|
|
#include <rulesets/TerrainProperty.h>
|
2015-09-23 22:03:31 +02:00
|
|
|
|
2015-09-20 20:03:13 +02:00
|
|
|
#include "navigation/Awareness.h"
|
|
|
|
|
#include "navigation/Steering.h"
|
|
|
|
|
|
2015-09-23 22:03:31 +02:00
|
|
|
#include "common/log.h"
|
2015-09-25 21:25:04 +02:00
|
|
|
#include "common/SystemTime.h"
|
|
|
|
|
#include "common/Tick.h"
|
2015-09-26 20:37:31 +02:00
|
|
|
#include "common/debug.h"
|
2015-09-23 22:03:31 +02:00
|
|
|
|
|
|
|
|
#include <Atlas/Objects/RootEntity.h>
|
2015-09-25 21:25:04 +02:00
|
|
|
#include <Atlas/Objects/Operation.h>
|
|
|
|
|
#include <Atlas/Objects/Entity.h>
|
|
|
|
|
|
|
|
|
|
#include <wfmath/atlasconv.h>
|
2016-03-31 21:08:12 +02:00
|
|
|
|
|
|
|
|
#include <Mercator/Terrain.h>
|
|
|
|
|
|
2015-09-26 20:37:31 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2015-09-27 22:41:40 +02:00
|
|
|
static const bool debug_flag = true;
|
2015-09-23 22:03:31 +02:00
|
|
|
|
|
|
|
|
AwareMind::AwareMind(const std::string &id, long intId, SharedTerrain& sharedTerrain, AwarenessStoreProvider& awarenessStoreProvider) :
|
2016-03-31 21:08:12 +02:00
|
|
|
BaseMind(id, intId), mSharedTerrain(sharedTerrain), mAwarenessStoreProvider(awarenessStoreProvider), mAwarenessStore(nullptr), mSteering(new Steering(*this)), mServerTimeDiff(
|
|
|
|
|
0)
|
2015-09-20 20:03:13 +02:00
|
|
|
{
|
2015-09-23 22:03:31 +02:00
|
|
|
m_map.setListener(this);
|
2015-09-20 20:03:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AwareMind::~AwareMind()
|
|
|
|
|
{
|
2016-03-15 22:20:36 +01:00
|
|
|
if (mAwareness) {
|
|
|
|
|
auto& entities = m_map.getEntities();
|
|
|
|
|
//Remove all our still known entities from the awareness
|
|
|
|
|
for (auto entry : entities) {
|
|
|
|
|
if (entry.second->m_location.m_loc == m_location.m_loc) {
|
|
|
|
|
mAwareness->removeEntity(*this, *entry.second);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mAwareness->removeAwarenessArea(getId());
|
2016-03-20 20:19:35 +01:00
|
|
|
mAwareness->removeObserver();
|
2016-03-15 22:20:36 +01:00
|
|
|
}
|
2015-09-25 21:25:04 +02:00
|
|
|
delete mSteering;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AwareMind::operation(const Operation & op, OpVector & res)
|
|
|
|
|
{
|
|
|
|
|
|
2016-03-15 23:45:12 +01:00
|
|
|
//If it's a "move" tick we'll process it here and won't send it on to the mind.
|
2015-09-25 21:25:04 +02:00
|
|
|
if (op->getClassNo() == Atlas::Objects::Operation::TICK_NO) {
|
|
|
|
|
if (!op->getArgs().empty()) {
|
|
|
|
|
auto arg = op->getArgs().front();
|
|
|
|
|
if (arg->getName() == "move") {
|
|
|
|
|
processMoveTick(op, res);
|
2016-03-15 23:45:12 +01:00
|
|
|
return;
|
2015-09-25 21:25:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-15 23:45:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mServerTimeDiff == 0 && op->getClassNo() == Atlas::Objects::Operation::SIGHT_NO) {
|
2015-09-26 20:37:31 +02:00
|
|
|
if (op->hasAttrFlag(Atlas::Objects::Operation::SECONDS_FLAG)) {
|
|
|
|
|
double stamp = op->getSeconds();
|
2016-03-08 23:28:05 +01:00
|
|
|
mServerTimeDiff = getCurrentLocalTime() - stamp;
|
2015-09-26 20:37:31 +02:00
|
|
|
}
|
2015-09-25 21:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 23:45:12 +01:00
|
|
|
BaseMind::operation(op, res);
|
2015-09-20 20:03:13 +02:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 23:28:05 +01:00
|
|
|
double AwareMind::getCurrentLocalTime() const
|
|
|
|
|
{
|
|
|
|
|
SystemTime time;
|
|
|
|
|
time.update();
|
|
|
|
|
return (time.seconds() + (time.microseconds() * 0.000001));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double AwareMind::getCurrentServerTime() const
|
|
|
|
|
{
|
|
|
|
|
return getCurrentLocalTime() - mServerTimeDiff;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 21:25:04 +02:00
|
|
|
void AwareMind::processMoveTick(const Operation & op, OpVector & res)
|
|
|
|
|
{
|
2015-09-27 22:41:40 +02:00
|
|
|
//Default to checking movement every 0.2 seconds, unless steering tells us otherwise
|
2015-09-25 21:25:04 +02:00
|
|
|
double futureTick = 0.2;
|
|
|
|
|
|
|
|
|
|
if (mAwareness) {
|
|
|
|
|
auto remainingDirtyTiles = mAwareness->rebuildDirtyTile();
|
|
|
|
|
if (remainingDirtyTiles > 0) {
|
|
|
|
|
futureTick = 0;
|
|
|
|
|
} else {
|
|
|
|
|
if (mAwareness->needsPruning()) {
|
|
|
|
|
mAwareness->pruneTiles();
|
|
|
|
|
}
|
|
|
|
|
if (mAwareness->needsPruning()) {
|
|
|
|
|
futureTick = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mSteering) {
|
2016-03-08 23:28:05 +01:00
|
|
|
SteeringResult result = mSteering->update(getCurrentServerTime());
|
2015-09-25 21:25:04 +02:00
|
|
|
if (result.direction.isValid()) {
|
|
|
|
|
Atlas::Objects::Operation::Move move;
|
|
|
|
|
Atlas::Objects::Entity::Anonymous what;
|
|
|
|
|
// what->setLoc(m_entity->getLocation()->getId());
|
|
|
|
|
what->setId(getId());
|
|
|
|
|
what->setAttr("velocity", result.direction.toAtlas());
|
|
|
|
|
if (result.destination.isValid()) {
|
|
|
|
|
what->setAttr("pos", result.destination.toAtlas());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
move->setFrom(getId());
|
|
|
|
|
move->setArgs1(what);
|
|
|
|
|
|
2015-09-27 22:41:40 +02:00
|
|
|
// if (debug_flag) {
|
|
|
|
|
// std::cout << "Move arg {" << std::endl;
|
|
|
|
|
// debug_dump(what, std::cout);
|
|
|
|
|
// std::cout << "}" << std::endl << std::flush;
|
|
|
|
|
// }
|
2015-09-26 20:37:31 +02:00
|
|
|
|
2015-09-25 21:25:04 +02:00
|
|
|
res.push_back(move);
|
2015-09-27 22:41:40 +02:00
|
|
|
}
|
|
|
|
|
if (result.timeToNextWaypoint) {
|
|
|
|
|
futureTick = std::min(*result.timeToNextWaypoint, futureTick);
|
2015-09-25 21:25:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Atlas::Objects::Operation::Tick tick;
|
2015-09-26 20:37:31 +02:00
|
|
|
Atlas::Objects::Entity::Anonymous arg;
|
2015-09-25 21:25:04 +02:00
|
|
|
arg->setName("move");
|
|
|
|
|
tick->setArgs1(arg);
|
|
|
|
|
tick->setFutureSeconds(futureTick);
|
|
|
|
|
|
|
|
|
|
res.push_back(tick);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 23:28:05 +01:00
|
|
|
int AwareMind::updatePath()
|
|
|
|
|
{
|
|
|
|
|
return mSteering->updatePath(getCurrentServerTime());
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 21:25:04 +02:00
|
|
|
Steering& AwareMind::getSteering()
|
|
|
|
|
{
|
|
|
|
|
return *mSteering;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 22:03:31 +02:00
|
|
|
void AwareMind::setType(const TypeNode * t)
|
|
|
|
|
{
|
|
|
|
|
BaseMind::setType(t);
|
2015-09-26 20:37:31 +02:00
|
|
|
//log(INFO, "Creating store.");
|
2015-09-23 22:03:31 +02:00
|
|
|
mAwarenessStore = &mAwarenessStoreProvider.getStore(getType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AwareMind::entityAdded(const MemEntity& entity)
|
|
|
|
|
{
|
|
|
|
|
if (mAwareness) {
|
2015-09-27 22:41:40 +02:00
|
|
|
// log(INFO, String::compose("Adding entity %1", entity.getId()));
|
2015-09-23 22:03:31 +02:00
|
|
|
//TODO: check if the entity is dynamic
|
|
|
|
|
if (entity.m_location.m_loc == m_location.m_loc) {
|
|
|
|
|
mAwareness->addEntity(*this, entity, false);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2015-09-27 22:41:40 +02:00
|
|
|
//Check if we've received the current domain entity.
|
2015-09-23 22:03:31 +02:00
|
|
|
if (this->m_location.m_loc && entity.getIntId() == this->m_location.m_loc->getIntId()) {
|
2015-09-26 20:37:31 +02:00
|
|
|
//log(INFO, "Creating awareness.");
|
|
|
|
|
requestAwareness(entity);
|
2016-03-31 21:08:12 +02:00
|
|
|
|
|
|
|
|
Atlas::Message::Element terrainElem;
|
|
|
|
|
if (entity.getAttr("terrain", terrainElem) == 0) {
|
|
|
|
|
parseTerrain(terrainElem);
|
|
|
|
|
}
|
2015-09-26 20:37:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AwareMind::requestAwareness(const MemEntity& entity)
|
|
|
|
|
{
|
|
|
|
|
mAwareness = mAwarenessStore->requestAwareness(entity);
|
2016-03-20 20:19:35 +01:00
|
|
|
mAwareness->addObserver();
|
2015-09-26 20:37:31 +02:00
|
|
|
mAwareness->addEntity(*this, *this, true);
|
2016-03-15 22:20:36 +01:00
|
|
|
auto& entities = m_map.getEntities();
|
|
|
|
|
//Add all existing known entities that have the same parent entity as ourselves.
|
2015-09-26 20:37:31 +02:00
|
|
|
for (auto entry : entities) {
|
|
|
|
|
if (entry.first != getIntId()) {
|
|
|
|
|
if (entry.second->m_location.m_loc == m_location.m_loc) {
|
2016-03-15 22:20:36 +01:00
|
|
|
mAwareness->addEntity(*this, *entry.second, false);
|
2015-09-26 20:37:31 +02:00
|
|
|
}
|
2015-09-23 22:03:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-09-26 20:37:31 +02:00
|
|
|
mSteering->setAwareness(mAwareness.get());
|
2015-09-23 22:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AwareMind::entityUpdated(const MemEntity& entity, const Atlas::Objects::Entity::RootEntity & ent, LocatedEntity* oldLocation)
|
|
|
|
|
{
|
|
|
|
|
if (mAwareness) {
|
2015-09-27 22:41:40 +02:00
|
|
|
//Update the awareness if location, position, velocity, orientation or bbox has changed
|
|
|
|
|
if (ent->hasAttrFlag(Atlas::Objects::Entity::LOC_FLAG) || ent->hasAttrFlag(Atlas::Objects::Entity::POS_FLAG) || ent->hasAttrFlag(Atlas::Objects::Entity::VELOCITY_FLAG)
|
|
|
|
|
|| ent->hasAttr("orientation") || ent->hasAttr("bbox")) {
|
2015-09-23 22:03:31 +02:00
|
|
|
if (oldLocation == entity.m_location.m_loc) {
|
|
|
|
|
//Location wasn't changed
|
|
|
|
|
if (entity.m_location.m_loc == this->m_location.m_loc) {
|
2015-09-26 20:37:31 +02:00
|
|
|
//log(INFO, "Updated entity.");
|
2015-09-23 22:03:31 +02:00
|
|
|
mAwareness->updateEntityMovement(*this, entity);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//Check if new location is the domain, and then add the entity
|
|
|
|
|
if (entity.m_location.m_loc == this->m_location.m_loc) {
|
2015-09-26 20:37:31 +02:00
|
|
|
//log(INFO, "Adding entity.");
|
2015-09-23 22:03:31 +02:00
|
|
|
mAwareness->addEntity(*this, entity, false);
|
|
|
|
|
} else if (oldLocation == this->m_location.m_loc) {
|
2015-09-26 20:37:31 +02:00
|
|
|
//log(INFO, "Removing entity.");
|
2015-09-23 22:03:31 +02:00
|
|
|
mAwareness->removeEntity(*this, entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-27 16:50:00 +02:00
|
|
|
|
|
|
|
|
//If it was ourselves that moved we should notify steering that it shouldn't wait any more for a movement op.
|
|
|
|
|
if (entity.getIntId() == getIntId()) {
|
|
|
|
|
mSteering->setIsExpectingServerMovement(false);
|
|
|
|
|
}
|
2015-09-23 22:03:31 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (this->m_location.m_loc && entity.getIntId() == this->m_location.m_loc->getIntId()) {
|
2016-03-31 21:08:12 +02:00
|
|
|
if (ent->hasAttr("terrain")) {
|
|
|
|
|
|
|
|
|
|
Atlas::Message::Element terrainElement;
|
|
|
|
|
if (ent->copyAttr("terrain", terrainElement) == 0) {
|
|
|
|
|
parseTerrain(terrainElement);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-25 21:25:04 +02:00
|
|
|
if (!mAwareness) {
|
2015-09-26 20:37:31 +02:00
|
|
|
requestAwareness(entity);
|
2016-03-31 21:08:12 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AwareMind::parseTerrain(const Atlas::Message::Element& terrainElement)
|
|
|
|
|
{
|
|
|
|
|
if (terrainElement.isMap()) {
|
|
|
|
|
const Atlas::Message::MapType& terrainMap = terrainElement.Map();
|
|
|
|
|
auto pointsI = terrainMap.find("points");
|
|
|
|
|
if (pointsI != terrainMap.end() && pointsI->second.isMap()) {
|
|
|
|
|
|
|
|
|
|
std::vector<SharedTerrain::BasePointDefinition> pointDefs;
|
|
|
|
|
|
|
|
|
|
auto& pointsMap = pointsI->second.Map();
|
|
|
|
|
for (auto& pointEntry : pointsMap) {
|
|
|
|
|
if (pointEntry.second.isList()) {
|
|
|
|
|
auto& pointsList = pointEntry.second.List();
|
|
|
|
|
if (pointsList.size() == 3) {
|
|
|
|
|
if (!pointsList[0].isNum()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!pointsList[1].isNum()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!pointsList[2].isNum()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pointDefs.emplace_back(
|
|
|
|
|
SharedTerrain::BasePointDefinition { (int)pointsList[0].asNum(), (int)pointsList[1].asNum(), Mercator::BasePoint(pointsList[2].asNum()) });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::vector<SharedTerrain::BasePointDefinition> changedPoints = mSharedTerrain.setBasePoints(pointDefs);
|
|
|
|
|
if (mAwareness) {
|
|
|
|
|
int res = mSharedTerrain.getTerrain().getResolution();
|
|
|
|
|
for (auto& entry : changedPoints) {
|
|
|
|
|
WFMath::AxisBox<2> area(WFMath::Point<2>(entry.x - res, entry.y - res), WFMath::Point<2>(entry.x + res, entry.y + res));
|
|
|
|
|
mAwareness->markTilesAsDirty(std::move(area));
|
|
|
|
|
}
|
2015-09-25 21:25:04 +02:00
|
|
|
}
|
2015-09-23 22:03:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-31 21:08:12 +02:00
|
|
|
|
2015-09-23 22:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AwareMind::entityDeleted(const MemEntity& entity)
|
|
|
|
|
{
|
|
|
|
|
if (mAwareness) {
|
2015-09-26 20:37:31 +02:00
|
|
|
//log(INFO, "Removed entity.");
|
2015-09-23 22:03:31 +02:00
|
|
|
mAwareness->removeEntity(*this, entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AwareMind::onContainered(const LocatedEntity * new_loc)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 23:28:05 +01:00
|
|
|
double AwareMind::getServerTimeDiff() const
|
|
|
|
|
{
|
|
|
|
|
return mServerTimeDiff;
|
|
|
|
|
}
|
|
|
|
|
|