Mini Utilities

Mini Utilities

2M Downloads

Laser System

BobVonBob opened this issue ยท 0 comments

commented

I think the laser power system could perhaps use another look.

First issue, I believe this line is preventing laser hubs from being placed, as they seemingly detect themselves.

if (tileEntity.getTEsInRadius(LaserHubTile.class, 16).size() > 0) world.destroyBlock(pos, true);

(The intention here seems to be to prevent overlapping? If so the radius is too small. If two hubs are placed 17 blocks apart they won't break, but nearly half their range will be shared)

However, this is probably a blessing in disguise. Hubs currently scan 32,768 blocks

every tick

public void tick() {
if (world.isRemote) {return;}
List<LaserPortTile> blocks = getTEsInRadius(LaserPortTile.class, 16);

public <T extends TileEntity> List<T> getTEsInRadius(Class<T> clazz, int radius) {
List<T> output = new ArrayList<>();
for (int x = -radius; x < radius; x++) {
for (int y = -radius; y < radius; y++) {
for (int z = -radius; z < radius; z++) {
if (clazz.isInstance(world.getTileEntity(getPos().add(x, y, z)))) {
output.add((T) world.getTileEntity(getPos().add(x, y, z)));
}
}
}
}
return output;
}

and every frame.

public void render(LaserHubTile tile, float partialTicks, MatrixStack matrixStack, IRenderTypeBuffer buffer, int combinedLightIn, int combinedOverlayIn) {
List<Vector3d> vectorList = tile.getTEVectorsInRadius(LaserPortTile.class, 16, 0.25);
drawLinesFromPoint(matrixStack, Vector3d.copy(tile.getPos()), vectorList, new Color(255, 0, 0, 255), buffer, new Vector3d(0.5, 0.75, 0.5));
}

public List<Vector3d> getTEVectorsInRadius(Class<?> clazz, int radius, double distanceFromCenter) {
List<Vector3d> output = new ArrayList<>();
for (int x = -radius; x < radius; x++) {
for (int y = -radius; y < radius; y++) {
for (int z = -radius; z < radius; z++) {
BlockPos pos = getPos().add(x, y, z);
if (clazz.isInstance(world.getTileEntity(pos))) {
Vector3d v3d = Vector3d.copy(pos);
v3d.add(0.5, 0.5, 0.5);
if (world.getBlockState(pos).hasProperty(LaserPortBlock.FACING)) {
switch (world.getBlockState(pos).get(LaserPortBlock.FACING)) {
case UP:
v3d = v3d.add(0, -distanceFromCenter, 0);
break;
case DOWN:
v3d = v3d.add(0, distanceFromCenter, 0);
break;
case NORTH:
v3d = v3d.add(0, 0, distanceFromCenter);
break;
case SOUTH:
v3d = v3d.add(0, 0, -distanceFromCenter);
break;
case WEST:
v3d = v3d.add(distanceFromCenter, 0, 0);
break;
case EAST:
v3d = v3d.add(-distanceFromCenter, 0.5, 0.5);
break;
}
}
output.add(v3d);
}
}
}
}
return output;
}

I'm not a modder, but I suspect that's not very performant.

Some ideas:
You could take the route many wireless mods choose (LaserIO, Modular Routers, etc.) and require lasers to be linked by the player, removing automatic scanning entirely.

If you want to maintain the automatic linking functionality another option would be to have the hubs and ports scan, communicate, and save relevant positions when placed, then notify others when destroyed. I believe Powah! energizing orbs work like this.