From 695ec80a30d90859475a939b0977581686b18c96 Mon Sep 17 00:00:00 2001 From: Wobbo Date: Fri, 17 Jan 2014 21:44:11 +0100 Subject: [PATCH 1/5] Added package module to /lib --- assets/opencomputers/lua/rom/lib/package.lua | 87 ++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 assets/opencomputers/lua/rom/lib/package.lua diff --git a/assets/opencomputers/lua/rom/lib/package.lua b/assets/opencomputers/lua/rom/lib/package.lua new file mode 100644 index 000000000..f4f22ae8c --- /dev/null +++ b/assets/opencomputers/lua/rom/lib/package.lua @@ -0,0 +1,87 @@ +package = {} + +package.path = "./?.lua;/lib/?.lua;/usr/lib/?.lua;/home/lib/?.lua" + +local loading = {} + +local loaded = {} +package.loaded = setmetatable({}, {__index = loaded, __newindex = function(t, k, v) loaded[k] = v end}) + +local preload = {} +package.preload = setmetatable({}, {__index = preload, __newindex = function(t, k, v) preload[k] = v end}) + +package.searchers = {} + +function package.searchpath(name, path, sep, rep) + assert(name, "You must specify a name!") + assert(path, "You must specify a path!") + sep = sep or '.' + rep = rep or '/' + sep, rep = '%'..sep, '%'..rep + name = string.gsub(name, sep, rep) + local errorFiles = {} + for sPath in string.gmatch(path, "([^;]+)") do + sPath = string.gsub(sPath, "?", name) + sPath = shell.resolve(sPath) + if fs.exists(sPath) then + local file = io.open(sPath, "r") + if file then + file:close() + return sPath + else + table.insert(errorFiles, "Tried to open: "..sPath) + end + end + end + return nil, table.concat(errorFiles, " ") +end + +local function preloadSearcher(module) + return preload[module] +end + +local function pathSearcher(module) + local sPath, err = package.searchpath(module, package.path) + if sPath then + return loadfile(sPath, "bt", _G), sPath + else + return err + end +end + +table.insert(package.searchers, preloadSearcher) +table.insert(package.searchers, pathSearcher) + +function require(module) + if loaded[module] then + return loaded[module] + elseif not loading[module] then + loading[module] = true + local loader, value, errorMsg = nil, nil, {"Could not load "..module} + for i=1, #package.searchers do + local f, extra = package.searchers[i](module) + if f and type(f) ~= "string" then + loader = f + value = extra + break + else + table.insert(errorMsg, f) + end + end + if loader then + local ret = loader(module, value) + if ret then + loaded[module] = ret + elseif not loaded[module] then + loaded[module] = true + end + loading[module] = false + return loaded[module] + else + loading[module] = false + error(table.concat(errorMsg, '\n')) + end + else + error "Already loading: "..module + end +end From 62b704ae694563fb9b187a7f0dd93c3896c031a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Fri, 17 Jan 2014 23:45:26 +0100 Subject: [PATCH 2/5] fixed robot movement glitch when their tile entity was queried during their break/invalidate phase (e.g. from SGT2's network scan) --- li/cil/oc/common/tileentity/Robot.scala | 1 + mcmod.info | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/li/cil/oc/common/tileentity/Robot.scala b/li/cil/oc/common/tileentity/Robot.scala index 6edd01ebe..b2fcb66c5 100644 --- a/li/cil/oc/common/tileentity/Robot.scala +++ b/li/cil/oc/common/tileentity/Robot.scala @@ -151,6 +151,7 @@ class Robot(isRemote: Boolean) extends Computer(isRemote) with ISidedInventory w if (created) { assert(world.getBlockTileEntity(nx, ny, nz) == proxy) assert(x == nx && y == ny && z == nz) + world.setBlock(ox, oy, oz, 0, 0, 1) Blocks.robotAfterimage.setBlock(world, ox, oy, oz, 1) assert(Delegator.subBlock(world, ox, oy, oz).exists(_ == Blocks.robotAfterimage)) // Here instead of Lua callback so that it gets called on client, too. diff --git a/mcmod.info b/mcmod.info index d846ce8d2..1bac1e3a6 100644 --- a/mcmod.info +++ b/mcmod.info @@ -4,7 +4,7 @@ "modid": "OpenComputers", "name": "OpenComputers", "description": "This mod adds modular computers and robots that can be programmed in Lua.", - "version": "1.1.2a", + "version": "1.1.2b", "mcversion": "1.6.4", "url": "https://github.com/MightyPirates/OpenComputers/wiki", "authors": ["Florian 'Sangar' Nücke", "Johannes 'Lord Joda' Lohrer"], From 428161562ee293ea212c1cc7baf3429cd0fe7401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 18 Jan 2014 00:46:15 +0100 Subject: [PATCH 3/5] added a robot with max xp and full energy to creative tab for easier testing; fixed robot.level() value's post decimal value --- li/cil/oc/Blocks.scala | 13 +++++++++++-- li/cil/oc/common/block/Delegator.scala | 7 +++++-- li/cil/oc/common/tileentity/Robot.scala | 4 +++- li/cil/oc/server/component/Robot.scala | 4 +++- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/li/cil/oc/Blocks.scala b/li/cil/oc/Blocks.scala index 0f446a736..f8bf39549 100644 --- a/li/cil/oc/Blocks.scala +++ b/li/cil/oc/Blocks.scala @@ -2,10 +2,11 @@ package li.cil.oc import cpw.mods.fml.common.registry.GameRegistry import li.cil.oc.common.block._ -import li.cil.oc.common.tileentity +import li.cil.oc.common.{block, tileentity} import net.minecraft.block.Block import net.minecraft.item.ItemStack import net.minecraftforge.oredict.OreDictionary +import net.minecraft.nbt.NBTTagCompound object Blocks { var blockSimple: SimpleDelegator = _ @@ -32,7 +33,15 @@ object Blocks { blockSimple = new SimpleDelegator(Settings.get.blockId1) blockSimpleWithRedstone = new SimpleRedstoneDelegator(Settings.get.blockId2) blockSpecial = new SpecialDelegator(Settings.get.blockId3) - blockSpecialWithRedstone = new SpecialRedstoneDelegator(Settings.get.blockId4) + blockSpecialWithRedstone = new SpecialRedstoneDelegator(Settings.get.blockId4) { + override def subBlockItemStacks() = super.subBlockItemStacks() ++ Iterable({ + val stack = new ItemStack(this, 1, robotProxy.blockId) + stack.setTagCompound(new NBTTagCompound("tag")) + stack.getTagCompound.setDouble(Settings.namespace + "xp", Settings.get.baseXpToLevel + math.pow(30.0001 * Settings.get.constantXpGrowth, Settings.get.exponentialXpGrowth)) + stack.getTagCompound.setInteger(Settings.namespace + "storedEnergy", (Settings.get.bufferRobot + Settings.get.bufferPerLevel * 30).toInt) + stack + }) + } GameRegistry.registerBlock(blockSimple, classOf[Item], Settings.namespace + "simple") GameRegistry.registerBlock(blockSimpleWithRedstone, classOf[Item], Settings.namespace + "simple_redstone") diff --git a/li/cil/oc/common/block/Delegator.scala b/li/cil/oc/common/block/Delegator.scala index e07ce67de..8b774ff1d 100644 --- a/li/cil/oc/common/block/Delegator.scala +++ b/li/cil/oc/common/block/Delegator.scala @@ -57,10 +57,13 @@ class Delegator[Child <: Delegate](id: Int) extends Block(id, Material.iron) { override def getSubBlocks(itemId: Int, creativeTab: CreativeTabs, list: util.List[_]) = { // Workaround for MC's untyped lists... def add[T](list: util.List[T], value: Any) = list.add(value.asInstanceOf[T]) - (0 until subBlocks.length).filter(id => subBlocks(id).showInItemList). - foreach(id => add(list, new ItemStack(this, 1, id))) + subBlockItemStacks().sortBy(_.getUnlocalizedName).foreach(add(list, _)) } + def subBlockItemStacks() = (0 until subBlocks.length). + filter(id => subBlocks(id).showInItemList). + map(id => new ItemStack(this, 1, id)) + // ----------------------------------------------------------------------- // // Rotation // ----------------------------------------------------------------------- // diff --git a/li/cil/oc/common/tileentity/Robot.scala b/li/cil/oc/common/tileentity/Robot.scala index b2fcb66c5..45a5c9bec 100644 --- a/li/cil/oc/common/tileentity/Robot.scala +++ b/li/cil/oc/common/tileentity/Robot.scala @@ -74,7 +74,9 @@ class Robot(isRemote: Boolean) extends Computer(isRemote) with ISidedInventory w var xp = 0.0 - def xpForNextLevel = Settings.get.baseXpToLevel + Math.pow((level + 1) * Settings.get.constantXpGrowth, Settings.get.exponentialXpGrowth) + def xpForNextLevel = xpForLevel(level + 1) + + def xpForLevel(level: Int) = Settings.get.baseXpToLevel + Math.pow(level * Settings.get.constantXpGrowth, Settings.get.exponentialXpGrowth) var level = 0 diff --git a/li/cil/oc/server/component/Robot.scala b/li/cil/oc/server/component/Robot.scala index f5a87e00f..33309da41 100644 --- a/li/cil/oc/server/component/Robot.scala +++ b/li/cil/oc/server/component/Robot.scala @@ -40,7 +40,9 @@ class Robot(val robot: tileentity.Robot) extends Computer(robot) with RobotConte @LuaCallback(value = "level", direct = true) def level(context: Context, args: Arguments): Array[AnyRef] = { - result(robot.level + robot.xp / robot.xpForNextLevel) + val xpNeeded = robot.xpForNextLevel - robot.xpForLevel(robot.level) + val xpProgress = math.max(0, robot.xp - robot.xpForLevel(robot.level)) + result(robot.level + xpProgress / xpNeeded) } // ----------------------------------------------------------------------- // From aba968f6d780498a1b8a83192b46c9508fec9fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 18 Jan 2014 13:51:08 +0100 Subject: [PATCH 4/5] converted package lib indentation to spaces to be consistent with other libs --- assets/opencomputers/lua/rom/lib/package.lua | 118 +++++++++---------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/assets/opencomputers/lua/rom/lib/package.lua b/assets/opencomputers/lua/rom/lib/package.lua index f4f22ae8c..034142515 100644 --- a/assets/opencomputers/lua/rom/lib/package.lua +++ b/assets/opencomputers/lua/rom/lib/package.lua @@ -13,75 +13,75 @@ package.preload = setmetatable({}, {__index = preload, __newindex = function(t, package.searchers = {} function package.searchpath(name, path, sep, rep) - assert(name, "You must specify a name!") - assert(path, "You must specify a path!") - sep = sep or '.' - rep = rep or '/' - sep, rep = '%'..sep, '%'..rep - name = string.gsub(name, sep, rep) - local errorFiles = {} - for sPath in string.gmatch(path, "([^;]+)") do - sPath = string.gsub(sPath, "?", name) - sPath = shell.resolve(sPath) - if fs.exists(sPath) then - local file = io.open(sPath, "r") - if file then - file:close() - return sPath - else - table.insert(errorFiles, "Tried to open: "..sPath) - end - end - end - return nil, table.concat(errorFiles, " ") + assert(name, "You must specify a name!") + assert(path, "You must specify a path!") + sep = sep or '.' + rep = rep or '/' + sep, rep = '%'..sep, '%'..rep + name = string.gsub(name, sep, rep) + local errorFiles = {} + for sPath in string.gmatch(path, "([^;]+)") do + sPath = string.gsub(sPath, "?", name) + sPath = shell.resolve(sPath) + if fs.exists(sPath) then + local file = io.open(sPath, "r") + if file then + file:close() + return sPath + else + table.insert(errorFiles, "Tried to open: "..sPath) + end + end + end + return nil, table.concat(errorFiles, " ") end local function preloadSearcher(module) - return preload[module] + return preload[module] end local function pathSearcher(module) - local sPath, err = package.searchpath(module, package.path) - if sPath then - return loadfile(sPath, "bt", _G), sPath - else - return err - end + local sPath, err = package.searchpath(module, package.path) + if sPath then + return loadfile(sPath, "bt", _G), sPath + else + return err + end end table.insert(package.searchers, preloadSearcher) table.insert(package.searchers, pathSearcher) function require(module) - if loaded[module] then - return loaded[module] - elseif not loading[module] then - loading[module] = true - local loader, value, errorMsg = nil, nil, {"Could not load "..module} - for i=1, #package.searchers do - local f, extra = package.searchers[i](module) - if f and type(f) ~= "string" then - loader = f - value = extra - break - else - table.insert(errorMsg, f) - end - end - if loader then - local ret = loader(module, value) - if ret then - loaded[module] = ret - elseif not loaded[module] then - loaded[module] = true - end - loading[module] = false - return loaded[module] - else - loading[module] = false - error(table.concat(errorMsg, '\n')) - end - else - error "Already loading: "..module - end + if loaded[module] then + return loaded[module] + elseif not loading[module] then + loading[module] = true + local loader, value, errorMsg = nil, nil, {"Could not load "..module} + for i=1, #package.searchers do + local f, extra = package.searchers[i](module) + if f and type(f) ~= "string" then + loader = f + value = extra + break + else + table.insert(errorMsg, f) + end + end + if loader then + local ret = loader(module, value) + if ret then + loaded[module] = ret + elseif not loaded[module] then + loaded[module] = true + end + loading[module] = false + return loaded[module] + else + loading[module] = false + error(table.concat(errorMsg, '\n')) + end + else + error "Already loading: "..module + end end From e042d16f6852c0ba3847fd44189573bdc887e84d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 18 Jan 2014 14:13:58 +0100 Subject: [PATCH 5/5] using oc specific parameter checking function in package lib and adjusted error messages to be more consistent with vanilla Lua --- assets/opencomputers/lua/rom/lib/package.lua | 62 +++++++++++--------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/assets/opencomputers/lua/rom/lib/package.lua b/assets/opencomputers/lua/rom/lib/package.lua index 034142515..17ab28c8d 100644 --- a/assets/opencomputers/lua/rom/lib/package.lua +++ b/assets/opencomputers/lua/rom/lib/package.lua @@ -1,51 +1,54 @@ -package = {} +local package = {} package.path = "./?.lua;/lib/?.lua;/usr/lib/?.lua;/home/lib/?.lua" local loading = {} local loaded = {} -package.loaded = setmetatable({}, {__index = loaded, __newindex = function(t, k, v) loaded[k] = v end}) +package.loaded = loaded local preload = {} -package.preload = setmetatable({}, {__index = preload, __newindex = function(t, k, v) preload[k] = v end}) +package.preload = preload package.searchers = {} function package.searchpath(name, path, sep, rep) - assert(name, "You must specify a name!") - assert(path, "You must specify a path!") + checkArg(1, name, "string") + checkArg(2, path, "string") sep = sep or '.' rep = rep or '/' - sep, rep = '%'..sep, '%'..rep + sep, rep = '%' .. sep, '%' .. rep name = string.gsub(name, sep, rep) local errorFiles = {} - for sPath in string.gmatch(path, "([^;]+)") do - sPath = string.gsub(sPath, "?", name) - sPath = shell.resolve(sPath) - if fs.exists(sPath) then - local file = io.open(sPath, "r") + for subPath in string.gmatch(path, "([^;]+)") do + subPath = string.gsub(subPath, "?", name) + subPath = shell.resolve(subPath) + if fs.exists(subPath) then + local file = io.open(subPath, "r") if file then file:close() - return sPath - else - table.insert(errorFiles, "Tried to open: "..sPath) + return subPath end end + table.insert(errorFiles, "\tno file '" .. subPath .. "'") end - return nil, table.concat(errorFiles, " ") + return nil, table.concat(errorFiles, "\n") end local function preloadSearcher(module) - return preload[module] + if preload[module] ~= nil then + return preload[module] + else + return "\tno field package.preload['" .. module .. "']" + end end local function pathSearcher(module) - local sPath, err = package.searchpath(module, package.path) - if sPath then - return loadfile(sPath, "bt", _G), sPath + local filepath, reason = package.searchpath(module, package.path) + if filepath then + return loadfile(filepath, "bt", _G), filepath else - return err + return reason end end @@ -53,25 +56,26 @@ table.insert(package.searchers, preloadSearcher) table.insert(package.searchers, pathSearcher) function require(module) + checkArg(1, module, "string") if loaded[module] then return loaded[module] elseif not loading[module] then loading[module] = true - local loader, value, errorMsg = nil, nil, {"Could not load "..module} + local loader, value, errorMsg = nil, nil, {"module '" .. module .. "' not found:"} for i=1, #package.searchers do local f, extra = package.searchers[i](module) if f and type(f) ~= "string" then loader = f value = extra break - else + elseif f then table.insert(errorMsg, f) end end if loader then - local ret = loader(module, value) - if ret then - loaded[module] = ret + local result = loader(module, value) + if result then + loaded[module] = result elseif not loaded[module] then loaded[module] = true end @@ -79,9 +83,13 @@ function require(module) return loaded[module] else loading[module] = false - error(table.concat(errorMsg, '\n')) + error(table.concat(errorMsg, "\n")) end else - error "Already loading: "..module + error("already loading: " .. module) end end + +------------------------------------------------------------------------------- + +_G.package = package