private void PlanAndExecute() { var actions = new List <GOAPAction> { new GOAPAction("Patrol") .Effect("isPlayerInSight", true) .LinkedState(patrolState), new GOAPAction("Chase") .Pre("isPlayerInSight", true) .Effect("isPlayerNear", true) .LinkedState(chaseState), new GOAPAction("Melee Attack") .Pre("isPlayerNear", true) .Effect("isPlayerAlive", false) .LinkedState(meleeAttackState) }; var from = new GOAPState(); from.values["isPlayerInSight"] = false; from.values["isPlayerNear"] = false; from.values["isPlayerAlive"] = true; var to = new GOAPState(); to.values["isPlayerAlive"] = false; var planner = new GoapPlanner(); planner.OnPlanCompleted += OnPlanCompleted; planner.OnCantPlan += OnCantPlan; planner.Run(from, to, actions, StartCoroutine); }
public static void RefreshState(GOAPState state) { if (instance) { instance.Refresh_State(state); } }
private void PlanAndExecute() { var actions = new List <GOAPAction> { new GOAPAction("Patrol") .Effect("isPlayerInSight", true) .LinkedState(patrolState), new GOAPAction("Chase") .Pre("isPlayerInSight", true) .Effect("isPlayerNear", true) .LinkedState(chaseState), new GOAPAction("Melee Attack") .Pre("isPlayerNear", true) .Effect("isPlayerAlive", false) .LinkedState(meleeAttackState) }; var from = new GOAPState(); from.values["isPlayerInSight"] = false; from.values["isPlayerNear"] = false; from.values["isPlayerAlive"] = true; var to = new GOAPState(); to.values["isPlayerAlive"] = false; var planner = new GoapPlanner(); var plan = planner.Run(from, to, actions); ConfigureFsm(plan); }
private float heuristic(IGraphNode <GOAPState> curr) { float total = 0; GOAPState state = (GOAPState)curr; if (state.goalValues.ContainsKey(GOAPKeyEnum.isAttacking)) { total += (bool)state.currentValues[GOAPKeyEnum.isAttacking] ? 0 : 1; } if (state.goalValues.ContainsKey(GOAPKeyEnum.isInMeleeRange)) { total += (bool)state.currentValues[GOAPKeyEnum.isInMeleeRange] ? 0 : 1; } if (state.goalValues.ContainsKey(GOAPKeyEnum.medikitNearby)) { total += (bool)state.currentValues[GOAPKeyEnum.medikitNearby] ? 0 : 1; } if (state.goalValues.ContainsKey(GOAPKeyEnum.life)) { total += (float)state.currentValues[GOAPKeyEnum.life] > 5 ? 0 : 1; } if (state.goalValues.ContainsKey(GOAPKeyEnum.haveCriticalLife)) { total += (bool)state.currentValues[GOAPKeyEnum.haveCriticalLife] ? 0 : 1; } return(total); }
IEnumerator Plan() { var observedState = new Dictionary <string, bool>(); var nav = Navigation.instance; var everything = nav.AllItems(_ent.ownerType).Union(nav.AllInventories(_ent.ownerType)); List <GOAPAction> actions = CreatePossibleActionsList(); if (firstTime) { initial.worldSpace = new WorldSpace(); } else { print("recalculo con los valore snuevos"); } GOAPState goal = new GOAPState(); SetGoals(goal); ; StartPlanning(everything, actions, goal); yield return(new WaitForSeconds(.1f)); }
public List <Tuple <IAAction, Item> > RecalculatePlan(GOAPState curState) { var observedState = new Dictionary <string, bool>(); var nav = Navigation.instance; var everything = nav.AllItems(_ent.ownerType).Union(nav.AllInventories(_ent.ownerType)); List <GOAPAction> actions = CreatePossibleActionsList(); GOAPState goal = new GOAPState(); SetGoals(goal); var typeDict = new Dictionary <string, ItemType>() { { "mine", ItemType.Mine } , { "defense", ItemType.Defense } , { "cannon", ItemType.Cannon } , { "core", ItemType.Core } , { "workTable", ItemType.WorkTable } , { "waitZone", ItemType.WaitZone } }; var actDict = new Dictionary <string, IAAction>() { { "Pickup", IAAction.PickUp } , { "Create", IAAction.Create } , { "Upgrade", IAAction.Upgrade } , { "Attack", IAAction.Attack } , { "Wait", IAAction.Wait } , { "SuperAttack", IAAction.SuperAttack } }; var plan = GoapMiniTest.GoapRun(curState, goal, actions, aggressivePlan?true:false); return(plan .Select(pa => pa.name) .Select(a => { var i2 = everything.FirstOrDefault(i => typeDict.Any(kv => a.EndsWith(kv.Key)) ? i.type == typeDict.First(kv => a.EndsWith(kv.Key)).Value : false); if (actDict.Any(kv => a.StartsWith(kv.Key)) && i2 != null) { return Tuple.Create(actDict.First(kv => a.StartsWith(kv.Key)).Value, i2); } else { return null; } }).Where(a => a != null) .ToList()); }
private void SetGoals(GOAPState goal) { if (aggressivePlan) { goal.worldSpace.enemyLife = 0; goal.worldSpace.cannon = "cannon"; } else { goal.worldSpace.hasDefense = true; goal.worldSpace.defenseIsRepaired = true; } }
private void OnCantPlan() { Debug.LogWarning("!!! NO PUDO COMPLETAR EL PLAN !!!"); actions = new List <GOAPAction> { new GOAPAction("Patrol") .Effect("isPlayerInSight", true) .LinkedState(patrolState) .Cost(2), new GOAPAction("Chase") .Pre("isPlayerInSight", true) .Effect("isPlayerNear", true) .Effect("isPlayerInRange", true) .LinkedState(chaseState), new GOAPAction("Melee Attack") .Pre("isPlayerNear", true) .Effect("isPlayerAlive", false) .LinkedState(meleeAttackState), new GOAPAction("Range Attack") .Pre("isPlayerInRange", true) .Pre("hasMana", true) .Effect("isPlayerAlive", false) .Effect("hasMana", false) .LinkedState(rangeAttackState), new GOAPAction("Charge Mana") .Effect("hasMana", true) .LinkedState(chargeManaState) }; from = new GOAPState(); from.values["isPlayerInSight"] = false; from.values["isPlayerNear"] = false; from.values["isPlayerInRange"] = false; from.values["hasMana"] = false; from.values["isPlayerAlive"] = true; to = new GOAPState(); to.values["isPlayerAlive"] = false; planner = new GoapPlanner(); planner.OnPlanCompleted += OnPlanCompleted; planner.OnCantPlan += OnCantPlan; planner.Run(from, to, actions, StartCoroutine); _fsm = GoapPlanner.ConfigureFSM(actions, StartCoroutine); }
private void OnlyPlan() { var actions = new List <GOAPAction> { new GOAPAction("Patrol") .Effect("isPlayerInSight", true), new GOAPAction("Chase") .Pre("isPlayerInSight", true) .Effect("isPlayerInRange", true) .Effect("isPlayerNear", true), new GOAPAction("Melee Attack") .Pre("isPlayerNear", true) .Pre("hasMeleeWeapon", true) .Effect("isPlayerAlive", false) .Cost(2f), new GOAPAction("Range Attack") .Pre("isPlayerInRange", true) .Pre("hasRangeWeapon", true) .Effect("isPlayerAlive", false), new GOAPAction("Pick Melee Weapon") .Effect("hasMeleeWeapon", true) .Cost(2f), new GOAPAction("Pick Range Weapon") .Effect("hasRangeWeapon", true) }; var from = new GOAPState(); from.values["isPlayerInSight"] = true; from.values["isPlayerNear"] = true; from.values["isPlayerInRange"] = true; from.values["isPlayerAlive"] = true; from.values["hasRangeWeapon"] = true; from.values["hasMeleeWeapon"] = false; var to = new GOAPState(); to.values["isPlayerAlive"] = false; var planner = new GoapPlanner(); //planner.OnPlanCompleted += OnPlanCompleted; //planner.OnCantPlan += OnCantPlan; planner.Run(from, to, actions, StartCoroutine); }
public GOAPState(GOAPState source, GOAPAction gen = null) { foreach (var elem in source.values) { if (values.ContainsKey(elem.Key)) { values[elem.Key] = elem.Value; } else { values.Add(elem.Key, elem.Value); } } generatingAction = gen; }
public static IEnumerable <GOAPAction> RunGOAPOld(GOAPEntity entity, GOAPState from, IEnumerable <GOAPAction> actions, int watchdog = 6000) { int watchdogCount = watchdog; var sequence = Algorithms.AStar <GOAPState>( from, current => entity.Satisfies(current), current => entity.Heuristics(current), current => { var arcs = new List <Arc <GOAPState> >(); if (watchdogCount == 0) { return(arcs); } else { watchdogCount--; } foreach (GOAPAction act in actions) { if (act.Preconditions(current)) { var st = new GOAPState(current); st.generatingAction = act; act.Effects(current, st); st.stepId = current.stepId + 1; arcs.Add(new Arc <GOAPState>().SetArc(st, act.Cost)); } } return(arcs); } ); if (sequence == null) { return(null); } foreach (var step in sequence) { Debug.Log(step.generatingAction.Name); } Debug.Log("Watchdog: " + watchdogCount); return(sequence.Select(x => x.generatingAction)); }
private void OnlyPlan() { //var distanceKnife = var actions = new List <GOAPAction> { new GOAPAction("Patrol") .Effect("isPlayerInSight", true), new GOAPAction("Chase") .Pre("isPlayerInSight", true) .Effect("isPlayerInRange", true) .Effect("isPlayerNear", true), new GOAPAction("Melee Attack") .Pre("isPlayerNear", true) .Pre("hasMeleeWeapon", true) .Effect("isPlayerAlive", false) .Cost(2f), new GOAPAction("Range Attack") .Pre("isPlayerInRange", true) .Pre("hasRangeWeapon", true) .Effect("isPlayerAlive", false), new GOAPAction("Pick Melee Weapon") .Effect("hasMeleeWeapon", true), new GOAPAction("Pick Range Weapon") .Effect("hasRangeWeapon", true) }; var from = new GOAPState(); from.values["isPlayerInSight"] = false; from.values["isPlayerNear"] = false; from.values["isPlayerInRange"] = false; from.values["isPlayerAlive"] = true; from.values["hasRangeWeapon"] = false; from.values["hasMeleeWeapon"] = false; var to = new GOAPState(); to.values["isPlayerAlive"] = false; var planner = new GoapPlanner(); planner.Run(from, to, actions); }
public void Run(GOAPState from, GOAPState to, IEnumerable <GOAPAction> actions, Func <IEnumerator, Coroutine> startCoroutine) { _watchdog = _WATCHDOG_MAX; var astar = new AStar <GOAPState>(); astar.OnPathCompleted += OnPathCompleted; astar.OnCantCalculate += OnCantCalculate; var astarEnumerator = astar.Run(from, state => Satisfies(state, to), node => Explode(node, actions, ref _watchdog), state => GetHeuristic(state, to)); startCoroutine(astarEnumerator); }
public static IEnumerable <GOAPAction> RunGOAP(GOAPEntity entity, GOAPState from, IEnumerable <GOAPAction> actions, int watchdog = 6000) { int watchdogCount = 0; var sequence = Algorithms.AStarNew <GOAPState>( from, current => entity.Satisfies(current), (current, toCompare) => current.Equals(toCompare), current => entity.Heuristics(current), current => { return(actions .Where(a => a.Preconditions(current)) .Aggregate( new FList <Tuple <GOAPState, float> >(), (possibleList, a) => { if (watchdogCount < watchdog) { var st = new GOAPState(current); st.generatingAction = a; a.Effects(current, st); st.stepId = current.stepId + 1; watchdogCount++; return possibleList + Tuple.Create(st, a.Cost); } else { return possibleList; } } )); } ); if (sequence == null) { return(null); } Debug.Log("Watchdog: " + watchdogCount); return(sequence.Skip(1).Select(x => { Debug.Log(x.Item1.generatingAction.Name); return x.Item1.generatingAction; })); }
void Refresh_State(GOAPState state) { foreach (var s in state.values) { if (!col_Elements.ContainsKey(s.Key)) { var elem = GameObject.Instantiate(model, parent_elements); elem.Configure(OnValueChange, s.Key); elem.SetValue(s.Value); col_Elements.Add(s.Key, elem); } else { var elem = col_Elements[s.Key]; elem.SetValue(s.Value); } } }
private GOAPState GetCurState() { WorldSpace world = new WorldSpace(); world.gold = _gold; world.hasDefense = structures["Defense"]; world.cannon = structures["Cannon"] ? "cannon" : ""; if (structures["CannonUpgraded"]) { world.cannon = "cannon upgraded"; } world.defenseIsRepaired = structures["DefenseUpgraded"]; world.enemyLife = playerCore.GetLife(); world.bullet = bullets; GOAPState cur = new GOAPState(world); return(cur); }
private void OnReplan() { if (Time.time >= _lastReplanTime + _replanRate) { _lastReplanTime = Time.time; } else { return; } var actions = new List <GOAPAction> { new GOAPAction("Patrol") .Effect("isPlayerInSight", true) .LinkedState(patrolState), new GOAPAction("Chase") .Pre("isPlayerInSight", true) .Effect("isPlayerNear", true) .LinkedState(chaseState), new GOAPAction("Melee Attack") .Pre("isPlayerNear", true) .Effect("isPlayerAlive", false) .LinkedState(meleeAttackState) }; var from = new GOAPState(); from.values["isPlayerInSight"] = false; from.values["isPlayerNear"] = false; from.values["isPlayerAlive"] = true; var to = new GOAPState(); to.values["isPlayerAlive"] = false; var planner = new GoapPlanner(); var plan = planner.Run(from, to, actions); ConfigureFsm(plan); }
private static IEnumerable <WeightedNode <GOAPState> > Explode(GOAPState node, IEnumerable <GOAPAction> actions, ref int watchdog) { if (watchdog == 0) { return(Enumerable.Empty <WeightedNode <GOAPState> >()); } watchdog--; return(actions.Where(action => action.preconditions.All(kv => kv.In(node.values))) .Aggregate(new List <WeightedNode <GOAPState> >(), (possibleList, action) => { var newState = new GOAPState(node); newState.values.UpdateWith(action.effects); newState.generatingAction = action; newState.step = node.step + 1; possibleList.Add(new WeightedNode <GOAPState>(newState, action.cost)); return possibleList; })); }
private static bool Satisfies(GOAPState state, GOAPState to) => to.values.All(kv => kv.In(state.values));
public GOAPState(GOAPState source, GOAPAction gen = null) { worldSpace = new WorldSpace(source.worldSpace); generatingAction = gen; }
public GOAPState(GOAPState source, string _myNameState = "default")//NUEVO CONSTRUCTOR { //directamente piso todo el objeto WorldState, esto se va a usar para crear nuevos estados con la refe del estado del mundo anterior currentState = source.currentState; myNameState = _myNameState; }
private void Start() { actions = new List <GOAPAction>() { new GOAPAction("BuildHouse").SetPreconditions ( (a) => { return (a.HasHammer && a.Energy >= 65f && a.Wood >= 10 && a.PatienceRate >= 2 && !a.HouseOwned); } ).SetEffects ( (a, b) => { b.Energy = a.Energy - 65f; b.Wood = a.Wood - 10; b.HouseOwned = true; } ).SetCost(5) .SetAct(a => StartCoroutine(ActionRoutines.BuildHouseCoroutine(a))), new GOAPAction("GatherWood").SetPreconditions ( (a) => { return (a.HasAxe && a.Energy >= 40f); } ).SetEffects ( (a, b) => { b.Energy = a.Energy - 35f; b.Wood = a.Wood + 10; } ).SetCost(3.5f) .SetAct(a => StartCoroutine(ActionRoutines.GatherWoodCoroutine(a))), new GOAPAction("PickAxe").SetPreconditions ( (a) => { return (!a.HasAxe && a.PatienceRate >= 1); } ).SetEffects ( (a, b) => { b.HasAxe = true; b.PatienceRate = a.PatienceRate - 1; } ).SetCost(0.5f) .SetAct(a => StartCoroutine(ActionRoutines.GetToolCoroutine(a, ToolType.AXE))), new GOAPAction("Sleep").SetPreconditions ( (a) => { return (a.BedBuilt && a.Energy <= 75 && a.PatienceRate >= 2); } ).SetEffects ( (a, b) => { b.Energy = a.Energy + 50; b.PatienceRate = a.PatienceRate + 2; } ).SetCost(1f) .SetAct(a => StartCoroutine(ActionRoutines.SleepCoroutine(a))), new GOAPAction("PickHammer").SetPreconditions ( (a) => { return (!a.HasHammer && a.PatienceRate >= 1); } ).SetEffects ( (a, b) => { b.HasHammer = true; b.PatienceRate = a.PatienceRate - 1; } ).SetCost(0.5f) .SetAct(a => StartCoroutine(ActionRoutines.GetToolCoroutine(a, ToolType.HAMMER))), new GOAPAction("BuildBed").SetPreconditions ( (a) => { return (!a.BedBuilt && a.Energy >= 40f && a.Wood >= 3 && a.PatienceRate >= 1); } ).SetEffects ( (a, b) => { b.Energy = a.Energy - 40f; b.BedBuilt = true; b.PatienceRate = a.PatienceRate - 1; } ).SetCost(2f) .SetAct(a => ActionRoutines.BuildBed(a)), new GOAPAction("SummonSatan").SetPreconditions ( (a) => { return (a.PatienceRate == 0); } ).SetEffects ( (a, b) => { b.Energy = 100; b.HouseOwned = true; } ).SetCost(10f) .SetAct(a => StartCoroutine(ActionRoutines.SummonSatanCoroutine(a))), new GOAPAction("KillEnemy").SetPreconditions ( (a) => { return (a.PatienceRate <= 3 && a.HasAxe && a.Energy <= 50 && a.Energy >= 15); } ).SetEffects ( (a, b) => { b.Energy -= 30; b.Keys.Add("House 1"); b.PatienceRate += 10; } ).SetCost(7.5f) .SetAct(a => StartCoroutine(ActionRoutines.KillEnemyCoroutine(a))), new GOAPAction("OpenHouse1").SetPreconditions ( (a) => { return (a.Keys.Contains("House 1")); } ).SetEffects ( (a, b) => { b.HouseOwned = true; } ).SetCost(0.5f) .SetAct(a => StartCoroutine(ActionRoutines.OpenHouseCoroutine(a))), }; current = new GOAPState(100, 0); satisfies = (a) => { return(a.Energy >= 90 && a.HouseOwned); }; heuristics = (a) => { float cost = 0; cost += Mathf.Max(a.Energy, 0) < 90f ? (90f - Mathf.Max(a.Energy, 0)) * 0.3f : 0f; cost += !a.HouseOwned ? 1f : 0f; return(cost); }; sequence = GOAP.RunGOAPOld(this, current, actions); sequence.First().Act(this); }
public static IEnumerable <GOAPAction> GoapRun(GOAPState from, GOAPState to, IEnumerable <GOAPAction> actions, bool aggressive_heuristic) { int watchdog = 300; var seq = AStarNormal <GOAPState> .Run( from, to, (curr, goal) => { if (aggressive_heuristic) { return(curr.AgressiveHeuristic(goal.worldSpace)); } else { return(curr.PassiveHeuristic(goal.worldSpace)); } }, curr => { if (aggressive_heuristic) { return(curr.AgressiveComparison(to.worldSpace)); } else { return(curr.PassiveComparison(to.worldSpace)); } }, /*to.boolValues.All(kv => kv.In(curr.boolValues))&& * to.intValues.All(i => i.In(curr.intValues)) && * to.floatValues.All(i => i.In(curr.floatValues)) && * to.stringValues.All(i => i.In(curr.stringValues)) ,*/ curr => { if (watchdog == 0) { return(Enumerable.Empty <AStarNormal <GOAPState> .Arc>()); } else { watchdog--; } //return actions.Where(action => action.preconditionsBool.All(kv => kv.In(curr.boolValues)) && // action.preconditionsInt.All(k=> k.In(curr.intValues))) return(actions.Where(action => action.preConditions.All(f => f(curr.worldSpace) == true)) .Aggregate(new FList <AStarNormal <GOAPState> .Arc>(), (possibleList, action) => { var newState = new GOAPState(curr); // newState.boolValues.UpdateWith(action.effectsBool); // newState.intValues.UpdateWith(action.effectsInt); action.effects.ForEach((f) => { f(newState.worldSpace); }); newState.generatingAction = action; newState.step = curr.step + 1; return possibleList + new AStarNormal <GOAPState> .Arc(newState, action.cost); })); }); if (seq == null) { Debug.Log("Imposible planear"); return(null); } foreach (var act in seq.Skip(1)) { Debug.Log(act); } Debug.Log("WATCHDOG " + watchdog); return(seq.Skip(1).Select(x => x.generatingAction)); }
internal void SetCurState(GOAPState curState) { cur = curState; }
private void StartPlanning(IEnumerable <Item> everything, List <GOAPAction> actions, GOAPState goal) { var typeDict = new Dictionary <string, ItemType>() { { "mine", ItemType.Mine } , { "defense", ItemType.Defense } , { "cannon", ItemType.Cannon } , { "core", ItemType.Core } , { "workTable", ItemType.WorkTable } , { "waitZone", ItemType.WaitZone } }; var actDict = new Dictionary <string, IAAction>() { { "Pickup", IAAction.PickUp } , { "Create", IAAction.Create } , { "Upgrade", IAAction.Upgrade } , { "Attack", IAAction.Attack } , { "Wait", IAAction.Wait } , { "SuperAttack", IAAction.SuperAttack } }; var plan = GoapMiniTest.GoapRun(initial, goal, actions, true); if (plan == null) { print("Couldn't plan"); } else { var li = plan.Select(x => x.name).ToList(); print(li); _ia.SetPlan( plan .Select(pa => pa.name) .Select(a => { var i2 = everything.FirstOrDefault(i => typeDict.Any(kv => a.EndsWith(kv.Key)) ? i.type == typeDict.First(kv => a.EndsWith(kv.Key)).Value : false); if (actDict.Any(kv => a.StartsWith(kv.Key)) && i2 != null) { return(Tuple.Create(actDict.First(kv => a.StartsWith(kv.Key)).Value, i2)); } else { return(null); } }).Where(a => a != null) .ToList() ); if (firstTime) { firstTime = false; _ia.NextStep(); } } }
private static float GetHeuristic(GOAPState from, GOAPState goal) => goal.values.Count(kv => !kv.In(from.values));