fix wilderness.move_obj logic
This commit is contained in:
parent
1f4cd76e5f
commit
8e15928e7b
2 changed files with 28 additions and 51 deletions
|
|
@ -104,12 +104,16 @@ class TestWilderness(BaseEvenniaTest):
|
||||||
self.assertEqual(len(w.db.unused_rooms), 0)
|
self.assertEqual(len(w.db.unused_rooms), 0)
|
||||||
w.move_obj(self.char1, (0, 0))
|
w.move_obj(self.char1, (0, 0))
|
||||||
self.assertEqual(len(w.db.unused_rooms), 0)
|
self.assertEqual(len(w.db.unused_rooms), 0)
|
||||||
|
# and there should be one used room
|
||||||
|
self.assertEqual(len(w.db.rooms.values()), 1)
|
||||||
|
|
||||||
# And also no unused room after moving the second one in.
|
# And also no unused room after moving the second one in.
|
||||||
w.move_obj(self.char2, (1, 1))
|
w.move_obj(self.char2, (1, 1))
|
||||||
self.assertEqual(len(w.db.unused_rooms), 0)
|
self.assertEqual(len(w.db.unused_rooms), 0)
|
||||||
|
# but now there should be 2 used rooms
|
||||||
|
self.assertEqual(len(w.db.rooms.values()), 2)
|
||||||
|
|
||||||
# But if char2 moves into char1's room, we should have one unused room
|
# Now, if char2 moves into char1's room, we should have one unused room
|
||||||
# Which should be char2's old room that got created.
|
# Which should be char2's old room that got created.
|
||||||
w.move_obj(self.char2, (0, 0))
|
w.move_obj(self.char2, (0, 0))
|
||||||
self.assertEqual(len(w.db.unused_rooms), 1)
|
self.assertEqual(len(w.db.unused_rooms), 1)
|
||||||
|
|
|
||||||
|
|
@ -331,57 +331,30 @@ class WildernessScript(DefaultScript):
|
||||||
# appear in its old room should that room be deleted.
|
# appear in its old room should that room be deleted.
|
||||||
obj.location = None
|
obj.location = None
|
||||||
|
|
||||||
# By default, we'll assume we won't be making a new room and change this flag if necessary.
|
# we will need to do special handling if the old room is a different location
|
||||||
create_room = False
|
# check that here, so we only need to do it once
|
||||||
|
if self == getattr(old_room, 'wilderness', None):
|
||||||
# See if we already have a room for that location
|
# it is our own room
|
||||||
if room := self.db.rooms.get(new_coordinates):
|
from_outside = False
|
||||||
# There is. Try to destroy the old_room if it is not needed anymore
|
|
||||||
self._destroy_room(old_room)
|
|
||||||
else:
|
else:
|
||||||
# There is no room yet at new_location
|
# it's from another wilderness, or no wilderness
|
||||||
# Is the old room in a wilderness?
|
from_outside = True
|
||||||
if hasattr(old_room, "wilderness"):
|
|
||||||
# Yes. Is it in THIS wilderness?
|
|
||||||
if old_room.wilderness == self:
|
|
||||||
# Should we preserve rooms with any objects?
|
|
||||||
if self.preserve_items:
|
|
||||||
# Yes - check if ANY objects besides the exits are in old_room
|
|
||||||
if len(
|
|
||||||
[
|
|
||||||
ob
|
|
||||||
for ob in old_room.contents
|
|
||||||
if not inherits_from(ob, WildernessExit)
|
|
||||||
]
|
|
||||||
):
|
|
||||||
# There is, so we'll create a new room
|
|
||||||
room = self._create_room(new_coordinates, obj)
|
|
||||||
else:
|
|
||||||
# The room is empty, so we'll reuse it
|
|
||||||
room = old_room
|
|
||||||
else:
|
|
||||||
# Only preserve rooms if there are players behind
|
|
||||||
if len([ob for ob in old_room.contents if ob.has_account]):
|
|
||||||
# There is still a player there; create a new room
|
|
||||||
room = self._create_room(new_coordinates, obj)
|
|
||||||
else:
|
|
||||||
# The room is empty of players, so we'll reuse it
|
|
||||||
room = old_room
|
|
||||||
|
|
||||||
# It's in a different wilderness
|
# check if we have a room at the new coordinates already
|
||||||
else:
|
room = self.db.rooms.get(new_coordinates)
|
||||||
# It does, so we make sure to leave the other wilderness properly
|
|
||||||
old_room.wilderness.at_post_object_leave(obj)
|
|
||||||
# We'll also need to create a new room in this wilderness
|
|
||||||
room = self._create_room(new_coordinates, obj)
|
|
||||||
|
|
||||||
else:
|
if not from_outside:
|
||||||
# Obj comes from outside the wilderness entirely
|
# the old room is in the same wilderness
|
||||||
# We need to make a new room
|
# free up the old room if it's no longer needed
|
||||||
room = self._create_room(new_coordinates, obj)
|
self._destroy_room(old_room)
|
||||||
|
|
||||||
# Set `room` to the new coordinates, however it was made
|
if not room:
|
||||||
room.set_active_coordinates(new_coordinates, obj)
|
# we need claim a new room
|
||||||
|
room = self._create_room(new_coordinates, obj)
|
||||||
|
|
||||||
|
# Now that we have a valid room, run the leave hook on the previous location if necessary
|
||||||
|
if from_outside and old_room:
|
||||||
|
old_room.at_object_leave(obj, room)
|
||||||
|
|
||||||
# Put obj back, now in the correct room
|
# Put obj back, now in the correct room
|
||||||
obj.location = room
|
obj.location = room
|
||||||
|
|
@ -430,10 +403,8 @@ class WildernessScript(DefaultScript):
|
||||||
destination=room,
|
destination=room,
|
||||||
report_to=report_to,
|
report_to=report_to,
|
||||||
)
|
)
|
||||||
|
|
||||||
room.ndb.active_coordinates = coordinates
|
|
||||||
room.ndb.wildernessscript = self
|
room.ndb.wildernessscript = self
|
||||||
self.db.rooms[coordinates] = room
|
room.set_active_coordinates(coordinates, report_to)
|
||||||
|
|
||||||
return room
|
return room
|
||||||
|
|
||||||
|
|
@ -474,6 +445,8 @@ class WildernessScript(DefaultScript):
|
||||||
|
|
||||||
# Then delete its coordinate reference
|
# Then delete its coordinate reference
|
||||||
del self.db.rooms[room.ndb.active_coordinates]
|
del self.db.rooms[room.ndb.active_coordinates]
|
||||||
|
# ...on both sides
|
||||||
|
del room.ndb.active_coordinates
|
||||||
# And finally put this room away in storage
|
# And finally put this room away in storage
|
||||||
self.db.unused_rooms.append(room)
|
self.db.unused_rooms.append(room)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue