/***Script exécuté à l'utilisation du feu de din***/

//CREATE SOUND

{
	var ppos = player.getPosition();
	var pinv = player.getInventory();
	var dinfire = "sound.dinfire";
	var No_Magic;
	var canUse = true;
	var permit;
	var i;
	
	if(permit == undefined)
		permit = true;
		
		
	//IF YOU DON'T HAVE ENOUGH MAGIC
	if(pinv.getItemInSlot(17) != null)
	{
		if(pinv.getItemInSlot(17).getQuantity() < 10)
			canUse = false;
	}
	else if(pinv.getItemInSlot(17) == null)
		canUse = false;

	if(!canUse && permit)
	{
		sound.playSoundUI("sound.lttp_error");
	}

	//IF YOU HAVE ENOUGH MAGIC
	if(pinv.getItemInSlot(17) != null)
	{
		if(pinv.getItemInSlot(17).getQuantity() >= 10)
			canUse = true;
	}
	
	if (canUse && permit)
	{
		permit = false;
		sound.playSoundUI(dinfire);
		player.swingMainHand();
		time.sleep(0.32);
		player.setStunned(50);
		
		//USES MAGIC
		pinv.consumeItemAmount(289,0,10);

		//CREATES THE PARTICLES, FIRST WAVE
		for (var i=0; i<100; i++)
		{
			var px = ppos.x + ((Math.random() * 4) - 2);
			var py = ppos.y + ((Math.random() * 4) - 2);
			var pz = ppos.z + ((Math.random() * 4) - 2);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		//SETS ENTITIES WITHIN RANGE ON FIRE AND CONTINUES MAKING OTHER WAVES
		var target = player.getEntitiesWithinRange(8.0);
		if (target != null && target.length > 0)
		{
			for (var i = 0; i < target.length; i++)
			{
				if (target[i].getClassType() != "Player" && target[i].getClassType() != "Item" && target[i].getClassType() != "Model" && target[i].getClassType() != "NPC" && target[i].getClassType() != "Painting")
				{
					target[i].attackEntityFrom(target[i], 16);
					target[i].setFireLevel(200);
					target[i].setStunned(50);
				}
			}
		}
		var target2 = player.getEntitiesWithinRange(40.0);
		if (target2 != null && target2.length > 0)
		{
			for (var i = 0; i < target2.length; i++)
			{
				if (target2[i].getClassType() != "Player" && target2[i].getClassType() != "Item" && target2[i].getClassType() != "Model" && target2[i].getClassType() != "NPC" && target2[i].getClassType() != "Painting")
				{
					target2[i].setStunned(50);
				}
			}
		}

		time.sleep(0.2)

		for (var i=0; i<120; i++)
		{
			var px = ppos.x + ((Math.random() * 5) - 2.5);
			var py = ppos.y + ((Math.random() * 4) - 2);
			var pz = ppos.z + ((Math.random() * 5) - 2.5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);

		for (var i=0; i<140; i++)
		{
			var px = ppos.x + ((Math.random() * 6) - 3);
			var py = ppos.y + ((Math.random() * 4) - 2);
			var pz = ppos.z + ((Math.random() * 6) - 3);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);
		
		for(i=-2; i<=2 ; i++)
		{
			for(j=-1; j<=1 ; j++)
			{
				for(k=-2; k<=2 ; k++)
				{
					if(world.getBlockID(ppos.x+i,ppos.y+j,ppos.z+k) == 30)
					{
						world.setBlockID(ppos.x+i,ppos.y+j,ppos.z+k,0);
					}
				}
			}
		}

		for (var i=0; i<160; i++)
		{
			var px = ppos.x + ((Math.random() * 7) - 3.5);
			var py = ppos.y + ((Math.random() * 4) - 2);
			var pz = ppos.z + ((Math.random() * 7) - 3.5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);

		for (var i=0; i<180; i++)
		{
			var px = ppos.x + ((Math.random() * 8) - 4);
			var py = ppos.y + ((Math.random() * 4) - 2);
			var pz = ppos.z + ((Math.random() * 8) - 4);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);

		for (var i=0; i<200; i++)
		{
			var px = ppos.x + ((Math.random() * 9) - 4.5);
			var py = ppos.y + ((Math.random() * 8) - 4);
			var pz = ppos.z + ((Math.random() * 9) - 4.5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);
		
		for(i=-4; i<=4 ; i++)
		{
			for(j=-1; j<=4 ; j++)
			{
				for(k=-4; k<=4 ; k++)
				{
					if(world.getBlockID(ppos.x+i,ppos.y+j,ppos.z+k) == 30)
					{
						world.setBlockID(ppos.x+i,ppos.y+j,ppos.z+k,0);
					}
				}
			}
		}

		for (var i=0; i<210; i++)
		{
			var px = ppos.x + ((Math.random() * 9.5) - 5);
			var py = ppos.y + ((Math.random() * 8) - 4);
			var pz = ppos.z + ((Math.random() * 9.5) - 5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);

		for (var i=0; i<250; i++)
		{
			var px = ppos.x + ((Math.random() * 10) - 5);
			var py = ppos.y + ((Math.random() * 8) - 4);
			var pz = ppos.z + ((Math.random() * 10) - 5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);
		
		for(i=-6; i<=6 ; i++)
		{
			for(j=-1; j<=6 ; j++)
			{
				for(k=-6; k<=6 ; k++)
				{
					if(world.getBlockID(ppos.x+i,ppos.y+j,ppos.z+k) == 30)
					{
						world.setBlockID(ppos.x+i,ppos.y+j,ppos.z+k,0);
					}
				}
			}
		}

		for (var i=0; i<251; i++)
		{
			var px = ppos.x + ((Math.random() * 11) - 5);
			var py = ppos.y + ((Math.random() * 8) - 4);
			var pz = ppos.z + ((Math.random() * 11) - 5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);

		for (var i=0; i<226; i++)
		{
			var px = ppos.x + ((Math.random() * 12) - 5);
			var py = ppos.y + ((Math.random() * 8) - 4);
			var pz = ppos.z + ((Math.random() * 12) - 5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);
		
		for(i=-8; i<=8 ; i++)
		{
			for(j=-1; j<=8 ; j++)
			{
				for(k=-8; k<=8 ; k++)
				{
					if(world.getBlockID(ppos.x+i,ppos.y+j,ppos.z+k) == 30)
					{
						world.setBlockID(ppos.x+i,ppos.y+j,ppos.z+k,0);
					}
				}
			}
		}

		for (var i=0; i<201; i++)
		{
			var px = ppos.x + ((Math.random() * 13) - 5);
			var py = ppos.y + ((Math.random() * 8) - 4);
			var pz = ppos.z + ((Math.random() * 13) - 5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);

		for (var i=0; i<176; i++)
		{
			var px = ppos.x + ((Math.random() * 14) - 5);
			var py = ppos.y + ((Math.random() * 8) - 4);
			var pz = ppos.z + ((Math.random() * 14) - 5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}
		
		for(i=-10; i<=10 ; i++)
		{
			for(j=-1; j<=8 ; j++)
			{
				for(k=-10; k<=10 ; k++)
				{
					if(world.getBlockID(ppos.x+i,ppos.y+j,ppos.z+k) == 30)
					{
						world.setBlockID(ppos.x+i,ppos.y+j,ppos.z+k,0);
					}
				}
			}
		}

		time.sleep(0.2);

		for (var i=0; i<151; i++)
		{
			var px = ppos.x + ((Math.random() * 15) - 5);
			var py = ppos.y + ((Math.random() * 8) - 4);
			var pz = ppos.z + ((Math.random() * 15) - 5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}

		time.sleep(0.2);

		for (var i=0; i<101; i++)
		{
			var px = ppos.x + ((Math.random() * 15) - 5);
			var py = ppos.y + ((Math.random() * 8) - 4);
			var pz = ppos.z + ((Math.random() * 15) - 5);
			effect.spawnParticle("flame", px, py, pz, 0, 0, 0);
		}
		permit = true;
	} 
}

