| 1 | package net.minecraft.world.level.levelgen.densityfunction.op;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.util.Interval;
|
| 7 | import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
|
| 8 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 9 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 10 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 11 | import net.minecraft.world.level.levelgen.densityfunction.DfRewriteRule;
|
| 12 | import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
| 13 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 14 | import net.minecraft.world.level.levelgen.densityfunction.generator.ConstantFunction;
|
| 15 | import org.slf4j.Logger;
|
| 16 |
|
| 17 | public record BinaryFunction(BinaryFunction.Type type, DensityFunction left, DensityFunction right) implements DensityFunction {
|
| 18 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 19 |
|
| 20 | @Override
|
| 21 | public DensitySampler compileSampler(final DensityFunction.CompileContext context) {
|
| 22 | DensitySampler left = this.left.compileSampler(context);
|
| 23 | DensitySampler right = this.right.compileSampler(context);
|
| 24 |
|
| 25 | return (DensitySampler)(switch (this.type) {
|
| 26 | case ADD -> {
|
| 27 | if (this.left instanceof ConstantFunction(float leftValue)) {
|
| 28 | yield new BinaryFunction.ConstAddSampler(right, leftValue);
|
| 29 | } else {
|
| 30 | if (this.right instanceof ConstantFunction(float rightValue)) {
|
| 31 | yield new BinaryFunction.ConstAddSampler(left, rightValue);
|
| 32 | }
|
| 33 |
|
| 34 | yield new BinaryFunction.AddSampler(left, right);
|
| 35 | }
|
| 36 | }
|
| 37 | case SUB -> {
|
| 38 | if (this.left instanceof ConstantFunction(float leftValue)) {
|
| 39 | yield new BinaryFunction.ConstSubSampler(leftValue, right);
|
| 40 | } else {
|
| 41 | if (this.right instanceof ConstantFunction(float rightValue)) {
|
| 42 | yield new BinaryFunction.ConstAddSampler(left, -rightValue);
|
| 43 | }
|
| 44 |
|
| 45 | yield new BinaryFunction.SubSampler(left, right);
|
| 46 | }
|
| 47 | }
|
| 48 | case MUL -> {
|
| 49 | if (this.left instanceof ConstantFunction(float leftValue)) {
|
| 50 | yield new BinaryFunction.ConstMulSampler(right, leftValue);
|
| 51 | } else {
|
| 52 | if (this.right instanceof ConstantFunction(float rightValue)) {
|
| 53 | yield new BinaryFunction.ConstMulSampler(left, rightValue);
|
| 54 | }
|
| 55 |
|
| 56 | yield new BinaryFunction.MulSampler(left, right);
|
| 57 | }
|
| 58 | }
|
| 59 | case DIV -> {
|
| 60 | if (this.left instanceof ConstantFunction(float leftValue)) {
|
| 61 | yield new BinaryFunction.ConstDivSampler(leftValue, right);
|
| 62 | } else {
|
| 63 | if (this.right instanceof ConstantFunction(float rightValue)) {
|
| 64 | yield new BinaryFunction.ConstMulSampler(left, 1.0F / rightValue);
|
| 65 | }
|
| 66 |
|
| 67 | yield new BinaryFunction.DivSampler(left, right);
|
| 68 | }
|
| 69 | }
|
| 70 | case MIN -> {
|
| 71 | Interval leftRange = this.left.range();
|
| 72 | Interval rightRange = this.right.range();
|
| 73 | if (leftRange.max() < rightRange.min()) {
|
| 74 | this.warnNonIntersecting();
|
| 75 | yield left;
|
| 76 | } else if (rightRange.max() < leftRange.min()) {
|
| 77 | this.warnNonIntersecting();
|
| 78 | yield right;
|
| 79 | } else {
|
| 80 | yield this.left instanceof ConstantFunction(float leftValue)
|
| 81 | ? new BinaryFunction.ConstMinSampler(right, leftValue)
|
| 82 | : (
|
| 83 | this.right instanceof ConstantFunction(float rightValue)
|
| 84 | ? new BinaryFunction.ConstMinSampler(left, rightValue)
|
| 85 | : new BinaryFunction.MinSampler(left, right, rightRange.min())
|
| 86 | );
|
| 87 | }
|
| 88 | }
|
| 89 | case MAX -> {
|
| 90 | Interval leftRange = this.left.range();
|
| 91 | Interval rightRange = this.right.range();
|
| 92 | if (leftRange.min() > rightRange.max()) {
|
| 93 | this.warnNonIntersecting();
|
| 94 | yield left;
|
| 95 | } else if (rightRange.min() > leftRange.max()) {
|
| 96 | this.warnNonIntersecting();
|
| 97 | yield right;
|
| 98 | } else {
|
| 99 | yield this.left instanceof ConstantFunction(float leftValue)
|
| 100 | ? new BinaryFunction.ConstMaxSampler(right, leftValue)
|
| 101 | : (
|
| 102 | this.right instanceof ConstantFunction(float rightValue)
|
| 103 | ? new BinaryFunction.ConstMaxSampler(left, rightValue)
|
| 104 | : new BinaryFunction.MaxSampler(left, right, rightRange.max())
|
| 105 | );
|
| 106 | }
|
| 107 | }
|
| 108 | });
|
| 109 | }
|
| 110 |
|
| 111 | private void warnNonIntersecting() {
|
| 112 | LOGGER.warn(
|
| 113 | "Compiling a {} function between two non-overlapping inputs: {} ({}) and {} ({})", this.type, this.left, this.left.range(), this.right, this.right.range()
|
| 114 | );
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public DensityFunction rewriteChildren(final DfRewriteRule rule) {
|
| 119 | DensityFunction left = rule.rewrite(this.left);
|
| 120 | DensityFunction right = rule.rewrite(this.right);
|
| 121 | return left == this.left && this.right == right ? this : new BinaryFunction(this.type, left, right);
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public Interval range() {
|
| 126 | Interval left = this.left.range();
|
| 127 | Interval right = this.right.range();
|
| 128 |
|
| 129 | return switch (this.type) {
|
| 130 | case ADD -> Interval.add(left, right);
|
| 131 | case SUB -> Interval.sub(left, right);
|
| 132 | case MUL -> Interval.mul(left, right);
|
| 133 | case DIV -> Interval.div(left, right);
|
| 134 | case MIN -> Interval.min(left, right);
|
| 135 | case MAX -> Interval.max(left, right);
|
| 136 | };
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public @DensityFunction.Axes int domainAxes() {
|
| 141 | return this.left.domainAxes() | this.right.domainAxes();
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public MapCodec<BinaryFunction> codec() {
|
| 146 | return this.type().codec;
|
| 147 | }
|
| 148 |
|
| 149 | public record AddSampler(DensitySampler left, DensitySampler right) implements DensitySampler {
|
| 150 | @Override
|
| 151 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 152 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 153 |
|
| 154 | try (ScopedDensityBuffer rightBuffer = context.acquireBuffer(volume)) {
|
| 155 | this.right.sampleVolume(context, rightBuffer, volume);
|
| 156 |
|
| 157 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 158 | outputBuffer.addTo(i, rightBuffer.get(i));
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 165 | return this.left.sampleValue(context, blockX, blockY, blockZ) + this.right.sampleValue(context, blockX, blockY, blockZ);
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | public record ConstAddSampler(DensitySampler left, float right) implements DensitySampler {
|
| 170 | @Override
|
| 171 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 172 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 173 |
|
| 174 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 175 | outputBuffer.addTo(i, this.right);
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | @Override
|
| 180 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 181 | return this.left.sampleValue(context, blockX, blockY, blockZ) + this.right;
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | public record ConstDivSampler(float left, DensitySampler right) implements DensitySampler {
|
| 186 | @Override
|
| 187 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 188 | this.right.sampleVolume(context, outputBuffer, volume);
|
| 189 |
|
| 190 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 191 | outputBuffer.set(i, this.left / outputBuffer.get(i));
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 197 | return this.left / this.right.sampleValue(context, blockX, blockY, blockZ);
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | public record ConstMaxSampler(DensitySampler left, float right) implements DensitySampler {
|
| 202 | @Override
|
| 203 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 204 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 205 |
|
| 206 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 207 | if (this.right > outputBuffer.get(i)) {
|
| 208 | outputBuffer.set(i, this.right);
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 215 | return Math.max(this.left.sampleValue(context, blockX, blockY, blockZ), this.right);
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | public record ConstMinSampler(DensitySampler left, float right) implements DensitySampler {
|
| 220 | @Override
|
| 221 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 222 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 223 |
|
| 224 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 225 | if (this.right < outputBuffer.get(i)) {
|
| 226 | outputBuffer.set(i, this.right);
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | @Override
|
| 232 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 233 | return Math.min(this.left.sampleValue(context, blockX, blockY, blockZ), this.right);
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | public record ConstMulSampler(DensitySampler left, float right) implements DensitySampler {
|
| 238 | @Override
|
| 239 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 240 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 241 |
|
| 242 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 243 | outputBuffer.set(i, outputBuffer.get(i) * this.right);
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 249 | return this.left.sampleValue(context, blockX, blockY, blockZ) * this.right;
|
| 250 | }
|
| 251 | }
|
| 252 |
|
| 253 | public record ConstSubSampler(float left, DensitySampler right) implements DensitySampler {
|
| 254 | @Override
|
| 255 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 256 | this.right.sampleVolume(context, outputBuffer, volume);
|
| 257 |
|
| 258 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 259 | outputBuffer.set(i, this.left - outputBuffer.get(i));
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 265 | return this.left - this.right.sampleValue(context, blockX, blockY, blockZ);
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | public record DivSampler(DensitySampler left, DensitySampler right) implements DensitySampler {
|
| 270 | @Override
|
| 271 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 272 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 273 |
|
| 274 | try (ScopedDensityBuffer rightBuffer = context.acquireBuffer(volume)) {
|
| 275 | this.right.sampleVolume(context, rightBuffer, volume);
|
| 276 |
|
| 277 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 278 | outputBuffer.set(i, outputBuffer.get(i) / rightBuffer.get(i));
|
| 279 | }
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 285 | float left = this.left.sampleValue(context, blockX, blockY, blockZ);
|
| 286 | return left == 0.0F ? 0.0F : left / this.right.sampleValue(context, blockX, blockY, blockZ);
|
| 287 | }
|
| 288 | }
|
| 289 |
|
| 290 | public record MaxSampler(DensitySampler left, DensitySampler right, float rightMaxValue) implements DensitySampler {
|
| 291 | @Override
|
| 292 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 293 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 294 |
|
| 295 | try (ScopedDensityBuffer rightBuffer = context.acquireBuffer(volume)) {
|
| 296 | this.right.sampleVolume(context, rightBuffer, volume);
|
| 297 |
|
| 298 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 299 | float rightValue = rightBuffer.get(i);
|
| 300 | if (rightValue > outputBuffer.get(i)) {
|
| 301 | outputBuffer.set(i, rightValue);
|
| 302 | }
|
| 303 | }
|
| 304 | }
|
| 305 | }
|
| 306 |
|
| 307 | @Override
|
| 308 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 309 | float left = this.left.sampleValue(context, blockX, blockY, blockZ);
|
| 310 | return left >= this.rightMaxValue ? left : Math.max(left, this.right.sampleValue(context, blockX, blockY, blockZ));
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | public record MinSampler(DensitySampler left, DensitySampler right, float rightMinValue) implements DensitySampler {
|
| 315 | @Override
|
| 316 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 317 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 318 |
|
| 319 | try (ScopedDensityBuffer rightBuffer = context.acquireBuffer(volume)) {
|
| 320 | this.right.sampleVolume(context, rightBuffer, volume);
|
| 321 |
|
| 322 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 323 | float rightValue = rightBuffer.get(i);
|
| 324 | if (rightValue < outputBuffer.get(i)) {
|
| 325 | outputBuffer.set(i, rightValue);
|
| 326 | }
|
| 327 | }
|
| 328 | }
|
| 329 | }
|
| 330 |
|
| 331 | @Override
|
| 332 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 333 | float left = this.left.sampleValue(context, blockX, blockY, blockZ);
|
| 334 | return left <= this.rightMinValue ? left : Math.min(left, this.right.sampleValue(context, blockX, blockY, blockZ));
|
| 335 | }
|
| 336 | }
|
| 337 |
|
| 338 | public record MulSampler(DensitySampler left, DensitySampler right) implements DensitySampler {
|
| 339 | @Override
|
| 340 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 341 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 342 |
|
| 343 | try (ScopedDensityBuffer rightBuffer = context.acquireBuffer(volume)) {
|
| 344 | this.right.sampleVolume(context, rightBuffer, volume);
|
| 345 |
|
| 346 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 347 | outputBuffer.set(i, outputBuffer.get(i) * rightBuffer.get(i));
|
| 348 | }
|
| 349 | }
|
| 350 | }
|
| 351 |
|
| 352 | @Override
|
| 353 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 354 | float left = this.left.sampleValue(context, blockX, blockY, blockZ);
|
| 355 | return left == 0.0F ? 0.0F : left * this.right.sampleValue(context, blockX, blockY, blockZ);
|
| 356 | }
|
| 357 | }
|
| 358 |
|
| 359 | public record SubSampler(DensitySampler left, DensitySampler right) implements DensitySampler {
|
| 360 | @Override
|
| 361 | public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
|
| 362 | this.left.sampleVolume(context, outputBuffer, volume);
|
| 363 |
|
| 364 | try (ScopedDensityBuffer rightBuffer = context.acquireBuffer(volume)) {
|
| 365 | this.right.sampleVolume(context, rightBuffer, volume);
|
| 366 |
|
| 367 | for (int i = 0; i < outputBuffer.size(); i++) {
|
| 368 | outputBuffer.addTo(i, -rightBuffer.get(i));
|
| 369 | }
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
|
| 375 | return this.left.sampleValue(context, blockX, blockY, blockZ) - this.right.sampleValue(context, blockX, blockY, blockZ);
|
| 376 | }
|
| 377 | }
|
| 378 |
|
| 379 | public enum Type {
|
| 380 | ADD("add"),
|
| 381 | SUB("sub"),
|
| 382 | MUL("mul"),
|
| 383 | DIV("div"),
|
| 384 | MIN("min"),
|
| 385 | MAX("max");
|
| 386 |
|
| 387 | public final String id;
|
| 388 | public final MapCodec<BinaryFunction> codec = RecordCodecBuilder.mapCodec(
|
| 389 | i -> i.group(DensityFunction.CODEC.fieldOf("left").forGetter(BinaryFunction::left), DensityFunction.CODEC.fieldOf("right").forGetter(BinaryFunction::right))
|
| 390 | .apply(i, (left, right) -> new BinaryFunction(this, left, right))
|
| 391 | );
|
| 392 |
|
| 393 | Type(final String id) {
|
| 394 | this.id = id;
|
| 395 | }
|
| 396 | }
|
| 397 | }
|
| 398 |
|