forked from InspectorCaracal/evennia-things
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroomdecor.py
259 lines (216 loc) · 8.45 KB
/
roomdecor.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
"""
Room Decor Module
Allows you to decorate a room with various objects, which displays
those objects as part of the room's description instead of its
contents on look.
How To Use:
- Copy this file into your gamedir.
- Add 'CmdPlace' to your default CharacterCmdSet
- Create rooms or objects with the classes below to let them
be decoratable or used for decoration.
- Or, use them as parent classes or mixins for your own classes.
Once installed, DecorRooms can be decorated with DecorObjects by using
the `place` command. By default this is available to all players and
decorating permissions are controlled by the `decorate` lockfunc.
Since the decor objects are still in the room, players retain their
ability to look at or otherwise interact with them. Sittable chairs
will still be sittable, and so on.
"""
from evennia import DefaultObject, DefaultRoom
from evennia.utils.utils import list_to_string
from evennia.commands.cmdset import CmdSet
from evennia.commands.default.muxcommand import MuxCommand
from random import randint
import inflect
_INFLECT = inflect.engine()
class DecorObject(DefaultObject):
"""
Base class for any objects that can be used to decorate rooms.
"""
def at_pre_place(self, doer, location):
"""
Access and validity checks before placement
"""
return location.access(doer, "decorate")
def place(self, doer, location, position):
"""
Do the actual placement or replacement.
"""
if self.location != location:
drop = self.move_to(location, quiet=True)
if not drop:
doer.msg("You can't put things there.")
return
# fallback position if none was set
position = "here" if position == True
# set the placement status
if position[-1] in ('.','!','?',',',';',':'):
position = position[:-1]
self.db.placed = position
try:
location.update_decor()
except AttributeError:
self.db.placed = False
doer.msg("This location can't be decorated.")
return
doer.msg(f"You place the {self.get_display_name(self)} {position}.")
def at_pre_get(self, getter, **kwargs):
# if already placed in the room, check decor access
if self.db.placed:
# does getter have decor access for the room
if not self.location.access(getter, "decorate"):
return False
return True
def move_to(self, destination, **kwargs):
"""
Make sure that being moved unsets placement.
"""
location = self.location
success = super().move_to(destination, **kwargs)
if success or self.location == destination:
self.db.placed = False
try:
location.update_decor()
except AttributeError:
pass
return success
class DecorRoom(DefaultRoom):
"""
Rooms that can be decorated with decor objects.
"""
def get_visible_contents(self, looker, **kwargs):
"""
Get all contents of this room that a looker can see. Extends the default
functionality by filtering contents by view access and placement status.
Args:
looker (Object): The entity looking.
**kwargs (any): Passed from `return_appearance`. Unused by default.
Returns:
dict: A dict of lists categorized by type.
"""
def filter_visible(obj_list):
return [obj for obj in obj_list if obj != looker and obj.access(looker, "view") and not obj.db.placed]
return {
"exits": filter_visible(self.contents_get(content_type="exit")),
"characters": filter_visible(self.contents_get(content_type="character")),
"things": filter_visible(self.contents_get(content_type="object")),
}
def return_appearance(self, looker, **kwargs):
"""
Overloads the default `return_appearance` in order to include decor items
separately.
Args:
looker (Object): Object doing the looking.
**kwargs (dict): Optional arguments for other functionality.
This is passed into the helper methods and
into `get_display_name` and `get_desc` calls.
Returns:
str: The description of the room.
"""
if not looker:
return ""
# ourselves
name = self.get_display_name(looker, **kwargs)
desc = self.db.desc
decor = self.db.decor_desc
# contents
content_names_map = self.get_content_names(looker, **kwargs)
exits = list_to_string(content_names_map["exits"])
characters = list_to_string(content_names_map["characters"])
things = list_to_string(content_names_map["things"])
# we want the decor to be the room desc if there is no base desc set
if not desc:
desc = decor
else:
# if there is a desc, append with a linebreak
desc += f"\n{decor}" if decor else ""
# fallback in case it's still empty
if not desc:
desc = "You see nothing special."
# populate the appearance_template string. It's a good idea to strip it and
# let the client add any extra spaces instead.
return self.appearance_template.format(
header="",
name=name,
desc=desc,
exits=f"|wExits:|n {exits}" if exits else "",
characters=f"\n|wCharacters:|n {characters}" if characters else "",
things=f"\n|wYou see:|n {things}" if things else "",
footer="",
).strip()
def update_decor(self):
"""
Re-processes all placed decor objects to generate their description.
"""
decor_descs = []
placements = {}
# get a list of all objects that have been placed as decor
decor_list = [obj for obj in self.contents_get(content_type="object") if obj.db.placed]
# group decor by position
for decor in decor_list:
if decor.db.placed in placements.keys():
placements[decor.db.placed].append(_INFLECT.an(decor.get_display_name(self)))
else:
placements[decor.db.placed] = [_INFLECT.an(decor.get_display_name(self))]
for position, names_list in placements.items():
verb = "is" if len(names_list) == 1 else "are"
names = list_to_string(names_list)
# liven up the decor desc a bit by occasionally swapping the name/position order
if randint(1,4) == 1:
desc = f"{position} {verb} {names}."
else:
desc = f"{names} {verb} {position}."
# capitalize sentences
desc = desc[0].upper() + desc[1:]
decor_descs.append(desc)
# set the decor description
self.db.decor_desc = " ".join(decor_descs)
class CmdPlace(MuxCommand):
"""
Decorate a room with an object
Usage:
place <obj> [= position]
Examples:
place painting
place painting = hanging from the south wall
Placed objects will appear as part of the room's description instead of
its contents.
"""
key = "place"
aliases = ("arrange",)
help_category = "General"
def func(self):
"""
This performs the actual command.
"""
caller = self.caller
if not self.args:
caller.msg("Usage: place <obj> [= position]")
return
obj = caller.search(self.lhs.strip())
if not obj:
return
try:
allow = obj.at_pre_place(caller, caller.location)
except AttributeError:
caller.msg("You can't decorate with that.")
return
if not allow:
caller.msg("You can't decorate here.")
return
# set a positional string, or just put in the room
if self.rhs:
position = self.rhs.strip()
else:
position = True
# This is where you can validate positional descriptions
if len(self.rhs) > 50:
caller.msg("Please keep your positional description below 50 characters.")
# do the actual placement
obj.place(caller, caller.location, position)
class DecorCmdSet(CmdSet):
"""
Groups the extended basic commands.
"""
def at_cmdset_creation(self):
self.add(CmdPlace)