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

[RequireComponent(typeof(Collider))]
public class Bullet : MonoBehaviour {
	public Ship owner;
	
	public Transform hitEffect;

	public int damage;
	
	public float velocity;
	public float lifetime;

    public Vector3 baseVelocity;
    public bool applyBaseVelocity;
	
	private Vector3 lastPos;
	
	void FixedUpdate()
	{
        lastPos = transform.position;

		var frameSpeed = transform.forward * (velocity * Time.deltaTime);

        if (applyBaseVelocity)
        {
            frameSpeed += baseVelocity * Time.deltaTime;
        }

        var frameDistance = frameSpeed.magnitude;
        var frameDirection = frameSpeed / frameDistance;

		int raycastMask = ~LayerMask.GetMask("Bullets and Effects", "Ignore Raycast");
        
        Vector3 nextPos;
        RaycastHit hit;
        bool dead;

		if (Physics.Raycast(lastPos, 
            frameDirection, 
            out hit, 
            frameDistance, 
            raycastMask, 
            QueryTriggerInteraction.Ignore))
		{
			nextPos = hit.point;
			var hitObj = hit.collider.gameObject;

            dead = Hit(hitObj, nextPos);
		}
        else
        {
            nextPos = lastPos + frameSpeed;
            dead = false;
        }

		if (!dead)
		{
            transform.position = nextPos;

			lifetime -= Time.deltaTime;
			if (lifetime < 0)
			{
				Destroy(gameObject);
			}
		}
	}

	void OnTriggerEnter(Collider triggered)
	{
		Hit(triggered.gameObject, transform.position);
	}

	private bool Hit(GameObject hitObject, Vector3 hitPos)
	{
		if (hitObject != owner && hitObject != gameObject) //bullets are never triggered by the person shooting them
		{
			//Debug.Log(string.Format("bullet with owner {0} hit non-owner object {1}", owner, hitObject));
            
            var hitDamage = new HitDamage(hitPos, damage, owner);
			hitObject.gameObject.SendMessage("OnTakeDamage", hitDamage, SendMessageOptions.DontRequireReceiver);

            //if a handler changed it
            hitPos = hitDamage.Location;

            if (hitEffect)
            {
                var hitRotation = Quaternion.LookRotation(transform.forward);
                Instantiate(hitEffect, hitPos, hitRotation);
            }

			Destroy(gameObject);
			return true;
		}

		return false;
	}
}<commit_msg>Added mask check to Bullet's trigger collision event<commit_after>﻿using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Collider))]
public class Bullet : MonoBehaviour {
	public Ship owner;
	
	public Transform hitEffect;

	public int damage;
	
	public float velocity;
	public float lifetime;

    public Vector3 baseVelocity;
    public bool applyBaseVelocity;
	
	private Vector3 lastPos;

    private static readonly int RayMask = ~LayerMask.GetMask("Bullets and Effects", "Ignore Raycast");
    
    void FixedUpdate()
	{
        lastPos = transform.position;

		var frameSpeed = transform.forward * (velocity * Time.deltaTime);

        if (applyBaseVelocity)
        {
            frameSpeed += baseVelocity * Time.deltaTime;
        }

        var frameDistance = frameSpeed.magnitude;
        var frameDirection = frameSpeed / frameDistance;
                
        Vector3 nextPos;
        RaycastHit hit;
        bool dead;
        
		if (Physics.Raycast(lastPos, 
            frameDirection, 
            out hit, 
            frameDistance,
            RayMask, 
            QueryTriggerInteraction.Ignore))
		{
			nextPos = hit.point;
			var hitObj = hit.collider.gameObject;

            dead = Hit(hitObj, nextPos);
		}
        else
        {
            nextPos = lastPos + frameSpeed;
            dead = false;
        }

		if (!dead)
		{
            transform.position = nextPos;

			lifetime -= Time.deltaTime;
			if (lifetime < 0)
			{
				Destroy(gameObject);
			}
		}
	}

	void OnTriggerEnter(Collider triggered)
	{
        var triggeredMask = 1 << triggered.gameObject.layer;
        if ((RayMask & triggeredMask) == triggeredMask)
        {
            Hit(triggered.gameObject, transform.position);
        }
	}

	private bool Hit(GameObject hitObject, Vector3 hitPos)
	{
		if (hitObject != owner && hitObject != gameObject) //bullets are never triggered by the person shooting them
		{
			//Debug.Log(string.Format("bullet with owner {0} hit non-owner object {1}", owner, hitObject));
            
            var hitDamage = new HitDamage(hitPos, damage, owner);
			hitObject.gameObject.SendMessage("OnTakeDamage", hitDamage, SendMessageOptions.DontRequireReceiver);

            //if a handler changed it
            hitPos = hitDamage.Location;

            if (hitEffect)
            {
                var hitRotation = Quaternion.LookRotation(transform.forward);
                Instantiate(hitEffect, hitPos, hitRotation);
            }

			Destroy(gameObject);
			return true;
		}

		return false;
	}
}
{'fraction_non_alphanumeric': 0.08772289403566305, 'fraction_numerical': 0.003074400491904079, 'mean_word_length': 2.2795698924731185, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2608673', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1337, 'n_words': 344}