Completed
Push — master ( b75f51...3da424 )
by Marek
16s queued 14s
created

AIs.ais_renegade._buildNormalShips()   A

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nop 2
dl 0
loc 12
rs 10
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
import random
21
22
from ige import log
23
from ige.ospace import Const
24
from ige.ospace import Rules
25
from ige.ospace import Utils
26
27
import ai_tools as tool
28
from ai_tools import data
29
30
client = None
31
db = None
32
playerID = None
33
player = None
34
35
def _buildSpecialShips(player, planet):
36
    uraniumSpec = player.shipDesigns[4]
37
    titaniumSpec = player.shipDesigns[5]
38
    if player.stratRes.get(Const.SR_TL1A, 0) >= uraniumSpec.buildSRes[Const.SR_TL1A]:
39
        planet.prodQueue, player.stratRes = client.cmdProxy.startConstruction(planet.oid, 4, 1, planet.oid, True, False, Const.OID_NONE)
40
        return True
41
    elif player.stratRes.get(Const.SR_TL1B, 0) >= titaniumSpec.buildSRes[Const.SR_TL1B]:
42
        planet.prodQueue, player.stratRes = client.cmdProxy.startConstruction(planet.oid, 5, 1, planet.oid, True, False, Const.OID_NONE)
43
        return True
44
45
def _buildNormalShips(player, planet):
46
        shipDraw = random.randint(1, 10)
47
        # if there is enough special resources for our unique designs, build those!
48
        # 1/10 - Frigate
49
        # 3/10 - Corvette
50
        # 6/10 - Fighter
51
        if shipDraw == 1:
52
            planet.prodQueue, player.stratRes = client.cmdProxy.startConstruction(planet.oid, 3, 1, planet.oid, True, False, Const.OID_NONE)
53
        elif shipDraw <= 4:
54
            planet.prodQueue, player.stratRes = client.cmdProxy.startConstruction(planet.oid, 2, 1, planet.oid, True, False, Const.OID_NONE)
55
        else:
56
            planet.prodQueue, player.stratRes = client.cmdProxy.startConstruction(planet.oid, 1, 1, planet.oid, True, False, Const.OID_NONE)
57
58
def _buildShips(player, idlePlanets):
59
    for planetID in idlePlanets:
60
        planet = db[planetID]
61
        if _buildSpecialShips(player, planet):
62
            continue
63
        _buildNormalShips(player, planet)
64
65
def systemManager():
66
    global data, player, db
67
    for planetID in data.myPlanets:
68
        tool.sortStructures(client, db, planetID)
69
    for systemID in data.mySystems:
70
        system = db[systemID]
71
        # creation of final system plans
72
        finalSystemPlan = {}
73
        actualStats = tool.getSystemStructStats(client, db, systemID)
74
        buildStats = tool.getSystemStructStats(client, db, systemID, False)
75
        # create appropriate build plans
76
        for planetID in data.freePlanets & set(system.planets):
77
            finalSystemPlan[planetID] = {Rules.Tech.RENEGADEBASE:1}
78
        for planetID in data.myPlanets & set(system.planets):
79
            planet = db[planetID]
80
            if Rules.Tech.RENEGADECOSMODROME in actualStats.planets[planetID] or\
81
                    Rules.Tech.RENEGADEBASE3 in actualStats.planets[planetID] or\
82
                    Rules.Tech.RENEGADEBASE3MINOR in actualStats.planets[planetID]:
83
                finalSystemPlan[planetID] = {Rules.Tech.RENEGADEBASE3:1,
84
                                        Rules.Tech.RENEGADECOSMODROME:min(planet.plSlots - 1, 1),
85
                                        Rules.Tech.RENEGADEBASE3MINOR:max(planet.plSlots - 2, 0)}
86
                continue
87
            elif planet.plStratRes and\
88
                    tool.compareBuildStructPlans(actualStats.planets[planetID], {
89
                                                Rules.Tech.RENEGADEBASE2:1,
90
                                                Rules.Tech.RENEGADEBASE2MINOR:planet.plSlots - 1}):
91
                finalSystemPlan[planetID] = {Rules.Tech.RENEGADEBASE3:1,
92
                                            Rules.Tech.RENEGADEBASE2MINOR:planet.plSlots - 1}
93
                continue
94
            elif Rules.Tech.RENEGADEBASE2 in actualStats.planets[planetID]:
95
                finalSystemPlan[planetID] = {Rules.Tech.RENEGADEBASE2:1,
96
                                            Rules.Tech.RENEGADEBASE2MINOR:planet.plSlots - 1}
97
                continue
98
            elif actualStats.planets[planetID].setdefault(Rules.Tech.RENEGADEBASE, 0) > 0:
99
                planet.prodQueue, player.stratRes = client.cmdProxy.startConstruction(planetID, Rules.Tech.RENEGADEBASE2,
100
                        1, planetID, False, False, Rules.Tech.RENEGADEBASE)
101
                finalSystemPlan[planetID] = actualStats.planets[planetID]
102
                continue
103
            else:
104
                finalSystemPlan[planetID] = {Rules.Tech.RENEGADEBASE:1}
105
                continue
106
        idlePlanets = tool.buildSystem(client, db, systemID, data.myProdPlanets & set(system.planets), finalSystemPlan)
107
        # build ships just in case cosmodrome is present
108
        hasCosmodrome = False
109
        for planetID in data.myPlanets & set(system.planets):
110
            if Rules.Tech.RENEGADECOSMODROME in buildStats.planets[planetID]:
111
                hasCosmodrome = True
112
                break
113
        if hasCosmodrome:
114
            _buildShips(player, idlePlanets)
115
116
def shipDesignManager():
117
    # there are 5 basic designs created by the server
118
    # 1: Fighter [Small hull, Cockpit, 1x Cannon, 2x STL Engine]
119
    # 2: Corvette [Small hull, Cockpit, 2x Cannon, 1x Steel Armor, 1x STL Engine]
120
    # 3: Frigate [Medium hull, Bridge, 2x Cannon, 2x SS Rocket, 2x STL Engine]
121
    # special using uranium:
122
    # 4: Destroyer [Medium hull, Bridge, 4x Cannon, 2x SS Rocket, 3x Nuclear STL Engine]
123
    # special using titanium:
124
    # 5: Frigate [Crude Titanium Medium hull, Bridge, 5x Cannon, 3x SS Rocket, 4x STL Engine]
125
    pass
126
127
def researchManager():
128
    pass
129
130
def attackManager():
131
    pass
132
133 View Code Duplication
def diplomacyManager():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
134
    # renegades are friendly, want to trade, and can help refuel civilian ships
135
    global client, db, player, playerID
136
    for contactID in player.diplomacyRels:
137
        dipl = client.getDiplomacyWith(contactID)
138
        for pactID in [Const.PACT_ALLOW_CIVILIAN_SHIPS, Const.PACT_ALLOW_TANKING, Const.PACT_MINOR_CP_COOP, Const.PACT_MAJOR_CP_COOP]:
139
            pactSpec = Rules.pactDescrs[pactID]
140
            if dipl.relation < pactSpec.validityInterval[0] or dipl.relation > pactSpec.validityInterval[1]:
141
                # not friendly enough
142
                continue
143
            if pactID in dipl.pacts and dipl.pacts[pactID][0] in [Const.PACT_ACTIVE, Const.PACT_INACTIVE]:
144
                # nothing more to do, move along
145
                continue
146
            # hey, we should enable this pact!
147
            conditions = [pactID]
148
            player.diplomacyRels = client.cmdProxy.changePactCond(playerID, contactID, pactID, Const.PACT_INACTIVE, conditions)
149
150
def run(aclient):
151
    global client, db, player, playerID
152
    client = aclient
153
    db = client.db
154
    player = client.getPlayer()
155
    playerID = client.getPlayerID()
156
157
    tool.tool_parseDB(client, db)
158
159
    researchManager()
160
    shipDesignManager()
161
    systemManager()
162
    diplomacyManager()
163
164
    attackManager()
165
    client.saveDB()
166
167