rem YA-LAXIANS
rem By Jim Shaw 27-8-2002

open window 640,512
clear window

repeat
	titles()
	initgame()
	game()
until (1=0)

sub flip()
	setdispbuf d
	d = 1-d
	setdrawbuf d
end sub

sub titles()
	repeat
		clear window
		setrgb 1,255,0,0
		text 320,50,"WE ARE THE YA-LAXIANS","cc"
		text 320,70,"MISSION: DESTROY ALIENS","cc"
		text 320,110,"- SCORE ADVANCE TABLE -","cc"
		setrgb 1,0,255,255
		text 320,140,"CONVOY   CHARGER","cc"

		for x = 1 to 4
			baddy(220,x*40+125,x)
			setrgb 1,0,255,255
			text 320,x*40+125,str$((7-x)*10)+"   "+str$((7-x)*20),"cc"
		next x
		text 320,350,"PRESS X TO START","cc"
		c = peek("port1")
		if (and(c,16384)<>0) return
		flip()
	until (1=0)
end sub

sub baddy(x,y,type)
	if (type = 1) then
		setrgb 1,255,255,0
	elsif (type = 2) then
		setrgb 1,255,0,0
	elsif (type = 3) then
		setrgb 1,255,0,255
		elseif (type = 4) then
		setrgb 1,0,255,255
	fi
	fill triangle x+6,y+4 to x+16,y+4 to x+11,y+18
	fill rectangle x,y+4 to x+22,y+6
	fill rectangle x,y+2 to x+2,y+4
	fill rectangle x+20,y+2 to x+22,y+4
	if (type <> 2) then
		setrgb 1,0,0,0
	else
		setrgb 1,255,255,0
	fi
	fill rectangle x+8,y+8 to x+10,y+10
	fill rectangle x+12,y+8 to x+14,y+10
	setrgb 1,0,0,255
	if (and(x/xinc,1) <> 0) then
		fill rectangle x+2,y+12 to x+4,y+18
		fill rectangle x+18,y+12 to x+20,y+18
	else
		fill rectangle x+4,y+12 to x+6,y+18
		fill rectangle x+16,y+12 to x+18,y+18
	fi
end sub

sub drawgoody(x,y)
	setrgb 1,255,0,0
	fill triangle x+12,y to x+2,y+8 to x+22,y+8
	setrgb 1,128,128,128
	fill rectangle x+10,y+10 to x+14,y+30
	setrgb 1,255,255,255
	fill rectangle x,y+20 to x+4,y+40
	fill rectangle x+20,y+20 to x+24,y+40
	setrgb 1,0,255,255
	fill rectangle x+5,y+20 to x+9,y+30
	fill rectangle x+15,y+20 to x+19,y+30
end sub

sub stats()
	setrgb 1,255,255,255
	line 480,0 to 480,512
	text 510,20,"Score:"
	text 510,35,str$(score)
	text 510,65,"Level:"
	text 510,80,str$(level)
	text 510,385,"Lives:"
	for x = 1 to lives-1
		drawgoody(480+x*30,400)
	next x
end sub

sub control()
	c = peek("port1")
	if (and(c,128)<>0) then
		gx = gx - 4
		if (gx < 0) gx = 0
	elsif (and(c,32)<>0) then
		gx = gx + 4
		if (gx > 480-24) gx=480-24
	fi
	if (and(c,16384)<>0 and bullets(1,4)=0) then
		bullets(1,4)=-9-level
		bullets(1,2)=gx+12
	fi
end sub

sub fire(x,y,xtra)
	for z = 2 to 10+xtra
		if (bullets(z,1) = 0) then
			bullets(z,1)=1
			bullets(z,2)=x
			bullets(z,3)=y
			bullets(z,4)=9+level
			return
		fi
	next z
end sub

sub startflying(x)
	if (numflying >= level) then
		groupdata(x,1)=int(ran(100))
		return
	fi
	numflying=numflying+1
	for y = 1 to groupdata(x,7)
		map(groupdata(x,6+y*2),groupdata(x,7+y*2),3)=1
		map(groupdata(x,6+y*2),groupdata(x,7+y*2),2)=5+ran(10)
	next y
	groupdata(x,2)=1
	groupdata(x,3)=ran()
	groupdata(x,4)=ran(480)-240
	groupdata(x,5)=xs+(groupdata(x,8)-1)*34
	groupdata(x,6)=50+(groupdata(x,9)-1)*24
end sub

sub stopflying(x)
	numflying=numflying-1
	for y = 1 to groupdata(x,7)
		map(groupdata(x,6+y*2),groupdata(x,7+y*2),3)=0
		map(groupdata(x,6+y*2),groupdata(x,7+y*2),2)=50+ran(100)
	next y
	groupdata(x,1)=int(ran(100))
	groupdata(x,2)=0
end sub

sub baddylogic()
	for y = 1 to 6
		for x = 1 to 10
			if (map(x,y,1) <> 0 and map(x,y,3) = 0) then
				map(x,y,2) = map(x,y,2) - 1
				if (map(x,y,2) < 0) then
					fire(xs+(x-1)*34+10,50+y*24,-6+min(level,6))
					map(x,y,2) = 50+ran(100)
				fi
			fi
		next x
	next y

	for x = 1 to ngroups
		if (groupdata(x,1) <> 0) then
			groupdata(x,1) = groupdata(x,1) - 1
			if (groupdata(x,1) = 0)	startflying(x)
		fi
		if (groupdata(x,2) <> 0) then
			groupdata(x,2) = groupdata(x,2) + 8
			if (groupdata(x,2) > 480)	stopflying(x)
		fi
	next x

end sub

sub drawbullets()
	if (bullets(1,4) = 0)	bullets(1,2)=gx+12
	setrgb 1,255,255,0
	fill rectangle bullets(1,2),bullets(1,3) to bullets(1,2)+2,bullets(1,3)+8
	bullets(1,3) = bullets(1,3) + bullets(1,4)
	if (bullets(1,3) < 0) then
		bullets(1,3)=450-8
		bullets(1,4)=0
	fi
	setrgb 1,255,255,255
	for x = 2 to 15
		if (bullets(x,1)<>0) then
			fill rectangle bullets(x,2),bullets(x,3) to bullets(x,2)+2,bullets(x,3)+8
			bullets(x,3) = bullets(x,3) + bullets(x,4)
			if (bullets(x,3) > 480) bullets(x,1)=0
		fi
	next x
end sub

sub drawgroup(x)
	bx = groupdata(x,5)+groupdata(x,4) * sin(groupdata(x,2)*groupdata(x,3)/40)
	by = groupdata(x,6)+groupdata(x,2)
	for i = 1 to groupdata(x,7)
		x1 = groupdata(x,6+(i*2))
		y1 = groupdata(x,7+(i*2))

		if (map(x1,y1,1)<>0) then
			dx = 34 * (x1 - groupdata(x,8))
			dy = 24 * (y1 - groupdata(x,9))
			if (bx+dx+24 > 480 or bx+dx < 0) then
				stopflying(x)
				return
			fi

			baddy(bx+dx,by+dy,map(x1,y1,1))
			map(x1,y1,2) = map(x1,y1,2) - 1
			if (map(x1,y1,2) < 0) then
				fire(bx+dx+10,by+dy+24,5)
				map(x1,y1,2) = 5+ran(10)
			fi
		fi
	next i
end sub

sub drawbaddies()
	yy = 50
	for y = 1 to 6
		xx = xs
		for x = 1 to 10
			if (map(x,y,1)<>0 and map(x,y,3)=0) baddy(xx,yy,map(x,y,1))
			xx = xx + 34
		next x
		yy = yy + 24
	next y

	for x = 1 to ngroups
		if (groupdata(x,2) <> 0) drawgroup(x)
	next x
end sub

sub drawstars()
	for x = 1 to 20
		setrgb 1,ran(128),ran(128),ran(128)
		fill rectangle stars(x,1),stars(x,2) to stars(x,1)+2,stars(x,2)+2
		stars(x,2) = stars(x,2)+1
		if (stars(x,2) > 480) then
			stars(x,2)=0
			stars(x,1)=ran(480)
		fi
	next x
end sub

sub bang(xp,yp)
	setrgb 1,255,0,0
	fill circle xp,yp-6,10
end sub

sub die(xp,yp)
	if (nodietime=0) then
		setrgb 1,255,0,0
		fill circle xp,yp,10
		lives = lives - 1
		bullets(1,4)=0
		bullets(1,3)=450-8
		gx=240-12
		nodietime=50
	fi
end sub

sub fixlimits()
	limitl = 0
	for x = 1 to 10
		c = 0
		for y = 1 to 6
			c = c + map(x,y,1)
		next y
		if (c = 0) then
			limitl = limitl - 34
		else
			goto dor
		fi
	next x
label dor
	limitr = 480+12
	for x = 10 to 1 step -1
		c = 0
		for y = 1 to 6
			c = c + map(x,y,1)
		next y
		if (c = 0) then
			limitr = limitr + 34
		else
			goto done
		fi
	next x
label done
end sub

sub collidegroup(x,gcx,gcy)
	bx = groupdata(x,5)+groupdata(x,4) * sin(groupdata(x,2)*groupdata(x,3)/40)
	by = groupdata(x,6)+groupdata(x,2)
	for i = 1 to groupdata(x,7)
		x1 = groupdata(x,6+(i*2))
		y1 = groupdata(x,7+(i*2))
		dx = 34 * (x1 - groupdata(x,8))
		dy = 24 * (y1 - groupdata(x,9))
		if (map(x1,y1,1)<>0) then
			if (gcx > bx+dx and gcx < bx+dx+22) then
				if (gcy > by+dy and gcy < by+dy+18) then
					map(x1,y1,1)=0
					bang(gcx,gcy)
					lessthan10k = 0
					if (score < 10000) lessthan10k = 1
					score = score + (7-(map(x1,y1,1))) * 20
					if (score >= 10000 and lessthan10k=1) lives = lives + 1
					alive = alive-1
					if (alive = 0) nextlevelcount=100
					fixlimits()
					bullets(1,4)=0
					bullets(1,3)=450-8
				fi
			fi
			if (bx+dx+22 > gx and bx+dx < gx+24) then
				if (by+dy+18 > 450 and by+dy < 490) then
					die(bx+dx+11,by+dy+9)
				fi
			fi
		fi
	next i
end sub

sub bulletcollision()
	bx = (bullets(1,2)-xs)
	by = (bullets(1,3)-50)

	if (mod(bx,34) < 22 and mod(by,24)<18) then
		bx = bx / 34
		by = by / 24
		if (bx > 0 and bx < 10 and by > 0 and by < 6) then
			if (map(bx+1,by+1,1) <> 0 and map(bx+1,by+1,3)=0) then
				bang(bullets(1,2),bullets(1,3))
				lessthan10k = 0
				if (score < 10000) lessthan10k = 1
				score = score + (7-(map(bx+1,by+1,1))) * 10
				if (score >= 10000 and lessthan10k=1) lives = lives + 1
				map(bx+1,by+1,1)=0
				alive = alive-1
				if (alive = 0) nextlevelcount=100
				fixlimits()
				bullets(1,4)=0
				bullets(1,3)=450-8
			fi
		fi
	fi

	for z = 1 to ngroups
		if (groupdata(z,2) <> 0) collidegroup(z,bullets(1,2),bullets(1,3))
	next z

	for z = 2 to 15
		if (bullets(z,1) <> 0) then
			bx = bullets(z,2)
			by = bullets(z,3)
			if (bx > gx and bx < gx+24) then
				if (by > 450 and by < 490) then
					die(bx,by)
				fi
			fi
		fi
	next z

end sub

sub game()
	repeat
		clear window
		stats()
		drawstars()
		if (nextlevelcount <> 0) then
			setrgb 1,255,255,255

			text 240,240,"Level : "+str$(level+1),"cc"
			nextlevelcount = nextlevelcount - 1
			if (nextlevelcount = 0) nextlevel()
		fi
		drawbaddies()
		if (and(nodietime,1)=0 and lives<>0) then
			drawgoody(gx,450)
		fi
		nodietime=nodietime-1
		if (nodietime < 0) nodietime = 0
		drawbullets()
		bulletcollision()
		xs = xs + xinc
		if (xs + 340 > limitr or xs < limitl) then
			xinc = -xinc
			xs = xs + xinc
		fi
		control()
		if (nextlevelcount = 0)	baddylogic()

		if (lives=0) then
			gx = -100000
			gameover=gameover-1
			text 240,240,"Game Over!","cc"
		fi

		flip()
	until (gameover<0)
end sub

sub initstars()
	dim stars(20,2)
	for x = 1 to 20
		stars(x,1)=ran(480)
		stars(x,2)=ran(480)
	next x
end sub

sub readmap()
	restore leveldata
	for y = 1 to 6
		for x = 1 to 10
			read map(x,y,1)				:rem type
			map(x,y,2)=50+ran(100)		:rem start to shoot time
			map(x,y,3)=0					:rem is flying
		next x
	next y
end sub

rem countdown,active,freq,amplitude,gx,gy,count,xy,xy,xy,xy
sub readgroups()
	restore groups
	read ngroups
	dim groupdata(ngroups,16)
	for x = 1 to ngroups
		groupdata(x,1)=int(ran(100))
		groupdata(x,2)=0
		groupdata(x,3)=0
		groupdata(x,4)=0
		groupdata(x,5)=0
		groupdata(x,6)=0
		read groupdata(x,7)
		for y = 1 to groupdata(x,7)
			read groupdata(x,8+(y-1)*2)
			read groupdata(x,9+(y-1)*2)
		next y
	next x
end sub

sub nextlevel()
	xs = 70
	alive=46
	readmap()
	clear window
	nextlevelcount = 0
	level=level+1
	numflying=0
	limitl = 0
	limitr = 480+12
end sub

sub initgame()
	while (peek("port1") <> 0): wend
	numflying=0
	level=0
	nextlevelcount=100
	lives=3
	gx = 240-12
	xs = 70
	limitl = 0
	limitr = 480+12
	score=0
	nodietime=50
	alive = 46
	xinc=2
	gameover=100
	dim bullets(15,4)
	bullets(1,1)=1
	bullets(1,3)=450-8
	bullets(1,4)=0
	for x = 2 to 15
		bullets(x,1)=0
		bullets(x,2)=0
		bullets(x,3)=0
		bullets(x,4)=0
	next x
	dim map(10,6,3)
	initstars()
	readgroups()
	clear window
end sub

label leveldata
data 0,0,0,1,0,0,1,0,0,0
data 0,0,2,2,2,2,2,2,0,0
data 0,3,3,3,3,3,3,3,3,0
data 4,4,4,4,4,4,4,4,4,4
data 4,4,4,4,4,4,4,4,4,4
data 4,4,4,4,4,4,4,4,4,4

label groups
data 16
data 4,4,1,3,2,4,2,5,2
data 4,7,1,6,2,7,2,8,2
data 2,2,3,3,3
data 2,4,3,5,3
data 2,6,3,7,3
data 2,8,3,9,3
data 1,1,4
data 1,2,4
data 1,3,4
data 1,4,4
data 1,5,4
data 1,6,4
data 1,7,4
data 1,8,4
data 1,9,4
data 1,10,4
  
