Fallout/Elder Scrolls-style Random Vendor Inventory

It'd be amazing if someone could make me a Fallout-style (or Elder Scrolls-style) random vendor script in this fashion. If I can think of improvements I will add them later.
  • Randomly chooses items with a notetag like <vendor item I: or W: or A: # to #> in the database to determine if it's sellable by vendors, with the number being a rarity range. The rarities are also defined in the script.
  • Vendors reset when a switch is turned on, or a specified frame amount of time passes.
On the vendor event:
  • Definable inventory size so I can determine if I want a specific vendor to have 5 items, 20 items, etc.
  • Definable currency amount (30 to 200 for example) for the vendor to have.
  • Definable item, weapon, or armor slots that roll randomly in that database, with a rarity range.
For example, a vendor event would look like this. They would sell 2 items, 2 weapons, and 1 armor randomly picked.
1681964940742.png
Could this kind of work?
It's giving me an error. Most of my items/equipment are set up for Galv's Random Loot already so I don't have to do a ton of work, but when I try to access a shop it does this:
1682003444184.png

This is how a test shop looks:
1682003485184.png


I can't look at it much right now since I have to leave for work, I'll look more into it when I get back. I also have the script at the bottom of my load order.
Could it be the order of the scripts? Sometimes the order play an important role
Could it be the order of the scripts? Sometimes the order play an important role
I tried placing it up above everything else, didn't work. It works when I put it in a new project so maybe there's a particular script it doesn't like, idk.
I figured out what was causing the issue, its Hime's Shop Manager and Shop Stock. Still, the Simple Random Shop script doesn't work in the way I'm looking for because it randomly picks blocks from the database and not picking per item, which is really bad.

If anyone's willing to tackle my original request then I'd be eternally grateful.
I first set out to see if I can get the random shop script to work with Hime's Shop Manager & Shop Stock.
Give this a try and see if it works for you.
Spoiler: Shop Compatibility
Ruby:
# ╔═════════════════════════════════════╦════════════════════╗
# ║ Title: Random Shop Fix              ║  Version: 1.00     ║
# ║ Author: Roninator2                  ║                    ║
# ╠═════════════════════════════════════╬════════════════════╣
# ║ Function:                           ║   Date Created     ║
# ║                                     ╠════════════════════╣
# ║ Compatibility for Hime and Fhizban  ║    26 May 2023     ║
# ╚═════════════════════════════════════╩════════════════════╝
# ╔══════════════════════════════════════════════════════════╗
# ║ Instructions:                                            ║
# ║                                                          ║
# ║   Script order                                           ║
# ║     Galv's Random Loot                                   ║
# ║     Hime Shop Manager                                    ║
# ║     Hime Shop Stock                                      ║
# ║     Fhizban Random Shop                                  ║
# ║     This fix                                             ║
# ║                                                          ║
# ║   Set switch number below                                ║
# ║     Used to control if loading Hime Shop or Fhizban      ║
# ║                                                          ║
# ║   Set option to show a message when the random shop      ║
# ║     does not produce any items                           ║
# ║   Set message for empty shop                             ║
# ║                                                          ║
# ╚══════════════════════════════════════════════════════════╝
# ╔══════════════════════════════════════════════════════════╗
# ║ Terms of use:                                            ║
# ║ Free for all uses in RPG Maker - Except Nudity           ║
# ╚══════════════════════════════════════════════════════════╝

module R2_Fhizban_Hime_Random_Shop_Fix
  # Switch to activate random shop and not shop manager
  Switch = 5
  # show message that the shop is empty
  Show_Message = false
  Shop_Message = "The Shop is Empty"
end

#==============================================================================
# ** Game_Interpreter
#==============================================================================
class Game_Interpreter
  def random_shop(type, rarity_min, rarity_max, price=0, amount=0, subtype=0, family=0)
    $game_switches[R2_Fhizban_Hime_Random_Shop_Fix::Switch] = true
 
    @goods = []
    @loot = []
 
    #--- Determine Type
    if type == 0
      @loot = $data_items + $data_armors + $data_weapons
      subtype = 0
    elsif type == 1
      @loot = $data_items
      @itemtype = 0
      subtype = 0
    elsif type == 2
      @loot = $data_armors
      @itemtype = 2
    elsif type == 3
      @loot = $data_weapons
      @itemtype = 1
    else
      @loot = $data_items | $data_armors | $data_weapons
      subtype = 0
    end
 
    #--- Preperations
    @loot.shuffle
    rarity_max = rarity_min if rarity_max < rarity_min
    amount = Random_Shop::DEFAULT_AMOUNT if amount < 1
    subtype = 0 if subtype < 0 || subtype > 99
    subtype = 0 if family < 0 || family > 99
 
    #--- Determine Party Luck Bonus
    mem = 0
    eqs = 0
    luck_bonus = 0
    i = 0
    no_equips = $game_party.members.count * $game_party.members[0].equips.count
    $game_party.members.each do |mem|
      mem.equips.each do |eq|
        next if eq.nil?
        luck_bonus += eq.loot_lucky
      end
    end
     
    #--- Determine Rarity Chance
    checked = 0
    inventory = 0
    itemprice = 0
    duplicates = []
    restart = false
    continue = true
    no_items = @loot.count - 1
    rare_chance = rand(rarity_max - rarity_min) + rarity_min + luck_bonus + 1
    random_item = rand(no_items) + 1
    begin_count = rand(no_items) + 1

    #--- Determine Random Item
    no_items.times do |i|
      i = i + random_item
      begin_count += 1 if restart
      if i > no_items
        i = i - i + rand(no_items) + 1
        restart = true
      end
 
      #--- Get Random Item
      if family != 0 && @loot[i].loot_family != family
        continue = false
      end
      if subtype != 0 && type == 2
        if @loot[i].atype_id == subtype
          continue = false
        end
      end
      if subtype != 0 && type == 3
        if @loot[i].wtype_id == subtype
          continue = false
        end
      end

      #--- Setup Shop Inventory
      if continue
        next if @loot[i].nil?
        next if @loot[i].loot_noshop
        if @loot[i].loot_level_max >= $game_party.leader.level &&
          @loot[i].loot_level_min <= $game_party.leader.level
          if @loot[i].loot_rarity <= rare_chance
            if checked < no_items && inventory < amount
              if !duplicates.include?(@loot[i].id)
                #--- Recheck Item Type
                if @loot[i].is_a?(RPG::Item)
                  @itemtype = 0
                elsif @loot[i].is_a?(RPG::Weapon)
                  @itemtype = 1
                elsif @loot[i].is_a?(RPG::Armor)
                  @itemtype = 2
                end
             
                #--- Check Item Price
                if price != 0
                  itemprice = @loot[i].price + (@loot[i].price * (price/100))
                end
                if price <= 0
                  @goods.push([@itemtype, @loot[i].id, 0])
                else
                  @goods.push([@itemtype, @loot[i].id, 1, itemprice])
                end
                duplicates.push(@loot[i].id)
                inventory += 1
              end
            else
              break
            end
          end
        end
      end
      checked += 1
    end
    if @goods.size == 0 && R2_Fhizban_Hime_Random_Shop_Fix::Show_Message == true
      $game_message.add(R2_Fhizban_Hime_Random_Shop_Fix::Shop_Message)
    end

    if @goods.size > 0
      @goods.shuffle
      SceneManager.call(Scene_Shop)
      SceneManager.scene.prepare_original(@goods, true)
    end

  end
end # Game_Interpreter

#==============================================================================
# ** Window_ShopBuy
#==============================================================================

class Window_ShopBuy < Window_Selectable
  #--------------------------------------------------------------------------
  # * Display in Enabled State?
  #--------------------------------------------------------------------------
  def enable?(item)
    if $game_switches[R2_Fhizban_Hime_Random_Shop_Fix::Switch] == true
      item && price(item) <= @money && !$game_party.item_max?(item)
    else
      return false unless @goods[item].enable?
      th_shop_manager_enable?(item)
    end
  end
  #--------------------------------------------------------------------------
  # * Create Item List
  #--------------------------------------------------------------------------
  def make_item_list
    if $game_switches[R2_Fhizban_Hime_Random_Shop_Fix::Switch] == true
      @data = []
      @price = {}
      @shop_goods.each do |goods|
        case goods[0]
        when 0;  item = $data_items[goods[1]]
        when 1;  item = $data_weapons[goods[1]]
        when 2;  item = $data_armors[goods[1]]
        end
        if item
          @data.push(item)
          @price[item] = goods[2] == 0 ? item.price : goods[3]
        end
      end
    else
      @data = []
      @goods = {}
      @price = {}
      @shop_goods.each do |shopGood|
        next unless include?(shopGood)
        item = shopGood.item
        @data.push(item)
        @goods[item] = shopGood
        @price[item] = shopGood.price
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Draw Item
  #--------------------------------------------------------------------------
  def draw_item(index)
    if $game_switches[R2_Fhizban_Hime_Random_Shop_Fix::Switch] == true
      item = @data[index]
      rect = item_rect(index)
      draw_item_name(item, rect.x, rect.y, enable?(item))
      rect.width -= 4
      draw_text(rect, price(item), 2)
    else
      th_shop_stock_draw_item(index)
      rect = item_rect(index)
      item = @data[index]
      shopGood = @goods[item]
      draw_text(rect, shopGood.stock, 1) unless shopGood.unlimited?
    end
  end
end

#==============================================================================
# ** Scene_Shop
#==============================================================================

class Scene_Shop < Scene_MenuBase
  #--------------------------------------------------------------------------
  # Adjusted. The scene now takes a Game_Shop object
  #--------------------------------------------------------------------------
  def prepare_original(goods, purchase_only)
    @goods = goods
    @purchase_only = purchase_only
  end
  #--------------------------------------------------------------------------
  # * Create Command Window
  #--------------------------------------------------------------------------
  alias r2_create_command_compat_fix  create_command_window
  def create_command_window
    r2_create_command_compat_fix
    @command_window.set_handler(:cancel, method(:close_compat))
  end
  def close_compat
    $game_switches[R2_Fhizban_Hime_Random_Shop_Fix::Switch] = false
    return_scene
  end
end

Not a very effective script. Please don't use unless it is the exact one you want.
Better random shop script posted on next page by me.
@Roninator2 Sorry, I had no idea anyone was still commenting on this, I never get notifications for topic replies! Anyway, I can switch between the two shops if I do this:

1688686092559.png


But when I add the script switch in there, this pulls up an unmodified random shop:
1688686297365.png

I'm not sure what the script switch is supposed to achieve. It says it allows switching between the two, but I can do that without any switch on. The random shop is also bunched together still, like below. I only saw it random in the way I want only once out of a bunch of times, and no shop stock number shows up in the random shop:
1688686713574.png
It says it allows switching between the two, but I can do that without any switch on.
No the switch was there to control other parts when using the random_shop command.
You do not manually turn it on (and you should not use 1 as the switch number)

That script is just to test if the scripts can work together.
You have sort of proven that by going into the shop from the script command and the event command.

Next will be to have it work how you want.
From what I can tell you want the random_shop to generate items and have them show up as if they were in the limited stock fashion.

This will take some work as the random_shop generates an array in an array with just numbers.
The stock script takes the items in the goods hash and generates the stock for each item. So merging the two script is not easy as they have completely different data.

I'll work on it from time to time and see if I can figure it out.
No the switch was there to control other parts when using the random_shop command.
You do not manually turn it on (and you should not use 1 as the switch number)

That script is just to test if the scripts can work together.
You have sort of proven that by going into the shop from the script command and the event command.

Next will be to have it work how you want.
From what I can tell you want the random_shop to generate items and have them show up as if they were in the limited stock fashion.

This will take some work as the random_shop generates an array in an array with just numbers.
The stock script takes the items in the goods hash and generates the stock for each item. So merging the two script is not easy as they have completely different data.

I'll work on it from time to time and see if I can figure it out.
Also, what is very important is to not generate the random items by blocks, my database is very well organized so it's really bad if it does that. IDK if it's possible to generate each shop line individually, but that's what I am hoping for.

Also, is there some way to make it so the shop doesn't generate with each interaction, like have a timer or something so players can't just sit there and farm for what items they want?
Also, what is very important is to not generate the random items by blocks, my database is very well organized so it's really bad if it does that. IDK if it's possible to generate each shop line individually, but that's what I am hoping for.

Also, is there some way to make it so the shop doesn't generate with each interaction, like have a timer or something so players can't just sit there and farm for what items they want?
Try this.

Remove the random shop script and my previous script
You just need Hime's scripts

Script removed because of bugs and updated in new post on next page.
Try this.

Remove the random shop script and my previous script
You just need Hime's scripts
Spoiler: Roninator2 - Hime Shop Manager - Random Shop addon
Ruby:
# ╔═════════════════════════════════════╦════════════════════╗
# ║ Title: Hime Random Shop + Stock     ║  Version: 1.00     ║
# ║ Author: Roninator2                  ║                    ║
# ╠═════════════════════════════════════╬════════════════════╣
# ║ Function:                           ║   Date Created     ║
# ║                                     ╠════════════════════╣
# ║ Random shop for Hime Shop Manager   ║    11 Jul 2023     ║
# ╚═════════════════════════════════════╩════════════════════╝
# ╔══════════════════════════════════════════════════════════╗
# ║ Instructions:                                            ║
# ║                                                          ║
# ║   Script order                                           ║
# ║     Hime Shop Manager                                    ║
# ║     Hime Shop Stock                                      ║
# ║     This Script                                          ║
# ║                                                          ║
# ║   Script Call                                            ║
# ║     random_shop(type, # of items, Quantity minimum,      ║
# ║         Quantity maximum, Shop ID)                       ║
# ║                                                          ║
# ║     type = 0 -> items                                    ║
# ║            1 -> weapons                                  ║
# ║            2 -> armours                                  ║
# ║            3 -> mix of all three                         ║
# ║                                                          ║
# ║     # of items is how many items to have in the shop     ║
# ║                                                          ║
# ║     Quantity is the minimum and maximum number available ║
# ║       for each item in the shop.                         ║
# ║                                                          ║
# ║     Shop ID is the number used to register with the shop ║
# ║       manager script.                                    ║
# ║                                                          ║
# ║     random_shop(3, 6, 1, 3, 45)                          ║
# ║                                                          ║
# ║   If the shop number 45 exists then it will pull up that ║
# ║     shop and not create a new one.                       ║
# ║                                                          ║
# ║   Tag items in database with <random shop item>          ║
# ║     then it will be included as an item that can be      ║
# ║     selected for the random shop.                        ║
# ║                                                          ║
# ╚══════════════════════════════════════════════════════════╝
# ╔══════════════════════════════════════════════════════════╗
# ║ Terms of use:                                            ║
# ║ Free for all uses in RPG Maker - Except Nudity           ║
# ╚══════════════════════════════════════════════════════════╝

#===============================================================================

module Random_Shop_Item_Tags
  def random_item
    if @random_shop_item.nil?
      if @note =~ /<random shop item>/i
        @random_shop_item = true
      else
        @random_shop_item = false
      end
    end
    @random_shop_item
  end
end

#===============================================================================
 
class RPG::Item
  include Random_Shop_Item_Tags
end
class RPG::Weapon
  include Random_Shop_Item_Tags
end
class RPG::Armor
  include Random_Shop_Item_Tags
end

#===============================================================================

class Game_Interpreter
  def random_shop(type, num, min, max, shop_id)
    goods = []
    loot = []
    list = []
    shop = ShopManager.get_shop(shop_id)
    if shop != nil
      Fiber.yield
    else
      #--- Determine Type
      if type == 0
        loot = $data_items
      elsif type == 1
        loot = $data_weapons
      elsif type == 2
        loot = $data_armors
      elsif type == 3
        loot = $data_items + $data_weapons + $data_armors
      end
      loot.each do |itm|
        next if itm.nil?
        if itm.random_item
          list.push(itm)
        end
      end
      return if list.empty?
      cnt = list.size
      clear
      i = 0
      num.times do
        n = rand(cnt)
        q = rand(max) + 1
        item = list[n]
        @shop_stock[i] = q
        itype = 0 if item.is_a?(RPG::Item)
        itype = 1 if item.is_a?(RPG::Weapon)
        itype = 2 if item.is_a?(RPG::Armor)
        good = make_good([itype, item.id, 0, item.price], i)
        goods.push(good)
        i += 1
      end
    end
    shop = ShopManager.setup_shop(shop_id, goods, true, :Shop)
    setup_shop(shop)
    ShopManager.call_scene(shop)
    Fiber.yield
  end
end

#===============================================================================
# END OF SCRIPT
#===============================================================================
Looks amazing so far! I tried a couple different random shops and they all have different things! Only thing I noticed is that they reset when the game is exited.
:VXA1:
Only thing I noticed is that they reset when the game is exited.
I understand you want to prevent people from spamming the shop, but making this a global variable is wasteful in my opinion. it will reset simple because it is a random shop. So if someone is able to save the game before going to the random shop and then just waste time trying to get something specific, I think that is worth their time.
As soon as someone goes to the shop and saves the game, then the shop is set and cannot change.
So unless you tell people it's a random shop or they figure it out after playing multiple times, I don't think it's something to be concerned with.
It would be just as valid to force the player to save the game when visiting the shop.
I understand you want to prevent people from spamming the shop, but making this a global variable is wasteful in my opinion. it will reset simple because it is a random shop. So if someone is able to save the game before going to the random shop and then just waste time trying to get something specific, I think that is worth their time.
As soon as someone goes to the shop and saves the game, then the shop is set and cannot change.
So unless you tell people it's a random shop or they figure it out after playing multiple times, I don't think it's something to be concerned with.
It would be just as valid to force the player to save the game when visiting the shop.
Ok, well that's good to know. I'm going to test this in my main project to see if it works like it should.
Also, is it possible to add a function to use the "family" and "rarity" settings from Galv's Random Loot? That'll allow me to fine tune the settings, like if I want a doctor to only have random medical items.
Also, is it possible to add a function to use the "family" and "rarity" settings from Galv's Random Loot?
I added those two functions, but I'm not using Galv's code specifically. It can be put in but I wasn't sure if you wanted it exactly (and I didn't test it). I'm not 100% sure how the rarity is supposed to work.
I did use the family code, but not the rarity code.
I don't need the rarity per say, so you can remove it if you want. I tested the Family function and it works great.
Is there a script call I can do that'll reset shops globally if I want, like after a set period of time so I can simulate newly received trade goods? I did some research and couldn't find anything conclusive.
I don't need the rarity per say, so you can remove it if you want. I tested the Family function and it works great.
Is there a script call I can do that'll reset shops globally if I want, like after a set period of time so I can simulate newly received trade goods? I did some research and couldn't find anything conclusive.
Rarity removed and reset added, also fixed a bug.
Also note that the family is optional you don't need to specify one if you don't want to.
Random removed!? Lovecraft's fan never asked you to do that. It defeats the script's purpose. I hope you just got confused and made a typo of sorts.
Random removed!? Lovecraft's fan never asked you to do that. It defeats the script's purpose. I hope you just got confused and made a typo of sorts.
typo I meant rarity.
Also who is LoveCraft's Fan
typo I meant rarity.
Also who is LoveCraft's Fan
Lol they mean me, since my name's cthulhusquid and Cthulhu is a squid god from H.P. Lovecraft's stories. Anyway I'll check out the updated script later today.

Original thread: https://forums.rpgmakerweb.com/threads/fallout-elder-scrolls-style-random-vendor-inventory.156837/