All pastes #173058 Raw Edit

Mine

public text v1 · immutable
·published 2006-09-16 14:58 UTC
rendered paste body
import java.io.*;
import java.util.*;

public class SomeClass {
    public static void main(String[] args) {
        new SomeClass();
    }
    
    public SomeClass() {
        long l = System.currentTimeMillis();
        Map<Tile, GameObject> gameObjects = parseConfigFile(new File(".", "ObjectLocs.xml"));
        System.out.println(gameObjects.size());
        System.out.println(System.currentTimeMillis() - l);

        
        l = System.nanoTime();
        GameObject p = new GameObject(0, new Tile(100, 100), 0);
        Tile[] t = new Tile[1024];
        for(int i = 0; i < 32; i++)
            for(int j = 0; j < 32; j++)
                t[(i * 32) + j] = new Tile(84 + i, 84 + j);
        System.out.println((System.nanoTime() - l) / 1000000D);

        
        List<GameObject> s = new ArrayList<GameObject>();
        l = System.nanoTime();
        for(Tile tt : t)
            if(gameObjects.containsKey(tt))
                s.add(gameObjects.get(tt));
        System.out.println((System.nanoTime() - l) / 1000000D);
        System.out.println(s.size());
    }
    
    
    private Map<Tile, GameObject> parseConfigFile(java.io.File configFile) {
        try {
            org.jdom.Document xmlDocument = new org.jdom.input.SAXBuilder().build(configFile);
            org.jdom.Element rootElement = xmlDocument.getRootElement();
            
            Map<Tile, GameObject> gameObjects = new TreeMap<Tile, GameObject>();
            int i = 0;
            
            for(Object o : rootElement.getChildren()) {
                org.jdom.Element e = (org.jdom.Element)o;
                int objectType = Integer.parseInt(e.getChild("type").getValue());
                
                //if(objectType == 0)   {// Indicates regular game object 
                    Tile t = new Tile(Integer.parseInt(e.getChild("x").getValue()), Integer.parseInt(e.getChild("y").getValue()));
                    gameObjects.put(t, new GameObject(Integer.parseInt(e.getChild("id").getValue()), t, Integer.parseInt(e.getChild("direction").getValue())));
                //}
            }
            
            return gameObjects;
        } catch(Exception e) {
            System.err.print("Unable to read/parse "+ configFile +": ");
            e.printStackTrace();
            return null;
        }
    }
    
    private class GameObject {
        public int id, direction;
        public Tile pos;
        
        public GameObject(int id, Tile pos, int direction) {
            this.id = id;
            this.pos = pos;
            this.direction = direction;
        }
        
        public double getDistance(GameObject o) {
            return Math.hypot(Math.abs(pos.x - o.pos.x), Math.abs(pos.y - o.pos.y));
        }
    }
    
    public class Tile implements Comparable {
        public int x, y;
        public Tile(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
        public boolean equals(Object o) {
            if(o instanceof Tile) {
                Tile t = (Tile)o;
                return (x == t.x) && (y == t.y);
            }
            
            return false;
        }
        
        public int compareTo(Object t) {
            return hashCode() - t.hashCode();
        }
    
        public int hashCode() {
        /*
            int hashCode = 17;
            hashCode = 37 * hashCode + x;
            hashCode = 37 * hashCode + y;
            return hashCode;
            */
            return (x << 16) + y;
        }
    }
}