// IW6 GSC SOURCE
// Generated by https://github.com/xensik/gsc-tool

init_elevator( var_0 )
{
    var_1 = getent( var_0.name, "targetname" );
    var_1.unresolved_collision_func = ::handleunreslovedcollision;
    var_1.doors = [];

    if ( isdefined( var_0.doors ) )
    {
        foreach ( var_8, var_3 in var_0.doors )
        {
            var_4 = [];

            foreach ( var_6 in var_3 )
            {
                var_4[var_4.size] = setupdoor( var_6 + "left", 0, var_0.doormovedist );
                var_4[var_4.size] = setupdoor( var_6 + "right", 1, var_0.doormovedist );
            }

            var_1.doors[var_8] = var_4;
        }

        if ( isdefined( var_0.dooropentime ) )
            var_1.dooropentime = var_0.dooropentime;
        else
            var_1.dooropentime = 2;

        var_1.doorspeed = var_0.doormovedist / var_1.dooropentime;

        if ( isdefined( var_0.autoclosetimeout ) )
            var_1.autoclosetimeout = var_0.autoclosetimeout;
        else
            var_1.autoclosetimeout = 10;

        var_1.trigblock = getent( var_0.trigblockname, "targetname" );

        if ( isdefined( var_0.autoclosetimeout ) )
            var_1.autoclosetimeout = var_0.autoclosetimeout;
        else
            var_1.autoclosetimeout = 10;

        var_1.dooropensfx = var_0.dooropensfx;
        var_1.doorclosesfx = var_0.doorclosesfx;
    }

    if ( isdefined( var_0.movetime ) )
        var_1.movetime = var_0.movetime;
    else
        var_1.movetime = 5;

    var_1.destinations = [];
    var_1.pathblockers = [];
    var_1.buttons = getentarray( var_0.buttons, "targetname" );

    foreach ( var_10 in var_1.buttons )
        var_10 setupbutton( var_1 );

    var_12 = common_scripts\utility::getstructarray( var_0.destinations, "targetname" );

    foreach ( var_14 in var_12 )
        var_1 setupdestination( var_14 );

    var_1.destinationnames = var_0.destinationnames;
    var_1.curfloor = var_0.destinationnames[0];
    var_1.requestedfloor = var_1.curfloor;
    var_1.doorstate = 0;

    if ( isdefined( var_0.models ) )
    {
        var_16 = getentarray( var_0.models, "targetname" );

        if ( isdefined( var_16 ) )
        {
            foreach ( var_18 in var_16 )
                var_18 linkto( var_1 );
        }
    }

    var_1 thread elevatorthink();

    if ( var_1.doors.size > 0 )
        var_1 thread openelevatordoors( var_1.curfloor, 0 );

    var_1.startsfx = var_0.startsfx;
    var_1.stopsfx = var_0.stopsfx;
    var_1.loopsfx = var_0.loopsfx;
    var_1.beepsfx = var_0.beepsfx;
    var_1.onmovecallback = var_0.onmovecallback;
    var_1.onarrivedcallback = var_0.onarrivedcallback;
    return var_1;
}

setupdoor( var_0, var_1, var_2 )
{
    var_3 = getent( var_0, "targetname" );

    if ( isdefined( var_3 ) )
    {
        var_3.closepos = var_3.origin;

        if ( isdefined( var_3.target ) )
        {
            var_4 = common_scripts\utility::getstruct( var_3.target, "targetname" );
            var_3.openpos = var_4.origin;
        }
        else
        {
            var_5 = anglestoforward( var_3.angles ) * var_2;
            var_3.openpos = var_3.origin + var_5;
        }

        return var_3;
    }
    else
        return;
}

setupbutton( var_0 )
{
    self.owner = var_0;

    if ( isdefined( self.target ) )
    {
        var_1 = common_scripts\utility::getstruct( self.target, "targetname" );
        setupdestination( var_1 );
    }

    enablebutton();
}

setupdestination( var_0 )
{
    if ( isdefined( var_0 ) )
    {
        self.destinations[var_0.script_label] = var_0.origin;

        if ( isdefined( var_0.target ) )
        {
            var_1 = getent( var_0.target, "targetname" );

            if ( isdefined( var_1 ) )
                self.pathblockers[var_0.script_label] = var_1;
        }
    }
}

enablebutton()
{
    self sethintstring( &"MP_ELEVATOR_USE" );
    self makeusable();
    thread buttonthink();
}

disablebutton()
{
    self makeunusable();
}

buttonthink()
{
    var_0 = self.owner;
    var_0 endon( "elevator_busy" );

    for (;;)
    {
        self waittill( "trigger" );

        if ( !isdefined( self.script_label ) || self.script_label == "elevator" )
        {
            if ( var_0.curfloor == var_0.destinationnames[0] )
                var_0.requestedfloor = var_0.destinationnames[1];
            else
                var_0.requestedfloor = var_0.destinationnames[0];
        }
        else
            var_0.requestedfloor = self.script_label;

        var_0 notify( "elevator_called" );
    }
}

elevatorthink()
{
    var_0 = self.doors.size > 0;

    for (;;)
    {
        self waittill( "elevator_called" );

        foreach ( var_2 in self.buttons )
            var_2 disablebutton();

        if ( self.curfloor != self.requestedfloor )
        {
            if ( self.doorstate != 0 )
            {
                self notify( "elevator_stop_autoclose" );
                thread closeelevatordoors( self.curfloor );
                self waittill( "elevator_doors_closed" );
            }
            else if ( !var_0 )
                elevatorblockpath( self.curfloor );

            elevatormovetofloor( self.requestedfloor );
            wait 0.25;
        }

        if ( var_0 )
        {
            thread openelevatordoors( self.curfloor, 0 );
            self waittill( "elevator_doors_open" );
        }
        else
            elevatorclearpath( self.curfloor );

        foreach ( var_2 in self.buttons )
            var_2 enablebutton();
    }
}

elevatormovetofloor( var_0 )
{
    var_1 = self.destinations[var_0];
    var_2 = var_1[2] - self.origin[2];

    if ( isdefined( self.doors["elevator"] ) )
    {
        foreach ( var_4 in self.doors["elevator"] )
            var_4 movez( var_2, self.movetime );
    }

    self movez( var_2, self.movetime );

    if ( isdefined( self.onmovecallback ) )
        self thread [[ self.onmovecallback ]]( var_0 );

    wait( self.movetime );

    if ( isdefined( self.beepsfx ) )
        self playsound( self.beepsfx );

    self.curfloor = self.requestedfloor;

    if ( isdefined( self.onarrivedcallback ) )
        self thread [[ self.onarrivedcallback ]]( self.curfloor );
}

openelevatordoors( var_0, var_1 )
{
    var_2 = self.doors[var_0];
    self.doorstate = 1;
    var_3 = var_2[0];
    var_4 = ( var_3.openpos[0], var_3.openpos[1], var_3.origin[2] );
    var_5 = var_4 - var_3.origin;
    var_6 = length( var_5 );
    var_7 = var_6 / self.doorspeed;
    var_8 = 0.25;

    if ( var_7 == 0.0 )
    {
        var_7 = 0.05;
        var_8 = 0.0;
    }
    else
    {
        self playsound( self.dooropensfx );
        var_8 = min( var_8, var_7 );
    }

    foreach ( var_3 in var_2 )
        var_3 moveto( ( var_3.openpos[0], var_3.openpos[1], var_3.origin[2] ), var_7, 0.0, var_8 );

    wait( var_7 );
    self.doorstate = 2;
    self notify( "elevator_doors_open" );
    elevatorclearpath( var_0 );

    if ( var_1 )
        thread elevatordoorsautoclose();
}

closeelevatordoors( var_0 )
{
    self endon( "elevator_close_interrupted" );
    thread watchcloseinterrupted( var_0 );
    var_1 = self.doors[var_0];
    self.doorstate = 3;
    var_2 = var_1[0];
    var_3 = ( var_2.closepos[0], var_2.closepos[1], var_2.origin[2] );
    var_4 = var_3 - var_2.origin;
    var_5 = length( var_4 );

    if ( var_5 != 0.0 )
    {
        var_6 = var_5 / self.doorspeed;

        foreach ( var_2 in var_1 )
            var_2 moveto( ( var_2.closepos[0], var_2.closepos[1], var_2.origin[2] ), var_6, 0.0, 0.25 );

        self playsound( self.doorclosesfx );
        wait( var_6 );
    }

    self.doorstate = 0;
    elevatorblockpath( var_0 );
    self notify( "elevator_doors_closed" );
}

watchcloseinterrupted( var_0 )
{
    self endon( "elevator_doors_closed" );
    var_1 = 1;

    foreach ( var_3 in level.characters )
    {
        if ( var_3 istouchingtrigger( self.trigblock ) )
        {
            var_1 = 0;
            break;
        }
    }

    if ( var_1 )
        self.trigblock waittill( "trigger" );

    self notify( "elevator_close_interrupted" );
    openelevatordoors( var_0, 1 );
}

istouchingtrigger( var_0 )
{
    return isalive( self ) && self istouching( var_0 );
}

elevatordoorsautoclose()
{
    self endon( "elevator_doors_closed" );
    self endon( "elevator_stop_autoclose" );
    wait( self.autoclosetimeout );
    closeelevatordoors( self.curfloor );
}

handleunreslovedcollision( var_0 )
{
    if ( !isplayer( var_0 ) )
        var_0 dodamage( 1000, var_0.origin, self, self, "MOD_CRUSH" );
}

elevatorclearpath( var_0 )
{
    var_1 = self.pathblockers[var_0];

    if ( isdefined( var_1 ) )
    {
        var_1 connectpaths();
        var_1 hide();
        var_1 notsolid();
    }
}

elevatorblockpath( var_0 )
{
    var_1 = self.pathblockers[var_0];

    if ( isdefined( var_1 ) )
    {
        var_1 show();
        var_1 solid();
        var_1 disconnectpaths();
    }
}
