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

world editor prototype #873

Merged
merged 11 commits into from
Jun 9, 2023
Merged

world editor prototype #873

merged 11 commits into from
Jun 9, 2023

Conversation

kostmo
Copy link
Member

@kostmo kostmo commented Nov 21, 2022

Towards #558
I was motivated to build this after finding that editing scenario maps directly in the YAML file is rather constraining.

What I've implemented so far

  • A small, collapsible panel to the left of the REPL containing World Editing status/operations. Enter world-editing mode with CTRL+e to show the panel.
    • This works only in --cheat mode
  • Terrain selection
    • A "picker"/"eye dropper" middle-click mechanism to select a terrain style to draw.
    • A pop-up selector to choose between the 5 different types of terrain.
  • Drawing terrain with the left mouse button
  • Saving a rectangular section of the world map (terrain only) to a file with CTRL+s
  • Code organization
    • The complete state of the World Editor, including "painted overlays" of terrain, is contained within the uiWorldEditor field of UIState record.
    • The bulk of the World Editor functionality shall be in new modules
    • Some refactoring of Controller.hs and View.hs to extract functions utilized by the World Editor (towards Split large Haskell modules #707)

Vision

  • The audience for this tooling is strictly envisioned to be Scenario authors.
    • Though, if we eventually allow swarm-lang to program the UI, there may be some common code to extract.
  • The World Editor is intended to be compatible with a workflow of editing maps in text form within YAML scenario files.

Demos

Round-trip with random world

stack run -- --scenario creative --seed 0 --cheat

Then Ctrl+e, tab down to the Save button, hit Enter to save the map
In another tab run:

stack run -- --scenario mymap.yaml

Toggle between tabs to compare, observe the derived map is an identical 41x21 subset.

@kostmo kostmo force-pushed the world-editor branch 2 times, most recently from 3defea0 to b08eb40 Compare November 21, 2022 09:10
@xsebek
Copy link
Member

xsebek commented Nov 21, 2022

@kostmo looks nice, but what do you mean by "unambiguous mapping in the scenario file"? A palette is a JSON object, so its keys are unique.

@kostmo
Copy link
Member Author

kostmo commented Nov 21, 2022

what do you mean by "unambiguous mapping in the scenario file"? A palette is a JSON object, so its keys are unique.

It might not be a bijection, though; there may be multiple palette entries that produce a given terrain. So I guess some logic/heuristic must be used to choose one of the palette entries when producing the map string.

mergify bot pushed a commit that referenced this pull request Nov 22, 2022
This trick facilitates use of `listEnums` and more concise pattern matching.

Towards #873.
@kostmo kostmo force-pushed the world-editor branch 2 times, most recently from bd81ab1 to 613f28c Compare November 25, 2022 07:59
@kostmo
Copy link
Member Author

kostmo commented Dec 4, 2022

I have found myself splitting some of the larger modules to get this PR to work. Perhaps I should spread this work across two PRs: the first will have no World Editor code in it---it will just be moving pre-existing code into different modules (i.e. toward #707). This should make the subsequent PR (adding World Editor functionality) easier to review.

If I do that, the splits on their own may not make a ton of sense unless viewed from the context of this current PR. In other words, take a look at the "final" PR to see the direction I'm going with those splits.

mergify bot pushed a commit that referenced this pull request Dec 12, 2022
Partially towards #707, although `Scenario.hs` is not that large.  Primarily this refactoring is in anticipation of #873 (see [comment](#873 (comment))).

Just like #879, there are no functional changes in this PR---only code relocation.
mergify bot pushed a commit that referenced this pull request Dec 12, 2022
Just like #880, this refactoring is in anticipation of #873 (see [comment](#873 (comment))) for ease of review, and there are no functional changes in this PR.

Also towards #707.
@kostmo kostmo force-pushed the world-editor branch 4 times, most recently from 4ad962e to c97ee55 Compare December 12, 2022 22:18
kostmo added a commit that referenced this pull request Dec 12, 2022
mergify bot pushed a commit that referenced this pull request Dec 14, 2022
Towards #873.

* Removes the `Eq` instance for `ModalType` --- I plan to add another member to that enum that contains data without an `Eq` instance.
* Decompose `toggleModal` function
@kostmo kostmo force-pushed the world-editor branch 3 times, most recently from faf6263 to 75656a3 Compare December 14, 2022 06:29
Copy link
Member

@xsebek xsebek left a comment

Choose a reason for hiding this comment

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

Cool, I think I understand the reasoning better after reading through this PR. 👍

Can't wait to use it to create new scenarios. 🎨 👨‍🎨

src/Swarm/Game/Scenario/EntityFacade.hs Outdated Show resolved Hide resolved
src/Swarm/Game/Scenario/Cell.hs Show resolved Hide resolved
src/Swarm/Game/Scenario/Cell.hs Outdated Show resolved Hide resolved
src/Swarm/Game/Scenario/WorldDescription.hs Outdated Show resolved Hide resolved
src/Swarm/Game/Scenario/WorldDescription.hs Outdated Show resolved Hide resolved
@kostmo kostmo marked this pull request as ready for review March 8, 2023 07:31
@kostmo kostmo requested a review from byorgey March 8, 2023 07:31
@byorgey
Copy link
Member

byorgey commented Mar 15, 2023

@kostmo sorry it's taken me a while to get to it, but I'm now slowly making my way through this PR. I have some suggestions, but ultimately I think merging it as a feature toggle makes a lot of sense.

@kostmo kostmo mentioned this pull request Mar 20, 2023
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.

Sorry it's taken me so long to look at this. In general I agree that we should try to merge this, perhaps at first in such a way that it's disabled by default. I think my biggest meta-comment is that this is a lot of code with very few comments, which made it difficult to review. I lost steam somewhere in TUI.Editor.Controller --- it was a lot of work to try to figure out what all the code was for. I'm not so much looking for comments that explain what the code does, but comments explaining things like purpose, design choices, where the code fits into the big picture, etc.

src/Swarm/Game/Scenario/EntityFacade.hs Show resolved Hide resolved
src/Swarm/Game/Scenario/Cell.hs Outdated Show resolved Hide resolved
src/Swarm/Game/Scenario/EntityFacade.hs Outdated Show resolved Hide resolved
src/Swarm/Game/State.hs Outdated Show resolved Hide resolved
src/Swarm/TUI/Controller.hs Outdated Show resolved Hide resolved
src/Swarm/Game/Scenario/WorldDescription.hs Outdated Show resolved Hide resolved
src/Swarm/Game/Scenario/WorldDescription.hs Outdated Show resolved Hide resolved
toKey :: TerrainEntityFacadePair -> TerrainEntityNamePair
toKey = fmap $ fmap (\(EntityFacade eName _display) -> eName)

getUniquePairs :: [[CellPaintDisplay]] -> M.Map TerrainEntityNamePair TerrainEntityFacadePair
Copy link
Member

Choose a reason for hiding this comment

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

What is the purpose of this function? In what sense are the pairs "unique"? It looks to me like if there are multiple things in the input with the same key, one will just be arbitrarily chosen as the value for that key, is that correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have now added a comment to this (renamed) function. Is this rationale OK? Maybe there's a better way:

-- | We want to identify all of the unique (terrain, entity facade) pairs.
-- However, "EntityFacade" includes a "Display" record, which contains more
-- fields than desirable for use as a unique key.
-- Therefore, we extract just the entity name for use in a
-- (terrain, entity name) key, and couple it with the original
-- (terrain, entity facade) pair in a Map.

src/Swarm/Game/Scenario/Cell.hs Show resolved Hide resolved
mergify bot pushed a commit that referenced this pull request May 7, 2023
"Stub" maps generated by the World Editor (#873) do not define any robots.

It is convenient to view these via:

    scripts/play.sh --scenario stub.yaml --cheat

However, the static needs to be suppressed in creative mode when robots do not exist.
@xsebek xsebek mentioned this pull request Jun 9, 2023
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.

🚀

@kostmo kostmo added the merge me Trigger the merge process of the Pull request. label Jun 9, 2023
@mergify mergify bot merged commit 987ddd6 into main Jun 9, 2023
@mergify mergify bot deleted the world-editor branch June 9, 2023 18:14
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.

3 participants