changeset 54:168cfac9a445

Power and reliability.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 07 May 2012 22:43:54 +0200
parents 655a6912e0ae
children 86d83dcb7d42
files gamelib/schematics.py gamelib/tests/test_schematics.py
diffstat 2 files changed, 84 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/schematics.py	Mon May 07 22:10:26 2012 +0200
+++ b/gamelib/schematics.py	Mon May 07 22:43:54 2012 +0200
@@ -1,3 +1,5 @@
+# -*- test-case-name: gamelib.tests.test_schematics -*-
+
 from gamelib import research
 from gamelib.game_base import Science
 
@@ -10,12 +12,23 @@
 
 
 class Schematic(Science):
+    # For all Schematics
+    SCIENCE_TYPE = 'schematic'
+
+    # Acquisition
+    STARTING_PRODUCT = False
     ACQUISITION_CHANCE = 0.8
-    COST = None
+    CATEGORIES = ()
+
+    # Costs
     UPGRADE_REQUIREMENT = 1
-    STARTING_PRODUCT = False
-    SCIENCE_TYPE = 'schematic'
-    CATEGORIES = ()
+    COST = None
+
+    # Power and reliability
+    PROTOTYPE_RELIABILITY = 0.4
+    PRODUCTION_RELIABILITY = 0.8
+    BASE_POWER = None
+    POWER_INCREMENT = None
 
     def can_spend(self, lab):
         extra = self.UPGRADE_REQUIREMENT * self.points + 1
@@ -24,6 +37,18 @@
     def is_a(self, category):
         return category in self.CATEGORIES
 
+    def reliability(self):
+        if self.points:
+            exp = 1 + 0.5 * (self.points - 1)
+            return 1.0 - ((1.0 - self.PRODUCTION_RELIABILITY) ** exp)
+        return self.PROTOTYPE_RELIABILITY
+
+    def power(self):
+        power = self.BASE_POWER
+        if None not in (power, self.POWER_INCREMENT):
+            power += self.POWER_INCREMENT * self.points
+        return power
+
 
 class MachineGun(Schematic):
     NAME = "Machine gun"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gamelib/tests/test_schematics.py	Mon May 07 22:43:54 2012 +0200
@@ -0,0 +1,55 @@
+from unittest import TestCase
+
+from gamelib.schematics import Schematic
+
+
+class WaterPistol(Schematic):
+    NAME = "water pistol"
+
+    # Power and reliability
+    PROTOTYPE_RELIABILITY = 0.4
+    PRODUCTION_RELIABILITY = 0.8
+    BASE_POWER = 5
+    POWER_INCREMENT = 2
+
+
+class TestSchematic(TestCase):
+    def assertNearlyEqual(self, a, b, epsilon=0.001):
+        if abs(a - b) > epsilon:
+            self.assertEqual(a, b)
+
+    def test_schematic_reliability(self):
+        gun = WaterPistol()
+        self.assertNearlyEqual(0.4, gun.reliability())
+        gun.spend_point()
+        self.assertNearlyEqual(0.8, gun.reliability())
+        gun.spend_point()
+        self.assertNearlyEqual(0.91, gun.reliability())
+        gun.spend_point()
+        self.assertNearlyEqual(0.96, gun.reliability())
+
+    def test_schematic_power(self):
+        gun = WaterPistol()
+        self.assertEqual(5, gun.power())
+        gun.spend_point()
+        self.assertEqual(7, gun.power())
+        gun.spend_point()
+        self.assertEqual(9, gun.power())
+        gun.spend_point()
+        self.assertEqual(11, gun.power())
+
+    def test_schematic_power_none(self):
+        gun = WaterPistol()
+        gun.BASE_POWER = None
+        self.assertEqual(None, gun.power())
+        gun.spend_point()
+        self.assertEqual(None, gun.power())
+
+    def test_schematic_power_static(self):
+        gun = WaterPistol()
+        gun.POWER_INCREMENT = None
+        self.assertEqual(5, gun.power())
+        gun.spend_point()
+        self.assertEqual(5, gun.power())
+        gun.spend_point()
+        self.assertEqual(5, gun.power())