Imported Upstream version 2008.1+svn1553
[opeanno-debian-packaging.git] / game / world / building / __init__.py
blob00e8b3f1cf8724e43d853750a72e3ede48198dd0
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__ = ['building', 'housing', 'nature', 'path', 'production', 'storages', 'settler']
24 import game.main
25 import fife
27 class BuildingClass(type):
28 """Class that is used to create Building-Classes from the database.
29 @param id: int - building id in the database."""
30 def __new__(self, id):
31 class_package, class_name = game.main.db("SELECT class_package, class_type FROM data.building WHERE rowid = ?", id)[0]
32 __import__('game.world.building.'+class_package)
35 @classmethod
36 def load(cls, db, worldid):
37 self = cls.__new__(cls)
38 super(cls, self).load(db, worldid)
39 return self
41 return type.__new__(self, 'Building[' + str(id) + ']',
42 (getattr(globals()[class_package], class_name),),
43 {"load": load})
45 def __init__(self, id, **kwargs):
46 """
47 @param id: building id.
48 """
49 super(BuildingClass, self).__init__(**kwargs)
50 self.id = id
51 self._object = None
52 (size_x, size_y) = game.main.db("SELECT size_x, size_y FROM data.building WHERE rowid = ?", id)[0]
53 self.name = game.main.db("SELECT name FROM data.building WHERE rowid = ?", id)[0][0]
54 self.size = (int(size_x), int(size_y))
55 self.radius = game.main.db("SELECT radius FROM data.building WHERE rowid = ?", id)[0][0]
56 self.health = int(game.main.db("SELECT health FROM data.building WHERE rowid = ?", id)[0][0])
57 self.inhabitants = int(game.main.db("SELECT inhabitants_start FROM data.building WHERE rowid = ?", id)[0][0])
58 self.inhabitants_max = int(game.main.db("SELECT inhabitants_max FROM data.building WHERE rowid = ?", id)[0][0])
59 for (name, value) in game.main.db("SELECT name, value FROM data.building_property WHERE building = ?", str(id)):
60 setattr(self, name, value)
61 self.costs = {}
62 for (name, value) in game.main.db("SELECT resource, amount FROM data.building_costs WHERE building = ?", str(id)):
63 self.costs[name]=value
64 self._loadObject()
66 def load(cls, db, worldid):
67 self = cls.__new__(cls)
68 self.load(db, worlid)
69 return self
71 def _loadObject(cls):
72 """Loads building from the db.
73 """
74 print 'Loading building #' + str(cls.id) + '...'
75 try:
76 cls._object = game.main.session.view.model.createObject(str(cls.id), 'building')
77 except RuntimeError:
78 print 'already loaded...'
79 cls._object = game.main.session.view.model.getObject(str(cls.id), 'building')
80 return
82 for (action_id,) in game.main.db("SELECT action FROM data.action where building=? group by action", cls.id):
83 action = cls._object.createAction(action_id)
84 fife.ActionVisual.create(action)
85 for rotation, animation_id in game.main.db("SELECT rotation, animation FROM data.action where building=? and action=?", cls.id, action_id):
86 if cls.id == 1:
87 print "HALLO",cls.size,action_id, rotation, animation_id
88 if rotation == 45:
89 command = 'left-16,bottom+' + str(cls.size[0] * 8)
90 elif rotation == 135:
91 command = 'left-' + str(cls.size[1] * 16) + ',bottom+8'
92 elif rotation == 225:
93 command = 'left-' + str((cls.size[0] + cls.size[1] - 1) * 16) + ',bottom+' + str(cls.size[1] * 8)
94 elif rotation == 315:
95 command = 'left-' + str(cls.size[0] * 16) + ',bottom+' + str((cls.size[0] + cls.size[1] - 1) * 8)
96 else:
97 print "ERROR"
98 continue
99 command = 'left-16,bottom+8'
100 if cls.id == 1:
101 print command
102 anim_id = game.main.fife.animationpool.addResourceFromFile(str(animation_id) + ':shift:' + command)
103 action.get2dGfxVisual().addAnimation(int(rotation), anim_id)
104 action.setDuration(game.main.fife.animationpool.getAnimation(anim_id).getDuration())
106 #old code, but with "correcter" image positioning
107 """for rotation, file in game.main.db("SELECT rotation, (select file from data.animation where data.animation.animation_id = data.action.animation order by frame_end limit 1) FROM data.action where object=?", cls.id):
108 img = game.main.fife.imagepool.addResourceFromFile(str(file))
109 visual.addStaticImage(int(rotation), img)
110 img = game.main.fife.imagepool.getImage(img)
111 shift_x = 1 + img.getWidth() / 2 //left
112 shift_y = img.getHeight() / -2 //bottom
113 #currently a bit useless
114 if rotation == 45:
115 shift_x = shift_x - 15
116 shift_y = shift_y + cls.size[0] * 8
117 elif rotation == 135:
118 shift_x = shift_x - cls.size[1] * 16
119 shift_y = shift_y + 8
120 elif rotation == 225:
121 shift_x = shift_x - (cls.size[0] + cls.size[1] - 1) * 16
122 shift_y = shift_y + cls.size[1] * 8
123 elif rotation == 315:
124 shift_x = shift_x - cls.size[0] * 16
125 shift_y = shift_y + (cls.size[0] + cls.size[1] - 1) * 8
126 img.setXShift(shift_x)
127 img.setYShift(shift_y)"""