Advertisement
Klazam33

Mobfarm

Mar 3rd, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.59 KB | None | 0 0
  1. --Mobfarm, as written by Klazam
  2.  
  3. --Section 1: Wrapping any availble peripherals
  4. --This section hooks up peripherals to the turtle itself, and gives them names for functions related to them
  5. anvil = peripheral.wrap("right")
  6. xp = peripheral.wrap("left")
  7.  
  8.  
  9. --Section 2: Setting up function library
  10. --This section is basically a library of "shortcuts", lines of code that you keep on repeating, and would like to shorten.
  11.  
  12. --This function clears the screen and resets the cursor position
  13. function clear()                  --This line lets the program know you're setting up a function, and naming it. The () is required at the end of the name of that function.
  14.  term.clear()                     --This clears the screen
  15.  term.setCursorPos(1,1)           --Sets the cursor positon to X,Y (1,1 is top left of the screen)
  16. end                               --Lets the program know the function is ended
  17.  
  18. --This function waits until a specific key is pressed. (In most cases, the key is #28: enter. Refer to CC wiki (http://computercraft.info/wiki/File:CC-Keyboard-Charcodes.png)
  19. function rawread(x)                         -- The () at the end of a function can contain a variable or a string. Numberical variables (like this one will be) is written as such functionname(xyz) and string (letters) is written like this: functionname("xyz")
  20.  while true do                              --This sets up an infinte loop- this function will keep on going until something stops it
  21.   local sEvent, param = os.pullEvent("key") --Tells the turtle/computer to wait until a key is pressed, and assigns "key" to the param variable
  22.   if sEvent == "key" then                   --Checks if "sEvent" matches "key". if this is true, do the next thing. if not, do something else. In this case there isnt anything else. So it'll end the function and loop back to the start, thanks to while true do.
  23.    if param == x then                       --Checks if param is the value you assigned to x, earlier
  24.     break                                   --If the above two conditions are satifised, exit the loop.
  25.    end                                        
  26.   end
  27.  end
  28. end
  29.  
  30. --This function waits until ANY key is pressed, and tells you what that key was.
  31. function keyread()
  32.  while true do
  33.   local sEvent, param = os.pullEvent("key")
  34.   if sEvent == "key" then
  35.    return(param)                             --The "return" term means the function, upon completion, will have a value assigned to it. You'll learn why shortly
  36.   end
  37.    break
  38.   end
  39.  end
  40.  
  41. -- Section 3: The ACTUAL program the computer/turtle will run
  42. --Now for the program itself, now that we have set up an function library.
  43. --There are two parts to any program- one that runs once on activating the program, and one that constanly runs. Some programs may have only one or the other.
  44.  
  45. --This section runs only once
  46. --This calls on peripheal's function, in this case the "xp" peripheal. (this command sets the turtle to always collect any nearby xp orbs in a 2 block radius)
  47. xp.setAutoCollect(true)
  48.  
  49. --This section uses a loop ("while true do" sets up an infinite loop, which starts on activating this program) to run constanly after starting up
  50.  
  51. while true do
  52.  print("Hello! I am Merlin!\nYou can get items, enchant your items, anvil them!\nPress Enter to use my services.") --print prints a line of text to the screen. Use \n to have line breaks, and \t to have indentations
  53.  rawread(28)                                -- this calls the function "rawread(x)" with x assigned to 28, which is the enter key. The purpose of this is to just have the program idle while waiting for input to proceed.
  54.  clear()                                    -- Calls the "clear()" function                                
  55.  print("Press M to activate Mob Farm\nPress E to activate Enchanting Mode\nPress A to activate Anvil Mode\nPress B to activate Book Mode\nPress L to get Total Levels that I have")
  56.  x = keyread()                              --Recall the return in the "keyread()" function? This is where it comes in. The program assigns a variable- x in this case- to whatever keyread() returned.
  57.  clear()
  58.  if x == 50 then                            --This checks if the key pressed is "M"
  59.   print("Mob Farm Selector:\n\t1: Wither Skeleton\n\t2: Angry Zombie\n\t3: Skeleton\n\t4: Creeper\n\t5: Zombie Pigman\n\t6: Blaze\nPlease press the corresponding key to activate that Mob Grinder")
  60.   x = keyread()                             --Reads the key again
  61.   clear()
  62.   if x == 2 then                            --Checks the key again, in this case looking for the key "1"
  63.    rs.setBundledOutput("back",colors.white) --This turns the white cable in a bundled cable to "on"
  64.    print("Wither Skeleton \nFarm initialized, press Enter to shut down \n\nYou WILL not be getting wither skull drops. \nSee Klazam33 if you want some. \Reason must be provided.")
  65.    rawread(28)                              --Waits
  66.    l = xp.getLevels()                       --Calls a peripheral function and assigns the value it returns to give you the variable "l"
  67.    rs.setBundledOutput("back",0)            --Turns off the whole bundled cable
  68.    print("\nFarm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.") --Prints a line of text, with the variable "l" in it. Syntax is as follows: print("blah".. variable .. "blah")
  69.    rawread(28)
  70.   elseif x == 3 then --2
  71.    rs.setBundledOutput("back",colors.black)
  72.    print("Angry Zombie \nFarm initialized, press Enter to shut down")
  73.    rawread(28)
  74.    l = xp.getLevels()
  75.    rs.setBundledOutput("back",0)
  76.    print("Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
  77.    rawread(28)
  78.  elseif x == 4 then --3
  79.    rs.setBundledOutput("back",colors.red)
  80.    print("Skeleton \nFarm initialized, press Enter to shut down")
  81.    rawread(28)
  82.    l = xp.getLevels()
  83.    rs.setBundledOutput("back",0)
  84.    print("Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
  85.    rawread(28)
  86.   elseif x == 5 then --4
  87.    rs.setBundledOutput("back",colors.yellow)
  88.    print("Creeper \nFarm initialized, press Enter to shut down\n\nYou WILL not be getting gunpowder drops. \nSee Klazam33 if you want some. \Reason must be provided.")
  89.    rawread(28)
  90.    l = xp.getLevels()
  91.    rs.setBundledOutput("back",0)
  92.    print("\n Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
  93.    rawread(28)
  94.   elseif x == 6 then --5
  95.    rs.setBundledOutput("back",colors.green)
  96.    print("Zombie Pigman \nFarm initialized, press Enter to shut down")
  97.    rawread(28)
  98.    l = xp.getLevels()
  99.    rs.setBundledOutput("back",0)
  100.    print("Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
  101.    rawread(28)
  102.   elseif x == 7 then --6
  103.    rs.setBundledOutput("back",colors.gray)
  104.    print("Blaze \nFarm initialized, press Enter to shut down")
  105.    rawread(28)
  106.    l = xp.getLevels()
  107.    rs.setBundledOutput("back",0)
  108.    print("Farm deactivated. \nCurrent Levels:" .. l .. "\nPress Enter to return to main menu.")
  109.    rawread(28)
  110.   elseif x == 1 then                  -- Makes ESC key do nothing
  111.   else                                 --What if keyread() doesn't return a key that you want? Instead of just ending the program and looping back to the start, leaving you unsure if stuff worked, this section lets you know you pressed something wrong.
  112.    print("Not valid input!\nPress Enter to return to main menu.")
  113.    rawread(28)
  114.   end
  115.  elseif x == 38 then --L
  116.   l = xp.getLevels()
  117.   print("Levels I have:" .. l .. "\nPress Enter to return to main menu.")
  118.   rawread(28)
  119.  elseif x == 18 then --E
  120.   print("Enchanting mode:\nPlease put item to be enchanted in Slot 1 \nPress Enter when ready to proceed.")
  121.   rawread(28)
  122.   if turtle.getItemCount(1) == 1 then --This checks how much items are in (Slot). In this case, it's slot 1.
  123.    turtle.select(1)                   --Selects slot 1
  124.    xp.enchant(30)                     --Calls a peripheral function and runs it.
  125.    turtle.dropDown()                  --Drops item from selected slot, to the bottom of the turtle. Either it'll go on the ground or it'll go in an inventory such as an chest. Pipes will not work
  126.    print("Done, please take item from chest.\nPress Enter to return to main menu.")
  127.    rawread(28)
  128.   else                                --If there's too many items in slot one or too few items, things would mess up, so this "else" lets you know theres something wrong and doesn't execute the program
  129.    print("Too many items or not enough items!\nPress Enter to return to main menu.")
  130.    rawread(28)
  131.   end
  132.  elseif x == 30 then --A
  133.   print("Anvil mode:\nPlease put items to be repaired in Slot 2 and 3\nPress Enter when ready to proceed.")
  134.   rawread(28)
  135.   if turtle.getItemCount(2) == 1 and turtle.getItemCount(3) == 1 then     -- You can have multiple conditional statements by linking them together with and, or, nor. and means when BOTH is true, do, or means when ONE is true, do, nor means when BOTH is NOT true, do
  136.    turtle.select(2)
  137.    anvil.repair(2,3)
  138.    turtle.dropDown()
  139.    print("Done, please take item from chest.\nPress Enter to return to main menu.")
  140.    rawread(28)
  141.   else
  142.    print("Too many items or not enough items!\nPress Enter to return to main menu.")
  143.    rawread(28)
  144.   end
  145.  elseif x == 48 then --B
  146.   l = xp.getLevels()
  147.   if l > 0 then                  --If l is greater than 0, then do
  148.    y = math.floor(l / 30)        --Some maths. math.floor(variable) rounds down numbers to the greatest whole number, instead of having decimals.
  149.   else
  150.    y = 0                         --This skips the math part above. I dont want to risk a division by 0 error.
  151.   end
  152.   print("Book Mode:\nPlace books in slot 15\nEnsure slot 16 is empty\nMax books you can enchant at this time is " .. y .. "\nPress Enter when ready to proceed.")
  153.   rawread(28)
  154.   if turtle.getItemCount(15) > 0 and turtle.getItemCount(16) == 0 then
  155.    print("Running program, please be patient.")
  156.    repeat                       -- This is a loop that repeats until a specific condition is true
  157.     turtle.select(15)
  158.     turtle.transferTo(16,1)     --Transfers items from selected slot to (slot,quantity)
  159.     turtle.select(16)
  160.     xp.enchant(30)
  161.     turtle.dropDown()
  162.    until turtle.getItemCount(15) == 0 --This is the condition in which the loop ends.
  163.    print("Books done, and in chest under me. Please take them all with you.\nPress Enter to return to main menu.")
  164.    rawread(28)
  165.   else
  166.    print("Not enough items or items in wrong slot!\nPress Enter to return to main menu.")
  167.    rawread(28)
  168.   end
  169.  elseif x == 1 then
  170.  else
  171.   print("Not valid input!\nPress Enter to return to main menu.")
  172.   rawread(28)
  173.  end
  174.  clear()
  175. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement