mineflayer-statemachine icon indicating copy to clipboard operation
mineflayer-statemachine copied to clipboard

Add Instant Transitions

Open TheDudeFromCI opened this issue 3 years ago • 5 comments

Currently, manual transitions only mark the transition as should transition on the next physics tick. Since this is not Instant, many events may be missed or sent to the wrong target.

There should be a method for transitioning instantly.

TheDudeFromCI avatar Dec 17 '20 01:12 TheDudeFromCI

Hi @TheDudeFromCI , do you have any ideas how to do it? How can I help you?

I think I need it, I have some process so that it needs to be "faster"

Example -> When digging a block and it has water behind it, the BOT needs to calculate where to place the block to prevent water from entering the tunnel

Currently it is "a little" slow and the water gets to enter and bothers,

For now I have it done in different state machines and for that reason it is "slow", I have replaced it so that it calculates everything in a function without jumping between state machines, with this method dont jump between state machines and is faster, but the code is more "dirty"

It would be interesting what you comment to avoid dirty code

Maybe adding a opitional value similar has "fasterJump: true" ?

 new StateTransition({
      parent: mineBlock,
      child: placeBlock1,
      fasterJump: true, // <----
      name: 'mineBlock -> placeBlock1',
      onTransition: () => {
        placeBlock2Position = targets.position.clone()
        const positionForPlaceBlock = getNewPositionForPlaceBlock(targets.position.offset(0, -1, 0))
        targets.position = positionForPlaceBlock.newPosition
        placeBlock1.setOffset(positionForPlaceBlock.blockOffset)
      },
      shouldTransition: () => mineBlock.isFinished()
    })

PS: I am attaching a video that shows what I mean is "slow" https://www.youtube.com/watch?v=9OH_71bTH-k

sefirosweb avatar Sep 25 '21 12:09 sefirosweb

It couldn't be a normal shouldTransition? state transition as those all run on clocks. In order to achieve this effect, it will need to be triggered manually and run synchronously.

TheDudeFromCI avatar Sep 25 '21 12:09 TheDudeFromCI

Hi @TheDudeFromCI I don't know if we talking of the same,

If i do the "checks" via state machines is going a """bit slow""" (really only in 1-2 seconds is done but the water is faster =P )

I need to do that because i need to check a large numbers of possible blocks, in in the logic return one of them "good" then pass to new statemachine if not they return back for check if more blocks needs to be check

I can do it without state machine, but the code starts to be confusing, via "state machine" is friendly to see,

I attach video of """slow""" check

https://www.youtube.com/watch?v=B6JJwgIrTX0

sefirosweb avatar Sep 25 '21 16:09 sefirosweb

@sefirosweb I understand your problem. The problem comes from the fact that the state machine runs on an async timer which prevents instant updates. See this diagram.

state machine loop

This is basically how the state machine chooses when to transition and actually performs the transition. (Link)

The update() function is called once every tick current the physicsTick event. In the diagram, this is represented by the blue part. If a transition should occur, then it calls the yellow part which calls all of the corresponding events and marks the new state as active. Because this part is exclusively hidden behind the timer, it can't run any faster than this while still remaining async.

The solution would be to extract the yellow part of the code into a new function that you can call from your own code, the green part, on the state machine itself and turn this part into a synchronous function.

TheDudeFromCI avatar Sep 26 '21 06:09 TheDudeFromCI

Thanks a lot, understood!

I'm trying to read your code, if it is possible to add to the end of "onStateEntered" to force the update function without waiting to next physic tick,

But I think for a now the best way is do as you said

sefirosweb avatar Sep 26 '21 11:09 sefirosweb