public static Container FindRandomNearbyContainer(BaseAI instance, MaxStack <Container> knownContainers, string[] m_acceptedContainerNames, float containerSearchRadius) { Common.Dbgl($"Enter {nameof(FindRandomNearbyContainer)}, looking for {m_acceptedContainerNames.Join()}"); Vector3 position = instance.transform.position; var pieceList = new List <Piece>(); Piece.GetAllPiecesInRadius(position, containerSearchRadius, pieceList); var allcontainerPieces = pieceList.Where(p => m_acceptedContainerNames.Contains(GetPrefabName(p.name)) && CanSeeTarget(instance, p.gameObject)); //&& CanSeeTarget(instance, p.gameObject) var containers = allcontainerPieces?.Select(p => p.gameObject.GetComponentInChildren <Container>()).Where(c => !knownContainers.Contains(c)).ToList(); containers.AddRange(allcontainerPieces?.Select(p => p.gameObject.GetComponent <Container>()).Where(c => !knownContainers.Contains(c))); if (!containers.Any()) { Common.Dbgl("No containers found, returning null"); return(null); } // select random piece return(containers.RandomOrDefault()); }
public static bool AssignmentTimeoutCheck(ref MaxStack <Assignment> assignments, float dt, float timeBeforeAssignmentCanBeRepeated) { foreach (Assignment assignment in assignments) { assignment.AssignmentTime += dt; int multiplicator = 1; if (assignment.TypeOfAssignment.ComponentType == typeof(Fireplace)) { multiplicator = 3; } if (assignment.AssignmentTime > timeBeforeAssignmentCanBeRepeated * multiplicator) { Common.Dbgl($"GreAssignment: {assignment} forgotten"); assignments.Remove(assignment); if (!assignments.Any()) { return(false); } break; } } return(true); }
public static Assignment FindRandomNearbyAssignment(BaseAI instance, List <string> trainedAssignments, MaxStack <Assignment> knownassignments, float assignmentSearchRadius) { Common.Dbgl($"Enter {nameof(FindRandomNearbyAssignment)}"); Vector3 position = instance.transform.position; //Generate list of acceptable assignments var pieceList = new List <Piece>(); Piece.GetAllPiecesInRadius(position, assignmentSearchRadius, pieceList); var allAssignablePieces = pieceList.Where(p => Assignment.AssignmentTypes.Any(a => GetPrefabName(p.name) == a.PieceName && trainedAssignments.Contains(GetPrefabName(p.name)) && CanSeeTarget(instance, p.gameObject))); //&& CanSeeTarget(instance, p.gameObject) // no assignments detekted, return false if (!allAssignablePieces.Any()) { return(null); } Common.Dbgl($"Assignments found: {allAssignablePieces.Select(n => n.name).Join()}"); // filter out assignments already in list var newAssignments = allAssignablePieces.Where(p => !knownassignments.Any(a => a.AssignmentObject == p.gameObject)); Common.Dbgl($"Assignments after filter: {newAssignments.Select(n => n.name).Join()}"); // filter out inaccessible assignments //newAssignments = newAssignments.Where(p => Pathfinding.instance.GetPath(greylingPosition, p.gameObject.transform.position, null, Pathfinding.AgentType.Humanoid, true, true)); if (!newAssignments.Any()) { return(null); } // select random piece //var random = new System.Random(); //int index = random.Next(newAssignments.Count()); var selekted = newAssignments.RandomOrDefault(); Common.Dbgl($"Returning assignment: {selekted.name}"); Assignment randomAssignment = new Assignment(selekted); return(randomAssignment); }