Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking next mine to build #122

Open
Deenayd opened this issue Oct 11, 2022 · 0 comments
Open

Checking next mine to build #122

Deenayd opened this issue Oct 11, 2022 · 0 comments

Comments

@Deenayd
Copy link

Deenayd commented Oct 11, 2022

If you have catched a moment when on initialising automine one mine is shown as 'next mine' but few moments later another mine is started - here is the reason:

Inside function GetNextMineToBuild there is following code:

Dictionary<Buildables, float> dic = new();
............. get a dictionary of possible mines to be built into 'dic'
dic = dic.OrderBy(m => m.Value)
    ToDictionary(m => m.Key, m => m.Value);
var bestMine = dic.FirstOrDefault().Key;

What it does?

  1. checkes what mines can be build and puts them into 'dic'.
  2. orders possible mines by their ROI and put them into ordereddictionary (to keep the order)
  3. converts ordereddictionary back into dictionary, loosing the order and puts them back into 'dic'
  4. gets first of mines from 'dic' (not ordered dictionary) and says it should be built.

Obviously correct code would skip step 3, so it should look something like this:

Dictionary<Buildables, float> dic = new();
............. get a dictionary of possible mines to be built into 'dic'
var orderedDic = dic.OrderBy(m => m.Value);
var bestMine = orderedDic.FirstOrDefault().Key;

In fact current internal implementation of dictionary usually keeps the order or entered elements, so it usually works. But only usually.

Btw similar code appears in two copies of GetNextMineToBuild and in GetNextResearchToBuild too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant