Tutorialspoint

Lua Programming Examples

Execute Lua Online

lua

print("welcome to aryans calculator")
print("input your numbers in the script")
print("---------------------------------")

-- Add values below
number1 = 10 -- number you want numbers to get added to
number2 = 10 -- how much numbers you want to add to

print("Addition awenser is:", number1+number2)

print("------------------------")

-- subtract values below
number3 = 10 --number you want to subtract numbers from
number4 = 5 -- amount of numbers you want to subtract from the above number

print("Subtraction awenser is:", number3-number4)

Day6

lua

-- Get all lines from a file, return nil if the file does not exist or retutn the sum of all the lines if it exist
function findclosest(coord,x,y)
    for i=1, ncoord do
        for j=1, ncoord do
            if 
        end
    end
end
 
function lines_from(file)
    coord = {}
    i = 1
    for line in io.lines(file) do 
        coord[i] = {}
        x,y = line:match("(%d+),%s(%d+)")
        coord[x][y] = i 
        i = i + 1
    end
    ncoord = i - 1
 
    for i=1, ncoord do
        for j=1, ncoord do
             if not coord[x][y] then
                 findclosest(coord,x,y)
             end
        end   
    end
 
end

lines_from(stdin)

Execute Lua Online

lua

--local "modem_message", local_address, remote_address, port, distance, "nanomachines", command, ...messages=event.pull(5,"modem_message")
local computer=require("computer")
local component=require("component")
local term=require("term")
local event=require("event")
local m=component.modem
local gpu=component.gpu
local input_state,file_data={}
local tic,sai,start,user_input=1,1,1,1
local username,nick=""
os.execute("cls")

function main()
    aquire_user_data()
    initialize()
    print_gui()
    while start==1 do
        print_menu()
        user_input=request_input("n",0,tic)
        if user_input==0 then
            print_gui()
            print_options()
            user_input=request_input("n",0,9)
            check_command(user_input)
        else 
            toggle_input(user_input)
            computer.pullSignal(1)
        end
    end
end
--initialize
function aquire_user_data()
    while true do
        os.execute("cls")
        io.write("username: ")
        username=request_input("s",1,30)
        nick=username
        username="/usr/nanomech_data/"..username..".txt"
        local f=io.open(username,"r")
        if f==nil then
            print("No user found. Create a new user?(y/n) : ")
            local check=request_input("s",1,1)
            if check=="y" then
                local data=""
                for i=1,25 do
                    data=data.."."..i.."XXXXX"
                end
                f=io.open(username,"w")
                f:write(data)
                f:close()
                file_data=txt2table(data)
                return
            end
        else
            local data=f:read()
            io.close(f)
            file_data=txt2table(data)
            return
        end
    end
end
function initialize()
    os.execute("cls")
    gpu.set(10,10,"Welcome "..nick.."!")
    m.open(1)
    m.broadcast(1,"nanomachines","setResponsePort",1)
    local _=event.pull(5,"modem_message")
    gpu.set(10,12,"Loading...")
    tic,sai=check_input_info()
    init_inputs()
end
function init_inputs()
    gpu.set(10,13,"|")
    gpu.set((3*tic)+10,13,"|")
    term.setCursor(11,13)
    for i=1,tic do
        send_command("getInput",i)
        local _, _, _, _, _, header, _, _, state = event.pull(5,"modem_message")
        io.write("|||")
        input_state[i]=state
    end
end
function check_input_info()
    send_command("getTotalInputCount")
    local _, _, _, _, _, _, command, total_input_count=event.pull(5,"modem_message")
    send_command("getSafeActiveInputs")
    local _, _, _, _, _, _, command, safe_active_inputs=event.pull(5,"modem_message")
    return total_input_count,safe_active_inputs
end
--gui
function print_gui()
    os.execute("cls")
    gpu.set(57,1,"Available inputs  : "..tic)
    gpu.set(57,2,"Safe active inputs: "..sai)
    for i=1,25 do gpu.set(56,i,"|") end
    for i=1,tic do
        if i<10 then gpu.set(61,i+2,i..". ")
        else gpu.set(60,i+2,i..". ") end
        if input_state[i]==true then gpu.setForeground(0x00FF00)
        elseif input_state[i]==false then gpu.setForeground(0xFF0000) end
        gpu.set(64,i+2,file_data[i])
        gpu.setForeground(0xFFFFFF)
    end
end
function print_menu()
    clear_screen()
    term.setCursor(1,1)
    print("Enter 0 for other options")
    print("Enter input number to toggle")
    io.write("Command: ")
end
function print_options()
    clear_screen()
    term.setCursor(1,1)
    print("1. Check remaining power")
    print("2. Rename input")
    print("3. Reset all input names")
    print("4. Save user data")
    print("\n9. Back")
    print("0. Close app")
    io.write("Command: ")
end
--operations
function toggle_input(input_no)
    if input_state[input_no]==false then
        input_state[input_no]=true
        send_command("setInput",input_no,true)
        gpu.setForeground(0x00FF00)
    elseif input_state[input_no]==true then
        input_state[input_no]=false
        send_command("setInput",input_no,false)
        gpu.setForeground(0xFF0000)
    end
    gpu.set(64,input_no+2,file_data[input_no])
    gpu.setForeground(0xFFFFFF)
end
function check_command(comnum)
    if comnum==1 then
        print("Sending signal...")
        send_command("getPowerState")
        local _, _, _, _, _, header, _, current_power, max_power = event.pull(5,"modem_message")
        print((current_power*100/max_power).."%")
        print("Press enter to continue. . .\n")
        io.read("*l")
        return
    elseif comnum==2 then
        io.write("Enter the input number: ")
        local n=request_input("n",1,tic)
        io.write("Enter new name (1-15 letters): ")
        local newname=request_input("s",1,15)
        file_data[n]=newname
        print_gui()
        return
    elseif comnum==3 then
        file_data=reset_input_names()
        print_gui()
        return
    elseif comnum==4 then
        save_data(file_data)
        return
    elseif comnum==9 then
        return
    elseif comnum==0 then
        save_data(file_data)
        start=0
        os.execute("cls")
        return
    else
        return
    end
    
end
--mini functions
function request_input(t,mn,mx)
    while true do
        local usr_input=io.read("*l")
        if t=="n" then
            usr_input=tonumber(usr_input)
            if input_check(t,usr_input,mn,mx) then
                return usr_input
            else
                error_input_msg()
            end
        elseif t=="s" then
            usr_input=tostring(usr_input)
            if input_check(t,usr_input,mn,mx) then
                return usr_input
            else
                error_input_msg()
            end
        end
    end
end
function error_input_msg()
    print("Fuk You! Enter a proper input!!!")
    computer.pullSignal(1) print(".  ") computer.pullSignal(1)  print(".  ") computer.pullSignal(1)
    io.write("Try again: ")
end
function input_check(t,n,mn,mx)
    if t=="n" then
        if n~=nil then
            return (type(n)=="number") and (math.floor(n)==n) and n<=mx and n>=mn 
        else
            return false
        end
    elseif t=="s" then
        if n~=nil then
            return (type(n)=="string") and (string.len(n)<=mx) and (string.len(n)>=mn)
        else
            return false
        end
    end
end
function send_command(command,...)
    m.broadcast(1,"nanomachines",command,...)
end
function clear_screen()
    gpu.fill(1,1,55,25," ")
end
function txt2table(x)
    local t={}
    for section in string.gmatch(x,"%.%d*%a+") do
        local i=string.match(section,"%d+")
        i=tonumber(i)
        local name=string.match(section,"%a+")
        t[i]=tostring(name)
    end
    return t
end
function table2txt(x)
    local t=""
    for i,name in ipairs(x) do
        name=tostring(name)
        t=t.."."..i..name
    end
    return t
end
function reset_input_names()
    local x={}
    for i=1,25 do
        x[i]="XXXXX"
    end
    return x
end
function save_data(x)
    local data=""
    data=table2txt(x)
    local f=io.open(username,"w")
    f:flush()
    f:write(data)
    f:close()
end
main()

Execute Lua Online

lua

--LUA Script - precede every function and global member with lowercase name of script + '_main'
-- player converse


function converse_init(e)
texttinme=0
stage=0
fin=0
end
function converse_main(e)
playerDist=GetPlayerDistance(e)
if playerDist <100 then
if g-KeypressE==1 and GetTimer(e)>texttime nd fin==0 then
texttime=GetTimer(e)+1000
stage=stage+1
end
if stage==1 then
TextCenteronx(50,50,3,"Hello.")
end
if stage==2 then
TextCenteronx(50,50,3," like urtles.")
end
if stage==3 then
TextCenteronx(50,50,3," Bibble.")
end
if stage==4 then
fin"1
end
end
if playerDist>100 then
fin=0
stage=0
end
end

Execute Lua Online

lua

print("Hello World!")

vipin

lua

local tom_dist_coefficient = {
--ASECNA International coefficient derived from weight/distance multiplied by unit_rate
  {
    14001, {             -- MTOM upper limit (exclusive)
      {750000, 0},    -- distance upper limit (exclusive), coefficient
      {2000000, 0},     -- ...
	  {3500000, 0},     -- ...
      {math.huge, 0}  -- ...
    }
  }, {
    20001, {         
      {750000, 1}, 
      {2000000, 5},  
      {3500000, 12}, 
      {math.huge, 20}
	  }
  }, {
    50001, {         
      {750000, 1.2}, 
      {2000000, 6},  
      {3500000, 14.4}, 
      {math.huge, 24}}
  }, {
    90001, {         
      {750000, 1.4}, 
      {2000000, 7},  
      {3500000, 16.8}, 
      {math.huge, 28}}
  }, {
    140001, {         
      {750000, 1.6}, 
      {2000000, 8},  
      {3500000, 19.2}, 
      {math.huge, 32}}
  }, {
    200001, {         
      {750000, 1.8}, 
      {2000000, 9},  
      {3500000, 21.6}, 
      {math.huge, 36}}
  }, {
    270001, {         
      {750000, 2}, 
      {2000000, 10},  
      {3500000, 24}, 
      {math.huge, 40}}
  }, {
    350001, {         
      {750000, 2.15}, 
      {2000000, 10.75},  
      {3500000, 25.8}, 
      {math.huge, 43}}
  }, {
    440001, {         
      {750000, 2.3}, 
      {2000000, 11.5},  
      {3500000, 27.6}, 
      {math.huge, 46}}
  }, {
    540001, {         
      {750000, 2.45}, 
      {2000000, 12.25},  
      {3500000, 29.4}, 
      {math.huge, 49}}
  },
  {
    math.huge, { -- greater 540Tonnes
      {750000, 2.6}, 
      {2000000, 13},  
      {3500000, 31.2}, 
     {math.huge, 52}}
  }
}

-- find the first entry in an array of pairs, where pair[1] > value
function upper_bound(value, table)
  for index, item in ipairs(table) do
    if item[1] > value then
      return item[2]
    end
  end
end


-- find the matching coefficient for a given mass and distance
function coefficient(tom, dist)
  return upper_bound(dist, upper_bound(tom, tom_dist_coefficient))
end

local lib = {};
lib.distance_method = Distance_method.great_circle;

function lib:calculate(flight_context, route_context, distance)
  return self:estimate(flight_context, distance);
end
function lib:estimate(flight_context, distance)
    if flight_context:origin_icao_code == NZAA then
        return 10000
    else
        return self.unit_rate * coefficient(flight_context.mtom, distance);
    end
end
return lib;

Aoetracker

lua

!1QXIYjoYXVLZvD1j21GLepSXjovbyWpcl4GK3nBYfmdObq3kKiJe(vLIV90Dp6js4fV3DlEfIz6PF390tlnwBS5yd7XdQ1qttxBSH)yd9knRa3TASHf(hmFa)5GEoSfWKd8C5aubSzFZ3W(v(4bA6JnEE485(8GX9k3uvT(z61oRrDD1g6NQpEE566rlidoEjEnNEAn161uBQRvhU9StH1uR(ydM7SLEI78SDdgB0P7aZUJaeTMj(2iVawGTN74bQJn2iCgBSmiyT)5NCYtSfEvS9oPNG7(6lMcGQCXjAG4ypdGF6yJ5EUrYYW7n7FZGUY5EyMNJNaK2bA0F601Q01A4vGj85oZ3HFMYeDsTqvCH9QvxxtVH6zQ6vpT5PnjLqtez9oTw9QQA1pRE1M66nRPIZunHawCFwWgblGJS6AH3cb33)obFMTpjV1c1asD3xjfGJhJSs2UleEBwJ3UAJtGnCtaI0aMdhy5DhEMd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YgIiPQoPHqe1kQFCBNyYMMGZ124dQOIKKrv8go5TPtafws4TztcLBumruUbPKr0OGHoIuYdbwaLQwmPQwiTQwmXQgsn9iQ1MjMIcbuJwGS4PinGcugJNGsbXCPKyXj1I3rF7Ksz5rTI5rTc5rTI5rTqES6bXJfYx)0pLJZ0lMZ0lKZ0lMZIhEF8f5wkJftJrM4A5y4YRkteIi4YHd(fZTt6E5nW12D7p8lubTWSrLfM9uwYTfJNDknROISC3BJRZreFm1D3)qotpbBL0zSJGdzbOFRCuVrT(uxS2qxBhS9RLkCD0UXo2R97S02XcoHGsGydphSsQhtIWTUXrvsHm8KfTgz(LHJ(7hD8PhdeUu69wcLZWkzjb98m4trYQhn8ZDh1V1xpkdBiREqYexMdPP6C7pkY3bfP7fC4EIIIByWFbNB3ZolpJFuxYlLJHJp0DkKwWXUJoJ3Lc2t9XUMPKOHbNpI2)5DE0cyezr66fryzub4qidzsds8dAbL6Ksme)PwnBAShYB5dI3bUWZs3EOP5WpfDkzca03XC4D0yLsy(S9XDVcr1Ccr1DfIQ5eIQLkMk7QOlgQK(slEZooNB28DwPieeQPeihSVMomFrPK4gz(MIqEMSrjZHPSkxENPdjBKLaZSjjZ(GSTxau7XOi2CxrPu6IwANP(uQ2t6DxW3gR0B0nxDD0Zr(lHfjbL9IpSxMhpQ7UBwhcI0kbvEPgv7u7WQetEM1qzkqGFG96mprxIzhB8ZRJEE3hqfDubRPQ7Li4bvoQKzTTS4UqjGD)m(okiRQh5fyOH47WXCuTAeGpUB5ZpK66jPSLp1qJoJ62Dq8RIYEFm5zFu)SNXIqTDFKl8PxkdIBUu(EAqd4jSblLS8EJRho6M)1WbMT6Jpnsxl7OsqbI9QhybhOMuonvETgPDyyvOzF81rVsoWSpcGvJUD8))p

Fuel Precision

lua

local KG2LBS = 2.20462262185
local ROUND = 100
local USE_POUNDS = true
local PRECISION = 1 / (USE_POUNDS == true and KG2LBS or 1) * ROUND

function ceil_to_prescision(value)
    return math.ceil(value / PRECISION) * PRECISION
end

local FUEL = {}

function FUEL:new(value)
    local self = {}
    self.value = ceil_to_prescision(value)

    function self:print()
        print(self.value, USE_POUNDS and (self.value * KG2LBS) or "")
    end

    return self
end

print(PRECISION)

local F1 = FUEL:new(776.3344)
    
F1:print()

local F2 = FUEL:new(F1.value)

F2:print()

local F3 = FUEL:new(F2.value)

F3:print()

liferting

lua

local RingID = 3052

while true do
if Self.Ring().id == 0 then
Self.Equip(RingID, "ring")
end
wait(100,600)
end

lua prototype ideas

lua

-- isa

Object = {
    clone = function (self, ...)
        local obj = {}
        obj.__index = obj.__index or obj
        
        setmetatable(obj, self.mt or self)
        if obj.init then
            obj:init(...)
        end
        
        return obj
    end,
    isA = function (self, obj)
        return getmetatable(self) == (obj.mt or obj)
            or getmetatable(self):isA(obj)
    end
}
Object.__index = Object

Previous 1 ... 3 4 5 6 7 8 9 ... 27 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.