How to get information about map view?

Place to get help with not working mods / modding interface.
Post Reply
jarcionek
Fast Inserter
Fast Inserter
Posts: 137
Joined: Thu Mar 10, 2016 11:26 am
Contact:

How to get information about map view?

Post by jarcionek »

I am writing my first mod. I need to get some information about the map state, such as:
- is map open
- what is the current map position
- what is the in-game position of where the mouse cursor is pointing

I understand that some of these might not be doable (e.g. the last one), but I cannot find any way to get even the first one.

I can see the LuaGuiElement with some properties such as position, but I cannot find a way to get that object.

Could you please let me know if any of the above is doable and if so, point me in the right direction?

Bilka
Factorio Staff
Factorio Staff
Posts: 3130
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: How to get information about map view?

Post by Bilka »

jarcionek wrote:
Sat Mar 23, 2019 11:43 pm
- is map open
https://lua-api.factorio.com/latest/Lua ... ender_mode is what you are looking for
- what is the current map position
Not readable because it is not deterministic.
- what is the in-game position of where the mouse cursor is pointing
The closest you can get to this is https://lua-api.factorio.com/latest/Lua ... l.selected, however that only works if an entity is currently being selected. Depending on your use case there might be workarounds, so if you are willing to share that we might be able to help further.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

jarcionek
Fast Inserter
Fast Inserter
Posts: 137
Joined: Thu Mar 10, 2016 11:26 am
Contact:

Re: How to get information about map view?

Post by jarcionek »

Bilka wrote:
Sun Mar 24, 2019 8:56 am
- what is the current map position
Not readable because it is not deterministic.
What do you mean? Deterministic means non-random. The camera position on the map is not random. It is precisely to what the player set it to (by using WSAD, dragging, zooming) and doesn't change on its own.
Bilka wrote:
Sun Mar 24, 2019 8:56 am
- what is the in-game position of where the mouse cursor is pointing
The closest you can get to this is https://lua-api.factorio.com/latest/Lua ... l.selected, however that only works if an entity is currently being selected. Depending on your use case there might be workarounds, so if you are willing to share that we might be able to help further.
Thanks. I am trying to work around a lack of sensitivity of the mouse wheel for zooming in/out (source code). The problem is that I cannot zoom in from the map to the specific location easily. For zooming out while staying in "zoom to world" I could probably just change player.zoom instead of calling player.zoom_to_world(), however if I wanted to zoom out while changing the render more from "zoom to world" to "map", this is also problematic as I don't have position.
Screenshot.PNG
Screenshot.PNG (57.61 KiB) Viewed 3196 times

jarcionek
Fast Inserter
Fast Inserter
Posts: 137
Joined: Thu Mar 10, 2016 11:26 am
Contact:

Re: How to get information about map view?

Post by jarcionek »

Another solution that I was just poking around is that I could define 2 keys and assign them as mouse wheel up and mouse wheel down, and change the zoom level myself. This would allow me to define sensitivity. This would work perfectly while render mode is "player" or "zoom to world", however it would not allow me to switch render mode between "zoom to world" and "map". Also, changing game.player.zoom while in "map", doesn't change the zoom.

Bilka
Factorio Staff
Factorio Staff
Posts: 3130
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: How to get information about map view?

Post by Bilka »

jarcionek wrote:
Sun Mar 24, 2019 10:13 am
Bilka wrote:
Sun Mar 24, 2019 8:56 am
- what is the current map position
Not readable because it is not deterministic.
What do you mean? Deterministic means non-random. The camera position on the map is not random. It is precisely to what the player set it to (by using WSAD, dragging, zooming) and doesn't change on its own.
Player inputs aren't deterministic. The position depends on player input. For it to be deterministic, we would have to save it, or save the player input (to then be able to get the position from that). Both of these things are not feasible because it is a lot of data to save, for nearly no gain.

Since your end goal seems to be to simply zoom in and out, would a function to only set the scale of the map view be what you are looking for? This would be possible for me to implement.

I must ask though, how are you dealing with the fact that player zoom and map scale are not readable?
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

jarcionek
Fast Inserter
Fast Inserter
Posts: 137
Joined: Thu Mar 10, 2016 11:26 am
Contact:

Re: How to get information about map view?

Post by jarcionek »

Bilka wrote:
Sun Mar 24, 2019 10:47 am
Player inputs aren't deterministic. The position depends on player input. For it to be deterministic, we would have to save it, or save the player input (to then be able to get the position from that). Both of these things are not feasible because it is a lot of data to save, for nearly no gain.
Would it be possible for me to attach multiple event handlers too see and analyse user's input and calculate the map position myself?
Bilka wrote:
Sun Mar 24, 2019 10:47 am
Since your end goal seems to be to simply zoom in and out, would a function to only set the scale of the map view be what you are looking for? This would be possible for me to implement.
That would solve one of the two existing problems. The problem that would remain is to change between "zoom to world" and "map" (or vice versa), without changing position.
Bilka wrote:
Sun Mar 24, 2019 10:47 am
I must ask though, how are you dealing with the fact that player zoom and map scale are not readable?
I am not sure if I understood your question correctly? I don't read the zoom level, I set it to what player defined in the mod settings beforehand.

Bilka
Factorio Staff
Factorio Staff
Posts: 3130
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: How to get information about map view?

Post by Bilka »

jarcionek wrote:
Sun Mar 24, 2019 10:54 am

Would it be possible for me to attach multiple event handlers too see and analyse user's input and calculate the map position myself?
No, lua doesn't have access to non-deterministic things such as direct user input. As you already found out, the most you can have are hotkeys, which still doesnt give you mouse position etc.
The problem that would remain is to change between "zoom to world" and "map" (or vice versa), without changing position.
There is probably a way to make a lua api function that zooms into world at the current position. Currently I have different priorities so I can't really spend time on writing it, so please make a modding interface request for it so that I don't forget. In the meantime you will have to find some kind of workaround.
I am not sure if I understood your question correctly? I don't read the zoom level, I set it to what player defined in the mod settings beforehand.
My response was based on
Another solution that I was just poking around is that I could define 2 keys and assign them as mouse wheel up and mouse wheel down, and change the zoom level myself. This would allow me to define sensitivity.
which I understood as "I am replacing the zoom in and out logic with my own logic".
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

jarcionek
Fast Inserter
Fast Inserter
Posts: 137
Joined: Thu Mar 10, 2016 11:26 am
Contact:

Re: How to get information about map view?

Post by jarcionek »

Bilka wrote:
Sun Mar 24, 2019 11:12 am
jarcionek wrote:
Sun Mar 24, 2019 10:54 am

Would it be possible for me to attach multiple event handlers too see and analyse user's input and calculate the map position myself?
No, lua doesn't have access to non-deterministic things such as direct user input. As you already found out, the most you can have are hotkeys, which still doesnt give you mouse position etc.
This sounds really weird. How would the application know what to display when the map is open, if it didn't have coordinates? If I open a map and hold D, why do I see an eastern part of the map, rather than northern? Why do I see anything at all? You are already capturing the position of the map, in one form or another. The only question is how easy it would be to make it modding-friendly?


Bilka wrote:
Sun Mar 24, 2019 11:12 am
jarcionek wrote:
Sun Mar 24, 2019 10:54 am
I am not sure if I understood your question correctly? I don't read the zoom level, I set it to what player defined in the mod settings beforehand.
My response was based on
Another solution that I was just poking around is that I could define 2 keys and assign them as mouse wheel up and mouse wheel down, and change the zoom level myself. This would allow me to define sensitivity.
which I understood as "I am replacing the zoom in and out logic with my own logic".
Ah, this one. That would be just changing game.player.zoom. Multiplying or dividing existing value by a constant (sensitivity), right?



By the way, if you really want to implement something, how about making sensitivity of the mouse wheel configurable and adding a setting at which zoom level the map changes between zoom to world and map?

All I am really trying to do here is revert the behaviour introduced in 0.15.11 and 0.15.12 (issue raised):
The zoom level at which the map switches from 'map view' to 'world view' was increased.
Zooming with the mousewheel in the map and zoom-to-world is less aggresive.
These changes made me stop playing the game. 2 years later, after reading about all the cool stuff you have done, I gave it another try, but after 200h on a single map, all I am doing is scrolling up and down to inspect different areas of my factory and trying to align huge blueprints with existing structures (which takes ages because I cannot see more of a world).

Bilka
Factorio Staff
Factorio Staff
Posts: 3130
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: How to get information about map view?

Post by Bilka »

jarcionek wrote:
Sun Mar 24, 2019 11:28 am
This sounds really weird. How would the application know what to display when the map is open, if it didn't have coordinates? If I open a map and hold D, why do I see an eastern part of the map, rather than northern? Why do I see anything at all? You are already capturing the position of the map, in one form or another. The only question is how easy it would be to make it modding-friendly?
The game of course has access to it. Lua is not the game engine, and it has to be deterministic. I think you don't quite understand the concept of what determinism means for Factorio. Not deterministic means: In multiplayer, your game does not know at what position some other player has the map open and how zoomed in they are, because that information is not transferred between the different parties. It does not need to know it, it would just be useless data. To "make something deterministic" the information has to be transferred -> the inputs are transferred, or rather in most cases the resulting action is transferred, like placing an entity etc. The replay that you can enable when playing saves exactly those actions and then simply replays them when you watch it. Lua has to be deterministic because: Anything that a mod does has to be the same for all players (anything else would be a desync), so it cannot have access to information that not all players have access to.

To make it "modding friendly" as you say, whatever data you are looking for would need to be deterministic, or be made deterministic. In this case it is not deterministic currently, as explained above, and adding an action for every single movement and zooming on the map would be a lot of data to transfer (and save for replays), which is completely unused in the base game, so it is not feasible.
Ah, this one. That would be just changing game.player.zoom. Multiplying or dividing existing value by a constant (sensitivity), right?
You cannot multiply or divide the zoom, you can only write a number to it. You cannot read player.zoom. This is why I asked how you were going to deal with exactly that.
By the way, if you really want to implement something, how about making sensitivity of the mouse wheel configurable and adding a setting at which zoom level the map changes between zoom to world and map?
This is beyond a modding interface request, it is more of balancing change, so not something that I can easily implement. Suggestions like these have a better place in viewforum.php?f=6. Or just use a mod that allows you to zoom out in the normal view.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

jarcionek
Fast Inserter
Fast Inserter
Posts: 137
Joined: Thu Mar 10, 2016 11:26 am
Contact:

Re: How to get information about map view?

Post by jarcionek »

I see. Thanks for the explanation.
Ah, this one. That would be just changing game.player.zoom. Multiplying or dividing existing value by a constant (sensitivity), right?
You cannot multiply or divide the zoom, you can only write a number to it. You cannot read player.zoom. This is why I asked how you were going to deal with exactly that.
Since I would be replacing vanilla zooming logic, I can store the value myself. However, currently I can only deal with player render mode (I know that the game starts with zoom value 1, the rest is easy). I know that the map opens at player position with zoom level 0.2, however I cannot change the zoom while in "map" view (render_mode.chart) and I cannot switch the render mode to "zoom to world" (render_mode.chart_zoomed_in) without changing position.
By the way, if you really want to implement something, how about making sensitivity of the mouse wheel configurable and adding a setting at which zoom level the map changes between zoom to world and map?
This is beyond a modding interface request, it is more of balancing change, so not something that I can easily implement. Suggestions like these have a better place in viewforum.php?f=6. Or just use a mod that allows you to zoom out in the normal view.
I did raise is, multiple times in different places, including the category you mentioned. But no one's interested... :(

jarcionek
Fast Inserter
Fast Inserter
Posts: 137
Joined: Thu Mar 10, 2016 11:26 am
Contact:

Re: How to get information about map view?

Post by jarcionek »

I have found a workaround for getting the position of the map.

I can create a custom capsule item (just like the artillery remote) and then listen to on_player_used_capsule event, which will give me the position where user clicked (because it can be used in the map view).

Slightly inconvenient, but with a keyboard shortcut to create and put that item in player's hand and some auto destruct option so it never pollutes the inventory, it might be good enough.

Post Reply

Return to “Modding help”