123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776 |
- """
- Commands
- Commands describe the input the account can do to the game.
- """
- import re
- import random
- from evennia import utils, default_cmds, create_script, logger
- from evennia.utils import evtable
- from evennia.contrib.health_bar import display_meter
- from evennia.utils import inherits_from
- from evennia.utils.utils import list_to_string
- from evennia.utils.eveditor import EvEditor
- from utils.building import create_room, create_exit
- from utils.utils import has_tag, fmt_light, fmt_dark, toggle_effect, has_effect, indefinite_article
- from typeclasses.exits import BaseDoor
- from typeclasses.rooms import IndoorRoom
- from typeclasses.objects import Item, EquippableItem
- from typeclasses.scripts import Script, CmdActionScript
- from world import spells
- CMD_SEARCH_TIME = 5
- class Command(default_cmds.MuxCommand):
- """
- Inherit from this if you want to create your own command styles
- from scratch. Note that Evennia's default commands inherits from
- MuxCommand instead.
- Note that the class's `__doc__` string (this text) is
- used by Evennia to create the automatic help entry for
- the command, so make sure to document consistently here.
- Each Command implements the following methods, called
- in this order (only func() is actually required):
- - at_pre_cmd(): If this returns anything truthy, execution is aborted.
- - parse(): Should perform any extra parsing needed on self.args
- and store the result on self.
- - func(): Performs the actual work.
- - at_post_cmd(): Extra actions, often things done after
- every command, like prompts.
- """
- def at_post_cmd(self):
- caller = self.caller
- prompt = "|_|/°|w%s|n°: " % (caller.location)
- caller.msg(prompt=prompt)
- # overloading the look command with our custom MuxCommand with at_post_cmd hook
- class CmdLook(default_cmds.CmdLook, Command):
- pass
- class CmdDrop(default_cmds.CmdDrop, Command):
- pass
- class CmdGet(Command):
- """
- pick up something
- Usage:
- get <obj> [from <container>]
- Picks up an object from your location and puts it in
- your inventory.
- """
- key = "get"
- aliases = "grab"
- locks = "cmd:all()"
- arg_regex = r"\s|$"
- def parse(self):
- """
- Handle parsing of:
- <object> [FROM <container>]
- """
- self.args = args = self.args.strip().lower()
- if "from" in args:
- obj, *rest = args.split(" from ", 1)
- container = rest[0] if rest else ""
- else:
- obj = self.args
- container = ""
- self.obj_name = obj.strip()
- self.container_name = container.strip()
- def func(self):
- """implements the command."""
- caller = self.caller
- if not self.args or not self.obj_name:
- caller.msg("Get what?")
- return
- if inherits_from(caller.location, IndoorRoom) and not caller.location.db.is_lit:
- caller.msg("Its too dark to get anything.")
- return
- if self.container_name:
- container = caller.search(self.container_name, location=caller.location)
- obj = caller.search(self.obj_name, location=container)
- else:
- obj = caller.search(self.obj_name, location=caller.location)
- if not obj:
- return
- if caller == obj:
- caller.msg("You can't get yourself.")
- return
- if not obj.access(caller, "get"):
- if obj.db.get_err_msg:
- caller.msg(obj.db.get_err_msg)
- else:
- caller.msg("You can't get that.")
- return
- # calling at_before_get hook method
- if not obj.at_before_get(caller):
- return
- success = obj.move_to(caller, quiet=True)
- if not success:
- caller.msg("This can't be picked up.")
- else:
- caller.msg("You pick up %s." % obj.name)
- caller.location.msg_contents(
- "%s picks up %s." % (caller.name, obj.name), exclude=caller
- )
- # calling at_get hook method
- obj.at_get(caller)
- class CmdCharacter(Command):
- """
- Show character sheet.
- Usage:
- character
- Displays a list of your current ability values.
- """
- key = "character"
- aliases = ["char"]
- lock = "cmd:all()"
- help_category = "General"
- def func(self):
- self.caller.msg(
- '\u250B|w' + " {0:27}".format(self.caller.name,) + '|n\u250B|/')
- self.caller.msg('-HEALTH' + '-' * 23)
- meter = display_meter(self.caller.get_health(), 100,
- empty_color="222", align="center", fill_color=["R"])
- self.caller.msg(meter)
- self.caller.msg('-MANA' + '-' * 25)
- meter = display_meter(self.caller.get_mana(), 100,
- empty_color="222", align="center", fill_color=["B"])
- self.caller.msg(meter + '|/')
- str, agi, intel = self.caller.get_abilities()
- table = evtable.EvTable(border="tablecols")
- table.add_column("STRENGTH", "AGILITY",
- "INTELLECT", header="Attributes")
- table.add_column("|g%03d|n/100" % str, "|g%03d|n/100" %
- agi, "|g%03d|n/100" % intel, header="")
- self.caller.msg(table)
- class CmdLight(Command):
- """
- A command to light objects that support it.
- Usage:
- light <someone>
- Light an object in your inventory.
- """
- key = "light"
- aliases = []
- lock = "cmd:all()"
- help_category = "General"
- arg_regex = r"(?:^(?:\s+|\/).*$)|^$"
- def func(self):
- caller = self.caller
- if not self.args:
- self.msg("Light what?")
- return
- else:
- target = caller.search(self.args)
- if not target:
- self.msg("Light what?")
- return
- if not target.access(caller, 'light'):
- self.msg("You cannot do that.")
- return
- if not has_effect(target, "emit_light"):
- toggle_effect(target, "emit_light")
- self.msg("You light {}.".format(target.name))
- else:
- toggle_effect(target, "emit_light")
- self.msg("You put off {}.".format(target.name))
- if caller.location and inherits_from(caller.location, IndoorRoom):
- caller.location.check_light_state()
- class CmdSearch(Command):
- """
- """
- key = "search"
- aliases = ["ss"]
- locks = "cmd:all()"
- help_category = "General"
- arg_regex = r"(?:^(?:\s+|\/).*$)|^$"
- def func(self):
- caller = self.caller
- if has_effect(caller, "is_busy"):
- caller.msg("You are already busy {}.".format(caller.current_action.busy_msg()))
- return
- if not self.args:
- target = caller.location
- if not target:
- caller.msg("You have no location to search!")
- return
- else:
- target = caller.search(self.args)
- if not target:
- caller.msg("You cannot search {}!".format(self.args))
- return
- if target.access(caller, "search"):
- toggle_effect(caller, "is_busy")
- caller.msg("You search {}.".format(target.get_display_name(caller)))
- action_script = create_script("commands.command.CmdSearchComplete", obj=caller, interval=CMD_SEARCH_TIME, attributes=[("target", self.args)])
- caller.db.current_action = action_script
- else:
- caller.msg("You cannot search {}!".format(target.get_display_name(caller)))
- class CmdSearchComplete(CmdActionScript):
- def at_script_creation(self):
- super().at_script_creation()
- self.key = "cmd_search_complete"
- self.desc = ""
- self.db.target = ""
- def at_repeat(self):
- caller = self.obj
- target_string = self.db.target
- if has_effect(caller, "is_busy"):
- toggle_effect(caller, "is_busy")
- if not target_string:
- target = caller.location
- if not target:
- caller.msg("You have no location to search!")
- return
- else:
- target = caller.search(target_string)
- if not target:
- caller.msg("You cannot search {} anymore!".format(target_string))
- return
- if target.access(caller, "search"):
- items = []
- for con in target.contents:
- if inherits_from(con, Item) and con.access(caller, "get"):
- items.append(con)
- if items:
- found_item_idx = random.randrange(0, len(items))
- found_item = items[found_item_idx]
- found_item_name = found_item.get_numbered_name(1, caller)[0]
- caller.msg("You find {}.".format(found_item_name))
- if not found_item.access(caller, "get"):
- if found_item.db.get_err_msg:
- caller.msg(found_item.db.get_err_msg)
- else:
- caller.msg("You can't get that.")
- return
- # calling at_before_get hook method
- if not found_item.at_before_get(caller):
- return
- success = found_item.move_to(caller, quiet=True)
- if not success:
- caller.msg("This can't be picked up.")
- else:
- caller.msg("You pick up %s." % found_item_name)
- caller.location.msg_contents(
- "%s picks up %s." % (caller.name, found_item_name), exclude=caller
- )
- # calling at_get hook method
- found_item.at_get(caller)
- else:
- caller.msg("There is nothing to be found here.")
- else:
- caller.msg("You cannot search {} anymore!".format(target.get_display_name(caller)))
- def busy_msg(self):
- return "searching {}".format(self.db.target)
- class CmdPut(Command):
- """
- Put an item inside a container
- Usage:
- put <item> in <container>
- """
- key = "put"
- locks = "cmd:all()"
- help_category = "General"
- def parse(self):
- """
- Handle parsing of:
- <item> in <container>
- """
- self.args = args = self.args.strip().lower()
- item_name, container_name = "", ""
- if " in " in args:
- item_name, *rest = args.split(" in ", 1)
- container_name = rest[0] if rest else ""
- self.item_name = item_name.strip()
- self.container_name = container_name.strip()
- def func(self):
- caller = self.caller
- if not self.args or not self.item_name or not self.container_name:
- caller.msg("Usage: put <item> in <container>")
- return
- item = caller.search(self.item_name, typeclass="typeclasses.objects.Item")
- if not item:
- return
- container = caller.search(self.container_name, typeclass="typeclasses.objects.ContainerFeature")
- if not container:
- return
- if not item.access(caller, "get"):
- caller.msg("You cannot do that with {}.".format(item.name))
- return
- if not container.access(caller, "put"):
- caller.msg("You cannot access {}.".format(container.name))
- return
- if has_tag(item, "equipped", "general"):
- caller.msg("{} is equipped. Remove it first.".format(item.name))
- return
- item.move_to(container, use_destination=False)
- caller.msg("You put {} inside {}.".format(item.name, container.name))
- class CmdOpenCloseDoor(Command):
- """
- Open and close a door
- Usage:
- open <door>
- close <door>
- """
- key = "op"
- aliases = ["close"]
- locks = "cmd:all()"
- help_category = "General"
- def func(self):
- if not self.args:
- self.caller.msg("Usage: open||close <door>")
- return
- door = self.caller.search(self.args)
- if not door:
- return
- if not inherits_from(door, BaseDoor):
- self.caller.msg("This is not a door.")
- return
- if self.cmdstring == "op":
- if door.locks.check(self.caller, "traverse"):
- self.caller.msg("%s is already open." % door.key)
- else:
- door.setlock("traverse:true()")
- self.caller.msg("You open %s." % door.key)
- else: # close
- if not door.locks.check(self.caller, "traverse"):
- self.caller.msg("%s is already closed." % door.key)
- else:
- door.setlock("traverse:false()")
- self.caller.msg("You close %s." % door.key)
- class CmdEquip(Command):
- key = "equip"
- aliases = ["eq", "remove"]
- lock = "cmd:all()"
- help_category = "General"
- arg_regex = r"\s.+|$"
- def func(self):
- caller = self.caller
- if not self.args:
- caller.msg("You cannot do that.")
- return
- item = caller.search(self.args, location=caller, nofound_string="")
- if not item:
- caller.msg("You don't have any {}".format(self.args))
- return
- if inherits_from(item, EquippableItem) and item.access(caller, "equip"):
- if self.cmdstring == "remove":
- if has_tag(item, "equipped", "general"):
- if item.at_unequip(caller, ""):
- self.remove(caller, item)
- else:
- return
- else:
- caller.msg("{} is not equipped.".format(item.name))
- return
- else:
- if has_tag(item, "equipped", "general"):
- caller.msg("{} is already equipped.".format(item.name))
- return
- slots = [slot for slot in caller.db.equipment.keys() if item.db.slot in slot]
- if not slots:
- caller.msg("You don't have {} {}".format(indefinite_article(item.db.slot), item.db.slot))
- return
- selected_slot = slots[0]
- # find an empty slot
- empty_slots = [slot for slot in slots if not caller.db.equipment[slot]]
- if empty_slots:
- selected_slot = empty_slots[0]
- # remove equipment if slot is already in use
- if caller.db.equipment[selected_slot]:
- old_equipment = caller.search(caller.db.equipment[selected_slot], location=caller, quiet=True)
- if old_equipment:
- if old_equipment[0].at_unequip(caller, selected_slot):
- self.remove(caller, old_equipment[0])
- else:
- return
- if item.at_equip(caller, selected_slot):
- caller.db.equipment[selected_slot] = item.dbref
- item.tags.add("equipped", category="general")
- caller.msg("You equip |w{}|n on |w{}|n".format(item.name, selected_slot))
- caller.location.msg_contents("{} equips |w{}|n on {}".format(caller.name, item.name, selected_slot), exclude=caller)
- else:
- caller.msg("You cannot equip {}".format(item.name))
- def remove(self, caller, item):
- selected_slot = [slot for slot in caller.db.equipment.keys() if caller.db.equipment[slot] == item.dbref][0]
- caller.msg("You remove {} from {}".format(item, selected_slot))
- caller.db.equipment[selected_slot] = None
- item.tags.remove("equipped", category="general")
- class CmdInventory(Command):
- """
- view inventory
- Usage:
- inventory
- inv
- Shows your inventory.
- """
- key = "inventory"
- aliases = ["inv", "i"]
- locks = "cmd:all()"
- arg_regex = r"$"
- def func(self):
- """check inventory"""
- items = self.caller.contents
- if not items:
- string = "You are not carrying anything."
- else:
- from evennia.utils.ansi import raw as raw_ansi
- table = self.styled_table()
- for item in items:
- effect_string = ""
- if has_tag(item, 'equipped', 'general'):
- effect_string += "|w⚒|n"
- else:
- effect_string += "|=a⚒|n"
- if has_effect(item, 'emit_light'):
- effect_string += "|y☀|n"
- else:
- effect_string += "|=a☀|n"
- table.add_row(
- f"|w{item.name}|n",
- effect_string,
- "{}|n".format(utils.crop(raw_ansi(item.db.desc), width=50) or ""),
- )
- string = f"|wYou are carrying:\n{table}"
- self.caller.msg(string)
- class CmdCast(Command):
- """
- """
- key = "cast"
- aliases = ["cs"]
- lock = "cmd:false()"
- help_category = "General"
- arg_regex = r"\s.+|$"
- def parse(self):
- """
- Handle parsing of:
- ::
- <spell> [at <target>]
- """
- self.args = args = self.args.strip().lower()
- spell_name, target_name = "", ""
- if " at " in args:
- spell_name, *rest = args.split(" at ", 1)
- target_name = rest[0] if rest else ""
- else:
- spell_name = args
- self.spell_name = spell_name.strip()
- self.target_name = target_name.strip()
- def func(self):
- caller = self.caller
- if not self.args or not self.spell_name:
- caller.msg("Usage: cast <spell> [at <target>]")
- return
- spell_id = self.spell_name.replace(' ', '_')
- if self.spell_name not in caller.db.spells:
- caller.msg("You cannot cast {}.".format(self.spell_name))
- return
- if hasattr(spells, "spell_" + spell_id):
- spell = getattr(spells, "spell_" + spell_id)
- else:
- logger.log_err("Cannot find spell {}.".format("spell_" + spell_id))
- return
- spell(caller, self.target_name)
- class CmdTestPy(Command):
- key = "testpy"
- aliases = ["testpy"]
- lock = "cmd:false()"
- help_category = "General"
- arg_regex = r"\s.+|$"
- def func(self):
- caller = self.caller
- pattern = re.compile(r'''((?:[^ "']|"[^"]*"|'[^']*')+)''')
- self.args = pattern.split(self.lhs)[1::2]
- # room = create_room(self.args[0].strip(" \"'"), int(self.args[1].strip(" \"'")), int(self.args[2].strip(" \"'")), self.args[3].strip(" \"'"))
- # caller.msg(room)
- exit = create_exit("exit_empty", caller.location, "north")
- caller.msg(exit)
- # caller.msg(dkmud_oob=({"testarg": "valuetestarg"}))
- # if not self.args:
- # target = caller.location
- # if not target:
- # caller.msg("You have no location to test!")
- # return
- # else:
- # target = caller.search(self.args)
- # if not target:
- # return
- # -------------------------------------------------------------
- #
- # The default commands inherit from
- #
- # evennia.commands.default.muxcommand.MuxCommand.
- #
- # If you want to make sweeping changes to default commands you can
- # uncomment this copy of the MuxCommand parent and add
- #
- # COMMAND_DEFAULT_CLASS = "commands.command.MuxCommand"
- #
- # to your settings file. Be warned that the default commands expect
- # the functionality implemented in the parse() method, so be
- # careful with what you change.
- #
- # -------------------------------------------------------------
- # from evennia.utils import utils
- #
- #
- # class MuxCommand(Command):
- # """
- # This sets up the basis for a MUX command. The idea
- # is that most other Mux-related commands should just
- # inherit from this and don't have to implement much
- # parsing of their own unless they do something particularly
- # advanced.
- #
- # Note that the class's __doc__ string (this text) is
- # used by Evennia to create the automatic help entry for
- # the command, so make sure to document consistently here.
- # """
- # def has_perm(self, srcobj):
- # """
- # This is called by the cmdhandler to determine
- # if srcobj is allowed to execute this command.
- # We just show it here for completeness - we
- # are satisfied using the default check in Command.
- # """
- # return super().has_perm(srcobj)
- #
- # def at_pre_cmd(self):
- # """
- # This hook is called before self.parse() on all commands
- # """
- # pass
- #
- # def at_post_cmd(self):
- # """
- # This hook is called after the command has finished executing
- # (after self.func()).
- # """
- # pass
- #
- # def parse(self):
- # """
- # This method is called by the cmdhandler once the command name
- # has been identified. It creates a new set of member variables
- # that can be later accessed from self.func() (see below)
- #
- # The following variables are available for our use when entering this
- # method (from the command definition, and assigned on the fly by the
- # cmdhandler):
- # self.key - the name of this command ('look')
- # self.aliases - the aliases of this cmd ('l')
- # self.permissions - permission string for this command
- # self.help_category - overall category of command
- #
- # self.caller - the object calling this command
- # self.cmdstring - the actual command name used to call this
- # (this allows you to know which alias was used,
- # for example)
- # self.args - the raw input; everything following self.cmdstring.
- # self.cmdset - the cmdset from which this command was picked. Not
- # often used (useful for commands like 'help' or to
- # list all available commands etc)
- # self.obj - the object on which this command was defined. It is often
- # the same as self.caller.
- #
- # A MUX command has the following possible syntax:
- #
- # name[ with several words][/switch[/switch..]] arg1[,arg2,...] [[=|,] arg[,..]]
- #
- # The 'name[ with several words]' part is already dealt with by the
- # cmdhandler at this point, and stored in self.cmdname (we don't use
- # it here). The rest of the command is stored in self.args, which can
- # start with the switch indicator /.
- #
- # This parser breaks self.args into its constituents and stores them in the
- # following variables:
- # self.switches = [list of /switches (without the /)]
- # self.raw = This is the raw argument input, including switches
- # self.args = This is re-defined to be everything *except* the switches
- # self.lhs = Everything to the left of = (lhs:'left-hand side'). If
- # no = is found, this is identical to self.args.
- # self.rhs: Everything to the right of = (rhs:'right-hand side').
- # If no '=' is found, this is None.
- # self.lhslist - [self.lhs split into a list by comma]
- # self.rhslist - [list of self.rhs split into a list by comma]
- # self.arglist = [list of space-separated args (stripped, including '=' if it exists)]
- #
- # All args and list members are stripped of excess whitespace around the
- # strings, but case is preserved.
- # """
- # raw = self.args
- # args = raw.strip()
- #
- # # split out switches
- # switches = []
- # if args and len(args) > 1 and args[0] == "/":
- # # we have a switch, or a set of switches. These end with a space.
- # switches = args[1:].split(None, 1)
- # if len(switches) > 1:
- # switches, args = switches
- # switches = switches.split('/')
- # else:
- # args = ""
- # switches = switches[0].split('/')
- # arglist = [arg.strip() for arg in args.split()]
- #
- # # check for arg1, arg2, ... = argA, argB, ... constructs
- # lhs, rhs = args, None
- # lhslist, rhslist = [arg.strip() for arg in args.split(',')], []
- # if args and '=' in args:
- # lhs, rhs = [arg.strip() for arg in args.split('=', 1)]
- # lhslist = [arg.strip() for arg in lhs.split(',')]
- # rhslist = [arg.strip() for arg in rhs.split(',')]
- #
- # # save to object properties:
- # self.raw = raw
- # self.switches = switches
- # self.args = args.strip()
- # self.arglist = arglist
- # self.lhs = lhs
- # self.lhslist = lhslist
- # self.rhs = rhs
- # self.rhslist = rhslist
- #
- # # if the class has the account_caller property set on itself, we make
- # # sure that self.caller is always the account if possible. We also create
- # # a special property "character" for the puppeted object, if any. This
- # # is convenient for commands defined on the Account only.
- # if hasattr(self, "account_caller") and self.account_caller:
- # if utils.inherits_from(self.caller, "evennia.objects.objects.DefaultObject"):
- # # caller is an Object/Character
- # self.character = self.caller
- # self.caller = self.caller.account
- # elif utils.inherits_from(self.caller, "evennia.accounts.accounts.DefaultAccount"):
- # # caller was already an Account
- # self.character = self.caller.get_puppet(self.session)
- # else:
- # self.character = None
|