diff --git a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java index a3ca753..e31301b 100644 --- a/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java +++ b/Plugin Radioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java @@ -1,6 +1,6 @@ package net.simon987.pluginradioactivecloud; -import java.util.HashSet; +import java.util.ArrayList; import org.bson.Document; import net.simon987.server.assembly.HardwareModule; @@ -33,30 +33,64 @@ public class RadiationDetector extends HardwareModule { this.x = x; this.y = y; } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } - - if (!(o instanceof Tuple)) { - return false; - } - - Tuple t = (Tuple) o; - - return Integer.compare(x, t.x) == 0 && Integer.compare(y, t.y) == 0; - } } /** - * Find tiles between two given tiles. + * Find tiles between two given coordinates. + * + * @return List of tile coordinates. An empty list indicates tiles are next to + * each other. */ - private HashSet getTiles(int x0, int y0, int x1, int y1) { + public ArrayList getTiles(int x0, int y0, int x1, int y1) { - HashSet ret = new HashSet<>(); - double slope = (y1 - y0) / (double) (x1 - x0); + ArrayList ret = new ArrayList<>(); + double slope; + if (x1 > x0) + slope = (y1 - y0) / (double) (x1 - x0); + else { + slope = (y0 - y1) / (double) (x0 - x1); + + // Swap values so that x0 < x1. This preps the following code where y is + // determined by adding a step value (1) to x0 till it reaches x1. + int tmp = x1; + x1 = x0; + x0 = tmp; + + tmp = y1; + y1 = y0; + y0 = tmp; + } + + // If slope is zero or undefined, return tiles directly along the + // appropriate cardinal direction. + if (x0 == x1) { + int smaller = y0 < y1 ? y0 : y1; + int larger = y0 > y1 ? y0 : y1; + System.out.printf("%d %d", smaller, larger); + for (int i = smaller + 1; i < larger; i++) { + ret.add(new Tuple(x0, i)); + } + } else if (y0 == y1) { + int smaller = x0 < x1 ? x0 : x1; + int larger = x0 > x1 ? x0 : x1; + for (int i = smaller + 1; i < larger; i++) { + ret.add(new Tuple(i, y0)); + } + } else { + // Find all coordinates with 0.1 step + int lastX = x0; + int lastY = y0; + for (int i = x0 * 10; i < x1 * 10; i += 1) { + if (i / 10 != lastX || (int) (slope * i / 10) != lastY) { + // Update last values + lastX = i / 10; + lastY = (int) (slope * i / 10); + + // Add new values to array + ret.add(new Tuple(lastX, lastY)); + } + } + } return ret; }