inform.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """
  2. inform.py
  3. Python's mirror of C's inform.c -- contains various functions that perform
  4. informative duties. Examining places/things, displaying proper names, etc...
  5. """
  6. import utils, char, hooks, mudsock, mud, string, display
  7. ################################################################################
  8. # utility functions
  9. ################################################################################
  10. def build_who(ch = None):
  11. '''returns a formatted list of all the people currently online'''
  12. buf = [ ]
  13. buf.append(display.seperator)
  14. # build character info
  15. count = len(mudsock.socket_list())
  16. playing = 0
  17. for sock in mudsock.socket_list():
  18. if not (sock.ch == None or sock.ch.room == None):
  19. desc = sock.ch.name
  20. if ch != None:
  21. desc = ch.see_as(sock.ch)
  22. buf.append(" %-12s %-10s %53s" %
  23. (desc,sock.ch.race,sock.ch.user_groups))
  24. playing = playing + 1
  25. conn_end = "s"
  26. if count == 1: conn_end = ""
  27. play_end = "s"
  28. if playing == 1: play_end = ""
  29. # build our footer
  30. buf.append(display.seperator)
  31. buf.append((" %d socket" % count) + conn_end + " logged in." + (" %d player" % playing) + play_end + " currently playing.")
  32. buf.append(display.seperator)
  33. buf.append("")
  34. return "\r\n".join(buf)
  35. ################################################################################
  36. # look append functions
  37. ################################################################################
  38. def show_equipment(ch, tgt):
  39. '''shows ch tgt\'s equipment'''
  40. for obj in tgt.eq:
  41. if ch.cansee(obj) or ch == tgt:
  42. ch.send(" %-30s %s" % (obj.name, tgt.get_slots(obj)))
  43. cardinal_dirs = ["north", "south", "east", "west"]
  44. compass_dirs = ["north", "south", "east", "west",
  45. "northwest", "northeast", "southwest", "southeast"]
  46. def list_one_exit(ch, ex, dir):
  47. builder_info = ""
  48. if ch.isInGroup("builder"):
  49. builder_info = " [" + ex.dest.proto + "]"
  50. # display the direction we can go
  51. ch.send(" {n- %-10s :: %s%s" % (dir, ch.see_as(ex), builder_info))
  52. def list_room_exits(ch, room, filter_compass = False):
  53. # first, go through our standard exits
  54. if not filter_compass:
  55. for dir in compass_dirs:
  56. ex = room.exit(dir)
  57. if ex == None:
  58. continue
  59. if ex.dest == None:
  60. mud.log_string("ERROR: room %s headed %s to %s, which does not exist."%\
  61. room.proto, dir, ex.destproto)
  62. elif ch.cansee(ex):
  63. list_one_exit(ch, ex, dir)
  64. # now do special exits
  65. for dir in room.exnames:
  66. if not dir in compass_dirs:
  67. ex = room.exit(dir)
  68. if ex.dest == None:
  69. mud.log_string("ERROR: room %s headed %s to %s, which does not exist." % \
  70. room.proto, dir, ex.destproto)
  71. elif ch.cansee(ex):
  72. list_one_exit(ch, ex, dir)
  73. def list_one_furniture(ch, obj):
  74. '''list the contents of a piece of furniture to the character.'''
  75. sitters = obj.chars
  76. am_on = ch in sitters
  77. if am_on:
  78. sitters.remove(ch)
  79. # is it just us now?
  80. if len(sitters) == 0:
  81. ch.send(obj.rdesc)
  82. else:
  83. # build display info
  84. onat = obj.furniture_type
  85. isare = "is"
  86. if len(sitters) > 1:
  87. isare = "are"
  88. wth = ""
  89. if am_on:
  90. wth = " with you"
  91. sitter_descs = utils.build_show_list(ch, sitters,
  92. lambda x: x.name,
  93. lambda x: x.mname, ", ", True)
  94. ch.send("%s %s %s %s%s." % (sitter_descs, isare, onat, obj.name, wth))
  95. def list_room_contents(ch, room):
  96. # find our list of visible objects and characters
  97. vis_objs = utils.find_all_objs(ch, room.objs, "", None, True)
  98. vis_chars = utils.find_all_chars(ch, room.chars, "", None, True)
  99. # lists of used furnture, and characters using furniture
  100. furniture = [ ]
  101. # find our list of used furniture
  102. for obj in vis_objs:
  103. if len(obj.chars) > 0:
  104. furniture.append(obj)
  105. # now remove our used furniture and people using it from the visible lists
  106. for furn in furniture:
  107. vis_objs.remove(furn)
  108. for pers in furn.chars:
  109. vis_chars.remove(pers)
  110. # show our list of visible characters
  111. if ch in vis_chars:
  112. vis_chars.remove(ch)
  113. if len(vis_chars) > 0:
  114. utils.show_list(ch, vis_chars, lambda(x): x.rdesc, lambda(x): x.mdesc)
  115. # show our list of used furniture
  116. for furn in furniture:
  117. list_one_furniture(ch, furn)
  118. # show our list of visible objects
  119. if len(vis_objs) > 0:
  120. utils.show_list(ch, vis_objs, lambda(x): x.rdesc, lambda(x): x.mdesc)
  121. ################################################################################
  122. # hooks
  123. ################################################################################
  124. def equipment_look_hook(info):
  125. '''displays a character\'s equipment when looked at'''
  126. tgt, ch = hooks.parse_info(info)
  127. if ch != tgt:
  128. gndr = tgt.heshe
  129. act = "is"
  130. else:
  131. gndr = "You"
  132. act = "are"
  133. if len(tgt.eq) > 0:
  134. ch.send("\n" + gndr + " " + act + " wearing:")
  135. show_equipment(ch, tgt)
  136. def room_look_hook(info):
  137. '''diplays info about the room contents'''
  138. room, ch = hooks.parse_info(info)
  139. list_room_exits(ch, room)
  140. list_room_contents(ch, room)
  141. def exit_look_hook(info):
  142. ex, ch = hooks.parse_info(info)
  143. if not ex.is_closed and ch.cansee(ex) and ex.dest != None:
  144. list_room_contents(ch, ex.dest)
  145. ################################################################################
  146. # add our hooks
  147. ################################################################################
  148. hooks.add("look_at_char", equipment_look_hook)
  149. hooks.add("look_at_room", room_look_hook)
  150. hooks.add("look_at_exit", exit_look_hook)
  151. ################################################################################
  152. # unload procedure
  153. ################################################################################
  154. def __unload__():
  155. '''things that need to be detached when the module is un/reloaded'''
  156. hooks.remove("look_at_char", equipment_look_hook)
  157. hooks.remove("look_at_room", room_look_hook)
  158. hooks.remove("look_at_exit", exit_look_hook)