'NUMBEZZLED.BAS ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#define NUMBEZZLED_VERSION "0.9.2."+osgl_builddate

'http://www.darkphear.com

'http://cmcgames.blogspot.com

'Copyright (C) Cleber de Mattos Casali. All rights reserved.


'This file is released under the terms of the GNU General Public License (GPL) as
'published by the Free Software Foundation, GPL version 2 or any later version.


'This game was developed specially for the FBGD Numbers Competition 18/08/2014 (deadline for submission).


'FreeBASIC is an excellent, fast and completely free, open-source BASIC compiler.
'Check it at http://www.freebasic.net


#define _osgl_appfullname_ "Numbezzled"
'#define _osgl_gamestyle_arcade_
#define _osgl_gamestyle_blocks_

'CHEATS / DEBUG
'#define _debug_

#ifdef _debug_
	#define _osgl_debug_		'show debug messages
	#define _osgl_nomusic_ 	'disable all music
	'#define _osgl_debugfps_	'show FPS and memory usage
	'#define _osgl_nodebug_
	'#define _osgl_nosound_
	'#define _osgl_xscreen_ 800
	'#define _osgl_yscreen_ 600
	'#define _osgl_windowed_
	'#define _osgl_noconsole_
#endif

#include once "osgl-lite3.bas"

#ifdef _debug_
	#define notitle 			 'disable the title screen
	#define nosplash 			 'disable all splash screens
#endif

#define cheatkeys

dim shared cheat as integer=0
#ifdef _debug_
	cheat=1
#endif

'_osgl_bordertype=1 'green menu borders

_osgl_menuscale=.4 'menu and dialog scale

_osgl_textboxoffset=-5 'offset textbox

const as integer rndmatrixsize=1000

dim shared as double rndmatrix(1 to rndmatrixsize)

#macro rndSort()
	for iii as integer=1 to rndmatrixsize
		rndmatrix(iii)=rnd
	next iii
#endmacro

dim as integer burncount=0

dim shared as integer mousemove=0

const as integer id_nothing=0
const as integer id_wall=-999
const as integer id_rock=1001
const as integer id_bomb=1002 'pavio u
const as integer id_urocket=1003 'pavio l
const as integer id_lrocket=1004 'pavio r
const as integer id_rrocket=1005 'pavio d
const as integer id_nuke=1006
const as integer id_medusa=1007
const as integer id_surprise=1008
const as integer id_randomizer=1009
const as integer id_bird1=1011
const as integer id_bird2=1012
const as integer id_birdgone=1013

dim as integer diflevel_one
dim as integer diflevel_bomb
dim as integer diflevel_nuke
dim as integer diflevel_reduce2
dim as integer diflevel_rocket
dim as integer diflevel_reduce3
dim as integer diflevel_surprise
dim as integer diflevel_rock
dim as integer diflevel_randomizer
dim as integer diflevel_medusa


'cria players - dados do jogo
type playertype
	xaxis as double=0
	yaxis as double=0
	lrotate as integer=0
	rrotate as integer=0
	upkey as integer=0
	score as longint=0
	scoredisplay as double=0
	combo as integer=0
	timecount as longint=0
	level as integer=0
	xp as longint=0
	xpnext as longint=0
	speed as double=0.1
	pow as integer=0
	xoffset as integer=0
	yoffset as integer=-15
	scale as double=1.0
	gameover as integer=0
	depositdelay as integer=0
	below as integer=0
	dysum as double=0
	mousebut1 as integer=0
	mousebut2 as integer=0
	mousebut3 as integer=0
	mousewheel as integer=0
	rumble as integer=0
	rumblef as double=0
	rndindex as integer=0
	puzzlelevel as integer=0
	birds as integer=0
	shownext as integer=1
end type

'cria a table
type blocktype
	id as integer=0
	rlink as integer=0
	dlink as integer=0
	counted as integer=0
	burned as integer=0
	dy as double=0
	falling as integer=0
end type

dim shared emptyblock as blocktype
'dim shared dysum as double=0
dim shared as integer maxid=9
'dim shared as integer gameover=0

dim shared as integer numplayers=1
dim shared as integer pcur=1
dim shared as integer pother=2
dim shared as integer puzzlemode=0
dim shared as integer puzzlelevel=0
dim shared as integer puzzlesavelevel=1
dim shared as integer lastpuzzle=0

const as integer maxplayers=2
const as integer xmax=7
const as integer ymax=12
const as integer blocksize=14
const as integer xspeed=2
const as integer downspeed=4
const as integer scaleblocks=0
const as integer maxflybirds=18
const as integer tilesize=128*(scaleblocks+1)
const as integer maxpuzzle=99

dim shared as double blockscale=blocksize/tilesize

dim shared as double spacescroll=0

#macro clearPlayers()
	redim shared player(1 to maxplayers) as playertype
	if numplayers=1 then
		player(1).xoffset=-blocksize/2
	else
		player(1).xoffset=-58-blocksize/2
	end if
	player(2).xoffset=58-blocksize/2

#endmacro

dim shared as integer vswins(1 to maxplayers)

type flybirdtype
	exist as integer=0
	id as integer=id_bird1
	x as double
	y as double
	z as double
	dx as double
	dy as double
end type

dim as flybirdtype flybird(1 to maxflybirds) 

#macro newflybird(iid,xx,yy,dxx,dyy)
scope
	for iii as integer=1 to maxflybirds
		if flybird(iii).exist=0 then
			flybird(iii).exist=1
			flybird(iii).id=iid
			flybird(iii).x=xx
			flybird(iii).y=yy
			flybird(iii).z=0
			flybird(iii).dx=dxx
			flybird(iii).dy=dyy
			'osgl_textbox "new bird: "+str(flybird(iii).x)+","+str(flybird(iii).y)
			iii=maxflybirds 'sai
		end if
	next iii
end scope
#endmacro


#macro moveflybirds()
scope
if player(1).gameover=0 then
	for iii as integer=1 to maxflybirds
		if flybird(iii).exist then
			if getBlock(1,flybird(iii).x+sgn(flybird(iii).dx)*blocksize/2 - player(1).xoffset,flybird(iii).dy - player(1).yoffset - (blocksize/2))>0 then
			if getBlock(1,flybird(iii).x-sgn(flybird(iii).dx)*blocksize/2 - player(1).xoffset,flybird(iii).dy - player(1).yoffset - (blocksize/2))<1 then
				flybird(iii).dx=-.9*flybird(iii).dx
			end if
			end if
			flybird(iii).x+=flybird(iii).dx
			flybird(iii).y+=flybird(iii).dy
			flybird(iii).z-=1.5
			if abs(flybird(iii).x)>200 or abs(flybird(iii).y)>150 then 'sai da tela
				flybird(iii).exist=0
				if puzzlemode then
					if player(1).birds>1 then
						player(1).birds-=1
					else
						player(1).birds=0
						'drawScore()
						player(1).gameover=1
						'osgl_fadeout
						'osgl_cls
						'osgl_sync(1)
						'goto newgame
						'completa puzzle
					end if
				end if
			end if
		end if
	next iii
end if
end scope
#endmacro


#macro drawflybirds()
scope
	for iii as integer=1 to maxflybirds
		if flybird(iii).exist then
		
			dim as integer tmp=idFrame(flybird(iii).id)-1-((player(1).timecount\3) mod 2)
			
			osgl_Render(    flybird(iii).x, flybird(iii).y, 0.0, 0.0 ,_
                flybird(iii).z,_
                texture_tileset, tmp,_
                0,_
                1.25, 1.25,_
                0, 0,_
                blockscale, blockscale,_
                iif(flybird(iii).dx>0,1,0), 0,_
                1.0, 1.0, 0, 0,_
                1, 0,_
                rgba(255,255,255,255) , )
		
		end if
	next iii
end scope
#endmacro


#macro resetVsWins()
for iii as integer=1 to maxplayers
	vswins(iii)=0
next iii
#endmacro

clearPlayers()

dim shared table(1 to maxplayers, 0 to xmax+1, -2 to ymax+1) as blocktype

#macro clearTable()
for ppp as integer=1 to maxplayers

  for j as integer=lbound(table,3) to ubound(table,3)
  for i as integer=lbound(table,2) to ubound(table,2)
		table(ppp,i,j)=emptyblock
  next i
  next j

  for j as integer=lbound(table,3) to ubound(table,3)
		table(ppp,0,j).id=id_wall
		table(ppp,xmax+1,j).id=id_wall
  next j

  for i as integer=lbound(table,2) to ubound(table,2)
		table(ppp,i,ymax+1).id=id_wall
  next i

next ppp
#endmacro



clearTable()

#macro countPuzzles()
scope

dim as string puzzlefile=gamepath+"/data/puzzle.dat"
dim as string puzzlebuffer=""

printdebug "loading puzzle file: "+puzzlefile

puzzlebuffer=osgl_loadstring(puzzlefile)

for iii as integer=1 to maxpuzzle
	if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(iii),"exist","0")="1" then
		lastpuzzle=iii
	end if
next iii

end scope
#endmacro



#macro loadPuzzle()
scope

clearTable()

'load puzzle
player(1).birds=0
dim as string puzzlefile=gamepath+"/data/puzzle.dat"
dim as string puzzlebuffer=""

printdebug "loading puzzle file: "+puzzlefile

puzzlebuffer=osgl_loadstring(puzzlefile)

for iii as integer=1 to maxpuzzle
	if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(iii),"exist","0")="1" then
		lastpuzzle=iii
	end if
next iii

if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"one","0")="0" then
	diflevel_one=9999
else
	diflevel_one=1
end if

if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"bomb","0")="0" then
	diflevel_bomb=9999
else
	diflevel_bomb=1
end if

if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"rocket","0")="0" then
	diflevel_rocket=9999
else
	diflevel_rocket=1
end if

if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"nuke","0")="0" then
	diflevel_nuke=9999
else
	diflevel_nuke=1
end if

if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"rock","0")="0" then
	diflevel_rock=9999
else
	diflevel_rock=1
end if

if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"surprise","0")="0" then
	diflevel_surprise=9999
else
	diflevel_surprise=1
end if

if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"randomizer","0")="0" then
	diflevel_randomizer=9999
else
	diflevel_randomizer=1
end if

if INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"medusa","0")="0" then
	diflevel_medusa=9999
else
	diflevel_medusa=1
end if

maxid=val(INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"maxid","7"))

player(1).shownext=val(INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"shownext","1"))

player(1).level=val(INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"level","5"))-1
newLevel()

currentbackdrop=val(INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"backdrop","2"))
osgl_wrap(currentbackdrop,1,maxbackdrop)


dim puzzleline(1 to ymax) as string*xmax

pcur=1
for j as integer=ymax to 1 step -1
  
	puzzleline(j)=INIGetString(puzzlebuffer,"NUMBEZZLED_PUZZLE"+str(player(1).puzzlelevel),"line"+str(ymax-j+1),"       ")
  
	for i as integer=1 to xmax
		table(1,i,j).id=char2id(mid(puzzleline(j),i,1))
		if table(1,i,j).id=id_bird1 or table(1,i,j).id=id_bird2 then player(1).birds+=1
	next i
	
next j

for j as integer=1 to ymax
for i as integer=1 to xmax

	linkBlock(i,j)
	linkBlock(i,j-1)

next i
next j

end scope
#endmacro


dim shared as blocktype tmpblock


'falling block(s)
type fallblocktype
	id as integer=0
	idu as integer=0
	idl as integer=0
	idr as integer=0
	iddx as double=0
	iddy as double=0
	idudx as double=0
	idudy as double=0
	idldx as double=0
	idldy as double=0
	idrdx as double=0
	idrdy as double=0
	nextid as integer=0
	nextidu as integer=0
	rotation as integer=0
	x as double=0
	xd as double=0
	y as double=999
	yd as double=0
	mousedrag as integer=0
end type
dim shared fallblock(1 to maxplayers) as fallblocktype 

'hiscore table
type hiscoretype field=1
	score as longint=0
	playtime as longint=0
	level as integer=1
	playername as string*12="Numbezzled  "
end type
const as integer maxhiscore=9
dim shared hiscore(1 to maxhiscore+1) as hiscoretype 'ultima linha serve para adicionar score do jogador


const as integer xres=320, yres=200
const as integer syncrate=60

osgl_init _osgl_appfullname_ + " (C) Cleber M Casali",xres,yres,syncrate


dim shared as string hiscorefile

dim shared as string gamepath

gamepath=exepath()
hiscorefile=exepath()+"/hi.sav"

#ifndef __FB_WIN32__
	if gamepath="/usr/bin" or gamepath="/usr/games" then  'detecta instalação e ajusta diretorios de acordo
		gamepath="/usr/share/"+appname()
		hiscorefile=osgl_homedir+".numbezzled-hi.sav"
	end if
#endif

dim shared as integer filenum=0

#macro savehiscore()
	printdebug "saving hiscore table."
    hiscore(maxhiscore+1).level=puzzlesavelevel
    printdebug "puzzle level saved="+str(hiscore(maxhiscore+1).level)
	filenum=freefile
    open hiscorefile for binary Access Write as #filenum
       put #filenum,,hiscore()
    close #filenum
    filenum=0
#endmacro

#macro loadhiscore()
 if dir(hiscorefile)<>"" then
	printdebug "loading hiscore table."
	filenum=freefile
    open hiscorefile for binary Access Read as #filenum
       get #filenum,,hiscore()
    close #filenum
    printdebug "puzzle level loaded="+str(hiscore(maxhiscore+1).level)
    puzzlesavelevel=hiscore(maxhiscore+1).level
    if puzzlesavelevel<1 then puzzlesavelevel=1
    filenum=0
  else
	'default hiscores
	printdebug "creating hiscore table."
	for i as integer=1 to maxhiscore
		hiscore(i).score=(10-i)*20
		hiscore(i).playtime=(10-i)*600
		hiscore(i).level=1+((10-i)\2)
		hiscore(i).playername="Numbezzled"
	next i
	savehiscore()
  end if
#endmacro

loadhiscore()

printdebug "hiscore table:"
for i as integer=1 to maxhiscore
	printdebug hiscore(i).playername; hiscore(i).score; osgl_frames2minutes(hiscore(i).playtime,0); hiscore(i).level
next i


sub tableMessage(byval xx as integer=0, byval yy as integer=0, byval message as string)
osgl_particle(    player(pcur).xoffset + blocksize/2 + xx, player(pcur).yoffset + 80 + yy, 0 ,_
                        0,_
                        0.7, 1.1, .0025, .0025,_
                        0, 0, 240,_
                        0, 0, 0, -0.04, ,_
                        0 , 0,_
                        0, 0,_
                        1,_
                        255, 255, 0, 255, 1, message)
end sub

function idFrame(byval id as integer) as integer
select case id
	case 1 to 999: return (id-1)
	case id_rock: return 9
	case id_bomb: return 10
	case id_urocket: return 11
	case id_lrocket: return 12
	case id_rrocket: return 13
	case id_nuke: return 20
	case id_medusa: return 21
	case id_surprise: return 22
	case id_randomizer: return 23
	case id_bird1: return 26
	case id_bird2: return 29
	case id_wall: return 30
	case id_birdgone: return 31
	case else: return -1
end select
end function

dim as double xdrawscale=1.0, ydrawscale=1.0
dim as uinteger drawcolor=rgba(255,255,255,255)
dim as integer drawmirror=0


dim shared as integer sound_bird1, sound_shot1, sound_shot2, sound_life, sound_hurt1, sound_hurt2, sound_explo1, sound_explo2, sound_pac, sound_coin1, sound_click, sound_beep, sound_lit, sound_item1, sound_zeum

const as integer bomb_channel=8

#macro loadsounds()
'load sounds

'osgl_loadsound(sound_jump,gamepath+"/sound/jump1.wav")
'osgl_loadsound(sound_mola,gamepath+"/sound/mola1.wav")
'osgl_loadsound(sound_bummer,gamepath+"/sound/hurt2.wav")
'osgl_loadsound(sound_tru,gamepath+"/sound/tru.wav")
'osgl_loadsound(sound_shotw,gamepath+"/sound/shotw1.wav")
'osgl_loadsound(sound_chpoint,gamepath+"/sound/chpoint.wav")
'osgl_loadsound(sound_icebrk,gamepath+"/sound/icebrk.wav")

osgl_loadsound(sound_beep,gamepath+"/sound/beep1.wav")
osgl_loadsound(sound_click,gamepath+"/sound/click1.wav")
osgl_loadsound(sound_pac,gamepath+"/sound/pac.wav")
osgl_loadsound(sound_shot1,gamepath+"/sound/shot1.wav")
osgl_loadsound(sound_shot2,gamepath+"/sound/shot2.wav")
osgl_loadsound(sound_hurt1,gamepath+"/sound/hurt1.wav")
osgl_loadsound(sound_hurt2,gamepath+"/sound/hurt2.wav")
osgl_loadsound(sound_coin1,gamepath+"/sound/coin1.wav")
osgl_loadsound(sound_lit,gamepath+"/sound/lit.wav")
osgl_loadsound(sound_explo1,gamepath+"/sound/explo1.wav")
osgl_loadsound(sound_explo2,gamepath+"/sound/explo2.wav")
osgl_loadsound(sound_life,gamepath+"/sound/life.wav")
osgl_loadsound(sound_zeum,gamepath+"/sound/zeum.wav")
osgl_loadsound(sound_item1,gamepath+"/sound/item1.wav")
osgl_loadsound(sound_bird1,gamepath+"/sound/bird1.wav")

osgl_setmenusounds(sound_click,sound_beep)

#endmacro


#macro playMusic(level)
osgl_stopmusic
select case (level mod 6) 'mod 6: 6 musicas
	case 1 : osgl_loadmusic(gamepath+"/music/musopen-org/Sousa - king_cotton.ogg")
	case 2 : osgl_loadmusic(gamepath+"/music/musopen-org/Sousa - sound_off_so.ogg")
	case 3 : osgl_loadmusic(gamepath+"/music/musopen-org/Sousa - with_pleasure (dance hilarious).ogg")
	case 4 : osgl_loadmusic(gamepath+"/music/musopen-org/Sousa - HandsAcrosstheSea.ogg")
	case 5 : osgl_loadmusic(gamepath+"/music/musopen-org/Sousa - sabre_and_spurs.ogg")
	case else : osgl_loadmusic(gamepath+"/music/musopen-org/Sousa - the_fairest_of_the_fair.ogg")
end select
osgl_playmusic
#endmacro


splash:

#ifndef nosplash

	playMusic(3)

	osgl_fadein
	do
		osgl_box (-160,-100,160,100, 0,_
			40,180,110,255 , 40,180,110,255, 255,125,155,255 , 225,125,155,255 )

		osgl_print 0,-20,0,"A Cleber de Mattos Casali Game",0.6,0.6,1,1,rgba(255,255,255,255)
		osgl_print 0,60,0,"Royalty free music downloaded from http://www.musopen.org"+CRLF+"composed by John Philip Sousa and performed by the U.S. Marine Band",0.4,0.5,1,1,rgba(255,255,255,255)
		osgl_sync
	loop until osgl_fadein_complete
#else

	osgl_box
	osgl_print 0,0,0,"LOADING",0.75,0.75,1,1,rgba(255,255,255,255)
	osgl_sync(1)

#endif

'load stuff here

dim shared as integer texture_tileset=0

osgl_loadtexturePNG(texture_tileset, gamepath+"/image/tile01.png", scaleblocks)

osgl_SetTextureFrameSize (texture_tileset, tilesize, tilesize)


dim shared as integer texture_tutorial=0

osgl_loadtexturePNG(texture_tutorial, gamepath+"/image/tut01.png", scaleblocks)

osgl_SetTextureFrameSize (texture_tutorial, 128, 128)


const as integer maxbackdrop=9

dim shared as integer tex_backdrop(1 to maxbackdrop)
dim shared as integer currentbackdrop=1

const as integer scalebackdrop=0

for iii as integer=1 to maxbackdrop
	osgl_loadtexturePNG(tex_backdrop(iii),gamepath+"/image/bk0"+str(iii)+".png",scalebackdrop)
next iii

loadsounds()

#ifndef nosplash
	osgl_delay 300
#endif

osgl_fadeout

goto title



#macro drawblock(id, xx, yy)

'osgl_Render(    x1, y1, xcenter, ycenter,_
'                z,_
'                texturenumber, frame,_
'                angle,_
'                xscale, yscale,_
'                xoffset, yoffset,_
'                xstretch, ystretch,_
'                mirror, flipp,_
'                xrep, yrep, xscroll, yscroll,_
'                blendmode, depthtest,_
'                colour, orientation)

drawmirror=0

select case id
	case 1
		xdrawscale= blockscale + .0025*sin(radians(player(pcur).timecount*(15-id)/4)) : drawcolor=idcolor(id)
		ydrawscale=xdrawscale
	case 2 to 9
		xdrawscale= blockscale + .0025*sin(radians(player(pcur).timecount*(15-id)/4)) : drawcolor=rgba(255,255,255,255)
		ydrawscale=xdrawscale
	case id_bird1, id_bird2, id_birdgone
		ydrawscale=1.25*blockscale + .004*sin(radians(player(pcur).timecount*3)) : drawcolor=rgba(255,255,255,255)
		xdrawscale=1.25*blockscale
		drawmirror=cint((xx)+(yy))\blocksize mod 2
	case else
		xdrawscale=blockscale : drawcolor=rgba(255,255,255,255)
		ydrawscale=xdrawscale
end select

osgl_Render(    xx, yy+blocksize/2, 0.0, 0.0 ,_
                0,_
                texture_tileset, idFrame(id),_
                0,_
                1.0, 1.0,_
                0, 0,_
                xdrawscale, ydrawscale,_
                drawmirror, 0,_
                1.0, 1.0, 0, 0,_
                1, 0,_
                drawcolor , )

#endmacro


#macro drawTable()

	'bomb rumble
	if player(pcur).rumble>0 then
		player(pcur).rumble-=1
		player(pcur).rumblef=player(pcur).rumble*sin(2*player(pcur).timecount)/2
	else 
		player(pcur).rumblef=0
	end if

	osgl_box (player(pcur).xoffset + blocksize*(0-(xmax)/2), player(pcur).yoffset + blocksize*(1-(ymax+1)/2) +player(pcur).rumblef ,player(pcur).xoffset + blocksize*((xmax+1)-(xmax)/2), player(pcur).yoffset + blocksize*((ymax+2)-(ymax+1)/2) +player(pcur).rumblef , _
				1,_
				255,150+100*sin(degrees(player(pcur).timecount/6000)),255,125,_
				255,255,150+100*cos(degrees(player(pcur).timecount/7000)),125,_
				255,150+100*sin(degrees(player(pcur).timecount/4000)),255,125,_
				255,150+100*cos(degrees(player(pcur).timecount/5000)),150+100*sin(degrees(player(pcur).timecount/8000)),125)

	osgl_box (player(pcur).xoffset + blocksize*(.5-(xmax)/2), player(pcur).yoffset + blocksize*(1.5-(ymax+1)/2) +player(pcur).rumblef ,player(pcur).xoffset + blocksize*((xmax+.5)-(xmax)/2), player(pcur).yoffset + blocksize*((ymax+1.5)-(ymax+1)/2)+player(pcur).rumblef , _
				1,_
				25,15+10*sin(degrees(player(pcur).timecount/6000)),25,200,_
				25,15+10*cos(degrees(player(pcur).timecount/5000)),15+10*sin(degrees(player(pcur).timecount/8000)),200,_
				25,15+10*sin(degrees(player(pcur).timecount/4000)),25,200,_
				25,25,15+10*cos(degrees(player(pcur).timecount/7000)),200)

	for i as integer=1 to xmax
	  for j as integer=lbound(table,3) to ymax
		if table(pcur, i, j).id<>0 then
			'osgl_print player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) + table(pcur, i, j).dy, 0, id2char(table(pcur, i, j).id),1.0,1.0,1,1,idcolor(table(pcur, i, j).id)
			drawblock (table(pcur, i, j).id, player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) + table(pcur, i, j).dy + player(pcur).rumblef )
			
			'draw links
			if table(pcur, i, j).id<10 then
			if table(pcur, i, j).dy=0 then


				if table(pcur, i+1, j).dy=0 then
				if table(pcur, i, j).rlink then
				'osgl_print player(pcur).xoffset + blocksize*((i+.5)-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2), 0, "-",1.0,1.0,1,1,idcolor(table(pcur, i, j).id,63)
					osgl_sprite(    player(pcur).xoffset + blocksize*(i-(xmax)/2)+blocksize/2, player(pcur).yoffset + blocksize*(j-(ymax)/2) + table(pcur, i, j).dy + player(pcur).rumblef + blocksize/2, 0,_
					texture_tileset, 18, ,_
					80+20*rnd,blockscale, blockscale,0.8+.2*rnd,1.2+.4*rnd,_
					cint(rnd), cint(rnd),_
					0.0, 0.0,_
					3, 0,_
					idcolor(table(pcur, i, j).id,150))

				end if
				end if

'sub osgl_sprite(    byval x as single,byval y as single,byval z as single=0,_
 '               byval texturenumber as integer,byval frame as integer=0,byval prio as integer=0,_
  '              byval angle as single=0,byval xscale as single=1.0,byval yscale as single=1.0,byval xstretch as single=1.0,byval ystretch as single=1.0,_
   '             byval mirror as integer=0,byval flipp as integer=0,_
    '            byval xoffset as single=0,byval yoffset as single=0,_
     '           byval blendmode as integer=1,byval depthtest as integer=0,_
		'byval colour as uinteger=rgba(255,255,255,255), byval orientation as integer=-1)

			
				if table(pcur, i, j+1).dy=0 then
				if table(pcur, i, j).dlink then
				'osgl_print player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*((j+0.5)-(ymax)/2), 0, "|",1.0,1.0,1,1,idcolor(table(pcur, i, j).id,63)


					osgl_sprite(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) + table(pcur, i, j).dy + player(pcur).rumblef + blocksize, 0,_
					texture_tileset, 18, ,_
					-10+20*rnd,blockscale, blockscale,1.2+.4*rnd,0.8+.2*rnd,_
					cint(rnd), cint(rnd),_
					0.0, 0.0,_
					3, 0,_
					idcolor(table(pcur, i, j).id,150))

				end if
				end if
				
			end if
			end if
			
		end if
		
		'rachadura
		if table(pcur, i, j).burned then
		if table(pcur, i, j).id<>id_birdgone then
		
				osgl_Render(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) + table(pcur, i, j).dy + player(pcur).rumblef + blocksize/2,  0.0, 0.0 ,_
                0,_
                texture_tileset, 19,_
                i*12+j*22,_
                blockscale, blockscale,_
                0, 0,_
                1.0, 1.0,_
                0, 0,_
                1.0, 1.0, 0, 0,_
                1, 0,_
                rgba(255,255,255,255) , )

		end if
		end if
		
	  next j
	next i

#endmacro


sub sorthiscores()
dim swapped as integer=0
do
  swapped=0
  for i as integer= 1 To maxhiscore
    if hiscore(i).score<hiscore(i+1).score then swap hiscore(i), hiscore(i+1): swapped=1
  next i
loop until swapped=0
end sub


#macro checkgameover()

if player(pcur).gameover then

	player(pcur).gameover+=1

	if player(pcur).gameover=30 then
	if player(pother).gameover=0 then
		vswins(pother)+=1
	end if
	end if

	if numplayers=1 then
		if puzzlemode then
			if player(1).birds>0 then
				osgl_print player(pcur).xoffset+blocksize/2, player(pcur).yoffset-30, 0, "FAIL",1.5,1.5,1,1,rgba(255,25,25,255)
			else
				osgl_print player(pcur).xoffset+blocksize/2, player(pcur).yoffset-30, 0, "SUCCESS",1.5,1.5,1,1,rgba(25,255,255,255)
			end if
		else
			osgl_print player(pcur).xoffset+blocksize/2, player(pcur).yoffset-30, 0, "GAME OVER",1.5,1.5,1,1,rgba(255,25,25,255)
		end if
	else
		if player(pother).gameover<>0 then
			osgl_print player(pcur).xoffset+blocksize/2, player(pcur).yoffset-30, 0, "TIED",1.5,1.5,1,1,rgba(255,127,25,255)
		else
			osgl_print player(pcur).xoffset+blocksize/2, player(pcur).yoffset-30, 0, "LOSE",1.5,1.5,1,1,rgba(255,25,25,255)
			osgl_print player(pother).xoffset+blocksize/2, player(pother).yoffset-30, 0, "WIN",1.5,1.5,1,1,rgba(255,255,55,255)
		end if
	end if

	if player(pcur).gameover>60 then
	
		osgl_joyrumble(pcur,100,100,200)
		
		osgl_keypause()
		
		osgl_clearparticles()
		
		player(pcur).gameover=0
		
		if numplayers>1 then
			select case osgl_menu(0,50,,"Rematch,Quit")
				case 1
					goto newgame
				case else
					osgl_fadeout
					osgl_cls
					osgl_sync(1)
					goto title
			end select
		else
			if puzzlemode=0 then
			if player(1).score>hiscore(maxhiscore).score then 'new highscore new hiscore
				do
					osgl_sync
				loop until inkey=""
				hiscore(maxhiscore+1).playername=osgl_input ("New highscore! Insert your name:",12)
				if hiscore(maxhiscore+1).playername="" then hiscore(maxhiscore+1).playername="Numbezzled"
				hiscore(maxhiscore+1).score=player(1).score
				hiscore(maxhiscore+1).level=player(1).level
				hiscore(maxhiscore+1).playtime=player(1).timecount
				sorthiscores()
				savehiscore()
				osgl_fadeout
				osgl_cls
				osgl_sync(1)
				viewHighscores
			end if
			end if
			
			'puzzle retry
			if puzzlemode then
			
				if player(1).birds>0 then
					'fail
					if osgl_menu(0,0,,"Retry,Quit")=1 then
						osgl_fadeout
						osgl_cls
						osgl_sync(1)
						goto newgame
					end if
				else
					'success
					if puzzlelevel>=lastpuzzle then
						osgl_textbox "Congratulations! Please visit http://cmcgames.blogspot.com for game updates and new levels."
					end if
					if puzzlesavelevel=puzzlelevel then
						puzzlesavelevel+=1
					end if
					saveHiscore()
					osgl_fadeout
					osgl_cls
					osgl_sync(1)
					goto newgame
				end if
			
			end if

			osgl_fadeout
			osgl_cls
			osgl_sync(1)
			goto title
			
		end if
		
	end if

else

	for i as integer=1 to xmax
	  for j as integer=lbound(table,3) to 0
		if table(pcur, i, j).id<>0 then
		if table(pcur, i, j).burned=0 then
		if table(pcur, i, j).dy=0 then
			player(pcur).gameover+=1
		end if
		end if
		end if
	  next j
	next i

end if
	
#endmacro




#macro moveFallblock()

if player(pcur).dysum=0 then
	fallblock(pcur).y=fallblock(pcur).y+player(pcur).speed
end if

player(pcur).below=getBlock(pcur,fallblock(pcur).x,fallblock(pcur).y+blocksize)

if player(pcur).below=0 then 
	if fallblock(pcur).idl then
		player(pcur).below=getBlock(pcur,fallblock(pcur).x-blocksize,fallblock(pcur).y+blocksize)
	end if
	if fallblock(pcur).idr then
		player(pcur).below=getBlock(pcur,fallblock(pcur).x+blocksize,fallblock(pcur).y+blocksize)
	end if
end if

if player(pcur).below then

	player(pcur).depositdelay=10

	if fallblock(pcur).id then
		depositBlock(fallblock(pcur).x,fallblock(pcur).y,fallblock(pcur).id)
	end if
	if fallblock(pcur).idu then
		depositBlock(fallblock(pcur).x,fallblock(pcur).y-blocksize,fallblock(pcur).idu)
	end if
	if fallblock(pcur).idl then
		depositBlock(fallblock(pcur).x-blocksize,fallblock(pcur).y,fallblock(pcur).idl)
	end if
	if fallblock(pcur).idr then
		depositBlock(fallblock(pcur).x+blocksize,fallblock(pcur).y,fallblock(pcur).idr)
	end if

	newFallBlock()
	acomodaTable()

	osgl_joyrumble(pcur,10,10,150)
	osgl_playsound3d(sound_shot1,player(pcur).xoffset+fallblock(pcur).x,fallblock(pcur).y)

end if

if burn then
if player(pcur).dysum=0 then
	burnBlocks()
	acomodaTable()
end if
end if

if burnsound=1 then
	osgl_joyrumble(pcur,30,30,150)
	osgl_playsound3d(sound_hurt1,player(pcur).xoffset,0)
	burnsound=0
end if

if puzzlemode=0 then
if player(pcur).xp>=player(pcur).xpnext then
	newLevel()
end if
end if

#endmacro



dim shared as integer burnsound=0

#macro moveTable()

	player(pcur).dysum=0
				
	for i as integer=1 to xmax
	  for j as integer=lbound(table,3) to ymax
		if table(pcur, i, j).id<>0 then
			
			 select case table(pcur, i, j).dy
			   case 0
			   if table(pcur, i, j).falling then
				  table(pcur, i, j).falling=0
				  burnCheck(i,j)
			   end if
			   case is>=0
				 table(pcur, i, j).dy=0
		       case else
				table(pcur, i, j).dy+=downspeed
				player(pcur).dysum+=table(pcur, i, j).dy
			 end select
			
			if burn=0 then
				if table(pcur, i, j).burned then
					burn=1
					'burnsound=1
				end if
			end if
	
		end if
	  next j
	next i

#endmacro


function char2id(byval char as string) as integer
	select case char
		case " ": return 0
		case "2","3","4","5","6","7","8","9" : return val(char)
		case ">" : return id_rrocket
		case "<" : return id_lrocket
		case "^" : return id_urocket
		case "o" : return id_bomb
		case "r" : return id_rock
		case "#" : return id_wall
		case "b" : return id_bird1
		case "B" : return id_bird2
		case "S","s" : return id_surprise
		case else: return 0
	end select
end function



function id2char(byval id as integer=0) as string
select case id
	case 0: return " "
	case 1 to 9: return str(id)
	case id_rock: return "r"
	case id_rrocket: return ">"
	case id_lrocket: return "<"
	case id_urocket: return "^"
	case id_bomb: return "o"
	case id_nuke: return "N"
	case id_medusa: return "M"
	case id_surprise: return "S"
	case id_randomizer: return "?"
	case else : return "X"
end select
end function



function getBlock(byval player as integer=1,byval x as integer, byval y as integer) as integer

dim as integer xx=(x+(blocksize*xmax/2))\blocksize
dim as integer yy=(y+(blocksize*ymax/2))\blocksize

if xx>=lbound(table,2) then
if yy>=lbound(table,3) then
if xx<=ubound(table,2) then
if yy<=ubound(table,3) then
	return table(player,xx,yy).id
end if
end if
end if
end if

return id_wall

end function


#macro linkBlock(xx,yy)
	if table(pcur,xx,yy).id>0 then
	if table(pcur,xx+1,yy).id=table(pcur,xx,yy).id then table(pcur,xx,yy).rlink=1 else table(pcur,xx,yy).rlink=0
	if table(pcur,xx,yy+1).id=table(pcur,xx,yy).id then table(pcur,xx,yy).dlink=1 else table(pcur,xx,yy).dlink=0
	if table(pcur,xx-1,yy).id=table(pcur,xx,yy).id then table(pcur,xx-1,yy).rlink=1  else table(pcur,xx-1,yy).rlink=0
	if yy>ubound(table,3) then
		if table(pcur,xx,yy-1).id=table(pcur,xx,yy).id then table(pcur,xx,yy-1).dlink=1 else table(pcur,xx,yy-1).dlink=0
	end if
	end if
#endmacro



#macro acomodaTable()
for k as integer=1 to xmax*ymax
	
	'player(pcur).birds=0
	
	for i as integer=1 to xmax
	  for j as integer=lbound(table,3)+1 to ymax
  		
  		'if table(1,i,j).id=id_bird1 or table(1,i,j).id=id_bird2 then player(1).birds+=1
		
		if table(pcur, i, j).id=id_nothing then
		if table(pcur, i, j-1).id<>id_nothing then
			swap table(pcur, i, j), table(pcur, i, j-1)
			clearBlock(pcur,i,j-1)
			table(pcur, i, j).dy-=blocksize
			table(pcur, i, j).falling=1
			linkBlock(i,j)
			linkBlock(i,j-1)
		end if 
		end if 
		

	  next j
	next i

next k
#endmacro


#macro resetTableCounter()
  for j as integer=lbound(table,3) to ymax
	for i as integer=1 to xmax
		table(pcur,i,j).counted=0
	next i
  next j
#endmacro


#macro clearBlock(pp1,ii1,jj1)
	table(pp1, ii1, jj1)=emptyblock
	if ii1>1 then table(pp1, ii1-1, jj1).rlink=0
	if jj1>1 then table(pp1, ii1, jj1-1).dlink=0
#endmacro


#macro getScore(id)
	dim as integer tmp=player(pcur).combo
	if tmp<1 then tmp=1
	if id<10 then
		player(pcur).score+=id*tmp 'blocks
	else
		player(pcur).score+=5*tmp 'rocks, bombs, etc
	end if
#endmacro


#macro markBurnBlocks()
  for j as integer=lbound(table,3) to ymax
	for i as integer=1 to xmax
		if table(pcur,i,j).counted then
			table(pcur,i,j).burned=1
			getScore(table(pcur,i,j).id)
		end if
	next i
  next j
#endmacro



#macro checkBirds()
  for j as integer=1 to ymax
	for i as integer=1 to xmax
		if table(pcur,i,j).id=id_bird1 or table(pcur,i,j).id=id_bird2 then
		if table(pcur,i,j).burned=0 then
			if table(pcur,i,j-1).id=id_nothing then
				'bird foge up
				table(pcur,i,j).burned=1
				newflybird(table(pcur,i,j).id, (player(pcur).xoffset + blocksize*(i-(xmax)/2)), (player(pcur).yoffset + blocksize*(j-(ymax)/2)+ blocksize/2), (rnd-rnd)/2, -2.5+(rnd-rnd)/2)
				table(pcur,i,j).id=id_birdgone
			else
				if table(pcur,i-1,j).id=id_nothing then
					'bird foge left
					table(pcur,i,j).burned=1
					newflybird(table(pcur,i,j).id, (player(pcur).xoffset + blocksize*(i-(xmax)/2)), (player(pcur).yoffset + blocksize*(j-(ymax)/2)+ blocksize/2), -2+(rnd-rnd)/2, -1.5+(rnd-rnd)/2)
					table(pcur,i,j).id=id_birdgone
				else
					if table(pcur,i+1,j).id=id_nothing then
						'bird foge right
						table(pcur,i,j).burned=1
						newflybird(table(pcur,i,j).id, (player(pcur).xoffset + blocksize*(i-(xmax)/2)), (player(pcur).yoffset + blocksize*(j-(ymax)/2)+ blocksize/2), 2+(rnd-rnd)/2, -1.5+(rnd-rnd)/2)
						table(pcur,i,j).id=id_birdgone
					end if
				end if
			end if
		end if
		end if
	next i
  next j
#endmacro



#macro burnBlocks()
burncount=0
  for j as integer=lbound(table,3) to ymax
	for i as integer=1 to xmax
		
		if table(pcur,i,j).burned then
			
			if player(pcur).depositdelay=0 then 
			if table(pcur,i,j).falling=0 then
				table(pcur,i,j).burned+=1
			end if
			end if
			
			if table(pcur,i,j).burned>5 then
				player(pcur).xp+=1
				burncount+=1
				if burncount>6 then
					player(pcur).pow+=cint(burncount/4)
					tableMessage(0,-20,"GREAT MOVE!")

					osgl_joyrumble(pcur,30,30,150)
					osgl_playsound3d(sound_shot2,player(pcur).xoffset+fallblock(pcur).x,fallblock(pcur).y)

					if numplayers>1 then
						penaliza(pother, id_rock)
					end if

				end if



				select case table(pcur,i,j).id
					case id_bomb
						for iii as integer=-1 to 1
						for jjj as integer=-1 to 1
							if iii<>0 or jjj<>0 then
							if table(pcur,i+iii,j+jjj).id>0 then
							if table(pcur,i+iii,j+jjj).id<>id_bird1 then
							if table(pcur,i+iii,j+jjj).id<>id_bird2 then
							if table(pcur,i+iii,j+jjj).id<>id_birdgone then
								table(pcur,i+iii,j+jjj).burned=99
								getScore(table(pcur,i+iii,j+jjj).id)
							end if
							end if
							end if
							end if
							end if
						next jjj
						next iii

						osgl_playsound3d(sound_explo1,player(pcur).xoffset,0,0,bomb_channel)
						player(pcur).depositdelay=10
						player(pcur).rumble=25
						tableMessage(0,-30,"Bomb!")

						osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) +blocksize/2, 0 ,_
                        texture_tileset,_
                        0.1, 0.1, .15, .15,_
                        14, 16, 35,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        rnd*360, 15,_
                        1,_
                        255, 255, 255, 255, 18)

						osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) +blocksize/2, 0 ,_
                        texture_tileset,_
                        0.1, 0.1, .2, .2,_
                        14, 16, 30,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        rnd*360, 25,_
                        1,_
                        255, 255, 255, 255, 22)


					case id_rrocket
						for iii as integer=xmax+1 to i+1 step -1
							if table(pcur,iii,j).id>0 then
							if table(pcur,iii,j).id<>id_bird1 then
							if table(pcur,iii,j).id<>id_bird2 then
							if table(pcur,iii,j).id<>id_birdgone then
							if table(pcur,iii,j).id<>id_wall then
								table(pcur,iii,j).burned=99
								getScore(table(pcur,iii,j).id)
							end if
							end if
							end if
							end if
							end if
						next iii

						osgl_playsound3d(sound_explo1,player(pcur).xoffset,0,0,bomb_channel)
						player(pcur).depositdelay=10
						player(pcur).rumble=10
						tableMessage(0,-30,"Rocket! >")

						osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2) , player(pcur).yoffset + blocksize*(j-(ymax)/2)+ blocksize/2, 0 ,_
                        texture_tileset,_
                        blockscale, blockscale, .0025, .0025,_
                        idFrame(id_rrocket), , 240,_
                        0, 0, 1, 0, ,_
                        0 , 0,_
                        0, 0,_
                        1,_
                        255, 255, 255, 255, 1)

					case id_lrocket
						for iii as integer=0 to i-1 step 1
							if table(pcur,iii,j).id>0 then
							if table(pcur,iii,j).id<>id_bird1 then
							if table(pcur,iii,j).id<>id_bird2 then
							if table(pcur,iii,j).id<>id_birdgone then
							if table(pcur,iii,j).id<>id_wall then
								table(pcur,iii,j).burned=99
								getScore(table(pcur,iii,j).id)
							end if
							end if
							end if
							end if
							end if
						next iii

						osgl_playsound3d(sound_explo1,player(pcur).xoffset,0,0,bomb_channel)
						player(pcur).depositdelay=10
						player(pcur).rumble=10
						tableMessage(0,-30,"< Rocket!")

						osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2)+ blocksize/2, 0 ,_
                        texture_tileset,_
                        blockscale, blockscale, .0025, .0025,_
                        idFrame(id_lrocket), , 240,_
                        0, 0, -1, 0, ,_
                        0 , 0,_
                        0, 0,_
                        1,_
                        255, 255, 255, 255, 1)

					case id_urocket
						for jjj as integer=j-1 to 0 step -1
							if table(pcur,i,jjj).id>0 then
							if table(pcur,i,jjj).id<>id_bird1 then
							if table(pcur,i,jjj).id<>id_bird2 then
							if table(pcur,i,jjj).id<>id_birdgone then
							if table(pcur,i,jjj).id<>id_wall then
								table(pcur,i,jjj).burned=99
								getScore(table(pcur,i,jjj).id)
							end if
							end if
							end if
							end if
							end if
						next jjj

						osgl_playsound3d(sound_explo1,player(pcur).xoffset,0,0,bomb_channel)
						player(pcur).depositdelay=10
						player(pcur).rumble=10
						tableMessage(0,-30,"^ Rocket! ^")

						osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2)+ blocksize/2, 0 ,_
                        texture_tileset,_
                        blockscale, blockscale, .0025, .0025,_
                        idFrame(id_urocket), , 240,_
                        0, 0, 0, -1, ,_
                        0 , 0,_
                        0, 0,_
                        1,_
                        255, 255, 255, 255, 1)
                        
                        
                     case id_bird1, id_bird2, id_birdgone
                     
						osgl_playsound3d(sound_bird1, player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2)+ blocksize/2 )
						
						if puzzlemode then
						
							if player(1).birds<2 then
								tableMessage(0,-40,"PUZZLE COMPLETED!")
							else
								tableMessage(0,30,"Free bird!")
								'if player(1).birds>2 then
									'tableMessage(0,30,str(player(1).birds-1)+" birds to go!")
								'else
									'tableMessage(0,30,"1 bird to go!")
								'end if
							end if
						
						else
						
								tableMessage(0,30,"Free bird!")
						
								player(pcur).score+=20
							
						end if

						
						

				end select


				player(pcur).speed+=0.001*(player(pcur).level/1.7)
				if player(pcur).speed>5 then player(pcur).speed=5
				
				burnsound=1
				
				'burn particle
				
				dim as ubyte r,g,b
				getidcolor(table(pcur,i,j).id, r, g, b)
				
						osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) +blocksize/2, 0 ,_
                        texture_tileset,_
                        0.05, 0.05, .035, .035,_
                        17, 17, 30,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 25,_
                        3,_
                        r, g, b, 255, 20)

						osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) +blocksize/2, 0 ,_
                        texture_tileset,_
                        0.07, 0.07, .03, .03,_
                        16, 16, 20,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 15,_
                        1,_
                        r, g, b, 255, 30)

					for pp as integer=1 to 4
						osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) +blocksize, 0 ,_
                        texture_tileset,_
                        0.03, 0.03, 0, 0,_
                        16, 16, 180,_
                        3-6*rnd, -3-3*rnd, 0, .2, ,_
                        0 , 0,_
                        360*rnd, 25,_
                        3,_
                        r, g, b, 255, 5)
					next pp
					
				clearBlock(pcur,i,j)
				burn=0

			 'abre presente
			 for ii as integer=i-1 to i+1
			 for jj as integer=j-1 to j+1
				if ii=i or jj=j then
				if table(pcur,ii,jj).id=id_surprise then
				if table(pcur,ii,jj).dy=0 then
				
					select case rnd
						case 0 to .2
				
							table(pcur,ii,jj).id=int(rnd*(maxid-1))+2 'numbers   int(rnd2(pcur)*(maxid-1))+2
							linkBlock(ii,jj)
							burnCheck(ii,jj)
							
						case .21 to .5
						
							table(pcur,ii,jj).id=id_rock + rnd*5 '1001 - 1005 - pedra, bomb ou rocket
							linkBlock(ii,jj)
							burnCheck(ii,jj)

						case .51 to .6
							
							if puzzlemode then 
								table(pcur,ii,jj).id=2
							else
								table(pcur,ii,jj).id=id_bird1 + rnd 'birds
								player(pcur).birds+=1
							end if
							linkBlock(ii,jj)
							burnCheck(ii,jj)
							
						case else
							
							'just score
							clearBlock(pcur,ii,jj)
							
							dim as integer kk=cint(rnd*5 + 1)*10

							if puzzlemode=0 then
							osgl_particle(    player(pcur).xoffset + blocksize*(ii-(xmax)/2), player(pcur).yoffset + blocksize*(jj-(ymax)/2) +blocksize/2, 0 ,_
							,_
							0.75, 0.75, 0, 0,_
							16, 16, 240,_
							0, -.5, 0, 0, ,_
							0 , 0,_
							0, 0,_
							1,_
							255, 255, 25, 255, 5, str(kk))
							end if
							
							player(pcur).score+=kk
							
					end select

							
						osgl_particle(    player(pcur).xoffset + blocksize*(ii-(xmax)/2), player(pcur).yoffset + blocksize*(jj-(ymax)/2)+blocksize/2, 0 ,_
						texture_tileset,_
                        0.05, 0.05, .035, .035,_
                        16, 16, 30,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 25,_
                        3,_
                        255, 255, 255, 255, 15)
                        
						osgl_playsound3d(sound_item1,player(pcur).xoffset,0)
							
				end if
				end if
				end if
				
			 next jj
  			 next ii


			end if
			
			

			'acende bomba / rocket
			
			if table(pcur,i,j-1).id=id_urocket then
			if table(pcur,i,j-1).burned=0 then
				table(pcur,i,j-1).burned=1
				getScore(9)
				osgl_playsound3d(sound_lit,player(pcur).xoffset,0,0,bomb_channel)
				
					osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) +.1*blocksize, 0 ,_
						texture_tileset,_
                        0.08, 0.08, 0, 0,_
                        16, 16, 40,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 15,_
                        1,_
                        255, 255, 127, 250, 5)

				
			end if
			end if

			if table(pcur,i,j+1).id=id_bomb then
			if table(pcur,i,j+1).burned=0  then
				table(pcur,i,j+1).burned=1
				getScore(9)
				osgl_playsound3d(sound_lit,player(pcur).xoffset,0,0,bomb_channel)

					osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2), player(pcur).yoffset + blocksize*(j-(ymax)/2) + .75*blocksize, 0 ,_
						texture_tileset,_
                        0.08, 0.08, 0, 0,_
                        16, 16, 40,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 15,_
                        1,_
                        255, 255, 127, 250, 5)

			end if
			end if

			if table(pcur,i-1,j).id=id_lrocket then
			if table(pcur,i-1,j).burned=0 then
				table(pcur,i-1,j).burned=1
				getScore(9)
				osgl_playsound3d(sound_lit,player(pcur).xoffset,0,0,bomb_channel)

					osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2) - .4*blocksize, player(pcur).yoffset + blocksize*(j-(ymax)/2) + blocksize/2, 0 ,_
						texture_tileset,_
                        0.08, 0.08, 0, 0,_
                        16, 16, 40,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 15,_
                        1,_
                        255, 255, 127, 250, 5)

			end if
			end if

			if table(pcur,i+1,j).id=id_rrocket then
			if table(pcur,i+1,j).burned=0 then
				table(pcur,i+1,j).burned=1
				getScore(9)
				osgl_playsound3d(sound_lit,player(pcur).xoffset,0,0,bomb_channel)

					osgl_particle(    player(pcur).xoffset + blocksize*(i-(xmax)/2) + .4*blocksize, player(pcur).yoffset + blocksize*(j-(ymax)/2) + blocksize/2, 0 ,_
						texture_tileset,_
                        0.08, 0.08, 0, 0,_
                        16, 16, 40,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 15,_
                        1,_
                        255, 255, 127, 250, 5)

			end if
			end if


		end if
	next i
  next j

'if burncount>0 then
'if burn=0 then
'	osgl_joyrumble(pcur,30,30,150)
'	osgl_playsound3d(sound_hurt1,player(pcur).xoffset,0,0)
'end if
'end if

if burn=0 then
	player(pcur).combo+=1
	player(pcur).rumble+=2
	if player(pcur).combo>1 then
		player(pcur).pow+=1
		player(pcur).rumble+=player(pcur).combo*2
		tableMessage(0,0,str(player(pcur).combo)+" HIT COMBO!")
		
		osgl_joyrumble(pcur,100,100,200)
		osgl_playsound3d(sound_coin1,player(pcur).xoffset+fallblock(pcur).x,fallblock(pcur).y)
		
		if numplayers>1 then
				penaliza(pother, id_rock)
		end if

	end if
	player(pcur).depositdelay=10
end if
 
#endmacro





dim shared as integer burn=0

sub burnCheck(byval xx as integer, byval yy as integer)
'checkburn

if yy>ymax then exit sub

if table(pcur,xx,yy+1).id=0 then
	exit sub
end if

dim as integer id=table(pcur,xx,yy).id

if id<1 then exit sub

dim as integer count=0

table(pcur,xx,yy).counted=1
count=1

select case id

case id_randomizer

	osgl_joyrumble(pcur,100,100,200)
	osgl_playsound3d(sound_life,player(pcur).xoffset,0)
	tableMessage(0,-30,"Randomizer!")
	table(pcur,xx,yy).burned=99

	player(pcur).depositdelay=5
	player(pcur).rumble=10
	
	dim as integer rndfactor=1 + rnd*maxid + maxid
	
	osgl_wrap(rndfactor,1,maxid-2)

					osgl_particle(    player(pcur).xoffset + blocksize*(xx-(xmax)/2), player(pcur).yoffset + blocksize*(yy-(ymax)/2)+blocksize/2, 0 ,_
                        texture_tileset,_
                        0.1, 0.1, 0.4, 0.4,_
                        16, 16, 45,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 25,_
                        3,_
                        255, 255, 255, 255, 25)

	for j as integer=lbound(table,3)+1 to ymax
	  for i as integer=1 to xmax
		if i<>xx or j<>yy then
		if table(pcur,i,j).id>0 then
		if table(pcur,i,j).id<=maxid then
			table(pcur,i,j).id=table(pcur,i,j).id+rndfactor
			osgl_wrap(table(pcur,i,j).id,2,maxid)
			table(pcur,i,j).dy=-downspeed
			table(pcur,i,j).falling=1
		end if
		end if
		end if
	  next i
	next j


case id_medusa

	osgl_joyrumble(pcur,100,100,200)
	osgl_playsound3d(sound_zeum,player(pcur).xoffset,0)
	tableMessage(0,-30,"Medusa!")

	player(pcur).depositdelay=10
	player(pcur).rumble=20

	id=table(pcur,xx,yy+1).id 'down
	if id=0 or id=id_wall or id=id_bird1 or id=id_bird2 or id=id_birdgone then id=maxid
	table(pcur,xx,yy).burned=1

					osgl_particle(    player(pcur).xoffset + blocksize*(xx-(xmax)/2), player(pcur).yoffset + blocksize*(yy-(ymax)/2)+blocksize/2, 0 ,_
                        texture_tileset,_
                        0.1, 0.1, 0.4, 0.4,_
                        16, 16, 45,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 25,_
                        3,_
                        255, 255, 255, 255, 25)

	for j as integer=lbound(table,3)+1 to ymax
	  for i as integer=1 to xmax
		if table(pcur,i,j).id=id then 'mesmo id
			table(pcur,i,j).id=id_rock
		end if
	  next i
	next j



case id_nuke

	osgl_joyrumble(pcur,100,100,200)
	osgl_playsound3d(sound_explo2,player(pcur).xoffset,0)
	tableMessage(0,-30,"NUKE!!!")
	player(pcur).depositdelay=10
	player(pcur).rumble=40
	id=table(pcur,xx,yy+1).id 'down
	if id=0 or id=id_wall or id=id_bird1 or id=id_bird2 or id=id_birdgone then id=maxid
	
	clearBlock(pcur,xx,yy)
	getscore(id_nuke)


						osgl_particle(    player(pcur).xoffset + blocksize*(xx-(xmax)/2), player(pcur).yoffset + blocksize*(yy-(ymax)/2)+blocksize/2, 0 ,_
                        texture_tileset,_
                        0.3, 0.3, 0.2, 0.2,_
                        14, 16, 60,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 15,_
                        1,_
                        255, 255, 255, 255, 20)

						osgl_particle(    player(pcur).xoffset + blocksize*(xx-(xmax)/2), player(pcur).yoffset + blocksize*(yy-(ymax)/2)+blocksize/2, 0 ,_
                        texture_tileset,_
                        0.3, 0.3, 0.25, 0.25,_
                        14, 16, 50,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 25,_
                        3,_
                        255, 255, 255, 255, 8)

						osgl_particle(    player(pcur).xoffset + blocksize*(xx-(xmax)/2), player(pcur).yoffset + blocksize*(yy-(ymax)/2)+blocksize/2, 0 ,_
                        texture_tileset,_
                        0.05, 0.05, .035, .035,_
                        16, 16, 30,_
                        0, 0, 0, 0, ,_
                        0 , 0,_
                        360*rnd, 25,_
                        3,_
                        255, 255, 255, 255, 15)

	
	for j as integer=lbound(table,3)+1 to ymax
	  for i as integer=1 to xmax
		if table(pcur,i,j).id=id then 'mesmo id
			table(pcur,i,j).counted=1
			table(pcur,i,j).burned=99
			count=id
		end if
	  next i
	next j

	if numplayers>1 then 'no caso de nuke, penaliza outro player 
		fallblock(pother).nextid=id_medusa
		fallblock(pother).nextidu=0
		if player(pother).pow=0 then player(pother).pow+=10 else player(pother).pow+=6 'dá um pow pra aliviar a barra
	end if


case 1 '1 = coringa : queima tipo de bloco que estiver em baixo

	osgl_joyrumble(pcur,10,10,150)
	osgl_playsound3d(sound_hurt2,player(pcur).xoffset,0)

	player(pcur).depositdelay=1
	player(pcur).rumble=4

	id=table(pcur,xx,yy+1).id 'down

	if id=0 or id=id_wall then
		id=table(pcur,xx-1,yy).id 'left
		if table(pcur,xx+1,yy).id<> 0 then table(pcur,xx+1,yy).burned=1 'right
	end if

	if id=0 or id=id_wall then
		id=table(pcur,xx+1,yy).id 'right
		if table(pcur,xx-1,yy).id<> 0 then table(pcur,xx+1,yy).burned=1 'right
	end if

	if id=0 or id=id_wall then
		id=1
		count=1 'got nothing
	else
		count=id 'got something
	end if

end select

for k as integer=1 to xmax*ymax 'precisa mais interações para pegar os vizinhos
	for j as integer=lbound(table,3)+1 to ymax
	  for i as integer=1 to xmax
		if table(pcur,i,j).id=id then 'mesmo id
		if table(pcur,i,j+1).id<>0 then 'não está caindo
		if table(pcur,i,j).falling=0 then 'não está caindo
		 if table(pcur,i,j).counted=0 then 'ainda não foi checado
		  if table(pcur,i-1,j).counted or table(pcur,i+1,j).counted or table(pcur,i,j-1).counted or table(pcur,i,j+1).counted then  'toca em algum já checado
			table(pcur,i,j).counted=1
			count+=1
		  end if
 	 	 end if
		end if
		end if
		end if
	  next i
	next j
next k

if count>=id then
	markBurnBlocks()
	burn=1
	'burnsound=1
end if

resetTableCounter()

end sub




sub depositBlock(byval x as integer, byval y as integer, byval id as integer)

if id=0 then exit sub

dim as integer xx=(x+(blocksize*xmax/2))\blocksize
dim as integer yy=(y+(blocksize*ymax/2))\blocksize

if yy<=lbound(table,3) then
	player(pcur).gameover+=1
	player(pcur).depositdelay=10
else
	'printdebug "%%       yy="+str(yy)
	table(pcur,xx,yy).id=id
	linkBlock(xx,yy)
	burnCheck(xx,yy)
end if

end sub



#macro drawScore()

if numplayers=1 then

	if puzzlemode then

		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -70, 0, "PUZZLE  "                      ,0.5,0.8,0,1,rgba(255,255,55,255)
		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -70, 0, "       "+str(player(pcur).puzzlelevel) ,0.5,0.8,0,1,rgba(255,255,255,255)

		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -55, 0, "BIRDS  "                      ,0.5,0.8,0,1,rgba(255,255,55,255)
		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -55, 0, "       "+str(int(player(pcur).birds)) ,0.5,0.8,0,1,rgba(255,255,255,255)

		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -40, 0, "TIME   "                                          ,0.5,0.8,0,1,rgba(255,255,55,255)
		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -40, 0, "       "+osgl_frames2minutes(player(pcur).timecount,0) ,0.5,0.8,0,1,rgba(255,255,255,255)
		
	else
		
		if player(pcur).scoredisplay<player(pcur).score then player(pcur).scoredisplay+=(player(pcur).score-player(pcur).scoredisplay)*.1  'score aumenta devagar

		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -70, 0, "SCORE  "                      ,0.5,0.8,0,1,rgba(255,255,55,255)
		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -70, 0, "       "+str(int(player(pcur).scoredisplay)) ,0.5,0.8,0,1,rgba(255,255,255,255)

		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -55, 0, "TIME   "                                          ,0.5,0.8,0,1,rgba(255,255,55,255)
		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -55, 0, "       "+osgl_frames2minutes(player(pcur).timecount,0) ,0.5,0.8,0,1,rgba(255,255,255,255)

		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -40, 0, "LEVEL  "                      ,0.5,0.8,0,1,rgba(255,255,55,255)
		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -40, 0, "       "+str(player(pcur).level) ,0.5,0.8,0,1,rgba(255,255,255,255)
	end if

else

	if player(pcur).scoredisplay<player(pcur).score then player(pcur).scoredisplay+=(player(pcur).score-player(pcur).scoredisplay)*.1  'score aumenta devagar
	
	osgl_print player(pcur).xoffset +75*sgn(player(pcur).xoffset) +blocksize/2, player(pcur).yoffset -70, 0, "SCORE"                      ,0.4,0.8,1,1,rgba(255,255,55,255)
	osgl_print player(pcur).xoffset +75*sgn(player(pcur).xoffset) +blocksize/2, player(pcur).yoffset -58, 0, str(int(player(pcur).scoredisplay)) ,0.4,0.8,1,1,rgba(255,255,255,255)

	osgl_print player(pcur).xoffset +75*sgn(player(pcur).xoffset) +blocksize/2, player(pcur).yoffset -40, 0, "LEVEL"                      ,0.4,0.8,1,1,rgba(255,255,55,255)
	osgl_print player(pcur).xoffset +75*sgn(player(pcur).xoffset) +blocksize/2, player(pcur).yoffset -28, 0, str(player(pcur).level) ,0.4,0.8,1,1,rgba(255,255,255,255)

	if pcur=1 then
		osgl_print -15, 90, 0, "TIME" ,0.4,0.6,1,1,rgba(255,255,55,255)
		osgl_print 15, 90, 0, osgl_frames2minutes(player(pcur).timecount,0) ,0.4,0.6,1,1,rgba(255,255,255,255)
	end if

	osgl_print player(pcur).xoffset +75*sgn(player(pcur).xoffset) +blocksize/2, player(pcur).yoffset +60, 0, "WINS"                      ,0.4,0.8,1,1,rgba(255,255,55,255)
	osgl_print player(pcur).xoffset +75*sgn(player(pcur).xoffset) +blocksize/2, player(pcur).yoffset +72, 0, str(vswins(pcur)) ,0.4,0.8,1,1,rgba(255,255,255,255)

end if

if pcur=1 then
	osgl_print -155, 93, 0, "NUMBEZZLED (C) Cleber M Casali"                      ,0.25,0.4,0,1,rgba(100,255,255,255)
end if


#endmacro


#macro drawNextblock()

if numplayers=1 then


	if player(pcur).shownext then

		osgl_print player(pcur).xoffset -120, player(pcur).yoffset -25, 0, "NEXT", 0.5,0.8,0,1,rgba(255,255,55,255)

		if fallblock(pcur).nextidu then '2 blocos
		'	osgl_print player(pcur).xoffset -85, player(pcur).yoffset -25 ,  0, id2char(fallblock(pcur).nextidu),1.0,1.0,1,1,idcolor(fallblock(pcur).nextidu)
		'	osgl_print player(pcur).xoffset -85, player(pcur).yoffset -25 + blocksize,  0, id2char(fallblock(pcur).nextid),1.0,1.0,1,1,idcolor(fallblock(pcur).nextid)
			drawblock (fallblock(pcur).nextidu , player(pcur).xoffset -85, player(pcur).yoffset -25 )
			drawblock (fallblock(pcur).nextid  , player(pcur).xoffset -85, player(pcur).yoffset -25 + blocksize)
		else '1 bloco só
		'	osgl_print player(pcur).xoffset -85, player(pcur).yoffset -25 ,  0, id2char(fallblock(pcur).nextid),1.0,1.0,1,1,,idcolor(fallblock(pcur).nextid)
			drawblock (fallblock(pcur).nextid , player(pcur).xoffset -85, player(pcur).yoffset -25 )
		end if

	end if

else


	osgl_print player(pcur).xoffset +75*sgn(player(pcur).xoffset) +blocksize/2, player(pcur).yoffset -10, 0, "NEXT", 0.4,0.8,1,1,rgba(255,255,55,255)
	if fallblock(pcur).nextidu then '2 blocos
		drawblock (fallblock(pcur).nextidu , player(pcur).xoffset +75*sgn(player(pcur).xoffset) + blocksize/2, player(pcur).yoffset +5 )
		drawblock (fallblock(pcur).nextid  , player(pcur).xoffset +75*sgn(player(pcur).xoffset) + blocksize/2, player(pcur).yoffset +5 + blocksize)
	else '1 bloco só
		drawblock (fallblock(pcur).nextid , player(pcur).xoffset +75*sgn(player(pcur).xoffset) + blocksize/2, player(pcur).yoffset +5 )
	end if

end if

#endmacro



#macro drawBackdrop()

osgl_3dquad ( -160, -100, 0,   160, -100, 0,   160, 100, 0,    -160, 100, 0,_
			tex_backdrop(currentbackdrop),,_
			0,0,_
			2.0,2.0,spacescroll, spacescroll,_
			0,0,_
			rgba(63,63,63,7))

osgl_3dquad ( -160, -100, 0,   160, -100, 0,   160, 100, 0,    -160, 100, 0,_
			tex_backdrop(currentbackdrop),,_
			0,0,_
			1.0,1.0,spacescroll, spacescroll,_
			13,0,_
			rgba(255,255,255,7))

	spacescroll+=.000125 + (player(1).speed+player(2).speed)/2000
	
	osgl_wrap(spacescroll,0,1)

#endmacro


#macro drawTutorial(frame)
scope

currentbackdrop=4
dim as single delta=100
'osgl_fadeout
'osgl_cls
'osgl_sync(1)
'osgl_fadein(,4)

'do
for ii as integer=1 to 30
 delta=.9*delta
 drawBackdrop()
 if frame>0 then
	osgl_Render(    0, -delta, 0.0, 0.0 ,_
                0,_
                texture_tutorial, frame-1,_
                0,_
                0.5*(1-delta/100), 0.5*(1-delta/100),_
                0, 0,_
                1.0, 1.0,_
                0, 0,_
                1.0, 1.0, 0, 0,_
                1, 0,_
                rgba(255,255,255,255))
 end if
                
 osgl_sync
'loop until osgl_fadein_complete
next ii

osgl_sync(1)

currentbackdrop=1

end scope
#endmacro

sub howToPlay() 'tutorial
osgl_fadeout
osgl_cls
osgl_sync(1)

drawTutorial(1)
osgl_textbox "Blocks will begin to fall from the top of the screen. Use your keyboard LEFT/ RIGHT ARROWS or gamepad X AXIS to move them. You can also use your mouse or touchscreen to drag them."

drawTutorial(2)
osgl_textbox "Use the UP key, right mouse button or the gamepad buttons to rotate the block. You can also click/tap the block to rotate it."

drawTutorial(1)
osgl_textbox "Press DOWN to make the block fall faster. You can also use your mouse or touchscreen to drag them down."

drawTutorial(3)
osgl_textbox "Your objective is to group together blocks with the same number. For example, when you group together two blocks with the number two, they will burn and disappear, and your score will increase."

drawTutorial(4)
osgl_textbox "Same thing with three threes, four fours, five fives, and so on. They don't have to be in same line."

drawTutorial(1)
osgl_textbox "Difficulty will increase as you play. Blocks will fall faster."

drawTutorial(5)
osgl_textbox "There is a special type of block with the number one, that will burn itself and the block below it. It will also burn any neighbor blocks with the same number."

drawTutorial(7)
osgl_textbox "There are other types of special blocks, but you will have to find out by yourself."

drawTutorial(6)
osgl_textbox "When you burn multiple blocks in a chain reaction, you will get a COMBO message. When you burn many blocks in single move, you will see a GREAT MOVE message. Performing COMBOs and GREAT MOVEs will dramatically increase your score and your chances of receiving special blocks."

drawTutorial(0)
osgl_textbox "Well, that's all I can tell you for now. You may find a few surprises while playing. Have fun!"

osgl_fadeout

end sub



function idColor(id as integer, a as ubyte=255) as uinteger
select case id
	case 1 : return rgba(255,227+27*cos(degrees(player(pcur).timecount/200)),227+27*sin(90+degrees(player(pcur).timecount/300)), a*(.8+.2*sin(90+degrees(player(pcur).timecount/100))) )
	case 2 : return rgba(255,0,0,a)
	case 3 : return rgba(0,255,0,a)
	case 4 : return rgba(255,127,0,a)
	case 5 : return rgba(255,0,255,a)
	case 6 : return rgba(0,255,255,a)
	case 7 : return rgba(255,255,0,a)
	case 8 : return rgba(0,0,255,a)
	case 9 : return rgba(255,255,255,a)
	case id_bomb : return rgba(63,63,63,a)
	case id_urocket, id_lrocket, id_rrocket : return rgba(127,127,127,a)
	case id_rock : return rgba(127,63,0,a)
	case id_nuke : return rgba(127,127,127,a)
	case id_medusa: return rgba(127,127,127,a)
	case id_surprise: return rgba(127,255,255,a)
	case id_randomizer: return rgba(255,255,127,a)
	case id_bird1,id_bird2,id_birdgone: return rgba(127,63,2,a)
	case else : return rgba(255,255,255,a)
end select
end function


#macro getidcolor(id, r, g, b)
	dim as uinteger gotcolor=idColor(id)
	r=rgbar(gotcolor)
	g=rgbag(gotcolor)
	b=rgbab(gotcolor)
#endmacro


#macro drawFallblock()
fallblock(pcur).iddx*=.5
fallblock(pcur).iddy*=.5
fallblock(pcur).idrdx*=.5
fallblock(pcur).idrdy*=.5
fallblock(pcur).idldx*=.5
fallblock(pcur).idldy*=.5
fallblock(pcur).idudx*=.5
fallblock(pcur).idudy*=.5
if fallblock(pcur).id then
	'osgl_print player(pcur).xoffset + fallblock(pcur).x + fallblock(pcur).xd + fallblock(pcur).iddx , player(pcur).yoffset + fallblock(pcur).y + fallblock(pcur).yd  + fallblock(pcur).iddy ,  0, id2char(fallblock(pcur).id),1.0,1.0,1,1,idcolor(fallblock(pcur).id)
	drawblock (fallblock(pcur).id, player(pcur).xoffset + fallblock(pcur).x + fallblock(pcur).xd + fallblock(pcur).iddx , player(pcur).yoffset + fallblock(pcur).y + fallblock(pcur).yd  + fallblock(pcur).iddy )
end if
if fallblock(pcur).idu then
	'osgl_print player(pcur).xoffset + fallblock(pcur).x + fallblock(pcur).xd  + fallblock(pcur).idudx, player(pcur).yoffset + fallblock(pcur).y + fallblock(pcur).yd - blocksize  + fallblock(pcur).idudy ,  0, id2char(fallblock(pcur).idu),1.0,1.0,1,1,idcolor(fallblock(pcur).idu)
	drawblock (fallblock(pcur).idu, player(pcur).xoffset + fallblock(pcur).x + fallblock(pcur).xd  + fallblock(pcur).idudx, player(pcur).yoffset + fallblock(pcur).y + fallblock(pcur).yd - blocksize  + fallblock(pcur).idudy )
end if
if fallblock(pcur).idl then
	'osgl_print player(pcur).xoffset + fallblock(pcur).x + fallblock(pcur).xd - blocksize  + fallblock(pcur).idldx , player(pcur).yoffset + fallblock(pcur).y + fallblock(pcur).yd + fallblock(pcur).idldy , 0, id2char(fallblock(pcur).idl),1.0,1.0,1,1,idcolor(fallblock(pcur).idl)
	drawblock (fallblock(pcur).idl, player(pcur).xoffset + fallblock(pcur).x + fallblock(pcur).xd - blocksize  + fallblock(pcur).idldx , player(pcur).yoffset + fallblock(pcur).y + fallblock(pcur).yd + fallblock(pcur).idldy )
end if
if fallblock(pcur).idr then
	'osgl_print player(pcur).xoffset + fallblock(pcur).x + fallblock(pcur).xd + blocksize  + fallblock(pcur).idrdx , player(pcur).yoffset + fallblock(pcur).y + fallblock(pcur).yd  + fallblock(pcur).idrdy, 0, id2char(fallblock(pcur).idr),1.0,1.0,1,1,idcolor(fallblock(pcur).idr)
	drawblock (fallblock(pcur).idr, player(pcur).xoffset + fallblock(pcur).x + fallblock(pcur).xd + blocksize  + fallblock(pcur).idrdx , player(pcur).yoffset + fallblock(pcur).y + fallblock(pcur).yd  + fallblock(pcur).idrdy )
end if
#endmacro



#macro penaliza(pp, idd)
scope
	dim as integer tmp=1+rnd*(xmax-1)
	clearblock(pp,tmp,1)
	table(pp,tmp,1).id=idd
	table(pp,tmp,1).dy=-3*blocksize
	dim as integer ppbak=pcur
	pcur=pp
	acomodaTable()
	pcur=ppbak
	osgl_joyrumble(pp,100,100,150)
end scope
#endmacro



#macro playerControls()

'pause
if osgl_pausebutton(pcur) or osgl_esckey then
	osgl_mousehide()
	osgl_playsound(sound_beep)
	
	if puzzlemode then

		dim as integer tmp=osgl_menu(0,0,"GAME PAUSED","Resume,Restart,Quit")
		select case as const tmp
			case 2 'restart
				osgl_fadeout
				puzzlelevel-=1
				goto newgame
			
			case 3 'quit
				osgl_fadeout
				osgl_stopmusic
				osgl_delay(30)
				#ifdef notitle
					goto endprogram
				#else
					goto title
				#endif
		end select
	
	else
	
		dim as integer tmp=osgl_menu(0,0,"GAME PAUSED","Resume Game,Quit")
		select case as const tmp
			case 2 'quit
				osgl_fadeout
				osgl_stopmusic
				osgl_delay(30)
				#ifdef notitle
					goto endprogram
				#else
					goto title
				#endif
		end select
	
	end if
	
end if


if pcur=1 then
	player(pcur).mousewheel=osgl_mousewheel()
	player(pcur).mousebut1=osgl_mousebutton(1)
	player(pcur).mousebut2=osgl_mousebutton(2)
	player(pcur).mousebut3=osgl_mousebutton(3)
end if

if osgl_axis(pcur,2)<-.1 then
	if player(pcur).upkey=0 then player(pcur).upkey=1 else player(pcur).upkey=2
else
	player(pcur).upkey=0
end if

player(pcur).xaxis=osgl_axis(pcur,1)
player(pcur).yaxis=osgl_axis(pcur,2)
if player(pcur).mousewheel>0 then player(pcur).yaxis=1
player(pcur).lrotate=(osgl_button(pcur,1)=1 or osgl_button(pcur,3)=1 or osgl_button(pcur,5)=1 or osgl_button(pcur,7)=1 or player(pcur).mousebut3=1 or player(pcur).mousewheel<0)
player(pcur).rrotate=(osgl_button(pcur,2)=1 or osgl_button(pcur,4)=1 or osgl_button(pcur,6)=1 or osgl_button(pcur,8)=1 or player(pcur).upkey=1  or player(pcur).mousebut2=1)


'mouse / touch controls

dim as integer mx=osgl_mousex-player(pcur).xoffset
dim as integer my=osgl_mousey-player(pcur).yoffset

select case player(pcur).mousebut1
  
  case 0 'release
	if fallblock(pcur).mousedrag=2 then
  		if mx>fallblock(pcur).x then player(pcur).rrotate=1 else player(pcur).lrotate=1 'rotate
  	end if
  	
  	fallblock(pcur).mousedrag=0 'reset

  
  case 1 'click
	if my<fallblock(pcur).y+blocksize then
	if my>fallblock(pcur).y-blocksize then
	if abs(mx-fallblock(pcur).x)<=(blocksize+2) then
		fallblock(pcur).mousedrag=2 'click to rotate
	end if
	end if
	end if
  
  case 2 'hold

	if abs(mx-fallblock(pcur).x)>(blocksize/2) then
		fallblock(pcur).mousedrag=1 'drag x
		if mx>fallblock(pcur).x then player(pcur).xaxis=1 else player(pcur).xaxis=-1 'move
	end if

	if my>fallblock(pcur).y+blocksize then
		fallblock(pcur).mousedrag=1 'drag y
		player(pcur).yaxis=1 'down
	end if

end select



if abs(fallblock(pcur).xd)<.5 then
fallblock(pcur).xd=0
select case player(pcur).xaxis
	case -1.0 to -0.1
		if getBlock(pcur,fallblock(pcur).x-blocksize,fallblock(pcur).y+blocksize)=0 then
		if (fallblock(pcur).idl=0 or getBlock(pcur,fallblock(pcur).x-(2*blocksize),fallblock(pcur).y+blocksize)=0) then
		if fallblock(pcur).x>-blocksize*(-1+(xmax)/2) then
			fallblock(pcur).x-=blocksize
			fallblock(pcur).xd+=blocksize
		end if
		end if
		end if
	
	case 0.1 to 1.0 
		if getBlock(pcur,fallblock(pcur).x+blocksize,fallblock(pcur).y+blocksize)=0 then
		if (fallblock(pcur).idr=0 or getBlock(pcur,fallblock(pcur).x+(2*blocksize),fallblock(pcur).y+blocksize)=0) then
		if fallblock(pcur).x<blocksize*xmax/2 then
			fallblock(pcur).x+=blocksize
			fallblock(pcur).xd-=blocksize
		end if
		end if
		end if

end select
end if

if player(pcur).lrotate then

	osgl_joyrumble(pcur,10,0,150)
	osgl_playsound3d(sound_pac,player(pcur).xoffset+fallblock(pcur).x,fallblock(pcur).y)
	
	if getBlock(pcur,fallblock(pcur).x-blocksize,fallblock(pcur).y+blocksize)=0 then
	
		if fallblock(pcur).idr then
			fallblock(pcur).idu=fallblock(pcur).idr : fallblock(pcur).idr=0 : fallblock(pcur).idl=0
			fallblock(pcur).idudx=blocksize : fallblock(pcur).idudy=blocksize
			goto endrotate
		end if
	
		if fallblock(pcur).idu then
			fallblock(pcur).idl=fallblock(pcur).idu : fallblock(pcur).idu=0 : fallblock(pcur).idr=0
			fallblock(pcur).idldx=blocksize : fallblock(pcur).idldy=-blocksize
			goto endrotate
		end if
		
		if fallblock(pcur).idl then
			fallblock(pcur).idu=fallblock(pcur).id : fallblock(pcur).id=fallblock(pcur).idl : fallblock(pcur).idl=0 : fallblock(pcur).idr=0
			fallblock(pcur).idudy=blocksize : fallblock(pcur).iddx=-blocksize
			goto endrotate
		end if
	else
		if fallblock(pcur).idu then
			swap fallblock(pcur).id,fallblock(pcur).idu
			fallblock(pcur).idudy=blocksize : fallblock(pcur).iddy=-blocksize
		end if
		if fallblock(pcur).idr then
			swap fallblock(pcur).idu,fallblock(pcur).idr
			fallblock(pcur).idudx=blocksize : fallblock(pcur).idudy=blocksize
			fallblock(pcur).idrdx=-blocksize : fallblock(pcur).idrdy=-blocksize
		end if
	end if
	
end if

if player(pcur).rrotate then

	osgl_joyrumble(pcur,0,10,150)
	osgl_playsound3d(sound_pac,player(pcur).xoffset+fallblock(pcur).x,fallblock(pcur).y)

	if getBlock(pcur,fallblock(pcur).x+blocksize,fallblock(pcur).y+blocksize)=0 then
		
		if fallblock(pcur).idl then
			fallblock(pcur).idu=fallblock(pcur).idl : fallblock(pcur).idl=0 : fallblock(pcur).idr=0
			fallblock(pcur).idudx=-blocksize : fallblock(pcur).idudy=blocksize
			goto endrotate
		end if
		
		if fallblock(pcur).idu then
			fallblock(pcur).idr=fallblock(pcur).idu : fallblock(pcur).idu=0 : fallblock(pcur).idl=0
			fallblock(pcur).idrdx=-blocksize : fallblock(pcur).idrdy=-blocksize
			goto endrotate
		end if
		
		if fallblock(pcur).idr then
			fallblock(pcur).idu=fallblock(pcur).id : fallblock(pcur).id=fallblock(pcur).idr : fallblock(pcur).idl=0: fallblock(pcur).idr=0
			fallblock(pcur).idudy=blocksize : fallblock(pcur).iddx=blocksize
			goto endrotate
		end if
		
	else
		if fallblock(pcur).idu then
			swap fallblock(pcur).id,fallblock(pcur).idu
			fallblock(pcur).idudy=blocksize : fallblock(pcur).iddy=-blocksize
		end if
		if fallblock(pcur).idl then
			swap fallblock(pcur).idu,fallblock(pcur).idl
			fallblock(pcur).idudx=-blocksize : fallblock(pcur).idudy=blocksize
			fallblock(pcur).idldx=blocksize : fallblock(pcur).idldy=-blocksize
		end if
	end if
end if

endrotate:

if fallblock(pcur).xd>0 then fallblock(pcur).xd-=xspeed
if fallblock(pcur).xd<0 then fallblock(pcur).xd+=xspeed

if player(pcur).dysum=0 then
	if player(pcur).yaxis>0.1 then fallblock(pcur).y+=downspeed
end if


#ifdef cheatkeys
if cheat then

	if multikey(59) then 'F1
		fallblock(pcur).id=1
		fallblock(pcur).idu=id_nothing
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(60) then 'F2
		fallblock(pcur).id=id_bomb
		fallblock(pcur).idu=id_nothing
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(61) then 'F3
		fallblock(pcur).id=id_urocket
		fallblock(pcur).idu=id_nothing
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(62) then 'F4
		fallblock(pcur).id=id_lrocket
		fallblock(pcur).idu=id_nothing
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(63) then 'F5
		fallblock(pcur).id=id_rrocket
		fallblock(pcur).idu=id_nothing
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(64) then 'F6
		fallblock(pcur).id=id_nuke
		fallblock(pcur).idu=id_nothing
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(65) then 'F7
		fallblock(pcur).id=id_medusa
		fallblock(pcur).idu=id_nothing
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(66) then 'F8
		fallblock(pcur).id=id_surprise
		'fallblock(pcur).id=id_wall
		fallblock(pcur).idu=id_nothing
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(67) then 'F9
		'fallblock(pcur).id=id_randomizer
		player(pcur).birds+=1
		fallblock(pcur).id=id_bird1 + cint(rnd)
		'fallblock(pcur).idu=id_nothing
		fallblock(pcur).idu=2
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(68) then 'F10
		'fallblock(pcur).id=id_rock
		fallblock(pcur).id=id_bird2
		fallblock(pcur).idu=id_nothing
		fallblock(pcur).idl=id_nothing
		fallblock(pcur).idr=id_nothing
		osgl_delay 10
	end if

	if multikey(69) then 'F11
		penaliza(pcur,id_rock)
		osgl_delay 10
	end if

end if
#endif

#endmacro

function rnd2(byval pp as integer) as double
player(pp).rndindex+=1
if player(pp).rndindex>rndmatrixsize then player(pp).rndindex=1
return rndmatrix(player(pp).rndindex)
end function


#macro newFallBlock()
	'fallblock(pcur).x=(((xmax/2)*blocksize)*blocksize)\blocksize
	fallblock(pcur).x=blocksize/2
	fallblock(pcur).xd=0
	fallblock(pcur).y=-ymax*blocksize/2 -blocksize
	fallblock(pcur).yd=0

	if fallblock(pcur).nextid then
		fallblock(pcur).id=fallblock(pcur).nextid
		fallblock(pcur).idu=fallblock(pcur).nextidu
	else
		fallblock(pcur).id=int(rnd2(pcur)*3)+2
		fallblock(pcur).idu=int(rnd2(pcur)*3)+2
	end if
	
	fallblock(pcur).nextid=int(rnd2(pcur)*(maxid-1))+2
	fallblock(pcur).nextidu=int(rnd2(pcur)*(maxid-1))+2
	
	if player(pcur).level>=diflevel_reduce2 then 'reduz 2
		if fallblock(pcur).nextid=2 then
			if rnd2(pcur)>.5 then fallblock(pcur).nextid=maxid
		end if
		if fallblock(pcur).nextidu=2 then
			if rnd2(pcur)>.5 then fallblock(pcur).nextid=maxid
		end if
	end if

	if player(pcur).level>=diflevel_reduce3 then 'reduz 3
		if fallblock(pcur).nextid=3 then
			if rnd2(pcur)>.5 then fallblock(pcur).nextid=maxid
		end if
		if fallblock(pcur).nextidu=3 then
			if rnd2(pcur)>.5 then fallblock(pcur).nextid=maxid
		end if
	end if

	


	if player(pcur).pow>=3 then 'use pow
	if rnd2(pcur)<.1 then
		if rnd2(pcur)>.5 then
			if player(pcur).level>=diflevel_bomb then
				dim as integer tmp=int(rnd2(pcur)*100)
				select case tmp 'bonus rockets e bombs  spawn bomb / spawn rocket
					case 0 to 25: fallblock(pcur).nextid=id_bomb
					case 26 to 50: fallblock(pcur).nextid=id_rrocket
					case 51 to 75: fallblock(pcur).nextid=id_lrocket
					case 76 to 100: fallblock(pcur).nextid=id_urocket
				end select
			end if
		else
			if player(pcur).level>=diflevel_one then
				fallblock(pcur).nextid=1 'bonus coringa spawn 1
				fallblock(pcur).nextidu=id_nothing
			end if
		end if
	
		if player(pcur).level<diflevel_rocket then 'troca rocket por bomb (nivel baixo)
		if fallblock(pcur).nextid=id_rrocket or fallblock(pcur).nextid=id_lrocket or fallblock(pcur).nextid=id_urocket then
			fallblock(pcur).nextid=id_bomb
		end if
		end if

		if rnd<.5 then
		if player(pcur).pow>=10 then  
			
			if player(pcur).level>=diflevel_nuke then  'spawn nuke
				player(pcur).pow-=7
				fallblock(pcur).nextid=id_nuke
				fallblock(pcur).nextidu=id_nothing
			end if
			
			if player(pcur).level>=diflevel_randomizer then  'spawn randomizer
			if rnd<.02*(player(pcur).level/4) then 
				fallblock(pcur).nextid=id_randomizer
				fallblock(pcur).nextidu=id_nothing
				player(pcur).pow-=7
			end if
			end if
			
		end if
		end if



		player(pcur).pow-=3
		if player(pcur).pow<0 then player(pcur).pow=0
		
	end if
	end if  'use pow


 if fallblock(pcur).nextidu<>id_nothing then

	if player(pcur).level>=diflevel_surprise then 'spawn surprise
		if rnd2(pcur)<.007*(player(pcur).level/2) then
			fallblock(pcur).nextid=id_surprise
		else
			if rnd2(pcur)<.007*(player(pcur).level/2) then
				fallblock(pcur).nextidu=id_surprise
				if player(pcur).level>=diflevel_one then
					if fallblock(pcur).nextid=1 then fallblock(pcur).nextid=id_surprise
				end if
			end if
		end if
	end if
	
	if player(pcur).level>=diflevel_rock then 'spawn rock
		if rnd2(pcur)<.003*(player(pcur).level/3) then
			fallblock(pcur).nextid=id_rock
		else
			if rnd2(pcur)<.003*(player(pcur).level/3) then
				fallblock(pcur).nextidu=id_rock
				if player(pcur).level>=diflevel_one then
					if fallblock(pcur).nextid=1 then fallblock(pcur).nextid=id_surprise
				end if
			end if
		end if
	end if

	if player(pcur).level>=diflevel_medusa then 'spawn medusa
		if rnd2(pcur)<.00075*(player(pcur).level/5) then
			fallblock(pcur).nextid=id_medusa
			fallblock(pcur).nextidu=id_nothing
			if player(pcur).pow=0 then player(pcur).pow+=10 else player(pcur).pow+=6
		end if
	end if
	
 end if

	fallblock(pcur).idl=0
	fallblock(pcur).idr=0
	fallblock(pcur).iddx=0
	fallblock(pcur).iddy=-3*blocksize
	fallblock(pcur).idudx=0
	fallblock(pcur).idudy=-3*blocksize
	fallblock(pcur).idldx=0
	fallblock(pcur).idldy=0
	fallblock(pcur).idrdx=0
	fallblock(pcur).idrdy=0
	player(pcur).combo=0
	
#endmacro





#macro newLevel()

player(pcur).level+=1

if puzzlemode then
	'tableMessage(0,-40,"PUZZLE "+str(player(pcur).puzzlelevel))
	tableMessage(0,-40,"RELEASE THE BIRDS!")
else
	tableMessage(0,-40,"LEVEL "+str(player(pcur).level))
end if

if player(pcur).level>1 then
	osgl_joyrumble(pcur,100,100,200)
	osgl_playsound3d(sound_life,player(pcur).xoffset,0)
end if

if numplayers=1 or player(pcur).level>player(pother).level then
	if puzzlemode=0 then
		playMusic(player(pcur).level)
	else
		playMusic(player(1).puzzlelevel)
	end if
	currentbackdrop+=1
	osgl_wrap(currentbackdrop,1,maxbackdrop)
end if

if numplayers>1 then
if player(pcur).level>player(pother).level then
	penaliza(pother, id_rock)
end if
end if

player(pcur).xp=0

if player(pcur).level>10 then
	player(pcur).xpnext=player(pcur).level*50
else
	player(pcur).xpnext=player(pcur).level*30
end if

player(pcur).speed=.1+(player(pcur).level-1)*.1
if player(pcur).speed>2.5 then player(pcur).speed=2.5

if puzzlemode=0 then
select case player(pcur).level
	case 1
		maxid=4
	case 2
		maxid=5
	case 3
		maxid=6
	case 5
		maxid=7
	case 9
		maxid=8
	case is >14
		maxid=9
end select
end if

#endmacro



sub viewHighscores() 'show highscores
osgl_fadeout
osgl_cls
osgl_sync(1)
osgl_fadein
do

	osgl_sync

	drawBackdrop()
	
	osgl_box (player(pcur).xoffset + blocksize*(.5-(2*xmax)/2), player(pcur).yoffset + blocksize*(1.5-(ymax+1)/2) +player(pcur).rumblef ,player(pcur).xoffset + blocksize*((2*xmax+.5)-(2*xmax)/2), player(pcur).yoffset + blocksize*((ymax+1.5)-(ymax+1)/2)+player(pcur).rumblef , _
				1,_
				25,15+10*sin(degrees(player(pcur).timecount/6000)),25,200,_
				25,15+10*cos(degrees(player(pcur).timecount/5000)),15+10*sin(degrees(player(pcur).timecount/8000)),200,_
				25,15+10*sin(degrees(player(pcur).timecount/4000)),25,200,_
				25,25,15+10*cos(degrees(player(pcur).timecount/7000)),200)

	osgl_print 0, -75, 0, "HIGHSCORES",1.0,1.3,1,1,rgba(255,255,55,255)

	osgl_print 0, -50, 0, "                    SCORE   TIME    LEVEL",0.5,0.5,1,1,rgba(255,255,55,255)

	for i as integer=1 to maxhiscore
		osgl_print 0, -40+12*(i-1), 0, right(space(12)+str(i),2)+". "+left(str(hiscore(i).playername)+space(10),13)+right(space(10)+str(hiscore(i).score),7)+right(space(10)+str(osgl_frames2minutes(hiscore(i).playtime,0)),8)+right(space(10)+str(hiscore(i).level),7) ,0.5,0.5,1,1,rgba(255,255,255,255)
		'printdebug hiscore(i).playername; hiscore(i).score; osgl_frames2minutes(hiscore(i).playtime,0); hiscore(i).level
	next i
	
loop until osgl_button() or osgl_mousebutton()

osgl_fadeout
osgl_cls
osgl_sync(1)

end sub



title:
'title here

puzzlemode=0

puzzlelevel=0

currentbackdrop=1

osgl_clearparticles

osgl_cls

osgl_sync(1)

osgl_fadein

scope 'title

dim as integer tmp=0, tmp2=300, but=0
dim as single tmp3=0

dim as double tmp4=200

const as integer raio=100

'x² + y² = r²
'y² = r² - x²
'y = sqr(r^2 - x^2)

dim as string titleword="NUMBEZZLED"

do

osgl_sync

if tmp2>0 then tmp2-=10

tmp+=5

osgl_wrap(tmp,0,359)

tmp3+=.025

osgl_wrap(tmp3,0,359)

tmp4+=1

osgl_wrap(tmp4,0,359)

drawBackdrop()

'osgl_box

'fundo clock1
'osgl_render(0, 0, 0, 0, 0, tex_clock, 0,_
'			-tmp3*6, 2.5, 2.5, 0,0, 2.2 +.005*cos(tmp3), 2.0+.005*cos(tmp3), 0,0, 0,0, 0,0, 1, 0, rgba(255,255,255,2))

'fundo clock
'for kkk as integer=0 to 1
'osgl_render(0, 0, 0, 0, 0, tex_clock, 0,_
'			tmp3, 1.0, 1.0, 0,0, 2.2 +.005*sin(tmp3)+.03*kkk, 2.0+.005*sin(tmp3)+.03*kkk, 0,0, 1,1, 0,0, 1, 0, rgba(255,255,255,15))
'next kkk

'Ody
'osgl_render(0, -10, 0, 0, tmp2, tex_title, 0,_
'			2*cos(tmp3), 0.75+.01*cos(tmp3), 0.75+.01*cos(tmp3), 0,0, 1,1, 0,0, 1,1, 0,0, 1, 0, rgba(255,255,255,255))

'if rnd<.03 then
'	titleword="NUMBEZZLED"
'end if
'                sqr(raio2 - tmp4^2)/100,_

for iii as integer=1 to 12

osgl_Render(    sin(radians(tmp4+30*iii))*(raio+30*sin(radians(tmp3))), 3*cos(radians(6*tmp4+135*iii)) -.55*cos(radians(tmp4+30*iii))*(raio+20*sin(radians(tmp3))) -55, 0.0, 0.0 ,_
				cos(radians(tmp4+30*iii))*(raio+30*sin(radians(tmp3))),_
                texture_tileset, (iii mod 6),_
                0,_
                2.5*blockscale, 2.5*blockscale,_
                0, 0,_
                1.0, 1.0,_
                0, 0,_
                1.0, 1.0, 0, 0,_
                1, 1,_
                rgba(255,255,255,255) , )


next iii


if rnd<.02 then
	select case rnd
		case 0 to 0.1
			titleword="NUMB3ZZLED"
		'case 0.11 to 0.2
		'	titleword="NUMBE22LED"
		case 0.21 to 0.3
			titleword="NUMBEZ2LED"
		case 0.31 to 0.4
			titleword="NUMBE2ZLED"
		case 0.41 to 0.5
			titleword="NUMBEZZL3D"
		'case 0.51 to 0.6
		'	titleword="NUMB3ZZL3D"
		'case 0.61 to 0.7
		'	titleword="NUMB322L3D"
		case else
			titleword="NUMBEZZLED"
	end select
end if



osgl_print 0,-70+tmp2/3,0,titleword,1.5+.05*sin(tmp3),2.0+.05*sin(tmp3),1,1,rgba(55,220+35*cos(tmp3),205,255)

osgl_print 0,80,0,"Copyright (C) Cleber M. Casali"+CRLF+"http://cmcgames.blogspot.com"+CRLF+"v"+NUMBEZZLED_VERSION,0.35,0.35,1,1,rgba(255,255,255,255)

'osgl_print 110,-99,0, "HI "+str(p.hiscore), 0.5, 0.5, 1, textblend, rgba(255,255,255,255)

#macro launch_donate_page()
	osgl_openfile("https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=D3DP4DXUCKXSU&lc=US&item_name=CMC%20Games%20:%20Odysseasons&no_note=1&no_shipping=1&rm=1&return=http%3a%2f%2fcmcgames%2edarkphear%2ecom%2fp%2fblog%2dpage%2ehtml&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted",1)
	osgl_stopmusic
	osgl_textbox("Thank you very much for your donation. It will help me to continue developing games for you.")
	osgl_fadeout
	goto endprogram
'	if myscreen.fullscreen then 'se estiver em tela cheia
		'osgl_emulateAltEnter
'	end if
'	osgl_playmusic
#endmacro

'buttons
if osgl_fadein_complete then

	if osgl_pausebutton(1) or osgl_esckey or osgl_button or osgl_mousebutton then
		osgl_playsound(sound_beep)
		osgl_sync(1)
			select case osgl_menu(0,38,,"1 Player Normal,1 Player Puzzle,2 Players Versus,View Highscores,How To Play,Quit")
			'select case osgl_menu(0,38,,"1 Player,2 Players Versus,Donate,Quit")
				case 6
					savehiscore()
					osgl_fadeout 5
					osgl_cls	
					osgl_sync(1)
					goto endprogram
				'case 3
				'	launch_donate_page()
				case 5
					howToPlay()
				case 4
					viewHighscores()
				case 3
					but=1
					numplayers=2
					puzzlemode=0
					resetVsWins()
				case 2
					numplayers=1
					but=1
					puzzlemode=1
						'select case osgl_menu(50,10,,"Normal Mode,Puzzle Mode")
						'	case 1
						'		puzzlemode=0
						'	case 2
						'		puzzlemode=1
						'	case else
						'		but=0
						'end select
				case 1
					numplayers=1
					but=1
					puzzlemode=0
						'select case osgl_menu(50,10,,"Normal Mode,Puzzle Mode")
						'	case 1
						'		puzzlemode=0
						'	case 2
						'		puzzlemode=1
						'	case else
						'		but=0
						'end select
			end select
	else
		osgl_print 0,60,0,"Press button to start",0.5,0.5,1,1,rgba(255,255,255,255-127*abs(sin(RADIANS(tmp))))
	end if

end if

osgl_drawparticles()

loop until but

osgl_sync(1)

end scope 'title

osgl_joyrumble(pcur,30,30,150)

osgl_fadeout

osgl_cls

osgl_sync(1)

goto newgame







newgame:

osgl_clearparticles()

osgl_sync(1)

clearPlayers()

if puzzlemode then
	
	countPuzzles()

	player(1).puzzlelevel=puzzlesavelevel

	osgl_range(player(1).puzzlelevel,1,lastpuzzle)

	drawBackdrop()
	
	osgl_sync(1)

	player(1).puzzlelevel=osgl_numbermenu(0, 0, "Puzzle Level Select", 1, player(1).puzzlelevel, player(1).puzzlelevel)
		
	if player(1).puzzlelevel<1 then goto title

	puzzlelevel=player(1).puzzlelevel
	
	randomize player(1).puzzlelevel
	
else

	'difficulty level parameters
	
	diflevel_one=1
	diflevel_bomb=3
	diflevel_nuke=4
	diflevel_reduce2=4
	diflevel_rocket=4
	diflevel_reduce3=5
	diflevel_surprise=4
	diflevel_rock=6
	diflevel_randomizer=6
	diflevel_medusa=4

	randomize timer

end if

rndSort()

currentbackdrop=1

for pcur=numplayers to 1 step -1

	if pcur=1 then pother=2 else pother=1

	if puzzlemode then
	
		loadPuzzle()
		
	else

		player(pcur).puzzlelevel=0

		newLevel()	

		clearTable()
		
	end if

	
	fallblock(pcur).idu=0
	fallblock(pcur).id=0
	fallblock(pcur).nextidu=0
	fallblock(pcur).nextid=0

	newFallBlock()
	
next pcur

osgl_fadein()

goto rotina



'ROTINA-------------------

rotina:

do

for pcur=numplayers to 1 step -1

	if pcur=1 then
		pother=2
		moveflybirds()
	else
		pother=1
	end if

	player(pcur).timecount+=1

	if player(pcur).depositdelay>0 then

		player(pcur).depositdelay-=1

	else

		checkgameover()

		if player(pcur).gameover=0 then
		if player(pother).gameover=0 then

			if player(pcur).dysum=0 then
			if player(pcur).depositdelay=0 then
				checkBirds()
			end if
			end if
			
			moveTable()
	
			playerControls()

			moveFallblock()

			
		end if
		end if
	
	end if

next pcur

osgl_sync()

drawBackdrop()


for pcur=numplayers to 1 step -1

	if pcur=1 then pother=2 else pother=1

	drawTable()

	drawFallblock()

	drawScore()

	drawNextblock()
	
next pcur

drawflybirds()

osgl_drawsprites()

osgl_drawparticles()

osgl_drawmouse()

'loop until osgl_esckey
loop
'ROTINA-----------------

endprogram:

osgl_cls
osgl_sync(1)

osgl_end
'NUMBEZZLED.BAS ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
