Page 1 of 1

[Scripting Assistance] Help identifying mining drill functions

Posted: Sun Dec 05, 2021 9:03 am
by DwarfTech
Hello all. This is my first post here, and I'm quite new to .lua and scripts using it. I'm trying to make mining drills which have no mineable resources continue to mine up basic stone as if they were on a stone patch. However, I have tried studying scripts and looking up relevant functions on lua-api.factorio.com, and I can't make heads or tails of accomplishing my goal.
I have two ideas on how to go about it. One is to grab the position of the mining drill upon resource depletion, then spawn a single stone tile under it. However, I still wish to use mods like prospector, which already spawns depleted ores when a tile is depleted, so I don't fancy overwriting those with stone tiles. My alternative idea was to make miners with no mineable resources continue playing their sounds and animations, and simply spawn stone at their output position based on their mining speed. This one seems optimal to me, although likely more complex to pull off. After trying and getting nowhere in particular, I wanted to turn to you all and ask about ideas, opinions, or perhaps lua scripting advice on the subject.

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Sun Dec 05, 2021 4:47 pm
by DaveMcW
So you want a machine that
- Plays animations like a mining drill
- Outputs a resource
- Requires no resource under it

The last requirement is impossible with a mining drill, even a modded one. I would use an assembling machine and change the graphics to look a like mining drill.

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Mon Dec 06, 2021 4:46 am
by FuryoftheStars
DaveMcW wrote: Sun Dec 05, 2021 4:47 pm So you want a machine that
- Plays animations like a mining drill
- Outputs a resource
- Requires no resource under it

The last requirement is impossible with a mining drill, even a modded one. I would use an assembling machine and change the graphics to look a like mining drill.
Is there a function that could be monitored for when a mining drill removes a piece of resource from a tile or a resource tile is depleted that the OP could then use to replace depleted resource tiles with a stone resource tile?

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Mon Dec 06, 2021 7:22 am
by Silari
There's https://lua-api.factorio.com/latest/eve ... e_depleted that would work - it'd have to catch the resource depleting then make sure nothing like prospector was activating, then if there's still nothing he could either spawn stone ore or change the miner to an assembler with miner graphics.

Keep in mind AFAIK there's no way to make an assembler auto-output like a miner does. You can fake it with an inserter but it's not going to work the same.

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Wed Dec 08, 2021 7:21 am
by DwarfTech
Thanks for the advice. If auto-output with no resources isn't possible, I suppose I'll have to go with spawning stone tiles under. I've been trying to make a script for that, but I'm still fumbling a lot with it. I'm starting to learn how some functions work through both the wiki and studying other opensource mods, but as far as making a script to detect drills running out of resources and then making a stone tile...well, I tried and got something that didn't *crash* the game, but it didn't really work either. Just didn't do anything. The things I know I'd need are as follows, please do add on or correct me if I'm wrong:
-The location of the particular drill that runs out of resources
-The event on_resource_depleted, to actually trigger the resupply
-A variable to reference mining drills, which I assume would run under the on_resource_depleted function to then call for and locate the corresponding drill entities
-Not all sure what else. I'm a doof, and still very green to the whole lua scripting thing. I'll keep trying and studying mods that look up drills with depleted resources, and hopefully get something functional up. By any chance, for the sake of development, is there a good way to have each function run a message in the chat so I can verify that each command is, in fact, getting run properly?

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Wed Dec 08, 2021 7:53 am
by Stringweasel
DwarfTech wrote: Wed Dec 08, 2021 7:21 am -The location of the particular drill that runs out of resources
-The event on_resource_depleted, to actually trigger the resupply
-A variable to reference mining drills, which I assume would run under the on_resource_depleted function to then call for and locate the corresponding drill entities
-Not all sure what else. I'm a doof, and still very green to the whole lua scripting thing. I'll keep trying and studying mods that look up drills with depleted resources, and hopefully get something functional up. By any chance, for the sake of development, is there a good way to have each function run a message in the chat so I can verify that each command is, in fact, getting run properly?
How I would do it is the following:
  1. Listen to on_resource_depleted as you mentioned. For a start add a simple "print" to the handler and see if it's called. The rest of the steps will all happen in this event handler.
  2. This function gives you "local drill = event.entity", which is the mining drill that was depleted. You will have to check here that it's indeed a mining drill though, and not a pumpjack for example. You could do this by simply "if event.entity.type ~= "mining-drill" then return end"
  3. Make sure that there is no resources left that this drill can mine. I haven't tested this, but a drill can reach like 25 resource tiles? And this handler will fire when any one of those tiles get depleted. You could probably check this with "if drill.active then return end", because if the drill is still active then it likely still has resources to mine. (See Point 5.)
  4. Add the stone resource at "drill.position"
  5. Reactivate the drill. When drills run out of resources they go sleep IIRC, so you would need to "drill.active = true" again. Otherwise the drill won't notice the new resource.

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Wed Dec 08, 2021 8:36 am
by PFQNiet
Stringweasel wrote: Wed Dec 08, 2021 7:53 am This function gives you "local drill = event.entity", which is the mining drill that was depleted. You will have to check here that it's indeed a mining drill though, and not a pumpjack for example. You could do this by simply "if event.entity.type ~= "mining-drill" then return end"
Pumpjacks are mining drills, just with a different category. A better option here would be to check the entity's prototype mining category(ies) to see if it can mine stone.

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Wed Dec 08, 2021 8:42 am
by Stringweasel
PFQNiet wrote: Wed Dec 08, 2021 8:36 am Pumpjacks are mining drills, just with a different category. A better option here would be to check the entity's prototype mining category(ies) to see if it can mine stone.
Haha oops :lol: I probably meant to type <entity.name == "mining-drill">, but PFQNiet's way is much more resiliant.

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Wed Dec 08, 2021 11:38 pm
by Silari
Stringweasel wrote: Wed Dec 08, 2021 7:53 am
  1. Listen to on_resource_depleted as you mentioned. For a start add a simple "print" to the handler and see if it's called. The rest of the steps will all happen in this event handler.
I believe the entity in the event is the RESOURCE ENTITY that was depleted, not a mining drill/pumpjack. You couldn't rely on that anyway, since one resource can be mined by several miners, so it could result in more than one being out of resources. You'd have to search the area, and you'd have to ensure the area is large enough that any miner in reach is found.

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Thu Dec 09, 2021 2:34 am
by FuryoftheStars
Silari wrote: Wed Dec 08, 2021 11:38 pm
Stringweasel wrote: Wed Dec 08, 2021 7:53 am
  1. Listen to on_resource_depleted as you mentioned. For a start add a simple "print" to the handler and see if it's called. The rest of the steps will all happen in this event handler.
I believe the entity in the event is the RESOURCE ENTITY that was depleted, not a mining drill/pumpjack. You couldn't rely on that anyway, since one resource can be mined by several miners, so it could result in more than one being out of resources. You'd have to search the area, and you'd have to ensure the area is large enough that any miner in reach is found.
Which, considering mod miners, may be more trouble than it's worth. That said, if you're replacing the resource tiles with stone as they are depleted, then unless that was the only tile the miner could reach, then it should never enter the disabled state anyway? So leaving it up to the player to replace the edge case miners may not be a huge deal.

Re: [Scripting Assistance] Help identifying mining drill functions

Posted: Thu Dec 09, 2021 8:39 am
by Stringweasel
Silari wrote: Wed Dec 08, 2021 11:38 pm
Stringweasel wrote: Wed Dec 08, 2021 7:53 am
  1. Listen to on_resource_depleted as you mentioned. For a start add a simple "print" to the handler and see if it's called. The rest of the steps will all happen in this event handler.
I believe the entity in the event is the RESOURCE ENTITY that was depleted, not a mining drill/pumpjack. You couldn't rely on that anyway, since one resource can be mined by several miners, so it could result in more than one being out of resources. You'd have to search the area, and you'd have to ensure the area is large enough that any miner in reach is found.
Ah yes of course. So what I'd do is wait for the depletion event, and then on the event search for miners that can reach that resource. Then for each of those miners you will have to check if it still has access to any other resourse tiles. And if it doesn't, then continue creating the stone patch.
FuryoftheStars wrote: Thu Dec 09, 2021 2:34 am Which, considering mod miners, may be more trouble than it's worth. That said, if you're replacing the resource tiles with stone as they are depleted, then unless that was the only tile the miner could reach, then it should never enter the disabled state anyway? So leaving it up to the player to replace the edge case miners may not be a huge deal.
I don't really agree that it's too much trouble. You only need to find the maximum miner size once on startup by going through the prototypes. And nobody likes babysitting miners ;) That's the entire idea of this mod.

The initial functionality sounds a lot like Auto Deconstruct, which will likely need to follow these exact steps. Only difference is your mod will not mark it for deconstruction, but rather spawn your stone patch. You can check out how Auto Deconstruct does it. :)