diff --git a/bin/net/grosinger/nomads/DroneTools.class b/bin/net/grosinger/nomads/DroneTools.class index 6be4f11..18740a2 100644 Binary files a/bin/net/grosinger/nomads/DroneTools.class and b/bin/net/grosinger/nomads/DroneTools.class differ diff --git a/bin/net/grosinger/nomads/InitializeGame.class b/bin/net/grosinger/nomads/InitializeGame.class index 7dd1c1f..505f2b7 100644 Binary files a/bin/net/grosinger/nomads/InitializeGame.class and b/bin/net/grosinger/nomads/InitializeGame.class differ diff --git a/bin/net/grosinger/nomads/Nomads.class b/bin/net/grosinger/nomads/Nomads.class index 90dc6e9..0395bee 100644 Binary files a/bin/net/grosinger/nomads/Nomads.class and b/bin/net/grosinger/nomads/Nomads.class differ diff --git a/bin/net/grosinger/nomads/World.class b/bin/net/grosinger/nomads/World.class index 4416603..74072dd 100644 Binary files a/bin/net/grosinger/nomads/World.class and b/bin/net/grosinger/nomads/World.class differ diff --git a/src/net/grosinger/nomads/DroneListItem.java b/src/net/grosinger/nomads/DroneListItem.java index 0acd1d4..ab689f1 100644 --- a/src/net/grosinger/nomads/DroneListItem.java +++ b/src/net/grosinger/nomads/DroneListItem.java @@ -238,7 +238,7 @@ public class DroneListItem { /** * Used when adding the drone to the map * - * @param newY + * @param newX * int new X location */ public void setX(int newX) { diff --git a/src/net/grosinger/nomads/DroneTeam.java b/src/net/grosinger/nomads/DroneTeam.java index 9a2457d..a816847 100644 --- a/src/net/grosinger/nomads/DroneTeam.java +++ b/src/net/grosinger/nomads/DroneTeam.java @@ -30,7 +30,7 @@ public class DroneTeam { /** * Class Constructor * - * @param firstDrone + * @param firstList * DroneListItem that will be the first Drone */ public DroneTeam(DroneListItem firstList) { @@ -105,7 +105,7 @@ public class DroneTeam { /** * Sets the last DroneListItem to that provided * - * @param theFirst + * @param theLast * DroneListItem to be made last */ public void setLast(DroneListItem theLast) { diff --git a/src/net/grosinger/nomads/DroneTools.java b/src/net/grosinger/nomads/DroneTools.java index 5620b15..51a8028 100644 --- a/src/net/grosinger/nomads/DroneTools.java +++ b/src/net/grosinger/nomads/DroneTools.java @@ -107,6 +107,11 @@ public class DroneTools { return worldReference.inSafeZone(getX(), getY()); } + /** + * Retrieve a list of all Drones that are visible within your sight range. (Sight range can be upgraded) + * + * @return ArrayList of Neighbors + */ public ArrayList checkRadar(){ ArrayList neighbors = new ArrayList(); int maxDistance = listItem.getVisibleDistance(); diff --git a/src/net/grosinger/nomads/InitializeGame.java b/src/net/grosinger/nomads/InitializeGame.java index a4b9def..3bad486 100644 --- a/src/net/grosinger/nomads/InitializeGame.java +++ b/src/net/grosinger/nomads/InitializeGame.java @@ -2,8 +2,10 @@ package net.grosinger.nomads; import java.io.File; import java.io.IOException; +import java.math.BigInteger; import java.net.URL; import java.net.URLClassLoader; +import java.security.SecureRandom; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -34,7 +36,7 @@ public class InitializeGame { // Obtain a list of the classes that exist in the directory String[] classesToLoad = generateList(); - if(classesToLoad.length == 0){ + if (classesToLoad.length == 0) { System.out.println("No Drones to load"); Nomads.running = false; } @@ -66,10 +68,12 @@ public class InitializeGame { // Create new GameObject // TODO - This will break if it loads a class from a jar // that does not extend Drone - GameObject newDrone = (GameObject) c.newInstance(); - newDrone.setName(className); + GameObject newGameObject = (GameObject) c.newInstance(); + newGameObject.setName(className); + Drone newDrone = (Drone) newGameObject; + newDrone.setUID(generateUID()); - createNewDrone(newDrone); + createNewDrone(newGameObject); System.out.println("Class loaded sucessfully"); } catch (Exception e) { @@ -119,6 +123,16 @@ public class InitializeGame { } } + /** + * Creates a unique identifier for each drone that is loaded + * + * @return String- UID + */ + private static String generateUID() { + SecureRandom random = new SecureRandom(); + return new BigInteger(130, random).toString(32); + } + /** * Generates and places required buildings in the world * diff --git a/src/net/grosinger/nomads/Nomads.java b/src/net/grosinger/nomads/Nomads.java index da8afea..7aa3a30 100644 --- a/src/net/grosinger/nomads/Nomads.java +++ b/src/net/grosinger/nomads/Nomads.java @@ -63,8 +63,12 @@ public class Nomads { if (DEBUGSTATUS) System.out.println("Game loop starting..."); + int turn = 0; + while (running) { + turn++; + long startTime = System.currentTimeMillis(); for (DroneTeam currentTeam : allTeams) { @@ -76,6 +80,8 @@ public class Nomads { if (DEBUGSTATUS) System.out.println("Moves took " + (endTime - startTime) + "milliseconds"); + awesomeWorld.generateMap(turn); + try { Thread.sleep(200); } catch (InterruptedException e) { diff --git a/src/net/grosinger/nomads/World.java b/src/net/grosinger/nomads/World.java index e19e511..e97fd7b 100644 --- a/src/net/grosinger/nomads/World.java +++ b/src/net/grosinger/nomads/World.java @@ -1,5 +1,14 @@ package net.grosinger.nomads; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + /** * Main class where information about the world is stored * @@ -134,6 +143,15 @@ public class World { setObjectAt(lastUsedX, 20, newDrone.getCurrent()); } + /** + * Returns if the Drone at given coordinates is in a safe zone + * + * @param x + * - Specified X value + * @param y + * - Specified Y value + * @return boolean + */ public boolean inSafeZone(int x, int y) { /* * Safe Zones - Measured in radius of a square TownHall - 3 : RepairShop @@ -143,8 +161,8 @@ public class World { // Maximum distance away a building that provides a safehouse is 3 for (int i = -3; i <= 3; i++) { for (int j = -3; j <= 3; j++) { - //Prevent OutofBounds. Indexes = - WORLDSIZE-1 - if (x + i >= WORLDSIZE-1 || x + i < 0 || y + j >= WORLDSIZE-1 || y + j < 0) { + // Prevent OutofBounds. Indexes = - WORLDSIZE-1 + if (x + i >= WORLDSIZE - 1 || x + i < 0 || y + j >= WORLDSIZE - 1 || y + j < 0) { } else if (i != 0 && j != 0) { GameObject objectHere = theWorld[x + i][y + j]; @@ -162,4 +180,76 @@ public class World { } return false; } + + /** + * Outputs an HTML file showing the world + */ + public void generateMap(int turn) { + ArrayList allColors = new ArrayList(); + addColors(allColors); + Map colorMap = new HashMap(); + int counter = 0; + + // Create the color map + for (DroneTeam team : Nomads.allTeams) { + String name = team.getName(); + Color color = null; + if (counter <= 8) + color = allColors.get(counter); + colorMap.put(name, color); + counter++; + } + + new File("maps").mkdir(); + BufferedImage image = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB); + Graphics2D g2d = image.createGraphics(); + + g2d.setColor(Color.white); + g2d.fillRect(0, 0, 1000, 1000); + + // draw grid lines + g2d.setColor(Color.black); + for (int i = 10; i <= 1000; i += 100) { + g2d.drawLine(i, 0, i, 1000); + g2d.drawLine(0, i, 1000, i); + } + for (int i = 0; i < WORLDSIZE; i++) { + for (int j = 0; j < WORLDSIZE; j++) { + GameObject objectHere = theWorld[j][i]; + if (objectHere == null) { + // Do nothing + } else if (objectHere instanceof Drone) { + String name = objectHere.getName(); + Color color = colorMap.get(name); + g2d.setColor(color); + g2d.fillOval(j * 10, i * 10, 10, 10); + } else if (objectHere instanceof Building) { + // TODO - output for buildings + } + } + } + writeImage(image, "maps/Map-Turn" + turn); + // g2d.dispose(); + } + + private void addColors(ArrayList allColors) { + allColors.add(Color.blue); + allColors.add(Color.orange); + allColors.add(Color.red); + allColors.add(Color.green); + allColors.add(Color.darkGray); + allColors.add(Color.yellow); + allColors.add(Color.cyan); + allColors.add(Color.magenta); + } + + private void writeImage(BufferedImage image, String fileName) { + try { + String ext = "png"; // bmp, gif, png, jpg okay + File file = new File(fileName + "." + ext); + javax.imageio.ImageIO.write(image, ext, file); + } catch (IOException e) { + System.out.println("write error: " + e.getMessage()); + } + } }