Added a prototype flag to typeclass to allow setting existing objects to a new prototype.

This commit is contained in:
Julie Iaccarino 2020-02-16 01:43:39 -08:00
parent 3a10f35abd
commit d43f4f9789

View file

@ -1930,9 +1930,12 @@ class CmdTypeclass(COMMAND_DEFAULT_CLASS):
list - show available typeclasses. Only typeclasses in modules actually list - show available typeclasses. Only typeclasses in modules actually
imported or used from somewhere in the code will show up here imported or used from somewhere in the code will show up here
(those typeclasses are still available if you know the path) (those typeclasses are still available if you know the path)
prototype - clean and overwrite the object with the specified
prototype key - effectively making a whole new object.
Example: Example:
type button = examples.red_button.RedButton type button = examples.red_button.RedButton
type/prototype button=a red button
If the typeclass_path is not given, the current object's typeclass is If the typeclass_path is not given, the current object's typeclass is
assumed. assumed.
@ -1954,7 +1957,7 @@ class CmdTypeclass(COMMAND_DEFAULT_CLASS):
key = "typeclass" key = "typeclass"
aliases = ["type", "parent", "swap", "update"] aliases = ["type", "parent", "swap", "update"]
switch_options = ("show", "examine", "update", "reset", "force", "list") switch_options = ("show", "examine", "update", "reset", "force", "list", "prototype")
locks = "cmd:perm(typeclass) or perm(Builder)" locks = "cmd:perm(typeclass) or perm(Builder)"
help_category = "Building" help_category = "Building"
@ -2038,6 +2041,28 @@ class CmdTypeclass(COMMAND_DEFAULT_CLASS):
new_typeclass = self.rhs or obj.path new_typeclass = self.rhs or obj.path
prototype = None
if "prototype" in self.switches:
key = self.rhs
prototype = protlib.search_prototype(key=key)
if len(prototype) > 1:
caller.msg(
"More than one match for {}:\n{}".format(
key, "\n".join(proto.get("prototype_key", "") for proto in prototype)
)
)
return
elif prototype:
# one match
prototype = prototype[0]
else:
# no match
caller.msg("No prototype '{}' was found.".format(key))
return
new_typeclass = prototype["typeclass"]
self.switches.append("force")
self.switches.append("reset")
if "show" in self.switches or "examine" in self.switches: if "show" in self.switches or "examine" in self.switches:
string = "%s's current typeclass is %s." % (obj.name, obj.__class__) string = "%s's current typeclass is %s." % (obj.name, obj.__class__)
caller.msg(string) caller.msg(string)
@ -2075,6 +2100,9 @@ class CmdTypeclass(COMMAND_DEFAULT_CLASS):
new_typeclass, clean_attributes=reset, clean_cmdsets=reset, run_start_hooks=hooks new_typeclass, clean_attributes=reset, clean_cmdsets=reset, run_start_hooks=hooks
) )
if "prototype" in self.switches:
spawner.batch_update_objects_with_prototype(prototype, objects=[obj])
if is_same: if is_same:
string = "%s updated its existing typeclass (%s).\n" % (obj.name, obj.path) string = "%s updated its existing typeclass (%s).\n" % (obj.name, obj.path)
else: else: