00001
00002 import java.awt.*;
00003 import java.awt.image.BufferedImage;
00004 import java.util.*;
00005 import javax.swing.*;
00006
00008
00015 public class Main
00016 {
00018 public static void helpPrinter()
00019 {
00020 System.out.println(" Command Line Parameters are as follows:");
00021 System.out.println(" \"--help\" : You're looking at it");
00022 System.out.println(" \"-w [int]\" : Set the width of the terrain map");
00023 System.out.println(" Example: -w 500");
00024 System.out.println(" \"-h [int]\" : Set the height of the terrain map");
00025 System.out.println(" Example: -h 500");
00026 System.out.println(" \"-seed [int]\" : Set the seed value for generating the terrain map");
00027 System.out.println(" Example: -seed 3");
00028 System.out.println(" \"-roughness [0-7]\" : Set the roughness of the terrain map");
00029 System.out.println(" Example: -roughness 4");
00030 System.out.println(" \"-movement [cem]\" : Set the type of movement [c: Chess, e: Euclidean, m: Manhattan]");
00031 System.out.println(" Example: -movement c");
00032 System.out.println(" \"-contour\" : Displays the order of revealed nodes");
00033 System.out.println(" \"-chaotic\" : Slightly corrupts the terrain every one second");
00034 System.out.println(" \"-load [filename]\" : Loads up the given file to use as the terrain map");
00035 System.out.println(" Example: -load MTAFT.XYZ");
00036 System.out.println(" All other arguments are algorithms to be run on the generated map");
00037 System.out.println("Note: Later command-line options override earlier ones if they are incompatable\n");
00038 System.out.println("Example: java Main -w 500 -h 500 DijkstraAI AstarAI");
00039 }
00040
00042
00046 public static void createDisplayWindow(final BufferedImage im, final String title)
00047 {
00048
00049 final JFrame frame = new JFrame("Path Finder: " + title);
00050 final JPanel panel = new JPanel()
00051 {
00052 @Override
00053 public void paintComponent(final Graphics g)
00054 {
00055 super.paintComponent(g);
00056 g.drawImage(im, 0, 0, getWidth(), getHeight(), null);
00057 }
00058 };
00059
00060 panel.setPreferredSize(new Dimension(im.getWidth(), im.getHeight()));
00061 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
00062 frame.add(panel);
00063 frame.pack();
00064 frame.setVisible(true);
00065 }
00066
00068 public static void main(String[] args)
00069 {
00070 ArrayList<AIModule> aiList = new ArrayList();
00071 TerrainGenerator terraGen = null;
00072 boolean flexible = true;
00073 boolean showContour = false;
00074 boolean chaotic = false;
00075 int width = 500;
00076 int height = 500;
00077 int roughness = 3;
00078 long seed = System.currentTimeMillis();
00079 TerrainMap.MovementType moveType = TerrainMap.MovementType.Chess;
00080
00081
00082 int i = 0;
00083 try
00084 {
00085 while(i < args.length)
00086 {
00087
00088 if(args[i].equalsIgnoreCase("-w"))
00089 {
00090 if(!flexible)
00091 continue;
00092
00093 width = Integer.parseInt(args[i + 1]);
00094 if(width <= 0)
00095 {
00096 throw new IllegalArgumentException("Widths must be nonnegative.");
00097 }
00098 }
00099
00100 else if(args[i].equalsIgnoreCase("-h"))
00101 {
00102 if(!flexible)
00103 continue;
00104
00105 height = Integer.parseInt(args[i + 1]);
00106 if(width <= 0)
00107 {
00108 throw new IllegalArgumentException("Heights must be nonnegative.");
00109 }
00110 }
00111
00112 else if(args[i].equalsIgnoreCase("-seed"))
00113 {
00114 seed = Integer.parseInt(args[i + 1]);
00115 }
00116
00117 else if(args[i].equalsIgnoreCase("-roughness"))
00118 {
00119 roughness = Integer.parseInt(args[i + 1]);
00120 if(roughness < 0 || roughness >= 8)
00121 throw new IllegalArgumentException("Roughness must be between 0 and 7.");
00122 }
00123
00124 else if(args[i].equalsIgnoreCase("-movement"))
00125 {
00126 switch(args[i + 1].charAt(0))
00127 {
00128 case 'c': moveType = TerrainMap.MovementType.Chess; break;
00129 case 'e': moveType = TerrainMap.MovementType.Euclidean; break;
00130 case 'm': moveType = TerrainMap.MovementType.Manhattan; break;
00131 default: throw new IllegalArgumentException("Unrecognized movement type.");
00132 }
00133 }
00134
00135 else if(args[i].equalsIgnoreCase("-contour"))
00136 {
00137 showContour = true;
00138 i--;
00139 }
00140
00141 else if(args[i].equalsIgnoreCase("-chaotic"))
00142 {
00143 chaotic = true;
00144 i--;
00145 }
00146
00147 else if(args[i].equalsIgnoreCase("-load"))
00148 {
00149 String filename = args[i + 1];
00150 terraGen = new TerrainFileLoader(filename);
00151 byte[][] terrain = terraGen.getTerrain();
00152 width = terrain.length;
00153 height = terrain[0].length;
00154 flexible = false;
00155 }
00156
00157 else if(args[i].equalsIgnoreCase("--help"))
00158 {
00159 helpPrinter();
00160 System.exit(0);
00161 }
00162
00163 else
00164 {
00165 aiList.add((AIModule) Class.forName(args[i]).newInstance());
00166 i--;
00167 }
00168 i += 2;
00169 }
00170 }
00171 catch(ClassNotFoundException cnf)
00172 {
00173 System.err.println("AI Not Found: " + args[i]);
00174 System.exit(1);
00175 }
00176 catch(IndexOutOfBoundsException ioob)
00177 {
00178 System.err.println("Invalid Arguements: " + args[i]);
00179 System.exit(2);
00180 }
00181 catch(NumberFormatException e)
00182 {
00183 System.err.println("Invalid Integer: " + args[i]);
00184 System.exit(3);
00185 }
00186 catch(Exception e)
00187 {
00188 System.err.println("Unknown Error");
00189 System.exit(4);
00190 }
00191
00192 if(terraGen == null)
00193 {
00194 terraGen = new PerlinTerrainGenerator(width, height, roughness, seed);
00195 }
00196
00197
00198
00199 for(AIModule ai : aiList)
00200 {
00201 final TerrainMap map = new TerrainMap(width, height, terraGen, moveType, chaotic);
00202 final long startTime = System.currentTimeMillis();
00203 final double cost = map.findPath(ai);
00204 final long endTime = System.currentTimeMillis();
00205
00206 System.out.println(ai.getClass().getName());
00207 System.out.println("Path Cost: " + cost);
00208 System.out.println("Uncovered: " + map.getNumVisited());
00209 System.out.println("Time taken: " + (endTime - startTime) + "\n");
00210
00211 createDisplayWindow(map.createImage(), ai.getClass().getName());
00212 if(showContour)
00213 createDisplayWindow(map.createContourImage(), ai.getClass().getName());
00214 }
00215 }
00216 }
00217