induktio / thinker

AI improvement patch for SMACX.
https://discord.gg/XdFuwWzzku
GNU General Public License v2.0
68 stars 10 forks source link

VehBaseUnit is hardwired #37

Closed bvanevery closed 2 years ago

bvanevery commented 2 years ago

terranx_enums.h contains a hardwiring of predefined units in alphax.txt. Nobody ever guaranteed that these units are as claimed. It may be of no consequence to Thinker itself, but it is of consequence to those of us who mod units in alphax.txt. For example in a game where I combined Thinker with my SMACX AI Growth mod, I got illegal creation of a Clean 3-Res Transport every game, because I have it in the "unit = 3" slot (counting from 0). Thinker assumes that's always a Transport with no other capabilities, and that's not true in general. https://alphacentauri2.info/index.php?topic=21768.msg133377#msg133377

The stock binary, in contrast, does not require a Transport unit to be defined in alphax.txt at all, for Transports to appear in the game. It somehow designs a plain Transport if such is needed.

induktio commented 2 years ago

This game engine supports only limited moddability and as such it uses hardcoded IDs for various items all over the place. This particular header just follows the definitions that the game engine uses for various basic units. Changing these hardcoded definitions isn't practical and isn't planned anyway. If modders need to define additional special units, the best practise is to add them to the end of the list and not change the pre-existing IDs.

In this particular case Thinker does make an assumption that a transport foil will be available in that particular unit slot after the transport module tech is discovered. The default choice is only a fallback, but since this unit got probably the highest score, it was picked for production. Probably it should involve an additional check if the unit prereq tech is discovered but it can run into problems if there are no valid transport prototypes available even if the transport module tech is discovered. This already causes problems with sea colony pods, since sometimes the prototype is not created by the vanilla game. That's why the AIs are sometimes forced to use Sea Escape Pod prototypes.

bvanevery commented 2 years ago

Most operative comment 1st:

Probably it should involve an additional check if the unit prereq tech is discovered

That would help. And should be done for every predefined unit.

but it can run into problems if there are no valid transport prototypes available even if the transport module tech is discovered.

Transport module alone doesn't mean you can make a ship. You must also have a foil or cruiser chassis. Surely, any given code has to handle the possibility that various ships aren't allowed yet.

This already causes problems with sea colony pods, since sometimes the prototype is not created by the vanilla game. That's why the AIs are sometimes forced to use Sea Escape Pod prototypes.

Sea Escape pods are used when Aliens and Humans depopulate each other's bases. No other time. I've never seen the stock binary produce a Sea Escape Pod in any other circumstance. I assume they did it for narrative reasons (to get to call it "Escape"), for moddability (even if ultimately they didn't change anything), and to separate implementation concerns (lest they stir up new bugs in old code).

As for moddability stuff:

This game engine supports only limited moddability and as such it uses hardcoded IDs for various items all over the place.

That's a regrettable practice that might be worth cleaning up at some point. I haven't stared at the code long enough to know how easy that would be to do.

my suspicion is that it got spawned by some of the vanilla engine code that just happens to assume a transport foil is always available with unit id=3.

I ran 2 test games using the stock binary only. Although it's not definitive, no modded Clean 3-Res Transport was created. I have also never observed this problem in my own .txt modding work with the stock binary. Whereas, Thinker always creates this unit in the course of play.

AFAICT the stock binary simply does not require that Transport unit to be any particular thing. Other units, such as mindworms, the various Unity units, and the Battle Ogres, yes the stock binary expects them to be very specific things.

Unless you actually see a base producing this unit,

It is well documented in that thread that Thinker bases produce the units. At least, the 2nd, 3rd, 4th etc. units are definitely so produced. I'd have to go back and look at whether I have a save of how the 1st unit came to be. I'm not sure what time_warp is (cloning? dimensional rift?), but as for whatever "goody_box" is... random pops from supply pods generally produce Unity units, not plain Transports. I bet "goody_box" is not supposed to deal with the first few units in the alphax.txt #UNITS list. Why would you be awarded a Scout Patrol, for instance?

the best practise is to add them to the end of the list and not change the pre-existing IDs.

It is not actually the best practice when working around bugs in the stock binary. Disabling plain Transports works around a problem in the stock binary where it obsesses about producing them. The problem with changing Thinker's predefined unit behavior compared to the stock binary, is that then you're expecting alphax.txt files to be written specifically for Thinker's benefit. That's your prerogative as a modder, but it doesn't count as a best practice, because it puts an additional implementation burden on other modders. They have to ship 2 files and test 2 different cases for bugs, if they're going to support Thinker. The path of least resistance is to blow Thinker off and not worry about it. It would be better if Thinker and the stock binary both worked the same way, i.e. Transports and Sea Formers are dynamically conjured from the ether as needed.

Also, predefined unit slots are a bit precious in the stock binary. If you have too many, things get weird in the game. There are good reasons to have additional predefined units, and not necessarily in small quantity either. They're one of the main tools for reprogramming the stock binary to do something more appropriate and challenging.

induktio commented 2 years ago

I updated the comment later to reflect that I noticed one of the AIs was indeed producing a modified transport foil and the reason was stated there. So in this case I don't claim it was spawned by a supply pod or similar. Also "Sea Escape pods are used when Aliens and Humans depopulate each other's bases. No other time." is not accurate. For example the first sea colony pods you get when spawning as an aquatic faction are actually Sea Escape Pods. At the spawning phase it's unlikely any custom prototypes are available.

So it appears your mod does allow building both foils and transport units straight away? I don't see the reasoning then for changing the transport foil prototype to something entirely different. To use the transports, players need to have some kind of a prototype to produce. Btw, Thinker checks if both ships and transport "weapons" are available before producing them.

If there are no valid basic transport prototypes defined, then the AIs would also need extra code for creating suitable prototypes. That uses an additional faction-specific design slot. If the default Transport Foil prototype prereq were set to Disable it could function as a fallback for AIs only if they don't have other suitable prototypes available. And also avoid any issues in the vanilla game if it happened to assume a particular prototype in that design slot or if the vanilla AI produced too much of them. It looks like your mod currently doesn't provide any predefined transport unit that can be produced right away even if the weapon/chassis tech prereqs in question are set to none, unless I misread something.

bvanevery commented 2 years ago

the first sea colony pods you get when spawning as an aquatic faction are actually Sea Escape Pods.

I'll be darned. I must have noticed that at some point, but I haven't noticed in awhile.

Pirates start with Sea Escape Pods

At the spawning phase it's unlikely any custom prototypes are available.

I modded my Sea Escape Pods. They're Clean units, same as Sea Colony Pods in my mod. Game starts with Clean Reactors available. Simple colony types have a special dispensation that they're Clean and don't cost anything extra for being Clean. This helps stock AI with early SUPPORT problems.

So it appears your mod does allow building both foils and transport units straight away?

Yes. It solves AI performance problems with limited land, as well as some Probe Team and Former tech dependency problems. There really isn't any good narrative reason for avoiding ships. This is not 4000 B.C. and it's not Civ II. SMAC mostly aped its tech tree from Civ II because it was a game progression that "worked" for the most part. That doesn't mean it was credible or worked well in all cases. For instance in Civ II, getting stuck on an ice floe at one of the poles was a pretty "legendary" start.

I implement this by: Foil,M1, Skimship,M1, Hoverboat,M1, Coastal,M1, 5, 1, 0, 0, 2, 4, None, Megafoil,M1, Superfoil,M1, and Troop Transport, Transport, 0, 7, 3, -1, None,

Anyone can do anything with a foil chassis, or a Transport package. No actual unit is predefined. The stock binary somehow takes these ingredients and coughs up a Transport foil with them.

I don't see the reasoning then for changing the transport foil prototype to something entirely different.

It saves a predefined unit slot, in a place that is not needed by the stock binary for anything. Predefined unit slots are a bit precious.

Failing to provide a plain Transport, and instead providing something that isn't available until E4 Bioadaptive Resonance, seems to stop the AI from obsessing about "making too many transports" for a good while. This is a huge problem for the Pirate AI, because it is so dependent upon ferrying units.

To use the transports, players need to have some kind of a prototype to produce.

No they don't. Here, let me start a game with a non-Pirate faction. SMACX AI Growth mod version 1.52, Morganites Turn 1:

Morganites can make a Transport

My alphax.txt has no "plain Transport foil" unit in it.

If there are no valid basic transport prototypes defined, then the AIs would also need extra code for creating suitable prototypes.

Stock binary has such code. Also happens for Sea Formers, but it's buggy and sometimes doesn't work. That's why I have Clean Sea Formers as a backup.

That uses an additional faction-specific design slot.

This, I haven't much noticed or paid attention to. Nor what the consequences might be. I'm not sure it's bad:

Morganic starter designs

So let's say later in the game, I never want to make a "plain Fission Transport foil" ever ever again. Well it just goes faction-specific bye-bye. Not stuck with it as a base predefined unit forever. That sounds good. Saves a slot as the game progresses and there are more designs cluttering things. Only got so many slots.

avoid any issues in the vanilla game if it happened to assume a particular prototype in that design slot

My testing of the stock binary over 3.5+ years is extensive. Let's see, how many mod releases have I changed the Transport predefined unit to something else? Hmm, in release 1.39 quite awhile ago, that Transport became a Clean Transport. There were no bugs for making it Clean, but of course it was a Transport. And Clean Reactors were available from the beginning of the game, with me doing that sort of thing. Unlike 3-Res armor, which definitely isn't.

What else... in version 1.33, even longer ago, I had that slot being used for a Cruiser Transport. Didn't cause any issues, and of course Cruisers are not available at game start. Also in that release, the notation "- Transport: now occupies the very last position in the predefined units list, to try to curb the Pirate AI's obsession with it." Nothing broke. I think this indicates the stock binary doesn't assume Transports are in a particular slot, and is robust about checking the tech requirements of a predefined unit.

or if the vanilla AI produced too much of them.

I've been around and around and around and around with that. All kinds of Transport designs, trying to head it off. I can see if a "disabled Transport" somehow affects Pirate production behavior in the stock binary, but I'm not expecting much.

Here's what I think really happens, currently. You can't stop a plain Transport from existing if its tech prereqs are available, so they are still produced in substantial numbers. But, they get killed. Then by C3 Advanced Subatomic Theory when Plasma armor is available, my predefined unit table tells the stock AI, "go build you some Clean Plasma Transports". And it does that. A lot. And it stops making the plain ones anymore, because it's an inferior design.

When E4 Bioadaptive Resonance becomes available, my predefined unit table tells the stock AI, "go build you some Clean 3-Res Transports". And it does that. A lot. A bit better chance to hold off an Isle, and the ship doesn't actually cost any more.

These 2 ship designs hold up better defensively, and they don't impact SUPPORT. So even though the AI is obsessing, there's some defensive value to it, and it's not getting SUPPORT crippled. The early plain Transport stuff has a regrettable impact, but it's still better to have the tougher more expensive ships built in quantity later. Otherwise if I defined a Clean Transport, the AI would just see that as a cheaper ship and produce tons of 'em. I think it's more of a waste of minerals, building stuff that has no defense. Like, what if it's carrying cargo? One sinks; the other has a chance to live.

induktio commented 2 years ago

I'm not sure what you're trying to claim in response here: "To use the transports, players need to have some kind of a prototype to produce. [...] No they don't". Every vehicle existing on the map needs to have a some kind of a prototype assigned to it. This is also the ID used when it is selected for production. So obviously this prototype needs to exist either in the pre-defined units or faction-specific design slots. There's no way around it.

In that specific save file it doesn't look like the AIs have any faction-specific transport prototypes created, so it appears the vanilla code didn't create the custom prototypes in this case, for reasons unknown. That means the AIs would be unable to produce any transports at all in the beginning. AI factions don't necessarily get created the same prototypes than player factions. Generally editing the pre-defined units in an arbitrary fashion is not really supported by the game engine given the hardcoded IDs used. The table is named "Basic Units". Thinker code also relies on fixed IDs for many basic units. I may look into whether there could be more code added for creating extra prototypes, but this may or may not be added to Thinker. It might also be useful for dealing with the non-existent Sea Colony Pods issue.

The obvious fix here is to keep the default Transport Foil prototype in alphax.txt pre-defined units, and not add any abilities on it that cannot be validly produced when the transport package/foil chassis features become available. It can be also set to Disable prereq to prevent the vanilla AI from producing them.

bvanevery commented 2 years ago

The obvious fix here

Workaround. A fix would be handling it the way the stock binary does, not hardwiring things. "Combo" mods need things to work the way they work in the stock binary, to the extent that anyone messes heavily with predefined units. I get that you're not excited about writing code in support of that, that you see Thinker mostly as a standalone deal. Maybe in the future I'll provide a patch. But as I've only just begun to look at your code, I can't promise anything. I can't yet tell what's easy or hard to do, or even how anything in general is done.

Hopefully you do agree at least that the tech prereq for a predefined unit should be checked, and that this is low hanging fruit.

induktio commented 2 years ago

Well, I'm not convinced the stock binary will consistently create transport foil prototypes for the AI factions if the default one is not available. It didn't seem to happen in the savegame either. As it stands now, the prototype picker has a list of fallback default prototype choices whenever it will looks to produce given triad/weapon combo. These units are: BSC_SCOUT_PATROL BSC_COLONY_POD BSC_SEA_ESCAPE_POD BSC_FORMERS BSC_SEA_FORMERS BSC_SUPPLY_CRAWLER BSC_TRANSPORT_FOIL BSC_PROBE_TEAM.

So changing these prototypes from the defaults may currently cause it to produce units it should not produce yet. It may be possible to force the AI design missing prototypes by using propose_proto function, but I haven't tested it yet for these additional circumstances. It's not an ideal situation, but the game engine relies on fixed IDs for many of these units anyway.

bvanevery commented 2 years ago

Well, I'm not convinced the stock binary will consistently create transport foil prototypes for the AI factions if the default one is not available.

It is not actually desirable for the stock AI to create Transports prematurely. Land is more valuable for all factions except the Pirates. I can't swear to current Pirate behavior in my current release 1.52, but I don't recall them being crippled in any way. For one thing, Pirates will at least make Clean Sea Colony Pods early on, regardless of whether they have Transports. Colonization of the sea is the main job the Pirates should be doing in the early game.

Transport predefines might also be specifically irrelevant to AQUATIC factions. I doubt I've crippled the Pirates but I can check.

The only exit case that is important, is if a stock AI faction gets a "cramped land start" and fails to free itself of this circumstance via a sea route. Although, when it makes Clean Sea Colony pods, it is getting partly free of the problem. I suppose the real acid test would be to forego my usual 30%..50% ocean generation, and try playing a game on a mostly water world. It is not the design center of my mod and not something I feel terribly responsible for making sure it works, but it would be a pretty thorough test. If there's no problem in this circumstance, then there is no problem in any circumstance.

Freeing factions by immediate sea access, is something I do in my mod, that you don't do in Thinker. You're playing with mostly stock rules. You don't give ships away on Turn 1. You can have cramped factions just twiddle their thumbs and die. So if you are greatly worried about sea performance... maybe you'll be persuaded that the vanilla tech tree is bad.

propose_proto function

I will try to understand it. Somewhere in between trying to understand Microsoft DirectX-Graphics-Samples, which are probably going to look like hell.

Meanwhile, since running test games requires almost no brainpower, I will try the waterworld.

bvanevery commented 2 years ago

SMACX AI Growth mod version 1.53 alpha with stock binary. Marr of the Usurpers, 2165.zip

Appears to design Transport when/if actual need arises. Mission Year 2165. Usurpers start a prototype. Caretakers and University have already fielded Transports.

AI designs Transport

another AI faction's Transports

yet more AI Transports

Interestingly, the Pirates have not built any. Neither did the Morganites, but their island was big enough that they may not have had the need. Believers haven't. Hive says they did, but that they got killed... I'm not sure I believe that. I think sometimes the game displays wrong, dead units.

did Hive really build and lose Transports

So, this proves the stock AI will build a Transport prototype in various circumstances in the early game. You wondered about the stock binary consistently producing such prototypes. This depends on what you want "consistent" to mean. Some metrics could be: is it interfering with their ability to colonize? Or to make war? Or to explore?

I'm realizing that the waterworld is actually not the most difficult case. Everyone's guaranteed their own separate land mass. The worst cases are being squeezed on a peninsula behind another faction, or being the only faction on a little bitty island. I think I'd need to test on a smaller map for that. Anything less than Huge is not the design center of my mod, and not something I care about supporting. But, I'm sure people play my mod and don't follow my Huge recommendation. So I will try some Standard and Tiny maps to see what happens.

Meanwhile by MY 2180, the Believers and Hive have built Transports. Morganites and Pirates have not. But... would they need to? Morganites have a good land mass with the Manifold Nexus, and are a Passive Build faction. Pirates are at war. Marr of the Usurpers, 2180.zip

By MY 2200, Morganites have made Transports. Pirates are producing 1, and unit screen claims they've lost 32. I'm not sure I believe that. I could watch the Pirates from the previous save. Marr of the Usurpers, 2200.zip

Watched the Pirates. The sunk Transport claim is not credible. It's some kind of display error. Certainly could have lost that number of Synth Laser Foils or something.

bvanevery commented 2 years ago

SMACX AI Growth mod version 1.53 alpha with stock binary. Standard map, 30%..50% ocean. Data Angels end up in worst case and do not produce coastal settlements until MY 2172. How long until they produce a Transport? Roze of the Angels, 2172.zip 2 new coastal cities MY 2179. Angels begin making a Foil Probe Team, which is not insane but rather expensive for them. Under what circumstances would the AI rationally decide to build a Transport? At this time, wouldn't it be better to build a Clean Sea Colony Pod? Roze of the Angels, 2179.zip can't stop the probes MY 2188. Angels start producing a 2nd Foil Probe team in the other coastal city. They have 2 land colonists and in principle are pursuing a policy of land settlement, but in practice are milling those 2 units about and not settling them. It may be that the stock binary's colonization algorithm is too inferior to provide a solution, regardless of Transports. After all, the real short term answer is a Clean Sea Colony Pod, or perhaps some exploration of uncharted waters. Roze of the Angels, 2188.zip lack of extrication

MY 2196. The Gaians and Free Drones carve up the Data Angels. No chance. I don't think a Transport would have ever helped. A Clean Sea Colony Pod just goes land --> sea, whereas a Transport loaded with a Clean Colony Pod has to go land --> sea --> land. As well as being logistically more difficult, it inevitably spreads the empire at a greater distance. Roze of the Angels, 2196.zip toast

induktio commented 2 years ago

Transport Foils are needed the moment sea colonization or invasions begin for Thinker AIs, otherwise the AIs might do some weird things. There are some very specific strategies that rely on them. The reason I say the stock binary is incosistent in creating these prototypes is because they don't appear with any consistency for all factions in my tests. These prototypes are supposed to be added via consider_designs > propose_proto > make_proto function calls (vanilla game logic) which are executed every turn for every AI faction. Even when I add code to call propose_proto with explicit parameters for transport foils, the prototype seems to be created only sometimes. The reasons for this are unknown, so that's a problem.

bvanevery commented 2 years ago

Yet, the stock binary seems to create Transports when they're really required for something. MY 2151, SMACX AI Growth mod, stock binary. Tiny map. I followed the plight of the greatly disadvantaged Morganites. They used the Unity Transport they were gifted at the beginning of the game, to settle a nearby land colony. They also cheated and Terraformed UP their starting island. Caretakers trashed the Morganic city on the other island. Then used Transports to trash Morgan's capitol. They built 2 of them, did not have any captured Isles, and did not have any Unity Transports. Morgan of the Morganites, 2151.zip Caretaker assault

Belatedly just remembered 1 quirk about this though. The Caretakers were the faction I started the AI vs. AI game with. They were me the human player on Turn 1, and thus had Transports prototyped on Turn 1. So, not a fair test.

Only the University AI has designed a Transport on its own. Looks like it may have moved a Clean Colony Pod onto what turned out to be Gaian territory. ill-fated bridging

In another waterworld game on a Standard map, I learned that the Cult doesn't have to worry about early Transports. They'll capture Isles and use them to do their assaults.

bvanevery commented 2 years ago

In my mod I'm going to try Clean Transport in the Transport slot, and Clean Sea Formers in the "*Sea Formers" (disabled) slot. And hope it doesn't send the Pirates right back to making ridiculous numbers of wasteful early Clean Transports. Also will look to see if early island hopping colonization and neighbor invasion improves on Tiny waterworld maps.

bvanevery commented 2 years ago

Unfortunately making the Clean Transport available in the stock binary, causes the Pirates to engage in extreme foolishness. 34 units produced, and the vast majority aren't doing anything useful. They tend to sit around in bases with nothing to do, just Holding. Svensgaard of the Pirates, 2210.zip excessive Pirate transports in stock binary typical non-use of Transports in Pirate base

Also turns out that as good an idea as Clean Plasma Transport and Clean 3-Res Transport are for other factions, these units are in the Conquer part of the tech tree. The Pirates in my mod are an Explore Build faction that pursues Wealth. So they don't learn how to make the more useful defensive units for quite some time.

I will try the "fake disabled" unit idea.

bvanevery commented 2 years ago

"Fake Transport" (disabled) does prevent the stock binary from obsessing about Transports, or creating them at all, absent some "demonstrative need". Some factions that were always under AI control, did come up with their own Transport designs. The Pirates never did, which was the objective. In MY 2244 they finally learned Plasma armor despite their Explore Build focus. They started making the predefined Clean Plasma Transport unit in large quantities. Svensgaard of the Pirates, 2244.zip first Clean Plasma Transport Their empire seems to have done just fine with this production strategy. They made a lot of spammy Synth Foils that fended off hostile, nearby Usurpers, even lacking Laser armament. Probably because the Usurpers were mostly busy conquering on land and not giving the Pirates their undivided attention. Which totally counts; my whole philosophy of the Pirates is they have a huge moat and are a PITA to actually conquer.

The Pirates did make 2 Clean Colony Pods early on, that lacking Transports to take them somewhere, never turned into cities. That's not a problem though, as they don't use up any SUPPORT and aren't especially expensive to make in my mod.

So I have 2 further questions:

  1. Does this "Fake Transport" (disabled) actually work with Thinker? To be tested.
  2. Would offering a Clean Synth Transport be an acceptable strategy for the Pirates? It would be available almost immediately. Thus the Pirates would start obsessing almost immediately. Other factions might obsess too. Does it cause the stock binary to use different early game strategies? And are those strategies any better than what the Pirates did this game?

Errr, having watched the game to MY 2282, I'm thinking the answer to question 2 is NO. They've made 190 Clean Plasma Transports, pretty much torpedoing any good empire trajectory they were previously on. I'm starting to think that not offering any kind of Transport unit might be the needed fix for the stock binary. ridiculous numbers of Clean Plasma Transports