Coder Social home page Coder Social logo

alternate-current's Introduction

Alternate Current

Alternate Current is an efficient and non-locational redstone dust implementation. Its main focus lies in reducing the lag caused by redstone dust, by optimizing the power calculations and reducing the number of shape and block updates emitted. As a side effect of these changes the block update order of redstone dust networks is predictable and intuitive rather than locational and chaotic.

Performance

MSPT contributions of redstone dust are up to 20 times lower with Alternate Current, all the while maintaining a high level of Vanilla parity. Its low number of code modifications make it minimally invasive, so it is an easy drop-in replacement for Vanilla redstone dust.

How does it work?

The algorithm Alternate Current uses was designed with the following goals in mind:

  1. Minimize the number of times a wire checks its surroundings to determine its power level.
  2. Minimize the number of block and shape updates emitted.
  3. Emit block and shape updates in a deterministic, non-locational order, fixing bug MC-11193.

In Vanilla redstone wire is laggy because it fails on points 1 and 2.

Redstone wire updates recursively and each wire calculates its power level in isolation rather than in the context of the network it is a part of. This means a wire in a grid can change its power level over half a dozen times before settling on its final value. This problem used to be worse in 1.13 and below, where a wire would only decrease its power level by 1 at a time.

In addition to this, a wire emits 42 block updates and up to 22 shape updates each time it changes its power level.

Of those 42 block updates, 6 are to itself, which are thus not only redundant, but a big source of lag, since those cause the wire to unnecessarily re-calculate its power level. A block only has 24 neighbors within a Manhattan distance of 2, meaning 12 of the remaining 36 block updates are duplicates and thus also redundant.

Of the 22 shape updates, only 6 are strictly necessary. The other 16 are sent to blocks diagonally above and below. These are necessary if a wire changes its connections, but not when it changes its power level.

Redstone wire in Vanilla also fails on point 3, though this is more of a quality-of-life issue than a lag issue. The recursive nature in which it updates, combined with the location-dependent order in which each wire updates its neighbors, makes the order in which neighbors of a wire network are updated incredibly inconsistent and seemingly random.

Alternate Current fixes each of these problems as follows.

(1) To make sure a wire calculates its power level as little as possible, we remove the recursive nature in which redstone wire updates in Vanilla. Instead, we build a network of connected wires, find those wires that receive redstone power from "outside" the network, and spread the power from there. This has a few advantages:

  • Each wire checks for power from non-wire components at most once, and from nearby wires just twice.
  • Each wire only sets its power level in the world once. This is important, because calls to Level.setBlock are even more expensive than calls to Level.getBlockState.

(2) There are 2 obvious ways in which we can reduce the number of block and shape updates.

  • Get rid of the 18 redundant block updates and 16 redundant shape updates, so each wire only emits 24 block updates and 6 shape updates whenever it changes its power level.
  • Only emit block updates and shape updates once a wire reaches its final power level, rather than at each intermediary stage.

For an individual wire, these two optimizations are the best you can do, but for an entire grid, you can do better!

Since we calculate the power of the entire network, sending block and shape updates to the wires in it is redundant. Removing those updates can reduce the number of block and shape updates by up to 20%.

(3) To make the order of block updates to neighbors of a network deterministic, the first thing we must do is to replace the location- dependent order in which a wire updates its neighbors. Instead, we base it on the direction of power flow. This part of the algorithm was heavily inspired by theosib's 'RedstoneWireTurbo', which you can read more about in theosib's comment on Mojira here or by checking out its implementation in carpet mod here.

The idea is to determine the direction of power flow through a wire based on the power it receives from neighboring wires. For example, if the only power a wire receives is from a neighboring wire to its west, it can be said that the direction of power flow through the wire is east.

We make the order of block updates to neighbors of a wire depend on what is determined to be the direction of power flow. This not only removes locationality entirely, it even removes directionality in a large number of cases. Unlike in 'RedstoneWireTurbo', however, I have decided to keep a directional element in ambiguous cases, rather than to introduce randomness, though this is trivial to change.

While this change fixes the block update order of individual wires, we must still address the overall block update order of a network. This turns out to be a simple fix, because of a change we made earlier: we search through the network for wires that receive power from outside it, and spread the power from there. If we make each wire transmit its power to neighboring wires in an order dependent on the direction of power flow, we end up with a non-locational and largely non-directional wire update order.

alternate-current's People

Contributors

lizelive avatar spacewalkerrs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

alternate-current's Issues

Question

i am making my own redstone power map (basically re-implementing redstone) and i am wondering as to what areas of your code deal with what functionality in terms of power propagation, state tracking, block tracking, power source tracking, etc

this is my current implementation
https://github.com/mgood7123/Infinity-Wire/blob/main/src/main/java/smallville7123/modid_infinity_wire/client/redstone/RedstonePowerManagement.java

it is basically an implementation of redstone that does not loose it's input power over-time

i plan to add more functionality to it but the power map needs to be stable since everything will depend on it for correct power management

(basically the vanilla redstone becomes self-powering if it's decay is removed, which quickly becomes a problem as it cannot be de-powered until physically destroyed)

currently direct and indirect power sources work correctly for both wire placement/destruction and when building next to an existing wire (and then optonally destroying that wire)

(which is simply states PLACED, REMOVED, and NEIGHBOR_CHANGED)

and i have yet to implement stair-case power detection

EDIT: "shared power" has been implemented - mgood7123/Infinity-Wire@a1b9616

(per vanilla, POWER must NOT be removed from the map if it is still providing power to any existing WIRE)

eg

POWER, WIRE
WIRE

if either WIRE is removed, the POWER should not be removed because the other WIRE still relies on this POWER

Observers run slower with alternate-current enabled.

This may not be a very useful report but when testing a lag machine that used redstone updates to lag the game I noticed adding observer ran worse with alternate-current installed as opposed to with it off. It could be due to interactions with other mods or something but I have not looked into it. I can provide more details if needed.

[Question] Where this mod has different behavior than vanilla?

Are there any redstone mechanics that work differently here than they do in vanilla?
How many possibilities are there that this changes would impact my build?

I'm asking this question because on Modrinth you wrote:

*While Lithium can boast 100% Vanilla parity, Alternate Current cannot.

Is it still a thing or there were changes?

Thank you a lot for your good work on this project :)

Water not watering?

I have no clue how to describe it but basically fish would flop on the ground in the water and die as if they were on land, you would take fall damage when you touched the ground in the water.

Vertical Block Conveyor Breaks On IcebergLettuce's Mangrove Farm

The pistons in the vertical block conveyor are not well-timed causing the block to be pushed horizontally occasionally causing the farm to break. This did not happen in my test server with vanilla redstone implementation. The following attachments are the conveyor in my mangrove farm which I built in my SMP server.

2022-07-19_17 18 40
2022-07-19_17 18 08
2022-07-19_17 19 42

To replicate this bug, try the farm in the world download at a server with alternate-current redstone implementation.

The Mangrove Farm (w/ world download): https://youtu.be/9aWn-DMolxQ

The Vertical Block Conveyor: https://youtu.be/fm36BxDXId8

Tnt duper design not working AC on 1.20.4

Hey!
I've been using AC for a long time now, never encountered an issue until now.

I found a specific design for a tree farm, and in this case it doesn't use the traditional slime and coral mechanic to make it happen, I couldn't make it to work until AC was the only mod removed, I tried even with only AC installed and it still would stop launch/dupe the tnt.

tree farm chosen: https://youtu.be/aTww2A6QGPA

world download: https://www.mediafire.com/file_premium/y1wy373tl4mcl6r/TheySix_-_UTFX_Tree_Farm_V2.rar/file

Dispensers/Droppers not untriggering after power is removed

Hi, I recently had one of my players encounter a bug with AC, where a dropper will not untrigger after power is removed in a certain configuration. I have provided a schematic below that demonstrates this bug.

Schematic: ac_bug.zip
Screenshot:
image

Simply paste the schematic and press the button twice. You will note that the dropper triggers initially as expected, but will not drop any more items after subsequent button presses. This behavior doesn't make any sense and also isn't present in vanilla.

Cheers.

Atlantean Power Dust from the Atlantis mod not updating

Atlantean Power Dust from the Atlantis mod is not updating while this mod and Atlantis are installed together

to test place an Atlantean Lever, Atlantean Power Dust, and a Redstone lamp next to each other and try to turn it on

the redstone will not update unless replaced by the player.

Loom 0.8

Loom 0.8-SNAPSHOT should not be used for Minecraft 1.16 and below because it either breaks compatibility with Java <16 or it fails to comnpile with Java <16 (I forgot which one). Loom 0.8 is essential for Minecraft 1.17 (specifically 21w19a+), but for MC <=1.16, Loom 0.7-SNAPSHOT should be used.

id 'fabric-loom' version '0.8-SNAPSHOT'

/alternatecurrent broke in 0.3.0

Minecraft: 1.17.1
Alternate Current: 0.3.0

As of version 0.3.0 the command /alternatecurrent can no longer be ran as it does not show up in the auto suggestions and fails to run. I am certain that the mod is initialized as I see the message in the logs, it shows up on modmenu on a client, and I can see the optimizations with the neighbor updates debug view enabled.

1.19.1?

Is 1.19.1 support coming soon?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.