Search Unity

Potential Capabilities of AI in Unity

Discussion in 'Scripting' started by Don-Megel, Dec 13, 2016.

  1. Don-Megel


    Dec 4, 2016
    This is not a ‘Can Unity do this?’ post, I know that with enough effort Unity can do anything. My question is, ‘is this practical given a small team/budget?’

    I am looking at creating a turn based strategy game. Ideally, I would like the map to have around 3,000 tiles (Spaces for armies to move on). There are assets that support 10,000 tiles out of the box so I know Unity can do that part. What I am concerned with is the AI. With six factions, armies, diplomacy, and 3,000 tiles to move armies around on, would this be too taxing on the AI? I know many larger games (Hearts of Iron, Crusader Kings) have large numbers of tiles and a great many things for the AI to consider but these are produced by teams with more resources than are available to me.

    Would a strategic AI that can handle that many tiles be a reasonable and obtainable goal? Or should I reduce the number of tiles the map is composed of?

    Thank you in advance.
  2. Zaflis


    May 26, 2014
    First impression is that it may be a tough challenge to take. Civ V guys with bigger budget managed to make a laggy game with much less units in game. But they made the AI very complex and possibly using badly optimized data structures.

    "Strategic AI" is difficult to put into words. It means different thing for every developer and game. Only you can know your own limitations and the game's requirements.
  3. KelsoMRK


    Jul 18, 2010
    You're assuming the AI calculations are what made the performance poor when it could be any number of things.

    I'm not sure why you're trying the number of tiles on a map to how performant the AI is. Why does the AI need to constantly evaluate the entire map? What data from each tile is it even evaluating? What decisions is it making around those tiles? You've also listed a number of other variables in regards to AI - factions, armies, diplomacy - without outlining what the AI needs to take into consideration regarding those things.

    You've got a distinct advantage in turn-based games because the AI doesn't need to calculate it's goals in real time. A small pause (assuming it doesn't actually cause a hitch in your framerate) would actually give the illusion that the AI was thinking like a regular person would.

    Ultimately - you've more or less said "I know Unity can do this but can I do it?" which is more-or-less akin to "I've seen people make 6 course Christmas dinners before, do you think I could do it given the size of my kitchen?" Depends on how good a cook you are and how well you prep. :)
    Kiwasi likes this.
  4. ericbegue


    May 31, 2013
    This is a very broad and open question...

    3000 tiles is not that big. That would be a map about 55*55 tiles. But I don't think the map size should be the only factor to consider to estimate how much CPU the AI would require. You would need to consider other aspects of the AI.
    - What would be the maximum number of units?
    - How complex is the AI per unit?

    Also, since Unity does not offer AI features natively, you can completely factor it out. Rather, you would need to consider the capabilities of C# or more specifically the Mono implementation of the .Net platform.

    The platform/devices your are targeting would have a huge impact about scaling the AI. Will your game run on PC, Mobile, Web or all.

    It's very hard to answer this question before hand. I guess the best you could do is to start making some prototypes of your game (or part of it) and benchmark them. That would give you some hard numbers and measures about what could be realized.
    Last edited: Dec 13, 2016
  5. Don-Megel


    Dec 4, 2016
    Thank you so much for your responses. Let me elaborate a little:

    Each tile would represent some land which would have a resource, victory point, and terrain value. So a tile might be worth 3 money and is easy to move through, while another tile might be worth 0 money and be very hard to move through. Armies move from one tile to another, defending important tiles, engaging or checking enemy armies, etc. That's pretty standard stuff. Now the leader of each army is a character with loyalty, vices and virtues. The six factions must manage the leaders of these armies, replacing the less loyal ones, or taking steps to increase their loyalty. Characters also control land within the factions, but this only effects income and loyalty. Finally, each faction must deal with the other factions, making alliances and other diplomatic treatise.

    I come from the art world and before I recruited a programmer for my project I wanted to see if it was even feasible. If not, I would reduce some of the complexity in my design. If the bottleneck is C# then I would have the same question about feasibility but directed towards C# rather than Unity.

    When I say feasible, I suppose I mean to ask 'what would this involve?' How much time and resources, assuming a competent programmer, would I be looking at?

    Development would be for PC only, none of this console/mobile business.
  6. aer0ace


    May 11, 2012
    Pre-last post response:

    Handling AI Calculations will depend on more than the number of tiles on the map. The time and space complexity of calculating AI is more influenced by the properties and attributes of units and tiles more than the size of the map. For example, I'm making a turn-based tactical game similar to Advance Wars. You have to consider terrain type, terrain defense, travel characteristics over that terrain (or not), attackability, mobile range, attack range, fog of war, unit cost, unit health, unit ammo, faction (enemy or ally), and whatever other features you'll want in your game. You can consider caching some of this info in the background to save on calc time between turns, at the expense of RAM. And, it will also depend on how long you would like your player to wait until the turn is calculated. Maybe you wouldn't want your player to wait more than 10 seconds to resolve a turn. Then set that as your benchmark, and see how much calculation you can get in that time. And continue to optimize the AI to fit within that benchmark. Every one of those subsystems will make your AI more complex, and will obviously take longer to implement, tune, and polish, which quickly becomes an issue of how much time you want to devote to make it fun.

    Post-last post response:

    Leaders, Loyalty, vices and virtues, alliances, diplomacy sound like more attributes to check against in AI. As I stated above, all of this will be factored into calculating the AI, and my general rule, for a small team, is that each feature you add increases the complexity of the implementation exponentially. So, as you get more complex, your programmers will have to prepare for the long haul, as fixing bugs and getting large test coverage for all the different possibilities and cases can get out of control easily. This also means you'll need to eventually recruit QA to test these cases.

    "What would this involve?"

    There's still not enough of a game design to throw out a proper estimate for one AI engineer to implement all that in a game. I could say that it could take an AI engineer 1 or more year(s) to do all that, but I couldn't tell you how accurate that is.

    The AI can involve many different sorts of algorithms. Some keywords are A* pathfinding, quadtrees, influence maps, decision trees, behavior trees, utility theory, fuzzy logic, etc.

    Your AI engineer may implement some or all of these. He/she would have to then adapt those systems to the game logic itself, and the game itself will most likely have its own level of AI:
    For example:
    Tactical AI on the unit level
    Operational AI on the war front
    Strategic AI for the faction/empire
    Grand Stategic AI for long term goals for winning

    And once this gets to a satisfactory engineering implementation, you still have to make sure the game is actually fun! You may need to remove some sorts of features of the game that will affect the AI, which means you'll have to go in and either gut them or change them, which is more time.

    Let's assume you get to a point that the AI systems work and the game is fun. But then you realize that it's slow as hell. This is where your AI engineer will have to use a Profiler to investigate where the slow downs occur. Suppose you want 10 seconds max deliberation from your AI, and currently it runs at 35 seconds on the target machine. The AI engineer will determine where the slowdowns are in the profiler.

    The engineer then investigates solutions for reducing this time to the target.
    He/she will refactor those subsystems, implementing more code to help minimize calculation time.
    One of those solutions, you may find that C# runs too slowly for doing mass calculations for one system. And the solution may be to have those calcs done in native code. The AI engineer will then standup a native dll that contains all the AI calcs, and hook it into the game.

    Rinse and repeat until the game is fun and runs well!

    So, all of that, you can set aside a certain amount of time for. In reality, reducing the number of features helps tremendously, especially since you personally don't have any benchmarks for how long it will take.
    Of course, your AI engineer will know better of the time and effort it will take to complete (or will he/she?). Typically, if you get an estimate from them, double the time, and that's how long it will take, given the current feature set (That means, without changing the feature set later on, which almost always happens anyway).

    Take a look at the Shattered Throne blog by Checkmark games. He has a series of AI videos about his implementation.

    Good luck!
    Don-Megel and ericbegue like this.
  7. radicalEd


    Mar 19, 2016
    It would be completely possible to make a challenging "AI" (real ai is off the table, obviously, as none exist), but it wouldn't be easy to make them behave very "human".

    Assuming there aren't too many super special unit movements or ways to "checkmate" an opponent like chess, it wouldn't be that hard to break down AI behavior into a few basic behaviors (all would be performed better or worse based on the "difficulty" setting);
    1.) Maximize resource gain (if resources exist).
    2.) Make diplomatic decisions 100% based off of power, with a few random biases (A given AI might like power, fishing capabilities, and catholicism)
    3.) When mobilizing armies, follow a few preset configurations depending on the size / scale of your opponents (hard coded configurations that are selected based on stuff like your size vs the enemy, what troops the ai has, etc)

    A lot of your "AI" would be pretty similar to a finite state machine, which can be difficult to nail just right, but it's totally possible.
  8. Don-Megel


    Dec 4, 2016
    Everyone, thank you so much for your help. You have answered my question to my satisfaction.
  9. Kiwasi


    Dec 5, 2013
    AI wars was made in Unity. It can throw a couple million Units at you if you mess things up. Its certainly doable.