Search Unity

Making a "living" town through AI

Discussion in 'Game Design' started by Jaimi, Feb 16, 2015.

  1. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    4,715
    I'm considering writing my own town AI for my RPG. My thoughts are that I would create an "Area of interest" component, and the component might have various attributes. AI characters would also have various attributes, and I would use these attributes to direct characters to the areas of interest based on various criteria (how close they are to an area, if it's in line of sight, how much the attributes align, how often they have had that particular need/want filled lately).

    For example, an AI character might have the following attributes (among many)

    1. Calm (100)
    2. Beauty (75)
    3. Avarice (30)

    And there might be a pot of flowers nearby with "Calm = 50, Beauty=100". There might also be a bench by the river with "Calm=100", and a jewelry shop window with "Beauty = 50, Avarice= 100".

    The system would scan for things nearby - things that are closer would rate higher. Things that are in line-of-sight would rate highest, areas with too many characters there would rater lower, and then some formula would be used (along with some random modifiers) to find out where to send the character.

    When reaching the location, the character would play whatever sort of animation is appropriate for that area of interest - ie gazing through a window, tapping foot or clapping in time to music, sitting at a table and drinking, etc. While at that area of interest, the "need" for fulfilling that attribute would decrease, and when it is no longer needed, the ai character would then go on to looking for the next thing. Ai characters could be attracted to other ai characters (as they would be an area of interest also). At which point, the character could attempt to talk to the other, and if their interests matched, hold a conversation (using random topics of discussion) until they grow bored of each other and move on.

    Any thoughts on this? Seems like it could add a bit of life to an otherwise stale town or village...
     
    GarBenjamin likes this.
  2. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,865
    Sounds very "Sims" like. It's a great concept, however, the reason this isn't typically done is not because it CAN'T be done, but rather it normally shouldn't be done. You only have so much processing power to run your game, and having AI running all the time, whether the player is there or not will choke up your game pretty quick.

    So, unless it's the main focus of the game, it's probably not worth the cost.
     
  3. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    2,142
    heh, this sounds like something i'm planning, though what i intend is much more complex (imagine a survival quasi-rpg where npcs have the same stats as the player and must satisfy them through in-game means). khanstruct is right, though, you'd be using processing power for something the player might not notice. however, directx 12 may bring some advances in that area.
     
  4. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    It'd be nice for games to have some way to simulate ai inexpensively after reloading an area. Minecraft and starbound have mods that keep track of the last time crops were loaded and simulate the time that has passed once their chunk is reloaded. AI is obviously more complicated than plants, but it'd still be nice to see.

    If an ai begins a task that takes a few minutes, and you leave for a few minutes... on returning, the ai should finish. But what if you're gone for hours?
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    14,294
    My first thought upon seeing this thread was for Clock Town in Majora's Mask. It uses a system of schedules for different shops and NPCs rather than an actual AI. Someone mentioned earlier that a purely AI-based system might be heavy on resources as it need to be constantly running in the background.

    Using a combination of the two might produce acceptable results while maintaining good performance. You could have the NPCs, especially those that are important, start off at specific locations based on one or more fixed schedules and interact with potential AOIs along the way to their next scheduled location.

    This way when the player leaves the town the game can simply unload everything and start back up later at a different point in the schedule.
     
    Tomnnn likes this.
  6. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    I don't think you'll get an example better than Majora's Mask. Modern games might strive for some variation in the routines, but it was managed very well coming and going from town and seeing every single npc in the middle of some task.
     
    GarBenjamin likes this.
  7. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    Good example of the hybrid approach would be the town NPCs in Oblivion and Skyrim. They are deterministic enough to know exactly where an NPC is at any given moment, but aren't rigidly locked on rails when loaded.


    Unless your graphics are ASCII or there are only four NPCs, trying to run this kind of simulation on the fly is suicide. Maybe if you are getting hardcore into pathing/mapping solutions this can become viable, but without that, scaling this up is likely to be an unproductive nightmare.
     
  8. siblingrivalry

    siblingrivalry

    Joined:
    Nov 25, 2014
    Posts:
    384
    Go even older. Lure of the temptress gave similar feeling as majoras mask.

    makes you believe the world is alive at a low cost. I did similar thing years ago for simple isometric game.

    remember ultima online. They had amazing advanced ai for the wildlife and nature but no one noticed how clever it was and was just replaced with code to just simulate it and give the illusion. In the end it was better as the world was easier for the devs to control.
     
    GarBenjamin likes this.
  9. Elbon

    Elbon

    Joined:
    Jul 24, 2013
    Posts:
    8
    If you want to keep the performance cost low, one thing you could possibly try is to give locations their own schedules. For example, a forge could have instructions that say, "from 7:00 A.M. to 5:00 P.M, have the town blacksmith here pounding metal. If it isn't 7:00 A.M. to 5:00 P.M, don't spawn the blacksmith." If you get close enough to the forge, it could check its instructions and spawn the blacksmith if appropriate. Then, if you left, the forge and its instructions could be unloaded, saving you memory. And if you went close again, it would check its instructions again.

    Of course, that might make it tricky for an NPC to move from place to place when you can see them. You don't want the player to see them pop out of nowhere or vanish instantly because of the location schedules (unless of course your game has teleportation:D). One solution could be that once the NPC has been spawned, it follows its own schedule as long as the player is nearby. However, if the player gets far enough away, the NPC and its schedule would still be unloaded. You could have the buildings only process their instructions and spawn NPCs if they aren't already in existence. And of course, when you first spawn the NPCs, it would probably be good to make sure the player doesn't actually see them spawning.

    The advantage here is that you wouldn't have to process all the NPCs in the city at once. You can just focus on areas where the player actually is. Remember, if the player won't see it, you don't need to create it!

    Edit:
    Those are some interesting ideas you have about AI. I can picture an NPC with a 100 point love for sugar going straight for a cake in his location.:p
     
    Last edited: Feb 17, 2015
  10. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    2,142
    Additionally, you may want to look at S.T.A.L.K.E.R. That game has one of the most advanced AI systems out there, I'm told (I have yet to play any of them). Useful for anyone contemplating serious NPC AI.

    Relevant link: http://aigamedev.com/open/interviews/stalker-alife/
     
    Ryiah likes this.
  11. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    383
    One thing to keep in mind in regards to NPC AI performance...

    The average human reaction time is about 250ms... I guess I'm getting old because I tested at 280ms..

    That's 1/4 of a second.

    If humans take 1/4 of a second to react, there's no reason your AI has to react in 1/60th or even 1/30th of a second.

    Time slicing is of huge benefit... spread your calculations out over multiple frames... either by slicing up big calculations, or by only processing a fraction of your total NPC's each frame. If your NPC's get an update only 4 times a second, they are reacting as fast as humans.
     
  12. iDontLikeHipsters

    iDontLikeHipsters

    Joined:
    Mar 24, 2014
    Posts:
    24
    I think everyone in this thread is kind of underestimating modern PC's a bit... C'mon, guys, it's not 2005.


    Anyway, despite the power of modern PC's, you WILL need to optimize this system if you have any plans of implementing it. Keep in mind that it is not a programmer's job to emulate an environment, but rather to give the illusion of said environment.


    When the NPC is not reacting to any particular event? The player doesn't care what he's thinking. No need to call for an update on their emotions or anything. However, you can make characters analyze their environment before they perform certain events.


    The way you handle this on such a large scale, like in RPG's, you would use a method that I like to call: Pseudo-Processing. The concept is that only NPC's within a certain radius should process, but (depending on the direction you plan to take your game), they will all stay in memory, and will try their best to emulate a schedule based on the time passed and the last emotional state they were in.


    Hope that helps.
     
  13. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    2,829
    If you can keep the "Search for next action" script fast, then it should be pretty easy. Once an NPC has an action, they can just do it and that's super light weight, right? So the intensive stuff is the searching. Most NPCs will only do that once every 10-15 minutes, if even?

    Keeping it relatively simplistic may be the best approach, but I think it could create some believable scenarios.

    In my head I keep thinking of an NPC the player needs to find, and perhaps as the NPC walks by other NPCs that they "know", they pass info along. So if you talk to the shop keeper, they can say "Oh, She was looking to relax, she was a bit stressed out", which implies she's looking for a "calm" spot, so the player has to figure out which calm spot she may have gone to. A time-sensitive quest like this could be interesting, making the player think about the "personality" of the NPC and their daily activities -- so that the player knows in the afternoon usually the NPC is by the water, so if she's looking for a calm spot, she'll likely sit on the edge of the dock, or something like that.