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)) -- return string.format("loadstring(%q)", string.dump(o))
elseif tp == "userdata" then elseif tp == "userdata" then
if o.is_valid and not o:is_valid() 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 end
if o.is_player then if o.is_player then
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 else
local e = o:get_luaentity() local e = o:get_luaentity()
if e then if e then
return string.format('#<luaentity: "%s">', e.name) return string.format('#<Obj:lua: "%s">', e.name)
else else
return string.format('#<ObjectRef: %s>', o) return string.format('#<Obj: %s>', o)
end end
end end
end end

View File

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