Get random asteroid

Hello

i am trying to make bot which mines ore from same place (I used some fleet mining script here). But my two hulks steal each other asteroids, because asteroid is picked firstOrdefault. I need to figure out how to pick random asteroid (which is in range) to by more optimalized.

i found script which pick random item from an array :

T RandomElement(IEnumerable sequence)
{
var array = (sequence as T[]) ?? sequence?.ToArray();

if (!(0 < array?.Length))
    return default(T);

return array[RandomInt() % array.Length];

}

but cannot find way to make asteroidOverviewEntryNextNotTargeted to an array (tryed .where but it doesn work)

Parse.IOverviewEntry[] ListAsteroidOverviewEntry =>
WindowOverview?.ListView?.Entry
?.Where(entry => null != OreTypeFromAsteroidName(entry?.Name))
?.OrderByDescending(entry => ModifiedOrePreference(entry?.Name))
?.OrderBy(entry => entry?.DistanceMax ?? int.MaxValue)
?.ToArray();

var asteroidOverviewEntryNextNotTargeted = ListAsteroidOverviewEntry?.FirstOrDefault(entry => !((entry?.MeTargeted ?? false) || (entry?.MeTargeting ?? false)));

1 Like

you have to make the random element to be between 0 and max of (int) index ListAsteroidOverviewEntry
then
var asteroidtotake = ListAsteroidOverviewEntry?.Elemetatordefault (random i)

or since you wanna mine in fleet
mark the asteroid you took with tag ( helped by window fleet window or menu)
and avoid to target marked asteroids; but you have to see if sanderling take tag collumn entry ( better look on raw, dont think is marked the same way like type or name

1 Like

Thank you very much, but i undestand that i still need somewhat apply that entry: entry => !((entry?.MeTargeted ?? false) || (entry?.MeTargeting ?? false))

yeas, usual is a need (
txt to fil min req caracters)

i feel like i am starting to understand these scripts. This code works for me as i expected

var edibleAsteroids = ListAsteroidOverviewEntry?.Where(entry => !((entry?.MeTargeted ?? false) || (entry?.MeTargeting ?? false)));	
    var asteroidOverviewEntryNextNotTargeted = RandomElement(edibleAsteroids?.ToArray());
1 Like

Welcome Peca! :wave:

Thank you for sharing your experience and for adding the code to solve the picking problem.