// LateUpdate is called after Update each frame
    void LateUpdate()
    {
        if (target == null)
        {
            checkWASDCamera();
        }
        else
        {
            smoothCamera();
        }

        checkMouseInput();

        // If space bar is pressed detach from the current focucs
        if (target != null && Input.GetKey(KeyCode.Space))
        {
            if (target.gameObject.tag == "PreyAgent")
            {
                PreyAgent targetScript = target.gameObject.GetComponent <PreyAgent>();
                targetScript.deselect();
            }

            if (target.gameObject.tag == "PredatorAgent")
            {
                PredatorAgent targetScript = target.gameObject.GetComponent <PredatorAgent>();
                targetScript.deselect();
            }
            target = null;
        }

        checkScreenshotInput();
    }
    // Helper function to initiate an attack.
    //
    // Attacks can happen once every "attackTime" seconds, where "attackTime" is in seconds.
    private void initiateAttack(GameObject closestPrey)
    {
        PreyAgent getPreyScript = closestPrey.GetComponent <PreyAgent>();

        //Debug.Log ("we're attempting to attack");
        // if the current time is greater than (initiated + attackTime), we can attack
        if (Time.time >= (attackTimeStampInitiated + attackTime))
        {
            //Debug.Log ("we're initiating an attack");
            predatorAnimState        = 1;
            attackTimeStampInitiated = Time.time;
            getPreyScript.bitePrey();
        }
    }
    public void changeObjectFocus(Transform agentPos)
    {
        if (target != null && target.gameObject.tag.Equals("PreyAgent"))
        {
            PreyAgent targetScript = target.gameObject.GetComponent <PreyAgent>();
            targetScript.deselect();
        }

        if (target != null && target.gameObject.tag.Equals("PredatorAgent"))
        {
            PredatorAgent targetScript = target.gameObject.GetComponent <PredatorAgent>();
            targetScript.deselect();
        }

        target = agentPos;
    }
Esempio n. 4
0
    /*
     * Adds a new row to the .CSV file that contains the data
     * from the current run.
     *
     * @wasSuccess     Indicates if the predators were successful.
     * @theCaughtPrey  The specific prey entity that was caught if there was one.
     * @theTime        The amount of time the run lasted for.
     */
    private void updateReport(bool wasSuccess, PreyAgent theCaughtPrey, double theTime)
    {
        // Run ID
        myDataReport.Append(runCount);
        myDataReport.Append(Config.DELIMITER);

        // Success / Failure
        myDataReport.Append(wasSuccess);
        myDataReport.Append(Config.DELIMITER);

        // Time to Completion
        myDataReport.Append(theTime);
        myDataReport.Append(Config.DELIMITER);

        // Class of prey caught
        if (wasSuccess)
        {
            myDataReport.Append(getWeakenedState(theCaughtPrey));
        }
        else
        {
            myDataReport.Append("");
        }
        myDataReport.Append(Config.DELIMITER);

        // Endurance of prey caught
        if (wasSuccess)
        {
            myDataReport.Append(Math.Round(theCaughtPrey.endurance, 2));
        }
        else
        {
            myDataReport.Append("");
        }
        myDataReport.Append(Config.DELIMITER);

        /*
         * if (myPreyHitWall)
         *  myDataReport.Append(myPreyHitWall);
         */
        myDataReport.Append(myPreyDistanceZ);

        if (runCount < Config.NUMBER_OF_RUNS)
        {
            myDataReport.Append(Environment.NewLine);
        }
    }
Esempio n. 5
0
    /*
     * Retrieves the weakened state of the prey.
     *
     * @thePrey    The prey being checked.
     *
     * @return     The string name of the prey's state.
     */
    private static string getWeakenedState(PreyAgent thePrey)
    {
        string theState = "";

        int length = thePrey.gameObject.GetComponent <PreyAgent>().isWeakened.Length;

        for (int j = 0; j < length; j++)
        {
            if (thePrey.gameObject.GetComponent <PreyAgent>().isWeakened[j])
            {
                switch (j)
                {
                case Config.ENDURANCE_INDEX:
                    theState = "Endurance";
                    break;

                case Config.MAXSPEED_INDEX:
                    theState = "MaxSpeed";
                    break;

                case Config.BOTH_INDEX:
                    theState = "Both";
                    break;

                case Config.HEALTHY_INDEX:
                    theState = "Healthy";
                    break;

                default:
                    theState = "";
                    break;
                }
            }
        }

        return(theState);
    }
Esempio n. 6
0
    /*
     * Destroys entities in current run then initializes new entities.
     *
     * @wasSuccess      Indicates if the predators were successful.
     * @theCaughtPrey   The specific prey caught if there was one.
     */
    private void reloadScene(bool wasSuccess, PreyAgent theCaughtPrey)
    {
        watch.Stop();
        double seconds = watch.Elapsed.TotalSeconds;

        seconds = seconds * Config.SIMULATION_SPEED_MULTIPLIER;
        seconds = Math.Round(seconds, 1);

        runCount++;
        // Transcribe data here.
        updateReport(wasSuccess, theCaughtPrey, seconds);
        appendToFile(myDataReport.ToString());
        myDataReport.Length = 0;

        if (!Config.USE_AUTOMATION)
        {
            if (runCount % (Config.NUMBER_OF_RUNS / 10) == 0)
            {
                UnityEngine.Debug.Log("Runs: " + ((double)runCount / Config.NUMBER_OF_RUNS * 100) + "%");
            }
        }


        // Only reload the scene if there are still runs to do, otherwise freeze simulation.
        if (runCount < Config.NUMBER_OF_RUNS)
        {
            //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            for (int i = 0; i < predators.Length; i++)
            {
                Destroy(predators[i]);
            }
            for (int i = 0; i < preys.Length; i++)
            {
                Destroy(preys[i]);
            }
            initEntities();
        }
        else
        {
            for (int i = 0; i < predators.Length; i++)
            {
                Destroy(predators[i]);
            }
            for (int i = 0; i < preys.Length; i++)
            {
                Destroy(preys[i]);
            }
            // Out of runs, freeze simulation and write report to file.
            Time.timeScale = 1.0f;
            //Time.fixedDeltaTime = 0.0f;

            /*
             * string dataReport = generateReport(mySuccesses, myFailures,
             *                                 mySuccessTargetCounts[Config.ENDURANCE_INDEX],
             *                                 mySuccessTargetCounts[Config.MAXSPEED_INDEX],
             *                                 mySuccessTargetCounts[Config.BOTH_INDEX],
             *                                 mySuccessTargetCounts[Config.HEALTHY_INDEX]);
             */
            firstRun = false;
            runAutomation();
        }
    }
Esempio n. 7
0
    // Update is called once per frame
    void Update()
    {
        if (preys != null && predators != null && runCount < Config.NUMBER_OF_RUNS)
        {
            bool      isOver     = true;
            bool      wasSuccess = true;
            PreyAgent caughtPrey = null;


            for (int i = 0; i < predators.Length; i++)
            {
                if (predators[i].gameObject.GetComponent <PredatorAgent>().areTargets)
                {
                    // A predator can still see a prey, simulation isn't over yet.
                    isOver = false;
                    break;
                }
            }

            // Only check prey if simulation wasn't determined to be over from predator checks.
            if (!isOver)
            {
                for (int i = 0; i < preys.Length; i++)
                {
                    /*
                     * if (!myPreyHitWall && preys[i].gameObject.GetComponent<PreyAgent>().transform.position.z >= 8000-5)
                     * {
                     *  myPreyHitWall = true;
                     * }
                     */
                    if (preys[i].gameObject.GetComponent <PreyAgent>().transform.position.z > myPreyDistanceZ)
                    {
                        myPreyDistanceZ = (int)preys[i].gameObject.GetComponent <PreyAgent>().transform.position.z;
                    }

                    if (preys[i].gameObject.GetComponent <PreyAgent>().health <= 0)
                    {
                        // A prey is dead, simulation is over.
                        caughtPrey = preys[i].gameObject.GetComponent <PreyAgent>();

                        // check what weakened state the prey was in if in any.
                        int length = preys[i].gameObject.GetComponent <PreyAgent>().isWeakened.Length;
                        for (int j = 0; j < length; j++)
                        {
                            if (preys[i].gameObject.GetComponent <PreyAgent>().isWeakened[j])
                            {
                                // increment the count of the weakened state the prey was in.
                                mySuccessTargetCounts[j]++;
                            }
                        }

                        isOver     = true;
                        wasSuccess = true;
                        mySuccesses++;
                        break;
                    }
                }
            }
            else
            {
                // Predators failed hunt, can't see anymore prey.
                wasSuccess = false;
                myFailures++;
            }

            // If simulation is over, reload the scene.
            if (isOver)
            {
                reloadScene(wasSuccess, caughtPrey);
            }
        }

        // for built versions of the simulation escape will exit the application
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    }