immibis

Untitled

Aug 24th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.22 KB | None | 0 0
  1. package mods.immibis.redlogic.gates.compiled;
  2.  
  3. import static mods.immibis.redlogic.Utils.*;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import java.util.List;
  9.  
  10. import mods.immibis.redlogic.api.chips.compiler.ICompilableBlock;
  11. import mods.immibis.redlogic.api.chips.compiler.ICompilableExpression;
  12. import mods.immibis.redlogic.api.chips.compiler.ICompileContext;
  13. import mods.immibis.redlogic.api.chips.compiler.IUnconnectedExpression;
  14. import mods.immibis.redlogic.api.chips.scanner.IScanProcess;
  15. import mods.immibis.redlogic.api.chips.scanner.IScannedInput;
  16. import mods.immibis.redlogic.api.chips.scanner.IScannedNode;
  17. import mods.immibis.redlogic.api.chips.scanner.IScannedOutput;
  18. import mods.immibis.redlogic.gates.GateLogic;
  19. import mods.immibis.redlogic.gates.GateRendering;
  20. import mods.immibis.redlogic.gates.GateLogic.Stateless;
  21. import net.minecraft.nbt.NBTTagCompound;
  22.  
  23. import org.objectweb.asm.Opcodes;
  24.  
  25. public class GateAND {
  26.     public static class Compiler extends GateCompiler {
  27.  
  28.         @Override
  29.         public Collection<ICompilableBlock> toCompilableBlocks(IScanProcess process, IScannedNode[] nodes, NBTTagCompound gateTag, int gateSettings) {
  30.             // gateSettings 1: left input ignored
  31.             // gateSettings 2: back input ignored
  32.             // gateSettings 4: right input ignored
  33.            
  34.             List<IScannedNode> inNodes = new ArrayList<IScannedNode>(3);
  35.             if((gateSettings & 1) == 0) inNodes.add(nodes[LEFT]);
  36.             if((gateSettings & 2) == 0) inNodes.add(nodes[BACK]);
  37.             if((gateSettings & 4) == 0) inNodes.add(nodes[RIGHT]);
  38.            
  39.             List<IScannedInput> inputs = new ArrayList<IScannedInput>(inNodes.size());
  40.            
  41.             for(IScannedNode n : inNodes) {
  42.                 IScannedInput i = process.createInput();
  43.                 n.getWire(0).addInput(i);
  44.                 inputs.add(i);
  45.             }
  46.            
  47.             IScannedOutput output = process.createOutput();
  48.             nodes[FRONT].getWire(0).addOutput(output);
  49.            
  50.             IScannedInput[] inputsArray = inputs.toArray(new IScannedInput[inputs.size()]);
  51.             IScannedOutput[] outputsArray = new IScannedOutput[] {output};
  52.            
  53.             return Collections.<ICompilableBlock>singleton(new AndBlock(inputsArray, outputsArray));
  54.         }
  55.        
  56.         private static class AndBlock implements ICompilableBlock {
  57.             private IScannedOutput[] outputsArray;
  58.             private IScannedInput[] inputsArray;
  59.            
  60.             AndBlock(IScannedInput[] inputs, IScannedOutput[] outputs) {
  61.                 this.outputsArray = outputs;
  62.                 this.inputsArray = inputs;
  63.             }
  64.            
  65.             @Override
  66.             public IScannedOutput[] getOutputs() {
  67.                 return outputsArray;
  68.             }
  69.            
  70.             @Override
  71.             public IScannedInput[] getInputs() {
  72.                 return inputsArray;
  73.             }
  74.            
  75.             @Override
  76.             public ICompilableExpression[] compile(ICompileContext ctx, ICompilableExpression[] inputs) {
  77.                 boolean anyUnconnected = false;
  78.                 for(ICompilableExpression inExpr : inputs)
  79.                     if(inExpr instanceof IUnconnectedExpression)
  80.                         anyUnconnected = true;
  81.                
  82.                 if(anyUnconnected) {
  83.                     // surely nobody is dumb enough to wire an AND gate to a constant 0
  84.                     return new ICompilableExpression[] {new ZeroExpr()};
  85.                 }
  86.                
  87.                 if(inputs.length == 0) {
  88.                     // or to disable all the inputs
  89.                     return new ICompilableExpression[] {new OneExpr()};
  90.                 }
  91.                
  92.                 if(inputs.length == 1) {
  93.                     // this is silly too
  94.                     return new ICompilableExpression[] {inputs[0]};
  95.                 }
  96.                
  97.                 return new ICompilableExpression[] {new AndExpr(inputs)};
  98.             }
  99.         }
  100.        
  101.         private static class ZeroExpr implements ICompilableExpression {
  102.             @Override
  103.             public boolean alwaysInline() {
  104.                 return true;
  105.             }
  106.            
  107.             @Override
  108.             public void compile(ICompileContext ctx) {
  109.                 ctx.getCodeVisitor().visitInsn(Opcodes.ICONST_0);
  110.             }
  111.         }
  112.        
  113.         private static class OneExpr implements ICompilableExpression {
  114.             @Override
  115.             public boolean alwaysInline() {
  116.                 return true;
  117.             }
  118.            
  119.             @Override
  120.             public void compile(ICompileContext ctx) {
  121.                 ctx.getCodeVisitor().visitInsn(Opcodes.ICONST_1);
  122.             }
  123.         }
  124.        
  125.         private static class AndExpr implements ICompilableExpression {
  126.             private ICompilableExpression[] inputs;
  127.            
  128.             AndExpr(ICompilableExpression[] inputs) {
  129.                 assert inputs.length > 0;
  130.                
  131.                 this.inputs = inputs;
  132.             }
  133.            
  134.             @Override
  135.             public boolean alwaysInline() {
  136.                 return false;
  137.             }
  138.            
  139.             @Override
  140.             public void compile(ICompileContext ctx) {
  141.                 inputs[0].compile(ctx);
  142.                 for(int k = 1; k < inputs.length; k++) {
  143.                     inputs[k].compile(ctx);
  144.                     ctx.getCodeVisitor().visitInsn(Opcodes.IAND);
  145.                 }
  146.             }
  147.         }
  148.     }
  149.    
  150.     public static class Logic extends GateLogic implements Stateless {
  151.         // gateSettings 1: left input ignored
  152.         // gateSettings 2: back input ignored
  153.         // gateSettings 4: right input ignored
  154.         @Override
  155.         public void update(short[] inputs, short[] outputs, int gateSettings) {
  156.             boolean left = inputs[LEFT] != 0 || (gateSettings & 1) != 0;
  157.             boolean back = inputs[BACK] != 0 || (gateSettings & 2) != 0;
  158.             boolean right = inputs[RIGHT] != 0 || (gateSettings & 4) != 0;
  159.             outputs[FRONT] = (short)(left && back && right ? 255 : 0);
  160.         }
  161.         @Override
  162.         public int getRenderState(short[] inputs, short[] outputs, int gateSettings) {
  163.             return
  164.                 (outputs[FRONT] != 0 ? 1 : 0)
  165.                 | (inputs[BACK] != 0 ? 2 : 0)
  166.                 | (inputs[LEFT] != 0 || outputs[LEFT] != 0 ? 4 : 0)
  167.                 | (inputs[RIGHT] != 0 || outputs[RIGHT] != 0 ? 8 : 0)
  168.                 | (gateSettings << 4);
  169.         }
  170.        
  171.         @Override
  172.         public boolean getInputID(int side, int gateSettings) {
  173.             return (side == LEFT && (gateSettings & 1) == 0)
  174.                 || (side == BACK && (gateSettings & 2) == 0)
  175.                 || (side == RIGHT && (gateSettings & 4) == 0);
  176.         }
  177.         @Override
  178.         public boolean getOutputID(int side, int gateSettings) {
  179.             return side == FRONT;
  180.         }
  181.        
  182.         @Override
  183.         public int configure(int gateSettings) {
  184.             return (gateSettings + 1) & 7;
  185.         }
  186.     }
  187.    
  188.     public static class Rendering extends GateRendering implements GateRendering.Stateless {
  189.         {
  190.             segmentTex = new String[] {"and-base", "and-ovl-out", "and-ovl-back", "and-ovl-right", "and-ovl-left"};
  191.             segmentCol = new int[] {0xFFFFFF, 0, 0, 0, 0};
  192.             torchX = new float[] {8.5f, 4.5f, 8.5f, 12.5f};
  193.             torchY = new float[] {2.5f, 6.5f, 6.5f, 6.5f};
  194.             torchState = new boolean[] {false, true, true, true};
  195.         }
  196.         @Override
  197.         public void set(int renderState) {
  198.             boolean out_on = (renderState & 1) != 0;
  199.             boolean back_on = (renderState & 2) != 0;
  200.             boolean left_on = (renderState & 4) != 0;
  201.             boolean right_on = (renderState & 8) != 0;
  202.             boolean left_disabled = (renderState & 16) != 0;
  203.             boolean back_disabled = (renderState & 32) != 0;
  204.             boolean right_disabled = (renderState & 64) != 0;
  205.             segmentCol[1] = out_on ? OFF : ON;
  206.             segmentCol[2] = back_disabled ? DISABLED : back_on ? ON : OFF;
  207.             segmentCol[3] = right_disabled ? DISABLED : right_on ? ON : OFF;
  208.             segmentCol[4] = left_disabled ? DISABLED : left_on ? ON : OFF;
  209.             torchState[0] = out_on;
  210.             torchState[1] = !left_on && !left_disabled;
  211.             torchState[2] = !back_on && !back_disabled;
  212.             torchState[3] = !right_on && !right_disabled;
  213.         }
  214.         @Override
  215.         public void setItemRender() {
  216.             segmentCol[1] = ON;
  217.             segmentCol[2] = OFF;
  218.             segmentCol[3] = OFF;
  219.             segmentCol[4] = OFF;
  220.             torchState[0] = false;
  221.             torchState[1] = true;
  222.             torchState[2] = true;
  223.             torchState[3] = true;
  224.         }
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment