فهرست منبع

Add OpAmp logic

Toby Chui 2 هفته پیش
والد
کامیت
b5faead890

+ 23 - 1
src/web/Blocky/README.md

@@ -21,7 +21,7 @@ its folder.
 | `js/mesher.js` | Section meshing (smooth light + ambient occlusion) and item models. |
 | `js/mesher.js` | Section meshing (smooth light + ambient occlusion) and item models. |
 | `js/render.js` | three.js scene, terrain shader, sky, clouds, rain, lightning, selection, particles, held item. |
 | `js/render.js` | three.js scene, terrain shader, sky, clouds, rain, lightning, selection, particles, held item. |
 | `js/realistic.js` | Realistic graphics pipeline: shadow map, sea reflection, terrain lighting chunk, HDR post passes. |
 | `js/realistic.js` | Realistic graphics pipeline: shadow map, sea reflection, terrain lighting chunk, HDR post passes. |
-| `js/power.js` | Spark circuits: power sources, wire levels, torches, lamps, pistons, rail shapes. |
+| `js/power.js` | Spark circuits: power sources, wire levels, torches, lamps, pistons, OpAmps, rail shapes. |
 | `js/carts.js` | Minecarts (plain, chest, furnace) following rails. |
 | `js/carts.js` | Minecarts (plain, chest, furnace) following rails. |
 | `js/boats.js` | Boats: floating, rowing, steering, riding. |
 | `js/boats.js` | Boats: floating, rowing, steering, riding. |
 | `js/machines.js` | Feeders (moving items between containers, carts and the ground) and dispensers. |
 | `js/machines.js` | Feeders (moving items between containers, carts and the ground) and dispensers. |
@@ -318,6 +318,28 @@ front; a wire beside it neither branches into its side nor runs on into it
 (`wire` in `mesher.js`). Changes on their way wait in `world.retimerDue`, so a
 (`wire` in `mesher.js`). Changes on their way wait in `world.retimerDue`, so a
 pulse shorter than the delay is stretched to it.
 pulse shorter than the delay is stretched to it.
 
 
+OpAmps (three spark torches round an iron ingot over three stone) are the
+one part with an output level of its own. `B.OPAMP`, one id, placed like a
+retimer: meta bits 0-1 facing (`HDIR`), bit 2 subtract mode, bits 3-6 output
+level, which the top tile and the input pins light up from. Inputs, read
+in `opampTick` two ticks after anything near changes:
+- back: a storage block (chest, dispenser, feeder, furnace or a chest cart
+  on a rail there) gives its fill level (`Machines.fillLevel` through
+  `world.storageLevel`: 0 empty, 1 with anything in it, 15 with every slot
+  at its stack limit). Otherwise wire gives its level, a source or retimer
+  15, a powered solid block the level powering it (`blockLevel`).
+- sides: the stronger of the two; wire, sources, retimers and OpAmps
+  pointing in, but not through solid blocks.
+
+Right click switches the mode. Compare (front pin low) sends the back level
+on unless the side level is higher, then 0; subtract (front pin up and lit)
+sends back minus side. With nothing at the sides both just pass the back
+level on. The output goes to the block in front only: wire takes it as its
+level, a solid block passes that level on (not 15, so wire touching it gets
+it too; `opampsInto`), and the binary parts count anything above 0.
+Inventories change in too many ways to report, so an OpAmp with storage or
+a rail behind it looks again every `OPAMP_POLL` ticks by rescheduling itself.
+
 ## Rails and carts
 ## Rails and carts
 
 
 Rails shape themselves (`railPlaced` / `shapeRail`): straight, rising toward
 Rails shape themselves (`railPlaced` / `shapeRail`): straight, rising toward

+ 1 - 0
src/web/Blocky/index.html

@@ -213,6 +213,7 @@
                 <div><b>Left click</b> an animal to hit it; hold <b>Right click</b> with a bow to draw, let go to shoot</div>
                 <div><b>Left click</b> an animal to hit it; hold <b>Right click</b> with a bow to draw, let go to shoot</div>
                 <div><b>Right click</b> levers and buttons, or step on a pressure plate, to power spark wire, lamps, pistons and doors</div>
                 <div><b>Right click</b> levers and buttons, or step on a pressure plate, to power spark wire, lamps, pistons and doors</div>
                 <div><b>Right click</b> a retimer to change its delay; it passes the signal on in the direction you placed it</div>
                 <div><b>Right click</b> a retimer to change its delay; it passes the signal on in the direction you placed it</div>
+                <div>An <b>OpAmp</b> sends on the signal behind it, or how full the storage behind it is; a stronger signal at a side stops it. <b>Right click</b> to subtract the sides instead</div>
                 <div><b>Right click</b> a sign to write on it (4 lines); Shift + right click to place a block against it</div>
                 <div><b>Right click</b> a sign to write on it (4 lines); Shift + right click to place a block against it</div>
                 <div>Power a rail where three tracks meet (lever, button or spark wire) to switch it to the other track</div>
                 <div>Power a rail where three tracks meet (lever, button or spark wire) to switch it to the other track</div>
                 <div><b>Right click</b> a cart to ride it (Shift to get out), a chest cart to open it, a furnace cart with fuel to drive it</div>
                 <div><b>Right click</b> a cart to ride it (Shift to get out), a chest cart to open it, a furnace cart with fuel to drive it</div>

+ 11 - 2
src/web/Blocky/js/blocks.js

@@ -26,7 +26,8 @@ export const B = {
     PISTON_HEAD: 83, RAIL: 84, POWERED_RAIL: 85, SPARK_BLOCK: 86, PLATE_WOOD: 87, PLATE_STONE: 88,
     PISTON_HEAD: 83, RAIL: 84, POWERED_RAIL: 85, SPARK_BLOCK: 86, PLATE_WOOD: 87, PLATE_STONE: 88,
     RETIMER: 89, RETIMER_ON: 90, SIGN: 91, WALL_SIGN: 92, IRON_BARS: 93,
     RETIMER: 89, RETIMER_ON: 90, SIGN: 91, WALL_SIGN: 92, IRON_BARS: 93,
     PLANK_STAIRS: 94, COBBLE_STAIRS: 95, STONE_STAIRS: 96, STONE_BRICK_STAIRS: 97, BRICK_STAIRS: 98,
     PLANK_STAIRS: 94, COBBLE_STAIRS: 95, STONE_STAIRS: 96, STONE_BRICK_STAIRS: 97, BRICK_STAIRS: 98,
-    SANDSTONE_STAIRS: 99, TRAPDOOR: 100, DETECTOR_RAIL: 101, FEEDER: 102, DISPENSER: 103
+    SANDSTONE_STAIRS: 99, TRAPDOOR: 100, DETECTOR_RAIL: 101, FEEDER: 102, DISPENSER: 103,
+    OPAMP: 104
 };
 };
 
 
 //Wool and dye colours: key, name, colour
 //Wool and dye colours: key, name, colour
@@ -231,6 +232,10 @@ def(B.PLATE_STONE, "Stone Pressure Plate", { tex: "stone", shape: "plate", solid
 const RETIMER_DEF = { shape: "retimer", solid: false, opaque: false, lightOpacity: 0, hardness: 0, sound: "stone", support: "solid", interact: "retimer" };
 const RETIMER_DEF = { shape: "retimer", solid: false, opaque: false, lightOpacity: 0, hardness: 0, sound: "stone", support: "solid", interact: "retimer" };
 def(B.RETIMER, "Retimer", Object.assign({ tex: { top: "retimer_top", side: "slab_side", bottom: "slab_top" } }, RETIMER_DEF));
 def(B.RETIMER, "Retimer", Object.assign({ tex: { top: "retimer_top", side: "slab_side", bottom: "slab_top" } }, RETIMER_DEF));
 def(B.RETIMER_ON, "Retimer", Object.assign({ tex: { top: "retimer_top_on", side: "slab_side", bottom: "slab_top" }, noItem: true, drops: () => [[B.RETIMER, 1]] }, RETIMER_DEF));
 def(B.RETIMER_ON, "Retimer", Object.assign({ tex: { top: "retimer_top_on", side: "slab_side", bottom: "slab_top" }, noItem: true, drops: () => [[B.RETIMER, 1]] }, RETIMER_DEF));
+//OpAmp: sends out the level at its back (a signal, or how full the storage
+//there is) unless a side input beats it, or minus the sides in subtract mode.
+//Meta: bits 0-1 facing (HDIR), bit 2 subtract mode, bits 3-6 output level.
+def(B.OPAMP, "OpAmp", Object.assign({}, RETIMER_DEF, { tex: { top: "opamp_top", side: "slab_side", bottom: "slab_top" }, interact: "opamp" }));
 def(B.FIRE, "Fire", { tex: "fire", shape: "fire", solid: false, opaque: false, lightOpacity: 0, light: 15, hardness: 0, replaceable: true, noItem: true, sound: "fire", drops: () => [] });
 def(B.FIRE, "Fire", { tex: "fire", shape: "fire", solid: false, opaque: false, lightOpacity: 0, light: 15, hardness: 0, replaceable: true, noItem: true, sound: "fire", drops: () => [] });
 def(B.LAVA, "Lava", { tex: "lava", shape: "liquid", solid: false, opaque: false, lightOpacity: 15, light: 15, hardness: -1, replaceable: true, noItem: true, targetable: false, sound: "lava" });
 def(B.LAVA, "Lava", { tex: "lava", shape: "liquid", solid: false, opaque: false, lightOpacity: 15, light: 15, hardness: -1, replaceable: true, noItem: true, targetable: false, sound: "lava" });
 def(B.OBSIDIAN, "Obsidian", { tex: "obsidian", hardness: 35, tool: "pickaxe", tier: 4 });
 def(B.OBSIDIAN, "Obsidian", { tex: "obsidian", hardness: 35, tool: "pickaxe", tier: 4 });
@@ -325,6 +330,9 @@ export function layerOfTile(name) {
 //Texture layer of a block face, taking meta into account
 //Texture layer of a block face, taking meta into account
 export function blockFaceLayer(id, face, meta) {
 export function blockFaceLayer(id, face, meta) {
     const fl = faceLayers[id];
     const fl = faceLayers[id];
+    if (id === B.OPAMP && face === 2 && (meta >> 3) & 15) {
+        return tileLayer.get("opamp_top_on");
+    }
     if (id === B.WHEAT) {
     if (id === B.WHEAT) {
         return tileLayer.get("wheat_" + Math.min(7, meta));
         return tileLayer.get("wheat_" + Math.min(7, meta));
     }
     }
@@ -671,6 +679,7 @@ shapeless(B.BUTTON, 1, [B.STONE], "Spark");
 shaped(B.PLATE_WOOD, 1, ["PP"], { P: B.PLANKS }, "Spark");
 shaped(B.PLATE_WOOD, 1, ["PP"], { P: B.PLANKS }, "Spark");
 shaped(B.PLATE_STONE, 1, ["SS"], { S: B.STONE }, "Spark");
 shaped(B.PLATE_STONE, 1, ["SS"], { S: B.STONE }, "Spark");
 shaped(B.RETIMER, 1, ["TDT", "SSS"], { T: B.SPARK_TORCH, D: I.SPARK_DUST, S: B.STONE }, "Spark");
 shaped(B.RETIMER, 1, ["TDT", "SSS"], { T: B.SPARK_TORCH, D: I.SPARK_DUST, S: B.STONE }, "Spark");
+shaped(B.OPAMP, 1, [".T.", "TIT", "SSS"], { T: B.SPARK_TORCH, I: I.IRON_INGOT, S: B.STONE }, "Spark");
 shaped(B.SPARK_LAMP, 1, [".D.", "DLD", ".D."], { D: I.SPARK_DUST, L: B.LAMP }, "Spark");
 shaped(B.SPARK_LAMP, 1, [".D.", "DLD", ".D."], { D: I.SPARK_DUST, L: B.LAMP }, "Spark");
 shaped(B.PISTON, 1, ["PPP", "CIC", "CDC"], { P: B.PLANKS, C: B.COBBLE, I: I.IRON_INGOT, D: I.SPARK_DUST }, "Spark");
 shaped(B.PISTON, 1, ["PPP", "CIC", "CDC"], { P: B.PLANKS, C: B.COBBLE, I: I.IRON_INGOT, D: I.SPARK_DUST }, "Spark");
 shapeless(B.STICKY_PISTON, 1, [B.PISTON, I.CLAY_BALL], "Spark");
 shapeless(B.STICKY_PISTON, 1, [B.PISTON, I.CLAY_BALL], "Spark");
@@ -834,7 +843,7 @@ export function creativeGroups() {
         tools.push(swordId(mi));
         tools.push(swordId(mi));
     }
     }
     tools.push(I.BOW, I.ARROW, I.BUCKET, I.WATER_BUCKET, I.LAVA_BUCKET, I.FLINT_AND_STEEL, I.BOAT, I.COMPASS);
     tools.push(I.BOW, I.ARROW, I.BUCKET, I.WATER_BUCKET, I.LAVA_BUCKET, I.FLINT_AND_STEEL, I.BOAT, I.COMPASS);
-    const spark = [B.SPARK_ORE, I.SPARK_DUST, B.SPARK_BLOCK, B.SPARK_TORCH, B.LEVER, B.BUTTON, B.PLATE_WOOD, B.PLATE_STONE, B.RETIMER, B.SPARK_LAMP,
+    const spark = [B.SPARK_ORE, I.SPARK_DUST, B.SPARK_BLOCK, B.SPARK_TORCH, B.LEVER, B.BUTTON, B.PLATE_WOOD, B.PLATE_STONE, B.RETIMER, B.OPAMP, B.SPARK_LAMP,
         B.PISTON, B.STICKY_PISTON, B.DISPENSER, B.FEEDER, B.RAIL, B.POWERED_RAIL, B.DETECTOR_RAIL, I.MINECART, I.CHEST_MINECART, I.FURNACE_MINECART];
         B.PISTON, B.STICKY_PISTON, B.DISPENSER, B.FEEDER, B.RAIL, B.POWERED_RAIL, B.DETECTOR_RAIL, I.MINECART, I.CHEST_MINECART, I.FURNACE_MINECART];
     const items = [I.STICK, I.COAL, I.IRON_INGOT, I.GOLD_INGOT, I.CRYSTAL, I.FLINT, I.CLAY_BALL, I.BRICK,
     const items = [I.STICK, I.COAL, I.IRON_INGOT, I.GOLD_INGOT, I.CRYSTAL, I.FLINT, I.CLAY_BALL, I.BRICK,
         I.WHEAT, I.BOWL, I.APPLE, I.BREAD, I.STEW, I.RAW_MEAT, I.COOKED_MEAT, I.RAW_MUTTON,
         I.WHEAT, I.BOWL, I.APPLE, I.BREAD, I.STEW, I.RAW_MEAT, I.COOKED_MEAT, I.RAW_MUTTON,

+ 6 - 1
src/web/Blocky/js/game.js

@@ -115,6 +115,7 @@ export class Game {
             },
             },
             onPistonPush: (cells, d) => this.pistonPush(cells, d),
             onPistonPush: (cells, d) => this.pistonPush(cells, d),
             onDispense: (x, y, z, face) => this.machines.dispense(x, y, z, face),
             onDispense: (x, y, z, face) => this.machines.dispense(x, y, z, face),
+            storageLevel: (x, y, z) => this.machines.fillLevel(x, y, z),
             onFizz: (x, y, z) => {
             onFizz: (x, y, z) => {
                 this.sound.play("fizz");
                 this.sound.play("fizz");
                 this.renderer.spawnParticles(x + 0.5, y + 1, z + 0.5, [200, 200, 200], 12, 0.4, 2);
                 this.renderer.spawnParticles(x + 0.5, y + 1, z + 0.5, [200, 200, 200], 12, 0.4, 2);
@@ -1349,7 +1350,7 @@ export class Game {
                 bid = B.WALL_SIGN;
                 bid = B.WALL_SIGN;
                 meta = face;
                 meta = face;
             }
             }
-        } else if (bid === B.RETIMER) {
+        } else if (bid === B.RETIMER || bid === B.OPAMP) {
             //Signal runs away from the player
             //Signal runs away from the player
             meta = facingFromYaw(p.yaw);
             meta = facingFromYaw(p.yaw);
         } else if (SHAPE[bid] === "rail") {
         } else if (SHAPE[bid] === "rail") {
@@ -1517,6 +1518,10 @@ export class Game {
             const steps = (((hit.meta >> 2) & 3) + 1) % 4;
             const steps = (((hit.meta >> 2) & 3) + 1) % 4;
             w.setBlock(hit.x, hit.y, hit.z, hit.id, (hit.meta & 3) | (steps << 2));
             w.setBlock(hit.x, hit.y, hit.z, hit.id, (hit.meta & 3) | (steps << 2));
             this.sound.play("click");
             this.sound.play("click");
+        } else if (kind === "opamp") {
+            //Compare or subtract
+            w.setBlock(hit.x, hit.y, hit.z, B.OPAMP, hit.meta ^ 4);
+            this.sound.play("click");
         } else if (kind === "gate") {
         } else if (kind === "gate") {
             w.setBlock(hit.x, hit.y, hit.z, B.FENCE_GATE, hit.meta ^ 4, false);
             w.setBlock(hit.x, hit.y, hit.z, B.FENCE_GATE, hit.meta ^ 4, false);
             this.sound.block("place", "wood");
             this.sound.block("place", "wood");

+ 17 - 0
src/web/Blocky/js/machines.js

@@ -116,6 +116,23 @@ export class Machines {
         return cart ? { kind: "cart", slots: cart.slots } : null;
         return cart ? { kind: "cart", slots: cart.slots } : null;
     }
     }
 
 
+    //How full the storage at (x, y, z) is, as a spark level: 0 empty, 1 with
+    //anything in it, 15 full (stacks at their limit in every slot). -1 when
+    //there is no storage there.
+    fillLevel(x, y, z) {
+        const to = this.containerAt(x, y, z);
+        if (!to) {
+            return -1;
+        }
+        let filled = 0;
+        for (const s of to.slots) {
+            if (s) {
+                filled += s.count / maxStack(s.id);
+            }
+        }
+        return filled > 0 ? 1 + Math.floor(filled / to.slots.length * 14) : 0;
+    }
+
     //One item out of the spout (face: 3 down, else a side)
     //One item out of the spout (face: 3 down, else a side)
     feedOut(te, x, y, z, face) {
     feedOut(te, x, y, z, face) {
         const d = FACE_DIR[face];
         const d = FACE_DIR[face];

+ 17 - 2
src/web/Blocky/js/mesher.js

@@ -296,7 +296,7 @@ export function meshSection(world, chunk, sy, opts) {
                 } else if (shape === "sign") {
                 } else if (shape === "sign") {
                     sign(solidBuf, p, x, y, z, id, meta);
                     sign(solidBuf, p, x, y, z, id, meta);
                 } else if (shape === "retimer") {
                 } else if (shape === "retimer") {
-                    retimer(solidBuf, p, x, y, z, id, meta);
+                    (id === B.OPAMP ? opamp : retimer)(solidBuf, p, x, y, z, id, meta);
                 } else if (shape === "plate") {
                 } else if (shape === "plate") {
                     boxFaces(solidBuf, p, x, y, z, id, meta, U, 0, U, 15 * U, meta & 1 ? U / 2 : U, 15 * U, (f, np) => f === 3 && pOpq[np] === 1);
                     boxFaces(solidBuf, p, x, y, z, id, meta, U, 0, U, 15 * U, meta & 1 ? U / 2 : U, 15 * U, (f, np) => f === 3 && pOpq[np] === 1);
                 } else if (shape === "piston") {
                 } else if (shape === "piston") {
@@ -679,7 +679,7 @@ function ironBars(buf, p, x, y, z, id, meta, links) {
     }
     }
 }
 }
 
 
-const WIRE_LINKS = new Set([B.SPARK_WIRE, B.LEVER, B.BUTTON, B.PLATE_WOOD, B.PLATE_STONE, B.RETIMER, B.RETIMER_ON, B.SPARK_TORCH, B.SPARK_TORCH_OFF, B.SPARK_LAMP,
+const WIRE_LINKS = new Set([B.SPARK_WIRE, B.LEVER, B.BUTTON, B.PLATE_WOOD, B.PLATE_STONE, B.RETIMER, B.RETIMER_ON, B.OPAMP, B.SPARK_TORCH, B.SPARK_TORCH_OFF, B.SPARK_LAMP,
     B.SPARK_LAMP_ON, B.PISTON, B.STICKY_PISTON, B.SPARK_BLOCK, B.POWERED_RAIL, B.DETECTOR_RAIL]);
     B.SPARK_LAMP_ON, B.PISTON, B.STICKY_PISTON, B.SPARK_BLOCK, B.POWERED_RAIL, B.DETECTOR_RAIL]);
 
 
 function flatQuad(buf, x0, z0, x1, z1, yy, layer, ls, lb, r, g, b) {
 function flatQuad(buf, x0, z0, x1, z1, yy, layer, ls, lb, r, g, b) {
@@ -751,6 +751,21 @@ function retimer(buf, p, x, y, z, id, meta) {
     }
     }
 }
 }
 
 
+//OpAmp: a thin slab with its symbol turned to the facing, a pin on each of
+//the two inputs (lit while it gives a signal) and one on the output, which
+//stands up and lights in subtract mode
+function opamp(buf, p, x, y, z, id, meta) {
+    const f = meta & 3, d = HDIR[f];
+    boxFaces(buf, p, x, y, z, id, meta, 0, 0, 0, 1, 2 * U, 1, (ff, np) => ff === 3 && pOpq[np] === 1, f);
+    const on = layerOfTile("retimer_pin_on"), off = layerOfTile("retimer_pin_off");
+    const sub = (meta & 4) !== 0;
+    const pins = [[-0.34, -0.17, (meta >> 3) & 15 ? on : off, 7], [-0.34, 0.17, (meta >> 3) & 15 ? on : off, 7], [0.34, 0, sub ? on : off, sub ? 7 : 4]];
+    for (const [along, across, layer, top] of pins) {
+        const cx = 0.5 + d[0] * along - d[1] * across, cz = 0.5 + d[1] * along + d[0] * across;
+        turnedBox(buf, p, x, y, z, [cx - U, 2 * U, cz - U, cx + U, top * U, cz + U], 2, () => layer);
+    }
+}
+
 //A box in sign space (see SIGN_BOARD) turned by angle a about the block's
 //A box in sign space (see SIGN_BOARD) turned by angle a about the block's
 //vertical axis, lit by the sign's own cell
 //vertical axis, lit by the sign's own cell
 //frontShade, when given, lights the two broad faces (along the box's local
 //frontShade, when given, lights the two broad faces (along the box's local

+ 92 - 12
src/web/Blocky/js/power.js

@@ -5,7 +5,9 @@
     blocks of spark), spark wire that carries a level 0-15 losing one per
     blocks of spark), spark wire that carries a level 0-15 losing one per
     block, and the parts that react: spark lamps, pistons, powered rails and
     block, and the parts that react: spark lamps, pistons, powered rails and
     torches (which switch off when the block they hang on is powered, so they
     torches (which switch off when the block they hang on is powered, so they
-    work as inverters). Also lays out rail shapes.
+    work as inverters). OpAmps give out a level of their own, read from the
+    signals around them or from how full a storage block is. Also lays out
+    rail shapes.
 
 
     Rules, kept simple:
     Rules, kept simple:
     - a source powers the wire, lamps, pistons and rails right next to it,
     - a source powers the wire, lamps, pistons and rails right next to it,
@@ -22,11 +24,11 @@ import { FACE_DIR } from "./shapes.js";
 //1 wire, 2 source, 4 reacts, 8 rail
 //1 wire, 2 source, 4 reacts, 8 rail
 export const POWER = new Uint8Array(256);
 export const POWER = new Uint8Array(256);
 POWER[B.SPARK_WIRE] = 1;
 POWER[B.SPARK_WIRE] = 1;
-for (const id of [B.LEVER, B.BUTTON, B.SPARK_TORCH, B.SPARK_BLOCK, B.PLATE_WOOD, B.PLATE_STONE, B.RETIMER_ON, B.DETECTOR_RAIL]) {
+for (const id of [B.LEVER, B.BUTTON, B.SPARK_TORCH, B.SPARK_BLOCK, B.PLATE_WOOD, B.PLATE_STONE, B.RETIMER_ON, B.DETECTOR_RAIL, B.OPAMP]) {
     POWER[id] |= 2;
     POWER[id] |= 2;
 }
 }
 for (const id of [B.SPARK_LAMP, B.SPARK_LAMP_ON, B.PISTON, B.STICKY_PISTON, B.SPARK_TORCH, B.SPARK_TORCH_OFF, B.POWERED_RAIL, B.BUTTON,
 for (const id of [B.SPARK_LAMP, B.SPARK_LAMP_ON, B.PISTON, B.STICKY_PISTON, B.SPARK_TORCH, B.SPARK_TORCH_OFF, B.POWERED_RAIL, B.BUTTON,
-    B.PLATE_WOOD, B.PLATE_STONE, B.DOOR, B.TRAPDOOR, B.RETIMER, B.RETIMER_ON, B.DETECTOR_RAIL, B.DISPENSER]) {
+    B.PLATE_WOOD, B.PLATE_STONE, B.DOOR, B.TRAPDOOR, B.RETIMER, B.RETIMER_ON, B.DETECTOR_RAIL, B.DISPENSER, B.OPAMP]) {
     POWER[id] |= 4;
     POWER[id] |= 4;
 }
 }
 //Plain rails react too: a junction is a switch (railSwitch)
 //Plain rails react too: a junction is a switch (railSwitch)
@@ -37,6 +39,10 @@ POWER[B.DETECTOR_RAIL] |= 8;
 const SIDES = [[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]];
 const SIDES = [[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]];
 const FLAT = [[1, 0], [-1, 0], [0, 1], [0, -1]];
 const FLAT = [[1, 0], [-1, 0], [0, 1], [0, -1]];
 
 
+//Ticks between two looks at the storage behind an OpAmp: its contents change
+//in too many ways (the player, feeders, furnaces, carts) to be told about it
+const OPAMP_POLL = 4;
+
 const same = (a, b) => a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
 const same = (a, b) => a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
 const isPlate = (id) => id === B.PLATE_WOOD || id === B.PLATE_STONE;
 const isPlate = (id) => id === B.PLATE_WOOD || id === B.PLATE_STONE;
 //Rails that cannot curve
 //Rails that cannot curve
@@ -55,6 +61,24 @@ function retimerInto(w, nx, ny, nz, t) {
     return w.getBlock(nx, ny, nz) === B.RETIMER_ON && same(retimerFront(w.getMeta(nx, ny, nz), nx, ny, nz), t);
     return w.getBlock(nx, ny, nz) === B.RETIMER_ON && same(retimerFront(w.getMeta(nx, ny, nz), nx, ny, nz), t);
 }
 }
 
 
+//The level an OpAmp at n sends into t (0 unless it is one, pointing at t)
+function opampInto(w, nx, ny, nz, t) {
+    if (w.getBlock(nx, ny, nz) !== B.OPAMP) {
+        return 0;
+    }
+    const m = w.getMeta(nx, ny, nz);
+    return same(retimerFront(m, nx, ny, nz), t) ? (m >> 3) & 15 : 0;
+}
+
+//The strongest OpAmp pointing into the solid block at (x, y, z)
+function opampsInto(w, x, y, z) {
+    let best = 0;
+    for (const [dx, dz] of FLAT) {
+        best = Math.max(best, opampInto(w, x + dx, y, z + dz, [x, y, z]));
+    }
+    return best;
+}
+
 //Block a lever, button or torch hangs on
 //Block a lever, button or torch hangs on
 export function attachedTo(meta, x, y, z) {
 export function attachedTo(meta, x, y, z) {
     const n = FACE_DIR[meta & 7] || FACE_DIR[2];
     const n = FACE_DIR[meta & 7] || FACE_DIR[2];
@@ -92,20 +116,24 @@ export function blockPoweredBySource(w, x, y, z) {
     return w.getBlock(x, y - 1, z) === B.SPARK_TORCH;
     return w.getBlock(x, y - 1, z) === B.SPARK_TORCH;
 }
 }
 
 
-//A solid block powered at all: by a source, or by powered wire touching it
-export function blockPowered(w, x, y, z) {
+//The level a solid block is powered at: 15 by a source, else that of the
+//OpAmp pointing into it or of the wire touching it
+function blockLevel(w, x, y, z) {
     if (blockPoweredBySource(w, x, y, z)) {
     if (blockPoweredBySource(w, x, y, z)) {
-        return true;
+        return 15;
     }
     }
+    let best = opampsInto(w, x, y, z);
     for (const [dx, dy, dz] of SIDES) {
     for (const [dx, dy, dz] of SIDES) {
-        if (dy === -1) {
-            continue;
-        }
-        if (w.getBlock(x + dx, y + dy, z + dz) === B.SPARK_WIRE && w.getMeta(x + dx, y + dy, z + dz) > 0) {
-            return true;
+        if (dy !== -1 && w.getBlock(x + dx, y + dy, z + dz) === B.SPARK_WIRE) {
+            best = Math.max(best, w.getMeta(x + dx, y + dy, z + dz));
         }
         }
     }
     }
-    return false;
+    return best;
+}
+
+//A solid block powered at all
+export function blockPowered(w, x, y, z) {
+    return blockLevel(w, x, y, z) > 0;
 }
 }
 
 
 //Does a lamp / piston / rail at (x, y, z) receive power?
 //Does a lamp / piston / rail at (x, y, z) receive power?
@@ -125,6 +153,12 @@ export function isPowered(w, x, y, z) {
             }
             }
             continue;
             continue;
         }
         }
+        if (id === B.OPAMP) {
+            if (opampInto(w, nx, ny, nz, [x, y, z]) > 0) {
+                return true;
+            }
+            continue;
+        }
         if (sourceOn(id, m)) {
         if (sourceOn(id, m)) {
             return true;
             return true;
         }
         }
@@ -150,6 +184,10 @@ function wireTarget(w, x, y, z) {
         if (isOpaqueAt(id, m) && blockPoweredBySource(w, nx, ny, nz)) {
         if (isOpaqueAt(id, m) && blockPoweredBySource(w, nx, ny, nz)) {
             return 15;
             return 15;
         }
         }
+        best = Math.max(best, opampInto(w, nx, ny, nz, [x, y, z]));
+        if (isOpaqueAt(id, m)) {
+            best = Math.max(best, opampsInto(w, nx, ny, nz));
+        }
     }
     }
     const coveredAbove = isOpaqueAt(w.getBlock(x, y + 1, z), w.getMeta(x, y + 1, z));
     const coveredAbove = isOpaqueAt(w.getBlock(x, y + 1, z), w.getMeta(x, y + 1, z));
     for (const [dx, dz] of FLAT) {
     for (const [dx, dz] of FLAT) {
@@ -620,12 +658,51 @@ function retimerInput(w, x, y, z, meta) {
     if (id === B.RETIMER_ON) {
     if (id === B.RETIMER_ON) {
         return retimerInto(w, bx, y, bz, [x, y, z]);
         return retimerInto(w, bx, y, bz, [x, y, z]);
     }
     }
+    if (id === B.OPAMP) {
+        return opampInto(w, bx, y, bz, [x, y, z]) > 0;
+    }
     if (sourceOn(id, m)) {
     if (sourceOn(id, m)) {
         return true;
         return true;
     }
     }
     return isOpaqueAt(id, m) && blockPowered(w, bx, y, bz);
     return isOpaqueAt(id, m) && blockPowered(w, bx, y, bz);
 }
 }
 
 
+//The level the block at n gives the OpAmp at t. solid: a powered solid block
+//counts too (the back input does, the sides only take signals directly).
+function opampInput(w, nx, ny, nz, t, solid) {
+    const id = w.getBlock(nx, ny, nz), m = w.getMeta(nx, ny, nz);
+    if (id === B.SPARK_WIRE) {
+        return m;
+    }
+    if (id === B.RETIMER_ON) {
+        return retimerInto(w, nx, ny, nz, t) ? 15 : 0;
+    }
+    if (id === B.OPAMP) {
+        return opampInto(w, nx, ny, nz, t);
+    }
+    if (sourceOn(id, m)) {
+        return 15;
+    }
+    return solid && isOpaqueAt(id, m) ? blockLevel(w, nx, ny, nz) : 0;
+}
+
+//One look at an OpAmp's inputs: sets its output and keeps watching a storage
+//block, or a rail a chest cart may come along, behind it
+function opampTick(w, x, y, z, meta) {
+    const d = HDIR[meta & 3], t = [x, y, z];
+    const bx = x - d[0], bz = z - d[1];
+    const stored = w.storageLevel ? w.storageLevel(bx, y, bz) : -1;
+    const back = stored >= 0 ? stored : opampInput(w, bx, y, bz, t, true);
+    const side = Math.max(opampInput(w, x + d[1], y, z - d[0], t, false), opampInput(w, x - d[1], y, z + d[0], t, false));
+    const level = meta & 4 ? Math.max(0, back - side) : back >= side ? back : 0;
+    if (level !== ((meta >> 3) & 15)) {
+        w.setBlock(x, y, z, B.OPAMP, (meta & 7) | (level << 3));
+    }
+    if (stored >= 0 || (POWER[w.getBlock(bx, y, bz)] & 8)) {
+        w.schedule(x, y, z, OPAMP_POLL);
+    }
+}
+
 export function powerTick(w, x, y, z, id, meta, sound) {
 export function powerTick(w, x, y, z, id, meta, sound) {
     switch (id) {
     switch (id) {
         case B.SPARK_WIRE:
         case B.SPARK_WIRE:
@@ -712,6 +789,9 @@ export function powerTick(w, x, y, z, id, meta, sound) {
             }
             }
             break;
             break;
         }
         }
+        case B.OPAMP:
+            opampTick(w, x, y, z, meta);
+            break;
         case B.DOOR: {
         case B.DOOR: {
             //A door opens when power arrives and shuts when it goes; in
             //A door opens when power arrives and shuts when it goes; in
             //between it can still be opened by hand
             //between it can still be opened by hand

+ 25 - 0
src/web/Blocky/js/textures.js

@@ -1400,6 +1400,29 @@ function paintRetimerTop(t, on) {
     }
     }
 }
 }
 
 
+//OpAmp top: smooth stone with an amplifier triangle pointing to the top of
+//the tile (the way it faces), two input lines and an output line
+function paintOpAmpTop(t, on) {
+    paintSmoothStone(t, false);
+    const line = on ? [255, 52, 40] : [104, 26, 22];
+    for (let y = 4; y <= 11; y++) {
+        const half = Math.round((y - 3) * 0.62);
+        t.set(7 - half, y, line);
+        t.set(8 + half, y, line);
+    }
+    for (let x = 2; x <= 13; x++) {
+        t.set(x, 12, line);
+    }
+    for (let y = 13; y < 16; y++) {
+        t.set(5, y, line);
+        t.set(10, y, line);
+    }
+    for (let y = 0; y < 4; y++) {
+        t.set(7, y, line);
+        t.set(8, y, line);
+    }
+}
+
 //Iron bars: four bars with a band near the top and bottom, see-through
 //Iron bars: four bars with a band near the top and bottom, see-through
 function paintIronBars(t) {
 function paintIronBars(t) {
     t.clear();
     t.clear();
@@ -1905,6 +1928,8 @@ Object.assign(PAINTERS, {
     sign_item: paintSignItem,
     sign_item: paintSignItem,
     retimer_top: (t) => paintRetimerTop(t, false),
     retimer_top: (t) => paintRetimerTop(t, false),
     retimer_top_on: (t) => paintRetimerTop(t, true),
     retimer_top_on: (t) => paintRetimerTop(t, true),
+    opamp_top: (t) => paintOpAmpTop(t, false),
+    opamp_top_on: (t) => paintOpAmpTop(t, true),
     retimer_pin_off: (t) => t.fill([92, 24, 20], 10),
     retimer_pin_off: (t) => t.fill([92, 24, 20], 10),
     retimer_pin_on: (t) => t.fill([255, 60, 44], 14),
     retimer_pin_on: (t) => t.fill([255, 60, 44], 14),
     minecart: (t) => paintMinecart(t, null),
     minecart: (t) => paintMinecart(t, null),

+ 2 - 1
src/web/Blocky/js/world.js

@@ -112,6 +112,7 @@ export class World {
         this.onPowerSound = opts.onPowerSound || null;
         this.onPowerSound = opts.onPowerSound || null;
         this.onPistonPush = opts.onPistonPush || null;
         this.onPistonPush = opts.onPistonPush || null;
         this.onDispense = opts.onDispense || null;
         this.onDispense = opts.onDispense || null;
+        this.storageLevel = opts.storageLevel || null;
         this.powerBusy = false;
         this.powerBusy = false;
         this.pistonMoving = false;
         this.pistonMoving = false;
         this.buttonDue = new Map();
         this.buttonDue = new Map();
@@ -268,7 +269,7 @@ export class World {
                 this.schedule(x, y, z, 5 + Math.floor(this.rng() * 30));
                 this.schedule(x, y, z, 5 + Math.floor(this.rng() * 30));
             } else if (id === B.FIRE || ((id === B.WATER || id === B.LAVA) && c.meta[i] !== 0) || (id === B.BUTTON && (c.meta[i] & 8)) ||
             } else if (id === B.FIRE || ((id === B.WATER || id === B.LAVA) && c.meta[i] !== 0) || (id === B.BUTTON && (c.meta[i] & 8)) ||
                 ((id === B.PLATE_WOOD || id === B.PLATE_STONE) && (c.meta[i] & 1)) || (id === B.DETECTOR_RAIL && (c.meta[i] & 8)) ||
                 ((id === B.PLATE_WOOD || id === B.PLATE_STONE) && (c.meta[i] & 1)) || (id === B.DETECTOR_RAIL && (c.meta[i] & 8)) ||
-                id === B.RETIMER || id === B.RETIMER_ON) {
+                id === B.RETIMER || id === B.RETIMER_ON || id === B.OPAMP) {
                 this.schedule(bx + (i >> 11), i & 127, bz + ((i >> 7) & 15), 5 + Math.floor(this.rng() * 30));
                 this.schedule(bx + (i >> 11), i & 127, bz + ((i >> 7) & 15), 5 + Math.floor(this.rng() * 30));
             }
             }
         }
         }

+ 1 - 1
src/web/Blocky/js/worldedit.js

@@ -655,7 +655,7 @@ for (const id of [B.TORCH, B.SPARK_TORCH, B.SPARK_TORCH_OFF]) {
 }
 }
 const HDIR_KIND = new Uint8Array(256);
 const HDIR_KIND = new Uint8Array(256);
 for (let id = 0; id < 256; id++) {
 for (let id = 0; id < 256; id++) {
-    if (SHAPE[id] === "stairs" || [B.FENCE_GATE, B.RETIMER, B.RETIMER_ON, B.BED, B.DOOR, B.TRAPDOOR].includes(id)) {
+    if (SHAPE[id] === "stairs" || [B.FENCE_GATE, B.RETIMER, B.RETIMER_ON, B.OPAMP, B.BED, B.DOOR, B.TRAPDOOR].includes(id)) {
         HDIR_KIND[id] = 1;
         HDIR_KIND[id] = 1;
     }
     }
 }
 }