/***************************************************************************/
/*																		   */
/*	     /\                       PLEASE,                         /\       */
/*		/| \             DO NOT COPY SCRIPTS FROM                /| \      */
/*     / |  \                ZELDA ADVENTURE !                  / |  \     */
/*    /  |   \                                                 /  |   \    */
/*   /        \        READ THE credits.txt FILE FOR          /        \   */
/*  /    o     \             MORE INFORMATION.               /    o     \  */
/*  ------------                                             ------------  */
/***************************************************************************/

TICKS_PER_SECOND = 20 ;
TICKS_PER_MINUTE = 60*TICKS_PER_SECOND;
scree = ui.getHeight()
pinv = player.getInventory();
v = player.getVelocity();
ppos = player.getPosition();
sac = player.getInventory().getSlotContainingItem(281);
aut = false;
UIocc = 0;
toucheh = 0; taille = 105; place = 40; nbtrait = 1; nbtrait2 = 1; nbtrait3 = 1; touchespa = 0; suite = true; // Utilisées pour le menu d'Aide
NPCtalk = 0; playGame = 0; // Utilisées pour la galerie de tire
worm_init = false; worm_finished = false; worm_dir = new Vec2(1.0,0.0); // Utilisées pour le boss du donjon de glace
var manaTime = 0; // Utilisée pour le masque de vérité
var delayCrystal = 20; var allowCrystal = 1; var timeDelay = 0; var chronoBoss = 0; var tempsRestantBoss = 4*TICKS_PER_MINUTE; labelTempsRestant = undefined; labelTempsEnchant = undefined ; var naviText = 0;// Utilisées dans bossFeuEvents.js et item_272.js
var salleChaude = 0; var tempsRestantSalleC = 8*TICKS_PER_MINUTE; labelTempsRestantSalleC = undefined; // Utilisées pour la salle chaude du temple du feu
var resistanceChaleur; var tempsMilieuChaud = 0; // Utilisées pour la tunique Goron
player.cloak = "/models/none.png"; // Enlèver la cape
script.runScript("sprintf.js");
script.runScript("SoldatsChemins.js");

resetDelayCrystal = function() // Utilisée dans bossFeuEvents.js
{	chronoBoss = 1;}
endDelayCrystal = function() // Utilisée dans boosFeuFin.js
{	chronoBoss = 0;}

salleChaudeON = function() // Utilisée dans salleChaudeChronoON.js
{	salleChaude = 1;}
salleChaudeOFF = function()
{	salleChaude = 0;}


increTimeDelay = function(timeDelay)
{
	timeDelay++;
	return(timeDelay);
}

if(pinv.getItemInSlot(35) != null)
{
	qurub1 = pinv.getItemInSlot(35).getQuantity();
}
else
{
	qurub1 = 0;
}

pinv = player.getInventory();
if (pinv.getSlotContainingItem(266) != -1)
{
    if (pinv.getSlotContainingItem(266) == 35)
    {
        if (pinv.getItemInSlot(35).getQuantity() <= -1)
        {
            pinv.getItemInSlot(35).setQuantity(pinv.getItemInSlot(35).getQuantity() + 256);
        }
    }
}




if (pinv.getSlotContainingItem(262) != -1)
{
    if (pinv.getSlotContainingItem(262) == 26)
    {
        pinv.getItemInSlot(26).setQuantity(pinv.getItemInSlot(26).getDamage());
    }
}

if(player.getInventory().getSlotContainingItem(281) == -1)
{
	sac = 0;
}
const FROD_COSTS = new Array(Array(289, 0, 1));	// Items required to use the fire rod. Currently set to Green Dye and Arrows
const IROD_COSTS = new Array(Array(289, 0, 1)); 	// Items required to use the ice rod. Currently set to Green Dye and Arrows

var pinv = player.getInventory();

// Arrays to keep track of projectiles
var fireProjectiles = new Array();
var iceProjectiles = new Array();
var frozenMobs = new Array();

// Creates a fire rod projectile
function CreateFireProjectile()
{
	var ppos = player.getPosition();
	var pdir = player.getLookVec();
	
	// Create the projectile
	var projectile = effect.spawnParticle("bubble", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
	projectile.setPosition(ppos.x + pdir.x, ppos.y, ppos.z + pdir.z);
	projectile.setVelocity(pdir.x * FIRE_SPEED, pdir.y * FIRE_SPEED, pdir.z * FIRE_SPEED);
	projectile.collidesWithClipBlocks = false;
	fireProjectiles.splice(fireProjectiles.length, 0, Array(FIRE_LIFE, projectile, projectile.position.x, projectile.position.y, projectile.position.z, projectile.velocity.x, projectile.velocity.y, projectile.velocity.z));
}

// Controls all fire rod projectiles
function UpdateFireProjectiles()
{
	for (var i = 0; i < fireProjectiles.length; i++)
	{
		var deleted = false;
		
		// Move the projectile
		fireProjectiles[i][2] += fireProjectiles[i][5];
		fireProjectiles[i][3] += fireProjectiles[i][6];
		fireProjectiles[i][4] += fireProjectiles[i][7];
		
		// Prevent main particle from despawning by creating copies every tick
		var projectile = effect.spawnParticle("bubble", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
		var fpx = fireProjectiles[i][2];
		var fpy = fireProjectiles[i][3];
		var fpz = fireProjectiles[i][4];
		
		var fvx = fireProjectiles[i][5];
		var fvy = fireProjectiles[i][6];
		var fvz = fireProjectiles[i][7];
		
		projectile.setPosition(fpx, fpy, fpz);
		projectile.setVelocity(fvx, fvy, fvz);
		projectile.collidesWithClipBlocks = false;
		fireProjectiles[i][1].setDead();
		fireProjectiles[i][1] = projectile;
		
		// Create particles
		for (var j = 0; j < 5; j++)
		{
			var x = fireProjectiles[i][1].position.x + ((Math.random() * 0.2) - 0.1);
			var y = fireProjectiles[i][1].position.y + ((Math.random() * 0.2) - 0.1);
			var z = fireProjectiles[i][1].position.z + ((Math.random() * 0.2) - 0.1);
			var arg1 = 1.0;
			var arg2 = 0.5 + ((Math.random() * 0.5) - 0.25);
			var arg3 = 0.0;
			
			effect.spawnParticle("reddust", x, y, z, arg1, arg2, arg3);
		}
	
		// Decrement lifetime and remove if needed
		fireProjectiles[i][0]--;
		if (fireProjectiles[i][0] <= 0)
		{
			fireProjectiles.splice(i, 1);
			deleted = true;
		}
		
		if (!deleted)
		{
			// Check collision against mobs
			try{
				var mobsHit = fireProjectiles[i][1].getEntitiesWithinRange(1.0);
				if (mobsHit != null && mobsHit.length > 0)
				{
					for (var i = 0; i < mobsHit.length; i++)
					{
						if (mobsHit[i].getClassType() != "Player" && mobsHit[i].getClassType() != "Item")
						{
							mobsHit[i].attackEntityFrom(mobsHit[i], FIRE_DAMAGE);
							mobsHit[i].setFireLevel(200);
							FireRodImpact(fireProjectiles[i][1].position.x, fireProjectiles[i][1].position.y, fireProjectiles[i][1].position.z, false);
							fireProjectiles[i][1].setDead();
							fireProjectiles.splice(i, 1);
							deleted = true;
													break;
						}
					}
				}
			} catch(e) {}
		}
		
		if (!deleted && fireProjectiles[i] != null)
		{
			// Check collision against blocks
			fProjPosX = fireProjectiles[i][1].position.x + fireProjectiles[i][1].velocity.x;
			fProjPosY = fireProjectiles[i][1].position.y + fireProjectiles[i][1].velocity.y;
			fProjPosZ = fireProjectiles[i][1].position.z + fireProjectiles[i][1].velocity.z;
			if (world.getBlockID(Math.round(fProjPosX), Math.round(fProjPosY), Math.round(fProjPosZ)) != 0)
			{
				FireRodImpact(fireProjectiles[i][1].position.x, fireProjectiles[i][1].position.y, fireProjectiles[i][1].position.z, true);
				fireProjectiles[i][1].setDead();
				fireProjectiles.splice(i, 1);
			}
		}
	}
}

function FireRodImpact(x, y, z, hitBlock)
{
	x = Math.round(x);
	y = Math.round(y);
	z = Math.round(z);

	for (var i = 0; i < 25; i++)
	{
		var px = x + ((Math.random() * 0.5) - 0.25);
		var py = y + ((Math.random() * 0.5) - 0.25);
		var pz = z + ((Math.random() * 0.5) - 0.25);
		effect.spawnParticle("lava", px, py, pz, 0.0, 0.0, 0.0);
	}
	
	if (hitBlock)
	{
		if (FROD_MELT_SNOW)
		{
			for (var i = x - S_MELT_RANGE; i < x + S_MELT_RANGE; i++)
			{
				for (var j = y - S_MELT_RANGE; j < y + S_MELT_RANGE; j++)
				{
					for (var k = z - S_MELT_RANGE; k < z + S_MELT_RANGE; k++)
					{
						if (world.getBlockID(i, j, k) == 78 || world.getBlockID(i, j, k) == 80)
						{
							world.setBlockID(i, j, k, 0);
						}
					}
				}
			}
		}
		
		if (FROD_START_FIRES)
		{
			for (var i = 0; i < 25; i++)
			{
				var fx = Math.round(x + ((Math.random() * 5) - 2.5));
				var fy = Math.round(y + ((Math.random() * 5) - 2.5));
				var fz = Math.round(z + ((Math.random() * 5) - 2.5));
				
				if (world.getBlockID(fx, fy, fz) == 0)
				{
					world.setBlockID(fx, fy, fz, 51);
				}
			}
		}
		
		if (FROD_MELT_ICE)
		{	
			MeltIce(x, y, z, MELT_RANGE);
		}
	}
}

function MeltIce(x, y, z, depth)
{
	depth--;
	if (world.getBlockID(x, y, z) == 79)
	{
		if (world.getMetadata(x, y, z) == 1)
		{
			world.setBlockID(x, y, z, 9);
		}
		else
		{
			world.setBlockID(x, y, z, 0);
			world.setBlockID(x, y, z, 0);
		}
	}
	if (depth > 0)
	{
		MeltIce(x+1, y, z, depth);
		MeltIce(x-1, y, z, depth);
		MeltIce(x, y+1, z, depth);
		MeltIce(x, y-1, z, depth);
		MeltIce(x, y, z+1, depth);
		MeltIce(x, y, z-1, depth);
	}
}

// Fire rod
function UseFireRod()
{
	var i;
	var canUse = true;
	
	for (i=0; i<FROD_COSTS.length; i++)
	{
		if (!pinv.consumeItemAmount(FROD_COSTS[i][0], FROD_COSTS[i][1], FROD_COSTS[i][2]))
		{
			canUse = false;
			break;
		}
	}
	
	if (canUse)
	{
		player.swingMainHand();
		sound.playSoundUI(FIRE_ROD_SOUND);
		CreateFireProjectile();
	}
	else
	{
		for (var j=0; j<i; j++)
		{
			pinv.addItem(Item(FROD_COSTS[j][0], FROD_COSTS[j][2], FROD_COSTS[j][1]));
		}
		sound.playSoundUI(NO_MAGIC_SOUND);
	}
}

// Creates an ice rod projectile
function CreateIceProjectile()
{
	var ppos = player.getPosition();
	var pdir = player.getLookVec();
	
	// Create the projectile
	var projectile = effect.spawnParticle("bubble", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
	projectile.setPosition(ppos.x + pdir.x, ppos.y, ppos.z + pdir.z);
	projectile.setVelocity(pdir.x * ICE_SPEED, pdir.y * ICE_SPEED, pdir.z * ICE_SPEED);
	projectile.collidesWithClipBlocks = false;
	iceProjectiles.splice(iceProjectiles.length, 0, Array(ICE_LIFE, projectile, projectile.position.x, projectile.position.y, projectile.position.z, projectile.velocity.x, projectile.velocity.y, projectile.velocity.z));
}

// Controls all ice rod projectiles
function UpdateIceProjectiles()
{
	for (var i = 0; i < iceProjectiles.length; i++)
	{
		var deleted = false;
		
		// Move the projectile
		iceProjectiles[i][2] += iceProjectiles[i][5];
		iceProjectiles[i][3] += iceProjectiles[i][6];
		iceProjectiles[i][4] += iceProjectiles[i][7];
		
		// Prevent main particle from despawning by creating copies every tick
		var projectile = effect.spawnParticle("bubble", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
		var fpx = iceProjectiles[i][2];
		var fpy = iceProjectiles[i][3];
		var fpz = iceProjectiles[i][4];
		
		var fvx = iceProjectiles[i][5];
		var fvy = iceProjectiles[i][6];
		var fvz = iceProjectiles[i][7];
		
		projectile.setPosition(fpx, fpy, fpz);
		projectile.setVelocity(fvx, fvy, fvz);
		projectile.collidesWithClipBlocks = false;
		iceProjectiles[i][1].setDead();
		iceProjectiles[i][1] = projectile;
		
		// Create particles
		for (var j = 0; j < 5; j++)
		{
			var x = iceProjectiles[i][1].position.x + ((Math.random() * 0.2) - 0.1);
			var y = iceProjectiles[i][1].position.y + ((Math.random() * 0.2) - 0.1);
			var z = iceProjectiles[i][1].position.z + ((Math.random() * 0.2) - 0.1);
			var arg1 = Math.random();
			var arg2 = 1.0;
			var arg3 = 1.0;
			
			effect.spawnParticle("reddust", x, y, z, arg1, arg2, arg3);
		}
	
		// Decrement lifetime and remove if needed
		iceProjectiles[i][0]--;
		if (iceProjectiles[i][0] <= 0)
		{
			iceProjectiles.splice(i, 1);
			deleted = true;
		}
		
		if (!deleted)
		{
			// Check collision against mobs
			var mobsHit = iceProjectiles[i][1].getEntitiesWithinRange(1.0);
			if (mobsHit != null && mobsHit.length > 0)
			{
				for (var i = 0; i < mobsHit.length; i++)
				{
					if (mobsHit[i].getClassType() != "Player" && mobsHit[i].getClassType() != "Item")
					{
						mobsHit[i].attackEntityFrom(mobsHit[i], ICE_DAMAGE);
						mobsHit[i].setStunned(60);
						frozenMobs.splice(frozenMobs.length - 1, 0, mobsHit[i]);
						IceRodImpact(iceProjectiles[i][1].position.x, iceProjectiles[i][1].position.y, iceProjectiles[i][1].position.z, false);
						iceProjectiles[i][1].setDead();
						iceProjectiles.splice(i, 1);
						deleted = true;
                                                break;
					}
				}
			}
		}
		
		if (!deleted && iceProjectiles[i] != null)
		{
			// Check collision against blocks
			fProjPosX = iceProjectiles[i][1].position.x + iceProjectiles[i][1].velocity.x;
			fProjPosY = iceProjectiles[i][1].position.y + iceProjectiles[i][1].velocity.y;
			fProjPosZ = iceProjectiles[i][1].position.z + iceProjectiles[i][1].velocity.z;
			if (world.getBlockID(Math.round(fProjPosX), Math.round(fProjPosY), Math.round(fProjPosZ)) != 0)
			{
				IceRodImpact(iceProjectiles[i][1].position.x, iceProjectiles[i][1].position.y, iceProjectiles[i][1].position.z, true);
				iceProjectiles[i][1].setDead();
				iceProjectiles.splice(i, 1);
			}
		}
	}
}

function IceRodImpact(x, y, z, hitBlock)
{
	x = Math.round(x);
	y = Math.round(y);
	z = Math.round(z);

	for (var i = 0; i < 40; i++)
	{
		var px = x + ((Math.random() * 2.0) - 0.1);
		var py = y + ((Math.random() * 2.0) - 0.1);
		var pz = z + ((Math.random() * 2.0) - 0.1);
		effect.spawnParticle("snowballpoof", px, py, pz, 0.0, 0.0, 0.0);
	}
	
	if (hitBlock)
	{
		if (IROD_MAKE_ICE)
		{
			BuildIce(x, y, z, ICE_SIZE);
		}
		
		if (IROD_MAKE_OBSID)
		{
			//BuildObsid(x, y, z, OBSID_SIZE);
		}
		
		if (IROD_REMOVE_FIRE)
		{
			for (var i = x - RF_RANGE; i < x + RF_RANGE; i++)
			{
				for (var j = y - RF_RANGE; j < y + RF_RANGE; j++)
				{
					for (var k = z - RF_RANGE; k < z + RF_RANGE; k++)
					{
						if (world.getBlockID(i, j, k) == 51)
						{
							world.setBlockID(i, j, k, 0);
						}
					}
				}
			}
		}
		
		if (IROD_MAKE_SNOW)
		{
			for (var i = 0; i < 40; i++)
			{
				var ix = Math.round(x + ((Math.random() * 5) - 2.5));
				var iy = Math.round(y + ((Math.random() * 5) - 2.5));
				var iz = Math.round(z + ((Math.random() * 5) - 2.5));
				
				if (world.getBlockID(ix, iy, iz) == 0 && world.getBlockID(ix, iy - 1, iz) != 0 && world.getBlockID(ix, iy - 1, iz) != 8 && world.getBlockID(ix, iy - 1, iz) != 9
					&& world.getBlockID(ix, iy - 1, iz) != 10 && world.getBlockID(ix, iy - 1, iz) != 11 && world.getBlockID(ix, iy - 1, iz) != 78 && world.getBlockID(ix, iy - 1, iz) != 51)
				{
					world.setBlockID(ix, iy, iz, 78);
				}
			}
		}
	}
}

function BuildIce(x, y, z, depth)
{
	depth--;
	var allow = true;
	if (world.getBlockID(x, y, z) == 8 || world.getBlockID(x, y, z) == 9)
	{
		if(world.getBlockID(x,y+1,z) != 8 && world.getBlockID(x,y+1,z) != 9 && world.getBlockID(x,y+1,z) != 79)
		{
			for(a=-2;a<3;a++){
				for(b=-2;b<3;b++){
					if(world.getBlockID(x+a, y, z+b) == 0){
						allow = false;
			}	}	}
			if(allow == true)
			{
				world.setBlockIDAndMetadata(x, y, z, 79, 1);
			}
		}
	}
	if (depth > 0)
	{
		BuildIce(x+1, y, z, depth);
		BuildIce(x-1, y, z, depth);
		BuildIce(x, y+1, z, depth);
		BuildIce(x, y-1, z, depth);
		BuildIce(x, y, z+1, depth);
		BuildIce(x, y, z-1, depth);
	}
}

function BuildObsid(x, y, z, depth)
{
	depth--;
	if (world.getBlockID(x, y, z) == 10 || world.getBlockID(x, y, z) == 11)
	{
		world.setBlockID(x, y, z, 49);
	}
	if (depth > 0)
	{
		BuildObsid(x+1, y, z, depth);
		BuildObsid(x-1, y, z, depth);
		BuildObsid(x, y+1, z, depth);
		BuildObsid(x, y-1, z, depth);
		BuildObsid(x, y, z+1, depth);
		BuildObsid(x, y, z-1, depth);
	}
}

// Ice Rod
function UseIceRod()
{
	var i;
	var canUse = true;
	
	for (i=0; i<IROD_COSTS.length; i++)
	{
		if (!pinv.consumeItemAmount(IROD_COSTS[i][0], IROD_COSTS[i][1], IROD_COSTS[i][2]))
		{
			canUse = false;
			break;
		}
	}
	
	if (canUse)
	{
		player.swingMainHand();
		sound.playSoundUI(ICE_ROD_SOUND);
		CreateIceProjectile();
	}
	else
	{
		for (var j=0; j<i; j++)
		{
			pinv.addItem(Item(IROD_COSTS[j][0], IROD_COSTS[j][2], IROD_COSTS[j][1]));
		}
		sound.playSoundUI(NO_MAGIC_SOUND);
	}
}

function UpdateFrozenMobs()
{
	for (var i = 0; i < frozenMobs.length; i++)
	{
		var hTime = frozenMobs[i].getHurtTime();
		if (hTime > 0)
		{
			frozenMobs[i].setHurtTime(0);
		}
		
		var r = Math.random();
		
		if (r <= 0.05)
		{
			for (var j = 0; j < 10; j++)
			{
				px = frozenMobs[i].getPosition().x + ((Math.random() * 0.5) - 0.25);
				py = frozenMobs[i].getPosition().y + ((Math.random() * 0.5) - 0.25);
				pz = frozenMobs[i].getPosition().z + ((Math.random() * 0.5) - 0.25);
				
				effect.spawnParticle("snowballpoof", px, py, pz, 0.0, 0.0, 0.0);
			}
		}
		
		if (frozenMobs[i].getStunned() == 0)
		{
			frozenMobs.splice(i, 1);
		}
	}
}


// VEC2

function Vec2(x, z){
    this.x = x;
    this.z = z;
}

Vec2.prototype.set = function(x,z){
    this.x = x;
    this.z = z;
}

Vec2.prototype.copy = function() {
    return new Vec2f(this.x, this.z);
}

Vec2.prototype.length = function() {
    return Math.sqrt(this.x*this.x+this.z*this.z);
}

Vec2.prototype.normalize = function() {
    var l = this.length();
    if(l > 0){
        this.x = this.x / l;
        this.z = this.z / l;
    }
    return this;
}

Vec2.prototype.getTheta = function() {
    var a = Math.atan2(this.z, this.x);
    if(a < 0){
        a += 2 * Math.PI;
    }
    return 180 * a / Math.PI; 
}

Vec2.prototype.setTheta = function(theta) {
    if ((theta <= -360) || (theta >= 360)) {
        theta = theta % 360;
    }
    if (theta < 0) {
        theta = 360 + theta;
    }
    theta = Math.PI * theta / 180;
    var l = this.length();
    this.x = l * Math.cos(theta);
    this.z = l * Math.sin(theta);
    return this;
}