This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Things's profile picture

Rocket Turtle! (Fireworks factory)

Started by Things, 10 June 2013 - 04:01 AM
Things #1
Posted 10 June 2013 - 06:01 AM
OK, no, it won't send your turtle flying off to the moon, but instead a fully computer controlled method of crafting fireworks!

Just a heads up, this is quite a long tutorial, and it can be a bit resource and time consuming to set up. I'll try and make it as brief and easy to follow as possible, but there are some specifics that need to be known for this to work.

I wanted to create a decent firework display on my server, however I needed a way of making fireworks in "bulk". There is a block called the Firework Launcher than can do just this, however, I find it a little cheaty as it doesn't require paper, and as far as I can tell, you can't change the duration of the rocket. You also can't pull the firework out in it's crafted state, so I'd need a bunch of launchers and computers to do what I wanted. It also had basically no documentation I could find online.

Originally I attempted to do this with autocrafting tables from Buildcraft. They would work fine, however either you needed to have set firework recipes, or pump in 2 sets of ingredients to make a rocket, then pump them back out again. I wanted something more configurable and compact, so, I decided to use turtles and computers!

The setup

SpoilerI highly recommend using Filters from the Redpower mod for this. They offer a method of accurately sending the correct amount of items based on redstone pulses. I also recommend using bundled cable to connect them to the main control computer, as you'll need more redstone outputs than you can get from the computer sides alone. Wireless redstone is also very handy, but not necessary.

You will need 1 crafty turtle with wireless modem, an interactive sorter and one regular (or advanced, doesn't matter) computer with a wireless modem on the TOP.

Start by placing all the chests you want to hold firework star ingredients, and putting a filter on all of them. At a minimum you'll need gunpowder and a dye, you can add as many dye of effect chests as you'd like (however, you can only use 8 in any one firework star).



In my setup, you'll notice a lot of gates and extra wireless redstone - this is just part of the automatic refilling and not needed.

You can also add gold nuggets, glowstone dust, diamonds, fire charges etc for extra effects.

Now, either assign a wireless receiver, or connect different coloured insulated wires to every filter (don't use black insulated wire). Connect all the pneumatic tubes together.

Place the wireless crafty turtle, and put a chest in front of this turtle to put the fireworks in. Place the interactive sorter and a chest above him.



Now, we need to get paper into the chest above the turtle. So once again, place another chest and filter, and connect the tubing to the existing tubes or to the chest. It does not matter how the tubes are connected, as long as they are actually connected to the chest in some way.

So, by now you should have chests set up for your dyes, gunpowder, star effects and paper all feeding into the chest above the turtle/interactive sorter.

Decide on the multiples you want to make fireworks in, and place that many of each item type in the chests respective filter.



Next, throw down your regular/advanced computer, and connect some bundled cable to the BACK of it. Connect each filter to it's own wire colour (Or use wireless redstone). I put the dye colours on their respective wire colours to make it easier to remember, but you can change this in the code.

So, if you have 2 dye colours, and 1 firework effect, you should have a total of 5 different coloured cables. I have 6 colours and 4 firework effects in my system, so a total of 12 different cable colours. There are technically 16 colours you could use, however I'm using black as a placeholder for the colours.combine function.


From left to right, I have Diamonds, Fire Charges, Glowstone Dust, Gunpowder, Gold Nuggets, Red, Orange, Lightblue, Purple, Green, Yellow, Paper


The code

SpoilerPut this on the turtle and run it. You can name the program startup and it will run all the time.


modem = peripheral.wrap("right")
sorter = peripheral.wrap("top")
modem.open(1)
modem.open(2)
modem.open(3)
modem.open(5)
numdyes = 0
gotAmt = false
rxamt = false
rxdur = false
rxqty = false
dyes = {}
cntr = 0
gotColours = false

function craftRocket()
if turtle.getItemCount(1) == amt then
   if dur == 1 then
	sorter.extract(1,289,0,amt)
	end
	if dur == 2 then
	sorter.extract(1,289,0,amt)
	sorter.extract(1,289,0,amt)
	end
	if dur == 3 then
	sorter.extract(1,289,0,amt)
	sorter.extract(1,289,0,amt)
	sorter.extract(1,289,0,amt)
	end
   if turtle.getItemCount(2) > amt then
	 turtle.select(2)
	 turtle.transferTo(3, turtle.getItemCount(2) - amt)
   end
   if turtle.getItemCount(3) > amt then
	 turtle.select(3)
	 turtle.transferTo(5, turtle.getItemCount(3) - amt)
   end
   if turtle.getItemCount(4) > 0 then
	 turtle.select(4)
	 turtle.transferTo(5)
   end
  sorter.extract(1,339,0,amt)
  turtle.select(4)
  turtle.transferTo(6)
end
gotQty = turtle.getItemCount(1) + turtle.getItemCount(2) + turtle.getItemCount(3) + turtle.getItemCount(5) + turtle.getItemCount(6)
  if gotQty == itemCount and gotAmt == true then
  if dur == 1 and turtle.getItemCount(2) == amt or dur == 2 and turtle.getItemCount(3) == amt or dur == 3 and turtle.getItemCount(5) == amt then
	turtle.select(16)
	turtle.craft(amt)
	turtle.drop()
	 gotAmt = false
	 rxdur = false
	 rxamt = false
	 rxqty = false
	 cntr = 0
	 gotColours = false
	 itemCount = 0
	 print("Crafted all!")
	 modem.transmit(4,4,"Done!")
	end
	end
	sleep(0.01)
   end


while (true) do
if gotAmt == false then
  local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  if senderChannel == 1 then
  amt = message
  rxamt = true
  print(amt)
  end
  if senderChannel == 2 then
  dur = message
  rxdur = true
  print(dur)
  end
  if senderChannel == 3 then
  qty = message
  rxqty = true
  print(qty)
  end
  if senderChannel == 5 then
	dyes[numdyes] = message
	numdyes = numdyes + 1
	print(message)
	if numdyes == qty then
		gotColours = true
		numdyes = 0
	end
  end
  if rxamt == true and rxdur == true and rxqty == true and gotColours == true then
	gotAmt = true
	itemCount = amt * dur + amt + amt
	qty = qty * amt + amt
	print(string.format("Total star item count: %d",qty))
	sorter.extract(1,289,0,amt)
end
end
if gotAmt == true then
if cntr <= #dyes then
	if dyes[cntr] == "red" then
		sorter.extract(1,33119,0,amt)
	end
	if dyes[cntr] == "green" then
		sorter.extract(1,65887,0,amt)
	end
	if dyes[cntr] == "yellow" then
		sorter.extract(1,360799,0,amt)
	end
	if dyes[cntr] == "lightblue" then
		sorter.extract(1,393567,0,amt)
	end
	if dyes[cntr] == "purple" then
		sorter.extract(1,164191,0,amt)
	end
	if dyes[cntr] == "orange" then
		sorter.extract(1,459103,0,amt)
	end
	if dyes[cntr] == "star" then
		sorter.extract(1,371,0,amt)
	end
	if dyes[cntr] == "twinkle" then
		sorter.extract(1,348,0,amt)
	end
	if dyes[cntr] == "trail" then
		sorter.extract(1,264,0,amt)
	end
	if dyes[cntr] == "largeball" then
		sorter.extract(1,385,0,amt)
	end
	cntr = cntr + 1
end
	
	
if turtle.getItemCount(4) > 0 then
turtle.select(4)
turtle.transferTo(11)
if turtle.getItemCount(11) > 0 then
turtle.transferTo(10)
if turtle.getItemCount(10) > 0 then
turtle.transferTo(9)
if turtle.getItemCount(9) > 0 then
turtle.transferTo(7)
if turtle.getItemCount(7) > 0 then
turtle.transferTo(6)
if turtle.getItemCount(6) > 0 then
turtle.transferTo(5)
end
end
end
end
end
end

gotQty = turtle.getItemCount(1) + turtle.getItemCount(2) + turtle.getItemCount(3) + turtle.getItemCount(5) + turtle.getItemCount(6) + turtle.getItemCount(7) + turtle.getItemCount(9) + turtle.getItemCount(10) + turtle.getItemCount(11)
end
if gotQty == qty and gotAmt == true then
turtle.select(16)
turtle.craft(amt)
turtle.transferTo(1)
craftRocket()
end
sleep(0.01)
end



And finally, the code for the control computer. Note this setup uses modem channels 1, 2 3, 4 and 5, you can change them if needed.




-- Config stuff
redWire = colours.red -- Wire colour for red dye
yellowWire = colours.yellow
lightblueWire = colours.lightBlue
greenWire = colours.green
purpleWire = colours.purple
orangeWire = colours.orange
starWire = colours.cyan
twinkleWire = colours.lime
trailWire = colours.magenta
largeWire = colours.grey
multiple = 16 -- Multiple of fireworks
gunpowderWire1 = colours.blue
paperWire = colours.white
-- End config stuff

tArgs = {...}
amt = tonumber(tArgs[1])
dur = tonumber(tArgs[2])
qty = 0
star = false
large = false
fcolour = colours.black
if tArgs[3] == null then
  print("Specify at least 1 colour!")
  error()
end
for i=3,#tArgs do
  if tArgs[i] == "red" then
	qty = qty + 1
	fcolour = colours.combine(redWire,fcolour)
  end
  if tArgs[i] == "yellow"  then
	qty = qty + 1
	fcolour = colours.combine(yellowWire,fcolour)
  end
  if tArgs[i] == "lightblue" then
	qty = qty + 1
	fcolour = colours.combine(lightblueWire,fcolour)
  end
  if tArgs[i] == "green" then
	qty = qty + 1
	fcolour = colours.combine(greenWire,fcolour)
  end
  if tArgs[i] == "purple" then
	qty = qty + 1
	fcolour = colours.combine(purpleWire,fcolour)
  end
  if tArgs[i] == "orange" then
	qty = qty + 1
	fcolour = colours.combine(orangeWire,fcolour)
  end
  if tArgs[i] == "star" then
	qty = qty + 1
	fcolour = colours.combine(starWire,fcolour)
	star = true
  end
  if tArgs[i] == "twinkle" then
	qty = qty + 1
	fcolour = colours.combine(twinkleWire,fcolour)
  end
  if tArgs[i] == "trail" then
	qty = qty + 1
	fcolour = colours.combine(trailWire,fcolour)
  end
  if tArgs[i] == "largeball" then
	qty = qty + 1
	fcolour = colours.combine(largeWire,fcolour)
	large = true
  end  
end
print(string.format("Total dyes: %d",qty))
if amt == null or  amt % multiple ~= 0 or amt > 64 then
  print("Invalid amount!")
  error()
end
if dur > 3 or dur < 0 then
  print("Invalid duration! 1-3")
  error()
end
if qty > 8 then
  print("Too many items!")
  error()
end
if star == true and large == true then
print("Can't combine Large and Star")
error()
end
modem = peripheral.wrap("top")
print(string.format("Amount: %d",amt))
print(string.format("Duration: %d",dur))
modem.transmit(1,1,amt)
sleep(0.2)
modem.transmit(2,2,dur)
sleep(0.2)
modem.transmit(3,3,qty)
modem.open(4)
sleep(0.2)
for i=3,#tArgs do
	modem.transmit(5,5,tArgs[i])
	sleep(0.2)
end
print("Working..")

  for i=1,amt / multiple do
	redstone.setBundledOutput("back", fcolour+paperWire)
	sleep(0.1)
	redstone.setBundledOutput("back",0)
	sleep(0.5)
   end
   for i=1,((amt * dur) / multiple) + (amt/multiple) do
	redstone.setBundledOutput("back", gunpowderWire1)
	sleep(0.1)
	redstone.setBundledOutput("back",0)
	sleep(0.5)
   end
   local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
   if senderChannel == 4 then
   print(message)
   end


I have put most of the variables you'd need to modify around the top of the code. If you wish to change the name or add more colours, just look through both codes at the if statements, and copy/change them. Google the interactive sorter and it explains the numbers in the sorter.extract commands.

Example:

redWire is the wire colour in the bundled cable that connects to the red dye filter.

multiple is how many items it will craft at once, you need to have the same number of items in all your filters.

The rest should be self explanatory.

If you clog the system, you will need to clear the items out of the turtles and reboot them, and send the firework command from the main control computer again. The turtles need to know a bit of information before they can craft, so rebooting after you've sent the craft command won't work! You'll know if it has received the data as it'll print 3 numbers and the dyes/effects you chose each time you use the command.

How to use!

Well, if you've managed to make it through that far, now's the time to try it out! Do a final reboot on both turtles and the main control PC, then type <programname> <amount> <duration> <colours effects="">

Amount is how many fireworks to make, it'll need to be a multiple of your specified multiple, and be less than 64. Duration is 1-3, and colours/effects are the names you specified in the config. For example:

<programname> 32 3 red green yellow star twinkle

Will craft 32 fireworks with red, green and yellow, star shaped, and a twinkle effect, with a duration of 3. The speed it creates the fireworks is dependent upon your multiple, and the length of the pneumatic tubes. My setup can produce a stack of fireworks with a multiple of 16 with 8 colours/effects, and a duration of 3 in about 9 seconds.

Troubleshootings
SpoilerThe filters are being pulsed with redstone, but no items are coming out

Make sure that they are facing the correct way. The larger hole should be facing into the chest, and the smaller hole to the pneumatic tubes. Also make sure that the pneumatic pipes are connected to the chest, as they won't send items if they have nowhere to go.

Items are going into the turtle, but it isn't crafting!

Multiple things can cause this, first make sure the programs are actually running. 2nd, make sure that you see 3 numbers and the dye colours printed in the turtle GUI. 3rd, make sure that all the items in the turtle lie within slots 1-3, 5-7 and 9-11. Finally, check that there are actually the correct number of items in there, as the turtle won't craft anything even with 1 item missing. If the number of items is not correct, re-check your filters.

Also make sure that the turtle actually has a valid firework recipe. For example, the star and large ball effects can't be combined, which I have accounted for in the code.


The end!
Well, almost, don't forget to build it all into a firework shaped building :D/>



How it works
SpoilerThere were a lot of challenges in getting this system to work properly. Infact, one of the hurdles I hit was a bug that still exists in Minecraft 1.5.2 (Which I've reported to Mojang) regarding firework stacking.

In a basic form, the system goes like this:

You enter a few parameters into the main computer. This computer counts how many different types of items the turtle should get, sends that information to the turtle, then starts sending the items to replace the ones it's about to use.

The turtle will quickly collect the items it needs from the chest above the interactive sorter, and spread them around between slots 1-3, 5-7 and 9-11. The items need to be within this 3x3 grid for crafting to work. It then counts the total number of items, and compares it against the value given by the main computer. If it matches, it crafts the stars and puts
them back into slot 1.

Originally it crafted them one by one, however they frequently would not stack, messing up the rest of the process. The only workaround to this was to craft the whole lot at once. It's a heap faster, too.

One the turtle has crafted the stars, it determines the flight duration you selected, and sucks in the correct amount of gunpowder, and spreads it between slots 2, 3 and 5. It then sucks in paper to slot 6, checks that slots 2, 3 or 5 have the correct number of items, and that the total number of items match, and it's done!

The main computer is still in the program, waiting for a modem message event, which is the turtle saying it has finished crafting the rockets. It then exits, and is ready for another run.

——-

This is a lot of fun to play with, and I really like being able to craft such a wide variety of fireworks from a computer. If you try it out yourself, show me your setup! :)/>

Of course, there are a few limitations with this setup:
- Can only craft up to 1 stack at a time
- Can only have 1 star per rocket
- No fade effects yet, but might add them later
- If you don't have sufficient resources to complete the crafting, the turtles will sit there clogged until you remove the items and reboot them (and re-run the program on the main PC). I may eventually add a timeout so they spit the items out if they're sitting for too long.

Let me know what you think. The code is bound to have a few bugs, but it's working fine for me so far :)/>/&amp;gt;/&amp;gt;

EDIT: Updated system to not be reliant on item timing, using an interactive sorter instead. Thanks to "UNOBTAINIUM" for the idea.

Double edit: Have completely re-done the system thanks to suggestions, it now operates much faster and more efficiently, requiring only 1 turtle vs 2.</programname></colours></duration></amount></programname>

Update 11/06: Fixed only sending 3 lots of gunpowder.
superaxander #2
Posted 10 June 2013 - 06:04 AM
Really nice now i Will have a lot of firework shows around my house well done!
Things #3
Posted 10 June 2013 - 06:21 AM
Thanks, I have a lot of fun with this setup, so thought I'd share it for anyone keen enough to read the block of text :D/>

I just added a small section about how the system works, in case anyone has any suggestions on making it a bit less of a pain to set up. I know there is a addon mod that lets turtles get item ID's, but it'd be nice to have it work reliably without that. Once you have it set up right and the timing sorted out, it should be pretty reliable.
unobtanium #4
Posted 10 June 2013 - 06:44 AM
Wall of Text >.< This is the craziest thing i ever seen on these forums :D/>
I see a lot of work was involved into this! Well done.

The Interactive Sorter give you id identical ids. Let the user give it a name and the turtles know with what they are dealing with.
Things #5
Posted 10 June 2013 - 06:54 AM
Yeah, I could probably eliminate 1/2 that text if you could just pump items into the 2nd turtle in whatever order and have it still work. Might have to have a look into the interactive sorters.

Originally I just set it up so the turtle always kept 1 paper and 1 gunpowder in it's inv, so it always knew where they were going to stack, but it was annoying only being able to craft 63 at once.

EDIT: OK, I've figured out a way to do this with the interactive sorter. I've updated the first post with new code, screenshot and fixed up the text a bit. Hopefully it doesn't sound like such a scary project anymore :P/>
unobtanium #6
Posted 10 June 2013 - 08:44 AM
You also could store stuff above and underneath the turtle for quick access. If you save what the turtle placed into the chest into an array and load it afterwards while sucking down it allways knows what to deal with again or how many times it has to suck until it has what it needs.
Try to play around with it ;D

SpoilerOne idea with it am having:
A chest above the turtle and one underneath. For example 3 different items are in the top chest. You have an array like this:
topChest = { [1]="Gunpowder", [2]="Paper", [3]="Diamond" , [4]="Firework Star" //ECT}
You have a second array for the other chest:
bottomChest = {}
One last one for the turtles inventory 1-16
inv = { [1]="" , [2]="", //ECT }

Turtle would check the next empty slot in its inventory and suck down from above.
Slot 1 is free and Gunpowder gets sucked in and saves into the inventory array.. Now it checks if it needs gunpowder. Yes it does and keep it.
topChest = { [1]="", [2]="Paper", [3]="Diamond", [4]="Firework Star" }
inv = { [1]="Gunpowder" , [2]="", [3]="" }
bottomChest = {}

It selects the next free slot 2 and sucks paper in it. It needs it.
topChest = { [1]="", [2]="", [3]="Diamond", [4]="Firework Star" }
inv = { [1]="Gunpowder" , [2]="Paper", [3]="" }
bottomChest = {}

But now it doesnt need Diamonds. It is sucking it into slot 3 and dumps it right down into the bottom chest. Now it dumps Firework Star into slot 3 and it is finished.
topChest = { [1]="", [2]="", [3]="", [4]="" }
inv = { [1]="Gunpowder" , [2]="Paper", [3]="Firework Star" }
bottomChest = {[1]="Diamond"}

After finishing the crafting, the slots get refilled overflow gets dropped and everything gets dumped into the top chest again.
Things #7
Posted 10 June 2013 - 08:55 AM
Indeed, the biggest delay in the system now is the pneumatic tubes. I was considering putting interactive sorters on all the chests instead of filters, but I don't think I'll bother. It can craft fireworks _MUCH_ faster than my sugarcane/dye/gunpowder farms can supply resources. Using the interactive sorter dropped my time to make a full stack down another 4 seconds, bring total time to 21 seconds from hitting enter on the control computer, to having fireworks in the chest :)/>
unobtanium #8
Posted 10 June 2013 - 09:15 AM
Wouldnt it better to have the items as near as possible at the turtle? Like a buffer? Having a stack allways in a chest and if this is going to run out you ask for more in a free time.
Things #9
Posted 10 June 2013 - 09:22 AM
Yeah, although you'd need to somehow make sure there was always a free slot for that item after the turtle pulled it out. I suppose it wouldn't be too difficult using the item sorter's list function.

What would be REALLY awesome is if the interactive sorter could act like Redpower's retriever :D/>

Now you've inspired me, gonna try get this design down to a single turtle with a buffer chest. Really shouldn't take much changing at all.
unobtanium #10
Posted 10 June 2013 - 09:33 AM
The computer on the sides could, couldnt they?
Things #11
Posted 10 June 2013 - 12:22 PM
Yeah, it can go anywhere you want, you'd just have to change the code on the turtle to account for it. I put it on top because I am intending on running some pipes behind it.

I just made a huge update to the first post, pretty much eliminating the first turtle and having a single one perform both tasks. As such, from hitting enter on the computer to having a stack of fireworks, takes less than 9 seconds. Awesome :D/>
RoD #12
Posted 21 June 2013 - 08:07 AM
Very useful program :D/> i alaways want to craft some fire works but they take a lot of time to craft. This program is simply one of the most usefull prgrams i've ever seen. Good work!
svdragster #13
Posted 21 June 2013 - 10:02 AM
Nice job! It's a pain to craft fireworks.