'
'
'

?zone = 16' chunk
?base = 3' aire de jeu
?max = 3' taille max d'une pièce ?? pas sûr


?pop_zoom = 0
?flag_start = 0' bloqué + 1ère pièce = game over


' style
?asphalt = 71



fov 50

delete "light"

create orb as "sun"
color "sun" to #8cf
move "sun" to ?zone * 2, ?zone / 2, ?zone

power "glow" to 50

create shadow as "shadow"
?sx = ?zone
move "shadow" to ?zone, ?zone, ?zone
target "shadow" to ?zone * 1.2, ?zone / 4, ?zone / 2
power "shadow" to 50
color "shadow" to #fed
shadow "shadow" to ?zone * 1.5

'

goto "niveau_suivant"
goto "start_level"
goto "tecity"

'

label "tecity"
	do
		
		if (key "x") = 1
			goto "fin_etage"
			goto "game_over"
		end
		
		if ?over = 0
			
			'surtout d'abord déplacement
			goto "deplacement"
			goto "pose"
			
			?cx = ?zone / 2
			?cy = ?zone + ?niveau * 2
			?cz = (?zone - ?niveau) / 2
		else
			goto "outro"
		end
		
		?cam_x + (?cx - ?cam_x) / 20
		?cam_y + (?cy - ?cam_y) / 60
		?cam_z + (?cz - ?cam_z) / 20
		
		move "camera" to ?cam_x, ?cam_y, ?cam_z
		target "camera" to ?zone / 2, 1, ?zone / 2
		
		update
		
	loop
return

'

label "start_level"
	
	hide fog
	
	?over = 0
	?niveau = 0
	?piece = 0
	
	?s = "sol"
	create sprite ?zone as ?s
	color 25
	add ?s box 1, 1, 1 to ?zone, 1, ?zone
	center ?s to 1, 1, 1
	move ?s to 1, 0, 1
	
	' arbres
	color 15
	for 1 to 12
		?x = random 2 to ?zone - 1
		?z = random 2 to ?zone - 1
		add ?s dot ?x, 2, ?z
	next
	
	' zone de jeu
	?u = round (?zone - ?base) / 2 + 1
	?v = ?zone - ?u + 1
	color 0
	add ?s box ?u - 1, 2, ?u - 1 to ?v + 1, 2, ?v + 1
	
	
	'
	goto "debut_etage"
	
	'
	?px = (round ?zone / 2)
	?pz = (round ?zone / 2)
	
	
return


'

label "debut_etage"
	
	?niveau + 1
	?flag_start = 1
	?niveau_blocs = 0
	
	' zone autorisée
	create sprite ?zone as "socle"
	move "socle" to 1, ?niveau - .4, 1
	zoom "socle" to 1, .01, 1
	'hide "socle"
	paint "socle" to "neon"
	center "socle" to 1, 1, 1
	
	?c = #888
	color ?c
	if ?niveau = 1
		?u = round (?zone - ?base) / 2 + 1
		?v = ?zone - ?u + 1
		add "socle" box ?u, 1, ?u to ?v, 1, ?v
		'add "socle" dot to 1,1,1
		'add "socle" dot to ?zone,1,?zone
	else
		?prev = "e" + (?niveau - 1)
		for 1 to ?zone as ?x
			for 1 to ?zone as ?z
				if (cell ?prev to ?x, 1, ?z) > 0
					cell "socle" to ?x, 1, ?z = ?c
				end
			next
		next
	end
	
	
	'
	goto "nouvelle_piece"
	
return

'

label "nouvelle_piece"
	
	' piece de tetris
	if ?piece = 0
		?piece = random 1 to 6
	else
		' conserve la précédente
	end
	
	create sprite ?max as "piece"
	color ?piece + 2
	
	if ?piece = 1
		' L
		add "piece" dot 1, 1, 1
		add "piece" dot 2, 1, 1
		add "piece" dot 1, 1, 2
	else if ?piece = 2
		' carré
		add "piece" frame 1, 1, 1 to 2, 1, 2
	else if ?piece = 3
		' T
		add "piece" dot 1, 1, 1
		add "piece" dot 2, 1, 1
		add "piece" dot 3, 1, 1
		add "piece" dot 2, 1, 2
	else if ?piece = 4
		' +
		add "piece" dot 2, 1, 1
		add "piece" dot 2, 1, 2
		add "piece" dot 2, 1, 3
		add "piece" dot 1, 1, 2
		add "piece" dot 3, 1, 2
	else if ?piece = 5
		'U
		add "piece" dot 1, 1, 1
		add "piece" dot 2, 1, 1
		'add "piece" dot to 3,1,1
		add "piece" dot 2, 1, 2
	else if ?piece = 6
		' . de Paul
		add "piece" dot 1, 1, 1
	end
	
	center "piece" to 1, 1, 1
	?pop_zoom = 0
	
	' pos "pas possible
	?py = ?niveau + 1
	
	?vpx = ?px
	?vpz = ?pz
	' pas vy
	
	if (goto "test_possible") = 0
		if ?flag_start = 0
			goto "fin_etage"
		else
			goto "game_over"
		end
	end
	
	?flag_start = 0
	
return

'

label "test_possible"
	
	' reste une place pour la pièce en cours ?
	?possible = 0
	
	for 1 to ?zone as ?x
		for 1 to ?zone as ?z
			?valide = goto "test_pose", ?x, ?z
			?possible + ?valide
		next
	next
	
return ?possible

'


label "test_pose"
	param ?vx
	param ?vz
	
	' ok pour poser la pièce ici ?
	
	?valide = 1
	
	for 1 to ?max as ?mx
		for 1 to ?max as ?mz
			?dx = ?vx + (?mx - 1)
			?dz = ?vz + (?mz - 1)
			if (cell "piece" to ?mx, 1, ?mz) > 0' and (peek "niveau" to ?dx,?dy,1) <> 0
				if (cell "socle" to ?dx, 1, ?dz) < 1
					?valide = 0
				else if (is "niveau") = 0
					'
				else if (cell "niveau" to ?dx, 1, ?dz) > 0
					?valide = 0
				end
			end
		next
	next
	
return ?valide

'

label "deplacement"
	
	?futur_x = ?px
	?futur_z = ?pz
	
	if (press "left") = 1
		?futur_x - 1
	else if (press "right") = 1
		?futur_x + 1
	else if (press "up") = 1
		?futur_z + 1
	else if (press "down") = 1
		?futur_z - 1
	end
	
	if ?px <> ?futur_x or ?pz <> ?futur_z
		'?valide = gosub test_place ?futur_x,?futur_y
		if (goto "test_deplace", ?futur_x, ?futur_z) = 1
			?px = ?futur_x
			?pz = ?futur_z
			'gosub touche
			if (goto "test_pose", ?px, ?pz) = 1
				?py = ?niveau
			else
				?py = ?niveau + 1
			end
		end
	end
	
	?vpx + (?px - ?vpx) / 3
	?vpz + (?pz - ?vpz) / 3
	?vpy + (?py - ?vpy) / 15
	
 	' anim et place
	?pop_zoom + (1 - ?pop_zoom) / 20 
	 zoom "piece" to ?pop_zoom
	move "piece" to ?vpx, ?vpy, ?vpz
	
return

'

label "test_deplace"
	param ?vx
	param ?vz
	
	' ok pour déplacer la pièce ici ?
	' ne doit pas sortir de la zone définie
	?valide = 1
	for 1 to ?max as ?mx
		for 1 to ?max as ?mz
			?dx = ?vx + (?mx - 1)
			?dz = ?vz + (?mz - 1)
			if (cell "piece" to ?mx, 1, ?mz) > 0
				'if (peek "socle" to ?dx,1,?dy) < 0
				if ?dx < 1 or ?dz < 1 or ?dx > ?zone or ?dz > ?zone
					' en dehors de la plage
					?valide = 0
				end
			end
		next
	next
	
return ?valide

'

label "pose"
	
	if (press once "space") = 1
		if (goto "test_pose", ?px, ?pz) = 1
			
			print "POSE"
			' nouveau niveau
			if (is "niveau") = 0
				create sprite ?zone as "niveau"
				center "niveau" to 1, 1, 1
			end
			
			color 2
			for 1 to ?max as ?mx
				for 1 to ?max as ?mz
					if (cell "piece" to ?mx, 1, ?mz) > 0
						?dx = ?px + (?mx - 1)
						?dz = ?pz + (?mz - 1)
						add "niveau" dot ?dx, 1, ?dz
					end
				next
			next
			
			move "niveau" to 1, ?niveau, 1
			
			?piece = 0
			goto "nouvelle_piece"
		end
	end
	
return

'

label "fin_etage"
	
	print "FIN ETAGE ?niveau"
	
	create sprite ?zone as "e?niveau"
	paint "e?niveau" to "metal"
	center "e?niveau" to 1, 1, 1
	
	if (is "niveau") = 0
		' pas encore débuté
	else
		for 1 to ?zone as ?x
			for 1 to ?zone as ?z
				if (cell "niveau" to ?x, 1, ?z) > 0
					cell "e?niveau" to ?x, 1, ?z = 41
				end
			next
		next
	end
	move "e?niveau" to 1, ?niveau, 1
	
	'delete "socle"
	delete "niveau"
	'delete "piece"
	
	goto "debut_etage"
	
return

'

label "game_over"
	
	?over = 1
	print "GAME OVER"
	
	goto "fin_immeuble"
	
	'
	delete "socle"
	delete "niveau"
	delete "piece"
	
	?angle = 0
	
	background 3
	show fog 3 to ?zone * 6
	
return

'

label "fin_immeuble"
	
	'
	?voxel = "Q"
	create sprite ?zone * 3 as ?voxel
	?ratio = 1 / 3
	zoom ?voxel to ?ratio
	'center ?voxel to 2.5,2.5,2.5' POURQUOI !? pour 50
	center ?voxel to 4, 4, 4' pour 33
	move ?voxel to 1, 1, 1
	paint ?voxel to "metal"
	
	color 99
	for 1 to ?niveau as ?n
		if (is "e?n") = 1
			?outro_height = ?n
			zoom "e?n" to .99, .99, .99
			for 1 to ?zone as ?x
				for 1 to ?zone as ?z
					if (cell "e?n" to ?x, 1, ?z) > 0
						
						?t = random 1 to 3
						
						?xx = ?x * 3
						?zz = ?z * 3
						?yy = ?n * 3
						if (random 1 to 2) = 1
							?c = #004
						else
							?c = #220
						end
						
						color 41
						add ?voxel frame ?xx, ?yy, ?zz to ?xx + 2, ?yy + 2, ?zz + 2
						color ?c
						add ?voxel frame ?xx, ?yy + 1, ?zz to ?xx + 2, ?yy + 1, ?zz + 2
						color 41
						add ?voxel dot ?xx + 1, ?yy + 2, ?zz + 1
						
						if ?t = 1
							add ?voxel frame ?xx + 1, ?yy, ?zz to ?xx + 1, ?yy + 2, ?zz + 2
						else if ?t = 2
							add ?voxel frame ?xx, ?yy, ?zz + 1 to ?xx + 2, ?yy + 2, ?zz
						end
					end
				next
			next
			
			color 1
			fill "e?n"
			delete "e?n"' temp - relook
		else
			?niveau - 1
		end
	next
	
	
	?s = "sol"
	color ?asphalt
	add ?s frame 1, 1, 1 to ?zone, 1, ?zone
	
	?u = round (?zone - ?base) / 2 + 1
	?v = ?zone - ?u + 1
	'add "sol" box ?u -1 ,1 , ?u -1 to ?v +1 ,1 , ?v +1
	add ?s box ?u, 1, ?u to ?v, 1, ?v
	
return

'

label "outro"
	
	?voxel = "Q"
	
	if ?outro_height > 0
		?outro_height - .02
		move ?voxel to 1, 1 - ?outro_height, 1
	end
	
	?m = ?zone / 2
	?n = ?niveau + 1
	?angle + .5
	
	' inverse de y
	?rad = ?n * 2 - ?n * (cos ?angle / 2)
	
	?cx = ?m + ?rad * (sin ?angle)
	?cy = ?n + ?n * .5 * (sin ?angle / 2)
	?cz = ?m + ?rad * (cos ?angle)
	
	if (press once "space") = 1
		goto "niveau_suivant"
	end
	
return

'

label "niveau_suivant"
	
	'	déplace
	'	Q1
	'	sol1
	
	?base + 1
	goto "start_level"
	
return
x