#------------------------------------------------------------
# World Objects version 2.0 - by Prexus
#------------------------------------------------------------
# Instructions:
# When setting up World Objects, create an event on the map
# with the name 'World Setup'. It doesn't have to be parallel
# process or autostart, it just needs to be on the map.
# To create an object to be placed, you'll need to add some
# comments to the World Setup event.
# The comments look like this format:
#
# Comment: Object
# Comment: <density> <type_id> <node_level>
# Comment: <item type> ...
# Comment: <item id> ...
# Comment: <item chance> ...
# Comment: <node_graphic> <node_graphic_expired>
#
# Here is the breakdown:
# <density> : replace this with either DENSE, MEDIUM, or SPARSE.
# They determine the amount of nodes that will spawn
# on the map. The constants can be changed in the
# World Constants module below these instructions.
# <type_id> : This is the ID of the skill, which is generally the
# order you create them in. In the case of the demo,
# Herbalism is id 0, Mining is id 1, Fishing is id 2
# and Lockpicking is id 3.
# <node_level> : This is the level required to harvest the node,
# and corresponds with the type_id.
#
# <item_type> : replace this with either ITEM, WEAPON, or ARMOR.
# It determines which database it'll pull the result
# from, depending on the ID given in <item_id>.
# The ... signifies that you can add multiple
# <item_type>, as long as there are an equal amount of
# <item_id> and <item_chance>.
# <item_id> : This is the ID of the item in the database.
# The ... signifies that you can add multiple
# <item_id>, as long as there are an equal amount of
# <item_type> and <item_chance>.
# <item_chance>: This number represents the chance that you'll find
# the item (out of 100, a percentage, so 50 = 50% chance)
# It checks the item chances in the order they are presented,
# ie if you input Comment: 70 90 100
# You have a 70 percent chance to find the first item, and
# --FAILING THAT-- you have a 90% chance to find the next
# item, and failing that you have a 100% chance to find the
# last item (this doesn't have to be 100, you can find nothing.)
# The ... signifies that you can add multiple
# <item_chance>, as long as there are an equal amount of
# <item_type> and <item_id>.
#
# <node_graphic> : This is the name of the graphic you want to use for the
# node -before- it is harvested. For example you might put
# 001-Fighter01.
# <node_graphic_expired> : This is the name of the graphic you want to use
# for the node -after- it is harvested.
#
# You can have subsequent Objects commented, such as:
# Comment: Object
# Comment: "blah blah"
# Comment: "blah blah etc."
# Comment: Object
# Comment: "blah blah blah blah" etc..
#
# This will result in two types of nodes appearing on the same map.
#
# To teach a skill to a player, use this command in a Call Script:
# anActor.train_skill("skill_name")
# for example:
# $game_party.actors[0].train_skill("Herbalism")
# You can also add the extra parameter to define the starting level of the skill:
# $game_party.actors[0].train_skill("Herbalism", 10)
# You can also use the .untrain_skill() method to forget a skill:
# $game_party.actors[0].untrain_skill("Herbalism")
#
# To setup skills, go in the script where it says @skills = [] and add after it:
# @skills.push(World::Skills.new('<name of skill>'[, <weapon_required>, <max_level>]))
# <name of skill> being the name of the skill in single quotes.
# <weapon_required> being the ID of an item that has to be equipped (such as an axe)
# <max_level> being the maximum level of the skill.
# the Weapon and the Max Level aren't required.
module World_Constants
DENSE = 0.05
MEDIUM = 0.03
SPARSE = 0.01
TYPES = ['Herbalism', 'Mining', 'Fishing', 'Lockpicking']
end
class World
attr_accessor :objects
attr_accessor :skills
#----------------------------------------------------------------------
def initialize
@objects = {}
@skills = []
@skills.push(World::Skills.new('Herbalism'))
@skills.push(World::Skills.new('Mining', 2))
@skills.push(World::Skills.new('Fishing', 3))
@skills.push(World::Skills.new('Lockpicking'))
end
#----------------------------------------------------------------------
def setup(events)
@objects = {}
for event in events.values
if event.name == 'World Setup'
list = event.list
next if list == nil
next unless list.is_a?(Array)
for item in list
next if item.code != 108
if item.parameters[0] == 'Object'
item1 = list[list.index(item) + 1]
item2 = list[list.index(item) + 2]
item3 = list[list.index(item) + 3]
item4 = list[list.index(item) + 4]
item5 = list[list.index(item) + 5]
next unless item1 and item2 and item3 and item4 and item5
add_objects(item1.parameters[0], item2.parameters[0],
item3.parameters[0], item4.parameters[0],
item5.parameters[0])
$game_map.update_last_event_id
end
end
end
end
end
# item1
# density, type, level
# item2
# result_types
# item3
# result_indices
# item4
# result_chances
# item5
# graphic_name1, graphic_name2
#----------------------------------------------------------------------
def add_objects(item1, item2, item3, item4, item5)
creation_info = item1.split
resulttype_info = item2.split
results_info = item3.split
resultchance_info = item4.split
graphic_info = item5.split
map_w = $game_map.width
map_h = $game_map.height
map_tiles = map_w * map_h
case creation_info[0]
when 'Dense'
num_objects = map_tiles * World_Constants::DENSE
when 'Medium'
num_objects = map_tiles * World_Constants::MEDIUM
when 'Sparse'
num_objects = map_tiles * World_Constants::SPARSE
else
num_objects = 0
end
for i in 0...num_objects
id = @objects.size + 1
@objects[id] = World::Object.new(id,
World_Constants::TYPES[creation_info[1].to_i], creation_info[2],
resulttype_info, results_info, resultchance_info,
graphic_info[0], graphic_info[1])
end
end
#----------------------------------------------------------------------
def check_gather
for object in @objects
#print "#{$game_map.events[object.event_id].name} #{$game_map.events[object.event_id].x}, #{$game_map.events[object.event_id].y}"
next unless check_range(object[1].event_id)
event = $game_map.events[object[1].event_id]
key = [event.map_id, event.id, 'A']
next if $game_self_switches[key]
object[1].gather($game_party.actors[0])
return
end
end
#----------------------------------------------------------------------
def check_range(event_id, range = 1)
element = $game_player
object = $game_map.events[event_id]
x = (object.x - element.x) * (object.x - element.x)
y = (object.y - element.y) * (object.y - element.y)
r = x + y
if r <= (range * range)
if element.direction == 2 # Down
return true if (object.x == element.x) and (object.y > element.y)
end
if element.direction == 4 # Left
return true if (object.y == element.y) and (object.x < element.x)
end
if element.direction == 6 # Right
return true if (object.y == element.y) and (object.x > element.x)
end
if element.direction == 8 # Up
return true if (object.x == element.x) and (object.y < element.y)
end
end
return false
end
#----------------------------------------------------------------------
def erase_object(object)
event = $game_map.events[object.event_id]
key = [event.map_id, event.id, 'A']
$game_self_switches[key] = true
$game_map.refresh
#@objects.delete(object.event_id)
end
#----------------------------------------------------------------------
def reset_objects
for obj in @objects.values
next unless obj
event = $game_map.events[obj.event_id]
next unless event
key = [event.map_id, event.id, 'A']
$game_self_switches[key] = false
end
$game_map.refresh
end
#----------------------------------------------------------------------
# * Object
#----------------------------------------------------------------------
# This handles the objects by making them into 'special events' which
# are managed and interacted with via. the World class.
#----------------------------------------------------------------------
class Object
attr_accessor :event_id
attr_accessor :type
attr_accessor :level
attr_accessor :result_types
attr_accessor :results
attr_accessor :result_chances
#--------------------------------------------------------------------
def initialize(id, type, level, result_types, results, result_chances, graphic1, graphic2)
for ski in $world.skills
if ski.name == type
@type = ski
end
break if @type
end
$world.erase_object(self) unless @type
@level = level.to_i
@result_types = result_types
@results = results
@result_chances = result_chances
@id = id
@event_id = $game_map.add_event(@id, graphic1, graphic2)
end
#--------------------------------------------------------------------
def gather(actor)
skill_level = actor.gather_skill[@type]
unless skill_level
$game_system.se_play($data_system.cancel_se)
$scene.get_window.start("You require the skill #{@type.name}.")
return
end
text1 = ""
text2 = ""
if @type.item_id != 0
if @type.item_id != $game_party.actors[0].weapon_id
$game_system.se_play($data_system.buzzer_se)
text1 = "You must equip a #{$data_weapons[@type.item_id].name}."
$scene.get_window.start(text1, text2)
return
end
end
if skill_level < @level
$game_system.se_play($data_system.buzzer_se)
text1 = "#{@type.name} skill not sufficient."
$scene.get_window.start(text1, text2)
return
end
if rand(100) < (100 - @level)
result = rand(100) + 1
end_result = -1
for i in 0...@result_chances.size
next unless @result_chances[i]
end_result = i if @result_chances[i].to_i >= result
break if end_result != -1
end
if end_result == -1
text1 = "You failed to find any usable materials."
end
case @result_types[end_result].upcase
when "ITEM"
$game_party.gain_item(@results[end_result].to_i, 1)
text1 = "Gathered #{$data_items[@results[end_result].to_i].name}."
when "WEAPON"
$game_party.gain_weapon(@results[end_result].to_i, 1)
text1 = "Gathered #{$data_weapons[@results[end_result].to_i].name}."
when "ARMOR"
$game_party.gain_armor(@results[end_result].to_i, 1)
text1 = "Gathered #{$data_armors[@results[end_result].to_i].name}."
end
if skill_level < @type.max_level
if (skill_level < @level + 10) and (rand(100)+@level > skill_level)
actor.gather_skill[@type] += 1
text2 = "(#{@type.name} skill up!)"
end
end
$game_system.se_play($data_system.decision_se)
else
text1 = "You failed to find any usable materials."
$game_system.se_play($data_system.cancel_se)
end
$scene.get_window.start(text1, text2)
$world.erase_object(self)
end
end
#----------------------------------------------------------------------
# * Skills
#----------------------------------------------------------------------
# This object is a container that holds the values for skills and
# organizes them for future reference. These are not RMXP's default
# 'skills' but instead gathering skills which are used to seperate
# different kinds of gathering points.
#----------------------------------------------------------------------
class Skills
attr_accessor :name
attr_accessor :max_level
attr_accessor :item_id
#--------------------------------------------------------------------
def initialize(name, item_id = 0, max_level = 100)
@name = name.to_s
@max_level = max_level.to_i
@item_id = item_id.to_i
end
end
#----------------------------------------------------------------------
# * Window_Get
#----------------------------------------------------------------------
# This is a special window, similar to the Help Window in RMXP's
# non-extended code. However it will appear and fade automatically when
# new text is added. It is the standard way for World Objects to display
# information to the player.
#----------------------------------------------------------------------
class Window_Get < Window_Base
def initialize
super(16, 16, 608, 64)
self.contents = Bitmap.new(width - 32, height - 32)
self.opacity = 0
self.back_opacity = 0
self.contents_opacity = 0
@text = ""
@text2 = ""
end
#--------------------------------------------------------------------
def refresh
self.contents.clear
self.contents.draw_text(4, 0, self.contents.width - 8, 32,
"#{@text.to_s} #{@text2.to_s}", 1)
end
#--------------------------------------------------------------------
def start(text, text2 = "")
@text = text
@text2 = text2
@timer = 0
refresh
end
#--------------------------------------------------------------------
def update
if @text != "" and self.opacity != 0
if $game_player.y <= 5
self.y = 400 if self.y == 16
else
self.y = 16 if self.y == 400
end
end
if @text != "" and self.opacity != 255
self.opacity += 20 unless self.opacity >= 255
self.back_opacity += 10 unless self.back_opacity >= 120
self.contents_opacity += 20 unless self.contents_opacity >= 255
elsif @text == "" and self.opacity != 0
self.opacity -= 20 unless self.opacity <= 0
self.back_opacity -= 10 unless self.back_opacity <= 0
self.contents_opacity -= 20 unless self.contents_opacity <= 0
elsif @text != "" and self.opacity >= 255
@timer += 1
@text = "" if @timer == 100
@text2 = "" if @timer == 100
end
end
end
#----------------------------------------------------------------------
# * Window_WorldSkills
#----------------------------------------------------------------------
# This is a special window, designed to display information about your
# world skills.
#----------------------------------------------------------------------
class Window_WorldSkills < Window_Selectable
def initialize(actor = $game_party.actors[0])
super(0, 0, 640, 480)
self.contents = Bitmap.new(width - 32, height - 32)
@actor = actor
refresh
end
def refresh
self.contents.clear
i = 0
for ski in $world.skills
next unless ski
skill_level = @actor.gather_skill[ski]
self.contents.draw_text(4, 32 * i, 600, 32, ski.name)
if ski.item_id != 0
self.contents.draw_text(4, 32 * i, 600, 32, "Requires: " + $data_weapons[ski.item_id].name, 2)
end
if skill_level
self.contents.draw_text(4, 32 * (i + 1), 600, 32, "#{skill_level}/#{ski.max_level}")
else
self.contents.draw_text(4, 32 * (i + 1), 600, 32, "Skill not trained.")
end
i += 2
end
end
end
end
#------------------------------------------------------------------------
# * Scene_Map
#------------------------------------------------------------------------
# RMXP's Default Scene_Map editted via aliases to include the Window_Get
# subclass of the World class and to take proper input to control the
# World class and the Objects within it.
#------------------------------------------------------------------------
class Scene_Map
attr_accessor :get_window
#----------------------------------------------------------------------
alias prx_world_s_map_main_draw main
#----------------------------------------------------------------------
def main
@get_window = World::Window_Get.new
prx_world_s_map_main_draw
@get_window.dispose
end
#----------------------------------------------------------------------
alias prx_world_s_map_update update
#----------------------------------------------------------------------
def update
$world.check_gather if (Input.trigger?(Input::A) and
@get_window.opacity <= 0)
@get_window.update
prx_world_s_map_update
end
end
#------------------------------------------------------------------------
# * Game_Actor
#------------------------------------------------------------------------
# RMXP's Default Game_Actor editted via aliases to initialize the
# @gather_skill hash properly. Also includes the train_skill() method to
# update that hash accordingly when learning new skills, and the
# untrain_skill() method which does the same.
#------------------------------------------------------------------------
class Game_Actor
attr_accessor :gather_skill
#----------------------------------------------------------------------
alias prx_world_g_actor_setup setup
#----------------------------------------------------------------------
def setup(actor_id)
prx_world_g_actor_setup(actor_id)
@gather_skill = Hash.new(nil)
end
#----------------------------------------------------------------------
def train_skill(name, level = 1)
for ski in $world.skills
if ski.name == name
@gather_skill[ski] = level
if $scene.is_a?(Scene_Map)
$scene.get_window.start("#{@name} learned #{ski.name}!")
end
return
end
end
end
#----------------------------------------------------------------------
def untrain_skill(name)
for ski in $world.skills
if ski.name == name
@gather_skill[ski] = nil
@gather_skill.flatten
if $scene.is_a?(Scene_Map)
$scene.get_window.start("#{@name} forgot #{ski.name}!")
end
return
end
end
end
end
#------------------------------------------------------------------------
# * Game_Map
#------------------------------------------------------------------------
# RMXP's Default Game_Map class, aliased to add some initialization
# features and given the method 'add event' to add a blank event to the
# map. add_event also returns the id of the newly created event (as the id
# sent to the method is offset from the original events)
#------------------------------------------------------------------------
class Game_Map
alias px_g_map_setup setup
#----------------------------------------------------------------------
def setup(map_id)
$world.reset_objects if $world
px_g_map_setup(map_id)
@events.sort
@last_event_id = @events.values[-1].id
$world = World.new if !$world
$world.setup(@events)
end
#----------------------------------------------------------------------
def update_last_event_id
@events.sort
@last_event_id = @events.values[-1].id
end
#----------------------------------------------------------------------
def add_event(id, graphic1, graphic2)
event = RPG::Event.new(-1, -1)
event.id = @last_event_id + id
event.name = 'World Object'
loop {
event.x = rand(width)
event.y = rand(height)
break if valid?(event.x, event.y) and passable?(event.x, event.y, 0) and
passable?(event.x, event.y, 2) and passable?(event.x, event.y, 4) and
passable?(event.x, event.y, 6) and passable?(event.x, event.y, 8)
}
page1 = RPG::Event::Page.new
page1.direction_fix = true
page1graphic = RPG::Event::Page::Graphic.new
page1graphic.character_name = graphic1
page1.graphic = page1graphic
page2 = RPG::Event::Page.new
page2.direction_fix = true
page2graphic = RPG::Event::Page::Graphic.new
page2graphic.character_name = graphic2
page2.graphic = page2graphic
page2condition = RPG::Event::Page::Condition.new
page2condition.self_switch_valid = true
page2condition.self_switch_ch = "A"
page2.condition = page2condition
event.pages = [page1, page2]
$game_map.events[event.id] = Game_Event.new(@map_id, event)
return event.id
end
end
#------------------------------------------------------------------------
# * Game_Event
#------------------------------------------------------------------------
# RMXP's Default Game_Event with a map_id attribute and a definition to
# return the name of an event. Used to locate the World Objects initialization
# event.
#------------------------------------------------------------------------
class Game_Event < Game_Character
attr_reader :map_id
attr_reader :page
#----------------------------------------------------------------------
def name
return @event.name
end
end
#------------------------------------------------------------------------
# * Scene_WorldSkills
#------------------------------------------------------------------------
# A small class made to demonstrate the World Skill information window.
#------------------------------------------------------------------------
class Scene_WorldSkills
def initialize(actor = $game_party.actors[0])
@actor = actor
end
def main
@skills_window = World::Window_WorldSkills.new(@actor)
Graphics.transition
loop do
Graphics.update
Input.update
update
break if $scene != self
end
Graphics.freeze
@skills_window.dispose
end
def update
@skills_window.update
if Input.trigger?(Input::B)
$scene = Scene_Map.new
$game_system.se_play($data_system.decision_se)
return
end
end
end
Expand to see the code.