public static void BuildBed(GOAPEntity ent) { var bed = GameObject.CreatePrimitive(PrimitiveType.Cube); bed.transform.position = new Vector3( ent.transform.position.x + Random.Range(-2f, 2f), 0, ent.transform.position.z + Random.Range(-2f, 2f) ); bed.GetComponent <Collider>().isTrigger = true; bed.AddComponent <BedNode>(); bed.GetComponent <BedNode>().neighbors = new Node[] { }; var neighbors = NodeList.allNodes .Where(a => Vector3.Distance(a.transform.position, bed.transform.position) < 15f && a != bed); foreach (var n in neighbors) { n.neighbors = n.neighbors.Concat(new[] { bed.GetComponent <BedNode>() }).ToArray(); } bed.GetComponent <BedNode>().neighbors.Concat(neighbors.ToArray()); ent.Sequence.First().Effects(ent.Current, ent.Current); ent.Sequence = ent.Sequence.Skip(1); if (ent.Sequence.Count() > 0) { ent.Sequence.First().Act(ent); } }
public static IEnumerator SummonSatanCoroutine(GOAPEntity ent) { Node startingPoint = NodeList.allNodes.OrderBy(a => { return(Vector3.Distance(a.transform.position, ent.transform.position)); }).First(); HouseNode target = NodeList.allNodes .Select(a => { return(a.GetComponent <HouseNode>()); }) .Where(a => a != null && !a.owned && !a.constructed) .OrderBy(a => { return(Vector3.Distance(a.transform.position, ent.transform.position)); }) .First(); var path = Algorithms.AStar( startingPoint, a => a == target, a => Vector3.Distance(target.transform.position, a.transform.position), a => { return(a.neighbors.Where(b => b.gameObject.activeSelf).Select(n => new Arc <Node>().SetArc(n, Vector3.Distance(n.transform.position, a.transform.position)))); } ); while (path.Count > 0) { ent.transform.forward = new Vector3(path.Peek().transform.position.x - ent.transform.position.x, ent.transform.forward.y, path.Peek().transform.position.z - ent.transform.position.z); ent.transform.position += ent.transform.forward * Mathf.Min(Vector3.Distance(path.Peek().transform.position, ent.transform.position), ent.speed * Time.deltaTime); if (Vector3.Distance(path.Peek().transform.position, ent.transform.position) <= 2f) { path.Pop(); } yield return(new WaitForEndOfFrame()); } var satanSymbol = ent.transform.GetChild(0).gameObject; satanSymbol.SetActive(true); target.constructed = true; target.owned = true; while (true) { satanSymbol.transform.eulerAngles += satanSymbol.transform.forward * 50f * Time.deltaTime; satanSymbol.transform.localScale += (Vector3.one * Time.deltaTime); yield return(new WaitForEndOfFrame()); } ent.Sequence.First().Effects(ent.Current, ent.Current); ent.Sequence = ent.Sequence.Skip(1); if (ent.Sequence.Count() > 0) { ent.Sequence.First().Act(ent); } }
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)); }
public static IEnumerator KillEnemyCoroutine(GOAPEntity ent) { Node startingPoint = NodeList.allNodes.OrderBy(a => { return(Vector3.Distance(a.transform.position, ent.transform.position)); }).First(); var houses = NodeList.allNodes .Select(a => { return(a.GetComponent <HouseNode>()); }) .Where(a => a != null && a.owned && a.constructed) .Select(a => a.keyId); EnemyWaypoint target = NodeList.allNodes .Select(a => { return(a.GetComponent <EnemyWaypoint>()); }) .Where(a => a != null && a.gameObject.activeSelf && houses.Contains(a.keyId)) .OrderBy(a => { return(Vector3.Distance(a.transform.position, ent.transform.position)); }) .First(); var path = Algorithms.AStar( startingPoint, a => a == target, a => Vector3.Distance(target.transform.position, a.transform.position), a => { return(a.neighbors.Where(b => b.gameObject.activeSelf).Select(n => new Arc <Node>().SetArc(n, Vector3.Distance(n.transform.position, a.transform.position)))); } ); while (path.Count > 0) { ent.transform.forward = new Vector3(path.Peek().transform.position.x - ent.transform.position.x, ent.transform.forward.y, path.Peek().transform.position.z - ent.transform.position.z); ent.transform.position += ent.transform.forward * Mathf.Min(Vector3.Distance(path.Peek().transform.position, ent.transform.position), ent.speed * Time.deltaTime); if (Vector3.Distance(path.Peek().transform.position, ent.transform.position) <= 2f) { path.Pop(); } yield return(new WaitForEndOfFrame()); } ent.Current.Keys.Add(target.keyId); target.gameObject.SetActive(false); ent.Sequence.First().Effects(ent.Current, ent.Current); ent.Sequence = ent.Sequence.Skip(1); if (ent.Sequence.Count() > 0) { ent.Sequence.First().Act(ent); } }
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; })); }
public static IEnumerator GetToolCoroutine(GOAPEntity ent, ToolType type) { Node startingPoint = NodeList.allNodes.OrderBy(a => { return(Vector3.Distance(a.transform.position, ent.transform.position)); }).First(); ToolNode target = NodeList.allNodes .Select(a => { return(a.GetComponent <ToolNode>()); }) .Where(a => a != null && a.gameObject.activeSelf && a.type == type) .OrderBy(a => { return(Vector3.Distance(a.transform.position, ent.transform.position)); }) .First(); var path = Algorithms.AStar( startingPoint, a => a == target, a => Vector3.Distance(target.transform.position, a.transform.position), a => { return(a.neighbors.Where(b => b.gameObject.activeSelf).Select(n => new Arc <Node>().SetArc(n, Vector3.Distance(n.transform.position, a.transform.position)))); } ); while (path.Count > 0) { ent.transform.forward = new Vector3(path.Peek().transform.position.x - ent.transform.position.x, ent.transform.forward.y, path.Peek().transform.position.z - ent.transform.position.z); ent.transform.position += ent.transform.forward * Mathf.Min(Vector3.Distance(path.Peek().transform.position, ent.transform.position), ent.speed * Time.deltaTime); if (Vector3.Distance(path.Peek().transform.position, ent.transform.position) <= 2f) { path.Pop(); } yield return(new WaitForEndOfFrame()); } target.transform.GetChild(0).parent = ent.transform; target.gameObject.SetActive(false); ent.Sequence.First().Effects(ent.Current, ent.Current); ent.Sequence = ent.Sequence.Skip(1); if (ent.Sequence.Count() > 0) { ent.Sequence.First().Act(ent); } }