Module: OrefinderEstimate::Cluster

Defined in:
lib/orefinder_estimate/cluster.rb

Constant Summary collapse

SECONDARY_XZ_MIN =
10
SECONDARY_XZ_MAX =
30
PRIMARY_XZ_MIN =
8
PRIMARY_XZ_MAX =
15
SECONDARY_Y_MAX =
3
PRIMARY_Y_MAX =
2

Class Method Summary collapse

Class Method Details

.adaptive_center_band(cfg, search_radius) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/orefinder_estimate/cluster.rb', line 28

def adaptive_center_band(cfg, search_radius)
  f = cfg['frequency'].to_s
  lo = 50
  hi = 150
  if %w[very_rare rare].include?(f)
    lo = 40
    hi = 90
  elsif %w[very_common common].include?(f)
    lo = 60
    hi = 140
  end
  band_hi = [hi, search_radius].min
  band_lo = [lo, band_hi].min
  [band_lo, band_hi]
end

.cluster_rank_score(max_prob, mean_prob, mean_norm_dist) ⇒ Object



53
54
55
# File 'lib/orefinder_estimate/cluster.rb', line 53

def cluster_rank_score(max_prob, mean_prob, mean_norm_dist)
  (max_prob * 0.7 + mean_prob * 0.3) - mean_norm_dist * 12
end

.dist2(x1, z1, x2, z2) ⇒ Object



22
23
24
25
26
# File 'lib/orefinder_estimate/cluster.rb', line 22

def dist2(x1, z1, x2, z2)
  dx = x1 - x2
  dz = z1 - z2
  Math.sqrt(dx * dx + dz * dz)
end

.generate_clusters(seed:, ore:, era:, cfg:, px:, pz:, biome:, search_radius: 220) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/orefinder_estimate/cluster.rb', line 57

def generate_clusters(seed:, ore:, era:, cfg:, px:, pz:, biome:, search_radius: 220)
  peak_y = Probability.optimal_y_for_mining(cfg)
  band_lo, band_hi = adaptive_center_band(cfg, search_radius)
  num_centers = search_radius < 48 ? 2 : 3
  points_per_cluster = [4, 3, 3]
  min_y = cfg['minY']
  max_y = cfg['maxY']

  expand = lambda do |cx, cz, n, rng, confidence_bonus, primary|
    xz_min = primary ? PRIMARY_XZ_MIN : SECONDARY_XZ_MIN
    xz_max = primary ? PRIMARY_XZ_MAX : SECONDARY_XZ_MAX
    y_max = primary ? PRIMARY_Y_MAX : SECONDARY_Y_MAX
    pts = []

    push = lambda do |x, y, z|
      prob = Probability.calculate_ore_probability(cfg, y, biome)
      d = dist2(x, z, px, pz)
      conf = Probability.clamp(Probability.calculate_confidence(prob, d, y, peak_y) + confidence_bonus, 0, 100)
      pts << {
        'x' => x, 'y' => y, 'z' => z,
        'confidence' => Rng.round_to_tenth(conf),
        'probabilityPct' => Rng.round_half_up(prob * 1000) / 10.0,
        'distance' => Rng.round_to_tenth(d)
      }
    end

    if primary && n > 0
      center_y = Probability.clamp(Rng.round_half_up(peak_y + (rng.call - 0.5) * 2), min_y, max_y)
      push.call(Rng.round_half_up(cx), center_y, Rng.round_half_up(cz))
    end

    start_i = (primary && n > 0) ? 1 : 0
    (start_i...n).each do
      mag_x = xz_min + rng.call * (xz_max - xz_min)
      mag_z = xz_min + rng.call * (xz_max - xz_min)
      sx = rng.call < 0.5 ? -1 : 1
      sz = rng.call < 0.5 ? -1 : 1
      x = Rng.round_half_up(cx + sx * mag_x)
      z = Rng.round_half_up(cz + sz * mag_z)
      dy = if primary
             Probability.clamp(Rng.round_half_up((rng.call + rng.call - 1) * y_max), -y_max, y_max)
           else
             Probability.clamp(Rng.round_half_up((rng.call + rng.call + rng.call - 1.5) * 2), -y_max, y_max)
           end
      y = Probability.clamp(Rng.round_half_up(peak_y + dy), min_y, max_y)
      push.call(x, y, z)
    end

    if primary && !pts.empty?
      max_probs = pts.map { |p| p['probabilityPct'] }
      max_p = max_probs.max
      if max_p < 75
        k = max_probs.index(max_p)
        y_best = Probability.clamp(Rng.round_half_up(peak_y), min_y, max_y)
        p0 = pts[k]
        prob = Probability.calculate_ore_probability(cfg, y_best, biome)
        d = dist2(p0['x'], p0['z'], px, pz)
        conf = Probability.clamp(Probability.calculate_confidence(prob, d, y_best, peak_y) + confidence_bonus, 0, 100)
        pts[k] = {
          'x' => p0['x'], 'y' => y_best, 'z' => p0['z'],
          'confidence' => Rng.round_to_tenth(conf),
          'probabilityPct' => Rng.round_half_up(prob * 1000) / 10.0,
          'distance' => Rng.round_to_tenth(d)
        }
      end
    end

    # Stable sort: pointUxScore desc, then confidence desc.
    pts.each_with_index.sort_by do |p, i|
      [-point_ux_score(p['probabilityPct'], p['distance'], search_radius), -p['confidence'], i]
    end.map(&:first)
  end

  base_rng = Rng.make_rng([seed.to_s, ore, era, 'centers', num_str(Rng.round_half_up(px)), num_str(Rng.round_half_up(pz))])
  base_angle = base_rng.call * Math::PI * 2

  built = []
  (0...num_centers).each do |c|
    r = Rng.make_rng([seed.to_s, ore, era, 'c', c.to_s, num_str(band_lo), num_str(band_hi)])
    angle = base_angle + c * ((2 * Math::PI) / num_centers) + (r.call - 0.5) * 0.45
    rad = band_lo + r.call * (band_hi - band_lo)
    cx = px + Math.cos(angle) * rad
    cz = pz + Math.sin(angle) * rad

    n = points_per_cluster[c] || 3
    primary = c.zero?
    bonus = primary ? 5 : 0
    pts = expand.call(cx, cz, n, r, bonus, primary)

    if pts.empty?
      max_prob = 0
      mean_prob = 0
      mean_norm_dist = 1
    else
      max_prob = pts.map { |p| p['probabilityPct'] }.max
      mean_prob = pts.sum { |p| p['probabilityPct'] } / pts.length.to_f
      mean_norm_dist = pts.sum { |p| norm_distance(p['distance'], search_radius) } / pts.length.to_f
    end
    rank = cluster_rank_score(max_prob, mean_prob, mean_norm_dist)
    rank += 22 if c.zero?
    built << { 'pts' => pts, 'rank' => rank }
  end

  sorted = built.each_with_index.sort_by { |b, i| [-b['rank'], i] }.map(&:first)
  best = sorted.empty? ? [] : sorted[0]['pts']
  secondary = sorted[1..].map { |b| b['pts'] }
  { 'bestCluster' => best, 'secondaryClusters' => secondary }
end

.norm_distance(d, search_radius) ⇒ Object



44
45
46
47
# File 'lib/orefinder_estimate/cluster.rb', line 44

def norm_distance(d, search_radius)
  cap = [search_radius, 1].max
  Probability.clamp(d / cap.to_f, 0, 1)
end

.num_str(v) ⇒ Object



18
19
20
# File 'lib/orefinder_estimate/cluster.rb', line 18

def num_str(v)
  v == v.to_i ? v.to_i.to_s : v.to_s
end

.point_ux_score(prob_pct, distance, search_radius) ⇒ Object



49
50
51
# File 'lib/orefinder_estimate/cluster.rb', line 49

def point_ux_score(prob_pct, distance, search_radius)
  (prob_pct / 100.0) * 0.8 - norm_distance(distance, search_radius) * 0.2
end