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

Check loaded mapgen for being used at all. #37425

Merged
merged 27 commits into from
Mar 9, 2020
Merged

Conversation

BevapDin
Copy link
Contributor

@BevapDin BevapDin commented Jan 26, 2020

SUMMARY: None

This refactors the oter_mapgen variable a bit. It merges the oter_mapgen_weights into it, so we do not have to look up the entry in two maps.

It uses weighted_int_list instead of keeping yet another implementation of the same algorithm.


Finally it adds checks for the loaded mapgen instances to be used. As it turns out, some are not used at all:

  • farm_dairy_twd_second_floor_1
  • farm_dairy_twd_second_floor_10
  • farm_dairy_twd_second_floor_11
  • farm_dairy_twd_second_floor_2
  • farm_dairy_twd_second_floor_7
  • farm_dairy_twd_second_floor_8
  • farm_dairy_twd_second_floor_9
  • house_crack1
  • house_crack1_roof
  • house_crack2
  • house_crack3
  • house_crack3_roof
  • microlab_generic_edge_edge

The game will now issue debug messages for each of those mapgen instances, but it will continue normally.

Do not merge right now (therefor I added the WIP tag). Fix the usage of the mentioned mapgen instances first: either add a usage (e.g. by adding a matching overmap terrain) or remove the mapgen instance.

@curstwist curstwist mentioned this pull request Jan 27, 2020
@curstwist
Copy link
Contributor

curstwist commented Jan 27, 2020

#37428 will take care of the houses. I'll let the other creators fix their maps.

will this search nested chunks as well?

@John-Candlebury
Copy link
Member

#37458 Fixed the wrong microlab entry.

@BevapDin
Copy link
Contributor Author

will this search nested chunks as well?

No.Those are stored in a different variable.

@I-am-Erk I-am-Erk added [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Code: Tests Measurement, self-control, statistics, balancing. Map / Mapgen Overmap, Mapgen, Map extras, Map display labels Jan 28, 2020
@BevapDin BevapDin force-pushed the lib branch 2 times, most recently from dc55200 to 5867f40 Compare February 1, 2020 21:38
src/mapgen.cpp Outdated
* @p hardcoded_weight Weight for an additional entry. If that entry is chosen,
* a null pointer is returned. If unsure, just use 0 for it.
*/
mapgen_function *pick( const int hardcoded_weight ) const {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this function return a constant pointer? The function itself is marked const, but its result points at the class internals which can be changed through it.

Suggested change
mapgen_function *pick( const int hardcoded_weight ) const {
const mapgen_function *pick( const int hardcoded_weight ) const {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it should. But mapgen_function::generate is not const either, so you would have to cast the pointer anyway in order to call that function. And calling generate is basically the only thing one will do with that pointer.

Copy link
Contributor

@codemime codemime Feb 2, 2020

Choose a reason for hiding this comment

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

generate should probably be marked const either. It's meant to operate on mapgendata and shouldn't mutate its internal state. Anyways, a const function which returns a non-const pointer to its internals is suspicious at the very least.

src/mapgen.cpp Show resolved Hide resolved
src/mapgen.cpp Outdated Show resolved Hide resolved
/**
* Calls @ref mapgen_function::setup and sets up the internal weighted list using
* the **current** value of @ref mapgen_function::weight. This value may have
* changed since it was first added, so this is needed to recalculate the weighted list.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please elaborate on this. Why would weight change after adding to the container? Maybe it should take ownership of the mapgen_function's themselves (instead of possessing mere pointers) and protect its own guts form external interference? The current scheme seems to be rather fragile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why would weight change after adding to the container?

When erase at line 209 is called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

instead of possessing mere pointer

Nah, pointers are exposed e.g. via load_mapgen_function. If I read the code correctly, this can be used to connect one mapgen instance with multiple overmap terrains (equivalent to giving each overmap terrain a copy of the mapgen). It seems like the mapgen in "lab/lab_trains.json" make use of that.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, thanks. It would be ideal to refrain form int indexes at some point and use persistent iterators instead. Resetting weight to 0 in a temporary container as a sign of an item being erased, and then not being able to operate on indexes after invocation of setup looks, well, a bit hackish.

If you still decide to stick to this approach, it would be nice to issue a debugmsg in case someone tries to call add or erase after setup.

Just get the value and let the JSON system deal with the existence check and the error message.

Also throw errors via the JSON system so it creates a nice message with error location.
Basically the whole look up of the mapgen function pointer within the `oter_mapgen` and so on.
This is mostly done to avoid having to expose `oter_mapgen` in the header.
Those variables are already declared in a header or not even used within this cpp file.
Again: this is to avoid having to expose `oter_mapgen` in the header.
Merges the comments from the header with the comments in the cpp file for the very same objects.
Those instances can have their own members and stuff.
Instead of having a separate map, this reuses the already existing entries in `oter_mapgen`.
… into the vector itself.

This avoids the repeated look up within the vector and it avoids a potential error situation: the index taken from `weight_` could be invalid.
We already have this class, so we should make use of it.
…stency

Note: the function name is chosen for consistency (no pun), our other consistency checking function are named that way.
No need to expose them.

The last direct access to the vector had to be wrapped within a new member function.
And move all the functions that deal with it into that class.
This will find mapgen instances that were loaded from JSON, but have no usage within the game (e.g. because the belong to undefined overmap terrain).

Register some mapgen ids that are only used within the C++ code as used
…ctory`

Don't expose them at all. All access to them is done internally.
This terrain should never appears and is therefor never generated. Removing it here may free up some memory.
@BevapDin BevapDin changed the title [WIP] Check loaded mapgen for being used at all. Check loaded mapgen for being used at all. Feb 2, 2020
Adds the default value to all calls to it that did not supply a value.
This feature is not available anymore.

It was added by 04e45ea without any explanation.
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 Code: Tests Measurement, self-control, statistics, balancing. Map / Mapgen Overmap, Mapgen, Map extras, Map display
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants