/*
	This file is part of The Adventures of Stickman by Matt Trobaugh, which is dedicated to the public domain under a CC0 Public Domain Dedication. To view a copy of this dedication, visit http://creativecommons.org/publicdomain/zero/1.0/.

	The Adventures of Stickman is built on the Turpidge Game Engine version 1.1 by Matt Trobaugh.
*/

try{





levelTemplates = new LevelTemplates();

// exp needed to reach level 2
levelingConstant = 10;

fighterTemplate = levelTemplates.addTemplate("Fighter");
//				(stat to increase, percent increase each level)
fighterTemplate.addStat("attack", 16);
fighterTemplate.addStat("defense", 10);
fighterTemplate.addStat("maxHP", 15);
fighterTemplate.addStat("speed", 15);

healerTemplate = levelTemplates.addTemplate("Healer");
//				(stat to increase, percent increase each level)
healerTemplate.addStat("attack", 5);
healerTemplate.addStat("defense", 10);
healerTemplate.addStat("maxHP", 12);
healerTemplate.addStat("maxMP", 15);
healerTemplate.addStat("speed", 15);
//				(spell name, level character learns it)
healerTemplate.addSpell("Shield", 3);
healerTemplate.addSpell("Heal 2", 4);
healerTemplate.addSpell("Shield 2", 7);
healerTemplate.addSpell("Heal 3", 10);

berzerkerTemplate = levelTemplates.addTemplate("Berzerker");
//				(stat to increase, percent increase each level)
berzerkerTemplate.addStat("attack", 17);
berzerkerTemplate.addStat("defense", 15);
berzerkerTemplate.addStat("maxHP", 12);
berzerkerTemplate.addStat("speed", 10);

wizardTemplate = levelTemplates.addTemplate("Wizard");
//				(stat to increase, percent increase each level)
wizardTemplate.addStat("attack", 5);
wizardTemplate.addStat("defense", 10);
wizardTemplate.addStat("maxHP", 12);
wizardTemplate.addStat("maxMP", 15);
wizardTemplate.addStat("speed", 10);
//				(spell name, level character learns it)
wizardTemplate.addSpell("Fireball", 5);
wizardTemplate.addSpell("Nuke", 10);
wizardTemplate.addSpell("Laser", 15);

archerTemplate = levelTemplates.addTemplate("Archer");
//				(stat to increase, percent increase each level)
archerTemplate.addStat("attack", 13);
archerTemplate.addStat("defense", 10);
archerTemplate.addStat("maxHP", 12);
archerTemplate.addStat("speed", 15);





playerCharacters = new PlayerCharacters();

//						(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, charClass)
stickman = playerCharacters.addCharacter("Stickman", 3, 3, 8, 0, 2, 3, 1, "stickman4.png", "Fighter");

//						(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, charClass)
mrWhite = playerCharacters.addCharacter("Mr. White", 1, 1, 5, 10, 2, 3, 1, "mrWhite4.png", "Healer");
mrWhite.learnSpell("Heal");
mrWhite.levelUpTo(2);

//						(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, charClass)
mrPink = playerCharacters.addCharacter("Mr. Pink", 4, 4, 7, 0, 2, 3, 1, "mrPink4.png", "Berzerker");

//						(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, charClass)
mrBlonde = playerCharacters.addCharacter("Mr. Blonde", 4, 4, 7, 0, 2, 3, 1, "mrBlonde4.png", "Berzerker");
mrBlonde.levelUpTo(3);
items.giveCharacterEquipment(mrBlonde, "Razor");

//						(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, charClass)
mrBlue = playerCharacters.addCharacter("Mr. Blue", 2, 1, 5, 12, 2, 3, 1, "mrBlue4.png", "Wizard");
mrBlue.learnSpell("Spark");
mrBlue.levelUpTo(5);

//						(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, charClass)
mrBrown = playerCharacters.addCharacter("Mr. Brown", 3, 2, 5, 0, 2, 3, 1, "mrBrown4.png", "Archer");
items.giveCharacterEquipment(mrBrown, "Bow");
mrBrown.levelUpTo(7);

//						(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, charClass)
mrOrange = playerCharacters.addCharacter("Mr. Orange", 3, 3, 8, 0, 2, 3, 1, "mrOrange4.png", "Fighter");
items.giveCharacterEquipment(mrOrange, "Longsword");
mrOrange.levelUpTo(8);





enemyCharacters = new EnemyCharacters();

//			(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
bunny = enemyCharacters.addCharacter("Bunny", 1, 1, 3, 0, 2, 3, 1, "bunny.png", "sad", 1, 2);
items.giveCharacterItem(bunny, "Green Potion");

//			(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
enemyCharacters.addCharacter("Spider", 2, 1, 5, 0, 3, 2, 1, "spider.png", "ads", 2, 4);

//			(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
enemyCharacters.addCharacter("Bat", 2, 1, 5, 0, 3, 4, 1, "bat.png", "ads", 2, 5);

//			(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
enemyCharacters.addCharacter("Goblin", 15, 7, 10, 0, 4, 3, 1, "goblin.png", "das", 5, 10);

//					(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
voodooDoll = enemyCharacters.addCharacter("Voodoo Doll", 4, 4, 7, 22, 7, 3, 1, "voodooDoll.png", "ads", 5, 10);
voodooDoll.learnSpell("Shrink");

//			(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
enemyCharacters.addCharacter("Fighter", 20, 10, 15, 0, 7, 3, 1, "fighter.png", "das", 10, 15);

//			(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
devil = enemyCharacters.addCharacter("Devil", 7, 7, 10, 30, 8, 3, 1, "devil.png", "sad", 8, 17);
devil.learnSpell("Heal");
devil.learnSpell("Spark");

//			(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
imp = enemyCharacters.addCharacter("Imp", 6, 6, 6, 15, 10, 3, 1, "imp.png", "ads", 10, 17);
imp.learnSpell("Spark");
imp.learnSpell("Fireball");
imp.learnSpell("Immobilize");

//			(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
witch = enemyCharacters.addCharacter("Witch", 2, 10, 25, 180, 20, 5, 1, "witch.png", "sad", 20, 25);
witch.learnSpell("Heal 2");
witch.learnSpell("Fireball");
witch.learnSpell("Immobilize");

//			(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
enemyCharacters.addCharacter("Demon", 50, 10, 10, 0, 20, 3, 1, "demon.png", "ads", 5, 20);

//						(name, attack, defense, maxHP, maxMP, speed, moveRange, attackRange, image, mode, money, exp)
theBeast = enemyCharacters.addCharacter("The Beast", 30, 15, 50, 250, 25, 0, 0, "TheBeast.png", "dsa", 241, 425);
theBeast.learnSpell("Nuke");
theBeast.learnSpell("Laser");

theBeast.bossStrategy = function()
{
	/*
		if there are at least 2 dead demons and at least 2 places to put them
			if the beast has attack strategies: 
				2/3 chance of attacking (spell), 1/3 chance of summoning demons
			if the beast has no attack strategies:
				2/3 chance of summoning demons, 1/3 chance of doing nothing
		else just attack if you can
	*/	
	
	var deadDemons = 0;
	var i;
	for(i=0;i<enemyTeam.length;i++)
	{
		if(enemyTeam[i].name == "Demon" && enemyTeam[i].status == "dead")
		{
			deadDemons++;
		}
	}
	
	
	var spaceOptions = getRangeSpaces(this.x, this.y, 6);
	
	// I need to exclude unwalkable and occupied spaces
	var occSpaces = new Array();
	for(i=0;i<bothTeams.length;i++)
	{
		occSpaces[i] = bothTeams[i].x + "-" + bothTeams[i].y;
	}
	
	for(i=spaceOptions.length-1;i>-1;i--)
	{
		var spaceCoords = spaceOptions[i].spaceID.split("-");
	
		if(occSpaces.indexOf(spaceOptions[i].spaceID) > -1)
		{
			spaceOptions.splice(i, 1);
		}
		else if(currentLocation.spaces[spaceCoords[0]][spaceCoords[1]].walk == 0)
		{
			spaceOptions.splice(i, 1);
		}
	}
	
	setMoveOptions(); // this needs to be set to determine attackOption.inRange
	var attackOptions = getAttackOptions();
	
	if(attackOptions.length > 1)
	{
		attackOptions.sort(function(a,b){return b.efficacy - a.efficacy;});
	}
	
	for(i=attackOptions.length-1;i>-1;i--)
	{
		if(attackOptions[i].inRange == 0)
		{
			attackOptions.splice(i, 1);
		}
	}
	
	var randNum = Math.random() * 3;
	var action = "";
	
	if(deadDemons > 1 && spaceOptions.length > 1)
	{
		if(attackOptions.length > 0)
		{
			if(randNum >= 2)
			{
				action = "summon demons";
			}
			else
			{
				action = "cast spell";
			}
		}
		else // no attack options
		{
			if(randNum >= 1)
			{
				action = "summon demons";
			}
			else
			{
				action = "end turn";
			}
		}
	}
	else if(attackOptions.length > 0)
	{
		action = "cast spell";
	}
	else
	{
		action = "end turn";
	}
	
	if(action == "summon demons")
	{
		clearPanel();
		panelOut("The Beast summons demons");
		
		beastAnimation(4);
		
		var demonsSummoned = 0;
		
		for(i=0;i<enemyTeam.length;i++)
		{
			if(enemyTeam[i].name == "Demon" && enemyTeam[i].status == "dead")
			{
				enemyTeam[i].HP = enemyTeam[i].maxHP;
				enemyTeam[i].status = "ok";
				bothTeams.push(enemyTeam[i]);
				
				if(spaceOptions.length > 1)
				{
					shuffleArray(spaceOptions);
				}
				
				var spaceCoords = spaceOptions[0].spaceID.split("-");
				
				placeUnit(enemyTeam[i], spaceCoords[0], spaceCoords[1]);
				
				spaceOptions.shift();
				
				var scoreBoardId = "scoreBoardEnemyChar" + i;
				document.getElementById(scoreBoardId + "Hp").innerHTML = enemyTeam[i].HP;
				removeCssClass(scoreBoardId, "scoreBoardDeadChar");
				
				demonsSummoned++;
			}
			if(demonsSummoned > 1)
			{
				break;
			}
		}
		
		document.getElementById("endTurnButton").disabled = false;
	}
	else if(action == "cast spell")
	{
		var targetIndex = bothTeams.indexOf(attackOptions[0].target);
		turn.spell = attackOptions[0].method.spell;
		
		setSights("spell", targetIndex);
		startMagic(targetIndex);
	}
	else
	{
		// just make a scary face
		beastAnimation(2);
		etto = setTimeout("endTurn()", 300);
	}
}

theBeast.onDefeat = function()
{
	// when you beat The Beast the battle should be over

	var i;
	for(i=0;i<enemyTeam.length;i++)
	{
		enemyTeam[i].status = "dead";
	}
}



function beastAnimation(n)
{
	busy = true;
	charImg = document.getElementById(turn.going.elementId + "Img");

	// this is what does the attacking animation
	if(charImg.offsetLeft == 0)
	{
		charImg.style.left = (tileSize * -2) + "px";
	}
	else
	{
		charImg.style.left = "0px";
	}
	
	n--;
	if(n > 0)
	{
		bato = setTimeout("beastAnimation(" + n + ")", 100);
	}
	else
	{
		charImg.style.left = "0px";
		busy = false;
	}
}















}
catch(e)
{
	alert("An error has occurred in characters.js\n\n" + e);
}
