Advertisement
HeroBrine1st

WarpDrive Drone

Mar 20th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. local core = {}
  2. local component = require("component")
  3. local controller = component.warpdriveShipController
  4. local TG = require("Telegram")
  5. local SD = require("SaveData")
  6. core.movement = {0,0,0}
  7. core.rotation = 0
  8.  
  9. function core.setToken(token)
  10. if core.token then return end
  11. core.token = token
  12. while not core.chatID do
  13. os.sleep(0.01)
  14. local messages = TG.receiveMessages(core.token)
  15. for i = 1, #messages do
  16. local message = messages[i]
  17. local text = message.text
  18. local chatID = message.chat_id
  19. if text == "/start" then
  20. core.chatID = chatID
  21. core.saveData()
  22. TG.sendMessage(core.token,core.chatID,"Калибровка завершена. Идентификатор чата сохранен.")
  23. end
  24. end
  25. end
  26. end
  27.  
  28. function core.log(msg)
  29. TG.sendMessage(core.token,core.chatID,msg)
  30. end
  31.  
  32. function core.setDimension(front,back,right,left,up,down)
  33. controller.dim_positive(front,right,up)
  34. controller.dim_negative(back,left,down)
  35. end
  36.  
  37. function core.shipMovToWorld(mx,my,mz)
  38. local wx,wy,wz
  39. local oX,_,oZ = controller.getOrientation()
  40. if oX == 1 then
  41. wx,wy,wz = mx,my,mz
  42. elseif oX == -1 then
  43. wx,wy,wz = -mx,my,mz
  44. elseif oZ == 1 then
  45. wx,wy,wz = mz,my,-mx
  46. elseif oZ == -1 then
  47. wx,wy,wz = -mz,my,mx
  48. end
  49. return wx,wy,wz
  50. end
  51.  
  52. function core.setMovement(front,up,right)
  53. controller.movement(front,up,right)
  54. local wx,wy,wz = core.shipMovToWorld(front,up,right)
  55. core.movement = {wx,wy,wz}
  56. end
  57.  
  58. function core.setRotation(inDegs)
  59. local steps = math.floor(inDegs/90)
  60. if steps < 0 or steps > 3 then steps = 0 end
  61. controller.rotationSteps(steps)
  62. core.rotation = steps
  63. end
  64.  
  65. function core.getPosition()
  66. local pos = {}
  67. pos[1], pos[2], pos[3], planet = controller.position()
  68. if controller.isInSpace() then pos[4] = "Space" elseif controller.isInHyperspace() then pos[4] = "Hyperspace" else pos[4] = planet end
  69. return pos
  70. end
  71.  
  72. function core.warp()
  73. local pos = core.getPosition()
  74. local mov = core.movement
  75. mov = {core.shipMovToWorld(mov[1],mov[2],mov[3])}
  76. local newPos = {pos[1]+mov[1],pos[2]+mov[2],pos[3]+mov[3]}
  77. core.log("Совершаю прыжок из " .. tostring(pos[1]) .. " " .. tostring(pos[2]) .. " " .. tostring(pos[3]) .. " в " .. tostring(newPos[1]) .. " " .. tostring(newPos[2]) .. " " .. tostring(newPos[3]))
  78. controller.command("MANUAL")
  79. controller.enable(true)
  80. end
  81.  
  82. function core.switchHyper()
  83. controller.command("HYPERDRIVE")
  84. controller.enable(true)
  85. end
  86.  
  87. function core.saveData()
  88. local data = {token=core.token,chatID = core.chatID,movement=core.movement}
  89. SD.saveData("ShipData",data)
  90. end
  91.  
  92. local data = SD.readData("ShipData")
  93. if data then
  94. core.token = data.token
  95. core.chatID = data.chatID
  96. core.movement = data.movement
  97. end
  98.  
  99. local function sendMsgFrag(token,chatID,msg)
  100. local str = msg
  101. if str:len() < 4096 then
  102. TG.sendMessage(token,chatID,str)
  103. else
  104. for i = 1, math.ceil(#str/4096) do
  105. TG.sendMessage(token,chatID,str:sub(((i-1)*4096)+1,i*4096))
  106. end
  107. end
  108. end
  109.  
  110. local function split(source, delimiters)
  111. local elements = {}
  112. local pattern = '([^'..delimiters..']+)'
  113. string.gsub(source, pattern, function(value) elements[#elements + 1] = value; end);
  114. return elements
  115. end
  116.  
  117. function core.getJumpDistance()
  118. controller.command("MANUAL")
  119. local _,max = controller.getMaxJumpDistance()
  120. local front, right, up = controller.dim_positive()
  121. local back, left, down = controller.dim_negative()
  122. local frontL = front+back+1
  123. local rightL = right+left+1
  124. local upL = up+down+1
  125. local dist = {
  126. max = {max+frontL,max+rightL,upL+max},
  127. min = {frontL+1,rightL+1,upL+1}
  128. }
  129. return dist
  130. end
  131.  
  132. --------------------------------------------------------------------------------------------------
  133.  
  134. local event = require("event")
  135. event.listen("shipCoreCooldownDone",function()
  136. TG.sendMessage(core.token,core.chatID,"Ядро готово к прыжку.")
  137. end)
  138.  
  139. core.setToken("") --сюда токен от botFather в Telegram
  140.  
  141. while true do
  142. os.sleep(0.01)
  143. local msgs = TG.receiveMessages(core.token)
  144. for i = 1, #msgs do
  145. local msg = msgs[i]
  146. local text = msg.text
  147. local cmd = split(text," ")
  148. local chatID = msg.chat_id
  149. if core.chatID == chatID then
  150. --COMMANDS EXECUTING
  151. if text:find("setMovement") then
  152. core.setMovement(tonumber(cmd[2]) or 0,tonumber(cmd[3]) or 0,tonumber(cmd[4]) or 0)
  153. TG.sendMessage(core.token,core.chatID,"Команда принята")
  154. elseif text:find("ping") then
  155. TG.sendMessage(core.token,core.chatID,"Понг!")
  156. elseif text:find("jump") then
  157. core.warp()
  158. elseif text:find("radarScan") then
  159. local radius = tonumber(cmd[2]) or 1000
  160. if component.isAvailable("warpdriveRadar") then
  161. radar = component.warpdriveRadar
  162. local energy, energyMax = radar.energy()
  163. local energyRequired = radar.getEnergyRequired(radius)
  164. local scanDuration = radar.getScanDuration(radius)
  165. if energy < energyRequired then
  166. TG.sendMessage(core.token,core.chatID,"Низкий уровень энергии... (" .. energy .. "/" .. energyRequired .. ")")
  167. else
  168. radar.radius(radius)
  169. radar.start()
  170. os.sleep(0.5)
  171.  
  172. TG.sendMessage(core.token,core.chatID,"Сканирование в процессе... (" .. scanDuration .. " s)")
  173. TG.sendMessage(core.token,core.chatID,"Вы можете посылать команды")
  174. event.timer(scanDuration,function()
  175. local delay = 0
  176. local count
  177. repeat
  178. count = radar.getResultsCount()
  179. os.sleep(0.1)
  180. delay = delay + 1
  181. until (count ~= nil and count ~= -1) or delay > 10
  182.  
  183. if count ~= nil and count > 0 then
  184. for i=0, count-1 do
  185. success, type, name, x, y, z = radar.getResult(i)
  186. if success then
  187. TG.sendMessage(core.token,core.chatID,type .. " " .. name .. " @ (" .. x .. " " .. y .. " " .. z .. ")")
  188. else
  189. TG.sendMessage(core.token,core.chatID,"Error " .. type)
  190. end
  191. end
  192. else
  193. TG.sendMessage(core.token,core.chatID,"Ничего не обнаружено(")
  194. end
  195. end,1)
  196. end
  197. else
  198. TG.sendMessage(core.token,core.chatID,"Нет радара!")
  199. end
  200. elseif text:find("hyperspace") then
  201. core.switchHyper()
  202. TG.sendMessage(core.token,core.chatID,"Команда принята")
  203. elseif text:find("getJumpDistance") then
  204. local dist = core.getJumpDistance()
  205. local str = "Forward movement:" .. tostring(dist.max[1]) .. "-" .. tostring(dist.min[1]) .. "\n"
  206. str = str .. "Vertical movement:" .. tostring(dist.max[2]) .. "-" .. tostring(dist.min[2]) .. "\n"
  207. str = str .. "Lateral movement:" .. tostring(dist.max[3]) .. "-" .. tostring(dist.min[3]) .. "\n"
  208. TG.sendMessage(core.token,core.chatID,str)
  209. elseif text:find("getPosition") then
  210. local x,y,z,space = table.unpack(core.getPosition())
  211. TG.sendMessage(core.token,core.chatID,tostring(x) .. " " .. tostring(y) .. " " .. tostring(z) .. " Пространство: " .. tostring(space))
  212. end
  213. end
  214. end
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement