Search in sources :

Example 1 with WorldCoordinates

use of thaumcraft.api.WorldCoordinates in project PneumaticCraft by MineMaarten.

the class VisNetHandler method calculateNearbyNodes.

private static void calculateNearbyNodes(World world, int x, int y, int z) {
    HashMap<WorldCoordinates, WeakReference<TileVisNode>> sourcelist = sources.get(world.provider.dimensionId);
    if (sourcelist == null) {
        sourcelist = new HashMap<WorldCoordinates, WeakReference<TileVisNode>>();
        return;
    }
    ArrayList<WeakReference<TileVisNode>> cn = new ArrayList<WeakReference<TileVisNode>>();
    WorldCoordinates drainer = new WorldCoordinates(x, y, z, world.provider.dimensionId);
    ArrayList<Object[]> nearby = new ArrayList<Object[]>();
    for (WeakReference<TileVisNode> root : sourcelist.values()) {
        if (!isNodeValid(root))
            continue;
        TileVisNode source = root.get();
        TileVisNode closest = null;
        float range = Float.MAX_VALUE;
        float r = inRange(world, drainer, source.getLocation(), source.getRange());
        if (r > 0) {
            range = r;
            closest = source;
        }
        ArrayList<WeakReference<TileVisNode>> children = new ArrayList<WeakReference<TileVisNode>>();
        children = getAllChildren(source, children);
        for (WeakReference<TileVisNode> child : children) {
            TileVisNode n = child.get();
            if (n != null && !n.equals(root)) {
                float r2 = inRange(n.getWorldObj(), n.getLocation(), drainer, n.getRange());
                if (r2 > 0 && r2 < range) {
                    range = r2;
                    closest = n;
                }
            }
        }
        if (closest != null) {
            cn.add(new WeakReference(closest));
        }
    }
    nearbyNodes.put(drainer, cn);
}
Also used : WeakReference(java.lang.ref.WeakReference) WorldCoordinates(thaumcraft.api.WorldCoordinates) ArrayList(java.util.ArrayList)

Example 2 with WorldCoordinates

use of thaumcraft.api.WorldCoordinates in project PneumaticCraft by MineMaarten.

the class VisNetHandler method drainVis.

// NODE DRAINING
/**
	 * This method drains vis from a relay or source near the passed in
	 * location. The amount received can be less than the amount requested so
	 * take that into account.
	 * 
	 * @param world
	 * @param x the x position of the draining block or entity
	 * @param y the y position of the draining block or entity
	 * @param z the z position of the draining block or entity
	 * @param aspect what aspect to drain
	 * @param amount how much to drain
	 * @return how much was actually drained
	 */
public static int drainVis(World world, int x, int y, int z, Aspect aspect, int amount) {
    int drainedAmount = 0;
    WorldCoordinates drainer = new WorldCoordinates(x, y, z, world.provider.dimensionId);
    if (!nearbyNodes.containsKey(drainer)) {
        calculateNearbyNodes(world, x, y, z);
    }
    ArrayList<WeakReference<TileVisNode>> nodes = nearbyNodes.get(drainer);
    if (nodes != null && nodes.size() > 0)
        for (WeakReference<TileVisNode> noderef : nodes) {
            TileVisNode node = noderef.get();
            if (node == null)
                continue;
            int a = node.consumeVis(aspect, amount);
            drainedAmount += a;
            amount -= a;
            if (a > 0) {
                int color = Aspect.getPrimalAspects().indexOf(aspect);
                generateVisEffect(world.provider.dimensionId, x, y, z, node.xCoord, node.yCoord, node.zCoord, color);
            }
            if (amount <= 0) {
                break;
            }
        }
    return drainedAmount;
}
Also used : WeakReference(java.lang.ref.WeakReference) WorldCoordinates(thaumcraft.api.WorldCoordinates)

Example 3 with WorldCoordinates

use of thaumcraft.api.WorldCoordinates in project PneumaticCraft by MineMaarten.

the class VisNetHandler method findClosestNodes.

public static ArrayList<Object[]> findClosestNodes(TileVisNode target, TileVisNode parent, ArrayList<Object[]> in) {
    if (cache.size() > 512 || cache.contains(new WorldCoordinates(parent)))
        return in;
    cache.add(new WorldCoordinates(parent));
    for (WeakReference<TileVisNode> childWR : parent.getChildren()) {
        TileVisNode child = childWR.get();
        if (child != null && !child.equals(target) && !child.equals(parent)) {
            float r2 = inRange(child.getWorldObj(), child.getLocation(), target.getLocation(), target.getRange());
            if (r2 > 0) {
                in.add(new Object[] { child, r2 });
            }
            in = findClosestNodes(target, child, in);
        }
    }
    return in;
}
Also used : WorldCoordinates(thaumcraft.api.WorldCoordinates)

Example 4 with WorldCoordinates

use of thaumcraft.api.WorldCoordinates in project PneumaticCraft by MineMaarten.

the class VisNetHandler method addNode.

public static WeakReference<TileVisNode> addNode(World world, TileVisNode vn) {
    WeakReference ref = new WeakReference(vn);
    HashMap<WorldCoordinates, WeakReference<TileVisNode>> sourcelist = sources.get(world.provider.dimensionId);
    if (sourcelist == null) {
        sourcelist = new HashMap<WorldCoordinates, WeakReference<TileVisNode>>();
        return null;
    }
    ArrayList<Object[]> nearby = new ArrayList<Object[]>();
    for (WeakReference<TileVisNode> root : sourcelist.values()) {
        if (!isNodeValid(root))
            continue;
        TileVisNode source = root.get();
        float r = inRange(world, vn.getLocation(), source.getLocation(), vn.getRange());
        if (r > 0) {
            nearby.add(new Object[] { source, r - vn.getRange() * 2 });
        }
        nearby = findClosestNodes(vn, source, nearby);
        cache.clear();
    }
    float dist = Float.MAX_VALUE;
    TileVisNode closest = null;
    if (nearby.size() > 0) {
        for (Object[] o : nearby) {
            if ((Float) o[1] < dist && (vn.getAttunement() == -1 || ((TileVisNode) o[0]).getAttunement() == -1 || //) {
            vn.getAttunement() == ((TileVisNode) o[0]).getAttunement()) && canNodeBeSeen(vn, (TileVisNode) o[0])) {
                dist = (Float) o[1];
                closest = (TileVisNode) o[0];
            }
        }
    }
    if (closest != null) {
        closest.getChildren().add(ref);
        nearbyNodes.clear();
        return new WeakReference(closest);
    }
    return null;
}
Also used : WeakReference(java.lang.ref.WeakReference) WorldCoordinates(thaumcraft.api.WorldCoordinates) ArrayList(java.util.ArrayList)

Example 5 with WorldCoordinates

use of thaumcraft.api.WorldCoordinates in project PneumaticCraft by MineMaarten.

the class VisNetHandler method addSource.

public static void addSource(World world, TileVisNode vs) {
    HashMap<WorldCoordinates, WeakReference<TileVisNode>> sourcelist = sources.get(world.provider.dimensionId);
    if (sourcelist == null) {
        sourcelist = new HashMap<WorldCoordinates, WeakReference<TileVisNode>>();
    }
    sourcelist.put(vs.getLocation(), new WeakReference(vs));
    sources.put(world.provider.dimensionId, sourcelist);
    nearbyNodes.clear();
}
Also used : WeakReference(java.lang.ref.WeakReference) WorldCoordinates(thaumcraft.api.WorldCoordinates)

Aggregations

WorldCoordinates (thaumcraft.api.WorldCoordinates)5 WeakReference (java.lang.ref.WeakReference)4 ArrayList (java.util.ArrayList)2