1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 mp_path and AI movement query

Discussion in 'Programming' started by Bogan666, Jun 5, 2019.

  1. Bogan666

    Bogan666 Member

    Joined:
    Jul 9, 2018
    Posts:
    32
    I'm using mp_path functions for my AI currently, and from what I have setup it works well. The only issue I'm *currently* running into is how I've currently got it setup and how it works.
    I've got something like this set up:
    nearest_tree = instance_nearest(x,y,obj_tree);
    mp_grid_path(grid, path,x,y, nearesttree.x+16,nearesttree.y+16,false);

    This works great! Minis one issue, if a tree is the closest tree or more than one npc they'll both go to it and stack on eachother, I can't think of a way for a tree to be marked as occupied and have it not come up as the nearest tree unless there is no one on it.

    Any help would be appreciated
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,867
    You have to create a routine that gets all trees, sorts them by distance, and goes through them starting from nearest until it finds a tree that is not marked as occupied. When an NPC is told to cut a tree, it gets the nearest tree by calling that function. When the NPC approaches a tree, it constantly monitors the state of that tree, and if it becomes occupied, it calls the routine to find another tree. When an NPC gets to a tree, it marks it as occupied.

    You'll also notice that nearest, as bird flies, is not necessarily the nearest to pathfind. There might be a tree two grid cells away, but path has to go around 20-cell wall, but it is still deemed closer than another that is three grid cells away but with unobstructed path. Many systems deem this an acceptable price, since it would be too expensive to get path lengths to every potential target. Or they get into more complex pathfinding systems, but that's one deep rabbit hole to jump into.
     
  3. Bogan666

    Bogan666 Member

    Joined:
    Jul 9, 2018
    Posts:
    32
    Right, makes sense.
    What I'm having a tough time grasping is letting the npc know that a tree is occupied. I have code currently on the tree that if there is an npc on it mark it as an "active_tree", but have no idea how to do about it.
    I could probably think of how to go about creating a routine to get all trees/find the nearest, or find info about how to do that. But how would I go about using the info of "this is the nearest tree that is currently in use" I don't know what lines of code Id need to check that variable inside of the tree.
    Thanks for the reply though, thought no one was going to.
     
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,867
    You can use ds priority queue for the finder routine. Loop through all trees, and for each calculate distance to calling NPC, then add to queue with tree ID as value, distance as priority. Next, while-loop the queue until its size is zero. Get and delete from queue the tree with shortest distance (var _This_Tree = ds_priority_delete_min()), check if it has occupied variable set (if(_This_Tree.Occupied == false)) and either make it pathing target and break out of the loop, or continue. If you emptied the queue without finding a target, it means there are no valid targets.
     
  5. Bogan666

    Bogan666 Member

    Joined:
    Jul 9, 2018
    Posts:
    32
    ds_proirity_queue seems to be a GMS2 feature.. I'm on 1.4
     
  6. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,460
    Priority queues are in GMS1 as well, but they might be named just ds_priority there.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice