Skip to content

Commit

Permalink
Bug fixes and cleanups for prior commits
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed May 26, 2016
1 parent 17f183f commit 97bb441
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public TurtleCommandResult useTool(ITurtleAccess turtle, TurtleSide side, Turtle
// back out if there is no fuel
// note that the laser does still fire if its journey is limited
// by fuel, it just does not go as far
if (turtle.getFuelLevel() == 0)
if (turtle.isFuelNeeded() && turtle.getFuelLevel() == 0)
return TurtleCommandResult.failure("No fuel");

// start block location, used to make boxes for particles and
Expand Down Expand Up @@ -129,22 +129,31 @@ public TurtleCommandResult useTool(ITurtleAccess turtle, TurtleSide side, Turtle

// fuel is consumed all at once for efficiency, we already check
// earlier if we have enough
turtle.consumeFuel(fuelCost);

if (turtle.isFuelNeeded())
turtle.consumeFuel(fuelCost);

// coordinates for bounding boxes and particles
int boxXStart = Math.min(startX, x);
int boxYStart = Math.min(startY, y);
int boxZStart = Math.min(startZ, z);
int boxXEnd = Math.max(startX, x);
int boxYEnd = Math.max(startY, y);
int boxZEnd = Math.max(startZ, z);

// damage entities in the laser beam
// note that it does not specify a type of entity, so that means
// entities such as items and paintings will also be damaged
AxisAlignedBB box = AxisAlignedBB.getBoundingBox(Math.min(startX, x), Math.min(startY, y),
Math.min(startZ, z), Math.max(startX, x) + 1.0D, Math.max(startY, y) + 1.0D,
Math.max(startZ, z) + 1.0D);
AxisAlignedBB box = AxisAlignedBB.getBoundingBox(
boxXStart, boxYStart, boxZStart,
boxXEnd + 1.0D, boxYEnd + 1.0D, boxZEnd + 1.0D);
@SuppressWarnings("unchecked")
List<Entity> entities = world.getEntitiesWithinAABB(Entity.class, box);
for (Entity entity : entities)
entity.attackEntityFrom(DamageSource.onFire, 10);

// draw the laser
KnightPeripheralsPacketHandler.INSTANCE.sendToAllAround(
new TurtleParticleMessage(0, startX, startY, startZ, x, y, z),
new TurtleParticleMessage(0, boxXStart, boxYStart, boxZStart, boxXEnd, boxYEnd, boxZEnd),
new TargetPoint(world.provider.dimensionId, x, y, z, 64));

// we fired the laser, so return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,24 @@ public String[] getMethodNames() {
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments)
throws LuaException, InterruptedException {

// we use the same range arg for all methods, so process it outside of the switch
int range = Config.sensorRange;
if (method != 0) { // except the getter of course
if (arguments.length > 0 && !(arguments[0] instanceof Double))
throw new LuaException("Bad argument #1 (expected number)");

if (arguments.length > 0) {
// yes, I have to cast it three times since the classes can only
// be cast to the corresponding primitive type
int rangeArg = (int)(double)(Double) arguments[0];
// if the arg is too big, throw an exception
if (rangeArg > range)
throw new LuaException("Range cannot be greater than " + range);
else
range = rangeArg;
}
}

switch (method) {
// getRange - returns the maximum range for the sensor
case 0:
Expand All @@ -42,19 +59,9 @@ public Object[] callMethod(IComputerAccess computer, ILuaContext context, int me
case 1:
case 2:
case 3: {
// test the first parameter for a number
if (arguments.length > 0 && !(arguments[0] instanceof Double)) {
throw new LuaException("Bad argument #1 (expected number)");
}

// setup the parameters
int direction = method == 2 ? 1 : method == 3 ? 0 : turtle.getDirection();

// yes, I have to cast it three times since the classes can only
// be cast to the corresponding primitive type
if (arguments.length > 0)
range = (int) (double) (Double) arguments[0];

// execute main task
return context.executeMainThreadTask(new TaskSensorSonar(turtle, direction, range));
}
Expand All @@ -63,19 +70,9 @@ public Object[] callMethod(IComputerAccess computer, ILuaContext context, int me
case 4:
case 5:
case 6: {
// test the first parameter for a number
if (arguments.length > 0 && !(arguments[0] instanceof Double)) {
throw new LuaException("Bad argument #1 (expected number)");
}

// setup the parameters
int direction = method == 5 ? 1 : method == 6 ? 0 : turtle.getDirection();

// yes, I have to cast it three times since the classes can only
// be cast to the corresponding primitive type
if (arguments.length > 0)
range = (int) (double) (Double) arguments[0];

Object[] result = context.executeMainThreadTask(new TaskSensorDensity(turtle, direction, range));

// delay the command to discourage using it to find exact
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ public TaskSensorDensity(ITurtleAccess turtle, int direction, int range) {
public Object[] execute() throws LuaException {
ChunkCoordinates turtlePos = turtle.getPosition();
World world = turtle.getWorld();
int startX = turtlePos.posX;
int startY = turtlePos.posY;
int startZ = turtlePos.posZ;
int x = startX;
int y = startY;
int z = startZ;
int x = turtlePos.posX;
int y = turtlePos.posY;
int z = turtlePos.posZ;

double density = 0;
for (int i = 0; i < range; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,29 @@ public TaskSensorSonar(ITurtleAccess turtle, int direction, int range) {

@Override
public Object[] execute() throws LuaException {
ChunkCoordinates turtlePos = turtle.getPosition();
World world = turtle.getWorld();
int x = turtlePos.posX;
int y = turtlePos.posY;
int z = turtlePos.posZ;

int distance = 0;
boolean found = false;
for (int i = 0; i < range; i++) {
ChunkCoordinates turtlePos = turtle.getPosition();
World world = turtle.getWorld();
int startX = turtlePos.posX;
int startY = turtlePos.posY;
int startZ = turtlePos.posZ;
int x = startX;
int y = startY;
int z = startZ;

x += Facing.offsetsXForSide[direction];
y += Facing.offsetsYForSide[direction];
z += Facing.offsetsZForSide[direction];

// first, make sure the block is either a liquid or air, if not then
// end
// first, make sure the block is either a liquid or air, if not end
// note liquids increase the value
int result = 0;
if (world.isAirBlock(x, y, z))
result = 1;
else if (world.getBlock(x, y, z).getMaterial().isLiquid())
int result = 1;
if (world.getBlock(x, y, z).getMaterial().isLiquid())
result = 2; // liquids add two to the distance
else {
else if (!world.isAirBlock(x, y, z)) {
distance += 1;
found = true;
break;
}

distance += result;

// next, check if the block contains an entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
import knightminer.knightperipherals.KnightPeripherals;
import knightminer.knightperipherals.reference.Reference;

public class KnightPeripheralsPacketHandler {
private static int id = 0;
public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MOD_ID);

public static void init() {
KnightPeripherals.logger.info("Loading packet handler");

// used to add particles to the turtle for the mining laser beam and
// alike
INSTANCE.registerMessage(TurtleParticleMessage.TurtleParticleHandler.class, TurtleParticleMessage.class, id++,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ public IMessage onMessage(TurtleParticleMessage message, MessageContext ctx) {
for (float px = message.startX; px <= message.endX; px += 0.1D)
for (float py = message.startY; py <= message.endY; py += 0.1D)
for (float pz = message.startZ; pz <= message.endZ; pz += 0.1D)
ctx.getClientHandler().handleParticles(new S2APacketParticles("reddust", px + 0.5F,
py + 0.5F, pz + 0.5F, -1.0F, 1.0F, 1.0F, 1.0F, 0));
ctx.getClientHandler().handleParticles(new S2APacketParticles(
"reddust",
px + 0.5F, py + 0.5F, pz + 0.5F,
-1.0F, 1.0F, 1.0F,
1.0F, 0));

break;

Expand Down

0 comments on commit 97bb441

Please sign in to comment.