Taking cover behind objects

0 favourites
  • 9 posts
From the Asset Store
Be discreet and rescue your friends from behind enemy lines.
  • Is there a way to have an enemy take cover behind an object using calculations and without the use of nodes? I want the system to be as dynamic as possible.

    Any and all help is greatly appreciated!

  • construct.net/en/forum/construct-3/how-do-i-8/tactically-intelligent-ai-173259

    I completely forgot about that post, thank you.

  • construct.net/en/forum/construct-3/how-do-i-8/tactically-intelligent-ai-173259

    I looked into past posts and did some digging and discovered this system made in Gamemaker, https://youtu.be/XuhkKmNyYTY

    It does everything I need it to do, but I can't figure out how it finds cover. I used to use GM a long time ago but I can't wrap my head around this function.

    This is the code that finds cover

    function scr_find_cover(argument0, argument1, argument2) {

    /*

    If there is line of site between the target enemy and yourself, then

    move behind the nearest wall.

    */

    can_shoot=0 //don't shoot while in this state

    if !collision_line(x+lengthdir_x(argument0.sprite_width,point_direction(argument0.x,argument0.y,x,y)),y+lengthdir_y(argument0.sprite_width,point_direction(argument0.x,argument0.y,x,y)),argument1.x,argument1.y,argument0,1,1)

    //if line of sight....

    {

    //pathfind to a point behind the nearest wall relative to the enemy

    mp_potential_step(argument0.x+lengthdir_x(argument0.sprite_width,point_direction(argument1.x,argument1.y,argument0.x,argument0.y)),argument0.y+lengthdir_y(argument0.sprite_width,point_direction(argument1.x,argument1.y,argument0.x,argument0.y)),argument2*2,0)

    }

    else

    {

    //if there is no line of sight (you are in cover), then decrease your fear level.

    fear-=1

    }

    }

    From my understanding, the fear level is a instance variable that makes enemies take cover when its high.

    "Argument0" is the nearest wall, "Argument1" is the target or the attacker and "Argument2" is the enemies speed.

    If you could translate this then that would be awesome, and if you need anymore details please ask me. Thank you

  • "Nearest wall" would be easy, but it's a weak algorithm would only work with very specific level designs.

    It can only work for uniform wall tiles, maybe from a tilemap, and even then it would be illogical for more complex walls. For example, in the following image if blue were the player and red were the enemy, green was the nearest wall, then the pathfinding target would be at yellow, which is silly.

    If you want to do it like this anyway:

    On "flee" state

    Pick nearest wall to enemy - Create helper "target" sprite at wall

    While target sprite is overlapping wall - Move target sprite one pixel/tile/step at angle(player.x,player.y,target.x,target.y)

    Pathfind to target, destroy target

    If enemy does not have line of sight to player, stop pathfinding, change state

  • I see, that doesn't seem all to great in practice. The solution you gave in https://www.construct.net/en/forum/construct-3/how-do-i-8/tactically-intelligent-ai-173259?kws=ai%2bline%2bof%2bsight sounds great, but I'm not sure how to cast rays at angles.

  • Nah my idea in that thread would have the exact same problem actually. It assumes empty space behind the target, otherwise the wall behind the target would be the first thing hit.

    A better system would probably have a bunch of potential "cover locations" at all times set by the player position, and the enemies would pick the one with lowest pathfinding cost, since the nearest point by distance may not always be the shortest path.

    Unfortunately I'm not seeing a way to get the path length with the built in pathfinder expressions, besides which it could get quite costly to find a path to every potential cover location just to check the distance.

    Getting the path length is possible by looping through all the generated nodes after finding a path, but it would still need to be calculated and compared with all potential pathfinding targets, which sounds awfully heavy to me.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • That does sound tedious, I might just continue digging and try and epitomize the nearest wall algorithm so it doesn't suck as much.

    Thanks a ton for the help, I appreciate it.

  • The solution for nearest wall is simply to design the level taking it into account. Basically no long stretches of walls. Alternatively, use a different type of wall for long sets of walls - just put the "cover" type wall at the ends or corners.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)