Commit Graph

1254 Commits

Author SHA1 Message Date
Karl Ostmo
743ec13a1c
Eliminate trailing whitespace in all scenarios (#1710)
Towards #845

This is to avoid the undesirable transformation by `yq` of whitespace-preserving string fields into quoted one-line strings, described in https://github.com/swarm-game/swarm/pull/1706#discussion_r1442274007.

Command:

    find data/scenarios -type f -name '*.yaml' -print0 | xargs -0 --max-args 1 sed -i -e 's/[[:blank:]]\+$//'
2024-01-05 16:56:01 +00:00
Brent Yorgey
e350ab8ae7
update to LTS-21.25 (#1708)
21.25 uses GHC 9.4.8 instead of 9.4.7; the latest HLS release supports 9.4.8 but not 9.4.7.
2024-01-04 22:12:36 +00:00
Brent Yorgey
6fa150bdcb
fix color of trees in main logo (#1707) 2024-01-04 22:00:58 +00:00
Karl Ostmo
be34d0c027
sort module list (#1705) 2024-01-04 13:23:15 -08:00
Karl Ostmo
e4487aba0f
move 'Swarm.Language.*' modules to sublibrary (#1704) 2024-01-04 13:21:02 -08:00
Karl Ostmo
979c72b72c
bump cabal-build version, fix haddock script (#1703)
Encountered this bug: https://github.com/commercialhaskell/stack/issues/5254#issuecomment-1874622685

Fixed by using `cabal` to generate haddocks instead of `stack`.
Also bumped `cabal-version` to latest and adjusted `swarm.cabal` to conform.
2024-01-03 20:47:00 +00:00
Karl Ostmo
ea2863334a
snake game (#1699)
Demos use of a string to maintain a queue of coordinates.

![Screenshot from 2024-01-02 10-34-05](https://github.com/swarm-game/swarm/assets/261693/eaa24d48-40dc-4294-8243-2977caf4b79f)
2024-01-03 17:46:16 +00:00
Karl Ostmo
1da74a79dd
move ReadableIORef to util sublibrary (#1700)
As title.
2024-01-01 02:29:07 +00:00
Karl Ostmo
f6f01b264c
web sublibrary (#1697)
Towards #1043

Creates a new sub-library for the web service.  Also moved the `Swarm.App` module from the `swarm` library to the `swarm` executable.
2024-01-01 02:06:58 +00:00
Karl Ostmo
ccd64fcff8
return distance from path command (#1698)
Augments #836.

Should have access to the remaining distance, since it's available internally.  This will help in implementing #1696.
2023-12-31 20:14:47 +00:00
Karl Ostmo
358e60bb87
demo pixi.js (#1650)
Towards #1415.

Potential candidate for a web-based world display

Uses [pixi.js](https://pixijs.com/).

# Demo

1. Run swarm: `scripts/play.sh -i scenarios/Fun/horton.yaml --autoplay --speed 6`
1. Visit http://localhost:5357/play.html
2023-12-20 06:41:38 +00:00
Karl Ostmo
c48a46cac4
refactor for colors and grid type (#1692)
This is in preparation for both #1672 and #1650.

* Added some utility methods for color flattening
* The `Grid` type has a new home in `Area.hs` and now derives more instances
* `getTerrainEntityColor` is extracted from the `getDisplayColor` function
* Add a favicon to silence browser warnings
2023-12-18 15:07:47 +00:00
Karl Ostmo
7fa97b6461
short module descriptions (#1690)
Towards #1689

The [`Description:` field](https://haskell-haddock.readthedocs.io/en/latest/markup.html#the-module-description) in a module's toplevel haddock will be displayed on the Contents page.

![Screenshot from 2023-12-14 20-55-26](https://github.com/swarm-game/swarm/assets/261693/841f2834-8273-4394-b764-8f5f4479b9c4)
2023-12-16 19:05:27 +00:00
Karl Ostmo
53e6f35c0e
dim sum restaurant (#1686)
scripts/play.sh -i scenarios/Challenges/dimsum.yaml --autoplay

![Screenshot from 2023-12-13 22-39-53](https://github.com/swarm-game/swarm/assets/261693/d3039776-c819-4b1c-acfe-dbd0265e9cb0)
2023-12-15 04:49:54 +00:00
Karl Ostmo
eaa5551471
robot cardinal direction literals (#1685)
Closes #1679.

Split into two commits:
1. Parser and JSON schema changes
2. Update all scenario files (except `chess_horse.yaml`)
2023-12-14 21:04:44 +00:00
Karl Ostmo
b158251a20
pushable property (#1683)
Closes #1681
2023-12-13 21:32:55 +00:00
Karl Ostmo
13ae996306
recognize structures with rotation (#1678)
Closes #1644.

The `"recognize"` property in scenario `.yaml` files is changed from a boolean to a list of "up" directions.

The structure recognizer adds a rotated copy of each supported orientation to its automaton.  Rotational symmetry is accounted for to avoid duplicate work in the recognizer.

Also in this PR:
* Add cardinal directions to the JSON schema
* Tetromino packing challenge scenario

## Demos

    scripts/run-tests.sh --test-arguments '--pattern "1644-rotated"'

### Structures dialog

![Screenshot from 2023-12-10 18-47-01](https://github.com/swarm-game/swarm/assets/261693/3904b66e-dd22-455b-8b68-5913021f806a)

### Tetromino packing

    scripts/play.sh -i data/scenarios/Challenges/pack-tetrominoes.yaml --autoplay

![Screenshot from 2023-12-09 23-11-00](https://github.com/swarm-game/swarm/assets/261693/0ad7c0ce-3553-4ad5-a927-82bbfdbe63d8)
2023-12-13 21:21:08 +00:00
Brent Yorgey
e227230bbb
Specifically build swarm:swarm component in play.sh script (#1682)
This seems to help with rebuild times, since we are specifically requesting to only build the `swarm` executable and not, say, the `swarm-docs` executable.
2023-12-13 20:34:43 +00:00
Karl Ostmo
1bcd050780
Inventory requirements for child robots of system robots (#1677)
Towards #1664.

This PR adds a failing unit test.
2023-12-13 16:49:33 +00:00
Karl Ostmo
36a0046ece
unit test for recipe coverage (#1676)
Towards #1268

## Demo

    scripts/run-tests.sh --test-arguments '--pattern "Recipe coverage"'
2023-12-12 15:32:38 +00:00
Karl Ostmo
8e1fe17758
use strictly background color for terrain (#1674)
Towards #1662

Prerequisite for #1672

To allow a robot-occupied cell to take on the underlying terrain color as the background color of its cell, the terrain color must be representable strictly as a "background".  However, currently, the `dirt`, `stone`, and `grass` terrains are represented by a half-shaded **foreground** glyph upon `black` background.

Currently the ["Medium Shade" unicode character](https://www.compart.com/en/unicode/U+2592) (`▒`) is used  to "blend" a somewhat bright color with a black background, resulting in a moderately dark color in the terminal for `dirt`, `stone`, and `grass`.  However, these same dark colors are not reproducible in the 240-color scheme without this foreground+background blending trick; the closest approximations as a background-only or foreground-only color come out quite a bit lighter.

## Visual comparison

Using:

    scripts/play.sh -i creative --seed 2 --autoplay

| Before | After |
| --- | --- |
| ![Screenshot from 2023-12-03 23-33-15](https://github.com/swarm-game/swarm/assets/261693/edeeaeac-13e0-4641-9822-773fdb20f1d4) | ![Screenshot from 2023-12-03 23-32-36](https://github.com/swarm-game/swarm/assets/261693/ae5a5b5d-aa69-4580-b7e1-85eec21b4aeb) |

## Possible approaches

So, we need to decide whether to:
1. Accept the new lighter colors
2. Choose new, alternative terrain colors that may be darker given the 240-color palette
3. Abandon support for 240 colors and assume "full" color terminals
4. Abandon efforts to passthrough terrain color as background of robot cells
2023-12-12 04:08:32 +00:00
Karl Ostmo
938aa2cbce
split utils into sublibrary (#1675)
Towards #1043

This is the first of potentially more granular sub-library splits.
2023-12-07 17:21:03 +00:00
Karl Ostmo
b8d37a9364
extract doc generator to separate executable (#1671)
Closes #1443.

Also added `-Wunused-packages` to clean up dependencies.

## Demo

This still works as usual:

    stack run

Output editor keywords:

    stack run swarm-docs -- editors --emacs
2023-12-04 03:45:07 +00:00
Karl Ostmo
437f70418c
Move more code to Robots.hs, do not export private fields (#1669)
A continuation of #1667.

* Do not export `_viewCenter` or `_focusedRobotID`
* Move record initialization code to `Robot.hs` as `setRobotInfo`.
* `modifyViewCenter`, `unfocus`, `recalcViewCenter` now operate directly on `Robots` instead of `GameState`.
2023-12-03 22:31:35 +00:00
Karl Ostmo
536f1dcf47
Inherit parent robot display (#1670)
Closes #1663.

The status quo is that built-children of system robots are visible, even if the system robot is invisible.  So we preserve that behavior for now, with the potential to override it at some future point.  We don't yet have a definite need to make children of system robots invisible, but this PR does at least provide a mechanism to set the foreground and background of child robots (as a stopgap until #1230).

Tested with:

    scripts/play.sh -i scenarios/Challenges/Ranching/beekeeping.yaml --autoplay

| Before | After |
| --- | --- |
| ![Screenshot from 2023-12-01 17-31-00](https://github.com/swarm-game/swarm/assets/261693/9a9a49cb-fee5-424d-a748-9cb3c65ffe8f) | ![Screenshot from 2023-12-01 17-35-19](https://github.com/swarm-game/swarm/assets/261693/4da17e20-8f1a-46fc-ac9c-6154ccb7d4c7) |
2023-12-03 22:04:07 +00:00
Karl Ostmo
3094abd565
Offload subrecords of GameState to other modules (#1667)
This is a continuation of #1652.

Most of the sub-records are bundled into `Swarm.Game.State.Substate`, but we create a `Swarm.Game.State.Robot` module just for robots.

We introduce a `zoomRobots` function so that applicable functions can operate directly on `Robots` state instead of `GameState`.

## Size comparison

### Before

| File | Lines |
| --- | --- |
| `State.hs` | 1569 |

### After

| File | Lines |
| --- | --- |
| `State.hs` | 812 |
| `Substate.hs` | 497 |
| `Robot.hs` | 395 |
| `Config.hs` | 21 |
## For follow-up PR:
- [ ]  Remove exports of `_viewCenter` and `_focusedRobotID` from `Swarm.Game.State.Robot`
2023-11-30 21:57:39 +00:00
Karl Ostmo
b244a4223c
Split Step.hs (#1665)
Both `execConst` and `stepCESK` are huge case statements.  This refactor puts them in different modules.

Also many supporting functions for implementing commands in `execConst` are moved to their own modules.

Whereas `Step.hs` previously had `2931` lines, the new linecounts are:

| File | lines |
| --- | --- |
| `Step.hs` | 848 |
| `Arithmetic.hs` | 124 |
| `Const.hs` | 1687 |
| `Command.hs` | 417 |

The only tricky thing was that `execConst` is mutually recursive with `runCESK`.  So to get them into different modules, I pass a wrapper of `runCESK` as an argument to `execConst`.
2023-11-28 17:41:14 +00:00
Karl Ostmo
e03251cc0b
beekeeping scenario (#1599)
Builds upon #1579

Requires player to constuct beehives to attract bees and make honey, which is then brewed as mead.

## Demo

    scripts/play.sh -i scenarios/Challenges/Ranching/beekeeping.yaml --autoplay

![image](https://github.com/swarm-game/swarm/assets/261693/f8c5c898-d865-4fe7-954d-c9e5b5f9a5c8)

Map:
![map](https://github.com/swarm-game/swarm/assets/261693/9c288edb-e71f-4a59-bd32-e1ecfdb1b60e)

## References

Mead hall inspiration: https://cartographyassets.com/assets/13507/the-mead-hall-of-the-clan-ulfgar-50-x-50/

![Mead-Hal-v1-No-Light-or-Shadow-with-grid-copy-scaled](https://github.com/swarm-game/swarm/assets/261693/a2d276f1-a522-499e-9a4b-7ce1df754107)
2023-11-28 03:39:28 +00:00
Karl Ostmo
44c2e607b3
Use a nonempty list for unchainFun (#1656)
Use a nonempty list for `unchainFun`
2023-11-22 21:57:42 +00:00
Karl Ostmo
8239f27247
dedicated record for randomness (#1653)
Reduces the number of fields in the toplevel `GameState` by one.
2023-11-22 21:44:31 +00:00
Karl Ostmo
4b7cb8d1af
validate display character widths (#1655)
Characters that are too wide mess up the world rendering.

## Demo

    scripts/play.sh -i data/scenarios/Testing/_Validation/1655-display-characters.yaml
2023-11-22 21:21:13 +00:00
Karl Ostmo
40f26df9a8
use built-in portal functionality for Hackman scenario (#1651)
Previously, the logic to wrap horizontally was hand-coded, both for the player (using a dedicated monitor robot) and for ghosts (who, as system robots, implemented the teleportation themselves).

Now that we can use built-in portal functionality (#1356), no custom logic is needed to handle the teleportation.  A (much-simplified) monitor robot still observes the base robot to award the hidden "World wrap" goal.

## Demo

    scripts/play.sh -i data/scenarios/Challenges/hackman.yaml --autoplay --speed 6
2023-11-22 18:18:42 +00:00
Karl Ostmo
3c970c19ff
group robot info into subrecord (#1652)
Add another subrecord to `GameState` dedicated to robot fields.  This shrinks the size of `GameState` by 8 more fields.  It's getting reasonable now!

The `_viewCenter*` fields were put there too, for now, because the manually-defined `setter` of the `viewCenterRule` lens needs access to the `robotMap` field.
2023-11-22 18:06:13 +00:00
Karl Ostmo
724650d8d9
serve static index page (#1648)
API docs are moved under `api/`, and the landing page for http://localhost:5357/ is now a static `index.html` page.

This paves the way for a JS-enabled web frontend demo.
2023-11-21 17:10:46 +00:00
Karl Ostmo
27f0d087ad
render recognized structures in bold (#1646)
Visually differentiate entities in the plane when they become part of a recognized structure.

## Demo

    scripts/play.sh -i data/scenarios/Testing/1575-structure-recognizer/1575-browse-structures.yaml --autoplay --speed 2

![image](https://github.com/swarm-game/swarm/assets/261693/71d7f3cc-40b9-4ffe-88c9-7c528cc358b3)
2023-11-21 16:58:50 +00:00
Karl Ostmo
bba15a7337
split cheatsheet generation to separate module (#1640)
Towards #1546.

This is a no-op refactoring to reduce the size of `Gen.hs` by about 200 lines.
2023-11-21 03:43:00 +00:00
Karl Ostmo
303e58db97
fix wiki table generation (#1639)
Tables were not rendering properly (same fix as #1577).

Commands table is now updated: https://github.com/swarm-game/swarm/wiki/Commands-Cheat-Sheet
2023-11-20 17:24:49 +00:00
Karl Ostmo
01a5b070e7
render map to PNG format (#1632)
Towards #1415.

## Uses

This capability could be used to quickly iterate on DSL world descriptions, e.g. when tuning noise parameters.

## Implementation notes

* For the hard-coded ANSI terminal color names, I chose RGB triples that matched my own terminal settings.  This means that a rendered PNG might not exactly match one's own terminal colors.
* `Blank` terrain corresponds to a transparent pixel.
* Implemented parse-time validation of `attr` references.  Previously, referencing a nonexistent `attr` by an entity would fail silently at runtime.
* Normalization: strings like `"rock"` now only exist once; the string is shared via toplevel variable definitions
* Entities and terrain have TUI-independent color definitions from which VTY Attrs are derived, but all TUI user-interface colors are defined only as VTY Attrs.

## Demos
Each pixel in the output image correponds to one world cell.  To enlarge, can use [imagemagick](https://legacy.imagemagick.org/Usage/resize/#scale):

    stack run -- map data/scenarios/classic.yaml --seed 0 --png -w 300 -h 200 && convert output.png -scale 800% out2.png

![out2](https://github.com/swarm-game/swarm/assets/261693/51794b63-7d78-4738-b20a-8b4f4352f006)

    stack run -- map data/scenarios/Challenges/bridge-building.yaml --png && convert output.png -scale 800% out2.png

![image](https://github.com/swarm-game/swarm/assets/261693/b04895a2-eb61-4499-a122-ae8444f7e4fb)
2023-11-20 04:37:49 +00:00
Karl Ostmo
e43ff3e915
Structure recognition with encroaching entities (#1637)
Structure recognition already supports overlapping bounding boxes between two structures (where one of the structure templates has vacant cells).  This PR adds test coverage for this.

More importantly, this PR makes sure that structures can be recognized even with non-participating entities within their bounding box.  The same integration test (`1575-bounding-box-overlap.yaml`) covers this case as well.

A "non-participating entity" is an entity that is not an ingredient to any structures participating in structure recognition.  Such entities are "masked out" when the world cells (i.e. the "haystack") are queried to apply the Aho-Corasick matcher to.

## Example

Given a structure defined as `boulder`s (`@`) in a backwards "L" shape, it will be recognized despite a `mountain` (`A`) within its bounding box:

![image](https://github.com/swarm-game/swarm/assets/261693/1c559a50-e9bd-4f97-87a1-20d4f964cfa9)

This is because `mountain` is not a member of any recognizable structures.

## Caveats

This PR strictly increases the situations in which valid structures may be recognized.

However, it is still the case that encroaching entities that **are** members of some structure template will thwart structure recognition.  One consequence of this is that the order in which structures are completed can matter.

If some partially-built but incomplete structure lies within the bounding box of another candidate structure, the candidate structure will not be recognized as "complete" unless (1) the offending entities are removed first, or (2) the other structure is completed first.
2023-11-20 04:21:14 +00:00
Karl Ostmo
37cae2ac15
Implement entity tags and commands (#1635)
Closes #1631

## Design

* Entities have a new property: a `Set` of textual tags.
* Two new commands are introduced:
    * `HasTag` checks whether a single entity has a given tag
    * `TagMembers` allows cycling through all members with a given tag
* `TagMembers` may be considered more powerful than `HasTag`, so has its own separate capability (`CTagmembers`).
* A map is computed at scenario initialization to facilitate `TagMembers` lookups.
* Tag names are highlighted in yellow in markdown.

## Demo

    scripts/play.sh -i scenarios/Testing/1631-tags.yaml --autoplay

## Other changes

* Incidentally, changed `knownEntities` from a list to a `Set` so that `Set.member` can be used instead of `elem`.
2023-11-20 00:01:46 +00:00
Karl Ostmo
4630e89314
Decouple robot log message colors from entity colors (#1634)
This is code cleanup towards #1415, which will require some refactoring of `Attr.hs`.

## Demo

    scripts/play.sh -i data/scenarios/Testing/9999-message-colors.yaml --autoplay --speed 2

![Screenshot from 2023-11-16 17-25-14](https://github.com/swarm-game/swarm/assets/261693/1fdfa852-1acc-4ff4-93d5-7818ae858f86)
2023-11-17 04:30:39 +00:00
Karl Ostmo
ae535dee3d
achievement for giving to self (#1629)
Closes #1540.

![Screenshot from 2023-11-14 22-28-34](https://github.com/swarm-game/swarm/assets/261693/e1a1cd47-9ddf-4120-aa1a-197b2acb8d49)
2023-11-15 15:57:14 +00:00
persik
a306d05f61
Add TimeEffect effect for getting current time (#1620)
fixes #1502

Add TimeEffect effect for getting current time.
2023-11-15 06:17:09 +00:00
Brent Yorgey
8f52e53a22
Fix the stack build (#1627)
Closes #1624.

Note, however, that currently (until #1623) this will not work on Windows, since I explicitly added `vty-unix` as an `extra-dep` in the `stack.yaml` file.  However, I expect we will get #1623 soon and then we can remove it.
2023-11-15 03:23:23 +00:00
Karl Ostmo
f9ef0094f0
path caching (#1595)
Closes #1569

## Performance

Path cache invalidation upon world modifications (i.e. entities inserted or removed) entails iterating over all of the previously-cached paths [here](https://github.com/swarm-game/swarm/pull/1595/files#r1390158411).  For efficiency's sake, we avoid iterating over "all existing robots".

Any scenario that does not use the `path` command is entirely unaffected by this change.

## Demo

Previously, this demo was virtually unplayable, since when moving between widely-spaced "clusters" of flowers, an expensive A-star search was invoked at almost every tick.  Now, the vast majority of moves utilize the cache, and the demo exhibits minimal stuttering (e.g. when a single A-star search is performed when moving between distant clusters).

    scripts/play.sh -i scenarios/Fun/horton.yaml --autoplay --speed 7

### Event log

An event log specific to the path cache is maintained with its own ring buffer:

    scripts/play.sh -i scenarios/Testing/1569-pathfinding-cache/1569-harvest-batch.yaml --autoplay

 and view http://localhost:5357/paths/log
2023-11-14 21:15:00 +00:00
Chris Hackett
ca1918a443
Update to support running on Windows (#1617)
Added support for running on Windows, in command line or Powershell terminals.

Currently terminal emulators such as mintty, ConEmu, alacritty, etc are not supported.

Addresses issues #1607 and #53.
2023-11-14 20:58:34 +00:00
Karl Ostmo
bd2de6c9b0
structure recognition enhancements (#1618)
* Highlight occurrences of structures in markdown in red
* Add a new `blueprint` entity to provide `floorplan` and `structure` commands
* new `floorplan` command to get the dimensions of a structure
* structure location is southwest instead of northwest corner
* handle structures with "holes"  for statically-recognized structures and when modifying interior cells post-recognition

Each of these is its own commit for reviewability's sake.
2023-11-14 18:08:02 +00:00
persik
aca8049789
Don't auto-complete commands that require God capability outside creative mode (#1619)
fixes #1572
Fixed auto-completion for non-creative mode
2023-11-13 19:01:32 +00:00
Brent Yorgey
3592b4ea6d
Fix REPL type display (#1610)
Make sure it again prints the type on one line, by using `prettyTextLine` instead of `prettyText` to format the type.

Fixes #1597.
2023-11-11 20:47:45 +00:00
Karl Ostmo
17299175d4
use mkReturn in more places (#1615)
`mkReturn` originally introduced in #1588.
2023-11-11 00:36:05 +00:00