<?php
	$maxZ = 32;


	$eol = "\r\n";
	// init project table
    $rast = -24;
    $projectTblY = '';
	$projectTblX = '';
	for ($x = 0; $x < 48; $x++) {
		$xx = $x - 24;
		$valuesX = array();
		$valuesY = array();
		for ($z = 0; $z < $maxZ; $z += 2) {
			$zz = $z - 16;
			$pp = round($rast * $xx / ($zz + $rast));
			$px = $pp + 32 - 4 + 8 +1 -2;
			$py = $pp + 22 - 4;

			$checkX = ($px >= 0 && $px < 64 + 8*2);
			$checkY = ($py >= -8 && $py < 48 + 8);
			if (!$checkX) {
				echo 'px=' . $px . $eol;
			}
			if (!$checkY) {
				echo 'py=' . $py . $eol;
			}

			//$valuesX[] = $px;
			$valuesX[] = $checkX ? $px : 0;
			
			$yyy = 'BUFF ' . ($py > 0 ? '+ ' : '- ') . abs($py * 64);
			
			//$valuesY[] = $yyy;
			$valuesY[] = $checkY ? $yyy : 'BUFF - 64*8';
        }
		$projectTblY .= "\tDW\t" . implode(', ', $valuesY) . $eol;
		$projectTblX .= "\tDB\t" . implode(', ', $valuesX) . $eol;
    }

	file_put_contents(__DIR__ . '/tbl_proj_x.asm', $projectTblX);
	file_put_contents(__DIR__ . '/tbl_proj_y.asm', $projectTblY);



	$rMax = 12;
	$rMin = 9;
	$alphaStep = 64+11;//8*7=56;

	// init stars
	$starQty = 27;
	$codeStars = '';
	$alpha = 0;
	for ($i = 0; $i < $starQty; $i++) {
		$r = $rMin + ($rMax - $rMin) * rand(0, 1);
		$alpha += $alphaStep;
		$x = 24 + round($r * cos($alpha * pi() / 128));
		$y = 24 + round($r * sin($alpha * pi() / 128));
		$codeStars .= "\tDB\t" . floor($maxZ/2 * $i / $starQty) . $eol;
		$codeStars .= "\tDW\tTBL_PROJ_X + " . ($maxZ/2) . " * " . $x . $eol;
		$codeStars .= "\tDW\tTBL_PROJ_Y + " . ($maxZ) . " * " . $y . $eol;
	}
	file_put_contents(__DIR__ . '/tbl_stars.asm', $codeStars);	


	// init stars
	$codeStars = '';
	$alpha = 0;
	for ($i = 0; $i < 256; $i++) {
		$r = $rMin + ($rMax - $rMin) * rand(0, 1);
		$alpha += $alphaStep;
		$x = 24 + round($r * cos($alpha * pi() / 128));
		$y = 24 + round($r * sin($alpha * pi() / 128));
		$codeStars .= "\tDW\tTBL_PROJ_X + " . ($maxZ/2) . " * " . $x . $eol;
		$codeStars .= "\tDW\tTBL_PROJ_Y + " . ($maxZ) . " * " . $y . $eol;
	}
	file_put_contents(__DIR__ . '/stars_list.asm', $codeStars);	
