Package horizons :: Package world :: Module diplomacy
[hide private]
[frames] | no frames]

Source Code for Module horizons.world.diplomacy

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from horizons.util.changelistener import metaChangeListenerDecorator 
23 24 25 @metaChangeListenerDecorator("diplomacy_status_changed") 26 -class Diplomacy:
27 """ 28 Diplomacy class 29 handles diplomacy between players 30 two players can be allies, neutral or enemies 31 allies: set of (a, b) tuples of player instances meaning a and b are allies 32 for making the relationship symmetrical a has a lower worldid than b 33 enemies: set of (a, b) tuples of player instances meaning a and b are enemies 34 if to players are not allies nore enemies, they are neutral 35 36 for listeners that are not Callback instances, the arguments must be: 37 caller, old_state, new_state, a, b 38 old_state and new_state are a strings that can be 'ally', 'neutral' or 'enemy' 39 a is first player 40 b is second player 41 """ 42
43 - def __init__(self):
44 self.allies = set() 45 self.enemies = set()
46
47 - def add_ally_pair(self, a, b):
48 tup = make_tup(a, b) 49 if tup is None or tup in self.allies: 50 return 51 old_state = self._get_state_string(tup) 52 self.remove_enemy_pair(a, b) 53 self.allies.add(tup) 54 self.on_diplomacy_status_changed(old_state=old_state, new_state='ally', a=a, b=b)
55
56 - def add_enemy_pair(self, a, b):
57 tup = make_tup(a, b) 58 if tup is None or tup in self.enemies: 59 return 60 old_state = self._get_state_string(tup) 61 self.remove_ally_pair(a, b) 62 self.enemies.add(tup) 63 self.on_diplomacy_status_changed(old_state=old_state, new_state='enemy', a=a, b=b)
64
65 - def add_neutral_pair(self, a, b):
66 tup = make_tup(a, b) 67 if tup is None or (tup not in self.allies and tup not in self.enemies): 68 return 69 old_state = self._get_state_string(tup) 70 self.remove_ally_pair(a, b) 71 self.remove_enemy_pair(a, b) 72 self.on_diplomacy_status_changed(old_state=old_state, new_state='neutral', a=a, b=b)
73
74 - def remove_enemy_pair(self, a, b):
75 tup = make_tup(a, b) 76 if tup is None: 77 return 78 if tup in self.enemies: 79 self.enemies.remove(tup)
80
81 - def remove_ally_pair(self, a, b):
82 tup = make_tup(a, b) 83 if tup is None: 84 return 85 if tup in self.allies: 86 self.allies.remove(tup)
87
88 - def are_allies(self, a, b):
89 if a is b: 90 return True 91 tup = make_tup(a, b) 92 return tup in self.allies
93
94 - def are_enemies(self, a, b):
95 if a is b: 96 return False 97 tup = make_tup(a, b) 98 return tup in self.enemies
99
100 - def are_neutral(self, a, b):
101 if a is b: 102 return False 103 tup = make_tup(a, b) 104 return tup not in self.allies and tup not in self.enemies
105
106 - def can_trade(self, a, b):
107 # NOTE: this has not been specified w.r.t. game play 108 if a is b: # can trade with self 109 return True 110 else: 111 return not self.are_enemies(a, b)
112
113 - def _get_state_string(self, tup):
114 if tup in self.enemies: 115 return 'enemy' 116 elif tup in self.allies: 117 return 'ally' 118 else: 119 return 'neutral'
120
121 - def load(self, world, db):
122 for a, b in db("SELECT player1, player2 FROM diplomacy_allies"): 123 tup = world.get_object_by_id(a), world.get_object_by_id(b) 124 self.allies.add(tup) 125 for a, b in db("SELECT player1, player2 FROM diplomacy_enemies"): 126 tup = world.get_object_by_id(a), world.get_object_by_id(b) 127 self.enemies.add(tup)
128
129 - def save(self, db):
130 for tup in self.allies: 131 db("INSERT INTO diplomacy_allies(player1, player2) VALUES(?, ?)", 132 tup[0].worldid, tup[1].worldid) 133 for tup in self.enemies: 134 db("INSERT INTO diplomacy_enemies(player1, player2) VALUES(?, ?)", 135 tup[0].worldid, tup[1].worldid)
136
137 138 -def make_tup(a, b):
139 """ 140 Utility function that returns x,y tuple with x.worldid < y.worldid 141 """ 142 if a is b: 143 return None 144 elif a.worldid < b.worldid: 145 return a, b 146 else: 147 return b, a
148