-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic_commands.py
274 lines (221 loc) · 7.23 KB
/
basic_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from evennia import CmdSet
from evennia.utils import list_to_string
from evennia.commands.default.muxcommand import MuxCommand
import inflect
_INFLECT = inflect.engine()
# Custom Look command expanding the look syntax to view objects contained
# within other objects, with natural language syntax.
class CmdLook(MuxCommand):
"""
look
Usage:
look
look <obj>
look in <obj>
look <container>'s <obj>
look <obj> in <container>
Observes your location or objects in your vicinity.
"""
key = "look"
aliases = ["l", "look at"]
locks = "cmd:all()"
arg_regex = r"\s|$"
rhs_split = (" in ", " on ")
def func(self):
"""
Handle the looking
"""
caller = self.caller
location = caller.location
if not self.args:
target = location
if not target:
caller.msg("You have no location to look at.")
return
self.msg((caller.at_look(target), {"type": "look"}), options=None)
return
# parse for possessive 's
if not self.rhs and ("'s " in self.args):
# split at the first possessive and swap
self.rhs, self.lhs = self.args.strip().split("'s ", maxsplit=1)
# at this point, `lhs` is the target and `rhs` is the container
holder = None
target = None
look_in = False
# if there's a rhs, get the container and its access
if self.rhs:
holder = caller.search(self.rhs)
if not holder:
return
if not holder.access(caller, "viewcon") and not holder.access(caller, "getfrom"):
self.msg("You can't look there.")
return
# if there's a lhs, get the target
if self.lhs:
candidates = holder.contents if holder else caller.contents + location.contents
target = caller.search(self.lhs, candidates=candidates)
if not target:
return
# at this point, all needed objects have been found
# if "target" isn't specified, the container IS the target
if holder and not target:
look_in = True
target = holder
self.msg((caller.at_look(target, look_in=look_in), {"type": "look"}), options=None)
# Custom Get command allowing you to get objects from within other objects.
class CmdGet(MuxCommand):
"""
pick up something
Usage:
get <obj>
get <obj> from <obj>
Picks up an object from your location or another object you have permission
to get (or get from) and puts it in your inventory.
"""
key = "get"
aliases = "grab"
locks = "cmd:all()"
arg_regex = r"\s|$"
rhs_split = (" from ",)
def func(self):
"""implements the command."""
caller = self.caller
if not self.args:
caller.msg("Get what?")
return
if self.rhs:
holder = caller.search(self.rhs)
if not holder:
return
if not holder.access(caller, "getfrom"):
self.msg("You can't get things from there.")
return
else:
holder = None
# add support for a csl here
if holder:
obj = caller.search(self.lhs, holder.contents)
else:
obj = caller.search(self.lhs)
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:
if obj.db.get_err_msg:
caller.msg(obj.db.get_err_msg)
else:
caller.msg("This can't be picked up.")
return
if holder:
caller.location.msg_contents("gets %s from %s." % (_INFLECT.an(obj.name), _INFLECT.an(holder.name)))
else:
caller.location.msg_contents("picks up %s." % _INFLECT.an(obj.name))
# calling at_get hook method
obj.at_get(caller)
class CmdPut(MuxCommand):
"""
put something on something else
Usage:
put <obj> on <obj>
put <obj> in <obj>
Lets you place an object in your inventory into (or onto)
another object.
"""
key = "put"
# aliases = "place"
locks = "cmd:all()"
arg_regex = r"\s|$"
def func(self):
"""Implement command"""
caller = self.caller
if not self.args:
caller.msg("Put down what?")
return
target = None
# remember command split syntax
if " on " in self.args:
self.lhs, self.rhs = self.args.strip().split(" on ", maxsplit=1)
syntax = "on"
elif " in " in self.args:
self.lhs, self.rhs = self.args.strip().split(" in ", maxsplit=1)
syntax = "in"
else:
# "put" requires two arguments
caller.msg("Put it where?")
return
# add support for a csl here
obj = caller.search(self.lhs, location=caller)
if not obj:
return
# Call the object script's at_before_drop() method.
if not obj.at_before_drop(caller):
return
target = caller.search(self.rhs)
if not target:
return
if not target.access(caller, 'getfrom'):
caller.msg("You can't put things there.")
return
success = obj.move_to(target, quiet=True)
if not success:
caller.msg("This couldn't be put down.")
return
caller.location.msg_contents("puts %s %s %s." % (_INFLECT.an(obj.name), syntax, _INFLECT.an(target.name)))
# Call the object script's at_drop() method.
obj.at_drop(caller)
class CmdUse(MuxCommand):
"""
use something
Usage:
use <obj>
use <obj> on <target>
Lets you use a particular item, for whatever its intended purpose is.
"""
key = "use"
locks = "cmd:all()"
rhs_split = (" on ", "=")
arg_regex = r"\s|$"
def func(self):
caller = self.caller
if not self.args:
caller.msg("Use what?")
return
obj = caller.search(self.lhs)
if not obj:
return
if self.rhs:
target = caller.search(self.rhs)
if not target:
return
else:
target = None
# Call the object script's at_before_use() method.
try:
if not obj.at_before_use(caller, target=target):
return
except AttributeError:
caller.msg("That is not usable.")
return
obj.at_use(caller, target=target)
# CmdSet for extended basic commands
class BasicsCmdSet(CmdSet):
"""
Groups the extended basic commands.
"""
def at_cmdset_creation(self):
self.add(CmdLook)
self.add(CmdGet)
self.add(CmdPut)
self.add(CmdUse)