yairm210 / Unciv

Open-source Android/Desktop remake of Civ V
Mozilla Public License 2.0
8.34k stars 1.56k forks source link

Automated workers getting stuck in infinite loop of two possible tile improvements #8127

Closed SpacedOutChicken closed 1 year ago

SpacedOutChicken commented 1 year ago

In DeCiv Redux, certain resources can be improved by either a Research Site or a Borough (both improvements give you access to the underlying resource). The Borough is the better improvement as it comes with more stat bonuses. The issue is that having two possible improvements is confusing automated workers. They'll build a Research Site on a resource, then replace that with a Borough, then replace that with a Research Site, then replace that with a Borough, and on and on forever.

When faced with two possible improvements that can improve a resource, the system needs to at least choose one, and preferably choose the better one when there are two choices.

To reproduce the bug:

  1. Start a game in DeCiv Redux at a late enough stage that both Research Sites and Boroughs can be constructed.
  2. Settle a city near a resource such as Data that needs a Research Site
  3. Build a Worker and set it to Automate
  4. Watch as the Worker builds the same two units on top of each other forever.

I've attached a save file with a worker on top of Data that's stuck in the loop. infinite loop improvement.txt

yairm210 commented 1 year ago

Oh cool! That's a case I never even considered 😉

SpacedOutChicken commented 1 year ago

Sorry for being rude. I only wanted to point out that the situation in DeCiv Redux doesn't exist in the base game. In regular Unciv, most resources can be improved by only one specific tile improvement, and in the case of oil, there's no instance where a user can choose between building an oil well or an offshore platform, because they can't be built on the same terrain.

In DeCiv, we've created a situation where the AI has to choose between improvements that are both good for the underlying resource, and the AI just can't choose.

itanasi commented 1 year ago

I bet this is also why the AI when getting to create an Improvement via unique often replaces a previous one. No check for optimal improvement placement.

Need to update WorkerAutomation.getPriority() to deprioritize already improved tiles and evaluate which tile will have the most improvement potential

SpacedOutChicken commented 1 year ago

If I could code worth a damn, I think I'd make it so that when there are multiple improvements that can be made, the AI scores each possibility by adding up the total tile yields and picks the one with the highest score. So, an improvement that grants +2 Culture and +2 Food would beat out an improvement that grants +3 science. Can this be done?

itanasi commented 1 year ago

There's already a ranking for an Improvement to choose for the tile. However, it doesn't take into consideration the existing improvement. Nor the yields of the underlying tile.

yairm210 commented 1 year ago

Well err It doesn't seem to be happening in the save file you provided And it doesn't look like we dedicated any specific commit to solving this problem So either

Can you try and replicate elsewhere?

SpacedOutChicken commented 1 year ago

We actually figured out a way to prevent it from occurring. We added the unique "Obsolete with [Computers]" to the less powerful tile improvements. That way, when the player researches Computers, the old improvements can no longer be built, so the automated workers will just build Boroughs.

If you want replicate the bug, I can provide you with a modified DeCiv mod that doesn't have that change.

SpacedOutChicken commented 1 year ago

Here's a DeCiv that doesn't have that obsolete unique built in. You should be able to replicate the bug with this. DeCiv-Redux-tweaked.zip