The RPG Maker Resource Kit

RMRK RPG Maker Creation => VX => VX Scripts Database => Topic started by: modern algebra on July 09, 2011, 03:09:30 AM

Title: Wounded Enemy Graphics
Post by: modern algebra on July 09, 2011, 03:09:30 AM
Wounded Enemy Graphics
Version: 1.0a
Author: modern algebra
Date: July 29, 2011

Version History



Description


This script allows you to change the graphics used for enemy battlers and depending on the amount of HP they have left. This allows you to make them look wounded when they get below a certain percentage of health

Features


Screenshots

(https://rmrk.net/proxy.php?request=http%3A%2F%2Fimg94.imageshack.us%2Fimg94%2F2001%2Fwounded1.png&hash=bbb27ab7bce784efe971ce946f6495bb0c31aea7)

Instructions

Paste this script into its own slot above Main but below Materials in the Script Editor. If you are using the Note Editor and General Patch, then this script should go below it.

See the header of the script for instructions on use.

Script


Code: [Select]
#==============================================================================
#    Wounded Enemy Graphics
#    Version: 1.0a
#    Author: modern algebra (rmrk.net)
#    Date: July 29, 2011
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Description:
#
#    This script allows you to change the graphics used for enemy battlers and
#   depending on the amount of HP they have left. This allows you to make them
#   look wounded when they get below a certain percentage of health
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Instructions:
#
#    Paste this script into its own slot above Main but below Materials in the
#   Script Editor. If you are using the Note Editor and General Patch, then
#   this script should go below it.
#
#    This script is easy to use. To setup a wounded graphic, all you need to do
#   is put the following code in the notebox of an enemy:
#
#        \WG[threshold, "filename", hue]
#           threshold : this is an integer and it is the percentage of HP below
#             which this graphic will be shown. If excluded, it defaults to the
#             value you set at line 50.
#           filename  : the name of the battler graphic it changes to. You
#             should use quotation marks to avoid ambiguity (especially with
#             filenames that are numbers) but you can also omit them.
#           hue       : the hue the graphic is when it changes. Defaults to 0
#             if excluded.
#
#    EXAMPLES:
#
#      \wg[20, "Slime"]
#   The graphic would change to slime with hue 0 when the enemy goes below 20%
#  health
#
#      \Wg["54", 70]
#    The graphic would change to that of 54 with hue 70 when the enemy's HP
#   falls below whatever percentage you set at line 50 (by default, 15)
#
#      \WG[40, Slime, 75]
#    The graphic would change to Slime with hue 75 when the enemy goes below
#   40% health.
#==============================================================================

$imported = {} unless $imported
$imported["MAWoundedEnemyGraphics"] = true

#  The below value is the default HP threshold to show a new graphic.
MAWEG_DEFAULT_THRESHOLD = 15

#==============================================================================
# ** RPG::Enemy
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Summary of Changes:
#    new method - wounded_graphics
#    aliased method (if using Note Editor & Patch) - ma_reset_note_values
#==============================================================================

class RPG::Enemy
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Wounded Graphics
  #``````````````````````````````````````````````````````````````````````````
  #  This method interprets the notes and returns an array with each battler
  # in the form [threshold, battler_graphic, hue]
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def wounded_graphics
    if !@wounded_graphics
      @wounded_graphics = { 100 => [self.battler_name, self.battler_hue] }
      self.note.gsub (/\\WG\[\s*(\d*?)\s*[,;]?\s*\"?([^,;\]\"]+)\"?[,;]?\s*(\d*?)\s*\]/i) {
        threshold = $1.empty? ? MAWEG_DEFAULT_THRESHOLD : $1.to_i
        @wounded_graphics[threshold] = [$2, $3.to_i]; ""
      }
    end
    return @wounded_graphics
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Reset Note Values
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  if self.method_defined? (:ma_reset_note_values)
    alias mlg_woundgrp_resetnote_6th2 ma_reset_note_values
    def ma_reset_note_values (*args)
      mlg_woundgrp_resetnote_6th2 (*args) # Run Original Method
      @wounded_graphics = nil
    end
  end
end

#==============================================================================
# ** Game_Enemy
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Summary of Changes:
#    aliased method - hp=
#==============================================================================

class Game_Enemy
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Set HP
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias mag_wound_sethp_6gz9 hp= unless self.method_defined? (:mag_wound_sethp_6gz9)
  def hp= (*args, &block)
    mag_wound_sethp_6gz9 (*args, &block) # Run Original Method
    wg = enemy.wounded_graphics
    return if wg.empty? || self.dead? # Ignore if no wounded set or dead
    # Set battle graphics depending on whether the threshold has been reached
    health_percent = (self.hp * 100) / self.maxhp
    wg.keys.sort.each { |threshold|
      if health_percent <= threshold
        @battler_name, @battler_hue = *wg[threshold]
        break
      end
    }
  end
end

Credit



Support


Please post in this topic at RMRK for support, no matter how old the topic is. Do not PM me.

Known Compatibility Issues

Except for scripts that dramatically change battler graphics, this should be compatible with most other scripts. If using the Note Editor, then the General Compatibility Patch is required and both should be above this script in the Editor.
Title: Re: Wounded Enemy Graphics
Post by: cozziekuns on July 09, 2011, 05:10:16 AM
Flawles scripting as always, MA! I wish my REGEXP skills were as good as yours :P
Title: Re: Wounded Enemy Graphics
Post by: pacdiggity on July 09, 2011, 05:32:07 AM
... I do not understand quite a bit of what's going on here:
Code: [Select]
  def wounded_graphics
    if !@wounded_graphics
      @wounded_graphics = { 100 => [self.battler_name, self.battler_hue] }
      self.note.gsub (/\\WG\[(\d*?)[,;]?\s*([^,;\]]+)[,;]?\s*(\d*?)\]/i) {
        threshold = $1.empty? ? MAWEG_DEFAULT_THRESHOLD : $1.to_i
        hue = $3.empty? ? 0 : $3.to_i
        @wounded_graphics[threshold] = [$2.to_s, hue]
      }
    end
    return @wounded_graphics
  end
Seriously, your notebox workings are amazing.
Title: Re: Wounded Enemy Graphics
Post by: Zeriab on July 14, 2011, 08:02:40 AM
Hey Modern,
Can you tell me what happens if I put \WG[40, Slime ,75] in the note for an enemy?

*hugs*
Title: Re: Wounded Enemy Graphics
Post by: modern algebra on July 14, 2011, 12:57:52 PM
It would throw an error since it would be asking for a graphic named "Slime ". However, I'm not sure how I could safely avoid it. To do so, I couldn't just add a space to the [^,;\]] since I want it to capture battlers with two words in their name (like "Dark Knight"). I'd have to do it like this:

Code: [Select]
([^,;\]]+?)\s*[,;]?

But that creates a new problem since I need that code to be greedy. Otherwise the RegExp will have problems when trying to use graphics that end in numbers and where the user does not set the hue. Ie. a code like this:

\WG[20, Angel1]

Since I intentionally made the last argument optional, none of the [,;]?\s*... is necessary for the RegExp to be met so the code, when the middle argument isn't greedy, would separate the 1 from the Angel and give us a battler "Angel" with a hue of 1, which would cause an error.

Maybe the better solution would be to leave it as it is but include a code below to shear off any end spaces from $2? Then the code would be (also adding to it a couple other buffers since the code would have also been corrupted by putting spaces before the very end or before the first comma):

Code: [Select]
      self.note.gsub (/\\WG\[(\d*?)\s*[,;]?\s*([^,;\]]+)[,;]?\s*(\d*?)\s*\]/i) {
        threshold = $1.empty? ? MAWEG_DEFAULT_THRESHOLD : $1.to_i
        hue = $3.to_i
        filename = $2.dup
        filename.slice! (/\s*\z/)
        @wounded_graphics[threshold] = [filename, hue]
      }

Do you think that would work or have I missed something?
Title: Re: Wounded Enemy Graphics
Post by: Sashikinaroji on July 14, 2011, 07:51:58 PM
I think a simpler way to do it would be to have the spaces be the EoS and then use underscores between words then have the script check the string for an underscore and make the underscores into spaces... Or I've just done what my professor tells me what I always do and made a simple problem complex... I've never been good with that.

And this isn't exactly my language of choice, neither...
Title: Re: Wounded Enemy Graphics
Post by: cozziekuns on July 14, 2011, 08:56:32 PM
Slicing the filename's spaces at the end seems like the best method. Or you could just keep the error message telling the user that they did something wrong if your lazy.

Also something like \wg[ 80, Slime , 75 ] with a space in front of the damaged percent would return an empty threshold, hue and filename and produce no error so if you're really paranoid you can check for spaces before the threshold.
Title: Re: Wounded Enemy Graphics
Post by: exhydra on July 14, 2011, 09:11:07 PM
Or perhaps put the name in quotations?

\WG[20, "Death Angel", 1]

Then there would be an easy start and end point, and you'd get to keep the spaces?

\\WG\[(\d*?)\s*[,;]?\s*\"([^,;\]]+)\"[,;]?\s*(\d*?)\s*\]

Don't mind me ... fiddling with this Regex Creator I found while trying to figure out how this arcane language works ...
Title: Re: Wounded Enemy Graphics
Post by: modern algebra on July 14, 2011, 10:27:18 PM
@Sashi - that wouldn't work since we'd want filenames to be able to have underscores in them, and automatically removing them would mean that a filename "Angel_1" for instance, could never be recognized. Besides, the error we are trying to resolve is one that would essentially be a user error (in the sense that it would occur because the user weren't following format). Our goal should, therefore, be the loosening of unnecessary format restrictions and imposing a stricter format with underscores would not really resolve it.

@Exhydra - Quotations would certainly work, though we'd need to include \" in the [^,;\]]. To some extent, it also comes within my enforcing a stricter format objection, but it would be a fine way to solve the problem in a simpler way.

@cozzie - thanks for the headsup; you're right and I am really paranoid :)
Title: Re: Wounded Enemy Graphics
Post by: exhydra on July 14, 2011, 10:34:21 PM
Hm, when I parsed the the regex, it seemed to work alright. Well, I was just looking to remove the quotations from the returned value of the file name so that there wouldn't be a need to take an extra step of chopping them off.

At any rate, slowly learning these ancient runes called regex ...  D;
Title: Re: Wounded Enemy Graphics
Post by: modern algebra on July 14, 2011, 11:29:30 PM
Never mind, you're right about that. I was thinking about something else, but yeah - you wouldn't need to include the \".
Title: Re: Wounded Enemy Graphics
Post by: pacdiggity on July 15, 2011, 12:55:21 AM
Exhydra, stop being so smart and resourceful. It's making us all look bad. And by "us all", I mean "me".

Anyway, my outrageous suggestion. You could make the tags for threshold, graphic and hue different. i.e.
Code: [Select]
when /<(?:WG|wg)[ ](.*):[ ](\d+)>/i
  case $1.upcase
    when "THRESHOLD"
      @threshold = $2.to_i
    when "HUE"
      @hue = $2.to_i
    etc.
But don't listen to me, I'm sure every other person's ideas are much better than mine ;9
Title: Re: Wounded Enemy Graphics
Post by: exhydra on July 15, 2011, 01:09:21 AM
Eh, there's plenty of good ways to go about reigning in the possible errors. My suggestion isn't amazing. :P  Adding a piece of text to the script instructions telling the user not to add extra, unneeded spaces would work just as well. Maybe toss in an error message too ... it could read : 'Stop being silly.'
Title: Re: Wounded Enemy Graphics
Post by: Zeriab on July 15, 2011, 08:38:04 AM
You have all missed this ambiguity: \wg[40, 30]
What should happen when you have two arguments and the filename is a number?
You need to make a decision as to whether it will be percentage and filename or filename and hue.

I could take the decision for you and give you somethingl like this:
/\\WG\[(\s*(\d+?)\s*[,;])?\s*([^,;\]]+?)\s*([,;]\s*(\d+)\s*)?\]/i

Mind you, while a filename cannot start with a space it can end with spaces (before the extension which you do not include)
One way to accomodate for that possibility is to add a case using quotation marks, now requiring quotation marks both places "My pic" and disallowing My pic" and "My pic is fairly bothersome the easiest way probably being something like this: (Maybe it's ok to allow those two cases)

/\\WG\[(\s*(\d+?)\s*[,;])?\s*([^,;\]]+?)\s*([,;]\s*(\d+)\s*)?\]|\\WG\[(\s*(\d+?)\s*[,;])?\s*\"([^,;\]]+?)\"\s*([,;]\s*(\d+)\s*)?\]/i


Of course you can split up and have several regex checks rather than compressing all the possibilities into one.

I hope this have giving you some ideas for solving the problem.

*hugs*
Title: Re: Wounded Enemy Graphics
Post by: Infinate X on July 19, 2011, 06:34:30 PM
If this works with tankentai ATB and can be used for dead enemes then I'll will definatley use this script.
Title: Re: Wounded Enemy Graphics
Post by: modern algebra on July 19, 2011, 07:29:03 PM
It probably works for Tankentai. It won't for dead enemies unless you fiddle with the collapse stuff.