moved custom Lua methods from os table to new 'computer' table; some typos in config comments

This commit is contained in:
Florian Nücke 2013-12-06 15:44:16 +01:00
parent 039b3cce9b
commit c54f285345
18 changed files with 73 additions and 73 deletions

View File

@ -1,7 +1,6 @@
local deadline = 0
local realTime = os.realTime
local function checkDeadline()
if realTime() > deadline then
if computer.realTime() > deadline then
debug.sethook(coroutine.running(), checkDeadline, "", 1)
error("too long without yielding", 0)
end
@ -223,26 +222,28 @@ sandbox = {
rename = nil, -- in lib/os.lua
time = os.time,
tmpname = nil, -- in lib/os.lua
},
-------------------------------------------------------------------------------
-- Start of non-standard stuff.
isRobot = os.isRobot,
address = os.address,
romAddress = os.romAddress,
tmpAddress = os.tmpAddress,
freeMemory = os.freeMemory,
totalMemory = os.totalMemory,
uptime = os.uptime,
computer = {
isRobot = computer.isRobot,
address = computer.address,
romAddress = computer.romAddress,
tmpAddress = computer.tmpAddress,
freeMemory = computer.freeMemory,
totalMemory = computer.totalMemory,
uptime = computer.uptime,
users = os.users,
users = computer.users,
addUser = function(name)
checkArg(1, name, "string")
return os.addUser(name)
return computer.addUser(name)
end,
removeUser = function(name)
checkArg(1, name, "string")
return os.removeUser(name)
return computer.removeUser(name)
end,
shutdown = function(reboot)
@ -254,17 +255,17 @@ sandbox = {
for i = 1, args.n do
checkArg(i + 1, args[i], "nil", "boolean", "string", "number")
end
return os.pushSignal(name, ...)
return computer.pushSignal(name, ...)
end,
pullSignal = function(timeout)
local deadline = os.uptime() +
local deadline = computer.uptime() +
(type(timeout) == "number" and timeout or math.huge)
repeat
local signal = table.pack(coroutine.yield(deadline - os.uptime()))
local signal = table.pack(coroutine.yield(deadline - computer.uptime()))
if signal.n > 0 then
return table.unpack(signal, 1, signal.n)
end
until os.uptime() >= deadline
until computer.uptime() >= deadline
end
},
@ -330,7 +331,7 @@ local function main()
-- Minimalistic hard-coded pure async proxy for our ROM.
local rom = {}
function rom.invoke(method, ...)
return invoke(true, os.romAddress(), method, ...)
return invoke(true, computer.romAddress(), method, ...)
end
function rom.open(file) return rom.invoke("open", file) end
function rom.read(handle) return rom.invoke("read", handle, math.huge) end
@ -387,7 +388,7 @@ local function main()
end
local co, args = bootstrap(), {n=0}
while true do
deadline = os.realTime() + timeout -- timeout global is set by host
deadline = computer.realTime() + timeout -- timeout global is set by host
debug.sethook(co, checkDeadline, "", 10000)
local result = table.pack(coroutine.resume(co, table.unpack(args, 1, args.n)))
if not result[1] then

View File

@ -1 +1 @@
print(os.address())
print(computer.address())

View File

@ -1,2 +1,2 @@
print("Rebooting...")
os.shutdown(true)
computer.shutdown(true)

View File

@ -2,7 +2,7 @@ local args, options = shell.parse(...)
local history = {}
if options.v then
print("OpenOS v1.0 (" .. math.floor(os.totalMemory() / 1024) .. "k RAM)")
print("OpenOS v1.0 (" .. math.floor(computer.totalMemory() / 1024) .. "k RAM)")
end
while true do
@ -12,7 +12,7 @@ while true do
end
term.clear()
if options.v then
print("OpenOS v1.0 (" .. math.floor(os.totalMemory() / 1024) .. "k RAM)")
print("OpenOS v1.0 (" .. math.floor(computer.totalMemory() / 1024) .. "k RAM)")
end
end
while term.isAvailable() do

View File

@ -1,2 +1,2 @@
term.clear()
os.shutdown()
computer.shutdown()

View File

@ -1,4 +1,4 @@
local seconds = math.floor(os.uptime())
local seconds = math.floor(computer.uptime())
local minutes, hours = 0, 0
if seconds >= 60 then
minutes = math.floor(seconds / 60)

View File

@ -4,7 +4,7 @@ if #args < 1 then
return
end
local result, reason = os.addUser(args[1])
local result, reason = computer.addUser(args[1])
if not result then
print(reason)
end

View File

@ -4,6 +4,6 @@ if #args < 1 then
return
end
if not os.removeUser(args[1]) then
if not computer.removeUser(args[1]) then
print("no such user")
end

View File

@ -1,8 +1,8 @@
fs.mount(os.romAddress(), "/")
if os.tmpAddress() then fs.mount(os.tmpAddress(), "/tmp") end
fs.mount(computer.romAddress(), "/")
if computer.tmpAddress() then fs.mount(computer.tmpAddress(), "/tmp") end
for c, t in component.list() do
os.pushSignal("component_added", c, t)
computer.pushSignal("component_added", c, t)
end
os.sleep(0.5) -- Allow signal processing by libraries.

View File

@ -55,10 +55,10 @@ function component.setPrimary(componentType, address)
end
primaries[componentType] = address and component.proxy(address) or nil
if wasAvailable then
os.pushSignal("component_unavailable", componentType)
computer.pushSignal("component_unavailable", componentType)
end
if component.isAvailable(componentType) then
os.pushSignal("component_available", componentType)
computer.pushSignal("component_available", componentType)
end
end

View File

@ -40,13 +40,13 @@ local function tick()
local function elapsed()
local list = {}
for id, timer in pairs(timers) do
if timer.after <= os.uptime() then
if timer.after <= computer.uptime() then
table.insert(list, timer.callback)
timer.times = timer.times - 1
if timer.times <= 0 then
timers[id] = nil
else
timer.after = os.uptime() + timer.interval
timer.after = computer.uptime() + timer.interval
end
end
end
@ -131,7 +131,7 @@ function event.pull(...)
end
local deadline = seconds and
(os.uptime() + seconds) or
(computer.uptime() + seconds) or
(hasFilter and math.huge or 0)
repeat
@ -139,23 +139,23 @@ function event.pull(...)
for _, timer in pairs(timers) do
closest = math.min(closest, timer.after)
end
local signal = table.pack(os.pullSignal(closest - os.uptime()))
local signal = table.pack(computer.pullSignal(closest - computer.uptime()))
if signal.n > 0 then
dispatch(table.unpack(signal, 1, signal.n))
end
tick()
if event.shouldInterrupt() then
lastInterrupt = os.uptime()
lastInterrupt = computer.uptime()
error("interrupted", 0)
end
if not (seconds or hasFilter) or matches(signal, name, filter) then
return table.unpack(signal, 1, signal.n)
end
until os.uptime() >= deadline
until computer.uptime() >= deadline
end
function event.shouldInterrupt()
return os.uptime() - lastInterrupt > 1 and
return computer.uptime() - lastInterrupt > 1 and
keyboard.isControlDown() and
keyboard.isAltDown() and
keyboard.isKeyDown(keyboard.keys.c)
@ -171,7 +171,7 @@ function event.timer(interval, callback, times)
until not timers[id]
timers[id] = {
interval = interval,
after = os.uptime() + interval,
after = computer.uptime() + interval,
callback = callback,
times = times or 1
}

View File

@ -5,7 +5,7 @@ function file.new(mode, stream, nogc)
mode = mode or "r",
stream = stream,
buffer = "",
bufferSize = math.max(128, math.min(8 * 1024, os.freeMemory() / 8)),
bufferSize = math.max(128, math.min(8 * 1024, computer.freeMemory() / 8)),
bufferMode = "full"
}
local metatable = {

View File

@ -31,10 +31,10 @@ os.rename = fs.rename
function os.sleep(timeout)
checkArg(1, timeout, "number", "nil")
local deadline = os.uptime() + (timeout or 0)
local deadline = computer.uptime() + (timeout or 0)
repeat
event.pull(deadline - os.uptime())
until os.uptime() >= deadline
event.pull(deadline - computer.uptime())
until computer.uptime() >= deadline
end
function os.tmpname()

View File

@ -1,4 +1,4 @@
if not os.isRobot() then
if not computer.isRobot() then
return
end

View File

@ -33,7 +33,7 @@ function term.clearLine()
cursorX = 1
end
function term.getCursor(col, row)
function term.getCursor()
return cursorX, cursorY
end
@ -43,8 +43,8 @@ function term.setCursor(col, row)
if cursorBlink and cursorBlink.state then
toggleBlink()
end
cursorX = col
cursorY = row
cursorX = math.floor(col)
cursorY = math.floor(row)
end
function term.getCursorBlink()
@ -371,7 +371,7 @@ local function onComponentAvailable(_, componentType)
screenAvailable = true
end
if not wasAvailable and term.isAvailable() then
os.pushSignal("term_available")
computer.pushSignal("term_available")
end
end
@ -383,7 +383,7 @@ local function onComponentUnavailable(_, componentType)
screenAvailable = false
end
if wasAvailable and not term.isAvailable() then
os.pushSignal("term_unavailable")
computer.pushSignal("term_unavailable")
end
end

View File

@ -761,6 +761,20 @@ class Computer(val owner: tileentity.Computer) extends ManagedComponent with Con
})
lua.setField(-2, "time")
// Pop the os table.
lua.pop(1)
// Computer API, stuff that kinda belongs to os, but we don't want to
// clutter it.
lua.newTable()
// Allow getting the real world time via os.realTime() for timeouts.
lua.pushScalaFunction(lua => {
lua.pushNumber(System.currentTimeMillis() / 1000.0)
1
})
lua.setField(-2, "realTime")
// The time the computer has been running, as opposed to the CPU time.
lua.pushScalaFunction(lua => {
// World time is in ticks, and each second has 20 ticks. Since we
@ -876,8 +890,8 @@ class Computer(val owner: tileentity.Computer) extends ManagedComponent with Con
})
lua.setField(-2, "removeUser")
// Pop the os table.
lua.pop(1)
// Set the computer table.
lua.setGlobal("computer")
// Until we get to ingame screens we log to Java's stdout.
lua.pushScalaFunction(lua => {

View File

@ -142,20 +142,6 @@ object LuaStateFactory {
state.pushNil()
state.setGlobal("loadfile")
// Push a couple of functions that override original Lua API functions or
// that add new functionality to it.
state.getGlobal("os")
// Allow getting the real world time via os.realTime() for timeouts.
state.pushScalaFunction(lua => {
lua.pushNumber(System.currentTimeMillis() / 1000.0)
1
})
state.setField(-2, "realTime")
// Pop the os table.
state.pop(1)
state.getGlobal("math")
// We give each Lua state it's own randomizer, since otherwise they'd

View File

@ -154,7 +154,7 @@ opencomputers {
# an utter pain in the part you sit on, because it makes robots meant to
# dig holes utterly useless: the poor things couldn't break cobwebs in
# mining shafts with their golden pick axes. So, if this setting is true,
# we check for cobweb and allow robots to break 'em anyway, no matter
# we check for cobwebs and allow robots to break 'em anyway, no matter
# their current tool. After all, the hardness value of cobweb can only
# rationally explained by Steve's fear of spiders, anyway.
notAfraidOfSpiders: true
@ -163,8 +163,7 @@ opencomputers {
# is the distance to the center of block the robot swings the tool in to
# the side the tool is swung towards. I.e. for the collision check, which
# is performed via ray tracing, this determines the end point of the ray
# like so:
# `block_center + unit_vector_towards_side * swingRange`
# like so: `block_center + unit_vector_towards_side * swingRange`
# This defaults to a value just below 0.5 to ensure the robots will not
# hit anything that's actually outside said block.
swingRange: 0.49
@ -190,7 +189,7 @@ opencomputers {
# replaced with the name of the player that owns the robot, so for the
# first robot placed this will be the name of the player that placed it.
# This is transitive, i.e. when a robot in turn places a robot, that
# robot's owner, too will be the owner of the placing robot.
# robot's owner, too, will be the owner of the placing robot.
# The substring $random$ will be replaced with a random number in the
# interval [1, 0xFFFFFF], which may be useful if you need to differentiate
# individual robots.
@ -209,13 +208,13 @@ opencomputers {
# This controls how much experience a robot gains for each successful
# action it performs. "Actions" only include the following: swinging a
# tool and killing something or destroying a block and placing a block
# tool and killing something or destroying a block and placing a block
# successfully. Note that a call to `swing` or `use` while "bare handed"
# will *not* gain a robot any experience.
actionXp: 0.05
# This determines how much "exhaustion" contributes to a robots
# experience. This is addiditve to the "action" xp, so digging a block
# experience. This is additive to the "action" xp, so digging a block
# will per default give 0.05 + 0.025 [exhaustion] * 1.0 = 0.075 XP.
exhaustionXpRate: 1.0
@ -264,8 +263,8 @@ opencomputers {
# The time in seconds to pause execution after a robot successfully
# used an equipped tool (or it's 'hands' if nothing is equipped).
# Successful in this case means that it either used the equipped item,
# for example bone meal, or that it activated a block, for example by
# pushing a button.
# for example a splash potion, or that it activated a block, for
# example by pushing a button.
# Note that if an item is used for a specific amount of time, like
# when shooting a bow, the maximum of this and the duration of the
# item use is taken.