Help - Search - Members - Calendar
Full Version: Checking Skill Level in DBZ_SkillLevel
RPG RPG Revolution Forums > Scripting > Script Development and Support > RGSS2
challudym
The script allows skills to have levels.
I'm having trouble with this... how do set a certain character's level of a skill to a variable o_O?

http://www.rpgmakervx.net/index.php?showtopic=28545

CODE
# ============================================================================ #
#
#     * DBZ_SkillLevel *
#     Author: dividedbyzero
#     Version: 1.1
#
# ============================================================================ #
# Introduction:
# ---------------------------------------------------------------------------- #
#   This script allows you to implement a Ragnarok Online-style skill point
# system in which skills do increased damage for each level. You can also
# set a sort of "skill tree"; certain skills may only be learned after you
# have another skill(s) at a specific level.
# ============================================================================ #
# Change Log:
# ---------------------------------------------------------------------------- #
# v1.1  - Added COMMANDS to "How to Use"
#       - Added support for resetting skills.
#       - Added support for setting skill point requirements and curves.
# v1.0  - THE BEGINNING.
# ============================================================================ #
# Features:
# ---------------------------------------------------------------------------- #
#   - Skills can be leveled up using skill points, increasing their power.
#   - Skills can require other skills at certain levels to be learned.
# ============================================================================ #
# How to Use:
# ---------------------------------------------------------------------------- #
# COMMANDS:
#   All commands must be prepended with "$game_actor[x].", where "x" is the ID
#   of the actor you are modifying.
#   add_skill_points(i)
#     Adds skill points according to "i".
#   learn_skill(i)
#     Learns skill ID "i".
#   unlock_skill(i)
#     Unlocks skill ID "i".
#   level_up_skill(i)
#     Levels up skill ID "i".
#   reset_skills
#     Removes all learned skills and grants skill points for their levels.
#
# SKILL SETUP:
#   Simply list a skill's requirements in its note tag using various "tags".
#   Such tags include:
#   <max_lvl x>
#     Sets a skill's maximum level.
#   <req x,y[|x,y]>
#     Sets a skill's requirements. The syntax is: (Skill ID),(Level). Seperate
#     multiple requirements with a | character like so.
#     e.g. <req 1,10|2,5>
#   <dmg_curve x>
#     Determines how much the damage of a skill increases per level. Also works
#     with negative values (healing).
#   <mp_curve x>
#     Determines how much the MP cost of a skill increases per level.
#
# MONSTER SETUP:
#   By default, a monster will use a skill at level 1. To set the skill levels
#   of a monster's skills, use the following "tag" in its note field:
#   <skill x,y[|x,y]>
#   "x" is the ID of the skill you're setting the level for and "y" is the level.
# ============================================================================ #
$imported = {} if $imported == nil
$imported["DBZ_SkillLevel"] = true
# ============================================================================ #
# * Configuration *
# ============================================================================ #
module DBZ
  module SkillLevel
    # How many skill points each character starts with.
    DEFAULT_SKP = 0
    # Appears on the icon of each skill along with its level.
    # e.g. "Lv."
    LVL_PREFIX = ""
    # Adjusts how many skill points an actor gets on level up.
    SKP_PERLEVEL = 1
    # How many skill points are required a level up.
    SKP_REQ = 1
    # Increases the skill point requirement each level.
    SKP_CURVE = 2
  end
  # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  # Do not edit beyond this point unless you know what you're doing.
  # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  module Regexp
    module Skill
      # Basic
      MAXLVL = /^<(?:maxlvl|max_lvl|max_level?)[ ]*(.*)>/i
      REQ = /^<(?:req|require|requires?)[ ]*(.*)>/i
      ENEMY_SKLV = /^<(?:skill?)[ ]*(.*)>/i
      # Curves
      DMG_CURVE = /^<(?:dmg_curve|dmg|damage?)[ ]*(.*)>/i
      MP_CURVE = /^<(?:mp_curve|mp?)[ ]*(.*)>/i
    end
  end
end
# ============================================================================ #
# * Game_Actor *
# ============================================================================ #
class Game_Actor
  alias sklvl_setup setup
  def setup(actor_id)
    @skill_lvls = []
    @unlocked_skills = []
    @skp = DBZ::SkillLevel::DEFAULT_SKP
    sklvl_setup(actor_id)
    @skills = []
    unlock_skills
  end
  
  def skill_points
    return @skp
  end
  
  def add_skill_points(num)
    @skp += num
  end
  
  def reset_skills
    skp = 0
    @skill_lvls.each { |i|
      skp += i == nil ? 0 : i
    }
    @skills = []
    @skp += skp
    unlock_skills
  end
  
  def unlocked_skills
    result = []
    for i in @unlocked_skills
      result.push($data_skills[i])
    end
    return result
  end
  
  def skill_level(skill_id)
    return @skill_lvls[skill_id]
  end
  
  def learn_skill(skill_id, free=true)
    unless skill_learn?($data_skills[skill_id])
      @skills.push(skill_id)
      unless free
        @skp -= DBZ::SkillLevel::SKP_REQ
      end
      @unlocked_skills.delete(skill_id)
      @skill_lvls[skill_id] = 1
    end
  end
  
  def unlock_skill(skill_id)
    unless skill_learn?($data_skills[skill_id]) and @unlocked_skills.include?(skill_id)
      @unlocked_skills.push(skill_id)
      @skill_lvls[skill_id] = 0
    end
  end
  
  def unlock_skills
    self.class.learnings.each { |i|
      if i.level <= @level and requirements_met(i.skill_id)
        @unlocked_skills.push(i.skill_id)
        @skill_lvls[i.skill_id] = 0
      end
    }
  end
  
  def level_up_skill(skill_id, free=true)
    @skill_lvls[skill_id] += 1
    unless free
      skp = DBZ::SkillLevel::SKP_REQ
      if DBZ::SkillLevel::SKP_CURVE > 0
        skp += (skill_level(skill_id)-1) * DBZ::SkillLevel::SKP_CURVE
      end
      @skp -= skp
    end
    unlock_skills
  end
  
  def level_up
    @level += 1
    @skp += DBZ::SkillLevel::SKP_PERLEVEL
    unlock_skills
  end
  
  def requirements_met(skill_id)
    if skill_learn?($data_skills[skill_id])
      return false
    end
    if @unlocked_skills.include?(skill_id)
      return false
    end
    item = $data_skills[skill_id].note
    item.split(/[\r\n]+/).each { |line|
      if line =~ DBZ::Regexp::Skill::REQ
        skills = $1.split('|')
        skills.each { |req|
          req = req.split(',')
          rskill= req[0].to_i
          rlvl = req[1].to_i
          if !@skills.include?(rskill)
            return false
          end
          if @skill_lvls[rskill] < rlvl
            return false
          end
        }
      end
    }
    return true
  end
  
  def max_lvl(skill_id)
    max_lvl = 1
    item = $data_skills[skill_id].note
    item.split(/[\r\n]+/).each { |line|
      if line =~ DBZ::Regexp::Skill::MAXLVL
        max_lvl = $1.to_i
      end
    }
    return max_lvl
  end
  
  def calc_mp_cost(skill)
    cost = get_curve($game_actors[@actor_id], skill, "mp")
    if half_mp_cost
      return cost / 2
    else
      return cost
    end
  end
  
  def sort_skills
    @skills.sort!
  end
end
# ============================================================================ #
# * Game_Battler *
# ============================================================================ #
class Game_Battler
  def skill_level(skill_id)
    level = 1
    item = enemy.note
    item.split(/[\r\n]+/).each { |line|
      if line =~ DBZ::Regexp::Skill::ENEMY_SKLV
        skills = $1.split('|')
        skills.each { |skill|
          skill = skill.split(',')
          return skill[1].to_i if skill[0].to_i == skill_id
        }
      end
    }
    return level
  end
  
  def get_curve(user, obj, stat)
    case stat
      when "dmg"
        dmg = obj.base_damage
        item = obj.note
        item.split(/[\r\n]+/).each { |line|
          if line =~ DBZ::Regexp::Skill::DMG_CURVE
            dmg = obj.base_damage
            #user.actor?
            skill_lvl = user.skill_level(obj.id)
            dmg += $1.to_i * (skill_lvl - 1)
          end
        }
        return dmg
      when "mp"
        mp = obj.mp_cost
        item = obj.note
        item.split(/[\r\n]+/).each { |line|
          if line =~ DBZ::Regexp::Skill::MP_CURVE
            mp = obj.mp_cost
            unless user.skill_level(obj.id) == 0
              mp += $1.to_i * (user.skill_level(obj.id) - 1)
            end
          end
        }
        return mp
      else
    end
  end
  
  def make_obj_damage_value(user, obj)
    damage = get_curve(user, obj, "dmg")            # get base damage
    if damage > 0                                   # a positive number?
      damage += user.atk * 4 * obj.atk_f / 100      # Attack F of the user
      damage += user.spi * 2 * obj.spi_f / 100      # Spirit F of the user
      unless obj.ignore_defense                     # Except for ignore defense
        damage -= self.def * 2 * obj.atk_f / 100    # Attack F of the target
        damage -= self.spi * 1 * obj.spi_f / 100    # Spirit F of the target
      end
      damage = 0 if damage < 0                      # If negative, make 0
    elsif damage < 0                                # a negative number?
      damage -= user.atk * 4 * obj.atk_f / 100      # Attack F of the user
      damage -= user.spi * 2 * obj.spi_f / 100      # Spirit F of the user
    end
    damage *= elements_max_rate(obj.element_set)    # elemental adjustment
    damage /= 100
    damage = apply_variance(damage, obj.variance)   # variance
    damage = apply_guard(damage)                    # guard adjustment
    if obj.damage_to_mp  
      @mp_damage = damage                           # damage MP
    else
      @hp_damage = damage                           # damage HP
    end
  end
end
# ============================================================================ #
# * Window_SkillPoint *
# ============================================================================ #
class Window_SkillPoint < Window_Base
  def initialize(x, y, actor)
    super(x, y, 180, WLH + 32)
    @actor = actor
    refresh
  end
  
  def refresh
    self.contents.clear
    cx = contents.text_size("Skill Points: ").width
    skp = @actor.skill_points
    self.x = 364
    self.width = 180
    self.contents.font.color = normal_color
    self.contents.draw_text(self.contents.rect, "Skill Points: " + skp.to_s, 0)
  end
end
# ============================================================================ #
# * Window_Skill *
# ============================================================================ #
class Window_Skill < Window_Selectable
  attr_accessor   :index
  
  def refresh
    @data = []
    id = []
    unlocked = []
    for skill in @actor.skills
      @data.push(skill)
      id.push(skill.id)
      unlocked.push(true)
      if skill.id == @actor.last_skill_id
        self.index = @data.size - 1
      end
    end
    unless $game_temp.in_battle
      for skill in @actor.unlocked_skills
        @data.push(skill)
        id.push(skill.id)
        unlocked.push(false)
        if skill.id == @actor.last_skill_id
          self.index = @data.size - 1
        end
      end
    end
    @item_max = @data.size
    create_contents
    for i in 0...@item_max
      draw_item(i, id[i], unlocked[i])
    end
  end
  
  def draw_item(index, skill_id, unlocked)
    rect = item_rect(index)
    self.contents.clear_rect(rect)
    skill = @data[index]
    if skill != nil
      rect.width -= 4
      enabled = @actor.skill_can_use?(skill)
      learned = @actor.skill_learn?($data_skills[skill_id])
      draw_item_name(skill, rect.x, rect.y, learned, enabled)
      self.contents.draw_text(rect, @actor.calc_mp_cost(skill), 2)
      rect.x += 1
      rect.y += 5
      self.contents.font.bold = true
      self.contents.font.size = 12
      if learned
        skill_lvl = DBZ::SkillLevel::LVL_PREFIX + @actor.skill_level(skill_id).to_s
        self.contents.draw_text(rect,skill_lvl,0)
      end
      self.contents.font.size = 20
      self.contents.font.color = Color.new(255,255,255,255)
      self.contents.font.bold = false
    end
  end
  
  def draw_item_name(item, x, y, learned, enabled = true)
    if item != nil
      draw_icon(item.icon_index, x, y, learned, enabled)
      self.contents.font.color = normal_color
      self.contents.font.color.alpha = enabled ? 255 : 128
      unless learned
        self.contents.font.color = Color.new(153, 153, 153, 255)
      end
      self.contents.draw_text(x + 24, y, 172, WLH, item.name)
    end
  end
  
  def draw_icon(icon_index, x, y, learned, enabled = true)
    bitmap = Cache.system("Iconset")
    rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
    if learned
      self.contents.blt(x, y, bitmap, rect, enabled ? 255 : 128)
    else
      gray = Tone.new(0, 0, 0, 255)
      self.contents.blt(x, y, bitmap, rect, 255)
      self.contents.font.color = Color.new(153, 153, 153, 255)
    end
  end
end
# ============================================================================ #
# * Scene_Skill *
# ============================================================================ #
class Scene_Skill < Scene_Base
    alias sklv_start start
    def start
      sklv_start
      @skp_window = Window_SkillPoint.new(384, 360, @actor)
    end
    
    alias sklv_terminate terminate
    def terminate
      sklv_terminate
      @skp_window.dispose
      $game_party.members[@actor_index].sort_skills
    end
    
    alias sklv_update update
    def update
      sklv_update
      #@skp_window.refresh
      #@skill_window.refresh
    end
    
    def update_skill_selection
    if Input.trigger?(Input::B)
      Sound.play_cancel
      return_scene
    elsif Input.trigger?(Input::R)
      Sound.play_cursor
      next_actor
    elsif Input.trigger?(Input::L)
      Sound.play_cursor
      prev_actor
    elsif Input.trigger?(Input::C)
      @skill = @skill_window.skill
      if @actor.skill_learn?(@skill)
        if @skill != nil
          @actor.last_skill_id = @skill.id
        end
        if @actor.skill_can_use?(@skill)
          Sound.play_decision
          determine_skill
        else
          Sound.play_buzzer
        end
      else
        Sound.play_buzzer
      end
      @actor.last_skill_id = nil
    elsif Input.trigger?(Input::X)
      skill_id = @skill_window.skill.id
      req = DBZ::SkillLevel::SKP_REQ
      req += (@actor.skill_level(skill_id)) * DBZ::SkillLevel::SKP_CURVE
      skill_points = @actor.skill_points >= req
      if @actor.skill_level(skill_id) < @actor.max_lvl(skill_id) and skill_points
        Sound.play_use_item
        unless @actor.skill_learn?($data_skills[skill_id])
          @actor.learn_skill(skill_id, false)
          @skill_window.index = @actor.skills.size-1
        else
          @actor.level_up_skill(skill_id, false)
        end
        @skp_window.refresh
        @skill_window.refresh
      else
        Sound.play_buzzer
      end
    end
  end
end
Jens of Zanicuud
First off, add this in the Materials section, under your script:

CODE
class Game_Actor < Game_Battler
#set level
def set_skill_level(skill_id, value)
@skill_lvls[skill_id] = value
end
#get level
def get_level(skill_id)
return @skill_lvls[skill_id]
end
end


To set a certain skill level via variable, call it this way via event or script:
CODE
$game_actors[actor_id].set_skill_leve(skill_id,
$game_variables[X])

where actor_id is to be replaced with the chosen actor id, skill_id with the id of the skill you want to modify and X with the desired variable's id.

To get the skill's level and set it to a variable, call it this way via event or script:
CODE
$game_variables[X] = $game_actors[actor_id].set_skill_leve(skill_id)

where, again, actor_id is to be replaced with the chosen actor id, skill_id with the id of the skill you want to modify and X with the desired variable's id.
I hope this can help,

Jens
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.