gamekit/Tutorial_GameKitTraverseLogic.cpp

gamekit/Tutorial_GameKitTraverseLogic.cpp
/*
* Copyright 2016 Autodesk, Inc. All rights reserved.
* Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
* or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
*/
#include "common/oneworldenv.h"
namespace
{
// Note that in this test path are constructed, so a node should always be on the navtag.
bool IsAPathNodeOnNavTag(const Kaim::Path* path, const Kaim::GameKitNavTag& gameKitNavTag)
{
for (KyUInt32 i = 0; i < path->GetNodeCount(); ++i)
{
if (gameKitNavTag.CompareAsGameKitNavTag(path->GetNodeNavTrianglePtr(i).GetNavTag()))
return true;
}
return false;
}
#define KT_TEST_ENV_CLASS OneWorldEnv
KT_TEST_ENV {}
KT_TUTORIAL // NavTag Layers
{
KT_LOG_TITLE_BEGIN("TUTORIAL - How to setup GameKitNavTag with GameKitTraverseLogicData");
KT_ASSERT(KT_ENV.AddNavData("generated/plane200x200_80ktri/plane200x200_80ktri.NavData") != nullptr);
Kaim::GameKitNavTag noLayerNavTag; // no layer, always walkable
Kaim::GameKitNavTag navTag_exclusive;
navTag_exclusive.SetAsExclusive();
navTag_0.SetLayerID(0);
navTag_0.SetColor(Kaim::Color::Green);
navTag_1.SetLayerID(1);
navTag_1.SetColor(Kaim::Color::Violet);
Kaim::Vec2f center;
Kaim::TagVolumeInitConfig tagVolumeInitConfig;
tagVolumeInitConfig.m_world = KT_ENV.GetNavWorld();
tagVolumeInitConfig.m_altitudeMin = -1.0f;
tagVolumeInitConfig.m_altitudeMax = 1.0f;
center.Set(0.0f, 110.0f);
tagVolumeInitConfig.Init4PointsContour(center, Kaim::Vec2f::UnitX()*50.f, Kaim::Vec2f::UnitY()*60.0f);
navTag_0.GetAsDynamicNavTag(tagVolumeInitConfig.m_navTag);
Kaim::Ptr<Kaim::TagVolume> tagVolume_0 = *KY_NEW Kaim::TagVolume();
tagVolume_0->Init(tagVolumeInitConfig);
tagVolume_0->AddToWorld();
center.Set(0.0f, 30.0f);
tagVolumeInitConfig.Init4PointsContour(center, Kaim::Vec2f::UnitX()*5.f, Kaim::Vec2f::UnitY()*130.0f);
navTag_exclusive.GetAsDynamicNavTag(tagVolumeInitConfig.m_navTag);
Kaim::Ptr<Kaim::TagVolume> tagVolume_exclusive = *KY_NEW Kaim::TagVolume();
tagVolume_exclusive->Init(tagVolumeInitConfig);
tagVolume_exclusive->AddToWorld();
center.Set(0.0f, -50.0f);
tagVolumeInitConfig.Init4PointsContour(center, Kaim::Vec2f::UnitX()*50.f, Kaim::Vec2f::UnitY()*60.0f);
navTag_1.GetAsDynamicNavTag(tagVolumeInitConfig.m_navTag);
Kaim::Ptr<Kaim::TagVolume> tagVolume_1 = *KY_NEW Kaim::TagVolume();
tagVolume_1->Init(tagVolumeInitConfig);
tagVolume_1->AddToWorld();
KT_ENV.StartVisualDebugUsingLocalFile("GamkitNavTag.VisualDebug");
// wait for tag volume to be integrated into navmesh
while (KT_ENV.GetNavWorld()->GetTagVolumesToBeIntegratedCount() != 0)
KT_ENV.Update();
Kaim::Ptr<Kaim::GameKitNavTagLayerCostTable> layerCostTable = *KY_NEW Kaim::GameKitNavTagLayerCostTable();
Kaim::GameKitTraverseLogicData traverseLogicData;
traverseLogicData.SetLayerCostTable(layerCostTable); // layerCostTable can be shared amongst multiple GameKitTraverseLogicData
Kaim::Ptr<Kaim::AStarQuery<Kaim::GameKitTraverseLogic> > query = *KY_NEW Kaim::AStarQuery<Kaim::GameKitTraverseLogic>();
query->BindToDatabase(KT_ENV.GetDefaultDatabase());
query->SetTraverseLogicUserData(&traverseLogicData);
// go through navTag_0 since cost of Layers 0 and 1 is the same
query->Initialize(Kaim::Vec3f(-45.0f, 60.0f, 0.0f), Kaim::Vec3f(25.0f, 60.0f, 0.0f));
query->PerformQueryBlocking();
KT_ASSERT(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
KT_ASSERT(IsAPathNodeOnNavTag(query->GetPath(), navTag_0));
// go through navTag_1 since cost of Layers 0 is 10 times more expensive
layerCostTable->SetLayerCostMultiplier(0, 10.f);
query->Initialize(Kaim::Vec3f(-45.0f, 60.0f, 0.0f), Kaim::Vec3f(25.0f, 60.0f, 0.0f));
query->PerformQueryBlocking();
KT_ASSERT(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
KT_ASSERT(IsAPathNodeOnNavTag(query->GetPath(), navTag_1));
// go through navTag_1 even if it's cost increased
layerCostTable->SetLayerCostMultiplier(1, 2.f);
query->Initialize(Kaim::Vec3f(-45.0f, 60.0f, 0.0f), Kaim::Vec3f(25.0f, 60.0f, 0.0f));
query->PerformQueryBlocking();
KT_ASSERT(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
KT_ASSERT(IsAPathNodeOnNavTag(query->GetPath(), navTag_1));
// go through navTag_0 since NavTag 1 is forbidden
layerCostTable->ForbidLayer(1);
query->Initialize(Kaim::Vec3f(-45.0f, 60.0f, 0.0f), Kaim::Vec3f(25.0f, 60.0f, 0.0f));
query->PerformQueryBlocking();
KT_ASSERT(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
KT_ASSERT(IsAPathNodeOnNavTag(query->GetPath(), navTag_0));
// go through navTag_1 since it is re-allowed and navTag 0 still more expensive
layerCostTable->AllowLayer(1);
query->Initialize(Kaim::Vec3f(-45.0f, 60.0f, 0.0f), Kaim::Vec3f(25.0f, 60.0f, 0.0f));
query->PerformQueryBlocking();
KT_ASSERT(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
KT_ASSERT(IsAPathNodeOnNavTag(query->GetPath(), navTag_1));
// go through navTag_0
layerCostTable->SetLayerCostMultiplier(0, 1.f);
query->Initialize(Kaim::Vec3f(-45.0f, 60.0f, 0.0f), Kaim::Vec3f(25.0f, 60.0f, 0.0f));
query->PerformQueryBlocking();
KT_ASSERT(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
KT_ASSERT(IsAPathNodeOnNavTag(query->GetPath(), navTag_0));
}
}