1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Artificial Unintelligence

Discussion in 'Civ5 - Mod Components' started by Delnar_Ersike, Oct 23, 2014.

  1. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    I just had a look at the AI's great works swapping logic on a whim, and holy fish on a stick, the code looks like it's been deliberately written to run as slowly as possible. If you have the time, take a look at CvPlayerCulture::MoveWorks(), CvPlayerCulture::ThemeBuilding(), CvPlayerCulture::ThemeEqualArtArtifact(), CvPlayerCulture::FillBuilding() and the last three lines of CvPlayerCulture:: DoSwapGreatWorks() to bask in the glory of multiple excessive loops, already-processed items of a vector not being removed before the vector is processed a second or third time, iterator for() loops that use iterator++ instead of ++iterator (the latter is supposedly circa 40% faster than the former), arbitrary mixing of signed and unsigned integers, excessive use of sequential search, flat-out useless loops (thinking about the last few lines of MoveWorks() specifically), and other marvelous examples of badly written code. The cherry on top is that some of the code written in those functions doesn't even do what Firaxis' comments say it should (eg. the last few lines of MoveWorks() are supposed to first fill non-endangered buildings with unthemed works, and then fill endangered buildings with unthemed works, but instead, the game just attempts to fill non-endangered buildings with works twice).

    Heh, thanks, guess that's one more notch on my invisible barrel. I measure the success of the mod in how many of its users it can force to lower their difficulty without me having to resort to blatant AI cheating. :D
     
  2. notque

    notque Artificially Intelligent

    Joined:
    Nov 13, 2005
    Messages:
    1,654
    I used to write Civ 4 AI mods, and did some AI for Fall From Heaven/Fall Further, so I'm aware of how difficult it can be. Thanks for providing a challenge.

    I'm going to give it several more iterations hoping to get some success on Deity.
     
  3. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    18,076
    Gender:
    Male
    Location:
    Little Rock
    Hehe, yeah, that was my reaction as well once I started looking at it. They probably need to be re-written entirely, but that seems like a chore. Still, it'd probably be a big performance enhancement to do so, as those functions are called a magnitude more than any function in the game (aside from pathfinding stuff).

    Edit: also, I'll note that your reworked disband logic for the AI may be a bit too aggressive: the AI was disbanding units as soon as they were produced in a game I was observing, and the AI log was filled with deletions (far more than normal) even if the civs were not in deficit.
     
  4. HorseshoeHermit

    HorseshoeHermit 20% accurate as usual, Morty

    Joined:
    Apr 5, 2013
    Messages:
    1,310
    Location:
    Canada
    I have worked on -small- mod projects for Civ5 , and seriously

    I kid you not

    I have seriously considered five times to go and refactor the entire damn source code just to make modding easier. And not even in an altruistic way.

    And ummm... wow, Delnar. I'm. Speechless. Let me go download CPU-Z and I'll... pm you... the info?
     
  5. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    Hmm, I added in an extra worker scrap() option in HomelandAI that simply scraps idle workers instead of relying on EconomicAI's DisbandExtraWorker() stuff, I may need to ease off even more on its conditionals.

    A complete refactor is not really a good idea unfortunately. Refactoring your own, one-man project is one thing, but refactoring code that's been worked on by more than 30-40 people would take forever. Plus, some of the code (mainly the stuff that's been there since Civ4 or before) is quite elegant, so there's usually no need to touch those bits. Heck, it's sometimes quite hilarious to see two, functionally almost-identical methods right next to each other and instantly be able to pick out which one was added in for Civ5 (it's the one with the slightly worse or wonkily implemented code, bonus points if the formatting is inconsistent as well).

    Yeah, that'll work. The trick's probably in the project's compiler settings, so I don't actually need to sift through all the code just to make things compatible for older CPU architectures: even if removing some of the defines and compiler flags breaks the code, the compiler will error out at the broken lines anyway.
     
  6. Tiramisu

    Tiramisu Warlord

    Joined:
    Aug 16, 2013
    Messages:
    141
    Location:
    Leonberg (BW, Germany)
    Can I have the C++ datas for the dll of this mod please? I want to learn something about the AI programming.
     
  7. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    Well, the mod's source code is available on its GitHub page (linked to in the first post), but it's a rather poor source for learning about AI programming, as it's more intended for people who either already know a fair bit about AI programming and are curious about Firaxis' and/or my implementations, or who wish to merge my code with theirs. If you already have a programming background, I highly recommend you start with Amit Patel's (of Stanford University and Red Blob Games) list of game programming articles, specifically the Shortest Paths and Artificial Intelligence ones. Heck, even if you don't know much about programming, Amit Patel's stuff is quite excellent, but try to take baby steps and don't be overambitious, lest you give up prematurely because you became too frustrated after biting off more than you could chew.
     
  8. Tiramisu

    Tiramisu Warlord

    Joined:
    Aug 16, 2013
    Messages:
    141
    Location:
    Leonberg (BW, Germany)
    Oh, thanks! At first I was not sure where to find your changes in this huge amount of source codes. :eek: But now I see it.
    For now my biggest problem is to understand the connections between the numerous variables and functions. Of course I do not want to change the whole AI for the first time, but as soon as I can find and understand a specific AI behaviour in the code (e.g. its fighting with naval units) I could try small changes. Furthermore the source code could help me to understand some cryptic parameters in the XML files.
    Unfortunately I could not find a very helpful tutorial on this issue yet. :cry:
     
  9. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    For those "cryptic" XML parameters, just search for their names in the visual studio solution and you'll be able to see where they fit in.
    As for understanding the DLL's code, the reason you couldn't find a very helpful tutorial is because there aren't any (to my understanding). I had to spend about 2-3 months just reading code before I really got into the swing of things, and I'm still finding out new things about the DLL to this day (ca. 6 months later).
    Just take your time and be cautious. Make good use of the "Jump to Definition" and "Find all References" menu items in Visual Studio, since those are your only leads; the code is so intertwined that you can literally start anywhere and make your way to almost all the AI functions via those two. If you make any minor modifications, try to wrap them in preprocessor defines so that you can keep track of what you changed and can easily disable any of your modifications should they cause trouble.


    The reason I've slowed down on my GitHub commits is because the DangerPlots restructuring turned out to be a much bigger task than I originally imagined: I had to add in all sorts of extra helper functions and the like, and it's still inaccurate for things like enemy bombers' danger values when they can be intercepted (though it should be spot-on for everything else). Plus I haven't even tested whether it works properly or not, and I'm embarrassed to admit that I don't even know how to properly profile its performance should it work (I've been relying on game-generated performance log files so far).
    On the other hand, the pathfinder path limiter took maybe 2 hours to implement from scratch, of which 10 minutes were spent actually adding in the new stuff, while 110 minutes were spent refactoring all the pathfinder calls to have them use the path limiter.

    Oh yeah, and I spent a few hours yesterday exploiting the current Beyond Earth Free Weekend by uploading Artificial Unintelligence Lite's Beyond Earth port to the Steam Workshop; turns out the Free Weekend also gave access to the game's SDK.
     
  10. Killcycle

    Killcycle Chieftain

    Joined:
    Sep 11, 2013
    Messages:
    3
    Have you done any compatibility testing with CEP? I believe it modifies AI to some extent as well.
     
  11. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    CEP's AI stuff seems very much about tweaking flavor values, so pretty much the stuff I left out in the mod's lite version; however, without access to their source code, I cannot tell for certain if and how well my DLL would work with their changes. The other problem is that I actually tinker with flavor values on the DLL side, so eg. if they've inflated the science flavor value of all science buildings, my tech goal-based science flavor multiplier's (it multiplies science flavor of buildings and policies based if the player does not have some key techs for their chosen Grand Strategy, eg. Archaeology and The Internet for culture, Globalization for diplomatic) effects will be compounded.

    If their AI stuff is limited to flavor modifications though, then yes, their tweaks should be compatible with this mod. This is of course until I start overhauling the flavor system the way I'm overhauling danger plots right now (which is actually coming along pretty well, though it does tend to crash games quite often, hence why I haven't committed it to the mod's Experimental branch), but that's really far off.
     
  12. Killcycle

    Killcycle Chieftain

    Joined:
    Sep 11, 2013
    Messages:
    3
    Yeah, that makes sense. I believe the author has also finished with the project, but I found what I think is the main repo: https://github.com/Thalassicus/cep-bnw. I've played a ways (about 100 turns) through a game with both your mod and CEP enabled, and while, as you point out, there are probably hidden consequences, there were no obvious bugs or quirks I found.

    Anyway, thanks for you reply and your mod! I've started playing again after grabbing it, and I can really feel the difference; that, and it's fun reading your commentary on the firaxis codebase. :goodjob:
     
  13. oedali

    oedali King

    Joined:
    Apr 6, 2007
    Messages:
    906
    Gender:
    Male
    Location:
    Washington, DC
    SAve game file re: AI behavior posted in Steam workshop, AI not capturing empty barb camp w/ melee
     
  14. Typhlomence

    Typhlomence Magical Tomomo

    Joined:
    Mar 19, 2014
    Messages:
    394
    Location:
    Brisbane, Australia
    I was just wondering if it would be possible to get a direct link to the Lite version? Steam Workshop is playing up for me and I can't get a copy of the latest Lite version. Unless I can just copy the Full version to another folder and remove CvGameCore_Expansion2.dll?
     
  15. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    I have just uploaded a copy of the Lite version to the CivFanatics database, link is in the original post.

    I would not recommend creating an ad-hoc lite version of the mod by removing CvGameCore_Expansion2.dll from the full version: the XML changes in the Full version assume that you're using the mod's modified DLL, while the XML changes in the Lite version assume that you're using an unmodified DLL (unmodified in terms of how it handles the XML values changed by the mod). For example, the flavor values of paratroopers and XCOM squads has been brought in line with other units in the Full version because the AI should technically be able to handle units that can paradrop just like any other unit, while they remain low in the Lite version (since the unmodified AI basically can only use paratroopers for pillaging isolated tiles).
     
  16. Typhlomence

    Typhlomence Magical Tomomo

    Joined:
    Mar 19, 2014
    Messages:
    394
    Location:
    Brisbane, Australia
    I thought that might be the case, hence why I asked you for confirmation first. It's moot now, though, since you've posted a direct link. Thanks a lot! :)
     
  17. notque

    notque Artificially Intelligent

    Joined:
    Nov 13, 2005
    Messages:
    1,654
    Game is slowing down to a crawl as the game goes on. Anyway to help diagnose what it is that is taking so much time?
     
  18. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    Based on my work on v10, the main culprit is most likely DangerPlots: pre-v10, the AI calculates the full IgnoreUnits pathfinder information of all enemy units it can see and sums the strength values of those units to an integer "Danger" value for each plot (it also divides the strength value by 2 or more if the unit cannot reach the plot in one turn). Despite the fact that paths are allowed to be reused, a lot of visible enemy units with high (3+) movement speeds will put a fairly large strain on the AI, especially since each player runs through the same pathfinder calls even if the location of the enemy unit has not changed.
    Starting in v10, DangerPlots is used for a different purpose entirely which makes its generation faster but its use slower (in short, instead of storing a sum of combat strengths, it simply populates a set of pointer vectors with the units that could attack the tile the following turn, and when accessed, the "danger" info calculates and returns the maximum amount of damage a unit or city could take next turn if it were located on the tile being checked).

    The other possible culprits are the sorting and swapping of Great Works (as discussed before with LoneGazebo, that bit of code looks like it was written to run as slow as possible), as well as the fact that the AI will often attempt to generate paths for units 10 turns away from a target, only to throw the result out when the path building completes because it is too long.
    I've already half-restructured/rewrote the Great Works swapping stuff for v10, and though I've yet to test how well it works, it should definitely run faster than Firaxis' one (short version: instead of constantly maintaining and copying vectors of Great Works, I just maintain one vector and handle all of its manipulations through pointers and pointers of pointers).
    As for the turn limits thing, my pathfinder turn limiter for v10 seems to be working fairly well, though I still need to do some extended testing. The pathfinder turn limiter is simply an integer value set when the pathfinder data is set, and it automatically invalidates paths with turn costs larger than it. Though not very useful for human players' unit pathfinding, it should help with AI pathfinding performance when the AI simply wants to check whether a unit is within a turn's range of a target, since it doesn't need to build a complete path to the target before realizing the path is too long. The only time this should hurt performance is when the turn limiter is set to a high amount of turns, e.g. 3 (since then there could be cases when it would be faster to calculate the shortest path and invalidate it than to keep invalidating paths at the last tile due to length, forcing the pathfinder to keep building paths until it runs out of options), but the AI never actually does anything that would require that.
     
  19. notque

    notque Artificially Intelligent

    Joined:
    Nov 13, 2005
    Messages:
    1,654
    Sounds good. One other thing, in my current game I've had Mongolia massed outside of my boarder on the coast (with units also on the other side roaming around) for a very very long time now. They are mostly melee units.

    No DOW has happened, although it clearly should have. I now also have China starting to mass in the ocean on my border, and they are both just moving around as if they'd DOW provided some "action" that is required occurred, but it just isn't happening.

    Any thoughts?
     
  20. notque

    notque Artificially Intelligent

    Joined:
    Nov 13, 2005
    Messages:
    1,654
    Additional detail, decided to declare war and just destroy their units, and there were an unendless supply of melee units in the ocean.

    I found a 2 tile area with a barbarian camp, unit spawned. and another unit in the 2nd tile. They sent over like 60+ melee units to try and clear the camp. They did not have access to the Ocean via a city, so were unable to send ships.

    Just bizarre.
     

Share This Page