Package horizons :: Package world :: Package production :: Module utilization
[hide private]
[frames] | no frames]

Source Code for Module horizons.world.production.utilization

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  from horizons.constants import PRODUCTION 
23  from horizons.scheduler import Scheduler 
24   
25   
26 -class Utilization:
27 """Basic utilization class used in producers""" 28
29 - def capacity_utilization(self, instance):
30 productions = instance.get_productions() 31 total = 0 32 if not productions: 33 return 0 # catch the border case, else there'll be a div by 0 34 for production in productions: 35 state_history = production.get_state_history_times(False) 36 total += state_history[PRODUCTION.STATES.producing.index] 37 return total / len(productions)
38
39 - def capacity_utilization_below(self, limit, instance):
40 """Returns whether the capacity utilization is below a value. 41 It is equivalent to "foo.capacity_utilization <= value, but faster.""" 42 # idea: retrieve the value, then check how long it has to take until the limit 43 # can be reached (from both sides). Within this timespan, don't check again. 44 cur_tick = Scheduler().cur_tick 45 if not hasattr(self, "_old_capacity_utilization") or \ 46 self._old_capacity_utilization[0] < cur_tick or \ 47 self._old_capacity_utilization[1] != limit: 48 capac = self.capacity_utilization(instance) 49 diff = abs(limit - capac) 50 # all those values are relative values, so we can just do this: 51 interval = diff * PRODUCTION.STATISTICAL_WINDOW 52 self._old_capacity_utilization = (cur_tick + interval, # expiration date 53 limit, capac < limit) 54 return self._old_capacity_utilization[2]
55 56
57 -class FullUtilization(Utilization):
58 """Used for producers where no utilization calculation is necessary""" 59
60 - def capacity_utilization(self, instance):
61 return 1.0
62 63
64 -class FieldUtilization(Utilization):
65 66 max_fields_possible = 8 # only for utilization calculation 67
68 - def capacity_utilization(self, instance):
69 """ 70 Calculate productivity by the number of fields nearby. 71 """ 72 73 result = float(len(instance.instance.get_providers())) / self.max_fields_possible 74 # sanity checks for theoretically impossible cases: 75 result = min(result, 1.0) 76 result = max(result, 0.0) 77 return result
78