Skip to content

Commit

Permalink
Tracer replacement (cuberite#3704)
Browse files Browse the repository at this point in the history
* Replaced cTracer usage with cLineBlockTracer.

* Exported new cLineBlockTracer utility functions to Lua API.
  • Loading branch information
madmaxoft authored and worktycho committed May 11, 2017
1 parent 7c4576a commit 2c3c1f1
Show file tree
Hide file tree
Showing 18 changed files with 671 additions and 134 deletions.
137 changes: 105 additions & 32 deletions Server/Plugins/APIDump/Classes/Geometry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -977,52 +977,103 @@ return
{
Desc = [[
This class provides an easy-to-use interface for tracing lines through individual
blocks in the world. It will call the provided callbacks according to what events it encounters along the
way.</p>
<p>
For the Lua API, there's only one static function exported that takes all the parameters necessary to do
the tracing. The Callbacks parameter is a table containing all the functions that will be called upon the
various events. See below for further information.
blocks in the world. It can either be used to call the provided callbacks according
to what events it encounters along the way, or there are shortcut functions used for
the most popular tracing reasons - line of sight and solid hits.
]],
Functions =
{
Trace =
FirstSolidHitTrace =
{
IsStatic = true,
Params =
{
IsStatic = true,
Params =
{
Name = "World",
Type = "cWorld",
{ Name = "World", Type = "cWorld" },
{ Name = "StartX", Type = "number" },
{ Name = "StartY", Type = "number" },
{ Name = "StartZ", Type = "number" },
{ Name = "EndX", Type = "number" },
{ Name = "EndY", Type = "number" },
{ Name = "EndZ", Type = "number" },
},
Returns =
{
Name = "Callbacks",
Type = "table",
{ Name = "HasHitSolid", Type = "boolean" },
{ Name = "HitCoords", Type = "Vector3d" },
{ Name = "HitBlockCoords", Type = "Vector3i" },
{ Name = "HitBlockFace", Type = "eBlockFace" },
},
Notes = "If the specified line hits a solid block, return true and the coordinates / face of the first such solid block hit. Returns false if there's no solid block on that line.",
},
{
IsStatic = true,
Params =
{
Name = "StartX",
Type = "number",
{ Name = "World", Type = "cWorld" },
{ Name = "Start", Type = "Vector3d" },
{ Name = "End", Type = "Vector3d" },
},
Returns =
{
Name = "StartY",
Type = "number",
{ Name = "HasHitSolid", Type = "boolean" },
{ Name = "HitCoords", Type = "Vector3d" },
{ Name = "HitBlockCoords", Type = "Vector3i" },
{ Name = "HitBlockFace", Type = "eBlockFace" },
},
Notes = "If the specified line hits a solid block, return true and the coordinates / face of the first such solid block hit. Returns false if there's no solid block on that line.",
},
}, -- FirstSolidHitTrace
LineOfSightTrace =
{
{
IsStatic = true,
Params =
{
Name = "StartZ",
Type = "number",
{ Name = "World", Type = "cWorld" },
{ Name = "StartX", Type = "number" },
{ Name = "StartY", Type = "number" },
{ Name = "StartZ", Type = "number" },
{ Name = "EndX", Type = "number" },
{ Name = "EndY", Type = "number" },
{ Name = "EndZ", Type = "number" },
{ Name = "Sight", Type = "number" },
},
Returns =
{
Name = "EndX",
Type = "number",
{ Name = "CanSee", Type = "boolean" },
},
Notes = "Returns true if the two points specified are within line of sight of each other. The Sight parameter specifies which blocks are considered transparent for the trace, it is a combination of {{cLineBlockTracer#eLineOfSight|losXXX}} values added together."
},
{
IsStatic = true,
Params =
{
Name = "EndY",
Type = "number",
{ Name = "World", Type = "cWorld" },
{ Name = "Start", Type = "Vector3d" },
{ Name = "End", Type = "Vector3d" },
{ Name = "Sight", Type = "number" },
},
Returns =
{
Name = "EndZ",
Type = "number",
{ Name = "CanSee", Type = "boolean" },
},
Notes = "Returns true if the two points specified are within line of sight of each other. The Sight parameter specifies which blocks are considered transparent for the trace, it is a combination of {{cLineBlockTracer#eLineOfSight|losXXX}} values added together."
},
}, -- LineOfSightTrace
Trace =
{
IsStatic = true,
Params =
{
{ Name = "World", Type = "cWorld" },
{ Name = "Callbacks", Type = "table" },
{ Name = "StartX", Type = "number" },
{ Name = "StartY", Type = "number" },
{ Name = "StartZ", Type = "number" },
{ Name = "EndX", Type = "number" },
{ Name = "EndY", Type = "number" },
{ Name = "EndZ", Type = "number" },
},
Returns =
{
Expand All @@ -1033,6 +1084,29 @@ various events. See below for further information.
Notes = "Performs the trace on the specified line. Returns true if the entire trace was processed (no callback returned true)",
},
},
Constants =
{
losAir =
{
Notes = "LineOfSight tracing can 'see' through air blocks.",
},
losWater =
{
Notes = "LineOfSight tracing can 'see' through water blocks.",
},
losLava =
{
Notes = "LineOfSight tracing can 'see' through lava blocks.",
},
},
ConstantGroups =
{
eLineOfSight =
{
Include = "los.*",
TextBefore = "The following constants are used to speficy which blocks are see-through when tracing a LineOfSight trace. Add them together to make up the Sight parameter.",
},
},
AdditionalInfo =
{
{
Expand Down Expand Up @@ -1109,16 +1183,15 @@ end
cTracer =
{
Desc = [[
A cTracer object is used to trace lines in the world. One thing you can use the cTracer for, is
tracing what block a player is looking at, but you can do more with it if you want.</p>
<p>
The cTracer is still a work in progress and is not documented at all.</p>
<p>
See also the {{cLineBlockTracer}} class for an alternative approach using callbacks.
This class is <b>OBSOLETE</b>, do not use it.
See the {{cLineBlockTracer}} class for the replacement.
]],
Functions =
{

Trace =
{
Notes = "<b>OBSOLETE</b>, use the {{cLineBlockTracer}} class instead.",
},
},
},
Vector3d =
Expand Down
72 changes: 72 additions & 0 deletions Server/Plugins/Debuggers/Debuggers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,44 @@ end



function HandleConsoleHitTrace(a_Split)
local world = cRoot:Get():GetDefaultWorld()
local s = Vector3d(0, 70, 0)
local e = Vector3d(100, 75, 100)
if (tonumber(a_Split[2])) then
s.x = tonumber(a_Split[2])
end
if (tonumber(a_Split[3])) then
s.y = tonumber(a_Split[3])
end
if (tonumber(a_Split[4])) then
s.z = tonumber(a_Split[4])
end
if (tonumber(a_Split[5])) then
e.x = tonumber(a_Split[5])
end
if (tonumber(a_Split[6])) then
e.y = tonumber(a_Split[6])
end
if (tonumber(a_Split[7])) then
e.z = tonumber(a_Split[7])
end
local res, hitCoords, hitBlockCoords, hitBlockFace = cLineBlockTracer:FirstSolidHitTrace(world, s, e)
if (res) then
return true, string.format("The line hits block {%d, %d, %d} at point {%f, %f, %f}, face %s",
hitBlockCoords.x, hitBlockCoords.y, hitBlockCoords.z,
hitCoords.x, hitCoords.y, hitCoords.z,
BlockFaceToString(hitBlockFace)
)
else
return true, "The two points specified don't have a solid block between them."
end
end





--- Monitors the state of the "inh" entity-spawning hook
-- if false, the hook is installed before the "inh" command processing
local isInhHookInstalled = false
Expand Down Expand Up @@ -1954,6 +1992,40 @@ end



function HandleConsoleLosTrace(a_Split)
local world = cRoot:Get():GetDefaultWorld()
local s = Vector3d(0, 70, 0)
local e = Vector3d(100, 75, 100)
if (tonumber(a_Split[2])) then
s.x = tonumber(a_Split[2])
end
if (tonumber(a_Split[3])) then
s.y = tonumber(a_Split[3])
end
if (tonumber(a_Split[4])) then
s.z = tonumber(a_Split[4])
end
if (tonumber(a_Split[5])) then
e.x = tonumber(a_Split[5])
end
if (tonumber(a_Split[6])) then
e.y = tonumber(a_Split[6])
end
if (tonumber(a_Split[7])) then
e.z = tonumber(a_Split[7])
end
local res = cLineBlockTracer:LineOfSightTrace(world, s, e, cLineBlockTracer.losAir)
if (res) then
return true, "The two points can see each other."
else
return true, "The two points cannot see each other"
end
end





function HandleConsolePluginStats(a_Split)
cPluginManager:ForEachPlugin(
function (a_CBPlugin)
Expand Down
12 changes: 12 additions & 0 deletions Server/Plugins/Debuggers/Info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ g_PluginInfo =
HelpString = "Tests the crypto hashing functions",
},

["hittrace"] =
{
Handler = HandleConsoleHitTrace,
HelpString = "Tests the FirstSolidHit trace",
},

["inh"] =
{
Handler = HandleConsoleInh,
Expand All @@ -302,6 +308,12 @@ g_PluginInfo =
HelpString = "Loads the specified chunk into memory",
},

["lostrace"] =
{
Handler = HandleConsoleLosTrace,
HelpString = "Tests a LineOfSight trace",
},

["pluginstats"] =
{
Handler = HandleConsolePluginStats,
Expand Down
46 changes: 46 additions & 0 deletions src/Bindings/DeprecatedBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../World.h"
#include "../Entities/Player.h"
#include "LuaState.h"
#include "../Tracer.h"



Expand Down Expand Up @@ -291,6 +292,47 @@ static int tolua_AllToLua_StringToMobType00(lua_State* tolua_S)



/* method: Trace of class cTracer */
static int tolua_cTracer_Trace(lua_State * a_LuaState)
{
// Log a deprecation warning with stacktrace:
cLuaState S(a_LuaState);
LOGWARNING("The function cTracer:Trace is obsolete, use the cLineBlockTracer instead");
S.LogStackTrace();

// Check params:
if (
!S.CheckParamUserType(1, "cTracer") ||
!S.CheckParamUserType(2, "const Vector3<float>", 3) ||
!S.CheckParamNumber (4)
)
{
return 0;
}

// Read params:
cTracer * self;
Vector3d * start;
Vector3d * direction;
int distance;
bool lineOfSight = false;
if (!S.GetStackValues(1, self, start, direction, distance))
{
LOGWARNING("Cannot retrieve parameters for cTracer::Trace. Expected a cTracer (self), Vector3d, Vector3d, number and optional boolean.");
S.LogStackValues();
return 0;
}
S.GetStackValue(5, lineOfSight);

// Call and push the result:
S.Push(self->Trace(*start, *direction, distance, lineOfSight));
return 1;
}





/** function: cWorld:SetSignLines */
static int tolua_cWorld_SetSignLines(lua_State * tolua_S)
{
Expand Down Expand Up @@ -398,6 +440,10 @@ void DeprecatedBindings::Bind(lua_State * tolua_S)

tolua_function(tolua_S, "StringToMobType", tolua_AllToLua_StringToMobType00);

tolua_beginmodule(tolua_S, "cTracer");
tolua_function(tolua_S, "Trace", tolua_cTracer_Trace);
tolua_endmodule(tolua_S);

tolua_beginmodule(tolua_S, "cWorld");
tolua_function(tolua_S, "UpdateSign", tolua_cWorld_SetSignLines);
tolua_endmodule(tolua_S);
Expand Down
Loading

0 comments on commit 2c3c1f1

Please sign in to comment.