Navigation Menu

Skip to content

Commit

Permalink
Enabled "typed @ operator" compiler check to catch porting bugs
Browse files Browse the repository at this point in the history
Catched a few
Memleaks fix
  • Loading branch information
Kromster80 committed Dec 22, 2014
1 parent 1bbecd6 commit 0e61405
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 117 deletions.
20 changes: 11 additions & 9 deletions RN_DetourNavMeshBuilder.pas
Expand Up @@ -21,6 +21,8 @@
interface

type
PPByte = ^PByte;

/// Represents the source data used to build an navigation mesh tile.
/// @ingroup detour
PdtNavMeshCreateParams = ^TdtNavMeshCreateParams;
Expand Down Expand Up @@ -109,7 +111,7 @@ TdtNavMeshCreateParams = record
/// @param[out] outData The resulting tile data.
/// @param[out] outDataSize The size of the tile data array.
/// @return True if the tile data was successfully created.
function dtCreateNavMeshData(params: PdtNavMeshCreateParams; outData: PPointer; outDataSize: PInteger): Boolean;
function dtCreateNavMeshData(params: PdtNavMeshCreateParams; outData: PPByte; outDataSize: PInteger): Boolean;

/// Swaps the endianess of the tile data's header (#dtMeshHeader).
/// @param[in,out] data The tile data array.
Expand Down Expand Up @@ -262,7 +264,7 @@ procedure subdivide(items: PBVItem; nitems, imin, imax: Integer; curNode: PInteg
else
begin
// Split
calcExtends(items, nitems, imin, imax, @node.bmin, @node.bmax);
calcExtends(items, nitems, imin, imax, @node.bmin[0], @node.bmax[0]);

axis := longestAxis(node.bmax[0] - node.bmin[0],
node.bmax[1] - node.bmin[1],
Expand Down Expand Up @@ -379,7 +381,7 @@ function classifyOffMeshPoint(pt, bmin, bmax: PSingle): Byte;
/// mesh.
///
/// @see dtNavMesh, dtNavMesh::addTile()
function dtCreateNavMeshData(params: PdtNavMeshCreateParams; outData: PPointer; outDataSize: PInteger): Boolean;
function dtCreateNavMeshData(params: PdtNavMeshCreateParams; outData: PPByte; outDataSize: PInteger): Boolean;
var nvp: Integer; offMeshConClass: PByte; storedOffMeshConCount,offMeshConLinkCount: Integer; hmin,hmax,h: Single;
i,j: Integer; iv,p: PWord; bmin,bmax: array [0..2] of Single; p0,p1: PSingle; totPolyCount, totVertCount: Integer;
edgeCount, portalCount: Integer; dir: Word; maxLinkCount, uniqueDetailVertCount, detailTriCount,ndv,nv: Integer;
Expand Down Expand Up @@ -436,17 +438,17 @@ function dtCreateNavMeshData(params: PdtNavMeshCreateParams; outData: PPointer;
hmin := hmin - params.walkableClimb;
hmax := hmax + params.walkableClimb;

dtVcopy(@bmin, @params.bmin);
dtVcopy(@bmax, @params.bmax);
dtVcopy(@bmin[0], @params.bmin[0]);
dtVcopy(@bmax[0], @params.bmax[0]);
bmin[1] := hmin;
bmax[1] := hmax;

for i := 0 to params.offMeshConCount - 1 do
begin
p0 := @params.offMeshConVerts[(i*2+0)*3];
p1 := @params.offMeshConVerts[(i*2+1)*3];
offMeshConClass[i*2+0] := classifyOffMeshPoint(p0, @bmin, @bmax);
offMeshConClass[i*2+1] := classifyOffMeshPoint(p1, @bmin, @bmax);
offMeshConClass[i*2+0] := classifyOffMeshPoint(p0, @bmin[0], @bmax[0]);
offMeshConClass[i*2+1] := classifyOffMeshPoint(p1, @bmin[0], @bmax[0]);

// Zero out off-mesh start positions which are not even potentially touching the mesh.
if (offMeshConClass[i*2+0] = $ff) then
Expand Down Expand Up @@ -571,8 +573,8 @@ offMeshConClass[i*2+1] := classifyOffMeshPoint(p1, @bmin, @bmax);
header.polyCount := totPolyCount;
header.vertCount := totVertCount;
header.maxLinkCount := maxLinkCount;
dtVcopy(@header.bmin, @params.bmin);
dtVcopy(@header.bmax, @params.bmax);
dtVcopy(@header.bmin[0], @params.bmin[0]);
dtVcopy(@header.bmax[0], @params.bmax[0]);
header.detailMeshCount := params.polyCount;
header.detailVertCount := uniqueDetailVertCount;
header.detailTriCount := detailTriCount;
Expand Down
84 changes: 42 additions & 42 deletions RN_DetourObstacleAvoidance.pas
Expand Up @@ -162,14 +162,14 @@ function sweepCircleCircle(const c0: PSingle; const r0: Single; const v: PSingle
const EPS = 0.0001;
var s: array [0..2] of Single; r,c,a,b,d,rd: Single;
begin
dtVsub(@s,c1,c0);
dtVsub(@s[0],c1,c0);
r := r0+r1;
c := dtVdot2D(@s,@s) - r*r;
c := dtVdot2D(@s[0],@s[0]) - r*r;
a := dtVdot2D(v,v);
if (a < EPS) then Exit(0); // not moving

// Overlap, calc time to exit.
b := dtVdot2D(v,@s);
b := dtVdot2D(v,@s[0]);
d := b*b - a*c;
if (d < 0.0) then Exit(0); // no intersection.
a := 1.0 / a;
Expand All @@ -183,14 +183,14 @@ function isectRaySeg(const ap, u, bp, bq: PSingle;
t: PSingle): Integer;
var v,w: array [0..2] of Single; d,s: Single;
begin
dtVsub(@v,bq,bp);
dtVsub(@w,ap,bp);
d := dtVperp2D(u,@v);
dtVsub(@v[0],bq,bp);
dtVsub(@w[0],ap,bp);
d := dtVperp2D(u,@v[0]);
if (abs(d) < 0.000001) then Exit(0);
d := 1.0/d;
t^ := dtVperp2D(@v,@w) * d;
t^ := dtVperp2D(@v[0],@w[0]) * d;
if (t^ < 0) or (t^ > 1) then Exit(0);
s := dtVperp2D(u,@w) * d;
s := dtVperp2D(u,@w[0]) * d;
if (s < 0) or (s > 1) then Exit(0);
Result := 1;
end;
Expand Down Expand Up @@ -225,13 +225,13 @@ constructor TdtObstacleAvoidanceDebugData.Create();

destructor TdtObstacleAvoidanceDebugData.Destroy;
begin
FreeAndNil(m_vel);
FreeAndNil(m_ssize);
FreeAndNil(m_pen);
FreeAndNil(m_vpen);
FreeAndNil(m_vcpen);
FreeAndNil(m_spen);
FreeAndNil(m_tpen);
if m_vel <> nil then FreeMem(m_vel);
if m_ssize <> nil then FreeMem(m_ssize);
if m_pen <> nil then FreeMem(m_pen);
if m_vpen <> nil then FreeMem(m_vpen);
if m_vcpen <> nil then FreeMem(m_vcpen);
if m_spen <> nil then FreeMem(m_spen);
if m_tpen <> nil then FreeMem(m_tpen);

inherited;
end;
Expand Down Expand Up @@ -377,8 +377,8 @@ procedure TdtObstacleAvoidanceQuery.addCircle(const pos: PSingle; const rad: Sin
Inc(m_ncircles);
dtVcopy(@cir.p, pos);
cir.rad := rad;
dtVcopy(@cir.vel, vel);
dtVcopy(@cir.dvel, dvel);
dtVcopy(@cir.vel[0], vel);
dtVcopy(@cir.dvel[0], dvel);
end;

procedure TdtObstacleAvoidanceQuery.addSegment(const p, q: PSingle);
Expand All @@ -390,7 +390,7 @@ procedure TdtObstacleAvoidanceQuery.addSegment(const p, q: PSingle);
seg := @m_segments[m_nsegments];
Inc(m_nsegments);
dtVcopy(@seg.p, p);
dtVcopy(@seg.q, q);
dtVcopy(@seg.q[0], q);
end;

procedure TdtObstacleAvoidanceQuery.prepare(const pos, dvel: PSingle);
Expand All @@ -406,11 +406,11 @@ procedure TdtObstacleAvoidanceQuery.prepare(const pos, dvel: PSingle);
pb := @cir.p;

orig[0] := 0; orig[1] := 0; orig[2] := 0;
dtVsub(@cir.dp,pb,pa);
dtVnormalize(@cir.dp);
dtVsub(@dv, @cir.dvel, dvel);
dtVsub(@cir.dp[0],pb,pa);
dtVnormalize(@cir.dp[0]);
dtVsub(@dv[0], @cir.dvel[0], dvel);

a := dtTriArea2D(@orig, @cir.dp, @dv);
a := dtTriArea2D(@orig[0], @cir.dp[0], @dv[0]);
if (a < 0.01) then
begin
cir.np[0] := -cir.dp[2];
Expand All @@ -429,7 +429,7 @@ procedure TdtObstacleAvoidanceQuery.prepare(const pos, dvel: PSingle);

// Precalc if the agent is really close to the segment.
r := 0.01;
seg.touch := dtDistancePtSegSqr2D(pos, @seg.p, @seg.q, @t) < Sqr(r);
seg.touch := dtDistancePtSegSqr2D(pos, @seg.p[0], @seg.q[0], @t) < Sqr(r);
end;
end;

Expand Down Expand Up @@ -470,16 +470,16 @@ function TdtObstacleAvoidanceQuery.processSample(const vcand: PSingle; const cs:
cir := @m_circles[i];

// RVO
dtVscale(@vab, vcand, 2);
dtVsub(@vab, @vab, vel);
dtVsub(@vab, @vab, @cir.vel);
dtVscale(@vab[0], vcand, 2);
dtVsub(@vab[0], @vab[0], vel);
dtVsub(@vab[0], @vab[0], @cir.vel[0]);

// Side
side := side + dtClamp(dtMin(dtVdot2D(@cir.dp,@vab)*0.5+0.5, dtVdot2D(@cir.np,@vab)*2), 0.0, 1.0);
side := side + dtClamp(dtMin(dtVdot2D(@cir.dp[0],@vab[0])*0.5+0.5, dtVdot2D(@cir.np[0],@vab[0])*2), 0.0, 1.0);
Inc(nside);

htmin := 0; htmax := 0;
if (sweepCircleCircle(pos,rad, @vab, @cir.p,cir.rad, @htmin, @htmax) = 0) then
if (sweepCircleCircle(pos,rad, @vab[0], @cir.p,cir.rad, @htmin, @htmax) = 0) then
continue;

// Handle overlapping obstacles.
Expand Down Expand Up @@ -509,18 +509,18 @@ function TdtObstacleAvoidanceQuery.processSample(const vcand: PSingle; const cs:
if (seg.touch) then
begin
// Special case when the agent is very close to the segment.
dtVsub(@sdir, @seg.q, @seg.p);
dtVsub(@sdir[0], @seg.q[0], @seg.p[0]);
snorm[0] := -sdir[2];
snorm[2] := sdir[0];
// If the velocity is pointing towards the segment, no collision.
if (dtVdot2D(@snorm, vcand) < 0.0) then
if (dtVdot2D(@snorm[0], vcand) < 0.0) then
continue;
// Else immediate collision.
htmin := 0.0;
end
else
begin
if (isectRaySeg(pos, vcand, @seg.p, @seg.q, @htmin) = 0) then
if (isectRaySeg(pos, vcand, @seg.p[0], @seg.q[0], @htmin) = 0) then
continue;
end;

Expand Down Expand Up @@ -588,12 +588,12 @@ function TdtObstacleAvoidanceQuery.sampleVelocityGrid(const pos: PSingle; const

if (Sqr(vcand[0])+Sqr(vcand[2]) > Sqr(vmax+cs/2)) then continue;

penalty := processSample(@vcand, cs, pos,rad,vel,dvel, minPenalty, debug);
penalty := processSample(@vcand[0], cs, pos,rad,vel,dvel, minPenalty, debug);
Inc(ns);
if (penalty < minPenalty) then
begin
minPenalty := penalty;
dtVcopy(nvel, @vcand);
dtVcopy(nvel, @vcand[0]);
end;
end;
end;
Expand Down Expand Up @@ -661,9 +661,9 @@ function TdtObstacleAvoidanceQuery.sampleVelocityAdaptive(const pos: PSingle; co
sa := sin(da);

// desired direction
dtVcopy(@ddir, dvel);
dtNormalize2D(@ddir);
dtRorate2D(@ddir[3], @ddir, da*0.5); // rotated by da/2
dtVcopy(@ddir[0], dvel);
dtNormalize2D(@ddir[0]);
dtRorate2D(@ddir[3], @ddir[0], da*0.5); // rotated by da/2

// Always add sample at zero
pat[npat*2+0] := 0;
Expand Down Expand Up @@ -707,13 +707,13 @@ function TdtObstacleAvoidanceQuery.sampleVelocityAdaptive(const pos: PSingle; co

// Start sampling.
cr := vmax * (1.0 - m_params.velBias);
dtVset(@res, dvel[0] * m_params.velBias, 0, dvel[2] * m_params.velBias);
dtVset(@res[0], dvel[0] * m_params.velBias, 0, dvel[2] * m_params.velBias);
ns := 0;

for k := 0 to depth - 1 do
begin
minPenalty := MaxSingle;
dtVset(@bvel, 0,0,0);
dtVset(@bvel[0], 0,0,0);

for i := 0 to npat - 1 do
begin
Expand All @@ -723,21 +723,21 @@ function TdtObstacleAvoidanceQuery.sampleVelocityAdaptive(const pos: PSingle; co

if (Sqr(vcand[0])+Sqr(vcand[2]) > Sqr(vmax+0.001)) then continue;

penalty := processSample(@vcand,cr/10, pos,rad,vel,dvel, minPenalty, debug);
penalty := processSample(@vcand[0],cr/10, pos,rad,vel,dvel, minPenalty, debug);
Inc(ns);
if (penalty < minPenalty) then
begin
minPenalty := penalty;
dtVcopy(@bvel, @vcand);
dtVcopy(@bvel[0], @vcand[0]);
end;
end;

dtVcopy(@res, @bvel);
dtVcopy(@res[0], @bvel[0]);

cr := cr * 0.5;
end;

dtVcopy(nvel, @res);
dtVcopy(nvel, @res[0]);

Result := ns;
end;
Expand Down

0 comments on commit 0e61405

Please sign in to comment.