selectorx = 163
selectory = 38
selector = Image.load("./selector.png")
math.randomseed(os.time())
statpage = Image.load("./statpage.png")
current = 1
wildpokemon = 0
ballthrow = 0
chance = 0
d = 0
pkball = Image.load("./misc/pokeball.png")
pokeballx = 15
pokebally = 150
enemyx = 311
enemyy = 28
enemy = {}
enemy[1] = { lv = 0, name = "n/a", exp = 0, STR = 0,AGI = 0, DEF = 0, SPA = 0,SPD = 0, MAX_HP= 0, C_HP = 0, pic = Image.load("none.png"), picB = Image.load("none.png")}
enemy[1].lv = math.random(5)
e1_attacks = {}
e1_attacks[1] = {name = "n/a",attribute = "n/a", power = 0, accuracy = 0}
e1_attacks[2] = {name = "n/a",attribute = "n/a", power = 0, accuracy = 0}
e1_attacks[3] = {name = "n/a",attribute = "n/a", power = 0, accuracy = 0}
e1_attacks[4] = {name = "n/a",attribute = "n/a", power = 0, accuracy = 0}

exps = 0
attack = 0
levelstuff = 0
attackhelper = 0
y = 0
yi= 0
yii= 0
yiii= 0
selectorattx = 40
selectoratty = 183
selectory = 183
selectorx = 355
if battleplace == "route1 up" then
	R = math.random(50)
	if R <=25 then
		dofile("./pokemon/caterpie/caterpie_enemy.lua")
	end
	if R >25 and R<=40 then
		dofile("./pokemon/sentret/sentret_enemy.lua")
	end
	if R>40 and R<= 50 then
		dofile("./pokemon/poochyena/poochyena_enemy.lua")
	end
end
if battleplace == "route1 down" then
	R = math.random(100)
	if R > 50 then
		dofile("./pokemon/caterpie/caterpie_enemy.lua")
	end
	if R <= 50 then
		dofile("./pokemon/starly/starly_enemy.lua")
	end
end
randombattle = 0
counter = 0
battlescreen = Image.load("grassbattlescene.png")
turn = 0
battle = 0
a = 0
while battle == 0 do
if randombattle == 0 then
pad = Controls.read()
screen:blit(0,0,battlescreen)
screen:blit(88, 100, pokemon[current].picB)
screen:blit(enemyx, enemyy, enemy[1].pic)
screen:print(33,11,enemy[1].name, blue)
screen:print(15,20,enemy[1].C_HP.."/"..enemy[1].MAX_HP.."  lv"..enemy[1].lv, blue)
screen:print(393, 125, pokemon[current].name, blue)
screen:print(385, 140, pokemon[current].C_HP.."/"..pokemon[current].MAX_HP.."  lv"..pokemon[current].lv, blue)

if turn == 1 then
	if attackhelper == 0 then
	if e1_attacks[1].accuracy > 0 and y == 0 then
	attack = attack + 1
	y = 1
	end
	if e1_attacks[2].accuracy > 0 and yi == 0 then
	attack = attack +1	
	yi = 1
	end
	if e1_attacks[3].accuracy > 0 and yii == 0 then
	attack = attack +1
	yii = 1
	end
	if e1_attacks[4].accuracy > 0 and yiii == 0 then
	attack = attack +1	
	yiii = 1
	end
	attackhelper = 1
	end
	rattack = math.random(attack)
	dofile("./enemy attacks/"..e1_attacks[rattack].name..".lua")
end

if turn == 0 and a == 0 then
counter = 0
screen:print(374, 190, "FIGHT   PKMN", blue)

screen:print(374, 210, "ITEM     RUN", blue)
screen:blit(selectorx, selectory, selector)
screen:print(41,186, "What will you do?", blue)
if pad:up() and oldpad:up() ~= pad:up() and selectory > 190 then
	selectory = selectory -20
end
if pad:down() and oldpad:down() ~= pad:down() and selectory < 203 then
	selectory = selectory +20
end
if pad:right() and oldpad:right() ~= pad:right() and selectorx < 415 then
	selectorx = selectorx + 60
end
if pad:left() and oldpad:left() ~= pad:left() and selectorx > 355 then
 	selectorx = selectorx -60
end
if pad:r() and oldpad:r() ~= pad:r() then


	a = 1

	if selectory == 183 and selectorx == 415 then
		a = 2
	end
	if selectory == 203 and selectorx == 355 then
		a = 3
	end
	if selectory == 203 and selectorx == 415 then
		a = 4
	end
end

end

if a == 1 then
	counter = counter +1
	screen:print(55, 190, p_attacks[(current * 100) +1].name, blue)
	screen:print(172, 190, p_attacks[(current * 100) +2].name, blue)
	screen:print(55, 220," "..p_attacks[(current * 100) +3].name, blue)
	screen:print(172, 220," ".. p_attacks[(current * 100) +4].name, blue)
	screen:blit(selectorattx,selectoratty, selector)
	if pad:down() and oldpad:down() ~= pad:down() and selectoratty < 220 then
		selectoratty = selectoratty +30
	end
	if pad:up() and oldpad:up() ~= pad:up() and selectoratty > 190 then
		selectoratty = selectoratty -30
	end
	if pad:left() and oldpad:left() ~= pad:left() and selectorattx > 50 then
		selectorattx = selectorattx -117
	end
	if pad:right() and oldpad:right() ~= pad:right() and selectorattx < 172 then
		selectorattx = selectorattx +117
	end


	if pad:l() and oldpad:l() ~= pad:l() then
		a = 0
	end
	if pad:r() and oldpad:r() ~= pad:r() and counter >= 25 then
	if selectorattx == 40 and selectoratty == 183 then
	a = 5
	y = 1
	dofile("./attacks/" .. p_attacks[(current *100) +1].name..".lua")
	end	
	if selectorattx == 157 and selectoratty == 183 then
	a = 5
	y = 1
	dofile("./attacks/" .. p_attacks[(current *100) +2].name..".lua")
	end
	if selectorattx == 40 and selectoratty == 213 then	
	a = 5
	y = 1
	dofile("./attacks/" .. p_attacks[(current *100) +3].name .. ".lua")
	end
	if selectorattx == 157 and selectoratty == 213 then
	a = 5
	y = 1
	dofile("./attacks/" .. p_attacks[(current *100) +4].name .. ".lua")
	end
end
end
if a == 4 then
	randombattle = 4
	battle = 1
	r = 0
	rbattle = 0
	a = 1
end

if a == 2 then
	if pad:l() and oldpad:l() ~= pad:l() then
		a = 0
	end
end
if a == 3 then
	counter = counter +1
	if pad:l() then
		a = 0
	end
	itemlist = 1
	if counter > 5 then
		 dofile("./itemlist(battle).lua")
	end
end
if a == "pokecatch" then
screen:blit(pokeballx,pokebally,pkball)

if ballthrow == 0 and pokebally ~=77 then
	pokeballx = pokeballx + 4
	pokebally = pokebally - 1
end
if pokebally == 77 and ballthrow ==0 then
	enemyx = enemyx + 200


ballthrow = 1
end
if wildpokemon == "caught" and ballthrow == 1 then
	screen:blit(0,0,battlescreen)
	screen:blit(88, 100, pokemon[current].picB)
	screen:blit(enemyx, enemyy, enemy[1].pic)
	screen:print(33,11,enemy[1].name, blue)
	screen:print(15,20,enemy[1].C_HP.."/"..enemy[1].MAX_HP.."  lv"..enemy[1].lv, blue)
	screen:print(393, 125, pokemon[current].name, blue)
	screen:print(385, 140, pokemon[current].C_HP.."/"..pokemon[current].MAX_HP.."  lv"..pokemon[current].lv, blue)
	screen:print(55, 190, enemy[1].name .. " was caught.", blue)
	if pad:r() and oldpad:r() ~=pad:r() then
	randombattle = 4
	battle = 1
	r = 0
	rbattle = 0
	a = 1
	dofile("./randompokemon_get.lua")
	end
end


if wildpokemon == "notcaught1" then
	
	turn = 1
end
if wildpokemon == "notcaught1" then
	
	turn = 1
end
end
end
if randombattle == 1 then
	screen:blit(1,171,textbox)
	counter = counter + 1
	screen:print(55, 190, pokemon[current].name.." has fainted.", blue)
	if counter >= 15 then
		screen:clear()
		if pokemon[2].C_HP ~= 0 or pokemon[3].C_HP ~= 0 or pokemon[4].C_HP ~= 0 or pokemon[5].C_HP ~= 0 or pokemon[6].C_HP ~= 0 then
			dofile("./misc/pokeswitch.lua")
		else
			screen:print(55,205,pokemon[current].name.." was you're last pokemon...", blue)
			
			
				screen:print(55,220,"you have whited out...", blue)
				if counter >= 45 then
					dofile("./script2.lua")
				end
		end
	end
	
end
if randombattle == 2 then
	screen:blit(1,171,textbox)
	counter = counter +1
	screen:print(55,190, "enemy "..enemy[1].name.." has fainted", blue)
	exps=enemy[1].lv * 9
	screen:print(55,210, pokemon[current].name.." has gained "..exps.." experience.", blue)
	if levelstuff == 0 then
		pokemon[current].exp = pokemon[current].exp + exps
		exps = 0
		levelstuff = 1
	end
if counter >= 15 then
	if pokemon[current].exp > 44+ (13 +(7*(5- pokemon[current].lv))) then
		screen:blit(1,171,textbox)
		screen:print(55,190,pokemon[current].name.." has leveled up.", blue)
		if counter > 30 then
			dofile("./pokemon/"..pokemon[current].name.."/"..pokemon[current].name.."levelup.lua")
			randombattle = 4
			pokemon[current].exp = 0
			battle = 1
			r = 0
			rbattle = 0
			a = 1
		end
	else
		randombattle = 4
		battle = 1
		r = 0
		rbattle = 0
		a = 1
	end
end
end



--screen:print(240,135,battleplace,blue)
--screen:print(150,150,R,blue)
--screen:print(240,140, selectory, blue)
screen.waitVblankStart()
screen.flip()
oldpad = pad
end