AtkinsSJ

Procedural Attack Strings

Jun 3rd, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """A random string generator, for 'You hit th bat!' type messages.
  4.  
  5. Usage: generate_message(enemeyName, weaponName) - returns a string"""
  6.  
  7. import random
  8.  
  9. class StringGenerator:
  10.     """A class for generating strings procedurally,
  11.         from many random combinations."""
  12.    
  13.     messages = [
  14.         "you {attack} with your {weapon}",
  15.         "{preparation} {weapon}, then {attack} with it",
  16.         "{preparation} {weapon}, then {attack}"
  17.         ]
  18.    
  19.     preparations = [
  20.         "you {adverb} take aim with your",
  21.         "you {adverb} grab for your",
  22.         "you grip your trusty",
  23.         "you steady your"
  24.         ]
  25.    
  26.     intros = [
  27.         "with little time to prepare",
  28.         "in a blaze of fury",
  29.         "in a blind panic",
  30.         "confident in your abilities",
  31.         "fully aware of the danger",
  32.         "without hesitation",
  33.         "with no mercy",
  34.         "with no remorse",
  35.         "feeling threatened",
  36.         "like a cornered rat",
  37.         "unconcerned by the situation",
  38.         "like a hero",
  39.         "without breaking a sweat"
  40.         ]
  41.        
  42.     asides = [
  43.         "HUGE SUCCESS",
  44.         "Hooray!",
  45.         "Incredible!",
  46.         "Spectaular!",
  47.         "Ten out of ten!",
  48.         "A textbook move!",
  49.         "You make it look easy!",
  50.         "Amazing!",
  51.         "What a hero!",
  52.         "Ouch!", "Oof!", "I bet that hurt."
  53.         ]
  54.    
  55.     attacks = [
  56.         "{hit} the {enemy} {adverb} {bodypart}",
  57.         "{adverb} {hit} the {enemy} {bodypart}",
  58.         "{hit} the {enemy} {bodypart} {adverb}",
  59.         "{adverb} {hit} the {enemy}, right {bodypart}",
  60.         "{adverb} leap at the {enemy}, and {hit} it {bodypart}",
  61.         "leap at the {enemy}, and {hit} it {adverb} {bodypart}"
  62.         ]
  63.    
  64.     bodyparts_singular = [
  65.         "face", "neck", "nose", "mouth", "forehead", "chin", "teeth", "tongue",
  66.         "chest", "belly", "unmentionables", "spine", "back",
  67.         "stomach", "spleen", "heart", "intestines", "bladder"
  68.         ]
  69.    
  70.     bodyparts_directional = [
  71.         "arm", "elbow", "wrist", "hand", "finger", "thumb",
  72.         "leg", "thigh", "knee", "ankle", "shin", "foot", "toe",
  73.         "eye", "nostril", "ear",
  74.         "nipple", "shoulder", "buttock",
  75.         "lung", "kidney"
  76.         ]
  77.    
  78.     hit = [
  79.         "stab", "poke", "skewer",
  80.         "slice", "cut",
  81.         "whack", "thwack", "smash", "smack", "wallop", "hit"
  82.         ]
  83.    
  84.     adverbs = [
  85.         "quickly", "swiftly", "suddenly",
  86.         "angrily", "vengefully", "desperately",
  87.         "joyously", "triumphantly", "courageously",
  88.         "elegantly", "precisely", "calmly", "carefully", "casually",
  89.         "steadily", "slowly", "gradually"
  90.         ]
  91.    
  92.     def generate_message(self, theEnemy, theWeapon):
  93.         """Generate an attack message string, and return it."""
  94.         message = self.random_message().format( enemy=theEnemy, weapon=theWeapon,
  95.                     attack=self.random_attack(theEnemy), preparation=self.random_preparation() ) + "!"
  96.        
  97.         # Randomly prefix with an intro
  98.         if random.randint(0,1) == 0:
  99.             message = self.random_intro() + ", " + message
  100.            
  101.         message = message.capitalize()
  102.        
  103.         # Randomly add an aside
  104.         if random.randint(0,1) == 0:
  105.             message = message + " " + self.random_aside()
  106.            
  107.         return message
  108.        
  109.     def random_message(self):
  110.         """Return a random base message"""
  111.         return random.choice(self.messages)
  112.    
  113.     def random_attack(self, theEnemy):
  114.         """Returns a random attack clause"""
  115.         return random.choice(self.attacks).format(enemy=theEnemy,
  116.                     hit=self.random_hit(), bodypart=self.random_bodypart(),
  117.                     adverb=self.random_adverb())
  118.        
  119.     def random_intro(self):
  120.         """Return a random intro"""
  121.         if random.randint(0,2) == 0:
  122.             return self.random_adverb()
  123.         else:
  124.             return random.choice(self.intros)
  125.    
  126.     def random_aside(self):
  127.         """Return a random aside"""
  128.         return random.choice(self.asides)
  129.    
  130.     def random_preparation(self):
  131.         """Return a random preparation"""
  132.         return random.choice(self.preparations).format(adverb=self.random_adverb())
  133.    
  134.     def random_hit(self):
  135.         """Returns a random 'hit' verb"""
  136.         return random.choice(self.hit)
  137.    
  138.     def random_bodypart(self):
  139.         """Returns a random body part name"""
  140.         option = random.randint(0,2)
  141.         if option == 0:
  142.             return "in the " + random.choice(self.bodyparts_singular)
  143.         elif option == 1:
  144.             return "in the " + random.choice(["left", "right"]) + \
  145.                     " " + random.choice(self.bodyparts_directional)
  146.         else:
  147.             return "between the " + random.choice(self.bodyparts_directional) + "s"
  148.        
  149.     def random_adverb(self):
  150.         """Return a random adverb"""
  151.         return random.choice(self.adverbs)
  152.        
  153. # run the main function only if this module is executed as the main script
  154. # (if you import this as a module then nothing is executed)
  155. if __name__=="__main__":
  156.     # call the main function
  157.     s = StringGenerator()
  158.     print "Demonstration: 10 random strings about attacking an orc with a spoon."
  159.     for i in range(10):
  160.         print s.generate_message("orc", "spoon")
Advertisement
Add Comment
Please, Sign In to add comment