All pastes #126569 Raw Edit

Stuff

public ruby v1 · immutable
·published 2006-08-11 16:36 UTC
rendered paste body
#!/usr/bin/rubyrequire 'rexml/document'# Class CreatureXML# Provides an interface with the creatures.xml file, desgined for the exact setup and naming used with the default one# TODO: Add description support, once more creatures have it# TODO: Add images for each of the creaturesclass CreatureXML	# initialize: Starts the ReXML parser, and sets a few ground variables	def initialize(xml_file = "creatures.xml")		xml = REXML::Document.new(File.new(xml_file.to_s))		@root = xml.root		@creatures = @root.elements		@number_of_creatures = @creatures.size		@creature_names = get_all_creatures	end			# returns a list of all the creatures in the XML file. Used many times within the class	def get_all_creatures		creatures = {}		i = 1		while i <= @number_of_creatures			creatures[@creatures[i].attributes['name']] = i			i += 1		end		creatures	end		# returns true or false if the creature name is a real one or not	def is_a_creature?(creature)		@creature_names.include? creature	end		# returns the creature's name from the index in @creature_names	def get_creature(creature_id)		@creature_names[creature_id]	end	# quickie, for convience. Returns the number of creatures loaded. (Duhh...)	def number_of_creatures		@number_of_creatures	end			# Development function, used to return the root element in ReXML format	# Possible modularity is achived through this function	def get_xml		@root	end			# The BIG KAHUNA! Returns the stats of a creature, through it's name	def get_creature_stats(name)		atk = @creatures[@creature_names[name]].elements[2].elements[1].attributes[1]		de_f = @creatures[@creature_names[name]].elements[2].elements[2].attributes[1]		hp = @creatures[@creature_names[name]].elements[2].elements[3].attributes[1]		speed = @creatures[@creature_names[name]].elements[2].elements[4].attributes[1]		xp = @creatures[@creature_names[name]].elements[5].attributes[1]		lvl = @creatures[@creature_names[name]].elements[3].attributes[1]		[hp,atk,de_f,speed,xp,lvl]	end		# same as get_creature_stats, but you use the index, which is dynamicly made in get_creature_stats, as the parameter	def get_creature_id_stats(id)		atk = @creatures[id].elements[2].elements[1].attributes[1]		de_f = @creatures[id].elements[2].elements[2].attributes[1]		hp = @creatures[id].elements[2].elements[3].attributes[1]		speed = @creatures[id].elements[2].elements[4].attributes[1]		xp = @creatures[id].elements[5].attributes[1]		lvl = @creatures[id].elements[3].attributes[1]		{:hp => hp, :atk => atk, :def => de_f, :speed => speed, :xp => xp, :level => lvl}	end	# get a monsters drops (anything that is in it's corpse, at death)	def get_drops(name)		drops = @creatures[@creature_names[name]].elements[6]		hash = {}		for i in drops.size			hash[drops[i].attributes[1]] = {:quantity => drops[i].attributes[1], :probability => drops[i].attributes[1]}		end		hash	end	# again, another id-instead-of-name function	def get_drops_id(id)		drops = @creatures[id].elements[6]		hash = {}		for i in drops.size			hash[drops[i].attributes[1]] = {:quantity => drops[i].attributes[1], :probability => drops[i].attributes[1]}		end		hash	end	# sometimes, when you battle, the creature will make a sound of some sort	# this gets that sound, returns nil on non-existence	def get_creature_noise(name)		ai_tag = @creatures[@creature_names[name]].elements[@creatures[@creature_names[name]].elements.size - 1]		says = ai_tag.elements[1] if ai_tag.has_elements?		unless says.has_attributes?			noise = says.elements[1].attributes[1]		end		nil unless noise	end	# another id-instead-of-name function	def get_creature_id_noise(id)		ai_tag = @creatures[id].elements[@creatures[id].elements.size - 1]		says = ai_tag.elements[1] if ai_tag.has_elements?		unless says.has_attributes?			noise = says.elements[1].attributes[1]		end		nil unless noise	endend