znmud-0.0.1/benchmark/
znmud-0.0.1/cmd/
znmud-0.0.1/cmd/emotes/
znmud-0.0.1/cmd/objects/
znmud-0.0.1/cmd/tiny/
znmud-0.0.1/doc/
znmud-0.0.1/farts/
znmud-0.0.1/lib/
znmud-0.0.1/lib/combat/
znmud-0.0.1/lib/core/bodytypes/
znmud-0.0.1/lib/engine/
znmud-0.0.1/lib/farts/
znmud-0.0.1/logs/
# File: olc.rb
# Author: Craig Smith
# This source code copyright (C) 2009 Craig Smith
# All rights reserved.
#
# Released under the terms of the GNU Public License
# See COPYING file for additional information.
#


require 'utility/log'
require 'olc/olcmenu'
require 'olc/olcmenuoption'
require 'olc/olceditor'

# Core Online Creation Object
class Olc
  logger 'DEBUG'

  attr_accessor :id, :ch, :obj, :menu, :editstr, :title, :newobj, :menu_stack

  # Initializes an OLC Object
  # [+oid+] Optional oid of what to edit
  # [+args+] Optional args that maybe used
  def initialize(id, oid, args=nil)
	self.title = "OLC Core Editor" if not title
	self.id = id
	self.ch = get_object(id)
	if oid
		self.newobj = false
		self.obj = get_object(oid)
		if not verify_perm(obj)
			ch.sendto("You do not have permision to edit that object")
			exit_olc(true)
		end
	end
	self.menu = olc_basemenu(title,self,nil)
	self.menu_stack = []
	ch.mode = :olc
	ch.sendroom("#{ch.name} closes #{ch.pos_pronoun} eyes and begins creating...")
  end

  # Creates a basic menu with just a Quit button
  # [+title+] title of the menu
  # [+calling+] Calling object (one who controls this menu)
  # [+init+] Init function that sets up this menu
  # [+reset+] Optional reset
  # [+return+] basic menu object
  def olc_basemenu(title,calling,init,reset=nil)
	if reset == "reset"
		m = menu
	else
		m = OlcMenu.new(title,calling,init)
	end
	m.add_text_option("Q","Quit", self, :quit_menu)
	m
  end

  # Main command parsing routine.  Overwrite with your object editor
  # [+m+] command to parse
  def parse(m)
	return if not menu
	msg = menu.parse(m)
	if msg
		if msg.size > 0
			ch.sendto(msg)
		else
			show_menu
		end
	else
		show_menu
	end
  end

  # Add a submenu
  # [+menu+] A created OlcMenu object
  # [+reset+] Optional.  If set ignore
  def submenu(newmenu, reset=nil)
	if not reset == "reset"
		self.menu_stack.push menu
		self.menu = newmenu
	end
	show_menu
  end

  # Quit the current menu.  If last menu, exit OLC
  # [+m+] ignored
  def quit_menu(m=nil)
	if menu_stack.size > 0
		self.menu = menu_stack.pop
		menu.reset
		show_menu
	else
		exit_olc
	end
  end

  # Exits the OLC session
  def exit_olc(cancel=false)
	ch.mode = :playing
	ch.olc = nil
	self.menu = nil
	if not cancel
		if newobj
			obj.isclone = false
			obj.unused = true
			obj.location = 0
			put_object(obj)
		end
		ch.sendto("Saved ##{obj.id}.")
	end
	ch.sendroom("#{ch.name} has finished creating.")
  end

  # Verify you can edit this object
  # [+obj+] Object to try and edit
  # [+return+] true if you are allowed to edit this object
  def verify_perm(obj)
	return true if ch.world.is_admin? id || obj.owner == id
	false
  end

  # Display the menu to the user
  def show_menu
	ch.sendto(menu.to_s)
  end

  def get_object(oid)
    Engine.instance.db.get(oid)
  end

  def put_object(oid)
    Engine.instance.db.put(oid)
  end
end