Quipex

Disk/mem/redworks/apis/redworks

Apr 22nd, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.26 KB | None | 0 0
  1. local user=""
  2. local locked=false
  3. -- functions for making read only values
  4.  
  5. function version()
  6.     return "1.21"
  7. end
  8.  
  9. function sprint(text)
  10.   textutils.slowPrint(text)
  11. end
  12.  
  13. function waitForKey()
  14.     while true do
  15.         event, param = os.pullEvent()
  16.         if event == "key" then
  17.             break
  18.         end
  19.     end
  20.     return param
  21. end
  22.  
  23. function setUser(sUser)
  24.     if not locked then user=sUser end
  25.     locked=true
  26. end
  27.  
  28. function getUser()
  29.     return user
  30. end
  31.  
  32. function reading()
  33.   sleep(0.5)
  34.   write "."
  35. end
  36.  
  37. function showLogo()
  38.     term.clear()
  39.     term.setCursorPos(1,1)
  40.     print("   " .. os.version() .. " with Redworks "..redworks.version().." Terminal ID# " .. os.getComputerID())
  41.     print("   _ \\            |                    |        ")
  42.     print("     /   -_)   _' | \\ \\  \\ /  _ \\   _| | / (_-< ")
  43.     print("  _|_\\ \\___| \\__,_|  \\_/\\_/ \\___/ _|  _\\_\\ ___/ ")
  44.     print("    Development Team at http://redworksos.com")
  45. end
  46.  
  47. function showLogo2()
  48.     term.clear()
  49.     print"@@@@@@@  @@@@@@@@ @@@@@@@"
  50.     print"@@!  @@@ @@!      @@!  @@@"
  51.     print"@!@!!@!  @!!!:!   @!@  !@!"
  52.     print"!!: :!!  !!:      !!:  !!!"
  53.     print"::   : : : :: ::  :: :::"
  54.     print"@@@  @@@  @@@  @@@@@@  @@@@@@@  @@@  @@@  @@@@@@"
  55.     print"@@!  @@!  @@! @@!  @@@ @@!  @@@ @@!  !@@ !@@"
  56.     print"@!!  !!@  @!@ @!@  !@! @!@!!@!  @!@@!@!   !@@!!"
  57.     print" !:  !!:  !!  !!:  !!! !!: :!!  !!: :!!      !:!"
  58.     print"  ::.:  :::    : :. :   :   : :  :   ::: ::.: : "
  59. end
  60.  
  61. function doLogin() --returns true if login succeeds, false if it fails
  62.     local sPasswdPath = "ACCOUNTS.F"
  63.     --check we have a login path, if not, what we enter will create the account instead of log in
  64.     if fs.exists( sPasswdPath ) then--Password exists so this is not the first run
  65.         local loginCount=0
  66.         while loginCount < 3 do
  67.             local tPasswd={}
  68.             local file = fs.open(sPasswdPath,"r")
  69.             local sLine = file:readLine()
  70.             while sLine do
  71.                 for k, v in string.gmatch(sLine, "(%w+)%s(%w+)") do
  72.                     tPasswd[k]=v
  73.                 end
  74.                 sLine = file:readLine()
  75.             end
  76.             file:close()
  77.             numItems = 0
  78.             for k,v in pairs(tPasswd) do
  79.                 numItems = numItems + 1
  80.             end
  81.  
  82.             if (numItems==0) then
  83.                 --no passwords, not first run, so exit with out login.
  84.                 print(" ")
  85.                 print("To secure this terminal use:\n\nadduser username password")
  86.                 sleep(3)
  87.                 return "",true
  88.             end
  89.             local ux,uy,px,py=8,8,8,10
  90.             local size,len = 2,10
  91.             rawWriteLoc(ux,uy,"Username:")
  92.             rawWriteLoc(px,py,"Password:")
  93.             textBox(ux+10,uy-1,len,size,false,false,"",true)
  94.             textBox(px+10,py-1,len,size,false,true,"",true)
  95.             local enterPressed = false
  96.             local username,password="",""
  97.             while not enterPressed do
  98.                 username, enterPressed=textBox(ux+10,uy-1,len,size,true,false,username,true)
  99.                 password, enterPressed=textBox(px+10,py-1,len,size,true,true,"",true)
  100.             end
  101.             if (tPasswd[username]==password) then --Login ok
  102.                 return username,true
  103.             else --invalid login
  104.                 loginCount=loginCount+1
  105.                 rawWriteLoc(ux+10,7,"Invalid login")
  106.             end
  107.         end
  108.         return "",false
  109.     else--This is the first run, ask if they want to secure this box.      
  110.         local iSelected, sText = selectWindow(nil,nil,"Lock Terminal",{"Yes","No"})
  111.         clearArea(16,6,17,7)
  112.         local userFocus=true
  113.         if (sText=="Yes") then
  114.             local passwordMatch=false
  115.             local username,password,password2="",""
  116.             while not passwordMatch do
  117.                 local ux,uy=12,8
  118.                 local px,py=ux,10
  119.                 local size,len = 2,10
  120.                 rawWriteLoc(ux,uy,"Username:")
  121.                 rawWriteLoc(px,py,"Password:")
  122.                 rawWriteLoc(px,py+2,"Re-enter:")
  123.                 textBox(ux+10,uy-1,len,size,false,false,"xlilcasper",true)
  124.                 textBox(px+10,py-1,len,size,false,true,"",true)
  125.                 textBox(px+10,py+1,len,size,false,true,"",true)
  126.                 local enterPressed = false
  127.  
  128.                 while not enterPressed do
  129.                     username, enterPressed=textBox(ux+10,uy-1,len,size,userFocus,false,username,true)
  130.                     password, enterPressed=textBox(px+10,py-1,len,size,true,true,"",true)
  131.                     password2, enterPressed=textBox(px+10,py+1,len,size,true,true,"",true)
  132.                 end
  133.                 if (password == password2) then
  134.                     passwordMatch = true
  135.                 else
  136.                     userFocus=false
  137.                     rawWriteLoc(ux+1,7,"Passwords do not match")
  138.                 end
  139.             end
  140.             --Our passwords match, store them for later.
  141.                 tPasswd={}
  142.                 tPasswd[username]=password
  143.  
  144.                 local file = io.open(sPasswdPath,"w")
  145.                 for user,pass in pairs(tPasswd) do
  146.                     file:write(user .. " " .. pass .."\n")
  147.                 end
  148.                 file:close()
  149.                 return username,true
  150.         else --Do not secure
  151.             local file = io.open(sPasswdPath,"w")
  152.             file:close()
  153.             return "",true
  154.         end
  155.     end
  156. end
  157.  
  158. --Helper functions
  159.  
  160. --XLC (XLilCaspers) Windows libary included with permissions. Access with redworks.showWindow,redworks.textBox, redworks.selectWindow...
  161. function rawWriteLoc(x,y,sText)
  162.     term.setCursorPos(x,y)
  163.     term.write(sText)
  164. end
  165.  
  166. function textBox(x,y,len,size,focus,hideChar,default,disableTerm)
  167.     local h=2
  168.     local w=len+3
  169.     local char=""
  170.     local line=""
  171.     if size==1 then
  172.         y=y
  173.         w=len
  174.         for tx=0,w do
  175.             rawWriteLoc(x+tx,y,"_")
  176.         end
  177.     elseif size==2 then
  178.         for tx=0,w do
  179.             rawWriteLoc(x+tx,y+2,"-")
  180.         end
  181.     else
  182.         for ty=0,h do
  183.             for tx=0,w do
  184.                 char=" "
  185.                 if (ty==0) or (ty==h) then --horizonal line
  186.                     if (tx==0 or tx==w) then
  187.                         char="+"
  188.                     else
  189.                         char="-"
  190.                     end
  191.                 elseif (ty==1) then
  192.                     if (tx==0 or tx==w) then
  193.                         char="|"
  194.                     end
  195.                 else--normal line
  196.                     if (tx==0 or tx==w) then
  197.                         char="|"
  198.                     end
  199.                 end
  200.             line=line..char
  201.             end
  202.             rawWriteLoc(x,y+ty,line)
  203.             line=""
  204.         end
  205.     end
  206.  
  207.     if (focus) then
  208.         if size==2 then
  209.             y=y+1
  210.             len=len+3
  211.         elseif size==3 then
  212.             x=x+2
  213.             y=y+1
  214.         end
  215.         term.setCursorPos(x,y)
  216.         term.setCursorBlink(true)
  217.         sText=default or ""
  218.         rawWriteLoc(x,y,sText)
  219.         bExit = false
  220.         local dispText=""
  221.         local clearChar=" "
  222.         if size==1 then clearChar="_" end
  223.         local falt, sEvent, param = nil,nil,nil
  224.         while not bExit do
  225.             if disableTerm then
  226.                 falt, sEvent, param = pcall(os.pullEvent)
  227.             else
  228.                 sEvent, param = os.pullEvent()
  229.                 falt=false
  230.             end
  231.  
  232.             if sEvent == "key" then
  233.                 if param == 28 then
  234.                     return sText,true
  235.                 elseif param==15 then
  236.                     return sText,false
  237.                 elseif param == 14 then
  238.                     sText = string.sub(sText,1,string.len(sText)-1)
  239.                 end
  240.             end
  241.  
  242.             if sEvent == "char" then
  243.                 sText = sText .. param
  244.             end
  245.  
  246.             local dispChars=string.len(sText)
  247.             if dispChars > len then dispChars=len end
  248.             if not hideChar then
  249.                 dispText = string.sub(sText,-len)
  250.             else
  251.                 dispText=""
  252.                 for n=1,dispChars do
  253.                     dispText=dispText.."*"
  254.                 end
  255.                 dispText = string.sub(dispText,-len)
  256.             end
  257.             dispChars=string.len(dispText)
  258.             for n=dispChars,len do
  259.                 dispText=dispText..clearChar
  260.             end
  261.             rawWriteLoc(x,y,dispText)
  262.             term.setCursorPos(x+dispChars,y)
  263.         end
  264.     else
  265.         return sText,true
  266.     end
  267. end
  268.  
  269. function tCopy(t)
  270.   local u = { }
  271.   for k, v in pairs(t) do u[k] = v end
  272.   return setmetatable(u, getmetatable(t))
  273. end
  274.  
  275. function showWindow(x,y,w,h,title,text)
  276.     local sTitle = title or ""
  277.     local sText = text or ""
  278.     width, height = term.getSize()
  279.     sTitle = string.sub(sTitle,1,w-2)
  280.     iTitleStart = ((w/2)-1)-((#sTitle-1)/2)
  281.     if (not x) then
  282.         x=(width-w)/2
  283.     end
  284.     if (not y) then
  285.         y=(height-h)/2
  286.     end
  287.     if (w > width) then w = width end
  288.     if (h > height) then h = height end
  289.     local line=""
  290.     local char=""
  291.     local curChar=1
  292.     local waitForLine=1
  293.     if (sTitle ~= "") then waitForLine = 2 end
  294.     for ty=0,h do
  295.         for tx=0,w do
  296.             char=" "
  297.             if (ty==0) or ((ty==2) and (sTitle ~= "")) or (ty==h) then --horizonal line
  298.                 if (tx==0 or tx==w) then
  299.                     char="+"
  300.                 else
  301.                     char="-"
  302.                 end
  303.             elseif (ty==1) then
  304.                 if (tx==0 or tx==w) then
  305.                     char="|"
  306.                 else
  307.                     --if (tx-iTitleStart < #sTitle+1) and (tx > iTitleStart) then
  308.                     --  char = string.sub(sTitle,tx-iTitleStart,tx-iTitleStart)
  309.                     --end
  310.                 end
  311.             else--normal line
  312.                 if (tx==0 or tx==w) then
  313.                     char="|"
  314.                 end
  315.             end
  316.         line=line..char
  317.         end
  318.         rawWriteLoc(x,y+ty,line)
  319.         line=""
  320.     end
  321.     if (sTitle ~= "") then
  322.         rawWriteLoc(x+iTitleStart+1,y+1,sTitle)
  323.     end
  324.     if (sText ~= "") then
  325.         term.setCursorPos(x+1,y+3)
  326.         local wrapedText = getWrapedText(x+1,y+2,x+w-2,y+h-1,sText)
  327.         local offset = 0
  328.         if (sTitle ~= "") then offset=2 end
  329.         for ty=1,#wrapedText do
  330.             rawWriteLoc(x+1,ty+y+offset,wrapedText[ty])
  331.             if (ty>h-offset-2) then break end
  332.         end
  333.     end
  334. end
  335.  
  336. function getWrapedText(sx,sy,w,h, sText )
  337.     local x,y = sx,sy
  338.     local line = ""
  339.     local wraped = { }
  340.     local function newLine()
  341.         table.insert(wraped,line)
  342.         line=""
  343.         x=sx
  344.     end
  345.  
  346.     local function storeText(text)
  347.         line=line..text
  348.         return #text
  349.     end
  350.  
  351.     -- Print the line with proper word wrapping
  352.     while string.len(sText) > 0 do
  353.         local whitespace = string.match( sText, "^[ \t]+" )
  354.         if whitespace then
  355.             -- Print whitespace
  356.             x = x + storeText(whitespace)
  357.             sText = string.sub( sText, string.len(whitespace) + 1 )
  358.         end
  359.  
  360.         local newline = string.match( sText, "^\n" )
  361.         if newline then
  362.             -- Print newlines
  363.             newLine()
  364.             sText = string.sub( sText, 2 )
  365.         end
  366.  
  367.         local text = string.match( sText, "^[^ \t\n]+" )
  368.         if text then
  369.             sText = string.sub( sText, string.len(text) + 1 )
  370.             if string.len(text) > w then
  371.                 -- Print a multiline word              
  372.                 while string.len( text ) > 0 do
  373.                 if x > w then
  374.                     newLine()
  375.                 end
  376.                     x = x + storeText( text )
  377.                     text = string.sub( text, (w-x) + 2 )
  378.                 end
  379.             else
  380.                 -- Print a word normally
  381.                 if x + string.len(text) > w then
  382.                     newLine()
  383.                 end
  384.                 x = x + storeText( text )
  385.             end
  386.         end
  387.     end
  388.     newLine()
  389.     return wraped
  390. end
  391.  
  392. function selectWindow(x,y,title,tOptions,iSelected)
  393.     local w=#title
  394.     local h=1
  395.     local selected=iSelected or 1
  396.     local tOptionsOrg=tCopy(tOptions)
  397.     local width, height = term.getSize()
  398.     for n=1,#tOptions do
  399.         tOptions[n]=" "..tOptionsOrg[n].."  "
  400.         if (string.len(tOptions[n])>w) then w=string.len(tOptions[n]) end      
  401.     end
  402.     w=w+2
  403.     local sTitle = title or ""
  404.     if (sTitle ~= "") then h = h + 2 end
  405.     h = h + #tOptions
  406.     tOptions[selected]="*"..tOptionsOrg[selected].."*"
  407.     if (not x) then
  408.         x=(width-w)/2
  409.     end
  410.     if (not y) then
  411.         y=(height-h)/2
  412.     end
  413.     showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
  414.  
  415.     --loop and capture arrow keys for selecting output.
  416.     bExit = false
  417.     while not bExit do
  418.         event, param = os.pullEvent()
  419.         if event == "key" then
  420.             if param == 197 then --Exit on pause/break
  421.                 bExit = true
  422.             elseif param == 208 then
  423.                 tOptions[selected]=" "..tOptionsOrg[selected].."  " --Clear selection
  424.                 selected=selected+1
  425.                 if (selected>#tOptions) then selected=1 end
  426.                 tOptions[selected]="*"..tOptionsOrg[selected].."*"
  427.                 showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
  428.             elseif param == 200 then
  429.                 tOptions[selected]=" "..tOptionsOrg[selected].."  " --Clear selection
  430.                 selected=selected-1
  431.                 if (selected<1) then selected=#tOptions end
  432.                 tOptions[selected]="*"..tOptionsOrg[selected].."*"
  433.                 showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
  434.             elseif param == 28 then
  435.                 return selected,tOptionsOrg[selected]
  436.             end
  437.         end
  438.  
  439.     end
  440. end
  441.  
  442. function clearArea(x,y,w,h,char)
  443.     local sChar = char or " "
  444.     for yy=0,h do
  445.         for xx=0,w do
  446.             rawWriteLoc(x+xx,y+yy,sChar)
  447.         end
  448.     end
  449. end
  450.  
  451. --End of windows libary
  452.  
  453. function class(base, init)
  454.    local c = {}    -- a new class instance
  455.    if not init and type(base) == 'function' then
  456.       init = base
  457.       base = nil
  458.    elseif type(base) == 'table' then
  459.     -- our new class is a shallow copy of the base class!
  460.       for i,v in pairs(base) do
  461.          c[i] = v
  462.       end
  463.       c._base = base
  464.    end
  465.    -- the class will be the metatable for all its objects,
  466.    -- and they will look up their methods in it.
  467.    c.__index = c
  468.  
  469.    -- expose a constructor which can be called by <classname>(<args>)
  470.    local mt = {}
  471.    mt.__call = function(class_tbl, ...)
  472.    local obj = {}
  473.    setmetatable(obj,c)
  474.    if init then
  475.       init(obj,...)
  476.    else
  477.       -- make sure that any stuff from the base class is initialized!
  478.       if base and base.init then
  479.       base.init(obj, ...)
  480.       end
  481.    end
  482.    return obj
  483.    end
  484.    c.init = init
  485.    c.is_a = function(self, klass)
  486.       local m = getmetatable(self)
  487.       while m do
  488.          if m == klass then return true end
  489.          m = m._base
  490.       end
  491.       return false
  492.    end
  493.    setmetatable(c, mt)
  494.    return c
  495. end
  496. -----------EXTRA----------
  497.  
  498. --[[ (( Rin API ))
  499.  
  500. Usage: Just use the following information to utilize the available commands
  501.  
  502. :::Functions:::
  503.  
  504.  ENTITY
  505.  
  506. [entity.new( Name, X, Y, Text, Visibility, Physical )] returns Integer ID
  507. Creates a new entity
  508. Name;String -- Entity Name -- Currently only usable via entity.getvar() and entity.setvar()
  509. X;Integer -- Entity X Position
  510. Y;Integer -- Entity Y Position
  511. Text;String -- The text on-screen that represents the entity
  512. Visibility;Bool -- Whether or not the entity is drawn
  513. Physical;Bool -- Whether or not the entity is a physical entity -- Currently only changeable via entity.getvar() and entity.setvar()
  514. Note: Physical entities are unable to move to the position of other physical entities, but can be created at the same position, watch out.
  515.  
  516. [entity.setpos(ID, x, y, r)] -- ID is the returned value of entity.new()
  517. Sets the position of entity ID to position x, y
  518. Or, if r is set to true, sets position relative to current position
  519. entity.setpos(ID, 3, 0, r) would add 3 to ID's x position, and 0 to ID's y position
  520.  
  521. [entity.setchar(ID, Text)]
  522. Changes the character, or group of characters, that represent the entity on-screen
  523.  
  524. [entity.setvis(ID, Bool)]
  525. Sets whether the entity is visible on-screen
  526.  
  527. [entity.setabs(ID, Bool)]
  528. Sets whether the entity's position is absolute( x:1 y:1 remains the same regardless of screen position. Useful for GUI )
  529.  
  530. [entity.addvar(ID, Var, Value)]
  531. Creates a new variable named Var of value Value to the entity ID
  532. Var:String -- Variable Name
  533. Value:Any -- Variable Value
  534.  
  535. [entity.setvar(ID, Var, Value)]
  536. Sets the value of the variable Var to the value Value on the entity ID
  537.  
  538. [entity.getvar(ID, Var)]
  539. Returns the value of the variable Var on the entity ID
  540.  
  541. [entity.getposx(ID)]
  542. Returns the X position of the entity ID
  543.  
  544. [entity.getposy(ID)]
  545. returns the Y position of the entity ID
  546.  
  547. [entity.remvar(ID, Var)]
  548. Removes the variable Var from the entity ID
  549.  
  550.  GET
  551.  
  552. [get.locphys(x, y)]
  553. Checks if there is a physical entity at position x,y
  554. returns physical entity's ID, otherwise returns false
  555.  
  556.  EXECUTABLE
  557.  
  558. [exec.quit()]
  559. Ends the current execution
  560.  
  561. [exec.draw()]
  562. Draws the screen -- Runs automatically if a custom exec.update isn't set
  563.  
  564. [exec.update()]
  565. User defined;
  566. Runs every step, exec.update() overrides exec.defupdate(), which is the default update function
  567. If you update yourself, you need to run exec.draw() or create your own drawing procedure
  568.  
  569. [exec.start()]
  570. Starts the execution, nothing will be executed beyond this point until exec.stop() is called!
  571.  
  572. [exec.beginstep()] [exec.step()] [exec.endstep()]
  573. User defined;
  574. These are run every step, and in the order seen above.
  575. This is a good place to put any sort of logic or conditional checks.
  576.  
  577. [exec.key(k)]
  578. User defined;
  579. This is run when a key is pressed, and the argument k contains the code for the key.
  580. Find the Key Codes here; http://www.minecraftwiki.net/wiki/Key_Codes
  581.  
  582. [exec.quit]
  583. Ends the execution, returning to the command line.
  584.  
  585. :::Variables:::
  586.  
  587. window.w -- Window Width
  588. window.h -- Window Height
  589. window.midw -- Center of the Window Width
  590. window.midh -- Center of the Window Height
  591.  
  592. screen.x -- Screen x offset
  593. screen.y -- Screen y offset
  594.  
  595. :::Entity Variables:::
  596. These are the default Entity variables, found with entity.getvar()
  597. 'Name' -- String -- Entity's name, which you set when making a new entity
  598. 'X Position' -- Integer -- Entity's X Position
  599. 'Y Position' -- Integer -- Entity's Y Position
  600. 'Character' -- String -- The string that represents the entity on-screen
  601. 'Visibility' -- Bool -- Defines whether an entity is drawn on-screen
  602. 'Physical' -- Bool -- Defines whether an entity is physical or not
  603. 'Absolute' -- Bool -- Defines whether an entity's location is absolute;
  604. Position (1,1) is the top left corner of the screen at all times if this is true for the object
  605. 'Variable Count' -- Integer -- Defines how many variables the entity has :::DO NOT CHANGE:::
  606.  
  607. ]]--
  608.  
  609. window = {}
  610. window.w,window.h = term.getSize()
  611. window.midw = window.w/2
  612. window.midh = window.h/2
  613.  
  614. screen = {}
  615.  
  616. screen.x = 0
  617. screen.y = 0
  618.  
  619. --[[ ENTITY VARIABLES AND FUNCTIONS ]]--
  620. entity = {}
  621. entitynum = 1
  622.  
  623. function entity.new(a, x, y, t, c, d) -- Adds a new Entity to the execution
  624.     id = entitynum
  625.     entity[id] = {}
  626.  
  627.     entity[id][1] = {}
  628.     entity[id][1][1] = 'Name'
  629.     entity[id][1][2] = a
  630.  
  631.     entity[id][2] = {}
  632.     entity[id][2][1] = 'X Position'
  633.     entity[id][2][2] = x -- XPos
  634.  
  635.     entity[id][3] = {}
  636.     entity[id][3][1] = 'Y Position'
  637.     entity[id][3][2] = y -- YPos
  638.  
  639.     entity[id][4] = {}
  640.     entity[id][4][1] = 'Character'
  641.     entity[id][4][2] = t -- Text Representing the Entity
  642.  
  643.     entity[id][5] = {}
  644.     entity[id][5][1] = 'Visibility'
  645.     entity[id][5][2] = c -- Depicts whether the object is visible or not
  646.  
  647.     entity[id][6] = {}
  648.     entity[id][6][1] = 'Physical'
  649.     entity[id][6][2] = d -- If an object is Physical, it cannot move onto the same spot as a physical object
  650.                          -- WARNING: Objects can still be set to physical using entity.setvar(), or by creating an object at the same spot as another object!
  651.  
  652.     entity[id][7] = {}
  653.     entity[id][7][1] = 'Absolute'
  654.     entity[id][7][2] = false -- Sets whether an object has an absolute position(1 = 1 on screen regardless of screen offset) or not
  655.  
  656.     entity[id][8] = {}
  657.     entity[id][8][1] = 'Variable Count'
  658.     entity[id][8][2] = 7 -- Counts how many variables there are
  659.  
  660.     entitynum = entitynum+1
  661.     return id
  662. end
  663.  
  664. function entity.setchar(a, b)
  665.     entity[a][4][2] = b
  666. end
  667.  
  668. function entity.setphys(a, b)
  669.     entity[a][5][2] = b
  670. end
  671.  
  672. function entity.setabs(a, b)
  673.     entity[a][7][2] = b
  674. end
  675.  
  676. function entity.addvar(a, b, c) -- Adds a new variable to an Entity
  677.     varcount = entity[a][8][2] + 1
  678.     entity[a][varcount] = {}
  679.     entity[a][varcount][1] = b
  680.     entity[a][varcount][2] = c
  681.     entity[a][8][2] = varcount
  682. end
  683.  
  684. function entity.remvar(a, b)
  685.     for i in ipairs(entity[a]) do
  686.         if entity[a][i][1] == b then
  687.             table.remove(entity[a], i)
  688.             entity[a][8][2] = entity[a][8][2] - 1
  689.             break
  690.         end
  691.     end
  692. end
  693.  
  694. function entity.setvar(a, b, c)
  695.     for i in ipairs(entity[a]) do
  696.         if entity[a][i][1] == b then
  697.             entity[a][i][2] = c
  698.             break
  699.         end
  700.     end
  701. end
  702.  
  703. function entity.getvar(a, b)
  704.     answer = 0
  705.     for i in ipairs(entity[a]) do
  706.         if entity[a][i][1] == b then
  707.             answer = entity[a][i][2]
  708.             break
  709.         end
  710.     end
  711.     return answer
  712. end
  713.  
  714.  
  715. function entity.rem(a) -- Removes Entity from the execution
  716.     --Don't know if this is possible or not. Might just zero out the entity. Reason? table.remove() would shift all the higher index entities down, changing their ID's
  717. end
  718.  
  719. function entity.getposx(a) return entity[a][2][2] end
  720. function entity.getposy(a) return entity[a][3][2] end
  721.  
  722. function entity.setpos(a, x, y, r) -- Sets the Entity's Position
  723.     if r ~= nil then
  724.         local resx = entity[a][2][2] + x
  725.         local resy = entity[a][3][2] + y
  726.         if get.locphys(resx,resy) == false then
  727.             entity[a][2][2] = resx
  728.             entity[a][3][2] = resy
  729.         end
  730.     else
  731.         if get.locphys(x,y) == false then
  732.             entity[a][2][2] = x
  733.             entity[a][3][2] = y
  734.         end
  735.     end
  736. end
  737.  
  738. --[[ GET FUNCTIONS ]]--
  739. get = {}
  740.  
  741. function get.locphys(x, y)
  742.     answer = false
  743.     for i,v in ipairs(entity) do
  744.         if entity[i][6][2] then
  745.             if entity[i][2][2] == x then
  746.                 if entity[i][3][2] == y then
  747.                     answer = i
  748.                     break
  749.                 end
  750.             end
  751.         end
  752.     end
  753.     return answer
  754. end
  755.  
  756. --[[ SCREEN FUNCTIONS ]]--
  757. function printCenter( y, s ) -- Prints centered text on the screen
  758.     local x = math.floor((w - string.len(s)) / 2)
  759.     term.setCursorPos(x, y)
  760.     term.clearLine()
  761.     term.write( s )
  762. end
  763.  
  764. --[[ RUNTIME ]]--
  765. --Variables
  766. exec = {}
  767. exec.active = false
  768. --Functions
  769. function exec.quit() -- Ends the current execution
  770.     term.clear()
  771.     exec.active = false
  772.     term.setCursorPos(window.midw-7,window.h)
  773.     term.write('Thanks for using the RinAPI')
  774.     sleep(2)
  775.     term.clear()
  776.     term.setCursorPos(1,1)
  777. end
  778.  
  779. function exec.draw() -- Draws the screen
  780.     term.clear()
  781.     for i,v in ipairs(entity) do
  782.         if entity[i][5][2] == true and entity[i][7][2] == false then
  783.             local x = entity[i][2][2]
  784.             local y = entity[i][3][2]
  785.             term.setCursorPos(x - screen.x, y - screen.y)
  786.             term.write(entity[i][4][2])
  787.         end
  788.     end
  789.     for i,v in ipairs(entity) do
  790.         if entity[i][5][2] == true and entity[i][7][2] == true then
  791.             local x = entity[i][2][2]
  792.             local y = entity[i][3][2]
  793.             term.setCursorPos(x, y)
  794.             term.write(entity[i][4][2])
  795.         end
  796.     end
  797. end
  798.  
  799. function exec.defupdate() -- Default Update, can be overridden for custom draw handling
  800.     term.clear()
  801.     exec.draw()
  802. end
  803.  
  804. function exec.start() -- Starts the current execution
  805.     exec.active = true
  806.     os.startTimer(1/30)
  807.     while exec.active do
  808.         local t,k = os.pullEvent()
  809.         if t == 'timer' then
  810.             if exec.beginstep ~= nil then exec.beginstep() end
  811.             if exec.step ~= nil then exec.step() end
  812.             if exec.endstep ~= nil then exec.endstep() end
  813.             if exec.update ~= nil then exec.update()
  814.             else exec.defupdate() end
  815.             os.startTimer(1/30)
  816.         elseif t == 'key' then
  817.             if k == 14 then
  818.                 exec.quit()
  819.             else
  820.                 if exec.key ~= nil then exec.key(k) end
  821.             end
  822.         end
  823.     end
  824. end
Advertisement
Add Comment
Please, Sign In to add comment