// This script contains functionality to to use a switch. // Usage: // TODO: // For cleanliness, we're going to store all our functions and variables inside a table, // within the ScriptGoals global table. // The ScriptGoals is a global table created by the bot by default, as a standardized storage location // for goals implemented in script. Script goals should make their own table under the ScriptGoals global table. ScriptGoals.UseSwitch = { // The name for this goal Name = "use_switch", // A debug flag for enabling/disabling printed messages. Useful for debugging this goal. Debug = false, DebugScriptGoal = false, // We'll store the goal check delay for the thread function here, just to put most of the // configuration options neatly at the top of the script for readability. ThreadCheckDelay = 3, Begin = function(bot) { yield(); if ( GetEntFlags(bot.GetGameEntity(), ENTFLAG.LIMBO ) ) { return; } //ScriptGoals.UseSwitch.Finish(bot); while ( bot.scriptgoal == ScriptGoals.dispense_ammo.Name || bot.scriptgoal == ScriptGoals.dispense_health.Name ) { yield(); } // make sure the there is some switches configured. if(!Map || !Map.Switches) { //print("No Switches table in global Map table."); return; } foreach ( i and switchTable in Map.Switches ) { if ( !Map.Switches[ i ].LimitBots ) { Map.Switches[ i ].LimitBots = 64; } } //print("UseSwitch.Begin, existing", bot.UseSwitchThread); if(bot.UseSwitchThread) { //print("Killing Thread.", bot.UseSwitchThread); threadKill(bot.UseSwitchThread); } bot.UseSwitchThread = bot:thread(ScriptGoals.UseSwitch.ThreadFunc); //print("Creating Thread.", bot.UseSwitchThread); }, ThreadFunc = function() { //setFinalFunction(ScriptGoals.UseSwitch.Finish); firstCall = true; while( true ) { if( this.scriptgoal == null ) { myTeamMask = (1< 0 ) { dist = this.DistanceTo(wpPos); if ( ScriptGoals.UseSwitch.Debug || switchTable.Debug ) { print(this.Name, "^2is", dist, "away from", switchTable.WaypointName); } if ( dist > switchTable.LimitDistance ) { continue; } } this.SetScriptControlled(true); this.scriptgoal = ScriptGoals.UseSwitch.Name; if ( ScriptGoals.UseSwitch.Debug || switchTable.Debug ) { print(this.Name, "^2is", "going to", switchTable.WaypointName); } assert(radius && wpPos && wpFacing); this.GoTo(wpPos, wpFacing); if( block(EVENT.GOAL_SUCCESS, EVENT.GOAL_FAILED) == EVENT.GOAL_SUCCESS ) { this.SetDesiredFacing(wpFacing); counter = 0; exitCondition = false; while( ( switchTable.LimitTeam & myTeamMask ) && ( switchTable.LimitClass & myClassMask) && !exitCondition) { this.MoveTowards( wpPos, radius ); if( this.TurnToFacing(wpFacing) ) { if(counter & 2) { this.PressButton( BTN.USE ); if ( switchTable.ExitConditions ) { foreach ( id and _function in switchTable.ExitConditions ) { if ( _function(this) ) { if ( ScriptGoals.UseSwitch.Debug || switchTable.Debug ) { print(this.Name, "^2exit condition met for", switchTable.WaypointName); } if ( switchTable.PressOnce ) { switchTable[ this ].Pressed = true; } exitCondition = true; break; } } } } } counter += 1; yield(); } ScriptGoals.UseSwitch.Finish(this); break; } else { ScriptGoals.UseSwitch.Finish(this); break; } if ( GetEntFlags(this.GetGameEntity(), ENTFLAG.LIMBO ) ) { ScriptGoals.UseSwitch.Finish(this); } } yield(); } } sleep( ScriptGoals.UseSwitch.ThreadCheckDelay ); } ScriptGoals.UseSwitch.Finish(this); }, Finish = function(bot) { if( bot && bot.scriptgoal == ScriptGoals.UseSwitch.Name ) { // print("bot:", bot.Name); bot.SetScriptControlled(false); bot.scriptgoal = null; } else if ( bot && ScriptGoals.UseSwitch.DebugScriptGoal ) { if ( bot.IsScriptControlled() ) { displaytime = 5; playerEnt = Util.GetPlayerEntity(); output = bot.Name + " ^1UseSwitch Finish Error: mismatched ScriptGoal "; EchoToScreen( playerEnt, displaytime, output ); print(bot.Name, "^1UseSwitch Finish Error: mismatched ScriptGoal"); print(bot.Name, "scriptgoal =", bot.scriptgoal); } } if( this && this.scriptgoal == ScriptGoals.UseSwitch.Name ) { this.SetScriptControlled(false); this.scriptgoal = null; } }, }; if(!Map) { global Map = table(); }