Archived
1
0

Added new mapping feature. Will now create a directory and output generated map files every turn. Will probably add a way to change the frequency these are generated.

Signed-off-by: Tony Grosinger <github2@grosinger.net>
This commit is contained in:
Tony Grosinger 2011-04-25 20:02:34 -07:00
parent 806a3da1c3
commit db1b945815
10 changed files with 124 additions and 9 deletions

Binary file not shown.

View File

@ -238,7 +238,7 @@ public class DroneListItem {
/**
* Used when adding the drone to the map
*
* @param newY
* @param newX
* <code>int</code> new X location
*/
public void setX(int newX) {

View File

@ -30,7 +30,7 @@ public class DroneTeam {
/**
* Class Constructor
*
* @param firstDrone
* @param firstList
* <code>DroneListItem</code> 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
* <code>DroneListItem</code> to be made last
*/
public void setLast(DroneListItem theLast) {

View File

@ -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<Neighbor> checkRadar(){
ArrayList<Neighbor> neighbors = new ArrayList<Neighbor>();
int maxDistance = listItem.getVisibleDistance();

View File

@ -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 <code>String</code>- UID
*/
private static String generateUID() {
SecureRandom random = new SecureRandom();
return new BigInteger(130, random).toString(32);
}
/**
* Generates and places required buildings in the world
*

View File

@ -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) {

View File

@ -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 <code>boolean</code>
*/
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<Color> allColors = new ArrayList<Color>();
addColors(allColors);
Map<String, Color> colorMap = new HashMap<String, Color>();
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<Color> 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());
}
}
}