SHOW:
|
|
- or go back to the newest paste.
1 | - | --Enchanting Turtle V0.2 by QQII |
1 | + | --Enchanting Turtle V0.3 by QQII |
2 | --Declerations | |
3 | - | local enchantLevel = 0 --The level the turtle initially attemps to perform an enchant at |
3 | + | local Arguments = { ... } --Program arguments |
4 | local enchantLevel = tonumber(Arguments[1]) or 0 --The level the turtle initially attemps to perform an enchant at | |
5 | local enchants = 0 --The number of enchants performed by the turtle | |
6 | - | |
6 | + | |
7 | local level = 0 --Requested by Perry | |
8 | ||
9 | --Wrap peripherals | |
10 | function wrapPeripheral(thing) | |
11 | local Sides = { | |
12 | "left","right", | |
13 | "top","bottom", | |
14 | "front","back", | |
15 | } | |
16 | ||
17 | for i,v in pairs(Sides) do | |
18 | if (peripheral.isPresent(v) and peripheral.getType(v) == thing) then | |
19 | print("'"..thing.."' found on side '"..v.."'.") | |
20 | return peripheral.wrap(v), v | |
21 | end | |
22 | end | |
23 | ||
24 | print("ERROR: '"..thing.."' not found!") | |
25 | return nil, nil | |
26 | - | local function toLevel(experience) |
26 | + | |
27 | - | if experience <= 272 then |
27 | + | |
28 | - | return experience / 17 |
28 | + | |
29 | - | elseif experience > 272 and experience <= 825 then |
29 | + | |
30 | - | return 9.84 + 0.17 * math.sqrt(24 * experience - 5159) |
30 | + | |
31 | os.shutdown() | |
32 | - | return 21.6 + 0.075 * math.sqrt(56 * experience - 32511) |
32 | + | |
33 | xp.setAutoCollect(true) --Sets the turtle to automatically collect xp | |
34 | ||
35 | --Functions | |
36 | local function clearSlot(slotNumber) --Attemps to clear the slot slotNumber | |
37 | if turtle.getItemCount(slotNumber) ~= 0 then | |
38 | turtle.select(slotNumber) | |
39 | - | if not turtle.drop() then |
39 | + | if turtle.dropDown() then |
40 | return true | |
41 | else | |
42 | print("ERROR: failure to clear slot number " .. slotNumber) | |
43 | return false | |
44 | end | |
45 | else | |
46 | return true | |
47 | end | |
48 | end | |
49 | ||
50 | local function clearSlots() --Places all the items into the chest under the turtle | |
51 | for i = 1,16 do | |
52 | if turtle.getItemCount(i) ~= 0 then | |
53 | - | local function getItem() --Attempts to take something from the chest above the turtle |
53 | + | |
54 | end | |
55 | end | |
56 | - | if not turtle.suck() then |
56 | + | |
57 | - | print("ERROR: failure to obtain item to enchant") |
57 | + | |
58 | local function getItem() | |
59 | - | if turtle.getItemCount(slot) ~= 1 then |
59 | + | if clearSlot(slot) then |
60 | - | if not turtle.drop(turtle.getItemCount(slot) - 1) then |
60 | + | turtle.select(slot) |
61 | - | print("ERROR: failure to reduce slot number " .. slot .. " to one item") |
61 | + | if turtle.suck() then |
62 | if turtle.getItemCount(slot) ~= 1 and turtle.getItemCount(slot) ~= 0 then | |
63 | if turtle.drop(turtle.getItemCount(slot) - 1) then | |
64 | return true | |
65 | else | |
66 | print("ERROR: failure to reduce slot number " .. slot .. " to one item") | |
67 | return false | |
68 | end | |
69 | end | |
70 | else | |
71 | print("ERROR: failure to obtain item to enchant") | |
72 | return false | |
73 | end | |
74 | end | |
75 | end | |
76 | ||
77 | local function enchant() --Attemps to enchant the item | |
78 | turtle.select(slot) | |
79 | if xp.enchant(enchantLevel) then | |
80 | enchants = enchants + 1 | |
81 | print("Enchants Performed: " .. enchants) | |
82 | end | |
83 | clearSlots() | |
84 | end | |
85 | ||
86 | --User Inputs | |
87 | print("The turtle will attack mobs above it.") | |
88 | print("It will look for items to enchant in the inventory infront of it.") | |
89 | print("It will place enchanted items in an inventory below it.") | |
90 | io.write("Level to Enchant: ") | |
91 | if enchantLevel <= 0 then | |
92 | while enchantLevel <= 0 do | |
93 | enchantLevel = tonumber(read()) | |
94 | end | |
95 | - | |
95 | + | |
96 | print(enchantLevel) | |
97 | - | while true do |
97 | + | |
98 | - | while not turtle.attackUp() do |
98 | + | |
99 | - | xp.collect() |
99 | + | |
100 | while true do | |
101 | while xp.getLevels() < enchantLevel do | |
102 | turtle.attack() | |
103 | if level ~= xp.getLevels() then | |
104 | print("Level: " .. xp.getLevels()) | |
105 | end | |
106 | level = xp.getLevels() | |
107 | os.sleep(0.1) | |
108 | end | |
109 | if xp.getLevels() >= enchantLevel then | |
110 | print("Enchanting...") | |
111 | getItem() | |
112 | enchant() | |
113 | end | |
114 | end |