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::    cmd_spawn.rb
#
# This source code copyright (C) 2009 Craig Smith
# All rights reserved.
#
# Released under the terms of the TeensyMUD Public License
# See LICENSE file for additional information.
#


module Cmd

  bindtextdomain("cmd")

  # Creates a spawning point
  # Syntax:
  #   spawn #<objectid> max=1
  #   spawn add #<spawnid> inv=#11 wear=#12 wield=#13 max=3 freq=15
  #   spawn del #<spawnid>
  def cmd_spawn(args)
    case args
    when /^\s*del\s*#(\d+)\s*(.*)/
    	loc = get_object(location)
	sid = $1.to_i
	args = $2
	if args.size > 0
		inv = []
		wear = []
		wield = []
		inv << $1.to_i if args=~/inv=#*(\d+)/
		wear << $1.to_i if args=~/wear=#*(\d+)/
		wield << $1.to_i if args=~/wield=#*(\d+)/
		if loc.spawns.include? sid
			spawn = get_object(sid)
			inv.each do |oid|
				spawn.inv.delete oid if spawn.inv.include? oid
			end
			wear.each do |oid|
				spawn.wear.delete oid if spawn.wear.include? oid
			end
			wield.each do |oid|
				spawn.wield.delete oid if spawn.wield.include? oid
			end
			sendto _("Spawn item modified ... probably ;)")
		else
			sendto _("Could not find spawn with id of %{sid}" % {:sid => sid})
		end
	else
		if loc.spawns.include? sid
			loc.spawns.delete sid
			sendto _("Removed spawn point")
		else
			sendto _("Could not find spawn with id of %{sid}" % {:sid => sid})
		end
	end
    when /^\s*#(\d+)\s*(.*)/
    	loc = get_object(location)
    	oid = $1.to_i
	args = $2
	max = 1
	freq = nil
	max_in_room = nil
	inv = []
	wear = []
	wield = []
	# Saftey checks
	o = get_object(oid)
	if not valid_spawn_oid? oid
		sendto _("Not a valid OID.")
		return
	end
	max = $1.to_i if args=~/max=(\d+)/
	max_in_room = $1.to_i if args=~/max_in_room=(\d+)/
	freq = $1.to_i if args=~/freq=(\d+)/
	inv << $1.to_i if args=~/inv=#*(\d+)/
	wear << $1.to_i if args=~/wear=#*(\d+)/
	wield << $1.to_i if args=~/wield=#*(\d+)/
	sp = Spawn.new(oid, id)
	sp.max = max
	sp.max_in_room = max_in_room if max_in_room
	sp.location = location
	sp.frequency = freq if freq
	inv.each { |i| sp.inv.push i }
	wear.each { |w| sp.wear.push w }
	wield.each { |w| sp.wield.push w }
	put_object(sp)
	loc.spawns = [] if not loc.spawns
	loc.spawns << sp.id
	sendto _("Added new spawn point #%{spid} (%{name})" % {:spid => sp.id, :name => get_object(oid).name})
    when /^\s*add\s+#(\d+)\s*(.*)/
    	sid = $1.to_i
	args = $2
	inv = []
	wear = []
	wield = []
	max = nil
	max_in_room = nil
	freq = nil
	loc = get_object(location)
	if loc.spawns.include? sid
		sp = get_object(sid)
		max = $1.to_i if args=~/max=(\d+)/
		max_in_room = $1.to_i if args=~/max_in_room=(\d+)/
		freq = $1.to_i if args=~/freq=(\d+)/
		inv << $1.to_i if args=~/inv=#*(\d+)/
		wear << $1.to_i if args=~/wear=#*(\d+)/
		wield << $1.to_i if args=~/wield=#*(\d+)/
		sp.max = max if max
		sp.max_in_room = max_in_room if max_in_room
		sp.frequency = freq if freq
		inv.each { |i| sp.inv.push i }
		wear.each { |w| sp.wear.push w }
		wield.each { |w| sp.wield.push w }
		sendto _("Spawn object adjusted.")
	else
		sendto _("No spawn object with id of %{sid}" % {:sid => sid})
	end
    else
    	loc = get_object(location)
	if loc.spawns
		if loc.spawns.size > 0
			loc.spawns.each do |sid|
				add_event(sid,id,:describespawn)
			end
		else
			sendto _("This room has no spawn points")
		end
	else
		sendto _("This room has no spawn points")
	end
    end
  end

  def valid_spawn_oid?(oid)
	o = get_object(oid)
	if o.isclone or o.is_a? Spawn or o.is_a? Account or o.is_a? World or 
	   o.is_a? Room or o.is_a? Body or o.is_a? Exit or o.class.to_s == "Character"
		return false
	end
	true
  end
end