Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- petdata.py by Stendec
- Revision 1
- A system for associating mobs with a character, so that they're loaded and
- unloaded along with that character. This can be used for making persistent pets
- or other similar situations.
- And yes, this is very, very hacky.
- To use, just access ch.pets. For example, if the player is stored in ch, and
- you want to create a new dog@examples mob to be their pet, do the following:
- mob = char.load_mob("dog@examples", ch.room)
- ch.pets += mob
- Just keep in mind that this doesn't do any following or anything like that. It
- ONLY keeps track of characters attached to a player, for saving and loading
- purposes.
- '''
- import auxiliary, char, hooks, mud, mudsys, room, storage, obj as mudobj
- ################################################################################
- ## Pet Storage Auxiliary Class
- ################################################################################
- class PetStorage:
- def __init__(s, set=None):
- s.pets = []
- s.petsets = []
- s.owner = None
- # If there's a storage set provided, load the pets.
- if set and set.contains("list"):
- for st in set.readList("list").sets():
- rm = room.get_room(st.readString("room"))
- # Create a character from the pet.
- c = char.read(st.readSet("char"))
- s.append(c)
- # Try loading inventory
- if st.contains('inv'):
- for objset in st.readList('inv').sets():
- obj = mudobj.read(objset)
- obj.carrier = c
- else:
- mud.log_string("No inv for %s" % c.name)
- if st.contains('eq'):
- for objset in st.readList('eq').sets():
- obj = mudobj.read(objset)
- obj.carrier = c
- c.equip(obj)
- else:
- mud.log_string("No eq for %s" % c.name)
- # Try moving to room.
- if rm:
- c.room = rm
- def copyTo(s, to):
- to.pets[:] = [ch for ch in s.pets]
- return to
- def copy(s):
- return s.copyTo(PetStorage())
- def store(s):
- set = storage.StorageSet()
- l = storage.StorageList()
- for ch in s.pets:
- st = storage.StorageSet()
- try:
- st.storeSet("char",ch.store())
- if ch.room:
- st.storeString("room", ch.room.proto)
- else:
- st.storeString("room","")
- inv = storage.StorageList()
- for obj in ch.inv:
- inv.add(obj.store())
- st.storeList("inv", inv)
- eq = storage.StorageList()
- for obj in ch.eq:
- eq.add(obj.store())
- st.storeList("eq", eq)
- except: continue
- if st:
- l.add(st)
- # Add all petsets too.
- for st in s.petsets:
- l.add(st)
- set.storeList("list", l)
- return set
- def __delitem__(*args,**kwargs):
- raise NotImplementedError
- __delattr__ = __delitem__
- __delslice__ = __delitem__
- __reduce__ = __delitem__
- __reduce_ex__ = __delitem__
- def __iadd__(s, other):
- ''' Add a ch to pets with +=. '''
- if type(other) not in (list,char.Char):
- raise TypeError, "can only add list or Char to pet list."
- if type(other) is list:
- for ch in other:
- if type(ch) is char.Char:
- s.append(ch)
- else:
- # Just add the character.
- s.append(other)
- def __isub__(s, other):
- ''' Remove a ch from pets with -=. '''
- if type(other) not in (list,char.Char):
- raise TypeError, "can only remove list or Char from pet list."
- if type(other) is list:
- for ch in other:
- s.remove(ch)
- else:
- s.remove(other)
- def append(s, ch):
- ''' Register a character as the belonging to another character. '''
- if not type(ch) is char.Char:
- raise TypeError, "can only add Char to pet list."
- if not ch.is_npc:
- raise TypeError, "can only add NPCs to a pet list."
- # Don't add if we're already here.
- if ch in s.pets: return
- # If there's a previous owner, remove from it.
- if ch.pets.owner: ch.pets.owner.remove(ch)
- # Set the owner and add
- ch.pets.owner = s
- s.pets.append(ch)
- def extend(s, iterable):
- for ch in iterable:
- s.append(ch)
- def insert(s, index, ch):
- ''' Insert a character at a given index in the list. '''
- if not type(ch) is char.Char:
- raise TypeError, "can only add Char to pet list."
- # Don't add if we're already here.
- if ch in s.pets: return
- # If there's a previous owner, remove from it.
- if ch.pets.owner: ch.pets.owner.remove(ch)
- # Set the owner and add
- ch.pets.owner = s
- s.pets.insert(index,ch)
- def remove(s, ch):
- if type(ch) is char.Char and ch.pets.owner is s: ch.pets.owner = None
- s.pets.remove(ch)
- def clear(s):
- for ch in s.pets[:]:
- s.remove(ch)
- def __getattr__(s, key):
- return getattr(s.pets, key)
- ################################################################################
- ## The Property for Accessing It
- ################################################################################
- def __getpetstore(ch): return ch.aux("petstore")
- def __setpetstore(ch,v): pass
- def __delpetstore(ch): ch.aux("petstore").clear()
- ################################################################################
- ## The Hooks for Unloading Characters
- ################################################################################
- def char_from_game(info):
- ''' Unload all a character's pets along with that character. Yes, this is
- repetitive and I'm pretty sure it's completely pointless, since store is
- called first. Still, here it is. It prevents memory leaks and stuff. '''
- ch, = hooks.parse_info(info)
- # Make sure we aren't referenced by our owner still.
- if ch.pets.owner:
- ch.pets.owner.remove(ch)
- for c in ch.pets.pets[:]:
- try:
- s = storage.StorageSet()
- s.storeSet("char", c.store())
- if c.room:
- s.storeString("room", c.room.proto)
- else:
- s.storeString("room","")
- inv = storage.StorageList()
- for obj in c.inv:
- inv.add(obj.store())
- s.storeList("inv", inv)
- eq = storage.StorageList()
- for obj in c.eq:
- eq.add(obj.store())
- s.storeList("eq", eq)
- c.pets.owner = None
- ch.pets.pets.remove(c)
- mud.extract(c)
- except:
- mud.log_string('Cant save/extract: %s' % c.name)
- continue
- if s:
- ch.pets.petsets.append(s)
- ################################################################################
- ## Initialization
- ################################################################################
- auxiliary.install("petstore", PetStorage, "character")
- mudsys.add_char_method("pets",property(__getpetstore,__setpetstore,__delpetstore,
- doc="Gets or sets the 'pets' of a given character."))
- hooks.add("char_from_game", char_from_game)
- def __unload__():
- hooks.remove("char_from_game", char_from_game)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement