private void disengageInteraction()
 {
     animIndex = 0;
     animationScript = GameObject.FindObjectOfType(typeof(spt_monsterAnimations)) as spt_monsterAnimations;
     interactionScript = GameObject.FindObjectOfType(typeof(spt_monsterInteraction)) as spt_monsterInteraction;
     interactionScript.interactWithObject(interactionScript.getInteractionName(), interactionScript.getInteractionItemName());
     animationScript.disengageInteraction();
     disengageActivated = false;
 }
    // Use this for initialization
    void Start()
    {
        movementScript = GameObject.FindObjectOfType(typeof(spt_monsterMovement)) as spt_monsterMovement;
        animationScript = GameObject.FindObjectOfType(typeof(spt_monsterAnimations)) as spt_monsterAnimations;
        if (!isServer) return;

        players = getHost();

        // Sets the initial anger level of the monster to zero.
        angerLevel = 0;
        isAttacking = false;

        //Begins the gradual anger depreciation over time.
        InvokeRepeating("angerDepreciation", 1, 1);

        GameObject[] _players = GameObject.FindGameObjectsWithTag("Player");
    }
    // Update is called once per frame
    void Update()
    {
        animationScript = GameObject.FindObjectOfType(typeof(spt_monsterAnimations)) as spt_monsterAnimations;
        if (animIndex != lastAnimIndex)
        {
            animationScript.animator.SetInteger("animation", animIndex);
            lastAnimIndex = animIndex;
        }

        if (!isServer) return;

        if (!navMeshInitialized || !waypointInitialized)
        {
            //Gets the waypoint graph from another script, then sets the first waypoint to the center of the room.
            if (garageScript != null)
                waypointGraph = garageScript.getWaypointGraph();
            else if (rangerOutpostScript != null)
                waypointGraph = rangerOutpostScript.getWaypointGraph();
            else
                waypointGraph = opticsLabScript.getWaypointGraph();

            agent = GetComponent<NavMeshAgent>();
            agent.enabled = true;
            agent.SetDestination(waypoints[0].position);
            currentWaypoint = 0;

            if (agent != null)
                navMeshInitialized = true;
            if (waypointGraph != null)
                waypointInitialized = true;
        }

        networkScript = GameObject.FindGameObjectWithTag("Player").GetComponent<spt_NetworkPuzzleLogic>();
        if (monsterPuzzleCompletionIndex == -1)
        {
            for (int i = 0; i < networkScript.PuzzleStates.Count; i++)
            {
                if (networkScript.PuzzleStates[i].name == "puzzleCompletionMonster" )
                {
                    monsterPuzzleCompletionIndex = i;
                }
            }
        }

        //Either Attack or Drag.
        if (agent.remainingDistance <= 5 && agent.remainingDistance > 2 && currentWaypoint == 999)
        {
            animIndex = 1;
            if (opticsLabScript != null && monsterPuzzleCompletionIndex != -1 && networkScript.PuzzleStates[monsterPuzzleCompletionIndex].state == true)
            {
                animIndex = 2;
                networkScript.updatePuzzleState("explosionTime", true, "MonsterStandin");
            }
            //animationScript.animator.SetInteger("animation", 1);
        }

        // If the monster is attacking, or the players have just won...
        else if (agent.remainingDistance <= 2 && currentWaypoint == 999 && currentWaypoint != 888)
        {

            // If loss needs to happen
            if (networkScript.PuzzleStates[monsterPuzzleCompletionIndex].state == false)
            {
                Debug.LogWarning("attempting to alter playerLoss in puzzleStates...");
                networkScript.updatePuzzleState("playerLoss", true, "MonsterStandin");
                pLoss = true;
            }

            // If win needs to happen
            else
            {
                Debug.LogWarning("attempting to alter puzzleCompletion in puzzleStates...");
                networkScript.updatePuzzleState("puzzleCompletion", true, networkScript.PuzzleStates[0].itemName);
            }
        }
        // If the monster is interacting with an item...
        else if (agent.remainingDistance <= 2 && currentWaypoint == 888)
        {
            if (!disengageActivated)
            {
                animIndex = 3;
                Invoke("disengageInteraction", 1.5f);
                disengageActivated = true;
            }

            //animationScript = GameObject.FindObjectOfType(typeof(spt_monsterAnimations)) as spt_monsterAnimations;
            //interactionScript = GameObject.FindObjectOfType(typeof(spt_monsterInteraction)) as spt_monsterInteraction;
            //animationScript.disengageInteraction();
            //interactionScript.interactWithObject(interactionScript.getInteractionName(), interactionScript.getInteractionItemName());
        }

        else if (agent.remainingDistance <= 2 && currentWaypoint != 999 && currentWaypoint != 888 && currentWaypoint != 777)
            chooseDestination();
    }
    // Update is called once per frame
    void Update()
    {
        if (network == null)
        {
            network = GameObject.FindGameObjectWithTag("Player").GetComponent<spt_NetworkPuzzleLogic>();
            if (network != null)
            {
                indecies = new int[network.PuzzleStates.Count];
                weights = new double[network.PuzzleStates.Count];
                //network.Cmd_UpdatePuzzleLogic("extCordPlugged", true, "mdl_extCord");
            }
        }
        else
        {
            //network = GameObject.FindObjectOfType(typeof(spt_NetworkPuzzleLogic)) as spt_NetworkPuzzleLogic;
            //Debug.Log(network.loaded);
            if (loadedTheNetwork == false)
            {
                // Iterates through the puzzle logic communicator, gets all necessary data for item interaction (populates the above arrays)
                int j = 0;
                for (int i = 0; i < network.PuzzleStates.Count; i++)
                {
                    if (network.PuzzleStates[i].isMonsterInteractable)
                    {

                        indecies[j] = i;
                        weights[j] = 0.5;
                        j++;
                    }
                }

                InvokeRepeating("updateTime", 1, 1);
                loadedTheNetwork = true;
            }

            else if (loadedTheNetwork == true)
            {

                // If the monster is not in a downtime period...
                if ((currentTime - lastInteractionTime) > interactionDowntime)
                {

                    // Cycle through the possible interactable objects.
                    for (int i = 0; i < indecies.Length; i++)
                    {

                        // If the monster is within interaction range...
                        if (Vector3.Distance(new Vector3(GameObject.Find(network.PuzzleStates[indecies[i]].itemName).transform.position.x, 1, GameObject.Find(network.PuzzleStates[indecies[i]].itemName).transform.position.z), gameObject.transform.position) < 2 && checkIfInteractableYet(network.PuzzleStates[indecies[i]].itemName))
                        {

                            // Perform interaction some of the time, dependent on a random number.
                            float decision = Random.Range(0, 1);
                            if (decision < weights[i])
                            {
                                animationScript = GameObject.FindObjectOfType(typeof(spt_monsterAnimations)) as spt_monsterAnimations;
                                interactionName = network.PuzzleStates[indecies[i]].name;
                                interactionItemName = network.PuzzleStates[indecies[i]].itemName;
                                Debug.LogWarning("Interacting with: " + network.PuzzleStates[indecies[i]].itemName);
                                animationScript.interactWithObject(interactionItemName);
                                //interactWithObject(network.PuzzleStates[indecies[i]].name, network.PuzzleStates[indecies[i]].itemName);
                            }
                        }
                    }
                }
            }
        }
    }