def diplomacy(self, target): 'Learn something about another country.' print('{0} sent a diplomat to {1}.'.format(self.name, target.name)) self_target_key = identity_key(self, target) if target.identity == 'P': while True: response = input('Do you accept the diplomat [y,N]? ').lower() if response in ('y', 'yes', 'n', 'no', ''): break print('Invalid response.') if response in ('y', 'yes'): print('You accepted the diplomat.') self.__relationships__[self_target_key] += 3 else: print('You rejected the diplomat.') self.__relationships__[identity_key(self, target)] -= 1 return if self.__relationships__[identity_key(self, target)] < randint(35, 45): statement = [ '{0} ordered the diplomat away.'.format(target.name), 'He mysteriously disappeared.', 'He got lynched.', 'She defected to {0}.'.format(target.name), 'She was never seen again.' ] print(choice(statement)) self.__relationships__[identity_key(self, target)] -= 1 return self.__relationships__[self_target_key] += 3
def dual_attack(self, ally, target): 'Country proposes dual attack on another country.' print('You propose to attack {0} with {1}'.format( target.name, ally.name)) self_ally_key = identity_key(self, ally) self_target_key = identity_key(self, target) ally_target_key = identity_key(ally, target) # Calculate ally response response_calc = (self.__relationships__[self_ally_key] - randint(-2, 5) - self.__relationships__[ally_target_key] // 2) # This may change if response_calc > 30: print('{0} agrees to attack {1}.'.format(ally.name, target.name)) else: print('{0} refuses to attack {1}.'.format(ally.name, target.name)) return damage = int(lognormvariate(2.4, 0.4)) industry_drain = damage * 3 // 4 self.resources['industry'] = max( 0, self.resources['industry'] - industry_drain) ally.resources['industry'] = max( 0, ally.resources['industry'] - industry_drain) target.resources['population'] -= damage relationship_drain = 7 + damage * 2 self.__relationships__[self_target_key] -= relationship_drain self.__relationships__[ally_target_key] -= relationship_drain if damage == 0: print('Somehow, you both failed to kill anyone!') elif damage <= 4: statement = [ 'Your bombs rain down from above!', 'Your soldiers storm the mainland!' ] print(choice(statement)) print('{0} thousand were killed!'.format(damage)) elif damage <= 14: statement = [ 'Tactile missiles strike from two sides!', 'Soldiers overwhelm {0}, wreaking havoc!'.format(target.name) ] print(choice(statement)) print('{0} thousand were killed!'.format(damage)) else: print('Dual nuclear strikes obliterate the enemy!') print('{0} thousand are now dust!!!'.format(damage)) print('Your resources are now {0}.'.format(self.resources['industry']))
def charity(self, target): 'Give a country a gift.' types = ['food', 'industry'] while True: print('What will you give?') print('(1) Food') print('(2) Industrial Resources') try: send_input = int(input('>> ')) except ValueError: print('Invalid input.') continue if send_input in (1, 2): send_type = types[send_input - 1] break print('Invalid choice.') if self.resources[send_type] == 0: print("You don't have anything to give.") return while True: print('How much will you give?') try: send_quant = int(input('>> ')) except ValueError: print('Invalid input.') continue if send_quant <= self.resources[send_type] and send_quant > 0: break print('Invalid quantity.') print('{0} sends its regards.'.format(target.name)) self.resources[send_type] -= send_quant target.resources[send_type] += send_quant self.__relationships__[identity_key(self, target)] += max( 1, send_quant // 4)
def charity(self, target): 'Country gives to another country.' print('{0} gave a gift to {1}.'.format(self.name, target.name)) types = ['food', 'industry'] send_type = choice(types) send_quant = randint(1, self.resources[send_type] // 3) self.resources[send_type] -= send_quant target.resources[send_type] += send_quant self.__relationships__[identity_key(self, target)] += max(1, send_quant // 4)
def trade(self, target): 'Country proposes a trade.' seed() types = ['food', 'industry'] send_type = choice(types) send_max = (self.resources[send_type] + 1) * 3 // 5 rec_type = choice(types) rec_max = (target.resources[rec_type] + 1) * 4 // 5 self_target_key = identity_key(self, target) send_quant = randint(1, send_max) radius = randint(-5, 5) rec_quant = max(1, min(send_quant + radius, rec_max)) if target.identity == 'P': while True: print('{0} wants to trade {1} tons of {2} resources'.format(self.name, send_quant, send_type), end=' ') print('for {0} tons of {1} resources.'.format(rec_quant, rec_type)) response = input('Do you accept this trade [y,N]? ').lower() if response in ('y', 'yes', 'n', 'no', ''): if response in ('y', 'yes'): print('Trade accepted.') self.resources[send_type] -= send_quant self.resources[rec_type] += rec_quant target.resources[send_type] += send_quant target.resources[rec_type] -= rec_quant self.__relationships__[self_target_key] += 5 else: print('Trade declined.') self.__relationships__[self_target_key] -= 5 break else: print("{0} attempts to trade with {1}.".format(self.name, target.name)) response = choice((True, True, True, False)) if response: self.resources[send_type] -= send_quant self.resources[rec_type] += rec_quant target.resources[send_type] += send_quant target.resources[rec_type] -= rec_quant self.__relationships__[self_target_key] += 5 else: self.__relationships__[self_target_key] -= 5
def attack(self, target): 'Country attacks another country.' print('{0} is attacking {1}!'.format(self.name, target.name)) seed() # Damage mostly 1-3, theoretical range is 0-12 damage = int(lognormvariate(1, 0.5)) # This may change industry_drain = damage * 3 // 2 if industry_drain > self.resources['industry']: industry_drain = self.resources['industry'] self.resources['industry'] = max(0, self.resources['industry'] - industry_drain) target.resources['population'] -= damage if damage == 0: # Randomized statements statement = [ '{0} tried to attack {1}, but failed!'.format(self.name, target.name), '{0} saw it coming a mile away!'.format(target.name), 'A spy warned {0} of the attack!'.format(target.name) ] print(choice(statement), end=' ') print('No one was killed.') elif damage <= 4: print('{0} attacked {1}! {2} thousand were killed!'.format(self.name, target.name, damage)) elif damage <= 8: # Randomized statements statement = [ '{0} launched missiles at {1}!'.format(self.name, target.name), '{0} surprise-attacked {1}!'.format(self.name, target.name), ] print(choice(statement), end=' ') print('{0} thousand were killed!'.format(damage)) else: print('A devastating nuclear attack devastated {0}'.format(target.name)) print('{0} thousand were wiped out!!!'.format(damage)) if self.identity == 'P': print('Your industrial resources are now {0} thousand tons.'.format( self.resources['industry'])) relationship_drain = 5 + damage * 2 self.__relationships__[identity_key(self, target)] -= relationship_drain
def trade(self, target): 'Country proposes a trade.' types = ['food', 'industry'] while True: print('What do you want to trade?') print('(1) Food') print('(2) Industrial Resources') try: send_input = int(input('>> ')) except ValueError: print('Invalid input.') continue if send_input in (1, 2): send_type = types[send_input - 1] break print('Invalid choice.') if self.resources[send_type] == 0: print('You have nothing to give.') return print('How much?') while True: try: send_quant = int(input('>> ')) except ValueError: print('Invalid input.') continue if send_quant <= self.resources[send_type] and send_quant > 0: break print('Invalid quantity.') while True: print('What do you want in return?') print('(1) Food') print('(2) Industrial Resources') try: rec_input = int(input('>> ')) except ValueError: print('Invalid input.') continue if rec_input in (1, 2): rec_type = types[rec_input - 1] break print('Invalid choice.') while True: print('How much?') try: rec_quant = int(input('>> ')) except ValueError: print('Invalid input.') continue if rec_quant > 0: break print('Invalid quantity.') if (rec_quant > target.resources[rec_type] + 5 or rec_quant + randint(0, 10) > send_quant or self.__relationships__[identity_key(self, target)] + randint(0, 20) < 50): print('{0} refuses your trade.'.format(target.name)) self.__relationships__[identity_key(self, target)] -= 5 else: print('{0} accepts your trade.'.format(target.name)) self.resources[send_type] -= send_quant self.resources[rec_type] += rec_quant target.resources[send_type] += send_quant target.resources[rec_type] -= rec_quant self.__relationships__[identity_key(self, target)] += send_quant // 5
def dual_attack(self, ally, target): 'Country proposes dual attack on another country.' if self.identity == 'P': print('You propose to attack {0} with {1}'.format(target.name, ally.name)) else: print('{0} proposes to attack {1} with {2}'.format(self.name, target.name, ally.name)) if ally.identity == 'P': while True: ally_response = input('Do you accept [y,N]? ').lower() if ally_response in ('y', 'yes', 'n', 'no', ''): break if ally_response in ('y', 'yes'): print('You agree to attack {0} with {1}'.format(target.name, ally.name)) else: print('You refuse to attack {0}'.format(target.name)) return else: # Calculate ally response response_calc = (self.__relationships__[identity_key(self, ally)] - randint(0, 10) - self.__relationships__[identity_key(ally, target)] // 2) # This may change if response_calc > 30: print('{0} agrees to attack {1}.'.format(ally.name, target.name)) else: print('{0} refuses to attack {1}.'.format(ally.name, target.name)) return damage = int(lognormvariate(2.4, 0.4)) industry_drain = damage * 3 // 4 self.resources['industry'] = max(0, self.resources['industry'] - industry_drain) ally.resources['industry'] = max(0, ally.resources['industry'] - industry_drain) target.resources['population'] -= damage relationship_drain = 10 + damage * 2 self.__relationships__[identity_key(self, target)] -= relationship_drain self.__relationships__[identity_key(ally, target)] -= relationship_drain if damage == 0: print('Somehow, you both failed to kill anyone!') elif damage <= 4: statement = [ 'Bombs rain down from above!', 'Soldiers storm the mainland!' ] print(choice(statement)) print('{0} thousand were killed!'.format(damage)) elif damage <= 14: statement = [ 'Tactile missiles strike from two sides!', 'Soldiers overwhelm {0}, wreaking havoc!'.format(target.name) ] print(choice(statement)) print('{0} thousand were killed!'.format(damage)) else: print('Dual nuclear strikes obliterate the enemy!') print('{0} thousand are now dust!!!'.format(damage)) if self.identity == 'P': print('Your resources are now {0}.'.format(self.resources['industry'])) elif ally.identity == 'P': print('Your resources are now {0}.'.format(ally.resources['industry']))