Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow zero-tick recipes to apply immediately #1272

Merged
merged 2 commits into from
May 20, 2023
Merged

Conversation

kostmo
Copy link
Member

@kostmo kostmo commented May 20, 2023

Summary

This PR allows a recipe's effects to be applied within the same tick if time: 0 is specified for the recipe.

The previous behavior was that if the time of the recipe was zero, it would nonetheless behave the same as time: 1, and the entity updates would not be applied until the next tick.

This new behavior is useful when "abusing" the drill command to emulate various other games via help from a system robot.

Motivation

For the "Lights Out" game (#1273) I have implemented a system robot that instant-ly toggles the four adjacent lights and the center light when a center light is drill-ed by the player. The light entities are named "off" and "on".

I did some experiments to see if the system robot would react "fast enough" if the player drilled quickly in succession.

Experiments

The 5x5 light grid is initially "off". I moved the player robot to the center of the grid, facing east. From this position, I tried three command sequence variations both before and after this PR. The recipes for toggling lights specify time: 0.

Before this PR

1. Drilling twice in succession

drill down; drill left;

This resulted in an error:
no-instant-error-message

so only the first drill got applied:

no-instant

This is because I believe the sequence of events was:

Tick / Robot Action
Tick 1: Player Execute the first drill command, queue entity modification
Tick 1: System No changes observed
Tick 2: Player Entity modification from first drill is applied. Execute second drill command, queue second entity modifications
Tick 2: System Observe modified entity, toggle the adjacent lights
Tick 3: Player Try to apply entity modification from second drill command. Throw an error because the system robot has toggled the drill's target entity to on whereas the drill was initially executed when it was still off.

2. Instant for both drills

instant $ (drill down; drill left)

In this case, both of the player's drill commands were executed, but the system robot then failed to doubly-invert one of the cells:

instant-both-drills

This is due to some kind of race condition in the system robot's logic. However, it is irrelevant because the instant command shall not be available to the player.

3. Instant for first drill only, then second drill

instant (drill down); drill left;

This accomplished what approach number 1 was supposed to; both drill commands were applied and the two overlapping cells got properly inverted:

instant-first-drill

Explanation: In this case, the entire drilling operation (including applying all effects to entities on the ground) was completed within the player's first tick, so the system robot updated the lights appropriately on its turn. Then, in the next tick the player robot applied another drill command, and again the system robot appropriately did another inversion on the two overlapping middle cells.

After this PR

1. Drilling twice in succession

drill down; drill left;

Now works as expected:

after-no-instant

2. and 3.

Behaved identically to before the PR.

@kostmo kostmo force-pushed the immediate-recipes branch from 238bc50 to b6fbf30 Compare May 20, 2023 04:18
@kostmo kostmo marked this pull request as ready for review May 20, 2023 04:56
@kostmo kostmo requested a review from byorgey May 20, 2023 04:56
@kostmo kostmo force-pushed the immediate-recipes branch from b6fbf30 to 5099ecf Compare May 20, 2023 04:59
@kostmo kostmo force-pushed the immediate-recipes branch from 5099ecf to 1da7fdd Compare May 20, 2023 05:00
src/Swarm/Game/Step.hs Outdated Show resolved Hide resolved
src/Swarm/Game/Step.hs Outdated Show resolved Hide resolved
@kostmo
Copy link
Member Author

kostmo commented May 20, 2023

How's it look now?

Copy link
Member

@byorgey byorgey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@kostmo kostmo added the merge me Trigger the merge process of the Pull request. label May 20, 2023
@mergify mergify bot merged commit 6044145 into main May 20, 2023
@mergify mergify bot deleted the immediate-recipes branch May 20, 2023 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge me Trigger the merge process of the Pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants