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

The following examples show how to use com.watabou.utils.Random#element() . 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: Char.java    From remixed-dungeon with GNU General Public License v3.0 6 votes vote down vote up
public void move(int step) {
	if(!isMovable()) {
		return;
	}

	if (hasBuff(Vertigo.class) && level().adjacent(getPos(), step)) { //ignore vertigo when blinking or teleporting

		List<Integer> candidates = new ArrayList<>();
		for (int dir : Level.NEIGHBOURS8) {
			int p = getPos() + dir;
			if (level().cellValid(p)) {
				if ((level().passable[p] || level().avoid[p]) && Actor.findChar(p) == null) {
					candidates.add(p);
				}
			}
		}

		if (candidates.isEmpty()) { // Nowhere to move? just stay then
			return;
		}

		step = Random.element(candidates);
	}

	placeTo(step);
}
 
Example 2
Source File: RegularPainter.java    From shattered-pixel-dungeon with GNU General Public License v3.0 6 votes vote down vote up
private void placeDoors( Room r ) {
	for (Room n : r.connected.keySet()) {
		Room.Door door = r.connected.get( n );
		if (door == null) {
			
			Rect i = r.intersect( n );
			ArrayList<Point> doorSpots = new ArrayList<>();
			for (Point p : i.getPoints()){
				if (r.canConnect(p) && n.canConnect(p))
					doorSpots.add(p);
			}
			if (doorSpots.isEmpty()){
				ShatteredPixelDungeon.reportException(
						new RuntimeException("Could not place a door! " +
								"r=" + r.getClass().getSimpleName() +
								" n=" + n.getClass().getSimpleName()));
				continue;
			}
			door = new Room.Door(Random.element(doorSpots));
			
			r.connected.put( n, door );
			n.connected.put( r, door );
		}
	}
}
 
Example 3
Source File: MagicWellPainter.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@SneakyThrows
public static void paint( Level level, Room room ) {

	fill( level, room, Terrain.WALL );
	fill( level, room, 1, Terrain.EMPTY );
	
	Point c = room.center();
	set( level, c.x, c.y, Terrain.WELL );
	
	@SuppressWarnings("unchecked")
	Class<? extends WellWater> waterClass = 
		Dungeon.depth >= Dungeon.transmutation ?
		WaterOfTransmutation.class :		
		(Class<? extends WellWater>)Random.element( WATERS );
		
	if (waterClass == WaterOfTransmutation.class) {
		Dungeon.transmutation = Integer.MAX_VALUE;
	}
	
	WellWater water = (WellWater)level.blobs.get( waterClass );
	if (water == null) {
		water = waterClass.newInstance();
	}
	water.seed( c.x + level.getWidth() * c.y, 1 );
	level.blobs.put( waterClass, water );
	
	room.entrance().set( Room.Door.Type.REGULAR );
}
 
Example 4
Source File: Bee.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Char chooseEnemy() {
	//if the pot is no longer present, target the hero
	if (potHolder == -1 && potPos == -1)
		return Dungeon.hero;

	//if something is holding the pot, target that
	else if (Actor.findById(potHolder) != null)
		return (Char)Actor.findById(potHolder);

	//if the pot is on the ground
	else {

		//if already targeting something, and that thing is still alive and near the pot, keeping targeting it.
		if (enemy != null && enemy.isAlive() && Level.distance(enemy.pos, potPos) <= 3) return enemy;

		//find all mobs near the pot
		HashSet<Char> enemies = new HashSet<Char>();
		for (Mob mob : Dungeon.level.mobs)
			if (!(mob instanceof Bee) && Level.distance(mob.pos, potPos) <= 3 && (mob.hostile || mob.ally))
				enemies.add(mob);

		//pick one, if there are none, check if the hero is near the pot, go for them, otherwise go for nothing.
		if (enemies.size() > 0) return Random.element(enemies);
		else return (Level.distance(Dungeon.hero.pos, potPos) <= 3) ? Dungeon.hero : null ;
	}
}
 
Example 5
Source File: Yog.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int defenseProc( Char enemy, int damage ) {

	ArrayList<Integer> spawnPoints = new ArrayList<Integer>();
	
	for (int i=0; i < Level.NEIGHBOURS8.length; i++) {
		int p = pos + Level.NEIGHBOURS8[i];
		if (Actor.findChar( p ) == null && (Level.passable[p] || Level.avoid[p])) {
			spawnPoints.add( p );
		}
	}
	
	if (spawnPoints.size() > 0) {
		Larva larva = new Larva();
		if (Dungeon.difficultyLevel == Dungeon.DIFF_ENDLESS) {
			larva.infiniteScaleMob(Dungeon.depth + 5);
		} else {
			larva.scaleMob();
		}
		larva.pos = Random.element( spawnPoints );
		
		GameScene.add( larva );
		Actor.addDelayed( new Pushing( larva, pos, larva.pos ), -1 );
	}

	for (Mob mob : Dungeon.level.mobs) {
		if (mob instanceof BurningFist || mob instanceof RottingFist || mob instanceof Larva) {
			mob.aggro( enemy );
		}
	}

	return super.defenseProc(enemy, damage);
}
 
Example 6
Source File: SewerFly.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int defenseProc( Char enemy, int damage ) {

    if (HP >= damage + 2) {
        ArrayList<Integer> candidates = new ArrayList<>();
        boolean[] passable = Level.passable;

        int[] neighbours = {pos + 1, pos - 1, pos + Level.WIDTH, pos - Level.WIDTH};
        for (int n : neighbours) {
            if (passable[n] && Actor.findChar(n) == null) {
                candidates.add( n );
            }
        }

        if (candidates.size() > 0) {

            SewerFly clone = split();
            clone.HP = (HP - damage) / 2;
            clone.pos = Random.element(candidates);
            clone.state = clone.HUNTING;

            if (Dungeon.level.map[clone.pos] == Terrain.DOOR) {
                Door.enter(clone.pos);
            }

            GameScene.add(clone, SPLIT_DELAY);
            Actor.addDelayed( new Pushing( clone, pos, clone.pos ), -1 );

            HP -= clone.HP;
        }
    }

    return super.defenseProc(enemy, damage);
}
 
Example 7
Source File: Level.java    From YetAnotherPixelDungeon with GNU General Public License v3.0 5 votes vote down vote up
public Item itemToSpawnAsPrize() {
	if (Random.Int( itemsToSpawn.size() + 1 ) > 0) {
		Item item = Random.element( itemsToSpawn );
		itemsToSpawn.remove( item );
		return item;
	} else {
		return null;
	}
}
 
Example 8
Source File: SewerBossLevel.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void createMobs() {
	Goo mob = new Goo();
	if (Dungeon.difficultyLevel == Dungeon.DIFF_ENDLESS) {
		mob.infiniteScaleMob(Dungeon.depth + 7);
	} else {
		mob.scaleMob();
	}
	Room room;
	do {
		room = Random.element(rooms);
	} while (room.type != Type.STANDARD);
	mob.pos = room.random();
	mobs.add( mob );
}
 
Example 9
Source File: Succubus.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
private void blink( int target ) {
	
	Ballistica route = new Ballistica( pos, target, Ballistica.PROJECTILE);
	int cell = route.collisionPos;

	//can't occupy the same cell as another char, so move back one.
	if (Actor.findChar( cell ) != null && cell != this.pos)
		cell = route.path.get(route.dist-1);

	if (Level.avoid[ cell ]){
		ArrayList<Integer> candidates = new ArrayList<Integer>();
		for (int n : Level.NEIGHBOURS8) {
			cell = route.collisionPos + n;
			if (Level.passable[cell] && Actor.findChar( cell ) == null) {
				candidates.add( cell );
			}
		}
		if (candidates.size() > 0)
			cell = Random.element(candidates);
		else
			return;
	}
	
	ScrollOfTeleportation.appear( this, cell );
	
	delay = BLINK_DELAY;
}
 
Example 10
Source File: RegularLevel.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
protected Room randomRoom(Room.Type type, int tries) {
    if(rooms.isEmpty()) {
        return null;
       }

	for (int i = 0; i < tries; i++) {
		Room room = Random.element(rooms);
		if (room.type == type) {
			return room;
		}
	}
	return null;
}
 
Example 11
Source File: QuickSlot.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public Item randomNonePlaceholder(){

		ArrayList<Item> result = new ArrayList<>();
		for (int i = 0; i < SIZE; i ++)
		if (getItem(i) != null && !isPlaceholder(i))
				result.add(getItem(i));

		return Random.element(result);
	}
 
Example 12
Source File: DriedRose.java    From unleashed-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Char chooseEnemy() {
	if (enemy == null || !enemy.isAlive() || state == WANDERING) {

		HashSet<Mob> enemies = new HashSet<Mob>();
		for (Mob mob : Dungeon.level.mobs) {
			if (mob.hostile && Level.fieldOfView[mob.pos] && mob.state != mob.PASSIVE) {
				enemies.add(mob);
			}
		}
		enemy = enemies.size() > 0 ? Random.element( enemies ) : null;
	}
	return enemy;
}
 
Example 13
Source File: Bee.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Char chooseEnemy() {
	//if the pot is no longer present, default to regular AI behaviour
	if (alignment == Alignment.ALLY || (potHolder == -1 && potPos == -1)){
		return super.chooseEnemy();
	
	//if something is holding the pot, target that
	}else if (Actor.findById(potHolder) != null){
		return (Char) Actor.findById(potHolder);
		
	//if the pot is on the ground
	}else {
		
		//try to find a new enemy in these circumstances
		if (enemy == null || !enemy.isAlive() || !Actor.chars().contains(enemy) || state == WANDERING
				|| Dungeon.level.distance(enemy.pos, potPos) > 3
				|| (alignment == Alignment.ALLY && enemy.alignment == Alignment.ALLY)){
			
			//find all mobs near the pot
			HashSet<Char> enemies = new HashSet<>();
			for (Mob mob : Dungeon.level.mobs) {
				if (!(mob == this)
						&& Dungeon.level.distance(mob.pos, potPos) <= 3
						&& mob.alignment != Alignment.NEUTRAL
						&& !mob.isInvulnerable(getClass())
						&& !(alignment == Alignment.ALLY && mob.alignment == Alignment.ALLY)) {
					enemies.add(mob);
				}
			}
			
			if (!enemies.isEmpty()){
				return Random.element(enemies);
			} else {
				if (alignment != Alignment.ALLY && Dungeon.level.distance(Dungeon.hero.pos, potPos) <= 3){
					return Dungeon.hero;
				} else {
					return null;
				}
			}
			
		} else {
			return enemy;
		}

		
	}
}
 
Example 14
Source File: CeremonialCandle.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
private static void checkCandles(){
	Heap heapTop = Dungeon.level.heaps.get(ritualPos - Dungeon.level.width());
	Heap heapRight = Dungeon.level.heaps.get(ritualPos + 1);
	Heap heapBottom = Dungeon.level.heaps.get(ritualPos + Dungeon.level.width());
	Heap heapLeft = Dungeon.level.heaps.get(ritualPos - 1);

	if (heapTop != null &&
			heapRight != null &&
			heapBottom != null &&
			heapLeft != null){

		if (heapTop.peek() instanceof CeremonialCandle &&
				heapRight.peek() instanceof CeremonialCandle &&
				heapBottom.peek() instanceof CeremonialCandle &&
				heapLeft.peek() instanceof CeremonialCandle){

			heapTop.pickUp();
			heapRight.pickUp();
			heapBottom.pickUp();
			heapLeft.pickUp();

			NewbornElemental elemental = new NewbornElemental();
			Char ch = Actor.findChar( ritualPos );
			if (ch != null) {
				ArrayList<Integer> candidates = new ArrayList<>();
				for (int n : PathFinder.NEIGHBOURS8) {
					int cell = ritualPos + n;
					if ((Dungeon.level.passable[cell] || Dungeon.level.avoid[cell]) && Actor.findChar( cell ) == null) {
						candidates.add( cell );
					}
				}
				if (candidates.size() > 0) {
					elemental.pos = Random.element( candidates );
				} else {
					elemental.pos = ritualPos;
				}
			} else {
				elemental.pos = ritualPos;
			}
			elemental.state = elemental.HUNTING;
			GameScene.add(elemental, 1);

			for (int i : PathFinder.NEIGHBOURS9){
				CellEmitter.get(ritualPos+i).burst(ElmoParticle.FACTORY, 10);
			}
			Sample.INSTANCE.play(Assets.SND_BURNING);
		}
	}

}
 
Example 15
Source File: Honeypot.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
public Item shatter( Char owner, int pos ) {
	
	if (Dungeon.level.heroFOV[pos]) {
		Sample.INSTANCE.play( Assets.SND_SHATTER );
		Splash.at( pos, 0xffd500, 5 );
	}
	
	int newPos = pos;
	if (Actor.findChar( pos ) != null) {
		ArrayList<Integer> candidates = new ArrayList<>();
		boolean[] passable = Dungeon.level.passable;
		
		for (int n : PathFinder.NEIGHBOURS4) {
			int c = pos + n;
			if (passable[c] && Actor.findChar( c ) == null) {
				candidates.add( c );
			}
		}

		newPos = candidates.size() > 0 ? Random.element( candidates ) : -1;
	}
	
	if (newPos != -1) {
		Bee bee = new Bee();
		bee.spawn( Dungeon.depth );
		bee.setPotInfo( pos, owner );
		bee.HP = bee.HT;
		bee.pos = newPos;
		
		GameScene.add( bee );
		Actor.addDelayed( new Pushing( bee, pos, newPos ), -1f );
		
		bee.sprite.alpha( 0 );
		bee.sprite.parent.add( new AlphaTweener( bee.sprite, 1, 0.15f ) );
		
		Sample.INSTANCE.play( Assets.SND_BEE );
		return new ShatteredPot();
	} else {
		return this;
	}
}
 
Example 16
Source File: LastShopLevel.java    From remixed-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected boolean build() {
	
	initRooms();
	
	int distance;
	int retry = 0;
	int minDistance = (int)Math.sqrt( rooms.size() );
	do {
		int innerRetry = 0;
		do {
			if (innerRetry++ > 10) {
				return false;
			}
			roomEntrance = Random.element( rooms );
		} while (roomEntrance.width() < 4 || roomEntrance.height() < 4);
		
		innerRetry = 0;
		do {
			if (innerRetry++ > 10) {
				return false;
			}
			setRoomExit(Random.element( rooms ));
		} while (getRoomExit() == roomEntrance || getRoomExit().width() < 6 || getRoomExit().height() < 6 || getRoomExit().top == 0);

		Graph.buildDistanceMap( rooms, getRoomExit());
		distance = Graph.buildPath(roomEntrance, getRoomExit()).size();
		
		if (retry++ > 10) {
			return false;
		}
		
	} while (distance < minDistance);
	
	roomEntrance.type = Type.ENTRANCE;
	getRoomExit().type = Type.EXIT;
	
	Graph.buildDistanceMap( rooms, getRoomExit());
	List<Room> path = Graph.buildPath(roomEntrance, getRoomExit());
	
	Graph.setPrice( path, roomEntrance.distance );
	
	Graph.buildDistanceMap( rooms, getRoomExit());
	path = Graph.buildPath(roomEntrance, getRoomExit());
	
	Room room = roomEntrance;
	for (Room next : path) {
		room.connect( next );
		room = next;
	}
	
	Room roomShop = null;
	int shopSquare = 0;
	for (Room r : rooms) {
		if (r.type == Type.NULL && r.connected.size() > 0) {
			r.type = Type.PASSAGE; 
			if (r.square() > shopSquare) {
				roomShop = r;
				shopSquare = r.square();
			}
		}
	}
	
	if (roomShop == null || shopSquare < 30) {
		return false;
	} else {
		roomShop.type = Imp.Quest.isCompleted() ? Room.Type.SHOP : Room.Type.STANDARD;
	}
	
	paint();
	
	paintWater();
	paintGrass();
	
	return true;
}
 
Example 17
Source File: Belongings.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
public Item randomUnequipped() {
	return Random.element( backpack.items );
}
 
Example 18
Source File: DwarfKing.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean act() {
	delay--;

	if (delay <= 0){

		if (summon == DKWarlock.class){
			particles.burst(ShadowParticle.CURSE, 10);
			Sample.INSTANCE.play(Assets.Sounds.CURSED);
		} else if (summon == DKMonk.class){
			particles.burst(ElmoParticle.FACTORY, 10);
			Sample.INSTANCE.play(Assets.Sounds.BURNING);
		} else {
			particles.burst(Speck.factory(Speck.BONE), 10);
			Sample.INSTANCE.play(Assets.Sounds.BONES);
		}
		particles = null;

		if (Actor.findChar(pos) != null){
			ArrayList<Integer> candidates = new ArrayList<>();
			for (int i : PathFinder.NEIGHBOURS8){
				if (Dungeon.level.passable[pos+i] && Actor.findChar(pos+i) == null){
					candidates.add(pos+i);
				}
			}
			if (!candidates.isEmpty()){
				pos = Random.element(candidates);
			}
		}

		if (Actor.findChar(pos) == null) {
			Mob m = Reflection.newInstance(summon);
			m.pos = pos;
			m.maxLvl = -2;
			GameScene.add(m);
			m.state = m.HUNTING;
			if (((DwarfKing)target).phase == 2){
				Buff.affect(m, KingDamager.class);
			}
		} else {
			Char ch = Actor.findChar(pos);
			ch.damage(Random.NormalIntRange(20, 40), summon);
			if (((DwarfKing)target).phase == 2){
				target.damage(target.HT/12, new KingDamager());
			}
		}

		detach();
	}

	spend(TICK);
	return true;
}
 
Example 19
Source File: DriedRose.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void execute( Hero hero, String action ) {

	super.execute(hero, action);

	if (action.equals(AC_SUMMON)) {

		if (!Ghost.Quest.completed())   GameScene.show(new WndUseItem(null, this));
		else if (ghost != null)         GLog.i( Messages.get(this, "spawned") );
		else if (!isEquipped( hero ))   GLog.i( Messages.get(Artifact.class, "need_to_equip") );
		else if (charge != chargeCap)   GLog.i( Messages.get(this, "no_charge") );
		else if (cursed)                GLog.i( Messages.get(this, "cursed") );
		else {
			ArrayList<Integer> spawnPoints = new ArrayList<>();
			for (int i = 0; i < PathFinder.NEIGHBOURS8.length; i++) {
				int p = hero.pos + PathFinder.NEIGHBOURS8[i];
				if (Actor.findChar(p) == null && (Dungeon.level.passable[p] || Dungeon.level.avoid[p])) {
					spawnPoints.add(p);
				}
			}

			if (spawnPoints.size() > 0) {
				ghost = new GhostHero( this );
				ghostID = ghost.id();
				ghost.pos = Random.element(spawnPoints);

				GameScene.add(ghost, 1f);
				Dungeon.level.occupyCell(ghost);
				
				CellEmitter.get(ghost.pos).start( ShaftParticle.FACTORY, 0.3f, 4 );
				CellEmitter.get(ghost.pos).start( Speck.factory(Speck.LIGHT), 0.2f, 3 );

				hero.spend(1f);
				hero.busy();
				hero.sprite.operate(hero.pos);

				if (!firstSummon) {
					ghost.yell( Messages.get(GhostHero.class, "hello", Dungeon.hero.name()) );
					Sample.INSTANCE.play( Assets.Sounds.GHOST );
					firstSummon = true;
					
				} else {
					if (BossHealthBar.isAssigned()) {
						ghost.sayBoss();
					} else {
						ghost.sayAppeared();
					}
				}
				
				charge = 0;
				partialCharge = 0;
				updateQuickslot();

			} else
				GLog.i( Messages.get(this, "no_space") );
		}

	} else if (action.equals(AC_DIRECT)){
		if (ghost == null && ghostID != 0){
			Actor a = Actor.findById(ghostID);
			if (a != null){
				ghost = (GhostHero)a;
			} else {
				ghostID = 0;
			}
		}
		if (ghost != null) GameScene.selectCell(ghostDirector);
		
	} else if (action.equals(AC_OUTFIT)){
		GameScene.show( new WndGhostHero(this) );
	}
}
 
Example 20
Source File: Mimic.java    From pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
public static Mimic spawnAt( int pos, List<Item> items ) {
	Char ch = Actor.findChar( pos ); 
	if (ch != null) {
		ArrayList<Integer> candidates = new ArrayList<Integer>();
		for (int n : Level.NEIGHBOURS8) {
			int cell = pos + n;
			if ((Level.passable[cell] || Level.avoid[cell]) && Actor.findChar( cell ) == null) {
				candidates.add( cell );
			}
		}
		if (candidates.size() > 0) {
			int newPos = Random.element( candidates );
			Actor.addDelayed( new Pushing( ch, ch.pos, newPos ), -1 );
			
			ch.pos = newPos;
			// FIXME
			if (ch instanceof Mob) {
				Dungeon.level.mobPress( (Mob)ch );
			} else {
				Dungeon.level.press( newPos, ch );
			}
		} else {
			return null;
		}
	}
	
	Mimic m = new Mimic();
	m.items = new ArrayList<Item>( items );
	m.adjustStats( Dungeon.depth );
	m.HP = m.HT;
	m.pos = pos;
	m.state = m.HUNTING;
	GameScene.add( m, 1 );
	
	m.sprite.turnTo( pos, Dungeon.hero.pos );
	
	if (Dungeon.visible[m.pos]) {
		CellEmitter.get( pos ).burst( Speck.factory( Speck.STAR ), 10 );
		Sample.INSTANCE.play( Assets.SND_MIMIC );
	}
	
	return m;
}