Esempio n. 1
0
        public bool GetRandomNodeForLocation(string groupPath, TimeOfDay timeOfDay, out ActionNodeState nodeState)
        {
            bool foundNode = false;

            nodeState = null;
            List <ActionNodeState> nodeStates = null;

            if (GetNodesForLocation(groupPath, out nodeStates))
            {
                List <int> numbersTried = new List <int> ();
                //try random numbers until we have none left
                while (numbersTried.Count <= nodeStates.Count)
                {
                    int randomIndex = UnityEngine.Random.Range(0, nodeStates.Count);
                    //get another number that we haven't tried yet
                    while (numbersTried.Contains(randomIndex))
                    {
                        randomIndex = UnityEngine.Random.Range(0, nodeStates.Count);
                    }
                    numbersTried.Add(randomIndex);
                    //check to see if the node's time of day lines up
                    ActionNodeState checkState = nodeStates [randomIndex];
                    if (Flags.Check((uint)checkState.RoutineHours, (uint)timeOfDay, Flags.CheckType.MatchAny))
                    {
                        foundNode = true;
                        nodeState = checkState;
                        break;
                    }
                }
            }
            return(foundNode);
        }
Esempio n. 2
0
        public bool GetRandomNodeForLocation(string groupPath, Vector3 nearPoint, float range, out ActionNodeState nodeState)
        {
            bool foundNode = false;

            nodeState = null;
            List <ActionNodeState> nodeStates = null;

            if (GetNodesForLocation(groupPath, out nodeStates))
            {
                List <int> numbersTried = new List <int> ();
                //try random numbers until we have none left
                while (numbersTried.Count <= nodeStates.Count)
                {
                    int randomIndex = UnityEngine.Random.Range(0, nodeStates.Count);
                    //get another number that we haven't tried yet
                    while (numbersTried.Contains(randomIndex))
                    {
                        randomIndex = UnityEngine.Random.Range(0, nodeStates.Count);
                    }
                    numbersTried.Add(randomIndex);
                    //check to see if the node is in range
                    ActionNodeState checkState = nodeStates [randomIndex];
                    if (checkState.IsLoaded && Vector3.Distance(checkState.actionNode.transform.position, nearPoint) < range)
                    {
                        foundNode = true;
                        nodeState = checkState;
                        break;
                    }
                }
            }
            return(foundNode);
        }
Esempio n. 3
0
        public ActionNode SpawnActionNode(WIGroup group, ActionNodeState actionNodeState, Transform nodeParentTransform)
        {
            ActionNode actionNode = null;

            if (!actionNodeState.IsLoaded)
            {
                GameObject newNodeGameObject = nodeParentTransform.gameObject.CreateChild(actionNodeState.FullName).gameObject;
                actionNode       = newNodeGameObject.AddComponent <ActionNode> ();
                actionNode.State = actionNodeState;
                //since we're spawning this in the chunk we have to apply the group's transforms to the node
                actionNode.State.Transform.ApplyTo(actionNode.transform);
                //now move it to the final point
                actionNode.transform.parent = Transforms.Nodes;
                actionNode.State.actionNode = actionNode;
            }
            else
            {
                actionNode = actionNodeState.actionNode;
            }
            //add the action node state to the lookup
            actionNodeState.ParentGroupPath = group.Path;
            List <ActionNodeState> nodeStates = null;

            if (!NodeData.NodeStates.TryGetValue(group.Path, out nodeStates))
            {
                nodeStates = new List <ActionNodeState> ();
                NodeData.NodeStates.Add(group.Path, nodeStates);
            }
            nodeStates.SafeAdd(actionNodeState);
            return(actionNode);
        }
Esempio n. 4
0
 public bool GetOrCreateNode(WIGroup group, Transform parentTransform, string nodeName, out ActionNodeState nodeState)
 {
     nodeState = null;
     if (GetNode(nodeName, false, out nodeState))
     {
         return(true);
     }
     else
     {
         nodeState      = new ActionNodeState();
         nodeState.Name = nodeName;
         SpawnActionNode(group, nodeState, parentTransform);
     }
     return(nodeState != null);
 }
Esempio n. 5
0
        public bool GetNodes(List <string> actionNodeNames, bool skipReserved, List <ActionNodeState> nodeStates)
        {
            ActionNodeState nodeState       = null;
            bool            foundAtLeastOne = false;

            for (int i = 0; i < actionNodeNames.Count; i++)
            {
                if (GetNode(actionNodeNames [i], skipReserved, out nodeState))
                {
                    nodeStates.Add(nodeState);
                    foundAtLeastOne = true;
                }
            }
            return(foundAtLeastOne);
        }
Esempio n. 6
0
        public static MotileAction Wait(ActionNodeState state)
        {
            MotileAction newAction = new MotileAction();

            newAction.Type = MotileActionType.Wait;
            if (state.IsLoaded)
            {
                newAction.LiveTarget = state.actionNode;
            }
            newAction.Target        = new MobileReference(state.Name, state.ParentGroupPath);
            newAction.Expiration    = MotileExpiration.Never;
            newAction.YieldBehavior = MotileYieldBehavior.YieldAndFinish;
            newAction.IdleAnimation = state.IdleAnimation;
            return(newAction);
        }
Esempio n. 7
0
 public bool FindSuccessNode()
 {
     if (SuccessNode == null)
     {
         ActionNodeState successState = null;
         if (ParentChunk.GetNode(State.SuccessActionNodeName, false, out successState))
         {
             SuccessNode = successState.actionNode;
             if (SuccessNode == null)
             {
                 //Debug.Log ("Couldn't find success node in " + name + " but it doesn't matter, still returning true");
                 return(false);
             }
         }
     }
     return(true);
 }
Esempio n. 8
0
        public void CreateNodesAndTriggers()
        {
            foreach (KeyValuePair <string, KeyValuePair <string, string> > triggerStatePair in TriggerData.TriggerStates)
            {
                AddTrigger(triggerStatePair, Transforms.Triggers, false, false);
            }

            foreach (KeyValuePair <string, List <ActionNodeState> > actionNodeStateList in NodeData.NodeStates)
            {
                for (int i = 0; i < actionNodeStateList.Value.Count; i++)
                {
                    ActionNodeState actionNodeState   = actionNodeStateList.Value [i];
                    GameObject      newNodeGameObject = Transforms.Nodes.gameObject.FindOrCreateChild(actionNodeState.FullName).gameObject;
                    ActionNode      newNode           = newNodeGameObject.GetOrAdd <ActionNode> ();
                    newNode.State = actionNodeState;
                    newNode.State.Transform.ApplyTo(newNode.transform);
                    newNode.State.actionNode      = newNode;
                    newNode.State.ParentGroupPath = actionNodeStateList.Key;
                }
            }
        }
Esempio n. 9
0
 public bool FindGuardNode()
 {
     if (GuardNode == null)
     {
         ActionNodeState guardNodeState = null;
         if (ParentChunk.GetNode(State.GuardActionNodeName, false, out guardNodeState))
         {
             guardNodeState.OccupantIsDead = State.GuardIsDead;
             GuardNode = guardNodeState.actionNode;
             if (GuardNode == null)
             {
                 //Debug.Log ("Couldn't get guard node from action node state, quitting in " + name);
                 return(false);
             }
         }
         else
         {
             //Debug.Log ("Couldn't get guard node from parent chunk, quitting in " + name);
             return(false);
         }
     }
     return(true);
 }
Esempio n. 10
0
        public bool GetNode(string actionNodeName, bool skipReserved, out ActionNodeState nodeState)
        {
            bool foundNode = false;

            nodeState = null;
            foreach (Transform nodeTransform in Transforms.Nodes)
            {
                if (nodeTransform.name.Contains(actionNodeName))
                {
                    ActionNode actionNode = null;
                    if (nodeTransform.gameObject.HasComponent <ActionNode> (out actionNode) &&
                        actionNode.State.Name == actionNodeName)
                    {
                        if (!(skipReserved && actionNode.IsReserved))
                        {
                            nodeState = actionNode.State;
                            foundNode = true;
                            break;
                        }
                    }
                }
            }
            return(foundNode);
        }
Esempio n. 11
0
        public IEnumerator SpawnOverTime()
        {
            ActionNodeState nodeState = AvailableSpawnNodes [0];

            if (State.SpawnBehindPlayer)
            {
                Vector3 nodeDirection = Vector3.zero;
                float   dot           = 0f;
                float   leastDotSoFar = Mathf.Infinity;
                for (int i = 0; i < AvailableSpawnNodes.Count; i++)
                {
                    nodeDirection = (AvailableSpawnNodes [i].actionNode.Position - Player.Local.Position).normalized;
                    dot           = Vector3.Dot(Player.Local.FocusVector, nodeDirection);
                    if (dot < leastDotSoFar)
                    {
                        leastDotSoFar = dot;
                        nodeState     = AvailableSpawnNodes [i];
                    }
                }
            }
            CurrentSpawnNode = nodeState.actionNode;
            if (!string.IsNullOrEmpty(State.CustomSpeech))
            {
                //overrides node state's speech
                nodeState.CustomSpeech = State.CustomSpeech;
            }
            if (!string.IsNullOrEmpty(State.CustomConversation))
            {
                //overrides node state's speech
                nodeState.CustomConversation = State.CustomConversation;
            }
            //wait until we're ready to spawn
            double waitUntil = Frontiers.WorldClock.AdjustedRealTime + State.SpawnDelay;

            while (Frontiers.WorldClock.AdjustedRealTime < waitUntil)
            {
                yield return(null);
            }
            //then boom! go
            if (!Characters.GetOrSpawnCharacter(CurrentSpawnNode, State.CharacterName, ParentChunk.AboveGroundGroup, out SpawnedCharacter))
            {
                Debug.Log("Couldn't spawn character");
            }
            else
            {
                if (!string.IsNullOrEmpty(State.DTSOnSpawn))
                {
                    if (!State.DTSFirstTimeOnly || State.NumTimesTriggered < 2)
                    {
                        Talkative talkative = null;
                        if (SpawnedCharacter.worlditem.Is <Talkative> (out talkative))
                        {
                            talkative.SayDTS(State.DTSOnSpawn);
                            SpawnedCharacter.LookAtPlayer();
                        }
                    }
                }
            }
            mSpawningOverTime = false;
            yield break;
        }
Esempio n. 12
0
        public static bool StartAction(Motile m, MotileAction action)
        {
            //Debug.Log("trying to start action");
            //open up the door for mod-supplied delegtates
            //do some general cleanup - if we're starting an action, we want to start from scratch
            if (m.GoalObject == null)
            {
                Debug.Log("Goal object is null, action was cancelled");
                action.Cancel();
                return(true);
            }

            if (!GetUpdateCoroutine(m, action))
            {
                Debug.Log("Couldn't get coroutine");
                action.Cancel();
                return(true);
            }
            //reset this just in case
            m.AvoidingObstacle    = false;
            m.GoalObject.parent   = m.worlditem.Group.transform;
            m.GoalObject.position = m.LastKnownPosition;            // + transform.forward;
            //m.rvoController.PositionLocked = false;
            //m.rvoController.RotationLocked = false;
            m.TargetMovementSpeed        = 0.0f;
            m.CurrentRotationChangeSpeed = m.State.MotileProps.RotationChangeSpeed;
            //get the default pathfinding method
            action.Method = GetDefaultGoToMethod(m.State.MotileProps.DefaultGoToMethod, action.Method);

            //okay, now handle the new action
            if (action.State != MotileActionState.Waiting || !action.ResetAfterInterrupt)               //if we're NOT resuming OR we're supposed to reset on resuming
            {
                action.WTStarted = WorldClock.AdjustedRealTime;
            }
            bool started = false;

            switch (action.Type)
            {
            case MotileActionType.FocusOnTarget:
            case MotileActionType.FollowRoutine:
                started = true;
                break;

            case MotileActionType.FollowGoal:
                if (action.HasLiveTarget)
                {
                    //goal objects can be moved externally
                    //so we don't need a live target
                    m.GoalObject.position = action.LiveTarget.Position;
                }
                started = true;
                break;

            case MotileActionType.FleeGoal:
                if (action.HasLiveTarget)
                {
                    m.GoalObject.position = action.LiveTarget.Position;
                }
                started = true;
                break;

            case MotileActionType.WanderIdly:
                m.GoalObject.position = m.worlditem.Position;
                started = true;
                break;

            case MotileActionType.FollowTargetHolder:
                if (!action.HasLiveTarget)
                {
                    //TODO get live target?
                }
                if (action.HasLiveTarget)
                {
                    m.GoalHolder = action.LiveTarget.gameObject.GetOrAdd <RVOTargetHolder> ();
                }
                else
                {
                    m.GoalHolder = action.LiveTargetHolder;
                }
                started = true;
                break;

            case MotileActionType.GoToActionNode:
                //wait a tick to let the live target load
                if (m.LastOccupiedNode != null)
                {
                    m.LastOccupiedNode.VacateNode(m.worlditem);
                }
                if (!action.HasLiveTarget)
                {
                    //get live target
                    ActionNodeState nodeState = null;
                }
                started = true;
                break;

            case MotileActionType.Wait:
            default:
                m.TargetMovementSpeed = 0.0f;
                started = true;
                break;
            }

            if (started)
            {
                if (action.State != MotileActionState.Error)                    //preserve the error
                {
                    action.State = MotileActionState.Started;
                }
                //send messages
                action.OnStartAction.SafeInvoke();
                action.OnStartAction = null;
            }

            return(started);
        }