forked from minetest-edu/turtleminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptvm.lua
103 lines (96 loc) · 2.19 KB
/
scriptvm.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
local function parse_statement(line, lineno)
if line == "left" or line == "turn left"
or line == "l" or line == "<" then --HJG
return {
command = "rotate",
params = "left"
}
elseif line == "right" or line == "turn right"
or line == "r" or line == ">" then --HJG
return {
command = "rotate",
params = "right"
}
elseif line == "forward" or line == "backward"
or line == "f" or line == "b" --HJG
or line == "up" or line == "down"
or line == "u" or line == "d" then
return {
command = "move",
params = line
}
elseif line == "build" or line == "place front" then
return {
command = "build",
params = "front"
}
elseif line == "place below" then
return {
command = "build",
params = "below"
}
elseif line == "dig" or line == "dig front" then
return {
command = "dig",
params = "front"
}
elseif line == "dig below" then
return {
command = "dig",
params = "below"
}
elseif line == "dig above" then --HJG
return {
command = "dig",
params = "above"
}
else
return nil
end
end
function turtleminer.build_script(owner, t_id, source)
local lines = source:split("\n")
local statements = {}
local lineno = 1
for _, line in pairs(lines) do
line = line:trim()
if string.sub(line,1,2) == "--" then line = "" end -- ignore lines with comment
if line ~= "" then
local res = parse_statement(line, lineno)
if res then
statements[#statements + 1] = res
else
print("Error at line " .. lineno .. ": " .. line)
return "Error at line " .. lineno .. ": " .. line
end
end
lineno = lineno + 1
end
local errored = false
local pointer = 1
return {
next_statement = function(self)
if pointer <= #statements then
local stmt = statements[pointer]
pointer = pointer + 1
return stmt
end
end,
is_alive = function(self)
return pointer <= #statements and not errored
end,
step = function(self)
if errored then
return
end
local stmt = self:next_statement()
if stmt then
local pos = turtleminer.turtles[t_id].pos
local res = turtleminer.run_command(owner, pos, stmt.command, stmt.params)
if not pos then
errored = true
end
end
end,
}
end