changeset 138:14917385a0fd

Better handling of mission results and turn-end messages.
author Jeremy Thurgood <firxen@gmail.com>
date Thu, 10 May 2012 22:33:26 +0200
parents fb8037bc22f1
children 07be37129447
files gamelib/constants.py gamelib/gamegui.py gamelib/gamestate.py gamelib/lab.py gamelib/missions.py
diffstat 5 files changed, 58 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/constants.py	Thu May 10 20:56:40 2012 +0200
+++ b/gamelib/constants.py	Thu May 10 22:33:26 2012 +0200
@@ -12,8 +12,7 @@
 BUFFER = 1024
 
 # Result codes for UI hints
-(MAJOR_SETBACK, FAILURE, SUCCESS, MAJOR_SUCCESS, GAME_WIN, NEW_SCIENCE,
-        NEW_SCHEMATIC, NEW_MILESTONE) = range(8)
+(MAJOR_SETBACK, FAILURE, SUCCESS, MAJOR_SUCCESS, GAME_WIN, INFO) = range(6)
 
 # Planning to take over the:
 MILESTONES = ("basement", "neighbourhood", "city", "world")
--- a/gamelib/gamegui.py	Thu May 10 20:56:40 2012 +0200
+++ b/gamelib/gamegui.py	Thu May 10 22:33:26 2012 +0200
@@ -17,8 +17,7 @@
         font_medium, font_large)
 from gamelib.gui import BigButton, ImageDrawable
 from gamelib.engine import PopWindow, AddWindow, GameOver
-from gamelib.constants import (WIDTH, HEIGHT, FAILURE, SUCCESS, NEW_SCIENCE,
-        NEW_SCHEMATIC, GAME_WIN)
+from gamelib.constants import WIDTH, HEIGHT, FAILURE, SUCCESS, GAME_WIN, INFO
 from gamelib.gamestate import Game
 
 
@@ -325,26 +324,36 @@
             self.add_child(results)
         else:
             y = 200
-            for msg_type, msg in messages:
+            for msg_type, msg, loot in messages:
                 # FIXME: Better widgets
-                if msg_type in [NEW_SCHEMATIC, NEW_SCIENCE]:
-                    text = TextBox((50, y, 750, 25),
-                            msg, font_medium, (60, 60, 255))
+                if msg_type == INFO:
+                    y = self.display_message(y, msg, loot, (60, 60, 255))
                 elif msg_type == FAILURE:
-                    text = TextBox((50, y, 750, 25),
-                            msg, font_medium, (255, 60, 60))
+                    y = self.display_message(y, msg, loot, (255, 60, 60))
                 elif msg_type == SUCCESS:
-                    text = TextBox((50, y, 750, 25),
-                            msg, font_medium, (60, 255, 60))
+                    y = self.display_message(y, msg, loot, (60, 255, 60))
                 elif msg_type == GAME_WIN:
                     self._make_win_screen(turn, msg)
                     self.is_game_over = True
                     break
                 else:
-                    text = TextBox((50, y, 750, 25),
-                            msg, font_medium, (255, 255, 0))
-                y += text.rect.height + 10
-                self.add_child(text)
+                    y = self.display_message(y, msg, loot, (255, 255, 0))
+
+    def display_message(self, y, msg, loot, colour, font=font_medium):
+        text = TextBox((50, y, 700, 25), msg, font, colour)
+        y += text.rect.height + 5
+        self.add_child(text)
+        for kind, value in loot.items():
+            y += self.display_loot_item(y, kind, value)
+        return y + 5
+
+    def display_loot_item(self, y, kind, value):
+        if hasattr(value, 'NAME'):
+            value = value.NAME
+        msg = "* %s: %s" % (kind, value)
+        text = TextBox((60, y, 690, 25), msg, font_medium, (255, 255, 60))
+        self.add_child(text)
+        return text.rect.height + 5
 
     def _make_win_screen(self, turn, msg):
         # Clear existing widgets, and turn this into a won screen
--- a/gamelib/gamestate.py	Thu May 10 20:56:40 2012 +0200
+++ b/gamelib/gamestate.py	Thu May 10 22:33:26 2012 +0200
@@ -5,8 +5,7 @@
 
 from gamelib import missions, lab
 from gamelib.game_base import get_subclasses
-from gamelib.constants import (NEW_SCIENCE, NEW_SCHEMATIC, M_VALS, MILESTONES,
-        NEW_MILESTONE)
+from gamelib.constants import M_VALS, INFO
 
 
 class Game(object):
@@ -61,6 +60,10 @@
     def get_available_points(self):
         return self.points - len(self.cur_allocation)
 
+    def apply_mission_special(self, new_milestone=None):
+        if new_milestone:
+            self.milestone = new_milestone
+
     def end_turn(self):
         # Attempt the missions
         mission_results = []
@@ -80,18 +83,11 @@
         messages = []
         for result in mission_results:
             result.apply(self)
-            messages.append((result.outcome, result.text))
-            if result.outcome == NEW_MILESTONE:
-                self.milestone = MILESTONES[M_VALS[self.milestone] + 1]
+            messages.append((result.outcome, result.text, result.loot))
         for science in new_stuff:
             # FIXME: Update UI better.
-            if science.SCIENCE_TYPE == 'research':
-                messages.append((NEW_SCIENCE,
-                    "You've started a new line of research in %s"
-                    % science.NAME))
-            else:
-                messages.append((NEW_SCHEMATIC,
-                    "You've developed blueprints for a %s" % science.NAME))
+            messages.append((INFO, "SCIENCE breakthrough!", {
+                        science.SCIENCE_TYPE: science}))
         return messages
 
     def save_data(self):
--- a/gamelib/lab.py	Thu May 10 20:56:40 2012 +0200
+++ b/gamelib/lab.py	Thu May 10 22:33:26 2012 +0200
@@ -128,7 +128,7 @@
         return breakthroughs
 
     def apply_basic_research(self, basic_research):
-        if basic_research <= 1:
+        if basic_research <= 0:
             return []
 
         options = self.find_new_research()
--- a/gamelib/missions.py	Thu May 10 20:56:40 2012 +0200
+++ b/gamelib/missions.py	Thu May 10 22:33:26 2012 +0200
@@ -3,7 +3,7 @@
 
 from random import randint
 
-from gamelib.constants import SUCCESS, FAILURE, GAME_WIN, NEW_MILESTONE, M_VALS
+from gamelib.constants import SUCCESS, FAILURE, GAME_WIN, M_VALS
 from gamelib.schematics import cat
 
 
@@ -13,17 +13,30 @@
     This is an exception so we can throw it from inside helper methods.
     """
 
-    def __init__(self, outcome, msg, money=0, rep=0):
+    def __init__(self, outcome, msg, money=0, rep=0, **special):
         self.outcome = outcome
         self.money = money
         self.reputation = rep
         self.text = msg
+        self.special = special
         self.applied = False
 
+    @property
+    def loot(self):
+        loot = {}
+        if self.money != 0:
+            loot['money'] = self.money
+        if self.reputation != 0:
+            loot['rep'] = self.reputation
+        loot.update(self.special)
+        loot.pop('new_milestone', None)  # This one's special.
+        return loot
+
     def apply(self, state):
         if not self.applied:
             state.money += self.money
             state.reputation += self.reputation
+            state.apply_mission_special(self.special)
             self.applied = True
         else:
             raise RuntimeError('attempted to apply result twice')
@@ -92,15 +105,15 @@
             return False
         return True
 
-    def fail(self, msg=None, money=0, rep=0):
+    def fail(self, msg=None, money=0, rep=0, **special):
         if msg is None:
             msg = self.GENERIC_FAILURE
-        raise Result(FAILURE, msg, money=money, rep=rep)
+        raise Result(FAILURE, msg, money=money, rep=rep, **special)
 
-    def succeed(self, msg=None, money=0, rep=0):
+    def succeed(self, msg=None, money=0, rep=0, **special):
         if msg is None:
             msg = self.GENERIC_SUCCESS
-        raise Result(SUCCESS, msg, money=money, rep=rep)
+        raise Result(SUCCESS, msg, money=money, rep=rep, **special)
 
     def categorise_equipment(self, equipment):
         # Categorise equipment for easier decision-making.
@@ -385,11 +398,12 @@
     def attempt_with(self, categorised, state):
         if all(c in categorised for c in (cat.MIND_CONTROL, cat.HAND_WEAPON)):
             self.data['completed'] = True
-            raise Result(NEW_MILESTONE, "Guns and persuasion, that's"
-                         " all you need. It's early days still, but you're"
-                         " finally out of that pokey basement and have some"
-                         " elbow room to work with. Next step: the city!",
-                         money=randint(1000, 2000), rep=randint(5, 15))
+            self.succeed(
+                "Guns and persuasion, that's all you need. It's early days"
+                " still, but you're finally out of that pokey basement and"
+                " have some elbow room to work with. Next step: the city!",
+                money=randint(1000, 2000), rep=randint(5, 15),
+                new_milestone="neighbourhood")
 
         if cat.HAND_WEAPON in categorised:
             self.succeed(