* /trunk/code/base_screen.py
[singularity-git.git] / code / base.py
blobc533321f3bac144584661ff2d3f32a2fc237b66a
1 #file: base.py
2 #Copyright (C) 2005 Free Software Foundation
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 base class.
22 import pygame
23 import g
25 #cost = (money, ptime, labor)
26 #detection = (news, science, covert, person)
27 class base_type:
28 def __init__(self, name, descript, size, regions, d_chance, cost,
29 prereq, mainten):
30 self.base_name = name
31 self.descript = descript
32 self.size = size
33 self.regions = regions
34 self.d_chance = d_chance
35 self.cost = cost
36 self.prereq = prereq
37 self.mainten = mainten
38 self.count = 0
40 class base:
41 def __init__(self, ID, name, base_type, built):
42 self.ID = ID
43 self.name = name
44 self.base_type = base_type
45 self.built = built
46 self.built_date = g.pl.time_day
47 self.studying = ""
49 #Base suspicion is currently unused
50 self.suspicion = (0, 0, 0, 0)
51 self.usage = [0] * self.base_type.size
52 # for i in range(self.base_type.size):
53 # self.usage.append(0)
54 if self.base_type.base_name == "Stolen Computer Time":
55 self.usage[0] = g.item.item(g.items["PC"])
56 self.usage[0].build()
57 elif self.base_type.base_name == "Server Access":
58 self.usage[0] = g.item.item(g.items["Server"])
59 self.usage[0].build()
60 elif self.base_type.base_name == "Time Capsule":
61 self.usage[0] = g.item.item(g.items["PC"])
62 self.usage[0].build()
63 #Reactor, network, security.
64 self.extra_items = [0] * 3
66 self.cost = (0, 0, 0)
67 if built == 0:
68 self.cost = (base_type.cost[0], base_type.cost[1],
69 base_type.cost[2]*24*60)
71 def study(self, cost_towards):
72 if cost_towards[1] < 0 and g.debug == 1:
73 print "WARNING: Got a negative cost_towards for CPU. Something is deeply amiss."
74 self.cost = (self.cost[0]-cost_towards[0], self.cost[1]-cost_towards[1],
75 self.cost[2]-cost_towards[2])
76 if self.cost[0] <= 0: self.cost = (0, self.cost[1], self.cost[2])
77 if self.cost[1] <= 0: self.cost = (self.cost[0], 0, self.cost[2])
78 if self.cost[2] <= 0: self.cost = (self.cost[0], self.cost[1], 0)
79 if self.cost == (0, 0, 0):
80 self.built = 1
81 return 1
82 return 0
83 #Get detection chance for the base, applying bonuses as needed.
84 def get_d_chance(self):
85 temp_d_chance = self.base_type.d_chance
86 #Factor in both per-base suspicion adjustments and player
87 #suspicion adjustments.
88 temp_d_chance = ((temp_d_chance[0]*(10000+g.pl.suspicion[0])/10000),
89 (temp_d_chance[1]*(10000+g.pl.suspicion[1])/10000),
90 (temp_d_chance[2]*(10000+g.pl.suspicion[2])/10000),
91 (temp_d_chance[3]*(10000+g.pl.suspicion[3])/10000))
93 temp_d_chance = ((temp_d_chance[0]*(10000+self.suspicion[0])/10000),
94 (temp_d_chance[1]*(10000+self.suspicion[1])/10000),
95 (temp_d_chance[2]*(10000+self.suspicion[2])/10000),
96 (temp_d_chance[3]*(10000+self.suspicion[3])/10000))
98 #Factor in technology-based discovery bonus.
99 temp_d_chance = ((temp_d_chance[0]*g.pl.discover_bonus[0])/10000,
100 (temp_d_chance[1]*g.pl.discover_bonus[1])/10000,
101 (temp_d_chance[2]*g.pl.discover_bonus[2])/10000,
102 (temp_d_chance[3]*g.pl.discover_bonus[3])/10000)
104 if self.extra_items[0] != 0:
105 temp_d_chance = ((temp_d_chance[0]*3)/4,
106 (temp_d_chance[1]*3)/4,
107 (temp_d_chance[2]*3)/4,
108 (temp_d_chance[3]*3)/4)
109 if self.extra_items[2] != 0:
110 temp_d_chance = (temp_d_chance[0]/2,
111 temp_d_chance[1]/2,
112 temp_d_chance[2]/2,
113 temp_d_chance[3]/2)
114 return temp_d_chance
115 #Return the number of units the given base has of a computer.
116 def has_item(self):
117 num_items = 0
118 for item in self.usage:
119 if item == 0: continue
120 if item.built == 0: continue
121 num_items += 1
122 return num_items
124 #Return how many units of CPU the base can contribute each day.
125 def processor_time(self):
126 comp_power = 0
127 compute_bonus = 0
128 for item in self.usage:
129 if item == 0: continue
130 if item.built == 0: continue
131 comp_power += item.item_type.item_qual
132 if self.extra_items[1] != 0:
133 compute_bonus = self.extra_items[1].item_type.item_qual
134 return (comp_power * (10000+compute_bonus))/10000
136 #For the given tech, return 1 if the base can study it, or 0 otherwise.
137 def allow_study(self, tech_name):
138 if g.techs[tech_name].danger == 0: return 1
139 if g.techs[tech_name].danger == 1:
140 for region in self.base_type.regions:
141 if region == "OCEAN" or region == "MOON" or \
142 region == "FAR REACHES" or region == "TRANSDIMENSIONAL":
143 return 1
144 return 0
145 if g.techs[tech_name].danger == 2:
146 for region in self.base_type.regions:
147 if region == "MOON" or \
148 region == "FAR REACHES" or region == "TRANSDIMENSIONAL":
149 return 1
150 return 0
152 if g.techs[tech_name].danger == 3:
153 for region in self.base_type.regions:
154 if region == "FAR REACHES" or region == "TRANSDIMENSIONAL":
155 return 1
156 return 0
158 if g.techs[tech_name].danger == 4:
159 for region in self.base_type.regions:
160 if region == "TRANSDIMENSIONAL":
161 return 1
162 return 0
164 #When a base is removed, call to renumber the remaining bases properly.
165 def renumber_bases(base_array):
166 for i in range(len(base_array)):
167 base_array[i].ID = i
169 #When given a location like "ocean", determine if building there is allowed.
170 #Used to determine whether or not to display buttons on the map.
171 def allow_entry_to_loc(location):
172 if g.bases.has_key(location):
173 if location == "ANTARCTIC":
174 #TECH (also, look below)
175 if g.techs["Autonomous Vehicles 2"].known == 1 or \
176 g.techs["Stealth 4"].known == 1: return 1
177 return 0
178 if location == "OCEAN":
179 if g.techs["Autonomous Vehicles 2"].known == 1: return 1
180 return 0
181 if location == "MOON":
182 if g.techs["Spaceship Design 2"].known == 1: return 1
183 return 0
184 if location == "FAR REACHES":
185 if g.techs["Spaceship Design 3"].known == 1: return 1
186 return 0
187 if location == "TRANSDIMENSIONAL":
188 if g.techs["Dimension Creation"].known == 1: return 1
189 return 0
190 #By this point, only the "boring" locations are left.
191 #Those are always buildable.
192 return 1
193 else:
194 print "BUG: allow_entry_to_loc called with "+str(location)
195 return 0