Java Code Examples for com.watabou.utils.Random

The following examples show how to use com.watabou.utils.Random. These examples are extracted from open source projects. 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
private void placeKingRoom() {
	ArrayList<Room> candidates = new ArrayList<>();
	for (Room r : getRoomExit().neighbours) {
		if (!getRoomExit().connected.containsKey( r ) &&
			(getRoomExit().left == r.right || getRoomExit().right == r.left || getRoomExit().bottom == r.top) &&
			!(r.type == Type.EXIT)) {
			candidates.add( r );
		}
	}

	if (!candidates.isEmpty()) {
		Room kingsRoom = Random.element( candidates );
		kingsRoom.connect(getRoomExit());
		kingsRoom.type = Type.RAT_KING;
	}
}
 
Example 2
private static void paintBurned( Level level, Room room ) {
	for (int i=room.top + 1; i < room.bottom; i++) {
		for (int j=room.left + 1; j < room.right; j++) {
			int cell = i * Level.WIDTH + j;
			int t = Terrain.EMBERS;
			switch (Random.Int( 5 )) {
			case 0:
				t = Terrain.EMPTY;
				break;
			case 1:
				t = Terrain.TRAP;
				level.setTrap(new FireTrap().reveal(), cell);
				break;
			case 2:
				t = Terrain.SECRET_TRAP;
				level.setTrap(new FireTrap().hide(), cell);
				break;
			case 3:
				t = Terrain.INACTIVE_TRAP;
				break;
			}
			level.map[cell] = t;
		}
	}
}
 
Example 3
@Override
public boolean act() {
	if (target.isAlive()) {
		if (Dungeon.depth > 4)
			target.damage( Dungeon.depth/5, this );
		else if (Random.Int(2) == 0)
			target.damage( 1, this );
		if (!target.isAlive() && target == Dungeon.hero) {
			Dungeon.fail( getClass() );
			GLog.n( Messages.get(this, "ondeath") );
		}
		spend( TICK );
		left -= TICK;
		if (left <= 0){
			detach();
		}
	} else {
		detach();
	}
	if (Dungeon.level.water[target.pos]) {
		detach();
	}
	return true;
}
 
Example 4
@Override
public void update() {
	
	if (visible = (pos < Dungeon.level.heroFOV.length && Dungeon.level.heroFOV[pos])) {
		
		super.update();
		
		if ((delay -= Game.elapsed) <= 0) {
			
			delay = Random.Float( 5 );
			
			((WindParticle)recycle( WindParticle.class )).reset(
				x + Random.Float( DungeonTilemap.SIZE ),
				y + Random.Float( DungeonTilemap.SIZE ) );
		}
	}
}
 
Example 5
Source Project: shattered-pixel-dungeon   Source File: Gold.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean doPickUp( Hero hero ) {
	
	Dungeon.gold += quantity;
	Statistics.goldCollected += quantity;
	Badges.validateGoldCollected();

	MasterThievesArmband.Thievery thievery = hero.buff(MasterThievesArmband.Thievery.class);
	if (thievery != null)
		thievery.collect(quantity);

	GameScene.pickUp( this, hero.pos );
	hero.sprite.showStatus( CharSprite.NEUTRAL, TXT_VALUE, quantity );
	hero.spendAndNext( TIME_TO_PICK_UP );
	
	Sample.INSTANCE.play( Assets.Sounds.GOLD, 1, 1, Random.Float( 0.9f, 1.1f ) );
	
	return true;
}
 
Example 6
Source Project: unleashed-pixel-dungeon   Source File: Holy.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean proc( Weapon weapon, Char attacker, Char defender, int damage ) {
    int curDamage = 0;
    int level = Math.max( 0, weapon.level );

    if (defender.TYPE_UNDEAD) {
        if (Random.Int(level + 4) >= 3) {
            curDamage += Random.Int(0, damage + weapon.level);
            defender.damage(curDamage, this);
            return true;
        }
    } else if (defender.TYPE_EVIL) {
        if (Random.Int(level + 4) >= 5) {
            curDamage += Random.Int(0, ((damage + weapon.level) / 2));
            defender.damage(curDamage, this);
            return true;
        }
    }
    return false;
}
 
Example 7
@Override
public void explode(int cell) {
	super.explode(cell);
	
	PathFinder.buildDistanceMap( cell, BArray.not( Dungeon.level.solid, null ), 2 );
	for (int i = 0; i < PathFinder.distance.length; i++) {
		if (PathFinder.distance[i] < Integer.MAX_VALUE) {
			if (Dungeon.level.insideMap(i)
					&& Actor.findChar(i) == null
					&& !(Dungeon.level.pit[i])) {
				Sheep sheep = new Sheep();
				sheep.lifespan = Random.NormalIntRange( 8, 16 );
				sheep.pos = i;
				Dungeon.level.occupyCell(sheep);
				GameScene.add(sheep);
				CellEmitter.get(i).burst(Speck.factory(Speck.WOOL), 4);
			}
		}
	}
	
	Sample.INSTANCE.play(Assets.Sounds.PUFF);
	
	
}
 
Example 8
@Override
public void update() {
	float x2 = (start.x + end.x) / 2 + Random.Float( -4, +4 );
	float y2 = (start.y + end.y) / 2 + Random.Float( -4, +4 );

	float dx = x2 - start.x;
	float dy = y2 - start.y;
	arc1.angle = (float)(Math.atan2( dy, dx ) * A);
	arc1.scale.x = (float)Math.sqrt( dx * dx + dy * dy ) / arc1.width;

	dx = end.x - x2;
	dy = end.y - y2;
	arc2.angle = (float)(Math.atan2( dy, dx ) * A);
	arc2.scale.x = (float)Math.sqrt( dx * dx + dy * dy ) / arc2.width;
	arc2.x = x2 - arc2.origin.x;
	arc2.y = y2 - arc2.origin.x;
}
 
Example 9
@Override
public int damageRoll(Char owner) {
	if (owner instanceof Hero) {
		Hero hero = (Hero)owner;
		if (enemy instanceof Mob && ((Mob) enemy).surprisedBy(hero)) {
			//deals 60% toward max to max on surprise, instead of min to max.
			int diff = max() - min();
			int damage = augment.damageFactor(Random.NormalIntRange(
					min() + Math.round(diff*0.6f),
					max()));
			int exStr = hero.STR() - STRReq();
			if (exStr > 0) {
				damage += Random.IntRange(0, exStr);
			}
			return damage;
		}
	}
	return super.damageRoll(owner);
}
 
Example 10
@Override
public void pressHero(int cell, Hero hero ) {
	
	super.pressHero( cell, hero );
	
	if (!enteredArena && outsideEntranceRoom( cell ) && hero == Dungeon.hero) {
		
		enteredArena = true;

		int pos;

		do {
			pos = Random.Int(getLength());
		} while (
				!passable[pos] ||
						!outsideEntranceRoom(pos ) ||
						Dungeon.visible[pos]);

		spawnBoss(pos);
	}
}
 
Example 11
protected final Point getDoorCenter(){
	PointF doorCenter = new PointF(0, 0);

	for (Door door : connected.values()) {
		doorCenter.x += door.x;
		doorCenter.y += door.y;
	}

	Point c = new Point((int)doorCenter.x / connected.size(), (int)doorCenter.y / connected.size());
	if (Random.Float() < doorCenter.x % 1) c.x++;
	if (Random.Float() < doorCenter.y % 1) c.y++;
	c.x = (int)GameMath.gate(left+1, c.x, right-1);
	c.y = (int)GameMath.gate(top+1, c.y, bottom-1);

	return c;
}
 
Example 12
protected final Point getDoorCenter(){
	PointF doorCenter = new PointF(0, 0);

	for (Door door : connected.values()) {
		doorCenter.x += door.x;
		doorCenter.y += door.y;
	}

	Point c = new Point((int)doorCenter.x / connected.size(), (int)doorCenter.y / connected.size());
	if (Random.Float() < doorCenter.x % 1) c.x++;
	if (Random.Float() < doorCenter.y % 1) c.y++;
	c.x = (int) GameMath.gate(left+2, c.x, right-2);
	c.y = (int)GameMath.gate(top+2, c.y, bottom-2);

	return c;
}
 
Example 13
Source Project: shattered-pixel-dungeon   Source File: Goo.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void die( Object cause ) {
	
	super.die( cause );
	
	Dungeon.level.unseal();
	
	GameScene.bossSlain();
	Dungeon.level.drop( new SkeletonKey( Dungeon.depth ), pos ).sprite.drop();
	
	//60% chance of 2 blobs, 30% chance of 3, 10% chance for 4. Average of 2.5
	int blobs = Random.chances(new float[]{0, 0, 6, 3, 1});
	for (int i = 0; i < blobs; i++){
		int ofs;
		do {
			ofs = PathFinder.NEIGHBOURS8[Random.Int(8)];
		} while (!Dungeon.level.passable[pos + ofs]);
		Dungeon.level.drop( new GooBlob(), pos + ofs ).sprite.drop( pos );
	}
	
	Badges.validateBossSlain();
	
	yell( Messages.get(this, "defeated") );
}
 
Example 14
Source Project: YetAnotherPixelDungeon   Source File: Bee.java    License: GNU General Public License v3.0 6 votes vote down vote up
public Char chooseEnemy() {
	
	if (enemy == null || !enemy.isAlive()) {
		HashSet<Mob> enemies = new HashSet<Mob>();
		for (Mob mob:Dungeon.level.mobs) {
			if (mob.hostile && Level.fieldOfView[mob.pos]) {
				enemies.add( mob );
			}
		}
		
		return enemies.size() > 0 ? Random.element( enemies ) : null;
		
	} else {
		
		return enemy;
		
	}
}
 
Example 15
@Override
public int damageRoll(Char owner) {
	if (owner instanceof Hero) {
		Hero hero = (Hero)owner;
		if (enemy instanceof Mob && ((Mob) enemy).surprisedBy(hero)) {
			//deals 60% toward max to max on surprise, instead of min to max.
			int diff = max() - min();
			int damage = augment.damageFactor(Random.NormalIntRange(
					min() + Math.round(diff*0.6f),
					max()));
			int exStr = hero.STR() - STRReq();
			if (exStr > 0) {
				damage += Random.IntRange(0, exStr);
			}
			return damage;
		}
	}
	return super.damageRoll(owner);
}
 
Example 16
@Override
protected void generatePrize() {
	super.generatePrize();
	//all existing prize items are guaranteed uncursed, and have a 50% chance to be +1 if they were +0
	for (Item i : items){
		if (i instanceof EquipableItem || i instanceof Wand){
			i.cursed = false;
			i.cursedKnown = true;
			if (i instanceof Weapon && ((Weapon) i).hasCurseEnchant()){
				((Weapon) i).enchant(null);
			}
			if (i instanceof Armor && ((Armor) i).hasCurseGlyph()){
				((Armor) i).inscribe(null);
			}
			if (!(i instanceof MissileWeapon) && i.level() == 0 && Random.Int(2) == 0){
				i.upgrade();
			}
		}
	}
}
 
Example 17
public void processDamage( int damage ){
	if (target == null) return;
	ParalysisResist resist = target.buff(ParalysisResist.class);
	if (resist == null){
		resist = Buff.affect(target, ParalysisResist.class);
	}
	resist.damage += damage;
	if (Random.NormalIntRange(0, resist.damage) >= Random.NormalIntRange(0, target.HP)){
		if (Dungeon.level.heroFOV[target.pos]) {
			target.sprite.showStatus(CharSprite.NEUTRAL, Messages.get(this, "out"));
		}
		detach();
	}
}
 
Example 18
private boolean summonSubject( int delay ){
	//4th summon is always a monk or warlock, otherwise ghoul
	if (summonsMade % 4 == 3){
		return summonSubject( delay, Random.Int(2) == 0 ? DKMonk.class : DKWarlock.class );
	} else {
		return summonSubject( delay, DKGhoul.class );
	}
}
 
Example 19
Source Project: remixed-dungeon   Source File: SpiderArmor.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public int defenceProc(Char attacker, Char defender, int damage) {
	if (Random.Int(100) < 50) {
		GameScene.add(Blob.seed(defender.getPos(), Random.Int(5, 7), Web.class));
	}
	return super.defenceProc(attacker, defender, damage);
}
 
Example 20
private void checkEnemies(Hero hero) {
	var candidates = new ArrayList<Char>();

	candidates.clear();
	int v = hero.visibleEnemies();
	for (int i=0; i < v; i++) {
		Char mob = hero.visibleEnemy( i );

		if (hero.canAttack(mob) && !mob.friendly(hero)) {
			candidates.add( mob );
		}
	}
	
	if (!candidates.contains( lastTarget )) {
		if (candidates.isEmpty()) {
			lastTarget = null;
		} else {
			target( Random.element( candidates ));
			flash();
		}
	} else {
		if (!bg.getVisible()) {
			flash();
		}
	}
	
	visible( lastTarget != null );
	enable( bg.getVisible() );
}
 
Example 21
Source Project: pixel-dungeon   Source File: Gold.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean doPickUp( Hero hero ) {
	
	Dungeon.gold += quantity;
	Statistics.goldCollected += quantity;
	Badges.validateGoldCollected();
	
	GameScene.pickUp( this );
	hero.sprite.showStatus( CharSprite.NEUTRAL, TXT_VALUE, quantity );
	hero.spendAndNext( TIME_TO_PICK_UP );
	
	Sample.INSTANCE.play( Assets.SND_GOLD, 1, 1, Random.Float( 0.9f, 1.1f ) );
	
	return true;
}
 
Example 22
public FlowParticle() {
	super();
	
	lifespan = 0.6f;
	acc.set( 0, 32 );
	angularSpeed = Random.Float( -360, +360 );
}
 
Example 23
Source Project: unleashed-pixel-dungeon   Source File: Wand.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void staffFx( MagesStaff.StaffParticle particle ){
	particle.color(0xFFFFFF); particle.am = 0.3f;
	particle.setLifespan( 1f);
	particle.speed.polar( Random.Float(PointF.PI2), 2f );
	particle.setSize( 1f, 2.5f );
	particle.radiateXY(1f);
}
 
Example 24
Source Project: remixed-dungeon   Source File: King.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public int attackProc(@NotNull Char enemy, int damage ) {
	if (Random.Int( MAX_ARMY_SIZE ) == 0) {
		Buff.prolong( enemy, Paralysis.class, 1 );
	}
	
	return damage;
}
 
Example 25
Source Project: YetAnotherPixelDungeon   Source File: Char.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void move( int step ) {
	
	if (Level.adjacent( step, pos ) && Random.Int( 2 ) == 0 && ( ( buff( Vertigo.class ) != null ) ) ) {

		step = pos + Level.NEIGHBOURS8[Random.Int( 8 )];

		if ( Level.solid[step] || Actor.findChar( step ) != null ) {
			return;
		}
	}
	
	if (Dungeon.level.map[pos] == Terrain.OPEN_DOOR) {
		Door.leave( pos );
	}

       Actor.moveToCell( this, step );
       pos = step;
	
	if (Dungeon.level.map[pos] == Terrain.DOOR_CLOSED) {
		Door.enter( pos );
	}
	
	if (this != Dungeon.hero) {
		sprite.visible = Dungeon.visible[pos];
	}

       Dungeon.level.press(pos, this);

       moving = true;
}
 
Example 26
@Override
public void activate() {

	Char c = Actor.findChar( pos );

	if (c != null) {
		int damage = Math.max( 0,  (Dungeon.depth + 3) - Random.IntRange( 0, c.dr() / 2 ) );
		Buff.affect( c, Bleeding.class ).set( damage );
		Buff.prolong( c, Cripple.class, Cripple.DURATION );
		Wound.hit( c );
	} else {
		Wound.hit( pos );
	}

}
 
Example 27
Source Project: shattered-pixel-dungeon   Source File: Yog.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void spawnFists() {
	RottingFist fist1 = new RottingFist();
	BurningFist fist2 = new BurningFist();
	
	do {
		fist1.pos = pos + PathFinder.NEIGHBOURS8[Random.Int( 8 )];
		fist2.pos = pos + PathFinder.NEIGHBOURS8[Random.Int( 8 )];
	} while (!Dungeon.level.passable[fist1.pos] || !Dungeon.level.passable[fist2.pos] || fist1.pos == fist2.pos);
	
	GameScene.add( fist1 );
	GameScene.add( fist2 );

	notice();
}
 
Example 28
private static Item genEquipmentDrop( int level ){
	Item result;
	//each upgrade increases depth used for calculating drops by 1
	int floorset = (Dungeon.depth + level)/5;
	switch (Random.Int(5)){
		default: case 0: case 1:
			Weapon w = Generator.randomWeapon(floorset);
			if (!w.hasGoodEnchant() && Random.Int(10-level) == 0)   w.enchant();
			else if (w.hasCurseEnchant())                           w.enchant(null);
			result = w;
			break;
		case 2:
			Armor a = Generator.randomArmor(floorset);
			if (!a.hasGoodGlyph() && Random.Int(10-level) == 0)     a.inscribe();
			else if (a.hasCurseGlyph())                             a.inscribe(null);
			result = a;
			break;
		case 3:
			result = Generator.random(Generator.Category.RING);
			break;
		case 4:
			result = Generator.random(Generator.Category.ARTIFACT);
			break;
	}
	//minimum level of sqrt(ringLvl)
	if (result.isUpgradable()){
		if (result.level() < Math.floor(Math.sqrt(level))){
			result.level((int)Math.floor(Math.sqrt(level)));
		}
	}
	result.cursed = false;
	result.cursedKnown = true;
	if (result.level() >= 2) {
		latestDropTier = 4;
	} else {
		latestDropTier = 3;
	}
	return result;
}
 
Example 29
@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 30
public static void setupVariance(int size, long seed){
	Random.seed( seed );
	tileVariance = new byte[size];
	for (int i = 0; i < tileVariance.length; i++)
		tileVariance[i] = (byte)Random.Int(100);
	Random.seed();
}