Deavid: Improvements to prevent steam loss
[lightyears.git] / code / steam_model.py
blob4d854dd13457f6f5401d91dee238dd8300cd48f6
2 # 20,000 Light Years Into Space
3 # This game is licensed under GPL v2, and copyright (C) Jack Whitham 2006-07.
6 # A model for the movement of steam within the system.
9 from primitives import *
12 class Voltage_Model:
13 # Don't understand steam? Confused by the thought of
14 # boiling water being used as a source of energy?
15 # Why not just assume that it's electricity.
17 def __init__(self,node):
18 # Invariant:
19 self.capacitance = 1.0
20 # May change:
21 self.charge = 0.0
22 self.voltage = 0.0
23 # Changed by upgrades
24 self.capacity = INITIAL_NODE_CAPACITY
25 self.node = node
27 NEGLIGIBLE = 0.05
28 TIME_CONSTANT = 0.1
30 def Source(self, current):
31 dq = current * self.TIME_CONSTANT
32 self.charge += dq
33 self.__Bound()
35 def Think(self, neighbour_list):
36 self.voltage = self.charge / self.capacitance
37 currents = []
39 for (neighbour, resist, max_intensity) in neighbour_list:
40 dir = 0
41 # Potential difference:
42 dv = self.voltage - neighbour.voltage
43 if ( dv >= self.NEGLIGIBLE ):
44 # Current flow:
45 if (resist<1.0):
46 resist=1.0
48 i = dv / resist
49 # Charge flow:
50 dq = i * self.TIME_CONSTANT
52 adq=math.fabs(dq);
53 if (adq>1): dq/=math.sqrt(adq) # Limit accurracy problems
54 # Apply Intensity Limit
55 if (dq>max_intensity*self.TIME_CONSTANT):
56 dq=max_intensity*self.TIME_CONSTANT
58 if (dq<-max_intensity*self.TIME_CONSTANT):
59 dq=-max_intensity*self.TIME_CONSTANT
61 if ( self.charge-dq > self.capacity ):
62 dq=self.charge-self.capacity
63 if ( neighbour.charge+dq > neighbour.capacity ):
64 dq=neighbour.capacity-neighbour.charge
66 i = dq / self.TIME_CONSTANT
69 self.charge -= dq
70 neighbour.charge += dq
71 currents.append(i)
72 else:
73 currents.append(0.0)
74 self.__Bound()
75 return currents
77 def __Bound(self):
78 original_charge=self.charge
79 if ( self.charge < 0 ):
80 self.charge = 0
81 elif ( self.charge > self.capacity ):
82 self.charge = self.capacity # vent
84 diff_charge=original_charge-self.charge
86 if len(self.node.pipes)>0:
87 diff_charge/=len(self.node.pipes)
89 for pipe in self.node.pipes:
90 if pipe.n1==self.node: dest=pipe.n2
91 if pipe.n2==self.node: dest=pipe.n1
92 dest.steam.charge+=diff_charge
96 def Get_Pressure(self):
97 return self.charge
99 def Get_Capacity(self):
100 return self.capacity
102 def Capacity_Upgrade(self):
103 self.capacity += CAPACITY_UPGRADE
105 class Steam_Model(Voltage_Model):
106 pass