import com.swath.*;
import com.swath.cmd.*;
import java.util.*;
import java.lang.*;

/**
 * Twarps to SD or Designated Sector (for evils)
 * Buys merch, Tows it back to where you left from (sector with a Sxx)
 * reloads Fuel to do as many times as set for
 * By Raybz
 */
public class Furber extends UserDefinedScript {
        private Parameter sector;
        private Parameter esect;
        private Parameter home;
        private Parameter evil;
        private Parameter turns;

        public String getName() {

                // Return the name of the script
                return "Buy and tow back a Merc to Furb with";
        }

        public boolean initScript() throws Exception {
                
                // Check that we are at the correct prompt
                if (!atPrompt(Swath.COMMAND_PROMPT)) return false;

                //Where is Stardock
                sector = new Parameter("Stardock");
                sector.setInteger(Swath.main.stardock());

                //Landing Sector for Evils
                esect = new Parameter("Landing Sector,(if evil)");
                esect.setType(Parameter.INTEGER);
                esect.setInteger(0);

                //Where to drop off furbs
                home = new Parameter("Where to drop off Furb");
                home.setType(Parameter.INTEGER);
                home.setInteger(Swath.main.currSector());

                //Evil Trader
                evil=new Parameter("Evil? No Commish? check if YES");
                evil.setType(Parameter.BOOLEAN);
                evil.setBoolean(true);
                turns = new Parameter("How Many Ships?");
                turns.setType(Parameter.INTEGER);
                turns.setInteger(8);

        
                // Register the parameters
                registerParam(sector);
                registerParam(esect);
                registerParam(home);
                registerParam(evil);
        
                return true;
        }

        public boolean runScript() throws Exception {

                String str;
                Sector sectorinfo;
                int m_figs=esect.getInteger();
                boolean e_evil=evil.getBoolean();
                int i=0;


                // Get the Sector to Move to

                if (e_evil) {
					// You Are Evil
					while (i<turns.getInteger()){
                        SendString.exec("i");
                        WaitForPrompt.exec(Swath.COMMAND_PROMPT);
                        Trade.exec(Swath.ship.emptyHolds(),0,0);
                        TransWarp.exec(m_figs);
                        Move.exec(sector.getInteger());
                        LandOnStarDock.exec();

                        SendString.exec("sbnyhyp");
                        SendString.exec("furb" + SendString.RETURN_KEY + "n" + SendString.RETURN_KEY);
                        SendString.exec("q");
                        LeaveStarDock.exec();
                        WaitForPrompt.exec(Swath.COMMAND_PROMPT);
                        SendString.exec("W" + SendString.RETURN_KEY);
                        str=WaitForText.exec("furb");
                        SendString.exec(SendString.RETURN_KEY);
                        WaitForPrompt.exec(Swath.COMMAND_PROMPT);
                        StringTokenizer tokenizer = new StringTokenizer(str, " ");
                        String token = tokenizer.nextToken();
                        int m_token = Integer.parseInt(token);
                
                        TowShip.exec(m_token);
                        TransWarp.exec(home.getInteger());
                        ShutOffTractorBeam.exec();
                        i++;
					}
                }   //end if twarp
                else {
                        while (i<turns.getInteger()){
                        SendString.exec("i");
                        WaitForPrompt.exec(Swath.COMMAND_PROMPT);
                        Trade.exec(Swath.ship.emptyHolds(),0,0);
                        TransWarp.exec(sector.getInteger());
                        
                        LandOnStarDock.exec();
                
                        SendString.exec("sbnyhyp");
                        SendString.exec("furb" + SendString.RETURN_KEY + "n" + SendString.RETURN_KEY);
                        SendString.exec("q");
                        LeaveStarDock.exec();
                        WaitForPrompt.exec(Swath.COMMAND_PROMPT);
                        SendString.exec("W" + SendString.RETURN_KEY);
                        str=WaitForText.exec("furb");
                        SendString.exec(SendString.RETURN_KEY);
                        WaitForPrompt.exec(Swath.COMMAND_PROMPT);
                        StringTokenizer tokenizer = new StringTokenizer(str, " ");
                        String token = tokenizer.nextToken();
                        int m_token = Integer.parseInt(token);
                
                        TowShip.exec(m_token);
                        TransWarp.exec(home.getInteger());
                        ShutOffTractorBeam.exec();
                        i++;
                    }               
                }
                return true;
        }   //end RUN

        public void endScript(boolean finished) {        
        }
}
