Package horizons :: Package component :: Module namedcomponent
[hide private]
[frames] | no frames]

Source Code for Module horizons.component.namedcomponent

  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 typing import List 
 23   
 24  from horizons.component import Component 
 25  from horizons.util.changelistener import ChangeListener 
26 27 28 -class NamedComponent(Component):
29 """An object that has a special name. "Special" means, that it's not (only) autogenerated.""" 30 31 NAME = "namedcomponent" 32 33 names_used = [] # type: List[str] 34
35 - def __init__(self, name=None):
36 super().__init__() 37 self.name = name
38
39 - def initialize(self):
40 self.set_name(self.name)
41
42 - def set_name(self, name=None):
43 """Actually sets the name.""" 44 if self.name is not None and self.name in NamedComponent.names_used: 45 NamedComponent.names_used.remove(self.name) 46 if name is None: 47 name = self.get_default_name() 48 self.name = name 49 NamedComponent.names_used.append(self.name) 50 if isinstance(self.instance, ChangeListener): 51 self.instance._changed()
52
53 - def _possible_names(self):
54 return ['object_{}'.format(self.instance.worldid)]
55
56 - def get_default_name(self):
57 available_names = [name for name in self._possible_names() if name not in NamedComponent.names_used] 58 59 if available_names != []: 60 return self.session.random.choice(available_names) 61 else: 62 newname = self.session.random.choice(self._possible_names()) 63 index = 2 64 while "{newname} {index}".format(newname=newname, index=index) in NamedComponent.names_used: 65 index += 1 66 return "{newname} {index}".format(newname=newname, index=index)
67
68 - def save(self, db):
69 super().save(db) 70 db("INSERT INTO name (rowid, name) VALUES(?, ?)", self.instance.worldid, self.name)
71
72 - def load(self, db, worldid):
73 super().load(db, worldid) 74 self.name = None 75 name = db("SELECT name FROM name WHERE rowid = ?", worldid)[0][0] 76 # We need unicode strings as the name is displayed on screen. 77 self.set_name(name)
78 79 @classmethod
80 - def reset(cls):
81 cls.names_used = []
82
83 84 -class ShipNameComponent(NamedComponent):
85
86 - def _possible_names(self):
87 names = self.session.db("SELECT name FROM shipnames WHERE for_player = 1") 88 return [x[0] for x in names]
89
90 91 -class PirateShipNameComponent(NamedComponent):
92
93 - def _possible_names(self):
94 names = self.session.db("SELECT name FROM shipnames WHERE for_pirate = 1") 95 return [x[0] for x in names]
96
97 98 -class SettlementNameComponent(NamedComponent):
99
100 - def _possible_names(self):
101 names = self.session.db("SELECT name FROM citynames WHERE for_player = 1") 102 return [x[0] for x in names]
103
104 105 -class SoldierNameComponent(NamedComponent):
106
107 - def _possible_names(self):
108 names = self.session.db("SELECT name FROM groundunitnames WHERE for_soldier = 1") 109 return [x[0] for x in names]
110
111 112 -class InhabitantNameComponent(NamedComponent):
113
114 - def _possible_names(self):
115 names = self.session.db("SELECT name FROM groundunitnames WHERE for_inhabitant = 1") 116 return [x[0] for x in names]
117