Browse Source

Fine tune blocky game physics mechanism

Toby Chui 2 weeks ago
parent
commit
0176890880

+ 26 - 5
src/web/Blocky/README.md

@@ -277,9 +277,17 @@ wire, 2 source, 4 reacts, 8 rail. Lever and button meta is the face they are
 attached to plus bit 8 for on / pressed; a pressed button's release time is
 kept in `world.buttonDue`, so other updates cannot let it go early.
 
-Pressure plates (two planks or two stone in a row) lie on a solid block;
-meta 1 while pressed. `Game.pressPlates` runs every game tick: the player
-and animals press both kinds, dropped items only wooden ones. Each press
+Pressure plates (two planks or two stone in a row) lie on a solid block,
+or on the same flat tops a torch stands on (top slab, upside down stairs,
+fence post: `flatTop` in world.js, support kind "plate"); meta 1 while
+pressed. `Game.pressPlates` runs every game tick and, like MC, senses any
+entity box that reaches the bottom quarter of the plate's block (inset
+1/16 at the sides), so a body passing beside a plate on a fence post
+presses it too. The player and animals press both kinds; dropped items,
+carts and boats only wooden ones. A cart's rider reaches 1.55 above the
+rail (feet 0.25 below it, 1.8 tall), so a plate on a fence over the track
+(sensing from 2.0) is never pressed by a rider; carts ignore blocks above
+their rail and roll through that fence. Each press
 sets the plate's release time 20 ticks ahead in `world.buttonDue`, so a
 plate lets go a second after the last thing leaves it. A pressed plate
 powers the wire beside it, the block under it and whatever touches it.
@@ -318,6 +326,13 @@ lines. A cart entering another rail block carries on away from the end it
 came in by, which takes it through the right angle where the two curves
 of a U-turn meet.
 
+A plain rail laid where three or four rails lead into it curves (MC-like),
+never runs straight: toward the half of the block the player clicked
+(`world.railAim`, set by game.js for that one placement), else south-east.
+A cart arriving from the side the curve leaves out is carried on along the
+curve, away from the end it is nearer (a gravity cart detector: the cart
+runs through onto a slope, and rolling back it turns onto the stem).
+
 Switches (`railSwitch`): a plain rail with rails on three sides is a
 junction. The two opposite sides are the through line, the third is the
 stem. Power (a lever or button beside it, wire, a powered block) sends the
@@ -332,7 +347,12 @@ rolls depends on its load (`Carts.loadOf`, `LOAD_*` in `carts.js`): reaching
 full speed (8) it cruises for a while, then slows at a steady rate; brakes
 are a much stronger steady deceleration that also holds it on a slope or
 against a furnace cart's push. Both come after anything pushing, so the
-stopping distance grows with the speed. From full speed on level track:
+stopping distance grows with the speed. A powered rail pushes a ridden
+(or loaded chest) cart harder than an empty one (`BOOST_RIDDEN` 26,
+`BOOST_EMPTY` 14 blocks/s^2): launched from a wall by one powered rail
+with two plain rails before a slope, the empty cart climbs 0.6 of the
+first slope block and rolls back, the ridden one just makes it past it
+(about 1.1). From full speed on level track:
 
 | Load | Stops on plain rails | Full speed for | Stops on brakes |
 | --- | --- | --- | --- |
@@ -350,7 +370,8 @@ speed through curves and onto slopes, roll downhill, bump into each other,
 are saved with the world. At the end of a track a cart stops against a
 block (a buffer) but rolls on into open space: off the rails it keeps its
 speed through the air and falls (`c.air`), a block in its way stops it
-sideways but not its fall, on the ground it slides to a stop, and it lands
+sideways but not its fall, on the ground it scrapes to a stop
+(`GROUND_DRAG` and `GROUND_STOP`: 1.25 blocks from full speed), and it lands
 back on any rail it comes down onto. A player walking into a cart on an
 unpowered powered rail pushes it at a slow pace (`BRAKE_PUSH`, 1.5
 blocks/s, also up a slope); the cart holds them back to that pace, so they

+ 3 - 3
src/web/Blocky/js/blocks.js

@@ -216,8 +216,8 @@ def(B.POWERED_RAIL, "Powered Rail", { tex: "powered_rail", shape: "rail", solid:
 def(B.SPARK_BLOCK, "Block of Spark", { tex: "spark_block", hardness: 5, tool: "pickaxe", tier: 1, sound: "metal" });
 //Pressure plates: meta 1 while pressed. Anything presses a wooden one;
 //only players and animals press a stone one.
-def(B.PLATE_WOOD, "Wooden Pressure Plate", { tex: "planks", shape: "plate", solid: false, opaque: false, lightOpacity: 0, hardness: 0.5, tool: "axe", sound: "wood", support: "solid" });
-def(B.PLATE_STONE, "Stone Pressure Plate", { tex: "stone", shape: "plate", solid: false, opaque: false, lightOpacity: 0, hardness: 0.5, support: "solid" });
+def(B.PLATE_WOOD, "Wooden Pressure Plate", { tex: "planks", shape: "plate", solid: false, opaque: false, lightOpacity: 0, hardness: 0.5, tool: "axe", sound: "wood", support: "plate" });
+def(B.PLATE_STONE, "Stone Pressure Plate", { tex: "stone", shape: "plate", solid: false, opaque: false, lightOpacity: 0, hardness: 0.5, support: "plate" });
 //Retimer: passes a signal on in the direction it faces, after a delay.
 //Meta: bits 0-1 facing (HDIR), bits 2-3 delay steps - 1 (2 ticks each).
 const RETIMER_DEF = { shape: "retimer", solid: false, opaque: false, lightOpacity: 0, hardness: 0, sound: "stone", support: "solid", interact: "retimer" };
@@ -437,7 +437,7 @@ item(I.FURNACE_MINECART, "Furnace Minecart", { icon: { flat: "furnace_minecart"
 item(I.BOAT, "Boat", { icon: { flat: "boat_item" }, stack: 1, boat: true, fuel: 400 });
 //Its needle points to the world spawn (the held compass turns, game.js);
 //with WorldEdit on it is also the navigation tool (worldedit.js)
-item(I.COMPASS, "Compass", { icon: { flat: "compass" } });
+item(I.COMPASS, "Compass", { icon: { flat: "compass" }, stack: 1 });
 
 //Fuel for blocks (ticks at 20 ticks per second; one smelt takes 200 ticks)
 ITEMS[B.LOG].fuel = 300;

+ 27 - 13
src/web/Blocky/js/carts.js

@@ -33,24 +33,34 @@ const RAIL_EXITS = [
 
 export const CART_ITEM = { cart: I.MINECART, chest: I.CHEST_MINECART, furnace: I.FURNACE_MINECART };
 const MAX_SPEED = 8;
-//How a cart rolls, by what it carries: [coast, roll, brake]. coast: blocks
-//it keeps full speed for before it starts to slow; roll: its deceleration
-//on plain rails after that (blocks / s^2); brake: its deceleration on an
-//unpowered powered rail (stronger than a slope's pull, so a braked cart
-//stops and stays put). From full speed on level track that comes to:
+const BOOST_EMPTY = 14, BOOST_RIDDEN = 26;
+//How a cart rolls, by what it carries: [coast, roll, brake, boost]. coast:
+//blocks it keeps full speed for before it starts to slow; roll: its
+//deceleration on plain rails after that (blocks / s^2); brake: its
+//deceleration on an unpowered powered rail (stronger than a slope's pull,
+//so a braked cart stops and stays put); boost: the push of a powered rail
+//(blocks / s^2). A ridden cart takes more from a powered rail than an
+//empty one: launched from a wall by one powered rail, the empty cart climbs
+//most of one block up a slope and rolls back, the ridden one just makes
+//it past the first block (the "empty cart detector").
+//From full speed on level track that comes to:
 //          stops on plain rails  (coast + 64 / 2 roll)   on brakes
 //  ridden  309 blocks            (176 + 133)             1.5 blocks
 //  empty   165                   (65 + 100)              1
 //  chest with 5 stacks  101      (45 + 56)               1.5
 //  full chest  40                (0 + 40)                1
-const LOAD_RIDDEN = [176, 0.24, 21];
-const LOAD_EMPTY = [65, 0.32, 32];
-const LOAD_SOME = [45, 0.57, 21];
-const LOAD_FULL = [0, 0.8, 32];
+const LOAD_RIDDEN = [176, 0.24, 21, BOOST_RIDDEN];
+const LOAD_EMPTY = [65, 0.32, 32, BOOST_EMPTY];
+const LOAD_SOME = [45, 0.57, 21, BOOST_RIDDEN];
+const LOAD_FULL = [0, 0.8, 32, BOOST_RIDDEN];
 const SOME_STACKS = 5;
 //Pace of a cart a player pushes along an unpowered powered rail
 const BRAKE_PUSH = 1.5;
 const HALF = 0.49;
+//Off the rails on the ground: speed falls by GROUND_DRAG per second
+//(exponential) plus a steady GROUND_STOP, so it comes to a real stop
+const GROUND_DRAG = 5;
+const GROUND_STOP = 3;
 
 export class Carts {
     constructor(game) {
@@ -349,8 +359,12 @@ export class Carts {
             //Sliding only with a block under its middle: a cart leaving a
             //track end grazes the last block without slowing
             if (ground && BLOCKS[w.getBlock(Math.floor(c.x), Math.floor(box[1] - 0.05), Math.floor(c.z))].solid) {
-                c.vx *= Math.exp(-4 * h);
-                c.vz *= Math.exp(-4 * h);
+                //Wheels scraping on the ground: full speed stops in about
+                //one and a half blocks
+                const sp = Math.hypot(c.vx, c.vz);
+                const k = sp > 0 ? Math.max(0, sp * Math.exp(-GROUND_DRAG * h) - GROUND_STOP * h) / sp : 0;
+                c.vx *= k;
+                c.vz *= k;
             }
             if (moveAxis(w, box, 0, c.vx * h) !== c.vx * h) {
                 c.vx = 0;
@@ -391,6 +405,7 @@ export class Carts {
         }
         //Downhill pull
         s -= 12 * dy * h;
+        const [coast, roll, brake, boost] = this.loadOf(c);
         if (r.id === B.POWERED_RAIL) {
             if (r.powered) {
                 if (Math.abs(s) < 0.4) {
@@ -402,7 +417,7 @@ export class Carts {
                         s = -2;
                     }
                 } else {
-                    s += Math.sign(s) * 14 * h;
+                    s += Math.sign(s) * boost * h;
                 }
             }
         }
@@ -425,7 +440,6 @@ export class Carts {
         }
         //Rolling and braking come last, after anything pushing: steady
         //decelerations, so the stopping distance grows with the speed
-        const [coast, roll, brake] = this.loadOf(c);
         const slow = (dec) => (Math.abs(s) <= dec * h ? 0 : s - Math.sign(s) * dec * h);
         if (r.id === B.POWERED_RAIL && !r.powered) {
             //A player leaning on it moves it at a slow pace

+ 32 - 18
src/web/Blocky/js/game.js

@@ -470,36 +470,49 @@ export class Game {
 
     /* ---------------- ticks ---------------- */
 
-    //Pressure plates under the player, animals and (wooden ones only)
-    //dropped items
+    //Pressure plates pressed by the player, animals and (wooden ones only)
+    //dropped items, carts and boats. A plate senses the bottom quarter of
+    //its block, inset 1/16 at the sides: feet standing on it, or a body
+    //passing through (beside a plate on a fence post, say)
     pressPlates() {
         const w = this.world, U = 1 / 16;
-        const stand = (x, y, z, half, living) => {
-            const by = Math.floor(y + 0.01);
-            for (let bx = Math.floor(x - half); bx <= Math.floor(x + half); bx++) {
-                for (let bz = Math.floor(z - half); bz <= Math.floor(z + half); bz++) {
-                    const id = w.getBlock(bx, by, bz);
-                    if ((id !== B.PLATE_WOOD && id !== B.PLATE_STONE) || (id === B.PLATE_STONE && !living)) {
-                        continue;
-                    }
-                    if (y - by > 0.25 || x + half <= bx + U || x - half >= bx + 1 - U || z + half <= bz + U || z - half >= bz + 1 - U) {
-                        continue;
-                    }
-                    if (w.pressPlate(bx, by, bz)) {
-                        this.sound.play("click");
+        const press = (b, living) => {
+            for (let by = Math.floor(b[1]); by < b[4]; by++) {
+                if (b[1] >= by + 0.25) {
+                    continue;
+                }
+                for (let bx = Math.floor(b[0]); bx < b[3]; bx++) {
+                    for (let bz = Math.floor(b[2]); bz < b[5]; bz++) {
+                        const id = w.getBlock(bx, by, bz);
+                        if ((id !== B.PLATE_WOOD && id !== B.PLATE_STONE) || (id === B.PLATE_STONE && !living)) {
+                            continue;
+                        }
+                        if (b[3] <= bx + U || b[0] >= bx + 1 - U || b[5] <= bz + U || b[2] >= bz + 1 - U) {
+                            continue;
+                        }
+                        if (w.pressPlate(bx, by, bz)) {
+                            this.sound.play("click");
+                        }
                     }
                 }
             }
         };
         const p = this.player;
         if (!p.dead) {
-            stand(p.x, p.y, p.z, 0.3, true);
+            press(p.box(), true);
         }
         for (const m of this.mobs.list) {
-            stand(m.x, m.y, m.z, m.def.w / 2, true);
+            press(this.mobs.box(m), true);
         }
+        const h = ITEM_HALF;
         for (const e of this.entities.items) {
-            stand(e.x, e.y, e.z, 0.125, false);
+            press([e.x - h, e.y, e.z - h, e.x + h, e.y + 2 * h, e.z + h], false);
+        }
+        for (const c of this.carts.list) {
+            press(this.carts.box(c), false);
+        }
+        for (const b of this.boats.list) {
+            press(this.boats.box(b), false);
         }
     }
 
@@ -1329,6 +1342,7 @@ export class Game {
         } else if (bid === B.RAIL || bid === B.POWERED_RAIL) {
             meta = Math.abs(Math.sin(p.yaw)) > Math.abs(Math.cos(p.yaw)) ? 1 : 0;
             w.railPreferX = meta === 1;
+            w.railAim = face === 2 ? [hit.point[0] - x - 0.5, hit.point[2] - z - 0.5] : null;
         } else if (bid === B.TORCH || bid === B.SPARK_TORCH) {
             if (face === 3) {
                 return;

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

@@ -377,12 +377,18 @@ function railCandidates(w, x, y, z) {
     return out;
 }
 
-export function shapeRail(w, x, y, z, preferX) {
+//aim: where on the block the player clicked, [dx, dz] from its middle (or
+//null), which picks the side a rail laid into a junction curves to
+export function shapeRail(w, x, y, z, preferX, aim) {
     const id = w.getBlock(x, y, z);
     const cands = railCandidates(w, x, y, z);
     let links = cands.map((c) => [c.k, c.dy]);
     if (cands.length > 2 || (cands.length === 2 && id === B.POWERED_RAIL && (cands[0].k ^ 1) !== cands[1].k)) {
-        //Best pair: rails leading in count most, then a straight line
+        //Best pair: rails leading in count most. Then a plain rail where
+        //three or four tracks meet curves (a switch, see railSwitch) toward
+        //the side clicked, else south-east like MC; a powered rail, which
+        //cannot curve, runs straight.
+        const [ax, az] = aim || [0.01, 0.01];
         let best = null, bestScore = -1;
         for (let i = 0; i < cands.length; i++) {
             for (let j = i + 1; j < cands.length; j++) {
@@ -390,7 +396,8 @@ export function shapeRail(w, x, y, z, preferX) {
                 if (!straight && id === B.POWERED_RAIL) {
                     continue;
                 }
-                const score = (a.back ? 2 : 1) + (b.back ? 2 : 1) + (straight ? 0.5 : 0);
+                const toward = straight ? 0 : (FLAT[a.k][0] + FLAT[b.k][0]) * ax + (FLAT[a.k][1] + FLAT[b.k][1]) * az;
+                const score = (a.back ? 2 : 1) + (b.back ? 2 : 1) + (straight ? 0 : 0.5 + 0.1 * Math.max(-1, Math.min(1, toward)));
                 if (score > bestScore) {
                     bestScore = score;
                     best = [a, b];
@@ -475,11 +482,11 @@ function openUTurn(w, x, y, z) {
 
 //A new rail: shape it, then let the neighbours it leads into turn toward
 //it, unless they are already joined at both ends
-export function railPlaced(w, x, y, z, preferX) {
+export function railPlaced(w, x, y, z, preferX, aim) {
     if (w.getBlock(x, y, z) === B.RAIL || w.getBlock(x, y, z) === B.POWERED_RAIL) {
         openUTurn(w, x, y, z);
     }
-    shapeRail(w, x, y, z, preferX);
+    shapeRail(w, x, y, z, preferX, aim);
     for (const k of railEnds(w, x, y, z)) {
         const n = railBeside(w, x, y, z, k);
         //(also when it leads here already: one block lower it must slope up)

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

@@ -43,6 +43,12 @@ function opacityOf(id, meta) {
     return o;
 }
 
+//Tops that are not full blocks but still hold a torch or a pressure plate:
+//a top slab, upside down stairs and a fence post
+function flatTop(id, meta) {
+    return (IS_SLAB[id] === 1 && meta === 1) || (SHAPE[id] === "stairs" && (meta & 4) !== 0) || id === B.FENCE;
+}
+
 const DIRS = [1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1];
 
 //Liquid behaviour: ticks between updates, level lost per block, last level
@@ -112,6 +118,8 @@ export class World {
         //Retimer output changes on their way: key -> { due, on }
         this.retimerDue = new Map();
         this.railPreferX = false;
+        //Where the player clicked the block a rail is laid on (game.js)
+        this.railAim = null;
         this.rules = Object.assign({}, DEFAULT_RULES);
         this.cropProgress = new Map();
         this.quiet = false;
@@ -675,7 +683,9 @@ export class World {
             this.notifyNeighbors(x, y, z);
         }
         if (oldId !== id && (POWER[id] & 8)) {
-            railPlaced(this, x, y, z, this.railPreferX);
+            const aim = this.railAim;
+            this.railAim = null;
+            railPlaced(this, x, y, z, this.railPreferX, aim);
         }
         if (!this.powerBusy && (POWER[oldId] || POWER[id])) {
             powerChanged(this, x, y, z);
@@ -778,6 +788,8 @@ export class World {
                 return below === B.GRASS || below === B.DIRT || below === B.FARMLAND;
             case "solid":
                 return isOpaqueAt(below, belowMeta);
+            case "plate":
+                return isOpaqueAt(below, belowMeta) || flatTop(below, belowMeta);
             case "sand":
                 return below === B.SAND || below === B.GRASS || below === B.DIRT;
             case "farmland":
@@ -812,7 +824,7 @@ export class World {
                     return true;
                 }
                 //Torches also stand on top slabs, upside down stairs and fence posts
-                return BLOCKS[id].support === "torch" && meta === 2 && ((IS_SLAB[aid] === 1 && am === 1) || (SHAPE[aid] === "stairs" && (am & 4)) || aid === B.FENCE);
+                return BLOCKS[id].support === "torch" && meta === 2 && flatTop(aid, am);
             }
             case "door":
                 if (meta & 8) {