Further refactoring in PokeBattle_Pokemon

This commit is contained in:
jonisavo
2020-09-08 11:32:36 +03:00
parent 3e1c482c21
commit 1b3a1f3f3c

View File

@@ -1,21 +1,36 @@
# This class stores data on each Pokémon. Refer to $Trainer.party for an array # This class stores data on each Pokémon. Refer to $Trainer.party for an array
# of each Pokémon in the Trainer's current party. # of each Pokémon in the Trainer's current party.
class PokeBattle_Pokemon class PokeBattle_Pokemon
attr_accessor :name # Nickname # @return [String] nickname
attr_reader :species # Species (National Pokedex number) attr_accessor :name
attr_reader :exp # Current experience points # @return [Integer] national Pokédex number
attr_reader :hp # Current HP attr_reader :species
attr_reader :totalhp # Current Total HP # @return [Integer] current experience points
attr_reader :attack # Current Attack stat attr_reader :exp
attr_reader :defense # Current Defense stat # @return [Integer] current HP
attr_reader :speed # Current Speed stat attr_reader :hp
attr_reader :spatk # Current Special Attack stat # @return [Integer] current Total HP
attr_reader :spdef # Current Special Defense stat attr_reader :totalhp
attr_accessor :status # Status problem (PBStatuses) # @return [Integer] current Attack stat
attr_accessor :statusCount # Sleep count/Toxic flag attr_reader :attack
attr_accessor :abilityflag # Forces the first/second/hidden (0/1/2) ability # @return [Integer] current Defense stat
attr_accessor :genderflag # Forces male (0) or female (1) attr_reader :defense
attr_accessor :natureflag # Forces a particular nature # @return [Integer] current Speed stat
attr_reader :speed
# @return [Integer] current Special Attack stat
attr_reader :spatk
# @return [Integer] current Special Defense stat
attr_reader :spdef
# @return [Integer] status problem (from PBStatus)
attr_accessor :status
# @return [Integer] sleep count / toxic flag
attr_accessor :statusCount
# @return [0, 1, 2] forces the first / second / hidden ability
attr_accessor :abilityflag
# @return [0, 1] forces male (0) or female (1)
attr_accessor :genderflag
# @return [Integer] forces a particular nature (nature ID)
attr_accessor :natureflag
attr_accessor :natureOverride # Overrides nature's stat-changing effects attr_accessor :natureOverride # Overrides nature's stat-changing effects
attr_accessor :shinyflag # Forces the shininess (true/false) attr_accessor :shinyflag # Forces the shininess (true/false)
attr_accessor :moves # Moves (PBMove) attr_accessor :moves # Moves (PBMove)
@@ -59,38 +74,41 @@ class PokeBattle_Pokemon
#============================================================================= #=============================================================================
# Ownership, obtained information # Ownership, obtained information
#============================================================================= #=============================================================================
# Returns the public portion of the original trainer's ID.
# @return [Integer] public portion of the original trainer's ID
def publicID def publicID
return @trainerID & 0xFFFF return @trainerID & 0xFFFF
end end
# Returns whether the specified Trainer is NOT this Pokémon's original trainer. # @param trainer [PokeBattle_Trainer] the trainer to compare to the OT
# @return [Boolean] whether the trainer and OT don't match
def foreign?(trainer) def foreign?(trainer)
return @trainerID != trainer.id || @ot != trainer.name return @trainerID != trainer.id || @ot != trainer.name
end end
alias isForeign? foreign? alias isForeign? foreign?
# Returns the gender of this Pokémon's original trainer (2=unknown). # @return [0, 1, 2] gender of this Pokémon original trainer (0 = male, 1 = female, 2 = unknown)
def otgender def otgender
return @otgender || 2 return @otgender || 2
end end
# Returns this Pokémon's level when this Pokémon was obtained. # @return [Integer] this Pokémon's level when it was obtained
def obtainLevel def obtainLevel
return @obtainLevel || 0 return @obtainLevel || 0
end end
# Returns the time when this Pokémon was obtained. # @return [Time] time when this Pokémon was obtained
def timeReceived def timeReceived
return @timeReceived ? Time.at(@timeReceived) : Time.gm(2000) return @timeReceived ? Time.at(@timeReceived) : Time.gm(2000)
end end
# Sets the time when this Pokémon was obtained (in seconds since Unix epoch). # Sets the time when this Pokémon was obtained.
# @param value [Integer, Time, #to_i] time in seconds since Unix epoch
def timeReceived=(value) def timeReceived=(value)
@timeReceived = value.to_i @timeReceived = value.to_i
end end
# Returns the time when this Pokémon hatched. # @return [Time] time when this Pokémon hatched
def timeEggHatched def timeEggHatched
if obtainMode==1 if obtainMode==1
return @timeEggHatched ? Time.at(@timeEggHatched) : Time.gm(2000) return @timeEggHatched ? Time.at(@timeEggHatched) : Time.gm(2000)
@@ -99,7 +117,8 @@ class PokeBattle_Pokemon
end end
end end
# Sets the time when this Pokémon hatched (in seconds since Unix epoch). # Sets the time when this Pokémon hatched.
# @param value [Integer, Time, #to_i] time in seconds since Unix epoch
def timeEggHatched=(value) def timeEggHatched=(value)
@timeEggHatched = value.to_i @timeEggHatched = value.to_i
end end
@@ -107,13 +126,15 @@ class PokeBattle_Pokemon
#============================================================================= #=============================================================================
# Level # Level
#============================================================================= #=============================================================================
# Returns this Pokémon's level.
# @return [Integer] this Pokémon's level
def level def level
@level = PBExperience.pbGetLevelFromExperience(@exp,self.growthrate) if !@level @level = PBExperience.pbGetLevelFromExperience(@exp,self.growthrate) if !@level
return @level return @level
end end
# Sets this Pokémon's level. # Sets this Pokémon's level.
# @param value [Integer] new level (between 1 and the maximum level)
def level=(value) def level=(value)
if value<1 || value>PBExperience.maxLevel if value<1 || value>PBExperience.maxLevel
raise ArgumentError.new(_INTL("The level number ({1}) is invalid.",value)) raise ArgumentError.new(_INTL("The level number ({1}) is invalid.",value))
@@ -123,29 +144,30 @@ class PokeBattle_Pokemon
end end
# Sets this Pokémon's Exp. Points. # Sets this Pokémon's Exp. Points.
# @param value [Integer] new experience points
def exp=(value) def exp=(value)
@exp = value @exp = value
@level = nil @level = nil
end end
# Returns whether this Pokémon is an egg. # @return [Boolean] whether this Pokémon is an egg
def egg? def egg?
return @eggsteps > 0 return @eggsteps > 0
end end
alias isEgg? egg? alias isEgg? egg?
# Returns this Pokémon's growth rate. # @return [Integer] this Pokémon's growth rate (from PBGrowthRates)
def growthrate def growthrate
return pbGetSpeciesData(@species,formSimple,SpeciesGrowthRate) return pbGetSpeciesData(@species,formSimple,SpeciesGrowthRate)
end end
# Returns this Pokémon's base Experience value. # @return [Integer] this Pokémon's base Experience value
def baseExp def baseExp
return pbGetSpeciesData(@species,formSimple,SpeciesBaseExp) return pbGetSpeciesData(@species,formSimple,SpeciesBaseExp)
end end
# Returns a number between 0 and 1 indicating how much of the current level's # @return [Float] number between 0 and 1 indicating how much of the current level's
# Exp this Pokémon has. # Exp this Pokémon has
def expFraction def expFraction
l = self.level l = self.level
return 0.0 if l >= PBExperience.maxLevel return 0.0 if l >= PBExperience.maxLevel
@@ -158,7 +180,8 @@ class PokeBattle_Pokemon
#============================================================================= #=============================================================================
# Gender # Gender
#============================================================================= #=============================================================================
# Returns this Pokémon's gender. 0=male, 1=female, 2=genderless
# @return [0, 1, 2] this Pokémon's gender (0 = male, 1 = female, 2 = genderless)
def gender def gender
# Return sole gender option for all male/all female/genderless species # Return sole gender option for all male/all female/genderless species
genderRate = pbGetSpeciesData(@species,formSimple,SpeciesGenderRate) genderRate = pbGetSpeciesData(@species,formSimple,SpeciesGenderRate)
@@ -172,8 +195,8 @@ class PokeBattle_Pokemon
return ((@personalID & 0xFF) < PBGenderRates.genderByte(genderRate)) ? 1 : 0 return ((@personalID & 0xFF) < PBGenderRates.genderByte(genderRate)) ? 1 : 0
end end
# Returns whether this Pokémon species is restricted to only ever being one # @return [Boolean] whether this Pokémon species is restricted to only ever being one
# gender (or genderless). # gender (or genderless)
def singleGendered? def singleGendered?
genderRate = pbGetSpeciesData(@species,formSimple,SpeciesGenderRate) genderRate = pbGetSpeciesData(@species,formSimple,SpeciesGenderRate)
return genderRate == PBGenderRates::AlwaysMale || return genderRate == PBGenderRates::AlwaysMale ||
@@ -182,41 +205,45 @@ class PokeBattle_Pokemon
end end
alias isSingleGendered? singleGendered? alias isSingleGendered? singleGendered?
# Returns whether this Pokémon is male. # @return [Boolean] whether this Pokémon is male
def male? def male?
return self.gender == 0 return self.gender == 0
end end
alias isMale? male? alias isMale? male?
# Returns whether this Pokémon is female. # @return [Boolean] whether this Pokémon is female
def female? def female?
return self.gender == 1 return self.gender == 1
end end
alias isFemale? female? alias isFemale? female?
# Returns whether this Pokémon is genderless. # @return [Boolean] whether this Pokémon is genderless
def genderless? def genderless?
return self.gender == 2 return self.gender == 2
end end
alias isGenderless? genderless? alias isGenderless? genderless?
# Sets this Pokémon's gender to a particular gender (if possible). # Sets this Pokémon's gender to a particular gender (if possible).
# @param value [0, 1, 2] new gender (0 = male, 1 = female, 2 = genderless)
def setGender(value) def setGender(value)
@genderflag = value if !singleGendered? @genderflag = value if !singleGendered?
end end
# Makes this Pokémon male.
def makeMale; setGender(0); end def makeMale; setGender(0); end
# Makes this Pokémon female.
def makeFemale; setGender(1); end def makeFemale; setGender(1); end
#============================================================================= #=============================================================================
# Ability # Ability
#============================================================================= #=============================================================================
# Returns the index of this Pokémon's ability.
# @return [Integer] the index of this Pokémon's ability
def abilityIndex def abilityIndex
return @abilityflag || (@personalID & 1) return @abilityflag || (@personalID & 1)
end end
# Returns the ID of this Pokémon's ability. # @return [Integer] the ID of this Pokémon's ability
def ability def ability
abilIndex = abilityIndex abilIndex = abilityIndex
# Hidden ability # Hidden ability
@@ -240,24 +267,30 @@ class PokeBattle_Pokemon
return abilities || 0 return abilities || 0
end end
# Returns whether this Pokémon has a particular ability. # Returns whether this Pokémon has a particular ability. If no value
def hasAbility?(value=0) # is given, returns whether this Pokémon has an ability set.
abil = self.ability # @param ability [Integer] ability ID to check
return abil>0 if value==0 # @return [Boolean] whether this Pokémon has a particular ability or
return abil==getID(PBAbilities,value) # an ability at all
def hasAbility?(ability = 0)
current_ability = self.ability
return current_ability > 0 if ability == 0
return current_ability == getID(PBAbilities,ability)
end end
# Sets this Pokémon's ability to a particular ability (if possible). # Sets this Pokémon's ability index.
# @param value [Integer] new ability index
def setAbility(value) def setAbility(value)
@abilityflag = value @abilityflag = value
end end
# @return [Boolean] whether this Pokémon has a hidden ability
def hasHiddenAbility? def hasHiddenAbility?
abil = abilityIndex abil = abilityIndex
return abil!=nil && abil>=2 return abil!=nil && abil>=2
end end
# Returns the list of abilities this Pokémon can have. # @return [Array<Array<Integer>>] the list of abilities this Pokémon can have
def getAbilityList def getAbilityList
ret = [] ret = []
abilities = pbGetSpeciesData(@species,formSimple,SpeciesAbilities) abilities = pbGetSpeciesData(@species,formSimple,SpeciesAbilities)
@@ -278,26 +311,33 @@ class PokeBattle_Pokemon
#============================================================================= #=============================================================================
# Nature # Nature
#============================================================================= #=============================================================================
# Returns the ID of this Pokémon's nature.
# @return [Integer] the ID of this Pokémon's nature
def nature def nature
return @natureflag || (@personalID%25) return @natureflag || (@personalID%25)
end end
# Returns the calculated nature, taking into account things that change its # Returns the calculated nature, taking into account things that change its
# stat-altering effect (i.e. Gen 8 mints). Only used for calculating stats. # stat-altering effect (i.e. Gen 8 mints). Only used for calculating stats.
# @return [Integer] calculated nature
def calcNature def calcNature
return @natureOverride if @natureOverride return @natureOverride if @natureOverride
return self.nature return self.nature
end end
# Returns whether this Pokémon has a particular nature. # Returns whether this Pokémon has a particular nature. If no value
def hasNature?(value=-1) # is given, returns whether this Pokémon has a nature set.
nat = self.nature # @param nature [Integer] nature ID to check
return nat>=0 if value<0 # @return [Boolean] whether this Pokémon has a particular nature or
return nat==getID(PBNatures,value) # a nature at all
def hasNature?(nature = -1)
current_nature = self.nature
return current_nature >= 0 if nature < 0
return current_nature == getID(PBNatures,nature)
end end
# Sets this Pokémon's nature to a particular nature. # Sets this Pokémon's nature to a particular nature.
# @param value [Integer] nature to change to
def setNature(value) def setNature(value)
@natureflag = getID(PBNatures,value) @natureflag = getID(PBNatures,value)
calcStats calcStats
@@ -306,7 +346,8 @@ class PokeBattle_Pokemon
#============================================================================= #=============================================================================
# Shininess # Shininess
#============================================================================= #=============================================================================
# Returns whether this Pokémon is shiny (differently colored).
# @return [Boolean] whether this Pokémon is shiny (differently colored)
def shiny? def shiny?
return @shinyflag if @shinyflag != nil return @shinyflag if @shinyflag != nil
a = @personalID ^ @trainerID a = @personalID ^ @trainerID
@@ -330,12 +371,15 @@ class PokeBattle_Pokemon
#============================================================================= #=============================================================================
# Pokérus # Pokérus
#============================================================================= #=============================================================================
# Returns the Pokérus infection stage for this Pokémon.
# @return [Integer] the Pokérus infection stage for this Pokémon
def pokerusStrain def pokerusStrain
return @pokerus/16 return @pokerus/16
end end
# Returns the Pokérus infection stage for this Pokémon. # Returns the Pokérus infection stage for this Pokémon.
# @return [0, 1, 2] Pokérus infection stage
# (0 = not infected, 1 = cured, 2 = infected)
def pokerusStage def pokerusStage
return 0 if !@pokerus || @pokerus==0 # Not infected return 0 if !@pokerus || @pokerus==0 # Not infected
return 2 if @pokerus>0 && (@pokerus%16)==0 # Cured return 2 if @pokerus>0 && (@pokerus%16)==0 # Cured
@@ -343,6 +387,7 @@ class PokeBattle_Pokemon
end end
# Gives this Pokémon Pokérus (either the specified strain or a random one). # Gives this Pokémon Pokérus (either the specified strain or a random one).
# @param strain [Integer] Pokérus strain to give
def givePokerus(strain = 0) def givePokerus(strain = 0)
return if self.pokerusStage == 2 # Can't re-infect a cured Pokémon return if self.pokerusStage == 2 # Can't re-infect a cured Pokémon
strain = 1 + rand(15) if strain <= 0 || strain >= 16 strain = 1 + rand(15) if strain <= 0 || strain >= 16
@@ -369,6 +414,7 @@ class PokeBattle_Pokemon
#============================================================================= #=============================================================================
# Types # Types
#============================================================================= #=============================================================================
# Returns this Pokémon's first type. # Returns this Pokémon's first type.
def type1 def type1
return pbGetSpeciesData(@species,formSimple,SpeciesType1) return pbGetSpeciesData(@species,formSimple,SpeciesType1)