blucalm

Plow Terrain Ra Gardening 8 Marg 2013

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