#base "../mod.vmt"

"UnlitGeneric" {
	$digit 0 
	$override 0
	
	$leading 0
	$leading_check 0
	
	$left 0
	$left_factor1 0
	$left_factor2 0
	$left_factor3 0
	
	$lt 9999
	$gt -1

	$const_interval	0.06 // update interval
	$const_ten 10 $const_one 1 $const_half 0.5 $const_zero 0
	$const_hun 100 $const_tou 1000

	$value 0 $value_digit 0
	$temp 0.0 $time 0.0 $interval 0.0 $update 0 $oldtime 0.0

	$pos "[0.0 0.0 0.0]" $posx 0.0 $posy 0.0
	$dx 0.0 $dy 0.0 $oldposx 0.0 $oldposy 0.0
	$dx2 0.0 $dy2 0.0 $hd2 0.0
	$number 0.0 $sqrt 0.0
	$hd 0.0 $hspeed 0.0

	Proxies {
		// Reset oldtime on new map
		LessOrEqual {
			lessEqualVar $oldtime greaterVar $const_zero 
			srcVar1 $oldtime srcVar2 $time 
			resultVar $oldtime
		}

		// Get current time
		CurrentTime { resultVar $time }

		// Calculate current interval
		Subtract { srcVar1 $time srcVar2 $oldtime resultVar $interval }

		// Sets $update to 1 if it's time to update
		LessOrEqual {
			lessEqualVar $const_zero greaterVar $const_one
			srcVar1 $interval srcVar2 $const_interval
			resultVar $update
		}

		// Save current time if updating
		LessOrEqual {
			lessEqualVar $oldtime greaterVar $time
			srcVar1 $update srcVar2 $const_zero
			resultVar $oldtime
		}

		// Get player's position
		PlayerPosition { scale 1 resultVar $pos }
		Clamp { srcVar1 $const_zero min "$pos[0]" max "$pos[0]" resultVar $posx }
		Clamp { srcVar1	$const_zero min	"$pos[1]" max "$pos[1]" resultVar $posy }

		// Calculate displacement
		Subtract { srcVar1 $posx srcVar2 $oldposx resultVar $dx }
		Subtract { srcVar1 $posy srcVar2 $oldposy resultVar $dy }

		// Store current position if updating
		// Save current time if updating
		LessOrEqual {
			lessEqualVar $oldposx greaterVar $posx
			srcVar1 $update srcVar2 $const_zero
			resultVar $oldposx
		}
		LessOrEqual {
			lessEqualVar $oldposy greaterVar $posy
			srcVar1 $update srcVar2 $const_zero
			resultVar $oldposy
		}

		// Square of horizontal distance moved
		Multiply { srcVar1 $dx srcVar2 $dx resultVar $dx2 }
		Multiply { srcVar1 $dy srcVar2 $dy resultVar $dy2 }
		Add { srcVar1 $dx2 srcVar2 $dy2 resultVar $hd2 }

		// Start square root
		Equals { srcVar1 $hd2 resultVar $number }
		Equals { srcVar1 $const_one resultVar $sqrt }

		// 20 iterations of Newton's method, sqrt = 0.5*(sqrt + number/sqrt)
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }
		
		Divide { srcVar1 $number srcVar2 $sqrt resultVar $temp }
		Add { srcVar1 $sqrt srcVar2 $temp resultVar $temp }
		Multiply { srcVar1 $const_half srcVar2 $temp resultVar $sqrt }

		// Set horizontal displacement
		Equals { srcVar1 $sqrt resultVar $hd }

		// Calculate horizontal speed
		Divide { srcVar1 $hd srcVar2 $interval resultVar $hspeed }

		// Round horizontal speed
		Add { srcVar1 $hspeed srcVar2 $const_half resultVar $hspeed }
		Int { srcVar1 $hspeed resultVar $hspeed }

		// Change display value if updating
		LessOrEqual {
			lessEqualVar $value greaterVar $hspeed
			srcVar1 $update srcVar2 $const_zero
			resultVar $value
		}

		// Swap digits if ($left == 1)
		Equals { srcVar1 $digit resultVar $override }
	
		Equals { srcVar1 $const_ten resultVar $left_factor1 }
		Equals { srcVar1 $const_hun resultVar $left_factor2 }
		Equals { srcVar1 $const_tou resultVar $left_factor3 }
		Divide { srcVar1 $left_factor1 srcVar2 $digit resultVar $left_factor1 }
		Divide { srcVar1 $left_factor2 srcVar2 $digit resultVar $left_factor2 }
		Divide { srcVar1 $left_factor3 srcVar2 $digit resultVar $left_factor3 }
		LessOrEqual {
			lessEqualVar $override greaterVar $left_factor1
			srcVar1 $value srcVar2 $const_ten
			resultVar $override
		}
		LessOrEqual {
			lessEqualVar $override greaterVar $left_factor2
			srcVar1 $value srcVar2 $const_hun
			resultVar $override
		}
		LessOrEqual {
			lessEqualVar $override greaterVar $left_factor3
			srcVar1 $value srcVar2 $const_tou
			resultVar $override
		}
		LessOrEqual {
			lessEqualVar $digit greaterVar $override
			srcVar1 $left srcVar2 $const_zero
			resultVar $override
		}
		
		// Display digit
		Divide { srcVar1 $value srcVar2 $override resultVar $value_digit }
		WrapMinMax { srcVar1 $value_digit maxVal $const_ten resultVar $frame }
		
		// Set to frame 10 (empty) if ($leading == 1) && digit is leading zero 
		Equals { srcVar1 $const_zero resultVar $leading_check }
		LessOrEqual {
			lessEqualVar $value greaterVar $const_tou
			srcVar1 $leading srcVar2 $const_zero
			resultVar $leading_check
		}
		Add { srcVar1 $leading_check srcVar2 $const_one resultVar $leading_check }
		LessOrEqual {
			lessEqualVar $const_ten greaterVar $frame
			srcVar1 $leading_check srcVar2 $digit
			resultVar $frame
		}
		
		// Set to frame 10 (empty) if (less than $lt or greater than $gt)
		LessOrEqual {
			lessEqualVar $frame greaterVar $const_ten
			srcVar1 $value srcVar2 $lt
			resultVar $frame
		}
		LessOrEqual {
			lessEqualVar $const_ten greaterVar $frame
			srcVar1 $value srcVar2 $gt
			resultVar $frame
		}
	}
}
