//OnCreated

var hiding = true;			// Whether the scrub is hiding in the ground or not
var shotDelay = 60;			// Number of ticks between each shot
var shotTime = shotDelay;
var yawMax = 20;			// Max yaw difference for hiding/emerging animation
var yawSpeed = 5;			// Yaw speed for hiding/emerging animation
var p = entity.getPosition();
var minRange = 6;			// Distance player can be before the Deku Scrub hides
var maxRange = 18;			// Distance player has to be for the Deku Scrub to appear
var dekuNuts = Array();
var scrubHeight = 1;		// Height of Deku Scrub when revealed. Only affects collision box
var nonMobs = Array("Item", "Snowball", "Particle", "Arrow");	// Things the Deku Nut shouldn't hit

// Create the model
var m = Model();
m.addBoxExpanded("Body", -4, -4, -4, 8, 8, 8, 0, 0, 2.0);
m.addBoxExpanded("Body2", -3, -7, -3, 6, 3, 6, 38, 16, 2.0);
m.addBoxExpanded("Mouth", -2.5, -2.5, 5, 5, 5, 6, 0, 16, 0.0);
m.addBoxExpanded("Leaves", -4, -5, -4, 8, 8, 8, 32, 0, 4.0);
m.addBoxExpanded("Leaves2", -2, 6, -2, 4, 5, 4, 22, 16, 0.0);
m.y = 0.7;

// Assign texture
m.texture = "/models/mob/dekuscrub.png";

// Attach to this entity
m.attachedTo = entity;

// Ray Trace function for better projectile handling (it's still buggy though :|)
function RayTrace(start, end)
{
	var collisions = Array();

	var dx = Math.abs(end.x - start.x);
	var dy = Math.abs(end.y - start.y);
	var dz = Math.abs(end.z - start.z);

	var x = Math.floor(start.x);
	var y = Math.floor(start.y);
	var z = Math.floor(start.z);

	var dt_dx, dt_dy, dt_dz;

	if (dx != 0)
		dt_dx = 1.0 / dx;
	else
		dt_dx = null;

	if (dy != 0)
		dt_dy = 1.0 / dy;
	else
		dt_dy = null;

	if (dz != 0)
		dt_dz = 1.0 / dz;
	else
		dt_dz = null;

	var t = 0;

	var n = 1;
	var x_inc, y_inc, z_inc;
	var next_x, next_y, next_z;

	if (dx == 0)
	{
		x_inc = 0;
		next_x = null;
	}
	else if (end.x > start.x)
	{
		x_inc = 1;
		n += Math.floor(start.x) - x;
		next_x = (Math.floor(start.x) + 1 - start.x) * dt_dx;
	}
	else
	{
		x_inc = -1;
		n += x - Math.floor(end.x);
		next_x = (start.x - Math.floor(start.x)) * dt_dx;
	}

	if (dy == 0)
	{
		y_inc = 0;
		next_y = null;
	}
	else if (end.y > start.y)
	{
		y_inc = 1;
		n += Math.floor(end.y) - y;
		next_y = (Math.floor(start.y) + 1 - start.y) * dt_dy;
	}
	else
	{
		y_inc = -1;
		n += y - Math.floor(end.y);
		next_y = (start.y - Math.floor(start.y)) * dt_dy;
	}

	if (dz == 0)
	{
		z_inc = 0;
		next_z = null;
	}
	else if (end.z > start.z)
	{
		z_inc = 1;
		n += Math.floor(end.z) - z;
		next_z = (Math.floor(start.z) + 1 - start.z) * dt_dz;
	}
	else
	{
		z_inc = -1;
		n = z - Math.floor(end.z);
		next_z = (start.z - Math.floor(start.z)) * dt_dz;
	}

	for (; n>0; --n)
	{
		// Check for blocks
		if (world.getBlockID(x, y, z) != 0)
		{
			var block = Array()
			block.type = "Block";
			block.t = t;
			block.x = x;
			block.y = y;
			block.z = z;
			block.id = world.getBlockID(x, y, z);
			block.meta = world.getMetadata(x, y, z);
			collisions.push(block);
		}
		// Check for entities
		ex = start.x + (dx * t);
		ey = start.y + (dy * t);
		ez = start.z + (dz * t);
		var point = effect.spawnParticle("bubble", ex, ey, ez, 0, 0, 0);
		var ents = point.getEntitiesWithinRange(1.5);
		for (var i=0; i<ents.length; i++)
		{
			var hitEnt = Array();
			hitEnt.type = "Entity";
			hitEnt.ent = ents[i];
			hitEnt.t = t;
			collisions.push(hitEnt);
		}

		// Step along vector
		if (next_x != null)
		{
			if ((next_y == null || next_x < next_y) && (next_z == null || next_x < next_z))
			{
				x += x_inc;
				t = next_x;
				next_x += dt_dx;
				continue;
			}
		}
		if (next_y != null)
		{
			if ((next_x == null || next_y < next_x) && (next_z == null || next_y < next_z))
			{
				y += y_inc;
				t = next_y;
				next_y += dt_dy;
				continue;
			}
		}
		if (next_z != null)
		{
			if ((next_x == null || next_z < next_x) && (next_y == null || next_z < next_y))
			{
				z += z_inc;
				t = next_z;
				next_z += dt_dz;
				continue;
			}
		}
	}
	// Test end position for entities
	var point = effect.spawnParticle("bubble", end.x, end.y, end.z, 0, 0, 0);
	var ents = point.getEntitiesWithinRange(1.5);
	for (var i=0; i<ents.length; i++)
	{
		var hitEnt = Array();
		hitEnt.type = "Entity";
		hitEnt.ent = ents[i];
		hitEnt.t = t;
		collisions.push(hitEnt);
	}
	return collisions;
}