Script:Files:script/misc/spawn master.script

From Mod Wiki
/*
===============================================================================

spawn_master.script

contains the spawn masters

===============================================================================
*/

object spawn_master {
    void            preinit();
    void            destroy();

    void            SetupCommandmapIcon();
    void            ClearCommandmapIcon();
    void            ClearCommandmapSelectedIcon();

    void            OnSpawnPointSelected( float isDefault );
    void            OnSpawnPointDeselected();
    void            OnGuiMessage( entity p );
    void            OnSetTeam( object oldTeam, object newTeam );
    void            OnHighlight( boolean set );

    boolean            CanSetSpawn() { return true; }

    void            DeployTeam( object team );
    void            UnDeployTeam( object team );

    float            commandmapIcon;
    float            commandmapSelectedIcon;
    float            commandmapHighlightIcon;
}

void spawn_master::preinit() {
    commandmapIcon = -1;
    commandmapSelectedIcon = -1;
    commandmapHighlightIcon = -1;
}

void spawn_master::destroy() {
    ClearCommandmapIcon();
    ClearCommandmapSelectedIcon();

    object team = getGameTeam();
    if ( team != $null_entity ) {
        team.unRegisterSpawnPoint( self );
    }
    if( commandmapHighlightIcon != -1 ) {
        sys.freeCMIcon( self, commandmapHighlightIcon );
        commandmapHighlightIcon = -1;        
    }
}

void spawn_master::OnSetTeam( object oldTeam, object newTeam ) {
    if ( oldTeam != $null_entity ) {
        oldTeam.unRegisterSpawnPoint( self );
    }

    UnDeployTeam( oldTeam );
    DeployTeam( newTeam );

    if ( newTeam == $null_entity ) {
        ClearCommandmapIcon();
    } else {
        newTeam.registerSpawnPoint( self );

        SetupCommandmapIcon();
    }
}

void spawn_master::DeployTeam( object team ) {
    if ( team == $null_entity ) {
        return;
    }

    string teamName = team.getName();
    string lastMatch;
    entity targetEnt;

    string targetPrefix = "target_" + teamName;

    while ( true ) {
        lastMatch = getNextKey( targetPrefix, lastMatch );
        if ( lastMatch == "" ) {
            break;
        }

        targetEnt = getEntityKey( lastMatch );
        targetEnt.vOnDeploy();
    }
}

void spawn_master::UnDeployTeam( object team ) {
    if ( team == $null_entity ) {
        return;
    }

    string teamName = team.getName();
    string lastMatch;
    entity targetEnt;

    string targetPrefix = "target_" + teamName;

    while ( true ) {
        lastMatch = getNextKey( targetPrefix, lastMatch );
        if ( lastMatch == "" ) {
            break;
        }

        targetEnt = getEntityKey( lastMatch );
        targetEnt.vOnUnDeploy();
    }
}

void spawn_master::SetupCommandmapIcon() {
    ClearCommandmapIcon();

    object team = getGameTeam();
    if ( team == $null_entity ) {
        return;
    }

    commandmapIcon = sys.allocCMIcon( self, getFloatKey( "icon_sort_cm" ) );

    sys.setCMIconMaterial( commandmapIcon, GetMaterial( getKey( "mtr_icon_" + team.getName() ) ) );
    sys.setCMIconFlag( commandmapIcon, CMF_TEAMONLY );
    sys.setCMIconFlag( commandmapIcon, CMF_ALWAYSKNOWN );
    sys.setCMIconFlag( commandmapIcon, CMF_ONLYSHOWONFULLVIEW );
    //sys.setCMIconColorMode( commandmapIcon, CM_ALLEGIANCE );
    sys.setCMIconSize( commandmapIcon, getFloatKey( "icon_size_cm" ) );
    sys.setCMIconGuiMessage( commandmapIcon, "selectspawn" );
}

void spawn_master::OnGuiMessage( entity p ) {
    if ( getEntityAllegiance( p ) != TA_FRIEND ) {
        return;
    }

    string message = sys.argv( 0 );
    if ( message == "selectspawn" ) {
        if ( !CanSetSpawn() ) {
            return;
        }
        p.setSpawnPoint( self );

        if ( p.isLocalPlayer() ) {
            sys.startSoundDirect( getKey( "snd_selected" ), SND_STRUCTURE_ALARM );
        }
    }
}

void spawn_master::ClearCommandmapIcon() {
    if ( commandmapIcon != -1 ) {
        sys.freeCMIcon( self, commandmapIcon );
        commandmapIcon = -1;
    }
}

void spawn_master::ClearCommandmapSelectedIcon() {
    if ( commandmapSelectedIcon != -1 ) {
        sys.freeCMIcon( self, commandmapSelectedIcon );
        commandmapSelectedIcon = -1;
    }
}

void spawn_master::OnSpawnPointSelected( float isDefault ) {
    ClearCommandmapSelectedIcon();

    commandmapSelectedIcon = sys.allocCMIcon( self, getFloatKey( "icon_sort_selected_cm" ) );

    if ( isDefault ) {
        sys.setCMIconMaterial( commandmapSelectedIcon, GetMaterial( getKey( "mtr_icon_default" ) ) );
    } else {
        sys.setCMIconMaterial( commandmapSelectedIcon, GetMaterial( getKey( "mtr_icon_selected" ) ) );
    }
    sys.setCMIconFlag( commandmapSelectedIcon, CMF_ONLYSHOWONFULLVIEW );
    sys.setCMIconFlag( commandmapSelectedIcon, CMF_ALWAYSKNOWN );
    sys.setCMIconSize( commandmapSelectedIcon, getFloatKey( "icon_size_cm" ) );
}

void spawn_master::OnSpawnPointDeselected() {
    ClearCommandmapSelectedIcon();
}

void spawn_master::OnHighlight( boolean set ) {
    vector color;
    color_x = 1;
    color_y = 1;
    color_z = 1;

    if( commandmapHighlightIcon == -1 && set ) {
        commandmapHighlightIcon = sys.allocCMIcon( self, getFloatKey( "icon_sort_cm" ) );
        sys.setCMIconFlag( commandmapHighlightIcon, CMF_ONLYSHOWONFULLVIEW );        
        sys.setCMIconDrawMode( commandmapHighlightIcon, DM_CROSSHAIR );
        sys.setCMIconColor( commandmapHighlightIcon, color, 0.5 );
        sys.setCMIconMaterial( commandmapHighlightIcon, GetMaterial( getKey( "mtr_commandmapicon_tracking" ) ) );
        return;
    }

    if( !set && commandmapHighlightIcon != -1 ) {
        sys.freeCMIcon( self, commandmapHighlightIcon );
        commandmapHighlightIcon = -1;
    }
}



object spawn_host {
    void            preinit();
    void            init();
    void            destroy();
    void            syncFields();

    void            RemoveThread( float removeTime );
    void            Fizzle();

    void            Idle();

    void            vSetOwner( entity o );

    float            GetActivateCode( entity p, float distance );

    boolean            CanSetSpawn();

    void            FreeSelectTask();
    void            MakeSelectTask();

    void            CheckContextToolTip( float allegiance, float code, entity p );

    void            SetupCommandmapIcon();
    void            ClearCommandmapIcon();

    // game callbacks
    float            OnUpdateCrosshairInfo( entity p );
    float            OnActivate( entity p, float distance );
    void            OnPlayerSpawned( entity p );
    void            OnKilled( entity inflictor, entity attacker, float damage, vector direction, float location );

    void            OnSpawnPointActivate( entity p );
    void            OnSpawnPointDeactivate( entity p );

    void            OnSpawnPointSelected( float isDefault );
    void            OnSpawnPointDeselected();

    void            OnTimeStartedChanged();
    void            OnSpawnerChanged();

    void            vOnLocalTeamChanged();
    void            UpdateVisualState();

    string            vGetQuickChatString( entity p );

    void            vOnContextDestroy( entity p );

    void            vSetBody( entity body );

    void            InitDestroyTask();
    void            FreeDestroyTask();

    void            CreatedVOThread();

    entity            owner;
    float            usedProficiency;

    entity            spawner;
    boolean            selectedModel;

    entity            originalBody;

    boolean            expired;

    task            selectTask;

    float            timeStarted;

    float            toolTip_gdf_1;
    float            toolTip_gdf_2;
    float            toolTip_strogg_use;
    float            toolTip_strogg_destroyed;
    float            toolTip_strogg_select;
    float            toolTip_strogg_already_selected_1;
    float            toolTip_strogg_already_selected_2;

    float            lastTooltipTime;

    float            commandmapIcon;

    task            destroyTask;
    boolean            spawnerLocal;
}

void spawn_host::preinit() {
    usedProficiency    = GetProficiency( getKey( "prof_used" ) );
    timeStarted = -1;
    commandmapIcon = -1;

    toolTip_gdf_1                        = GetToolTip( getKey( "tt_intro_gdf_1" ) );
    toolTip_gdf_2                        = GetToolTip( getKey( "tt_intro_gdf_2" ) );
    toolTip_strogg_use                    = GetToolTip( getKey( "tt_intro_strogg_use" ) );
    toolTip_strogg_destroyed            = GetToolTip( getKey( "tt_strogg_destroyed" ) );
    toolTip_strogg_select                = GetToolTip( getKey( "tt_strogg_select" ) );
    toolTip_strogg_already_selected_1    = GetToolTip( getKey( "tt_strogg_already_selected_1" ) );
    toolTip_strogg_already_selected_2    = GetToolTip( getKey( "tt_strogg_already_selected_2" ) );

    thread CreatedVOThread();
}

void spawn_host::init() {

    if ( !sys.isClient() ) {
        owner.setPlayerSpawnHostState( self, false );

        if ( originalBody != $null_entity ) {
            originalBody.vCancel();
        }
     }

    if ( !sys.isClient() ) {
        timeStarted = sys.getTime();
        setState( "Idle" );
    }
}

void spawn_host::destroy() {
    if ( !sys.isClient() ) {
        owner.setPlayerSpawnHostState( self, true );
     }

    ClearCommandmapIcon();
    FreeDestroyTask();
    FreeSelectTask();

    if ( spawner != $null_entity ) {
        spawner.setSpawnPoint( $null_entity );
    }
}

void spawn_host::syncFields() {
    syncBroadcast( "spawner" );
    syncCallback( "spawner", "OnSpawnerChanged" );

    syncBroadcast( "timeStarted" );
    syncCallback( "timeStarted", "OnTimeStartedChanged" );
}

void spawn_host::FreeSelectTask() {
    if ( selectTask == $null ) {
        return;
    }
    selectTask.free();
    selectTask = $null;
}

void spawn_host::MakeSelectTask() {
    if ( selectTask != $null ) {
        return;
    }
    selectTask = taskManager.allocEntityTask( GetPlayerTask( getKey( "task_select" ) ), self );
}

void spawn_host::OnTimeStartedChanged() {
    setState( "Idle" );
}

void spawn_host::OnSpawnerChanged() {
    if ( spawner != $null_entity ) {
        spawnerLocal = false;
        if ( spawner == sys.getLocalPlayer() ) {
            spawnerLocal = true;
        }
    }
    UpdateVisualState();
}

void spawn_host::Idle() {
    setContents( CONTENTS_RENDERMODEL );
    setClipmask( 0 );

    UpdateVisualState();

    float timeLeft = ( sys.getTime() - timeStarted ) + 60.f;
    if ( timeLeft > 0.f ) {
        sys.wait( timeLeft );
    }

    expired = true;

    if ( spawner == $null_entity ) {
        Fizzle();
    }
}

void spawn_host::OnPlayerSpawned( entity p ) {
    if ( owner != $null_entity ) {
        owner.giveProficiency( usedProficiency, 1.f, $null, "spawnhost used" );
    }

    p.vSetSpawnHostSpawned();

    p.setSpawnPoint( $null_entity );

    remove();
}

void spawn_host::OnKilled( entity inflictor, entity attacker, float damage, vector direction, float location ) {
    Fizzle();
}

void spawn_host::vSetOwner( entity o ) {
    owner = o;
}

float spawn_host::GetActivateCode( entity p, float distance ) {
    if ( p.getViewingEntity() != p ) {
        return AK_NONE;
    }

    if ( p.getHealth() <= 0 ) {
        return AK_NONE;
    }

    if ( distance > DISTANCE_FOR_ACTION ) {
        return AK_NONE;
    }

    if ( p.getGameTeam() == stroggTeam ) {
        if ( spawner != $null_entity ) {
            return AK_NONE;
        }
    } else {
        return AK_DISARM_SPAWNHOST;
    }

    return AK_USE;
}

float spawn_host::OnUpdateCrosshairInfo( entity p ) {
    if ( sys.getLocalPlayer() == $null_entity ) {
        return 1.f;
    }

    float allegiance = getEntityAllegiance( p );
    vector color = GetAllegianceColor( allegiance );
    float distance = chGetDistance();
    float range = InchesToMetres( distance );

    chSetNumLines( 0 );
    float index;

    // see if theres a valid action to perform
    float code = GetActivateCode( p, distance );
    if ( code != AK_NONE && p.vHasActionItem( code ) ) {
        index = chAddLine();
        chSetLineMaterial( index, p.vGetActionIcon( code ) );
        chSetLineType( index, CI_IMAGE );
        chSetLineSize( index, 64, 64 );
        chSetLineColor( index, g_colorWhite, 0.9f );

        CheckContextToolTip( allegiance, code, p );
    }

    index = chAddLine();
    chSetLineTextIndex( index, g_locStr_SpawnHost );
    chSetLineColor( index, color, 1.f );
    chSetLineType( index, CI_TEXT );
    chSetLineSize( index, 0, 0 );

    index = chAddLine();
    chSetLineColor( index, color, 0.5f );
    chSetLineType( index, CI_BAR );
    chSetLineFraction( index, getHealth() / getMaxHealth() );
    chSetLineSize( index, 75.f, CROSSHAIR_INFO_BAR_HEIGHT );

    return 1.f;
}

float spawn_host::OnActivate( entity p, float distance ) {
    if ( distance > 128.f ) {
        return 0.f;
    }

    if ( p.getViewingEntity() != p ) {
        return 0.0f;
    }

    if ( p.getHealth() <= 0.f ) {
        return 0.f;
    }

    if ( p.getGameTeam() == gdfTeam ) {
        float code = GetActivateCode( p, distance );
        if ( code != AK_NONE ) {
            p.vSelectActionItem( code );
        }

        return 1.0f;
    }

    if ( getEntityAllegiance( p ) != TA_FRIEND ) {
        return 0.0f;
    }

    if ( spawner != $null_entity ) {
        if( p.isLocalPlayer() ) {
            if ( !p.isToolTipPlaying() ) {
                if ( p != spawner ) {
                    p.sendToolTip( toolTip_strogg_already_selected_1 );
                } else {
                    p.sendToolTip( toolTip_strogg_already_selected_2 );
                }
            }
        }
        return 0.0f;
    }

    p.setSpawnPoint( self );
    if( p.isLocalPlayer() ) {
        if ( !p.isToolTipPlaying() ) {
            p.sendToolTip( toolTip_strogg_select );
        }
    }

    return 1.f;
}

boolean spawn_host::CanSetSpawn() {
    if ( spawner == $null_entity ) {
        return true;
    }

    return false;
}

void spawn_host::Fizzle() {
    playEffect( "fx_fizzle", "", 0 );

    if ( spawnerLocal ) {
        entity p = sys.getLocalPlayer();
        p.sendToolTip( toolTip_strogg_destroyed );
    }

    if ( spawner != $null_entity ) {
        spawner.setSpawnPoint( $null_entity );
    }

    if ( !sys.isClient() ) {
        thread RemoveThread( 0.5 );
    }
}

void spawn_host::RemoveThread( float removeTime ) {
    sys.wait( removeTime );
    remove();
}

void spawn_host::CheckContextToolTip( float allegiance, float code, entity p ) {
    team_base team;

    if ( p.isLocalPlayer() ) {
        if ( !p.isToolTipPlaying() ) {
            float time = sys.getTime();
            if ( time - p.getCrosshairStartTime() > 0.5f ) {
                if ( time - lastTooltipTime > 5.0f ) {
                    if ( allegiance == TA_FRIEND ) {
                        if ( CanSetSpawn() ) {
                            p.sendToolTip( toolTip_strogg_use );
                        }
                    } else {
                        if ( p.getCurrentWeapon() != p.vGetActionItem( code ) ) {
                            p.sendToolTip( toolTip_gdf_1 );
                        } else {
                            p.sendToolTip( toolTip_gdf_2 );
                        }
                    }
                    lastTooltipTime = time;
                }
            }
        }
    }
}

void spawn_host::OnSpawnPointActivate( entity p ) {
    if ( spawner != $null_entity ) {
        sys.warning( "spawn_host::OnSpawnPointActivate Multiple Spawners Set" );
    }

    spawner = p;

    OnSpawnerChanged();
}

void spawn_host::OnSpawnPointDeactivate( entity p ) {
    if ( spawner == p ) {
        spawner = $null_entity;

        if ( expired ) {
            Fizzle();
        }

        OnSpawnerChanged();
    }
}

void spawn_host::vOnLocalTeamChanged() {
    UpdateVisualState();
}

void spawn_host::UpdateVisualState() {
    if ( !sys.isClient() ) {
        if ( spawner == $null_entity ) {
            MakeSelectTask();
        } else {
            FreeSelectTask();
        }
    }

    if ( spawner == $null_entity ) {
        if ( selectedModel ) {
            setModel( getKey( "model" ) );
            selectedModel = false;
        }
    } else {
        if ( !selectedModel ) {
            setModel( getKey( "model_selected" ) );
            selectedModel = true;
        }
    }
}

void spawn_host::SetupCommandmapIcon() {
    ClearCommandmapIcon();

    commandmapIcon = sys.allocCMIcon( self, getFloatKey( "icon_sort_cm" ) );

    team_base team = getGameTeam();

    sys.setCMIconMaterial( commandmapIcon, GetMaterial( getKey( "mtr_icon_" + team.getName() ) ) );
    sys.setCMIconFlag( commandmapIcon, CMF_ALWAYSKNOWN );
    sys.setCMIconFlag( commandmapIcon, CMF_ONLYSHOWONFULLVIEW );
    sys.setCMIconColorMode( commandmapIcon, CM_ALLEGIANCE );
    sys.setCMIconSize( commandmapIcon, getFloatKey( "icon_size_cm" ) );
}

void spawn_host::ClearCommandmapIcon() {
    if ( commandmapIcon != -1 ) {
        sys.freeCMIcon( self, commandmapIcon );
        commandmapIcon = -1;
    }
}

void spawn_host::OnSpawnPointSelected( float isDefault ) {
    SetupCommandmapIcon();

    sys.setGUIFloat( GUI_GLOBALS_HANDLE, "gameHud.spawnHostActive", 1 );
}

void spawn_host::OnSpawnPointDeselected() {
    ClearCommandmapIcon();

    sys.setGUIFloat( GUI_GLOBALS_HANDLE, "gameHud.spawnHostActive", 0 );
}

string spawn_host::vGetQuickChatString( entity p ) {
    if ( getEntityAllegiance( p ) == TA_FRIEND ) {
        return getKey( "contextmenu_quickchat_friendly" );
    }
    return getKey( "contextmenu_quickchat_enemy" );
}

void spawn_host::vOnContextDestroy( entity p ) {
    if ( getEntityAllegiance( p ) != TA_FRIEND ) {
        InitDestroyTask();
    }
}

void spawn_host::FreeDestroyTask() {
    if ( destroyTask != $null ) {
        destroyTask.free();
    }
}

void spawn_host::InitDestroyTask() {
    if ( destroyTask != $null ) {
        return;
    }

    float taskHandle = GetPlayerTask( getKey( "task_destroy" ) );
    if ( taskHandle != -1 ) {
        destroyTask = taskManager.allocEntityTask( taskHandle, self );
    }
}

void spawn_host::CreatedVOThread() {
    sys.wait( getFloatKey( "vo_created_delay" ) );
    if ( spawner == $null_entity ) {
        objManager.PlaySoundRanged( getKey( "snd_spawn_created" ), stroggTeam, getWorldOrigin(), 2048.f );
        objManager.CPrintEventRanged( sys.localizeString( "game/misc/spawn_host_created" ), stroggTeam, getWorldOrigin(), 2048.f );
    }
}

void spawn_host::vSetBody( entity body ) {
    originalBody = body;
}