private IEnumerator RunSlide()
    {
        m_states.m_movementControllState = MovementControllState.MovementDisabled;
        m_isSliding = true;


        Vector3 slideDir = transform.forward;

        m_slideTimer = 0;

        bool hasBeenOnSlope = false;

        while (m_slideTimer < m_slideTime)
        {
            m_slideTimer += Time.fixedDeltaTime;

            float progress = m_slideCurve.Evaluate(m_slideTimer / m_slideTime);

            float currentSlideSpeed = Mathf.Lerp(m_slideSpeed, m_baseMovementProperties.m_crouchMovementSpeed, progress);

            SlopeInfo slopeInfo = OnSlope();

            if (slopeInfo.m_onSlope)
            {
                hasBeenOnSlope = true;

                m_slideTimer = 0;

                float normalX = slopeInfo.m_slopeNormal.x > 0 ? slopeInfo.m_slopeNormal.x : slopeInfo.m_slopeNormal.x * -1;
                float normalZ = slopeInfo.m_slopeNormal.z > 0 ? slopeInfo.m_slopeNormal.z : slopeInfo.m_slopeNormal.z * -1;

                float slopeX = Mathf.Lerp(m_slideAngleBoostMin, m_slideAngleBoostMax, normalX / m_slopeTolerence) * Mathf.Sign(slopeInfo.m_slopeNormal.x);
                float slopeZ = Mathf.Lerp(m_slideAngleBoostMin, m_slideAngleBoostMax, normalZ / m_slopeTolerence) * Mathf.Sign(slopeInfo.m_slopeNormal.z);

                slideDir = new Vector3(slopeX, 0, slopeZ);
                Vector3 horizontalMovement = Vector3.SmoothDamp(m_velocity, slideDir, ref m_velocitySmoothing, m_slopeSlideAccelerationTime);

                m_slopeTransform.rotation = Quaternion.LookRotation(slideDir);

                Vector3 targetX   = m_slopeTransform.right * m_movementInput.x * m_slideSideShiftMaxSpeed;
                Vector3 shiftVelX = Vector3.SmoothDamp(m_slideSideShiftVelocity, targetX, ref m_slideSideShiftVelocitySmoothing, m_slideSideShiftAcceleration);

                m_velocity = new Vector3(horizontalMovement.x, m_velocity.y, horizontalMovement.z);

                m_velocity += shiftVelX;
            }
            else if (!hasBeenOnSlope)
            {
                Vector3 slideVelocity = slideDir * currentSlideSpeed;
                m_velocity = new Vector3(slideVelocity.x, m_velocity.y, slideVelocity.z);
            }

            yield return(new WaitForFixedUpdate());
        }

        m_isSliding = false;

        m_states.m_movementControllState = MovementControllState.MovementEnabled;
    }
        public double?GetPERatioFitness(SlopeInfo slopeInfo)
        {
            double?peRatioFitness = 0;
            var    peValues       = slopeInfo.NominalValues;

            if (peValues == null)
            {
                return(peRatioFitness);
            }

            if (peValues.Any(x => x.Value == null))
            {
                return(peRatioFitness);
            }
            if (peValues.Any(x => x.Value < 0))
            {
                return(peRatioFitness);
            }
            double?count = 0;

            foreach (var item in peValues)
            {
                if (item.Value <= 0.15)
                {
                    peRatioFitness = 3;
                }
                if (item.Value > 0.15 && item.Value <= 0.3)
                {
                    peRatioFitness = 1.5;
                }
                if (item.Value > 0.3 && item.Value <= 0.6)
                {
                    peRatioFitness = 0.7;
                }
                else if (item.Value >= 0.6)
                {
                    peRatioFitness = 0;
                }
                count += peRatioFitness;
            }
            peRatioFitness = count / (peValues.Count * 3);

            return(peRatioFitness);
        }
    private SlopeInfo OnSlope()
    {
        SlopeInfo slopeInfo = new SlopeInfo {
        };

        RaycastHit hit;

        Vector3 bottom = m_characterController.transform.position - new Vector3(0, m_characterController.height / 2, 0);

        if (Physics.Raycast(bottom, Vector3.down, out hit, 0.5f))
        {
            if (hit.normal != Vector3.up)
            {
                slopeInfo.m_onSlope     = true;
                slopeInfo.m_slopeAngle  = Vector3.Angle(Vector3.up, hit.normal);
                slopeInfo.m_slopeNormal = hit.normal;

                return(slopeInfo);
            }
        }

        return(slopeInfo);
    }
Exemple #4
0
    public void GenerateFlowMap()
    {
        flowMap      = new Texture2D(textureSize, textureSize);
        flowMap.name = "Generated Flow Map";
        Color[] flowMapColors = new Color[textureSize * textureSize];

        var forestController = ForestController.Instance;

        if (!forestController)
        {
            forestController = GameObject.FindObjectOfType <ForestController>();
        }
        mapSize          = forestController.mapSize;
        terrainLayerMask = forestController.terrainLayerMask;

        float sampleRadius = (mapSize.x + mapSize.z) / 2f / (float)textureSize;
//		Debug.Log("Sample Radius: " + sampleRadius);

        Vector3 centerOffset = new Vector3(-mapSize.x / 2f, 0f, -mapSize.z / 2f);

        int        totalSamples = 0;
        Vector3    samplePoint;
        RaycastHit hit;

        hitGrid = new SlopeInfo[textureSize, textureSize];
        for (int x = 0; x < textureSize; x++)
        {
            for (int y = 0; y < textureSize; y++)
            {
                samplePoint = new Vector3((float)x / (float)(textureSize - 1) * mapSize.x, mapSize.y, (float)y / (float)(textureSize - 1) * mapSize.z) + centerOffset;
                for (int i = 0; i <= samplesPerPixel; i++)
                {
                    Color debugRayColor = Color.blue;
                    hitGrid[x, y] = new SlopeInfo();
                    Vector3 offset = Vector3.zero;
                    if (i > 0)
                    {
                        float theta = (float)i / (float)samplesPerPixel * Mathf.PI * 2f;
                        offset        = new Vector3(Mathf.Cos(theta), 0f, Mathf.Sin(theta)) * sampleRadius;
                        debugRayColor = Color.red;
                    }
                    if (Physics.Raycast(samplePoint + offset, Vector3.down, out hit, mapSize.y * 1.5f, terrainLayerMask))
                    {
                        if (x % 64 == 0 && y % 64 == 0)
                        {
                            Debug.DrawLine(samplePoint + offset, hit.point, debugRayColor, 10f);
                        }

                        hitGrid[x, y].AddData(hit.point, hit.normal);
                        totalSamples++;
                    }
                }
                hitGrid[x, y].GetStats(x % 64 == 0 && y % 64 == 0);
            }
        }

        Debug.Log("Total Samples: " + totalSamples);

        if (smoothTexturePass.filterRadius > 0)
        {
            Color[,] smoothableGrid = new Color[textureSize, textureSize];
            for (int x = 0; x < textureSize; x++)
            {
                for (int y = 0; y < textureSize; y++)
                {
                    smoothableGrid[x, y] = GetFlowColorAtPoint(x, y);
                }
            }

            smoothableGrid = smoothTexturePass.SmoothColorGrid(smoothableGrid);

            for (int x = 0; x < textureSize; x++)
            {
                for (int y = 0; y < textureSize; y++)
                {
                    int pixelIndex = y * textureSize + x;
                    flowMapColors[pixelIndex] = smoothableGrid[x, y];
                }
            }
        }
        else
        {
            for (int x = 0; x < textureSize; x++)
            {
                for (int y = 0; y < textureSize; y++)
                {
                    int pixelIndex = y * textureSize + x;
                    flowMapColors[pixelIndex] = GetFlowColorAtPoint(x, y);
                }
            }
        }
        flowMap.SetPixels(flowMapColors);
        flowMap.Apply();
    }
    private void SlopePhysics()
    {
        SlopeInfo slopeInfo = OnSlope();

        if (slopeInfo.m_onSlope == true)
        {
            if (m_velocity.y > 0)
            {
                return;
            }

            if (m_hasJumped)
            {
                return;
            }

            RaycastHit hit;

            Vector3 bottom = m_characterController.transform.position - new Vector3(0, m_characterController.height / 2, 0);

            if (Physics.Raycast(bottom, Vector3.down, out hit))
            {
                if (slopeInfo.m_slopeAngle > m_characterController.slopeLimit)
                {
                    m_velocity.x += (1f - hit.normal.y) * hit.normal.x * (m_baseMovementProperties.m_slopeFriction);
                    m_velocity.z += (1f - hit.normal.y) * hit.normal.z * (m_baseMovementProperties.m_slopeFriction);
                }

                #region not suing

                /*
                 * if (m_isSliding)
                 * {
                 *      m_slideTimer = 0;
                 *
                 *      float anglePercent = slopeAngle / m_characterController.slopeLimit;
                 *
                 *      float normalX = hit.normal.x > 0 ? hit.normal.x : hit.normal.x * -1;
                 *      float normalZ = hit.normal.z > 0 ? hit.normal.z : hit.normal.z * -1;
                 *
                 *      float slopeX = Mathf.Lerp(m_slideAngleBoostMin, m_slideAngleBoostMax, normalX / m_slopeTolerence) * Mathf.Sign(hit.normal.x);
                 *      float slopeZ = Mathf.Lerp(m_slideAngleBoostMin, m_slideAngleBoostMax, normalZ / m_slopeTolerence) * Mathf.Sign(hit.normal.z);
                 *
                 *      Vector3 targetMovement = new Vector3(slopeX, 0, slopeZ);
                 *      Vector3 horizontalMovement = Vector3.SmoothDamp(m_velocity, targetMovement, ref m_velocitySmoothing, m_slopeSlideAccelerationTime);
                 *
                 *      m_slopeTransform.rotation = Quaternion.LookRotation(targetMovement);
                 *
                 *      Vector3 targetX = m_slopeTransform.right * m_movementInput.x * m_slideSideShiftMaxSpeed;
                 *      Vector3 shiftVelX = Vector3.SmoothDamp(m_slideSideShiftVelocity, targetX, ref m_slideSideShiftVelocitySmoothing, m_slideSideShiftAcceleration);
                 *
                 *      m_velocity = new Vector3(horizontalMovement.x, m_velocity.y, horizontalMovement.z);
                 *
                 *      m_velocity += shiftVelX;
                 * }
                 */
                #endregion

                m_characterController.Move(new Vector3(0, -(hit.distance), 0));
            }
        }
    }
        public double?GetRoicFitness(SlopeInfo slopeInfo)
        {
            if (slopeInfo.NominalValues == null)
            {
                return(0);
            }
            if (slopeInfo.GrowthTrendline == null)
            {
                return(null);
            }
            var slope = slopeInfo.GrowthTrendline.Slope;

            if (slope == null)
            {
                return(null);
            }
            var    deviation              = slopeInfo.GrowthDeviation;
            double?fitnessValueDeviation  = 0;
            double?fitnessSlopeValue      = 0;
            double?fitnessSlopeThreeYears = 0;

            #region All values bigger than 10%
            double?fitnessNominalValue = 0;
            double?count  = 0;
            var    values = slopeInfo.NominalValues;
            foreach (var item in values)
            {
                if (item.Value >= 0.1)
                {
                    fitnessNominalValue = 3;
                }

                if (item.Value >= 0 && item.Value < 0.1)
                {
                    fitnessNominalValue = 1.5;
                }

                else if (item.Value < 0)
                {
                    fitnessNominalValue = 0;
                }

                count += fitnessNominalValue;
            }

            fitnessNominalValue = count / values.Count;
            #endregion

            #region Small deviation
            if (deviation <= 15)
            {
                fitnessValueDeviation = 3;
            }
            if (deviation > 15 && deviation < 30)
            {
                fitnessValueDeviation = 1.5;
            }
            else if (deviation >= 30)
            {
                fitnessValueDeviation = 0;
            }
            #endregion

            #region Slope
            if (slope >= 0.1)
            {
                fitnessSlopeValue = 3;
            }
            if (slope >= 0 && slope < 0.1)
            {
                fitnessSlopeValue = 1.5;
            }
            else if (slope < 0)
            {
                fitnessSlopeValue = 0;
            }
            #endregion

            #region Slope last 3 years
            var lastThreeYearsGrowthSlope = slopeInfo.LastThreeYearsTrendLine.Slope;
            if (lastThreeYearsGrowthSlope >= 0.1)
            {
                fitnessSlopeThreeYears = 3;
            }
            if (lastThreeYearsGrowthSlope >= 0 && lastThreeYearsGrowthSlope < 0.1)
            {
                fitnessSlopeThreeYears = 1.5;
            }
            else if (lastThreeYearsGrowthSlope < 0)
            {
                fitnessSlopeThreeYears = 0;
            }
            #endregion

            var fitness = (fitnessSlopeThreeYears + fitnessSlopeValue + fitnessValueDeviation + fitnessNominalValue) / 12;
            return(fitness);
        }
        public double?GetGrowthFitness(SlopeInfo slopeInfo)
        {
            if (slopeInfo.Growth == null)
            {
                return(0);
            }
            var    slope                  = slopeInfo.GrowthTrendline.Slope;
            double?fitnessSlopeValue      = 0;
            double?fitnessSlopeThreeYears = 0;

            #region All values bigger than 10%
            double?fitnessBiggerThanTen = 0;
            double?count        = 0;
            var    growthValues = slopeInfo.Growth;
            foreach (var item in growthValues)
            {
                if (item.Value >= 0.1)
                {
                    fitnessBiggerThanTen = 3;
                }

                if (item.Value >= 0 && item.Value < 0.1)
                {
                    fitnessBiggerThanTen = 1.5;
                }

                else if (item.Value < 0)
                {
                    fitnessBiggerThanTen = 0;
                }

                count += fitnessBiggerThanTen;
            }

            fitnessBiggerThanTen = count / growthValues.Count;
            #endregion

            #region Slope
            if (slope >= 0.1)
            {
                fitnessSlopeValue = 3;
            }
            if (slope >= 0 && slope < 0.1)
            {
                fitnessSlopeValue = 1.5;
            }
            else if (slope < 0)
            {
                fitnessSlopeValue = 0;
            }
            #endregion

            #region Slope last 3 years
            var lastThreeYearsGrowthSlope = slopeInfo.LastThreeYearsTrendLine.Slope;
            if (lastThreeYearsGrowthSlope >= 0.1)
            {
                fitnessSlopeThreeYears = 3;
            }
            if (lastThreeYearsGrowthSlope >= 0 && lastThreeYearsGrowthSlope < 0.1)
            {
                fitnessSlopeThreeYears = 1.5;
            }
            else if (lastThreeYearsGrowthSlope < 0)
            {
                fitnessSlopeThreeYears = 0;
            }
            #endregion

            var fitness = (fitnessSlopeThreeYears + fitnessSlopeValue + fitnessBiggerThanTen) / 9;
            return(fitness);
        }
	public void GenerateFlowMap ()
	{
		flowMap = new Texture2D (textureSize, textureSize);
		flowMap.name = "Generated Flow Map";
		Color[] flowMapColors = new Color[textureSize * textureSize];

		var forestController = ForestController.Instance;
		if (!forestController)
		{
			forestController = GameObject.FindObjectOfType<ForestController>();
		}
		mapSize = forestController.mapSize;
		terrainLayerMask = forestController.terrainLayerMask;

		float sampleRadius = (mapSize.x + mapSize.z) / 2f / (float)textureSize;
//		Debug.Log("Sample Radius: " + sampleRadius);

		Vector3 centerOffset = new Vector3 (-mapSize.x / 2f, 0f, -mapSize.z / 2f);

		int totalSamples = 0;
		Vector3 samplePoint;
		RaycastHit hit;
		hitGrid = new SlopeInfo[textureSize,textureSize];
		for (int x = 0; x < textureSize; x++)
		{
			for (int y = 0; y < textureSize; y++)
			{
				samplePoint = new Vector3 ((float)x / (float)(textureSize - 1) * mapSize.x, mapSize.y, (float)y / (float)(textureSize - 1) * mapSize.z) + centerOffset;
				for (int i = 0; i <= samplesPerPixel; i++)
				{
					Color debugRayColor = Color.blue;
					hitGrid[x, y] = new SlopeInfo ();
					Vector3 offset = Vector3.zero;
					if (i > 0)
					{
						float theta = (float)i / (float)samplesPerPixel * Mathf.PI * 2f;
						offset = new Vector3 (Mathf.Cos(theta), 0f, Mathf.Sin(theta)) * sampleRadius;
						debugRayColor = Color.red;
					}
					if (Physics.Raycast(samplePoint + offset, Vector3.down, out hit, mapSize.y * 1.5f, terrainLayerMask))
					{
						if (x % 64 == 0 && y % 64 == 0)
						{
							Debug.DrawLine(samplePoint + offset, hit.point, debugRayColor, 10f);
						}

						hitGrid[x, y].AddData(hit.point, hit.normal);
						totalSamples++;
					}
				}
				hitGrid[x, y].GetStats(x % 64 == 0 && y % 64 == 0);
			}
		}

		Debug.Log("Total Samples: " + totalSamples);

		if (smoothTexturePass.filterRadius > 0)
		{
			Color[,] smoothableGrid = new Color[textureSize, textureSize];
			for (int x = 0; x < textureSize; x++)
			{
				for (int y = 0; y < textureSize; y++)
				{
					smoothableGrid[x, y] = GetFlowColorAtPoint(x, y);
				}
			}

			smoothableGrid = smoothTexturePass.SmoothColorGrid(smoothableGrid);

			for (int x = 0; x < textureSize; x++)
			{
				for (int y = 0; y < textureSize; y++)
				{
					int pixelIndex = y * textureSize + x;
					flowMapColors[pixelIndex] = smoothableGrid[x, y];
				}
			}
		}
		else
		{
			for (int x = 0; x < textureSize; x++)
			{
				for (int y = 0; y < textureSize; y++)
				{
					int pixelIndex = y * textureSize + x;
					flowMapColors[pixelIndex] = GetFlowColorAtPoint(x, y);
				}
			}
		}
		flowMap.SetPixels(flowMapColors);
		flowMap.Apply();
	}