Advertisement
D10d3

game_engine.py

Nov 13th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.88 KB | None | 0 0
  1. import game_data
  2. import os
  3. class engine(object):
  4.  
  5.     def describe_room(self,location,system_data,items): #print description from room data
  6.         print ""
  7.         print location.title
  8.         #if new room, or verbose, print long description
  9.         if location.title not in system_data.rooms_visited:
  10.             system_data.rooms_visited.append(location.title)
  11.             for i in location.long_description:
  12.                 print i
  13.             print ""
  14.         elif system_data.brief == False:
  15.             for i in location.long_description:
  16.                 print i
  17.         else:
  18.             print location.short_description
  19.         #describe room inventory
  20.         print "item descriptions:"
  21.         for item in location.inventory:
  22.             print item['placed']
  23.         print ""
  24.  
  25.     #   *** BEGIN EVENT FUNCTIONS ***
  26.     def event_handler(self,location,items,player,words,nouns,verb): #triggers events from player input
  27.         #inventory router
  28.        
  29.         if verb in words.inventory_commands:
  30.             if verb == "inventory":
  31.                 print "Inventory:"
  32.                 for item in player.inventory:
  33.                     print " %s"%item['name']
  34.             elif verb == "i":
  35.                 print "Inventory:"
  36.                 for item in player.inventory:
  37.                     print " %s"%item['name']                   
  38.             elif verb == "get":
  39.                 return "get"   
  40.             elif verb == "take":
  41.                 return "get"                   
  42.             elif verb == "drop":
  43.                 return "drop"
  44.             elif verb == "put":
  45.                 return "put"
  46.             elif verb == "from":
  47.                 return "get from"
  48.  
  49.     def move(): #change room
  50.         pass
  51.     def inventory(self,action,location,items,player,nouns,verb): #add or remove player_inventory
  52.         if action == "get":
  53.             get_true = False
  54.             for noun in nouns:
  55.                 for item in location.inventory:
  56.                     if noun == item:
  57.                         get_true = True
  58.                         location.inventory.remove(item)
  59.                         player.inventory.append(item)
  60.                         print "you get %s" % item['name']
  61.             if not get_true:
  62.                 print "You can't get that."
  63.         elif action == "drop":
  64.             drop_true = False
  65.             for noun in nouns:
  66.                 for item in player.inventory:
  67.                     if noun == item:
  68.                         drop_true = True
  69.                         location.inventory.append(item)
  70.                         player.inventory.remove(item)
  71.                         print "you drop %s" % item['name']
  72.             if not drop_true:
  73.                 print "You don't have that!"
  74.                
  75.     def place_item(self,action,location,items,player,nouns,verb): #placing item: "Put jewel in box"
  76.         to_put = []
  77.         put_in = []
  78.         take_from = []
  79.         thing = False
  80.         place = False
  81.         if action == "put":
  82.             for noun in nouns:
  83.                 for item in player.inventory:
  84.                     if noun == item:
  85.                         #print "for player inventory:"
  86.                         #print "to_put is set to: %s" % item['name']
  87.                         to_put = item
  88.                         thing = True
  89.             for noun in nouns:
  90.                 for item in location.inventory:
  91.                     if item['container'] == True:
  92.                         if noun == item:
  93.                             #print "for location inventory:"
  94.                             #print "Put_in is set to: %s" % item['name']
  95.                             put_in = item
  96.                             place = True
  97.             for noun in nouns:
  98.                 for item in location.static:
  99.                     if item['container'] == True:
  100.                         if noun == item:
  101.                             #print "for static inventory:"
  102.                             #print "Put_in is set to: %s" % item['name']
  103.                             put_in = item
  104.                             place = True
  105.             if thing and place:
  106.                 player.inventory.remove(to_put)
  107.                 put_in['inventory'].append(to_put)
  108.                 print "You put the %s in the %s."% (to_put['name'],put_in['name'])
  109.             else:
  110.                 print "Put what where? I'm confused."
  111.        
  112.         if action == "get from":
  113.             for noun in nouns:
  114.                 for item in location.inventory:
  115.                     if item['container'] == True:
  116.                         for object in item['inventory']:
  117.                             if noun == object:
  118.                                 print "for static inventory:"
  119.                                 print "to_put is set to: %s" % object['name']
  120.                                 to_put = object
  121.                                 thing = True
  122.                             if noun == item:
  123.                                 print "for static inventory:"
  124.                                 print "take_from is set to: %s" % item['name']
  125.                                 take_from = item
  126.                                 place = True
  127.             for noun in nouns:
  128.                 for item in location.static:
  129.                     if item['container'] == True:
  130.                         for object in item['inventory']:
  131.                             if noun == object:
  132.                                 #print "for static inventory:"
  133.                                 #print "to_put is set to: %s" % object['name']
  134.                                 to_put = object
  135.                                 thing = True
  136.                             if noun == item:
  137.                                 #print "for static inventory:"
  138.                                 #print "take_from is set to: %s" % item['name']
  139.                                 take_from = item
  140.                                 place = True
  141.                                
  142.             if thing and place:
  143.                 for item in take_from['inventory']:
  144.                     #print item['name']
  145.                     #print to_put['name']
  146.                     if to_put == item:
  147.                         player.inventory.append(to_put)
  148.                         take_from['inventory'].remove(to_put)
  149.                         print "You get the %s" % to_put['name']
  150.                     else:
  151.                         print "Get what from where? I'm confused."
  152.             else:
  153.                 print "Get what from where? I'm confused."
  154.                
  155.            
  156.     def change_exit(): #add or remove an exit
  157.         pass
  158.     def change_static(): #add or remove static objects
  159.         pass
  160.     def room_inventory(): #add or remove room_inventory
  161.         pass
  162.     def change_mob(): #add or remove mob
  163.         pass
  164.     def change_room_desc(): #change room descriptions
  165.         pass
  166.     def health(): #change player health
  167.         pass
  168.     def combat(): #initiate combat
  169.         pass
  170.     def talk(): #initiate conversation
  171.         pass
  172. class parser(object): #Various parsers for acting on user input
  173.    
  174.     def break_words(self,action): #breaks 'action' input into a list of words
  175.         words = action.split(' ')
  176.         print"" #dumb, but helps formatting
  177.         return words
  178.     def get_nouns(self,action,location,items,player): #pull nouns from player input
  179.         #loc_inv = location.inventory
  180.         #loc_static = location.static
  181.         #player_inv = player.inventory
  182.         exits = location.exits.keys()
  183.         nouns = [] #holder for found nouns
  184.         for word in action:
  185.             for item in location.inventory:
  186.                 if item['name'] == word:
  187.                     nouns.append(item)
  188.                 elif item['container'] == True:
  189.                     for object in item['inventory']:
  190.                         if object['name'] == word:
  191.                             nouns.append(object)
  192.             for item in location.static:
  193.                 if item['name'] == word:
  194.                     nouns.append(item)
  195.                 elif item['container'] == True:
  196.                     for object in item['inventory']:
  197.                         if object['name'] == word:
  198.                             nouns.append(object)
  199.             for item in player.inventory:
  200.                 if item['name'] == word:
  201.                     nouns.append(item)
  202.         return nouns
  203.     def get_verb(self,action):
  204.         current_place_words = game_data.words.current_place_words
  205.         verb = ""
  206.         words = game_data.words()
  207.         verb_list = []
  208.         verb_list.append(words.inventory_commands)
  209.         verb_list.append(words.verbs)
  210.         verb_list.append(words.describers)
  211.         verb_list.append(words.nav_commands)
  212.         verb_list.append(words.system_commands)
  213.         for word in action:
  214.             for list in verb_list:
  215.                 if word in list:
  216.                     verb = word
  217.         #this ugly chunk determines if you are looking at your location
  218.         if action[0] == "l":
  219.             verb = "look here"
  220.         for desc in words.describers:
  221.             if verb == desc:
  222.                 for word in current_place_words:
  223.                     for action_word in action:
  224.                         if action_word == word:
  225.                             verb = "look here" #look_at() looks for this specific phrase
  226.         #this ugly chunk determines if you are looking at a direction          
  227.         look_dir_desc = False
  228.         look_dir_dir = False
  229.         for desc in words.describers:
  230.             for action_word in action:
  231.                 if action_word == desc:
  232.                     look_dir_desc = True
  233.                 for word in words.nav_commands:
  234.                     if action_word == word:
  235.                         look_dir_dir = True
  236.                         if look_dir_desc and look_dir_dir:
  237.                             verb = "look direction" #look_at() looks for this specific phrase
  238.         return verb
  239.  
  240.     def exit(self,action,location,system_data,map_data): #checks input to see if it's a move order
  241.         #we are checking for three things here:
  242.         #we are checking for three things here:
  243.         #   is the user input just trying to look at an exit?
  244.         #   If not, is the input on the list of possible nav commands?
  245.         #   if so, is it actually a valid exit for this location?
  246.         #       detected look-direction inputs are ignored,
  247.         #       parser.look_at will also detect this and issue and error message
  248.         #if all of those are satisfied and it's a good command, the location is changed
  249.         describers = game_data.words.describers
  250.         nav_commands = game_data.words.nav_commands
  251.         converter = game_data.words.convert_direction
  252.         exitcheck = 0#these three are holders for checking input against lists
  253.         navcheck = 0 #we start assuming they are not on the list
  254.         looking_present = 0
  255.         going_to = ""
  256.        
  257.         #convert single letter directions.
  258.         for dir in converter:
  259.             conv = []
  260.             conv.append(converter[dir])
  261.             dirlist = []
  262.             dirlist.append(dir)
  263.             if action == conv:
  264.                 action = dirlist
  265.        
  266.        
  267.         for action_word in action: #start processing user input
  268.             for look in describers:#are you just trying to look at an exit?
  269.                 if action_word == look:
  270.                     looking_present += 1
  271.                 for nav in nav_commands: #does it contain a nav command?
  272.                     if action_word == nav:
  273.                         navcheck += 1
  274.                     for exit in location.exits: #is it on the list of exits for this location?
  275.                         exitkey = location.exits[exit]
  276.                         if action_word == exit:
  277.                             exitcheck += 1
  278.                             going_to = exitkey
  279.                            
  280.         #having determined if input is just looking, is a nav command, and is on the exit list
  281.         #we now start acting on that info
  282.         if not looking_present:
  283.             if navcheck:
  284.                 if not exitcheck:
  285.                     print "You can't go that way"
  286.                 else:
  287.                     print ""
  288.                     print "You go %s" % action_word
  289.                     for room in map_data.room_list:
  290.                         if room == going_to:
  291.                             location = map_data.room_list[going_to]
  292.         return location
  293.        
  294.     def profanity(self,action): #checks for profanity
  295.         profanity = game_data.words.profanity
  296.         cursing = 0
  297.         for action_word in action:
  298.             for i in profanity:
  299.                 if action_word == i:
  300.                     cursing += 1
  301.         if cursing:
  302.             print "Such language!"
  303.            
  304.     def system(self,system_data,verb): #checks for system commands
  305.         system_commands = game_data.words.system_commands
  306.         for word in system_commands:
  307.             if verb == word:
  308.                 if verb == "inv?":
  309.                     for inv_word in game_data.words.inventory_commands:
  310.                         print inv_word
  311.                 elif verb == "load":
  312.                     print "Command not yet implemented"
  313.                 elif verb == "save":
  314.                     print "Command not yet implemented"
  315.                 elif verb == "verbose":
  316.                     system_data.brief = False
  317.                     print "Setting Verbose, Always give full room description"
  318.                 elif verb == "brief":
  319.                     system_data.brief = True
  320.                     print "Setting brief, Only give room full description on first visit"
  321.                 elif verb == "score":
  322.                     print "Command not yet implemented"
  323.                 elif verb == "restart":
  324.                     return True
  325.                 elif verb == "quit":
  326.                     quit()
  327.                 elif verb == "q":
  328.                     quit()
  329.                 elif verb == "clear":
  330.                     os.system('cls')
  331.                 elif verb == "?" or "help":
  332.                     print "Adventure Framework ver0.5 by D10d3"
  333.                     print "    -= An adventure engine =-"
  334.                     print ""
  335.                     print "In addition to the following system commands you may type any action"
  336.                     print "that comes to mind with varying degrees of success. When interacting"
  337.                     print "with objects try to always place the verb before the noun."
  338.                     print "Thanks for playing."
  339.                     print ""
  340.                     print "System commands:"
  341.                     print " clear = Clear the screen"
  342.                     print " g = repeat last action"
  343.                     print " inv? : Lists inventory commands the player can use"
  344.                     print " load : Command not yet implemented"
  345.                     print " save : Command not yet implemented"
  346.                     print " verbose : Always give full room description"
  347.                     print " brief : Only give room full description on first visit"
  348.                     print " score : Command not yet implemented"
  349.                     print " restart : Restarts the game"
  350.                     print " help or ? : Displays this information"
  351.                     print " quit or q : Exits the game"
  352.    
  353.            
  354.     def look_at(self,verb,nouns,location,items,player): #checks for description query
  355.         #first we get a list of describer words, and all of the possible nouns
  356.         describers = game_data.words.describers
  357.         loc_inv = location.inventory
  358.         loc_static = location.static
  359.         player_inv = player.inventory
  360.         exits = location.exits.keys()
  361.         #current_place_words = game_data.words.current_place_words
  362.         present = False #assumes you are looking at nothing that's there.
  363.         if verb == "look here":
  364.             print "You look around."
  365.             present = True
  366.             for i in location.long_description:
  367.                 print i
  368.         if verb == "look direction":
  369.             print "You will need to go there to find out what's there."
  370.             present = True
  371.         if verb == "quit":
  372.             present = True
  373.             print "Goodbye!"
  374.         for word in describers:
  375.             if verb == word:
  376.                 for noun in nouns:
  377.                     present = True
  378.                     print noun['desc']
  379.                     try:
  380.                         if noun['inventory']:
  381.                             print "%s contains:" % noun['name']
  382.                             for item in noun['inventory']:
  383.                                 print item['name']
  384.                     except KeyError:
  385.                         fnord = "fnord"
  386.         #for list in game_data.words.wordlists:
  387.         #   if verb in list:
  388.         #       present +=1
  389.         if present == False:
  390.             print "What are you looking at?"
  391.                
  392.     def recognized(self,action): #is known word? error message if not
  393.         game_datas = game_data.words.wordlists
  394.         known_word = 0
  395.        
  396.         for list in game_datas:
  397.             for word in list:
  398.                 for action_word in action:
  399.                     if action_word == word:
  400.                         known_word += 1
  401.        
  402.         if not known_word:
  403.             print "I don't understand that."
  404.         else:
  405.             return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement