corrected power usage calculation to exclude area within inner radius
[tabakrolletjie.git] / tabakrolletjie / lights.py
index 318aefca3ca2579a4c36cc0b373944c2a91b455a..06f84badbb88c90034e2cfda6f129547093ffebe 100644 (file)
@@ -137,6 +137,23 @@ def light_fitting_by_type(light_type):
     return BaseLight.find_cls(light_type).FITTING_IMG
 
 
+def seed_cost(light_config, num_colours):
+    """Calculate a seed cost for a light from its configuration. """
+    cls = BaseLight.find_cls(light_config["type"])
+    return cls.BASE_COST + int(cls.find_cost(light_config) / 10) + num_colours
+
+def light_info(light_config):
+    """Generate info about a light to go in the tooltip. """
+    cls = BaseLight.find_cls(light_config["type"])
+    return cls.get_info(light_config)
+
+
+def light_name(light_config):
+    """Find formatted light name. """
+    cls = BaseLight.find_cls(light_config["type"])
+    return cls.NAME
+
+
 class BaseLight(object):
     """ Common light functionality. """
 
@@ -144,6 +161,8 @@ class BaseLight(object):
     RAY_MANAGER = RayPolyManager
     FITTING_IMG = None
     FITTING_RADIUS = 24.0
+    BASE_COST = 0
+    NAME = "light"
 
     # cached surfaces
     _surface_cache = {}
@@ -204,6 +223,15 @@ class BaseLight(object):
             if c.__name__.lower() == light_type]
         return light_class
 
+    @classmethod
+    def find_cost(cls, config):
+        cost = 5 * config["intensity"]
+        return cost
+
+    @classmethod
+    def get_info(cls, config):
+        return ["intensity: %g" % config["intensity"]]
+
     def add(self, space):
         if self.body.space is not None:
             space.remove(self.body, *self.body.shapes)
@@ -285,8 +313,9 @@ class BaseLight(object):
     def power_usage(self):
         if not self.on:
             return 0.0
-        area = math.pi * (self.ray_manager.max_radius ** 2)  # radius
-        area = area * (self.ray_manager.spread / (2 * math.pi))  # spread
+        rm = self.ray_manager
+        area = math.pi * (rm.max_radius ** 2 - rm.min_radius ** 2)  # radius
+        area = area * (rm.spread / (2 * math.pi))  # spread
         return 5 * area * self.intensity / 6400  # 80x80 unit area
 
     def base_damage(self):
@@ -313,6 +342,8 @@ class BaseLight(object):
 class Lamp(BaseLight):
 
     FITTING_IMG = "lamp.png"
+    BASE_COST = 1
+    NAME = "lamp"
 
 
 class PulsatingLamp(BaseLight):
@@ -322,6 +353,8 @@ class PulsatingLamp(BaseLight):
     DEFAULT_PULSE_VELOCITY = 2
     DEFAULT_INTENSITY_RANGE = (0.0, 0.9)
     DEFAULT_INTENSITY_VELOCITY = 0.1
+    BASE_COST = 3
+    NAME = "pulsating lamp"
 
     def __init__(self, **kw):
         self.pulse_range = kw.pop("pulse_range", self.DEFAULT_PULSE_RANGE)
@@ -357,10 +390,34 @@ class PulsatingLamp(BaseLight):
         self.intensity, self.intensity_velocity = self._update_range(
             self.intensity, self.intensity_velocity, self.intensity_range)
 
+    @classmethod
+    def find_cost(cls, config):
+        cost = super(PulsatingLamp, cls).find_cost(config)
+        cost += config.get("pulse_velocity", cls.DEFAULT_PULSE_VELOCITY)
+        pr = config.get("pulse_range", cls.DEFAULT_PULSE_RANGE)
+        cost += (pr[1] - pr[0]) / 10
+        cost += 5 * config.get("intensity_velocity", cls.DEFAULT_INTENSITY_VELOCITY)
+        ir = config.get("intensity_range", cls.DEFAULT_INTENSITY_RANGE)
+        cost += 5 * (ir[1] - ir[0])
+        return cost
+
+    @classmethod
+    def get_info(cls, config):
+        pr = config.get("pulse_range", cls.DEFAULT_PULSE_RANGE)
+        pv = config.get("pulse_velocity", cls.DEFAULT_PULSE_VELOCITY)
+        ir = config.get("intensity_range", cls.DEFAULT_INTENSITY_RANGE)
+        iv = config.get("intensity_velocity", cls.DEFAULT_INTENSITY_VELOCITY)
+        return [
+            "intensity: %g - %g, velocity %g" % (ir[0], ir[1], iv),
+            "pulse: %d - %d, velocity %g" % (pr[0], pr[1], pv),
+        ]
+
 
 class SpotLight(BaseLight):
 
     FITTING_IMG = "spotlight.png"
+    BASE_COST = 5
+    NAME = "spotlight"
 
     def __init__(self, **kw):
         self.angular_velocity = kw.pop("angular_velocity", None)
@@ -386,3 +443,23 @@ class SpotLight(BaseLight):
         if self.angular_velocity:
             self.ray_manager.direction -= self.angular_velocity
             self.ray_manager.update_shapes()
+
+    @classmethod
+    def find_cost(cls, config):
+        cost = super(SpotLight, cls).find_cost(config)
+        cost += config.get("angular_velocity", 0)
+        cost += config["spread"] / 10
+        rl = config["radius_limits"]
+        cost += (rl[1] - rl[0]) / 10
+        return cost
+
+    @classmethod
+    def get_info(cls, config):
+        info = super(SpotLight, cls).get_info(config)
+        rl = config["radius_limits"]
+        info.extend([
+            "spread: %d" % config["spread"],
+            "length: %d" % (rl[1] - rl[0]),
+            "angular velocity: %g" % config.get("angular_velocity", 0),
+        ])
+        return info