Detecting resources available to a drill

Place to get help with not working mods / modding interface.
trold
Inserter
Inserter
Posts: 45
Joined: Fri Jul 31, 2015 1:07 pm
Contact:

Detecting resources available to a drill

Post by trold »

I have written a mod called Bottleneck that adds lights to different entities depending on their status. For mining drills, this shows:
  • green light when the drill is running
  • yellow when it's idle because it cannot output the mined ore
  • red light when the resource is depleted, or a burner drill is out of fuel
I am currently detecting the red case with

Code: Select all

if entity.mining_target == nil
, but this has been reported in rare cases to give false positives. I would rather avoid having to do a find_entities search for available resources around each drill, due to efficiency concerns, since this check is fairly frequent. I could add a listener for "on_resource_depleted", but this would turn mining drills into a strange special case. Is there a simpler way to detect whether a mining drill is depleted?
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Detecting resources available to a drill

Post by aubergine18 »

Well, it will automatically stop functioning when depleted, so you could potentially check the .active state or it's .power_usage, etc?
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
trold
Inserter
Inserter
Posts: 45
Joined: Fri Jul 31, 2015 1:07 pm
Contact:

Re: Detecting resources available to a drill

Post by trold »

aubergine18 wrote:Well, it will automatically stop functioning when depleted, so you could potentially check the .active state or it's .power_usage, etc?
Unfortunately, neither of those would work. The problem is distinguishing between idle states. The drill remains "active", even when the resource is depleted, and it isn't drawing any power when it is output congested.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Detecting resources available to a drill

Post by aubergine18 »

Does the mining machine output any signals relating to quantity of resources it has remaining? If so those could be queried by script...?
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2173
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Detecting resources available to a drill

Post by Ranakastrasz »

Well.....

The brute force solution, (also known as the worst solution) is that, assuming you can access the mining radius, is to just check the resource values of each tile it is in range of.

Expensive, naturally.
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
trold
Inserter
Inserter
Posts: 45
Joined: Fri Jul 31, 2015 1:07 pm
Contact:

Re: Detecting resources available to a drill

Post by trold »

aubergine18 wrote:Does the mining machine output any signals relating to quantity of resources it has remaining? If so those could be queried by script...?
The patch an entity is currently mining is given by entity.mining_target. This is nil, when the resource is depleted, but in some rare cases, it can also be nil if it has just depleted a single square, even though there are other resources around. This would normally be resolved on the next tick, but if the drill cannot offload its newly mined material, it might stay in this state for a while.
trold
Inserter
Inserter
Posts: 45
Joined: Fri Jul 31, 2015 1:07 pm
Contact:

Re: Detecting resources available to a drill

Post by trold »

Ranakastrasz wrote:Well.....

The brute force solution, (also known as the worst solution) is that, assuming you can access the mining radius, is to just check the resource values of each tile it is in range of.

Expensive, naturally.
That is exactly the "find_entities search" that I mentioned that I wanted to avoid...
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5267
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Detecting resources available to a drill

Post by Klonan »

You could check the mining progress of the drill,

/c game.print(game.player.selected.mining_progress)

If you check over 2 ticks, and the value is the same, then you know that it cannot output (or that it has no power/ is disconnected)
trold
Inserter
Inserter
Posts: 45
Joined: Fri Jul 31, 2015 1:07 pm
Contact:

Re: Detecting resources available to a drill

Post by trold »

Klonan wrote:You could check the mining progress of the drill,

/c game.print(game.player.selected.mining_progress)

If you check over 2 ticks, and the value is the same, then you know that it cannot output (or that it has no power/ is disconnected)
That doesn't distinguish between "red" and "yellow" states, as described above. It would simply say "isn't currently running". This is actually how I am currently distinguishing between "yellow" and "green".
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Detecting resources available to a drill

Post by aubergine18 »

Sounds like you need a surface.count_resources_filtered() method or a .is_depleted flag on mining machines.

Currently your best bet is probably to detect when mining_target _becomes_ nil, and on that transition search for resources in given area prior to setting red state. The red state would act as a cache - once set, the only way to unset it (for a mining machine) is to destroy the machine.

Code: Select all

-- pseudo code

if state isn't already confirmed as red, yellow or green, then
  if mining target is nil then
    if search doesn't find ore then
      set red state
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
User avatar
anarchyinc
Inserter
Inserter
Posts: 29
Joined: Tue Jul 05, 2016 9:31 pm

Re: Detecting resources available to a drill

Post by anarchyinc »

You could check into "AutoDeconstruct" and see how he does it. Seems like both mods are trying to detect the same thing.
"Friends don't let friends drink & drive", "Gamers don't let gamers install steam"
trold
Inserter
Inserter
Posts: 45
Joined: Fri Jul 31, 2015 1:07 pm
Contact:

Re: Detecting resources available to a drill

Post by trold »

anarchyinc wrote:You could check into "AutoDeconstruct" and see how he does it. Seems like both mods are trying to detect the same thing.
That solution falls under the category of "weird special case" in my mod. It might be the right way to do it, but it actually both listens for on_resource_depleted, and does a find_entities search.

Like aubergine18 pointed out, what I really need is something like an .is_depleted flag,
Post Reply

Return to “Modding help”