package hypernova.activities;

import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import hypernova.Ship;
import hypernova.Universe;
import hypernova.Activity;
import hypernova.Realization;
import hypernova.pilots.SpaceFactory;
import hypernova.pilots.PilotFactory;

public class FactoryBattle extends Activity implements Realization {
    public static final double SPREAD = 250.0;
    public static final double RATE = 150.0;
    public static final double GMEAN = 10;
    public static final double GVAR = 5;
    public static final double GRATE = 0.2;
    public static final int EXTRAS = 3;

    private double x, y;
    private Shape zone;

    public void realize(double x, double y) {
        this.x = x;
        this.y = y;
        zone = new Rectangle2D.Double(x - SPREAD, y - SPREAD,
                                      SPREAD * 2, SPREAD * 2);
        Universe.get().addRealization(this);
    }

    public boolean shouldTrigger(double x, double y) {
        return zone.contains(x, y);
    }

    public void trigger(double px, double py) {
        Universe.get().removeRealization(this);
        Universe.get().queueMessage("These automated factories are battling!");
        createFactory("Humans", x, y, RATE / 1.5, false);
        for (int i = 0; i < 2; i++) {
            int dx = 1, dy = 1;
            if (i > 0 && i < 3) dx = -1;
            if (i % 2 == 0) dy = -1;
            createFactory("Invaders", SPREAD * dx + x,
                          SPREAD * dy + y, RATE, true);
        }
    }

    private void createFactory(String faction, double x, double y,
                               double rate, boolean extras) {
        Universe u = Universe.get();
        Ship f = new Ship("factory");
        f.setFaction(faction).setPosition(x, y, 0);
        PilotFactory pf = new PilotFactory.HunterSeekerFactory();
        SpaceFactory p = new SpaceFactory(f, "drone", rate, pf);
        p.addGold(GMEAN, GVAR, GRATE);
        f.setPilot(p);
        u.add(f);
        if (extras) {
            for(int i = 0; i < EXTRAS; i++) {
                Ship s = Ship.get("drone");
                s.setPilot(pf.create(s));
                s.setFaction(faction).setPosition(x, y, 0);
                u.add(s);
            }
        }
    }
}

@@ -24,16 +24,42 @@
 ities;%0A%0A
+import java.util.Random;%0A%0A
 import j
@@ -361,16 +361,68 @@
 ation %7B%0A
+    private static final Random RNG = new Random();%0A
     publ
@@ -2253,33 +2253,142 @@
 faction)
-.setPosition(x, y
+;%0A                s.setPosition(x + RNG.nextGaussian() * SPREAD,%0A                              y + RNG.nextGaussian() * SPREAD
 , 0);%0A  

Tweak FactoryBattle a bit.