1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
4 # This file is part of OpenAnno.
6 # OpenAnno 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.
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.
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 # ###################################################
22 from game
.world
.storage
import SizedSlotStorage
23 from game
.util
import WorldObject
, Point
, WeakList
25 class Settlement(WorldObject
):
26 """The Settlement class describes a settlement and stores all the necessary information
27 like name, current inhabitants, lists of tiles and houses, etc belonging to the village."""
28 def __init__(self
, owner
):
30 @param owner: Player object that owns the settlement
32 self
.name
= game
.main
.db("SELECT name FROM data.citynames WHERE for_player = 1 ORDER BY random() LIMIT 1")[0][0]
35 self
.buildings
= WeakList() # List of all the buildings belonging to the settlement
37 self
.buy_list
= {} # dict of resources that are to be bought. { res_id: limit, .. }
38 self
.sell_list
= {} # dict of resources that are to be sold. { res_id: limit, .. }
42 def setup_storage(self
):
43 self
.inventory
= SizedSlotStorage(30)
44 self
.inventory
.addChangeListener(self
._changed
)
46 def get_building(self
, point
):
47 """Returns the building at the position (x,y)
48 @param point: position to look at
49 @return: Building class instance or None if none is found.
51 for b
in self
.buildings
:
52 if b
.position
.contains(point
):
57 def add_inhabitants(self
, num
):
58 self
._inhabitants
+= num
60 def rem_inhabitants(self
, num
):
61 self
._inhabitants
-= num
63 def save(self
, db
, islandid
):
64 db("INSERT INTO settlement (rowid, island, owner, inhabitants) VALUES(?, ?, ?, ?)",
65 self
.getId(), islandid
, self
.owner
.getId(), self
._inhabitants
)
66 db("INSERT INTO name (rowid, name) VALUES(?, ?)",
67 self
.getId(), self
.name
)
68 self
.inventory
.save(db
, self
.getId())
74 def load(cls
, db
, worldid
):
75 self
= cls
.__new
__(cls
)
77 super(Settlement
, self
).load(db
, worldid
)
79 self
.owner
= db("SELECT owner FROM settlement WHERE rowid = ?", worldid
)[0][0]
80 self
.owner
= WorldObject
.getObjectById(self
.owner
)
82 self
._inhabitants
= int(db("SELECT inhabitants FROM settlement WHERE rowid = ?", worldid
)[0][0])
83 self
.name
= db("SELECT name FROM name WHERE rowid = ?", worldid
)[0][0]
86 self
.inventory
.load(db
, worldid
)
88 self
.buildings
= WeakList()
89 for building_id
, building_type
in \
90 db("SELECT rowid, type FROM building WHERE location = ?", worldid
):
91 buildingclass
= game
.main
.session
.entities
.buildings
[building_type
]
92 building
= buildingclass
.load(db
, building_id
)
93 building
.settlement
= self
94 game
.main
.session
.world
.get_island(building
.position
.origin
.x
,building
.position
.origin
.y
).add_building(building
, self
.owner
)
95 self
.buildings
.append(building
)