Garry's Mod Wiki

Revision Difference

PathFollower:Compute#561243

<function name="Compute" parent="PathFollower" type="classfunc"> <description>Compute shortest path from bot to 'goal' via A* algorithm.</description> <realm>Server</realm> <args> <arg name="from" type="NextBot">The nextbot we're generating for. This can also be a nextbot player (<page>player.CreateNextbot</page>).</arg> <arg name="to" type="Vector">To point</arg>⤶ <arg name="generator" type="function" default="nil">A funtion that allows you to alter the path generation. See example below for the default function.</arg>⤶ <arg name="bot" type="NextBot">The nextbot we're generating for. This can also be a nextbot player (<page>player.CreateNextbot</page>).</arg> <arg name="goal" type="Vector">The target location, the goal.</arg>⤶ <arg name="generator" type="function" default="nil">A funtion that allows you to alter the path generation. See example below for the default function.⤶ <callback>⤶ <arg name="area" type="CNavArea">The area to move to.</arg>⤶ <arg name="fromArea" type="CNavArea">The area to move from.</arg>⤶ <arg name="ladder" type="CNavLadder">The ladder to move to or from (Validation required), if any.</arg>⤶ <arg name="elevator" type="Entity">Will probably be always NULL</arg>⤶ <arg name="length" type="number">Precomputed length between `area` and `fromArea`.</arg>⤶ <ret name="cost" type="number">The cost of movement between `area` and `fromArea`.</ret>⤶ </callback>⤶ ⤶ </arg>⤶ </args> <rets> <ret name="" type="boolean">* If returns true, path was found to the goal position. * If returns false, path may either be invalid (use IsValid() to check), or valid but doesn't reach all the way to the goal.</ret> </rets> </function> <example> <description>The default path generator. You **do not have** to provide the `PathFollower.Compute` any generator functions if you want to use the default generator.</description> <code> path:Compute( self, pos, function( area, fromArea, ladder, elevator, length ) if ( !IsValid( fromArea ) ) then // first area in path, no cost return 0 else if ( !self.loco:IsAreaTraversable( area ) ) then // our locomotor says we can't move here return -1 end // compute distance traveled along path so far local dist = 0 if ( IsValid( ladder ) ) then dist = ladder:GetLength() elseif ( length > 0 ) then // optimization to avoid recomputing length dist = length else dist = ( area:GetCenter() - fromArea:GetCenter() ):GetLength() end local cost = dist + fromArea:GetCostSoFar() // check height change local deltaZ = fromArea:ComputeAdjacentConnectionHeightChange( area ) if ( deltaZ >= self.loco:GetStepHeight() ) then if ( deltaZ >= self.loco:GetMaxJumpHeight() ) then // too high to reach return -1 end // jumping is slower than flat ground local jumpPenalty = 5 cost = cost + jumpPenalty * dist elseif ( deltaZ < -self.loco:GetDeathDropHeight() ) then // too far to drop return -1 end return cost end end ) </code> </example>