Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Kye_Duo

Warp Drive controller for OC

Question

I'm trying to setup a warp drive controller program in OC, there exists one for CC, but it doesn't work out of the box.

I am a total noob when it comes to OC programming so I need help

also, is there an external way to write programs for OC like you could with CC?

 

here is the original CC code, what would I need to do to convert it to run on OC?

I've started to convert it, but I'm in way over my head.

print("loading...")

-- set alarm side if you need this
Alarm = "top"
local colors = require("colors")
Style = {
 CDeflt = colors.white,
 BGDeflt = colors.blue,
 CTitle = colors.black,
 BGTitle = colors.cyan,
 CWarn = colors.white,
 BGWarn = colors.red
}

function SetColorDeflt()
 term.setBackgroundColor(Style.BGDeflt)
 term.setTextColor(Style.CDeflt)
end

function SetColorTitle()
 term.setBackgroundColor(Style.BGTitle)
 term.setTextColor(Style.CTitle)
end

function SetColorWarn()
 term.setBackgroundColor(Style.BGWarn)
 term.setTextColor(Style.CWarn)
end

function Clear()
 term.clear()
 term.setCursorPos(1,1)
end

function Show(Text)
 term.write(Text)
 local xt,yt = term.getCursorPos()
 term.setCursorPos(1, yt+1)
end

function ShowTitle(Text)
 SetColorTitle()
 term.setCursorPos(12, 1)
 Show(Text)
 SetColorDeflt()
end

function ShowMenu(Text)
 term.write(Text)
 local xt, yt = term.getCursorPos()
 for i = xt, 51 do
  term.write(" ")
 end
 term.setCursorPos(1, yt+1)
end

function ShowWarning(Text)
  SetColorWarn()
  term.setCursorPos(10, 19)
  term.write(" "..Text.." ")
  SetColorDeflt()
end

function SaveData()
 local file = fs.open("shipdata.txt", "w")
 file.writeLine(textutils.serialize(SData))
 file.close()
end

function ReadData()
 local file = fs.open("shipdata.txt", "r")
 SData = textutils.unserialize(file.readAll())
 file.close()
end

function Explode(d, p)
 local t, ll
 t = {}
 ll = 0
 if(#p == 1) then return {p} end
 while true do
  l = string.find(p ,d, ll, true)
  if l ~= nil then 
   table.insert(t, string.sub(p, ll, l-1))
   ll = l+1
  else
   table.insert(t, string.sub(p, ll))
   break
  end
 end
 return t
end

function ShowDirection()
 if SData.Direction == 1 then
  Show(" Direction        = Up")
 elseif SData.Direction == 2 then
  Show(" Direction        = Down")
 elseif SData.Direction == 0 then
  Show(" Direction        = Front")
 elseif SData.Direction == 180 then
  Show(" Direction        = Back")
 elseif SData.Direction == 90 then
  Show(" Direction        = Left")
 elseif SData.Direction == 255 then
  Show(" Direction        = Right")
 end
end

function CalcRealDistance()
 if IsInHyper then
  RealDistance = SData.Distance * 100
  MinimumDistance = 1
  JumpCost = (1000 * Weight) + (1000 * SData.Distance)
 else
  if SData.Direction == 1 or SData.Direction == 2 then
   MinimumDistance = GUp + GDown
   RealDistance = SData.Distance + MinimumDistance
  elseif SData.Direction == 0 or SData.Direction == 180 then
   MinimumDistance = GFront + GBack
   RealDistance = SData.Distance + MinimumDistance
  elseif SData.Direction == 90 or SData.Direction == 255 then
   MinimumDistance = GLeft + GRight
   RealDistance = SData.Distance + MinimumDistance
  end
  MinimumDistance = MinimumDistance + 1
  JumpCost = (10 * Weight) + (100 * SData.Distance)
 end
end

function CalcNewCoords(cx, cy, cz)
 local res = {x=cx, y=cy, z=cz}
 if SData.Direction == 1 then
  res.y = res.y + RealDistance
 elseif SData.Direction == 2 then
  res.y = res.y - RealDistance
 end
 local dx = warp.get_dx()
 local dz = warp.get_dz()
 if dx ~= 0 then
  if SData.Direction == 0 then
   res.x = res.x + (RealDistance * dx)
  elseif SData.Direction == 180 then
   res.x = res.x - (RealDistance * dx)
  elseif SData.Direction == 90 then
   res.z = res.z + (RealDistance * dx)
  elseif SData.Direction == 255 then
   res.z = res.z - (RealDistance * dx)
  end
 else
  if SData.Direction == 0 then
   res.z = res.z + (RealDistance * dz)
  elseif SData.Direction == 180 then
   res.z = res.z - (RealDistance * dz)
  elseif SData.Direction == 90 then
   res.x = res.x + (RealDistance * dz)
  elseif SData.Direction == 255 then
   res.x = res.x - (RealDistance * dz)
  end
 end
 return res
end

function ShowInfo()
 ShowTitle(Title)
 Show("Core:")
 Show(" x, y, z          = "..X..", "..Y..", "..Z)
 local energy = warp.get_energy_level()
 Show(" Energy           = "..math.floor(energy / 1000000).." % ("..energy.."EU)")
 Show(" Attached players = "..warp.get_attached_players())
 Show("Dimensions:")
 Show(" Front, Right, Up = "..GFront..", "..GRight..", "..GUp)
 Show(" Back, Left, Down = "..GBack..", "..GLeft..", "..GDown)
 Show(" Size             = "..Weight.." blocks")
 Show("Warp data:")
 ShowDirection()
 local dest = CalcNewCoords(X, Y, Z)
 Show(" Distance         = "..RealDistance.." ("..JumpCost.."EU, "..math.floor(energy/JumpCost).." jumps)")
 Show(" Dest.coordinates = "..dest.x..", "..dest.y..", "..dest.z)
 if SData.Summon then
  Show(" Summon after     = Yes")
 else
  Show(" Summon after     = No")
 end
end

function Confirm()
 ShowWarning("Are you sure? (y/n)")
 local event, keycode = os.pullEvent("key")
 if keycode == 21 then
  return true
 else
  return false
 end
end

function Warp()
 rs.setOutput(Alarm, false)
 sleep(1)
 warp.set_direction(SData.Direction)
 if IsInHyper then
  warp.set_mode(2)
 else
  warp.set_mode(1)
 end
 warp.do_jump()
end

function SetDistance()
 Clear()
 ShowTitle("<====  Set distance  ====>")
 SData.Distance = 0
 CalcRealDistance()
 MaximumDistance = MinimumDistance + 127
 if IsInHyper then
  term.write("Distance * 100 (min "..MinimumDistance..", max "..MaximumDistance.."): ")
 else
  term.write("Distance (min "..MinimumDistance..", max "..MaximumDistance.."): ")
 end
 sleep(0.3)
 SData.Distance = tonumber(read())
 if SData.Distance == nil then SData.Distance = 1 end
 if SData.Distance < MinimumDistance or SData.Distance > MaximumDistance then
  SData.Distance = 1
  ShowWarning("Wrong distance. Try again.")
  os.pullEvent("key")
  CalcRealDistance()
 else
  if not IsInHyper then
   SData.Distance = SData.Distance - RealDistance
  end
  warp.set_distance(SData.Distance)
  CalcRealDistance()
 end
end

function SetDirection()
 local drun = true
 while(drun) do
  Clear()
  ShowTitle("<==== Set direction ====>")
  ShowDirection()
  term.setCursorPos(1, 16)
  SetColorTitle()
  ShowMenu("Use directional keys")
  ShowMenu("W/S keys for Up/Down")
  ShowMenu("Enter - confirm")
  SetColorDeflt()
  local event, keycode = os.pullEvent("key")
  if keycode == 200 then
   SData.Direction = 0
  elseif keycode == 17 then
   SData.Direction = 1
  elseif keycode == 203 then
   SData.Direction = 90
  elseif keycode == 205 then
   SData.Direction = 255
  elseif keycode == 208 then
   SData.Direction = 180
  elseif keycode == 31 then
   SData.Direction = 2
  elseif keycode == 28 then
   drun = false
  end
 end
end

function SetDimensions()
 Clear()
 sleep(0.3)
 ShowTitle("<==== Set dimensions ====>")
 term.write(" Front ("..GFront..") : ")
 GFront = tonumber(read())
 term.write(" Right ("..GRight..") : ")
 GRight = tonumber(read())
 term.write(" Up    ("..GUp..") : ")
 GUp = tonumber(read())
 term.write(" Back  ("..GBack..") : ")
 GBack = tonumber(read())
 term.write(" Left  ("..GLeft..") : ")
 GLeft = tonumber(read())
 term.write(" Down  ("..GDown..") : ")
 GDown = tonumber(read())
 term.write("Setting dimensions...")
 warp.dim_setp(GFront, GRight, GUp)
 warp.dim_setn(GBack, GLeft, GDown)
end

function Summon()
 Clear()
 ShowTitle("<==== Summon players ====>")
 local players = Explode(",", warp.get_attached_players())
 for i = 1, #players do
  Show(i..". "..players[i])
 end
 SetColorTitle()
 ShowMenu("Enter player number")
 ShowMenu("or press enter to summon everyone")
 SetColorDeflt()
 sleep(0.3)
 term.write(":")
 local input = read()
 if input == "" then
  warp.summon_all()
 else
  input = tonumber(input)
  warp.summon(input - 1)
 end
end

function JumpToBeacon()
 Clear()
 ShowTitle("<==== Jump to beacon ====>")
 sleep(0.3)
 term.write("Enter beacon frequency: ")
 local freq = tostring(read())
 rs.setOutput(Alarm, true)
 if Confirm() then
  rs.setOutput(Alarm, false)
  warp.set_mode(4)
  warp.set_beacon_frequency(freq)
  warp.do_jump()
 end
 rs.setOutput(Alarm, false)
end

function JumpToGate()
 Clear()
 ShowTitle("<==== Jump to JumpGate ====>")
 sleep(0.3)
 term.write("Enter jumpgate name: ")
 local name = tostring(read())
 rs.setOutput(Alarm, true)
 if Confirm() then
  rs.setOutput(Alarm, false)
  warp.set_mode(6)
  warp.set_target_jumpgate(name)
  warp.do_jump()
 end
 rs.setOutput(Alarm, false)
end

function SetShipName()
 Clear()
 ShowTitle("<==== Set ship name ====>")
 sleep(0.3)
 term.write("Enter ship name: ")
 SData.Shipname = tostring(read())
 os.setComputerLabel(SData.Shipname)
 warp.set_core_frequency(SData.Shipname)
 SaveData()
 os.reboot()
end

if fs.exists("shipdata.txt") then
 ReadData()
else
 SData = {
  Summon = false,
  Distance = 1,
  Direction = 0,
  Shipname = ""
 }
end

SetColorDeflt()

Side = { "bottom", "top", "back", "left", "right" }
for i = 1,5 do
 if peripheral.getType(Side[i]) == "warpcore" then
  warp = peripheral.wrap(Side[i])
  break
 end
end

if type(warp) ~= "table" then
 ShowWarning("No warpcore controller detected")
 os.pullEvent("key")
 os.shutdown()
end

if SData.Shipname == "" then
 SetShipName()
end

Title = "<Jump-S 1.6.1 \""..SData.Shipname.."\">"

if SData.Summon then
 warp.summon_all()
end

GFront, GRight, GUp = warp.dim_getp()
GBack, GLeft, GDown = warp.dim_getn()
IsInHyper = warp.is_in_hyperspace()
repeat
 X = warp.get_x()
 sleep(0.3)
until X ~= nil
Y = warp.get_y()
Z = warp.get_z()
Weight = warp.get_ship_size()

CalcRealDistance()

warp.set_mode(1)

mainloop = true
while(mainloop) do
 Clear()
 ShowInfo()
 term.setCursorPos(1, 15)
 SetColorTitle()
 ShowMenu("D - Dimensions, M - Toggle summon, N - Ship name")
 ShowMenu("S - Set Warp Data, J - Jump, G - Jump to JumpGate")
 ShowMenu("B - Jump to Beacon, H -Jump to Hyperspace")
 ShowMenu("C - Summon, X - Shutdown WarpCore and Exit")
 SetColorDeflt()
 local event, keycode = os.pullEvent("key")
 if keycode == 31 then
  SetDirection()
  SetDistance()
  SaveData()
 elseif keycode == 50 then
  if SData.Summon then
   SData.Summon = false
  else
   SData.Summon = true
  end
  SaveData()
 elseif keycode == 32 then
  SetDimensions()
  SaveData()
 elseif keycode == 36 then
  rs.setOutput(Alarm, true)
  if Confirm() then
   Warp()
  end
  rs.setOutput(Alarm, false)
 elseif keycode == 46 then
  Summon()
 elseif keycode == 48 then
  JumpToBeacon()
 elseif keycode == 34 then
  JumpToGate()
 elseif keycode == 35 then
  rs.setOutput(Alarm, true)
  if Confirm() then
   rs.setOutput(Alarm, false)
   warp.set_mode(5)
   warp.do_jump()
  end
  rs.setOutput(Alarm, false)
 elseif keycode == 45 then
  mainloop = false
 elseif keycode == 49 then
  SetShipName()
 end
end

if SData.Summon then
 SData.Summon = false
 SaveData()
end
Clear()
print("wish you good")
warp.set_mode(0)
sleep(0.5)
os.shutdown()
Link to post
Share on other sites

16 answers to this question

Recommended Posts

  • 0

The best thing for you to do Is to convert it yourself one line at a time. That will allow you to gain more OpenComputers knowledge yourself, And when you do find something you 100% cant figure out without help is went you should ask around on irc. If you really don't want to convert it yourself then just install cc and use that.

Link to post
Share on other sites
  • 0

I have an idea to get you started on porting this to OC. There are a few libraries that aren't available globally like their CC counterparts.

I.E. the term library isn't global and yadda.... This is a step to get you on your way.

-- Component lib for access to the gpu
local component = require "component"

-- Put term lib into local space..
local term = require "term"

-- Other libs we need as well
local colors = require "colors"
local textutils = require "serialization" -- CC to OC. The 'serialize' method is used the same here i think....
local gpu = component.getPrimary('gpu')
local fs = require "filesystem"

-- 'term' Compatibility. These libs are similar in interface but OC lib has shorter method names. 
term.setCursorPos, term.getCursorPos = term.setCursor, term.getCursor
term.setTextColor, term.setBackgroundColor = gpu.setForeground, gpu.setBackground

This should solve a decent portion of compatibility. Hope this helps.. Post back if you have questions..

Link to post
Share on other sites
  • 0

I've taken the above codes and gotten very close to making the program work. The problem is that the main loop isn't working right. When you press a key, it either does nothing or only goes to the warp data menu. I don't know whats wrong.
 

-- Component lib for access to the gpu
local component = require "component"
 
-- Put term lib into local space..
local term = require "term"
 
-- Other libs we need as well
local textutils = require("serialization") -- CC to OC. The 'serialize' method is used the same here i think....
local gpu = component.getPrimary("gpu")
local fs = require("filesystem")
local event = require("event")
local keyboard = require("keyboard")
 
-- 'term' Compatibility. These libs are similar in interface but OC lib has shorter method names. 
term.setCursorPos, term.getCursorPos = term.setCursor, term.getCursor
term.setTextColor, term.setBackgroundColor = gpu.setForeground, gpu.setBackground

print("loading...")
 

Style = {
 CDeflt = 0xFFFFFF,
 BGDeflt = 0x0000FF,
 CTitle = 0x000000,
 BGTitle = 0x0092FF,
 CWarn = 0xFFFFFF,
 BGWarn = 0xFF0000
}
 
function SetColorDeflt()
 term.setBackgroundColor(Style.BGDeflt)
 term.setTextColor(Style.CDeflt)
end
 
function SetColorTitle()
 term.setBackgroundColor(Style.BGTitle)
 term.setTextColor(Style.CTitle)
end
 
function SetColorWarn()
 term.setBackgroundColor(Style.BGWarn)
 term.setTextColor(Style.CWarn)
end
 
function Clear()
 term.clear()
 term.setCursorPos(1,1)
end
 
function Show(Text)
 term.write(Text)
 local xt,yt = term.getCursorPos()
 term.setCursorPos(1, yt+1)
end
 
function ShowTitle(Text)
 SetColorTitle()
 term.setCursorPos(12, 1)
 Show(Text)
 SetColorDeflt()
end
 
function ShowMenu(Text)
 term.write(Text)
 local xt, yt = term.getCursorPos()
 for i = xt, 51 do
  term.write(" ")
 end
 term.setCursorPos(1, yt+1)
end
 
function ShowWarning(Text)
  SetColorWarn()
  term.setCursorPos(10, 19)
  term.write(" "..Text.." ")
  SetColorDeflt()
end
 
function SaveData()
 local file = fs.open("shipdata.txt", "w")
 file.writeLine(textutils.serialize(SData))
 file.close()
end
 
function ReadData()
 local file = fs.open("shipdata.txt", "r")
 SData = textutils.unserialize(file.readAll())
 file.close()
end
 
function Explode(d, p)
 local t, ll
 t = {}
 ll = 0
 if(#p == 1) then return {p} end
 while true do
  l = string.find(p ,d, ll, true)
  if l ~= nil then 
   table.insert(t, string.sub(p, ll, l-1))
   ll = l+1
  else
   table.insert(t, string.sub(p, ll))
   break
  end
 end
 return t
end
 
function ShowDirection()
 if SData.Direction == 1 then
  Show(" Direction        = Up")
 elseif SData.Direction == 2 then
  Show(" Direction        = Down")
 elseif SData.Direction == 0 then
  Show(" Direction        = Front")
 elseif SData.Direction == 180 then
  Show(" Direction        = Back")
 elseif SData.Direction == 90 then
  Show(" Direction        = Left")
 elseif SData.Direction == 255 then
  Show(" Direction        = Right")
 end
end
 
function CalcRealDistance()
 if IsInHyper then
  RealDistance = SData.Distance * 100
  MinimumDistance = 1
  JumpCost = (1000 * Weight) + (1000 * SData.Distance)
 else
  if SData.Direction == 1 or SData.Direction == 2 then
   MinimumDistance = GUp + GDown
   RealDistance = SData.Distance + MinimumDistance
  elseif SData.Direction == 0 or SData.Direction == 180 then
   MinimumDistance = GFront + GBack
   RealDistance = SData.Distance + MinimumDistance
  elseif SData.Direction == 90 or SData.Direction == 255 then
   MinimumDistance = GLeft + GRight
   RealDistance = SData.Distance + MinimumDistance
  end
  MinimumDistance = MinimumDistance + 1
  JumpCost = (10 * Weight) + (100 * SData.Distance)
 end
end
 
function CalcNewCoords(cx, cy, cz)
 local res = {x=cx, y=cy, z=cz}
 if SData.Direction == 1 then
  res.y = res.y + RealDistance
 elseif SData.Direction == 2 then
  res.y = res.y - RealDistance
 end
 local dx = component.warpdriveShipController.direction("X")
 local dz = component.warpdriveShipController.direction("Z")
 if dx ~= 0 then
  if SData.Direction == 0 then
   res.x = res.x + (RealDistance * dx)
  elseif SData.Direction == 180 then
   res.x = res.x - (RealDistance * dx)
  elseif SData.Direction == 90 then
   res.z = res.z + (RealDistance * dx)
  elseif SData.Direction == 255 then
   res.z = res.z - (RealDistance * dx)
  end
 else
  if SData.Direction == 0 then
   res.z = res.z + (RealDistance * dz)
  elseif SData.Direction == 180 then
   res.z = res.z - (RealDistance * dz)
  elseif SData.Direction == 90 then
   res.x = res.x + (RealDistance * dz)
  elseif SData.Direction == 255 then
   res.x = res.x - (RealDistance * dz)
  end
 end
 return res
end
 
function ShowInfo()
 ShowTitle(Title)
 Show("Core:")
 Show(" x, y, z          = "..X..", "..Y..", "..Z)
 local energy = component.warpdriveShipController.energy()
 Show(" Energy           = "..math.floor(energy / 1000000).." % ("..energy.."EU)")
 Show(" Attached players = "..component.warpdriveShipController.getAttachedPlayers())
 Show("Dimensions:")
 Show(" Front, Right, Up = "..GFront..", "..GRight..", "..GUp)
 Show(" Back, Left, Down = "..GBack..", "..GLeft..", "..GDown)
 Show(" Size             = "..Weight.." blocks")
 Show("Warp data:")
 ShowDirection()
 local dest = CalcNewCoords(X, Y, Z)
 Show(" Distance         = "..RealDistance.." ("..JumpCost.."EU, "..math.floor(energy/JumpCost).." jumps)")
 Show(" Dest.coordinates = "..dest.x..", "..dest.y..", "..dest.z)
 if SData.Summon then
  Show(" Summon after     = Yes")
 else
  Show(" Summon after     = No")
 end
end
 
function Confirm()
 ShowWarning("Are you sure? (y/n)")
 ev, p1, p2, p3, p4, p5 = event.pull(nil, _, ev, p1, p2, p3, p4, p5)
 local char = keyboard.keys[p2]
 if char == y then
  return true
 else
  return false
 end
end
 
function Warp()
 os.sleep(1)
 component.warpdriveShipController.direction(SData.Direction)
 if IsInHyper then
  component.warpdriveShipController.mode(2)
 else
  component.warpdriveShipController.mode(1)
 end
 component.warpdriveShipController.jump()
end
 
function SetDistance()
 Clear()
 ShowTitle("<====  Set distance  ====>")
 SData.Distance = 0
 CalcRealDistance()
 MaximumDistance = MinimumDistance + 127
 if IsInHyper then
  term.write("Distance * 100 (min "..MinimumDistance..", max "..MaximumDistance.."): ")
 else
  term.write("Distance (min "..MinimumDistance..", max "..MaximumDistance.."): ")
 end
 os.sleep(0.3)
 SData.Distance = tonumber(term.read())
 if SData.Distance == nil then SData.Distance = 1 end
 if SData.Distance < MinimumDistance or SData.Distance > MaximumDistance then
  SData.Distance = 1
  ShowWarning("Wrong distance. Try again.")
  event.pull("key")
  CalcRealDistance()
 else
  if not IsInHyper then
   SData.Distance = SData.Distance - RealDistance
  end
  warp.set_distance(SData.Distance)
  CalcRealDistance()
 end
end
 
function SetDirection()
 local drun = true
 while(drun) do
  Clear()
  ShowTitle("<==== Set direction ====>")
  ShowDirection()
  term.setCursorPos(1, 16)
  SetColorTitle()
  ShowMenu("Use directional keys")
  ShowMenu("W/S keys for Up/Down")
  ShowMenu("Enter - confirm")
  SetColorDeflt()
  event.pull("key")
  if keycode == 200 then
   SData.Direction = 0
  elseif keycode == 17 then
   SData.Direction = 1
  elseif keycode == 203 then
   SData.Direction = 90
  elseif keycode == 205 then
   SData.Direction = 255
  elseif keycode == 208 then
   SData.Direction = 180
  elseif keycode == 31 then
   SData.Direction = 2
  elseif keycode == 28 then
   drun = false
  end
 end
end
 
function SetDimensions()
 Clear()
 os.sleep(0.3)
 ShowTitle("<==== Set dimensions ====>")
 term.write(" Front ("..GFront..") : ")
 GFront = tonumber(read())
 term.write(" Right ("..GRight..") : ")
 GRight = tonumber(read())
 term.write(" Up    ("..GUp..") : ")
 GUp = tonumber(read())
 term.write(" Back  ("..GBack..") : ")
 GBack = tonumber(read())
 term.write(" Left  ("..GLeft..") : ")
 GLeft = tonumber(read())
 term.write(" Down  ("..GDown..") : ")
 GDown = tonumber(read())
 term.write("Setting dimensions...")
 component.warpdriveShipController.dim_positive(GFront, GRight, GUp)
 component.warpdriveShipController.dim_negative(GBack, GLeft, GDown)
end
 
function Summon()
 Clear()
 ShowTitle("<==== Summon players ====>")
 local players = Explode(",", warp.get_attached_players())
 for i = 1, #players do
  Show(i..". "..players[i])
 end
 SetColorTitle()
 ShowMenu("Enter player number")
 ShowMenu("or press enter to summon everyone")
 SetColorDeflt()
 sleep(0.3)
 term.write(":")
 local input = read()
 if input == "" then
  component.warpdriveShipController.summon_all()
 else
  input = tonumber(input)
  component.warpdriveShipController.summon(input - 1)
 end
end
 
function JumpToBeacon()
 Clear()
 ShowTitle("<==== Jump to beacon ====>")
 sleep(0.3)
 term.write("Enter beacon frequency: ")
 local freq = tostring(read())
 if Confirm() then
  component.warpdriveShipController.mode(4)
  component.warpdriveShipCOntroller.beaconFrequency(freq)
  component.warpdriveShipController.jump()
 end
end
 
function JumpToGate()
 Clear()
 ShowTitle("<==== Jump to JumpGate ====>")
 os.sleep(0.3)
 term.write("Enter jumpgate name: ")
 local name = tostring(read())
 if Confirm() then
  component.warpdriveShipController.mode(6)
  component.warpdriveShipController.targetJumpgate(name)
  component.warpdriveShipController.jump()
 end
end
 
 
if fs.exists("shipdata.txt") then
 ReadData()
else
 SData = {
  Summon = false,
  Distance = 1,
  Direction = 0
 }
end
 
component.gpu.setResolution(50, 20)
SetColorDeflt()
 
component.getPrimary("warpdriveShipController") 
 
if SData.Summon then
 component.warpdriveShipController.summon_all()
end
 
GFront, GRight, GUp = component.warpdriveShipController.dim_positive()
GBack, GLeft, GDown = component.warpdriveShipController.dim_negative()
IsInHyper = component.warpdriveShipController.isInHyperspace()
X, Y, Z  = component.warpdriveShipController.position()
Weight = component.warpdriveShipController.getShipSize()
 
CalcRealDistance()
 
component.warpdriveShipController.mode(1)
 
mainloop = true
while(mainloop) do
 Clear()
 ShowInfo()
 term.setCursorPos(1, 15)
 SetColorTitle()
 ShowMenu("D - Dimensions, M - Toggle summon")
 ShowMenu("S - Set Warp Data, J - Jump, G - Jump to JumpGate")
 ShowMenu("B - Jump to Beacon, H -Jump to Hyperspace")
 ShowMenu("C - Summon, X - Shutdown WarpCore and Exit")
 SetColorDeflt()
 ev, p1, p2, p3, p4, p5 = event.pull(nil, _, ev,  p1, p2, p3, p4, p5)
 if ev == "key_down" then
  local char = keyboard.keys[p1]
  if char == s then
   SetDirection()
   SetDistance()
   SaveData()
  elseif char == m then
   if SData.Summon then
    SData.Summon = false
   else
    SData.Summon = true
   end
  SaveData()
  elseif char == d then
   SetDimensions()
   SaveData()
  elseif char == j then
   if Confirm() then
    Warp()
   end
  elseif char == c then
   Summon()
  elseif char == b then
   JumpToBeacon()
  elseif char == g then
   JumpToGate()
  elseif char == h then
   if Confirm() then
    component.warpdriveShipController.mode(5)
    component.warpdriveShipComponent.jump()
   end
  elseif char == x then
   mainloop = false
  end
 end
end
 
if SData.Summon then
 SData.Summon = false
 SaveData()
end
Clear()
print("Shutting Down")
component.warpdriveShipController.mode(0)
os.sleep(0.5)
Link to post
Share on other sites
  • 0

 

I've taken the above codes and gotten very close to making the program work. The problem is that the main loop isn't working right. When you press a key, it either does nothing or only goes to the warp data menu. I don't know whats wrong.

 

mainloop = true
while(mainloop) do
 Clear()
 ShowInfo()
 term.setCursorPos(1, 15)
 SetColorTitle()
 ShowMenu("D - Dimensions, M - Toggle summon")
 ShowMenu("S - Set Warp Data, J - Jump, G - Jump to JumpGate")
 ShowMenu("B - Jump to Beacon, H -Jump to Hyperspace")
 ShowMenu("C - Summon, X - Shutdown WarpCore and Exit")
 SetColorDeflt()
 ev, p1, p2, p3, p4, p5 = event.pull(nil, _, ev,  p1, p2, p3, p4, p5)
 if ev == "key_down" then
  local char = keyboard.keys[p1]
  if char == s then
   SetDirection()
   SetDistance()
   SaveData()
  elseif char == m then
   if SData.Summon then
    SData.Summon = false
   else
    SData.Summon = true
   end
  SaveData()
  elseif char == d then
   SetDimensions()
   SaveData()
  elseif char == j then
   if Confirm() then
    Warp()
   end
  elseif char == c then
   Summon()
  elseif char == b then
   JumpToBeacon()
  elseif char == g then
   JumpToGate()
  elseif char == h then
   if Confirm() then
    component.warpdriveShipController.mode(5)
    component.warpdriveShipComponent.jump()
   end
  elseif char == x then
   mainloop = false
  end
 end
end
 
if SData.Summon then
 SData.Summon = false
 SaveData()
end
Clear()
print("Shutting Down")
component.warpdriveShipController.mode(0)
os.sleep(0.5)

 

I might be missing something, but I can't see where you've assigned values to variables s, m, d, j, c, b, g, h, x etc.

 

I think you mean to put a " " (possibly ' ', not sure if it matters in lua) around these characters. At the moment it looks like you are trying to compare the key pressed with variables that haven't been declared or assigned values.

Link to post
Share on other sites
  • 0

sorry about the long delay getting back to this, thanks for the help. This is a  great help. I'll play around with it to see if I can fix that problem.

I would like to know if there is some way to externally edit the program files. Trying to type that all in game would be a chore.

Once this gets fixed, I'll pass it along to the Warp Drive modders and I'll def include you in the credit for the conversion.

Link to post
Share on other sites
  • 0

Okay try this go to: C:/user/(name)/.appdata/roaming/.minecraft/worlds/(world name)/OC (or is it OpenComputers) then look for the hard drive (example: a63c-15ca-6fse-

              (or what ever drive it is on)

353) and then use your favorite text and then make the program, make sure to add .lua at the end (autorun.lua etc...)

Link to post
Share on other sites
  • 0

Okay try this go to: C:/user/(name)/.appdata/roaming/.minecraft/worlds/(world name)/OC (or is it OpenComputers) then look for the hard drive (example: a63c-15ca-6fse-

              (or what ever drive it is on)

353) and then use your favorite text and then make the program, make sure to add .lua at the end (autorun.lua etc...)

I've tried that, and OC didn't want to recognize it....I'll try again.

I also am in my .minecraft folder so much I have it pinned. :)

 

edit: it does work, but it seems I loose the ability to edit the program in game....strange.

Link to post
Share on other sites
  • 0
    -- Component lib for access to the gpu
    local component = require "component"
     
    -- Put term lib into local space..
    local term = require "term"
     
    -- Other libs we need as well
    local textutils = require("serialization") -- CC to OC. The 'serialize' method is used the same here i think....
    local gpu = component.getPrimary("gpu")
    local fs = require("filesystem")
    local event = require("event")
    local keyboard = require("keyboard")
    local os=require("os")
    -- 'term' Compatibility. These libs are similar in interface but OC lib has shorter method names. 
    term.setCursorPos, term.getCursorPos = term.setCursor, term.getCursor
    term.setTextColor, term.setBackgroundColor = gpu.setForeground, gpu.setBackground
    read=term.read 
    sleep=os.sleep
    print("loading...")
    warp = component.warpdriveShipController
     
    Style = {
     CDeflt = 0xFFFFFF,
     BGDeflt = 0x0000FF,
     CTitle = 0x000000,
     BGTitle = 0x0092FF,
     CWarn = 0xFFFFFF,
     BGWarn = 0xFF0000
    }
     
    function SetColorDeflt()
     term.setBackgroundColor(Style.BGDeflt)
     term.setTextColor(Style.CDeflt)
    end
     
    function SetColorTitle()
     term.setBackgroundColor(Style.BGTitle)
     term.setTextColor(Style.CTitle)
    end
     
    function SetColorWarn()
     term.setBackgroundColor(Style.BGWarn)
     term.setTextColor(Style.CWarn)
    end
     
    function Clear()
     term.clear()
     term.setCursorPos(1,1)
    end
     
    function Show(Text)
     term.write(Text)
     local xt,yt = term.getCursorPos()
     term.setCursorPos(1, yt+1)
    end
     
    function ShowTitle(Text)
     SetColorTitle()
     term.setCursorPos(12, 1)
     Show(Text)
     SetColorDeflt()
    end
     
    function ShowMenu(Text)
     term.write(Text)
     local xt, yt = term.getCursorPos()
     for i = xt, 51 do
      term.write(" ")
     end
     term.setCursorPos(1, yt+1)
    end
     
    function ShowWarning(Text)
      SetColorWarn()
      term.setCursorPos(10, 19)
      term.write(" "..Text.." ")
      SetColorDeflt()
    end
     
    function SaveData()
     local file = fs.open("shipdata.txt", "w")
     file:write(textutils.serialize(SData))
     file:close()
    end
     
    function ReadData()
     local file = fs.open("shipdata.txt", "r")
     local size=fs.size("shipdata.txt")
     local rawD=file:read(size)
     print(rawD)
     SData = textutils.unserialize(rawD)
     file:close()
    end
     
    function Explode(d, p)
     local t, ll
     t = {}
     ll = 0
     if(#p == 1) then return {p} end
     while true do
      l = string.find(p ,d, ll, true)
      if l ~= nil then 
       table.insert(t, string.sub(p, ll, l-1))
       ll = l+1
      else
       table.insert(t, string.sub(p, ll))
       break
      end
     end
     return t
    end
     
    function ShowDirection()
     if SData.Direction == 1 then
      Show(" Direction        = Up")
     elseif SData.Direction == 2 then
      Show(" Direction        = Down")
     elseif SData.Direction == 0 then
      Show(" Direction        = Front")
     elseif SData.Direction == 180 then
      Show(" Direction        = Back")
     elseif SData.Direction == 90 then
      Show(" Direction        = Left")
     elseif SData.Direction == 255 then
      Show(" Direction        = Right")
     end
    end
     
    function CalcRealDistance()
     if IsInHyper then
      RealDistance = SData.Distance * 100
      MinimumDistance = 1
      JumpCost = (1000 * Weight) + (1000 * SData.Distance)
     else
      if SData.Direction == 1 or SData.Direction == 2 then
       MinimumDistance = GUp + GDown
       RealDistance = SData.Distance + MinimumDistance
      elseif SData.Direction == 0 or SData.Direction == 180 then
       MinimumDistance = GFront + GBack
       RealDistance = SData.Distance + MinimumDistance
      elseif SData.Direction == 90 or SData.Direction == 255 then
       MinimumDistance = GLeft + GRight
       RealDistance = SData.Distance + MinimumDistance
      end
      MinimumDistance = MinimumDistance + 1
      JumpCost = (10 * Weight) + (100 * SData.Distance)
     end
    end
     
    function CalcNewCoords(cx, cy, cz)
     local res = {x=cx, y=cy, z=cz}
     if SData.Direction == 1 then
      res.y = res.y + RealDistance
     elseif SData.Direction == 2 then
      res.y = res.y - RealDistance
     end
     local dx = component.warpdriveShipController.direction("X")
     local dz = component.warpdriveShipController.direction("Z")
     if dx ~= 0 then
      if SData.Direction == 0 then
       res.x = res.x + (RealDistance * dx)
      elseif SData.Direction == 180 then
       res.x = res.x - (RealDistance * dx)
      elseif SData.Direction == 90 then
       res.z = res.z + (RealDistance * dx)
      elseif SData.Direction == 255 then
       res.z = res.z - (RealDistance * dx)
      end
     else
      if SData.Direction == 0 then
       res.z = res.z + (RealDistance * dz)
      elseif SData.Direction == 180 then
       res.z = res.z - (RealDistance * dz)
      elseif SData.Direction == 90 then
       res.x = res.x + (RealDistance * dz)
      elseif SData.Direction == 255 then
       res.x = res.x - (RealDistance * dz)
      end
     end
     return res
    end
     
    function ShowInfo()
     ShowTitle(Title)
     Show("Core:")
     Show(" x, y, z          = "..X..", "..Y..", "..Z)
     local energy = component.warpdriveShipController.energy()
     Show(" Energy           = "..math.floor(energy / 1000000).." % ("..energy.."EU)")
     Show(" Attached players = "..component.warpdriveShipController.getAttachedPlayers())
     Show("Dimensions:")
     Show(" Front, Right, Up = "..GFront..", "..GRight..", "..GUp)
     Show(" Back, Left, Down = "..GBack..", "..GLeft..", "..GDown)
     Show(" Size             = "..Weight.." blocks")
     Show("Warp data:")
     ShowDirection()
     local dest = CalcNewCoords(X, Y, Z)
     Show(" Distance         = "..RealDistance.." ("..JumpCost.."EU, "..math.floor(energy/JumpCost).." jumps)")
     Show(" Dest.coordinates = "..dest.x..", "..dest.y..", "..dest.z)
     if SData.Summon then
      Show(" Summon after     = Yes")
     else
      Show(" Summon after     = No")
     end
    end
     
    function Confirm()
     ShowWarning("Are you sure? (y/n)")
     ev, p1, p2, p3, p4, p5 = event.pull("key_down")
     local char = keyboard.keys[p3]
     if char == 'y' then
      return true
     else
      return false
     end
    end
     
    function Warp()
     os.sleep(1)
     component.warpdriveShipController.direction(SData.Direction)
     if IsInHyper then
      component.warpdriveShipController.mode(2)
     else
      component.warpdriveShipController.mode(1)
     end
     component.warpdriveShipController.jump()
    end
     
    function SetDistance()
     Clear()
     ShowTitle("<====  Set distance  ====>")
     SData.Distance = 0
     CalcRealDistance()
     MaximumDistance = MinimumDistance + 127
     if IsInHyper then
      term.write("Distance * 100 (min "..MinimumDistance..", max "..MaximumDistance.."): ")
     else
      term.write("Distance (min "..MinimumDistance..", max "..MaximumDistance.."): ")
     end
     os.sleep(0.3)
     SData.Distance = tonumber(term.read())
     if SData.Distance == nil then SData.Distance = 1 end
     if SData.Distance < MinimumDistance or SData.Distance > MaximumDistance then
      SData.Distance = 1
      ShowWarning("Wrong distance. Try again.")
      event.pull("key")
      CalcRealDistance()
     else
      if not IsInHyper then
       SData.Distance = SData.Distance - RealDistance
      end
      warp.distance(SData.Distance)
      CalcRealDistance()
     end
    end
     
    function SetDirection()
 local drun = true
 while(drun) do
  Clear()
  ShowTitle("<==== Set direction ====>")
  ShowDirection()
  term.setCursorPos(1, 16)
  SetColorTitle()
  ShowMenu("Use directional keys")
  ShowMenu("W/S keys for Up/Down")
  ShowMenu("Enter - confirm")
  SetColorDeflt()
  _,_,_,keycode=event.pull("key")
  if keycode == 200 then
   SData.Direction = 0
  elseif keycode == 17 then
   SData.Direction = 1
  elseif keycode == 203 then
   SData.Direction = 90
  elseif keycode == 205 then
   SData.Direction = 255
  elseif keycode == 208 then
   SData.Direction = 180
  elseif keycode == 31 then
   SData.Direction = 2
  elseif keycode == 28 then
   drun = false
  end
 end
end
 
function SetDimensions()
 Clear()
 os.sleep(0.3)
 ShowTitle("<==== Set dimensions ====>")
 term.write(" Front ("..GFront..") : ")
 GFront = tonumber(read())
 term.write(" Right ("..GRight..") : ")
 GRight = tonumber(read())
 term.write(" Up    ("..GUp..") : ")
 GUp = tonumber(read())
 term.write(" Back  ("..GBack..") : ")
 GBack = tonumber(read())
 term.write(" Left  ("..GLeft..") : ")
 GLeft = tonumber(read())
 term.write(" Down  ("..GDown..") : ")
 GDown = tonumber(read())
 term.write("Setting dimensions...")
 component.warpdriveShipController.dim_positive(GFront, GRight, GUp)
 component.warpdriveShipController.dim_negative(GBack, GLeft, GDown)
end
 
function Summon()
 Clear()
 ShowTitle("<==== Summon players ====>")
 local players = Explode(",", component.warpdriveShipController.getAttachedPlayers())
 for i = 1, #players do
  Show(i..". "..players[i])
 end
 SetColorTitle()
 ShowMenu("Enter player number")
 ShowMenu("or press enter to summon everyone")
 SetColorDeflt()
 sleep(0.3)
 term.write(":")
 local input = read()
 if input == "" then
  component.warpdriveShipController.summon_all()
 else
  input = tonumber(input)
  component.warpdriveShipController.summon(input - 1)
 end
end
 
function JumpToBeacon()
 Clear()
 ShowTitle("<==== Jump to beacon ====>")
 sleep(0.3)
 term.write("Enter beacon frequency: ")
 local freq = tostring(read())
 if Confirm() then
  component.warpdriveShipController.mode(4)
  component.warpdriveShipController.beaconFrequency(freq)
  component.warpdriveShipController.jump()
 end
end
 
function JumpToGate()
 Clear()
 ShowTitle("<==== Jump to JumpGate ====>")
 os.sleep(0.3)
 term.write("Enter jumpgate name: ")
 local name = tostring(read())
 if Confirm() then
  print(name)
  os.sleep(10)
  component.warpdriveShipController.mode(6)
  component.warpdriveShipController.targetJumpgate(name)
  component.warpdriveShipController.jump()
 
end
end
 
 
if fs.exists("shipdata.txt") then
 ReadData()
else
 SData = {
  Summon = false,
  Distance = 1,
  Direction = 0
 }
end
 
component.gpu.setResolution(50, 20)
SetColorDeflt()
 
component.getPrimary("warpdriveShipController") 
 
if SData.Summon then
 component.warpdriveShipController.summon_all()
end
 
GFront, GRight, GUp = component.warpdriveShipController.dim_positive()
GBack, GLeft, GDown = component.warpdriveShipController.dim_negative()
IsInHyper = component.warpdriveShipController.isInHyperspace()
X, Y, Z  = component.warpdriveShipController.position()
Weight = component.warpdriveShipController.getShipSize()
 
CalcRealDistance()
 
component.warpdriveShipController.mode(1)
 
mainloop = true
while(mainloop) do
 Clear()
 ShowInfo()
 term.setCursorPos(1, 15)
 SetColorTitle()
 ShowMenu("D - Dimensions, M - Toggle summon")
 ShowMenu("S - Set Warp Data, J - Jump, G - Jump to JumpGate")
 ShowMenu("B - Jump to Beacon, H -Jump to Hyperspace")
 ShowMenu("C - Summon, X - Shutdown WarpCore and Exit")
 SetColorDeflt()
 ev, p1, p2, p3, p4, p5 = event.pull("key_down")
 if ev == "key_down" then
  local char = keyboard.keys[p3]
    if char == "s" then
   SetDirection()
   SetDistance()
   SaveData()
  elseif char == "m" then
   if SData.Summon then
    SData.Summon = false
   else
    SData.Summon = true
   end
  SaveData()
  elseif char == "d" then
   SetDimensions()
   SaveData()
  elseif char == "j" then
   if Confirm() then
    Warp()
   end
  elseif char == "c" then
   Summon()
  elseif char == "b" then
   JumpToBeacon()
  elseif char == "g" then
   JumpToGate()
  elseif char == "h" then
   if Confirm() then
    component.warpdriveShipController.mode(5)
    component.warpdriveShipController.jump()
   end
  elseif char == "x" then
   mainloop = false
  end
 end
end
 
if SData.Summon then
 SData.Summon = false
 SaveData()
end
Clear()
print("Shutting Down")
component.warpdriveShipController.mode(0)
os.sleep(0.5)

This one worked fine to me.

Link to post
Share on other sites
  • 0

it mostly works:

it will crash trying to set the warp distance after warping, resetting the program fixes this.

it won't properly update the coordinates on the screen, but does store the coordinates and will correct itself if you restart the program

the energy needs to be setup so that it will update every few seconds

the summon doesn't work, but that may be due to API changes in warp core, will have to check the code to make sure they haven't changed.

and the screen doesn't update after a jump.

it acts like several variables aren't being recalled after a jump or something.

I also need to add in a cooldown timer on the screen so that you know that you can't jump just yet and that is why the program appears to not be responding if you try and jump too soon.

Link to post
Share on other sites
  • 0

I've updated the mod with a OC script for the ship controller, available here: https://github.com/LemADEC/WarpDrive/blob/MC1.7/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveShipController/startup.lua

Thanks for showing me here the part I was missing on data saving.

 

Any idea how to rename a computer from a LUA script, as seen here https://github.com/LemADEC/WarpDrive/blob/MC1.7/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveShipController/startup.lua#L355 ?

 

Regarding variables values lost, it might be due to node being reset when ship jump, should be fixed in next update.

 

Last but not least, any idea how to have the startup.lua script actually run automatically?

Link to post
Share on other sites
  • 0

I've updated the mod with a OC script for the ship controller, available here: https://github.com/LemADEC/WarpDrive/blob/MC1.7/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveShipController/startup.lua

Thanks for showing me here the part I was missing on data saving.

 

Any idea how to rename a computer from a LUA script, as seen here https://github.com/LemADEC/WarpDrive/blob/MC1.7/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveShipController/startup.lua#L355 ?

 

Regarding variables values lost, it might be due to node being reset when ship jump, should be fixed in next update.

 

Last but not least, any idea how to have the startup.lua script actually run automatically?

last one is easy, rename it autorun.lua

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.