####################################################################
# Module which reads XML formatted note tags and returns them as
# HashMaps
module Tag_reader
###################################################################
# Represents a single XML node
class XML_node
#####################################
# Constructor
def initialize(name)
@name=name
# Set of name=value pairs
@attributes={}
end
###################################
# Adds an attribute
def add_attribute(name, value)
@attributes[name]=value
end
###################################
# Returns this as an XML string
def to_s
result="<"+@name
@attributes.each {
|key, value| result += " "+key+"=\""+value+"\""
}
result += " />\n"
return result
end
attr_reader

name)
attr_reader

attributes)
end
#######################################################
# Reads a set of XML tags
class XML_Reader
def initialize()
@nodes=[]
end
##########################################################
# Reads the XML tags in note_string. Does nothing if the
# note string is empty or nil. Returns the set of nodes read
# from the note_string. This may be empty
def read(note_string)
@nodes.clear
if (note_string == nil)
return @nodes
end
note_s=note_string.to_s.strip
# Ignore empty strings
string_len=note_s.length
if (string_len == 0)
return
end
index=0
worker=""
right_index=0
node_name=""
# Parse the XML string
while (index < string_len)
# Search for < mark
if (note_s[index] != '<')
index += 1
next
end
# Find the > mark after the < mark
right_index=note_s.index("/>",index)
if (right_index == nil)
right_index=note_s.index('>',index)
if (right_index == nil)
break
end
end
# This is a string from < to >
worker=note_s.slice(index, right_index-index+1)
worker=worker.strip
# Find name="value" pairs
# Skip the <name part of the XML
attr_index=worker.index(' ')
node_name=worker.slice(1,attr_index).strip
new_node=XML_node.new(node_name)
while (worker.length > attr_index)
if (worker[attr_index] == ' ')
attr_index += 1
next
end
value_index=worker.index("=\"", attr_index)
if (value_index == nil)
break
end
# Backspace to find the previous space or < mark - start of the
# attribute
attr_start_index=value_index-1
while (worker[attr_start_index] != '<' &&
worker[attr_start_index] != ' ')
attr_start_index -=1
end
# Get to the start of the attribute name
attr_start_index += 1
# Skip the =" marker after attribute name
second_quote=worker.index("\"", value_index+3)
if (second_quote == nil)
break
end
attr=worker.slice(attr_start_index, value_index-attr_index)
value=worker.slice(value_index+2, second_quote-value_index-2)
new_node.add_attribute(attr, value)
attr_index = second_quote+1
end
@nodes << new_node
index=right_index+1
end
return @nodes
end
end
end
# And an example:
#================================================
#
# Script: SwitchSetter
# by: whitesphere (whitesphere@comcast.net)
# July 5 2014
#================================================
=begin
================================================================================
Description: Sets or unsets groups of switches when the map is entered
--------------------------------------------------------------------------------
History
v1.0 (2014/07/10)
Initial release
--------------------------------------------------------------------------------
Terms of Use
- You are free to use this script for non-commercial projects.
- For commercial projects, at least contact me first.
- This script is provided as-is. Don't expect me to give support.
- Reported bug will be fixed, but no guarantee on requested features.
- No guarantee either for compatibility fixes.
- Give credit to whitesphere (me), and do not delete this header.
--------------------------------------------------------------------------------
QUICK START:
Add a note tag in the following format to a Map:
<switch on="1,2,3" off="4,5">
or
<variable set="1=23, 2=34" >
or
<common_event call="1">
or
<common_event call="1,2,3">
Common event executions take place after all switches and variables are
set.
The numbers are the Switch IDs to turn ON or OFF.
For variables, the number is the variable ID, the value after the = is what
the variable will be set to
--------------------------------------------------------------------------------
Below are the end-user SwitchSetter script functions:
Not Applicable
--------------------------------------------------------------------------------
=end
module SwitchSetter
# Helper function which tries to convert any Ruby object to a true or
# false value
def self.to_b object
if object == nil then
return false
end
if object.respond_to?

to_s) then
obj_str=object.to_s.downcase.strip
if obj_str == "true" || obj_str == "yes" || obj_str == "on" ||
obj_str == "t" || obj_str == "y" || obj_str == "1" then
return true
end
return false
end
if object.respond_to?

to_i) then
obj_int=object.to_i
if obj_int == 0 then
return false
else
return true
end
end
return true
end
###############################################################
###############################################################
# Encapsulates all entries for a single Switch or Variable
class Value_holder
attr_reader

value)
attr_reader

id)
def initialize(id, value)
@id=id.to_i
@value=value
end
def to_s
result="id="+@id.to_s+", value="+@value.to_s
return result
end
end
###############################################################
###############################################################
# This class handles the set of switches and variables
class SwitchVarHolder
# Constructor
def initialize
@switch_set=[]
@variable_set=[]
@common_event_set=[]
end
# Clear the sets
def clear()
@switch_set.clear
@variable_set.clear
@common_event_set.clear
end
def load_map(switch_set, variable_set, common_event_set)
@switch_set.clear
@variable_set.clear
@common_event_set.clear
if (switch_set != nil)
switch_set.each {
|entry| @switch_set << entry
puts("Loaded switch entry "+entry.to_s)
}
end
if (variable_set != nil)
variable_set.each {
|entry| @variable_set << entry
puts("Loaded variable entry "+entry.to_s)
}
end
if (common_event_set != nil)
common_event_set.each {
|entry| @common_event_set << entry
puts("Loaded common event entry "+entry.to_s)
}
end
# Update the switches
@switch_set.each {
|entry| if ($game_switches[entry.id] == nil)
next
end
$game_switches[entry.id]=SwitchSetter::to_b(entry.value)
}
# Update the variables
@variable_set.each {
|entry| if ($game_variables[entry.id] == nil)
next
end
$game_variables[entry.id]=entry.value.to_i
}
# Queue up the common events
@common_event_set.each {
|entry| $game_temp.reserve_common_event(entry.id)
}
end
end
def self.init()
@@instance=SwitchVarHolder.new()
end
def self.load(switches, variables, common_events)
@@instance.load_map(switches, variables, common_events)
end
def self.started_everything?
if (@@instance != nil)
return true
else
return false
end
end
end
SwitchSetter::init
# Add the accessors to get the switches to modify
class Game_Map
attr_reader

map_id)
alias SwitchSetter_setup setup
def setup(map_id)
SwitchSetter_setup(map_id)
# Load the tile to region mappings
SwitchSetter::load( switch_settings(),
variable_settings(), common_event_settings())
end
# We fetch these from the RPGMap
def switch_settings
return @map.switch_settings
end
# We fetch these from the RPGMap
def variable_settings
return @map.variable_settings
end
# We fetch these from the RPGMap
def common_event_settings
return @map.common_event_settings
end
end
class RPG::Map
def read_note_tag_as_nodes
if (@node_set == nil)
reader=Tag_reader::XML_Reader.new
@node_set=reader.read(@note)
end
return @node_set
end
# Returns the current switch settings
def switch_settings
if (@switch_settings != nil)
if (@switch_settings.empty?)
return nil
end
return @switch_settings
end
@switch_settings=[]
nodes=read_note_tag_as_nodes
if (nodes == nil)
return nil
end
nodes.each {
|current|
if (current.name != "switch")
next
end
on_string=current.attributes["on"]
off_string=current.attributes["off"]
if (on_string == nil && off_string == nil)
next
end
if (on_string != nil)
group=on_string.split(",")
group.each { |single|
new_entry=SwitchSetter::Value_holder.new(single, "true")
@switch_settings << new_entry
}
end
if (off_string != nil)
group=off_string.split(",")
group.each { |single|
new_entry=SwitchSetter::Value_holder.new(single, "false")
@switch_settings << new_entry
}
end
}
return @switch_settings
end
# Returns the current variable settings
def variable_settings
if (@variable_settings != nil)
if (@variable_settings.empty?)
return nil
end
return @variable_settings
end
@variable_settings=[]
nodes=read_note_tag_as_nodes
if (nodes == nil)
return nil
end
nodes.each {
|current|
if (current.name != "variable")
next
end
set_string=current.attributes["set"]
if (set_string == nil)
next
end
group=set_string.split(",")
group.each { |single|
name_value_pair=single.split("=")
if (name_value_pair.length < 2)
next
end
new_entry=SwitchSetter::Value_holder.new(name_value_pair[0],
name_value_pair[1])
@variable_settings << new_entry
}
}
return @variable_settings
end
# Returns the current common event settings
def common_event_settings
if (@common_event_settings != nil)
if (@common_event_settings.empty?)
return nil
end
return @common_event_settings
end
@common_event_settings=[]
nodes=read_note_tag_as_nodes
if (nodes == nil)
return nil
end
nodes.each {
|current|
if (current.name != "common_event")
next
end
set_string=current.attributes["set"]
if (set_string == nil)
next
end
group=set_string.split(",")
group.each { |single|
common_event_settings << single.to_i
new_entry=SwitchSetter::Value_holder.new(name_value_pair[0],
0)
@common_event_settings << new_entry
}
}
return @common_event_settings
end
end
# This is to support multiple common events at once which can
# certainly happen with multiple events
class Game_Temp
attr_reader :reserved_common_events
alias

ld_common_event_queue_init :initialize
def initialize
old_common_event_queue_init
@reserved_common_events = []
end
# re-write
def reserve_common_event(common_event_id)
@reserved_common_events.push(common_event_id) if common_event_id > 0
end
# Note that I don't actually need to clear it out. It's done
# by the queue.
# true if list is not empty
def common_event_reserved?
!@reserved_common_events.empty?
end
# Grab the first one, first-in-first-out order
def reserved_common_event
$data_common_events[@reserved_common_events.shift]
end
end