Refactoring.

master
Ilya Zhuravlev 2013-08-08 21:57:28 +04:00
parent a9dc4328ec
commit 1df100189e
14 changed files with 313 additions and 243 deletions

View File

@ -1,34 +1,40 @@
stressedArea = { __type = "stressedArea" }
local Area = { __type = "Area" }
function stressedArea.__call(self, first, second)
function Area.__call(self, first, second)
self = {
first = first,
second = second
}
setmetatable(self, stressedArea.__meta)
setmetatable(self, Area.__meta)
return self
end
function stressedArea.name(self, change)
function Area.name(self, change)
for pos in Stress:iterate(self.first, self.second) do
stressedNode(pos):name(change)
end
return self
end
function stressedArea.meta(self, name, value)
function Area.meta(self, name, value)
for pos in Stress:iterate(self.first, self.second) do
stressedNode(pos):meta(name, value)
end
return self
end
function stressedArea.each(self, func)
function Area.each(self, func)
for pos in Stress:iterate(self.first, self.second) do
func(stressedNode(pos))
end
return self
end
stressedArea.__meta = {
__index = stressedArea
Area.__meta = {
__index = Area
}
setmetatable(stressedArea, { __call = stressedArea.__call })
setmetatable(Area, { __call = Area.__call })
Stress.Area = Area

32
core/Inventory.lua Normal file
View File

@ -0,0 +1,32 @@
Inventory = { __type = "Inventory" }
function Inventory.__call(self, invref, list)
self = {
invref = invref,
list = list
}
setmetatable(self, Inventory.__meta)
return self
end
function Inventory.stack(self, number)
return Stress.Stack(self, number)
end
function Inventory.size(self, change)
if not change then
return self.invref:get_size(self.list)
else
self.invref:set_size(self.list, change)
return self
end
end
Inventory.__meta = {
__index = Inventory
}
setmetatable(Inventory, { __call = Inventory.__call })
Stress.Inventory = Inventory

View File

@ -1,36 +1,41 @@
stressedNode = { __type = "stressedNode" }
local Node = { __type = "Node" }
function stressedNode.__call(self, pos)
assert(pos.__type == "stressedPosition", "pos must be of type stressedNodePosition")
function Node.__call(self, pos)
assert(pos.__type == "stressedPosition", "pos must be of type NodePosition")
self = {
pos = pos
}
setmetatable(self, stressedNode.__meta)
setmetatable(self, Node.__meta)
return self
end
function stressedNode.name(self, change)
function Node.name(self, change)
if change == nil then
return minetest.env:get_node(self.pos).name
else
minetest.env:set_node(self.pos, {name = change})
return self
end
end
function stressedNode.meta(self, name, change)
function Node.meta(self, name, change)
if change == nil then
return minetest.env:get_meta(self.pos):get_string(name)
else
minetest.env:get_meta(self.pos):set_string(name, change)
return self
end
end
function stressedNode.inventory(self, name)
function Node.inventory(self, name)
return stressedInventory(minetest.get_inventory({type="node",pos=self.pos}), name)
end
stressedNode.__meta = {
__index = stressedNode
Node.__meta = {
__index = Node
}
setmetatable(stressedNode, { __call = stressedNode.__call })
setmetatable(Node, { __call = Node.__call })
Stress.Node = Node

23
core/NodeDef.lua Normal file
View File

@ -0,0 +1,23 @@
local NodeDef = { __type = "NodeDef" }
function NodeDef.__call(self, name)
assert(type(name) == "string", "name must be of type string")
self = {
name = name
}
setmetatable(self, NodeDef.__meta)
return self
end
function NodeDef.on(self, event, func)
Stress:on(event, func, self.name)
end
NodeDef.__meta = {
__index = NodeDef
}
setmetatable(NodeDef, { __call = NodeDef.__call })
Stress.NodeDef = NodeDef

61
core/Position.lua Normal file
View File

@ -0,0 +1,61 @@
Position = { __type = "Position" }
function Position.__call(self, something)
if something.__type == "Position" then
return something
end
if something.x and something.y and something.z then
self = {x = something.x,
y = something.y,
z = something.z}
else
self = {x = something[1],
y = something[2],
z = something[3]}
end
assert(Position.__valid(self))
setmetatable(self, Position.__meta)
return self
end
function Position.__valid(a)
if type(a) ~= "table" then
return false
end
if a.x and a.y and a.z then
return type(a.x) == "number" and type(a.y) == "number" and type(a.z) == "number"
else
return type(a[1]) == "number" and type(a[2]) == "number" and type(a[3]) == "number"
end
end
function Position.__add(a, b)
sa = Position(a)
sb = Position(b)
return Position({sa.x + sb.x, sa.y + sb.y, sa.z + sb.z})
end
function Position.__sub(a, b)
return Position(a) + (-Position(b))
end
function Position.__unm(a)
return Position({-a.x, -a.y, -a.z})
end
function Position.__eq(a, b)
return a.x == b.x and a.y == b.y and a.z == b.z
end
Position.__meta = {
__index = Position,
__unm = Position.__unm,
__add = Position.__add,
__sub = Position.__sub,
__eq = Position.__eq
}
setmetatable(Position, { __call = Position.__call })
Stress.Position = Position

View File

@ -1,15 +1,15 @@
stressedStack = { __type = "stressedStack" }
Stack = { __type = "Stack" }
function stressedStack.__call(self, inventory, number)
function Stack.__call(self, inventory, number)
self = {
inventory = inventory,
number = number
}
setmetatable(self, stressedStack.__meta)
setmetatable(self, Stack.__meta)
return self
end
function stressedStack.__modify_or_access(self, property, change)
function Stack.__modify_or_access(self, property, change)
local itemstack = self.inventory.invref:get_stack(self.inventory.list, self.number):to_table()
if not change then
return itemstack[property]
@ -19,26 +19,26 @@ function stressedStack.__modify_or_access(self, property, change)
end
end
function stressedStack.name(self, change)
function Stack.name(self, change)
return self:__modify_or_access("name", change)
end
function stressedStack.count(self, change)
function Stack.count(self, change)
return self:__modify_or_access("count", change)
end
function stressedStack.wear(self, change)
function Stack.wear(self, change)
return self:__modify_or_access("wear", change)
end
function stressedStack.take(self, number)
function Stack.take(self, number)
number = number or 1
local itemstack = self.inventory.invref:get_stack(self.inventory.list, self.number):to_table()
itemstack.count = math.max(0, itemstack.count - number)
self.inventory.invref:set_stack(self.inventory.list, self.number, itemstack)
end
function stressedStack.value(self, change)
function Stack.value(self, change)
if not change then
return self.inventory.invref:get_stack(self.inventory.list, self.number):to_table()
else
@ -46,8 +46,11 @@ function stressedStack.value(self, change)
end
end
stressedStack.__meta = {
__index = stressedStack
Stack.__meta = {
__index = Stack
}
setmetatable(stressedStack, { __call = stressedStack.__call })
setmetatable(Stack, { __call = Stack.__call })
Stress.Stack = Stack

119
core/Stress.lua Normal file
View File

@ -0,0 +1,119 @@
--[[
Stress for Minetest
]]
Stress = {}
stressEvents = {
place = {}
}
local myPath = minetest.get_modpath(minetest.get_current_modname())
dofile(myPath .. "/config.lua")
if Stress.config.debug then
print("Stress.config.debug is enabled. *Never* use this in production!")
dofile(myPath .. "/tests/run.lua")
end
dofile(myPath .. "/core/Position.lua")
dofile(myPath .. "/core/Area.lua")
dofile(myPath .. "/core/NodeDef.lua")
dofile(myPath .. "/core/Node.lua")
dofile(myPath .. "/core/Stack.lua")
dofile(myPath .. "/core/Inventory.lua")
function Stress.__call(self, first, second)
if Stress.Position.__valid(first) and Stress.Position.__valid(second) then
return Stress.Area(Stress.Position(first), Stress.Position(second))
elseif type(first) == "string" then
return Stress.NodeDef(first)
elseif Stress.Position.__valid(first) then
local pos = Stress.Position(first)
return Stress.Node(pos)
end
end
function Stress.on(self, event, func, name)
name = name or ""
if stressEvents[event] ~= nil then
if stressEvents[event][name] == nil then
stressEvents[event][name] = {}
end
table.insert(stressEvents[event][name], func)
end
end
function Stress.fireEvent(event_name, node_name, args)
if stressEvents[event_name][name] ~= nil then
for _, v in ipairs(stressEvents[event_name][name]) do
local r = false
if args.pos then
r = v(Stress.Node(Stress.Position(args.pos)), args)
else
r = v(args)
end
if r then
return true
end
end
end
return false
end
function Stress.iterate(self, a, b)
assert(Stress.Position.__valid(a) and Stress.Position.__valid(b), "both arguments to iterate should be positions")
local sa = Stress.Position(a)
local sb = Stress.Position(b)
local first = Stress.Position({math.min(sa.x, sb.x), math.min(sa.y, sb.y), math.min(sa.z, sb.z)})
local second = Stress.Position({math.max(sa.x, sb.x), math.max(sa.y, sb.y), math.max(sa.z, sb.z)})
local current = Stress.Position({first.x - 1, first.y, first.z})
return function()
current.x = current.x + 1
if current.x > second.x then
current.x = first.x
current.y = current.y + 1
end
if current.y > second.y then
current.y = first.y
current.z = current.z + 1
end
if current.z <= second.z then
return current
end
end
end
setmetatable(Stress, { __call = Stress.__call })
function Stress.fireEventEx(event, node, args)
if not Stress.fireEvent(event, node, args) then
Stress.fireEvent(event, "", args)
end
end
minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
local args = {pos = pos, actor = placer}
Stress.fireEventEx("place", newnode.name, args)
end)
minetest.register_on_dignode(function(pos, oldnode, digger)
local args = {pos = pos, actor = digger}
Stress.fireEventEx("dig", oldnode.name, args)
end)
minetest.register_on_punchnode(function(pos, node, puncher)
local args = {pos = pos, actor = puncher}
Stress.fireEventEx("punch", node.name, args)
end)
minetest.register_on_chat_message(function(name, message)
-- umm..
-- well
end)
_ = Stress

View File

@ -1 +1 @@
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/stress.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/core/Stress.lua")

View File

@ -1,93 +0,0 @@
--[[
Stress for Minetest
]]
Stress = {}
stressEvents = {
place = {}
}
local myPath = minetest.get_modpath(minetest.get_current_modname())
dofile(myPath .. "/config.lua")
if Stress.config.debug then
print("Stress.config.debug is enabled. *Never* use this in production!")
dofile(myPath .. "/tests.lua")
end
dofile(myPath .. "/stressedPosition.lua")
dofile(myPath .. "/stressedArea.lua")
dofile(myPath .. "/stressedNodeDef.lua")
dofile(myPath .. "/stressedNode.lua")
dofile(myPath .. "/stressedStack.lua")
dofile(myPath .. "/stressedInventory.lua")
function Stress.__call(self, first, second)
if stressedPosition.__valid(first) and stressedPosition.__valid(second) then
return stressedArea(stressedPosition(first), stressedPosition(second))
elseif type(first) == "string" then
return stressedNodeDef(first)
elseif stressedPosition.__valid(first) then
local pos = stressedPosition(first)
return stressedNode(pos)
end
end
function Stress.on(self, event, func, name)
name = name or ""
if stressEvents[event] ~= nil then
if stressEvents[event][name] == nil then
stressEvents[event][name] = {}
end
table.insert(stressEvents[event][name], func)
end
end
function Stress.firePlaceEvent(name, pos)
if stressEvents["place"][name] ~= nil then
for _, v in ipairs(stressEvents["place"][name]) do
if v(stressedNode(stressedPosition(pos))) then
return true
end
end
end
return false
end
function Stress.iterate(self, a, b)
assert(stressedPosition.__valid(a) and stressedPosition.__valid(b), "both arguments to iterate should be positions")
local sa = stressedPosition(a)
local sb = stressedPosition(b)
local first = stressedPosition({math.min(sa.x, sb.x), math.min(sa.y, sb.y), math.min(sa.z, sb.z)})
local second = stressedPosition({math.max(sa.x, sb.x), math.max(sa.y, sb.y), math.max(sa.z, sb.z)})
local current = stressedPosition({first.x - 1, first.y, first.z})
return function()
current.x = current.x + 1
if current.x > second.x then
current.x = first.x
current.y = current.y + 1
end
if current.y > second.y then
current.y = first.y
current.z = current.z + 1
end
if current.z <= second.z then
return current
end
end
end
setmetatable(Stress, { __call = Stress.__call })
minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
if not Stress.firePlaceEvent(newnode.name, pos) then
Stress.firePlaceEvent("", pos)
end
end)
_ = Stress

View File

@ -1,28 +0,0 @@
stressedInventory = { __type = "stressedInventory" }
function stressedInventory.__call(self, invref, list)
self = {
invref = invref,
list = list
}
setmetatable(self, stressedInventory.__meta)
return self
end
function stressedInventory.stack(self, number)
return stressedStack(self, number)
end
function stressedInventory.size(self, change)
if not change then
return self.invref:get_size(self.list)
else
self.invref:set_size(self.list, change)
end
end
stressedInventory.__meta = {
__index = stressedInventory
}
setmetatable(stressedInventory, { __call = stressedInventory.__call })

View File

@ -1,20 +0,0 @@
stressedNodeDef = { __type = "stressedNodeDef" }
function stressedNodeDef.__call(self, name)
assert(type(name) == "string", "name must be of type string")
self = {
name = name
}
setmetatable(self, stressedNodeDef.__meta)
return self
end
function stressedNodeDef.on(self, event, func)
Stress:on(event, func, self.name)
end
stressedNodeDef.__meta = {
__index = stressedNodeDef
}
setmetatable(stressedNodeDef, { __call = stressedNodeDef.__call })

View File

@ -1,67 +0,0 @@
stressedPosition = { __type = "stressedPosition" }
function stressedPosition.__call(self, something)
if something.__type == "stressedPosition" then
return something
end
if something.x and something.y and something.z then
self = {x = something.x,
y = something.y,
z = something.z}
else
self = {x = something[1],
y = something[2],
z = something[3]}
end
assert(stressedPosition.__valid(self))
setmetatable(self, stressedPosition.__meta)
return self
end
function stressedPosition.__valid(a)
if type(a) ~= "table" then
return false
end
if a.x and a.y and a.z then
return type(a.x) == "number" and type(a.y) == "number" and type(a.z) == "number"
else
return type(a[1]) == "number" and type(a[2]) == "number" and type(a[3]) == "number"
end
end
function stressedPosition.__add(a, b)
sa = stressedPosition(a)
sb = stressedPosition(b)
return stressedPosition({sa.x + sb.x, sa.y + sb.y, sa.z + sb.z})
end
function stressedPosition.__sub(a, b)
return stressedPosition(a) + (-stressedPosition(b))
end
function stressedPosition.__unm(a)
return stressedPosition({-a.x, -a.y, -a.z})
end
function stressedPosition.__eq(a, b)
return a.x == b.x and a.y == b.y and a.z == b.z
end
stressedPosition.__meta = {
__index = stressedPosition,
__unm = stressedPosition.__unm,
__add = stressedPosition.__add,
__sub = stressedPosition.__sub,
__eq = stressedPosition.__eq
}
setmetatable(stressedPosition, { __call = stressedPosition.__call })
function stressedPosition.test()
assert_equal(stressedPosition({1, 2, 3}), stressedPosition({x=1,y=2,z=3}))
assert_equal(stressedPosition({1, 2, 3}), -stressedPosition({-1, -2, -3}))
assert_equal(stressedPosition({1, 2, 3}) + stressedPosition({4, 5, 6}), stressedPosition({5, 7, 9}))
assert_equal(stressedPosition({1, 2, 3}) - stressedPosition({3, 2, 1}), stressedPosition({-2, 0, 2}))
assert_false(stressedPosition.__valid(nil))
assert_true(stressedPosition.__valid({1, 2, 3}))
end

View File

@ -28,8 +28,15 @@ vim luacov.report.out
require "luacov"
require "lunatest"
function Stress.test()
stressedPosition.test()
local stress_root = minetest.get_modpath(minetest.get_current_modname())
local function test()
package.path = package.path .. ";" .. stress_root .. "/tests/?.lua"
lunatest.suite("test_position")
lunatest.run()
minetest.request_shutdown()
end
minetest.after(0, Stress.test)
minetest.after(0, test)

22
tests/test_position.lua Normal file
View File

@ -0,0 +1,22 @@
module(..., package.seeall)
function test_constructor()
assert_equal(Stress.Position({1, 2, 3}), Stress.Position({x=1,y=2,z=3}))
end
function test_unary_minus()
assert_equal(Stress.Position({1, 2, 3}), -Stress.Position({-1, -2, -3}))
end
function test_sum()
assert_equal(Stress.Position({1, 2, 3}) + Stress.Position({4, 5, 6}), Stress.Position({5, 7, 9}))
end
function test_sub()
assert_equal(Stress.Position({1, 2, 3}) - Stress.Position({3, 2, 1}), Stress.Position({-2, 0, 2}))
end
function test_validness()
assert_false(Stress.Position.__valid(nil))
assert_true(Stress.Position.__valid({1, 2, 3}))
end