-
Notifications
You must be signed in to change notification settings - Fork 14
/
ocwe.lua
223 lines (213 loc) · 6.99 KB
/
ocwe.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
local component = require("component")
if not component.isAvailable("debug") then
errprint("Debug card is required")
return
end
local player, username
io.stdout:write("Input player name: ")
while true do
player = io.read()
local health,err = component.debug.getPlayer(player).getHealth()
if not health or err == "player is offline" then
print("No such player")
io.stdout:write("Input player name: ")
else
print("Bound OCWE to " .. player)
username = player
player = component.debug.getPlayer(player)
break
end
end
local world = component.debug.getWorld()
local info = {}
local function decodeBlock(block)
if block:match(".*:.+") then
local id,damage = block:match("(.*):(.+)")
if tonumber(id) ~= nil and tonumber(damage) ~= nil then
return tonumber(id), tonumber(damage)
end
else
if tonumber(block) ~= nil then
return tonumber(block), 0
end
end
end
while true do
io.stdout:write("> ")
local line = io.read()
if not line then break end
local parse = {}
for item in (line .. " "):gmatch("(.-) ") do
if item ~= "" then
parse[#parse + 1] = item
end
end
if parse[1] == nil then
elseif parse[1] == "pos1" then
local pX,pY,pZ = player.getPosition()
if not pX then
print("Unexpected error: " .. pY)
else
pX,pY,pZ = math.floor(pX),math.floor(pY),math.floor(pZ)
if info.pos1 == nil then
info.pos1 = {}
end
info.pos1[1] = pX
info.pos1[2] = pY
info.pos1[3] = pZ
local size
if info.pos1 and info.pos2 then
size = (math.abs(info.pos1[1]-info.pos2[1])+1) * (math.abs(info.pos1[2]-info.pos2[2])+1) * (math.abs(info.pos1[3]-info.pos2[3])+1)
end
print("Set pos1 to (" .. pX .. "," .. pY .. "," .. pZ .. ")" .. (size and (" (" .. size .. " blocks)") or ""))
end
elseif parse[1] == "pos2" then
local pX,pY,pZ = player.getPosition()
if not pX then
print("Unexpected error: " .. pY)
else
pX,pY,pZ = math.floor(pX),math.floor(pY),math.floor(pZ)
if info.pos2 == nil then
info.pos2 = {}
end
info.pos2[1] = pX
info.pos2[2] = pY
info.pos2[3] = pZ
local size
if info.pos1 and info.pos2 then
size = (math.abs(info.pos1[1]-info.pos2[1])+1) * (math.abs(info.pos1[2]-info.pos2[2])+1) * (math.abs(info.pos1[3]-info.pos2[3])+1)
end
print("Set pos2 to (" .. pX .. "," .. pY .. "," .. pZ .. ")" .. (size and (" (" .. size .. " blocks)") or ""))
end
elseif parse[1] == "set" then
local id, damage = decodeBlock(parse[2])
if parse[2] == nil then
print("Invalid argument, Expected: set block")
elseif not id or not damage then
print("Invalid block code")
elseif not info.pos1 then
print("Please set pos1")
elseif not info.pos2 then
print("Please set pos2")
else
local size = (math.abs(info.pos1[1]-info.pos2[1])+1) * (math.abs(info.pos1[2]-info.pos2[2])+1) * (math.abs(info.pos1[3]-info.pos2[3])+1)
print("Setting " .. size .. " block" .. (size == 1 and "" or "s"))
world.setBlocks(info.pos1[1],info.pos1[2],info.pos1[3],info.pos2[1],info.pos2[2],info.pos2[3],id,damage)
end
elseif parse[1] == "replace" then
local fid, fdamage = decodeBlock(parse[2])
local tid, tdamage = decodeBlock(parse[3])
if parse[2] == nil or parse[3] == nil then
print("Invalid argument, Expected: replace from-block to-block")
elseif not fid or not fdamage then
print("Invalid from-block code")
elseif not tid or not tdamage then
print("Invalid to-block code")
elseif not info.pos1 then
print("Please set pos1")
elseif not info.pos2 then
print("Please set pos2")
else
local blocks = 0
local x1,x2 = math.min(info.pos1[1],info.pos2[1]),math.max(info.pos1[1],info.pos2[1])
local y1,y2 = math.min(info.pos1[2],info.pos2[2]),math.max(info.pos1[2],info.pos2[2])
local z1,z2 = math.min(info.pos1[3],info.pos2[3]),math.max(info.pos1[3],info.pos2[3])
for y = y1,y2 do
for x = x1,x2 do
for z = z1,z2 do
local cid = world.getBlockId(x,y,z)
local cdamage = world.getMetadata(x,y,z)
if cid == fid and cdamage == fdamage then
world.setBlock(x,y,z,tid,tdamage)
blocks = blocks + 1
end
end
end
end
print("Replaced " .. blocks .. " block" .. (blocks == 1 and "" or "s"))
end
elseif parse[1] == "distr" then
if not info.pos1 then
print("Please set pos1")
elseif not info.pos2 then
print("Please set pos2")
else
local size = (math.abs(info.pos1[1]-info.pos2[1])+1) * (math.abs(info.pos1[2]-info.pos2[2])+1) * (math.abs(info.pos1[3]-info.pos2[3])+1)
local x1,x2 = math.min(info.pos1[1],info.pos2[1]),math.max(info.pos1[1],info.pos2[1])
local y1,y2 = math.min(info.pos1[2],info.pos2[2]),math.max(info.pos1[2],info.pos2[2])
local z1,z2 = math.min(info.pos1[3],info.pos2[3]),math.max(info.pos1[3],info.pos2[3])
local stats = {}
for y = y1,y2 do
for x = x1,x2 do
for z = z1,z2 do
local cid = world.getBlockId(x,y,z)
local cdamage = world.getMetadata(x,y,z)
local id = cid .. ":" .. cdamage
if stats[id] == nil then
stats[id] = 0
end
stats[id] = stats[id] + 1
end
end
end
local stats2 = {}
for k,v in pairs(stats) do
stats2[#stats2 + 1] = {k,v}
end
table.sort(stats2,function(a,b) return a[2] < b[2] end)
for i = 1,#stats2 do
print(string.format("%-8d%s",stats2[i][2],stats2[i][1]))
end
end
elseif parse[1] == "walls" then
local id, damage = decodeBlock(parse[2])
if parse[2] == nil then
print("Invalid argument, Expected: walls block")
elseif not id or not damage then
print("Invalid block code")
elseif not info.pos1 then
print("Please set pos1")
elseif not info.pos2 then
print("Please set pos2")
else
local size = "NaN" -- >_> why is this confusing.
print("Setting " .. size .. " block" .. (size == 1 and "" or "s"))
world.setBlocks(info.pos1[1],info.pos1[2],info.pos1[3],info.pos1[1],info.pos2[2],info.pos2[3],id,damage)
world.setBlocks(info.pos2[1],info.pos1[2],info.pos1[3],info.pos2[1],info.pos2[2],info.pos2[3],id,damage)
world.setBlocks(info.pos1[1],info.pos1[2],info.pos1[3],info.pos2[1],info.pos2[2],info.pos1[3],id,damage)
world.setBlocks(info.pos1[1],info.pos1[2],info.pos2[3],info.pos2[1],info.pos2[2],info.pos2[3],id,damage)
end
elseif parse[1] == "tp" then
if parse[2] == nil then
print("Invalid argument, Expected: tp player")
else
local top = component.debug.getPlayer(parse[2])
local health,err = top.getHealth()
if not health or err == "player is offline" then
print("No such player")
else
print("Teleporting " .. username .. " to " .. parse[2])
tX,tY,tZ = top.getPosition()
player.setPosition(tX,tY,tZ)
end
end
elseif parse[1] == "kill" then
if parse[2] == nil then
print("Invalid argument, Expected: kill player")
else
local top = component.debug.getPlayer(parse[2])
local health,err = top.getHealth()
if not health or err == "player is offline" then
print("No such player")
else
print("Killing " .. parse[2])
top.setHealth(-1)
end
end
elseif parse[1] == "quit" then
print("Goodbye!")
return
else
print("Unknown command: " .. parse[1])
end
end