Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package o1.adventure
- import scala.util.Random
- import scala.actors.Futures._
- abstract class Battle(user: Trainer) {
- this.user.originalParty = this.user.party.clone
- var escapeTries = 0
- var userSelected = PartySlot.choseFirst(this.user.party)
- if (!userSelected.isDefined)
- println("Something wrong, imagine this is missingno")
- if (Sounds.soundEnabled) {
- this.startSound.play
- future{
- this.duringSound.play
- }
- }
- def startSound: StereoSound
- def endSound: StereoSound
- def duringSound: StereoSound
- def userPokemon = userSelected.get
- def userHP = this.userPokemon.hp
- /**
- * Enemy pokemon
- *
- * @return PartySlot enemy's current pokemon
- */
- var enemyPokemon: PartySlot
- /**
- * Enemy pokemons hp
- *
- * @return int current hp
- */
- def enemyHP: Int
- /**
- * Name of the enemy
- *
- * @return return name of the enemy
- */
- def enemyName: String
- /**
- * Enemy pokemon that is next on the line
- *
- * @return nemy pokemons partyslot wraped in option, None if no pokemon found
- */
- def nextPokemon: Option[PartySlot]
- /**
- * Has player lost this battle
- *
- * @return boolean if true
- */
- def isLost = {
- if (user.party.exists(_.hp > 0))
- false
- else
- true
- }
- /**
- * Has player lost this round (pokemon is knocked out, but player has not lost yet
- *
- * @return boolean if true
- */
- def isRoundLost: Boolean = {
- if (!this.isLost && userPokemon.hp <= 0)
- true
- else
- false
- }
- /**
- * Has player won this battle
- *
- * @return boolean if true
- */
- def isWon: Boolean
- /**
- * Has player won this round (enemy pokemon is knocked out, but enemy has not lost yet
- *
- * @return boolean if true
- */
- def isRoundWon: Boolean
- /**
- * Ends the battle
- *
- * @return outcome of the battle
- */
- def endBattle: String
- /**
- * Check if player goes first
- *
- * @return boolean true or false depending if player goes first
- */
- def userFirst = {
- if (this.userPokemon.getStat("speed") >= this.enemyPokemon.getStat("speed"))
- true
- else
- false
- }
- /**
- * Return who the pokemon belongs to
- *
- * @return string name of the owner
- */
- def whos: String
- /**
- * Does the turn for enemy, enemy will chose random move from their pool (if they have PP for it) to make it simple.
- *
- * @return a description of what is going to happen
- */
- def enemyTurn: String = {
- if (this.isWon || this.isLost || this.isRoundWon || this.isRoundLost)// dont let enemy attack if there is no point of doing it
- return ""
- val shuffleAttacks = Random.shuffle(this.enemyPokemon.moves.toList)
- var move: Option[Move] = None
- if (shuffleAttacks.length > 0) {
- for (a <- shuffleAttacks) {
- if (!move.isDefined) {
- if (a._2 > 0) {
- move = Some (a._1)
- }
- }
- }
- }
- if (move.isDefined) {//time to ATTACK!!!!
- this.enemyPokemon.reducePP(move.get, 1)
- move.get.attack(this.enemyPokemon, this.userPokemon, this.whos, "your")
- }
- else
- "Struggle here"
- }
- /**
- * Try to escape the ongoing battle
- *
- * @return string outcome of the try.
- */
- def escape(): String
- def attack(attName: String): String =
- {
- if (this.userPokemon.moves.forall(_._2 <= 0))
- {
- return "stroggle"
- }
- var output = ""
- val index = this.userPokemon.moves.indexWhere(_._1.name == attName)
- if (index > -1)
- {
- if (this.userPokemon.moves(index)._2 <= 0)
- return "This move is out of PP, select another one"
- if (this.userFirst)
- {
- this.userPokemon.reducePP(this.userPokemon.moves(index)._1, 1)
- output = this.userPokemon.moves(index)._1.attack(this.userPokemon, this.enemyPokemon, "your", this.whos)
- if(!this.enemyPokemon.isKO)//make sure we dont do enemy pokemons turn if he is knocked out
- output += "\n" + this.enemyTurn
- else {
- output += "\n" + this.whos + " " + this.enemyPokemon.name + " fainted"
- output += this.userPokemon.levelUp ()
- if (this.nextPokemon.isDefined)
- output += "\n" + this.enemyName + " is about to switch in " + this.nextPokemon.get.name
- }
- if (this.userPokemon.isKO)
- output += "\nYour " + this.userPokemon.name + " fainted"
- }
- else
- {
- output = this.enemyTurn
- if(!this.userPokemon.isKO) {//make sure we dont do users pokemons turn if its is knocked out
- this.userPokemon.reducePP(this.userPokemon.moves(index)._1, 1)
- output += "\n" + this.userPokemon.moves(index)._1.attack(this.userPokemon, this.enemyPokemon, "your", this.whos)
- } else
- output += "\nYour " + this.userPokemon.name + " fainted"
- if (this.enemyPokemon.isKO) {
- output += "\n" + this.whos + " " + this.enemyPokemon.name + " fainted"
- output += this.userPokemon.levelUp ()
- if (this.nextPokemon.isDefined)
- output += "\n" + this.enemyName + " is about to switch in " + this.nextPokemon.get.name
- }
- }
- } else
- return "Your " + this.userPokemon.name + " does not know this move."
- output
- }
- /**
- * Use pokemon ball, diferent outcome depending if wild battle or not
- */
- def useBall (ball: Item): Option[(String, Boolean)]
- /**
- * plays players turn, there are few things players can do like trying to escape and attacking
- */
- def playTurn(action: String, move: String): String = {
- if (action == "run") {
- this.escape
- } else if (action == "attack") {
- this.attack (move)
- } else "Unknown command, THIS SHOULD NOT HAPPEN"
- }
- /**
- * Function checks if its good to print "type ok to continue screen
- */
- def printOK() = {
- if (this.isLost || this.isWon || this.isRoundWon)
- true
- else
- false
- }
- /**
- * Returns a multi-line description of the battle as a player sees it.
- */
- def fullDescription: String = {
- val userPoke = this.userPokemon.name + " Lv " + this.userPokemon.level
- val enemyPoke = this.enemyPokemon.name + " Lv " + this.enemyPokemon.level
- val spaceLen = 80 - userPoke.length - enemyPoke.length + 1
- val userHPPercent = this.userHP.toDouble / this.userPokemon.maxHP
- val enemyHPPercent = this.enemyHP.toDouble / this.enemyPokemon.maxHP
- val userHPSlider = "<" + "*" * (20 * userHPPercent).toInt + " " * (20 - 20 * userHPPercent).toInt + ">"
- val enemyHPSlider = "<" + " " * (20 - 20 * enemyHPPercent).toInt + "*" * (20 * enemyHPPercent).toInt + ">"
- val sliderSpaceLen = 80 - userHPSlider.length - enemyHPSlider.length + 1
- val userHPString = "HP (" + this.userHP + "/" + this.userPokemon.maxHP + ")"
- val enemyHPString = "HP (" + this.enemyHP + "/" + this.enemyPokemon.maxHP + ")"
- val hpSpaceLen = 80 - userHPString.length - enemyHPString.length + 1
- val pokeLine = userPoke + " " * spaceLen + enemyPoke + "\n"
- val sliderLine = userHPSlider + " " * sliderSpaceLen + enemyHPSlider + "\n"
- val hpLine = userHPString + " " * hpSpaceLen + enemyHPString + "\n"
- var output = ""
- if (this.isLost) {//player loses first, remember
- output += "\n" + this.user.name + " is out of useable pokemon"
- }else if (this.isWon) {
- output += "\n" +this.enemyName + " was defeated"
- if (Sounds.soundEnabled) {
- this.duringSound.forceStop = true
- future{this.endSound.play}
- }
- }
- if (this.printOK())
- output += "\n" + "Type 'ok' to contine"
- if (this.isRoundLost)
- output += "\n" + "Use command 'switch' to contine\n" + this.user.makeParty
- pokeLine + sliderLine + hpLine + output
- }
- def endRound = {
- }
- override def toString = {
- val spaceLen = (80 - this.user.name.length - this.enemyName.length) / 2
- this.user.name + " " * spaceLen + "vs" + " " * spaceLen + this.enemyName
- }
- }
- class TrainerBattle(user: Trainer, enemy: Trainer) extends Battle(user) {
- this.enemy.originalParty = this.enemy.party.clone
- def startSound = Sounds.WildStart.get
- def endSound = Sounds.WildVictory.get
- def duringSound = Sounds.WildBattle.get
- var enemyPokemon = this.enemy.party(0)
- def enemyHP = this.enemyPokemon.hp
- def enemyName = this.enemy.name
- def escape = "Cant run from trainer battle!"
- def useBall (ball: Item): Option[(String, Boolean)] = {
- Some(("You can only cature wild pokemon!", false))
- }
- def nextPokemon = PartySlot.choseFirst(this.enemy.party)
- def endBattle: String = {
- this.user.currentMatch = None
- var output = ""
- this.user.party = this.user.originalParty
- this.enemy.party = this.enemy.originalParty
- if (this.isWon) {
- this.user.addMoney(this.enemy.price)
- this.enemy.isDefeated = true
- output += "You got " + this.enemy.price + "$"
- }
- this.enemy.healAll
- output + this.user.checkEvolution(0)
- }
- def whos = "Foe's"
- def isWon = {
- if (enemy.party.exists(_.hp > 0))
- false
- else
- true
- }
- def isRoundWon = {
- if (!this.isWon && this.enemyPokemon.hp <= 0)
- true
- else
- false
- }
- override def endRound = {
- if (this.enemyPokemon.isKO && this.nextPokemon.isDefined) {
- this.enemyPokemon = this.nextPokemon.get
- this.enemyName + " sent out " + this.enemyPokemon.name
- }
- }
- }
- class WildBattle(user: Trainer, enemy: PartySlot) extends Battle(user) {
- def startSound = Sounds.WildStart.get
- def endSound = Sounds.WildVictory.get
- def duringSound = Sounds.WildBattle.get
- var enemyPokemon = enemy
- def enemyHP = enemy.hp
- def enemyName = "Wild " + this.enemy.name
- def whos = "Wild"
- def escape = {
- val A = this.userPokemon.getStat("speed")
- val B = (this.enemyPokemon.getStat("speed") / 4) % 256
- var escapeChange = 0
- if (B == 0)
- escapeChange = 256
- else
- escapeChange = (A * 32) / B + 30 * this.escapeTries
- val r = new Random
- val range = 0 to 255
- val random = range(r.nextInt(range.length))
- if (random < escapeChange) {
- this.user.currentMatch = None
- "Got away safely"
- } else {
- "Cant Escape!" + this.enemyTurn
- }
- }
- def useBall (ball: Item): Option[(String, Boolean)] = {
- Some(ball.use(Some(this.enemyPokemon), this.user))
- }
- def isWon = {
- if (enemy.hp <= 0 || enemy.isCaptured)
- true
- else
- false
- }
- def isRoundWon = false //wild battles always result in victory when enemy pokemon is knocked out
- def nextPokemon = None
- def endBattle: String = {
- this.user.party = this.user.originalParty
- this.user.currentMatch = None
- ""
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement