Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> Enemy can use items
cometstar
post May 25 2012, 12:53 PM
Post #1


Level 5
Group Icon

Group: Member
Posts: 60
Type: None
RM Skill: Undisclosed




Hello.

I need a script can make the following:
-Make enemies able to use items ,where I can specify for each enemy its available items and number of every item with scripts.
May this matter is weird but I need to use this in my project ,but if it wouldn't not done then no problem ; it's not so necessary.
Thank You
Go to the top of the page
 
+Quote Post
   
Night_Runner
post May 27 2012, 05:11 AM
Post #2


Level 50
Group Icon

Group: +Gold Member
Posts: 1,528
Type: Scripter
RM Skill: Undisclosed




Have a script happy.gif

Just keep in mind, it does a large number of changes to the battle system/how enemies function, so I can't guarantee that it will work well with other scripts that affect that area

CODE
#==============================================================================
# ** XP: Night_Runner's Evented Page Conditions
#------------------------------------------------------------------------------
# History:
#  Date Created: 27/May/2012
#  Created for: cometstar
#   @> http://www.rpgrevolution.com/forums/index.php?showtopic=56507
#
# Description:
#  This script allows developers to create enemies that use items in battle.
#
# How to Install:
#  Highlight and copy the entire script.
#  In your game's editor, select Tools >> Script Editor.
#  Along the left hand side scroll to the bottom, right click on on
#  'Main' and select 'Insert'.
#  Paste in the blank window on the right.
#==============================================================================



#==============================================================================
# ** Customisation
#==============================================================================

module NR_EnemyItems
  Enemy = []
  # Ghosts: Can Use Potions
  Enemy[1] =
    [
      {:item_id => 1, :rating => 10, :amount => 1},
    ]
  # Basilisks: Can Use High and Full Potions
  Enemy[1] =
    [
      {:item_id => 2, :rating => 10, :amount => 2},
      {:item_id => 3, :rating => 10, :amount => 3},
    ]
end



#==============================================================================
# ** RPG::Enemy::Action
#------------------------------------------------------------------------------
#  Edited to store the item_id for enemy attacks.
#==============================================================================

module RPG
  class Enemy
    class Action
      alias nr_eItems_initialize  initialize  unless $@
      def initialize(*args)
        @item_id = 0
        # Run the original initialize
        return nr_eItems_initialize(*args)
      end
      attr_accessor :item_id
    end
  end
end



#==============================================================================
# ** Game_Enemy
#------------------------------------------------------------------------------
#  Edited to allow items.
#==============================================================================

class Game_Enemy < Game_Battler
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :items
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias nr_eItems_initialize  initialize  unless $@
  alias nr_eItems_actions     actions     unless $@
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     troop_id     : troop ID
  #     member_index : troop member index
  #--------------------------------------------------------------------------
  def initialize(*args)
    # Run the original initiaze
    nr_eItems_initialize(*args)
    # Get a copy of this enemy's items
    @items = NR_EnemyItems::Enemy[@enemy_id].to_a.clone
    @item_actions = []
    for item in @items
      action = RPG::Enemy::Action.new
      action.kind = 2
      action.item_id = item[:item_id]
      action.rating = item[:rating]
      @item_actions << action
    end
  end
  #--------------------------------------------------------------------------
  # * Aquire Actions
  #--------------------------------------------------------------------------
  def actions(*args)
    # Add our actions to the original actions
    return nr_eItems_actions(*args) + @item_actions
  end
  #--------------------------------------------------------------------------
  # * Make Action
  #--------------------------------------------------------------------------
  def make_action
    # Clear current action
    self.current_action.clear
    # If unable to move
    unless self.movable?
      # End Method
      return
    end
    # Extract current effective actions
    available_actions = []
    highest_rating = 0
    for action in self.actions
      if action.kind == 2
        next if not can_use_item?(action)
      else
        next if not can_use_action?(action)
      end
      # Add this action to applicable conditions
      available_actions.push(action)
      if action.rating > highest_rating
        highest_rating = action.rating
      end
    end
    # Calculate total with max rating value at 3 (exclude 0 or less)
    ratings_total = 0
    for action in available_actions
      if action.rating > highest_rating - 3
        ratings_total += action.rating - (highest_rating - 3)
      end
    end
    # If ratings total isn't 0
    if ratings_total > 0
      # Create random numbers
      value = rand(ratings_total)
      # Set things that correspond to created random numbers as current action
      for action in available_actions
        if action.rating > highest_rating - 3
          if value < action.rating - (highest_rating - 3)
            set_action(action)
            return
          else
            value -= action.rating - (highest_rating - 3)
          end
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Can Use Item
  #--------------------------------------------------------------------------
  def can_use_item?(action)
    # Get the item associated with the action
    item = @items.select { |item| item[:item_id] == action.item_id }[0]
    # Skip if there's none of the item left
    return false if item[:amount] <= 0
    return false if get_item_usable_targets(action.item_id).empty?
    return true
  end
  #--------------------------------------------------------------------------
  # * Can Cause Action
  #--------------------------------------------------------------------------
  def can_use_action?(action)
    # Confirm turn conditions
    n = $game_temp.battle_turn
    a = action.condition_turn_a
    b = action.condition_turn_b
    if (b == 0 and n != a) or
       (b > 0 and (n < 1 or n < a or n % b != a % b))
      return false
    end
    # Confirm HP conditions
    if self.hp * 100.0 / self.maxhp > action.condition_hp
      return false
    end
    # Confirm level conditions
    if $game_party.max_level < action.condition_level
      return false
    end
    # Confirm switch conditions
    switch_id = action.condition_switch_id
    if switch_id > 0 and $game_switches[switch_id] == false
      return false
    end
    return true
  end
  #--------------------------------------------------------------------------
  # * Set Action
  #--------------------------------------------------------------------------
  def set_action(action)
    self.current_action.kind = action.kind
    self.current_action.basic = action.basic
    self.current_action.skill_id = action.skill_id
    self.current_action.item_id = action.item_id.to_i
    if action.kind == 2
      item = $data_items[action.item_id]
      if [2, 4, 6].include? item.scope
        self.current_action.target_index = -1
      else
        targets = get_item_usable_targets(item.id)
        target = targets[rand(targets.size)]
        if target.is_a?(Game_Enemy)
          self.current_action.target_index = $game_troop.enemies.index(target)
        else
          self.current_action.target_index = $game_party.actors.index(target)
        end
      end
    else
      self.current_action.decide_random_target_for_enemy
    end
  end
  #--------------------------------------------------------------------------
  # * Use Item
  #--------------------------------------------------------------------------
  def use_item(item_id)
    item = @items.select { |item| item[:item_id] == item_id }
    item[0][:amount] -= 1
  end
  #--------------------------------------------------------------------------
  # * Get Usable Targets
  #--------------------------------------------------------------------------
  def get_item_usable_targets(item_id)
    item = $data_items[item_id]
    targets = []
    # Use against enemies
    if [1, 2].include?(item.scope)
      # Loop through the actors
      for actor in $game_party.actors
        next if actor.dead?
        # Default to useless
        usable = false
        # If the item affects a parameter (max hp, strength, etc)
        usable |= true if item.parameter_type != 0
        # If it recovers hp
        recovers = item.recover_hp_rate > 0 || item.recover_hp > 0
        usable |= true if (recovers) and actor.hp < actor.maxhp
        # If it's a poison
        usable |= true if item.recover_hp_rate < 0 || item.recover_hp < 0
        # If it recovers sp
        recovers = item.recover_sp_rate > 0 || item.recover_sp > 0
        usable |= true if recovers and (actor.sp < actor.maxsp)
        # If it poisons sp
        usable |= true if item.recover_sp_rate < 0 || item.recover_sp < 0
        # If it affects something
        usable |= true if item.pdef_f != 0 || item.mdef_f != 0
        usable |= true if item.element_set != [] || item.plus_state_set != []
        usable |= true if item.minus_state_set != []
        # Add this actor to the targets if the item is usable
        targets << actor if usable == true
      end
    elsif [3, 4].include?(item.scope)
      # Loop through the enemies
      for enemy in $game_troop.enemies
        next if enemy.dead?
        # Default to useless
        usable = false
        # If the item affects a parameter (max hp, strength, etc)
        usable |= true if item.parameter_type != 0
        # If it recovers hp
        recovers = item.recover_hp_rate > 0 || item.recover_hp > 0
        usable |= true if (recovers) and enemy.hp < enemy.maxhp
        # If it's a poison
        usable |= true if item.recover_hp_rate < 0 or item.recover_hp < 0
        # If it recovers sp
        recovers = item.recover_sp_rate > 0 or item.recover_sp > 0
        usable |= true if recovers and (enemy.sp < enemy.maxsp)
        # If it poisons sp
        usable |= true if item.recover_sp_rate < 0 or item.recover_sp < 0
        # If it affects something
        usable |= true if item.pdef_f != 0 or item.mdef_f != 0
        usable |= true if item.element_set != [] or item.plus_state_set != []
        usable |= true if item.minus_state_set != []
        # Add this actor to the targets if the item is usable
        targets << enemy if usable == true
      end
    elsif item.scope == 5
      for enemy in $game_troop.enemies
        targets << enemy if enemy.dead?
      end
    elsif item.scope == 6
      for actor in $game_party.actors
        targets << actor if actor.dead?
      end
    end
    return targets
  end  
end



#==============================================================================
# ** Scene_Battle
#------------------------------------------------------------------------------
#  Edited to allow enemies using items.
#==============================================================================

class Scene_Battle
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias nr_eItems_make_item_action_result  make_item_action_result  unless $@
  #--------------------------------------------------------------------------
  # * Make Item Action Results
  #--------------------------------------------------------------------------
  def make_item_action_result(*args)
    if @active_battler.is_a?(Game_Enemy)
      @active_battler.use_item(@active_battler.current_action.item_id)
      @item = $data_items[@active_battler.current_action.item_id]
      # Display item name on help window
      @help_window.set_text(@item.name, 1)
      # Set animation ID
      @animation1_id = @item.animation1_id
      @animation2_id = @item.animation2_id
      # Set common event ID
      @common_event_id = @item.common_event_id
      # Decide on target
      index = @active_battler.current_action.target_index
      if [1, 2].include?(@item.scope)
        target = $game_party.smooth_target_actor(index)
      else
        target = $game_troop.smooth_target_enemy(index)
      end
      # Set targeted battlers
      set_target_battlers(@item.scope)
      # Apply item effect
      for target in @target_battlers
        target.item_effect(@item)
      end
    # Otherwise, run the original make_item_action_result
    else
      return nr_eItems_make_item_result(*args)
    end
  end
end



#==============================================================================
# ** End of Script.
#==============================================================================


__________________________
K.I.S.S.
Want help with your scripting problems? Upload a demo! Or at the very least; provide links to the scripts in question.

Most important guide ever: Newbie's Guide to Switches
Go to the top of the page
 
+Quote Post
   
cometstar
post May 27 2012, 12:24 PM
Post #3


Level 5
Group Icon

Group: Member
Posts: 60
Type: None
RM Skill: Undisclosed




Hello!
I'm very thankful to youbecause you made this script for me
I need it for my project for special purposes.
I'll try it and hopefully it will work.

I give special thank for you "Night Runner" because you made many scripts for my request
indeed you are perfect scripter .
Go to the top of the page
 
+Quote Post
   
cometstar
post Sep 14 2012, 02:33 PM
Post #4


Level 5
Group Icon

Group: Member
Posts: 60
Type: None
RM Skill: Undisclosed




Thank you for making this script for my request.
Now ,I have just recently tried it to see if it works well.
But Unfortunatly it isn't work absolutely
There are no wrongs but it seems useless where
I tried it in battle test with Ghost troops ,but the enemy didn't use items..
so, I tried to delete all Ghosts' action from database ,may that do something ,but it's useless.
So, I wish if you can try it and check it out to find the cause for its useless
and fix it if it can be done
Anyway ,I appreciate your effort to make it and hopefully it will well......
Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 25th May 2013 - 05:44 AM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker