PneumaticCraft: Repressurized

PneumaticCraft: Repressurized

50M Downloads

[Discussion] Loops and conditions in drone programs

lOmicronl opened this issue ยท 11 comments

commented

(Moved here from reddit)

I have a drone program that checks a condition and, if true, executes a snippet of code. After that is finished, the program does not return to the condition statement, but rather to the start piece of the entire program. I may only be a scripting amateur and not a real programmer, but I've never known a programming language that did that.

I would like to know if it is possible to return directly to the condition statement and have it checked again - a behavior like a while( ) loop, if you will. Or, failing that, if there's a more elegant way to handle looping instructions that simply didn't occur to me yet, as I've only recently started poking at drones. In the program in question, it ends up making little to no difference, but more complex programs could lead to a situation where using a condition becomes infeasible because it would need to be placed too far below too much other code which you would not want to be repeated everytime the condition evaluates true.

Screenshot of program: https://i.imgur.com/et4BPcv.png

Code for importing: https://pastebin.com/3i3nKmL1

You'll have to fix the area coordinates, but there's not too many of them. All it needs is a harvest area, a pickup area (identical to the harvest area), and the location of a chest which contains a hoe and has space for produce to be dropped off. For testing purposes, you probabaly also want to reduce the standby time to something more manageable.

commented

You're right for the most part. When the condition piece branches off and the branch ends, the whole program ends and restarts. It can cause loops when set up improperly, but it does have its uses.

To fix this, break off the item condition piece and add a jump from the hoe export to the item condition piece. At the bottom of the "PickMeUp" branch add a jump piece to the item condition. Remove the wait piece since standby does the same thing.

Anyway, I wrote a program for you that doesn't require a hoe. https://pastebin.com/u82ZGbuQ

I used wheat as an example. This only works if you are playing modded and plants don't uproot when right-click harvested. You will also want at least 1 dispenser upgrade installed to pick up wheat and seeds in one pass. Otherwise, just add a loop from the bottom of the "PickMeUp" branch to the "harvest" branch to make additional pick up checks. If you add a magnet upgrade you can remove the pick up piece. You could also add a go to piece set to your charging station before the standby piece to make your drone charge up while it waits for crops to mature. Also, I used coordinate operators and variables to make it easier to change this for you setup. You can remove them if you want and just enter coordinates in each area piece.

One thing to know with this approach is the you need to set the correct metadata for the crops to be harvested. For wheat the metadata is 7, but some plants have different or multiple metadata numbers.

commented

After that is finished, the program does not return to the condition statement, but rather to the start piece of the entire program. I may only be a scripting amateur and not a real programmer, but I've never known a programming language that did that.

Definitly, Drones are Grafcet programmed, but I think it wasn't the goal :D

I agree with blockstate-awarding, maybe extend this to dropped item's NBT?
(oh, and allow drone picking up minecarts and boats. necessary feature.)

commented

Regarding carrying non-living entities: looking at the Minecraft code, there don't appear to be any restrictions on which entities can be carried. So in theory that's doable, but I'd need to test it out to see if there are any pitfalls...

commented

Yep, basically what @mcsaboteur said! Conditions branches aren't like subroutines that condition pieces execute; it's a continuation of the main program branch (the concept of subroutines doesn't really exist for drone programming; it's all jumps).

Regarding metadata checks, I'll investigate the possibility of a separate "Condition: Mature" piece. I have a feeling MineMaarten looked at that some time back and discovered some subtleties that could make it problematic. But then he coded up the dedicated harvesting drones, so there is some code in the mod already for testing crop maturity which might be adaptable...

In any case, numeric block meta won't be a thing in 1.13 at all, so some fully blockstate-aware solution will be needed there.

commented

To fix this, break off the item condition piece and add a jump from the hoe export to the item condition piece. At the bottom of the "PickMeUp" branch add a jump piece to the item condition. Remove the wait piece since standby does the same thing.

Okay, I can definitely try that, and report back. What do you mean though, that the standby and wait pieces do the same thing? They don't even accept the same arguments, so they can't do the same thing.

For example, without a wait piece, how long does the drone stay in standby? There's no way to specify that. The description of the standby piece says that it stays in standby until it detects that there is more work to be done. In this case, the next item of work on schedule is restarting the program, and therefore, picking up a hoe. That work is always available. From my understanding of things, that would cause the drone to wake up again immediately the same second it shuts off, which defeats the purpose... hence the wait command. It causes the drone to defer checking if any new work is available, and thus enforces the standby for a user-specified amount of time.

commented

It will only start up again if there's actually a hoe to pick up (and space for the hoe, of course) . The drone first checks if the next program piece can be run, and then if so runs it. If the eligibility check fails (i.e. no hoe to pick up or no crops to harvest) , then it will stay in standby.

Having said that, a wait is not a bad idea, since it will be easier on server cpu - not constantly checking if work can be done.

commented

It will only start up again if there's actually a hoe to pick up (and space for the hoe, of course)

Yep, and there always is both a hoe and space for it. The program itself is written in a way that makes sure that there is, in the way it orders the drone to put the hoe back into the same chest it took it from. Therefore the standby piece alone will not actually cause it to stop working. It can always proceed again immediately.

I suppose the program could be changed in such a way that the order to pick up a hoe is the last thing before entering standby, instead of the first thing when coming out of standby. Then the drone would only wake once a crop is actually ready to harvest. But as you said, it would constantly be checking... and let's be honest, do we really, absolutely, positively need every crop to be harvested the moment it grows?

Like, I have two wheat, two carrots, two potatoes, two beetroots, one melon and one pumpkin in said farm. There are no growth accelerators of any kind, not even the Pneumaticcraft cropsticks. The drone only wakes up once every six minutes instead of chasing every crop as it grows. And yet, it still produces food several times as fast as I need to eat it. That's why I haven't even provided a recharge station nearby... no, I just pump the drone up to 10 bar, set it down, and let it run out of juice. That takes nearly than 24 RL hours with my program, it's that frugal. Then I can continue to eat for a good long while from the excess it made, and eventually when that runs low, I can pick up the drone and chuck it into my armor charging station for a minute and set it flying again.

The only real, actual functionality problem I have is the drone sometimes not replanting wheat, but I reckon that's due to wheat not always dropping a seed, and going to fetch one from the chest isn't a feature of the standard harvest puzzle piece. Perhaps I'll add a little section that makes the drone grab a seed and rightclick the two wheat spaces right before it goes into standby, but eh. It doesn't happen that often.

...Question: if I upgrade the drone to carry two things at the same time, and have it pick up a hoe and a seed, will it use the seed rom its second inventory slot to replant the wheat when there's no seed drop? Or does the harvest puzzle piece handle replanting only with what dropped from te harvest event?

commented

I just meant the wait and standby pieces both make the drone pause for a while, so both aren't really needed. You can remove the standby and the leave the wait if it works better for you. If you use a standby piece, I think you would want to start the program with a block condition piece checking crop maturity. If it's true/mature, run the harvest routine, if it's false run the standby routine. Using a wait piece is probably cheaper and easier at this point, since you already have a program made.

I'm not sure about the seed replanting questions. I would think so, but I've never tested. Did you check the program I posted? It lets you harvest without replanting and has the block condition setup I mentioned.

commented

I did, yes!

...I'm afraid to say, I didn't understand a thing about what's going on with the coordinate operators. Not even with the ingame wiki's help. Like, I get they're performing additions, but I don't get what purpose it serves in the program. Guess it's just too abstract for me.

So instead I modified my program like you suggested, splitting off the condition and making it accessible via a jump from both the harvesting section and the pickup section. The drone now no longer cycles through picking up and dropping the hoe everytime the condition is checked, so that's a straight success.

commented

Awesome! You're going to be a drone addict now.

commented

Yeah the operators and variables take a bit of getting used to. I'd strongly recommend viewing some of MineMaarten's original videos on drone programming (https://www.youtube.com/results?search_query=pneumaticcraft+drone+programming+minemaarten) - I find myself needing to refer to them at times too :)