// TestAI.cpp : defines the implementation of the application
#include <dtUtil/mswinmacros.h>
#ifdef DELTA_WIN32
#pragma warning(push)
#pragma warning(disable : 4005)
#endif
#include "testai.h"
#include <dtCore/camera.h>
#include <dtCore/transform.h>
#include <dtCore/map.h>
#include <dtCore/project.h>
#include <dtCore/exceptionenum.h>
#include <dtUtil/datapathutils.h>
#include <dtAI/waypoint.h>
#include <dtAI/waypointmanager.h>
#include <dtAI/deltaaiinterface.h>
#include <dtUtil/mathdefines.h>
#include <dtUtil/log.h>
#include <dtUtil/fileutils.h>
#include <osg/MatrixTransform>
#ifdef DELTA_WIN32
#pragma warning(pop)
#endif
using namespace dtCore;
using namespace dtABC;
using namespace dtCore;
using namespace dtAI;
////////////////////////////////////////////////////////////////////////////////
TestAI::TestAI(const std::string& pMapFilename, const std::string& configFilename)
: Application(configFilename)
, mMapFilename(pMapFilename)
, mCharacter(0)
,_aiProxy(NULL)
{
// Generating a default config file if there isn't one already
if (!dtUtil::FileUtils::GetInstance().FileExists(configFilename))
{
GenerateDefaultConfigFile();
}
}
////////////////////////////////////////////////////////////////////////////////
TestAI::~TestAI()
{
// Clean up so that so that dead references won't get called "atexit"
dtCore::Project::GetInstance().CloseAllMaps(true);
}
////////////////////////////////////////////////////////////////////////////////
void TestAI::Config()
{
Application::Config();
mMotionModel = new OrbitMotionModel(GetKeyboard(), GetMouse());
mMotionModel->SetTarget(GetCamera());
try
{
std::string contextName = dtUtil::GetDeltaRootPath()+"/examples/data/demoMap";
Project::GetInstance().SetContext(contextName);
}
catch (const dtUtil::Exception& e)
{
e.LogException();
}
LoadDemoMap(mMapFilename);
GetScene()->AddChild(_aiProxy->GetAIInterface()-> GetDebugDrawable());
////**********************************
dtAI::AIPluginInterface * aiPlugin = _aiProxy->GetAIInterface();
for(int i = 0;i <aiPlugin->GetNumWaypoints();++i)
{
Waypoint* pWaypoint = dynamic_cast<Waypoint*>( aiPlugin->GetWaypointById(i));
int tmp = pWaypoint->GetID();
WaypointManager::GetInstance().AddWaypoint(pWaypoint);
}
for(int i = 0;i <aiPlugin->GetNumWaypoints();++i)
{
for(int j = 0;j <aiPlugin->GetNumWaypoints();++j)
{
if(aiPlugin->HasEdge(i,j))
{
// WaypointManager::GetInstance().AddPathSegment(i,j);
std::cout<<i<<" , "<<j<<std::endl;
}
}
}
WaypointManager::GetInstance().CreateNavMesh(GetScene());
/* for()
{
ContainsEdge
}*/
////***********************************
// set camera offset
dtCore::Transform trans;
trans.SetTranslation(-1.0f, 5.5f, 1.5f);
trans.SetRotation(180.0f, -2.0f, 0.0f);
GetCamera()->SetTransform(trans);
// this is needed so OSG knows which camera can generate events
GetCamera()->GetOSGCamera()->setAllowEventFocus(true);
// create overhead camera and disable it by default
dtCore::View *overheadView = new dtCore::View("overhead view");
overheadView->SetScene( GetScene() );
AddView( *overheadView );
mOverheadCamera = new dtCore::Camera();
mOverheadCamera->SetWindow(GetWindow());
mOverheadCamera->SetEnabled(false);
overheadView->SetCamera( mOverheadCamera.get() );
// this is needed so OSG knows which camera can generate events
mOverheadCamera->GetOSGCamera()->setAllowEventFocus(false);
// set overhead camera offset
trans.SetTranslation(0.0f, -5.0f, 70.0f);
trans.SetRotation(0.0f, -90.f, 0.0f);
trans.Get(mCameraOffset);
mOverheadCamera->GetMatrixNode()->setMatrix(mCameraOffset);
// get the first waypoint to spawn the character at
if (_aiProxy->GetAIInterface()->GetNumWaypoints()<1)
{
LOG_ERROR("Map '" + mMapFilename + "' does not have any valid waypoints");
exit(1);
}
Waypoint* pWaypoint = dynamic_cast<Waypoint*>(_aiProxy->GetAIInterface()->GetWaypointById(0));
std::string characterFile = dtUtil::FindFileInPathList("SkeletalMeshes/marine.xml");
mCharacter = new dtAI::AICharacter(GetScene(), pWaypoint, characterFile, 5);
// add the two Cameras as children so they get moved along with the character
mCharacter->GetCharacter()->AddChild(GetCamera());
mCharacter->GetCharacter()->AddChild(mOverheadCamera.get());
GoToWaypoint(1);
// seed the random generator
srand(4);
CreateHelpLabel();
}
////////////////////////////////////////////////////////////////////////////////
bool TestAI::KeyPressed(const dtCore::Keyboard* keyboard, int key)
{
switch (key)
{
case ' ':
if (GetCamera()->GetEnabled())
{
GetCamera()->SetEnabled(false);
mOverheadCamera->SetEnabled(true);
if (mLabel->GetActive())
{
GetCamera()->RemoveChild(mLabel.get());
mOverheadCamera->AddChild(mLabel.get());
}
}
else
{
mOverheadCamera->SetEnabled(false);
GetCamera()->SetEnabled(true);
if (mLabel->GetActive())
{
mOverheadCamera->RemoveChild(mLabel.get());
GetCamera()->AddChild(mLabel.get());
}
}
return true;
case 'n':
mDrawNavMesh = !mDrawNavMesh;
WaypointManager::GetInstance().SetDrawNavMesh(mDrawNavMesh, true);
return true;
case 'a':
if (mOverheadCamera->GetEnabled())
{
mCameraOffset(3,2) -= 1.0f;
mCameraOffset(3,1) -= 15.5f / 100.0f;
mOverheadCamera->GetMatrixNode()->setMatrix(mCameraOffset);
}
return true;
case 'z':
if (mOverheadCamera->GetEnabled())
{
mCameraOffset(3,2) += 1.0f;
mCameraOffset(3,1) += 15.5f / 100.0f;
mOverheadCamera->GetMatrixNode()->setMatrix(mCameraOffset);
}
return true;
case osgGA::GUIEventAdapter::KEY_Escape:
Quit();
return true;
case osgGA::GUIEventAdapter::KEY_F1:
mLabel->SetActive(!mLabel->GetActive());
if (mLabel->GetActive())
{
if (GetCamera()->GetEnabled() &&
GetCamera()->GetChildIndex(mLabel.get()) == GetCamera()->GetNumChildren())
{
mOverheadCamera->RemoveChild(mLabel.get());
GetCamera()->AddChild(mLabel.get());
}
else if (mOverheadCamera->GetEnabled() &&
mOverheadCamera->GetChildIndex(mLabel.get()) == mOverheadCamera->GetNumChildren())
{
GetCamera()->RemoveChild(mLabel.get());
mOverheadCamera->AddChild(mLabel.get());
}
}
return true;
default:
break;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
void TestAI::PreFrame(const double deltaFrameTime)
{
mCharacter->Update(float(deltaFrameTime));
if (mCharacter->GetCurrentWaypoint() == mCurrentWaypoint)
{
// send the character to a random waypoint
unsigned int pNumWaypoints = _aiProxy->GetAIInterface()->GetNumWaypoints()-1;
unsigned pWaypointNum = dtUtil::RandRange(0U, unsigned(pNumWaypoints));
GoToWaypoint(pWaypointNum);
}
}
////////////////////////////////////////////////////////////////////////////////
void TestAI::LoadDemoMap(const std::string& pStr)
{
try
{
std::string pContext = Project::GetInstance().GetContext();
Map& myMap = Project::GetInstance().GetMap(pStr);
// Since we are in an Application we can simply call...
LoadMap(myMap);
std::vector< dtCore::RefPtr<BaseActorObject> > proxies