Completed
Push — master ( cc2800...40418d )
by Marek
15s queued 13s
created

ige.ospace.TechHandlers.spaceDocksTurn()   C

Complexity

Conditions 9

Size

Total Lines 31
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 27
nop 3
dl 0
loc 31
rs 6.6666
c 0
b 0
f 0
1
#
2
#  Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Outer Space.
5
#
6
#  Outer Space 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
#  Outer Space 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 Outer Space; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
import math
22
import random
23
import sys
24
25
import Const
26
import Rules
27
import Utils
28
29
from ige import log
30
31
def finishStructOUTPOST(tran, source, target, tech):
32
    log.debug("Finishing OUTPOST", tech.id, "target", target.oid)
33
    # setup morale if colonizing noninhabited planet
34
    if target.storPop == 0:
35
        target.morale = Rules.maxMorale
36
    # try to change owner of planet
37
    tran.gameMngr.cmdPool[target.type].changeOwner(tran, target, source.owner)
38
    # increase population
39
    target.storPop += tech.unpackPop
40
    target.maxPop += tech.unpackPop
41
42
def finishStructSPORECOLONY(tran, source, target, tech):
43
    log.debug("Finishing SPORE COLONY", tech.id, "target", target.oid)
44
    # setup morale if colonizing noninhabited planet
45
    if target.storPop == 0:
46
        target.morale = Rules.maxMorale
47
    # try to change owner of planet
48
    tran.gameMngr.cmdPool[target.type].changeOwner(tran, target, source.owner)
49
    # increase population
50
    target.storPop += tech.unpackPop
51
    target.maxPop += tech.unpackPop
52
53
    if target.plSlots > 1:
54
        for i in range(len(target.slots),target.plSlots-1):
55
            target.slots.insert(0, Utils.newStructure(tran, 9013, source.owner, hpRatio = structFromShipHpRatio))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable structFromShipHpRatio does not seem to be defined.
Loading history...
56
57
def finishStructGOVCENTER(tran, source, target, tech):
58
    player = tran.db[source.owner]
59
    # delete old center
60
    planet = tran.db[player.planets[0]]
61
    slots = planet.slots[:] # copy
62
    slots.reverse()
63
    govStr = 0
64
    for slot in slots:
65
        tech = Rules.techs[slot[Const.STRUCT_IDX_TECHID]]
66
        if tech.govPwr > 0 and planet.oid != target.oid:
67
            planet.slots.remove(slot)
68
            break
69
        elif tech.govPwr > 0:
70
            if govStr == 1:
71
                planet.slots.remove(slot)
72
                break
73
            else:
74
                govStr = 1
75
    # setup new one (move planet with new centre to the first position)
76
    player.planets.remove(target.oid)
77
    player.planets.insert(0, target.oid)
78
    # message
79
    Utils.sendMessage(tran, target, Const.MSG_NEW_GOVCENTER, target.oid, None)
80
81
def validateTRUE(tran,source,target,tech):
82
    return 1;
83
84
## Ecosystem initiation
85
def validateProjectECOINIT3(tran, source, target, tech):
86
    return target.plBio == 0 and target.plType not in ('G', 'A', '-')
87
88
def finishProjectECOINIT3(tran, source, target, tech):
89
    target.plBio = Rules.projECOINIT3PlBio
90
91
## Habitable Surface Expansion
92
def validateProjectADDSLOT3(tran, source, target, tech):
93
    return target.plSlots < target.plMaxSlots and target.plType not in ('G', 'A', '-')
94
95
def finishProjectADDSLOT3(tran, source, target, tech):
96
    target.plSlots += 1
97
98
## Terraforming
99
def validateDeployTERRAFORM3(tran, source, target, tech):
100
    return validateProjectTERRAFORM3(tran, source, target, tech) and \
101
        target.owner==source.owner
102
103
def validateProjectTERRAFORM3(tran, source, target, tech):
104
    spec = Rules.planetSpec[target.plType]
105
    solarminus = 0
106
    solarplus = 0
107
    if target.solarmod > 0:
108
        solarplus = target.solarmod
109
    if target.solarmod < 0:
110
        solarminus = target.solarmod
111
    #log.debug("En:",target.plEn,", Plus:",solarplus,", Minus:",solarminus,", Reqs:",spec.upgradeEnReqs)
112
    return spec.upgradeTo != None and \
113
        target.plEn + solarplus >= spec.upgradeEnReqs[0] and \
114
        target.plEn + solarminus <= spec.upgradeEnReqs[1] and \
115
        target.plBio >= spec.maxBio
116
117
def finishProjectTERRAFORM3(tran, source, target, tech):
118
    target.plType = Rules.planetSpec[target.plType].upgradeTo
119
120
## Uber Terraforming
121
def validateDeployTERRAFORMALIGNMENT6(tran, source, target, tech):
122
    return validateProjectTERRAFORMALIGNMENT6(tran, source, target, tech) and \
123
        target.owner==source.owner
124
125
def validateProjectTERRAFORMALIGNMENT6(tran, source, target, tech):
126
    log.debug('Validating TERRAFORM ALIGNMENT');
127
    spec = Rules.planetSpec[target.plType]
128
    return (target.plEnv < Rules.envMax or target.plBio < spec.upgradeEnReqs[0] or target.plBio > spec.upgradeEnReqs[1])
129
130
def finishProjectTERRAFORMALIGNMENT6(tran, source, target, tech):
131
    log.debug('Finishing TERRAFORM ALIGNMENT');
132
    spec = Rules.planetSpec[target.plType]
133
    techEff = Utils.getTechEff(tran, tech.id, source.owner)
134
    enAvg = int((spec.upgradeEnReqs[0] + spec.upgradeEnReqs[1]) / 2)
135
    delta = int(float(tech.data) * techEff)
136
    if target.plEn < enAvg:
137
        target.plEn = min(target.plEn+delta,enAvg)
138
    elif target.plEn > enAvg:
139
        target.plEn = max(target.plEn-delta,enAvg)
140
    target.plBio = min(target.plBio+int(delta/2),Rules.envMax)
141
    if validateProjectTERRAFORM3(tran, source, target, tech):
142
        target.plType = Rules.planetSpec[target.plType].upgradeTo
143
144
## Tech level advancement
145
def finishResTLAdvance(tran, player, tech):
146
    improvement = player.techs[tech.id]
147
    if improvement >= 3:
148
        player.techLevel = max(player.techLevel, tech.level + 1)
149
        player.race = tech.data
150
151
## Holidays
152
def finishProjectHOLIDAYS1(tran, source, target, tech):
153
    techEff = Utils.getTechEff(tran, tech.id, source.owner)
154
    # no battle
155
    system = tran.db[source.compOf]
156
    if system.combatCounter == 0:
157
        source.morale = min(source.morale + int(tech.moraleTrgt * techEff), Rules.maxMorale)
158
159
## Produce resources
160
def finishProjectPRODRSRC(tran, source, target, tech):
161
    techEff = Utils.getTechEff(tran, tech.id, source.owner)
162
    # raw resources
163
    target.storBio += int(tech.prodBio * techEff)
164
    target.storEn += int(tech.prodEn * techEff)
165
    # sci pts
166
    if target.owner != Const.OID_NONE:
167
        owner = tran.db[target.owner]
168
        owner.sciPoints += int(tech.prodSci * techEff)
169
170
## Produce strategic resource
171
def finishProjectNF(tran, source, target, tech):
172
    if target.owner != Const.OID_NONE:
173
        techEff = Utils.getTechEff(tran, tech.id, source.owner)
174
        # TODO success shall depend on the level of the technology
175
        owner = tran.db[target.owner]
176
        stratRes = int(tech.data)
177
        owner.stratRes[stratRes] = owner.stratRes.get(stratRes, 0) + Rules.stratResAmountSmall
178
179
## Antimatter transmutation
180
def finishProjectNF2(tran, source, target, tech):
181
    if target.owner != Const.OID_NONE:
182
        techEff = Utils.getTechEff(tran, tech.id, source.owner)
183
        # TODO success shall depend on the level of the technology
184
        owner = tran.db[target.owner]
185
        stratRes = int(tech.data)
186
        owner.stratRes[stratRes] = owner.stratRes.get(stratRes, 0) + 4 * Rules.stratResAmountBig
187
        Utils.sendMessage(tran, target, Const.MSG_EXTRACTED_ANTIMATTER_SYNTH, target.oid, stratRes)
188
189
## Upgrade ships
190
def finishProjectUPGRADESHIPS(tran, source, target, tech):
191
    techEff = Utils.getTechEff(tran, tech.id, source.owner)
192
    # fleet upgrade pool
193
    if target.owner != Const.OID_NONE:
194
        owner = tran.db[target.owner]
195
        owner.fleetUpgradePool += int(tech.prodProd * techEff)
196
197
## Deep scan
198
def finishProjectDEEPSPACESCAN(tran, source, target, tech):
199
    techEff = Utils.getTechEff(tran, tech.id, source.owner)
200
    target.scannerPwr = min(int(float(tech.data) * techEff * target.scannerPwr), Rules.scannerMaxPwr)
201
    system = tran.db[target.compOf]
202
    system.scannerPwrs[target.owner] = max(system.scannerPwrs.get(target.owner, 0), target.scannerPwr)
203
204
## improve environment
205
def finishProjectIMPRENV(tran, source, target, tech):
206
    techEff = Utils.getTechEff(tran, tech.id, source.owner)
207
    target.plEnv += int(float(tech.data) * techEff)
208
209
## QDev
210
def validateProjectCondPl(tran, source, target, tech):
211
    return target.plType == 'G'
212
213
def finishProjectCondPl(tran, source, target, tech):
214
    target.plType = 'R'
215
    target.plDiameter = target.plDiameter / 10
216
    target.plMaxSlots = target.plDiameter / 1000
217
    target.plSlots = target.plMaxSlots / 2
218
219
def validateProjectAssemblePl(tran, source, target, tech):
220
    return target.plType == 'A'
221
222
def finishProjectAssemblePl(tran, source, target, tech):
223
    target.plType = 'D'
224
    target.plDiameter = (random.randrange(1, 7) + random.randrange(1, 7) + 2) * 1000
225
    target.plMaxSlots = target.plDiameter / 1000
226
    target.plSlots = target.plMaxSlots / 2
227
228
def finishProjectAsteroidMining(tran, source, target, tech):
229
    techEff = Utils.getTechEff(tran, tech.id, source.owner)
230
    minerals = min(int(float(tech.data) * techEff), target.plMin)
231
    # now, transfer only minerals up to 200 on source planet
232
    minerals = min(minerals, 200 - source.plMin)
233
    target.plMin -= minerals
234
    source.plMin += minerals
235
236
def validateProjectBioEnrich(tran, source, target, tech):
237
    spec = Rules.planetSpec['E']
238
    return spec.upgradeTo != None and \
239
        target.plEn >= spec.upgradeEnReqs[0] and \
240
        target.plEn <= spec.upgradeEnReqs[1]
241
242
def finishProjectBioEnrich(tran, source, target, tech):
243
    target.plType = 'I'
244
    target.plBio = 200
245
    target.storPop = 1000
246
247
def validateProjectMinEnrich(tran, source, target, tech):
248
    return 1
249
250
def finishProjectMinEnrich(tran, source, target, tech):
251
    target.plMin = 200
252
    target.storPop = 1000
253
254
def validateProjectShiftPlDown(tran, source, target, tech):
255
    return target.plEn < 200
256
257
def finishProjectShiftPlDown(tran, source, target, tech):
258
    techEff = Utils.getTechEff(tran, tech.id, source.owner)
259
    target.plEn = min(200, int(target.plEn + int(tech.data) * techEff))
260
    system = tran.db[target.compOf]
261
    tran.gameMngr.cmdPool[system.type].sortPlanets(tran, system, None)
262
263
def validateProjectShiftPlUp(tran, source, target, tech):
264
    return target.plEn > 0
265
266
def finishProjectShiftPlUp(tran, source, target, tech):
267
    techEff = Utils.getTechEff(tran, tech.id, source.owner)
268
    target.plEn = max(0, int(target.plEn - int(tech.data) * techEff))
269
    system = tran.db[target.compOf]
270
    tran.gameMngr.cmdPool[system.type].sortPlanets(tran, system, None)
271
272
## Pirate colonization
273
def OLDgetPirateFameMod(tran, player, system):
274
    mod = 1.0
275
    for planetID in system.planets:
276
        planet = tran.db[planetID]
277
        if planet.owner == player.oid:
278
            # minimum reached, don't check rest
279
            return 0.0
280
        elif planet.plStratRes in (Const.SR_TL3A, Const.SR_TL3B, Const.SR_TL3C):
281
            mod = min(mod, Rules.pirateTL3StratResColonyCostMod)
282
    return mod
283
284
def distToNearestPiratePlanet(tran,obj,srcObj):
285
    # srcObj can be Planet or System type
286
    dist = sys.maxint
287
    for objID in obj.planets:
288
        pirPl = tran.db[objID]
289
        d = math.hypot(srcObj.x - pirPl.x, srcObj.y - pirPl.y)
290
        if d < dist:
291
            dist = d
292
    return dist
293
294
def getPirateFameMod(tran, player, system):
295
    mod = 1.0
296
    for planetID in system.planets:
297
        planet = tran.db[planetID]
298
        if getattr(planet, 'owner', Const.OID_NONE) == player.oid:
299
            # minimum reached, don't check rest
300
            return 0.0
301
        elif getattr(planet, 'plStratRes', None) in (Const.SR_TL3A, Const.SR_TL3B, Const.SR_TL3C):
302
            mod = min(mod, Rules.pirateTL3StratResColonyCostMod)
303
    dist = distToNearestPiratePlanet(tran, player, system)
304
    if Rules.pirateGainFamePropability(dist) > 0:
305
        mod = Rules.pirateColonyFameZoneCost(dist)
306
    else:
307
        mod = Rules.pirateColonyPlayerZoneCost(dist)
308
    return mod
309
310
def validateStructPIROUTPOST(tran, source, target, tech):
311
    player = tran.db[source.owner]
312
    if source.type == Const.T_FLEET and target.owner != player.oid:
313
        mod = getPirateFameMod(tran, player, tran.db[target.compOf])
314
        return player.pirateFame >= int(mod * Rules.pirateColonyCostMod * len(player.planets))
315
    else:
316
        return True
317
318
def finishStructPIROUTPOST(tran, source, target, tech):
319
    log.debug("Finishing PIRATE OUTPOST", tech.id, "target", target.oid)
320
    player = tran.db[source.owner]
321
    famePenalty = 0
322
    if source.type == Const.T_FLEET:
323
        mod = getPirateFameMod(tran, player, tran.db[target.compOf])
324
        log.debug(source.owner, "DEPLOYING MODULE -- BEFORE", player.pirateFame, mod)
325
        famePenalty = int(mod * Rules.pirateColonyCostMod * len(player.planets))
326
        log.debug(source.owner, "DEPLOYING MODULE -- AFTER", player.pirateFame - famePenalty, famePenalty)
327
    finishStructOUTPOST(tran, source, target, tech)
328
    player.pirateFame -= famePenalty
329