public static SequenceNode Create(params QueryPlanNode[] nodes) { var sequence = new SequenceNode(); foreach (QueryPlanNode node in nodes) { sequence.AddNode(node); } return(sequence); }
public void StatelessSelectNode() { var moqAction1 = new Mock <IBehaviourTreeNode <int, int> >(); var moqAction2 = new Mock <IBehaviourTreeNode <int, int> >(); var moqAction3 = new Mock <IBehaviourTreeNode <int, int> >(); var selectNode = new SequenceNode <int, int>("test", false); int func1CallCount = 0, func2CallCount = 0, func3CallCount = 0; moqAction1.Setup(f => f.Compile()).Returns((tick, state) => { func1CallCount++; return(BehaviourTreeState.Success); }); moqAction2.Setup(f => f.Compile()).Returns((tick, state) => { func2CallCount++; return(BehaviourTreeState.Failure); }); moqAction3.Setup(f => f.Compile()).Returns((tick, state) => { func3CallCount++; return(BehaviourTreeState.Running); }); selectNode.AddNode(moqAction1.Object); selectNode.AddNode(moqAction2.Object); selectNode.AddNode(moqAction3.Object); var func = selectNode.Compile(); var callResult1 = func(0, 0); var callResult2 = func(0, 0); Assert.Equal(BehaviourTreeState.Failure, callResult1); Assert.Equal(BehaviourTreeState.Failure, callResult2); Assert.Equal(2, func1CallCount); Assert.Equal(2, func2CallCount); Assert.Equal(0, func3CallCount); }
public void VerifyExecution(IEnumerable <IBehaviourTreeNode <int, int> > nodes, BehaviourTreeState expectedState) { var selectNode = new SequenceNode <int, int>("test", false); foreach (var node in nodes) { selectNode.AddNode(node); } var func = selectNode.Compile(); var state = func(0, 0); Assert.Equal(expectedState, state); }
public static OperationNode Build(QueryPlanContext context) { var root = new SequenceNode { CancelOnError = true }; context.Root = root; context.NodePath.Push(root); foreach (ISelection mutation in context.Operation.GetRootSelectionSet().Selections) { context.SelectionPath.Push(mutation); var mutationStep = new ResolverNode( mutation, context.SelectionPath.PeekOrDefault(), GetStrategyFromSelection(mutation)); root.AddNode(mutationStep); QueryStrategy.VisitChildren(mutation, context); } context.NodePath.Pop(); QueryPlanNode optimized = QueryStrategy.Optimize(context.Root); var operationNode = new OperationNode(optimized); if (context.Deferred.Count > 0) { foreach (QueryPlanNode?deferred in QueryStrategy.BuildDeferred(context)) { operationNode.Deferred.Add(deferred); } } if (context.Streams.Count > 0) { operationNode.Streams.AddRange(context.Streams.Values); } return(operationNode); }
// Use this for initialization void Start() { // -- ROOT AND ITS CHILDREN -- // Root node selector m_AI = new SelectorNode(); //m_AI = gameObject.AddComponent<SelectorNode>(); // 1st node of Root Condition whistleCondition = new Condition(Conditions.hasWhistled); ConditionNode whistle = new ConditionNode(whistleCondition, this); //ConditionNode whistle = gameObject.AddComponent<ConditionNode>(); // 2nd node of Root Condition inSphereCondition = new Condition(Conditions.inSphere); ConditionNode inSphere = new ConditionNode(inSphereCondition, this); // 3rd node of Root StochasticNode runFaster = new StochasticNode(); // Adding children to Root m_AI.AddNode(whistle); m_AI.AddNode(inSphere); m_AI.AddNode(runFaster); // -- CHILDREN OF WHISTLE CONDITION (1ST CHILD OF ROOT) -- Action approachAction = new Action(Actions.ApproachPlayer); ActionNode approach = new ActionNode(approachAction, this); whistle.AddNode(approach); // -- CHILDREN OF IN-SPHERE CONDITION (2ND CHILD OF ROOT) -- Condition interactingCondition = new Condition(Conditions.interacting); ConditionNode interacting = new ConditionNode(interactingCondition, this); inSphere.AddNode(interacting); // -- CHILDREN OF RUN-FASTER CONDITION (3RD CHILD OF ROOT) -- SequenceNode runAhead = new SequenceNode(); SequenceNode runWith = new SequenceNode(); runFaster.AddNode(runAhead); runFaster.AddNode(runWith); // -- CHILDREN OF INTERACTING CONDITION NODE (CHILD OF IN-SPHERE CONDITION) -- SequenceNode sitThenHunt = new SequenceNode(); SequenceNode idle = new SequenceNode(); interacting.AddNode(sitThenHunt); interacting.AddNode(idle); // -- CHILDREN OF SIT-THEN-HUNT SEQUENCE (CHILD OF SIT-THEN-HUNT SEQUENCE) -- Action sitAction = new Action(Actions.Sit); ActionNode sit = new ActionNode(sitAction, this); SequenceNode huntSequence = new SequenceNode(); sitThenHunt.AddNode(sit); sitThenHunt.AddNode(huntSequence); // -- CHILDREN OF IDLE SEQUENCE (CHILD OF IDLE SEQUENCE) -- Action sniffAction = new Action(Actions.Sniff); ActionNode randomSpot = new ActionNode(sniffAction, this); Condition tenMinutesCondition = new Condition(Conditions.tenMinutes); ConditionNode tenMinutes = new ConditionNode(tenMinutesCondition, this); idle.AddNode(randomSpot); idle.AddNode(tenMinutes); // joint child // -- CHILDREN OF RUN-AHEAD SEQUENCE (CHILD OF RUN-FASTER CONDITION) -- Action runFastAction = new Action(Actions.RunFast); ActionNode runFast = new ActionNode(runFastAction, this); Action waitAction = new Action(Actions.Wait); ActionNode turnAndWait = new ActionNode(waitAction, this); runAhead.AddNode(runFast); runAhead.AddNode(tenMinutes); // joint child tenMinutes.AddNode(huntSequence); runAhead.AddNode(turnAndWait); // -- CHILDREN OF RUN-WITH SEQUENCE (CHILD OF RUN-FASTER CONDITION) -- Action runWithAction = new Action(Actions.RunWith); ActionNode runNear = new ActionNode(runWithAction, this); runWith.AddNode(runNear); // -- CHILDREN OF HUNT SEQUENCE (CHILD OF SIT-THEN-HUNT SEQUENCE AND TEN-MINUTES CONDITION) -- Action dissapearAction = new Action(Actions.Dissapear); ActionNode dissapear = new ActionNode(dissapearAction, this); Action huntedAction = new Action(Actions.Hunted); ActionNode hunted = new ActionNode(huntedAction, this); huntSequence.AddNode(dissapear); huntSequence.AddNode(hunted); // START m_AI.StartCoroutine(m_AI.Execute()); }
private void Start() { player = GameObject.FindGameObjectWithTag(Tags.Player).GetComponent <UserSoldier>(); gameManager = GameObject.FindGameObjectWithTag(Tags.GameController).GetComponent <GameManager>(); npc = GetComponent <NPCSoldier>(); npc.Damaged += Damaged; nextRadius = gameManager.nextRadius; SetCoverPos(transform.position); var notInSafeZoneCond = new ConditionNode(IsInSafeZone, false); var haveEnemyCond = new ConditionNode(HaveEnemyInSight, true); var beingShootCond = new ConditionNode(BeingShoot, true); var haveWeaponCond = new ConditionNode(HaveWeapon, true); var seekWeaponCond = new ConditionNode(SeekWeapon, true); var equipAction = new SetEquipmentAction(npc, true); var notEquipAction = new SetEquipmentAction(npc, false); var crouchAction = new SetCrouchAction(npc, true); var notCrouchAction = new SetCrouchAction(npc, false); var toSafeZoneAction = new MoveAction(npc, blackBoard, safeZoneKey); var goCoverAction = new MoveAction(npc, blackBoard, coverPosKey); var FallBackAction = new MoveAction(npc, blackBoard, fallBackPosKey); var goWeaponAction = new MoveAction(npc, blackBoard, weaponPosKey); var pickUpWeaponAction = new PickUpWeaponAction(npc, blackBoard, weaponKey); var shootAction = new ShootAction(npc, blackBoard, enemyPosKey); var turnToShooterAction = new TurnAction(npc, blackBoard, shooterPosKey); var idleAction = new IdleAction(npc); var combat = new SequenceNode(); combat.AddCondition(haveWeaponCond); combat.AddNode(equipAction); combat.AddNode(shootAction); var fallBack = new SequenceNode(); fallBack.AddNode(FallBackAction); haveEnemy = new SelectionNode(); haveEnemy.AddCondition(haveEnemyCond); haveEnemy.AddNode(combat); haveEnemy.AddNode(fallBack); var counter = new SequenceNode(); counter.AddCondition(haveWeaponCond); counter.AddNode(turnToShooterAction); beingShoot = new SelectionNode(); beingShoot.AddCondition(beingShootCond); beingShoot.AddNode(counter); beingShoot.AddNode(fallBack); toSafeZone = new SequenceNode(); toSafeZone.AddCondition(notInSafeZoneCond); toSafeZone.AddNode(notCrouchAction); toSafeZone.AddNode(notEquipAction); toSafeZone.AddNode(toSafeZoneAction); stayInCover = new SequenceNode(); stayInCover.AddCondition(haveWeaponCond); stayInCover.AddNode(goCoverAction); stayInCover.AddNode(crouchAction); stayInCover.AddNode(idleAction); seekWeapon = new SequenceNode(); seekWeapon.AddCondition(seekWeaponCond); seekWeapon.AddNode(goWeaponAction); seekWeapon.AddNode(pickUpWeaponAction); idle = new SelectionNode(); idle.AddNode(stayInCover); idle.AddNode(seekWeapon); root.AddNode(haveEnemy); root.AddNode(beingShoot); root.AddNode(toSafeZone); root.AddNode(idle); }
// Use this for initialization public void Start() { inputData.attribute = GetComponent <CharacterAttribute>(); rootNode.nodeName += "根"; //条件 var hasNoTarget = new PreconditionNOT(() => { return(inputData.attribute.hasTarget); }); hasNoTarget.nodeName = "无目标"; var hasTarget = new Precondition(hasNoTarget); hasTarget.nodeName = "发现目标"; var isAnger = new Precondition(() => { return(inputData.attribute.isAnger); }); isAnger.nodeName = "愤怒状态"; var isNotAnger = new PreconditionNOT(isAnger); isNotAnger.nodeName = "非愤怒状态"; var HPLessThan500 = new Precondition(() => { return(inputData.attribute.health < 500); }); HPLessThan500.nodeName = "血少于500"; var HPMoreThan500 = new PreconditionNOT(HPLessThan500); HPMoreThan500.nodeName = "血大于500"; var isAlert = new Precondition(() => { return(inputData.attribute.isAlert); }); isAlert.nodeName = "警戒"; var isNotAlert = new PreconditionNOT(isAlert); isNotAlert.nodeName = "非警戒"; var patrolNode = new SequenceNode(); patrolNode.nodeName += "巡逻"; patrolNode.AddCondition(hasNoTarget); patrolNode.AddCondition(isNotAlert); patrolNode.AddNode(new PatrolAction()); var alert = new SequenceNode(); alert.nodeName += "警戒"; alert.AddCondition(hasNoTarget); alert.AddCondition(isAlert); alert.AddNode(new AlertAction()); var runaway = new SequenceNode(); runaway.nodeName += "逃跑"; runaway.AddCondition(hasTarget); runaway.AddCondition(HPLessThan500); runaway.AddNode(new RunAwayAction()); var attack = new SelectorNode(); attack.nodeName += "攻击"; attack.AddCondition(hasTarget); attack.AddCondition(HPMoreThan500); var attackCrazy = new SequenceNode(); attackCrazy.nodeName += "疯狂攻击"; attackCrazy.AddCondition(isAnger); attackCrazy.AddNode(new CrazyAttackAction()); attack.AddNode(attackCrazy); var attackNormal = new SequenceNode(); attackNormal.nodeName += "普通攻击"; attackNormal.AddCondition(isNotAnger); attackNormal.AddNode(new AttackAction()); attack.AddNode(attackNormal); rootNode.AddNode(patrolNode); rootNode.AddNode(alert); rootNode.AddNode(runaway); rootNode.AddNode(attack); var ret = rootNode.Enter(inputData); if (!ret) { Debug.Log("无可执行节点!"); } }