mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 18:44:05 +09:00
more work on the Terminal, ComputerCraft compatibility layer (wip), quarried stone texture
Former-commit-id: 1fd1b5ce05663dd41d6077077b64e08ac0f1b5a0 Former-commit-id: bd52729417fc4dfcd8ad11f00d34507943156a83
This commit is contained in:
168
src/net/torvald/terrarum/virtualcomputer/assets/lua/CCAPI.lua
Normal file
168
src/net/torvald/terrarum/virtualcomputer/assets/lua/CCAPI.lua
Normal file
@@ -0,0 +1,168 @@
|
||||
--[[
|
||||
-- ComputerCraft API compatibility layer
|
||||
Usage: require("CCAPI")
|
||||
|
||||
Created by minjaesong on 16-09-16.
|
||||
--]]
|
||||
|
||||
--------------
|
||||
-- PREAMBLE --
|
||||
--------------
|
||||
|
||||
if term.isTeletype() then error("This is a teletype; cannot use CCAPI layer") end
|
||||
|
||||
|
||||
table.insert(_COMPUTER.loadedCLayer, "CCAPI")
|
||||
|
||||
|
||||
local function intLog2(i)
|
||||
if i == 0 then return 0 end
|
||||
local log = 0
|
||||
if bit32.band(i, 0xffff0000) ~= 0 then i = bit32.rshift(i, 16) log = 16 end
|
||||
if i >= 256 then i = bit32.rshift(i, 8) log = log + 8 end
|
||||
if i >= 16 then i = bit32.rshift(i, 8) log = log + 4 end
|
||||
if i >= 4 then i = bit32.rshift(i, 8) log = log + 2 end
|
||||
return log + bit32.rshift(i, 1)
|
||||
end
|
||||
|
||||
|
||||
-------------
|
||||
-- BIT API --
|
||||
-------------
|
||||
|
||||
_G.bit = {} -- CC's weird BIT API
|
||||
|
||||
bit.blshift = function(n, bits) bit32.lshift(n, bits) end
|
||||
bit.brshift = function(n, bits) bit32.arshift(n, bits) end
|
||||
bit.blogic_rshift = function(n, bits) bit32.brshift(n, bits) end
|
||||
bit.bxor = function(m, n) bit32.bxor(m, n) end
|
||||
bit.bor = function(m, n) bit32.bor(m, n) end
|
||||
bit.band = function(m, n) bit32.band(m, n) end
|
||||
bit.bnot = function(n) bit32.bnot(n) end
|
||||
|
||||
|
||||
----------------
|
||||
-- COLORS API --
|
||||
----------------
|
||||
|
||||
_G.colors = {}
|
||||
|
||||
colors.white = 0x1
|
||||
colors.orange = 0x2
|
||||
colors.magenta = 0x4
|
||||
colors.lightBlue = 0x8
|
||||
colors.yellow = 0x10
|
||||
colors.lime = 0x20
|
||||
colors.pink = 0x40
|
||||
colors.gray = 0x80
|
||||
colors.lightGray = 0x100
|
||||
colors.cyan = 0x200
|
||||
colors.purple = 0x400
|
||||
colors.blue = 0x800
|
||||
colors.brown = 0x1000
|
||||
colors.green = 0x2000
|
||||
colors.red = 0x4000
|
||||
colors.black = 0x8000
|
||||
|
||||
colors.combine = function(...)
|
||||
local ret = 0
|
||||
for _, c in ipairs(...) do
|
||||
ret = bor(ret, c)
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
local function containsCol(target, cccol)
|
||||
return bit32.band(target, cccol) > 0
|
||||
end
|
||||
|
||||
colors.subtract = function(cccol, ...)
|
||||
for _, c in ipairs(...) do
|
||||
if not containsCol(cccol, c) then
|
||||
cccol = bit32.bxor(cccol, c)
|
||||
end
|
||||
end
|
||||
return cccol
|
||||
end
|
||||
|
||||
|
||||
local function normaliseCCcol(cccol)
|
||||
if cccol >= 0x1 and cccol <= 0x8FFF then
|
||||
return intLog2(cccol)
|
||||
else
|
||||
error("invalid CC Colors: "..cccol)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--------------
|
||||
-- TERM API --
|
||||
--------------
|
||||
|
||||
-- paint_index -> Terminal colour index
|
||||
local ccToGameCol = {--pink
|
||||
1, 5, 7, 10, 4, 11, 15, 2, 3, 10, 8, 9, 14, 12, 6, 0
|
||||
}
|
||||
|
||||
-- "a" -> 10, "3" -> 3
|
||||
local function cHexToInt(c)
|
||||
if type(c) == "number" then -- char
|
||||
if c >= 48 and c <= 57 then
|
||||
return c - 48
|
||||
elseif c >= 65 and c <= 70 then
|
||||
return c - 65
|
||||
elseif c >= 97 and c <= 102 then
|
||||
return c - 97
|
||||
else
|
||||
return 0
|
||||
end
|
||||
elseif type(c) == "string" then -- single-letter string
|
||||
if c:byte(1) >= 48 and c:byte(1) <= 57 then
|
||||
return c:byte(1) - 48
|
||||
elseif c:byte(1) >= 65 and c:byte(1) <= 70 then
|
||||
return c:byte(1) - 65
|
||||
elseif c:byte(1) >= 97 and c:byte(1) <= 102 then
|
||||
return c:byte(1) - 97
|
||||
else
|
||||
--error("unrepresentable: " .. c)
|
||||
-- return black, as defined in http://www.computercraft.info/wiki/Term.blit
|
||||
return 0
|
||||
end
|
||||
else
|
||||
error("bad argument (string or number expected, got "..type(c)..")")
|
||||
end
|
||||
end
|
||||
|
||||
-- str, str, str
|
||||
term.blit = function(text, foreCol, backCol)
|
||||
assert(
|
||||
type(text) == "string" and type(backCol) == "string" and type(foreCol) == "string",
|
||||
"bad argument: (string, string, string expected, got "..type(text)..", "..type(foreCol)..", "..type(backCol)..")"
|
||||
)
|
||||
if #text ~= #foreCol or #text ~= #backCol or #foreCol ~= #backCol then
|
||||
error("rrguments must be the same length")
|
||||
end
|
||||
|
||||
for i = 1, #text do
|
||||
term.setForeCol(cHexToInt(foreCol:byte(i)))
|
||||
term.setBackCol(cHexToInt(backCol:byte(i)))
|
||||
term.emit(text:byte(i))
|
||||
term.moveCursor(term.getX() + 1, term.getY())
|
||||
end
|
||||
end
|
||||
|
||||
term.getCursorPos = function() return term.getCursor() end
|
||||
term.setCursorPos = function(x, y) term.moveCursor(x, y) end
|
||||
term.setCursorBlink = function(b) term.blink(b) end
|
||||
term.isColor = function() return term.isCol() end
|
||||
term.getSize = function() return term.size() end
|
||||
term.setTextColor = function(cccol) term.setForeCol(ccToGameCol[normaliseCCcol(cccol)]) end
|
||||
term.getTextColor = function() return term.getForeCol() end
|
||||
term.setBackgroundColor = function(cccol) term.setBackCol(ccToGameCol[normaliseCCcol(cccol)]) end
|
||||
term.getBackgroundColor = function() return term.getBackCol() end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if _COMPUTER.verbose then print("ComputerCraft compatibility layer successfully loaded.") end
|
||||
@@ -1,13 +1,33 @@
|
||||
--[[
|
||||
Must be loaded VERY FIRST!
|
||||
|
||||
Created by minjaesong on 16-09-13.
|
||||
--]]
|
||||
|
||||
-- path for any ingame libraries
|
||||
package.path = "/net/torvald/terrarum/virtualcomputer/assets/lua/?.lua;" .. package.path
|
||||
|
||||
-- global variables
|
||||
_G.MONEYSYM = string.char(0x9D) -- currency sign
|
||||
_G.MIDDOT = string.char(0xFA) -- middle dot sign
|
||||
_COMPUTER = {} -- standard console colours
|
||||
_COMPUTER["DC1"] = string.char(17) -- black
|
||||
_COMPUTER["DC2"] = string.char(18) -- white
|
||||
_COMPUTER["DC3"] = string.char(19) -- dim grey
|
||||
_COMPUTER["DC4"] = string.char(20) -- light grey
|
||||
_COMPUTER["prompt"] = function()
|
||||
_COMPUTER.DC1 = string.char(17) -- black
|
||||
_COMPUTER.DC2 = string.char(18) -- white
|
||||
_COMPUTER.DC3 = string.char(19) -- dim grey
|
||||
_COMPUTER.DC4 = string.char(20) -- light grey
|
||||
_COMPUTER.prompt = function()
|
||||
io.write(_COMPUTER.DC3 .. "> " .. _COMPUTER.DC4)
|
||||
end
|
||||
-- greet user
|
||||
_COMPUTER.verbose = true -- print debug info
|
||||
_COMPUTER.loadedCLayer = {} -- list of loaded compatibility layers
|
||||
|
||||
-- load libraries that coded in Lua
|
||||
require("ROMLIB")
|
||||
|
||||
|
||||
-- load bios, if any
|
||||
|
||||
-- load Lua prompt, if bios is not found
|
||||
print("Rom basic " .. _COMPUTER.DC2 .. _VERSION .. _COMPUTER.DC4)
|
||||
-- print(_COMPUTER.DC2 .. freemem .. _COMPUTER.DC4 .. " bytes free"
|
||||
print("Ok")
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
--[[
|
||||
Created by minjaesong on 16-09-15.
|
||||
--]]
|
||||
|
||||
|
||||
--------------
|
||||
-- HEXUTILS --
|
||||
--------------
|
||||
|
||||
_G.hexutils = {}
|
||||
|
||||
_G.hexutils.toHexString = function(byteString)
|
||||
assert(type(byteString) == "string", error("Expected string."))
|
||||
|
||||
-- speedup
|
||||
local function iToHex(i)
|
||||
if i == 0 then return "0"
|
||||
elseif i == 1 then return "1"
|
||||
elseif i == 2 then return "2"
|
||||
elseif i == 3 then return "3"
|
||||
elseif i == 4 then return "4"
|
||||
elseif i == 5 then return "5"
|
||||
elseif i == 6 then return "6"
|
||||
elseif i == 7 then return "7"
|
||||
elseif i == 8 then return "8"
|
||||
elseif i == 9 then return "9"
|
||||
elseif i == 10 then return "a"
|
||||
elseif i == 11 then return "b"
|
||||
elseif i == 12 then return "c"
|
||||
elseif i == 13 then return "d"
|
||||
elseif i == 14 then return "e"
|
||||
elseif i == 15 then return "f"
|
||||
else error("unrepresentable: " .. i)
|
||||
end
|
||||
end
|
||||
|
||||
local ret = ""
|
||||
|
||||
for i = 1, #byteString do
|
||||
local c = byteString:byte(i)
|
||||
local msb = iToHex(bit32.rshift(c, 4) % 16)
|
||||
local lsb = iToHex(c % 16)
|
||||
|
||||
ret = ret .. (msb .. lsb)
|
||||
end
|
||||
|
||||
return ret
|
||||
end
|
||||
Reference in New Issue
Block a user