Script:Files:script/maps/base.script

From Mod Wiki
#define TIMED_MISSION_SHORT        0
#define TIMED_MISSION_MEDIUM    1
#define TIMED_MISSION_LONG        2

#define TIMED_MISSION_THREAD_NAME "mapObject_Default_TimedMissionThread"

object mapObject_Default : mapObject_Base {
    void            preinit();
    void            destroy();

    void            InitObjectives();

    void            StopTimedMission();
    void            StopTimedMissionThread();
    void            CreateInitialTimedMission( entity obj );
    void            CreateTimedMission( entity obj, float type );
    void            ClearTimedMission();
    void            TimedMissionThread( entity obj, float type );
    void            FinishTimedMission();

    void            CreateRespawnTimeThread( team_base attackingTeam );
    void            ResetRespawnTimeThread();
    void            ClearRespawnTimeThread();
    void            RespawnTimeThread( team_base _attackingTeam );

    task            AllocWorldTask( string name );
    void            FreeWorldTask( task t );
    void            CreateDeployTasks();

    entity            worldspawn;

    task            timedMission;
    float            timedMissionXP;

    float            timedMissionThread;

    float            respawnTimeThread;
    team_base        attackingTeam;

    task            engineerDeployTask;
    task            engineerMineTask;
    task            constructorDeployTask;
    task            constructorMineTask;
    task            fieldopsDeployTask;
    task            oppressorDeployTask;
    task            covertopsDeployTask;
    task            infiltratorDeployTask;
    task            medicDeployTask;
}

void mapObject_Default::preinit() {
    worldspawn        = sys.getEntity( "worldspawn" );
    if ( worldspawn == $null_entity ) {
        sys.error( "mapObject_Default::preinit - worldspawn is null!" );
    }

    timedMissionThread = -1;
    respawnTimeThread = -1;
}

void mapObject_Default::destroy() {
    ClearTimedMission();

    FreeWorldTask( engineerDeployTask );
    FreeWorldTask( engineerMineTask );
    FreeWorldTask( constructorDeployTask );
    FreeWorldTask( constructorMineTask );
    FreeWorldTask( fieldopsDeployTask );
    FreeWorldTask( oppressorDeployTask );
    FreeWorldTask( covertopsDeployTask );
    FreeWorldTask( infiltratorDeployTask );
    FreeWorldTask( medicDeployTask );

    fieldopsDeployTask = $null;
    oppressorDeployTask = $null;
    engineerDeployTask = $null;
    engineerMineTask = $null;
    constructorDeployTask = $null;
    constructorMineTask = $null;
    covertopsDeployTask = $null;
    infiltratorDeployTask = $null;
    medicDeployTask = $null;
}

void mapObject_Default::ClearTimedMission() {
    if ( timedMission != $null ) {
        timedMission.free();
        timedMission = $null;
    }
}

void mapObject_Default::InitObjectives() {
    // jrad - non-game maps don't need spawn waves
    CreateRespawnTimeThread( gdfTeam );
}

void mapObject_Default::CreateInitialTimedMission( entity obj ) {
    StopTimedMissionThread();
    CreateTimedMission( obj, TIMED_MISSION_SHORT );
}

void mapObject_Default::StopTimedMissionThread() {
    if ( timedMissionThread != -1 ) {
        sys.terminate( timedMissionThread );
        timedMissionThread = -1;
    }
}

void mapObject_Default::StopTimedMission() {
    StopTimedMissionThread();
    ClearTimedMission();
}

void mapObject_Default::CreateTimedMission( entity obj, float type ) {
    ClearTimedMission();
    if ( timedMissionThread != -1 ) {
        sys.warning( "Timed Mission Thread Already Running" );
    }
    timedMissionThread = thread TimedMissionThread( obj, type );
}

void mapObject_Default::TimedMissionThread( entity obj, float type ) {
    if ( obj == $null_entity ) {
        sys.assert( false );
        sys.warning( "mapObject_Default::TimedMissionThread Entity was null" );
        timedMissionThread = -1;
        return;
    }

    float timelimit = -1;
    string taskName = "task_defend_long";
    timedMissionXP = 30;
    if ( type == TIMED_MISSION_SHORT ) {
        timelimit = 4.f; // FIXME: get this from the task
        taskName = "task_defend_short";
        timedMissionXP = 10; // FIXME: get this from the task
    } else if ( type == TIMED_MISSION_MEDIUM ) {
        timelimit = 8.f;
        taskName = "task_defend_medium";
        timedMissionXP = 20;
    }

    timedMission = taskManager.allocEntityTask( GetPlayerTask( obj.getKey( taskName ) ), obj );
    if ( timedMission == $null ) {
        sys.warning( "mapObject_Default::TimedMissionThread Failed to create a Timed mission" );
        timedMissionThread = -1;
        return;
    }

    obj.vInitDefendMission( timedMission );

    if ( timelimit != -1 ) {
        sys.wait( timelimit * 60.f );

        timedMissionThread = -1;
        FinishTimedMission();

        // MCP might have been removed
        if ( obj != $null_entity ) {
            CreateTimedMission( obj, type + 1 );
        }

        return;
    }

    timedMissionThread = -1;
}

void mapObject_Default::FinishTimedMission() {
    if ( timedMission != $null ) {

        timedMission.giveObjectiveProficiency( timedMissionXP, "defend task" );

        timedMission.complete();
        timedMission.free();
        timedMission = $null;
    }

    StopTimedMissionThread();
}

void mapObject_Default::CreateRespawnTimeThread( team_base attackingTeam ) {
    sys.assert( respawnTimeThread == -1 );
    respawnTimeThread = thread RespawnTimeThread( attackingTeam );
}

void mapObject_Default::ResetRespawnTimeThread() {
    // map should have called CreateRespawnTimeThread before calls to this function is valid.
    ClearRespawnTimeThread();
    CreateRespawnTimeThread( attackingTeam );
}

void mapObject_Default::ClearRespawnTimeThread() {
    if ( respawnTimeThread != -1 ) {
        sys.terminate( respawnTimeThread );
        respawnTimeThread = -1;
    }
}

void mapObject_Default::RespawnTimeThread( team_base _attackingTeam ) {
    attackingTeam = _attackingTeam;

    if ( objManager.gameState != GS_GAMEON ) {
        respawnTimeThread = -1;
        return;
    }

    if ( !g_fasterSpawn.getBoolValue() ) {
        respawnTimeThread = -1;
        return;
    }

    float respawnTimeChange = attackingTeam.getFloatKey( "spawnTimeChange" );
    sys.wait( respawnTimeChange );

    float respawnTimeReduction = attackingTeam.getFloatKey( "spawnTimeReduction" );
    float respawnWait = attackingTeam.GetRespawnWait();
    attackingTeam.SetRespawnWait( respawnWait - respawnTimeReduction );

    respawnTimeThread = -1;
}

task mapObject_Default::AllocWorldTask( string name ) {
    sys.assert( worldspawn != $null_entity );
    return taskManager.allocEntityTask( GetPlayerTask( worldspawn.getKey( name ) ), worldspawn );
}

void mapObject_Default::FreeWorldTask( task t ) {
    if ( t != $null ) {
        t.free();
    }
}

void mapObject_Default::CreateDeployTasks() {
    engineerDeployTask                    = AllocWorldTask( "task_engineer_deploy" );
    engineerMineTask                    = AllocWorldTask( "task_engineer_plant_mines" );
    constructorDeployTask                = AllocWorldTask( "task_constructor_deploy" );
    constructorMineTask                    = AllocWorldTask( "task_constructor_plant_mines" );
    fieldopsDeployTask                    = AllocWorldTask( "task_fieldops_deploy" );
    oppressorDeployTask                    = AllocWorldTask( "task_oppressor_deploy" );
    covertopsDeployTask                    = AllocWorldTask( "task_covertops_deploy" );
    infiltratorDeployTask                = AllocWorldTask( "task_infiltrator_deploy" );
    medicDeployTask                        = AllocWorldTask( "task_medic_deploy" );
}