public override void OnRun(GoapState goalState) { var targetID = m_agent.GetMatchingTargetsInSight(GameMatcher.AllOf(GameMatcher.Enemy).NoneOf(GameMatcher.Dead))[0]; m_agent.SetTarget(targetID); OnComplete(); }
public override void OnRun(GoapState goalState) { var target = m_agent.GetTarget(); if (target == null) { OnFailed(); return; } var entity = m_agent.Entity; if (entity.isMoveComplete) { OnComplete(); return; } if (DidTargetMove()) { OnFailed(); return; } if (entity.hasMove) { return; } entity.AddMove(m_targetPosition); base.OnRun(goalState); }
public IGoapGoal Plan(IGoapAgent agent) { m_agent = agent; m_currentGoal = null; List <IGoapGoal> possibleGoals = GetPossibleGoals(agent); if (possibleGoals.Count == 0) { GoapLogger.LogWarning("[ReGoapPlanner] Agent does not have any Goals to perform. " + m_agent.GetName()); } while (possibleGoals.Count > 0) { m_currentGoal = possibleGoals[possibleGoals.Count - 1]; possibleGoals.RemoveAt(possibleGoals.Count - 1); if (CanFullfillWithActions(m_agent, m_currentGoal) == false) { //No actions can't handle this goal GoapLogger.LogWarning("GoalPlanner :: No Actions to handle Goal (" + m_currentGoal.GetName() + ")"); m_currentGoal = null; continue; } GoapState targetState = m_currentGoal.GetGoalState(agent); GoapNode <GoapState> leaf = (GoapNode <GoapState>)m_aStar.Run(GoapNode <GoapState> .Instantiate(this, targetState, null, null), targetState); if (leaf == null) { GoapLogger.LogWarning("GoapPlanner :: Pathfinding failed!"); m_currentGoal = null; continue; } Queue <IGoapAction> actions = leaf.CalculatePath(); if (actions.Count == 0) { GoapLogger.LogWarning("GoapPlanner :: Calculating Path failed!"); m_currentGoal = null; continue; } m_currentGoal.SetPlan(actions); break; } if (m_currentGoal != null) { GoapLogger.Log(string.Format("[ReGoapPlanner] Calculated plan for goal '{0}', plan length: {1}", m_currentGoal, m_currentGoal.GetPlan().Count)); } else { GoapLogger.LogWarning("[ReGoapPlanner] Error while calculating plan."); } return(m_currentGoal); }
private List <Tuple <GoapState, float> > Expand(GoapState state) { var list = new List <Tuple <GoapState, float> >(); foreach (var action in state.Actions) { var preconditionsNotSuccess = false; foreach (var precon in action.Preconditions) { if (!precon(state.CurrentWorldModel)) { preconditionsNotSuccess = true; break; } } if (!preconditionsNotSuccess) { var newState = Execute(action, state); list.Add(Tuple.Create(newState, newState.Heuristic(newState.CurrentWorldModel, state.CurrentWorldModel) + action.Cost)); } } return(list.OrderBy(i => i.Item2).ToList()); }
public override void OnRun(GoapState goalState) { if (m_agent.DoesCurrentTargetMatch(GameMatcher.OreBranch)) { OnComplete(); return; } if (HasValidTarget()) { //TODO : Add wait time, executionTime > 60ms then Failed var branches = (m_agent.GetMatchingTargetsInSight(GameMatcher.OreBranch)); foreach (var branchID in branches) { var branch = m_agent.Contexts.game.GetEntityWithId(branchID); if (TargetingHelpers.GetTargeters(m_agent.Contexts, branch).Count == 0) { m_agent.SetTarget(branchID); OnComplete(); return; } } //Wait till ore vein creates branch } if (m_agent.HasMatchingTargetInSight(GameMatcher.OreVein) == false) { OnFailed(); return; } var oreVein = m_agent.GetMatchingTargetsInSight(GameMatcher.OreVein)[0]; m_agent.SetTarget(oreVein); }
public override void OnBegin(GoapState goalState) { if (m_agent.Entity.combatDirector.director.DoAttack() == false) { OnFailed(); return; } }
public GoapPlanner() { GoapNode <GoapState> .Warmup(100); GoapState.Warmup(100); m_aStar = new AStar <GoapState>(100); }
public override void OnBegin(GoapState state) { if (m_agent.HasTarget() == false) { OnFailed(); return; } m_targetPosition = m_agent.GetTarget().position.value; m_isInitialized = true; base.OnBegin(state); }
public void Recycle() { m_currentState.Recycle(); m_currentState = null; m_targetState.Recycle(); m_targetState = null; lock (m_cachedNodes) { m_cachedNodes.Push(this); } }
public static GoapNode <T> Instantiate(IGoapPlanner planner, GoapState goalState, GoapNode <T> parent, IGoapAction action) { GoapNode <T> node; if (m_cachedNodes == null) { m_cachedNodes = new Stack <GoapNode <T> >(); } node = (m_cachedNodes.Count > 0) ? m_cachedNodes.Pop() : new GoapNode <T>(); node.Init(planner, goalState, parent, action); return(node); }
public bool IsEqual(GoapState state) { if (Equals(state.CurrentWorldModel, CurrentWorldModel)) { return(true); } if (state.GeneratedAction != GeneratedAction) { return(false); } return(false); }
public override void OnRun(GoapState goalState) { var entity = m_agent.Entity; if (entity.isAttackComplete) { if (entity.hasTarget == false || IsTargetDead()) { OnComplete(); return; } OnFailed(); } }
public void Init(IGoapPlanner planner, GoapState goalState, GoapNode <T> parent, IGoapAction action) { m_expandList.Clear(); m_planner = planner; m_parent = parent; m_action = action; if (m_parent != null) { m_currentState = parent.GetState().Clone(); m_gCost = parent.GetCost(); } else { m_currentState = m_planner.GetAgent().GetMemory().GetWorldState().Clone(); } if (action != null) { m_gCost += action.GetCost(); GoapState preconditions = action.GetPreConditions(goalState); m_targetState = goalState + preconditions; GoapState effects = action.GetPostEffects(goalState); m_currentState.AddFromState(effects); //Did this action's effect fulfill any of the goals? m_targetState.RemoveCompletedConditions(effects); //Did the world fulfill any of the goals? m_targetState.RemoveCompletedConditions(m_planner.GetAgent().GetMemory().GetWorldState()); } else { var diff = GoapState.Instantiate(); goalState.CreateStateWithMissingDifferences(m_currentState, ref diff); m_targetState = diff; } //Cost is equal to the amount of extra actions m_hCost = m_targetState.Count; }
public override void OnRun(GoapState goalState) { var targets = m_agent.GetMatchingTargetsInSight(GameMatcher.AllOf(GameMatcher.Miner).NoneOf(GameMatcher.Dead)); if (targets.Count == 0) { OnFailed(); return; } //Target random miner m_agent.SetTarget(targets[Random.Range(0, targets.Count)]); OnComplete(); }
public static AIEntity AddTestAction1(this AIContext context) { var condition = new GoapState <string, object> () { }; var effect = new GoapState <string, object> () { ["HasAction1"] = true }; return(context.AddAction(0, condition, effect, () => { Console.WriteLine("[ACTION 1]"); return GoapActionStatus.Success; })); }
private static void GenerateAgents(AIContext Context, int Count) { var Planner = new GoapPlanner(); for (int i = 0; i < Count; i++) { var WorldState = new GoapState <string, object> () { }; var Rand = new Random(i); var GoalState = new GoapState <string, object> () { ["HasAction" + Rand.Next(1, 5).ToString()] = true }; var Agent = Context.AddAgent(WorldState, GoalState, Planner); Agent.isGoapPlanRequest = true; } }
// test to see if the actions required states match our current world states. bool TestActionAgainstWorldState(GoapAction action) { if (action.RequiredStates.Count > 0) { foreach (GoapState actionState in action.RequiredStates) { bool currentStateCheck = false; foreach (GoapState worldState in CurrentWorldState) { if (GoapState.Compare(actionState, worldState)) { currentStateCheck = true; } } if (!currentStateCheck) { return(false); } } } return(true); }
// Returns a list of actions (from the available actions) that satisfy one or all of our goals required states. List <GoapAction> GetGoalActions(GoapGoal goal) { List <GoapAction> ReturnActions = new List <GoapAction>(); foreach (GoapState state in goal.RequiredWorldState) { foreach (GoapAction action in AvailableActions) { if (action.CanActionRun()) { for (int i = 0; i < action.SatisfiesStates.Count; ++i) { if (GoapState.Compare(state, action.SatisfiesStates[i])) { ReturnActions.Add(action); } } } } } return(ReturnActions); }
private bool CanFullfillWithActions(IGoapAgent agent, IGoapGoal goal) { GoapState goalState = goal.GetGoalState(agent).Clone(); foreach (IGoapAction action in agent.GetActions()) { if (CanRunAction(agent, action) == false) { continue; } goalState.RemoveCompletedConditions(action.GetContextPostEffects(goalState));; } goalState.RemoveCompletedConditions(m_agent.GetMemory().GetWorldState()); if (goalState.Count > 0) { return(false); } return(true); }
// test to see if our current world state matches that of our goals required states. bool TestGoalStateAgainstWorldState(GoapGoal goal) { bool GoalStatesMatch = true; foreach (GoapState goalState in goal.RequiredWorldState) { bool currentStateCheck = false; foreach (GoapState worldState in CurrentWorldState) { if (GoapState.Compare(worldState, goalState)) { currentStateCheck = true; } } if (!currentStateCheck) { GoalStatesMatch = false; break; } } return(GoalStatesMatch); }
public bool CanRunGoal(IGoapAgent agent, IGoapGoal goal) { if (agent.GetCurrentGoal() == goal) { return(false); } if (goal.CanRun(agent) == false) { return(false); } GoapState differenceState = goal.GetGoalState(agent).Clone(); differenceState.RemoveCompletedConditions(m_agent.GetMemory().GetWorldState()); if (differenceState.Count == 0) { return(false); } return(true); }
public List <INode <GoapState> > GetNeighbours() { m_expandList.Clear(); IGoapAgent agent = m_planner.GetAgent(); List <IGoapAction> actions = agent.GetActions(); for (int i = actions.Count - 1; i >= 0; i--) { IGoapAction possibleAction = actions[i]; if (possibleAction == m_action) { continue; } if (GoapPlannerManager.s_Instance.GetPlanner().CanRunAction(agent, possibleAction) == false) { continue; } GoapState preConditions = possibleAction.GetContextPreConditions(m_targetState); GoapState postEffects = possibleAction.GetContextPostEffects(m_targetState); bool isValid = (postEffects.HasAny(m_targetState)) && (!m_targetState.HasAnyConflict(preConditions)) && (!m_targetState.HasAnyConflict(postEffects)); if (isValid) { GoapState targetState = m_targetState; m_expandList.Add(Instantiate(m_planner, targetState, this, possibleAction)); } } return(m_expandList); }
public GOAP(GoapState initialState) { _currentState = initialState; }
public override void OnRun(GoapState goalState) { OnComplete(); }
public BaseGoapMemory() { m_state = GoapState.Instantiate(); }
public bool Compare(GoapState target) { return(Name == target.Name && Status == target.Status); }
public static bool Compare(GoapState a, GoapState b) { //Debug.Log("names: (" + a.Name + ", " + b.Name + ") " + (a.Name == b.Name) + " | Status: (" + a.Status + ", " + b.Status + ") " + (a.Status == b.Status) + " | Combined: " + (a.Name == b.Name && a.Status == b.Status)); return(a.Name == b.Name && a.Status == b.Status); }
public GoapActionState(IGoapAction <T, W> action, GoapState <T, W> settings) { Action = action; Settings = settings; }
void Plan() { var initialModel = new WorldModel(); initialModel.tomatoes = initialTomatoes; initialModel.seeds = initialSeeds; initialModel.money = initialMoney; initialModel.item = (int)initialitem; initialModel.givenAdvice = givenGradmaAdvice; if (initialModel.item == (int)InitialItem.DEBT_FREE) { initialModel.hasPayedTaxes = true; } else { initialModel.hasPayedTaxes = taxesPayed; } _ui.UpdateUI(initialModel); costCorrection(initialModel.item); Func <WorldModel, bool> goal = (g) => g.isPizzaMaster; var actions = GetActions(); var initialState = new GoapState(actions, null, initialModel, goal, Heuristic); var plan = new GOAP(initialState).Execute(); _actionQueue = new Queue <Tuple <string, WorldModel> >(); foreach (var step in plan) { Debug.Log("Action: " + step.Item1); if (!step.Item1) { continue; } var path = step.Item3.Reverse(); foreach (var state in path) { if (state.GeneratedAction != null) { Debug.Log("No action left to do - You will starve to dead - GG"); _actionQueue.Enqueue(Tuple.Create(state.GeneratedAction.Name, state.CurrentWorldModel)); } } } if (_actionQueue.Count == 0) { Debug.Log("No action left to do - You will starve to dead - GG"); } else { ExecuteAction(); } }
public bool IsGoal(GoapState goal) { return(m_hCost == 0); }