Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.01 KB | None | 0 0
  1. '''
  2. petdata.py by Stendec
  3. Revision 1
  4.  
  5. A system for associating mobs with a character, so that they're loaded and
  6. unloaded along with that character. This can be used for making persistent pets
  7. or other similar situations.
  8.  
  9. And yes, this is very, very hacky.
  10.  
  11. To use, just access ch.pets. For example, if the player is stored in ch, and
  12. you want to create a new dog@examples mob to be their pet, do the following:
  13.  
  14.  
  15. mob = char.load_mob("dog@examples", ch.room)
  16. ch.pets += mob
  17.  
  18.  
  19. Just keep in mind that this doesn't do any following or anything like that. It
  20. ONLY keeps track of characters attached to a player, for saving and loading
  21. purposes.
  22.  
  23. '''
  24.  
  25. import auxiliary, char, hooks, mud, mudsys, room, storage, obj as mudobj
  26.  
  27. ################################################################################
  28. ## Pet Storage Auxiliary Class
  29. ################################################################################
  30.  
  31. class PetStorage:
  32.     def __init__(s, set=None):
  33.         s.pets = []
  34.         s.petsets = []
  35.         s.owner = None
  36.        
  37.         # If there's a storage set provided, load the pets.
  38.         if set and set.contains("list"):
  39.             for st in set.readList("list").sets():
  40.                 rm = room.get_room(st.readString("room"))
  41.                
  42.                 # Create a character from the pet.
  43.                 c = char.read(st.readSet("char"))
  44.                 s.append(c)
  45.                
  46.                 # Try loading inventory
  47.                 if st.contains('inv'):
  48.                     for objset in st.readList('inv').sets():
  49.                         obj = mudobj.read(objset)
  50.                         obj.carrier = c
  51.                 else:
  52.                     mud.log_string("No inv for %s" % c.name)
  53.                
  54.                 if st.contains('eq'):
  55.                     for objset in st.readList('eq').sets():
  56.                         obj = mudobj.read(objset)
  57.                         obj.carrier = c
  58.                         c.equip(obj)
  59.                 else:
  60.                     mud.log_string("No eq for %s" % c.name)
  61.                
  62.                 # Try moving to room.
  63.                 if rm:
  64.                     c.room = rm
  65.    
  66.     def copyTo(s, to):
  67.         to.pets[:] = [ch for ch in s.pets]
  68.         return to
  69.    
  70.     def copy(s):
  71.         return s.copyTo(PetStorage())
  72.    
  73.     def store(s):
  74.         set = storage.StorageSet()
  75.         l = storage.StorageList()
  76.         for ch in s.pets:
  77.             st = storage.StorageSet()
  78.             try:
  79.                 st.storeSet("char",ch.store())
  80.                 if ch.room:
  81.                     st.storeString("room", ch.room.proto)
  82.                 else:
  83.                     st.storeString("room","")
  84.                
  85.                 inv = storage.StorageList()
  86.                 for obj in ch.inv:
  87.                     inv.add(obj.store())
  88.                 st.storeList("inv", inv)
  89.                
  90.                 eq = storage.StorageList()
  91.                 for obj in ch.eq:
  92.                     eq.add(obj.store())
  93.                 st.storeList("eq", eq)
  94.                
  95.             except: continue
  96.             if st:
  97.                 l.add(st)
  98.        
  99.         # Add all petsets too.
  100.         for st in s.petsets:
  101.             l.add(st)
  102.        
  103.         set.storeList("list", l)
  104.         return set
  105.    
  106.     def __delitem__(*args,**kwargs):
  107.         raise NotImplementedError
  108.     __delattr__ = __delitem__
  109.     __delslice__ = __delitem__
  110.     __reduce__ = __delitem__
  111.     __reduce_ex__ = __delitem__
  112.    
  113.     def __iadd__(s, other):
  114.         ''' Add a ch to pets with +=. '''
  115.         if type(other) not in (list,char.Char):
  116.             raise TypeError, "can only add list or Char to pet list."
  117.        
  118.         if type(other) is list:
  119.             for ch in other:
  120.                 if type(ch) is char.Char:
  121.                     s.append(ch)
  122.         else:
  123.             # Just add the character.
  124.             s.append(other)
  125.    
  126.     def __isub__(s, other):
  127.         ''' Remove a ch from pets with -=. '''
  128.         if type(other) not in (list,char.Char):
  129.             raise TypeError, "can only remove list or Char from pet list."
  130.        
  131.         if type(other) is list:
  132.             for ch in other:
  133.                 s.remove(ch)
  134.         else:
  135.             s.remove(other)
  136.    
  137.     def append(s, ch):
  138.         ''' Register a character as the belonging to another character. '''
  139.         if not type(ch) is char.Char:
  140.             raise TypeError, "can only add Char to pet list."
  141.        
  142.         if not ch.is_npc:
  143.             raise TypeError, "can only add NPCs to a pet list."
  144.        
  145.         # Don't add if we're already here.
  146.         if ch in s.pets: return
  147.        
  148.         # If there's a previous owner, remove from it.
  149.         if ch.pets.owner: ch.pets.owner.remove(ch)
  150.        
  151.         # Set the owner and add
  152.         ch.pets.owner = s
  153.         s.pets.append(ch)
  154.    
  155.     def extend(s, iterable):
  156.         for ch in iterable:
  157.             s.append(ch)
  158.    
  159.     def insert(s, index, ch):
  160.         ''' Insert a character at a given index in the list. '''
  161.         if not type(ch) is char.Char:
  162.             raise TypeError, "can only add Char to pet list."
  163.        
  164.         # Don't add if we're already here.
  165.         if ch in s.pets: return
  166.        
  167.         # If there's a previous owner, remove from it.
  168.         if ch.pets.owner: ch.pets.owner.remove(ch)
  169.        
  170.         # Set the owner and add
  171.         ch.pets.owner = s
  172.         s.pets.insert(index,ch)
  173.    
  174.     def remove(s, ch):
  175.         if type(ch) is char.Char and ch.pets.owner is s: ch.pets.owner = None
  176.         s.pets.remove(ch)
  177.    
  178.     def clear(s):
  179.         for ch in s.pets[:]:
  180.             s.remove(ch)
  181.    
  182.     def __getattr__(s, key):
  183.         return getattr(s.pets, key)
  184.  
  185. ################################################################################
  186. ## The Property for Accessing It
  187. ################################################################################
  188.  
  189. def __getpetstore(ch): return ch.aux("petstore")
  190. def __setpetstore(ch,v): pass
  191. def __delpetstore(ch): ch.aux("petstore").clear()
  192.  
  193. ################################################################################
  194. ## The Hooks for Unloading Characters
  195. ################################################################################
  196.  
  197. def char_from_game(info):
  198.     ''' Unload all a character's pets along with that character. Yes, this is
  199.        repetitive and I'm pretty sure it's completely pointless, since store is
  200.        called first. Still, here it is. It prevents memory leaks and stuff. '''
  201.     ch, = hooks.parse_info(info)
  202.    
  203.     # Make sure we aren't referenced by our owner still.
  204.     if ch.pets.owner:
  205.         ch.pets.owner.remove(ch)
  206.    
  207.     for c in ch.pets.pets[:]:
  208.         try:
  209.             s = storage.StorageSet()
  210.             s.storeSet("char", c.store())
  211.             if c.room:
  212.                 s.storeString("room", c.room.proto)
  213.             else:
  214.                 s.storeString("room","")
  215.            
  216.             inv = storage.StorageList()
  217.             for obj in c.inv:
  218.                 inv.add(obj.store())
  219.             s.storeList("inv", inv)
  220.            
  221.             eq = storage.StorageList()
  222.             for obj in c.eq:
  223.                 eq.add(obj.store())
  224.             s.storeList("eq", eq)
  225.            
  226.             c.pets.owner = None
  227.             ch.pets.pets.remove(c)
  228.             mud.extract(c)
  229.         except:
  230.             mud.log_string('Cant save/extract: %s' % c.name)
  231.             continue
  232.         if s:
  233.             ch.pets.petsets.append(s)
  234.    
  235. ################################################################################
  236. ## Initialization
  237. ################################################################################
  238. auxiliary.install("petstore", PetStorage, "character")
  239. mudsys.add_char_method("pets",property(__getpetstore,__setpetstore,__delpetstore,
  240.     doc="Gets or sets the 'pets' of a given character."))
  241.  
  242. hooks.add("char_from_game", char_from_game)
  243.  
  244. def __unload__():
  245.     hooks.remove("char_from_game", char_from_game)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement