Imported Upstream version 2008.1+svn1553
[opeanno-debian-packaging.git] / game / world / __init__.py
blobc80146465aab1d1d4786cddc04962c97c4afe644
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
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 __all__ = ['island', 'nature', 'player', 'settlement']
24 import weakref
26 import game.main
27 from game.world.island import Island
28 from game.world.player import Player
29 from game.util import livingObject, Point
31 class World(livingObject):
32 """
33 """
34 def begin(self, db):
35 #load properties
36 self.properties = {}
37 for (name, value) in db("select name, value from map_properties"):
38 self.properties[name] = value
40 # create playerlist
41 self.players = []
43 # load player
44 for player_id, client_id in db("SELECT rowid, client_id FROM player"):
45 player = Player.load(db, player_id)
46 self.players.append(player)
47 if client_id == game.main.settings.client_id or client_id == "":
48 self.player = player
50 #load islands
51 self.islands = []
52 for filename, offset_x, offset_y, islandid in db("select file, x, y, rowid from island"):
53 island = Island(Point(offset_x, offset_y), filename)
54 island.load(db, islandid)
55 self.islands.append(island)
57 #calculate map dimensions
58 self.min_x, self.min_y, self.max_x, self.max_y = None, None, None, None
59 for i in self.islands:
60 self.min_x = i.rect.left if self.min_x is None or i.rect.left < self.min_x else self.min_x
61 self.min_y = i.rect.top if self.min_y is None or i.rect.top < self.min_y else self.min_y
62 self.max_x = i.rect.right if self.max_x is None or i.rect.right > self.max_x else self.max_x
63 self.max_y = i.rect.bottom if self.max_y is None or i.rect.bottom > self.max_y else self.max_y
64 self.min_x -= 10
65 self.min_y -= 10
66 self.max_x += 10
67 self.max_y += 10
69 #add water
70 print "Filling world with water..."
71 self.water = [ (x,y) for x in xrange(self.min_x, self.max_x) for y in xrange(self.min_y, self.max_y) ]
72 for i in self.islands:
73 for g in i.grounds:
74 self.water.remove((g.x,g.y))
75 print "Adding %d water tiles..." % (len(self.water),)
76 self.grounds = []
77 self.ground_map = {}
78 for x, y in self.water:
79 ground = game.main.session.entities.grounds[int(self.properties.get('default_ground', 4))](x, y)
80 self.grounds.append(ground)
81 self.ground_map[(x,y)] = weakref.ref(ground)
82 print "Done."
84 # create ship position list. entries: ship_map[ship.unit_position] = ship
85 self.ship_map = {}
86 ## TODO same for blocking units on island, as soon as such are implemented
88 # create shiplist
89 self.ships = []
91 # load units
92 from game.world.units.ship import Ship
93 for (worldid, typeid) in db("SELECT rowid, type FROM unit"):
94 obj = game.main.session.entities.units[typeid].load(db, worldid)
95 if isinstance(obj, Ship):
96 self.ships.append(obj)
98 # reconstruct shipmap
99 for ship in self.ships:
100 self.ship_map[ship.position] = weakref.ref(ship)
102 def setupPlayer(self, name, color):
103 self.player = Player(0, name, color)
104 self.players.append(self.player)
105 game.main.session.ingame_gui.update_gold()
106 self.player.inventory.addChangeListener(game.main.session.ingame_gui.update_gold)
108 def get_tile(self, point):
109 """Returns the ground at x, y.
110 @param point: coords as Point
111 @return: instance of Ground at x, y
113 i = self.get_island(point.x, point.y)
114 if i is not None:
115 return i.get_tile(point)
116 assert (point.x, point.y) in self.ground_map, 'ground must be in water'
117 return self.ground_map[(point.x, point.y)]()
119 def get_building(self, x, y):
120 """Returns the building at the position x,y.
121 @param x,y: int coordinates.
122 @return: Building class instance if a building is found, else none."""
123 i = self.get_island(x, y)
124 return None if i is None else i.get_building(Point(x, y))
126 def get_island(self, x, y):
127 """Returns the island for that coordinate, if none is found, returns None.
128 @param x: int x position.
129 @param y: int y position. """
130 point = Point(x, y)
131 for i in self.islands:
132 if not i.rect.contains(point):
133 continue
134 if point.get_coordinates()[0] in i.ground_map:
135 return i
136 return None
138 def save(self, db):
139 """Saves the current game to the specified db.
140 @param db: DbReader object of the db the game is saved to."""
141 for name, value in self.properties.iteritems():
142 db("INSERT INTO map_properties (name, value) VALUES (?, ?)", name, value)
143 for island in self.islands:
144 island.save(db)
145 for player in self.players:
146 player.save(db)
147 for ship in self.ships:
148 ship.save(db)