Modifications to the @create and @parent commands, to safeguard against erroneous scriptlink names as well as allowing to define parent upon @creation. The @parent command calls at_object_creation(), seems that behaviour is the intuitive one.

This commit is contained in:
Griatch 2009-04-25 17:41:58 +00:00
parent 9abde7b60f
commit 9d199032b0
2 changed files with 69 additions and 32 deletions

View file

@ -264,27 +264,50 @@ GLOBAL_CMD_TABLE.add_command("@find", cmd_find,
def cmd_create(command):
"""
Creates a new object of type 'THING'.
@create
Usage: @create objname [=parent]
Creates a new object. If parent is given, the object is created as a child of this
parent. The parent script is assumed to be located under game/gamesrc/parents
and any further directory structure is given in Python notation. So if you
have a correct parent object defined in parents/examples/red_button.py, you could
load create a new object inheriting from this parent like this:
@create button=example.red_button
"""
source_object = command.source_object
if not command.command_argument:
source_object.emit_to("You must supply a name!")
else:
# Create and set the object up.
# TODO: This dictionary stuff is silly. Feex.
odat = {"name": command.command_argument,
"type": 3,
"location": source_object,
"owner": source_object}
new_object = Object.objects.create_object(odat)
return
eq_args = command.command_argument.split('=', 1)
target_name = eq_args[0]
# Create and set the object up.
# TODO: This dictionary stuff is silly. Feex.
odat = {"name": target_name,
"type": 3,
"location": source_object,
"owner": source_object}
new_object = Object.objects.create_object(odat)
if len(eq_args)>1:
parent_str = eq_args[1]
if parent_str and new_object.set_script_parent(parent_str):
source_object.emit_to("You create %s as a child of %s." %
(new_object, parent_str))
else:
source_object.emit_to("'%s' is not a valid parent. Using default." %
parent_str)
else:
source_object.emit_to("You create a new thing: %s" % (new_object,))
# Trigger stuff to happen after said object is created.
new_object.scriptlink.at_object_creation()
# Trigger stuff to happen after said object is created.
new_object.scriptlink.at_object_creation()
GLOBAL_CMD_TABLE.add_command("@create", cmd_create,
priv_tuple=("genperms.builder"))
priv_tuple=("genperms.builder"),auto_help=True)
def cmd_cpattr(command):
"""
@ -759,4 +782,4 @@ def cmd_destroy(command):
source_object.emit_to("You destroy %s." % target_obj.get_name())
target_obj.destroy()
GLOBAL_CMD_TABLE.add_command("@destroy", cmd_destroy,
priv_tuple=("genperms.builder"))
priv_tuple=("genperms.builder"))

View file

@ -27,40 +27,54 @@ def cmd_parent(command):
source_object = command.source_object
if not command.command_argument:
source_object.emit_to("Change the parent of what?")
source_object.emit_to("Change/check the parent of what?")
return
eq_args = command.command_argument.split('=', 1)
target_name = eq_args[0]
parent_name = eq_args[1]
target_obj = source_object.search_for_object(target_name)
if len(target_name) == 0:
source_object.emit_to("Change the parent of what?")
if not target_obj:
return
if len(eq_args) > 1:
target_obj = source_object.search_for_object(target_name)
# Use search_for_object to handle duplicate/nonexistant results.
if not target_obj:
return
#if we gave a command of the form @parent obj=something we want to
#somehow affect the parent.
parent_name = eq_args[1]
#check permissions
if not source_object.controls_other(target_obj):
source_object.emit_to(defines_global.NOCONTROL_MSG)
return
# Allow the clearing of a zone
if parent_name.lower() == "none":
# Clear parent if command was @parent obj= or obj=none
if not parent_name or parent_name.lower() == "none":
target_obj.set_script_parent(None)
source_object.emit_to("%s reverted to default parent." % (target_obj))
return
new_parent = target_obj.scriptlink()
source_object.emit_to("%s reverted to its default parent (%s)." %
(target_obj, new_parent))
return
target_obj.set_script_parent(parent_name)
source_object.emit_to("%s is now a child of %s." % (target_obj, parent_name))
# If we reach this point, attempt to change parent.
former_parent = target_obj.get_scriptlink()
if target_obj.set_script_parent(parent_name):
#new script path added; initialize the parent
target_obj.scriptlink.at_object_creation()
s = "%s's parent is now %s (instead of %s).\n\r"
s += "Note that the new parent type could have overwritten "
s += "same-named attributes on the existing object."
source_object.emit_to(s)
else:
source_object.emit_to("'%s' is not a valid parent path." % parent_name)
else:
# We haven't provided a target zone.
source_object.emit_to("What should the object's parent be set to?")
return
# We haven't provided a target; list the current parent
current_parent = target_obj.get_scriptlink()
source_object.emit_to("Current parent of %s is %s." %
(target_obj,current_parent))
GLOBAL_CMD_TABLE.add_command("@parent", cmd_parent,
priv_tuple=("genperms.builder"))