diff --git a/src/main/java/cr0s/warpdrive/block/detection/TileEntityRadar.java b/src/main/java/cr0s/warpdrive/block/detection/TileEntityRadar.java index 6c1928d5..1b726df7 100644 --- a/src/main/java/cr0s/warpdrive/block/detection/TileEntityRadar.java +++ b/src/main/java/cr0s/warpdrive/block/detection/TileEntityRadar.java @@ -76,6 +76,22 @@ public class TileEntityRadar extends TileEntityAbstractEnergy { super.writeToNBT(tag); } + private int calculateEnergyRequired(final int parRadius) { + return (int)Math.round(Math.max(WarpDriveConfig.RADAR_SCAN_MIN_ENERGY_COST, + WarpDriveConfig.RADAR_SCAN_ENERGY_COST_FACTORS[0] + + WarpDriveConfig.RADAR_SCAN_ENERGY_COST_FACTORS[1] * parRadius + + WarpDriveConfig.RADAR_SCAN_ENERGY_COST_FACTORS[2] * parRadius * parRadius + + WarpDriveConfig.RADAR_SCAN_ENERGY_COST_FACTORS[3] * parRadius * parRadius * parRadius)); + } + + private int calculateScanDuration(final int parRadius) { + return (int)Math.round(20 * Math.max(WarpDriveConfig.RADAR_SCAN_MIN_DELAY_SECONDS, + WarpDriveConfig.RADAR_SCAN_DELAY_FACTORS_SECONDS[0] + + WarpDriveConfig.RADAR_SCAN_DELAY_FACTORS_SECONDS[1] * parRadius + + WarpDriveConfig.RADAR_SCAN_DELAY_FACTORS_SECONDS[2] * parRadius * parRadius + + WarpDriveConfig.RADAR_SCAN_DELAY_FACTORS_SECONDS[3] * parRadius * parRadius * parRadius)); + } + // OpenComputer callback methods @Callback @Optional.Method(modid = "OpenComputers") @@ -83,6 +99,18 @@ public class TileEntityRadar extends TileEntityAbstractEnergy { return radius(argumentsOCtoCC(arguments)); } + @Callback + @Optional.Method(modid = "OpenComputers") + public Object[] getEnergyRequired(Context context, Arguments arguments) { + return getEnergyRequired(argumentsOCtoCC(arguments)); + } + + @Callback + @Optional.Method(modid = "OpenComputers") + public Object[] getScanDuration(Context context, Arguments arguments) { + return getScanDuration(argumentsOCtoCC(arguments)); + } + @Callback @Optional.Method(modid = "OpenComputers") public Object[] start(Context context, Arguments arguments) { @@ -117,6 +145,28 @@ public class TileEntityRadar extends TileEntityAbstractEnergy { return new Integer[] { radius }; } + private Object[] getEnergyRequired(Object[] arguments) { + try { + if (arguments.length == 1) { + return new Object[] { calculateEnergyRequired(toInt(arguments[0])) }; + } + } catch (Exception exception) { + return new Integer[] { -1 }; + } + return new Integer[] { -1 }; + } + + private Object[] getScanDuration(Object[] arguments) { + try { + if (arguments.length == 1) { + return new Object[] { 0.050D * calculateScanDuration(toInt(arguments[0])) }; + } + } catch (Exception exception) { + return new Integer[] { -1 }; + } + return new Integer[] { -1 }; + } + private Object[] start(Object[] arguments) { // always clear results results = null; @@ -126,17 +176,19 @@ public class TileEntityRadar extends TileEntityAbstractEnergy { radius = 0; return new Object[] { false, "Invalid radius" }; } - if (!consumeEnergy(Math.max(radius, 100) * Math.max(radius, 100), false)) { + int energyRequired = calculateEnergyRequired(radius); + if (!consumeEnergy(energyRequired, false)) { return new Object[] { false, "Insufficient energy" }; } // Begin searching scanningRadius = radius; - scanningDuration_ticks = (20 * ((scanningRadius / 1000) + 1)); + scanningDuration_ticks = calculateScanDuration(radius); scanning_ticks = 0; if (getBlockMetadata() != 2) { worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 2, 1 + 2); } + WarpDrive.logger.info("!!! Starting scan over radius " + scanningRadius + " for " + energyRequired + " EU, results expected in " + scanningDuration_ticks + " ticks"); return new Object[] { true }; } @@ -184,6 +236,12 @@ public class TileEntityRadar extends TileEntityAbstractEnergy { if (methodName.equals("radius")) { return radius(arguments); + } else if (methodName.equals("getEnergyRequired")) { + return getEnergyRequired(arguments); + + } else if (methodName.equals("getScanDuration")) { + return getScanDuration(arguments); + } else if (methodName.equals("start")) { return start(arguments); diff --git a/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipController.java b/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipController.java index 17b77145..39563151 100644 --- a/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipController.java +++ b/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipController.java @@ -722,9 +722,9 @@ public class TileEntityShipController extends TileEntityAbstractInterfaced { private Object[] getEnergyRequired(Object[] arguments) { try { if (arguments.length == 1 && core != null) { - return new Object[] { (int) (core.calculateRequiredEnergy(getMode(), core.shipMass, toInt(arguments[0]))) }; + return new Object[] { core.calculateRequiredEnergy(getMode(), core.shipMass, toInt(arguments[0])) }; } - } catch (Exception e) { + } catch (Exception exception) { return new Integer[] { -1 }; } return new Integer[] { -1 }; diff --git a/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java b/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java index 936bd199..4eed6c84 100644 --- a/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java +++ b/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java @@ -6,6 +6,7 @@ import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.InputStream; import java.lang.reflect.Method; +import java.util.Arrays; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -143,6 +144,10 @@ public class WarpDriveConfig { // Radar public static int RADAR_MAX_ENERGY_STORED = 100000000; // 100kk eU + public static int RADAR_SCAN_MIN_ENERGY_COST = 10000; + public static double[] RADAR_SCAN_ENERGY_COST_FACTORS = { 0.0, 0.0, 0.0, 0.0001 }; + public static int RADAR_SCAN_MIN_DELAY_SECONDS = 1; + public static double[] RADAR_SCAN_DELAY_FACTORS_SECONDS = { 1.0, 0.001, 0.0, 0.0 }; public static int RADAR_MAX_ISOLATION_RANGE = 2; public static int RADAR_MIN_ISOLATION_BLOCKS = 5; public static int RADAR_MAX_ISOLATION_BLOCKS = 60; @@ -151,8 +156,7 @@ public class WarpDriveConfig { // Ship Scanner public static int SS_MAX_ENERGY_STORED = 500000000; - public static int SS_ENERGY_PER_BLOCK_SCAN = 100; // eU per block of ship volume - // (including air) + public static int SS_ENERGY_PER_BLOCK_SCAN = 100; // eU per block of ship volume (including air) public static int SS_ENERGY_PER_BLOCK_DEPLOY = 5000; public static int SS_MAX_DEPLOY_RADIUS_BLOCKS = 50; @@ -459,7 +463,27 @@ public class WarpDriveConfig { // Radar RADAR_MAX_ENERGY_STORED = clamp(0, Integer.MAX_VALUE, - config.get("radar", "max_energy_stored", RADAR_MAX_ENERGY_STORED).getInt()); + config.get("radar", "max_energy_stored", RADAR_MAX_ENERGY_STORED, "maximum energy stored").getInt()); + + RADAR_SCAN_MIN_ENERGY_COST = clamp(0, Integer.MAX_VALUE, + config.get("radar", "min_energy_cost", RADAR_SCAN_MIN_ENERGY_COST, "minimum energy cost per scan (0+), independantly of radius").getInt()); + RADAR_SCAN_ENERGY_COST_FACTORS = + config.get("radar", "factors_energy_cost", RADAR_SCAN_ENERGY_COST_FACTORS, "energy cost factors {a, b, c, d}. You need to provide exactly 4 values.\n" + + "The equation used is a + b * radius + c * radius^2 + d * radius^3").getDoubleList(); + if (RADAR_SCAN_ENERGY_COST_FACTORS.length != 4) { + RADAR_SCAN_ENERGY_COST_FACTORS = new double[4]; + Arrays.fill(RADAR_SCAN_ENERGY_COST_FACTORS, 1.0); + } + RADAR_SCAN_MIN_DELAY_SECONDS = clamp(1, Integer.MAX_VALUE, + config.get("radar", "scan_min_delay_seconds", RADAR_SCAN_MIN_DELAY_SECONDS, "minimum scan delay per scan (1+), (measured in seconds)").getInt()); + RADAR_SCAN_DELAY_FACTORS_SECONDS = + config.get("radar", "scan_delay_factors_seconds", RADAR_SCAN_DELAY_FACTORS_SECONDS, "scan delay factors {a, b, c, d}. You need to provide exactly 4 values.\n" + + "The equation used is a + b * radius + c * radius^2 + d * radius^3, (measured in seconds)").getDoubleList(); + if (RADAR_SCAN_DELAY_FACTORS_SECONDS.length != 4) { + RADAR_SCAN_DELAY_FACTORS_SECONDS = new double[4]; + Arrays.fill(RADAR_SCAN_DELAY_FACTORS_SECONDS, 1.0); + } + RADAR_MAX_ISOLATION_RANGE = clamp(2, 8, config.get("radar", "max_isolation_range", RADAR_MAX_ISOLATION_RANGE, "radius around core where isolation blocks count (2 to 8), higher is lagger").getInt()); diff --git a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/ping b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/ping index c3fc70a3..3a314d52 100644 --- a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/ping +++ b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/ping @@ -38,24 +38,26 @@ if radius < 1 or radius > 9999 then end energy, energyMax = radar.energy() +energyRequired = radar.getEnergyRequired(radius) +scanDuration = radar.getScanDuration(radius) if energy < radius * radius then - error("Low energy level... (" .. energy .. "/" .. radius * radius .. ")") + error("Low energy level... (" .. energy .. "/" .. energyRequired .. ")") return end radar.radius(radius) radar.start() sleep(0.5) -print("Scanning...") +print("Scanning... (" .. scanDuration .. " s)") +os.sleep(scanDuration) -local seconds = 0 +local delay = 0 local count = nil repeat count = radar.getResultsCount() - sleep(1) - seconds = seconds + 1 -until (count ~= nil and count ~= -1) or seconds > 10 -print("took " .. seconds .. " seconds") + sleep(0.1) + delay = delay + 1 +until (count ~= nil and count ~= -1) or delay > 10 if count ~= nil and count > 0 then for i=0, count-1 do diff --git a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/scan b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/scan index 99991e0a..ba94dabf 100644 --- a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/scan +++ b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/scan @@ -66,7 +66,8 @@ end function scanAndDraw() local energy, energyMax = radar.energy() - if (energy < radius*radius) then + local energyRequired = radar.getEnergyRequired(radius) + if (energy < energyRequired) then hh = math.floor(h / 2) hw = math.floor(w / 2) @@ -80,7 +81,8 @@ function scanAndDraw() end radar.radius(radius) radar.start() - sleep(2) + local scanDuration = radar.getScanDuration(radius) + sleep(scanDuration) redraw() @@ -102,7 +104,7 @@ function redraw() paintutils.drawLine(1, 1, w, 1, colors.black) - textOut((w / 2) - 8, 1, "= Q-Radar v0.1 =", colors.white, colors.black) + textOut((w / 2) - 8, 1, "= Q-Radar v0.2 =", colors.white, colors.black) textOut(w - 3, 1, "[X]", colors.white, colors.red) diff --git a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/ping.lua b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/ping.lua index 17b4a364..066c812b 100644 --- a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/ping.lua +++ b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/ping.lua @@ -20,33 +20,35 @@ if radius < 1 or radius > 9999 then end energy, energyMax = radar.energy() -if energy < radius * radius then - print("Low energy level... (" .. energy .. "/" .. radius * radius .. ")") +energyRequired = radar.getEnergyRequired(radius) +scanDuration = radar.getScanDuration(radius) +if energy < energyRequired then + print("Low energy level... (" .. energy .. "/" .. energyRequired .. ")") return end radar.radius(radius) radar.start() os.sleep(0.5) -print("Scanning...") +print("Scanning... (" .. scanDuration .. " s)") +os.sleep(scanDuration) -local seconds = 0 +local delay = 0 local count = nil repeat count = radar.getResultsCount() - os.sleep(1) - seconds = seconds + 1 -until (count ~= nil and count ~= -1) or seconds > 10 -print("took " .. seconds .. " seconds") + os.sleep(0.1) + delay = delay + 1 +until (count ~= nil and count ~= -1) or delay > 10 if count ~= nil and count > 0 then for i=0, count-1 do success, type, name, x, y, z = radar.getResult(i) - if success then + if success then print(type .. " " .. name .. " @ (" .. x .. " " .. y .. " " .. z .. ")") - else - print("Error " .. type) - end + else + print("Error " .. type) + end end else print("Nothing was found =(") diff --git a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/scan.lua b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/scan.lua index 601f5381..83e1f2ae 100644 --- a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/scan.lua +++ b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/scan.lua @@ -67,7 +67,8 @@ end function scanAndDraw() local energy, energyMax = radar.energy() - if (energy < radius * radius) then + local energyRequired = radar.getEnergyRequired(radius) + if (energy < energyRequired) then hh = math.floor(h / 2) hw = math.floor(w / 2) @@ -79,7 +80,8 @@ function scanAndDraw() end radar.radius(radius) radar.start() - os.sleep(2) + local scanDuration = radar.getScanDuration(radius) + os.sleep(scanDuration) redraw() @@ -104,7 +106,7 @@ function redraw() drawBox(1, h, w, 1, 0x000000) drawBox(w, 1, w, h, 0x000000) - textOut((w / 2) - 8, 1, "= Q-Radar v0.1 =", 0xFFFFFF, 0x000000) + textOut((w / 2) - 8, 1, "= Q-Radar v0.2 =", 0xFFFFFF, 0x000000) textOut(w - 3, 1, "[X]", 0xFFFFFF, 0xFF0000)