Fixed OpenComputers support

Fixed Transporter, ChunkLoader, EnergyBank, EnanReactorCore and
LaserTreeFarm crahsing OC computer when CC isn't loaded
Added OpenComputers callbacks to Enantiomorphic reactor
This commit is contained in:
LemADEC 2015-09-05 00:43:35 +02:00
parent 940c803e8a
commit 71006f32b7
6 changed files with 112 additions and 43 deletions

View file

@ -2,6 +2,7 @@ package cr0s.warpdrive.block;
import java.util.Map;
import cpw.mods.fml.common.Optional;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.chunk.Chunk;
@ -144,6 +145,7 @@ public class TileEntityChunkLoader extends TileEntityAbstractChunkLoading implem
}
@Override
@Optional.Method(modid = "ComputerCraft")
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) {
String meth = methodsArray[method];

View file

@ -2,6 +2,7 @@ package cr0s.warpdrive.block.collection;
import java.util.LinkedList;
import cpw.mods.fml.common.Optional;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@ -243,6 +244,7 @@ public class TileEntityLaserTreeFarm extends TileEntityAbstractMiner {
// ComputerCraft IPeripheral methods implementation
@Override
@Optional.Method(modid = "ComputerCraft")
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) {
String methodName = methodsArray[method];
if (methodName.equals("start")) {

View file

@ -2,11 +2,15 @@ package cr0s.warpdrive.block.energy;
import java.util.Arrays;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Optional;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.api.IBlockUpdateDetector;
import cr0s.warpdrive.block.TileEntityAbstractEnergy;
@ -16,33 +20,33 @@ import dan200.computercraft.api.peripheral.IComputerAccess;
public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implements IBlockUpdateDetector {
private int containedEnergy = 0;
// generation & instability is 'per tick'
private static final int PR_MIN_GENERATION = 4;
private static final int PR_MAX_GENERATION = 64000;
private static final double PR_MIN_INSTABILITY = 0.004D;
private static final double PR_MAX_INSTABILITY = 0.060D;
// explosion parameters
private static final int PR_MAX_EXPLOSION_RADIUS = 6;
private static final double PR_MAX_EXPLOSION_REMOVAL_CHANCE = 0.1D;
// laser stabilization is per shot
// target is to consume 10% max output power every second, hence 2.5% per side
// laser efficiency is 33% at 16% power (target spot), 50% at 24% power, 84% at 50% power, etc.
// 10% * 20 * PR_MAX_GENERATION / (4 * 0.16) => ~200kRF => ~ max laser energy
private static final double PR_MAX_LASER_ENERGY = 200000.0D;
private static final double PR_MAX_LASER_EFFECT = PR_MAX_INSTABILITY * 20 / 0.33D;
private int tickCount = 0;
private double[] instabilityValues = { 0.0D, 0.0D, 0.0D, 0.0D }; // no instability = 0, explosion = 100
private float lasersReceived = 0;
private int lastGenerationRate = 0;
private int releasedThisTick = 0; // amount of energy released during current tick update
private int releasedThisCycle = 0; // amount of energy released during current cycle
private int releasedLastCycle = 0;
private boolean hold = true; // hold updates and power output until reactor is controlled (i.e. don't explode on chunk-loading while computer is booting)
private boolean active = false;
private static final int MODE_DONT_RELEASE = 0;
@ -53,9 +57,9 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
private int releaseMode = 0;
private int releaseRate = 0;
private int releaseAbove = 0;
private boolean init = false;
public TileEntityEnanReactorCore() {
super();
peripheralName = "warpdriveEnanReactorCore";
@ -70,12 +74,12 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
};
CC_scripts = Arrays.asList("startup");
}
private void increaseInstability(ForgeDirection from, boolean isNatural) {
if (canOutputEnergy(from) || hold) {
return;
}
int side = from.ordinal() - 2;
if (containedEnergy > WarpDriveConfig.ENAN_REACTOR_UPDATE_INTERVAL_TICKS * PR_MIN_GENERATION * 100) {
double amountToIncrease = WarpDriveConfig.ENAN_REACTOR_UPDATE_INTERVAL_TICKS
@ -89,27 +93,27 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
instabilityValues[side] = Math.max(0.0D, instabilityValues[side] - amountToDecrease);
}
}
private void increaseInstability(boolean isNatural) {
increaseInstability(ForgeDirection.NORTH, isNatural);
increaseInstability(ForgeDirection.SOUTH, isNatural);
increaseInstability(ForgeDirection.EAST, isNatural);
increaseInstability(ForgeDirection.WEST, isNatural);
}
public void decreaseInstability(ForgeDirection from, int energy) {
if (canOutputEnergy(from)) {
return;
}
// laser is active => start updating reactor
hold = false;
int amount = convertInternalToRF(energy);
if (amount <= 1) {
return;
}
lasersReceived = Math.min(10.0F, lasersReceived + 1F / WarpDriveConfig.ENAN_REACTOR_MAX_LASERS_PER_SECOND);
double nospamFactor = 1.0;
if (lasersReceived > 1.0F) {
@ -122,9 +126,9 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
double baseLaserEffect = 0.5D + 0.5D * Math.cos(Math.PI - (1.0D + Math.log10(0.1D + 0.9D * normalisedAmount)) * Math.PI); // 0.0 to 1.0
double randomVariation = 0.8D + 0.4D * worldObj.rand.nextDouble(); // ~1.0
double amountToRemove = PR_MAX_LASER_EFFECT * baseLaserEffect * randomVariation * nospamFactor;
int side = from.ordinal() - 2;
if (WarpDriveConfig.LOGGING_ENERGY) {
if (side == 3) {
WarpDrive.logger.info("Instability on " + from.toString()
@ -132,18 +136,18 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
+ " after consuming " + amount + "/" + PR_MAX_LASER_ENERGY + " lasersReceived is " + String.format("%.1f", lasersReceived) + " hence nospamFactor is " + nospamFactor);
}
}
instabilityValues[side] = Math.max(0, instabilityValues[side] - amountToRemove);
updateSideTextures();
}
private void generateEnergy() {
double stabilityOffset = 0.5;
for (int i = 0; i < 4; i++) {
stabilityOffset *= Math.max(0.01D, instabilityValues[i] / 100.0D);
}
if (active) {// producing, instability increase output, you want to take the risk
int amountToGenerate = (int) Math.ceil(WarpDriveConfig.ENAN_REACTOR_UPDATE_INTERVAL_TICKS * (0.5D + stabilityOffset)
* (PR_MIN_GENERATION + PR_MAX_GENERATION * Math.pow(containedEnergy / (double) WarpDriveConfig.ENAN_REACTOR_MAX_ENERGY_STORED, 0.6D)));
@ -162,21 +166,21 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
}
}
}
@Override
public void updateEntity() {
super.updateEntity();
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
return;
}
if (WarpDriveConfig.LOGGING_ENERGY) {
WarpDrive.logger.info("tickCount " + tickCount + " releasedThisTick " + releasedThisTick + " lasersReceived " + lasersReceived
+ " releasedThisCycle " + releasedThisCycle + " containedEnergy " + containedEnergy);
}
releasedThisTick = 0;
lasersReceived = Math.max(0.0F, lasersReceived - 0.05F);
tickCount++;
if (tickCount < WarpDriveConfig.ENAN_REACTOR_UPDATE_INTERVAL_TICKS) {
@ -185,25 +189,25 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
tickCount = 0;
releasedLastCycle = releasedThisCycle;
releasedThisCycle = 0;
if (!init) {
init = true;
updatedNeighbours();
}
updateSideTextures();
// unstable at all time
if (shouldExplode()) {
explode();
}
increaseInstability(true);
generateEnergy();
sendEvent("reactorPulse", new Object[] { lastGenerationRate });
}
private void explode() {
// remove blocks randomly up to x blocks around (breaking whatever protection is there)
double normalizedEnergy = containedEnergy / (double) WarpDriveConfig.ENAN_REACTOR_MAX_ENERGY_STORED;
@ -236,7 +240,7 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
4.0F + worldObj.rand.nextInt(3), true, true);
}
}
private void updateSideTextures() {
double maxInstability = 0.0D;
for (Double ins : instabilityValues) {
@ -246,20 +250,20 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
}
int instabilityNibble = (int) Math.max(0, Math.min(3, Math.round(maxInstability / 25.0D)));
int energyNibble = (int) Math.max(0, Math.min(3, Math.round(4.0D * containedEnergy / WarpDriveConfig.ENAN_REACTOR_MAX_ENERGY_STORED)));
int metadata = 4 * instabilityNibble + energyNibble;
if (getBlockMetadata() != metadata) {
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, metadata, 3);
}
}
private boolean shouldExplode() {
boolean exploding = false;
for (int i = 0; i < 4; i++) {
exploding = exploding || (instabilityValues[i] >= 100);
}
exploding &= (worldObj.rand.nextInt(4) == 2);
if (exploding) {
WarpDrive.logger.info(this
+ String.format(" Explosion trigerred, Instability is [%.2f, %.2f, %.2f, %.2f], Energy stored is %d, Laser received is %.2f, %s",
@ -289,13 +293,19 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
// OpenComputer callback methods
// FIXME: implement OpenComputers...
@Callback
@Optional.Method(modid = "OpenComputers")
public Object[] active(Context context, Arguments arguments) throws Exception {
return active(argumentsOCtoCC(arguments));
}
public Object[] active(Object[] arguments) throws Exception {
if (arguments.length == 1) {
boolean activate = false;
try {
activate = toBool(arguments[0]);
} catch (Exception e) {
throw new Exception("Function expects an boolean value");
throw new Exception("Function expects a boolean value");
}
if (active && !activate) {
sendEvent("reactorDeactivation", null);
@ -313,13 +323,19 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
}
}
@Callback
@Optional.Method(modid = "OpenComputers")
public Object[] release(Context context, Arguments arguments) throws Exception {
return release(argumentsOCtoCC(arguments));
}
private Object[] release(Object[] arguments) throws Exception {
boolean doRelease = false;
if (arguments.length > 0) {
try {
doRelease = toBool(arguments[0]);
} catch (Exception e) {
throw new Exception("Function expects an boolean value");
throw new Exception("Function expects a boolean value");
}
releaseMode = doRelease ? MODE_MANUAL_RELEASE : MODE_DONT_RELEASE;
@ -329,6 +345,12 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
return new Object[] { releaseMode != MODE_DONT_RELEASE };
}
@Callback
@Optional.Method(modid = "OpenComputers")
public Object[] releaseRate(Context context, Arguments arguments) throws Exception {
return releaseRate(argumentsOCtoCC(arguments));
}
private Object[] releaseRate(Object[] arguments) throws Exception {
int rate = -1;
try {
@ -349,6 +371,12 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
return new Object[] { MODE_STRING[releaseMode], releaseRate };
}
@Callback
@Optional.Method(modid = "OpenComputers")
public Object[] releaseAbove(Context context, Arguments arguments) throws Exception {
return releaseAbove(argumentsOCtoCC(arguments));
}
private Object[] releaseAbove(Object[] arguments) throws Exception {
int above = -1;
try {
@ -368,8 +396,15 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
return new Object[] { MODE_STRING[releaseMode], releaseAbove };
}
@Callback
@Optional.Method(modid = "OpenComputers")
public Object[] instability(Context context, Arguments arguments) throws Exception {
return new Double[] { instabilityValues[0], instabilityValues[1], instabilityValues[2], instabilityValues[3] };
}
// ComputerCraft IPeripheral methods implementation
@Override
@Optional.Method(modid = "ComputerCraft")
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) {
// computer is alive => start updating reactor
hold = false;
@ -441,7 +476,7 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
}
return false;
}
@Override
protected void energyOutputDone(int energyOutput_internal) {
int energyOutput_RF = convertInternalToRF(energyOutput_internal);
@ -455,17 +490,17 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
WarpDrive.logger.info("OutputDone " + energyOutput_internal + " (" + energyOutput_RF + " RF)");
}
}
@Override
public int getEnergyStored() {
return convertRFtoInternal(containedEnergy);
}
@Override
public int getMaxEnergyStored() {
return convertRFtoInternal(WarpDriveConfig.ENAN_REACTOR_MAX_ENERGY_STORED);
}
// Forge overrides
@Override
public void writeToNBT(NBTTagCompound nbt) {
@ -480,7 +515,7 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
nbt.setDouble("i3", instabilityValues[3]);
nbt.setBoolean("active", active);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
@ -494,7 +529,7 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen
instabilityValues[3] = nbt.getDouble("i3");
active = nbt.getBoolean("active");
}
@Override
public String toString() {
return String.format("%s \'%s\' @ \'%s\' %.2f, %.2f, %.2f", new Object[] {

View file

@ -1,5 +1,8 @@
package cr0s.warpdrive.block.energy;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
import cpw.mods.fml.common.Optional;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
@ -148,6 +151,29 @@ public class TileEntityEnanReactorLaser extends TileEntityAbstractLaser implemen
super.readFromNBT(nbt);
}
// OpenComputers callback methods
@Callback
@Optional.Method(modid = "OpenComputers")
public Object[] hasReactor(Context context, Arguments arguments) {
return new Object[] { scanForReactor() != null };
}
@Callback
@Optional.Method(modid = "OpenComputers")
public Object[] sendLaser(Context context, Arguments arguments) {
if (arguments.count() >= 1) {
laserReactor(arguments.checkInteger(0));
}
return null;
}
@Callback
@Optional.Method(modid = "OpenComputers")
public Object[] side(Context context, Arguments arguments) {
return new Object[] { side.ordinal() - 2 };
}
// ComputerCraft methods
@Override
@Optional.Method(modid = "ComputerCraft")

View file

@ -1,5 +1,6 @@
package cr0s.warpdrive.block.energy;
import cpw.mods.fml.common.Optional;
import net.minecraftforge.common.util.ForgeDirection;
import cr0s.warpdrive.block.TileEntityAbstractEnergy;
import cr0s.warpdrive.config.WarpDriveConfig;
@ -45,6 +46,7 @@ public class TileEntityEnergyBank extends TileEntityAbstractEnergy {
// ComputerCraft IPeripheral methods implementation
@Override
@Optional.Method(modid = "ComputerCraft")
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) {
String methodName = methodsArray[method];
if (methodName == "getEnergyLevel") {

View file

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import cpw.mods.fml.common.Optional;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
@ -138,6 +139,7 @@ public class TileEntityTransporter extends TileEntityAbstractEnergy implements I
}
@Override
@Optional.Method(modid = "ComputerCraft")
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) {
String methodName = methodsArray[method];
if (methodName.equals("getEnergyLevel")) {