Advertisement
Vendily

Bank of Mom [v20]

Jan 15th, 2023
1,806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.86 KB | None | 0 0
  1. #===============================================================================
  2. # Bank of Mom - By Vendily [v20]
  3. #===============================================================================
  4. # This script adds in the Bank of Mom Feature from GSC or HGSS, where
  5. #  the player's mother will save money and occasionally buy items.
  6. # The mom attempts to buy an item after the end of every battle, if she can
  7. #  afford one of the MOM_FIXED_ITEMS or MOM_RANDOM_ITEMS.
  8. # If she buys an item, she will call the player after the battle ends.
  9. #===============================================================================
  10. # Several additional, required edits in `def pbMessageDisplay` to add
  11. #  the `mgoldwindow` variable, to show how much money the Bank of Mom holds.
  12. #  1. Under `goldwindow = nil`, put `mgoldwindow = nil`
  13. #  2. In the long `while text` regex, add `mg|` before `g|`
  14. #  3. Under:
  15. #      when "g"      # Display gold window
  16. #        goldwindow&.dispose
  17. #        goldwindow = pbDisplayGoldWindow(msgwindow)
  18. #     Put:
  19. #       when "mg"    # Display mom gold window
  20. #         mgoldwindow&.dispose
  21. #         mgoldwindow = pbDisplayMomGoldWindow(msgwindow, goldwindow)
  22. #  4. Under `goldwindow&.dispose`, put `mgoldwindow&.dispose`
  23. # These edits may have to be made to a plugin if one overrides this method.
  24. #===============================================================================
  25. # Some neat details.
  26. #  - The mom can save 1/4, 1/2 or all the money the player wins in battle.
  27. #     This is set by the player. This does not affect earnings from Pay Day.
  28. #  - pbMomManageBank allows the player to withdraw, deposit, or change the
  29. #     amount saved, or cancel saving altogether.
  30. #  - The mom still spends money even if the player is not saving anymore.
  31. #  - The mom defaults to not saving any money.
  32. #===============================================================================
  33. module Settings
  34.   MAX_MOM_MONEY      = 999_999
  35.   # The fixed items that appear from the mom.
  36.   # Triggered when the player exceeds a certain bank balance.
  37.   # Earned in the order defined.
  38.   # Trigger, Cost, Item ID, Quantity (Defaults to 1)
  39.   # The Cost is NOT multiplied by the quantity.
  40.   # Technically if the Item ID is a number,
  41.   #  you can make an exception in the code for it (like decorations)
  42.   MOM_FIXED_ITEMS    = [
  43.                         [   900,  600, :SUPERPOTION],
  44.                         [  4000,  270, :REPEL],
  45.                         [  7000,  600, :SUPERPOTION],
  46.                         [ 10000, 1800, :SILKSCARF],
  47.                         [ 15000, 3000, :MOONSTONE],
  48.                         [ 19000,  600, :SUPERPOTION],
  49.                         [ 30000, 4800, :CHOICESCARF],
  50.                         [ 40000,  900, :HYPERPOTION],
  51.                         [ 50000, 8000, :MUSCLEBAND],
  52.                         [100000,10000, :FOCUSSASH]
  53.                        ]
  54.   # On a balance with a multiple of this number,
  55.   #  a random MOM_RANDOM_ITEMS item will be bought
  56.   #  provided a MOM_FIXED_ITEMS item is not available.
  57.   # Cost, Item ID, Quantity (Defaults to 1)
  58.   # The Cost is NOT multiplied by the quantity.
  59.   MOM_RANDOM_TRIGGER = 2300
  60.   MOM_RANDOM_ITEMS   = [
  61.                         [600, :SUPERPOTION],
  62.                         [ 90, :ANTIDOTE],
  63.                         [180, :POKEBALL],
  64.                         [450, :ESCAPEROPE],
  65.                         [500, :GREATBALL]
  66.                        ]
  67. end
  68.  
  69. class PokemonGlobalMetadata
  70.   attr_writer :mom_saving
  71.   attr_writer :mom_item_index
  72.   def mom_saving; return (@mom_saving || 0); end
  73.   def mom_item_index; return (@mom_item_index || 0); end
  74.   def mom_money; return (@mom_money || 0); end
  75.   def mom_money=(value)
  76.     validate value => Integer
  77.     @mom_money = value.clamp(0, Settings::MAX_MOM_MONEY)
  78.   end
  79. end
  80.  
  81. class Battle
  82.   def pbGainMoney
  83.     return if !@internalBattle || !@moneyGain
  84.     # Money rewarded from opposing trainers
  85.     if trainerBattle?
  86.       tMoney = 0
  87.       @opponent.each_with_index do |t, i|
  88.         tMoney += pbMaxLevelInTeam(1, i) * t.base_money
  89.       end
  90.       tMoney *= 2 if @field.effects[PBEffects::AmuletCoin]
  91.       tMoney *= 2 if @field.effects[PBEffects::HappyHour]
  92.       oldMoney = pbPlayer.money
  93.       mMoney = 0
  94.       oldmMoney = $PokemonGlobal.mom_money
  95.       if $PokemonGlobal.mom_saving>0
  96.         amount = [4,2,1][$PokemonGlobal.mom_saving-1]
  97.         mMoney = (tMoney/amount)
  98.         tMoney = tMoney-mMoney
  99.       end
  100.       pbPlayer.money += tMoney
  101.       $PokemonGlobal.mom_money += mMoney
  102.       moneyGained = pbPlayer.money - oldMoney
  103.       moneyGained +=  $PokemonGlobal.mom_money - oldmMoney
  104.       if moneyGained > 0
  105.         $stats.battle_money_gained += moneyGained
  106.         pbDisplayPaused(_INTL("You got ${1} for winning!", moneyGained.to_s_formatted))
  107.         if $PokemonGlobal.mom_saving>0
  108.           text = [_INTL("Sent some to Mom!"),
  109.                   _INTL("Sent half to Mom!"),
  110.                   _INTL("Sent all to Mom!")][$PokemonGlobal.mom_saving-1]
  111.           pbDisplayPaused(text)
  112.         end
  113.       end
  114.     end
  115.     # Pick up money scattered by Pay Day
  116.     if @field.effects[PBEffects::PayDay] > 0
  117.       @field.effects[PBEffects::PayDay] *= 2 if @field.effects[PBEffects::AmuletCoin]
  118.       @field.effects[PBEffects::PayDay] *= 2 if @field.effects[PBEffects::HappyHour]
  119.       oldMoney = pbPlayer.money
  120.       pbPlayer.money += @field.effects[PBEffects::PayDay]
  121.       moneyGained = pbPlayer.money - oldMoney
  122.       if moneyGained > 0
  123.         $stats.battle_money_gained += moneyGained
  124.         pbDisplayPaused(_INTL("You picked up ${1}!", moneyGained.to_s_formatted))
  125.       end
  126.     end
  127.   end
  128. end
  129.  
  130. # type 0 == regular item. More for convience's sake
  131. def pbMomBoughtMoneyCall(type=0)
  132.   pbMessage(_INTL("......\\wt[5] ......"))
  133.   pbMessage(_INTL("Hi, {1}! How are you?\\1",$player.name))
  134.   case type
  135.   when 0
  136.     pbMessage(_INTL("I found a useful item shopping, so\\1"))
  137.   end
  138.   pbMessage(_INTL("I bought it with your money. Sorry!\\1"))
  139.   case type
  140.   when 0
  141.     pbMessage(_INTL("It's in your PC. You'll like it!"))
  142.   end
  143.   pbMessage(_INTL("Click!\\wt[10]\\n......\\wt[5] ......"))
  144. end
  145.  
  146. def pbMomManageBank
  147.   commands = [_INTL("Withdraw"), _INTL("Deposit"), _INTL("Change"), _INTL("Cancel")]
  148.   cmd = 0
  149.   loop do
  150.     cmd = pbMessage(_INTL("\\G\\MGWhat do you want to do?"),commands,-1,nil,cmd)
  151.     case cmd
  152.     when 0 # Withdraw
  153.       params = ChooseNumberParams.new
  154.       params.setRange(0, $PokemonGlobal.mom_money)
  155.       params.setDefaultValue(0)
  156.       newval = pbMessageChooseNumber(
  157.         _INTL("\\G\\MGHow much do you want to take?"), params
  158.       )
  159.       if newval==0
  160.         next
  161.       elsif (newval+$player.money)>Settings::MAX_MONEY
  162.         pbMessage(_INTL("\\G\\MGYou can't take that much.\\1"))
  163.       else
  164.         pbSEPlay("Mart buy item")
  165.         $player.money+=newval
  166.         $PokemonGlobal.mom_money-=newval
  167.         pbMessage(_INTL("\\G\\MG{1}, don't give up!\\1"),$player.name)
  168.       end
  169.     when 1 # Deposit
  170.       params = ChooseNumberParams.new
  171.       params.setRange(0, $player.money)
  172.       params.setDefaultValue(0)
  173.       newval = pbMessageChooseNumber(
  174.         _INTL("\\G\\MGHow much do you want to save?"), params
  175.       )
  176.       if newval==0
  177.         next
  178.       elsif (newval+$PokemonGlobal.mom_money)>Settings::MAX_MOM_MONEY
  179.         pbMessage(_INTL("\\G\\MGYou can't save that much.\\1"))
  180.       else
  181.         pbSEPlay("Mart buy item")
  182.         $player.money-=newval
  183.         $PokemonGlobal.mom_money+=newval
  184.         pbMessage(_INTL("\\G\\MGYour money's safe here!\\1"))
  185.       end
  186.     when 2 # Start/Stop Saving
  187.       save_commands = [_INTL("Stop Saving"),_INTL("Save Some"),_INTL("Save Half"),_INTL("Save All")]
  188.       save_texts = [_INTL("\\G\\MGI'm not saving any money.\\nDo you want to save some money?"),
  189.                    _INTL("\\G\\MGI'm saving some of your money.\\nDo you want to save some money?"),
  190.                    _INTL("\\G\\MGI'm saving half of your money.\\nDo you want to save some money?"),
  191.                    _INTL("\\G\\MGI'm saving all of your money.\\nDo you want to save some money?")]
  192.       save_cmd = $PokemonGlobal.mom_saving
  193.       save_cmd = pbMessage(save_texts[save_cmd],save_commands,save_cmd+1,nil,save_cmd)
  194.       $PokemonGlobal.mom_saving = save_cmd
  195.       if $PokemonGlobal.mom_saving>0
  196.         pbMessage(_INTL("\\G\\MGOK, I'll save your money. Trust me!\\1"))
  197.       else
  198.         pbMessage(_INTL("\\G\\MGJust do what you can.\\1"))
  199.       end
  200.     else # Cancel
  201.       break
  202.     end
  203.   end
  204. end
  205.  
  206. def pbGetMomGoldString
  207.   return _INTL("${1}", $PokemonGlobal.mom_money.to_s_formatted)
  208. end
  209.  
  210. def pbDisplayMomGoldWindow(msgwindow, goldwindow)
  211.   moneyString = pbGetMomGoldString
  212.   mgoldwindow = Window_AdvancedTextPokemon.new(_INTL("Bank:\n<ar>{1}</ar>", moneyString))
  213.   mgoldwindow.setSkin("Graphics/Windowskins/goldskin")
  214.   mgoldwindow.resizeToFit(mgoldwindow.text, Graphics.width)
  215.   mgoldwindow.width = 160 if mgoldwindow.width <= 160
  216.   if msgwindow.y == 0
  217.     mgoldwindow.y = (goldwindow) ? goldwindow.y - mgoldwindow.height : Graphics.height - mgoldwindow.height
  218.   else
  219.     mgoldwindow.y = (goldwindow) ? mgoldwindow.height : 0
  220.   end
  221.   mgoldwindow.viewport = msgwindow.viewport
  222.   mgoldwindow.z = msgwindow.z
  223.   return mgoldwindow
  224. end
  225.  
  226. EventHandlers.add(:on_end_battle, :mom_buy_item,
  227.   proc { |decision, canLose|
  228.     next if decision == 2 || decision == 5 # No item if you lost.
  229.     # next if bad phone service goes here.
  230.     if !$PokemonGlobal.pcItemStorage
  231.       $PokemonGlobal.pcItemStorage = PCItemStorage.new
  232.     end
  233.     bought_something = -1
  234.     if $PokemonGlobal.mom_item_index<Settings::MOM_FIXED_ITEMS.length
  235.       current_item = Settings::MOM_FIXED_ITEMS[$PokemonGlobal.mom_item_index]
  236.       if $PokemonGlobal.mom_money >= current_item[0] # has trigger money
  237.         if $PokemonGlobal.mom_money >= current_item[1]
  238.           # Exceptions would go here.
  239.           qty = current_item[3] || 1
  240.           if $PokemonGlobal.pcItemStorage.can_add?(current_item[2],qty)
  241.             bought_something = 0
  242.             $PokemonGlobal.pcItemStorage.add(current_item[2],qty)
  243.             $PokemonGlobal.mom_money-=current_item[1]
  244.             $PokemonGlobal.mom_item_index+=1
  245.           end
  246.         end
  247.       end
  248.     end
  249.     # Try to buy a random item then
  250.     if bought_something<0
  251.       if ($PokemonGlobal.mom_money%Settings::MOM_RANDOM_TRIGGER) == 0
  252.         rand_item = Settings::MOM_RANDOM_ITEMS[rand(Settings::MOM_RANDOM_ITEMS.length)]
  253.         if $PokemonGlobal.mom_money >= current_item[0]
  254.           # Exceptions would go here.
  255.           qty = current_item[2] || 1
  256.           if $PokemonGlobal.pcItemStorage.can_add?(current_item[1],qty)
  257.             bought_something = 0
  258.             $PokemonGlobal.pcItemStorage.add(current_item[1],qty)
  259.             $PokemonGlobal.mom_money-=current_item[0]
  260.           end
  261.         end
  262.       end
  263.     end
  264.     pbMomBoughtMoneyCall(bought_something) if bought_something>=0
  265.   }
  266. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement