Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Looking for feedback on AI system

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Jan 5, 2015.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I'm working on the main AI module for my game, it needs to successfully simulate modern urban street and office environments with a large number of NPCs, which means fairly simple logic that can branch into a wide range of behaviors. To do this, I've been working with two concepts: Rooms, and Entities. The entire game's level geometry is broken down into Rooms, and inside each Room, every object that an NPC can interact with (desks, computers, small pets) is an Entity. Rooms are loaded and unloaded like chunks, which means that when given a location and a list of NPCs at that location, my system needs to quickly render a realistic-looking scene that can pass player scrutiny, as Rooms only get loaded when the player is within a few seconds of entering their line of sight.

    My goal to get convincing-looking areas is to basically use Sims logic: every NPC inside a Room takes inventory of the Entities inside of it, iterates through an internal hierarchy, and based on what that NPC wants (food, mental stimulation, work), it selects an entity, moves to it (or, if the room is being set up for the first time, teleports to it), and activates it, which calls any animations, stat alterations, or special effects of that Entity.

    This seems decent, but I have a couple of concerns with it. Chief among them is that it requires an awful lot of hard-coding: the only way I have for the system to see Rooms is through tags, and to see Entities inside of a Room the AI has to manually search through the Room's children. Many entities require orientation-sensitive animations, which means manually placing activation nodes for NPCs to path to, and on the whole it seems like I'm leaving an awful lot of room for human error. Is there an obvious, more optimal way to implement reasonably believable AI without the hardcoding, or am I doomed to manually delimit at least a large portion of the level geometry and interactivity?
     
    Last edited: Jan 5, 2015
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Brain dump of ideas that may help.

    Prefabs and polymorphism are your friends here.

    Each entity should be a prefab that has a series of standard components on it. The components should include a location for the NPC to stand when interacting, and a method to get the location. Each entity should also expose an interact method.

    Move a lot of the work of finding objects to the rooms themselves. At a minimum each room should have a List of all entities inside it, as well as a method to find food/stimulation/work. You could expand this out with find nearest methods that search multiple rooms.

    You can use the same list to simplify room generation. Each room simply grabs the entities it ccontains, and instantiated them in random positions.

    Make heavy use of retargetable animations and IK. Remember it's not the NPCs job to know how to use every entity, rather it's the entities job to let the NPC know how it should animate.
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    This is really helpful, thank you! Grabbing the entity list at runtime is trivial, and adds a lot of performance... pre-defining the entities a room contains and letting the room itself instantiate them is something I wanted to get working a while ago, my reasoning when I abandoned it was that it's simply too easy for rooms to organize themselves in a way that looks weird to the player, but is extremely difficult to detect and trap programmatically- things like vending machines or water coolers spawning in the middle of a cubicle farm, or objects spawning facing walls or doors. From there I initially jumped to the idea of pre-placing object spawn nodes in every possible location an object would look natural, but that was the same amount of effort as manually placing each entity, so hard-coding looked like the easiest medium. Is there a reasonably easy way I'm missing to implement procedural entity creation without the weirdness, or without a pre-existing system to analyze level geometry (as would be the case if I were building the entire level procedurally) would attempts to catch visual weirdness end up being a lot of work for questionable results?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I haven't tried to build something like this, but here are some ideas
    • Predefine a clear path through the room. Nothing can be placed on this path
    • Define if the room has a focus, (like a TV in a lounge, or table in a dinning room). Orientate everything towards the focus.
    • Define on each entity if it would be placed on the edge of a room or the center
    • Give every entity a priority. Instantiate in priority order.
    The more I type the harder this seems to be. Another option is a pseudo random room generator. Something half way between hard coding and a total random generator. Do this by defining a bunch of 'prefab' rooms. Set up different types of rooms (kitchen, office, bedroom). Define places (layouts) to have different item types (chair, table, vending machines). Then build a bunch of different chairs.

    Once you have everything pre built, you can randomise the type of room, the rotation of the room, the room layout, and the type of each item. Occasionally omit an item from a room. Do this well and the player will take a long time to realise they are walking into the same room over and over again.
     
  5. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    If anyone is looking for great written resource Artificial Intelligence for Games by Ian Millington is the best.

    It breaks down this kind of problem logically and then with source code.
     
  6. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Oh this looks excellent, thank you!

    BoredMormon, I think you're hitting the same thing I did- procedural placement shouldn't be that hard, but it has a way of coming up with all kinds of weird fringe cases that are incredibly tough to trap with a unified approach.
     
  7. adriandevera

    adriandevera

    Joined:
    May 18, 2013
    Posts:
    8