Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.13 #101

Merged
merged 8 commits into from
Jan 13, 2025
Merged

v0.13 #101

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ exclude_files = {
}

globals = {
-- Globals
"type",

-- Mineunit
"mineunit", "world",
"mineunit_path", "fixture", "fixture_path",
"mineunit_path", "fixture", "fixture_path", "sourcefile",

-- Engine
"INIT", "PLATFORM", "DIR_DELIM",
"core", "minetest",
"vector", "dump","dump2",

-- MTG
"default",
}

Expand All @@ -30,6 +39,7 @@ read_globals = {

-- Minetest
string = {fields = {"split", "trim"}},
table = {fields = {"copy", "getn"}},
table = {fields = {"copy", "getn", "indexof", "insert_all", "key_value_swap", "shuffle"}},
math = {fields = {"hypot", "sign", "factorial"}},
"PseudoRandom", "ItemStack", "VoxelArea", "VoxelManip", "Settings",
}
3 changes: 2 additions & 1 deletion assert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,6 @@ return {
round = round,
is_coordinate = is_coordinate,
has_item = has_item,
type = mineunit_type
type = mineunit_type,
luatype = lua_type,
}
33 changes: 26 additions & 7 deletions bin/mineunit
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local args = {
Xoutput = {},
}

mineunit_version = "0.12.0"
mineunit_version = "0.13.0"
local luaversion = (function()
local jit = rawget(_G, "jit")
return jit and jit.version or rawget(_G, "_VERSION")
Expand Down Expand Up @@ -116,6 +116,9 @@ local function read_mineunit_conf(configpath)
configfile()
if configenv.exclude then
for _,v in ipairs(configenv.exclude) do
if v:sub(-4) == ".lua" then
v = v:sub(1, #sub - 4) .. "$"
end
table.insert(args.luacov.exclude, v)
end
end
Expand Down Expand Up @@ -303,7 +306,10 @@ do -- Parse cli args
end
return name .. "(" .. table.concat(assertargs, ", ") .. ")", length
end)()
print(indenthdr..header..(hdrlen < indent and (" "):rep(indent - hdrlen) or joiner)..c.dim(content))
print(indenthdr..header..(#content > 0
and (hdrlen < indent and (" "):rep(indent - hdrlen) or joiner)..c.dim(content)
or ""
))
end
else
function doc(name, ...)
Expand All @@ -319,22 +325,35 @@ do -- Parse cli args
doc("integer", "thing", "Thing is integer and not floating point value.")
doc("gt", "num1", "num2", "Number num1 is greater than num2.")
doc("lt", "num1", "num2", "Number num1 is smaller than num2.")
doc("in_array", "value", "array", "Value is in contiguously indexed array. Values behind index holes will not be accepted.")
doc("in_array", "value", "array", "Value is in contiguously indexed array. Values behind index holes "..
"will not be accepted.")
doc("nodename", "expected", "pos", "Node name at position matches expected value.")
doc("param2", "expected", "pos", "Value of param2 at position matches expected value.")
doc("close_enough", "num1", "num2", "Value of num1 is close enough to value of num2. Values should be very close but do not have to be exact.")
doc("close_enough", "num1", "num2", "Value of num1 is close enough to value of num2. Values should be "..
"very close but do not have to be exact.")
doc("coordinate", "thing", "Thing is valid three dimensional coodrinate.")
doc("itemname", "thing", "Thing is valid item name.")
doc("itemstring", "thing", "Thing is valid itemstring and can safely be used to initialize new ItemStack instance.")
doc("player_or_name", "thing", "Thing is either a valid Player instance or well known and registered player name.")
doc("itemstring", "thing", "Thing is valid itemstring and can safely be used to initialize new ItemStack "..
"instance.")
doc("player_or_name", "thing", "Thing is either a valid Player instance or well known and registered "..
"player name.")
doc("ItemStack", "thing", "Thing is ItemStack class instance.")
doc("InvRef", "thing", "Thing is valid inventory class instance.")
doc("MetaDataRef", "thing", "Thing is valid MetaData class instance.")
doc("NodeMetaRef", "thing", "Thing is valid NodeMeta class instance.")
doc("Player", "thing", "Thing is valid Player class instance.")
doc("has_item", "item", "pos", "Position contains item.")
doc("has_item", "Player", "itemstring|ItemStack", "")
doc("has_item", "Player", "listname", "itemstring|ItemStack", "")
doc("has_item", "Player", "listname", "slotnumber", "itemstring|ItemStack", "")
doc("has_item", "coordinate", "itemstring|ItemStack)", "")
doc("has_item", "coordinate", "listname", "itemstring|ItemStack", "")
doc("has_item", "coordinate", "listname", "slotnumber", "itemstring|ItemStack", "Check that player "..
"inventory or world position contains specified item. Optionally restricting to named inventory list "..
"and optional slot number.")
print("\nAssertions can be prefixed with is_ (default) or not_ modifiers.")
print("Special assertions are provided in addition to luassert builtin assertions.")
print("Most assertions accept one additional argument besides what has been")
print(" described here, if provided it will be used as assertion failure message.")
return
elseif v == "-c" or v == "--coverage" then
args.coverage = true
Expand Down
3 changes: 2 additions & 1 deletion core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mineunit:apply_default_settings(_G.core.settings)
_G.core.register_on_joinplayer = noop
_G.core.register_on_leaveplayer = noop

_G.minetest.register_on_player_receive_fields = noop

mineunit("game/constants")
mineunit("common/vector")
mineunit("game/item")
Expand Down Expand Up @@ -76,7 +78,6 @@ _G.minetest.add_particlespawner = noop
_G.minetest.registered_chatcommands = {}
_G.minetest.register_chatcommand = noop
_G.minetest.chat_send_player = function(...) print(unpack({...})) end
_G.minetest.register_on_player_receive_fields = noop
_G.minetest.register_on_placenode = noop
_G.minetest.register_on_dignode = noop
_G.minetest.register_on_mods_loaded = function(func) mineunit:register_on_mods_loaded(func) end
Expand Down
9 changes: 4 additions & 5 deletions http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local defaultrequest = {
multipart = false, -- Optional, if true performs a multipart HTTP request. Post only, data must be array
post_data = function(post_data)
if post_data ~= nil then
DEPRECATED("HTTPRequest.post_data: Deprecated, use `data` instead.")
mineunit:DEPRECATED("HTTPRequest.post_data: Deprecated, use `data` instead.")
end
end,
}
Expand Down Expand Up @@ -120,12 +120,11 @@ core.http_add_fetch(httpenv)
function core.request_http_api()
local http_mods = core.settings:get("secure.http_mods")
local trusted_mods = core.settings:get("secure.trusted_mods")
local modname = mineunit:get_current_modname()
http_mods = (http_mods and trusted_mods) and http_mods..","..trusted_mods or http_mods or trusted_mods

if http_mods then
local current_modname = mineunit:get_current_modname()
for modname in http_mods:gmatch("[^%s,]+") do
if modname == current_modname then
for trusted in http_mods:gmatch("[^%s,]+") do
if trusted == modname then
return tablecopy(httpenv)
end
end
Expand Down
21 changes: 10 additions & 11 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ local tagged_paths = {
require("mineunit.print")
require("mineunit.globals")

local function mineunit_path(name)
return pl.path.normpath(string.format("%s/%s", mineunit:config("mineunit_path"), name))
end

local function require_mineunit(name, root, tag)
local modulename = name:gsub("/", ".")
if root and tag and tag ~= "mineunit" then
Expand Down Expand Up @@ -110,7 +106,10 @@ function mineunit:config(key)
end
return default_config[key]
end
mineunit._config.source_path = pl.path.normpath(("%s/%s"):format(mineunit:config("root"), mineunit:config("source_path")))

mineunit._config.source_path = pl.path.normpath(
("%s/%s"):format(mineunit:config("root"), mineunit:config("source_path"))
)

function mineunit:set_modpath(name, path)
path = pl.path.normpath(path)
Expand Down Expand Up @@ -208,7 +207,7 @@ function fixture(name)
if not _fixtures[name] then
mineunit:info("Loading fixture", path)
assert(pl.path.isfile(path), "Fixture not found: " .. path)
result = {dofile(path)}
local result = {dofile(path)}
_fixtures[name] = result
return unpack(result)
else
Expand All @@ -218,8 +217,8 @@ function fixture(name)
end

local function source_path(name)
local source_path = mineunit:config("source_path")
local path = pl.path.normpath(("%s/%s"):format(source_path, name))
local cfg_source_path = mineunit:config("source_path")
local path = pl.path.normpath(("%s/%s"):format(cfg_source_path, name))
mineunit:debug("source_path", path)
return path
end
Expand Down Expand Up @@ -251,9 +250,9 @@ function mineunit.export_object(obj, def)
end
setmetatable(obj, {
__call = function(...)
local obj = def.constructor(...)
obj._mineunit_typename = def.typename or def.name
return obj
local ins = def.constructor(...)
ins._mineunit_typename = def.typename or def.name
return ins
end
})
if not def.private then
Expand Down
33 changes: 32 additions & 1 deletion metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function InvRef:contains_item(listname, stack, match_meta)
local fieldcount = 0
local matching = true
for k,v in pairs(meta1) do
if not v == meta2[k] then
if v ~= meta2[k] then
matching = false
break
end
Expand Down Expand Up @@ -272,6 +272,37 @@ mineunit.export_object(InvRef, {
end,
})

local function getInvRef(thing)
local thingtype = mineunit.utils.type(thing) or type(thing)
if thingtype == "table" then
-- Coordinates
return core.get_meta(thing):get_inventory()
elseif thingtype == "MetaDataRef" or thingtype == "NodeMetaRef" or thingtype == "Player" then
-- Metadata
return thing:get_inventory()
elseif thingtype == "InvRef" then
-- Inventory
return thing
end
error("get_InvRef(thing): Invalid type '"..thingtype.."'.")
end

function mineunit:get_InvRef_data(thing)
thing = getInvRef(thing)
return {
lists = rawget(thing, "_lists"),
sizes = rawget(thing, "_sizes"),
empty = rawget(thing, "_empty"),
}
end

function mineunit:clear_InvRef(thing)
thing = getInvRef(thing)
for listname in pairs(rawget(thing, "_lists")) do
thing:set_list(listname, {})
end
end

--
-- MetaDataRef
--
Expand Down
19 changes: 16 additions & 3 deletions player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ local function raycast_collision(pos, dir, range, resolution)
-- TODO: Add support for entities
range = range or 4
resolution = resolution or 1
local result
local result = nil
local distance = resolution
while result == nil and distance <= range do
local raypos = vector.add(pos, vector.multiply(dir, distance))
Expand Down Expand Up @@ -370,6 +370,7 @@ function Player:do_use(...) -- (pointed_thing/pos/controls, controls if arg1)
local item = self:get_wielded_item()
local itemdef = item:get_definition()
if itemdef and itemdef.on_use then
mineunit:debugf("%s:do_use(%s, %s) with %s", self, pointed_thing, controls, item)
local pointed_thing = get_pointed_thing(self, pointed_thing_or_pos, itemdef.range)
local returnstack
tempcontrols(self, controls)
Expand All @@ -379,12 +380,15 @@ function Player:do_use(...) -- (pointed_thing/pos/controls, controls if arg1)
assert.is_ItemStack(returnstack)
self._inv:set_stack("main", self._wield_index, ItemStack(returnstack))
end
else
mineunit:debugf("%s:do_use(%s, %s) with unknown %s", self, pointed_thing, controls, item)
end
end

function Player:do_use_from_above(pos, controls)
-- Wrapper to move player above given position, look downwards and use on exact position
-- Targeted node is under, player looks from above position
mineunit:debugf("%s:do_use_from_above(%s, %s)", self, pos, controls)
self:set_pos(vector.add(pos, {x=0,y=1,z=0}))
self:do_set_look_xyz("Y-")
local pointed_thing = { type = "node", above = {x=pos.x, y=pos.y+1, z=pos.z}, under = {x=pos.x, y=pos.y, z=pos.z} }
Expand All @@ -399,6 +403,7 @@ function Player:do_place(...) -- (pointed_thing/pos/controls, controls if arg1)
local itemdef = item:get_definition()
if itemdef then
local pointed_thing = get_pointed_thing(self, pointed_thing_or_pos)
mineunit:debugf("%s:do_place(%s, %s) with %s", self, pointed_thing, controls, item)
local returnstack
tempcontrols(self, controls)
if itemdef.on_place and pointed_thing.type == "node" then
Expand All @@ -411,15 +416,18 @@ function Player:do_place(...) -- (pointed_thing/pos/controls, controls if arg1)
assert.is_ItemStack(returnstack)
self._inv:set_stack("main", self._wield_index, ItemStack(returnstack))
end
else
mineunit:debugf("%s:do_place(%s, %s) with unknown %s", self, pointed_thing_or_pos, controls, item)
end
end

function Player:do_place_from_above(pos, controls)
-- Wrapper to move player above given position, look downwards and place to exact position
-- Placed on above position, supporting node is under
mineunit:debugf("%s:do_place_from_above(%s, %s)", self, pos, controls)
self:set_pos(vector.add(pos, {x=0,y=1,z=0}))
self:do_set_look_xyz("Y-")
local pointed_thing = { type = "node", above = {x=pos.x, y=pos.y, z=pos.z}, under = {x=pos.x, y=pos.y-1, z=pos.z} }
local pointed_thing = { type = "node", above = {x=pos.x, y=pos.y+1, z=pos.z}, under = {x=pos.x, y=pos.y, z=pos.z} }
self:do_place(pointed_thing, controls)
-- TODO / TBD: Restore original position and camera orientation?
end
Expand All @@ -440,7 +448,7 @@ function Player:do_set_look_xyz(xyz)
["Z+"] = {0, 0},
["Z-"] = {0, math.pi},
}
dir = look[xyz:sub(1,2):upper()]
local dir = look[xyz:sub(1,2):upper()]
assert(dir, "do_set_look_xyz requires string X+, X-, Y+, Y-, Z+ or Z-")
self:set_look_vertical(dir[1])
if dir[2] then
Expand Down Expand Up @@ -491,6 +499,7 @@ function Player:get_player_control_bits() error("NOT IMPLEMENTED") end
function Player:get_player_name() return self._name end
function Player:is_player() return self._is_player end
function Player:get_wielded_item() return self._inv:get_stack("main", self._wield_index) end
function Player:set_wielded_item(stack) return self._inv:set_stack("main", self._wield_index, stack) end
function Player:get_meta() return self._meta end
function Player:get_inventory() return self._inv end

Expand Down Expand Up @@ -586,6 +595,10 @@ function Player:__index(key)
return result
end

function Player:__tostring()
return self._name
end

mineunit("entity")
mineunit.export_object(Player, {
name = "Player",
Expand Down
Loading
Loading