jaklsfjlsak

手 雷

Feb 15th, 2021 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.35 KB | None | 0 0
  1. if(fs.exists("widget") == false) then
  2. shell.run("pastebin get chGnuHbZ widget")
  3. term.clear()
  4. end
  5. os.loadAPI("widget")
  6.  
  7. if not term.isColor() then
  8. print("Advanced computer required")
  9. error()
  10. end
  11.  
  12. local radar
  13. local sides = peripheral.getNames()
  14. for key,side in pairs(sides) do
  15. if peripheral.getType(side) == "warpdriveRadar" then
  16. print("Radar found on " .. side)
  17. radar = peripheral.wrap(side)
  18. end
  19. end
  20. if radar == nil or radar.isInterfaced() == nil then
  21. showErrorAndExit("No radar detected")
  22. end
  23.  
  24. local range
  25. if(fs.exists("range.settings") == false) then
  26. file = fs.open("range.settings", "w")
  27. file.write("100")
  28. file.close()
  29. end
  30. if(fs.exists("range.settings")) then
  31. file = fs.open("range.settings", "r")
  32. range = tonumber(file.readAll())
  33. file.close()
  34. end
  35.  
  36. shipName = {}
  37. shipX = {}
  38. shipY = {}
  39. shipZ = {}
  40. shipMass = {}
  41. shipRange = {}
  42.  
  43. for i=0, 27 do
  44. shipName[i]=""
  45. shipX[i]=0
  46. shipY[i]=0
  47. shipZ[i]=0
  48. shipMass[i]=0
  49. shipRange[i]=0
  50. end
  51.  
  52. function interface()
  53. term.clear()
  54. title = widget.newLabel("HQ Scanning System")
  55. row1 = widget.newRow()
  56. row1:add(title)
  57. ship1 = widget.newButton(shipName[0])
  58. ship2 = widget.newButton(shipName[1])
  59. ship3 = widget.newButton(shipName[2])
  60. row2 = widget.newRow()
  61. row2:add(ship1)
  62. function ship1.onClick(button)
  63. shipData(0)
  64. end
  65. row2:add(ship2)
  66. function ship2.onClick(button)
  67. shipData(1)
  68. end
  69. row2:add(ship3)
  70. function ship3.onClick(button)
  71. shipData(2)
  72. end
  73. ship4 = widget.newButton(shipName[3])
  74. ship5 = widget.newButton(shipName[4])
  75. ship6 = widget.newButton(shipName[5])
  76. row3 = widget.newRow()
  77. row3:add(ship4)
  78. function ship4.onClick(button)
  79. shipData(3)
  80. end
  81. row3:add(ship5)
  82. function ship5.onClick(button)
  83. shipData(4)
  84. end
  85. row3:add(ship6)
  86. function ship6.onClick(button)
  87. shipData(5)
  88. end
  89. ship7 = widget.newButton(shipName[6])
  90. ship8 = widget.newButton(shipName[7])
  91. ship9 = widget.newButton(shipName[8])
  92. row4 = widget.newRow()
  93. row4:add(ship7)
  94. function ship7.onClick(button)
  95. shipData(6)
  96. end
  97. row4:add(ship8)
  98. function ship8.onClick(button)
  99. shipData(7)
  100. end
  101. row4:add(ship9)
  102. function ship9.onClick(button)
  103. shipData(8)
  104. end
  105. ship10 = widget.newButton(shipName[9])
  106. ship11 = widget.newButton(shipName[10])
  107. ship12 = widget.newButton(shipName[11])
  108. row5 = widget.newRow()
  109. row5:add(ship10)
  110. function ship10.onClick(button)
  111. shipData(9)
  112. end
  113. row5:add(ship11)
  114. function ship11.onClick(button)
  115. shipData(10)
  116. end
  117. row5:add(ship12)
  118. function ship12.onClick(button)
  119. shipData(11)
  120. end
  121. ship13 = widget.newButton(shipName[12])
  122. ship14 = widget.newButton(shipName[13])
  123. ship15 = widget.newButton(shipName[14])
  124. row6 = widget.newRow()
  125. row6:add(ship13)
  126. function ship13.onClick(button)
  127. shipData(12)
  128. end
  129. row6:add(ship14)
  130. function ship14.onClick(button)
  131. shipData(13)
  132. end
  133. row6:add(ship15)
  134. function ship15.onClick(button)
  135. shipData(14)
  136. end
  137. ship16 = widget.newButton(shipName[15])
  138. ship17 = widget.newButton(shipName[16])
  139. ship18 = widget.newButton(shipName[17])
  140. row7 = widget.newRow()
  141. row7:add(ship16)
  142. function ship16.onClick(button)
  143. shipData(15)
  144. end
  145. row7:add(ship17)
  146. function ship17.onClick(button)
  147. shipData(16)
  148. end
  149. row7:add(ship18)
  150. function ship18.onClick(button)
  151. shipData(17)
  152. end
  153. ship19 = widget.newButton(shipName[18])
  154. ship20 = widget.newButton(shipName[19])
  155. ship21 = widget.newButton(shipName[20])
  156. row8 = widget.newRow()
  157. row8:add(ship19)
  158. function ship19.onClick(button)
  159. shipData(18)
  160. end
  161. row8:add(ship20)
  162. function ship20.onClick(button)
  163. shipData(19)
  164. end
  165. row8:add(ship21)
  166. function ship21.onClick(button)
  167. shipData(20)
  168. end
  169. row20 = widget.newRow()
  170. sRange = widget.newButton("Range Settings")
  171. function sRange.onClick(button)
  172. rangeSettings()
  173. end
  174. startScan = widget.newButton("Scan")
  175. function startScan.onClick(button)
  176. scan()
  177. end
  178. row20:add(sRange)
  179. row20:add(startScan)
  180. main = widget.newColumn()
  181. main:add(row1)
  182. main:add(row2)
  183. main:add(row3)
  184. main:add(row4)
  185. main:add(row5)
  186. main:add(row6)
  187. main:add(row7)
  188. main:add(row8)
  189. main:add(row20)
  190. main:run()
  191. end
  192.  
  193. function scan()
  194. radar.radius(range)
  195. local success, result = radar.getEnergyRequired()
  196. if not success then
  197. term.clear()
  198. title = widget.newLabel("HQ Scanning System")
  199. row1 = widget.newRow()
  200. row1:add(title)
  201. row2 = widget.newRow()
  202. energy = widget.newLabel("Not enough Energy!")
  203. row3 = widget.newRow()
  204. row3:add(energy)
  205. row4 = widget.newRow()
  206. row5 = widget.newRow()
  207. row6 = widget.newRow()
  208. row7 = widget.newRow()
  209. row8 = widget.newRow()
  210. okay = widget.newButton("Okay!")
  211. function okay.onClick(button)
  212. interface()
  213. end
  214. row20 = widget.newRow()
  215. row20:add(okay)
  216. main = widget.newColumn()
  217. main:add(row1)
  218. main:add(row2)
  219. main:add(row3)
  220. main:add(row4)
  221. main:add(row5)
  222. main:add(row6)
  223. main:add(row7)
  224. main:add(row8)
  225. main:add(row20)
  226. main:run()
  227. end
  228. local energyRequired = result
  229. local energyStored, _, energyUnits = radar.getEnergyStatus()
  230. if energyStored < energyRequired then
  231. term.clear()
  232. title = widget.newLabel("HQ Scanning System")
  233. row1 = widget.newRow()
  234. row1:add(title)
  235. row2 = widget.newRow()
  236. energy = widget.newLabel("Not enough Energy!")
  237. row3 = widget.newRow()
  238. row3:add(energy)
  239. row4 = widget.newRow()
  240. row5 = widget.newRow()
  241. row6 = widget.newRow()
  242. row7 = widget.newRow()
  243. row8 = widget.newRow()
  244. okay = widget.newButton("Okay!")
  245. function okay.onClick(button)
  246. interface()
  247. end
  248. row20 = widget.newRow()
  249. row20:add(okay)
  250. main = widget.newColumn()
  251. main:add(row1)
  252. main:add(row2)
  253. main:add(row3)
  254. main:add(row4)
  255. main:add(row5)
  256. main:add(row6)
  257. main:add(row7)
  258. main:add(row8)
  259. main:add(row20)
  260. main:run()
  261. end
  262. local scanDuration = radar.getScanDuration()
  263. radar.start()
  264. os.sleep(0.5)
  265.  
  266. print(" Scanning... (" .. scanDuration .. " s)")
  267. os.sleep(scanDuration)
  268.  
  269. local delay = 0
  270. local count
  271. repeat
  272. count = radar.getResultsCount()
  273. os.sleep(0.1)
  274. delay = delay + 1
  275. until (count ~= nil and count ~= -1) or delay > 10
  276.  
  277. if count ~= nil and count > 0 then
  278. term.clear()
  279. term.setCursorPos(1, 1)
  280. filtered = 0
  281. for i=0, count-1 do
  282. local success, type, name, x, y, z, mass = radar.getResult(i)
  283. local ssuccess, dimension, sx, sy, sz, system = radar.getGlobalPosition()
  284. if success then
  285. if name ~= "" then
  286. shipName[i-filtered] = name
  287. shipX[i-filtered] = x
  288. shipY[i-filtered] = y
  289. shipZ[i-filtered] = z
  290. shipMass[i-filtered] = mass
  291. shipRange[i-filtered] = math.floor(math.sqrt((x-sx)^2+(z-sz)^2))
  292. else
  293. filtered = filtered+1
  294. end
  295. else
  296. showError("Error " .. type)
  297. end
  298. end
  299. interface()
  300. end
  301. end
  302.  
  303. function rangeSettings()
  304. term.clear()
  305. title = widget.newLabel("HQ Scanning System")
  306. row1 = widget.newRow()
  307. row1:add(title)
  308. row2 = widget.newRow()
  309. cRange = widget.newLabel("Current Range: "..range)
  310. row3 = widget.newRow()
  311. row3:add(cRange)
  312. slider = widget.newSlider(1, 40)
  313. sValue = slider.value
  314. row4 = widget.newRow()
  315. row4:add(slider)
  316. row5 = widget.newRow()
  317. row6 = widget.newRow()
  318. row7 = widget.newRow()
  319. accept = widget.newButton("Accept")
  320. function accept.onClick(button)
  321. range = slider.value*25
  322. if(fs.exists("range.settings")) then
  323. file = fs.open("range.settings", "w")
  324. file.write(slider.value*25)
  325. file.close()
  326. end
  327. interface()
  328. end
  329. decline = widget.newButton("Decline")
  330. function decline.onClick(button)
  331. interface()
  332. end
  333. row8 = widget.newRow()
  334. row8:add(accept)
  335. row8:add(decline)
  336. row20 = widget.newRow()
  337. main = widget.newColumn()
  338. main:add(row1)
  339. main:add(row2)
  340. main:add(row3)
  341. main:add(row4)
  342. main:add(row5)
  343. main:add(row6)
  344. main:add(row7)
  345. main:add(row8)
  346. main:add(row20)
  347. main:run()
  348. end
  349.  
  350. function shipData(num)
  351. if(shipName[num] ~= "") then
  352. term.clear()
  353. title = widget.newLabel("HQ Scanning System")
  354. row1 = widget.newRow()
  355. row1:add(title)
  356. row2 = widget.newRow()
  357. sName = widget.newLabel("Name: "..shipName[num])
  358. row3 = widget.newRow()
  359. row2:add(sName)
  360. sX = widget.newLabel("Position X: "..shipX[num])
  361. row4 = widget.newRow()
  362. row3:add(sX)
  363. sY = widget.newLabel("Position Y: "..shipY[num])
  364. row5 = widget.newRow()
  365. row4:add(sY)
  366. sZ = widget.newLabel("Position Z: "..shipZ[num])
  367. row6 = widget.newRow()
  368. row5:add(sZ)
  369. sMass = widget.newLabel("Mass: "..shipMass[num].." tons")
  370. row7 = widget.newRow()
  371. row6:add(sMass)
  372. sRange = widget.newLabel("Distance: "..shipRange[num].."m")
  373. row8 = widget.newRow()
  374. row7:add(sRange)
  375. okay = widget.newButton("Okay!")
  376. function okay.onClick(button)
  377. interface()
  378. end
  379. row20 = widget.newRow()
  380. row20:add(okay)
  381. main = widget.newColumn()
  382. main:add(row1)
  383. main:add(row2)
  384. main:add(row3)
  385. main:add(row4)
  386. main:add(row5)
  387. main:add(row6)
  388. main:add(row7)
  389. main:add(row8)
  390. main:add(row20)
  391. main:run()
  392. else
  393. term.clear()
  394. title = widget.newLabel("HQ Scanning System")
  395. row1 = widget.newRow()
  396. row1:add(title)
  397. row2 = widget.newRow()
  398. noShips = widget.newLabel("No Ship found!")
  399. row3 = widget.newRow()
  400. row3:add(noShips)
  401. row4 = widget.newRow()
  402. row5 = widget.newRow()
  403. row6 = widget.newRow()
  404. row7 = widget.newRow()
  405. row8 = widget.newRow()
  406. okay = widget.newButton("Okay!")
  407. function okay.onClick(button)
  408. interface()
  409. end
  410. row20 = widget.newRow()
  411. row20:add(okay)
  412. main = widget.newColumn()
  413. main:add(row1)
  414. main:add(row2)
  415. main:add(row3)
  416. main:add(row4)
  417. main:add(row5)
  418. main:add(row6)
  419. main:add(row7)
  420. main:add(row8)
  421. main:add(row20)
  422. main:run()
  423. end
  424. end
  425.  
  426. interface()
Add Comment
Please, Sign In to add comment