class "waiter"
function waiter:__init( mnstr, script_name )
	self.npc_name   = mnstr
    self.npc        = nil
    self.scr_name   = script_name
    self.on_line    = false
end

function waiter:execute()
	if( self:is_on_line() == false ) then return end
    if( self.npc:action() == nil ) then
        action( self.npc, anim( anim.sit_idle, 0 ), cond( cond.time_end, 100000000000000000 ) )
    end
end

function waiter:is_on_line()
	if( level.object( self.npc_name ) ) then
        if( self.on_line ) then return true end
        self.npc = level.object( self.npc_name )
        self.npc:script( true, self.scr_name )
        action( self.npc, anim( anim.sit_idle, 0 ), cond( cond.time_end, 100000000000000000 ) )
        self.on_line = true
    else
    	if( self.on_line == true ) then self.on_line = false end
    end
    return self.on_line
end

local snd = nil
local hit = nil

function waiter:check_completed()
	if( self.on_line == false ) 	then return false end
	if( self.npc:alive() == false ) then return true end
	if( level.actor() == nil ) 		then return false end

    if( self.npc:see( level.actor() ) ) then return true end
    if( distance_between( level.actor(), self.npc ) < 10 ) then return true end
    snd  = self.npc:get_sound_info()
    hit  = self.npc:get_monster_hit_info()

    if( snd.time ~= 0 and snd.who and snd.who:id() == level.actor():id() and distance_between( level.actor(), self.npc ) < 30 ) then return true end
    if( hit.time ~= 0 and hit.who and hit.who:id() == level.actor():id() ) then return true end

    return false
end

function waiter:finalize()
	if( self.on_line and self.npc:get_script() ) then
    	self.npc:script( false, self.scr_name )
    end
end

function action2(obj,...)
    local act = entity_action()
    local i = 1
    while true do
        if (arg[i] ~= nil) then
            act:set_action(arg[i])
        else
            break
        end
        i = i + 1
    end
    if (obj ~= nil) then
        obj:command(act,false)
    end
    return  entity_action(act)
end
