# HG changeset patch # User Jeremy Thurgood # Date 1336423434 -7200 # Node ID 168cfac9a445038e798bb56a10a4d9b318f2b575 # Parent 655a6912e0ae89b930f555dbf2c0434f69276c54 Power and reliability. diff -r 655a6912e0ae -r 168cfac9a445 gamelib/schematics.py --- 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" diff -r 655a6912e0ae -r 168cfac9a445 gamelib/tests/test_schematics.py --- /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())