Java Code Examples for com.watabou.utils.Random#chances()

The following examples show how to use com.watabou.utils.Random#chances() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Generator.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
public static Item random( Category cat ) {
	switch (cat) {
		case ARMOR:
			return randomArmor();
		case WEAPON:
			return randomWeapon();
		case MISSILE:
			return randomMissile();
		case ARTIFACT:
			Item item = randomArtifact();
			//if we're out of artifacts, return a ring instead.
			return item != null ? item : random(Category.RING);
		default:
			int i = Random.chances(cat.probs);
			if (i == -1) {
				reset(cat);
				i = Random.chances(cat.probs);
			}
			if (cat.defaultProbs != null) cat.probs[i]--;
			return ((Item) Reflection.newInstance(cat.classes[i])).random();
	}
}
 
Example 2
Source File: CursedWand.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
public static void cursedZap(final Item origin, final Char user, final Ballistica bolt, final Callback afterZap){
	switch (Random.chances(new float[]{COMMON_CHANCE, UNCOMMON_CHANCE, RARE_CHANCE, VERY_RARE_CHANCE})){
		case 0:
		default:
			commonEffect(origin, user, bolt, afterZap);
			break;
		case 1:
			uncommonEffect(origin, user, bolt, afterZap);
			break;
		case 2:
			rareEffect(origin, user, bolt, afterZap);
			break;
		case 3:
			veryRareEffect(origin, user, bolt, afterZap);
			break;
	}
}
 
Example 3
Source File: WandOfCorruption.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 6 votes vote down vote up
private void debuffEnemy( Mob enemy, HashMap<Class<? extends Buff>, Float> category ){
	
	//do not consider buffs which are already assigned, or that the enemy is immune to.
	HashMap<Class<? extends Buff>, Float> debuffs = new HashMap<>(category);
	for (Buff existing : enemy.buffs()){
		if (debuffs.containsKey(existing.getClass())) {
			debuffs.put(existing.getClass(), 0f);
		}
	}
	for (Class<?extends Buff> toAssign : debuffs.keySet()){
		 if (debuffs.get(toAssign) > 0 && enemy.isImmune(toAssign)){
		 	debuffs.put(toAssign, 0f);
		 }
	}
	
	//all buffs with a > 0 chance are flavor buffs
	Class<?extends FlavourBuff> debuffCls = (Class<? extends FlavourBuff>) Random.chances(debuffs);
	
	if (debuffCls != null){
		Buff.append(enemy, debuffCls, 6 + level()*3);
	} else {
		//if no debuff can be applied (all are present), then go up one tier
		if (category == MINOR_DEBUFFS)          debuffEnemy( enemy, MAJOR_DEBUFFS);
		else if (category == MAJOR_DEBUFFS)     corruptEnemy( enemy );
	}
}
 
Example 4
Source File: WandOfCorruption.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
private void debuffEnemy( Mob enemy, HashMap<Class<? extends Buff>, Float> category ){
	
	//do not consider buffs which are already assigned, or that the enemy is immune to.
	HashMap<Class<? extends Buff>, Float> debuffs = new HashMap<>(category);
	for (Buff existing : enemy.buffs()){
		if (debuffs.containsKey(existing.getClass())) {
			debuffs.put(existing.getClass(), 0f);
		}
	}
	for (Class<?extends Buff> toAssign : debuffs.keySet()){
		 if (debuffs.get(toAssign) > 0 && enemy.isImmune(toAssign)){
		 	debuffs.put(toAssign, 0f);
		 }
	}
	
	//all buffs with a > 0 chance are flavor buffs
	Class<?extends FlavourBuff> debuffCls = (Class<? extends FlavourBuff>) Random.chances(debuffs);
	
	if (debuffCls != null){
		Buff.append(enemy, debuffCls, 6 + buffedLvl()*3);
	} else {
		//if no debuff can be applied (all are present), then go up one tier
		if (category == MINOR_DEBUFFS)          debuffEnemy( enemy, MAJOR_DEBUFFS);
		else if (category == MAJOR_DEBUFFS)     corruptEnemy( enemy );
	}
}
 
Example 5
Source File: CursedWand.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
public static void cursedZap(final Wand wand, final Hero user, final Ballistica bolt){
	switch (Random.chances(new float[]{COMMON_CHANCE, UNCOMMON_CHANCE, RARE_CHANCE, VERY_RARE_CHANCE})){
		case 0:
		default:
			commonEffect(wand, user, bolt);
			break;
		case 1:
			uncommonEffect(wand, user, bolt);
			break;
		case 2:
			rareEffect(wand, user, bolt);
			break;
		case 3:
			veryRareEffect(wand, user, bolt);
			break;
	}
}
 
Example 6
Source File: OldDM300.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void die( Object cause ) {
	
	super.die( cause );
	
	GameScene.bossSlain();
	Dungeon.level.drop( new SkeletonKey( Dungeon.depth  ), pos ).sprite.drop();
	
	//60% chance of 2 shards, 30% chance of 3, 10% chance for 4. Average of 2.5
	int shards = Random.chances(new float[]{0, 0, 6, 3, 1});
	for (int i = 0; i < shards; i++){
		int ofs;
		do {
			ofs = PathFinder.NEIGHBOURS8[Random.Int(8)];
		} while (!Dungeon.level.passable[pos + ofs]);
		Dungeon.level.drop( new MetalShard(), pos + ofs ).sprite.drop( pos );
	}
	
	Badges.validateBossSlain();

	LloydsBeacon beacon = Dungeon.hero.belongings.getItem(LloydsBeacon.class);
	if (beacon != null) {
		beacon.upgrade();
	}
	
	yell( Messages.get(this, "defeated") );
}
 
Example 7
Source File: Generator.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
public static MeleeWeapon randomWeapon(int floorSet) {

		floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
		
		Category c = wepTiers[Random.chances(floorSetTierProbs[floorSet])];
		MeleeWeapon w = (MeleeWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]);
		w.random();
		return w;
	}
 
Example 8
Source File: Armor.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static Glyph random( Class<? extends Glyph> ... toIgnore ) {
	switch(Random.chances(typeChances)){
		case 0: default:
			return randomCommon( toIgnore );
		case 1:
			return randomUncommon( toIgnore );
		case 2:
			return randomRare( toIgnore );
	}
}
 
Example 9
Source File: NewDM300.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void die( Object cause ) {

	super.die( cause );

	GameScene.bossSlain();
	Dungeon.level.unseal();

	//60% chance of 2 shards, 30% chance of 3, 10% chance for 4. Average of 2.5
	int shards = Random.chances(new float[]{0, 0, 6, 3, 1});
	for (int i = 0; i < shards; i++){
		int ofs;
		do {
			ofs = PathFinder.NEIGHBOURS8[Random.Int(8)];
		} while (!Dungeon.level.passable[pos + ofs]);
		Dungeon.level.drop( new MetalShard(), pos + ofs ).sprite.drop( pos );
	}

	Badges.validateBossSlain();

	LloydsBeacon beacon = Dungeon.hero.belongings.getItem(LloydsBeacon.class);
	if (beacon != null) {
		beacon.upgrade();
	}

	yell( Messages.get(this, "defeated") );
}
 
Example 10
Source File: Generator.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public static Item random() {
	Category cat = Random.chances( categoryProbs );
	if (cat == null){
		reset();
		cat = Random.chances( categoryProbs );
	}
	categoryProbs.put( cat, categoryProbs.get( cat ) - 1);
	return random( cat );
}
 
Example 11
Source File: RegularLevel.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
protected void placeTraps() {
	
	int nTraps = nTraps();
	float[] trapChances = trapChances();

	for (int i=0; i < nTraps; i++) {
		
		int trapPos = Random.Int( LENGTH );
		
		if (map[trapPos] == Terrain.EMPTY) {
			map[trapPos] = Terrain.SECRET_TRAP;
			switch (Random.chances( trapChances )) {
			case 0:
				setTrap( new ToxicTrap().hide(), trapPos);
				break;
			case 1:
				setTrap( new FireTrap().hide(), trapPos);
				break;
			case 2:
				setTrap( new ParalyticTrap().hide(), trapPos);
				break;
			case 3:
				setTrap( new PoisonTrap().hide(), trapPos);
				break;
			case 4:
				setTrap( new AlarmTrap().hide(), trapPos);
				break;
			case 5:
				setTrap( new LightningTrap().hide(), trapPos);
				break;
			case 6:
				setTrap( new GrippingTrap().hide(), trapPos);
				break;
			case 7:
				setTrap( new LightningTrap().hide(), trapPos);
				break;
			}
		}
	}
}
 
Example 12
Source File: HallsLevel.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected int specialRooms() {
	//2 to 3, average 2.5
	return 2 + Random.chances(new float[]{1, 1});
}
 
Example 13
Source File: RegularLevel.java    From YetAnotherPixelDungeon with GNU General Public License v3.0 4 votes vote down vote up
protected void placeTraps() {
	
	int nTraps = nTraps();
	float[] trapChances = trapChances();
	
	for (int i=0; i < nTraps; i++) {
		
		int trapPos = randomTrapCell();

		if ( trapPos > -1 ) {
			switch (Random.chances( trapChances )) {
			case 0:
				map[trapPos] = Terrain.SECRET_TOXIC_TRAP;
				break;
			case 1:
				map[trapPos] = Terrain.SECRET_FIRE_TRAP;
				break;
			case 2:
				map[trapPos] = Terrain.SECRET_BOULDER_TRAP;
				break;
			case 3:
				map[trapPos] = Terrain.SECRET_POISON_TRAP;
				break;
			case 4:
				map[trapPos] = Terrain.SECRET_ALARM_TRAP;
				break;
			case 5:
				map[trapPos] = Terrain.SECRET_LIGHTNING_TRAP;
				break;
			case 6:
				map[trapPos] = Terrain.SECRET_BLADE_TRAP;
				break;
			case 7:
				map[trapPos] = Terrain.SECRET_SUMMONING_TRAP;
				break;
               case 8:
                   map[trapPos] = Terrain.INACTIVE_TRAP;
                   break;
			}
		}
	}
}
 
Example 14
Source File: HallsLevel.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected int specialRooms() {
	//2 to 3, average 2.5
	return 2 + Random.chances(new float[]{1, 1});
}
 
Example 15
Source File: PrisonLevel.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected int standardRooms() {
	//6 to 8, average 6.66
	return 6+Random.chances(new float[]{4, 2, 2});
}
 
Example 16
Source File: CityLevel.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected int standardRooms() {
	//7 to 10, average 7.9
	return 7+Random.chances(new float[]{4, 3, 2, 1});
}
 
Example 17
Source File: CavesLevel.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected int specialRooms() {
	//1 to 3, average 2.2
	return 1+Random.chances(new float[]{2, 4, 4});
}
 
Example 18
Source File: SewerLevel.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected int specialRooms() {
	//1 to 3, average 1.67
	return 1+Random.chances(new float[]{4, 4, 2});
}
 
Example 19
Source File: CavesLevel.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected int standardRooms() {
	//6 to 9, average 7.333
	return 6+Random.chances(new float[]{2, 3, 3, 1});
}
 
Example 20
Source File: ScrollOfDivination.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void doRead() {
	
	curUser.sprite.parent.add( new Identification( curUser.sprite.center().offset( 0, -16 ) ) );
	
	readAnimation();
	setKnown();
	
	Sample.INSTANCE.play( Assets.Sounds.READ );
	Invisibility.dispel();
	
	HashSet<Class<? extends Potion>> potions = Potion.getUnknown();
	HashSet<Class<? extends Scroll>> scrolls = Scroll.getUnknown();
	HashSet<Class<? extends Ring>> rings = Ring.getUnknown();
	
	int total = potions.size() + scrolls.size() + rings.size();
	
	if (total == 0){
		GLog.n( Messages.get(this, "nothing_left") );
		return;
	}
	
	ArrayList<Item> IDed = new ArrayList<>();
	int left = 4;
	
	float[] baseProbs = new float[]{3, 3, 3};
	float[] probs = baseProbs.clone();
	
	while (left > 0 && total > 0) {
		switch (Random.chances(probs)) {
			default:
				probs = baseProbs.clone();
				continue;
			case 0:
				if (potions.isEmpty()) {
					probs[0] = 0;
					continue;
				}
				probs[0]--;
				Potion p = Reflection.newInstance(Random.element(potions));
				p.setKnown();
				IDed.add(p);
				potions.remove(p.getClass());
				break;
			case 1:
				if (scrolls.isEmpty()) {
					probs[1] = 0;
					continue;
				}
				probs[1]--;
				Scroll s = Reflection.newInstance(Random.element(scrolls));
				s.setKnown();
				IDed.add(s);
				scrolls.remove(s.getClass());
				break;
			case 2:
				if (rings.isEmpty()) {
					probs[2] = 0;
					continue;
				}
				probs[2]--;
				Ring r = Reflection.newInstance(Random.element(rings));
				r.setKnown();
				IDed.add(r);
				rings.remove(r.getClass());
				break;
		}
		left --;
		total --;
	}
	
	GameScene.show(new WndDivination( IDed ));
}