Advertisement
MrMusAddict

Factorio Graph Theory

Apr 19th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.46 KB | None | 0 0
  1. ArrayList<Node> nodes;
  2.  
  3. Node selected;
  4. Node moving;
  5.  
  6. void setup() {
  7.   size(1200, 1200);
  8.   ellipseMode(CENTER);
  9.   textAlign(CENTER, CENTER);
  10.   textSize(20);
  11.  
  12.   nodes = new ArrayList<Node>();
  13. }
  14.  
  15. void draw() {
  16.   background(51);
  17.  
  18.   if (moving != null) {
  19.     moving.pos = new PVector(mouseX, mouseY);
  20.   }
  21.  
  22. for (Node n : nodes) {
  23.     n.showLines();
  24.   }
  25.  
  26.   for (Node n : nodes) {
  27.     n.showOutput();
  28.   }
  29.  
  30.   for (Node n : nodes) {
  31.     n.showSelf();
  32.   }
  33. }
  34.  
  35. void mousePressed() {
  36.   if (mouseButton == RIGHT) {
  37.     if (selected == null) {
  38.       for (Node n : nodes) {
  39.         if (n.isInside(mouseX, mouseY)) {
  40.           selected = n;
  41.         }
  42.       }
  43.     } else {
  44.       boolean noneFound = true;
  45.       for (Node n : nodes) {
  46.         if (n.isInside(mouseX, mouseY) && selected != n) {
  47.           n.setParent(selected);
  48.           n.setColor();
  49.           selected.setColor();
  50.           noneFound = true;
  51.         }
  52.       }
  53.  
  54.       if (noneFound) {
  55.         selected = null;
  56.       }
  57.     }
  58.   }
  59.  
  60.   if (mouseButton == LEFT) {
  61.     for (Node n : nodes) {
  62.       if (n.isInside(mouseX, mouseY)) {
  63.         moving = n;
  64.       }
  65.     }
  66.   }
  67. }
  68.  
  69. void mouseReleased() {
  70.   if (mouseButton == LEFT) {
  71.     moving = null;
  72.   }
  73. }
  74.  
  75. void keyPressed() {
  76.   if (key == ' ') {
  77.     for (Node n : nodes) {
  78.       n.pulse();
  79.     }
  80.  
  81.     for (Node n : nodes) {
  82.       n.settle();
  83.     }
  84.   }
  85.  
  86.   if (key == 'i') {
  87.     nodes.add(new Node(100, mouseX, mouseY));
  88.   }
  89.  
  90.   if (key == 'n') {
  91.     nodes.add(new Node(0, mouseX, mouseY));
  92.   }
  93.  
  94.   if (key == 'c') {
  95.     nodes.clear();
  96.   }
  97.  
  98.   if (key == 'a') {
  99.     for (Node n : nodes) {
  100.       n.resetAmount();
  101.     }
  102.   }
  103.  
  104.   if (key == 'r') {
  105.     if (selected == null) {
  106.       for (Node n : nodes) {
  107.         n.resetConnections();
  108.       }
  109.     } else {
  110.       selected.resetMyConnections();
  111.     }
  112.   }
  113.  
  114.   if (key == 'd') {
  115.     if (selected != null) {
  116.         selected.resetMyConnections();
  117.        
  118.         int temp = 0;
  119.        
  120.         for(int i = 0; i < nodes.size(); i++){
  121.           if(selected == nodes.get(i)){
  122.             temp = i;
  123.           }
  124.         }
  125.        
  126.         selected = null;
  127.         nodes.remove(temp);
  128.     }
  129.   }
  130. }
  131.  
  132.  
  133. class Node {
  134.  
  135.   PVector pos;
  136.   color col;
  137.  
  138.   Node parent1;
  139.   Node parent2;
  140.  
  141.   Node child1;
  142.   Node child2;
  143.  
  144.   float initialAmount;
  145.   float tempAmount;
  146.   float trueAmount;
  147.  
  148.   Node(float a, float x, float y) {
  149.     trueAmount = a;
  150.     initialAmount = a;
  151.     tempAmount = 0;
  152.  
  153.     pos = new PVector(x, y);
  154.     col = color(255);
  155.   }
  156.  
  157.   void pulse() {
  158.     tempAmount = 0;
  159.  
  160.     if (parent1 != null) {
  161.       if (parent1.child2 == null) {
  162.         tempAmount += parent1.trueAmount;
  163.       } else {
  164.         tempAmount += parent1.trueAmount / 2.0;
  165.       }
  166.     }
  167.  
  168.     if (parent2 != null) {
  169.       if (parent2.child2 == null) {
  170.         tempAmount += parent2.trueAmount;
  171.       } else {
  172.         tempAmount += parent2.trueAmount / 2.0;
  173.       }
  174.     }
  175.   }
  176.  
  177.   void settle() {
  178.     if (child1 == null && child2 == null) {
  179.       trueAmount += tempAmount;
  180.     } else {
  181.       trueAmount = tempAmount;
  182.     }
  183.     tempAmount = 0;
  184.   }
  185.  
  186.   void setParent(Node c) {
  187.     if (parent1 == null) {
  188.       parent1 = c;
  189.       c.setChild(this);
  190.     } else if ( parent2 == null && parent1 != c) {
  191.       parent2 = c;
  192.       c.setChild(this);
  193.     } else if (parent1 != c && parent2 != c) {
  194.       parent1 = parent2;
  195.       parent2 = c;
  196.       c.setChild(this);
  197.     }
  198.   }
  199.  
  200.   void setChild(Node c) {
  201.     if (child1 == null) {
  202.       child1 = c;
  203.     } else if ( child2 == null) {
  204.       child2 = c;
  205.     } else {
  206.       child1 = child2;
  207.       child2 = c;
  208.     }
  209.   }
  210.  
  211.   void showSelf() {
  212.     if (selected == this) {
  213.       fill(0, 0, 255);
  214.     } else {
  215.       fill(col);
  216.     }
  217.     stroke(0);
  218.     circle(pos.x, pos.y, 50);
  219.     fill(0);
  220.     text((int)trueAmount, pos.x, pos.y);
  221.   }
  222.  
  223.   void showLines() {
  224.     if (parent1 != null) {
  225.       strokeWeight(2);
  226.       stroke(255);
  227.       line(parent1.pos.x, parent1.pos.y, pos.x, pos.y);
  228.     }
  229.     if (parent2 != null) {
  230.       strokeWeight(2);
  231.       stroke(255);
  232.       line(parent2.pos.x, parent2.pos.y, pos.x, pos.y);
  233.     }
  234.   }
  235.  
  236.   void showOutput(){
  237.     if (parent1 != null) {
  238.       PVector edge = PVector.lerp(parent1.pos, pos, 25/pos.dist(parent1.pos));
  239.       strokeWeight(2);
  240.       stroke(255);
  241.       fill(127, 0, 255);
  242.       circle(edge.x, edge.y, 25);
  243.     }
  244.     if (parent2 != null) {
  245.       PVector edge = PVector.lerp(parent2.pos, pos, 25/pos.dist(parent2.pos));
  246.       strokeWeight(2);
  247.       stroke(255);
  248.       fill(127, 0, 255);
  249.       circle(edge.x, edge.y, 25);
  250.     }
  251.   }
  252.  
  253.   boolean isInside(PVector p) {
  254.     return dist(p.x, p.y, pos.x, pos.y) < 25;
  255.   }
  256.  
  257.   boolean isInside(float x, float y) {
  258.     return dist(x, y, pos.x, pos.y) < 25;
  259.   }
  260.  
  261.   void resetAmount() {
  262.     trueAmount = initialAmount;
  263.     tempAmount = 0;
  264.   }
  265.  
  266.   void resetConnections() {
  267.     parent1 = null;
  268.     parent2 = null;
  269.     child1 = null;
  270.     child2 = null;
  271.     setColor();
  272.   }
  273.  
  274.   void resetMyConnections() {
  275.     if (parent1 != null) {
  276.       if (parent1.child1 == this) {
  277.         parent1.child1 = parent1.child2;
  278.       }
  279.       parent1.child2 = null;
  280.       parent1.setColor();
  281.     }
  282.     if (parent2 != null) {
  283.       if (parent2.child1 == this) {
  284.         parent2.child1 = parent2.child2;
  285.       }
  286.       parent2.child2 = null;
  287.       parent2.setColor();
  288.     }
  289.    
  290.     if (child1 != null) {
  291.       if (child1.parent1 == this) {
  292.         child1.parent1 = child1.parent2;
  293.       }
  294.       child1.parent2 = null;
  295.       child1.setColor();
  296.     }
  297.    
  298.     if (child2 != null) {
  299.       if (child2.parent1 == this) {
  300.         child2.parent1 = child2.parent2;
  301.       }
  302.       child2.parent2 = null;
  303.       child2.setColor();
  304.     }
  305.    
  306.     parent1 = null;
  307.     parent2 = null;
  308.     child1 = null;
  309.     child2 = null;
  310.    
  311.     setColor();
  312.    
  313.     println(parent1);
  314.     println(parent2);
  315.     println(child1);
  316.     println(child2);
  317.   }
  318.  
  319.   void setColor() {
  320.     if (parent1 == null && parent2 == null && (child1 != null || child2 != null)) {
  321.       col = color(70, 160, 255);
  322.     }
  323.     if ((parent1 != null || parent2 != null) && (child1 != null || child2 != null)) {
  324.       col = color(255, 255, 70);
  325.     }
  326.     if ((parent1 != null || parent2 != null) && child1 == null && child2 == null) {
  327.       col = color(255, 70, 70);
  328.     }
  329.     if (parent1 == null && parent2 == null && child1 == null && child2 == null) {
  330.       col = color(255);
  331.     }
  332.   }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement