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. HorseshoeHermit

    HorseshoeHermit 20% accurate as usual, Morty

    Joined:
    Apr 5, 2013
    Messages:
    1,310
    Location:
    Canada
    I played a game on Emperor with this mod , and I was very excited and entertained by the challenge the AIs gave me. Attila eliminated Haile Selassie in the classical era, and I shared a landmass with Washington, who teched to his UU and gave me quite a scare when I still had Pikemen. He then fought a sneak attack where he didn't suicide his units, but my tech slingshotted to GWI and I beat him out.

    Two of four AIs actually took Rationalism, and Attila is executing an economic vic Grand Strategy really hard.
    So impressed.

    Thank you.
     
  2. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    Heh, thanks for the compliments. I've still got a long way to go, though, and I'm often running into the problem of having to choose between either an accurate but slow implementation or an inaccurate but still fast enough one. If anyone knows someone who's got a bit of practice with OpenCL, I'm curious if I can actually port some of the more computationally expensive AI routines to OpenCL (eg. all the pathfinder routines) and let the GPU do some heavy lifting.

    I've also made quite a bit of progress with my GitHub commits; most of the stuff I improved upon from Smart AI has already been uploaded, and while some improvements are quite modest (eg. shifting the pathfinder call for CvUnit::GetMovablePlotListOpt() to after the CanEverRangeStrikeAtFromPlot() check to avoid pointless pathfinder calls), others are actually quite substantial (eg. having Smart AI's order purchasing algorithm in CvEconomicAI::DoHurry() calculate possible purchases for all orders in the city's production queue, not just the first one, so the AI does not only purchase stuff that it's currently producing).

    Note that a few of my recent changes (for v9) do actually make some ever-so-slight non-AI modifications, primarily caused by the starting plot calculating algorithm using my modified plot valuing logic: plot yields are calculated assuming the player will own the plot if it's currently an unowned plot, and the strategic value of a plot involves a much more thorough calculation. The selection logic is actually better than before (eg. the game actually knows when a tile is a chokepoint instead of blindly saying any mountain within 2 tiles of a city could be a chokepoint), but it's still something that has gameplay effects on humans, so I thought I'd give a heads up.
    All related modifications have been uploaded to GitHub: CvPlot.cpp, CvSiteEvaluationClasses.cpp, and their respective headers are the main files involved. The chokepoint bit is in CvSiteEvaluationClasses.cpp, under the function CvCitySiteEvaluator::ComputeStrategicValue(), and the part I modified is wrapped in preprocessor directives towards the beginning of that function.

    Exams are coming up though, so don't expect too much progress. Thankfully I can always just set up a test game and have it churn away for a few hours while I go off and revise...
     
  3. slowcar

    slowcar King

    Joined:
    Jun 19, 2006
    Messages:
    819
    Location:
    hamburg, germany
    I saw some strange barbarian behavior on my playtests with this mod, and others on the German forum mentioned the same:
    Encampments are often empty, with the unit that should be guarding it roaming around it.
    I had several "clear encampment" quests i was able to do without fighting a single barbarian this way.
    It looks for me as an unintended consequence to the move-and-shoot improvement, maybe barbs protecting their home see an interesting target, pursue it and switch to a different AI behavior (i.e. no longer guarding the camp)?
     
  4. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    Nope, it's actually a bug from the original game that doesn't appear in-game because other unit management code bugs cover it up. I've fixed this for v9, no need to worry.

    Also, I think I've found out why disabling time victory messes up the AI and I've hopefully fixed the issue, but I need to do a bit more testing first. Right now, I'm getting a bit carried away with trying to sort out AI pathfinder move flags; in the unmodded game, these were only actually activated for automated units (not AI units), probably because some of them result in pathing that's FUBAR, so Firaxis decided it's better to disable the code than actually fix it. *sigh*

    The GitHub page is now fully populated with my modifications, so feel free to start poring over my code. All modifications are wrapped in preprocessor directives, and an index of all said preprocessor directives is located in AuI.h.
     
  5. ThorHammerz

    ThorHammerz zzz

    Joined:
    Jul 31, 2014
    Messages:
    836
    Many thanks Delnar!
     
  6. HorseshoeHermit

    HorseshoeHermit 20% accurate as usual, Morty

    Joined:
    Apr 5, 2013
    Messages:
    1,310
    Location:
    Canada
    v.8 killed my savegame. :( Can has rollback pls?
     
  7. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    Sorry, v7 wasn't uploaded fully to GitHub, and I deleted the only archive of it that I had a week after I released v8. v9 should be savegame compatible with v8 though, so you need not worry that the same thing will happen again for v9 (v10, on the other, probably will not be savegame compatible, as there is a ton of stuff I want to add that I cannot do without breaking savegame compatibility).
     
  8. HorseshoeHermit

    HorseshoeHermit 20% accurate as usual, Morty

    Joined:
    Apr 5, 2013
    Messages:
    1,310
    Location:
    Canada
    *sigh of acceptance*
    Consider versioning with some info on planned save compatibility. Y'know, do it like Sun updates Java, with 1.1s and 2.5s, and big updates going up a whole number. I know that Civ mods have integer version numbers but I mean you could put a version in the title or description or something.
     
  9. HorseshoeHermit

    HorseshoeHermit 20% accurate as usual, Morty

    Joined:
    Apr 5, 2013
    Messages:
    1,310
    Location:
    Canada
    So yeah, again, great stuff.

    Can you think of why this mod would be incompatible with Enhanced User Interface? You don't change -anything- about display info, right? And EUI is a silent DLC install that doesn't affect saved games. :confused:
     
  10. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    I honestly have no idea why; I haven't touched the notification or UI systems at all. Plus, for some reason, the minidump-generating code doesn't actually create minidumps that I can debug, so I this is something I'll have to troubleshoot myself.

    EDIT: Did some testing, Enhanced User Interface is fully compatible with this mod. Whatever's causing incompatibility problems, it definitely isn't EUI.

    Exams are over, so I've updated the GitHub page with a new batch of commits. I'm on the "fix bugs and optimize" stage of developing v9, which ranges from optimizing Firaxis' poorly structured loops and plot distance checks to trying to troubleshoot possible holes in my own code. Hopefully, I'll get it released this month.

    As for versioning for saved game compatibility, so much of my planned changes involve storing stuff that if I'd were to use the tens digit place for savegame-breaking updates, I'd easily climb into the 1000's within a few updates. I think I'll instead settle for having odd version numbered updates being savegame-compatible, with even-numbered versions having no such guarantee.
     
  11. oedali

    oedali King

    Joined:
    Apr 6, 2007
    Messages:
    906
    Gender:
    Male
    Location:
    Washington, DC
    Attached save game for issue mentioned on Steam workshop
     
  12. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    18,076
    Gender:
    Male
    Location:
    Little Rock
    Delnar,

    Any interest in bringing your work into the fold of the Community Patch? We've got quite a bit of AI dll work already done (from myself, Whoward, Ninaroku, Ilteroi, and others), which may or may not overlap with any plans you have for the future. If so, let me know and I'll see about doing an initial merge of your basecode with the CPs (if/when you feel your basecode is ready for such a merge).

    G
     
  13. Natan35

    Natan35 Mayor of St. Natansburg

    Joined:
    Jul 2, 2014
    Messages:
    5,139
    Location:
    Ashdod,israel
    Since i don't really understand what exactly this mod is doing, I'm asking :
    Does this mod will make the AI expand in more consistent way?(i see many civs that would found 7 cities in 150 turns in 1 game, and 3 cities in 200 turns in the next one, which is ridicules.)
     
  14. HorseshoeHermit

    HorseshoeHermit 20% accurate as usual, Morty

    Joined:
    Apr 5, 2013
    Messages:
    1,310
    Location:
    Canada
    and I definitely can't get the two to work together.

    :(:(:(:(:(

    They work separately. This is most curious.
    You're on updated architecture, right? I'm on an old machine which apparently, some mods dislike, even though I can't say specifically why that is. It's not performance - whoward said something about "the microsoft linker" . :|
     
  15. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    Feel free to look and borrow from my code, I've already split off my development branch (where I'll be uploading bits and pieces for the next version of the mod) from the master branch (the code for the current version of the mod). However, I doubt merging will be an easy process: a lot of my code relies on small but wide-sweeping changes (eg. changing the type of variables used to track scores or flavors internally from int to double, replacing every call for the active grand strategy with the grand strategy ratio equivalent), the stuff that doesn't work well with Whoward's preprocessor defines encapsulation style, so you'll probably have to merge most stuff by hand. In addition, almost all of Ninakoru's original improvements (Smart AI) have either been enhanced or rebuilt (examples: the DoHurry() function for purchasing buildings with gold can now purchase buildings/units that the AI has queued up in addition to the ones it's currently building and also has a faith-based equivalent, the GetMovablePlotListOpt() function now works with raw pathfinder data instead of TurnsToReachTarget() and can also calculate ideal plots for feigned/parthian retreats), so blindly merging my enhanced versions of those functions with Ninakoru's originals will undoubtedly cause problems.
    Oh yeah, and I am also still learning as I go along, so my earlier work may contain unoptimized and/or superfluous bits of code.

    Still, there definitely are a lot of enhancements that I think would be incredibly easy to merge: the non-AI related stuff listed at the top of AuI.h should mostly fall into this category, as well as most non-Tactical and non-Homeland alterations marked as fixes (their defines' names all have the word FIX in them right after AUI_<scope text>). Ninakoru's original improvements are almost all still recognizable (I'll admit that I've turned the ExecuteAttack() stuff into a bit of a mess though), so while merging won't be automatic, it won't be as painful as some of the other stuff I've changed (trying to read my belief scoring changes in their current form is like asking for a stroke); I've marked the defines whose enhancements originally came from Ninakoru's code as well, so searching for Ninakoru in AuI.h will highlight all relevant defines.

    I'd also like to take a look at the community patch's DLL source code to see if I can merge some of its AI and performance improvements with this mod. I know it's all up on GitHub, but I've simply been so preoccupied with getting my current plans to work and Ninakoru's functions to not produce undesired behavior (for example, the original move and shoot functions mess up royally if the ranged unit needs to set up before attacking) that I haven't had a chance to delve into the community patch's code. Plus running test games is quite time-consuming on my roughly 7-year-old rig, and it can often take 4-5 hours of running AI test games before I realize something's wrong and need to change a single line of code. Even now I'm wondering which I should get to work on first for v10: my planned pathfinder alteration that should drastically improve the performance of those turns-to-target checks, my planned overhaul of danger plots so the AI can use danger plot data as reliable tools instead of murky "avoid these tiles"-ish maps, or having a look at the community patch stuff.

    It really, really depends: there are a lot of little things responsible for the way the AI expands, including the AI's starting position, the map's size, the result of the random personality rolls on Expansion and Growth flavors for each AI player at the start of the game (other flavors only matter indirectly), whether the AI ends up picking up the policy that speeds up settler production (since production time is a huge factor when the AI is determining what to build next), and others.
    That said, you do encounter extreme personalities less often with my mod, thanks primarily to having swapped out the flat-distribution random number generator for a binomial-distribution one for all those personality rolls, as well as having implemented an overflow roll system for said personality rolls (short version: if a personality roll would take the AI out of the allowed bounds for personality values, the game keeps rerolling the parameter by however much it went over/under the limit until there is no more overflow with which to reroll). The former results in AI personalities usually being somewhere around their initial/default value, while the latter ensures that AIs with very low or very high initial/default values don't end up stuck with the lowest or highest allowed value.

    What's your exact processor type and model? I'll look up its peculiarities and see if I can compile a DLL specifically for your machine.
     
  16. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    18,076
    Gender:
    Male
    Location:
    Little Rock
    Sounds great. I made an initial merge just for fun and while I only merged around 75%, it was enough to test and see that a merge may eventually work. There's definitely some promise to merging the mods at some point, especially if it can prevent those of us working on the AI from doing the same tasks over again. I'd say we should try to get a fully-operational merge going sooner than later, if only because it'll make both our jobs easier.

    Cheers,
    G
     
  17. oedali

    oedali King

    Joined:
    Apr 6, 2007
    Messages:
    906
    Gender:
    Male
    Location:
    Washington, DC
    Attached Dump file re: the crash after v9 mini-update
     
  18. Delnar_Ersike

    Delnar_Ersike Prince

    Joined:
    Jan 1, 2007
    Messages:
    497
    Location:
    localhost
    I've uploaded a minor update that should hopefully fix that specific crash.

    There will be certain things that will always need to be merged by hand, especially once I start altering improvements originally borrowed from the community patch's code. The quickest example I can think of is that the modification for the pathfinder to consider mountain tiles in TradeRouteLandValid() is implemented incorrectly in the community patch (you've removed the isImpassable() check and left the isMountain() check instead of the other way around); while this is definitely something you can update/fix/merge in a matter of seconds, other, larger changes/improvements I'll come up with down the line may take much longer.
    The worst bits though will be the ones where both someone on the Community Patch team and I have tried fixing a piece of AI logic and ended up with two, different, incompatible solutions that each have their own upsides and downsides.

    Nevertheless, if you let me know which sections you plan on or have trouble with merging, and I'll try to clean those sections up to make merging as easy as possible.
    Also, if you want a savegame-compatible pathfinder performance improvement, keep an eye on the mod's experimental branch: it turns out that the expensive canEnterTerrain() function gets called two or three extra times per node depending on the MoveFlag, and I'm currently testing a (slightly clumsy, but still functional) addition that should remove all those extra calls.
    Oh, and I'm also going back and removing/rewriting some of my earlier enhancements, too. For example, MSVC doesn't distinguish between long double and double, so my little StopWatch precision "boost" is almost pointless; the only useful bit is that I subtract the old time value from the new one before casting the result as a double (from long long), whereas Firaxis' code casts both values as doubles before performing the subtraction.

    The two big things I'm planning on working on for v10 are: overhauling CvDangerPlots in a big way (eg. splitting the danger sum up into individual contributions per enemy unit via an int FFastVector is fast but still imprecise, while changing the main danger plot array to be an array of UnitHandle vectors and calculating danger values depending on the defending unit would be precise but very slow) and implementing a turn-limiter option into the pathfinder (it could be set an int value the same way unit data is set, and it would simply invalidate too long paths as they are being built instead of having to wait for path generation to complete before checking whether a path is too long). Since CvDangerPlots has such a wide influence on so many functions, I'll also undoubtedly be touching on all those functions as well (eg. move unit to safety tactical moves, all air unit logic, implementing a second CvDangerPlots map for civilian units exclusively, possibly implementing a third CvDangerPlots map that considers terrain defense bonuses if danger values are cached as combat strengths, etc.). Implementing a "ghost pathfinder"/"ghostfinder" is also on the list (it would allow the AI to simulate the pathing of a dummy unit, primarily used when wanting to build units at cities and to keep track of what water areas are connected via city/canal/etc.), but I'm a bit afraid of possible unintended side effects, so I'm leaving that until after I'm done with my first two planned items.
     
  19. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    18,076
    Gender:
    Male
    Location:
    Little Rock
    I'll throw Ilteroi under the bus for the Impassable bug – that was his code. :D

    I almost always do merges by hand (using WinMerge, of course, but slowly)- I don't trust the auto-merging programs.

    Your plans sound great – I too have noticed the canEnterTerrain being called a lot (using a dll sniffer while the game runs). I've also noticed that the checks for tourism buildings (i.e. the checks that deal with moving/valuing great works and creating theming bonuses) gets called a lot. I've been meaning to look at it. I'll keep an eye on your work, and once you feel like you've reached a stable point in your work, and the CP has calmed down, we'll start working on a merge.

    G
     
  20. notque

    notque Artificially Intelligent

    Joined:
    Nov 13, 2005
    Messages:
    1,654
    I play on Deity, and just tried the mod on Deity and got rocked hard. I haven't played a step lower in forever, but I think I might have to.

    Even the barbarians were ridiculously reasonable with their decisions. It was amazing.
     

Share This Page