movement.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. '''
  2. movement.py
  3. all of the functions concerned with movement and position change
  4. '''
  5. import inform, hooks, mudsys, mud
  6. # a ranking of positions
  7. positions = ["unconscious", "sleeping", "sitting", "standing", "flying"]
  8. pos_act = ["collapse", "sleep", "sit", "stand", "fly"]
  9. # stuff for handling movement
  10. dir_name = ["north", "east", "south", "west", "northeast", "northwest",
  11. "southwest", "southeast", "up", "down"]
  12. dir_abbr = ["n", "e", "s", "w", "ne", "nw", "sw", "se", "u", "d"]
  13. dir_opp = [ 2, 3, 0, 1, 6, 7, 4, 5, 9, 8]
  14. def try_use_furniture(ch, obj, pos):
  15. '''attempts to resituate a person on the piece of furniture'''
  16. # are we already on it?
  17. if ch.on == obj:
  18. ch.send("You are already " +ch.on.furniture_type+ " " +ch.on.name+ ".")
  19. # make sure we have room
  20. elif obj.furniture_capacity <= len(obj.chars):
  21. ch.send("There isn't any room left.")
  22. else:
  23. # are we already on something? get up first
  24. if ch.on:
  25. mud.message(ch,None,ch.on,None,True,"to_char",
  26. "You stand up from $o.")
  27. mud.message(ch,None,ch.on,None,True,"to_room",
  28. "$n stands up from $o.")
  29. ch.on = None
  30. # send our mud.messages for sitting down
  31. act = pos_act[positions.index(pos)]
  32. mud.message(ch, None, obj, None, True,
  33. "to_char", "You " + act + " " + obj.furniture_type + " $o.")
  34. mud.message(ch, None, obj, None, True,
  35. "to_room", "$n " + act + " " + obj.furniture_type + " $o.")
  36. # place ourselves down on our new furniture
  37. ch.on = obj
  38. ch.pos = pos
  39. return True
  40. # we didn't manage to get on the furniture
  41. return False
  42. def try_change_pos(ch, pos):
  43. '''this function attempts to change the position of the person'''
  44. if ch.pos == pos:
  45. ch.send("You are already " + pos + ".")
  46. return False
  47. else:
  48. if ch.pos == "flying":
  49. mud.message(ch,None,None,None,True, "to_char", "You stop flying.")
  50. mud.message(ch,None,None,None,True, "to_room", "$n stops flying.")
  51. act = pos_act[positions.index(pos)]
  52. mud.message(ch, None, None, None, True, "to_char", "You " + act + ".")
  53. mud.message(ch, None, None, None, True, "to_room", "$n " + act + "s.")
  54. ch.pos = pos
  55. return True
  56. def cmd_sit(ch, cmd, arg):
  57. '''If standing, attempts to sit on the ground.'''
  58. try:
  59. obj, = mud.parse_args(ch, True, cmd, arg, "| [on] obj.room")
  60. except: return
  61. if obj == None:
  62. try_change_pos(ch, "sitting")
  63. elif obj.istype("furniture"):
  64. try_use_furniture(ch, obj, "sitting")
  65. else:
  66. ch.send("You cannot sit on " + ch.see_as(obj) + ".")
  67. def cmd_sleep(ch, cmd, arg):
  68. '''If awake, attempts to lay down and sleep.'''
  69. try:
  70. obj, = mud.parse_args(ch, True, cmd, arg, "| [on] obj.room")
  71. except: return
  72. if obj == None:
  73. try_change_pos(ch, "sleeping")
  74. elif obj.istype("furniture"):
  75. try_use_furniture(ch, obj, "sleeping")
  76. else:
  77. ch.send("You cannot sleep on " + ch.see_as(obj) + ".")
  78. def cmd_stand(ch, cmd, arg):
  79. '''If sitting, attempts to stand. If flying, attempts to land.'''
  80. try_change_pos(ch, "standing")
  81. def cmd_wake(ch, cmd, arg):
  82. '''If sleep, attempts to wake up and sit.'''
  83. mud.message(ch,None,None,None,True, "to_char",
  84. "You stop sleeping and sit up.")
  85. mud.message(ch,None,None,None,True, "to_room",
  86. "$n stops sleeping and sits up.")
  87. ch.pos = "sitting"
  88. def dir_opposite(dir):
  89. '''returns the opposite direction of the specified one, or None if none.'''
  90. try:
  91. return dir_name[dir_opp[dir_index(dir)]]
  92. except: return None
  93. def dir_index(dir):
  94. '''returns the index of the direction name'''
  95. try:
  96. return dir_name.index(dir)
  97. except: pass
  98. try:
  99. return dir_abbr.index(dir)
  100. except: pass
  101. return -1
  102. def try_move_mssg(ch, dir):
  103. '''Handles all moving of characters from one room to another, through
  104. commands. Attempts a move. If successful, returns the exit left through.
  105. Informs people of our moving'''
  106. ex, success = try_move(ch, dir, True)
  107. return ex
  108. def try_move(ch, dir, mssg = False):
  109. '''Handles all moving of characters from one room to another, through
  110. commands. Attempts a move. If successful, returns the exit left
  111. through.'''
  112. ex = ch.room.exit(dir)
  113. success = False
  114. exname = "it"
  115. if ex != None and ex.name != "":
  116. exname = ex.name
  117. # did we find an exit?
  118. if ex == None or not ch.cansee(ex):
  119. ch.send("Alas, there is no exit in that direction.")
  120. elif ex.is_closed:
  121. ch.send("You will have to open " + exname + " first.")
  122. elif ex.dest == None:
  123. ch.send("It doesn't look like " + exname + " leads anywhere!")
  124. else:
  125. old_room = ch.room
  126. dirnum = dir_index(dir)
  127. # send out our leave mud.messages as needed. Is anyone in the old room?
  128. if mssg == True:
  129. if ex.leave_mssg != '':
  130. mud.message(ch, None, None, None, True, "to_room",ex.leave_mssg)
  131. elif dirnum == -1:
  132. mud.message(ch, None, None, None, True, "to_room", "$n leaves.")
  133. else:
  134. mud.message(ch, None, None, None, True, "to_room",
  135. "$n leaves " + dir_name[dirnum] + ".")
  136. # run our leave hooks
  137. hooks.run("exit", hooks.build_info("ch rm ex", (ch, ch.room, ex)))
  138. # if a hook hasn't moved us, go through with going through the exit
  139. if ch.room == old_room:
  140. ch.room = ex.dest
  141. # stuff that happens before we 'look'
  142. hooks.run("pre_enter", hooks.build_info("ch rm", (ch, ch.room)))
  143. ch.act("look")
  144. # send out our enter mud.messages as needed
  145. if mssg == True:
  146. if ex.enter_mssg != '':
  147. mud.message(ch,None,None,None,True,"to_room",ex.enter_mssg)
  148. elif dirnum == -1:
  149. mud.message(ch,None,None,None,True,"to_room","$n has arrived.")
  150. else:
  151. mud.message(ch, None, None, None, True, "to_room",
  152. "$n arrives from the "+dir_name[dir_opp[dirnum]]+".")
  153. # run our enter hooks
  154. hooks.run("enter", hooks.build_info("ch rm", (ch, ch.room)))
  155. success = True
  156. # return the exit we found (if we found any)
  157. return ex, success
  158. def cmd_move(ch, cmd, arg):
  159. '''A basic movement command, relocating you to another room in the
  160. specified direction.'''
  161. # cmd_move is the basic entry to all of the movement utilities.
  162. # See try_move() in movement.py
  163. try_move_mssg(ch, cmd)
  164. ################################################################################
  165. # mud commands
  166. ################################################################################
  167. mudsys.add_cmd("north", "n", cmd_move, "player", True)
  168. mudsys.add_cmd("west", "w", cmd_move, "player", True)
  169. mudsys.add_cmd("east", "e", cmd_move, "player", True)
  170. mudsys.add_cmd("south", "s", cmd_move, "player", True)
  171. mudsys.add_cmd("up", "u", cmd_move, "player", True)
  172. mudsys.add_cmd("down", "d", cmd_move, "player", True)
  173. mudsys.add_cmd("northwest", None, cmd_move, "player", True)
  174. mudsys.add_cmd("northeast", None, cmd_move, "player", True)
  175. mudsys.add_cmd("southwest", None, cmd_move, "player", True)
  176. mudsys.add_cmd("southeast", None, cmd_move, "player", True)
  177. mudsys.add_cmd("nw", None, cmd_move, "player", True)
  178. mudsys.add_cmd("ne", None, cmd_move, "player", True)
  179. mudsys.add_cmd("sw", None, cmd_move, "player", True)
  180. mudsys.add_cmd("se", None, cmd_move, "player", True)
  181. mudsys.add_cmd("wake", None, cmd_wake, "player", True)
  182. mudsys.add_cmd("sleep", None, cmd_sleep,"player", True)
  183. mudsys.add_cmd("stand", None, cmd_stand,"player", True)
  184. mudsys.add_cmd("land", None, cmd_stand,"player", True)
  185. mudsys.add_cmd("sit", None, cmd_sit, "player", True)
  186. # The mud needs to know our command for movement as well
  187. mudsys.set_cmd_move(cmd_move)
  188. # useful mud methods
  189. mud.dir_opposite = dir_opposite
  190. def chk_can_move(ch, cmd):
  191. if not ch.pos in ["standing", "flying"]:
  192. ch.send("You cannot do that while " + ch.pos + "!")
  193. return False
  194. for cmd in ["north", "west", "east", "south", "up", "down", "northwest",
  195. "northeast", "southwest", "southeast", "nw", "ne", "sw", "se"]:
  196. mudsys.register_dflt_move_cmd(cmd)
  197. mudsys.register_move_check(chk_can_move)
  198. def chk_wake(ch, cmd):
  199. if not ch.pos == "sleeping":
  200. ch.send("You must be asleep to wake up.")
  201. return False
  202. mudsys.add_cmd_check("wake", chk_wake)
  203. def chk_sleep(ch, cmd):
  204. if ch.pos == "sleeping":
  205. ch.send("You are already sleeping!")
  206. return False
  207. elif ch.pos == "unconscious":
  208. ch.send("You cannot sleep while you are unconscious.")
  209. return False
  210. mudsys.add_cmd_check("sleep", chk_sleep)
  211. def chk_stand(ch, cmd):
  212. if ch.pos == "standing":
  213. ch.send("You are already standing!")
  214. return False
  215. elif ch.pos != "sitting":
  216. ch.send("You cannot stand while " + ch.pos + ".")
  217. return False
  218. mudsys.add_cmd_check("stand", chk_stand)
  219. def chk_land(ch, cmd):
  220. if ch.pos == "standing":
  221. ch.send("You are already on the ground!")
  222. return False
  223. elif ch.pos != "flying":
  224. ch.send("You cannot land if you are not flying.")
  225. return False
  226. mudsys.add_cmd_check("land", chk_land)
  227. def chk_sit(ch, cmd):
  228. if ch.pos == "sitting":
  229. ch.send("You are already sitting!")
  230. return False
  231. elif ch.pos != "standing":
  232. ch.send("You must be standing to sit.")
  233. return False
  234. mudsys.add_cmd_check("sit", chk_sit)