<commit_before>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boid : MonoBehaviour {
    List<SteeringBehaviour> behaviours = new List<SteeringBehaviour>();
    
    public Vector3 force = Vector3.zero;
    public Vector3 acceleration = Vector3.zero;
    public Vector3 velocity = Vector3.zero;
    public float mass = 1;
    public float maxSpeed = 5.0f;

    // Seek fields
    public bool seekEnabled = false;
    public Vector3 seekTargetPos;
    public GameObject seekTarget;

    public bool arriveEnabled = false;
    public Vector3 arriveTargetPos;
    public GameObject arriveTarget;
    public float slowingDistance = 5;

    // Offset Pursue
    public bool offsetPursueEnabled;
    public Boid leader;
    private Vector3 offset;

    public bool followPathEnabled = false;
    public Path path;
    
    // Use this for initializationG
    void Start () {

        SteeringBehaviour[] behaviours = GetComponents<SteeringBehaviour>();

        foreach (SteeringBehaviour b in behaviours)
        {
            this.behaviours.Add(b);
        }


        if (offsetPursueEnabled)
        {
            offset = transform.position- leader.transform.position;
            offset = Quaternion.Inverse(leader.transform.rotation) * offset;
        }
	}

    Vector3 Pursue(Boid target)
    {
        float dist = Vector3.Distance(target.transform.position, transform.position);
        float time = dist / maxSpeed;

        Vector3 targetPos = target.transform.position
            + (time * target.velocity);

        return Seek(targetPos);
    }

    Vector3 FollowPath()
    {
        Vector3 nextWaypoint = path.NextWaypoint();
        if (Vector3.Distance(transform.position, nextWaypoint) < 3)
        {
            path.AdvanceToNext();
        }

        if (!path.looped && path.IsLast())
        {
            return Arrive(nextWaypoint);
        }
        else
        {
            return Seek(nextWaypoint);
        }
    }

    Vector3 OffsetPursue(Boid target, Vector3 offset)
    {
        Vector3 worldtarget = target.transform.TransformPoint(offset);
        float dist = Vector3.Distance(worldtarget
            , transform.position);
        float time = dist / maxSpeed;

        Vector3 targetPos = worldtarget + (target.velocity * time);
        return Arrive(targetPos);
    }

    Vector3 Seek(Vector3 target)
    {
        Vector3 desired = target - transform.position;
        desired.Normalize();
        desired *= maxSpeed;
        return desired- velocity;
    }

    Vector3 Arrive(Vector3 target)
    {
        Vector3 toTarget = target - transform.position;
        float distance = toTarget.magnitude;
        float ramped = maxSpeed * (distance / slowingDistance);
        float clamped = Mathf.Min(ramped, maxSpeed);
        Vector3 desiredVelocity = clamped * (toTarget / distance);
        return desiredVelocity - velocity;
    }


    Vector3 Calculate()
    {
        force = Vector3.zero;

        foreach (SteeringBehaviour b in behaviours)
        {
            if (b.isActiveAndEnabled)
            {
                force += b.Calculate();
            }
        }

        /*
        if (seekEnabled)
        {
            if (seekTarget != null)
            {
                seekTargetPos = seekTarget.transform.position;
            }
            force += Seek(seekTargetPos);
        }
        if (arriveEnabled)
        {
            if (arriveTarget != null)
            {
                arriveTargetPos = arriveTarget.transform.position;
            }
            force += Arrive(arriveTargetPos);
        }
        if (offsetPursueEnabled)
        {
            force += OffsetPursue(leader, offset);
        }

        if (followPathEnabled)
        {
            force += FollowPath();
        }
        */

        return force;
    }
	
	// Update is called once per frame
	void Update () {
        force = Calculate();

        acceleration = force / mass;
        velocity += acceleration * Time.deltaTime;

        velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
        if (velocity.magnitude > float.Epsilon)
        {
            transform.forward = velocity;
        }

        transform.position += velocity * Time.deltaTime;
        

	}
}<commit_msg>Updated integrator to use damping<commit_after>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boid : MonoBehaviour {
    List<SteeringBehaviour> behaviours = new List<SteeringBehaviour>();
    
    public Vector3 force = Vector3.zero;
    public Vector3 acceleration = Vector3.zero;
    public Vector3 velocity = Vector3.zero;
    public float mass = 1;
    public float maxSpeed = 5.0f;

    // Seek fields
    public bool seekEnabled = false;
    public Vector3 seekTargetPos;
    public GameObject seekTarget;

    public bool arriveEnabled = false;
    public Vector3 arriveTargetPos;
    public GameObject arriveTarget;
    public float slowingDistance = 5;

    // Offset Pursue
    public bool offsetPursueEnabled;
    public Boid leader;
    private Vector3 offset;

    public bool followPathEnabled = false;
    public Path path;
    
    // Use this for initializationG
    void Start () {

        SteeringBehaviour[] behaviours = GetComponents<SteeringBehaviour>();

        foreach (SteeringBehaviour b in behaviours)
        {
            this.behaviours.Add(b);
        }


        if (offsetPursueEnabled)
        {
            offset = transform.position- leader.transform.position;
            offset = Quaternion.Inverse(leader.transform.rotation) * offset;
        }
	}

    Vector3 Pursue(Boid target)
    {
        float dist = Vector3.Distance(target.transform.position, transform.position);
        float time = dist / maxSpeed;

        Vector3 targetPos = target.transform.position
            + (time * target.velocity);

        return Seek(targetPos);
    }

    Vector3 FollowPath()
    {
        Vector3 nextWaypoint = path.NextWaypoint();
        if (Vector3.Distance(transform.position, nextWaypoint) < 3)
        {
            path.AdvanceToNext();
        }

        if (!path.looped && path.IsLast())
        {
            return Arrive(nextWaypoint);
        }
        else
        {
            return Seek(nextWaypoint);
        }
    }

    Vector3 OffsetPursue(Boid target, Vector3 offset)
    {
        Vector3 worldtarget = target.transform.TransformPoint(offset);
        float dist = Vector3.Distance(worldtarget
            , transform.position);
        float time = dist / maxSpeed;

        Vector3 targetPos = worldtarget + (target.velocity * time);
        return Arrive(targetPos);
    }

    Vector3 Seek(Vector3 target)
    {
        Vector3 desired = target - transform.position;
        desired.Normalize();
        desired *= maxSpeed;
        return desired- velocity;
    }

    Vector3 Arrive(Vector3 target)
    {
        Vector3 toTarget = target - transform.position;
        float distance = toTarget.magnitude;
        float ramped = maxSpeed * (distance / slowingDistance);
        float clamped = Mathf.Min(ramped, maxSpeed);
        Vector3 desiredVelocity = clamped * (toTarget / distance);
        return desiredVelocity - velocity;
    }


    Vector3 Calculate()
    {
        force = Vector3.zero;

        foreach (SteeringBehaviour b in behaviours)
        {
            if (b.isActiveAndEnabled)
            {
                force += b.Calculate();
            }
        }

        /*
        if (seekEnabled)
        {
            if (seekTarget != null)
            {
                seekTargetPos = seekTarget.transform.position;
            }
            force += Seek(seekTargetPos);
        }
        if (arriveEnabled)
        {
            if (arriveTarget != null)
            {
                arriveTargetPos = arriveTarget.transform.position;
            }
            force += Arrive(arriveTargetPos);
        }
        if (offsetPursueEnabled)
        {
            force += OffsetPursue(leader, offset);
        }

        if (followPathEnabled)
        {
            force += FollowPath();
        }
        */

        return force;
    }
	
	// Update is called once per frame
	void Update () {
        force = Calculate();

        Vector3 newAcceleration = force / mass;

        float smoothRate = Mathf.Clamp(9.0f * Time.deltaTime, 0.15f, 0.4f) / 2.0f;
        acceleration = Vector3.Lerp(acceleration, newAcceleration, smoothRate);
        
        velocity += acceleration * Time.deltaTime;

        velocity = Vector3.ClampMagnitude(velocity, maxSpeed);

        Vector3 globalUp = new Vector3(0, 0.2f, 0);
        Vector3 accelUp = acceleration * 0.05f;
        Vector3 bankUp = accelUp + globalUp;
        smoothRate = Time.deltaTime * 3.0f;
        Vector3 tempUp = transform.up;
        tempUp = Vector3.Lerp(tempUp, bankUp, smoothRate);

        if (velocity.magnitude  > 0.0001f)
        {
            transform.forward = velocity;
            transform.forward.Normalize();
            transform.LookAt(transform.position + transform.forward, tempUp);
            velocity *= 0.99f;
        }
        transform.position += velocity * Time.deltaTime;        
	}
}
{'fraction_non_alphanumeric': 0.08750951190346777, 'fraction_numerical': 0.011305576693118817, 'mean_word_length': 1.9022082018927444, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7375820', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2437, 'n_words': 584}