void ProcessDataContext <TEventArgs>(object viewModel, TEventArgs e, Action <IStateful, TEventArgs> raiseEventAction) where TEventArgs : class { if (viewModel == null) { return; } IStateful statePreserver = viewModel as IStateful; if (statePreserver != null) { raiseEventAction(statePreserver, e); } var compositeViewModel = viewModel as ICompositeViewModel; if (compositeViewModel != null) { /* Process children recursively. */ foreach (var childViewModel in compositeViewModel.ChildViewModels) { ProcessDataContext(childViewModel, e, raiseEventAction); } } }
public void Init(IStateful target, GoapAction action) { actionData = action; this.target = target; startTime = 0; isInRange = false; isDone = false; }
/// <summary> /// Returns a WorldGoal that contains all the preconditions that the agent /// must satisfy. /// </summary> public virtual WorldGoal GetIndependentPreconditions(IStateful agent) { var worldPreconditions = WorldGoal.Borrow(); if (preconditions.Count > 0) { worldPreconditions[agent] = preconditions; } return(worldPreconditions); }
/// <summary> /// Returns a WorldGoal that contains all the preconditions that the target /// of the action must satisfy. /// </summary> public virtual WorldGoal GetDependentPreconditions(IStateful agent, IStateful target) { var worldPreconditions = WorldGoal.Borrow(); if (targetPreconditions.Count > 0) { worldPreconditions[target] = targetPreconditions; } return(worldPreconditions); }
public virtual WorldEffects GetEffectsOnAgent(IStateful agent) { var worldEffects = new WorldEffects(); if (effects.Count > 0) { worldEffects[agent] = effects; } return(worldEffects); }
public AnimatorStateController(IStateful pStateful, StatefulObjectData pData) : base(pStateful, pData) { _animator = _stateful.GetAnimationObject().GetComponent <Animator>(); if (!_animator) { _animator = _stateful.GetAnimationObject().AddComponent <Animator>(); } _resourceManager = ResourceManager.GetInstance(); _resourceManager.Load("Animation/" + _statefulObjectData.CurrentAnimatorInfo.Path, typeof(RuntimeAnimatorController), OnLoadAnimatorComplete); LoadAnimation(); }
public static WithContext Borrow(GoapAction action, GoapAgent agent, IStateful target) { var actionWithContext = pool.Borrow(); actionWithContext.actionData = action; actionWithContext.agent = agent; actionWithContext.target = target; actionWithContext.startTime = 0; actionWithContext.isInRange = false; actionWithContext.isDone = false; return(actionWithContext); }
protected virtual bool CanDoNow(GoapAgent agent, IStateful target) { var worldState = WorldState.pool.Borrow(); worldState.Clear(); worldState[agent] = agent.GetState(); worldState[target] = target.GetState(); var conditions = GetDependentPreconditions(agent, target); bool result = GoapPlanner.DoConditionsApplyToWorld(conditions, worldState); WorldState.pool.Return(worldState); return(result); }
/// <summary> /// Gets the closest target. /// </summary> /// <returns>The closest target.</returns> /// <param name="currentPosition">Current position.</param> /// <param name="agent">Agent.</param> public float CalculateCost(Vector2 currentPosition, IStateful target) { var obj = target as Component; DebugUtils.Log("#### current position: " + currentPosition.ToString()); DebugUtils.Log("$$$$ target position: " + (string)obj.transform.position.ToString()); var travelVector = (Vector2)obj.transform.position - currentPosition; float travelCost = travelVector.sqrMagnitude; DebugUtils.Log("sqrMagnitude: " + travelVector); float result = cost + travelCost; return(result); }
/// <summary> /// Returns the effects that should be applied to the agent and the target /// of the action. /// </summary> public virtual WorldEffects GetDependentEffects(IStateful agent, IStateful target) { var worldEffects = WorldEffects.Borrow(); if (effects.Count > 0) { worldEffects[agent] = effects; } if (targetEffects.Count > 0) { worldEffects[target] = targetEffects; } return(worldEffects); }
/// <summary> /// Search the area in a given radius from our location /// </summary> public bool LookAround() { bool foundNew = false; Collider[] hitColliders = Physics.OverlapSphere(transform.position, visionRadius); foreach (Collider collider in hitColliders) { // Ignore objects we've seen before if (foundColliders.Contains(collider)) { continue; } // Check if object is reachable NavMeshPath path = new NavMeshPath(); navAgent.CalculatePath(collider.transform.position, path); if (path.status == NavMeshPathStatus.PathComplete) { // Ignore this object in future searches foundColliders.Add(collider); IStateful pointOfInterest = collider.GetComponent <IStateful>(); if (pointOfInterest != null) { pointsOfInterest.Add(pointOfInterest); foundNew = true; } } } // Call the danger sensor in case we saw something scary if (foundNew && dangerSensor != null) { dangerSensor.CheckThreats(); } #if DEBUG_PLAN sightAreas.Clear(); foreach (IStateful poi in pointsOfInterest) { Component poiComp = poi as Component; sightAreas.Add(poiComp.name); } #endif return(foundNew); }
protected virtual bool CanDoNow(GoapAgent agent, IStateful target) { var worldState = WorldState.Borrow(); worldState[agent] = agent.GetState(); worldState[target] = target.GetState(); // Since the action is a part of the plan, its preconditions on the agent // should apply. var doesApply = worldState.IsGoalState(GetIndependentPreconditions(agent)); // NOTE: If the agent's state is volatile and can change outside of the // agent's plan, comment out this assertion. DebugUtils.AssertWarning( doesApply, "WARNING: Possible bug in definition of " + name + ". The agent " + agent.name + " planned to do it but now its state does not allow it"); // Need to check that the target's state still apply to the preconditions // defined by the action. doesApply = doesApply && worldState.IsGoalState(GetDependentPreconditions(agent, target)); worldState.ReturnSelf(); return(doesApply); }
/// <summary> /// Determine whether a user can execute a <see cref="StatePath"/> /// over a stateful instance. /// </summary> /// <typeparam name="ST">The type of state transitions, derived from <see cref="StateTransition{U}"/>.</typeparam> /// <param name="user">The user.</param> /// <param name="stateful">The stateful instance.</param> /// <param name="statePath">The state path to execute.</param> public bool CanUserExecuteStatePath <ST>(U user, IStateful <U, ST> stateful, StatePath statePath) where ST : StateTransition <U> { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (stateful == null) { throw new ArgumentNullException(nameof(stateful)); } if (statePath == null) { throw new ArgumentNullException(nameof(statePath)); } if (!CanUserReadEntity(user, stateful) || !CanUserWriteEntity(user, stateful)) { return(false); } var rolesAccessRight = GetRolesAccessRight(user); if (rolesAccessRight.SupportsStatePath(statePath)) { return(true); } if (stateful is ISegregatedEntity segregatedStateful) { AccessRight dispositionsAccessRight = GetDispositionsAccessRight(user, segregatedStateful); return(dispositionsAccessRight.SupportsStatePath(statePath)); } return(false); }
public void Register(IStateful stateful) { this.statefulRefs.Add(new WeakReference <IStateful>(stateful)); }
public void Init(Node parent, float runningCost, WorldGoal goal, GoapAction action, IStateful target) { this.parent = parent; this.runningCost = runningCost; this.goal = goal; this.action.Init(target, action); }
protected StateControllerBase(IStateful pStateful, StatefulObjectData pStatefulObjectData) { _stateful = pStateful; _statefulObjectData = pStatefulObjectData; }
/// <summary> /// A* forward search for a plan that satisfies the given goal. /// </summary> /// <returns>Returns null if a plan could not be found, or a list of the /// actions that must be performed, in order.</returns> public static Queue <GoapAction.WithContext> Plan( GoapAgent agent, List <GoapAction> availableActions, WorldGoal goal) { var exploredNodes = new Dictionary <WorldState, Node> (WorldStateComparer.instance); var closedSet = new HashSet <WorldState> (WorldStateComparer.instance); var openSet = new FastPriorityQueue <Node> (MAX_FRINGE_NODES); var currentNode = Node.pool.Borrow(); currentNode.Init(null, 0f, goal, null, null); // currentNode.position = Vector2.zero; currentNode.position = (agent as Component).transform.position; openSet.Enqueue(currentNode, 0f); int iterations = 0; while (openSet.Count > 0 && iterations < MAX_FRINGE_NODES - 10) { iterations++; currentNode = openSet.Dequeue(); //TODO: delete print //Debug.Log ("current world goal: " + GoapAgent.PrettyPrint(currentNode.goal)); if (DoConditionsApply(currentNode.goal[agent], agent.GetState())) { DebugUtils.Log("Selected plan with cost: " + currentNode.Score); var plan = UnwrapPlan(currentNode); //DebugUtils.Log("the plan: " + GoapAgent.PrettyPrint(plan)); // Return all nodes. Node.pool.ReturnAll(); // Check for leaks in the pools: //DebugUtils.LogError("Nodes: " + Node.pool.Count); //DebugUtils.LogError("WithContext: " + GoapAction.WithContext.pool.Count); //DebugUtils.LogError("WorldState: " + WorldState.pool.Count); return(plan); } foreach (GoapAction action in availableActions) { //TODO: delete print //Debug.Log("considering " + action.name); WorldGoal possibleChildGoal = action.reverseApplyToWorldGoal(currentNode.goal); //Debug.Log ("new goal will be: " + GoapAgent.PrettyPrint(possibleChildGoal)); if (agent.GetState().isGoalCloser(currentNode.goal, possibleChildGoal)) { List <IStateful> targets = action.GetAllTargets(agent); // No targets, move to next action if (targets.Count == 0) { continue; } float minCost = 99999f; float tempCost = 0f; IStateful closestTarget = null; Vector2 newPosition = currentNode.position; foreach (var target in targets) { //DebugUtils.Log ("targets..."); //TODO: check target preconds, make sure this works if (goal.ContainsKey(target)) { if (!DoConditionsApply(goal [target], target.GetPerceivedState())) { continue; } } if (action.RequiresInRange()) { tempCost = action.CalculateCost(currentNode.position, target); if (tempCost < minCost) { minCost = tempCost; closestTarget = target; newPosition = (target as Component).transform.position; DebugUtils.Log("minCost: " + minCost); DebugUtils.Log("closestTarget: " + closestTarget); } //DebugUtils.Log ("calculating tempCost"); } else { closestTarget = target; tempCost = 9999; DebugUtils.Log("+++ closestTarget: " + closestTarget); break; } } float cost = currentNode.runningCost + action.workDuration + tempCost; Node newChiledNode = Node.pool.Borrow(); DebugUtils.Log("*** closestTarget: " + closestTarget); newChiledNode.Init(currentNode, cost, possibleChildGoal, action, closestTarget); newChiledNode.position = newPosition; openSet.Enqueue(newChiledNode, newChiledNode.runningCost); } //TODO: delete 'else' scope else { //DebugUtils.Log (action.name + " doesnt improve goal"); } } } //TODO: return plan failed #yoel return(null); }