--Hundir la flota por Jorge_97
--2012

os.cpu(333)

x_o=font.load("GAMES/battle/font.pgf")

p1={
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
}

p2={
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
}


turn=timer.new()

function mapa1()
for y=1, 5 do

	for x=1, 12 do
		if y==posy and x==posx and multiplayer then draw.rect(30+30*x,70+30*y,25,25,negro) else draw.rect(30+30*x,70+30*y,25,25,azul) end
		if p2[y][x]=="b" then screen.print(x_o,36+30*x,80+30*y,"O",verde) end
		if p2[y][x]=="c" then screen.print(x_o,36+30*x,80+30*y,"X",rojo) end
	end
end	
end

function mapa2()
for y=1, 5 do
	for x=1, 12 do
		if y==posy and x==posx then draw.rect(30+30*x,70+30*y,25,25,negro) else draw.rect(30+30*x,70+30*y,25,25,blanco) end
		if p1[y][x]=="b" then screen.print(x_o,36+30*x,80+30*y,"O",verde) end
		if p1[y][x]=="c" then screen.print(x_o,36+30*x,80+30*y,"X",rojo) end
	end
end	
end

function mi_turno()
control=0
localiz=false
cambio=false

if controls.triangle() then 
mapa1()
else
mapa2()
end


if turn:time()<10 then
if controls.press("cross") and p1[posy][posx]=="a" then
cambio=true
if j1[posy][posx]=="b" then p1[posy][posx]="b" contador_1=contador_1+1 end
if j1[posy][posx]!="b" then p1[posy][posx]="c" end
end

if controls.press("left") and posx>1 then posx=posx-1 elseif controls.press("right") and posx<12 then posx=posx+1 end
if controls.press("up") and posy>1 then posy=posy-1 elseif controls.press("down") and posy<5 then posy=posy+1 end

if cambio then turn:start() end
end

end

function tu_turno()
if controls.triangle() then 
mapa2()
else
mapa1()
end

if turn:time()<10 then
if controls.press("cross") and p2[posy][posx]=="a" then
cambio=true
if j2[posy][posx]=="b" then p2[posy][posx]="b" contador_2=contador_2+1 end
if j2[posy][posx]!="b" then p2[posy][posx]="c" end
end

if controls.press("left") and posx>1 then posx=posx-1 elseif controls.press("right") and posx<12 then posx=posx+1 end
if controls.press("up") and posy>1 then posy=posy-1 elseif controls.press("down") and posy<5 then posy=posy+1 end

if cambio then turn:start() end
end

end

function rival()
mapa1()
turn:start()
if turn:time()<2 then
if not discover then

while true do
x=math.random(1,12)
y=math.random(1,5)
if p2[y][x]=="a" then break end
end

if j2[y][x]=="b" then p2[y][x]="b" dx=x dy=y discover=true contador_2=contador_2+1 return end
if j2[y][x]=="a" then p2[y][x]="c" return end
else
if dy<5 and j2[dy+1][dx]=="b" and p2[dy+1][dx]=="a" then p2[dy+1][dx]="b" dy=dy+1 contador_2=contador_2+1 return end
if dy>1 and j2[dy-1][dx]=="b" and p2[dy-1][dx]=="a" then p2[dy-1][dx]="b" dy=dy-1 contador_2=contador_2+1 return end
if dx<11 and j2[dy][dx+1]=="b" and p2[dy][dx+1]=="a" then p2[dy][dx+1]="b" dx=dx+1 contador_2=contador_2+1 return end
if dx>1 and j2[dy][dx-1]=="b" and p2[dy][dx-1]=="a" then p2[dy][dx-1]="b" dx=dx+1 contador_2=contador_2+1 return end
discover=false
end
end
end

function game()
while true do
draw.gradrect(0,0,480,272,sky,sky2,sky,sky2)
controls.read()

if turno>2 then turno=1 end
if turno==1 then screen.print(160,30,"Jugador 1",1,azul,blanco) mi_turno() end
if turno==2 then
if multiplayer then tu_turno() screen.print(160,30,"Jugador 2",1,azul,blanco) else screen.print(160,30,"COM",1,azul,blanco) rival() end
end

if turn:time()>1500 then posy=1 posx=1 turno=turno+1 turn:reset() turn:stop() end


screen.flip()
if controls.press("start") then main() end

if multiplayer then
if contador_1==10 then os.message("Jugador 1 ha ganado") main() end
if contador_2==10 then os.message("Jugador 2 ha ganado") main() end
else 
if contador_1==10 then os.message("Has ganado") main() end
if contador_2==10 then os.message("COM ha ganado") main() end
end

end
end

function reiniciar_b()
plantilla={
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
}
dir=1
barco=1
ship={}
ship[1]=false
ship[2]=false
ship[3]=false
casillas=5
end

function colocar()
moment=true
if controls.press("cross") and ship[barco]==false then 
	for i=0,casillas-1 do
		if dir==1 then
			if plantilla[posy][posx+i]!="a" then moment=false end
		else 
			if plantilla[posy+i][posx]!="a" then moment=false end
		end
	end
if moment then 
	for i=0,casillas-1 do
		if dir==1 then plantilla[posy][posx+i]="b" else plantilla[posy+i][posx]="b" end
	end
	ship[barco]=true
end
end
end

function terminado()
seguro=os.message("¿Deseas establecer estas posiciones para tu flota?",1)
if seguro==1 then 
	if player==1 then j2=plantilla if multiplayer==false then dofile("GAMES/battle/niveles.lua") game() end end
	if player==2 then j1=plantilla game() end
reiniciar_b()
player=player+1
end
end

function battle_colo()

p1={
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
}

p2={
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
{"a","a","a","a","a","a","a","a","a","a","a","a"},
}

player=1
reiniciar_b()
posx=1
posy=1
turno=1
contador_1=0
contador_2=0
discover=false
dx=""
dy=""
cambio=false
while true do
controls.read()
draw.gradrect(0,0,480,272,sky,sky2,sky,sky2)
screen.print(160,10,"Jugador" ..player,0.8,azul,sky)

if dir==1 then screen.print(160,30,"Dirección: Horizontal",0.8,azul,sky) else screen.print(160,30,"Dirección: Vertical",0.8,azul,sky) end

if ship[barco] then 
	screen.print(160,55,"Barco "..barco.." ("..casillas.." casillas)".."\n\nColocado",0.8,azul,sky)
else
	screen.print(160,55,"Barco "..barco.." ("..casillas.." casillas)".."\n\nNo Colocado",0.8,azul,sky)
end

if dir>2 then dir=1 end
if controls.press("r") then dir=dir+1 end


if controls.press("l") then barco=barco+1 end
if barco>3 then barco=1 end
if barco==1 then casillas=5 end
if barco==2 then casillas=3 end
if barco==3 then casillas=2 end

colocar()

for y=1, 5 do
	for x=1, 12 do
		if y==posy and x==posx then draw.rect(30+30*x,70+30*y,25,25,negro) else draw.rect(30+30*x,70+30*y,25,25,blanco) end
		if plantilla[y][x]=="b" then screen.print(x_o,36+30*x,80+30*y,"O",verde) end
	end
end	

if dir==1 then
if posx>13-casillas then posx=13-casillas end
if posy>5 then posy=5 end 
else
if posy>6-casillas then posy=6-casillas end
if posx>12 then posx=12 end 
end

if controls.press("left") and posx>1 then posx=posx-1 elseif controls.press("right") then posx=posx+1 end
if controls.press("up") and posy>1 then posy=posy-1 elseif controls.press("down") then posy=posy+1 end

screen.flip()

if controls.press("start") and ship[1] and ship[2] and ship[3] then terminado() end
	if controls.press("square") then 
		reiniciar_b()
	end
end
end

function main()
liberar(7)
elegida=1
bala={x=50,y=10,d=false}

while true do
controls.read()
mainb:blit(0,0)

if elegida==1 then 
bala.y=55
shipimg:blit(25,bala.y) 
if bala.x>110 then multiplayer=true battle_colo() end
end

if elegida==2 then 
bala.y=135
shipimg:blit(30,bala.y) 
if bala.x>130 then multiplayer=false battle_colo() end
end
if elegida==3 then 
bala.y=208
shipimg:blit(25,bala.y) 
if bala.x>175 then sistema() end
end

if bala.d then cañon:blit(bala.x,bala.y) bala.x=bala.x+1 else
if controls.press("down") then elegida=elegida+1 elseif controls.press("up") then elegida=elegida-1 end
if controls.press("cross") then snd:play() bala.d=true end
end

if elegida>3 then elegida=1 end
if elegida<1 then elegida=3 end

screen.flip()
end
end