Advertisement
Guest User

Untitled

a guest
Dec 10th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 11.01 KB | None | 0 0
  1. package o1.adventure
  2.  
  3. import scala.util.Random
  4. import scala.actors.Futures._
  5.  
  6. abstract class Battle(user: Trainer) {
  7.   this.user.originalParty = this.user.party.clone
  8.   var escapeTries = 0
  9.  
  10.   var userSelected = PartySlot.choseFirst(this.user.party)
  11.   if (!userSelected.isDefined)
  12.     println("Something wrong, imagine this is missingno")
  13.  
  14.    
  15.   if (Sounds.soundEnabled) {
  16.     this.startSound.play
  17.     future{
  18.       this.duringSound.play
  19.     }
  20.   }
  21.  
  22.   def startSound: StereoSound
  23.  
  24.   def endSound: StereoSound
  25.  
  26.   def duringSound: StereoSound
  27.  
  28.   def userPokemon = userSelected.get
  29.  
  30.   def userHP = this.userPokemon.hp
  31.  
  32.  
  33.   /**
  34.    * Enemy pokemon
  35.    *
  36.    * @return PartySlot  enemy's current pokemon
  37.    */
  38.   var enemyPokemon: PartySlot
  39.  
  40.   /**
  41.    * Enemy pokemons hp
  42.    *
  43.    * @return int current hp
  44.    */
  45.   def enemyHP: Int
  46.  
  47.   /**
  48.    * Name of the enemy
  49.    *
  50.    * @return return name of the enemy
  51.    */
  52.   def enemyName: String
  53.  
  54.   /**
  55.    * Enemy pokemon that is next on the line
  56.    *
  57.    * @return nemy pokemons partyslot wraped in option, None if no pokemon found
  58.    */
  59.   def nextPokemon: Option[PartySlot]
  60.  
  61.   /**
  62.    * Has player lost this battle
  63.    *
  64.    * @return boolean if true
  65.    */
  66.   def isLost = {
  67.     if (user.party.exists(_.hp > 0))
  68.       false
  69.     else
  70.       true
  71.   }
  72.  
  73.   /**
  74.    * Has player lost this round (pokemon is knocked out, but player has not lost yet
  75.    *
  76.    * @return boolean if true
  77.    */
  78.   def isRoundLost: Boolean = {
  79.     if (!this.isLost && userPokemon.hp <= 0)
  80.       true
  81.     else
  82.       false
  83.   }
  84.  
  85.   /**
  86.    * Has player won this battle
  87.    *
  88.    * @return boolean if true
  89.    */
  90.   def isWon: Boolean
  91.  
  92.   /**
  93.    * Has player won this round (enemy pokemon is knocked out, but enemy has not lost yet
  94.    *
  95.    * @return boolean if true
  96.    */
  97.   def isRoundWon: Boolean
  98.  
  99.   /**
  100.    * Ends the battle
  101.    *
  102.    * @return outcome of the battle
  103.    */
  104.   def endBattle: String
  105.  
  106.  
  107.   /**
  108.    * Check if player goes first
  109.    *
  110.    * @return boolean  true or false depending if player goes first
  111.    */
  112.   def userFirst = {
  113.     if (this.userPokemon.getStat("speed") >= this.enemyPokemon.getStat("speed"))
  114.       true
  115.     else
  116.       false
  117.   }
  118.  
  119.   /**
  120.    * Return who the pokemon belongs to
  121.    *
  122.    * @return string  name of the owner
  123.    */
  124.   def whos: String
  125.  
  126.   /**
  127.    * Does the turn for enemy, enemy will chose random move from their pool (if they have PP for it) to make it simple.
  128.    *
  129.    * @return a description of what is going to happen
  130.    */
  131.   def enemyTurn: String = {
  132.     if (this.isWon || this.isLost || this.isRoundWon || this.isRoundLost)// dont let enemy attack if there is no point of doing it
  133.       return ""
  134.  
  135.     val shuffleAttacks = Random.shuffle(this.enemyPokemon.moves.toList)
  136.     var move: Option[Move] = None
  137.     if (shuffleAttacks.length > 0) {
  138.       for (a <- shuffleAttacks) {
  139.         if (!move.isDefined) {
  140.           if (a._2 > 0) {
  141.             move = Some (a._1)
  142.           }
  143.         }
  144.       }
  145.     }
  146.    
  147.     if (move.isDefined) {//time to ATTACK!!!!
  148.       this.enemyPokemon.reducePP(move.get, 1)
  149.       move.get.attack(this.enemyPokemon, this.userPokemon, this.whos, "your")
  150.     }
  151.     else
  152.       "Struggle here"
  153.   }
  154.  
  155.   /**
  156.    * Try to escape the ongoing battle
  157.    *
  158.    * @return string outcome of the try.
  159.    */
  160.   def escape(): String
  161.  
  162.   def attack(attName: String): String =
  163.   {
  164.     if (this.userPokemon.moves.forall(_._2 <= 0))
  165.     {
  166.       return "stroggle"
  167.     }
  168.    
  169.     var output = ""
  170.  
  171.     val index = this.userPokemon.moves.indexWhere(_._1.name == attName)
  172.     if (index > -1)
  173.     {
  174.       if (this.userPokemon.moves(index)._2 <= 0)
  175.         return "This move is out of PP, select another one"
  176.  
  177.       if (this.userFirst)
  178.       {
  179.         this.userPokemon.reducePP(this.userPokemon.moves(index)._1, 1)
  180.         output = this.userPokemon.moves(index)._1.attack(this.userPokemon, this.enemyPokemon, "your", this.whos)
  181.         if(!this.enemyPokemon.isKO)//make sure we dont do enemy pokemons turn if he is knocked out
  182.           output += "\n" + this.enemyTurn
  183.         else {
  184.           output += "\n" + this.whos + " " + this.enemyPokemon.name + " fainted"
  185.           output += this.userPokemon.levelUp ()
  186.           if (this.nextPokemon.isDefined)
  187.             output += "\n" + this.enemyName + " is about to switch in " + this.nextPokemon.get.name
  188.         }
  189.        
  190.         if (this.userPokemon.isKO)
  191.           output += "\nYour " + this.userPokemon.name + " fainted"
  192.       }
  193.       else
  194.       {
  195.         output = this.enemyTurn
  196.         if(!this.userPokemon.isKO) {//make sure we dont do users pokemons turn if its is knocked out
  197.           this.userPokemon.reducePP(this.userPokemon.moves(index)._1, 1)
  198.           output += "\n" + this.userPokemon.moves(index)._1.attack(this.userPokemon, this.enemyPokemon, "your", this.whos)
  199.         } else
  200.           output += "\nYour " + this.userPokemon.name + " fainted"
  201.        
  202.         if (this.enemyPokemon.isKO) {
  203.           output += "\n" + this.whos + " " + this.enemyPokemon.name + " fainted"
  204.           output += this.userPokemon.levelUp ()
  205.           if (this.nextPokemon.isDefined)
  206.             output += "\n" + this.enemyName + " is about to switch in " + this.nextPokemon.get.name
  207.         }
  208.       }
  209.      
  210.     } else
  211.         return "Your " + this.userPokemon.name + " does not know this move."
  212.  
  213.     output
  214.   }
  215.  
  216.  
  217.   /**
  218.    * Use pokemon ball, diferent outcome depending if wild battle or not
  219.    */
  220.   def useBall (ball: Item): Option[(String, Boolean)]
  221.  
  222.   /**
  223.    * plays players turn, there are few things players can do like trying to escape and attacking
  224.    */
  225.   def playTurn(action: String, move: String): String = {
  226.     if (action == "run") {
  227.       this.escape
  228.     } else if (action == "attack") {
  229.       this.attack (move)
  230.     } else "Unknown command, THIS SHOULD NOT HAPPEN"
  231.   }
  232.  
  233.   /**
  234.    * Function checks if its good to print "type ok to continue screen
  235.    */
  236.   def printOK() = {
  237.     if (this.isLost || this.isWon || this.isRoundWon)
  238.       true
  239.     else
  240.       false
  241.   }
  242.   /**
  243.    * Returns a multi-line description of the battle as a player sees it.
  244.    */
  245.   def fullDescription: String = {
  246.    
  247.     val userPoke = this.userPokemon.name + " Lv " + this.userPokemon.level
  248.     val enemyPoke = this.enemyPokemon.name + " Lv " + this.enemyPokemon.level
  249.     val spaceLen = 80 - userPoke.length - enemyPoke.length + 1
  250.  
  251.     val userHPPercent = this.userHP.toDouble / this.userPokemon.maxHP
  252.     val enemyHPPercent = this.enemyHP.toDouble / this.enemyPokemon.maxHP
  253.    
  254.     val userHPSlider = "<" + "*" * (20 * userHPPercent).toInt + " " * (20 - 20 * userHPPercent).toInt + ">"
  255.     val enemyHPSlider = "<" + " " * (20 - 20 * enemyHPPercent).toInt + "*" * (20 * enemyHPPercent).toInt + ">"
  256.     val sliderSpaceLen = 80 - userHPSlider.length - enemyHPSlider.length + 1
  257.  
  258.     val userHPString = "HP (" + this.userHP + "/" + this.userPokemon.maxHP + ")"
  259.     val enemyHPString = "HP (" + this.enemyHP + "/" + this.enemyPokemon.maxHP + ")"
  260.     val hpSpaceLen = 80 - userHPString.length - enemyHPString.length + 1
  261.  
  262.     val pokeLine = userPoke + " " * spaceLen + enemyPoke + "\n"
  263.     val sliderLine = userHPSlider + " " * sliderSpaceLen + enemyHPSlider + "\n"
  264.     val hpLine = userHPString + " " * hpSpaceLen + enemyHPString + "\n"
  265.  
  266.     var output = ""
  267.    
  268.     if (this.isLost) {//player loses first, remember
  269.       output += "\n" + this.user.name + " is out of useable pokemon"
  270.     }else if (this.isWon) {
  271.       output += "\n" +this.enemyName + " was defeated"
  272.       if (Sounds.soundEnabled) {
  273.         this.duringSound.forceStop = true
  274.         future{this.endSound.play}
  275.       }
  276.     }
  277.    
  278.     if (this.printOK())
  279.       output += "\n" + "Type 'ok' to contine"
  280.    
  281.     if (this.isRoundLost)
  282.       output += "\n" + "Use command 'switch' to contine\n" + this.user.makeParty
  283.  
  284.     pokeLine + sliderLine + hpLine + output
  285.   }
  286.  
  287.   def endRound = {
  288.    
  289.   }
  290.  
  291.   override def toString = {
  292.     val spaceLen = (80 - this.user.name.length - this.enemyName.length) / 2
  293.     this.user.name + " " * spaceLen + "vs" + " " * spaceLen + this.enemyName
  294.   }
  295. }
  296.  
  297. class TrainerBattle(user: Trainer, enemy: Trainer) extends Battle(user) {
  298.   this.enemy.originalParty = this.enemy.party.clone
  299.  
  300.   def startSound = Sounds.WildStart.get
  301.  
  302.   def endSound = Sounds.WildVictory.get
  303.  
  304.   def duringSound = Sounds.WildBattle.get
  305.  
  306.   var enemyPokemon = this.enemy.party(0)
  307.  
  308.   def enemyHP = this.enemyPokemon.hp
  309.  
  310.   def enemyName = this.enemy.name
  311.  
  312.   def escape = "Cant run from trainer battle!"
  313.  
  314.  
  315.   def useBall (ball: Item): Option[(String, Boolean)] = {
  316.     Some(("You can only cature wild pokemon!", false))
  317.   }
  318.  
  319.   def nextPokemon = PartySlot.choseFirst(this.enemy.party)
  320.  
  321.   def endBattle: String = {
  322.     this.user.currentMatch = None
  323.     var output = ""
  324.     this.user.party = this.user.originalParty
  325.     this.enemy.party = this.enemy.originalParty
  326.    
  327.     if (this.isWon) {
  328.       this.user.addMoney(this.enemy.price)
  329.       this.enemy.isDefeated = true
  330.       output += "You got " + this.enemy.price + "$"
  331.     }
  332.    
  333.     this.enemy.healAll
  334.     output + this.user.checkEvolution(0)
  335.   }
  336.  
  337.  
  338.   def whos = "Foe's"
  339.  
  340.   def isWon = {
  341.     if (enemy.party.exists(_.hp > 0))
  342.       false
  343.     else
  344.       true
  345.   }
  346.  
  347.   def isRoundWon = {
  348.     if (!this.isWon && this.enemyPokemon.hp <= 0)
  349.       true
  350.     else
  351.       false
  352.   }
  353.  
  354.      
  355.   override def endRound = {
  356.     if (this.enemyPokemon.isKO && this.nextPokemon.isDefined) {
  357.       this.enemyPokemon = this.nextPokemon.get
  358.       this.enemyName + " sent out " + this.enemyPokemon.name
  359.     }
  360.   }
  361.  
  362. }
  363.  
  364. class WildBattle(user: Trainer, enemy: PartySlot) extends Battle(user) {
  365.  
  366.   def startSound = Sounds.WildStart.get
  367.  
  368.   def endSound = Sounds.WildVictory.get
  369.  
  370.   def duringSound = Sounds.WildBattle.get
  371.  
  372.  
  373.   var enemyPokemon = enemy
  374.  
  375.   def enemyHP = enemy.hp
  376.  
  377.   def enemyName = "Wild " + this.enemy.name
  378.  
  379.  
  380.   def whos = "Wild"
  381.  
  382.   def escape = {
  383.       val A = this.userPokemon.getStat("speed")
  384.       val B = (this.enemyPokemon.getStat("speed") / 4) % 256
  385.       var escapeChange = 0
  386.  
  387.       if (B == 0)
  388.         escapeChange = 256
  389.       else
  390.         escapeChange = (A * 32) / B + 30 * this.escapeTries
  391.  
  392.         val r = new Random
  393.         val range = 0 to 255
  394.         val random = range(r.nextInt(range.length))
  395.        
  396.         if (random < escapeChange) {
  397.           this.user.currentMatch = None
  398.          
  399.           "Got away safely"
  400.         } else {
  401.           "Cant Escape!" + this.enemyTurn
  402.         }
  403.   }
  404.  
  405.   def useBall (ball: Item): Option[(String, Boolean)] = {
  406.     Some(ball.use(Some(this.enemyPokemon), this.user))
  407.   }
  408.    
  409.  
  410.   def isWon = {
  411.     if (enemy.hp <= 0 || enemy.isCaptured)
  412.       true
  413.     else
  414.       false
  415.    
  416.   }
  417.  
  418.   def isRoundWon = false //wild battles always result in victory when enemy pokemon is knocked out
  419.  
  420.   def nextPokemon = None
  421.  
  422.   def endBattle: String = {
  423.     this.user.party = this.user.originalParty
  424.  
  425.     this.user.currentMatch = None
  426.     ""
  427.   }
  428.  
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement