carrier ops; persistency; ui improvements; refactoring

This commit is contained in:
Vasyl Horbachenko 2018-06-13 00:15:27 +03:00
parent 3d0babf65f
commit dc303997a6
22 changed files with 348 additions and 368 deletions

View File

@ -1,90 +1,24 @@
#!/usr/bin/env python3
import pickle
import dcs
import os
import gen
import theater.caucasus
import game.operation
import ui.window
import ui.mainmenu
from game.game import Game
from theater import start_generator
from theater.controlpoint import *
from userdata import persistency
from dcs.planes import *
from dcs.vehicles import *
game = persistency.restore_game()
if not game:
theater = theater.caucasus.CaucasusTheater()
start_generator.generate_initial(theater, "Russia")
m = dcs.Mission()
theater = theater.caucasus.CaucasusTheater()
start_generator.generate_initial(theater, "Russia")
g = Game(theater=theater)
game = Game(theater=theater)
w = ui.window.Window()
m = ui.mainmenu.MainMenu(w, None, g)
m = ui.mainmenu.MainMenu(w, None, game)
m.display()
w.run()
"""
selected_cp = None # type: ControlPoint
while True:
ptr = 0
print("Budget: {}m".format(g.budget))
if selected_cp is None:
print("Events:")
for event in g.events:
ptr += 1
print("{}. {} {}".format(ptr, event.attacker != g.side and "!" or " ", event))
print("Control Points:")
controlpoints = g.theater.controlpoints
controlpoints.sort(key=lambda x: x.captured)
for cp in g.theater.controlpoints:
ptr += 1
print("{}. [{}{}] {}{}{}{}".format(
ptr,
cp.captured and "x" or " ",
int(cp.base.readiness * 10),
cp.name,
"^" * cp.base.total_planes,
"." * cp.base.total_armor,
"*" * cp.base.total_aa))
events_boundary = len(g.events)
try:
selected_idx = int(input(">").strip()) - 1
except:
continue
if selected_idx == -1:
g.pass_turn()
continue
if selected_idx < events_boundary:
event = g.events[selected_idx]
else:
selected_cp = controlpoints[selected_idx - events_boundary]
else:
print("Units on the base: ")
for unit, count in selected_cp.base.all_units:
print("{} ({}) ".format(unit.name and unit.name or unit.id, count), end="")
print("")
try:
selected_idx = int(input(">").strip()) - 1
except:
continue
if selected_idx == -1:
selected_cp = None
if not os.path.exists("./build"):
os.mkdir("./build")
m.save("build/output.miz")
"""

View File

@ -1,7 +1,4 @@
import typing
import dcs
import globals
from dcs.vehicles import *
from dcs.unitgroup import *

View File

@ -1,11 +1,3 @@
import typing
import random
import math
import dcs
from theater.controlpoint import *
from userdata.debriefing_parser import *
from game.operation import *
DIFFICULTY_LOG_BASE = 1.5
@ -18,12 +10,12 @@ class Event:
difficulty = 1 # type: int
BONUS_BASE = 0
def __init__(self, attacker_name: str, defender_name: str, from_cp: ControlPoint, to_cp: ControlPoint):
self.mission = dcs.mission.Mission()
self.attacker = self.mission.country(attacker_name)
self.defender = self.mission.country(defender_name)
def __init__(self, attacker_name: str, defender_name: str, from_cp: ControlPoint, to_cp: ControlPoint, theater: ConflictTheater):
self.attacker_name = attacker_name
self.defender_name = defender_name
self.to_cp = to_cp
self.from_cp = from_cp
self.theater = theater
def bonus(self) -> int:
return math.ceil(math.log(self.difficulty, DIFFICULTY_LOG_BASE) * self.BONUS_BASE)
@ -31,10 +23,19 @@ class Event:
def is_successfull(self, debriefing: Debriefing) -> bool:
return self.operation.is_successfull(debriefing)
def generate(self):
self.operation.prepare(is_quick=False)
self.operation.generate()
self.operation.mission.save("build/nextturn.miz")
self.operation.prepare(is_quick=True)
self.operation.generate()
self.operation.mission.save('build/nextturn_quick.miz')
def commit(self, debriefing: Debriefing):
for country, losses in debriefing.destroyed_units.items():
cp = None # type: ControlPoint
if country == self.attacker.name:
if country == self.attacker_name:
cp = self.from_cp
else:
cp = self.to_cp
@ -72,27 +73,29 @@ class GroundInterceptEvent(Event):
pass
def player_attacking(self, position: Point, strikegroup: db.PlaneDict, clients: db.PlaneDict):
suitable_unittypes = db.find_unittype(CAP, self.defender.name)
suitable_unittypes = db.find_unittype(CAP, self.defender_name)
random.shuffle(suitable_unittypes)
unittypes = suitable_unittypes[:self.TARGET_VARIETY]
typecount = max(math.floor(self.difficulty * self.TARGET_AMOUNT_FACTOR), 1)
targets = {unittype: typecount for unittype in unittypes}
self.operation = GroundInterceptOperation(mission=self.mission,
attacker=self.attacker,
defender=self.defender,
attacker_clients=clients,
defender_clients={},
from_cp=self.from_cp,
position=position,
target=targets,
strikegroup=strikegroup)
op = GroundInterceptOperation(attacker_name=self.attacker_name,
defender_name=self.defender_name,
attacker_clients=clients,
defender_clients={},
from_cp=self.from_cp)
op.setup(position=position,
target=targets,
strikegroup=strikegroup)
self.operation = op
class InterceptEvent(Event):
ESCORT_AMOUNT_FACTOR = 2
BONUS_BASE = 5
STRENGTH_INFLUENCE = 0.25
GLOBAL_STRENGTH_INFLUENCE = 0.05
AIRDEFENSE_COUNT = 3
def __str__(self):
@ -101,7 +104,11 @@ class InterceptEvent(Event):
def commit(self, debriefing: Debriefing):
super(InterceptEvent, self).commit(debriefing)
if self.is_successfull(debriefing):
self.to_cp.base.affect_strength(self.STRENGTH_INFLUENCE * float(self.from_cp.captured and -1 or 1))
if self.from_cp.is_global:
for cp in self.theater.enemy_points():
cp.base.affect_strength(-self.GLOBAL_STRENGTH_INFLUENCE)
else:
self.to_cp.base.affect_strength(self.STRENGTH_INFLUENCE * float(self.from_cp.captured and -1 or 1))
else:
self.to_cp.base.affect_strength(self.STRENGTH_INFLUENCE * float(self.from_cp.captured and 1 or -1))
@ -111,39 +118,43 @@ class InterceptEvent(Event):
def player_attacking(self, interceptors: db.PlaneDict, clients: db.PlaneDict):
escort = self.to_cp.base.scramble_sweep(self.to_cp)
transport_unit = random.choice(db.find_unittype(Transport, self.defender.name))
transport_unit = random.choice(db.find_unittype(Transport, self.defender_name))
assert transport_unit is not None
airdefense_unit = db.find_unittype(AirDefence, self.defender.name)[0]
airdefense_unit = db.find_unittype(AirDefence, self.defender_name)[0]
self.operation = InterceptOperation(mission=self.mission,
attacker=self.attacker,
defender=self.defender,
attacker_clients=clients,
defender_clients={},
from_cp=self.from_cp,
to_cp=self.to_cp,
escort=escort,
transport={transport_unit: 1},
airdefense={airdefense_unit: self.AIRDEFENSE_COUNT},
interceptors=interceptors)
op = InterceptOperation(attacker_name=self.attacker_name,
defender_name=self.defender_name,
attacker_clients=clients,
defender_clients={},
from_cp=self.from_cp,
to_cp=self.to_cp)
op.setup(escort=escort,
transport={transport_unit: 1},
airdefense={airdefense_unit: self.AIRDEFENSE_COUNT},
interceptors=interceptors)
self.operation = op
def player_defending(self, escort: db.PlaneDict, clients: db.PlaneDict):
interceptors = self.from_cp.base.scramble_interceptors_count(self.difficulty * self.ESCORT_AMOUNT_FACTOR)
transport_unit = random.choice(db.find_unittype(Transport, self.defender.name))
transport_unit = random.choice(db.find_unittype(Transport, self.defender_name))
assert transport_unit is not None
self.operation = InterceptOperation(mission=self.mission,
attacker=self.attacker,
defender=self.defender,
attacker_clients={},
defender_clients=clients,
from_cp=self.from_cp,
to_cp=self.to_cp,
escort=escort,
transport={transport_unit: 1},
interceptors=interceptors,
airdefense={})
op = InterceptOperation(attacker_name=self.attacker_name,
defender_name=self.defender_name,
attacker_clients={},
defender_clients=clients,
from_cp=self.from_cp,
to_cp=self.to_cp)
op.setup(escort=escort,
transport={transport_unit: 1},
interceptors=interceptors,
airdefense={})
self.operation = op
class CaptureEvent(Event):
@ -159,6 +170,9 @@ class CaptureEvent(Event):
if self.is_successfull(debriefing):
if self.from_cp.captured:
self.to_cp.captured = True
self.to_cp.base.filter_units(db.UNIT_BY_COUNTRY[self.attacker_name])
self.to_cp.base.affect_strength(+self.STRENGTH_RECOVERY)
else:
if not self.from_cp.captured:
self.to_cp.captured = False
@ -173,47 +187,52 @@ class CaptureEvent(Event):
escort = self.from_cp.base.scramble_sweep(self.to_cp)
attackers = self.from_cp.base.assemble_cap(self.to_cp)
self.operation = CaptureOperation(mission=self.mission,
attacker=self.attacker,
defender=self.defender,
attacker_clients={},
defender_clients=clients,
from_cp=self.from_cp,
to_cp=self.to_cp,
cas=cas,
escort=escort,
attack=attackers,
intercept=interceptors,
defense=self.to_cp.base.armor,
aa=self.to_cp.base.aa)
op = CaptureOperation(attacker_name=self.attacker_name,
defender_name=self.defender_name,
attacker_clients={},
defender_clients=clients,
from_cp=self.from_cp,
to_cp=self.to_cp)
op.setup(cas=cas,
escort=escort,
attack=attackers,
intercept=interceptors,
defense=self.to_cp.base.armor,
aa=self.to_cp.base.aa)
self.operation = op
def player_attacking(self, cas: db.PlaneDict, escort: db.PlaneDict, armor: db.ArmorDict, clients: db.PlaneDict):
interceptors = self.to_cp.base.scramble_sweep(for_target=self.to_cp)
self.operation = CaptureOperation(mission=self.mission,
attacker=self.attacker,
defender=self.defender,
attacker_clients=clients,
defender_clients={},
from_cp=self.from_cp,
to_cp=self.to_cp,
cas=cas,
escort=escort,
attack=armor,
intercept=interceptors,
defense=self.to_cp.base.armor,
aa=self.to_cp.base.aa)
op = CaptureOperation(attacker_name=self.attacker_name,
defender_name=self.defender_name,
attacker_clients=clients,
defender_clients={},
from_cp=self.from_cp,
to_cp=self.to_cp)
op.setup(cas=cas,
escort=escort,
attack=armor,
intercept=interceptors,
defense=self.to_cp.base.armor,
aa=self.to_cp.base.aa)
self.operation = op
class UnitsDeliveryEvent(Event):
informational = True
units = None # type: typing.Dict[UnitType, int]
def __init__(self, attacker_name: str, defender_name: str, from_cp: ControlPoint, to_cp: ControlPoint):
def __init__(self, attacker_name: str, defender_name: str, from_cp: ControlPoint, to_cp: ControlPoint, theater: ConflictTheater):
super(UnitsDeliveryEvent, self).__init__(attacker_name=attacker_name,
defender_name=defender_name,
from_cp=from_cp,
to_cp=to_cp)
to_cp=to_cp,
theater=theater)
self.units = {}

View File

@ -1,9 +1,3 @@
import typing
import random
from theater.conflicttheater import *
from theater.controlpoint import *
from userdata.debriefing_parser import *
from game.event import *
COMMISION_LIMITS_SCALE = 2
@ -24,11 +18,15 @@ COMMISION_AMOUNTS_FACTORS = {
ENEMY_INTERCEPT_PROBABILITY_BASE = 10
ENEMY_INTERCEPT_GLOBAL_PROBABILITY_BASE = 1
ENEMY_CAPTURE_PROBABILITY_BASE = 3
PLAYER_INTERCEPT_PROBABILITY_BASE = 30
PLAYER_GROUNDINTERCEPT_PROBABILITY_BASE = 30
PLAYER_INTERCEPT_GLOBAL_PROBABILITY_BASE = 50
PLAYER_INTERCEPT_GLOBAL_PROBABILITY_LOG = 2
PLAYER_BUDGET_BASE = 25
PLAYER_BUDGET_IMPORTANCE_LOG = 2
@ -53,7 +51,8 @@ class Game:
self.events.append(CaptureEvent(attacker_name=self.player,
defender_name=self.enemy,
from_cp=from_cp,
to_cp=to_cp))
to_cp=to_cp,
theater=self.theater))
def _generate_enemy_caps(self):
for from_cp, to_cp in self.theater.conflicts(False):
@ -64,10 +63,12 @@ class Game:
self.events.append(CaptureEvent(attacker_name=self.enemy,
defender_name=self.player,
from_cp=from_cp,
to_cp=to_cp))
to_cp=to_cp,
theater=self.theater))
break
def _generate_interceptions(self):
enemy_interception = False
for from_cp, to_cp in self.theater.conflicts(False):
if from_cp.base.total_units(FighterSweep) == 0:
continue
@ -76,15 +77,36 @@ class Game:
self.events.append(InterceptEvent(attacker_name=self.enemy,
defender_name=self.player,
from_cp=from_cp,
to_cp=to_cp))
to_cp=to_cp,
theater=self.theater))
enemy_interception = True
break
for to_cp in self.theater.player_points():
if enemy_interception:
break
if to_cp in self.theater.conflicts(False):
continue
if self._roll(ENEMY_INTERCEPT_GLOBAL_PROBABILITY_BASE, 1):
for from_cp, _ in self.theater.conflicts(False):
if from_cp.base.total_units(FighterSweep) > 0:
self.events.append(InterceptEvent(attacker_name=self.enemy,
defender_name=self.player,
from_cp=from_cp,
to_cp=to_cp,
theater=self.theater))
enemy_interception = True
break
for from_cp, to_cp in self.theater.conflicts(True):
if self._roll(PLAYER_INTERCEPT_PROBABILITY_BASE, from_cp.base.strength):
self.events.append(InterceptEvent(attacker_name=self.player,
defender_name=self.enemy,
from_cp=from_cp,
to_cp=to_cp))
to_cp=to_cp,
theater=self.theater))
break
def _generate_groundinterceptions(self):
@ -93,7 +115,20 @@ class Game:
self.events.append(GroundInterceptEvent(attacker_name=self.player,
defender_name=self.enemy,
from_cp=from_cp,
to_cp=to_cp))
to_cp=to_cp,
theater=self.theater))
break
def _generate_globalinterceptions(self):
for from_cp in [x for x in self.theater.player_points() if x.is_global]:
probability = PLAYER_INTERCEPT_GLOBAL_PROBABILITY_BASE * math.log(len(self.theater.player_points()) + 1, PLAYER_INTERCEPT_GLOBAL_PROBABILITY_LOG)
if self._roll(probability, from_cp.base.strength):
to_cp = random.choice([x for x in self.theater.enemy_points() if x not in self.theater.conflicts()])
self.events.append(InterceptEvent(attacker_name=self.player,
defender_name=self.enemy,
from_cp=from_cp,
to_cp=to_cp,
theater=self.theater))
break
def _commision_units(self, cp: ControlPoint):
@ -123,7 +158,8 @@ class Game:
event = UnitsDeliveryEvent(attacker_name=self.player,
defender_name=self.player,
from_cp=to_cp,
to_cp=to_cp)
to_cp=to_cp,
theater=self.theater)
self.events.append(event)
return event
@ -132,8 +168,8 @@ class Game:
self.events.remove(event)
def initiate_event(self, event: Event):
event.operation.generate()
event.mission.save("build/next_mission.miz")
assert event in self.events
event.generate()
def finish_event(self, event: Event, debriefing: Debriefing):
event.commit(debriefing)
@ -143,7 +179,7 @@ class Game:
self.events.remove(event)
def is_player_attack(self, event: Event):
return event.attacker.name == self.player
return event.attacker_name == self.player
def pass_turn(self, no_action=False):
for event in self.events:
@ -151,12 +187,13 @@ class Game:
if not no_action:
self._budget_player()
for cp in self.theater.enemy_bases():
for cp in self.theater.enemy_points():
self._commision_units(cp)
self.events = [] # type: typing.List[Event]
self._fill_cap_events()
self._generate_enemy_caps()
self._generate_interceptions()
self._generate_globalinterceptions()
self._generate_groundinterceptions()

View File

@ -1,14 +1,6 @@
import typing
from globals import *
from userdata.debriefing_parser import *
from dcs.mission import *
from dcs.unitgroup import *
from dcs.vehicles import *
from theater.controlpoint import *
from theater.conflicttheater import *
from theater.base import *
from shop import *
from gen.armor import *
from gen.aircraft import *
@ -18,13 +10,42 @@ from gen.conflictgen import *
class Operation:
def __init__(self, mission: Mission, conflict: Conflict):
starting_position = None # type: db.StartingPosition
mission = None # type: dcs.Mission
conflict = None # type: Conflict
armorgen = None # type: ArmorConflictGenerator
airgen = None # type: AircraftConflictGenerator
aagen = None # type: AAConflictGenerator
shipgen = None # type: ShipGenerator
def __init__(self,
attacker_name: str,
defender_name: str,
attacker_clients: db.PlaneDict,
defender_clients: db.PlaneDict,
from_cp: ControlPoint,
to_cp: ControlPoint = None):
self.attacker_name = attacker_name
self.defender_name = defender_name
self.attacker_clients = attacker_clients
self.defender_clients = defender_clients
self.from_cp = from_cp
self.to_cp = to_cp
def initialize(self, mission: Mission, conflict: Conflict):
self.mission = mission
self.conflict = conflict
self.armorgen = ArmorConflictGenerator(self.mission, self.conflict)
self.airgen = AircraftConflictGenerator(self.mission, self.conflict)
self.aagen = AAConflictGenerator(self.mission, self.conflict)
self.shipgen = ShipGenerator(self.mission, self.conflict)
self.armorgen = ArmorConflictGenerator(mission, conflict)
self.airgen = AircraftConflictGenerator(mission, conflict)
self.aagen = AAConflictGenerator(mission, conflict)
self.shipgen = ShipGenerator(mission, conflict)
def prepare(self, is_quick: bool):
self.starting_position = is_quick and self.from_cp.at or None
def generate(self):
pass
def units_of(self, country_name: str) -> typing.Collection[UnitType]:
return []
@ -32,81 +53,77 @@ class Operation:
def is_successfull(self, debriefing: Debriefing) -> bool:
return True
def generate(self):
pass
class CaptureOperation(Operation):
def __init__(self,
mission: Mission,
attacker: Country,
defender: Country,
attacker_clients: db.PlaneDict,
defender_clients: db.PlaneDict,
from_cp: ControlPoint,
to_cp: ControlPoint,
cas: db.PlaneDict,
escort: db.PlaneDict,
attack: db.ArmorDict,
intercept: db.PlaneDict,
defense: db.ArmorDict,
aa: db.AirDefenseDict):
conflict = to_cp.conflict_attack(from_cp, attacker, defender)
cas = None # type: db.PlaneDict
escort = None # type: db.PlaneDict
intercept = None # type: db.PlaneDict
attack = None # type: db.ArmorDict
defense = None # type: db.ArmorDict
aa = None # type: db.AirDefenseDict
super(CaptureOperation, self).__init__(mission, conflict)
self.from_cp = from_cp
self.to_cp = to_cp
self.attacker_clients = attacker_clients
self.defender_clients = defender_clients
self.cas = cas
self.escort = escort
self.intercept = intercept
def setup(self,
cas: db.PlaneDict,
escort: db.PlaneDict,
attack: db.ArmorDict,
intercept: db.PlaneDict,
defense: db.ArmorDict,
aa: db.AirDefenseDict):
self.cas = cas
self.escort = escort
self.intercept = intercept
self.attack = attack
self.defense = defense
self.aa = aa
self.attack = attack
self.defense = defense
def prepare(self, is_quick: bool):
super(CaptureOperation, self).prepare(is_quick)
mission = dcs.Mission()
self.aa = aa
self.initialize(mission=mission,
conflict=self.to_cp.conflict_attack(self.from_cp,
mission.country(self.attacker_name),
mission.country(self.defender_name)))
def generate(self):
self.armorgen.generate(self.attack, self.defense)
self.aagen.generate(self.aa)
self.airgen.generate_defense(self.intercept, clients=self.defender_clients)
self.airgen.generate_cas(self.cas, clients=self.attacker_clients, at=self.from_cp.at)
self.airgen.generate_cas_escort(self.escort, clients=self.attacker_clients, at=self.from_cp.at)
self.airgen.generate_cas(self.cas, clients=self.attacker_clients, at=self.starting_position)
self.airgen.generate_cas_escort(self.escort, clients=self.attacker_clients, at=self.starting_position)
class InterceptOperation(Operation):
def __init__(self,
mission: Mission,
attacker: Country,
defender: Country,
attacker_clients: db.PlaneDict,
defender_clients: db.PlaneDict,
from_cp: ControlPoint,
to_cp: ControlPoint,
escort: db.PlaneDict,
transport: db.PlaneDict,
airdefense: db.AirDefenseDict,
interceptors: db.PlaneDict):
conflict = Conflict.intercept_conflict(
attacker=attacker,
defender=defender,
position=to_cp.position,
heading=randint(0, 360),
radials=ALL_RADIALS
)
escort = None # type: db.PlaneDict
transport = None # type: db.PlaneDict
interceptors = None # type: db.PlaneDict
airdefense = None # type: db.AirDefenseDict
super(InterceptOperation, self).__init__(mission, conflict)
self.to_cp = to_cp
self.from_cp = from_cp
self.attacker_clients = attacker_clients
self.defender_clients = defender_clients
def setup(self,
escort: db.PlaneDict,
transport: db.PlaneDict,
airdefense: db.AirDefenseDict,
interceptors: db.PlaneDict):
self.escort = escort
self.transport = transport
self.airdefense = airdefense
self.interceptors = interceptors
def prepare(self, is_quick: bool):
super(InterceptOperation, self).prepare(is_quick)
mission = dcs.Mission()
conflict = Conflict.intercept_conflict(
attacker=mission.country(self.attacker_name),
defender=mission.country(self.defender_name),
position=self.from_cp.position,
heading=randint(0, 360),
radials=ALL_RADIALS
)
self.initialize(mission=mission,
conflict=conflict)
def generate(self):
self.airgen.generate_transport(self.transport, self.to_cp.at)
self.airgen.generate_transport_escort(self.escort, clients=self.defender_clients)
@ -115,35 +132,31 @@ class InterceptOperation(Operation):
if self.from_cp.is_global:
self.airgen.generate_interception(self.interceptors, clients=self.attacker_clients, at=self.shipgen.generate(self.from_cp.at))
else:
self.airgen.generate_interception(self.interceptors, clients=self.attacker_clients, at=self.from_cp.at)
self.airgen.generate_interception(self.interceptors, clients=self.attacker_clients, at=self.starting_position)
class GroundInterceptOperation(Operation):
def __init__(self,
mission: Mission,
attacker: Country,
defender: Country,
from_cp: ControlPoint,
attacker_clients: db.PlaneDict,
defender_clients: db.PlaneDict,
position: Point,
target: db.ArmorDict,
strikegroup: db.PlaneDict):
def setup(self,
position: Point,
target: db.ArmorDict,
strikegroup: db.PlaneDict):
self.position = position
self.strikegroup = strikegroup
self.target = target
def prepare(self, is_quick: bool):
super(GroundInterceptOperation, self).prepare(is_quick)
mission = dcs.Mission()
conflict = Conflict.ground_intercept_conflict(
attacker=attacker,
defender=defender,
position=position,
attacker=mission.country(self.defender_name),
defender=mission.country(self.defender_name),
position=self.position,
heading=randint(0, 360),
radials=ALL_RADIALS
)
super(GroundInterceptOperation, self).__init__(mission, conflict)
self.attacker_clients = attacker_clients
self.defender_clients = defender_clients
self.from_cp = from_cp
self.strikegroup = strikegroup
self.target = target
def generate(self):
self.airgen.generate_cas(self.strikegroup, clients=self.attacker_clients, at=self.from_cp.at)
self.airgen.generate_cas(self.strikegroup, clients=self.attacker_clients, at=self.starting_position)
self.armorgen.generate({}, self.target)

View File

@ -1,21 +1,9 @@
import typing
import pdb
import dcs
from random import randint
import globals
from game import db
from .conflictgen import *
from .naming import *
from dcs.mission import *
from dcs.vehicles import *
from dcs.unitgroup import *
from dcs.unittype import *
from dcs.mapping import *
from dcs.point import *
from dcs.task import *
DISTANCE_FACTOR = 4, 5
@ -24,7 +12,7 @@ class AAConflictGenerator:
self.m = mission
self.conflict = conflict
def generate(self, units: typing.Dict[UnitType, int]):
def generate(self, units: db.AirDefenseDict):
for type, count in units.items():
for _, radial in zip(range(count), self.conflict.radials):
distance = randint(self.conflict.size * DISTANCE_FACTOR[0], self.conflict.size * DISTANCE_FACTOR[1])

View File

@ -1,21 +1,10 @@
import typing
import pdb
import dcs
from random import randint
import globals
from shop import db
from game import db
from .conflictgen import *
from .naming import *
from dcs.mission import *
from dcs.vehicles import *
from dcs.unitgroup import *
from dcs.unittype import *
from dcs.mapping import *
from dcs.point import *
from dcs.task import *
SPREAD_DISTANCE_FACTOR = 1, 2

View File

@ -1,20 +1,9 @@
import typing
import pdb
import dcs
from random import randint
import globals
from shop import db
from game import db
from .conflictgen import *
from .naming import *
from dcs.mission import *
from dcs.vehicles import *
from dcs.unitgroup import *
from dcs.unittype import *
from dcs.mapping import *
from dcs.point import *
from dcs.task import *
from dcs.country import *

View File

@ -1,20 +1,8 @@
import typing
import pdb
import dcs
from random import randint
import globals
from .conflictgen import *
from .naming import *
from dcs.mission import *
from dcs.vehicles import *
from dcs.unitgroup import *
from dcs.unittype import *
from dcs.mapping import *
from dcs.point import *
from dcs.task import *

View File

@ -1,5 +0,0 @@
import dcs
MISSION = dcs.mission.Mission()
US = MISSION.country("USA")
THEM = MISSION.country("Russia")

View File

View File

@ -1,11 +1,8 @@
import typing
import math
import random
import itertools
import dcs
from shop import db
from game import db
from theater.controlpoint import ControlPoint
from dcs.planes import *
@ -105,6 +102,10 @@ class Base:
return 0
def filter_units(self, applicable_units: typing.Collection):
self.aircraft = {k: v for k, v in self.aircraft.items() if k in applicable_units}
self.armor = {k: v for k, v in self.aircraft.items() if k in applicable_units}
def commision_units(self, units: typing.Dict[typing.Any, int]):
for value in units.values():
assert value > 0

View File

@ -40,5 +40,5 @@ class ConflictTheater:
for connected_point in [x for x in cp.connected_points if x.captured != from_player]:
yield (cp, connected_point)
def enemy_bases(self) -> typing.Collection[ControlPoint]:
def enemy_points(self) -> typing.Collection[ControlPoint]:
return [point for point in self.controlpoints if not point.captured]

View File

@ -1,10 +1,3 @@
import typing
import random
import dcs
from shop import db
from theater.controlpoint import *
from theater.base import *
from theater.conflicttheater import *
@ -13,7 +6,7 @@ UNIT_AMOUNT_FACTOR = 1
def generate_initial(theater: ConflictTheater, enemy: str):
for cp in theater.enemy_bases():
for cp in theater.enemy_points():
if cp.captured:
continue

View File

@ -1,7 +1,3 @@
from shop import db
from tkinter import *
from ui.window import *
from ui.eventmenu import *
from game.game import *

View File

@ -1,10 +1,7 @@
from tkinter import *
from ui.window import *
from ui.eventresultsmenu import *
from shop import db
from game.game import *
from game import event
from game import event, db
class EventMenu(Menu):
@ -55,7 +52,7 @@ class EventMenu(Menu):
row += 1
base = None # type: Base
if self.event.attacker.name == self.game.player:
if self.event.attacker_name == self.game.player:
base = self.event.from_cp.base
else:
base = self.event.to_cp.base

View File

@ -1,13 +1,7 @@
import math
import itertools
from tkinter import *
from tkinter.ttk import *
from ui.window import *
from userdata.debriefing_parser import *
from game.game import *
from game import event
class EventResultsMenu(Menu):
@ -21,18 +15,22 @@ class EventResultsMenu(Menu):
self.event = event
self.finished = False
wait_for_debriefing(lambda: self.process_debriefing)
def display(self):
self.window.clear_right_pane()
if not self.finished:
Button(self.frame, text="no losses, succ", command=self.simulate_result(0, 1, True)).grid()
Button(self.frame, text="no losses, fail", command=self.simulate_result(0, 1, False)).grid(column=1)
Button(self.frame, text="no losses, fail", command=self.simulate_result(0, 1, False)).grid(row=1, column=1)
Button(self.frame, text="half losses, succ", command=self.simulate_result(0.5, 0.5, True)).grid(row=1, )
Button(self.frame, text="half losses, fail", command=self.simulate_result(0.5, 0.5, False)).grid(row=1, column=1)
Button(self.frame, text="half losses, succ", command=self.simulate_result(0.5, 0.5, True)).grid(row=2, )
Button(self.frame, text="half losses, fail", command=self.simulate_result(0.5, 0.5, False)).grid(row=2, column=1)
Button(self.frame, text="full losses, succ", command=self.simulate_result(1, 0, True)).grid(row=2, )
Button(self.frame, text="full losses, fail", command=self.simulate_result(1, 0, False)).grid(row=2, column=1)
Button(self.frame, text="full losses, succ", command=self.simulate_result(1, 0, True)).grid(row=3, )
Button(self.frame, text="full losses, fail", command=self.simulate_result(1, 0, False)).grid(row=3, column=1)
Label(self.frame, text="Play the mission and save debriefing to {}".format(debriefing_directory_location())).grid(row=0, column=0)
else:
row = 0
if self.event.is_successfull(self.debriefing):
@ -56,6 +54,15 @@ class EventResultsMenu(Menu):
Button(self.frame, text="Okay", command=self.dismiss).grid(columnspan=1, row=row); row += 1
def process_debriefing(self, debriefing: Debriefing):
self.game.finish_event(debriefing)
self.game.pass_turn()
self.finished = True
self.player_losses = debriefing.destroyed_units[self.game.player]
self.enemy_losses = debriefing.destroyed_units[self.game.enemy]
self.display()
def simulate_result(self, player_factor: float, enemy_factor: float, result: bool):
def action():
debriefing = Debriefing()

View File

@ -1,12 +1,10 @@
from tkinter import *
from tkinter.ttk import *
import pickle
from ui.window import *
from ui.eventmenu import *
from ui.basemenu import *
from ui.overviewcanvas import *
from game.game import *
from userdata import persistency
class MainMenu(Menu):
@ -22,9 +20,10 @@ class MainMenu(Menu):
self.frame.grid_columnconfigure(0, weight=1)
def display(self):
persistency.save_game(self.game)
self.window.clear_right_pane()
self.upd.update()
row = 1
def label(text):
@ -51,7 +50,13 @@ class MainMenu(Menu):
if event.informational:
continue
event_button(event, "{} {}".format(event.attacker.name != self.game.player and "!" or " ", event))
prefix = ""
if event.attacker_name != self.game.player:
prefix += "!"
elif event.from_cp.is_global:
prefix += "CARRIER"
event_button(event, "{} {}".format(prefix, event))
def pass_turn(self):
self.game.pass_turn(no_action=True)

View File

@ -79,7 +79,9 @@ class OverviewCanvas:
extent=extent)
self.canvas.tag_bind(cp_id, "<Button-1>", self.display(cp))
self.create_cp_title((coords[0] + arc_size/2, coords[1] + arc_size/2), cp)
self.canvas.create_text(coords[0], coords[1] - arc_size / 1.5, text="8/4/2", font=("Helvetica", 10))
units_title = "{}/{}/{}".format(cp.base.total_planes, cp.base.total_armor, cp.base.total_aa)
self.canvas.create_text(coords[0], coords[1] - arc_size / 1.5, text=units_title, font=("Helvetica", 10))
def display(self, cp: ControlPoint):
def action(_):

View File

@ -1,15 +0,0 @@
import dcs
money = 2000
aircraft = []
armor = []
control_points = []
def add_aircraft(plane: dcs.planes.PlaneType):
aircraft.append(plane)
def add_armor(vehicle: dcs.vehicles.Armor):
armor.append(vehicle)
def add_control_point(cp):
control_points.append(cp)

View File

@ -10,3 +10,10 @@ class Debriefing:
with open(path, "r") as f:
events = json.load(f)
def debriefing_directory_location() -> str:
return "build/debrfiefing"
def wait_for_debriefing(callback: typing.Callable):
pass

38
userdata/persistency.py Normal file
View File

@ -0,0 +1,38 @@
import pickle
import os
import shutil
from game.game import Game
def _save_file() -> str:
return "build/save"
def _temporary_save_file() -> str:
return "build/save_tmp"
def _save_file_exists() -> bool:
return os.path.exists(_save_file())
def restore_game() -> Game:
if not _save_file_exists():
return None
try:
with open(_save_file(), "rb") as f:
return pickle.load(f)
except:
return None
def save_game(game: Game) -> bool:
try:
with open(_temporary_save_file(), "wb") as f:
pickle.dump(game, f)
shutil.copy(_temporary_save_file(), _save_file())
return True
except:
return False