From 8ff9bdd938b180ea6e47bf321cb6e72eace615b5 Mon Sep 17 00:00:00 2001 From: James Daly Date: Tue, 28 May 2019 22:17:08 -0400 Subject: [PATCH] Added swapping in for fallen monsters and looping turns --- FakeMon/src/mon/Game.scala | 14 ++++++----- FakeMon/src/mon/battle/BattleEngine.scala | 29 ++++++++++++++++++++--- FakeMon/src/mon/stat/Move.scala | 12 ++++++---- FakeMon/src/mon/stat/Party.scala | 18 ++++++++++++-- FakeMon/src/mon/stat/TrainerID.scala | 1 + 5 files changed, 59 insertions(+), 15 deletions(-) diff --git a/FakeMon/src/mon/Game.scala b/FakeMon/src/mon/Game.scala index 8c25829..095f90d 100644 --- a/FakeMon/src/mon/Game.scala +++ b/FakeMon/src/mon/Game.scala @@ -15,18 +15,20 @@ case class Prop(url : Seq[String]) object Game { def main(args : Array[String]): Unit = { println(Element("Water").effect) - println(Move.moves) - println(Form.forms) println(Status("psn")) implicit val rng = new scala.util.Random() val form1 = Form("Diabolo") val form2 = Form("Chanilla") - val movepool1 = IndexedSeq(Move("Frost Breath")) + val movepool1 = IndexedSeq(Move("Close Combat")) val movepool2 = IndexedSeq(Move("Absorb")) - val party1 = new Party(null, new Monster(new StorageMon("Allied Mon", Gene.randomGene(null, form1), form1, Statistic.emptyEvs, movepool1)), IndexedSeq()) - val party2 = new Party(null, new Monster(new StorageMon("Wild Mon", Gene.randomGene(null, form2), form2, Statistic.emptyEvs, movepool2)), IndexedSeq()) + val p1 = TrainerID("Jaeda", Gender.Female, 0) + val p2 = TrainerID("Wild Monster", Gender.Male, 0) + val party1 = new Party(p1, new Monster(new StorageMon("Allied Mon", Gene.randomGene(null, form1), form1, Statistic.emptyEvs, movepool1)), IndexedSeq()) + val party2 = new Party(p2, new Monster(new StorageMon("Wild Mon", Gene.randomGene(null, form2), form2, Statistic.emptyEvs, movepool2)), IndexedSeq( + new Monster(new StorageMon("Sideboard Mon", Gene.randomGene(null, form2), form2, Statistic.emptyEvs, movepool2)) + )) val engine = new BattleEngine(party1, party2) - engine.playTurn() + engine.play() println(party1.lead.elements) } } \ No newline at end of file diff --git a/FakeMon/src/mon/battle/BattleEngine.scala b/FakeMon/src/mon/battle/BattleEngine.scala index 2a1ed9b..c5f520b 100644 --- a/FakeMon/src/mon/battle/BattleEngine.scala +++ b/FakeMon/src/mon/battle/BattleEngine.scala @@ -8,16 +8,40 @@ import mon.util.Fraction class BattleEngine(val player: Party, val enemy: Party)(implicit val rng: Random) { + def play() = { + while (player.canFight && enemy.canFight) { + playTurn() + } + } + def playTurn() = { val playerMove = player.pollAction - val playerTarget = player.pollTarget(playerMove, player.lead, player, enemy) + val playerTarget = player.pollTarget(playerMove, player.lead, enemy) val enemyMove = enemy.pollAction - val enemyTarget = enemy.pollTarget(enemyMove, enemy.lead, enemy, player) + val enemyTarget = enemy.pollTarget(enemyMove, enemy.lead, player) val actions = Seq(Action(player.lead, playerMove, playerTarget), Action(enemy.lead, enemyMove, enemyTarget)) val queue = rng.shuffle(actions).sorted // Shuffle to randomize in the event of a tie queue.foreach(useMove) val eotQueue = Seq(player.lead, enemy.lead).sortBy(_(Speed)) eotQueue.foreach(mon => mon.status.map(_.onResidual.map(_(mon)))) + + if (!player.lead.isAlive) { + if (player.canFight) { + val replace = player.pollReplacement(enemy) + player.swapIn(replace) + } else { + println(s"${player.trainer} has lost!") + } + } + if (!enemy.lead.isAlive) { + if (enemy.canFight) { + val replace = enemy.pollReplacement(player) + enemy.swapIn(replace) + } else { + println(s"${enemy.trainer} has lost!") + } + } + println(s"${player.lead}(${player.lead.hp}/${player.lead(Hp)})") println(s"${enemy.lead}(${enemy.lead.hp}/${enemy.lead(Hp)})") } @@ -55,7 +79,6 @@ class BattleEngine(val player: Party, val enemy: Party)(implicit val rng: Random } applyBoosts(target, move.boosts) applyStatus(target, move.status) - // TODO : Secondary effects if (move.selfEffect != null) { applyEffect(user, move.selfEffect) } diff --git a/FakeMon/src/mon/stat/Move.scala b/FakeMon/src/mon/stat/Move.scala index 9c28bd6..876f839 100644 --- a/FakeMon/src/mon/stat/Move.scala +++ b/FakeMon/src/mon/stat/Move.scala @@ -93,11 +93,15 @@ case class MoveToken( } object Move { - val tokens = YamlHelper.extractSeq[MoveToken](Source.fromInputStream(Move.getClass.getResourceAsStream("data/moves.yaml"))) - val moves = tokens.map(_.instantiate()) - val byName = moves.map(m => (m.name, m)).toMap + private var moves = Map[String, Move]() + val tokens = YamlHelper.extractSeq[MoveToken](Source.fromInputStream(Move.getClass.getResourceAsStream("data/moves.yaml"))).map(t => (t.name, t)).toMap - def apply(s : String) = byName(s) + def apply(name : String) : Move = { + if (!moves.contains(name)) { + moves = moves.updated(name, tokens(name).instantiate()) + } + moves(name) + } def compilePowCallback(code: String): (Monster, Monster) => Int = { if (code != null) { diff --git a/FakeMon/src/mon/stat/Party.scala b/FakeMon/src/mon/stat/Party.scala index 694bd66..8f57e1f 100644 --- a/FakeMon/src/mon/stat/Party.scala +++ b/FakeMon/src/mon/stat/Party.scala @@ -5,13 +5,27 @@ import scala.util.Random import mon.battle.rngDice import mon.stat.Target._ -class Party(val trainer : TrainerID, var lead : Monster, val sideboard : IndexedSeq[Monster]) { +class Party(val trainer : TrainerID, var lead : Monster, var sideboard : IndexedSeq[Monster]) { def pollAction(implicit rng : Random) : Move = rng.pick(lead.base.moves) - def pollTarget(move : Move, user : Monster, us : Party, them: Party)(implicit rng : Random) : Monster = { + def pollTarget(move : Move, user : Monster, them: Party)(implicit rng : Random) : Monster = { if (move.target == Self) { user } else { them.lead } } + + def pollReplacement(them : Party)(implicit rng : Random) : Monster = { + sideboard.filter(_.isAlive).head + } + + def swapIn(mon : Monster) = { + val index = sideboard.indexOf(mon) + sideboard = sideboard.updated(index, lead) + lead = mon + } + + def canFight : Boolean = { + lead.isAlive || sideboard.exists(_.isAlive) + } } \ No newline at end of file diff --git a/FakeMon/src/mon/stat/TrainerID.scala b/FakeMon/src/mon/stat/TrainerID.scala index 28f9dc1..e0349a0 100644 --- a/FakeMon/src/mon/stat/TrainerID.scala +++ b/FakeMon/src/mon/stat/TrainerID.scala @@ -2,4 +2,5 @@ package mon.stat case class TrainerID(name : String, gender : Gender, id : Long) { + override def toString : String = name } \ No newline at end of file