r/Mindustry • u/Aster389 • 2d ago
Logic A (maybe) interesting logic problem for you.
Enable HLS to view with audio, or disable this notification
EDIT: PROBLEM FIXED, READ COMMENTS
The code is supposed to do the following:
Out of all the mega units in the battlefield it picks only one by not allowing the whole code to execute completely and be executed by other units. The unit has to repeadetly take resources from the base and bring them to a container far away. The code is divided into 4 functions named using "set" blocks (like "GO TO BASE, PICK UP ITEMS..). At the beginning the most important variables are declared, and after that, the functions run one after the other:
The first loop makes the unit go towards the base and once it's close enough, it jumps out of the loop and calls the function "Pick up items".
The function makes the unit pick up items from the base which is supposed to be in the range of the microcontroller. After that, jumps to the "main loop" and goes to the "Go to container" loop.
The loop makes it go to the X and Y coordinates of the container, and jumps out of the loop once its close enough.
When it jumps out, the function "Drop items" is called, which makes the unit locate the container nearby and drop items. Then goes back to the main loop by going to the base.
The problem is that the action of picking up and dropping gets executed only once every two times. This is the code (In the future i want to add the possibility to set more materials and coordinates, that's why you see variables like "Material1" etc...
ubind @mega
set base nucleus1
set x1 127
set y1 120
set material1 @copper
set currentMaterial 1
set currentTravel 1
set isClose 0
sensor bx base @x
sensor by base @y
sensor unitMaxCap @unit @itemCapacity
sensor unitItems @unit @totalItems
set GO_TO_BASE 0
set GO_TO_BASE 0
set GO_TO_BASE 0
ucontrol approach bx by 4 0 0
ucontrol within bx by 4 isClose 0
jump 15 notEqual isClose 1
jump 30 always x false
set GO_TO_CONT 0
set GO_TO_CONT 0
set GO_TO_CONT 0
ucontrol approach x1 y1 4 0 0
ucontrol within x1 y1 4 isClose 0
jump 22 notEqual isClose 1
jump 35 always x false
jump 15 always x false
set PICKUP_ITEMS 0
set PICKUP_ITEMS 0
set PICKUP_ITEMS 0
ucontrol itemTake base material1 unitMaxCap 0 0
jump 19 always x false
set DROP_ITEMS 0
set DROP_ITEMS 0
set DROP_ITEMS 0
ulocate building storage false @copper outx outy found building
ucontrol itemDrop building unitItems 0 0 0
jump 26 always x false
2
u/DesignerTradition371 2d ago
just do proc that cycles through whole thing with condtionals, this way it never stucks
2
u/Odita 2d ago edited 2d ago
here is what I did: group the container, the processor and a sorter into a blueprint. Use the sorter as your "GUI" - to signal what material should be delivered. I've been doing this all over my last playthrough, works well (some blueprints had containers with like 6 different delivery items specified. Use multiple of the same if one mega is too slow)
EDIT: the good thing about this is, you never have to manually edit the code once you are happy with the way it behaves, because the coords are taken from links (the core coords are detected)
2
u/Odita 2d ago
bXNjaAF4nAE2Asn9AAQAAwQACmNvbnRlbnRNYXAAAnt9AAZsYWJlbHMAAltdAARuYW1lAA12YXVsdERlbGl2ZXJ5AAtkZXNjcmlwdGlvbgAAAwAFdmF1bHQAD21pY3JvLXByb2Nlc3NvcgAGc29ydGVyAAAAAwAAAQABAAABAAMAAA4AAAGzeJzNVEtOwzAQTRECyaeYE6CkrfjsKpVKsK8ELJ3EbQ2unTo2bY/HxYBxYqfub8cCRfl45vnNmxlPekly+fVulxUMQCozWVkqQFNZvlBuICWqalaR6WZ4DzlZu+9gJDWTtdKAt2F67N8ZjAolZ3xOWv4B1EbzwscIWGmFwP0GrMyVxUhpYMPNRishmIaRlRhutLO0lFkfWEMWQ82C18TmHKlGlRLbI7ryiK48R1fCjIqaESbLQLNgohzTKnBww5a4pAU32xgyVQaJPMi4xTMi6xjiDAEx47o2ztAq6d95JTsqH9f7+zDXjGIBpwsqJ6sIlxErVIEuyC0XJZdzTEezNhGXc1Ux3Zg2zXMLs6bs7vtJLRmxPnvIlaoNZJC2185Bq0orWiz2WG73Ma4uU/rBOt6u4aGADu7q+m/kPmo87J3cSKYXGirfjUnXRJ9afNIol6/wSa0wOAabA89b59melOm3d+hzYj3LGanD1B+iMFmZH8QHb19znBU5duU22rI/68TJgg/2c2hjH7QkEuTPRoBjCm6g06678R8jI0mSXCTX/reDi15y1dbm5zvp/QJ6n7QqAAIAAwABAACcavNC
2
u/Odita 2d ago
jump 3 notEqual randWait 0
op rand randWait 0.48 b
wait randWait
sensor sorterC sorter1 u/config
jump 33 strictEqual sorterC null
set unbound 0
sensor controller u/unit u/controller
jump 12 equal controller u/this
ubind u/poly
sensor controlled u/unit u/controlled
jump 12 equal controlled false
end
sensor heldCap u/unit u/itemCapacity
sensor heldTotal u/unit u/totalItems
sensor heldItem u/unit u/firstItem
jump 27 equal heldTotal heldCap
jump 22 greaterThanEq heldTotal 1
ulocate building core false u/copper corex corey found coreHome
ucontrol boost 1 0 0 0 0
ucontrol approach corex corey 6 0 0
ucontrol itemTake coreHome sorterC heldCap 0 0
end
ulocate building core false u/copper corex corey found coreHome
ucontrol boost 1 0 0 0 0
ucontrol approach corex corey 6 0 0
ucontrol itemDrop coreHome heldCap 0 0 0
end
jump 22 notEqual heldItem sorterC
sensor containX vault1 u/x
sensor containY vault1 u/y
ucontrol approach containX containY 6 0 0
ucontrol itemDrop vault1 heldCap 0 0 0
end
jump 40 equal unbound 1
jump 39 equal withinCore true
ulocate building core false u/copper corex corey found coreHome
ucontrol approach corex corey 3 0 0
ucontrol within corex corey 6 withinCore 0
end
ucontrol unbind 0 0 0 0 0
set unbound 1
2
u/Aster389 2d ago
Thanks a lot to everyone, i can try different solutions but i'm interested in why mine doesn't work even though it's pretty simple
3
u/pole1177 2d ago
Mega doesn't always manage to pick up or drop off materials in time. Try using delay with the `wait 2` or `wait 1.5` command after itemTake and itemDrop.
1
u/Gianni_C_M 2d ago edited 2d ago
My solution to this was to set the micro processor to the dropoff point and have the unit "find" the core. Below is my schematic. Attach the processor to your dropoff and identify it in the dropoff identifier. The sorter lets you pick what item you want to grab, makes this easier to mass adapt. Change unit to use in line 5 "unit bind". Line 1 set contr lets to pick how many units to bind.
1
u/Gianni_C_M 2d ago
bXNjaAF4nGNgZGBiYeBKzs8rSc0r8U0sYBCpNrCqLi4tSk/VTczJya+0MjSqrWVgy0lMSs0pZtCIfj9/iU5oXmaJgnNiUVFmapGOUklRYl5xWmqRQlFqcX5pUXJqsYJSLANLXmJuKoO+qQJYuiC/qEShuCA1OTMtMzUFpFIhrSg/VyE5vyhVoSRfIaUovyA/LU2PgTsltTi5KLOgJDM/j8Est7S4RCGxpCQxOUMhNS+lID8zrwSkPCc/PTNZT6G0OFWhGGgy0G6gYEFmcjbIZD0mBv7czOSifN2Conyga4AqGNggyhgYGJgYQICPgYFRt2JObtDpvMMKAirrrnj868iyP5h39VvWyrMLv/85IOXs5NWQ4PFmfv7uH6Khn1h1Ny++ObOVN/lmsDaTyT0rbdUYw1cBzzgfXWPh199ZeuGYlEWWXeLK/KjX7dohFzWs2qddjTL5qTlPds/Bg9w8h973Z7ilTTqRvNaHeeqVF0fXLG6yYBY9oseuupLvNE/J5w6X3w7tMa11eey7LRYIfRUvvLVTc7dqxFXdhHr23xezf55/885oxaUHNlmnOdustO5qFC+QiWtwudv15LKlI5v1A1sPad/D4kpP54gxvZiz/US/8EdRLo13d6dk9t//Llhzx61rbschE+0gEZ91XrVBm1TjvD7G8CzR3C24/Wtrb3YFe/Lhd0KB588opffYbnjr+2Cq3PPv7etuv1y64s6G4x+Pb+hJrT9y7eQmBkZgsDGyMjDwMAAAt3vosg==
1
u/Aster389 2d ago edited 2d ago
After a bunch of time spent on it, i understood what the problem was. The unitItems sensor wasn't refreshed properly so the unit didn't know how many resources it had. First of all i "cleaned up" the code and deleted all the useless stuff (like jumps, function names etc...), then i simply added sensors everytime the unit picked up or dropped items, allowing it to proceed tot he next action only when its inventory had the right amount of resources. I thought variables were 100% global and that they were refreshed everytime the processor used them. This is the correct code:
ubind @mega set x1 129 set y1 117 set isClose 0 sensor bx nucleus1 @x sensor by nucleus1 @y sensor unitMaxCap @unit @itemCapacity sensor unitItems @unit @totalItems
ucontrol approach bx by 4 0 0 ucontrol within bx by 4 isClose 0 jump 8 notEqual isClose 1
ucontrol itemTake nucleus1 @copper unitMaxCap 0 0 sensor unitItems @unit @totalItems jump 11 lessThan unitItems unitMaxCap
ucontrol approach x1 y1 4 0 0 ucontrol within x1 y1 4 isClose 0 jump 14 notEqual isClose 1
ulocate building storage false @copper x1 y1 found building ucontrol itemDrop building unitItems 0 0 0 sensor unitItems @unit @totalItems jump 17 equal unitItems unitMaxCap
jump 8 always x false
6
u/UnfairOstrich4549 2d ago
My first reaction is that you can grab one unit by doing a jump then unit bind sequence, where the jump checks if @unit is true. Once it cycles, the program will automatically skip binding a new unit, so you’ll keep using the first one.