blucalm

Plow Land Ra Gardening 3813

Mar 9th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.53 KB | None | 0 0
  1. #~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
  2. # ** Plow Land Add-on for Ra Garding/Farming
  3. #~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
  4. # Author: Eshra
  5. # Release Date: 8 March 2013
  6. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. #   Setup:
  8. #   Place this script underneath the Ra Gardening/Farming Script.
  9. #
  10. #   This addon requires you to have a spritesheet named "$plowedland" in the
  11. #   character folder of your project.
  12. #
  13. #   Usage:
  14. #
  15. #   Items can now be used to plow land. Use the notetag:
  16. #     <seed plow 1,2,3>
  17. #   to specify an item which can "plow" map tiles with a terrain tag of 1,2,
  18. #   and 3 (the numbers can be replaced with others or you can only use one #, etc.)
  19. #
  20. #   Seeds can now be specified such that they can only be planted on plowed
  21. #   terrain. Use the notetag:
  22. #     <plowed land only>
  23. #   to specify a seed which can only be planted on land that has been plowed.
  24. #
  25. #   See the demo at rascripts.wordpress.com for more examples.
  26. #~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
  27.  
  28. ($imported ||= {})["Ra Gardening Plow Addon"] = 0.1
  29. raise "Plow addon reqs Ra Gardening" if ($imported["Ra Gardening"]||=-1) < 0.1
  30. raise "Plow addon reqs Era" if ($imported["Era Module"]||=-1) < 0.1
  31.  
  32. # Add new 'plow' related constants to GardenRa
  33. module GardenRa
  34.   PlowName = '$plowedland'
  35.   PlowIndex = 0
  36.   PlowRow = 0
  37.   ShowPlowedLand = true
  38.   PlowSym = :ara_plow_addon_hls
  39.  
  40.   CE_Plow_Land = Proc.new{ |id, is_item|
  41.     item = is_item ? $data_items[id] : $data_skills[id]
  42.     front = Input.press?(GardenRa::Front)
  43.     xy = $game_temp.garden_seed_temp_xy_helper(id, front)
  44.     $game_map.plow_land_era(1,[xy])
  45.   }
  46.   class << self
  47.     alias plant_seed_plow_addon_era plant_seed
  48.   end
  49.   #----------------------------------------------------------------------------
  50.   # * Alias plant_seed, remove the plow data after planting
  51.   #----------------------------------------------------------------------------
  52.   def self.plant_seed(*args)
  53.     plant_seed_plow_addon_era(*args)
  54.    
  55.     sys = $game_system
  56.     opts = (args[3] ||= {})
  57.     options = {:x => $game_player.x, :y => $game_player.y}.merge(opts)
  58.     x,y = options[:x], options[:y]
  59.    
  60.     # remove plow highlight and sys data
  61.     spm = SceneManager.scene.instance_eval("@spriteset")
  62.     spm.remove_highlight(x, y, GardenRa::PlowSym)
  63.     sys.remove_saved_highlight(x,y, nil, GardenRa::PlowSym)
  64.     spm.refresh_highlights
  65.     sys.unplow_era(x,y)
  66.   end # plant_seed
  67.  
  68.   module RE
  69.     # Terrain tags an item can plow
  70.     PlowTerrain = /<\s*seed\s*plow\s*(\d+\s*(?:,\s*\d+\s*)*)\s*>/i
  71.    
  72.     # Seed plow requirements
  73.     SeedPlowReqs = /<plowed land only>/i
  74.   end
  75. end
  76.  
  77. module DataManager
  78.   #----------------------------------------------------------------------------
  79.   # * Loading Notetags using Ra DB notetag methods
  80.   #----------------------------------------------------------------------------
  81.   class << self; alias eval_note_era_mar82013_plow eval_note_era; end
  82.   def self.eval_note_era(data, line)
  83.     eval_note_era_mar82013_plow(data,line)
  84.     case line
  85.     when GardenRa::RE::PlowTerrain
  86.       data.garden_item_default_values # only sets once unless param is true
  87.       data.plow_era= true
  88.       data.set_plow_terrains($1)
  89.     when GardenRa::RE::SeedPlowReqs
  90.       data.garden_item_default_values # only sets once unless param is true
  91.       data.requires_plow = true
  92.     end
  93.   end
  94. end
  95.  
  96. class RPG::UsableItem < RPG::BaseItem
  97.   attr_reader :plow_era
  98.   attr_accessor :plowable_terrains # hash of terrain tags this item can plow
  99.   attr_reader :requires_plow
  100.  
  101.   alias garden_item_default_values_mar82013558_era garden_item_default_values
  102.   def garden_item_default_values
  103.     garden_item_default_values_mar82013558_era
  104.     @plow_era = false
  105.     @plowable_terrains = {}
  106.     @fertillizer_terrain = {}
  107.     @requires_plow = false
  108.   end
  109.  
  110.   def plow_era=(v); @plow_era = v; end
  111.   def is_plow?; @plow_era == true; end # compare to true for encapsulation
  112.    
  113.   def requires_plow=(v); @requires_plow = v; end
  114.    
  115.   # Alias garden_terrains
  116.   alias garden_terrains_era_plow_addon garden_terrains
  117.   def garden_terrains(tag)
  118.     return true if @plow_era && plowable_terrains[tag]
  119.     garden_terrains_era_plow_addon(tag)
  120.   end
  121.  
  122.   def set_plow_terrains(tags)
  123.     split_tags_era(tags, @plowable_terrains={})
  124.   end
  125.  
  126.   alias garden_item_default_values_plow_addon garden_item_default_values
  127.   def garden_item_default_values(force = false)
  128.     return if @set_default_values && !force
  129.     garden_item_default_values_plow_addon
  130.     @set_default_values = true
  131.   end
  132. end # RPG::UsableItem
  133.  
  134.  
  135. # Data organization class for future use. Stores data about what type of
  136. # Gardening/Farming related events have happened at a certain location on
  137. # the map.
  138. class FarmMapDataXY
  139.  
  140.   attr_reader :x, :y, :plow_value
  141.  
  142.   IS_PLOWED = 1       # location has been plowed if plow_value > this const
  143.   INIT_PLOW_VALUE = 0 # initial plow value upon obj initialization
  144.   UNPLOW = 0
  145.   def initialize(x,y)
  146.     @x,@y = x,y
  147.     @plow_value = INIT_PLOW_VALUE
  148.   end
  149.  
  150.   def x=(v); @x = v; end
  151.   def y=(v); @y = v; end
  152.   def plow_value=(v); @plow_value = v; end
  153.   def mod_plow_val(v); @plow_value ||= 0; @plow_value += v; end  
  154.   def hash; hash_string_xy(@x, @y).hash; end
  155.  
  156.   def plowed?; @plow_value >= IS_PLOWED; end
  157.  
  158.   def unplow; @plow_value = UNPLOW; end
  159.    
  160.   # Converts an x,y coordinate to the hash value of the corresponding
  161.   #   FarmMapDataXY obj.
  162.   def self.hash_string_xy(x,y); "#{x},#{y}"; end
  163.  
  164.   def to_s
  165.     "FarmMapDataXY: (#{x},#{y}) plow val: #{plow_value} plowed: #{plowed?}"
  166.   end
  167. end
  168.  
  169. # New methods to access plowed terrain data
  170. class Game_System
  171.   # hash of FarmMapDataXY objects
  172.   def farm_data_xys
  173.     (@farm_data_xys ||= {})[$game_map.map_id] ||= {}
  174.   end
  175.  
  176.   def pos_plowed?(x,y)
  177.     data = farm_data_xys[FarmMapDataXY.hash_string_xy(x,y)]
  178.     data ? data.plowed? : false
  179.   end
  180.  
  181.   def unplow_era(x,y)
  182.     data = farm_data_xys[FarmMapDataXY.hash_string_xy(x,y)]
  183.     data.unplow if data
  184.   end
  185. end
  186.  
  187. class Game_Map
  188.   # param range is a list of [x,y] arrays, times is how many times the locations
  189.   #   have been plowed.
  190.   def plow_land_era(times, range)
  191.    
  192.     plow_data = $game_system.farm_data_xys
  193.    
  194.     range.each do |xy|
  195.       x,y = xy[0], xy[1]
  196.       hashstr = FarmMapDataXY.hash_string_xy(x,y)
  197.       md_obj = plow_data[hashstr]
  198.      
  199.       # case no data for the location yet
  200.       if !md_obj
  201.         md_obj = FarmMapDataXY.new(x,y)
  202.         plow_data[hashstr] = md_obj
  203.       end
  204.      
  205.       md_obj.mod_plow_val(times)
  206.      
  207.       sh = nil
  208.       if GardenRa::ShowPlowedLand
  209.         sh = Sheet_Data.new(GardenRa::PlowName,GardenRa::PlowIndex,GardenRa::PlowRow)
  210.       end
  211.    
  212.       # add plowed land graphic
  213.       spm = SceneManager.scene.instance_eval("@spriteset")
  214.       spm.add_highlight(:x => x, :y => y, :sh => sh, :retain => true,
  215.         :opacity => 255, :sym => GardenRa::PlowSym)
  216.     end
  217.   end
  218. end
  219.  
  220. # new methods for handling when an item that can plow land has been used.
  221. class Game_BattlerBase
  222.   alias usable_gdra_plow_addon_era usable?
  223.   def usable?(item)
  224.     return garden_area_valid?(item) if item && item.is_plow?
  225.     usable_gdra_plow_addon_era(item)
  226.   end
  227.  
  228.   # Alias garden_area_valid?, add checks for seeds requiring plowed land
  229.   alias garden_area_valid_plow_addon_era? garden_area_valid?
  230.   def garden_area_valid?(item)
  231.     garden_area_valid_plow_addon_era?(item) && otrogd_checks_plow(item)
  232.   end # garden_area_valid?
  233.  
  234.   # assumes data is stored in $game_temp.temp_seed_data
  235.   def otrogd_checks_plow(item)
  236.     sys = $game_system
  237.     hash = $game_temp.temp_seed_data[item.id]
  238.    
  239.     if item.is_garden_seed? && item.requires_plow
  240.       # check to make stored locations are plowed if the seed requires plowed
  241.       #   terrain to be planted.
  242.      
  243.       sx,sy = hash[:sx], hash[:sy]
  244.       px,py = hash[:px], hash[:py]
  245.      
  246.       (hash[:px],hash[:py] = nil, nil) if !sys.pos_plowed?(px,py)
  247.       (hash[:sx],hash[:sy] = nil, nil) if !sys.pos_plowed?(sx,sy)
  248.     end
  249.    
  250.     hash && ((hash[:px] && hash[:py]) || (hash[:sx] && hash[:sy]))
  251.   end
  252. end
  253.  
  254. class Game_Battler < Game_BattlerBase
  255.   alias usitm_addon_mar82013_era use_item
  256.   def use_item(item)
  257.     if item.is_plow?
  258.       strsym = "GardenRa::CE_Plow_Land"
  259.       $game_temp.reserve_internal_ce(strsym.to_sym, item.id, item.is_a?(RPG::Item))
  260.     end
  261.     usitm_addon_mar82013_era(item)
  262.   end
  263. end
Advertisement
Add Comment
Please, Sign In to add comment