Commit of a hook for adding items into world dungeon generation.

Uses a weighted random selection scheme.
Also a hook for adding custom mobs to the dungeon generation list.
This commit is contained in:
LexManos 2011-12-20 07:00:43 +00:00
parent 4c6e03e4ad
commit c2f165f211
5 changed files with 401 additions and 0 deletions

View file

@ -0,0 +1,51 @@
package net.minecraft.src.forge;
import java.util.Random;
import net.minecraft.src.ItemStack;
/**
*
* Used to hold a list of all items that can be spawned in a world dungeon
*
*/
public class DungeonLoot
{
private ItemStack item;
private int minCount = 1;
private int maxCount = 1;
/**
* @param item A item stack
* @param min Minimum stack size when randomly generating
* @param max Maximum stack size when randomly generating
*/
public DungeonLoot(ItemStack item, int min, int max)
{
this.item = item;
minCount = min;
maxCount = max;
}
/**
* Grabs a ItemStack ready to be added to the dungeon chest,
* the stack size will be between minCount and maxCount
* @param rand World gen random number generator
* @return The ItemStack to be added to the chest
*/
public ItemStack generateStack(Random rand)
{
ItemStack ret = this.item.copy();
ret.stackSize = minCount + (rand.nextInt(maxCount - minCount + 1));
return ret;
}
public boolean equals(ItemStack item, int min, int max)
{
return (min == minCount && max == maxCount && item.isItemEqual(this.item));
}
public boolean equals(ItemStack item)
{
return item.isItemEqual(this.item);
}
}

View file

@ -365,4 +365,246 @@ public class MinecraftForge {
}
}
private static int dungeonLootAttempts = 8;
private static ArrayList<ObjectPair<Float, String>> dungeonMobs = new ArrayList<ObjectPair<Float, String>>();
private static ArrayList<ObjectPair<Float, DungeonLoot>> dungeonLoot = new ArrayList<ObjectPair<Float, DungeonLoot>>();
/**
* Set the number of item stacks that will be attempted to be added to each Dungeon chest.
* Note: Due to random number generation, you will not always get this amount per chest.
* @param number The maximum number of item stacks to add to a chest.
*/
public static void setDungeonLootTries(int number)
{
dungeonLootAttempts = number;
}
/**
* @return The max number of item stacks found in each dungeon chest.
*/
public static int getDungeonLootTries()
{
return dungeonLootAttempts;
}
/**
* Adds a mob to the possible list of creatures the spawner will create.
* If the mob is already in the spawn list, the rarity will be added to the existing one,
* causing the mob to be more common.
*
* @param name The name of the monster, use the same name used when registering the entity.
* @param rarity The rarity of selecting this mob over others. Must be greater then 0.
* Vanilla Minecraft has the following mobs:
* Spider 1
* Skeleton 1
* Zombie 2
* Meaning, Zombies are twice as common as spiders or skeletons.
* @return The new rarity of the monster,
*/
public static float addDungeonMob(String name, float rarity)
{
if (rarity <= 0)
{
throw new IllegalArgumentException("Rarity must be greater then zero");
}
for(ObjectPair<Float, String> mob : dungeonMobs)
{
if (name.equals(mob.getValue2()))
{
mob.setValue1(mob.getValue1() + rarity);
return mob.getValue1();
}
}
dungeonMobs.add(new ObjectPair<Float, String>(rarity, name));
return rarity;
}
/**
* Will completely remove a Mob from the dungeon spawn list.
*
* @param name The name of the mob to remove
* @return The rarity of the removed mob, prior to being removed.
*/
public static float removeDungeonMob(String name)
{
for (ObjectPair<Float, String> mob : dungeonMobs)
{
if (name.equals(name))
{
dungeonMobs.remove(mob);
return mob.getValue1();
}
}
return 0;
}
/**
* Gets a random mob name from the list.
* @param rand World generation random number generator
* @return The mob name
*/
public static String getRandomDungeonMob(Random rand)
{
float maxRarity = 0f;
for (ObjectPair<Float, String> mob : dungeonMobs)
{
maxRarity += mob.getValue1();
}
float targetRarity = rand.nextFloat() * maxRarity;
for (ObjectPair<Float, String> mob : dungeonMobs)
{
if (targetRarity < mob.getValue1())
{
return mob.getValue2();
}
targetRarity -= mob.getValue1();
}
return "";
}
/**
* Adds a item stack to the dungeon loot list with a stack size
* of 1.
*
* @param item The ItemStack to be added to the loot list
* @param rarity The relative chance that this item will spawn, Vanilla has
* most of its items set to 1. Like the saddle, bread, silk, wheat, etc..
* Rarer items are set to lower values, EXA: Golden Apple 0.01
*/
public static void addDungeonLoot(ItemStack item, float rarity)
{
addDungeonLoot(item, rarity, 1, 1);
}
/**
* Adds a item stack, with a range of sizes, to the dungeon loot list.
* If a stack matching the same item, and size range, is already in the list
* the rarities will be added together making the item more common.
*
* @param item The ItemStack to be added to the loot list
* @param rarity The relative chance that this item will spawn, Vanilla has
* most of its items set to 1. Like the saddle, bread, silk, wheat, etc..
* Rarer items are set to lower values, EXA: Golden Apple 0.01
* @param minCount When this item does generate, the minimum number that is in the stack
* @param maxCount When this item does generate, the maximum number that can bein the stack
* @return The new rarity of the loot.
*/
public static float addDungeonLoot(ItemStack item, float rarity, int minCount, int maxCount)
{
for (ObjectPair<Float, DungeonLoot> loot : dungeonLoot)
{
if (loot.getValue2().equals(item, minCount, maxCount))
{
loot.setValue1(loot.getValue1() + rarity);
return loot.getValue1();
}
}
dungeonLoot.add(new ObjectPair<Float, DungeonLoot>(rarity, new DungeonLoot(item, minCount, maxCount)));
return rarity;
}
/**
* Removes a item stack from the dungeon loot list, this will remove all items
* as long as the item stack matches, it will not care about matching the stack
* size ranges perfectly.
*
* @param item The item stack to remove
* @return The total rarity of all items removed
*/
public static float removeDungeonLoot(ItemStack item)
{
return removeDungeonLoot(item, -1, 0);
}
/**
* Removes a item stack from the dungeon loot list. If 'minCount' parameter
* is greater then 0, it will only remove loot items that have the same exact
* stack size range as passed in by parameters.
*
* @param item The item stack to remove
* @param minCount The minimum count for the match check, if less then 0,
* the size check is skipped
* @param maxCount The max count used in match check when 'minCount' is >= 0
* @return The total rarity of all items removed
*/
public static float removeDungeonLoot(ItemStack item, int minCount, int maxCount)
{
float rarity = 0;
ArrayList<ObjectPair<Float, DungeonLoot>> lootTmp = (ArrayList<ObjectPair<Float, DungeonLoot>>)dungeonLoot.clone();
if (minCount < 0)
{
for (ObjectPair<Float, DungeonLoot> loot : lootTmp)
{
if (loot.getValue2().equals(item))
{
dungeonLoot.remove(loot);
rarity += loot.getValue1();
}
}
}
else
{
for (ObjectPair<Float, DungeonLoot> loot : lootTmp)
{
if (loot.getValue2().equals(item, minCount, maxCount))
{
dungeonLoot.remove(loot);
rarity += loot.getValue1();
}
}
}
return rarity;
}
/**
* Gets a random item stack to place in a dungeon chest during world generation
* @param rand World generation random number generator
* @return The item stack
*/
public static ItemStack getRandomDungeonLoot(Random rand)
{
float maxRarity = 0f;
for (ObjectPair<Float, DungeonLoot> loot : dungeonLoot)
{
maxRarity += loot.getValue1();
}
float targetRarity = rand.nextFloat() * maxRarity;
for (ObjectPair<Float, DungeonLoot> loot : dungeonLoot)
{
if (targetRarity < loot.getValue1())
{
return loot.getValue2().generateStack(rand);
}
targetRarity -= loot.getValue1();
}
return null;
}
static
{
addDungeonMob("Skeleton", 1.0f);
addDungeonMob("Zombie", 2.0f);
addDungeonMob("Spider", 1.0f);
addDungeonLoot(new ItemStack(Item.saddle), 1.00f );
addDungeonLoot(new ItemStack(Item.ingotIron), 1.00f, 1, 4);
addDungeonLoot(new ItemStack(Item.bread), 1.00f );
addDungeonLoot(new ItemStack(Item.wheat), 1.00f, 1, 4);
addDungeonLoot(new ItemStack(Item.gunpowder), 1.00f, 1, 4);
addDungeonLoot(new ItemStack(Item.silk), 1.00f, 1, 4);
addDungeonLoot(new ItemStack(Item.bucketEmpty), 1.00f );
addDungeonLoot(new ItemStack(Item.appleGold), 0.01f );
addDungeonLoot(new ItemStack(Item.redstone), 0.50f, 1, 4);
addDungeonLoot(new ItemStack(Item.record13), 0.05f );
addDungeonLoot(new ItemStack(Item.recordCat), 0.05f );
addDungeonLoot(new ItemStack(Item.dyePowder, 1, 3), 1.00f );
}
}

View file

@ -0,0 +1,40 @@
package net.minecraft.src.forge;
/**
* A class that holds two generic values, can be used as a Key/Value pair,
* but is used in forge as a frequency/object pair.
*
* @param <T1> The type of the first value
* @param <T2> The Type of the second value
*/
public class ObjectPair<T1, T2>
{
private T1 object1;
private T2 object2;
public ObjectPair(T1 obj1, T2 obj2)
{
this.object1 = obj1;
this.object2 = obj2;
}
public T1 getValue1()
{
return this.object1;
}
public T2 getValue2()
{
return this.object2;
}
public void setValue1(T1 value)
{
object1 = value;
}
public void setValue2(T2 value)
{
object2 = value;
}
}

View file

@ -0,0 +1,34 @@
--- ../src_base/minecraft/net/minecraft/src/WorldGenDungeons.java 0000-00-00 00:00:00.000000000 -0000
+++ ../src_work/minecraft/net/minecraft/src/WorldGenDungeons.java 0000-00-00 00:00:00.000000000 -0000
@@ -7,6 +7,8 @@
import java.io.PrintStream;
import java.util.Random;
+import net.minecraft.src.forge.MinecraftForge;
+
// Referenced classes of package net.minecraft.src:
// WorldGenerator, World, Material, Block,
// TileEntityChest, TileEntityMobSpawner, ItemStack, Item
@@ -129,11 +131,11 @@
int k4 = 0;
do
{
- if(k4 >= 8)
+ if(k4 >= MinecraftForge.getDungeonLootTries())
{
break label0;
}
- ItemStack itemstack = pickCheckLootItem(random);
+ ItemStack itemstack = MinecraftForge.getRandomDungeonLoot(random);
if(itemstack != null)
{
tileentitychest.setInventorySlotContents(random.nextInt(tileentitychest.getSizeInventory()), itemstack);
@@ -148,7 +150,7 @@
TileEntityMobSpawner tileentitymobspawner = (TileEntityMobSpawner)world.getBlockTileEntity(i, j, k);
if(tileentitymobspawner != null)
{
- tileentitymobspawner.setMobID(pickMobSpawner(random));
+ tileentitymobspawner.setMobID(MinecraftForge.getRandomDungeonMob(random));
} else
{
System.err.println((new StringBuilder()).append("Failed to fetch mob spawner entity at (").append(i).append(", ").append(j).append(", ").append(k).append(")").toString());

View file

@ -0,0 +1,34 @@
--- ../src_base/minecraft_server/net/minecraft/src/WorldGenDungeons.java 0000-00-00 00:00:00.000000000 -0000
+++ ../src_work/minecraft_server/net/minecraft/src/WorldGenDungeons.java 0000-00-00 00:00:00.000000000 -0000
@@ -7,6 +7,8 @@
import java.io.PrintStream;
import java.util.Random;
+import net.minecraft.src.forge.MinecraftForge;
+
// Referenced classes of package net.minecraft.src:
// WorldGenerator, World, Material, Block,
// TileEntityChest, TileEntityMobSpawner, ItemStack, Item
@@ -129,11 +131,11 @@
int k4 = 0;
do
{
- if(k4 >= 8)
+ if(k4 >= MinecraftForge.getDungeonLootTries())
{
break label0;
}
- ItemStack itemstack = pickCheckLootItem(random);
+ ItemStack itemstack = MinecraftForge.getRandomDungeonLoot(random);
if(itemstack != null)
{
tileentitychest.setInventorySlotContents(random.nextInt(tileentitychest.getSizeInventory()), itemstack);
@@ -148,7 +150,7 @@
TileEntityMobSpawner tileentitymobspawner = (TileEntityMobSpawner)world.getBlockTileEntity(i, j, k);
if(tileentitymobspawner != null)
{
- tileentitymobspawner.setMobID(pickMobSpawner(random));
+ tileentitymobspawner.setMobID(MinecraftForge.getRandomDungeonMob(random));
} else
{
System.err.println((new StringBuilder()).append("Failed to fetch mob spawner entity at (").append(i).append(", ").append(j).append(", ").append(k).append(")").toString());