RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Auto Pricetag Equipment

0 Members and 1 Guest are viewing this topic.

**
Rep:
Level 83
プログラマです
Auto Pricetag Equipment
Version: v1.0
Author: SeMcDun
Date: 2014.12.27

Description


This script will automatically generate the price for equipment instead of setting each price individually using the database.
Depending on the values you enter, a different price is generated.

BASE_VALUE adds a value to all equipment
STAT adds a value to all equipment for each stat the equipment gives. (So if stat is set to 10 and a weapon has 4 attack, this would add 40 to the price of the equipment.)
FEATURE_PRICE adds a value to all equipment for each feature the weapon has.
WEAPON_PRICE adds a value to all weapons.
ARMOR_PRICE adds a value to all armors.

IGNORE_PHYSICAL can be set to true or false. This is whether or not you want the Element Attack - Physical feature to also add the FEATURE_PRICE value.

Notetags;
<lock_price>
Keep the same price you set in the database.

<added_price>
Adds a value after the price is generated.


Instructions

See the header of the script.

Script


Code: [Select]
#===============================================================================
# SeM's Auto Equipment Pricetag VXA
# - v1.0
# By SeMcDun
# Last Update: 29/12/2014
#===============================================================================
# Description;
# -- Automatically price weapons/armours
# -- Set the values in the head of the script and when the game is run, prices
#    will automatically be set
#===============================================================================
# Installation;
# -- Set the values in the head of the script.
# -- Add <lock_price> to stop a price being changed.
# -- Add <added_price x> to add x to the generated price.
#===============================================================================
$imported = {} if $imported.nil?
$imported["SEM-autoPrice"] = true
#===============================================================================
# * module SEM::AUTOPRICE
#-------------------------------------------------------------------------------
module SEM
  module AUTOPRICE
   
    # All Items
    # This value is added to all weapons/armour
    BASE_PRICE = 100
   
    # This value is added to all weapons
    WEAPON_PRICE = 800
   
    # This value is added to all armours
    ARMOR_PRICE = 300
   
    # Equipment
    # Each stat increases the cost by this amount
    STAT = 340
   
    # For each feature - add this value
    # (This ignores Element Attack - Physical by default)
    FEATURE_PRICE = 850
   
    # When set to true, Element Attack - Physical will be ignored
    # When set to false, Element Attack - Physical will also add the
    # FEATURE_PRICE value
    IGNORE_PHYSICAL = true
   
    # Set whether to use this feature for weapons/armour or both
    AUTOSET_WEAPON_PRICE = true
    AUTOSET_ARMOR_PRICE = true
   
   
  end
  module REGEXP
    LOCK_PRICE = /<lock[ _-]?price>/i
    ADDED_PRICE = /<added[ _-]?price\s*(\d+)>/i
  end
end

#==============================================================================
# ■ DataManager
#==============================================================================
module DataManager
 
  #--------------------------------------------------------------------------
  # alias method: load_database
  #--------------------------------------------------------------------------
  class <<self; alias load_database_auto_price load_database; end
  def self.load_database
    load_database_auto_price
    auto_price
  end
 
  #--------------------------------------------------------------------------
  # new method: auto_price
  #--------------------------------------------------------------------------
  def self.auto_price
    for weapon in $data_weapons
      next if weapon.nil?
      weapon.load_notetags_lock_price
      weapon.auto_set_price
    end
    for armor in $data_armors
      next if armor.nil?
      armor.load_notetags_lock_price
      armor.auto_set_price
    end
   
  end
 
 
end # DataManager

#==============================================================================
# ■ RPG::BaseItem
#==============================================================================

class RPG::BaseItem
 
  #--------------------------------------------------------------------------
  # common cache: load_notetags_lock_price
  #--------------------------------------------------------------------------
  def load_notetags_lock_price
    @lock_price = false
    @added_price = 0
    #---
    self.note.split(/[\r\n]+/).each { |line|
      case line
      #---
      when SEM::REGEXP::LOCK_PRICE
        @lock_price = true
      when SEM::REGEXP::ADDED_PRICE
        @added_price = $1.to_i
      #---
      end
    } # self.note.split
    #---
  end
 
  def lock_price?
    return @lock_price
  end
 
  def auto_set_price
    # If the item isn't a key item
    if lock_price? == false
      # Not a locked price - allow the price change
      process_auto_price
    end
  end
 
  def process_auto_price
    if self.is_a?(RPG::Weapon)
      if SEM::AUTOPRICE::AUTOSET_WEAPON_PRICE
        # Set price to 0 to begin
        self.price = 0
        self.price += SEM::AUTOPRICE::BASE_PRICE
        add_equipment_price
        add_feature_price
        add_weapon_price
        add_added_price
      end
    end
    if self.is_a?(RPG::Armor)
      if SEM::AUTOPRICE::AUTOSET_ARMOR_PRICE
        # Set price to 0 to begin
        self.price = 0
        self.price += SEM::AUTOPRICE::BASE_PRICE
        add_equipment_price
        add_feature_price
        add_armor_price
        add_added_price
      end
    end
  end
 
  def add_added_price
    @price += @added_price
  end
 
  def add_weapon_price
    @price += SEM::AUTOPRICE::WEAPON_PRICE
  end
 
  def add_armor_price
    @price += SEM::AUTOPRICE::ARMOR_PRICE
  end
 
  def add_equipment_price
    for i in 0..7
      # for each stat
      @price += SEM::AUTOPRICE::STAT * self.params[i]
    end
  end
 
  def add_feature_price
    self.features.each do |feature|
      if SEM::AUTOPRICE::IGNORE_PHYSICAL == true
        if !(feature.code == 31 && feature.data_id == 1)
          # Physical attack - Don't add the price
          @price += SEM::AUTOPRICE::FEATURE_PRICE
        end
      else
        # Not ignoring physical - Add the price no matter what feature is found
        @price += SEM::AUTOPRICE::FEATURE_PRICE
      end
    end
  end
 
 
end # RPG::BaseItem


Credit


  • SeMcDun

Support


For any problems or questions just leave a post and I'll try and help when I can.

Known Compatibility Issues


Terms of Use

Free to use in commercial or non-commercial games. Credit would be nice, but not really necessary with a small utility script like this.

« Last Edit: December 29, 2014, 10:39:55 AM by SeMcDun »

*
Last Stop
Rep:
Level 88
Everyone Off
Secret Santa 2013 ParticipantFor taking arms in the name of your breakfast.Secret Santa 2012 ParticipantSilver - GIAW 10Silver - GIAW 92011 Biggest Drama WhoreBronze - GIAW HalloweenGold - Game In A Week VII
I was actually expecting a spambot because of the title. hahaha. Cool script though.

*
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Rep:
Level 96
&&&&&&&&&&&&&&&&&&&&&&&&&&&
GIAW 14: 2nd Place (Hard Mode)2013 Zero to Hero2013 Biggest Drama WhoreParticipant - GIAW 11Secret Santa 2013 ParticipantFor taking arms in the name of your breakfast.
It's always nice to have another scripter around. :)
Thank you for sharing this with us.
&&&&&&&&&&&&&&&&

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
Interesting idea you have. I like how it is optional and generally easy to adjust before and afterwards in the database.

I do suggest changing the regular expressions a bit:
Code: [Select]
  module REGEXP
    LOCK_PRICE = /<lock[ _-]?price>/i
    ADDED_PRICE = /<added[ _-]?price\s*(\d+)>/i
  end

The grouping without backreference in particular was not needed since you are using the case insensitive option.
I suggest you try to explain what my changes means. What different strings will match now. (See a reference for help if needed)

*hugs*
 - Zeriab

**
Rep:
Level 83
プログラマです
With the /i being at the end, it means case insensitive (so you can delete the ADDED_PRICE|added_price stuff).
The [ _-] means the user can put in a space, an underscore or a dash in between the two words.
The ? means use the previous regular expression. Does this mean there could be any number of spaces, underscores or dashes in between the "<lock" and the "price>" ?
The \s means if the tag begins on one line and ends on another, it will still work?

I'm not sure what
* 0 or more previous regular expression
means. Is that saying, there is possibly no regular expression? When you use $1, $2 and so on to assign the regexp to a variable?

And the (\d+) is grouping together \d (a digit from 0 to 9) and the + is saying there is at least 1 regular expression inside the ( )?
Any amount of digits between the () becomes 1 regular expression.

I've been meaning to go through your regexp tutorial for a while now, actually. I continued learning how to code from scratch, but I kept franken-coding the regexp stuff.

Ps. I can rename the subject if it sounds too much like broken English. aha :)
I'm gonna go with "It was late when I posted this" ;)



*
Last Stop
Rep:
Level 88
Everyone Off
Secret Santa 2013 ParticipantFor taking arms in the name of your breakfast.Secret Santa 2012 ParticipantSilver - GIAW 10Silver - GIAW 92011 Biggest Drama WhoreBronze - GIAW HalloweenGold - Game In A Week VII
it doesn't sound like broken english, just the title seemed worded like a spambot would word it. Like "Used Sports Equipment" or something.

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
Good try ^_^
No need to hurry about reading my tutorial, though do let me know if you have questions or suggestions should end up reading it.

? means either 0 or 1 occurrence. I.e. either a single space, _underscore or -dash. Or no character at all. So the following for strings are all ok.
  • <lock price>
  • <lock_price>
  • <lock-price>
  • <lockprice>
\s is a whitespace character. Could be space, tabular, carriage return, new line. \s* means there can be any number of whitespace characters including 0. The following are all valid matches:
  • <added_price   123>
  • <added-price 42>
  • <addedprice19>
The last one is quite ugly, but still ok. If we use \s+ instead there must be at least one whitespace character so the last one, <addedprice19>, would not be valid then. That is why we have \d+ and not \d*. It makes no sense not to have a number in the added_price tag. The ( ) is used for the backreference. I.e. whatever is matching that in the parentheses are going in the $1, $2, etc. depending which grouping it is. (?: ) is used for when you do not want to include it in the $1, $2, etc.



When releasing a script it is usually a good idea to clean up old debugging code.
Code: [Select]
      #msgbox_p(line)

I would also say your comments are a bit too verbose.
Take for example
Code: [Select]
  def add_armor_price
    @price += SEM::AUTOPRICE::ARMOR_PRICE
  end # add_armor_price
The comment there give no new nor important information. Just keep it like this instead:
Code: [Select]
  def add_armor_price
    @price += SEM::AUTOPRICE::ARMOR_PRICE
  end

Another example:
Code: [Select]
  def add_equipment_price
    for i in 0..7
      # for each stat
      @price += SEM::AUTOPRICE::STAT * self.params[i]
    end # for each stat
  end # add_equipment_price
Here I would change the comments thus:
Code: [Select]
  def add_equipment_price
    # For each stat
    for i in 0..7
      @price += SEM::AUTOPRICE::STAT * self.params[i]
    end
  end

What do you think? Does it make sense?
*hugs*
 - Zeriab