-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoetle.lua
149 lines (128 loc) · 4.3 KB
/
toetle.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
if turtle then return end -- We're not needed so we'll just exit
_G.turtle = {} -- Create dummy API
local turtle = _G.turtle
turtle.toetle = {} -- Insert our subAPI which will make the other one work
turtle.toetle.lists = {} -- Where we store our responces
turtle.toetle.indexes = {}
turtle.toetle.states = {}
turtle.toetle.nativeStates = {}
turtle.toetle.knownTurtleFunctions = {}
-- predeclare state handling functions
local loopHandler
local randomHandler
local interactiveHandler
-- cache for quicker access
local toetle = turtle.toetle
local lists = toetle.lists
local indexes = toetle.indexes
local states = turtle.toetle.states
local nativeStates = turtle.toetle.nativeStates
local knownTurtleFunctions = turtle.toetle.knownTurtleFunctions
nativeStates.loop = loopHandler
nativeStates.random = randomHandler
nativeStates.interactive = interactiveHandler
-- Fill list of turtle functions
do
knownTurtleFunctions.craft = true
knownTurtleFunctions.forward = true
knownTurtleFunctions.back = true
knownTurtleFunctions.up = true
knownTurtleFunctions.down = true
knownTurtleFunctions.turnLeft = true
knownTurtleFunctions.turnRight = true
knownTurtleFunctions.select = true
knownTurtleFunctions.getSelectedSlot = true
knownTurtleFunctions.getItemCount = true
knownTurtleFunctions.getItemSpace = true
knownTurtleFunctions.getItemDetail = true
knownTurtleFunctions.equipLeft = true
knownTurtleFunctions.equipRight = true
knownTurtleFunctions.attack = true
knownTurtleFunctions.attackUp = true
knownTurtleFunctions.attackDown = true
knownTurtleFunctions.dig = true
knownTurtleFunctions.digUp = true
knownTurtleFunctions.digDown = true
knownTurtleFunctions.place = true
knownTurtleFunctions.placeUp = true
knownTurtleFunctions.placeDown = true
knownTurtleFunctions.inspect = true
knownTurtleFunctions.inspectUp = true
knownTurtleFunctions.inspectDown = true
knownTurtleFunctions.compare = true
knownTurtleFunctions.compareUp = true
knownTurtleFunctions.compareDown = true
knownTurtleFunctions.compareTo = true
knownTurtleFunctions.drop = true
knownTurtleFunctions.dropUp = true
knownTurtleFunctions.dropDown = true
knownTurtleFunctions.suck = true
knownTurtleFunctions.suckUp = true
knownTurtleFunctions.suckDown = true
knownTurtleFunctions.refuel = true
knownTurtleFunctions.getFuelLevel = true
knownTurtleFunctions.getFuelLimit = true
knownTurtleFunctions.transferTo = true
end
-- Generic handler functions for responce types
function loopHandler()
-- Return the next responce from the list
local r = lists.TEMPLATE[indexes.TEMPLATE]
indexes.TEMPLATE = indexes.TEMPLATE + 1
if lists.TEMPLATE[indexes.TEMPLATE] == nil then
indexes.TEMPLATE = 1
end
return unpack(r)
end
function randomHandler()
-- Return a random responce from the list
return unpack(list.TEMPLATE[math.random(#list.TEMPLATE)])
end
function interactiveHandler()
-- Prompt user for input
end
-- add turtle functions
-- TODO - template mostly done
-- http://computercraft.info/wiki/Turtle_(API)
local turtleMeta = {}
local toetleMeta = {}
setmetatable(turtle, turtleMeta)
setmetatable(turtle.toetle, toetleMeta)
function turtleMeta.__index(t,k,...) -- When users call a turtle function we'll need to find it, arguments are then passed 'magically'
if (not states[k]) then
error("Toetle: function "..k.." is not initialised")
else
if type(states[k]) ~= "function" then
error("Toetle: state must be a function",2)
else
return state[k] -- found the function
end
end
end
function toetleMeta.__index(t,k) -- Initialise turtle functions
return function(...)
-- Look up in known turtle functions
local args = {...}
if not knownTurtleFunctions[k] then
error("Toetle: not a known turtle function, check your spelling. If it's a new function then you can add it to the list (turtle.toetle.knownTurtleFunctions."..k.." = true) and Toetle will do the rest.",2)
else
if not states.k then
states.k = nativeStates.loop
end
if not indexes.k then
indexes.k = 1
end
if not lists.k then
lists.k = {}
end
if not args[1] then
-- user wants nil
lists.k[indexes.k] = {}
else
-- User has given data which needs to be added to the list of responces
lists.k[indexes.k] = args
end
end
end
end
return turtle -- Just in case people want to load us this way