Page 1 of 1

Detecting resources available to a drill

Posted: Wed Sep 21, 2016 9:39 pm
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?

Re: Detecting resources available to a drill

Posted: Wed Sep 21, 2016 9:44 pm
by aubergine18
Well, it will automatically stop functioning when depleted, so you could potentially check the .active state or it's .power_usage, etc?

Re: Detecting resources available to a drill

Posted: Thu Sep 22, 2016 9:02 pm
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.

Re: Detecting resources available to a drill

Posted: Thu Sep 22, 2016 10:58 pm
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...?

Re: Detecting resources available to a drill

Posted: Thu Sep 22, 2016 11:45 pm
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.

Re: Detecting resources available to a drill

Posted: Fri Sep 23, 2016 11:26 am
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.

Re: Detecting resources available to a drill

Posted: Fri Sep 23, 2016 11:26 am
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...

Re: Detecting resources available to a drill

Posted: Fri Sep 23, 2016 11:30 am
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)

Re: Detecting resources available to a drill

Posted: Fri Sep 23, 2016 11:48 am
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".

Re: Detecting resources available to a drill

Posted: Fri Sep 23, 2016 12:02 pm
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

Re: Detecting resources available to a drill

Posted: Fri Sep 23, 2016 5:07 pm
by anarchyinc
You could check into "AutoDeconstruct" and see how he does it. Seems like both mods are trying to detect the same thing.

Re: Detecting resources available to a drill

Posted: Sat Sep 24, 2016 5:53 am
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,