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

Control laptop activity enhancement #28465

Conversation

prutschman
Copy link
Contributor

Summary

SUMMARY: Infrastructure "Allow a player_activity to refer to specific monster(s)"
SUMMARY: Content "Control laptop activity now keeps track of target"

Purpose of change

When #27816 turned hacking into an activity (yay!), it necessitated choosing a target at the end, since player_activity can't reference a monster. Now it can. The player selects a target, just as was the case prior to e089877, but this is now stored in the player_activity.

Describe the solution

Adds a list of monsters to player_activity, allowing activities to refer to monsters, at least as long as they continue to exist.

player_activity gains a member monsters, stored as a vector<weak_ptr<monster>>.
Because monsters don't have unique IDs, serialization/deserialization is added for weak_ptr<monster> using the last known location to refer to the monster. To allow for a future possible transition to a different reference mechanism, the location is serialized as a json object {"monster_at": [x, y, z] } instead of just the raw integer.

Describe alternatives you've considered

player does have a unique ID. This could in principle be hoisted into the Creature superclass. If that happens, the serialization can be changed to emit { "unique_id": <integer> } instead.

Additional context

This supersedes pull request #27954.

Serialize weak_ptr<monster> as a temporary critter tracker index

Serialize a monster reference as an object indicating ref type

Use position rather than temp_id to serialize a monster reference

serialize weak_ptr<monster> using location data
@ifreund ifreund added Items / Item Actions / Item Qualities Items and how they work and interact Mechanics: Character / Player Character / Player mechanics [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style labels Mar 3, 2019
@ifreund ifreund removed the 0.D Freeze label Mar 8, 2019
@kevingranade
Copy link
Member

        ./cataclysm(_Z21debug_write_backtraceRSo+0x25) [0x782371]
        ./cataclysm() [0x775bca]
        ./cataclysm() [0x775dcb]
        /lib64/libc.so.6(+0x347e0) [0x7f798c4297e0]
        ./cataclysm(_ZNK5mtype10in_speciesERK9string_idI12species_typeE+0x17) [0xa8f959]
        ./cataclysm(_ZN4iuse12robotcontrolEP6playerP4itembRK8tripoint+0x2a1) [0x910ad7]
        ./cataclysm(_ZNK21iuse_function_wrapper3useER6playerR4itembRK8tripoint+0x16) [0x8e2898]
        ./cataclysm(_ZNK5itype6invokeER6playerR4itemRK8tripointRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x130) [0x8f6346]
        ./cataclysm(_ZN6player11invoke_itemEP4itemRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERK8tripoint+0xae) [0xb960fc]
        ./cataclysm(_ZN6player11invoke_itemEP4itemRK8tripoint+0x45) [0xba1923]
        ./cataclysm(_ZN6player3useE13item_location+0x182) [0xba4648]
        ./cataclysm(_ZN6player3useEi+0x35) [0xba46eb]
        ./cataclysm(_ZN4game8use_itemEi+0x21a) [0x80ced0]
        ./cataclysm(_ZN4game13handle_actionEv+0x1d9c) [0x8524c8]
        ./cataclysm(_ZN4game7do_turnEv+0x3a3) [0x81d4e1]
        ./cataclysm(main+0x1152) [0x66da9b]
        /lib64/libc.so.6(__libc_start_main+0xea) [0x7f798c41602a]
        ./cataclysm(_start+0x2a) [0x6933ca]

Seems to be occurring here
https://github.com/CleverRaven/Cataclysm-DDA/pull/28465/files#diff-74bb173f819041f82ce1da375b45569cR5376

@prutschman
Copy link
Contributor Author

I'll look into it. I'm currently not able to reproduce. Do you have a save-game I could try? Did you do anything special to trigger it?

I just now started a new random game, set skills to 10, wished for a laptop+batteries, spawned a chicken walker, and was able to hack it.

My local build shows 0.C-37641-g262644def3-dirty (the dirty is only because astyle thinks boots.json is checked in with invalid style, so I had to run astyle before I could build).

@kevingranade
Copy link
Member

All I did was spawn, give myself a tent to hide in, spawn a chicken walker nearby and try to hack it.

@prutschman
Copy link
Contributor Author

prutschman commented Mar 11, 2019

I develop on macOS. I just tried it on linux, and I can reproduce your crash there. I'll investigate.

@prutschman
Copy link
Contributor Author

prutschman commented Mar 13, 2019

I'm leaning toward compiler bug on this one. This is a very very stripped-down model of the line in question, with abstracted versions of the non_dead_range business.

https://wandbox.org/permlink/jSGfrHzmzFs4tmd7

It assert fails in gcc 7.3 (default on Ubuntu Bionic), but not in 8.2 or later. It also works at least as far back as clang 6.0.1.

From everything I've read, this should work due to lifetime extension rules. But, clearly it doesn't in some cases in practice.

Anyhow, the following works as a workaround:

            const auto &known_mons = g->all_monsters();
            for( std::weak_ptr<monster> candidate_wp : known_mons.items ) {

I'm accessing the items member directly because non_dead_range<T>::iterator "forgets" the smart_ptr when you deference it, and that's what i'm looking to get.

I could also do something like (untested):

for(const monster&m : g->all_monsters()) {
    shared_ptr<monster> m_sptr = g->critter_tracker.find(m->pos());
...

Preferences?

@prutschman
Copy link
Contributor Author

I just discovered g->shared_from. I'll use that.

@kevingranade kevingranade merged commit 15b9287 into CleverRaven:development Mar 14, 2019
kevingranade added a commit that referenced this pull request Mar 14, 2019
* origin/development:
  Control laptop activity enhancement (#28465)
@prutschman prutschman deleted the control-laptop-activity-enhancement branch September 28, 2019 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Items / Item Actions / Item Qualities Items and how they work and interact Mechanics: Character / Player Character / Player mechanics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants