mineserver.lua

213 lines · 5.5 KB

Open raw

PHONE SERVER--

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/Maengorn/mineserver.lua
1--PHONE SERVER--
2
3local SERVER_PORT = 420
4local CLIENT_PORT = 0
5local SLOT_COUNT = 16
6
7
8local segmentation = 5
9if (#arg == 1) then
10 segmentation = tonumber(arg[1])
11elseif (#arg == 0) then
12 print(string.format("No segmentation size selected, defaulting to %d", segmentation))
13else
14 print('Too many args given...')
15 exit(1)
16end
17
18
19local modem = peripheral.wrap("right")
20modem.open(SERVER_PORT)
21
22local target = vector.new()
23local size = vector.new()
24local finish = vector.new()
25
26-- I STOLE --
27function split (inputstr, sep)
28 if sep == nil then
29 sep = "%s"
30 end
31 local t={}
32 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
33 table.insert(t, str)
34 end
35 return t
36end
37
38function parseParams(data)
39 coords = {}
40 params = split(data, " ")
41
42 coords[1] = vector.new(params[1], params[2], params[3])
43 coords[2] = vector.new(params[4], params[5], params[6])
44
45 return (coords)
46end
47
48function getItemIndex(itemName)
49 for slot = 1, SLOT_COUNT, 1 do
50 local item = turtle.getItemDetail(slot)
51 if(item ~= nil) then
52 if(item["name"] == itemName) then
53 return slot
54 end
55 end
56 end
57end
58
59function checkFuel()
60 turtle.select(1)
61
62 if(turtle.getFuelLevel() < 50) then
63 print("Attempting Refuel...")
64 for slot = 1, SLOT_COUNT, 1 do
65 turtle.select(slot)
66 if(turtle.refuel(1)) then
67 return true
68 end
69 end
70 return false
71 else
72 return true
73 end
74end
75
76function deployFuelChest()
77 if (not checkFuel()) then
78 print("SERVER NEEDS FUEL...")
79 exit(1)
80 end
81end
82
83
84function deploy(startCoords, quarySize, endCoords, options)
85 --Place turtle from inventory
86 turtle.select(getItemIndex("computercraft:turtle_advanced"))
87 while(turtle.detect()) do
88 os.sleep(0.3)
89 end
90
91 --Place and turn on turtle
92 turtle.place()
93 peripheral.call("front", "turnOn")
94
95
96 --Wait for client to send ping
97 event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
98 if(msg ~= "CLIENT_DEPLOYED") then
99 print("No client deploy message, exitting...")
100 os.exit()
101 end
102
103
104 if(options["withStorage"]) then
105 --Set up ender chest
106 if (not checkFuel()) then
107 print("SERVER NEEDS FUEL...")
108 exit(1)
109 end
110 end
111
112 deployFuelChest()
113 local storageBit = options["withStorage"] and 1 or 0
114
115 -- Client is deployed
116 modem.transmit(CLIENT_PORT,
117 SERVER_PORT,
118 string.format("%d %d %d %d %d %d %d %d %d %d",
119 startCoords.x, startCoords.y, startCoords.z,
120 quarySize.x, quarySize.y, quarySize.z,
121 endCoords.x, endCoords.y, endCoords.z,
122 storageBit
123 ))
124end
125
126
127
128-- Return array of arbitrary size for each bot placement
129function getPositioningTable(x, z, segmaentationSize)
130 local xRemainder = x % segmaentationSize
131 local zRemainder = z % segmaentationSize
132
133 local xMain = x - xRemainder
134 local zMain = z - zRemainder
135
136 xRemainder = (xRemainder == 0 and segmaentationSize or xRemainder)
137 zRemainder = (zRemainder == 0 and segmaentationSize or zRemainder)
138
139 local positions = {}
140
141 for zi = 0, z - 1 , segmaentationSize do
142 for xi = 0, x - 1, segmaentationSize do
143
144 local dims = {xi, zi, segmaentationSize, segmaentationSize}
145 if(xi >= x - segmaentationSize and xi <= x - 1 ) then
146 dims = {xi, zi, xRemainder, segmaentationSize}
147 end
148
149 if(zi >= z - segmaentationSize and zi <= z - 1 ) then
150 dims = {xi, zi, segmaentationSize, zRemainder}
151 end
152
153 table.insert(positions, dims)
154 end
155 end
156
157 return table.pack(positions, xRemainder, zRemainder)
158end
159
160while (true) do
161 -- Wait for phone
162 print("Waiting for target signal...")
163 event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
164
165 -- Parse out coordinates and options
166 local args = split(msg, " ")
167 local withStorage = args[#args]
168 withStorage = withStorage == "1" and true or false
169 data = parseParams(msg)
170 options = {}
171 options["withStorage"] = True
172
173 target = data[1]
174 size = data[2]
175
176 finish = vector.new(gps.locate())
177 finish.y = finish.y + 1
178 print(string.format( "RECEIVED QUARY REQUEST AT: %d %d %d", target.x, target.y, target.z))
179
180 tab, xDf, zDf = table.unpack(getPositioningTable(size.x, size.z, segmentation))
181
182 print(string.format("Deploying %d bots...", #tab))
183 for i = 1, #tab, 1 do
184 xOffset, zOffset, width, height = table.unpack(tab[i])
185 local offsetTarget = vector.new(target.x + xOffset, target.y, target.z + zOffset)
186 local sclaedSize = vector.new(width, size.y, height)
187
188 deploy(offsetTarget, sclaedSize, finish, options)
189 os.sleep(1)
190 print(string.format( "Deploying to; %d %d %d %d %d", target.x + xOffset, target.y, target.z + zOffset, sclaedSize.x, sclaedSize.z))
191 end
192
193 -- All bots deployed, wait for last bot finished signal
194 event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
195 turtle.digUp()
196 turtle.turnRight()
197 turtle.forward(1)
198 turtle.turnLeft()
199 turtle.select(getItemIndex("enderstorage:ender_storage"))
200 endercount = (turtle.getItemCount() - 2)
201 if (endercount ~= 0) then
202 print(string.format("Depositing %d Ender Chests.", endercount))
203 turtle.drop(endercount)
204 end
205
206 turtle.turnLeft()
207 turtle.forward(1)
208 turtle.turnRight()
209
210
211end
212
213