How is something being within range computed?

Place to get help with not working mods / modding interface.
jasongraph
Burner Inserter
Burner Inserter
Posts: 6
Joined: Fri Feb 22, 2019 8:19 pm
Contact:

How is something being within range computed?

Post by jasongraph »

Looking at https://lua-api.factorio.com/latest/LuaControl.html, I see the following values relate to reach.

build_distance :: uint [R] The build distance of this character or max uint when not a character or player connected to a character.
drop_item_distance :: uint [R] The item drop distance of this character or max uint when not a character or player connected to a character.
reach_distance :: uint [R] The reach distance of this character or max uint when not a character or player connected to a character.
item_pickup_distance :: double [R] The item pickup distance of this character or max double when not a character or player connected to a character.
loot_pickup_distance :: double [R] The loot pickup distance of this character or max double when not a character or player connected to a character.
resource_reach_distance :: double [R] The resource reach distance of this character or max double when not a character or player connected to a character.

When are each of these used?

By default these values are 10, 10, 10, 1, 2, 2.7 respectively but how are these values used to compute if something is within range? Also how does the game compute 'can_reach_entity(entity)'?


Edit: After doing some testing by making the player walk in specific directions for specific amount of frames, it seems that build_distance, reach_distance, resource_reach distance find what point in an entity's bounding box is closest to the player and checks if the distance between that and the players position is under the value.
Hiladdar
Fast Inserter
Fast Inserter
Posts: 214
Joined: Mon May 14, 2018 6:47 pm
Contact:

Re: How is something being within range computed?

Post by Hiladdar »

Although I do not work on the internal engine and Webe please correct me if I am wrong. Everything in the game has an X and Y coordinate, where the player is, and where the item the player intends to manipulate. Determining the absolute value between the to objects X and Y coordinates between the two objects, one can then calculate the distance between two objects with (X^2 + Y^2)^.5. Once the distance between the two objects is computed, it can be compared against the correct value.

Hiladdar
jasongraph
Burner Inserter
Burner Inserter
Posts: 6
Joined: Fri Feb 22, 2019 8:19 pm
Contact:

Re: How is something being within range computed?

Post by jasongraph »

I'm pretty sure that player's X and Y coordinate and the building's X and Y coordinate are not the points being measured but rather the player's coordinates and the closest coordinate within an entity's bounding box.

Start a new game, craft an iron chest, and you can build it on ( 9.5, 3.5 ) at a distance 10.12422837 tiles away from yourself at ( 0, 0 ). The closest point within the bounding box is (9,3) (if I remember correctly, if not is (9+epsilon,3+epsilon) ) which has distance 9.949874371 from (0,0).
Hiladdar
Fast Inserter
Fast Inserter
Posts: 214
Joined: Mon May 14, 2018 6:47 pm
Contact:

Re: How is something being within range computed?

Post by Hiladdar »

Jasongraph, you are probably right.

Things like buildings, containers can snap to the grid and should be represented as a whole number. At the same time, vehicles, the player character, mines, do not snap to the grid, and may be represented as a decimal. Without getting into the internal guts of the code, I don't know if the distance between two points is rounded up, round down, or if the decimal is truncated. Neither do I know when the adjustments if any are done, pre-processing the distance between two points, during the calculation, or post-processing. For me, knowing that a^2 + b^2 = c^2 is enough, and as long as the calculation remains constant, even if a bit off, is OK, given that this is a game.

As a mod developer it is important to figure this stuff out, especially if the proposed mod does something with object spawning, positioning, or pathing. For the average player, an error of less then 2% calculating something that is about 10 distance from the player, is not going to make that much of a difference.

Hiladdar
Post Reply

Return to “Modding help”