Initial Commit

This commit is contained in:
Kirigaya Kazuto 2017-10-09 22:53:37 +08:00
commit 76ec7e212d
4 changed files with 230 additions and 0 deletions

53
SignReader.lua Normal file
View File

@ -0,0 +1,53 @@
require("class")
local sides=require("sides")
SignReader=class("SignReader")
function SignReader:ctor(sign)
self.s=sign
end
function SignReader:countSigns()
local cnt=0
if(self:get(sides.north) ~= nil) then cnt=cnt+1 end
if(self:get(sides.south) ~= nil) then cnt=cnt+1 end
if(self:get(sides.east) ~= nil) then cnt=cnt+1 end
if(self:get(sides.west) ~= nil) then cnt=cnt+1 end
if(self:get(sides.up) ~= nil) then cnt=cnt+1 end
if(self:get(sides.down) ~= nil) then cnt=cnt+1 end
return cnt
end
function SignReader:getFirstSignSide()
if(self:get(sides.north)~=nil) then return sides.north end
if(self:get(sides.south)~=nil) then return sides.south end
if(self:get(sides.east)~=nil) then return sides.east end
if(self:get(sides.west)~=nil) then return sides.west end
if(self:get(sides.up)~=nil) then return sides.up end
if(self:get(sides.down)~=nil) then return sides.down end
return -1
end
function SignReader:get(side)
if(side == nil) then -- Try Smart Choice
if(self:countSigns() == 1) then
return self.s.getValue(self:getFirstSignSide())
else
return nil -- Cannot choose
end
else
return self.s.getValue(side)
end
end
function SignReader:set(val,side)
if(side == nil) then -- Try Smart Choice
if(self:countSigns() == 1) then
return self.s.setValue(self:getFirstSignSide(),val)
else
return nil -- Cannot choose
end
else
return self.s.setValue(side,val)
end
end

75
class.lua Normal file
View File

@ -0,0 +1,75 @@
function clone(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
function class(classname, super)
local superType = type(super)
local cls
if superType ~= "function" and superType ~= "table" then
superType = nil
super = nil
end
if superType == "function" or (super and super.__ctype == 1) then
cls = {}
if superType == "table" then
-- copy fields from super
for k, v in pairs(super) do
cls[k] = v
end
cls.__create = super.__create
cls.super = super
else
cls.__create = super
cls.ctor = function()
end
end
cls.__cname = classname
cls.__ctype = 1
function cls.new(...)
local instance = cls.__create(...)
for k, v in pairs(cls) do
instance[k] = v
end
instance.class = cls
instance:ctor(...)
return instance
end
else
if super then
cls = {}
setmetatable(cls, {__index = super})
cls.super = super
else
cls = {ctor = function()
end}
end
cls.__cname = classname
cls.__ctype = 2 -- lua
cls.__index = cls
function cls.new(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:ctor(...)
return instance
end
end
return cls
end

55
queue.lua Normal file
View File

@ -0,0 +1,55 @@
require("class")
Queue = class("Queue")
function Queue:reset()
self.size = 0
self.bus = {}
self.headindex = -1
self.tailindex = -1
end
function Queue:ctor()
self:reset()
end
function Queue:empty()
return self.size == 0
end
function Queue:push(val)
if (self:empty()) then
self.headindex = 0
self.tailindex = 0
self.size = 1
self.bus[0] = val
else
self.tailindex = self.tailindex + 1
self.bus[self.tailindex] = val
self.size = self.size + 1
end
end
function Queue:top()
if(self:empty()) then
return nil
else
return self.bus[self.headindex]
end
end
function Queue:pop()
if (self:empty()) then
return nil
else
ret = self.bus[self.headindex]
self.bus[self.headindex] = nil
self.size = self.size - 1
if (self:empty()) then
self:reset()
else
self.headindex = self.headindex + 1
end
return ret
end
end

47
util.lua Normal file
View File

@ -0,0 +1,47 @@
local component=require("component")
function getTableSize(t)
local cnt=0
for k in pairs(t) do
cnt=cnt+1
end
return cnt
end
function isTableEmpty(t)
return getTableSize(t) == 0
end
function rawproxy(id)
return component.proxy(id)
end
function proxy(componentType,beginWith)
local t=component.list(componentType)
local sz=getTableSize(t)
if(sz==0) then
print("Query List is Empty")
return nil
elseif(sz==1) then
for k in pairs(t) do
return rawproxy(k)
end
else
if(beginWith == nil) then
print("No beginWith value")
return nil
end
if(type(beginWith) ~= "string") then
print("beginWith is not string")
return nil
end
local bsz=string.len(beginWith)
for k in pairs(t) do
if(string.sub(k,1,bsz) == beginWith) then
return rawproxy(k)
end
end
print("Not found with beginWith value")
return nil
end
end