BadBehavior added

This commit is contained in:
Duion 2016-08-12 20:39:56 +02:00
parent b16f99d51c
commit eaa28844c9
63 changed files with 5698 additions and 0 deletions

View file

@ -0,0 +1,132 @@
//-----------------------------------------------------------------------------
// 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"
#include "console/consoleTypes.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;
}

View file

@ -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

View file

@ -0,0 +1,175 @@
//-----------------------------------------------------------------------------
// 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"
#include "console/engineAPI.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;
}

View file

@ -0,0 +1,97 @@
//-----------------------------------------------------------------------------
// 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_PARALLEL_H_
#define _BB_PARALLEL_H_
#ifndef _BB_CORE_H_
#include "BadBehavior/core/Composite.h"
#endif
#ifndef _BB_BRANCH_H_
#include "BadBehavior/core/Branch.h"
#endif
namespace BadBehavior
{
//---------------------------------------------------------------------------
// Parallel sequence node
// Runs all of its children irrespective of their return status
// The final return status depends on the chosen policy
// (not a true parallel, as branches are actually evaluated sequentially)
//---------------------------------------------------------------------------
class Parallel: public CompositeNode
{
typedef CompositeNode Parent;
public:
// parallel return policies
enum ParallelPolicy
{
REQUIRE_NONE,
REQUIRE_ONE,
REQUIRE_ALL,
};
protected:
ParallelPolicy mReturnPolicy;
public:
Parallel();
virtual Task *createTask(SimObject &owner, BehaviorTreeRunner &runner);
static void initPersistFields();
ParallelPolicy getReturnPolicy() const { return mReturnPolicy; }
DECLARE_CONOBJECT(Parallel);
};
//---------------------------------------------------------------------------
// Parallel Task
//---------------------------------------------------------------------------
class ParallelTask: public CompositeTask
{
typedef CompositeTask Parent;
protected:
Vector<BehaviorTreeBranch> mBranches;
bool mHasSuccess, mHasFailure;
virtual void onInitialize();
virtual Task* update();
public:
ParallelTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner);
virtual Status getStatus();
};
} // namespace BadBehavior
// make the return policy enum accessible from script
typedef BadBehavior::Parallel::ParallelPolicy ParallelReturnPolicy;
DefineEnumType( ParallelReturnPolicy );
#endif

View file

@ -0,0 +1,68 @@
//-----------------------------------------------------------------------------
// 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 "RandomSelector.h"
#include "math/mMathFn.h"
using namespace BadBehavior;
//------------------------------------------------------------------------------
// Random selector node
//------------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(RandomSelector);
Task *RandomSelector::createTask(SimObject &owner, BehaviorTreeRunner &runner)
{
return new RandomSelectorTask(*this, owner, runner);
}
//------------------------------------------------------------------------------
// Random selector task
//------------------------------------------------------------------------------
RandomSelectorTask::RandomSelectorTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
: Parent(node, owner, runner)
{
}
void RandomSelectorTask::onInitialize()
{
Parent::onInitialize();
// randomize the order of our child tasks
VectorPtr<Task *> randomChildren;
while(mChildren.size() > 0)
{
U32 index = mRandI(0, mChildren.size() - 1);
Task* child = mChildren[index];
randomChildren.push_back(child);
mChildren.erase_fast(index);
}
mChildren = randomChildren;
// normal init
mCurrentChild = mChildren.begin();
if(mCurrentChild != mChildren.end())
(*mCurrentChild)->reset();
}

View file

@ -0,0 +1,62 @@
//-----------------------------------------------------------------------------
// 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_RANDOMSELECTOR_H_
#define _BB_RANDOMSELECTOR_H_
#ifndef _BB_SELECTOR_H_
#include "Selector.h"
#endif
namespace BadBehavior
{
//---------------------------------------------------------------------------
// Random selector node
// selects its children in a random order until one of them succeeds
//---------------------------------------------------------------------------
class RandomSelector : public Selector
{
typedef Selector Parent;
public:
virtual Task *createTask(SimObject &owner, BehaviorTreeRunner &runner);
DECLARE_CONOBJECT(RandomSelector);
};
//---------------------------------------------------------------------------
// Random selector task
//---------------------------------------------------------------------------
class RandomSelectorTask : public SelectorTask
{
typedef SelectorTask Parent;
protected:
virtual void onInitialize();
public:
RandomSelectorTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner);
};
} // namespace BadBehavior
#endif

View file

@ -0,0 +1,54 @@
//-----------------------------------------------------------------------------
// 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 "Selector.h"
using namespace BadBehavior;
//------------------------------------------------------------------------------
// Selector node
//------------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(Selector);
Task *Selector::createTask(SimObject &owner, BehaviorTreeRunner &runner)
{
return new SelectorTask(*this, owner, runner);
}
//------------------------------------------------------------------------------
// Selector task
//------------------------------------------------------------------------------
SelectorTask::SelectorTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
: Parent(node, owner, runner)
{
}
void SelectorTask::onChildComplete(Status s)
{
mStatus = s;
// if child failed, move on to the next child
if(mStatus == FAILURE)
++mCurrentChild;
else
mIsComplete = true;
}

View file

@ -0,0 +1,60 @@
//-----------------------------------------------------------------------------
// 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_SELECTOR_H_
#define _BBSELECTOR_H_
#ifndef _BB_CORE_H_
#include "BadBehavior/core/Composite.h"
#endif
namespace BadBehavior
{
//---------------------------------------------------------------------------
// Selector Node
//---------------------------------------------------------------------------
class Selector : public CompositeNode
{
typedef CompositeNode Parent;
public:
virtual Task *createTask(SimObject &owner, BehaviorTreeRunner &runner);
DECLARE_CONOBJECT(Selector);
};
//---------------------------------------------------------------------------
// Selector Task
//---------------------------------------------------------------------------
class SelectorTask : public CompositeTask
{
typedef CompositeTask Parent;
public:
SelectorTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner);
virtual void onChildComplete(Status);
};
} // namespace BadBehavior
#endif

View file

@ -0,0 +1,55 @@
//-----------------------------------------------------------------------------
// 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 "Sequence.h"
using namespace BadBehavior;
//------------------------------------------------------------------------------
// Sequence node
//------------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(Sequence);
Task *Sequence::createTask(SimObject &owner, BehaviorTreeRunner &runner)
{
return new SequenceTask(*this, owner, runner);
}
//------------------------------------------------------------------------------
// Sequence Task
//------------------------------------------------------------------------------
SequenceTask::SequenceTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
: Parent(node, owner, runner)
{
}
void SequenceTask::onChildComplete(Status s)
{
mStatus = s;
// if child succeeded, move on to the next child
if(mStatus == SUCCESS)
++mCurrentChild;
else
mIsComplete = true;
}

View file

@ -0,0 +1,60 @@
//-----------------------------------------------------------------------------
// 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_SEQUENCE_H_
#define _BB_SEQUENCE_H_
#ifndef _BB_CORE_H_
#include "BadBehavior/core/Composite.h"
#endif
namespace BadBehavior
{
//---------------------------------------------------------------------------
// Sequence Node
//---------------------------------------------------------------------------
class Sequence : public CompositeNode
{
typedef CompositeNode Parent;
public:
virtual Task* createTask(SimObject &owner, BehaviorTreeRunner &runner);
DECLARE_CONOBJECT(Sequence);
};
//---------------------------------------------------------------------------
// Sequence Task
//---------------------------------------------------------------------------
class SequenceTask : public CompositeTask
{
typedef CompositeTask Parent;
public:
SequenceTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner);
virtual void onChildComplete(Status);
};
} // namespace BadBehavior
#endif