public override BTState Update() { BT selected; if (lastContinue != null) { selected = lastContinue; } else { selected = child[UnityEngine.Random.Range(0, childSize - 1)]; } BTState state = selected.Update(); if (state == BTState.CONTINUE) { lastContinue = selected; } else { lastContinue = null; } return(state); }
static LandPredatorBrain() { FindAndAttackEnemyTree = BT.Selector("Select Attacking", BT.If("Looking for an enemy", PredatorBehaviors.FindEnemy, BT.Selector("Try attacking", BT.If("Ready For Attack", PredatorBehaviors.CheckAttacking, BT.Selector("Attack Enemy", BT.If("Try Wake Up", x => ((x.State == AnimalState.Sleeping || x.State == AnimalState.LyingDown) && x.Alertness < Animal.WakeUpAlertness, "sleeping/lying and alert"), Anim(AnimalState.Idle, true, x => x.Species.TimeLayToStand)), BT.If("Try Make Route", PredatorBehaviors.ChasePrey) ) ), PredatorBehaviors.TryFleeing ) )); LandPredatorTreeRoot = BT.Selector("Land Predator Brain", MovementBehaviors.Flee, FindAndAttackEnemyTree, FindAndEatCorpse, LyingDownTree, MovementBehaviors.TryReturnHome, RelaxBehaviors.Relax, MovementBehaviors.Wander, RelaxBehaviors.Idle ); }
private void OnEnable() { m_EnemyBehaviour = GetComponent <SupportBot>(); m_Animator = GetComponent <Animator>(); m_Ai.OpenBranch( //BT.If(() => { return m_EnemyBehaviour.Target != null; }).OpenBranch( // BT.Call(m_EnemyBehaviour.CheckTargetStillVisible), // BT.Call(m_EnemyBehaviour.OrientToTarget), // BT.If(() => { return m_EnemyBehaviour.CheckLaunchMode() == true; }).OpenBranch( // BT.Call(() => { m_EnemyBehaviour.StartLaunch(true); }) // ), // BT.If(() => { return m_EnemyBehaviour.CheckLaunchMode() == false; }).OpenBranch( // BT.Call(() => { m_EnemyBehaviour.StartLaunch(false); }) // ), // BT.Call(m_EnemyBehaviour.RememberTargetPos) //), BT.If(() => { return(m_EnemyBehaviour.Target == null); }).OpenBranch( BT.RandomSequence().OpenBranch( BT.Sequence().OpenBranch( BT.SetBool(m_Animator, "Patrol", true), BT.Call(() => m_EnemyBehaviour.SetFacingData(Random.Range(0f, 1f) > 0.5f ? -1 : 1)) ), BT.SetBool(m_Animator, "Patrol", false) ), BT.Wait(Random.Range(3f, 5f)) ) ); }
IEnumerator LateStart() { yield return(new WaitUntil(delegate() { m_Arrester = Array.Find( GameObject.FindGameObjectsWithTag("Character"), delegate(GameObject go) { return go.GetComponent <Character>().GetID() == m_ArresterID; } ); return m_Arrester != null; })); yield return(new WaitUntil(delegate() { m_Player = Array.Find( GameObject.FindGameObjectsWithTag("Character"), delegate(GameObject go) { return go.GetComponent <Character>().GetID() == 0; } ); return m_Player != null; })); m_PlayerInitPos = m_Player.transform.position; m_AI = GetComponent <AI>(); m_Tree = BT.Root(); m_Tree.OpenBranch( BT.Call(m_AI.Stand), BT.Call(m_AI.RotateTo) ); }
/// <summary> /// AI 小怪智能 行为树模式 /// </summary> /// <param name="self"></param> void TreeMonsterInit(MonsterBehaviorTreeComponent self) { self.root = BT.Root(); self.root.OpenBranch( BT.Selector().OpenBranch( BT.Sequence().OpenBranch( BT.Call(self.GetParent <Unit>().GetComponent <AttackComponent>().CheckDeath), BT.Send(self.GetParent <Unit>().GetComponent <AttackComponent>().GetExpAndCoin), BT.Send(self.GetParent <Unit>().GetComponent <AttackComponent>().RemoveUnit) ), BT.Selector().OpenBranch( BT.Sequence().OpenBranch( BT.Call(self.GetParent <Unit>().GetComponent <SeekComponent>().CheckSeekTarget), BT.Selector().OpenBranch( BT.Sequence().OpenBranch( BT.Call(self.GetParent <Unit>().GetComponent <AttackComponent>().MonsterCheckAttackTarget), BT.Send(self.GetParent <Unit>().GetComponent <AttackComponent>().AttackTarget) ), BT.Send(self.GetParent <Unit>().GetComponent <SeekComponent>().SeekTarget) ) ), BT.Sequence().OpenBranch( BT.Send(self.GetParent <Unit>().GetComponent <RecoverComponent>().RecoverHp), BT.Send(self.GetParent <Unit>().GetComponent <PatrolComponent>().UpdatePatrol) ) ) ) ); }
// Use this for initialization void Awake() { m_TargetToTrack = GameObject.FindGameObjectWithTag("Player").transform; m_HashHitEffect = VFXController.StringToHash("CFX_Hit_Red"); m_HashDieEffect = VFXController.StringToHash("ExplodingHitEffectDark"); m_Animator = GetComponent <Animator>(); m_RigidBody2D = GetComponent <Rigidbody2D>(); m_SpriteRenderer = GetComponent <SpriteRenderer>(); //m_Damageable = GetComponent<Damageable>(); m_TargetPosition = m_TargetToTrack.transform.position; //Behaviour tree m_Ai.OpenBranch( BT.If(() => { return((m_TargetPosition - transform.position).sqrMagnitude < 0.9f); }).OpenBranch( BT.Call(() => m_Animator.ResetTrigger(m_HashWalkPara)), BT.Call(() => SetHorizontalSpeed(0f)), BT.Call(() => m_Animator.SetTrigger(m_HashAttackPara)), BT.Wait(0.5f), BT.WaitForAnimatorState(m_Animator, "MiniSoldier_Idle") ), BT.If(() => { return((m_TargetPosition - transform.position).sqrMagnitude >= 0.9f); }).OpenBranch( BT.Call(() => m_Animator.SetTrigger(m_HashWalkPara)), BT.Call(() => SetHorizontalSpeed(1f)) ), BT.Call(() => m_TargetPosition = m_TargetToTrack.transform.position), BT.Wait(0.5f), BT.Call(OrientToTarget) ); }
public void BT_GetRate_SuccessfulCall_ReturnsData() { BT unitUnderTest = new BT(); var result = unitUnderTest.getRate(); Assert.IsTrue(result.Count > 0 && result.Count % 4 == 0, ERR_MESSAGE); }
public void TestEmptyTree() { /// Test empty tree BT testTree = new BT(); Assert.Throws <NullReferenceException>(() => Program.IsBinaryTreeBalanced(testTree.Root)); }
public void MaxPathSum_OnTreeHoldingAllPositiveNodes() { int expected = 18; int actual = BT.MaxPathSum(treeOne); Assert.AreEqual(expected, actual); }
public void TestHeightofNull() { /// Test empty tree BT testTree = new BT(); Assert.Throws <NullReferenceException>(() => Program.CalculateBinaryTreeHeight(testTree.Root)); }
public void TestNonPerfectTree() { /// Test non perfect tree BT testTree = new BT(); testTree.Root = new Nodeb(100); Nodeb leftChild = new Nodeb(5); testTree.Root.Left = leftChild; Nodeb rightChild = new Nodeb(10); testTree.Root.Right = rightChild; Nodeb rightLeftRightChild = new Nodeb(12); testTree.Root.Right.Left = rightLeftRightChild; Nodeb rightRightChild = new Nodeb(15); testTree.Root.Right.Right = rightRightChild; Assert.Equal(2, Program.CalculateBinaryTreeHeight(testTree.Root)); }
public void TestHeightofThree() { /// Test height of three BT testTree = new BT(); testTree.Root = new Nodeb(100); Nodeb leftChild = new Nodeb(5); testTree.Root.Left = leftChild; Nodeb rightChild = new Nodeb(10); testTree.Root.Right = rightChild; Nodeb leftLeftChild = new Nodeb(15); testTree.Root.Left.Left = leftLeftChild; Nodeb leftRightChild = new Nodeb(20); testTree.Root.Left.Right = leftRightChild; Nodeb rightRightChild = new Nodeb(20); testTree.Root.Right.Right = rightRightChild; Nodeb leftLeftLeftChild = new Nodeb(15); testTree.Root.Left.Left.Left = leftLeftLeftChild; Assert.Equal(3, Program.CalculateBinaryTreeHeight(testTree.Root)); }
public void TestLevelofNull() { /// Test empty tree BT testTree = new BT(); Assert.Throws <NullReferenceException>(() => Program.FindLevel(testTree)); }
public void TestLevelofThree() { /// Test height of three BT testTree = new BT(); testTree.Root = new Nodeb(100); Nodeb leftChild = new Nodeb(5); testTree.Root.Left = leftChild; Nodeb rightChild = new Nodeb(10); testTree.Root.Right = rightChild; Nodeb leftLeftChild = new Nodeb(15); testTree.Root.Left.Left = leftLeftChild; Nodeb leftRightChild = new Nodeb(20); testTree.Root.Left.Right = leftRightChild; Assert.Equal(3, Program.FindLevel(testTree)); }
public void stopTurn() { SteerOutput.Power = 0; SteerOutput.TurnRatio = 0; BT.SendCommand(SteerOutput.ToCommand()); }
public void MaxPathSum_OnTreeHoldingPositiveAndNegetiveNodes() { int expected = 6; int actual = BT.MaxPathSum(treeTwo); Assert.AreEqual(expected, actual); }
protected override void Awake() { base.Awake(); this.contoller = new BTContoller ( BT.Root ( BT.Selector ( BT.Sequence ( BT.Call(SearchEnemy), BT.Call(WaypointToTarget), BT.Call(Move), BT.Call(Attack) ) , BT.Sequence ( BT.Call(FindWayPoint), BT.Call(Move) ) ) ) ); }
// Use this for initialization void Start() { m_Animator = GetComponent <Animator>(); m_EnemyBehaviour = GetComponent <SimpleEnemyBehaviour>(); m_AI.OpenBranch( BT.If(() => { return(m_EnemyBehaviour.CurrentTarget != null); }).OpenBranch( BT.Call(m_EnemyBehaviour.CheckTargetStillVisible), //Run to target BT.If(() => { return(!m_EnemyBehaviour.CheckMeleeAttack()); }).OpenBranch( BT.Call(() => m_EnemyBehaviour.RunToTarget(0.2f)) ), //Attack BT.If(() => { return(m_EnemyBehaviour.CheckMeleeAttack()); }).OpenBranch( BT.Call(m_EnemyBehaviour.StopPatrolling), BT.Call(m_EnemyBehaviour.StopRunning), BT.Call(m_EnemyBehaviour.PerformMeleeAttack), BT.Wait(0.5f), BT.WaitForAnimatorState(m_Animator, "MiniSoldier_Idle") ) ), //Patrolling Around BT.If(() => { return(m_EnemyBehaviour.CurrentTarget == null); }).OpenBranch( BT.Call(m_EnemyBehaviour.ScanForTarget), BT.Call(() => m_EnemyBehaviour.Patrolling(0.2f)) ) ); }
IEnumerator LateStart() { yield return(new WaitUntil(delegate() { m_Player = Array.Find( GameObject.FindGameObjectsWithTag("Character"), delegate(GameObject go) { return go.GetComponent <Character>().GetID() == 0; } ); return m_Player != null; })); m_PlayerInitPos = m_Player.transform.position; m_Character = GetComponent <Character>(); m_AI = GetComponent <AI>(); m_Tree = BT.Root(); m_Tree.OpenBranch( BT.If(m_AI.InRange).OpenBranch( BT.Sequence().OpenBranch( BT.Call(m_AI.Punch), BT.While(m_AI.ActionPlaying).OpenBranch( BT.Call(m_AI.RotateTo) ) ) ), BT.If(m_AI.OutOfRange).OpenBranch( BT.Call(m_AI.Walk), BT.Call(m_AI.RotateTo) ) ); }
/// <summary> /// Take in a binary tree and traverses it using breadth first principles, using a queue /// </summary> /// <param name="tree">binary tree</param> /// <returns> list of tree nodes in breadth first order</returns> public static List <int> BreadthFirst(BT tree) { List <int> list = new List <int>(); Queue bfQueue = new Queue(); bfQueue.Enqueue(tree.Root); if (tree.Root == null) { return(null); } while (bfQueue.Front != null) { Nodeb temp = bfQueue.Dequeue(); list.Add(temp.Value); Console.Write($" {temp.Value} =>"); if (temp.Left != null) { bfQueue.Enqueue(temp.Left); } if (temp.Right != null) { bfQueue.Enqueue(temp.Right); } } return(list); }
public void CheckBT(string textStream) { string[] entries = textStream.Split(new char[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries); BTAchieveList = new BT[entries.Length]; for (int i = 0; i < entries.Length; i++) { string[] entryInfo = entries[i].Split(new char[] { '|' }); string username = entryInfo[0]; int beaten = int.Parse(entryInfo[1]); BTAchieveList[i] = new BT(username, beaten); if (PlayerPrefs.GetString("Username") + beat == BTAchieveList[i].username) { gotUserBT = true; if (BTAchieveList [i].beaten == 1) { Color c = ten.color; c.a = 1f; ten.color = c; } else if (gotUserBT == false) { Color c = ten.color; c.a = 0.5f; ten.color = c; } } else if (gotUserBT == false) { Color c = collision.color; c.a = 0.5f; ten.color = c; } } }
public static void Main(string[] args) { Console.WriteLine("Hello World!"); BT tree = new BT(); tree.Root = new Nodeb(100); Nodeb leftChild = new Nodeb(5); tree.Root.Left = leftChild; Nodeb rightChild = new Nodeb(10); tree.Root.Right = rightChild; Nodeb leftLeftLeaf = new Nodeb(15); tree.Root.Left.Left = leftLeftLeaf; Nodeb leftRightLeaf = new Nodeb(20); tree.Root.Left.Right = leftRightLeaf; Nodeb rightLeftLeaf = new Nodeb(30); tree.Root.Right.Left = rightLeftLeaf; Nodeb rightRightLeaf = new Nodeb(35); tree.Root.Right.Right = rightRightLeaf; BreadthFirst(tree); Console.ReadLine(); }
protected override void Start() { base.Start(); skillReaderList.Add(new SkillReader(SkillManager.FindSkill("FrogGeneralAttack"))); BT BTAttack = BT.Sequence ( BT.Call(GeneralAttack) ); BT BTPatrol = BT.Sequence ( BT.Success(BT.Call(RandomWayPoint)), BT.Success(BT.Call(FindAroundEnemy)), BT.Condition(() => { return(!isFoundEnemy); }, BT.Call(Move)) ); AI = BT.Root ( BT.Sequence ( BT.Success(BT.Call(FindAroundEnemy)), BT.Selector ( BT.Condition(() => { return(isFoundEnemy); }, BTAttack), BT.Probability(0.3f * Time.fixedDeltaTime, BTPatrol) ) ) ); moveMaxDelay = new Delay(); }
public void TestMaxOnEmptyTree() { /// Test when maximum value is root BT testTree = new BT(); Assert.Throws <NullReferenceException>(() => Program.FindMaxValue(testTree)); }
private void OnEnable() { m_Dead = false; m_Collider.enabled = true; bossAudio.PlayAudio(end, 1); m_Ai.OpenBranch( BT.RandomSequence(new int[] { 2, 6, 1 }).OpenBranch( BT.Root().OpenBranch( BT.SetBool(m_Animator, "Walk 1", true), BT.Wait(Random.Range(1f, 2f)), BT.SetBool(m_Animator, "Walk 1", false), BT.WaitForAnimatorState(m_Animator, "Idle 1") ), BT.Root().OpenBranch( BT.If(() => { return(CheckMeleeAttack()); }).OpenBranch( BT.Wait(0.2f), BT.WaitForAnimatorState(m_Animator, "Idle 1") ) ), BT.Repeat(stage1StrafeTimes).OpenBranch( BT.Call(OrientToTarget), BT.Trigger(m_Animator, "RangeAttack 1"), BT.Wait(0.2f), BT.WaitForAnimatorState(m_Animator, "Idle 1"), BT.Wait(strafeInterval) ) ) ); tick = true; }
static LandPredatorBrain() { PredatorTreeRoot = BT.Selector( LandAnimalBrain.LyingDownTree, BT.If(x => x.Alertness > LandAnimalBrain.AlertThreshold && x.Alertness < LandAnimalBrain.FullyAlertThreshold && x.AnimationState != AnimalAnimationState.Flee && x.AnimationState != AnimalAnimationState.AlertIdle && x.OnFlatGround, // play an alert reaction before fleeing and intermittently while alert PlayFixedTimeAnimation(AnimalAnimationState.AlertIdle, LandAnimalBrain.AlertIdleTime)), MovementBehaviors.Flee, GroupBehaviors.SleepNearLeader, BT.If(x => x.Hunger > HungerThreshold, BT.Selector( TryEatNearbyCorpses, new HuntMemoryBehavior() { NoNearbyFoodBehavior = LandAnimalBrain.LandAnimalFindFood })), GroupBehaviors.StayNearLeader(10f), //TODO: show a failed chase if there are no corpses neary to eat BT.If(MovementBehaviors.ShouldReturnHome, MovementBehaviors.WanderHome), BT.If(x => x.Alertness < Animal.FleeThreshold && RandomUtil.Chance(LandAnimalBrain.ChanceToLieDown) && x.OnFlatGround, PlayAnimation(AnimalAnimationState.LyingDown, LandAnimalBrain.LyingTickDuration)), BT.If(x => RandomUtil.Chance(LandAnimalBrain.ChanceToIdle) && x.OnFlatGround, PlayAnimation(AnimalAnimationState.Idle, _ => RandomUtil.Range(LandAnimalBrain.MinIdleTime, LandAnimalBrain.MaxIdleTime))), MovementBehaviors.Wander); }
private void OnEnable() { points = GameObject.FindGameObjectsWithTag("WanderPoints"); //wander function randomwaittime = UnityEngine.Random.Range(1, 5); aiRoot.OpenBranch(BT.If(Playerisvisible).OpenBranch(BT.Call(Chase)), BT.If(Playerisnotvisible).OpenBranch(BT.Call(Walk))); }
static LandAnimalBrain() { LyingDownTree = BT.If("Lying", animal => (animal.LyingDown(), "currently lying"), BT.Selector("Select Lying", BT.If("Try Getup", x => ((x.State == AnimalState.Sleeping || x.State == AnimalState.LyingDown) && x.Alertness >= Animal.WakeUpAlertness, "sleeping/lying and alert"), Anim(AnimalState.Idle, true, x => x.Species.TimeLayToStand)), // waking up takes time, start the transition animation then decide what to do next tick BT.If("Try Flee", x => (x.Alertness > Animal.FleeThreshold, $"{x.Alertness} alert > {Animal.FleeThreshold} flee"), Anim(AnimalState.Flee, false, x => x.Species.TimeLayToStand)), BT.If("Try Hungry", x => (x.Hunger > HungerToStartEating, $"{x.Hunger} hunger > {HungerToStartEating}"), Anim(AnimalState.Idle, true, x => x.Species.TimeLayToStand)), RelaxBehaviors.TrySleep, RelaxBehaviors.RandomWake, Anim(AnimalState.LyingDown, true, _ => RandomUtil.Range(MinLyingIdleTime, MaxLyingIdleTime)))); LandAnimalTreeRoot = BT.Selector("Land Animal", LyingDownTree, PerceptionBehaviors.Alert, MovementBehaviors.Flee, FindAndEatPlants, MovementBehaviors.TryReturnHome, RelaxBehaviors.Relax, MovementBehaviors.Wander, RelaxBehaviors.Idle); }
/// <summary> /// start the behaviourtree assigned to NPC /// </summary> private void InitializeBehaviourTree() { // Contains useful data for the behavior tree DataModel d = new DataModel(this); _bt = AIFactory.Instance.CreateBehaviourTree(Difficulty, d); }
// Use this for initialization void Awake() { DieEffectHash = VFXController.StringToHash("Smoke"); rigidbody2D = GetComponent <Rigidbody2D>(); animator = GetComponent <Animator>(); rmx6Moving = GetComponent <rmx6Moving>(); oldVelocity = new Vector2(0, 0); die = false; rmx6_BT.OpenBranch( BT.If(() => { return(targetInRange); }).OpenBranch( BT.Call(() => animator.SetBool("walk", true)), BT.WaitForAnimatorState(animator, "rmx6_walking"), //BT.Wait(0.3f), BT.Call(() => { if (!die) { rmx6Moving.canWalk = true; } }) ), BT.If(() => { return(!targetInRange); }).OpenBranch( BT.Call(() => animator.SetBool("walk", false)), BT.Call(() => animator.SetBool("hide", true)), BT.WaitForAnimatorState(animator, "rmx6_hiding"), //BT.Wait(0.3f), BT.Call(() => rmx6Moving.canWalk = false) ) ); }