Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local core = {}
- local component = require("component")
- local controller = component.warpdriveShipController
- local TG = require("Telegram")
- local SD = require("SaveData")
- core.movement = {0,0,0}
- core.rotation = 0
- function core.setToken(token)
- if core.token then return end
- core.token = token
- while not core.chatID do
- os.sleep(0.01)
- local messages = TG.receiveMessages(core.token)
- for i = 1, #messages do
- local message = messages[i]
- local text = message.text
- local chatID = message.chat_id
- if text == "/start" then
- core.chatID = chatID
- core.saveData()
- TG.sendMessage(core.token,core.chatID,"Калибровка завершена. Идентификатор чата сохранен.")
- end
- end
- end
- end
- function core.log(msg)
- TG.sendMessage(core.token,core.chatID,msg)
- end
- function core.setDimension(front,back,right,left,up,down)
- controller.dim_positive(front,right,up)
- controller.dim_negative(back,left,down)
- end
- function core.shipMovToWorld(mx,my,mz)
- local wx,wy,wz
- local oX,_,oZ = controller.getOrientation()
- if oX == 1 then
- wx,wy,wz = mx,my,mz
- elseif oX == -1 then
- wx,wy,wz = -mx,my,mz
- elseif oZ == 1 then
- wx,wy,wz = mz,my,-mx
- elseif oZ == -1 then
- wx,wy,wz = -mz,my,mx
- end
- return wx,wy,wz
- end
- function core.setMovement(front,up,right)
- controller.movement(front,up,right)
- local wx,wy,wz = core.shipMovToWorld(front,up,right)
- core.movement = {wx,wy,wz}
- end
- function core.setRotation(inDegs)
- local steps = math.floor(inDegs/90)
- if steps < 0 or steps > 3 then steps = 0 end
- controller.rotationSteps(steps)
- core.rotation = steps
- end
- function core.getPosition()
- local pos = {}
- pos[1], pos[2], pos[3], planet = controller.position()
- if controller.isInSpace() then pos[4] = "Space" elseif controller.isInHyperspace() then pos[4] = "Hyperspace" else pos[4] = planet end
- return pos
- end
- function core.warp()
- local pos = core.getPosition()
- local mov = core.movement
- mov = {core.shipMovToWorld(mov[1],mov[2],mov[3])}
- local newPos = {pos[1]+mov[1],pos[2]+mov[2],pos[3]+mov[3]}
- core.log("Совершаю прыжок из " .. tostring(pos[1]) .. " " .. tostring(pos[2]) .. " " .. tostring(pos[3]) .. " в " .. tostring(newPos[1]) .. " " .. tostring(newPos[2]) .. " " .. tostring(newPos[3]))
- controller.command("MANUAL")
- controller.enable(true)
- end
- function core.switchHyper()
- controller.command("HYPERDRIVE")
- controller.enable(true)
- end
- function core.saveData()
- local data = {token=core.token,chatID = core.chatID,movement=core.movement}
- SD.saveData("ShipData",data)
- end
- local data = SD.readData("ShipData")
- if data then
- core.token = data.token
- core.chatID = data.chatID
- core.movement = data.movement
- end
- local function sendMsgFrag(token,chatID,msg)
- local str = msg
- if str:len() < 4096 then
- TG.sendMessage(token,chatID,str)
- else
- for i = 1, math.ceil(#str/4096) do
- TG.sendMessage(token,chatID,str:sub(((i-1)*4096)+1,i*4096))
- end
- end
- end
- local function split(source, delimiters)
- local elements = {}
- local pattern = '([^'..delimiters..']+)'
- string.gsub(source, pattern, function(value) elements[#elements + 1] = value; end);
- return elements
- end
- function core.getJumpDistance()
- controller.command("MANUAL")
- local _,max = controller.getMaxJumpDistance()
- local front, right, up = controller.dim_positive()
- local back, left, down = controller.dim_negative()
- local frontL = front+back+1
- local rightL = right+left+1
- local upL = up+down+1
- local dist = {
- max = {max+frontL,max+rightL,upL+max},
- min = {frontL+1,rightL+1,upL+1}
- }
- return dist
- end
- --------------------------------------------------------------------------------------------------
- local event = require("event")
- event.listen("shipCoreCooldownDone",function()
- TG.sendMessage(core.token,core.chatID,"Ядро готово к прыжку.")
- end)
- core.setToken("") --сюда токен от botFather в Telegram
- while true do
- os.sleep(0.01)
- local msgs = TG.receiveMessages(core.token)
- for i = 1, #msgs do
- local msg = msgs[i]
- local text = msg.text
- local cmd = split(text," ")
- local chatID = msg.chat_id
- if core.chatID == chatID then
- --COMMANDS EXECUTING
- if text:find("setMovement") then
- core.setMovement(tonumber(cmd[2]) or 0,tonumber(cmd[3]) or 0,tonumber(cmd[4]) or 0)
- TG.sendMessage(core.token,core.chatID,"Команда принята")
- elseif text:find("ping") then
- TG.sendMessage(core.token,core.chatID,"Понг!")
- elseif text:find("jump") then
- core.warp()
- elseif text:find("radarScan") then
- local radius = tonumber(cmd[2]) or 1000
- if component.isAvailable("warpdriveRadar") then
- radar = component.warpdriveRadar
- local energy, energyMax = radar.energy()
- local energyRequired = radar.getEnergyRequired(radius)
- local scanDuration = radar.getScanDuration(radius)
- if energy < energyRequired then
- TG.sendMessage(core.token,core.chatID,"Низкий уровень энергии... (" .. energy .. "/" .. energyRequired .. ")")
- else
- radar.radius(radius)
- radar.start()
- os.sleep(0.5)
- TG.sendMessage(core.token,core.chatID,"Сканирование в процессе... (" .. scanDuration .. " s)")
- TG.sendMessage(core.token,core.chatID,"Вы можете посылать команды")
- event.timer(scanDuration,function()
- local delay = 0
- local count
- repeat
- count = radar.getResultsCount()
- os.sleep(0.1)
- delay = delay + 1
- until (count ~= nil and count ~= -1) or delay > 10
- if count ~= nil and count > 0 then
- for i=0, count-1 do
- success, type, name, x, y, z = radar.getResult(i)
- if success then
- TG.sendMessage(core.token,core.chatID,type .. " " .. name .. " @ (" .. x .. " " .. y .. " " .. z .. ")")
- else
- TG.sendMessage(core.token,core.chatID,"Error " .. type)
- end
- end
- else
- TG.sendMessage(core.token,core.chatID,"Ничего не обнаружено(")
- end
- end,1)
- end
- else
- TG.sendMessage(core.token,core.chatID,"Нет радара!")
- end
- elseif text:find("hyperspace") then
- core.switchHyper()
- TG.sendMessage(core.token,core.chatID,"Команда принята")
- elseif text:find("getJumpDistance") then
- local dist = core.getJumpDistance()
- local str = "Forward movement:" .. tostring(dist.max[1]) .. "-" .. tostring(dist.min[1]) .. "\n"
- str = str .. "Vertical movement:" .. tostring(dist.max[2]) .. "-" .. tostring(dist.min[2]) .. "\n"
- str = str .. "Lateral movement:" .. tostring(dist.max[3]) .. "-" .. tostring(dist.min[3]) .. "\n"
- TG.sendMessage(core.token,core.chatID,str)
- elseif text:find("getPosition") then
- local x,y,z,space = table.unpack(core.getPosition())
- TG.sendMessage(core.token,core.chatID,tostring(x) .. " " .. tostring(y) .. " " .. tostring(z) .. " Пространство: " .. tostring(space))
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement