Vendily

DPPt Honey Trees

Aug 9th, 2020 (edited)
2,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.74 KB | None | 0 0
  1. #===============================================================================
  2. # DPPt Honey Trees - By Vendily [v17/v18]
  3. #===============================================================================
  4. # This script adds in Honey Trees from DPPt, and all the bells and whistles
  5. #  as described on Bulbapedia. That means Tree Shakes, The Group A/B/C, Munchlax
  6. #  trees, all that jazz.
  7. #===============================================================================
  8. # To use it, you must create an event named "HoneyTree(X)", where X is the
  9. #  tree's id (zero indexed). More than one event can have the same id, they
  10. #  will act like the  same tree in that case. The script will find these events
  11. #  when you interact with them, much like how boulders ask to use strength.
  12. # Place these events at the base of your honey tree. They can only be interacted
  13. #  from the the bottom (facing up). Remove the "next if $game_player.direction!=8"
  14. #  to disable that behavior.
  15. # You can technically put event commands in the Honey Tree event, but they
  16. #  activate after the honey tree asks to slather honey.
  17. # The species is fixed as soon as the honey is applied, much like DPPt.
  18. #  Soft resets will only change the generated properties like level and nature.
  19. # There are 4 Munchlax trees, calculated using the HONEYTREE_MAX_TREES and the
  20. #  Trainer ID. If HONEYTREE_MAX_TREES is less than 4, the Munchlax trees will
  21. #  stack up. I don't recommend fewer than 5 trees, but it won't break.
  22. #===============================================================================
  23. # * The Total number of trees in the game. Try not to change it after release,
  24. #    doing so will cause Munchlax trees to be recalculated and probably change.
  25. #    Ideally should be more than 4.
  26. # * Time before the tree will show that there is an encounter in the tree.
  27. #    (Default 6 hours)
  28. # * Time before the honey fades away and the encounter is lost. (Default 24 hours)
  29. # * The animation ids used to show a pokemon in the tree.
  30. # * The ratios used to calculate the number of shakes for a given group.
  31. # * The encounter table. Groups A and B must have the same number of entries as
  32. #    HONEYTREE_ENCOUNTER_RATIOS, but Group C is a 1:1 ratio and can have any
  33. #    length
  34. # * The ratios used to generate a species in Groups A and B.
  35. #    Think Encounter Densities, that's this.
  36. #===============================================================================
  37. begin
  38. PluginManager.register({
  39.   :name    => "DPPt Honey Trees",
  40.   :version => "1.0",
  41.   :link    => "https://reliccastle.com/resources/422/",
  42.   :credits => "Vendily"
  43. })
  44. rescue
  45.   # not v18
  46. end
  47. HONEYTREE_MAX_TREES = 5
  48. HONEYTREE_TIME_ENCOUNTER = 6*60*60 # 6 hours
  49. HONEYTREE_TIME_FADE = 24*60*60 # 24 hours
  50. HONEYTREE_TREESHAKE_ANIMS=[
  51.   RUSTLE_NORMAL_ANIMATION_ID, # 0 shakes, but there is a mon
  52.   RUSTLE_VIGOROUS_ANIMATION_ID, # 1 shake
  53.   RUSTLE_SHINY_ANIMATION_ID, # 2 shakes
  54.   PLANT_SPARKLE_ANIMATION_ID # 3 shakes
  55.   ]
  56. HONEYTREE_TREESHAKE_RATIOS=[
  57.   [20,59,20,1],
  58.   [1,20,75,4],
  59.   [1,1,5,93]
  60. ]
  61. HONEYTREE_ENCOUNTER_GROUPS=[
  62.   [:COMBEE,:WURMPLE,:BURMY,:CHERUBI,:AIPOM,:AIPOM], # Group A
  63.   [:BURMY,:CHERUBI,:COMBEE,:AIPOM,:AIPOM,:HERACROSS], # Group B
  64.   [:MUNCHLAX] # Group C
  65. ]
  66. HONEYTREE_ENCOUNTER_RATIOS=[40,20,20,10,5,5]
  67.  
  68. class PokemonGlobalMetadata
  69.   attr_accessor :honeytrees
  70.   attr_accessor :lasthoneytree
  71.  
  72.   def honeytrees
  73.     @honeytrees=[] if !@honeytrees
  74.     return @honeytrees
  75.   end
  76. end
  77.  
  78. Events.onAction+=proc{|sender,e|
  79.    next if $game_player.direction!=8
  80.    facingEvent = $game_player.pbFacingEvent
  81.    if facingEvent && facingEvent.name[/HoneyTree\((\d+)\)/]
  82.      pbHoneyTree($~[1].to_i)
  83.    end
  84. }
  85.  
  86.  
  87. def pbHoneyTree(tree)
  88.   raise "Invalid tree ID #{tree}" if tree>=HONEYTREE_MAX_TREES
  89.  
  90.   treedata=$PokemonGlobal.honeytrees[tree]
  91.   now=pbGetTimeNow
  92.   if !treedata || now-treedata[2]>HONEYTREE_TIME_FADE
  93.     Kernel.pbMessage(_INTL("There is a sweet scent in the air...\\1"))
  94.   elsif now-treedata[2]<HONEYTREE_TIME_ENCOUNTER
  95.     Kernel.pbMessage(_INTL("The bark is slathered with Honey...\\1"))
  96.   elsif treedata[0]>0 # in window, active tree
  97.     Kernel.pbMessage(_INTL("Oh! A Pokémon is in the tree!"))
  98.     pbWildBattle(treedata[0],rand(11)+5)
  99.     $PokemonGlobal.honeytrees[tree]=nil
  100.     return
  101.   else
  102.     Kernel.pbMessage(_INTL("The bark is slathered with Honey...\\1"))
  103.   end
  104.   if $PokemonBag.pbHasItem?(:HONEY)&& Kernel.pbConfirmMessage(_INTL("Slather the bark with Honey?"))
  105.     $PokemonBag.pbDeleteItem(:HONEY,1)
  106.     lasttree=$PokemonGlobal.lasthoneytree
  107.     group=nil
  108.     if lasttree && lasttree[0]==tree && rand(10)>0 # 90% chance to repick
  109.       group=lasttree[1]
  110.       group=0 if group==3
  111.     end
  112.     if !group
  113.       if honey_tree_munchlax.include?(tree)
  114.         ratio=[20,70,1,9]
  115.       else
  116.         ratio=[70,20,0,10]
  117.       end
  118.       total=ratio[0]+ratio[1]+ratio[2]+ratio[3]
  119.       num=rand(total)
  120.       cumtotal=0
  121.       for i in 0...4
  122.         cumtotal+=ratio[i]
  123.         if num<cumtotal
  124.           group=i
  125.           break
  126.         end
  127.       end
  128.     end
  129.     now=pbGetTimeNow
  130.     if group<3 # if pokemon, generate species
  131.       table=HONEYTREE_ENCOUNTER_GROUPS[group]
  132.       if group==2 # munchlax
  133.         species=getID(table[rand(table.length)])
  134.       else
  135.         ratio=HONEYTREE_ENCOUNTER_RATIOS
  136.         total=0
  137.         ratio.each{|r| total+=r}
  138.         num=rand(total)
  139.         cumtotal=0
  140.         for i in 0...4
  141.           cumtotal+=ratio[i]
  142.           if num<cumtotal
  143.             species=table[i]
  144.             break
  145.           end
  146.         end
  147.       end
  148.       ratio=HONEYTREE_TREESHAKE_RATIOS[group]
  149.       total=0
  150.       ratio.each{|r| total+=r}
  151.       num=rand(total)
  152.       cumtotal=0
  153.       for i in 0...4
  154.         cumtotal+=ratio[i]
  155.         if num<cumtotal
  156.           shakes=i
  157.           break
  158.         end
  159.       end
  160.       mon=[getID(PBSpecies,species),group,now,shakes]
  161.     else
  162.       mon=[0,3,now]
  163.     end
  164.     $PokemonGlobal.lasthoneytree=[tree,group]
  165.     $PokemonGlobal.honeytrees[tree]=mon
  166.     Kernel.pbMessage(_INTL("The bark was slathered with Honey."))
  167.   end
  168. end
  169.  
  170. def honey_tree_munchlax
  171.   id=$Trainer.id
  172.   sid1,sid2,tid1,tid2=id&(0xFF<<24),id&(0xFF<<16),id&(0xFF<<8),id&(0xFF)
  173.   a=sid1 % HONEYTREE_MAX_TREES
  174.   b=sid2 % HONEYTREE_MAX_TREES
  175.   c=tid1 % HONEYTREE_MAX_TREES
  176.   d=tid2 % HONEYTREE_MAX_TREES
  177.   if a==b
  178.     b+=1
  179.     b%=HONEYTREE_MAX_TREES
  180.   end
  181.   if a==c
  182.     c+=1
  183.     c%=HONEYTREE_MAX_TREES
  184.   elsif b==c
  185.     c+=1
  186.     c%=HONEYTREE_MAX_TREES
  187.   end
  188.   if a==d
  189.     d+=1
  190.     d%=HONEYTREE_MAX_TREES
  191.   elsif b==d
  192.     d+=1
  193.     d%=HONEYTREE_MAX_TREES
  194.   elsif c==d
  195.     d+=1
  196.     d%=HONEYTREE_MAX_TREES
  197.   end
  198.   return [a,b,c,d]
  199. end
  200.  
  201. class HoneyTreeSprite
  202.   def initialize(event,map,viewport)
  203.     @event=event
  204.     @map=map
  205.     @disposed=false
  206.     @frames=0
  207.     @frameskip=25+rand(10)+rand(10)
  208.     event.name[/HoneyTree\((\d+)\)/]
  209.     @treeid=$~[1].to_i
  210.   end
  211.  
  212.   def dispose
  213.     @event=nil
  214.     @map=nil
  215.     @disposed=true
  216.   end
  217.  
  218.   def disposed?
  219.     @disposed
  220.   end
  221.  
  222.   def update
  223.     @frames+=1
  224.     if @frames>=@frameskip
  225.       @frames=0
  226.       treedata=$PokemonGlobal.honeytrees[@treeid]
  227.       now=pbGetTimeNow
  228.       return if !treedata
  229.       return if now-treedata[2]<HONEYTREE_TIME_ENCOUNTER
  230.       return if now-treedata[2]>HONEYTREE_TIME_FADE
  231.       return if treedata[0] && treedata[0]<=0
  232.       return if !$scene.spriteset
  233.       $scene.spriteset.addUserAnimation(HONEYTREE_TREESHAKE_ANIMS[treedata[3]],@event.x,
  234.               @event.y-1,true)
  235.     end
  236.   end
  237. end
  238.  
  239. Events.onSpritesetCreate+=proc{|sender,e|
  240.    spriteset=e[0]
  241.    viewport=e[1]
  242.    map=spriteset.map
  243.    for i in map.events.keys
  244.      if map.events[i].name[/HoneyTree\((\d+)\)/]
  245.        spriteset.addUserSprite(HoneyTreeSprite.new(map.events[i],map,viewport))
  246.      end
  247.    end
  248. }
Add Comment
Please, Sign In to add comment