Advertisement
BIL123123

stargate my mod

Jul 20th, 2012
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.95 KB | None | 0 0
  1. gatelist={}
  2. strpart={}
  3. chat={}
  4. redsignal=0
  5. status="waiting"
  6.  
  7. function sgSay(word,pre,lng)
  8.     local i
  9.     local V={"givename","nomodem","skip","yesmodem","nogates","incoming","connect","opened","cmdlist","startup","yes","no","wrong","startsuc","namesuc","nocommand","cmdlist","install"}
  10.     local E={"Write address of the gate",
  11.         "Modem not detected, connect the modem",
  12.         "Press any key to continue",
  13.         "Modem is found",
  14.         "Gates not found",
  15.         "Incoming wormhole",
  16.         "Chevron 6 engaged",
  17.         "Gates are already opened",
  18.         "/list - view list of registred gates\n/getname - view the name of the gates\n/changename - change the name of the gates\n/changelang - change language\n/startup - add program to startup",
  19.         "Do you want to add a program to the startup?",
  20.         "1. Yes",
  21.         "2. No",
  22.         "Wrong value",
  23.         "Program added to startup",
  24.         "Name succsessfully changed",
  25.         "No such command",
  26.         "Type /cmdlist to view commands list",
  27.         "Do you want to install the program?"}
  28.     local R={"Napishi adres vrat",
  29.         "Modem ne obnaruzhen, podkljuchite modem",
  30.         "Nazhmite ljubuju klavishu dlja prodolzhenija",
  31.         "Modem najden",
  32.         "Vrat ne obnaruzheno",
  33.         "Vhodjashij tonnel'",
  34.         "Shevron 6 zablokirovan",
  35.         "Vrata uzhe otkryty",
  36.         "/list - pokazat' spisok najdennyh vrat\n/getname - pokazat' imja vrat\n/changename - pomenjat' imja vrat\n/changelang - pomenjat' jazyk\n/startup - dobavit' programmu v avtozapusk",
  37.         "Vy hotite dobavit' programmu v avtozapusk?",
  38.         "1. Da",
  39.         "2. Net",
  40.         "Nepravil'noe znachenie",
  41.         "Pprogramma dobavlena v avtozapusk",
  42.         "Imja uspeshno izmeneno",
  43.         "Takoj komandy ne suwestvuet",
  44.         "Napishi /cmdlist chtoby uvidet' spisok komand",
  45.         "Vy hotite ustanovit' programmu?"}
  46.     if lng==nil then lng=lang end
  47.     if pre==nil then pre="" end
  48.     for i=1,#V do
  49.         if V[i]==word then
  50.             word=i
  51.             break
  52.         end
  53.     end
  54.     if lng==1 then
  55.         sgWrite(E[word])
  56.     elseif lng==2 then
  57.         sgWrite(R[word])
  58.     end
  59. end
  60.  
  61. function sgCmdList(lng)
  62.     local i
  63.     local V={"/list","/getname","/changename","/changelang","/startup","/close","/clear"}
  64.     local E={"view list of registred gates",
  65.     "view the name of the gates",
  66.     "change the name of the gates",
  67.     "change language",
  68.     "add program to startup",
  69.     "close the gates",
  70.     "clear screen"}
  71.     local R={"pokazat' spisok najdennyh vrat",
  72.     "pokazat' imja vrat",
  73.     "pomenjat' imja vrat",
  74.     "pomenjat' jazyk",
  75.     "dobavit' programmu v avtozapusk",
  76.     "zakryt' vrata",
  77.     "otchistit' ekran"}
  78.     if lng==nil then lng=lang end
  79.     for i=1,#V do
  80.         if lng==1 then
  81.             sgWrite(V[i].." - "..E[i])
  82.         elseif lng==2 then
  83.             sgWrite(V[i].." - "..R[i])
  84.         end
  85.     end
  86. end
  87.  
  88. function sgSave()
  89.     strpart[1]=lang
  90.     strpart[2]=gatename
  91.     file = io.open("stargate/stargate.dat", "w")
  92.     local i=1
  93.     while true do
  94.         if strpart[i] == nil then break end
  95.         file:write(strpart[i].."\n")
  96.         i=i+1
  97.     end
  98.     file:close()
  99. end
  100.  
  101. function sgLoad()
  102.     file=io.open("stargate/stargate.dat", "r")
  103.     local i=1
  104.     while true do
  105.         line=file:read()
  106.         if line==nil then break end
  107.         strpart[i]=line
  108.         i=i+1
  109.     end
  110.     file:close()
  111.     lang=strpart[1]+0
  112.     gatename=strpart[2]
  113. end
  114.  
  115. function sgLang()
  116.     sgWrite("Choose language / Vyberi jazyk")
  117.     sgWrite("1. English")
  118.     sgWrite("2. Russkij")
  119.     repeat
  120.         lang=sgRead("lang")
  121.         if lang=="1" then sgWrite("English")
  122.         elseif lang=="2" then sgWrite("Russkij")
  123.         else sgWrite("Wrong value / Nepravil'noe znachenie") end
  124.     until lang=="1" or lang=="2"
  125.     lang=lang+0
  126. end
  127.  
  128. function sgWrite(text)
  129.     if text~="" then
  130.         local x,y=term.getCursorPos()
  131.         chat[#chat+1]=text
  132.         local k=#chat
  133.         local i
  134.         for i=16,2,-1 do
  135.             if chat[k]==nil then break end
  136.             term.setCursorPos(1,i)
  137.             term.clearLine()
  138.             write(chat[k])
  139.             k=k-1
  140.         end
  141.         term.setCursorPos(x,y)
  142.     end
  143. end
  144.  
  145. function sgRead(pre)
  146.     cmd=""
  147.     if pre==nil then pre="" end
  148.     term.setCursorPos(1, 18)
  149.     term.clearLine()
  150.     write(pre.."> ")
  151.     while true do
  152.         event,key=os.pullEvent()
  153.         if event=="char" then
  154.             write(key)
  155.             cmd=cmd..key
  156.         elseif event=="key" then
  157.             if key==14 and #cmd>0 then
  158.                 term.setCursorPos(#pre+#cmd+2,18)
  159.                 write(" ")
  160.                 term.setCursorPos(#pre+#cmd+2,18)
  161.                 cmd=string.sub(cmd,1,#cmd-1)
  162.             elseif key==28 then break end
  163.         end
  164.     end
  165.     sgWrite(cmd)
  166.     return cmd
  167. end
  168.  
  169. function sgReceiver() --ХBF?80ХB8ХBDХB8ХBCХB0е?82 ?81ХB8ХB3ХBDХB0ХBB ХB8 ХB7ХB0ХBFХB8?81?8BХB2ХB0е?82 ХB2?80ХB0?82ХB0 ХB2 ?81ХBFХB8?81ХBEк/ХBF?80ХB8ХBDХB8ХBCХB0е?82 ХB2?85ХBEХB4?8F?89ХB8ХB9 ?82ХBEХBDХBDеХBB?8C.
  170.     local i
  171.     local msgs
  172.     while true do
  173.         id,msg=rednet.receive()
  174.         msg=textutils.unserialize(msg)
  175.         if msg[1]=="announce" then
  176.             msgs=0
  177.             for i=1,#gatelist do
  178.                 if gatelist[i]==msg[2] then msgs=msgs+1 end
  179.             end
  180.             if msgs==0 then gatelist[#gatelist+1]=msg[2] end
  181.         elseif msg[1]=="open" then
  182.             if msg[2]==gatename and status=="waiting" then status="incoming" end
  183.         end
  184.     end
  185. end
  186.  
  187. function sgTransmitter() --ХBF?80ХB8ХBDХB8ХBCХB0е?82 кХBEХBCХB0ХBDХB4?8B/ХBE?82к?80?8BХB2ХB0е?82 ?82ХBEХBDХBDеХBB?8C
  188.     while true do
  189.         cmd=sgRead()
  190.         if cmd=="/cmdlist" then
  191.             sgCmdList()
  192.         elseif cmd=="/list" then
  193.             local i
  194.             if gatelist[1]==nil then
  195.                 sgSay("nogates")
  196.             else
  197.                 for i=1,#gatelist do
  198.                     if gatelist[i]==nil then break end
  199.                     sgWrite(gatelist[i])
  200.                 end
  201.             end
  202.         elseif cmd=="/getname" then
  203.             sgWrite(gatename)
  204.         elseif cmd=="/changename" then
  205.             while true do
  206.                 gatename=sgRead("gatename")
  207.                 if gatename=="" or string.byte(gatename,1)==47 or string.find(gatename,";")~=nil or string.find(gatename," ")~=nil then
  208.                     sgSay("wrong")
  209.                 else
  210.                     sgSay("namesuc")
  211.                     break
  212.                 end
  213.             end
  214.             sgSave()
  215.         elseif cmd=="/changelang" then
  216.             sgWrite("1. English")
  217.             sgWrite("2. Russkij")
  218.             repeat
  219.                 lang=sgRead("lang")
  220.                 if lang=="1" then sgWrite("English")
  221.                 elseif lang=="2" then sgWrite("Russkij")
  222.                 else sgSay("wrong") end
  223.             until lang=="1" or lang=="2"
  224.             lang=lang+0
  225.             sgSave()
  226.         elseif cmd=="/startup" then
  227.             file=io.open("startup", "w")
  228.             file:write("shell.run(\"stargate/stargate.dat\")")
  229.             file:close()
  230.             sgSay("startsuc")
  231.         elseif cmd=="/close" then
  232.             status="waiting"
  233.             redsignal=0
  234.         elseif cmd=="/clear" then
  235.             term.clear()
  236.             term.setCursorPos(1, 1)
  237.             write("GateOS 1.1")
  238.             term.setCursorPos(1, 16)
  239.             write("--------------------------------------------------")
  240.             chat={}
  241.         elseif string.find(cmd," ")~=nil then
  242.             sgSay("wrong")
  243.         elseif cmd=="" then
  244.         else
  245.             if string.byte(cmd,1)==47 then
  246.                 sgSay("nocommand")
  247.             elseif status=="waiting" then
  248.                 rednet.broadcast(textutils.serialize({"open",cmd}))
  249.                 status="connecting"
  250.                 sgSay("connect")
  251.             else
  252.                 sgSay("opened")
  253.             end
  254.         end
  255.     end
  256. end
  257.  
  258. function sgConnecter() --ХB2кХBB?8E?87ХB0е?82/ХB2?8BкХBB?8E?87ХB0е?82 ХB2?80ХB0?82ХB0
  259.     local cnt
  260.     local mo
  261.     while true do
  262.         modemside=nil
  263.         for i=1,#rs.getSides() do
  264.             if peripheral.getType(rs.getSides()[i])=="modem" then
  265.                 modemside=rs.getSides()[i]
  266.             end
  267.         end
  268.         if modemside==nil then
  269.             sgSay("nomodem")
  270.             mo=false
  271.         else
  272.             if mo==false then
  273.                 sgSay("yesmodem")
  274.                 sleep(1)
  275.                 os.reboot()
  276.             end
  277.         end
  278.         rednet.broadcast(textutils.serialize({"announce",gatename}))
  279.         if status=="incoming" then
  280.             redstone.setOutput("left", true)
  281.             sgSay("incoming")
  282.             status="opened"
  283.             cnt=0
  284.         elseif status=="connecting" then
  285.             redstone.setOutput("right", true)
  286.             sgSay("connecting")
  287.             status="opened"
  288.             cnt=0
  289.         end
  290.         if status=="opened" then
  291.             cnt=cnt+1
  292.             if cnt==10 then
  293.                 status="waiting"
  294.                 redstone.setOutput("left", false)
  295.                 redstone.setOutput("right", false)
  296.             end
  297.         end
  298.        
  299.         sleep(1)
  300.     end
  301. end
  302.  
  303. term.clear()
  304. term.setCursorPos(1, 1)
  305. write("GateOS 1.1")
  306. term.setCursorPos(1, 16)
  307. write("--------------------------------------------------")
  308. term.setCursorBlink(true)
  309. sgSay("cmdlist")
  310.  
  311. if shell.resolveProgram("stargate/stargate.dat")==nil then
  312.     if shell.resolveProgram("stargate/stargate.exe")==nil then
  313.         sgLang()
  314.         sgSay("install")
  315.         sgSay("yes")
  316.         sgSay("no")
  317.         repeat
  318.             inst=sgRead("install")
  319.             if inst=="1" then
  320.                 shell.run("mkdir","stargate")
  321.                 shell.run("mv",shell.getRunningProgram(),"stargate/stargate.exe")
  322.                 file=io.open("startup", "w")
  323.                 file:write("shell.run(\"stargate/stargate.exe\")")
  324.                 file:close()
  325.                 os.reboot()
  326.             elseif inst=="2" then
  327.                 os.reboot()
  328.             else sgSay("wrong") end
  329.         until inst=="1" or inst=="2"
  330.     end
  331.     sgLang()
  332.     sgSay("givename")
  333.     while true do
  334.         gatename=sgRead("gatename")
  335.         if gatename=="" or string.byte(gatename,1)==47 or string.find(gatename,";")~=nil or string.find(gatename," ")~=nil then
  336.             sgSay("wrong")
  337.         else
  338.             break
  339.         end
  340.     end
  341.     sgSave()
  342. else
  343.     sgLoad()
  344. end
  345.  
  346. while modemside==nil do
  347.     for i=1,#rs.getSides() do
  348.         if peripheral.getType(rs.getSides()[i])=="modem" then
  349.             modemside=rs.getSides()[i]
  350.         end
  351.     end
  352.     if modemside==nil then
  353.         sgSay("nomodem")
  354.         sgSay("skip")
  355.         os.pullEvent("char")
  356.     else
  357.         sgSay("yesmodem")
  358.         rednet.open(modemside)
  359.     end
  360. end
  361.  
  362. parallel.waitForAll(sgReceiver,sgConnecter,sgTransmitter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement