Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bad behaviour #2120

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions Engine/source/BadBehavior/composite/ActiveSelector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 Guy Allard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------

#include "ActiveSelector.h"

using namespace BadBehavior;

//------------------------------------------------------------------------------
// Active selector node
//------------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(ActiveSelector);

ActiveSelector::ActiveSelector()
: mRecheckFrequency(0)
{
}

Task *ActiveSelector::createTask(SimObject &owner, BehaviorTreeRunner &runner)
{
return new ActiveSelectorTask(*this, owner, runner);
}

void ActiveSelector::initPersistFields()
{
addGroup( "Behavior" );

addField( "recheckFrequency", TypeS32, Offset(mRecheckFrequency, ActiveSelector),
"@brief The minimum time period in milliseconds to wait between re-evaluations of higher priority branches.");

endGroup( "Behavior" );

Parent::initPersistFields();
}

//------------------------------------------------------------------------------
// Active selector task
//------------------------------------------------------------------------------
ActiveSelectorTask::ActiveSelectorTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
: Parent(node, owner, runner),
mRecheckTime(0)
{
}

void ActiveSelectorTask::onInitialize()
{
Parent::onInitialize();

if(mBranches.empty())
{
for (VectorPtr<Task*>::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
{
mBranches.push_back(BehaviorTreeBranch(*i));
}
}

mCurrentBranch = mBranches.begin();
mRunningBranch = mBranches.end();
mRecheckTime = 0;
}


Task* ActiveSelectorTask::update()
{
// empty node, bail
if(mBranches.empty())
{
mStatus = INVALID;
return NULL;
}

// is it time to re-check higher priority branches?
if(Sim::getCurrentTime() >= mRecheckTime)
{
// pick highest priority branch
mCurrentBranch = mBranches.begin();

// determine the next recheck time
mRecheckTime = Sim::getCurrentTime() + static_cast<ActiveSelector *>(mNodeRep)->getRecheckFrequency();
}

// run a branch, if it fails move on to the next
for(mCurrentBranch; mCurrentBranch != mBranches.end(); ++mCurrentBranch)
{
// reset the branch if it's not the current running branch
if(mCurrentBranch != mRunningBranch)
mCurrentBranch->reset();

mStatus = mCurrentBranch->update();

if(mStatus == FAILURE) // move on to next
continue;

if(mStatus == RUNNING || mStatus == SUSPENDED) // track the current running branch
mRunningBranch = mCurrentBranch;

break;
}

if( (mStatus != RUNNING && mStatus != SUSPENDED) || mCurrentBranch == mBranches.end() )
mIsComplete = true;

return NULL;
}

Status ActiveSelectorTask::getStatus()
{
if(mStatus == SUSPENDED && mCurrentBranch != mBranches.end())
return mCurrentBranch->getStatus(); // suspended branch may have resumed

return mStatus;
}
92 changes: 92 additions & 0 deletions Engine/source/BadBehavior/composite/ActiveSelector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 Guy Allard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------

#ifndef _BB_ACTIVESELECTOR_H_
#define _BB_ACTIVESELECTOR_H_

#ifndef _BB_CORE_H_
#include "BadBehavior/core/Composite.h"
#endif
#ifndef _BB_BRANCH_H_
#include "BadBehavior/core/Branch.h"
#endif

//==============================================================================
// Active Selector
// Re-evaluates its children from the beginning each tick. Lower priority
// children which previously returned RUNNING are resumed if re-selected
//
// ***** TODO - This runs OK, but may need a bit more work
// -- abort previously running branches?
//
//==============================================================================

namespace BadBehavior
{
//---------------------------------------------------------------------------
// Active selector Node
//---------------------------------------------------------------------------
class ActiveSelector : public CompositeNode
{
typedef CompositeNode Parent;

protected:
U32 mRecheckFrequency;

public:
ActiveSelector();

virtual Task *createTask(SimObject &owner, BehaviorTreeRunner &runner);

static void initPersistFields();

U32 getRecheckFrequency() const { return mRecheckFrequency; }

DECLARE_CONOBJECT(ActiveSelector);
};

//---------------------------------------------------------------------------
// Active selector Task
//---------------------------------------------------------------------------
class ActiveSelectorTask : public CompositeTask
{
typedef CompositeTask Parent;

protected:
Vector<BehaviorTreeBranch>::iterator mRunningBranch;
Vector<BehaviorTreeBranch>::iterator mCurrentBranch;
Vector<BehaviorTreeBranch> mBranches;

U32 mRecheckTime;

virtual void onInitialize();
virtual Task* update();

public:
ActiveSelectorTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner);

Status getStatus();
};

} // namespace BadBehavior

#endif
173 changes: 173 additions & 0 deletions Engine/source/BadBehavior/composite/Parallel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2014 Guy Allard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------

#include "Parallel.h"

using namespace BadBehavior;

//------------------------------------------------------------------------------
// Parallel node
//------------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(Parallel);

Parallel::Parallel()
: mReturnPolicy(REQUIRE_ALL)
{
}

ImplementEnumType( ParallelReturnPolicy,
"@brief The policy to use when determining the return status of a parallel node.\n\n"
"@ingroup AI\n\n")
{ Parallel::REQUIRE_ALL, "REQUIRE_ALL", "Will return success if all children succeed.\n"
"Will terminate and return failure if one child fails \n"},
{ Parallel::REQUIRE_NONE, "REQUIRE_NONE", "Will return success even if all children fail.\n" },
{ Parallel::REQUIRE_ONE, "REQUIRE_ONE", "Will terminate and return success when one child succeeds.\n" },
EndImplementEnumType;

void Parallel::initPersistFields()
{
addGroup( "Behavior" );

addField( "returnPolicy", TYPEID< Parallel::ParallelPolicy >(), Offset(mReturnPolicy, Parallel),
"@brief The policy to use when deciding the return status for the parallel sequence.");

endGroup( "Behavior" );

Parent::initPersistFields();
}

Task *Parallel::createTask(SimObject &owner, BehaviorTreeRunner &runner)
{
return new ParallelTask(*this, owner, runner);
}

//------------------------------------------------------------------------------
// Parallel Task
//------------------------------------------------------------------------------
ParallelTask::ParallelTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
: Parent(node, owner, runner),
mHasSuccess(false),
mHasFailure(false)
{
}

void ParallelTask::onInitialize()
{
Parent::onInitialize();

mHasSuccess = mHasFailure = false;

if(mBranches.empty())
{
for (VectorPtr<Task*>::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
{
mBranches.push_back(BehaviorTreeBranch(*i));
}
}
else
{
for (Vector<BehaviorTreeBranch>::iterator it = mBranches.begin(); it != mBranches.end(); ++it)
{
it->reset();
}
}
}

Task* ParallelTask::update()
{
bool hasRunning = false, hasSuspended = false, hasResume = false;
for (Vector<BehaviorTreeBranch>::iterator it = mBranches.begin(); it != mBranches.end(); ++it)
{
Status s = it->getStatus();
if(s == INVALID || s == RUNNING || s == RESUME)
{
s = it->update();

switch(it->getStatus())
{
case SUCCESS:
mHasSuccess = true;
break;
case FAILURE:
mHasFailure = true;
break;
case RUNNING:
hasRunning = true;
break;
case SUSPENDED:
hasSuspended = true;
break;
case RESUME:
hasResume = true;
break;
}
}
}

switch(static_cast<Parallel *>(mNodeRep)->getReturnPolicy())
{
// REQUIRE_NONE
// returns SUCCESS when all children have finished irrespective of their return status.
case Parallel::REQUIRE_NONE:
mStatus = hasResume ? RESUME : ( hasRunning ? RUNNING : ( hasSuspended ? SUSPENDED : SUCCESS ) );
break;

// REQUIRE_ONE
// terminates and returns SUCCESS when any of its children succeed
// returns FAILURE if no children succeed
case Parallel::REQUIRE_ONE:
mStatus = mHasSuccess ? SUCCESS : ( hasResume ? RESUME : ( hasRunning ? RUNNING : ( hasSuspended ? SUSPENDED : FAILURE ) ) );
break;

// REQUIRE_ALL
// returns SUCCESS if all of its children succeed.
// terminates and returns failure if any of its children fail
case Parallel::REQUIRE_ALL:
mStatus = mHasFailure ? FAILURE : ( hasResume ? RESUME : ( hasRunning ? RUNNING : ( hasSuspended ? SUSPENDED : SUCCESS ) ) );
break;
}

mIsComplete = (mStatus != RUNNING && mStatus != SUSPENDED && mStatus != RESUME);

return NULL;
}


Status ParallelTask::getStatus()
{
if(mStatus == SUSPENDED)
{
// need to check if the parallel is still suspended.
// A parallel will only report SUSPENDED when all of its children are suspended
for(Vector<BehaviorTreeBranch>::iterator it = mBranches.begin(); it != mBranches.end(); ++it)
{
switch(it->getStatus())
{
case RUNNING:
return RUNNING;
case RESUME:
return RESUME;
}
}
}
return mStatus;
}
Loading