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

casting a spell casts the wrong spell #76137

Merged
merged 6 commits into from
Sep 5, 2024

Conversation

ThePotatomancer
Copy link
Contributor

@ThePotatomancer ThePotatomancer commented Sep 2, 2024

currently selecting a spell in a long sorted spell list will cast the wrong spell due to select spell returning a sorted spell index while handle_action.cpp looks only at the unsorted spells

Summary

Bugfixes "Fix issues with the spell menu where choosing a spell to cast chooses the wrong one"

Purpose of change

fixes #76042 (and duplicate #76192)
makes is so the spell a player selected is cast.
reproduction:

  1. make a world with magicalysm or mind over matter installed
  2. make a character with some stating spells (I used dimensional mage with ascended teleporter proficiency)
  3. open a spell list, and try to select a spell to cast (any method)
  4. look at log and confirm what spell was actually cast

Describe the solution

select_spell will now returns the selected spell instead of an index, making the internal sorting of the function irrelevant for handle_action.cpp

Describe alternatives you've considered

returning spell_id directly from the uilist instead of returning an index and converting to a spell. but I am not sure if spell_id is always int based, and there is no easy way I saw to convert an int to a string_id even if there is an easy way to convert it back to int from one, and returning a spell is more convenient.

Testing

The steps detailed in purpose of change, casting multiple spells, using hotkeys and mouse clicks. Also checking that casting spells works as expected when changing spell hotkeys.
enter spell menu and exit it without casting a spell (by pressing esc for example).

Additional context

the wrong spell to be cast
currently selecting a spell in a long sorted spell
list will cast the wrong spell due to select spell returing a sorted
spell id while handle_action.cpp looks only at the unsorted spells

fixes CleverRaven#76042
Fix an issue where trying to cast a spell causes
the wrong spell to be cast
@github-actions github-actions bot added the [C++] Changes (can be) made in C++. Previously named `Code` label Sep 2, 2024
@github-actions github-actions bot requested a review from KorGgenT September 2, 2024 06:08
@github-actions github-actions bot added <Bugfix> This is a fix for a bug (or closes open issue) json-styled JSON lint passed, label assigned by github actions labels Sep 2, 2024
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@github-actions github-actions bot added the astyled astyled PR, label is assigned by github actions label Sep 2, 2024
@CLIDragon
Copy link
Contributor

get_spell_index can be replaced by std::find, e.g.

original_spell_index = std::find(known_spells_sorted.begin(), known_spells_sorted.end(), selected_spell->id() )

Might be neater than creating a new function.

@ThePotatomancer
Copy link
Contributor Author

ThePotatomancer commented Sep 2, 2024

@CLIDragon std::find return the element in question instead of the index from what I understood? In this case we need the index since the access to the element happens elsewhere (in handle action). I guess another possibility I haven't considered is changing the return type of select_spell to the actual spell/spell_id instead of the index. I might actually do that instead since it would be cleaner and more robust, just need to make sure no one besides handle_action uses select_spell

@CLIDragon
Copy link
Contributor

@CLIDragon std::find return the element in question instead of the index from what I understood? In this case we need the index since the access to the element happens elsewhere (in handle action).

It returns an iterator which points to the location of the spell in the list. You can convert that to an index with std::distance if necessary, but I assumed (bad, I know) that the index was just being used to find an element, in which case an iterator would work equally as well.

@ThePotatomancer ThePotatomancer marked this pull request as draft September 2, 2024 07:52
@ThePotatomancer
Copy link
Contributor Author

the reason I put it on draft is that I found a bug (when exiting spell menu without casting a spell, aka when selected id is negative). currently fixing along with the improvement of changing the return type of select spell instead of using the get_spell_index function

@ThePotatomancer ThePotatomancer marked this pull request as ready for review September 2, 2024 12:15
@ThePotatomancer
Copy link
Contributor Author

took some effort due to my rusty cpp, but got it to where I wanted. Works with the new test and all the previous ones, and is much much cleaner and more error proof (since we now return the exact spell instead of a disconnected index).

@github-actions github-actions bot removed the astyled astyled PR, label is assigned by github actions label Sep 2, 2024
local astyle somehow missed those

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@github-actions github-actions bot added the astyled astyled PR, label is assigned by github actions label Sep 2, 2024
ThePotatomancer added a commit to ThePotatomancer/Cataclysm-DDA that referenced this pull request Sep 2, 2024
list. Remove broken color tags in spells
Currently the new ui does not display spell
components, and there are colors tags in spell data that are ignored

resolves CleverRaven#75669 and CleverRaven#76137
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Sep 3, 2024
Maleclypse pushed a commit that referenced this pull request Sep 4, 2024
* show spell components again in the new imGui spell
list. Remove broken color tags in spells
Currently the new ui does not display spell
components, and there are colors tags in spell data that are ignored

resolves #75669 and #76137

* colors regression in spell info will be fixed
will use correct enery type when displaying "not enough"
fixing spell list regression from imgui uilist
update

* removed unused properties of spellcasting_callback

* fix warnings in magic.cpp
@Maleclypse Maleclypse merged commit 5d5ef55 into CleverRaven:master Sep 5, 2024
24 of 29 checks passed
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 <Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code` json-styled JSON lint passed, label assigned by github actions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Spell casting menu will always choose the 1st option
3 participants