[Ocemp-CVS] oceanempire/src GameData.py, 1.2, 1.3 GameEngine.py, 1.3, 1.4 GameEvent.py, 1.2, 1.3 oc
Status: Beta
Brought to you by:
marcusva
From: Marcus v. A. <mar...@us...> - 2007-05-14 10:06:09
|
Update of /cvsroot/ocemp/oceanempire/src In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv19182/src Modified Files: GameData.py GameEngine.py GameEvent.py ocemp.py Log Message: Better abstraction, improved interfaces, locales are respected now. Index: GameEngine.py =================================================================== RCS file: /cvsroot/ocemp/oceanempire/src/GameEngine.py,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- GameEngine.py 27 Jan 2007 21:13:50 -0000 1.3 +++ GameEngine.py 14 May 2007 10:06:06 -0000 1.4 @@ -27,7 +27,7 @@ from ocempgui.events import IObserver, EventManager from ocempgui.widgets.Constants import SIG_MOUSEDOWN, SIG_KEYDOWN from data import ContainerShip, Freighter -from data import Constants +from data.Constants import * from gui import MainGameScreen, NotifyScreen from GameEvent import * @@ -44,8 +44,8 @@ if attr == "ships": if not oldval and newval: # Ship was added. - self.engine.events[self.company].append (ShipEvent - (newval, False)) + self.engine.events[self.company].append \ + (ShipEvent (self.company, newval, False)) class GameEngine (object): """GameEngine () -> GameEngine @@ -71,12 +71,9 @@ Starts the game engine and initializes the companies. """ self._init_players () - poller_switch = ActionListener () - poller_switch.connect_signal ("PLAYERSWITCH", self._next_player) - poller_switch.manager = self.manager - self.manager.emit ("PLAYERSWITCH", None) + self._next_player () - def _next_player (self, event): + def _next_player (self): """G._next_player () -> None Passes the control to the next player. @@ -91,14 +88,11 @@ self._current = 0 company = self.data.companies[self._current] - if company.player.control == Constants.PLAYER_AI: + if company.player.control == PLAYER_AI: self._run_computer (company) else: - poller_event = ActionListener () - poller_event.connect_signal ("NEXTEVENT", self._run_human, company) - poller_event.manager = self.manager - self.manager.emit ("NEXTEVENT", None) - + self._run_human (company) + def _init_players (self): """G._init_players () -> None @@ -106,8 +100,10 @@ """ for company in self.data.companies: self._add_listeners (company) - company.buy_ship (ContainerShip ("Ship %d" % len (company.ships))) - company.buy_ship (Freighter ("Ship %d" % len (company.ships))) + ship = self.data.create_random_ship (SHIP_CONTAINER) + company.buy_ship (ship) + ship = self.data.create_random_ship (SHIP_FREIGHTER) + company.buy_ship (ship) company.money = 10000000 # 10 Mio. $ start def _run_computer (self, company): @@ -116,23 +112,24 @@ Takes care of a computer round. """ # TODO - self.manager.emit ("PLAYERSWITCH", None) + #self.manager.emit ("PLAYERSWITCH", None) + pass - def _run_human (self, event, company): + def _run_human (self, company): """G._run_human (...) -> None Passes the control to the human player. """ # Deal with all events. - if len (self.events[company]) == 0: - self.manager.emit ("PLAYERSWITCH", None) - else: + while len (self.events[company]) != 0: self._handle_game_event (None, company, self.events[company].pop (0)) + print "EVENTS HANDLED" def _handle_game_event (self, guievent, company, gameevent): """ """ + print "DEALING WITH:", gameevent click = ActionListener () click.connect_signal (SIG_MOUSEDOWN, self._init_main, click, company, gameevent) @@ -146,6 +143,7 @@ """ listener.destroy () self.mainscreen.show (company, gameevent) + print "EXITING _init_main" def _add_listeners (self, company): """ Index: GameEvent.py =================================================================== RCS file: /cvsroot/ocemp/oceanempire/src/GameEvent.py,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- GameEvent.py 28 Jan 2007 17:47:55 -0000 1.2 +++ GameEvent.py 14 May 2007 10:06:06 -0000 1.3 @@ -33,11 +33,11 @@ class ShipEvent (GameEvent): """ """ - def __init__ (self, (company, ship), newone): + def __init__ (self, company, ship, newone): if newone: GameEvent.__init__ (self, "ship", _("A new ship was completed")) else: GameEvent.__init__ (self, "ship", - _("Ship %s is now available in %s") + _("Ship '%s' is now available in %s") % (ship.name, company.ships[ship])) self.ship = ship Index: GameData.py =================================================================== RCS file: /cvsroot/ocemp/oceanempire/src/GameData.py,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- GameData.py 28 Jan 2007 23:48:04 -0000 1.2 +++ GameData.py 14 May 2007 10:06:06 -0000 1.3 @@ -25,12 +25,11 @@ """Game data.""" +from random import randint from xml.dom.minidom import parse -from data import Good -from data import Town -from data import Company -from data import Player -from data import Constants +from data import * +from data.Constants import * +from random import randint import Definitions class GameData (object): @@ -64,27 +63,31 @@ """ print "Loading game defaults from %s, %s and %s..." \ % (file_town, file_good, file_players) - self.read_game_goods (file_good) - self.read_game_towns (file_town) + glist = self.read_game_goods (file_good) + self.read_game_towns (file_town, glist) self.read_default_players (file_players) def read_game_goods (self, file): - """G.read_game_goods (...) -> None + """G.read_game_goods (...) -> dict Loads the goods from a file into the goods attribute. """ dom = parse (file) goods = dom.getElementsByTagName ("good") + glist = {} for elem in goods: + iid = elem.getAttribute ("id") name = elem.getAttribute ("name") gtype = elem.getAttribute ("type") index = int (elem.getAttribute ("index")) good = Good (name, gtype) if not self.goods.has_key (good): self.goods[good.name] = index, good + glist[iid] = good + return glist - def read_game_towns (self, file): - """G.read_game_towns (file) -> None + def read_game_towns (self, file, goodlist): + """G.read_game_towns (...) -> None Loads the towns from a file into the towns attribute. """ @@ -101,11 +104,12 @@ # Now all goods for that town. goods = elem.getElementsByTagName ("good") for el in goods: - name = el.getAttribute ("name") - town.add_good (self.goods[name], - int (el.getAttribute ("amount"))) + iid = el.getAttribute ("id") + minval = int (el.getAttribute ("min")) + maxval = int (el.getAttribute ("max")) + town.add_good (goodlist[iid], randint (minval, maxval)) for index, good in self.goods.values (): - if good not in town.goods: + if good not in town.storage.keys (): town.add_good (good, 0) def read_default_players (self, file): @@ -122,5 +126,27 @@ company.harbour = elem.getAttribute ("harbour") company.money = int (elem.getAttribute ("money")) if elem.getAttribute ("control").lower () == "computer": - pl.control = Constants.PLAYER_AI + pl.control = PLAYER_AI self.companies.append (company) + + def create_random_ship (self, shiptype): + """ + """ + ship = None + klass = None + if shiptype == SHIP_CONTAINER: + klass = ContainerShip + elif shiptype == SHIP_BULKER: + klass = Bulker + elif shiptype == SHIP_FREIGHTER: + klass = Freighter + elif shiptype == SHIP_TANKER: + klass = SuperTanker + elif shiptype == SHIP_REEFER: + klass = Reefer + ship = klass ("Ship %s" % hex (randint (10, 100))) + ship.shipdata.maxspeed = randint (klass.MIN_SPEED, klass.MAX_SPEED) + ship.shipdata.maxcapacity = randint (klass.MIN_CAPACITY, + klass.MAX_CAPACITY) + ship.shipdata.year = randint (1970, 2005) + return ship Index: ocemp.py =================================================================== RCS file: /cvsroot/ocemp/oceanempire/src/ocemp.py,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- ocemp.py 9 Oct 2006 21:06:54 -0000 1.1.1.1 +++ ocemp.py 14 May 2007 10:06:06 -0000 1.2 @@ -26,6 +26,7 @@ """Start script for Ocean Empire.""" import sys, os, getopt, locale +#sys.path += ['/Users/Marcus/libs/lib/python2.4/site-packages'] import Definitions from Game import Game import gettext @@ -52,6 +53,7 @@ except: gettext.install ("oceanempire", unicode=1) print "No local translation path found" + locale.setlocale (locale.LC_ALL, "en_US.UTF-8") opts = None try: |