svoid ai_parseTree_makeTotalWeights(PTElement e, double weight) { e.totalWeight = weight*e.weight; for (PTElement child : unnull(e.children)) ai_parseTree_makeTotalWeights(child, e.totalWeight); // prefer composites if weight is the same // so we get the least casting on top sortInPlaceByCalculatedFieldDesc(e.children, func(PTElement e) -> Pair { pair(e instanceof ChoosePart, e.totalWeight) }); }