Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes + tunnel colors + transparent latch outputs #179

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void renderCADCursor(GuiCAD parent, double mouseX, double mouseY, int gri
CircuitPartRenderer.renderPart(selectedPart, gridX * PART_SIZE, gridY * PART_SIZE);
GL11.glPopMatrix();
GL11.glColor3f(1, 1, 1);
} else if (selectedPart.getPart() instanceof PartWire) {
} else if (selectedPart.getPart().getClass().equals(PartWire.class)) {
PartWire wire = (PartWire) selectedPart.getPart();
switch (wire.getColor(selectedPart.getPos(), selectedPart)) {
case 1:
Expand Down Expand Up @@ -143,7 +143,7 @@ public void onMouseDown(GuiCAD parent, int mx, int my, int button) {
if (gridX > 0 && gridY > 0 && gridX < w - 1 && gridY < w - 1 && !GuiScreen.isShiftKeyDown()) {
parent.startX = gridX;
parent.startY = gridY;
if (selectedPart.getPart() instanceof PartWire) {
if (selectedPart.getPart().getClass().equals(PartWire.class)) {
parent.drag = true;
}
}
Expand All @@ -160,7 +160,7 @@ public void onMouseUp(GuiCAD parent, int mx, int my, int button) {
}

if (parent.drag) {
if (selectedPart.getPart() instanceof PartWire) {
if (selectedPart.getPart().getClass().equals(PartWire.class)) {
int id = CircuitPart.getId(selectedPart.getPart());
int state = selectedPart.getState();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,22 @@ public boolean mousePressed(Minecraft mc, int par1, int par2) {
public List<String> getHoverInformation() {
ArrayList<String> text = new ArrayList<String>();
ForgeDirection dir = MiscUtils.getDirection(side);
if (te.getCircuitData().getProperties().getModeAtSide(side) == EnumConnectionType.ANALOG)
text.add("S: " + color);
else
text.add("F: 0x" + Integer.toHexString(color));
if (isActive) {
EnumConnectionType mode = te.getCircuitData().getProperties().getModeAtSide(side);
if (mode != EnumConnectionType.SIMPLE) {
if (mode == EnumConnectionType.ANALOG)
text.add("S: " + color);
else
text.add("F: 0x" + Integer.toHexString(color));
}
text.add("I: "
+ I18n.format("gui.integratedcircuits.cad.mode."
+ (te.getExternalInputFromSide(dir, color) ? "high" : "low")));
text.add("O: "
+ I18n.format("gui.integratedcircuits.cad.mode."
+ (te.getOutputToSide(dir, color) ? "high" : "low")));
}
} else
text.add("N");
return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import moe.nightfall.vic.integratedcircuits.cp.part.PartIOBit;
import moe.nightfall.vic.integratedcircuits.cp.part.PartNull;
import moe.nightfall.vic.integratedcircuits.misc.CraftingAmount;
import moe.nightfall.vic.integratedcircuits.misc.MiscUtils;
import moe.nightfall.vic.integratedcircuits.misc.Vec2;

import net.minecraft.nbt.NBTTagCompound;
Expand All @@ -31,7 +32,7 @@
public class CircuitData implements Cloneable {

// cdata version
public static final int version = 1;
public static final int version = 2;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the legacy loader doesn't work at all (because it doesn't do the steps in sequence, there's a big TODO on it), so if you decide to change the version it should probably be fixed first

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These steps are NBT, transform and postTransform. For what I did this is fine.
Anyway, the proper solution is to do conversion using separate "frozen" codebase that will never have to be changed. That is, no CircuitData or any other such classes. It should receive NBT and produce new NBT.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess... Still would like to see that get fixed though


private int size;
private int[][] meta;
Expand Down Expand Up @@ -155,60 +156,53 @@ private EnumConnectionType[] getAndFixModePerSide() {
/** Clears the circuit and sets it up **/
public void clearAllAndSetup(int size) {
clearAll(size);
setupIO();
FixIO();
}

/** Clears the IOBits, and sets them up again. **/
public void clearIOAndSetupIO() {
clearIO();
setupIO();
}

/** Sets up the IOBits for the circuit. **/
public void setupIO() {
/** Sets up the IOBits and clears unused ones. **/
public void FixIO() {
getAndFixModePerSide();

int o = (supportsBundled() ? size / 2 - 8 : 1);
int o = (supportsBundled() ? size / 2 - 8 : 1); // Offset of first IOBit
// Get the ID of the IOBit
int cid = CircuitPart.getId(PartIOBit.class);
Vec2[] pos = new Vec2[4];

for (int i = 0; i < (supportsBundled() ? 16 : 1); i++) {
// Get the positions
Vec2 pos1 = new Vec2(size - 1 - (i + o), 0);
Vec2 pos2 = new Vec2(size - 1, size - 1 - (i + o));
Vec2 pos3 = new Vec2(i + o, size - 1);
Vec2 pos4 = new Vec2(0, i + o);

if (prop.getModeAtSide(0) != EnumConnectionType.NONE && !(prop.getModeAtSide(0) == EnumConnectionType.SIMPLE && i >= 1)) {
// Set the part at the position to be a IOBit
setID(pos1, cid);
// Get the IOBit at that position
PartIOBit io1 = (PartIOBit) getPart(pos1);
// Set the number of the IOBit (colour / redstone strength)
io1.setFrequency(pos1, parent, i);
// The rotation is what side the IOBit is on
io1.setRotation(pos1, parent, 0);
}

if (prop.getModeAtSide(1) != EnumConnectionType.NONE && !(prop.getModeAtSide(1) == EnumConnectionType.SIMPLE && i >= 1)) {
setID(pos2, cid);
PartIOBit io2 = (PartIOBit) getPart(pos2);
io2.setFrequency(pos2, parent, i);
io2.setRotation(pos2, parent, 1);
}
pos[0] = new Vec2(size - 1 - (i + o), 0);
pos[1] = new Vec2(size - 1, size - 1 - (i + o));
pos[2] = new Vec2(i + o, size - 1);
pos[3] = new Vec2(0, i + o);

if (prop.getModeAtSide(2) != EnumConnectionType.NONE && !(prop.getModeAtSide(2) == EnumConnectionType.SIMPLE && i >= 1)) {
setID(pos3, cid);
PartIOBit io3 = (PartIOBit) getPart(pos3);
io3.setFrequency(pos3, parent, i);
io3.setRotation(pos3, parent, 2);
}

if (prop.getModeAtSide(3) != EnumConnectionType.NONE && !(prop.getModeAtSide(3) == EnumConnectionType.SIMPLE && i >= 1)) {
setID(pos4, cid);
PartIOBit io4 = (PartIOBit) getPart(pos4);
io4.setFrequency(pos4, parent, i);
io4.setRotation(pos4, parent, 3);
for (int j = 0; j < 4; j++) {
if (prop.getModeAtSide(j) != EnumConnectionType.NONE && !(prop.getModeAtSide(j) == EnumConnectionType.SIMPLE && i >= 1)) {
// Set the part at the position to be a IOBit
setID(pos[j], cid);
// Get the IOBit at that position
PartIOBit io = (PartIOBit) getPart(pos[j]);
// Set the number of the IOBit (colour / redstone strength)
io.setFrequency(pos[j], parent, i);
// The rotation is what side the IOBit is on
io.setRotation(pos[j], parent, j);
// Make sure that IOBit does not stall
io.scheduleInputChange(pos[j], parent);
} else {
// Get old part at the position
CircuitPart part = getPart(pos[j]);
if (part instanceof PartIOBit) {
// There was an IOBit. Clear corresponding output signal.
PartIOBit io = (PartIOBit)part;
parent.setOutputToSide(
MiscUtils.getDirection(io.getRotation(pos[j], parent)),
io.getFrequency(pos[j], parent), false);
}
// Clear part at the position
setID(pos[j], 0);
setMeta(pos[j], 0);
// Notify neighbour parts about is
getPart(pos[j]).notifyNeighbours(pos[j], parent);
}
}
}
}
Expand Down Expand Up @@ -238,17 +232,6 @@ public void clearColumn(int x) {
}
}

public void clearIO() {
// Clear top
clearRow(0);
// Clear bottom
clearRow(this.size - 1);
// Clear left
clearColumn(0);
// Clear right
clearColumn(this.size - 1);
}

/** Clears the contents of the circuit and gives it a new size. **/
public void clearContents(int size) {
this.id = new int[size][size];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public final CircuitPart getNeighbourOnSide(Vec2 pos, ICircuit parent, ForgeDire
return parent.getCircuitData().getPart(pos.offset(side));
}

public final boolean getInput(Vec2 pos, ICircuit parent) {
public boolean getInput(Vec2 pos, ICircuit parent) {
return getInputFromSide(pos, parent, ForgeDirection.NORTH)
|| getInputFromSide(pos, parent, ForgeDirection.EAST)
|| getInputFromSide(pos, parent, ForgeDirection.SOUTH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class LegacyLoader implements Comparable<LegacyLoader> {
private static final List<LegacyLoader> legacyLoaders = new ArrayList<LegacyLoader>();
static {
legacyLoaders.add(new LegacyLoader_0_8());
legacyLoaders.add(new LegacyLoader_1_Tunnels());
Collections.sort(legacyLoaders);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package moe.nightfall.vic.integratedcircuits.cp.legacy;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import moe.nightfall.vic.integratedcircuits.cp.CircuitData;
import moe.nightfall.vic.integratedcircuits.cp.legacy.LegacyLoader;
import moe.nightfall.vic.integratedcircuits.misc.MiscUtils;
import moe.nightfall.vic.integratedcircuits.misc.Vec2;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;

public final class LegacyLoader_1_Tunnels extends LegacyLoader {
@Override
public int getVersion() {
return 1;
}

{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only a minor thing, but I hate using the initialization blocks (looks a lot cleaner in scala), so rather make it a normal constructor.
Also, class names with _ are definitely not my favorites

addTransformer(new PartTunnelTransformer(), 27);
}

private static class PartTunnelTransformer extends PartTransformer {
// The old PartTunnel property map
// PartCPGate:
protected final int oldInput = old.allocate(4);
// PartTunnel:
protected final int oldPosX = old.allocate(8);
protected final int oldPosY = old.allocate(8);
protected final int oldIn = old.allocate();

// Tunnels are now derived from wires
// PartCPGate:
protected final int newInput = transformed.allocate(4);
// PartWire:
protected final int newColor = transformed.allocate(2);
// PartTunnel:
protected final int newPosX = transformed.allocate(8);
protected final int newPosY = transformed.allocate(8);
protected final int newIn = transformed.allocate();

@Override
public void transformImpl() {
setInt(newInput, getInt(oldInput));
// Old tunnels are converted to green tunnels
setInt(newColor, 0); // 0 is green
setInt(newPosX, getInt(oldPosX));
setInt(newPosY, getInt(oldPosY));
setBit(newIn, getBit(oldIn));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
import net.minecraft.init.Items;
import net.minecraftforge.common.util.ForgeDirection;

public class PartTunnel extends CircuitPart {
public class PartTunnel extends PartWire {

public final IntProperty PROP_POS_X = new IntProperty("PROP_POS_X", stitcher, 255);
public final IntProperty PROP_POS_Y = new IntProperty("PROP_POS_Y", stitcher, 255);
public final BooleanProperty PROP_IN = new BooleanProperty("PROP_IN", stitcher);

@Override
public boolean getInput(Vec2 pos, ICircuit parent) {
return super.getInput(pos, parent) || getProperty(pos, parent, PROP_IN);
}

// pos is for CURRENT part
public Vec2 getConnectedPos(Vec2 pos, ICircuit parent) {
return new Vec2(getProperty(pos, parent, PROP_POS_X), getProperty(pos, parent, PROP_POS_Y));
Expand Down Expand Up @@ -80,7 +85,7 @@ public boolean getOutputToSide(Vec2 pos, ICircuit parent, ForgeDirection side) {
boolean in = getProperty(pos, parent, PROP_IN);
if (side == ForgeDirection.UNKNOWN)
return getInput(pos, parent) && !in;
return (getInput(pos, parent) || in) && !getInputFromSide(pos, parent, side);
return (getInput(pos, parent)) && !getInputFromSide(pos, parent, side);
}

@Override
Expand Down Expand Up @@ -126,20 +131,6 @@ public void onRemoved(Vec2 pos, ICircuit parent) {
dropConnected(pos, parent, getConnectedPos(pos, parent));
}

@Override
public void renderPart(Vec2 pos, ICircuit parent, double x, double y, EnumRenderType type) {
Tessellator tes = Tessellator.instance;

RenderUtils.applyColorIRGBA(tes, Config.colorGreen);
CircuitPartRenderer.addQuad(x, y, 16, 4 * 16, 16, 16);
if (getInput(pos, parent) || getProperty(pos, parent, PROP_IN)) {
RenderUtils.applyColorIRGBA(tes, Config.colorGreen);
} else {
RenderUtils.applyColorIRGBA(tes, Config.colorGreen, 0.4F);
}
CircuitPartRenderer.addQuad(x, y, 0, 4 * 16, 16, 16);
}

@Override
public Category getCategory() {
return Category.WIRE;
Expand All @@ -156,13 +147,15 @@ public String getLocalizedName(Vec2 pos, ICircuit parent) {

@Override
public void getCraftingCost(CraftingAmount amount, CircuitData parent, Vec2 pos) {
// Tunnels are twice as expensive as wires and also cost a bit of silicon.
amount.add(new ItemAmount(Items.redstone, 0.1));
amount.add(new ItemAmount(Content.itemSiliconDrop, 0.1));

int data = parent.getMeta(pos);
Vec2 end = new Vec2(PROP_POS_X.get(data), PROP_POS_Y.get(data));
if (isConnected(end)) {
amount.add(new ItemAmount(Items.redstone, 0.1 * pos.distanceTo(end)));
// Half the amount, because it will be added twice.
amount.add(new ItemAmount(Items.redstone, 0.05 * pos.distanceTo(end)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ public boolean getOutputToSide(Vec2 pos, ICircuit parent, ForgeDirection side) {

@Override
@SideOnly(Side.CLIENT)
// This renders BOTH WIRES AND TUNNELS
public void renderPart(Vec2 pos, ICircuit parent, double x, double y, CircuitPartRenderer.EnumRenderType type) {
int color = this.getColor(pos, parent);
boolean tun = this instanceof PartTunnel;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, find a OOP way of doing this, instanceof is an antipattern

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Properly using OOP is hell. I mean designing a proper class hierarchy.

I know that this check is actually this.canDrawAsStraightLine(), plus PartTunnel should render background before calling super.renderPart.

Oh wait! Call super is antipattern too! So this should actually be a renderWireLike method (probably static) that receives allowStraightLine flag among other arguments, and it should be called by both wire and tunnel renderers.

And what about if (selectedPart.getPart() instanceof PartWire)? Surely it should be a virtual allowDragPlacement() method of CircuitPart class.
Oh, and maybe PartWire should decide how it is drawn during placement and not CadGui? I mean CircuitPart.renderDuringPlacement(from, to, ...) and CircuitPart.doPlace(from, to, ...) methods. Overriding these methods will allow, for example, to rotate gates during placement, or set delay of timers proportional to distance, or to place pair of connected tunnels.
Or even better, these "placers" should not be implicitly tied to gates they place. This will allow to implement "connect 2 wires to turn them into tunnels" thing cleanly.

In general, hardcoding is bad, but avoiding hardcoding takes time and decreases runtime performance, while absolutely no hardcoding means infinite code. I do not mean that I reject your suggestion to "find an OOP way of doing this", just that I see no practical reason to not simply use part isinstance PartTunnel unless this check has obvious potential of becoming something more complex.

The code base has a ton of cleanup potential, and would likely benefit from more comments and documentation. But I guess no one has time to refactor it.
Also, I have been (un)lucky to experience how perfectionism can be a really bad thing in terms of time consumption and project completion, so my opinion is that result is what matters. And the result is that the code works and is more or less readable.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There have been a lot of requests to enable wire-like dragging for other parts as well so I think this should be something that's not bound to PartWire, specifically.

Otherwise I pretty much agree with you, and yes, the parts should handle rendering inside the GUI as well, instead of that one giant method I use to do that, mostly replicating already existing render code. But getting started on that would take a lot of time, and I still have a port to 1.8.9 somewhere in the back of my head so I don't want to put too much refactoring effort into this. Its a lot simpler to point these things out in PRs, doesn't mean I'm any better at it myself ;)

Tessellator tes = Tessellator.instance;

if (tun) {
RenderUtils.applyColorIRGBA(tes, Config.colorGreen);
CircuitPartRenderer.addQuad(x, y, 16, 4*16, 16, 16);
}

if (type == CircuitPartRenderer.EnumRenderType.GUI) {
switch (color) {
case 1:
Expand Down Expand Up @@ -66,9 +73,9 @@ public void renderPart(Vec2 pos, ICircuit parent, double x, double y, CircuitPar
int ty = type == CircuitPartRenderer.EnumRenderType.WORLD_16x ? 3 * 16 : 0;

int con = CircuitPartRenderer.checkConnections(pos, parent, this);
if ((con & 12) == 12 && (con & ~12) == 0)
if ((con & 12) == 12 && (con & ~12) == 0 && !tun)
CircuitPartRenderer.addQuad(x, y, 6 * 16, ty, 16, 16);
else if ((con & 3) == 3 && (con & ~3) == 0)
else if ((con & 3) == 3 && (con & ~3) == 0 && !tun)
CircuitPartRenderer.addQuad(x, y, 5 * 16, ty, 16, 16);
else {
if ((con & 8) > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public void process(EntityPlayer player, Side side) {
CircuitData data = te.getCircuitData();

data.getProperties().setCon(con);
data.clearIOAndSetupIO();
data.FixIO();

if (input && side == Side.SERVER) {
te.getCircuitData().updateInput();
data.updateInput();
CommonProxy.networkWrapper.sendToAllAround(this, new TargetPoint(te.getWorldObj().getWorldInfo()
.getVanillaDimension(), xCoord, yCoord, zCoord, 8));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public int isProvidingStrongPower(IBlockAccess world, int x, int y, int z, int s
if ((side & 6) == (socket.getSide() & 6))
return 0;
int rot = socket.getSideRel(side);
if (!socket.getConnectionTypeAtSide(side).isRedstone())
if (!socket.getConnectionTypeAtSide(rot).isRedstone())
return 0;

return socket.getRedstoneOutput(rot);
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/integratedcircuits/lang/PT_BR.lang
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ tile.integratedcircuits.assembler.name=Montadora
# parts
part.integratedcircuits.iobit.name=IOBit
part.integratedcircuits.torch.name=Tocha
part.integratedcircuits.tunnel.name=Tunel
part.integratedcircuits.tunnel.0.name=Tunel
part.integratedcircuits.tunnel.1.name=Tunel vermelho
part.integratedcircuits.tunnel.2.name=Tunel laranja
part.integratedcircuits.wire.0.name=Fio
part.integratedcircuits.wire.1.name=Fio vermelho
part.integratedcircuits.wire.2.name=Fio laranja
Expand Down
Loading