require "common"
require "hog_common"
require "ui_common"

-------------------------------------------------------------------------------
-- constants
-------------------------------------------------------------------------------
local c_CellW 				= 19.7
local c_CellH 				= 20
local c_FollowBias			= 10
local c_MoveSpeed 			= 100
local c_FriendThinkTime 	= { 0.5, 0.8 }
local c_FriendSmartness     = 0.90
local c_DeltaTime 			= 0.05
local c_MoveDir 			= { {0, -1}, {1, 0}, {0, 1}, {-1, 0} }
local c_ProbeSamples 		= 5

-------------------------------------------------------------------------------
--  initialization
-------------------------------------------------------------------------------
local bFinished = false
local bFriendWandering = true

local objPlayer = obj( "player" )
local objFriend = obj( "friend" )
set ( "FoundCaption", "visible", false )
set ( "FoundAmount", "visible", false )
set( objPlayer, "visible", true )
set( objFriend, "visible", true )
set ( "TutorialSwitch", "activeidx", "10" )
set( objPlayer, "position", get( "player_start", "position" ) )
set( objFriend, "position", get( "friend_start", "position" ) )
set ( "Hint", "enabled", "false")
set ( "startanim", "visible", "true" )
wait ( 12 )


-------------------------------------------------------------------------------
-- local methods
-------------------------------------------------------------------------------
function CanMove( x, y, nx, ny )
	local objProbe = obj( "labyrinth_probe" )
	for nProbe = 1, c_ProbeSamples do
		local t = nProbe/c_ProbeSamples
		local cx = lerp( nx, x, t )
		local cy = lerp( ny, y, t )
		set( objProbe, "position", cx .. "," .. cy )
		local alpha = tonumber( get( objProbe, "alpha" ) )
		if (alpha > 0.5) then
			return false
		end
	end
	return true
end

function Move( objAvatar, nDir )
	local x, y = parseVec2( get( objAvatar, "position" ) )
	local dir = c_MoveDir[nDir]
	local nx = x + dir[1]*c_CellW
	local ny = y + dir[2]*c_CellH
	if (CanMove( x, y, nx, ny )) then
		local moveT = vec2dist( x, y, nx, ny )/c_MoveSpeed
		for t = 0, moveT, c_DeltaTime do
			local cx = lerp( nx, x, t/moveT )
			local cy = lerp( ny, y, t/moveT )
			set( objAvatar, "position", cx .. "," .. cy )
			wait( c_DeltaTime )
            if bFriendWandering then 
                CheckFriendNear( nx, ny )
            end
		end
		set( objAvatar, "position", nx .. "," .. ny )
        CheckExit()
		return true
	end
	return false
end

function CheckFriendNear( nx, ny )
    local x, y = parseVec2( get( objPlayer, "position" ) )
    local fx, fy = parseVec2( get( objFriend, "position" ) )
    if vec2dist( x, y, fx, fy ) < c_CellW/2 then
        call( "gametracks", "stop" )
        PlayComics( "page_11_1" )
        call( "ComicsTrack", "Stop" )
        call( "MenuTrack", "Stop" )
        call( "gametracks", "play" )
        set( objPlayer, "visible", false )
        set( objFriend, "visible", false )
		objPlayer = obj( "playerfriend" )
        set( objPlayer, "visible", true )
        set( objPlayer, "position", nx .. "," .. ny )
        bFriendWandering = false
    end
end

function CheckExit()
    local x, y = parseVec2( get( objPlayer, "position" ) )
	local ex, ey = parseVec2( get( "exit", "position" ) )
	if vec2dist( x, y, ex, ey ) < c_CellW/2 then
		if bFriendWandering then
            messageBox( "txtFindGlebFirst", "", "OK" )
            y = y - c_CellH
            set( objPlayer, "position", x .. "," .. y )
        else
            bFinished = true
        end
	end
end

local bPlayerMoving = false
function MovePlayer( nDir )
	if bPlayerMoving then return end
	bPlayerMoving = true
	Move( objPlayer, nDir )
	bPlayerMoving = false
end 

function OnUp()		MovePlayer( 1 )	end
function OnRight()	MovePlayer( 2 ) end
function OnDown()	MovePlayer( 3 ) end
function OnLeft()	MovePlayer( 4 ) end

function FriendWandering()
    local nDir = 1
	while bFriendWandering do
		local thinkTime = rand( c_FriendThinkTime[1], c_FriendThinkTime[2] )
		wait( thinkTime )
		-- move friend, use "right-hand" rule to make movement appear natural
        for i = 4, 1, -1 do 
            local cDir = (nDir + i)%4 + 1
		    if Move( objFriend, cDir ) and rand(0.0, 1.0) < c_FriendSmartness then
                nDir = cDir
                break
            end
		end
	end
end

--  start friend wandering 
fork( "FriendWandering" )

-------------------------------------------------------------------------------
--  game loop
-------------------------------------------------------------------------------
while not bFinished do
    wait( 0.05 )
	-- mouse steering
	local x, y = getCursorPos()
	local px, py = parseVec2( get( objPlayer, "positionabs" ) )
	local dx = x - px
	local dy = y - py
	if not bPlayerMoving then
		if math.abs( dx ) > math.abs( dy ) then
			if dx < -c_FollowBias then 
				OnLeft()
			elseif dx > c_FollowBias then
				OnRight()
			end
		else
			if dy < -c_FollowBias then 
				OnUp()
			elseif dy > c_FollowBias then
				OnDown()
			end
		end
	end
end
set ( "FoundCaption", "visible", true )
set ( "FoundAmount", "visible", true )
set ( "Hint", "enabled", "true")
call( "OnWin", "play" )


