gamekit/Tutorial_GameKitTraverseLogic.cpp

gamekit/Tutorial_GameKitTraverseLogic.cpp
/*
* Copyright 2015 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"
//RUN_THIS_FILE
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::DynamicNavTag& navTag)
{
for (KyUInt32 i = 0; i < path->GetNodeCount(); ++i)
{
if (path->GetNodeNavTrianglePtr(i).GetNavTag() == navTag)
return true;
}
return false;
}
#define TEST_ENV_CLASS OneWorldEnv
TEST_ENV {}
TUTORIAL // NavTag Layers
{
KT_LOG_TITLE_BEGIN("TUTORIAL - How to setup GameKiNavTag with GameKitTraverseLogicData");
CHECK(env.LoadAndAddNavData("GeneratedNavData/plan/plan.NavData") != KY_NULL);
Kaim::GameKitNavTag noLayerNavTag; // no layer, always walkable
Kaim::GameKitNavTag navTag_exclusive;
navTag_exclusive.SetAsExclusive();
navTag_0.SetLayerID(0);
navTag_0.SetColor(Kaim::VisualColor::LimeGreen);
navTag_1.SetLayerID(1);
navTag_1.SetColor(Kaim::VisualColor::Orchid);
Kaim::Vec2f center;
Kaim::TagVolumeInitConfig tagVolumeInitConfig;
tagVolumeInitConfig.m_world = env.GetWorld();
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);
tagVolumeInitConfig.m_navTag = navTag_0.GetDynamicNavTag();
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);
tagVolumeInitConfig.m_navTag = navTag_exclusive.GetDynamicNavTag();
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);
tagVolumeInitConfig.m_navTag = navTag_1.GetDynamicNavTag();
Kaim::Ptr<Kaim::TagVolume> tagVolume_1 = *KY_NEW Kaim::TagVolume();
tagVolume_1->Init(tagVolumeInitConfig);
tagVolume_1->AddToWorld();
env.StartVisualDebugUsingLocalFile("GamkitNavTag.VisualDebug");
// wait for tag volume to be integrated into navmesh
while (env.GetWorld()->GetTagVolumesToBeIntegratedCount() != 0)
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(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();
CHECK(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
CHECK(IsAPathNodeOnNavTag(query->GetPath(), navTag_0.GetDynamicNavTag()));
// 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();
CHECK(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
CHECK(IsAPathNodeOnNavTag(query->GetPath(), navTag_1.GetDynamicNavTag()));
// 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();
CHECK(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
CHECK(IsAPathNodeOnNavTag(query->GetPath(), navTag_1.GetDynamicNavTag()));
// 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();
CHECK(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
CHECK(IsAPathNodeOnNavTag(query->GetPath(), navTag_0.GetDynamicNavTag()));
// 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();
CHECK(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
CHECK(IsAPathNodeOnNavTag(query->GetPath(), navTag_1.GetDynamicNavTag()));
// 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();
CHECK(query->GetPathFinderResult() == Kaim::IPathFinderQuery::PathFinderSuccess);
CHECK(IsAPathNodeOnNavTag(query->GetPath(), navTag_0.GetDynamicNavTag()));
}
}