cyphesis/rules/simulation/TerrainProperty.cpp

518 lines
17 KiB
C++
Raw Permalink Normal View History

2006-01-19 Al Riddoch <alriddoch@zepler.org> * aiclient/ClientAccount.cpp, aiclient/ClientAccount.h, aiclient/ClientConnection.cpp, aiclient/ClientConnection.h, aiclient/client.cpp, aiclient/client.h, client/BaseClient.cpp, client/BaseClient.h, client/CharacterClient.cpp, client/CharacterClient.h, client/ClientConnection.cpp, client/ClientConnection.h, client/CreatorClient.cpp, client/CreatorClient.h, client/ObserverClient.cpp, client/ObserverClient.h, client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, client/client.cpp, client/define_world.cpp, client/define_world.h, common/Add.h, common/Affect.h, common/AtlasFileLoader.cpp, common/AtlasFileLoader.h, common/Attack.h, common/BaseEntity.cpp, common/BaseEntity.h, common/BaseWorld.cpp, common/BaseWorld.h, common/Burn.h, common/Chop.h, common/Connect.h, common/CustomOp.cpp, common/CustomOp.h, common/CustomOp_impl.h, common/Cut.h, common/Database.cpp, common/Database.h, common/Delve.h, common/Dig.h, common/Eat.h, common/FormattedXMLWriter.cpp, common/FormattedXMLWriter.h, common/Generic.h, common/Load.h, common/Monitor.h, common/Mow.h, common/Nourish.h, common/OOGThing.cpp, common/OOGThing.h, common/Property.cpp, common/Property.h, common/Property_impl.h, common/Save.h, common/Setup.h, common/Tick.h, common/Unseen.h, common/Update.h, common/Use.h, common/Wield.h, common/accountbase.cpp, common/accountbase.h, common/const.cpp, common/const.h, common/custom.cpp, common/debug.h, common/globals.cpp, common/globals.h, common/id.cpp, common/id.h, common/inheritance.cpp, common/inheritance.h, common/inheritance_impl.h, common/log.cpp, common/log.h, common/newid.cpp, common/nls.h, common/op_switch.h, common/operations.cpp, common/operations.h, common/random.h, common/refno.h, common/serialno.cpp, common/serialno.h, common/system.cpp, common/system.h, common/terrain_utils.cpp, common/terrain_utils.h, common/type_utils.h, common/types.h, common/utility.cpp, common/utility.h, modules/DateTime.cpp, modules/DateTime.h, modules/Location.cpp, modules/Location.h, modules/WorldTime.cpp, modules/WorldTime.h, physics/BBox.cpp, physics/BBox.h, physics/Collision.cpp, physics/Collision.h, physics/Quaternion.cpp, physics/Quaternion.h, physics/Vector3D.cpp, physics/Vector3D.h, physics/VectorPol.h, rulesets/Area.cpp, rulesets/Area.h, rulesets/AreaProperty.cpp, rulesets/AreaProperty.h, rulesets/ArithmeticScript.cpp, rulesets/ArithmeticScript.h, rulesets/BaseMind.cpp, rulesets/BaseMind.h, rulesets/CalendarProperty.cpp, rulesets/CalendarProperty.h, rulesets/Character.cpp, rulesets/Character.h, rulesets/Combat.cpp, rulesets/Combat.h, rulesets/Container.cpp, rulesets/Container.h, rulesets/Creator.cpp, rulesets/Creator.h, rulesets/Entity.cpp, rulesets/Entity.h, rulesets/EntityProperties.cpp, rulesets/Entity_getLocation.h, rulesets/Fell.cpp, rulesets/Fell.h, rulesets/Food.cpp, rulesets/Food.h, rulesets/Line.cpp, rulesets/Line.h, rulesets/LineProperty.cpp, rulesets/LineProperty.h, rulesets/MemEntity.cpp, rulesets/MemEntity.h, rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h, rulesets/MindFactory.cpp, rulesets/MindFactory.h, rulesets/Missile.h, rulesets/Motion.cpp, rulesets/Motion.h, rulesets/Movement.cpp, rulesets/Movement.h, rulesets/Pedestrian.cpp, rulesets/Pedestrian.h, rulesets/Plant.cpp, rulesets/Plant.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_EntityWrapper.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Point3D.cpp, rulesets/Py_Point3D.h, rulesets/Py_Property.cpp, rulesets/Py_Property.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_RootEntity.cpp, rulesets/Py_RootEntity.h, rulesets/Py_Statistics.cpp, rulesets/Py_Statistics.h, rulesets/Py_Task.cpp, rulesets/Py_Task.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonArithmeticScript.cpp, rulesets/PythonArithmeticScript.h, rulesets/PythonMindScript.cpp, rulesets/PythonMindScript.h, rulesets/PythonScript.cpp, rulesets/PythonScript.h, rulesets/PythonThingScript.cpp, rulesets/PythonThingScript.h, rulesets/PythonWrapper.cpp, rulesets/PythonWrapper.h, rulesets/Python_API.cpp, rulesets/Python_API.h, rulesets/Python_Script_Utils.h, rulesets/Script.cpp, rulesets/Script.h, rulesets/Stackable.cpp, rulesets/Stackable.h, rulesets/Statistics.cpp, rulesets/Statistics.h, rulesets/StatisticsProperty.cpp, rulesets/StatisticsProperty.h, rulesets/Structure.cpp, rulesets/Structure.h, rulesets/Task.cpp, rulesets/Task.h, rulesets/TaskScript.cpp, rulesets/TaskScript.h, rulesets/TerrainProperty.cpp, rulesets/TerrainProperty.h, rulesets/Thing.cpp, rulesets/Thing.h, rulesets/ThingFactory.cpp, rulesets/ThingFactory.h, rulesets/World.cpp, rulesets/World.h, rulesets/attributes.h, server/Account.cpp, server/Account.h, server/Admin.cpp, server/Admin.h, server/ArithmeticFactory.cpp, server/ArithmeticFactory.h, server/CommClient.cpp, server/CommClient.h, server/CommIdleSocket.cpp, server/CommIdleSocket.h, server/CommListener.cpp, server/CommListener.h, server/CommLocalClient.cpp, server/CommLocalClient.h, server/CommMDNSPublisher.cpp, server/CommMDNSPublisher.h, server/CommMaster.cpp, server/CommMaster.h, server/CommMetaClient.cpp, server/CommMetaClient.h, server/CommPSQLSocket.cpp, server/CommPSQLSocket.h, server/CommPeer.cpp, server/CommPeer.h, server/CommPeerListener.cpp, server/CommPeerListener.h, server/CommRemoteClient.cpp, server/CommRemoteClient.h, server/CommServer.cpp, server/CommServer.h, server/CommSlaveClient.cpp, server/CommSlaveClient.h, server/CommSlaveListener.cpp, server/CommSlaveListener.h, server/CommSocket.cpp, server/CommSocket.h, server/CommUnixListener.cpp, server/CommUnixListener.h, server/Connection.cpp, server/Connection.h, server/Connection_methods.h, server/EntityFactory.cpp, server/EntityFactory.h, server/ExternalMind.cpp, server/ExternalMind.h, server/Idle.cpp, server/Idle.h, server/Lobby.cpp, server/Lobby.h, server/Master.cpp, server/Master.h, server/Peer.cpp, server/Peer.h, server/Persistance.cpp, server/Persistance.h, server/PersistantThingFactory.cpp, server/PersistantThingFactory.h, server/PersistantThingFactory_impl.h, server/Persistor.cpp, server/Persistor.h, server/Persistor_impl.h, server/Player.cpp, server/Player.h, server/Restoration.cpp, server/Restoration.h, server/Restorer.cpp, server/Restorer.h, server/Restorer_impl.h, server/ScriptFactory.cpp, server/ScriptFactory.h, server/ServerRouting.cpp, server/ServerRouting.h, server/ServerRouting_methods.h, server/SlaveClientConnection.cpp, server/SlaveClientConnection.h, server/TaskFactory.cpp, server/TaskFactory.h, server/TrustedConnection.cpp, server/TrustedConnection.h, server/WorldRouter.cpp, server/WorldRouter.h, server/server.cpp, server/server.h, server/slave.cpp, tests/Areatest.cpp, tests/BaseEntitytest.cpp, tests/Charactertest.cpp, tests/Containertest.cpp, tests/Creatortest.cpp, tests/EntityExerciser.h, tests/EntityFactorytest.cpp, tests/Entitytest.cpp, tests/Foodtest.cpp, tests/IGEntityExerciser.h, tests/Linetest.cpp, tests/Locationtest.cpp, tests/OOGThingtest.cpp, tests/Planttest.cpp, tests/Propertytest.cpp, tests/Stackabletest.cpp, tests/Structuretest.cpp, tests/Tasktest.cpp, tests/TestWorld.h, tests/Thingtest.cpp, tests/WorldTimetest.cpp, tests/Worldtest.cpp, tests/allOperations.h, tests/collisiontest.cpp, tests/consttest.cpp, tests/distancetest.cpp, tests/emergencetest.cpp, tests/inheritancetest.cpp, tests/logtest.cpp, tests/randomtest.cpp, tests/systemtest.cpp, tests/transformtest.cpp, tests/type_utilstest.cpp, tests/utilitytest.cpp, tools/AdminClient.cpp, tools/AdminClient.h, tools/cyaddrules.cpp, tools/cycmd.cpp, tools/cyconfig.cpp, tools/cyconvertrules.cpp, tools/cyctrl.cpp, tools/cydbload.cpp, tools/cydumprules.cpp, tools/cyisoload.cpp, tools/cyloadrules.cpp, tools/cypasswd.cpp, tools/cywatchdog.cpp: Replace the copyright and license statements with new versions that include disclaimer of warranty, and permit distribution under later versions of the GPL.
2006-01-19 02:17:06 +00:00
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2004 Alistair Riddoch
2006-01-19 Al Riddoch <alriddoch@zepler.org> * aiclient/ClientAccount.cpp, aiclient/ClientAccount.h, aiclient/ClientConnection.cpp, aiclient/ClientConnection.h, aiclient/client.cpp, aiclient/client.h, client/BaseClient.cpp, client/BaseClient.h, client/CharacterClient.cpp, client/CharacterClient.h, client/ClientConnection.cpp, client/ClientConnection.h, client/CreatorClient.cpp, client/CreatorClient.h, client/ObserverClient.cpp, client/ObserverClient.h, client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, client/client.cpp, client/define_world.cpp, client/define_world.h, common/Add.h, common/Affect.h, common/AtlasFileLoader.cpp, common/AtlasFileLoader.h, common/Attack.h, common/BaseEntity.cpp, common/BaseEntity.h, common/BaseWorld.cpp, common/BaseWorld.h, common/Burn.h, common/Chop.h, common/Connect.h, common/CustomOp.cpp, common/CustomOp.h, common/CustomOp_impl.h, common/Cut.h, common/Database.cpp, common/Database.h, common/Delve.h, common/Dig.h, common/Eat.h, common/FormattedXMLWriter.cpp, common/FormattedXMLWriter.h, common/Generic.h, common/Load.h, common/Monitor.h, common/Mow.h, common/Nourish.h, common/OOGThing.cpp, common/OOGThing.h, common/Property.cpp, common/Property.h, common/Property_impl.h, common/Save.h, common/Setup.h, common/Tick.h, common/Unseen.h, common/Update.h, common/Use.h, common/Wield.h, common/accountbase.cpp, common/accountbase.h, common/const.cpp, common/const.h, common/custom.cpp, common/debug.h, common/globals.cpp, common/globals.h, common/id.cpp, common/id.h, common/inheritance.cpp, common/inheritance.h, common/inheritance_impl.h, common/log.cpp, common/log.h, common/newid.cpp, common/nls.h, common/op_switch.h, common/operations.cpp, common/operations.h, common/random.h, common/refno.h, common/serialno.cpp, common/serialno.h, common/system.cpp, common/system.h, common/terrain_utils.cpp, common/terrain_utils.h, common/type_utils.h, common/types.h, common/utility.cpp, common/utility.h, modules/DateTime.cpp, modules/DateTime.h, modules/Location.cpp, modules/Location.h, modules/WorldTime.cpp, modules/WorldTime.h, physics/BBox.cpp, physics/BBox.h, physics/Collision.cpp, physics/Collision.h, physics/Quaternion.cpp, physics/Quaternion.h, physics/Vector3D.cpp, physics/Vector3D.h, physics/VectorPol.h, rulesets/Area.cpp, rulesets/Area.h, rulesets/AreaProperty.cpp, rulesets/AreaProperty.h, rulesets/ArithmeticScript.cpp, rulesets/ArithmeticScript.h, rulesets/BaseMind.cpp, rulesets/BaseMind.h, rulesets/CalendarProperty.cpp, rulesets/CalendarProperty.h, rulesets/Character.cpp, rulesets/Character.h, rulesets/Combat.cpp, rulesets/Combat.h, rulesets/Container.cpp, rulesets/Container.h, rulesets/Creator.cpp, rulesets/Creator.h, rulesets/Entity.cpp, rulesets/Entity.h, rulesets/EntityProperties.cpp, rulesets/Entity_getLocation.h, rulesets/Fell.cpp, rulesets/Fell.h, rulesets/Food.cpp, rulesets/Food.h, rulesets/Line.cpp, rulesets/Line.h, rulesets/LineProperty.cpp, rulesets/LineProperty.h, rulesets/MemEntity.cpp, rulesets/MemEntity.h, rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h, rulesets/MindFactory.cpp, rulesets/MindFactory.h, rulesets/Missile.h, rulesets/Motion.cpp, rulesets/Motion.h, rulesets/Movement.cpp, rulesets/Movement.h, rulesets/Pedestrian.cpp, rulesets/Pedestrian.h, rulesets/Plant.cpp, rulesets/Plant.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_EntityWrapper.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Point3D.cpp, rulesets/Py_Point3D.h, rulesets/Py_Property.cpp, rulesets/Py_Property.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_RootEntity.cpp, rulesets/Py_RootEntity.h, rulesets/Py_Statistics.cpp, rulesets/Py_Statistics.h, rulesets/Py_Task.cpp, rulesets/Py_Task.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonArithmeticScript.cpp, rulesets/PythonArithmeticScript.h, rulesets/PythonMindScript.cpp, rulesets/PythonMindScript.h, rulesets/PythonScript.cpp, rulesets/PythonScript.h, rulesets/PythonThingScript.cpp, rulesets/PythonThingScript.h, rulesets/PythonWrapper.cpp, rulesets/PythonWrapper.h, rulesets/Python_API.cpp, rulesets/Python_API.h, rulesets/Python_Script_Utils.h, rulesets/Script.cpp, rulesets/Script.h, rulesets/Stackable.cpp, rulesets/Stackable.h, rulesets/Statistics.cpp, rulesets/Statistics.h, rulesets/StatisticsProperty.cpp, rulesets/StatisticsProperty.h, rulesets/Structure.cpp, rulesets/Structure.h, rulesets/Task.cpp, rulesets/Task.h, rulesets/TaskScript.cpp, rulesets/TaskScript.h, rulesets/TerrainProperty.cpp, rulesets/TerrainProperty.h, rulesets/Thing.cpp, rulesets/Thing.h, rulesets/ThingFactory.cpp, rulesets/ThingFactory.h, rulesets/World.cpp, rulesets/World.h, rulesets/attributes.h, server/Account.cpp, server/Account.h, server/Admin.cpp, server/Admin.h, server/ArithmeticFactory.cpp, server/ArithmeticFactory.h, server/CommClient.cpp, server/CommClient.h, server/CommIdleSocket.cpp, server/CommIdleSocket.h, server/CommListener.cpp, server/CommListener.h, server/CommLocalClient.cpp, server/CommLocalClient.h, server/CommMDNSPublisher.cpp, server/CommMDNSPublisher.h, server/CommMaster.cpp, server/CommMaster.h, server/CommMetaClient.cpp, server/CommMetaClient.h, server/CommPSQLSocket.cpp, server/CommPSQLSocket.h, server/CommPeer.cpp, server/CommPeer.h, server/CommPeerListener.cpp, server/CommPeerListener.h, server/CommRemoteClient.cpp, server/CommRemoteClient.h, server/CommServer.cpp, server/CommServer.h, server/CommSlaveClient.cpp, server/CommSlaveClient.h, server/CommSlaveListener.cpp, server/CommSlaveListener.h, server/CommSocket.cpp, server/CommSocket.h, server/CommUnixListener.cpp, server/CommUnixListener.h, server/Connection.cpp, server/Connection.h, server/Connection_methods.h, server/EntityFactory.cpp, server/EntityFactory.h, server/ExternalMind.cpp, server/ExternalMind.h, server/Idle.cpp, server/Idle.h, server/Lobby.cpp, server/Lobby.h, server/Master.cpp, server/Master.h, server/Peer.cpp, server/Peer.h, server/Persistance.cpp, server/Persistance.h, server/PersistantThingFactory.cpp, server/PersistantThingFactory.h, server/PersistantThingFactory_impl.h, server/Persistor.cpp, server/Persistor.h, server/Persistor_impl.h, server/Player.cpp, server/Player.h, server/Restoration.cpp, server/Restoration.h, server/Restorer.cpp, server/Restorer.h, server/Restorer_impl.h, server/ScriptFactory.cpp, server/ScriptFactory.h, server/ServerRouting.cpp, server/ServerRouting.h, server/ServerRouting_methods.h, server/SlaveClientConnection.cpp, server/SlaveClientConnection.h, server/TaskFactory.cpp, server/TaskFactory.h, server/TrustedConnection.cpp, server/TrustedConnection.h, server/WorldRouter.cpp, server/WorldRouter.h, server/server.cpp, server/server.h, server/slave.cpp, tests/Areatest.cpp, tests/BaseEntitytest.cpp, tests/Charactertest.cpp, tests/Containertest.cpp, tests/Creatortest.cpp, tests/EntityExerciser.h, tests/EntityFactorytest.cpp, tests/Entitytest.cpp, tests/Foodtest.cpp, tests/IGEntityExerciser.h, tests/Linetest.cpp, tests/Locationtest.cpp, tests/OOGThingtest.cpp, tests/Planttest.cpp, tests/Propertytest.cpp, tests/Stackabletest.cpp, tests/Structuretest.cpp, tests/Tasktest.cpp, tests/TestWorld.h, tests/Thingtest.cpp, tests/WorldTimetest.cpp, tests/Worldtest.cpp, tests/allOperations.h, tests/collisiontest.cpp, tests/consttest.cpp, tests/distancetest.cpp, tests/emergencetest.cpp, tests/inheritancetest.cpp, tests/logtest.cpp, tests/randomtest.cpp, tests/systemtest.cpp, tests/transformtest.cpp, tests/type_utilstest.cpp, tests/utilitytest.cpp, tools/AdminClient.cpp, tools/AdminClient.h, tools/cyaddrules.cpp, tools/cycmd.cpp, tools/cyconfig.cpp, tools/cyconvertrules.cpp, tools/cyctrl.cpp, tools/cydbload.cpp, tools/cydumprules.cpp, tools/cyisoload.cpp, tools/cyloadrules.cpp, tools/cypasswd.cpp, tools/cywatchdog.cpp: Replace the copyright and license statements with new versions that include disclaimer of warranty, and permit distribution under later versions of the GPL.
2006-01-19 02:17:06 +00:00
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "TerrainProperty.h"
#include "rules/LocatedEntity.h"
#include "rules/Domain.h"
#include "rules/simulation/BaseWorld.h"
#include "common/debug.h"
#include "common/custom.h"
#include "common/TypeNode.h"
2018-10-30 20:12:45 +01:00
#include "common/operations/Nourish.h"
#include "modules/TerrainContext.h"
#include <Mercator/Terrain.h>
#include <Mercator/Segment.h>
#include <Mercator/Surface.h>
#include <Mercator/TileShader.h>
#include <Mercator/FillShader.h>
#include <Mercator/ThresholdShader.h>
#include <Mercator/DepthShader.h>
#include <Mercator/GrassShader.h>
#include <Atlas/Objects/Anonymous.h>
static const bool debug_flag = false;
using Atlas::Message::Element;
using Atlas::Message::MapType;
using Atlas::Message::ListType;
using Atlas::Message::FloatType;
using Atlas::Objects::Operation::Nourish;
using Atlas::Objects::Entity::Anonymous;
typedef Mercator::Terrain::Pointstore Pointstore;
typedef Mercator::Terrain::Pointcolumn Pointcolumn;
typedef enum { ROCK = 0, SAND = 1, GRASS = 2, SILT = 3, SNOW = 4} Surface;
TerrainProperty::TerrainProperty(const TerrainProperty& rhs) :
m_data(*new Mercator::Terrain(Mercator::Terrain::SHADED)),
m_tileShader(nullptr)
{
//Copy all points.
for (auto& pointColumn : rhs.m_data.getPoints()) {
for (auto& point : pointColumn.second) {
m_data.setBasePoint(pointColumn.first, point.first, point.second);
}
}
//Copy surface if available, as well as surface data.
if (!rhs.m_surfaces.empty()) {
m_surfaces = rhs.m_surfaces;
m_tileShader = createShaders(m_surfaces);
m_data.addShader(m_tileShader, 0);
}
}
/// \brief TerrainProperty constructor
TerrainProperty::TerrainProperty() :
m_data(*new Mercator::Terrain(Mercator::Terrain::SHADED)),
m_tileShader(nullptr)
{
}
TerrainProperty::~TerrainProperty()
{
delete &m_data;
delete m_tileShader;
}
void TerrainProperty::install(LocatedEntity *owner, const std::string &name)
{
owner->installDelegate(Atlas::Objects::Operation::EAT_NO, name);
}
void TerrainProperty::remove(LocatedEntity *owner, const std::string & name)
{
owner->removeDelegate(Atlas::Objects::Operation::EAT_NO, name);
}
2011-09-29 17:36:26 +01:00
int TerrainProperty::get(Element & ent) const
{
MapType & t = (ent = MapType()).asMap();
MapType & terrain = (t["points"] = MapType()).asMap();
const Pointstore & points = m_data.getPoints();
2016-10-06 21:54:57 +02:00
for (const auto& column : points) {
for (const auto point : column.second) {
const Mercator::BasePoint& bp = point.second;
std::stringstream key;
2016-10-06 21:54:57 +02:00
key << column.first << "x" << point.first;
size_t size = 3;
bool sendRoughness = false;
bool sendFalloff = false;
if (bp.falloff() != Mercator::BasePoint::FALLOFF) {
size = 5;
sendRoughness = true;
sendFalloff = true;
} else if (bp.roughness() != Mercator::BasePoint::ROUGHNESS) {
size = 4;
sendRoughness = true;
}
ListType & pointElem = (terrain[key.str()] = ListType(size)).List();
pointElem[0] = (FloatType)(column.first);
pointElem[1] = (FloatType)(point.first);
pointElem[2] = (FloatType)(bp.height());
if (sendRoughness) {
pointElem[3] = bp.roughness();
}
if (sendFalloff) {
pointElem[4] = bp.falloff();
}
}
}
t["surfaces"] = m_surfaces;
2011-09-29 17:36:26 +01:00
return 0;
}
void TerrainProperty::set(const Element & ent)
{
if (!ent.isMap()) {
return;
}
const MapType & t = ent.asMap();
debug(std::cout << "TerrainProperty::setTerrain()"
<< std::endl << std::flush;);
const Pointstore & base_points = m_data.getPoints();
int minX = std::numeric_limits<int>::max();
int maxX = std::numeric_limits<int>::min();
int minY = std::numeric_limits<int>::max();
int maxY = std::numeric_limits<int>::min();
2018-07-24 23:00:06 +02:00
auto I = t.find("points");
if (I != t.end() && I->second.isMap()) {
const MapType & points = I->second.asMap();
2018-07-24 23:00:06 +02:00
auto Iend = points.end();
2018-08-17 22:04:07 +02:00
for (auto pointsI = points.begin(); pointsI != Iend; ++pointsI) {
if (!pointsI->second.isList()) {
continue;
}
2018-08-17 22:04:07 +02:00
const ListType & point = pointsI->second.asList();
2016-10-06 21:54:57 +02:00
if (point.size() < 3) {
continue;
}
2016-12-19 21:55:08 +01:00
if (!point[0].isNum() || !point[1].isNum() || !point[2].isNum()) {
continue;
}
int x = (int)point[0].asNum();
int y = (int)point[1].asNum();
2016-12-19 21:55:08 +01:00
double h = point[2].asNum();
2016-10-06 21:54:57 +02:00
double roughness;
double falloff;
if (point.size() > 3) {
roughness = point[3].asFloat();
} else {
roughness = Mercator::BasePoint::ROUGHNESS;
}
if (point.size() > 4) {
falloff = point[4].asFloat();
} else {
falloff = Mercator::BasePoint::FALLOFF;
}
Mercator::BasePoint bp(h, roughness, falloff);
2018-07-24 23:00:06 +02:00
auto J = base_points.find(x);
if (J == base_points.end() ||
J->second.find(y) == J->second.end()) {
// Newly added point.
m_createdTerrain[x].insert(y);
} else {
// Modified point
PointSet::const_iterator K = m_createdTerrain.find(x);
if (K == m_createdTerrain.end() ||
K->second.find(y) == K->second.end()) {
// Already in database
m_modifiedTerrain[x].insert(y);
}
// else do nothing, as its currently waiting to be added.
}
minX = std::min(minX, x);
maxX = std::max(maxX, x);
minY = std::min(minY, y);
maxY = std::max(maxY, y);
2016-10-06 21:54:57 +02:00
m_data.setBasePoint(x, y, bp);
}
}
if (minX != std::numeric_limits<int>::max()) {
float spacing = m_data.getSpacing();
2016-09-27 22:44:31 +02:00
WFMath::Point<2> minCorner((minX * spacing) - (spacing * 0.5f), (minY * spacing) - (spacing * 0.5f));
WFMath::Point<2> maxCorner((maxX * spacing) + (spacing * 0.5f), (maxY * spacing) + (spacing * 0.5f));
WFMath::AxisBox<2> changedArea(minCorner, maxCorner);
m_changedAreas.push_back(changedArea);
}
I = t.find("surfaces");
if (I != t.end() && I->second.isList()) {
//Only alter shader if the definition has changed.
if (m_surfaces != I->second.List()) {
auto shader = createShaders(I->second.List());
if (m_tileShader) {
m_data.removeShader(m_tileShader, 0);
delete m_tileShader;
}
m_tileShader = shader;
if (shader) {
m_data.addShader(shader, 0);
}
m_surfaces = I->second.List();
}
}
}
void TerrainProperty::apply(LocatedEntity* entity) {
if (!m_changedAreas.empty()) {
Domain* domain = entity->getDomain();
if (domain) {
domain->refreshTerrain(m_changedAreas);
m_changedAreas.clear();
}
}
}
Mercator::TileShader* TerrainProperty::createShaders(const Atlas::Message::ListType& surfaceList) {
if (!surfaceList.empty()) {
2018-08-17 22:04:07 +02:00
auto* tileShader = new Mercator::TileShader();
for (auto& surfaceElement : surfaceList) {
if (!surfaceElement.isMap()) {
continue;
}
auto& surfaceMap = surfaceElement.Map();
auto patternI = surfaceMap.find("pattern");
if (patternI == surfaceMap.end() || !patternI->second.isString()) {
log(WARNING, "Surface has no 'pattern'.");
continue;
}
auto nameI = surfaceMap.find("name");
if (nameI == surfaceMap.end() || !nameI->second.isString()) {
log(WARNING, "Surface has no 'name'.");
continue;
}
const std::string& name = nameI->second.String();
int layer;
if (name == "rock") {
layer = ROCK;
} else if (name == "sand") {
layer = SAND;
} else if (name == "grass") {
layer = GRASS;
} else if (name == "silt") {
layer = SILT;
} else if (name == "snow") {
layer = SNOW;
} else {
log(WARNING, String::compose("Could not recognize surface with layer with name '%1'", name));
continue;
}
Mercator::Shader::Parameters shaderParams;
auto paramsI = surfaceMap.find("params");
if (paramsI != surfaceMap.end() && paramsI->second.isMap()) {
auto params = paramsI->second.Map();
for (auto& entry : params) {
if (entry.second.isNum()) {
shaderParams.insert(std::make_pair(entry.first, (float)entry.second.asNum()));
} else {
log(WARNING, "'terrain.shaders...params' entry must be a map of floats..");
}
}
}
auto& pattern = patternI->second.String();
if (pattern == "fill") {
tileShader->addShader(new Mercator::FillShader(shaderParams), layer);
} else if (pattern == "band") {
tileShader->addShader(new Mercator::BandShader(shaderParams), layer);
} else if (pattern == "grass") {
tileShader->addShader(new Mercator::GrassShader(shaderParams), layer);
} else if (pattern == "depth") {
tileShader->addShader(new Mercator::DepthShader(shaderParams), layer);
} else if (pattern == "high") {
tileShader->addShader(new Mercator::HighShader(shaderParams), layer);
} else {
log(WARNING, String::compose("Could not recognize surface with pattern '%1'", pattern));
continue;
}
}
return tileShader;
}
return nullptr;
}
TerrainProperty * TerrainProperty::copy() const
{
return new TerrainProperty(*this);
}
HandlerResult TerrainProperty::operation(LocatedEntity * e,
const Operation & op, OpVector & res)
{
return eat_handler(e, op, res);
}
2016-09-13 21:39:46 +02:00
void TerrainProperty::addMod(long id, const Mercator::TerrainMod *mod) const
{
2016-09-13 21:39:46 +02:00
m_data.updateMod(id, mod);
}
2016-09-13 21:39:46 +02:00
void TerrainProperty::updateMod(long id, const Mercator::TerrainMod *mod) const
{
2016-09-13 21:39:46 +02:00
m_data.updateMod(id, mod);
}
2016-09-13 21:39:46 +02:00
void TerrainProperty::removeMod(long id) const
{
2016-09-13 21:39:46 +02:00
m_data.updateMod(id, nullptr);
}
void TerrainProperty::clearMods(float x, float y)
{
2016-09-21 10:39:16 +02:00
Mercator::Segment *s = m_data.getSegmentAtPos(x,y);
2018-01-30 16:54:49 +01:00
if(s != nullptr) {
s->clearMods();
//log(INFO, "Mods cleared!");
}
}
/// \brief Return the height and normal to the surface at the given point
bool TerrainProperty::getHeightAndNormal(float x,
float y,
float & height,
Vector3D & normal) const
{
2018-07-24 23:00:06 +02:00
auto s = m_data.getSegmentAtPos(x, y);
if (s && !s->isValid()) {
s->populate();
}
return m_data.getHeightAndNormal(x, y, height, normal);
}
2018-07-24 23:00:06 +02:00
bool TerrainProperty::getHeight(float x, float y, float& height) const
{
auto s = m_data.getSegmentAtPos(x, y);
if (s && !s->isValid()) {
s->populate();
}
return m_data.getHeight(x, y, height);
}
/// \brief Get a number encoding the surface type at the given x,z coordinates
///
/// @param pos the x,z coordinates of the point on the terrain
/// @param material a reference to the integer to be used to store the
/// material identifier at this location.
2018-07-24 23:00:06 +02:00
boost::optional<int> TerrainProperty::getSurface(float x, float z) const
{
2018-01-10 15:11:20 +01:00
Mercator::Segment * segment = m_data.getSegmentAtPos(x, z);
2018-05-01 14:22:06 +02:00
if (segment == nullptr) {
debug(std::cerr << "No terrain at this point" << std::endl << std::flush;);
2018-07-24 23:00:06 +02:00
return boost::none;
}
if (!segment->isValid()) {
segment->populate();
}
x -= segment->getXRef();
2018-01-10 15:11:20 +01:00
z -= segment->getZRef();
assert(x <= segment->getSize());
2018-01-10 15:11:20 +01:00
assert(z <= segment->getSize());
const Mercator::Segment::Surfacestore & surfaces = segment->getSurfaces();
WFMath::Vector<3> normal;
float height = -23;
2018-01-10 15:11:20 +01:00
segment->getHeightAndNormal(x, z, height, normal);
debug(std::cout << "At the point " << x << "," << z
<< " of the segment the height is " << height << std::endl;
std::cout << "The segment has " << surfaces.size()
<< std::endl << std::flush;);
2018-05-01 14:22:06 +02:00
if (surfaces.empty()) {
log(ERROR, "The terrain has no surface data");
2018-07-24 23:00:06 +02:00
return boost::none;
}
Mercator::Surface & tile_surface = *surfaces.begin()->second;
if (!tile_surface.isValid()) {
tile_surface.populate();
}
2018-07-24 23:00:06 +02:00
return tile_surface((int)x, (int)z, 0);
}
2018-07-24 23:00:06 +02:00
boost::optional<std::vector<LocatedEntity*>> TerrainProperty::findMods(float x, float z) const
{
2018-07-24 23:00:06 +02:00
Mercator::Segment * seg = m_data.getSegmentAtPos(x, z);
if (seg == nullptr) {
return boost::none;
}
2018-07-24 23:00:06 +02:00
std::vector<LocatedEntity*> ret;
2016-09-13 21:39:46 +02:00
auto& seg_mods = seg->getMods();
for (auto& entry : seg_mods) {
const Mercator::TerrainMod * mod = entry.second;
WFMath::AxisBox<2> mod_box = mod->bbox();
2018-07-24 23:00:06 +02:00
if (x > mod_box.lowCorner().x() && x < mod_box.highCorner().x() &&
z > mod_box.lowCorner().y() && z < mod_box.highCorner().y()) {
Mercator::Effector::Context * c = mod->context();
2018-07-24 23:00:06 +02:00
if (c == nullptr) {
log(WARNING, "Terrrain mod with no context");
continue;
}
debug(std::cout << "Context has id" << c->id() << std::endl;);
2018-07-24 23:00:06 +02:00
auto tc = dynamic_cast<TerrainContext *>(c);
if (tc == nullptr) {
log(WARNING, "Terrrain mod with non Cyphesis context");
continue;
}
debug(std::cout << "Context has pointer " << tc->entity().get()
<< std::endl;);
ret.push_back(tc->entity().get());
}
}
2018-07-24 23:00:06 +02:00
return ret;
}
HandlerResult TerrainProperty::eat_handler(LocatedEntity * e,
const Operation & op, OpVector & res)
{
const std::string & from_id = op->getFrom();
auto from = BaseWorld::instance().getEntity(from_id);
if (!from) {
2018-05-01 13:19:53 +02:00
//The eating entity could have been destroyed in the interim.
debug_print(String::compose("Terrain got eat op from non-existent "
"entity %1.", from_id));
return OPERATION_IGNORED;
}
Point3D from_pos = relativePos(e->m_location, from->m_location);
2018-07-24 23:00:06 +02:00
auto material = getSurface(from_pos.x(), from_pos.z());
if (!material) {
debug(std::cout << "no surface hit" << std::endl << std::flush;);
return OPERATION_IGNORED;
}
const TypeNode * from_type = from->getType();
if (from_type->isTypeOf("plant")) {
2018-07-24 23:00:06 +02:00
if (*material == GRASS) {
debug(std::cout << "From grass" << std::endl << std::flush;);
Nourish nourish;
nourish->setTo(from_id);
Anonymous nour_arg;
nour_arg->setAttr("eat_type", "plant");
Element mass;
from->getAttr("mass", mass);
if (!mass.isFloat()) {
mass = 0.;
}
// FIXME to do this right we need to know how long since the
// last tick, so the from entity needs to tell us.
nour_arg->setAttr("mass",
std::pow(mass.Float(), 0.5) /
(60.0 * 24.0));
nourish->setArgs1(nour_arg);
res.push_back(nourish);
}
} else if (from_type->isTypeOf("character")) {
log(NOTICE, "Eat coming from an animal.");
2018-07-24 23:00:06 +02:00
if (*material == GRASS) {
debug(std::cout << "From grass" << std::endl << std::flush;);
}
}
return OPERATION_HANDLED;
}
2016-03-31 21:08:12 +02:00
Mercator::Terrain& TerrainProperty::getData()
{
return m_data;
}
Mercator::Terrain& TerrainProperty::getData() const
2016-03-31 21:08:12 +02:00
{
return m_data;
}