From 8a241bb6716db6041bb11385558a43a37b53f60b Mon Sep 17 00:00:00 2001 From: Daniel Mills Date: Mon, 19 Oct 2020 06:10:03 -0400 Subject: [PATCH] Fix loot --- .../volmit/iris/gen/layer/GenLayerUpdate.java | 107 +++++++----------- .../com/volmit/iris/object/IrisCompat.java | 16 +++ .../java/com/volmit/iris/object/IrisLoot.java | 4 +- .../com/volmit/iris/object/IrisLootTable.java | 23 +++- 4 files changed, 81 insertions(+), 69 deletions(-) diff --git a/src/main/java/com/volmit/iris/gen/layer/GenLayerUpdate.java b/src/main/java/com/volmit/iris/gen/layer/GenLayerUpdate.java index 9393d803b..27ca46172 100644 --- a/src/main/java/com/volmit/iris/gen/layer/GenLayerUpdate.java +++ b/src/main/java/com/volmit/iris/gen/layer/GenLayerUpdate.java @@ -1,5 +1,6 @@ package com.volmit.iris.gen.layer; +import java.util.Arrays; import java.util.Random; import org.bukkit.Chunk; @@ -196,24 +197,16 @@ public class GenLayerUpdate extends BlockPopulator { KList items = new KList<>(); - for(int t = 0; t < gen.getDimension().getLootTries(); t++) + int b = 4; + for(IrisLootTable i : tables) { - int b = 4; - for(IrisLootTable i : tables) - { - b++; - items.addAll(i.getLoot(debug, rng.nextParallelRNG(345911 * -t), slot, x, y, z, t + b + b, mgf + b)); - } + b++; + items.addAll(i.getLoot(debug, items.isEmpty(), rng.nextParallelRNG(345911), slot, x, y, z, b + b, mgf + b)); + } - for(ItemStack i : items) - { - inv.addItem(i); - } - - if(items.isNotEmpty()) - { - break; - } + for(ItemStack i : items) + { + inv.addItem(i); } scramble(inv, rng); @@ -231,11 +224,12 @@ public class GenLayerUpdate extends BlockPopulator if(slot != null) { KList tables = getLootTables(rng.nextParallelRNG(4568111), b); + InventorySlotType slott = slot; try { InventoryHolder m = (InventoryHolder) b.getState(); - addItems(false, m.getInventory(), rng, tables, slot, rx, b.getY(), rz, 15); + addItems(false, m.getInventory(), rng, tables, slott, rx, b.getY(), rz, 15); } catch(Throwable ignored) @@ -247,64 +241,49 @@ public class GenLayerUpdate extends BlockPopulator public void scramble(Inventory inventory, RNG rng) { - KList v = new KList<>(); + ItemStack[] items = inventory.getContents(); + ItemStack[] nitems = new ItemStack[inventory.getSize()]; + System.arraycopy(items, 0, nitems, 0, items.length); + boolean packedFull = false; - for(ItemStack i : inventory.getContents()) + splitting: for(int i = 0; i < nitems.length; i++) { - if(i == null) - { - continue; - } + ItemStack is = nitems[i]; - v.add(i); + if(is != null && is.getAmount() > 1 && !packedFull) + { + for(int j = 0; j < nitems.length; j++) + { + if(nitems[j] == null) + { + int take = rng.nextInt(is.getAmount()); + take = take == 0 ? 1 : take; + is.setAmount(is.getAmount() - take); + nitems[j] = is.clone(); + nitems[j].setAmount(take); + continue splitting; + } + } + + packedFull = true; + } } - inventory.clear(); - int sz = inventory.getSize(); - int tr = 5; - - while(v.isNotEmpty()) + for(int i = 0; i < 4; i++) { - int slot = rng.i(0, sz - 1); - - if(inventory.getItem(slot) == null) - { - tr = tr < 5 ? tr + 1 : tr; - int pick = rng.i(0, v.size() - 1); - ItemStack g = v.get(pick); - - if(g.getAmount() == 1) - { - v.remove(pick); - inventory.setItem(pick, g); - } - - else - { - int portion = rng.i(1, g.getAmount() - 1); - ItemStack port = g.clone(); - port.setAmount(portion); - g.setAmount(g.getAmount() - portion); - v.add(g); - inventory.setItem(slot, port); - } - } - - else - { - tr--; - } - - if(tr <= 0) + try { + Arrays.parallelSort(nitems, (a, b) -> rng.nextInt()); break; } + + catch(Throwable e) + { + + } } - for(ItemStack i : v) - { - inventory.addItem(i); - } + inventory.setContents(nitems); } public void updateLight(Block b, FastBlockData data) diff --git a/src/main/java/com/volmit/iris/object/IrisCompat.java b/src/main/java/com/volmit/iris/object/IrisCompat.java index 281134f9c..eaff4653e 100644 --- a/src/main/java/com/volmit/iris/object/IrisCompat.java +++ b/src/main/java/com/volmit/iris/object/IrisCompat.java @@ -94,8 +94,16 @@ public class IrisCompat return txf; } + int nomore = 64; + searching: while(true) { + if(nomore < 0) + { + return B.parseBlockDataOrNull("STONE").getType(); + } + + nomore--; if(err-- <= 0) { break; @@ -127,9 +135,17 @@ public class IrisCompat { return tx.getType(); } + nomore = 64; searching: while(true) { + if(nomore < 0) + { + return B.parseBlockDataOrNull("STONE").getType(); + } + + nomore--; + if(err-- <= 0) { return B.parseBlockDataOrNull("STONE").getType(); diff --git a/src/main/java/com/volmit/iris/object/IrisLoot.java b/src/main/java/com/volmit/iris/object/IrisLoot.java index f11ab572b..6928152f7 100644 --- a/src/main/java/com/volmit/iris/object/IrisLoot.java +++ b/src/main/java/com/volmit/iris/object/IrisLoot.java @@ -217,14 +217,14 @@ public class IrisLoot return new ItemStack(Material.AIR); } - public ItemStack get(boolean debug, IrisLootTable table, RNG rng, int x, int y, int z) + public ItemStack get(boolean debug, boolean giveSomething, IrisLootTable table, RNG rng, int x, int y, int z) { if(debug) { chance.reset(); } - if(chance.aquire(() -> NoiseStyle.STATIC.create(rng)).fit(1, rarity * table.getRarity(), x, y, z) == 1) + if(giveSomething || chance.aquire(() -> NoiseStyle.STATIC.create(rng)).fit(1, rarity * table.getRarity(), x, y, z) == 1) { if(getType() == null) { diff --git a/src/main/java/com/volmit/iris/object/IrisLootTable.java b/src/main/java/com/volmit/iris/object/IrisLootTable.java index f5bc64c48..bbe485b2b 100644 --- a/src/main/java/com/volmit/iris/object/IrisLootTable.java +++ b/src/main/java/com/volmit/iris/object/IrisLootTable.java @@ -39,14 +39,19 @@ public class IrisLootTable extends IrisRegistrant @MinNumber(1) @DontObfuscate @Desc("The maximum amount of loot that can be picked in this table at a time.") - private int maxPicked = 3; + private int maxPicked = 5; + + @MinNumber(0) + @DontObfuscate + @Desc("The minimum amount of loot that can be picked in this table at a time.") + private int minPicked = 1; @DontObfuscate @Desc("The loot in this table") @ArrayType(min = 1, type = IrisLoot.class) private KList loot = new KList<>(); - public KList getLoot(boolean debug, RNG rng, InventorySlotType slot, int x, int y, int z, int gg, int ffs) + public KList getLoot(boolean debug, boolean doSomething, RNG rng, InventorySlotType slot, int x, int y, int z, int gg, int ffs) { KList lootf = new KList<>(); @@ -56,7 +61,7 @@ public class IrisLootTable extends IrisRegistrant { if(i.getSlotTypes().equals(slot)) { - ItemStack item = i.get(debug, this, rng, x, y, z); + ItemStack item = i.get(debug, false, this, rng, x, y, z); if(item != null) { @@ -72,6 +77,18 @@ public class IrisLootTable extends IrisRegistrant } } + if(lootf.size() < getMinPicked()) + { + for(int i = 0; i < getMinPicked() - lootf.size(); i++) + { + ItemStack item = loot.get(rng.nextParallelRNG(3945).nextInt(loot.size())).get(debug, doSomething, this, rng, x, y, z); + if(item != null) + { + lootf.add(item); + } + } + } + return lootf; } }