feat: cargo unload challenge

This commit is contained in:
Ilia Denisov
2026-02-21 09:57:02 +02:00
parent 233c9ebc2a
commit 9e36d7151e
12 changed files with 137 additions and 166 deletions
+17 -18
View File
@@ -171,9 +171,6 @@ func (c *Cache) listRoutedSendGroupIds(pn uint) iter.Seq[int] {
}
// Невозможно лишь выгрузить колонистов на чужой планете.
/*
TODO: Очерёдность выгрузки согласно правилам
*/
func (c *Cache) TurnUnloadEnroutedGroups() {
for i := range c.g.Map.Planet {
p := &c.g.Map.Planet[i]
@@ -245,7 +242,7 @@ func (c *Cache) selectColUnloadGroup(groups []int) (result iter.Seq[int]) {
}
// select winner to unload cargo
id := MaxOrRandomLoadId(loadByRace)
id := MaxOrRandomLoadId(loadByRace, func(ri int) float64 { return float64(c.g.Race[ri].Votes) })
result = slices.Values(groupByRace[id])
return
@@ -277,23 +274,25 @@ func (c *Cache) listRoutedUnloadShipGroupIds(pn uint, routeType game.RouteType)
}
}
func MaxOrRandomLoadId(loadByRace map[int]float64) int {
if len(loadByRace) < 2 {
func MaxOrRandomLoadId(raceLoad map[int]float64, pop func(int) float64) int {
if len(raceLoad) < 2 {
panic("loadByRace must contain at least 2 keys")
}
IDs := slices.Collect(maps.Keys(loadByRace))
slices.SortFunc(IDs, func(id1, id2 int) int {
raceIndex := slices.Collect(maps.Keys(raceLoad))
slices.SortFunc(raceIndex, func(ria, rib int) int {
return cmp.Or(
cmp.Compare(loadByRace[id2], loadByRace[id1]),
// maximum quantity of unloading colonists
cmp.Compare(raceLoad[rib], raceLoad[ria]),
// maximum population of the race
cmp.Compare(pop(rib), pop(ria)),
// Random winner
cmp.Compare(rand.Float64(), rand.Float64()),
// in theoty, unreacheable option, but let's randomize again
cmp.Compare(rand.Float64(), rand.Float64()),
)
})
// no single winner with highest load
if loadByRace[IDs[0]] == loadByRace[IDs[1]] {
// remove IDs which load less than maximum
IDs = slices.DeleteFunc(IDs, func(v int) bool { return loadByRace[v] < loadByRace[IDs[0]] })
// IDs[0] will have random index
rand.Shuffle(len(IDs), func(i, j int) { IDs[i], IDs[j] = IDs[j], IDs[i] })
}
return IDs[0]
return raceIndex[0]
}