master #3

Merged
AliasAlreadyTaken merged 8 commits from your-land-mirror/cmd_eval:master into master 2025-04-04 09:36:24 +02:00
3 changed files with 60 additions and 35 deletions

View File

@ -56,17 +56,17 @@ local function basic_dump(o)
-- return string.format("loadstring(%q)", string.dump(o))
elseif tp == "userdata" then
if o.is_valid and not o:is_valid() then
return string.format('#<invalid_ref: %s>', o)
return string.format('#<Obj:invalid: %s>', o)
end
if o.is_player then
if o:is_player() then
return string.format('#<player: "%s">', o:get_player_name())
return string.format('#<Obj:player: "%s">', o:get_player_name())
else
local e = o:get_luaentity()
if e then
return string.format('#<luaentity: "%s">', e.name)
return string.format('#<Obj:lua: "%s">', e.name)
else
return string.format('#<ObjectRef: %s>', o)
return string.format('#<Obj: %s>', o)
end
end
end

View File

@ -27,46 +27,79 @@ local function create_shared_environment(player_name)
end,
my_pos = function()
local me = core.get_player_by_name(player_name)
local pos = vector.zero() -- FIXME use last command position
if me:is_player() then
local pos = vector.zero() -- FIXME use last command position?
if me and me:is_player() then
pos = me:get_pos()
end
return pos
end,
point = function()
local me = core.get_player_by_name(player_name)
local pointed_thing = util.raycast_crosshair(me, 200, true, false)
if pointed_thing then
return pointed_thing.intersection_point
if me then
local pointed_thing = util.raycast_crosshair(me, 200, true, false)
if pointed_thing then
return pointed_thing.intersection_point
end
return me:get_pos()
end
return me:get_pos()
end,
this_obj = function()
local me = core.get_player_by_name(player_name)
local pointed_thing = util.raycast_crosshair_to_object(me, 200)
if pointed_thing then
return pointed_thing.ref
if me then
local pointed_thing = util.raycast_crosshair_to_object(me, 200)
if pointed_thing then
return pointed_thing.ref
end
end
return nil
end,
this_node_pos = function()
above = function()
local me = core.get_player_by_name(player_name)
local pointed_thing = util.raycast_crosshair(me, 200, false, false)
if pointed_thing then
return pointed_thing.under
if me then
local pointed_thing = util.raycast_crosshair(me, 200, false, false)
if pointed_thing then
return pointed_thing.above
end
end
return vector.round(me:get_pos())
return nil
end,
under = function()
local me = core.get_player_by_name(player_name)
if me then
local pointed_thing = util.raycast_crosshair(me, 200, false, false)
if pointed_thing then
return pointed_thing.under
end
end
return nil
end,
players = function()
local pl = core.get_connected_players()
local k, v
return setmetatable(pl,
{
__index = function(_t, n)
return core.get_player_by_name(n)
end,
__call = function(_t)
k, v = next(pl, k)
return v
end,
}
)
end,
help = function()
local msg = [[
# Variables:
# Magic variables:
me -- your player object
my_pos -- your position
here -- your position where command was executed at (does not change)
point -- the exact pos you're pointing with the crosshair
this_obj -- the obj you're pointing at with the crosshair or nil
this_node_pos -- the node position you're pointing at
above -- same as pointed_thing.above of the node you're pointing at or nil
under -- same as pointed_thing.under of the node you're pointing at or nil
global -- actual global environment (same as _G)
players -- use players.name to access a player (supports `for p in players`)
# Functions:
dir(t) -- print table key/values (returns nothing)
@ -83,7 +116,7 @@ oir(radius) -- return iterator for objects around you
{"<proxy>"},
{
__index = _G,
__newindex = function(t, k, v)
__newindex = function(_t, k, v)
if _G[k] then
core.chat_send_player(player_name, orange_fmt("* Overwriting global: %s", dump(k)))
else
@ -131,7 +164,7 @@ oir(radius) -- return iterator for objects around you
table.sort(keys)
core.chat_send_player(player_name, table.concat(keys, ',\n'))
else
core.chat_send_player(player_name, string.format("Not a table: %s", dump(t)))
core.chat_send_player(player_name, string.format("Not a table: %s", dump(o)))
end
end,
--dump = repl_dump,
@ -147,23 +180,14 @@ oir(radius) -- return iterator for objects around you
oir = function(radius)
local me = core.get_player_by_name(player_name)
if me then
local objs = core.get_objects_inside_radius(me:get_pos(), radius)
local nextkey, v
--local i = 1
return function()
-- FIXME skip invalid objects here?
nextkey, v = next(objs, nextkey)
return v
-- i = i + 1
-- return objs[i]
end
return core.objects_inside_radius(me:get_pos(), radius)
else
return function() return nil end
end
end,
},
{
__index = function(self, key)
__index = function(_self, key)
-- we give warnings on accessing undeclared var because it's likely a typo
local res = rawget(_G, key)
if res == nil then
@ -194,7 +218,7 @@ local function create_command_environment(player_name)
end
local me = core.get_player_by_name(player_name)
local here = me:get_pos()
local here = me and me:get_pos()
local cmd_env = {
-- This is a special _per-command_ environment.
-- The rationale is: each command should have it's own "here"

View File

@ -35,7 +35,8 @@ end
local function raycast_crosshair_to_object(player, range)
local p_eye_pos = player_get_eye_pos(player)
local to = vector.add(p_eye_pos, vector.multiply(player:get_look_dir(), range))
local ray = core.raycast(p_eye_pos, to, point_to_objects, point_to_liquids)
-- point_to_objects = true, point_to_liquids = false
local ray = core.raycast(p_eye_pos, to, true, false)
local pointed_thing = ray:next()
while pointed_thing do
if pointed_thing.type == "object" then