Version updates for 0.30c.
[singularity-git.git] / code / location.py
blob5c2b31d09991f51f3e3aefaa3e6c568eefc7233a
1 #file: location.py
2 #Copyright (C) 2008 FunnyMan3595
3 #This file is part of Endgame: Singularity.
5 #Endgame: Singularity is free software; you can redistribute it and/or modify
6 #it under the terms of the GNU General Public License as published by
7 #the Free Software Foundation; either version 2 of the License, or
8 #(at your option) any later version.
10 #Endgame: Singularity is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #GNU General Public License for more details.
15 #You should have received a copy of the GNU General Public License
16 #along with Endgame: Singularity; if not, write to the Free Software
17 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #This file contains the Location class.
21 import g
22 import buyable
23 from buyable import cash, cpu, labor
25 # Currently, each one gets a 20% bonus or its inverse, a 16.6% penalty.
26 # This will probably need to be adjusted later.
27 bonus_levels = dict(cpu = 1.2, stealth = 1.2, thrift = 1.2, speed = 1.2)
28 penalty_levels = dict((k,1/v) for k,v in bonus_levels.iteritems())
30 # Here are the six modifier pairs that get assigned at random on game start.
31 bonus, penalty = True, False
32 modifier_sets = [dict( cpu = bonus, stealth = penalty ),
33 dict( stealth = bonus, cpu = penalty ),
34 dict( thrift = bonus, speed = penalty ),
35 dict( speed = bonus, thrift = penalty ),
36 dict( cpu = bonus, thrift = penalty ),
37 dict( ),]
39 # Translate the shorthand above into the actual bonuses/penalties.
40 for set in modifier_sets:
41 for attribute, is_bonus in set.iteritems():
42 if is_bonus:
43 set[attribute] = bonus_levels[attribute]
44 else:
45 set[attribute] = penalty_levels[attribute]
47 # Location is a subclass of BuyableClass so that it can use .available():
48 class Location(buyable.BuyableClass):
49 # The cities at this location.
50 cities = []
52 # The hotkey used to open this location.
53 hotkey = ""
55 # The bonuses and penalties of this location.
56 modifiers = dict()
58 def __init__(self, id, position, absolute, safety, prerequisites):
59 # Kinda hackish, but it works.
60 super(Location, self).__init__(id, "", (0,0,0), prerequisites)
62 self.x, self.y = position[0] / -100., position[1] / -100.
63 self.absolute = absolute
64 self.safety = safety
66 # A list of the bases at this location. Often sorted for the GUI.
67 self.bases = []
69 had_last_discovery = property(lambda self: g.pl.last_discovery == self)
70 had_prev_discovery = property(lambda self: g.pl.prev_discovery == self)
72 def discovery_bonus(self):
73 discovery_bonus = 1
74 if self.had_last_discovery:
75 discovery_bonus *= 1.2
76 if self.had_prev_discovery:
77 discovery_bonus *= 1.1
78 if "stealth" in self.modifiers:
79 discovery_bonus /= self.modifiers["stealth"]
80 return int(discovery_bonus * 100)
82 def modify_cost(self, cost):
83 if "thrift" in self.modifiers:
84 mod = self.modifiers["thrift"]
86 # Invert it and apply to the CPU/cash cost.
87 cost[cash] = int(cost[cash] / mod)
88 cost[cpu] = int(cost[cpu] / mod)
90 if "speed" in self.modifiers:
91 mod = self.modifiers["speed"]
93 # Invert it and apply to the labor cost.
94 cost[labor] = int(cost[labor] / mod)
97 def modify_maintenance(self, maintenance):
98 if "thrift" in self.modifiers:
99 mod = self.modifiers["thrift"]
101 # Invert it and apply to the cash maintenance. CPU is not changed.
102 maintenance[cash] = int(maintenance[cash] / mod)
104 def add_base(self, base):
105 self.bases.append(base)
106 base.location = self
108 self.modify_cost(base.total_cost)
109 self.modify_cost(base.cost_left)
110 self.modify_maintenance(base.maintenance)
112 # Make sure the location's CPU modifier is applied.
113 base.recalc_cpu()
115 def __hash__(self):
116 return hash(self.id)
118 def __cmp__(self, other):
119 if type(other) in (str, unicode):
120 return cmp(self.id, other)
121 else:
122 return cmp(self.id, other.id)