Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 144 lines 5.6 kB view raw
1package net.lerariemann.infinity.options; 2 3import net.lerariemann.infinity.InfinityMod; 4import net.lerariemann.infinity.dimensions.RandomDimension; 5import net.lerariemann.infinity.util.core.ConfigType; 6import net.lerariemann.infinity.util.core.NbtUtils; 7import net.lerariemann.infinity.util.core.RandomProvider; 8import net.minecraft.nbt.CompoundTag; 9import net.minecraft.nbt.ListTag; 10import net.lerariemann.infinity.util.core.CommonIO; 11import java.util.ArrayList; 12import java.util.Collections; 13import java.util.List; 14import java.util.Random; 15 16public class RandomInfinityOptions { 17 CompoundTag data; 18 String path; 19 public RandomInfinityOptions(RandomDimension parent, boolean isEasterDim) { 20 data = new CompoundTag(); 21 path = parent.getStoragePath(); 22 RandomProvider prov = parent.PROVIDER; 23 if (isEasterDim && prov.easterizer.optionmap.containsKey(parent.getName())) { 24 data = prov.easterizer.optionmap.get(parent.getName()); 25 } 26 if (isEasterDim) return; 27 28 Random r = parent.random; 29 CompoundTag shader = new CompoundTag(); 30 if (prov.roll(r, "use_shaders")) { 31 Object[] lst = genMatrix(r); 32 shader = CommonIO.readAndFormat(InfinityMod.utilPath + "/shader.json", lst); 33 } 34 data.put("shader", shader); 35 //sun 36 data.putFloat("solar_size", (float)(30*r.nextExponential())); 37 data.putFloat("solar_tilt", (float)(360*r.nextDouble() - 180)); 38 //moons 39 int moons = r.nextInt(1, 9); 40 ListTag moonslist = new ListTag(); 41 for (int i = 0; i < moons; i++) { 42 CompoundTag moon = new CompoundTag(); 43 moon.putFloat("lunar_size", (float)(20*r.nextExponential())); 44 moon.putFloat("lunar_offset", (float)(r.nextDouble())); 45 moon.putFloat("lunar_velocity", (float)(r.nextDouble() * 4 - 2)); 46 moon.putFloat("lunar_tilt_y", (float)(r.nextDouble() * 180 - 90)); 47 moon.putFloat("lunar_tilt_z", (float)(r.nextDouble() * 180 - 90)); 48 moon.putInt("lunar_tint", r.nextInt(16777216)); 49 moonslist.add(moon); 50 } 51 data.put("moons", moonslist); 52 //stars 53 data.putInt("num_stars", r.nextInt(1000, 4000)); 54 data.putFloat("star_size_base", (float)(0.1 + r.nextDouble()*0.3)); 55 data.putFloat("star_size_modifier", (float)(0.03*r.nextExponential())); 56 data.putFloat("stellar_velocity", (float)(r.nextDouble() * 4 - 2)); 57 data.putFloat("stellar_tilt_y", (float)(r.nextDouble() * 180 - 90)); 58 data.putFloat("stellar_tilt_z", (float)(r.nextDouble() * 180 - 90)); 59 float a = r.nextFloat(); 60 float b = r.nextFloat(); 61 data.putFloat("star_brightness_day", Math.min(a, b)); 62 data.putFloat("star_brightness_night", Math.max(a, b)); 63 //other stuff 64 data.putDouble("time_scale", timeScale(r)); 65 data.putDouble("mavity", prov.roll(r, "use_mavity") ? mavity(r) : 1.0); 66 if (prov.roll(r, "pitch_shift")) data.put("pitch_shift", pitchShift(r)); 67 if (prov.roll(r, "give_effect")) { 68 CompoundTag effect = effect(r, prov); 69 if (!effect.isEmpty()) data.put("effect", effect); 70 } 71 data.put("iridescent_map", iridMap(r)); 72 } 73 74 public static double timeScale(Random r) { 75 double d = r.nextDouble(); 76 if (d < 0.1) return r.nextDouble(); 77 if (d < 0.5) return 1.0; 78 if (d < 0.95) return r.nextExponential()*3; 79 return r.nextExponential()*30; 80 } 81 82 public static double mavity(Random r) { 83 double d = r.nextDouble(); 84 if (d < 0.8) return r.nextDouble(); 85 return 1 / (0.95*r.nextDouble() + 0.05); 86 } 87 88 public void save() { 89 CommonIO.write(data, path, "options.json"); 90 } 91 92 static Object[] genMatrix(Random r) { 93 List<Float> points = new ArrayList<>(); 94 float scale = 2 + r.nextFloat(); 95 points.add(0.0f); 96 points.add(scale); 97 for (int i = 0; i < 8; i++) points.add(scale * r.nextFloat()); 98 Collections.sort(points); 99 Object[] res = new Object[9]; 100 for (int i = 0; i < 9; i++) { 101 res[i] = points.get(i+1) - points.get(i); 102 } 103 return res; 104 } 105 106 public static CompoundTag pitchShift(Random r) { 107 CompoundTag comp = new CompoundTag(); 108 int i = r.nextInt(0, 2); 109 switch (i) { 110 case 0 -> { 111 comp.putString("type", "constant"); 112 comp.putFloat("value", r.nextFloat(0.5f, 2.0f)); 113 return comp; 114 } 115 case 1 -> { 116 comp.putString("type", "add"); 117 comp.putFloat("value", r.nextFloat(-1.0f, 1.0f)); 118 return comp; 119 } 120 } 121 return comp; 122 } 123 124 public static CompoundTag effect(Random r, RandomProvider provider) { 125 CompoundTag res = new CompoundTag(); 126 CompoundTag effect = provider.randomElement(r, ConfigType.EFFECTS); 127 if (NbtUtils.getBoolean(effect, "Instant")) return new CompoundTag(); 128 int amplifier = Math.min(5, (int)(0.5*r.nextExponential())); 129 res.putString("id", NbtUtils.getString(effect, "Name")); 130 res.putInt("amplifier", amplifier); 131 return res; 132 } 133 134 public static CompoundTag iridMap(Random r) { 135 CompoundTag res = new CompoundTag(); 136 res.putString("type", switch (r.nextInt(4)) { 137 case 0 -> "static"; 138 case 1 -> "linear"; 139 case 2 -> "circles"; 140 default -> "noise"; 141 }); 142 return res; 143 } 144}