Skip to content

Commit

Permalink
Cleanup trace for bones
Browse files Browse the repository at this point in the history
Cleanup waypointcount
cleanup str concat
  • Loading branch information
ineed bots committed Apr 4, 2024
1 parent f31a306 commit 1bf8314
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 69 deletions.
39 changes: 22 additions & 17 deletions maps/mp/bots/_bot_internal.gsc
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,23 @@ targetObjUpdateNoTrace( obj )
obj.didlook = false;
}

/*
Returns true if myEye can see the bone of self
*/
checkTraceForBone( myEye, bone )
{
boneLoc = self gettagorigin( bone );

if ( !isdefined( boneLoc ) )
{
return false;
}

trace = bullettrace( myEye, boneLoc, false, undefined );

return ( sighttracepassed( myEye, boneLoc, false, undefined ) && ( trace[ "fraction" ] >= 1.0 || trace[ "surfacetype" ] == "glass" ) );
}

/*
The main target thread, will update the bot's main target. Will auto target enemy players and handle script targets.
*/
Expand Down Expand Up @@ -1447,21 +1464,9 @@ target_loop()
}
else
{
targetHead = player gettagorigin( "j_head" );
targetAnkleLeft = player gettagorigin( "j_ankle_le" );
targetAnkleRight = player gettagorigin( "j_ankle_ri" );

traceHead = bullettrace( myEye, targetHead, false, undefined );
traceAnkleLeft = bullettrace( myEye, targetAnkleLeft, false, undefined );
traceAnkleRight = bullettrace( myEye, targetAnkleRight, false, undefined );

canTargetPlayer = ( ( sighttracepassed( myEye, targetHead, false, undefined ) ||
sighttracepassed( myEye, targetAnkleLeft, false, undefined ) ||
sighttracepassed( myEye, targetAnkleRight, false, undefined ) )

&& ( ( traceHead[ "fraction" ] >= 1.0 || traceHead[ "surfacetype" ] == "glass" ) ||
( traceAnkleLeft[ "fraction" ] >= 1.0 || traceAnkleLeft[ "surfacetype" ] == "glass" ) ||
( traceAnkleRight[ "fraction" ] >= 1.0 || traceAnkleRight[ "surfacetype" ] == "glass" ) )
canTargetPlayer = ( ( player checkTraceForBone( myEye, "j_head" ) ||
player checkTraceForBone( myEye, "j_ankle_le" ) ||
player checkTraceForBone( myEye, "j_ankle_ri" ) )

&& ( ignoreSmoke ||
SmokeTrace( myEye, player.origin, level.smokeradius ) ||
Expand Down Expand Up @@ -2372,9 +2377,9 @@ walk_loop()

dist = 16;

if ( level.waypointcount )
if ( level.waypoints.size )
{
goal = level.waypoints[ randomint( level.waypointcount ) ].origin;
goal = level.waypoints[ randomint( level.waypoints.size ) ].origin;
}
else
{
Expand Down
55 changes: 29 additions & 26 deletions maps/mp/bots/_bot_utility.gsc
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,6 @@ readWpsFromFile( mapname )
*/
load_waypoints()
{
level.waypointcount = 0;
level.waypointusage = [];
level.waypointusage[ "allies" ] = [];
level.waypointusage[ "axis" ] = [];
Expand Down Expand Up @@ -1301,9 +1300,7 @@ load_waypoints()
BotBuiltinPrintConsole( "No waypoints loaded!" );
}

level.waypointcount = level.waypoints.size;

for ( i = 0; i < level.waypointcount; i++ )
for ( i = level.waypoints.size - 1; i >= 0; i-- )
{
if ( !isdefined( level.waypoints[ i ].children ) || !isdefined( level.waypoints[ i ].children.size ) )
{
Expand Down Expand Up @@ -1400,7 +1397,7 @@ getWaypointsOfType( type )
{
answer = [];

for ( i = 0; i < level.waypointcount; i++ )
for ( i = level.waypoints.size - 1; i >= 0; i-- )
{
wp = level.waypoints[ i ];

Expand Down Expand Up @@ -2117,16 +2114,18 @@ RemoveWaypointUsage( wp, team )
return;
}

if ( !isdefined( level.waypointusage[ team ][ wp + "" ] ) )
wpstr = wp + "";

if ( !isdefined( level.waypointusage[ team ][ wpstr ] ) )
{
return;
}

level.waypointusage[ team ][ wp + "" ]--;
level.waypointusage[ team ][ wpstr ]--;

if ( level.waypointusage[ team ][ wp + "" ] <= 0 )
if ( level.waypointusage[ team ][ wpstr ] <= 0 )
{
level.waypointusage[ team ][ wp + "" ] = undefined;
level.waypointusage[ team ][ wpstr ] = undefined;
}
}

Expand All @@ -2138,7 +2137,7 @@ GetNearestWaypointWithSight( pos )
candidate = undefined;
dist = 2147483647;

for ( i = 0; i < level.waypointcount; i++ )
for ( i = level.waypoints.size - 1; i >= 0; i-- )
{
if ( !bullettracepassed( pos + ( 0, 0, 15 ), level.waypoints[ i ].origin + ( 0, 0, 15 ), false, undefined ) )
{
Expand Down Expand Up @@ -2167,7 +2166,7 @@ getNearestWaypoint( pos )
candidate = undefined;
dist = 2147483647;

for ( i = 0; i < level.waypointcount; i++ )
for ( i = level.waypoints.size - 1; i >= 0; i-- )
{
curdis = distancesquared( level.waypoints[ i ].origin, pos );

Expand Down Expand Up @@ -2252,7 +2251,8 @@ AStarSearch( start, goal, team, greedy_path )
// pop bestnode from queue
bestNode = open.data[ 0 ];
open HeapRemove();
openset[ bestNode.index + "" ] = undefined;
bestNodeStr = bestNode.index + "";
openset[ bestNodeStr ] = undefined;
wp = level.waypoints[ bestNode.index ];

// check if we made it to the goal
Expand All @@ -2262,14 +2262,16 @@ AStarSearch( start, goal, team, greedy_path )

while ( isdefined( bestNode ) )
{
bestNodeStr = bestNode.index + "";

if ( isdefined( team ) && isdefined( level.waypointusage ) )
{
if ( !isdefined( level.waypointusage[ team ][ bestNode.index + "" ] ) )
if ( !isdefined( level.waypointusage[ team ][ bestNodeStr ] ) )
{
level.waypointusage[ team ][ bestNode.index + "" ] = 0;
level.waypointusage[ team ][ bestNodeStr ] = 0;
}

level.waypointusage[ team ][ bestNode.index + "" ]++;
level.waypointusage[ team ][ bestNodeStr ]++;
}

// construct path
Expand All @@ -2285,6 +2287,7 @@ AStarSearch( start, goal, team, greedy_path )
for ( i = wp.children.size - 1; i >= 0; i-- )
{
child = wp.children[ i ];
childStr = child + "";
childWp = level.waypoints[ child ];

penalty = 1;
Expand All @@ -2293,9 +2296,9 @@ AStarSearch( start, goal, team, greedy_path )
{
temppen = 1;

if ( isdefined( level.waypointusage[ team ][ child + "" ] ) )
if ( isdefined( level.waypointusage[ team ][ childStr ] ) )
{
temppen = level.waypointusage[ team ][ child + "" ]; // consider how many bots are taking this path
temppen = level.waypointusage[ team ][ childStr ]; // consider how many bots are taking this path
}

if ( temppen > 1 )
Expand All @@ -2314,16 +2317,16 @@ AStarSearch( start, goal, team, greedy_path )
newg = bestNode.g + distancesquared( wp.origin, childWp.origin ) * penalty; // bots on same team's path are more expensive

// check if this child is in open or close with a g value less than newg
inopen = isdefined( openset[ child + "" ] );
inopen = isdefined( openset[ childStr ] );

if ( inopen && openset[ child + "" ].g <= newg )
if ( inopen && openset[ childStr ].g <= newg )
{
continue;
}

inclosed = isdefined( closed[ child + "" ] );
inclosed = isdefined( closed[ childStr ] );

if ( inclosed && closed[ child + "" ].g <= newg )
if ( inclosed && closed[ childStr ].g <= newg )
{
continue;
}
Expand All @@ -2332,11 +2335,11 @@ AStarSearch( start, goal, team, greedy_path )

if ( inopen )
{
node = openset[ child + "" ];
node = openset[ childStr ];
}
else if ( inclosed )
{
node = closed[ child + "" ];
node = closed[ childStr ];
}
else
{
Expand All @@ -2352,19 +2355,19 @@ AStarSearch( start, goal, team, greedy_path )
// check if in closed, remove it
if ( inclosed )
{
closed[ child + "" ] = undefined;
closed[ childStr ] = undefined;
}

// check if not in open, add it
if ( !inopen )
{
open HeapInsert( node );
openset[ child + "" ] = node;
openset[ childStr ] = node;
}
}

// done with children, push onto closed
closed[ bestNode.index + "" ] = bestNode;
closed[ bestNodeStr ] = bestNode;
}

return [];
Expand Down
68 changes: 42 additions & 26 deletions maps/mp/bots/_wp_editor.gsc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ init()
level.waypoints = [];
}

level.waypointcount = 0;
level.waypointcount = level.waypoints.size;

level waittill( "connected", player );
player thread onPlayerSpawned();
Expand Down Expand Up @@ -153,29 +153,29 @@ watchAstarCommand()
self endon( "death" );

self notifyonplayercommand( "astar", "+gostand" );
self.astar = undefined;

for ( ;; )
{
self waittill( "astar" );

if ( 1 )
if ( isdefined( self.astar ) )
{
continue;
self iprintln( "Clear AStar" );
self.astar = undefined;
self waittill( "astar" );
}

self iprintln( "Start AStar" );
self.astar = undefined;
astar = spawnstruct();
astar.start = self.origin;
self.astar = spawnstruct();
self.astar.start = self.origin;

self waittill( "astar" );
self iprintln( "End AStar" );
astar.goal = self.origin;
self.astar.goal = self.origin;

astar.nodes = AStarSearch( astar.start, astar.goal, undefined, true );
self iprintln( "AStar size: " + astar.nodes.size );

self.astar = astar;
self.astar.nodes = AStarSearch( self.astar.start, self.astar.goal, undefined, true );
self iprintln( "AStar size: " + self.astar.nodes.size );
}
}

Expand Down Expand Up @@ -405,30 +405,45 @@ updateWaypointsStats()

if ( isdefined( self.astar ) )
{
// print3d(self.astar.start + (0, 0, 35), "start", (0,0,1), 2);
// print3d(self.astar.goal + (0, 0, 35), "goal", (0,0,1), 2);
if ( timeToUpdate )
if ( isdefined( self.astar.start ) )
{
drawPath( self.astar.start );
drawPath( self.astar.goal );
// print3d( self.astar.start + ( 0, 0, 35 ), "start", ( 0, 0, 1 ), 2 );

if ( timeToUpdate )
{
drawPath( self.astar.start );
}
}

prev = self.astar.start + ( 0, 0, 35 );

for ( i = self.astar.nodes.size - 1; i >= 0; i-- )
if ( isdefined( self.astar.goal ) )
{
node = self.astar.nodes[ i ];
// print3d( self.astar.goal + ( 0, 0, 35 ), "goal", ( 0, 0, 1 ), 2 );

// line(prev, level.waypoints[ node ].origin + (0, 0, 35), (0,1,1));
if ( timeToUpdate )
{
drawPath( level.waypoints[ node ].origin );
drawPath( self.astar.goal );
}

prev = level.waypoints[ node ].origin + ( 0, 0, 35 );
}

// line(prev, self.astar.goal + (0, 0, 35), (0,1,1));
if ( isdefined( self.astar.start ) && isdefined( self.astar.goal ) && isdefined( self.astar.nodes ) )
{
prev = self.astar.start + ( 0, 0, 35 );

for ( i = self.astar.nodes.size - 1; i >= 0; i-- )
{
node = self.astar.nodes[ i ];

// line(prev, level.waypoints[ node ].origin + (0, 0, 35), (0,1,1));
if ( timeToUpdate )
{
drawPath( level.waypoints[ node ].origin );
}

prev = level.waypoints[ node ].origin + ( 0, 0, 35 );
}

// line(prev, self.astar.goal + (0, 0, 35), (0,1,1));
}
}
}
}
Expand Down Expand Up @@ -646,6 +661,7 @@ LoadWaypoints()
// self DeleteAllWaypoints();
self iprintlnbold( "Loading WPS..." );
load_waypoints();
level.waypointcount = level.waypoints.size;

wait 1;

Expand Down Expand Up @@ -934,7 +950,7 @@ DeleteAllWaypoints()
self iprintln( "DelAllWps" );
}

buildChildCountString ( wp )
buildChildCountString( wp )
{
if ( wp == -1 )
{
Expand Down

0 comments on commit 1bf8314

Please sign in to comment.