' Have fun with your new project!
'
' afficher dernière pièce impossible...
'
' effet build immeuble
'


?zone = 16
?base = 3
?max = 3

?zoom_piece = 0
?debut_etage = 0' bloqu& 1ère pièce = game over

?quartier = 0
?ville_x = 0
?ville_z = 0

' style
?asphalt = 71
'

fov 50

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 100
color "shadow" to #fed
shadow "shadow" to ?zone * 2

'

goto "niveau_suivant"
goto "debut_immeuble"
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 "debut_immeuble"
	
	hide fog
	
	?over = 0
	?niveau = 0
	?piece = 0
	
	?s = "sol?quartier"
	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 = dice 2 to ?zone - 1
		?z = dice 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
	?debut_etage = 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 1 + ?max,1,1 + ?max to ?zone - ?max,1,?zone - ?max
		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 = dice 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
	?zoom_piece = 0
	
	' pos "pas possible
	?py = ?niveau + 1
	
	?vpx = ?px
	?vpz = ?pz
	' pas vy
	
	if (goto "test_possible") = 0
		if ?debut_etage = 0
			goto "fin_etage"
		else
			goto "game_over"
		end
	end
	
	?debut_etage = 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
			'if ?futur_x > 0 and ?futur_y > 0 and ?futur_x < ?zone - ?max and ?futur_y < ?zone - ?max
			?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
	?zoom_piece + (1-?zoom_piece) / 20
	zoom "piece" to ?zoom_piece
	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?quartier"
	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 = dice 1 to 3
						
						?xx = ?x * 3
						?zz = ?z * 3
						?yy = ?n * 3
						if (dice 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
	'copy "socle" to "socle-?quartier"
	
	?s = "sol?quartier"
	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?quartier"
	
	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
	
	'	trouver une position pour l'immeuble
	?distance = 0
	?dispo = 0
	?loop = 0
	
	do
		?qx = (dice -?distance to ?distance)
		?qz = (dice -?distance to ?distance)
		?coords = "?qx,?qz"
		
		if ?qx <> 0 and ?qz <> 0
			' pas en diagonale
		else if [ville, ?coords] = 0
			' disponible
			[ville, ?coords] = ?quartier + 1
			?dispo = 1
		else if ?loop > 30
			' pas de solution
			?loop = 0
			?distance + 1
			print "distance... ?distance"
		else
			?loop + 1
		end
		
	loop if ?dispo = 0
	
	' on déplace tous les autres
	for 1 to ?quartier step 1 as ?q
		?dx = (-?qx - ?ville_x) * ?zone
		?dz = (-?qz - ?ville_z) * ?zone
		move "sol?q" by ?dx, 0, ?dz
		move "Q?q" by ?dx, 0, ?dz
	next
	print "ville = ?ville_x,?ville_z"
	print "target = ?qx,?qz"
	print "deplace = ?dx,?dz"
	?ville_x = -?qx
	?ville_z = -?qz
	
	?quartier + 1
	?base + 1
	?over = 0
	goto "debut_immeuble"
	
return
x