Search Unity

Contextually fulfilling preconditions in AI plan

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, May 24, 2019.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    673
    I'm trying to build a smart object system like The Sims, wherein which each object advertises various plans, and each plan is a set of steps to be executed in order.

    I have all that working, but some plans have preconditions that NPCs may or may not satisfy. As a goofy example, let's say a Cook A Meal plan has four basic steps: place ingredients on counter, prepare ingredients, cook ingredients, place meal on table.

    Critically, the prepare ingredients step requires cooking utensils: a knife or spatula or something. If the NPC is carrying a utensil, it can execute that step the instant it reaches it, and if no knife is available it needs to fail out of the plan, but if there's a knife nearby, the plan needs to update its action queue and add some new pick up knife action.

    So, what is the actual design pattern I should be building this around? Right now every plan is just a linear set of actions that I write in a CSV, but the sims obviously had some degree of flexibility I'm not accounting for since their agents were capable of improvising a fair bit to satisfy the preconditions of each prescribed action.

    My own description sounds kinda-sorta like an action planner but that feels like overkill, and I'm reluctant to build an entire GOAP system because of how hard they are to debug, and how easily they can lead to bizarre behavior.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    737
    You can either insert an "acquire knife" plan into your queue, or you can design your plans to allow for sub-plans. I think since you already have this idea of sub-plans (you called them steps), it should be straightforward to expand on this.

    Your example has "cook a meal" consisting of four steps. You could make "prepare ingredients" also consist of sub-plans: acquire knife, chop vegetables, put knife in sink.

    Your CSV of all known plans would need a new parent plan field: "chop vegetables" is a sub-plan of "prepare ingredients". When beginning to execute a plan, you find all the sub-plans with that plan as a parent.

    As for Sims visualization, I would suggest you show the current top-level plan sequence on one row, and its sub-plans on another row or inserted in parentheses after the first top-level plan. I would not show the entire hierarchy of all plans and their sub-plans and their sub-sub-plans, etc.
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    673
    Hmm, how would you specifically handle checking preconditions, so you don't end up with weirdness like characters who are already holding a knife (which they picked up from an unrelated task) getting a second one? Would you just set it up exactly like an action planner, where items and objects have some kind of interface that returns a list of <key, value> conditions that object satisfies, and the AI builds a graph of everything in its immediate vicinity and runs A* across it to assemble, going back to our example, something that gives (CookingTool,1), (RawIngredients,1), and so forth?
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    737
    Nope. The sub-plan is in the queue. It starts. If character has knife at the beginning of the sub-plan, mission accomplished, move on.
     
  5. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    673
    Ooh I see where you're thinking, that makes it much more manageable and keeps the whole thing anchored in the CSVs... thanks for the feedback! :)
     
unityunity