/************************************************
 ******         Mods (Default)             ******
 ************************************************/
 
//A mod made by Atilla for LV's level generator
//Paste it into the end of the tiles script.
//And don't forget to add it to the array at the start of the script!
function openBox() //should create a box with randomly placed gaps to let you in. Seems to work, though it's not the most elegant piece of code ever written.
{

	openBox.prototype.Create = Create;
	openBox.prototype.Prob = Prob;

	var x;
	var y;
	var length;
	var height;
	var thickness;
	var gapwidth;	//width of opening
	var gapside;	//which side the gap appears in
	var gappos;	//offset of the gap - in the middle of the side, or near one edge?
	var gapnum;	//number of gaps

	var probability = 5;


	function Prob() {
		return probability;
	}

	function Create() {

		this.x = Math.floor(Math.random()*31);
		this.y = Math.floor(Math.random()*23);
		this.length = Math.ceil(Math.random()*Math.random()*29)+2;
		this.height = Math.ceil(Math.random()*Math.random()*21)+2;	//must be at least 3*3 for a box.
		this.thickness = 20;	//large enough to ensure the random calculation below always runs

		while (this.length <= 2*this.thickness || this.height <= 2*this.thickness) //Check if the thickness too large to actually make a box - keep going until it's small enough
		{
			this.thickness = Math.ceil(Math.random()*4); //random thickness. 1 to 4 should give reasonable boxes - width and height both > 8 is unlikely so higher numbers aren't really needed.
		}

		for (var i=this.length;i>=0;i--) {
			for (var j=this.height;j>=0;j--) {
				if (this.x+i<=30 && this.y+j<=22) {	// Check that the tile added is within the array.

					arrayTiles[this.x+i][this.y+j] = 1;

						// Check that each corner is inside the tile array, and check if it's
						// meant to be decorated. If both are true, then change the corner to
						// a styled tile.
					if (this.x<=30 && this.x>=0 && this.y<=22 && this.y>=0 &&
					decoration>Math.random()) {
						arrayTiles[this.x][this.y] = arrayTileGroups[tileGroup][1];
					}
					if (this.x+this.length<=30 && this.x+this.length>=0 && this.y<=22 && this.y>=0 &&
					decoration>Math.random()) {
						arrayTiles[this.x+this.length][this.y] = arrayTileGroups[tileGroup][0];
					}
					if (this.x+this.length<=30 && this.x+this.length>=0 && this.y+this.height<=22 &&
					this.y+this.height>=0 && decoration>Math.random()) {
						arrayTiles[this.x+this.length][this.y+this.height] = arrayTileGroups[tileGroup][3];
					}
					if (this.x<=30 && this.x>=0 && this.y+this.height<=22 && this.y+this.height>=0 &&
					decoration>Math.random()) {
						arrayTiles[this.x][this.y+this.height] = arrayTileGroups[tileGroup][2];
					}
				}
			}
		}

		for (var i=this.length-(2*this.thickness);i>=0;i--) {
			for (var j=this.height-(2*this.thickness);j>=0;j--) {
				if (this.x+this.thickness+i<=30 && this.y+this.thickness+j<=22) {	// Remove tiles from a smaller rectangle inside the first

					arrayTiles[this.x+this.thickness+i][this.y+this.thickness+j] = 0; //start (this.thickness) blocks in from the edge

						// Check that each corner is inside the tile array, and check if it's
						// meant to be decorated. If both are true, then change the corner to
						// a styled tile.
					if (this.x<=30 && this.x>=0 && this.y<=22 && this.y>=0 &&
					decoration>Math.random()) {
						arrayTiles[this.x+this.thickness][this.y+this.thickness] = arrayTileGroups[tileGroup][3];
					}
					if (this.x+this.length<=30 && this.x+this.length>=0 && this.y<=22 && this.y>=0 &&
					decoration>Math.random()) {
						arrayTiles[this.x+this.length-this.thickness][this.y+this.thickness] = arrayTileGroups[tileGroup][2];
					}
					if (this.x+this.length<=30 && this.x+this.length>=0 && this.y+this.height<=22 &&
					this.y+this.height>=0 && decoration>Math.random()) {
						arrayTiles[this.x+this.length-this.thickness][this.y+this.height-this.thickness] = arrayTileGroups[tileGroup][1];
					}
					if (this.x<=30 && this.x>=0 && this.y+this.height<=22 && this.y+this.height>=0 &&
					decoration>Math.random()) {
						arrayTiles[this.x+this.thickness][this.y+this.height-this.thickness] = arrayTileGroups[tileGroup][0];
					}
					
				}
			}
		}

		//gap stuff

		this.gapnum = Math.ceil(Math.random()*Math.random()*4); //Between one and four gaps, although the latter is unlikely

		for (var k=0;k < this.gapnum; k++)	//this just iterates for (gapnum) random gaps
		{

			this.gapside = Math.ceil(Math.random()*4);	//Random side - 1 to 4 = top, left, bottom, right

			if (this.gapside == 1)		//for top
			{
			this.gapwidth = Math.ceil(Math.random()*(this.length-2*this.thickness));	//random gapwidth - cannot be greater than size of inner rectangle, must be at least 1
			this.gappos = Math.floor(Math.random()*(this.length-2*this.thickness-this.gapwidth+2)); //random offset. cannot be smaller than 0 or so large the gap extends past the edge of the inner rectangle.
													//I'm not sure this actually works properly, but it seems to be giving a more-or-less even chance of each offset.
			
				for (var i=this.gapwidth-this.thickness;i>=0;i--) 	//this bit actually removes the tiles for the gap
				{
					for (var j=this.thickness;j>=0;j--) 
					{
						if (this.x+this.thickness+this.gappos+i<=30 && this.y+j<=22) 
						{	// Remove edge of box along section of length gapwidth
						
							arrayTiles[this.x+this.thickness+this.gappos+i][this.y+j] = 0;
						}
					}
				}
			}

			if (this.gapside == 2)		//for left
			{
			this.gapwidth = Math.ceil(Math.random()*(this.height-2*this.thickness));	//random gapwidth - cannot be greater than size of inner rectangle, must be at least 1
			this.gappos = Math.floor(Math.random()*(this.height-2*this.thickness-this.gapwidth+2)); //random offset. cannot be smaller than 0 or so large the gap extends past the edge of the inner rectangle.
														//I'm not sure this actually works properly, but it seems to be giving a more-or-less even chance of each offset.
				
				for (var i=this.thickness;i>=0;i--) 	//this bit actually removes the tiles for the gap
				{
					for (var j=this.gapwidth-this.thickness;j>=0;j--) 
					{
						if (this.x+i<=30 && this.y+this.thickness+this.gappos+j<=22) 
						{	// Remove edge of box along section of length gapwidth
						
							arrayTiles[this.x+i][this.y+this.thickness+this.gappos+j] = 0;
						}
					}
				}
			}

			if (this.gapside == 3)		//for bottom
			{
			this.gapwidth = Math.ceil(Math.random()*(this.length-2*this.thickness));	//random gapwidth - cannot be greater than size of inner rectangle, must be at least 1
			this.gappos = Math.floor(Math.random()*(this.length-2*this.thickness-this.gapwidth+2)); //random offset. cannot be smaller than 0 or so large the gap extends past the edge of the inner rectangle.
														//I'm not sure this actually works properly, but it seems to be giving a more-or-less even chance of each offset.
				
				for (var i=this.gapwidth-this.thickness;i>=0;i--) 	//this bit actually removes the tiles for the gap
				{
					for (var j=this.thickness;j>=0;j--) 
					{
						if (this.x+this.thickness+this.gappos+i<=30 && this.y+this.height-this.thickness+j<=22) 
						{	// Remove edge of box along section of length gapwidth
						
							arrayTiles[this.x+this.thickness+this.gappos+i][this.y+this.height-this.thickness+j] = 0;
						}
					}
				}
			}

			if (this.gapside == 4)		//for right
			{
			this.gapwidth = Math.ceil(Math.random()*(this.height-2*this.thickness));	//random gapwidth - cannot be greater than size of inner rectangle, must be at least 1
			this.gappos = Math.floor(Math.random()*(this.height-2*this.thickness-this.gapwidth+2)); //random offset. cannot be smaller than 0 or so large the gap extends past the edge of the inner rectangle.
														//I'm not sure this actually works properly, but it seems to be giving a more-or-less even chance of each offset.
				
				for (var i=this.thickness;i>=0;i--) 	//this bit actually removes the tiles for the gap
				{
					for (var j=this.gapwidth-this.thickness;j>=0;j--) 
					{
						if (this.x+this.length-this.thickness+i<=30 && this.y+this.thickness+this.gappos+j<=22) 
						{	// Remove edge of box along section of length gapwidth
						
							arrayTiles[this.x+this.length-this.thickness+i][this.y+this.thickness+this.gappos+j] = 0;
						}
					}
				}
			}
		}

		
		//end gap stuff
	}
}
