#!/usr/bin/env ruby
# $Revision: 1.41 $
# $Date: 2003/12/06 18:12:08 $
# $Author: jefus, mikeman2 $

class Base
	attr_accessor :name, :keywords, :description, :location, :contents, :config, :progs

	def loadProgs
		if @config['@progs'].instance_of?(Array)
			@config['@progs'].each{|cfname|
				prog = Prog.new(cfname)
				$system.progs << prog
				@progs << prog
			}
		elsif @config['@progs'].instance_of?(String)
			prog = Prog.new(@config['@progs'], self)
			$system.progs << prog
			@progs << prog
		end if @config['@progs'] && @config['@progs'] != ""
	end

	def initialize(config)
		#p @config
		@config = config

		begin
			@progs = []
			@name = @config['@name']
			@keywords = Array.new
			if @config['@keywords'].instance_of?(Array)
				@config['@keywords'].each{|keyword| @keywords << keyword}
			else
				@keywords << @config['@keywords']
			end
			@description = @config['@description']
			@contents = []
		rescue
			Logger.log("Failed on Base::initialze!: #{$!}\n\n*******TRACE**********\n\n#{$@}")
			#Logger.log($@)
		end
		begin
			loadInventory
		rescue
			Logger.log("Failed loading inventory!: #{$!}\n\n*******TRACE**********\n\n#{$@}")
		end
		begin
			loadProgs
		rescue
			Logger.log("Failed loading progs!: #{$!}\n\n*******TRACE**********\n\n#{$@}")
		end
	end

	def loadInventory
		if @config['@inventory'].instance_of?(Array)
			@config['@inventory'].each{|cfname|
				item = Item.new(cfname)
				@contents << item
				$system.items << item
			}
		else
			@contents << Item.new(@config['@inventory'])
		end if @config['@inventory'] != nil && config['@inventory'].length > 0
	end

	def saveInventory
		invsave = []
		@contents.each{|obj|
			if obj.kind_of?(Item)
				invsave << obj.config.filename
			end
		}
		@config['@inventory'] = invsave
	end

	def save
		begin
			self.instance_attributes.each{|key, value|
				if value.instance_of?(String) || value.instance_of?(Array)
					#Logger.log("saving value #{value} to attribute #{key} in cfile #{@config.to_s}")
					@config[key] = value
				end
			}
		rescue
			Logger.log("Error saving object '#{@name}': #{$!}\n\n*******TRACE**********\n\n#{$@}")
			Logger.log($@)
		end
		begin
			saveInventory
		rescue
			Logger.log("Error saving inventory for object '#{@name}': #{$!}\n\n*******TRACE**********\n\n#{$@}")
		end
		return
	end
	def long
		output = "#{@name} &*"
		output << "lies" if self.kind_of?(Item)
		output << "is" if self.kind_of?(Mobile)
		output << " here."
	end

	def render
		output =  "\n&r#{@name}&*\n"
		output << "#{@description}\n"

		output << "&WExits: &*" if @exits.length > 0
		@exits.each{|exit| output << "#{exit.dir}" } if @exits.length > 0
		output << "&*\n"
		@contents.each {|thing| 
			if thing != self
				output << "&c" if thing.kind_of?(Mobile)
				output << "&l" if thing.kind_of?(Item)
				output << "#{thing.long}\n"
			end
		}
		return output
	end

	def move(where)
		@location.contents.delete(self) if @location	
		@location = where
		@location.contents << self
	end

	def instance_attributes
		attrs = Hash.new
		instance_variables.each{|var|
			value = eval(var)
			attrs[var] = value
		}
		return attrs
	end
end

Logger.log("Base object code initialized.")