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

JSON-ify sleep-affecting mutations, trying to sleep, and sleep comfort #75852

Merged
merged 17 commits into from
Aug 25, 2024

Conversation

phi2dao
Copy link
Contributor

@phi2dao phi2dao commented Aug 21, 2024

Summary

Infrastructure "JSON-ify sleep-affecting mutations"

Purpose of change

Several mutations change where a character can try to sleep. This is hard coded into those mutations. Accordingly, these mutations cannot be changed, nor can new sleep-affecting mutations be added, without editing code.

Describe the solution

Added an optional comfort field to traits, which is a list of comfort_data. comfort_data consists of:

  • A list of conditions used to evaluate a location
  • The comfort of that location if the conditions are true
  • Several modifiers for mutations that take 'normal' comfort into account or can use sleep aids
  • A message to print when a character tries to sleep in a location where the conditions are true
  • A message to print after the previous message, currently used by post-threshold spiders to suggest spinning a web first.
  • A message to print when a character actually falls asleep in that location (not currently used by any mutations)

When a character tries to sleep, all of their traits are checked for comfort data. Within a trait, the first comfort data with true conditions is chosen. If multiple traits choose comfort data, the comfort data with the worst base comfort is chosen. This is so "impossible" comfort levels, such as experienced by a post-threshold spider sleeping without a web, are prioritized.

The resulting comfort is cached so the handful of functions that use comfort and related information can fetch it without having to go back through all the comfort data. This cache is invalidated if time passes or the location changes.

Originally, I used dialogue conditions to evaluate locations, but I found myself using character-focused conditions such as u_underwater instead of location-focused conditions like map_terrain_has_flag. This was a problem, as NPCs evaluate the comfort of multiple locations around them to decide where to sleep, and character-focused conditions would result in not actually evaluating those locations.

Accordingly, I took a page from climbing aids' book and rolled my own conditions. They allow checking terrain, furniture, traps, fields, vehicles, character flags, and traits. With the exception of traps, all are used by at least one of the existing sleep-affecting mutations. I added traps because traps can affect comfort in the form of the bed roll, cot, and similar.

This solution has not changed the behavior of any mutations, with the following exceptions:

  • Plants (CHLOROMORPH) can sleep on all PLOWABLE and PLANTABLE terrain, not just t_dirt, t_grass, t_dirtmound, t_pit, and t_pit_shallow. This can be changed back if desired.
  • Gastropods (SHELL2 and SHELL3) can use sleep aids while in their shell and in a vehicle. They previously couldn't, but could use sleep aids when in their shell and outside of a vehicle. There was even a comment saying that they could still use sleep aids, so this seemed like a bug.
  • Aqueous Repose no longer gives an extra 10 comfort on top of its base 10 comfort. WATERSLEEP was being checked in both Character::base_comfort_value and Character::sleep_spot and added 10 comfort in both places. This seemed unintentional.
  • Aquatic Sleeper no longer allows characters to try and sleep in deep water. Previously, all it did was bypass the "You cannot sleep while swimming." message; it did not actually affect comfort, and thus your ability to actually fall asleep, in any way. I'm not actually sure how Aquatic Sleeper is supposed to differ from Aqueous Repose, so I just left it comfort_data-less.
  • Sleeping in an uncomfortable location prints a message describing the location. Previously, certain mutation/situation combinations prevented this message from printing. I don't think it's a problem if a plant hears that the rock floor they are sleeping on is uncomfortable.

Describe alternatives you've considered

Adding a WATERSLEEP flag. That's what I did initially, then I made the mistake of thinking that creating a more robust solution "wouldn't be that bad".

Testing

I debug mutated each sleep-affecting trait one at a time and tried sleeping in all conditions that would produce a distinct result. For example, for Aqueous Repose I tried sleeping on land, in deep water, and in swimmable terrain. When I got a result that didn't match the hardcoding, I fixed the code or json until it did. As of my most recent test, all sleep-affecting mutations behave as they used to, save for the above-mentioned exceptions.

Additional context

I somehow missed the "keep your PR small" advice until right now, so, uh, sorry. Almost all the new code is in sleep.h and sleep.cpp if that helps.

Also, I'm pretty sure there's nothing actually stopping a character from trying to fall asleep in mid-air (via the LEVITATION flag or the like). I didn't actually check, and didn't add any code to prevent that, but I thought it was pretty funny. I can fix it with another PR if desired.

There are also a bunch of hardcoded things that occur while a character is sleeping, some of which can probably be moved to enchantments. I didn't touch those either.

@github-actions github-actions bot added <Documentation> Design documents, internal info, guides and help. NPC / Factions NPCs, AI, Speech, Factions, Ownership [JSON] Changes (can be) made in JSON Mods Issues related to mods or modding Mutations / Traits / Professions/ Hobbies Mutations / Traits / Professions/ Hobbies [C++] Changes (can be) made in C++. Previously named `Code` [Markdown] Markdown issues and PRs EOC: Effects On Condition Anything concerning Effects On Condition Mods: Xedra Evolved Anything to do with Xedra Evolved labels Aug 21, 2024
@github-actions github-actions bot requested a review from Maleclypse August 21, 2024 02:41
@github-actions github-actions bot added json-styled JSON lint passed, label assigned by github actions astyled astyled PR, label is assigned by github actions labels Aug 21, 2024
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Aug 22, 2024
@Maleclypse
Copy link
Member

I'm going to try and test this tonight or over the weekend and then merge it :)

@Maleclypse Maleclypse merged commit 0d9bfb5 into CleverRaven:master Aug 25, 2024
27 checks passed
@Maleclypse
Copy link
Member

Thank you for your contribution!

@phi2dao phi2dao deleted the jsonify_sleep branch November 22, 2024 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style <Documentation> Design documents, internal info, guides and help. EOC: Effects On Condition Anything concerning Effects On Condition [JSON] Changes (can be) made in JSON json-styled JSON lint passed, label assigned by github actions [Markdown] Markdown issues and PRs Mods: Xedra Evolved Anything to do with Xedra Evolved Mods Issues related to mods or modding Mutations / Traits / Professions/ Hobbies Mutations / Traits / Professions/ Hobbies new contributor NPC / Factions NPCs, AI, Speech, Factions, Ownership
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants