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 copy |
21
|
|
|
from ige import log |
22
|
|
|
from ige.ospace import Const |
23
|
|
|
from ige.ospace import Rules |
24
|
|
|
from ige.ospace import Utils |
25
|
|
|
|
26
|
|
|
import ai_tools as tool |
27
|
|
|
|
28
|
|
|
class AI(object): |
29
|
|
|
def __init__(self, client): |
30
|
|
|
self.client = client |
31
|
|
|
self.db = client.db |
32
|
|
|
self.player = client.getPlayer() |
33
|
|
|
|
34
|
|
|
self.data = tool.tool_parseDB(self.client, self.db) |
35
|
|
|
|
36
|
|
|
def economy_manager(self): |
37
|
|
|
raise NotImplementedError |
38
|
|
|
|
39
|
|
|
def defense_manager(self): |
40
|
|
|
raise NotImplementedError |
41
|
|
|
|
42
|
|
|
def offense_manager(self): |
43
|
|
|
raise NotImplementedError |
44
|
|
|
|
45
|
|
|
def research_manager(self): |
46
|
|
|
raise NotImplementedError |
47
|
|
|
|
48
|
|
|
def diplomacy_manager(self): |
49
|
|
|
raise NotImplementedError |
50
|
|
|
|
51
|
|
|
def _find_best_planet(self, planet_ids): |
52
|
|
|
# right now, it's simply the largest one |
53
|
|
|
max_slots = 0 |
54
|
|
|
largest_planet_id = None |
55
|
|
|
for planet_id in planet_ids: |
56
|
|
|
planet = self.db[planet_id] |
57
|
|
|
if max_slots < planet.plSlots: |
58
|
|
|
max_slots = planet.plSlots |
59
|
|
|
largest_planet_id = planet_id |
60
|
|
|
return largest_planet_id |
61
|
|
|
|
62
|
|
|
def _explore(self, explorer_design_id): |
63
|
|
|
should_repeat = False |
64
|
|
|
explorer_fleets = self.data.myFleetsWithDesign.get(explorer_design_id, set()) |
65
|
|
|
for fleet_id in copy.copy(explorer_fleets & self.data.idleFleets): |
66
|
|
|
max_range = tool.subfleetMaxRange(self.client, self.db, {explorer_design_id:1}, fleet_id) |
67
|
|
|
nearest = tool.findNearest(self.db, self.db[fleet_id], self.data.unknownSystems, max_range) |
68
|
|
|
if len(nearest) > 0: |
69
|
|
|
system_id = nearest[0] |
70
|
|
|
# send the fleet |
71
|
|
|
fleet, new_fleet, my_fleets = tool.orderPartFleet(self.client, self.db, |
72
|
|
|
{explorer_design_id:1}, True, fleet_id, Const.FLACTION_MOVE, system_id, None) |
73
|
|
|
self.data.myFleetSheets[fleet_id][explorer_design_id] -= 1 |
74
|
|
|
if self.data.myFleetSheets[fleet_id][explorer_design_id] == 0: |
75
|
|
|
del self.data.myFleetSheets[fleet_id][explorer_design_id] |
76
|
|
|
explorer_fleets.remove(fleet_id) |
77
|
|
|
else: |
78
|
|
|
should_repeat = True |
79
|
|
|
self.data.unknownSystems.remove(system_id) |
80
|
|
|
return should_repeat |
81
|
|
|
|
82
|
|
|
def _colonize_free_systems(self, valid_systems, colony_design_id): |
83
|
|
|
should_repeat = False |
84
|
|
|
colony_fleets = self.data.myFleetsWithDesign.get(colony_design_id, set()) |
85
|
|
|
for fleet_id in copy.copy(colony_fleets & self.data.idleFleets): |
86
|
|
|
max_range = tool.subfleetMaxRange(self.client, self.db, {colony_design_id:1}, fleet_id) |
87
|
|
|
nearest = tool.findNearest(self.db, self.db[fleet_id], valid_systems, max_range) |
88
|
|
|
if len(nearest) > 0: |
89
|
|
|
system_id = nearest[0] |
90
|
|
|
system = self.db[system_id] |
91
|
|
|
target_id = self._find_best_planet(system.planets) |
92
|
|
|
fleet, new_fleet, my_fleets = tool.orderPartFleet(self.client, self.db, |
93
|
|
|
{colony_design_id:1}, True, fleet_id, Const.FLACTION_DEPLOY, target_id, colony_design_id) |
94
|
|
|
self.data.myFleetSheets[fleet_id][colony_design_id] -= 1 |
95
|
|
|
if self.data.myFleetSheets[fleet_id][colony_design_id] == 0: |
96
|
|
|
del self.data.myFleetSheets[fleet_id][colony_design_id] |
97
|
|
|
colony_fleets.remove(fleet_id) |
98
|
|
|
else: |
99
|
|
|
should_repeat = True |
100
|
|
|
self.data.freeSystems.remove(system_id) |
101
|
|
|
valid_systems.remove(system_id) |
102
|
|
|
return should_repeat |
103
|
|
|
|
104
|
|
|
def diplomacy_manager(self, friendly_types = None, pacts = None): |
105
|
|
|
if friendly_types is None: |
106
|
|
|
return |
107
|
|
|
if pacts is None: |
108
|
|
|
return |
109
|
|
|
for contact_id in self.player.diplomacyRels: |
110
|
|
|
contact = self.client.get(contact_id, publicOnly = True) |
111
|
|
|
if contact.type not in friendly_types: |
112
|
|
|
continue |
113
|
|
|
dipl = self.client.getDiplomacyWith(contact_id) |
114
|
|
|
for pact_id in pacts: |
115
|
|
|
pactSpec = Rules.pactDescrs[pact_id] |
116
|
|
|
if dipl.relation < pactSpec.validityInterval[0] or dipl.relation > pactSpec.validityInterval[1]: |
117
|
|
|
# not friendly enough |
118
|
|
|
continue |
119
|
|
|
if pact_id in dipl.pacts and dipl.pacts[pact_id][0] in [Const.PACT_ACTIVE, Const.PACT_INACTIVE]: |
120
|
|
|
# nothing more to do, move along |
121
|
|
|
continue |
122
|
|
|
# hey, we should enable this pact! |
123
|
|
|
conditions = [pact_id] |
124
|
|
|
self.player.diplomacyRels = self.client.cmdProxy.changePactCond(self.player.oid, contact_id, pact_id, Const.PACT_INACTIVE, conditions) |
125
|
|
|
|
126
|
|
|
def run(self): |
127
|
|
|
self.economy_manager() |
128
|
|
|
self.defense_manager() |
129
|
|
|
self.offense_manager() |
130
|
|
|
self.research_manager() |
131
|
|
|
self.diplomacy_manager() |
132
|
|
|
|
133
|
|
|
|