コード例 #1
0
 def add_unit(self, unit_type, name, pos_x, pos_y, heading) -> Ship:
     unit = Ship(self.game.next_unit_id(), f"{self.go.group_name}|{name}",
                 unit_type)
     unit.position.x = pos_x
     unit.position.y = pos_y
     unit.heading = heading
     self.vg.add_unit(unit)
     return unit
コード例 #2
0
 def create_ship(self, unit: Unit, atc_channel: RadioFrequency) -> Ship:
     ship = Ship(self.m.next_unit_id(), self.m.string(unit.name),
                 unit_type_from_name(unit.type))
     ship.position.x = unit.position.x
     ship.position.y = unit.position.y
     ship.heading = unit.heading
     # TODO: Verify.
     ship.set_frequency(atc_channel.hertz)
     return ship
コード例 #3
0
 def add_unit_to_group(
     self,
     group: ShipGroup,
     unit_type: Type[ShipType],
     name: str,
     position: Point,
     heading: int,
 ) -> Ship:
     unit = Ship(self.game.next_unit_id(), f"{self.go.group_name}|{name}",
                 unit_type)
     unit.position = position
     unit.heading = heading
     group.add_unit(unit)
     return unit
コード例 #4
0
 def generate_group(self, group_def: Group, unit_type: UnitType):
     group = self.m.ship_group(self.country,
                               group_def.name,
                               unit_type,
                               position=group_def.position,
                               heading=group_def.units[0].heading)
     group.units[0].name = self.m.string(group_def.units[0].name)
     # TODO: Skipping the first unit looks like copy pasta from the carrier.
     for unit in group_def.units[1:]:
         unit_type = unit_type_from_name(unit.type)
         ship = Ship(self.m.next_unit_id(), self.m.string(unit.name),
                     unit_type)
         ship.position.x = unit.position.x
         ship.position.y = unit.position.y
         ship.heading = unit.heading
         group.add_unit(ship)
     self.set_alarm_state(group)
コード例 #5
0
 def generate_group(self, group_def: ShipGroup,
                    first_unit_type: Type[ShipType]) -> None:
     group = self.m.ship_group(
         self.country,
         group_def.name,
         first_unit_type,
         position=group_def.position,
         heading=group_def.units[0].heading,
     )
     group.units[0].name = group_def.units[0].name
     # TODO: Skipping the first unit looks like copy pasta from the carrier.
     for unit in group_def.units[1:]:
         unit_type = unit_type_from_name(unit.type)
         ship = Ship(self.m.next_unit_id(), unit.name, unit_type)
         ship.position.x = unit.position.x
         ship.position.y = unit.position.y
         ship.heading = unit.heading
         group.add_unit(ship)
     self.set_alarm_state(group)
     self._register_unit_group(group_def, group)
コード例 #6
0
    def generate(self):

        for cp in self.game.theater.controlpoints:

            if cp.captured:
                country = self.game.player_country
            else:
                country = self.game.enemy_country
            side = self.m.country(country)

            for ground_object in cp.ground_objects:
                if ground_object.dcs_identifier == "AA":

                    if self.game.position_culled(ground_object.position):
                        continue

                    for g in ground_object.groups:
                        if len(g.units) > 0:
                            utype = unit_type_from_name(g.units[0].type)

                            if not ground_object.sea_object:
                                vg = self.m.vehicle_group(
                                    side,
                                    g.name,
                                    utype,
                                    position=g.position,
                                    heading=g.units[0].heading)
                                vg.units[0].name = self.m.string(
                                    g.units[0].name)
                                vg.units[0].player_can_drive = True
                                for i, u in enumerate(g.units):
                                    if i > 0:
                                        vehicle = Vehicle(
                                            self.m.next_unit_id(),
                                            self.m.string(u.name), u.type)
                                        vehicle.position.x = u.position.x
                                        vehicle.position.y = u.position.y
                                        vehicle.heading = u.heading
                                        vehicle.player_can_drive = True
                                        vg.add_unit(vehicle)

                                if hasattr(utype, 'eplrs'):
                                    if utype.eplrs:
                                        vg.points[0].tasks.append(EPLRS(vg.id))
                            else:
                                vg = self.m.ship_group(
                                    side,
                                    g.name,
                                    utype,
                                    position=g.position,
                                    heading=g.units[0].heading)
                                vg.units[0].name = self.m.string(
                                    g.units[0].name)
                                for i, u in enumerate(g.units):
                                    utype = unit_type_from_name(u.type)
                                    if i > 0:
                                        ship = Ship(self.m.next_unit_id(),
                                                    self.m.string(u.name),
                                                    utype)
                                        ship.position.x = u.position.x
                                        ship.position.y = u.position.y
                                        ship.heading = u.heading
                                        vg.add_unit(ship)

                            if self.game.settings.perf_red_alert_state:
                                vg.points[0].tasks.append(OptAlarmState(2))
                            else:
                                vg.points[0].tasks.append(OptAlarmState(1))

                elif ground_object.dcs_identifier in ["CARRIER", "LHA"]:
                    for g in ground_object.groups:
                        if len(g.units) > 0:

                            utype = unit_type_from_name(g.units[0].type)
                            if ground_object.dcs_identifier == "CARRIER" and self.game.settings.supercarrier == True:
                                utype = db.upgrade_to_supercarrier(
                                    utype, cp.name)

                            sg = self.m.ship_group(side,
                                                   g.name,
                                                   utype,
                                                   position=g.position,
                                                   heading=g.units[0].heading)
                            atc_channel = self.radio_registry.alloc_uhf()
                            sg.set_frequency(atc_channel.hertz)
                            sg.units[0].name = self.m.string(g.units[0].name)

                            for i, u in enumerate(g.units):
                                if i > 0:
                                    ship = Ship(self.m.next_unit_id(),
                                                self.m.string(u.name),
                                                unit_type_from_name(u.type))
                                    ship.position.x = u.position.x
                                    ship.position.y = u.position.y
                                    ship.heading = u.heading
                                    # TODO: Verify.
                                    ship.set_frequency(atc_channel.hertz)
                                    sg.add_unit(ship)

                            # Find carrier direction (In the wind)
                            found_carrier_destination = False
                            attempt = 0
                            while not found_carrier_destination and attempt < 5:
                                point = sg.points[
                                    0].position.point_from_heading(
                                        self.m.weather.wind_at_ground.direction
                                        + 180, 100000 - attempt * 20000)
                                if self.game.theater.is_in_sea(point):
                                    found_carrier_destination = True
                                    sg.add_waypoint(point)
                                else:
                                    attempt = attempt + 1

                            # Set UP TACAN and ICLS
                            tacan = self.tacan_registry.alloc_for_band(
                                TacanBand.X)
                            icls_channel = next(self.icls_alloc)
                            # TODO: Assign these properly.
                            if ground_object.dcs_identifier == "CARRIER":
                                tacan_callsign = random.choice([
                                    "STE",
                                    "CVN",
                                    "CVH",
                                    "CCV",
                                    "ACC",
                                    "ARC",
                                    "GER",
                                    "ABR",
                                    "LIN",
                                    "TRU",
                                ])
                            else:
                                tacan_callsign = random.choice([
                                    "LHD",
                                    "LHA",
                                    "LHB",
                                    "LHC",
                                    "LHD",
                                    "LDS",
                                ])
                            sg.points[0].tasks.append(
                                ActivateBeaconCommand(
                                    channel=tacan.number,
                                    modechannel=tacan.band.value,
                                    callsign=tacan_callsign,
                                    unit_id=sg.units[0].id,
                                    aa=False))
                            sg.points[0].tasks.append(
                                ActivateICLSCommand(icls_channel,
                                                    unit_id=sg.units[0].id))
                            # TODO: Make unit name usable.
                            # This relies on one control point mapping exactly
                            # to one LHA, carrier, or other usable "runway".
                            # This isn't wholly true, since the DD escorts of
                            # the carrier group are valid for helicopters, but
                            # they aren't exposed as such to the game. Should
                            # clean this up so that's possible. We can't use the
                            # unit name since it's an arbitrary ID.
                            self.runways[cp.name] = RunwayData(
                                cp.name,
                                "N/A",
                                atc=atc_channel,
                                tacan=tacan,
                                tacan_callsign=tacan_callsign,
                                icls=icls_channel,
                            )

                else:

                    if self.game.position_culled(ground_object.position):
                        continue

                    static_type = None
                    if ground_object.dcs_identifier in warehouse_map:
                        static_type = warehouse_map[
                            ground_object.dcs_identifier]
                    elif ground_object.dcs_identifier in fortification_map:
                        static_type = fortification_map[
                            ground_object.dcs_identifier]
                    elif ground_object.dcs_identifier in FORTIFICATION_UNITS_ID:
                        for f in FORTIFICATION_UNITS:
                            if f.id == ground_object.dcs_identifier:
                                unit_type = f
                                break
                    else:
                        print("Didn't find {} in static _map(s)!".format(
                            ground_object.dcs_identifier))
                        continue

                    if static_type is None:
                        if not ground_object.is_dead:
                            group = self.m.vehicle_group(
                                country=side,
                                name=ground_object.string_identifier,
                                _type=unit_type,
                                position=ground_object.position,
                                heading=ground_object.heading,
                            )
                            logging.info(
                                "generated {}object identifier {} with mission id {}"
                                .format(
                                    "dead " if ground_object.is_dead else "",
                                    group.name, group.id))
                    else:
                        group = self.m.static_group(
                            country=side,
                            name=ground_object.string_identifier,
                            _type=static_type,
                            position=ground_object.position,
                            heading=ground_object.heading,
                            dead=ground_object.is_dead,
                        )

                        logging.info(
                            "generated {}object identifier {} with mission id {}"
                            .format("dead " if ground_object.is_dead else "",
                                    group.name, group.id))
コード例 #7
0
    def load_from_dict(self, mission, d):
        for country_idx in d["country"]:
            imp_country = d["country"][country_idx]
            _country = countries.get_by_id(imp_country["id"])

            if "vehicle" in imp_country:
                for vgroup_idx in imp_country["vehicle"]["group"]:
                    vgroup = imp_country["vehicle"]["group"][vgroup_idx]
                    vg = unitgroup.VehicleGroup(
                        vgroup["groupId"],
                        mission.translation.get_string(vgroup["name"]),
                        vgroup["start_time"])
                    vg.load_from_dict(vgroup)
                    mission.current_group_id = max(mission.current_group_id,
                                                   vg.id)

                    Coalition._import_moving_point(mission, vg, vgroup)

                    # units
                    for imp_unit_idx in vgroup["units"]:
                        imp_unit = vgroup["units"][imp_unit_idx]
                        unit = Vehicle(id=imp_unit["unitId"],
                                       name=mission.translation.get_string(
                                           imp_unit["name"]),
                                       _type=imp_unit["type"])
                        unit.load_from_dict(imp_unit)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      unit.id)
                        vg.add_unit(unit)
                    _country.add_vehicle_group(vg)

            if "ship" in imp_country:
                for group_idx in imp_country["ship"]["group"]:
                    imp_group = imp_country["ship"]["group"][group_idx]
                    vg = unitgroup.ShipGroup(
                        imp_group["groupId"],
                        mission.translation.get_string(imp_group["name"]),
                        imp_group["start_time"])
                    vg.load_from_dict(imp_group)
                    mission.current_group_id = max(mission.current_group_id,
                                                   vg.id)

                    Coalition._import_moving_point(mission, vg, imp_group)

                    # units
                    for imp_unit_idx in imp_group["units"]:
                        imp_unit = imp_group["units"][imp_unit_idx]
                        unit = Ship(id=imp_unit["unitId"],
                                    name=mission.translation.get_string(
                                        imp_unit["name"]),
                                    _type=ships.ship_map[imp_unit["type"]])
                        unit.load_from_dict(imp_unit)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      unit.id)
                        vg.add_unit(unit)
                    _country.add_ship_group(vg)

            if "plane" in imp_country:
                for pgroup_idx in imp_country["plane"]["group"]:
                    pgroup = imp_country["plane"]["group"][pgroup_idx]
                    plane_group = unitgroup.PlaneGroup(
                        pgroup["groupId"],
                        mission.translation.get_string(pgroup["name"]),
                        pgroup["start_time"])
                    plane_group.load_from_dict(pgroup)
                    mission.current_group_id = max(mission.current_group_id,
                                                   plane_group.id)

                    Coalition._import_moving_point(mission, plane_group,
                                                   pgroup)

                    # units
                    for imp_unit_idx in pgroup["units"]:
                        imp_unit = pgroup["units"][imp_unit_idx]
                        plane = Plane(_id=imp_unit["unitId"],
                                      name=mission.translation.get_string(
                                          imp_unit["name"]),
                                      _type=planes.plane_map[imp_unit["type"]],
                                      _country=_country)
                        plane.load_from_dict(imp_unit)

                        self._park_unit_on_airport(mission, plane_group, plane)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      plane.id)
                        plane_group.add_unit(plane)

                    # check runway start
                    # if plane_group.points[0].airdrome_id is not None and plane_group.units[0].parking is None:
                    #     airport = mission.terrain.airport_by_id(plane_group.points[0].airdrome_id)
                    #     airport.occupy_runway(plane_group)
                    _country.add_plane_group(plane_group)

            if "helicopter" in imp_country:
                for pgroup_idx in imp_country["helicopter"]["group"]:
                    pgroup = imp_country["helicopter"]["group"][pgroup_idx]
                    helicopter_group = unitgroup.HelicopterGroup(
                        pgroup["groupId"],
                        mission.translation.get_string(pgroup["name"]),
                        pgroup["start_time"])
                    helicopter_group.load_from_dict(pgroup)
                    mission.current_group_id = max(mission.current_group_id,
                                                   helicopter_group.id)

                    Coalition._import_moving_point(mission, helicopter_group,
                                                   pgroup)

                    # units
                    for imp_unit_idx in pgroup["units"]:
                        imp_unit = pgroup["units"][imp_unit_idx]
                        heli = Helicopter(
                            _id=imp_unit["unitId"],
                            name=mission.translation.get_string(
                                imp_unit["name"]),
                            _type=helicopters.helicopter_map[imp_unit["type"]],
                            _country=_country)
                        heli.load_from_dict(imp_unit)

                        self._park_unit_on_airport(mission, helicopter_group,
                                                   heli)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      heli.id)
                        helicopter_group.add_unit(heli)

                    # check runway start
                    # if helicopter_group.points[0].airdrome_id is not None and helicopter_group.units[0].parking is None:
                    #     airport = mission.terrain.airport_by_id(helicopter_group.points[0].airdrome_id)
                    #     airport.occupy_runway(helicopter_group)
                    _country.add_helicopter_group(helicopter_group)

            if "static" in imp_country:
                for sgroup_idx in imp_country["static"]["group"]:
                    sgroup = imp_country["static"]["group"][sgroup_idx]
                    static_group = unitgroup.StaticGroup(
                        sgroup["groupId"],
                        mission.translation.get_string(sgroup["name"]))
                    static_group.load_from_dict(sgroup)
                    mission.current_group_id = max(mission.current_group_id,
                                                   static_group.id)

                    Coalition._import_static_point(mission, static_group,
                                                   sgroup)

                    # units
                    for imp_unit_idx in sgroup["units"]:
                        imp_unit = sgroup["units"][imp_unit_idx]
                        if imp_unit["type"] == "FARP":
                            static = FARP(unit_id=imp_unit["unitId"],
                                          name=mission.translation.get_string(
                                              imp_unit["name"]))
                        elif imp_unit["type"] == "SINGLE_HELIPAD":
                            static = SingleHeliPad(
                                unit_id=imp_unit["unitId"],
                                name=mission.translation.get_string(
                                    imp_unit["name"]))
                        else:
                            static = Static(
                                unit_id=imp_unit["unitId"],
                                name=mission.translation.get_string(
                                    imp_unit["name"]),
                                _type=imp_unit["type"])
                        static.load_from_dict(imp_unit)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      static.id)
                        static_group.add_unit(static)
                    _country.add_static_group(static_group)
            self.add_country(_country)
コード例 #8
0
ファイル: coalition.py プロジェクト: gronank/dcs
    def load_from_dict(self, mission, d) -> List[StatusMessage]:
        status: List[StatusMessage] = []
        for country_idx in d["country"]:
            imp_country = d["country"][country_idx]
            _country = countries.get_by_id(imp_country["id"])

            if "vehicle" in imp_country:
                for vgroup_idx in imp_country["vehicle"]["group"]:
                    vgroup = imp_country["vehicle"]["group"][vgroup_idx]
                    vg = unitgroup.VehicleGroup(
                        vgroup["groupId"],
                        self.get_name(mission, vgroup["name"]),
                        vgroup["start_time"])
                    vg.load_from_dict(vgroup)
                    mission.current_group_id = max(mission.current_group_id,
                                                   vg.id)

                    Coalition._import_moving_point(mission, vg, vgroup)

                    # units
                    for imp_unit_idx in vgroup["units"]:
                        imp_unit = vgroup["units"][imp_unit_idx]
                        unit = Vehicle(id=imp_unit["unitId"],
                                       name=self.get_name(
                                           mission, imp_unit["name"]),
                                       _type=imp_unit["type"])
                        unit.load_from_dict(imp_unit)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      unit.id)
                        vg.add_unit(unit)
                    _country.add_vehicle_group(vg)

            if "ship" in imp_country:
                for group_idx in imp_country["ship"]["group"]:
                    imp_group = imp_country["ship"]["group"][group_idx]
                    vg = unitgroup.ShipGroup(
                        imp_group["groupId"],
                        self.get_name(mission, imp_group["name"]),
                        imp_group["start_time"])
                    vg.load_from_dict(imp_group)
                    mission.current_group_id = max(mission.current_group_id,
                                                   vg.id)

                    Coalition._import_moving_point(mission, vg, imp_group)

                    # units
                    for imp_unit_idx in imp_group["units"]:
                        imp_unit = imp_group["units"][imp_unit_idx]
                        unit = Ship(id=imp_unit["unitId"],
                                    name=self.get_name(mission,
                                                       imp_unit["name"]),
                                    _type=ships.ship_map[imp_unit["type"]])
                        unit.load_from_dict(imp_unit)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      unit.id)
                        vg.add_unit(unit)
                    _country.add_ship_group(vg)

            if "plane" in imp_country:
                for pgroup_idx in imp_country["plane"]["group"]:
                    pgroup = imp_country["plane"]["group"][pgroup_idx]
                    plane_group = unitgroup.PlaneGroup(
                        pgroup["groupId"],
                        self.get_name(mission, pgroup["name"]),
                        pgroup["start_time"])
                    plane_group.load_from_dict(pgroup)
                    mission.current_group_id = max(mission.current_group_id,
                                                   plane_group.id)

                    Coalition._import_moving_point(mission, plane_group,
                                                   pgroup)

                    # units
                    for imp_unit_idx in pgroup["units"]:
                        imp_unit = pgroup["units"][imp_unit_idx]
                        plane = Plane(_id=imp_unit["unitId"],
                                      name=self.get_name(
                                          mission, imp_unit["name"]),
                                      _type=planes.plane_map[imp_unit["type"]],
                                      _country=_country)
                        plane.load_from_dict(imp_unit)

                        if _country.reserve_onboard_num(plane.onboard_num):
                            msg = "{c} Plane '{p}' already using tail number: {t}".format(
                                c=self.name.upper(),
                                p=plane.name,
                                t=plane.onboard_num)
                            status.append(
                                StatusMessage(
                                    msg, MessageType.ONBOARD_NUM_DUPLICATE,
                                    MessageSeverity.WARN))
                            print("WARN:", msg, file=sys.stderr)
                        status += self._park_unit_on_airport(
                            mission, plane_group, plane)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      plane.id)
                        plane_group.add_unit(plane)

                    # check runway start
                    # if plane_group.points[0].airdrome_id is not None and plane_group.units[0].parking is None:
                    #     airport = mission.terrain.airport_by_id(plane_group.points[0].airdrome_id)
                    #     airport.occupy_runway(plane_group)
                    _country.add_plane_group(plane_group)

            if "helicopter" in imp_country:
                for pgroup_idx in imp_country["helicopter"]["group"]:
                    pgroup = imp_country["helicopter"]["group"][pgroup_idx]
                    helicopter_group = unitgroup.HelicopterGroup(
                        pgroup["groupId"],
                        self.get_name(mission, pgroup["name"]),
                        pgroup["start_time"])
                    helicopter_group.load_from_dict(pgroup)
                    mission.current_group_id = max(mission.current_group_id,
                                                   helicopter_group.id)

                    Coalition._import_moving_point(mission, helicopter_group,
                                                   pgroup)

                    # units
                    for imp_unit_idx in pgroup["units"]:
                        imp_unit = pgroup["units"][imp_unit_idx]
                        heli = Helicopter(
                            _id=imp_unit["unitId"],
                            name=self.get_name(mission, imp_unit["name"]),
                            _type=helicopters.helicopter_map[imp_unit["type"]],
                            _country=_country)
                        heli.load_from_dict(imp_unit)

                        if _country.reserve_onboard_num(heli.onboard_num):
                            msg = "{c} Helicopter '{h}' already using tail number: {t}".format(
                                c=self.name.upper(),
                                h=heli.name,
                                t=heli.onboard_num)
                            status.append(
                                StatusMessage(
                                    msg, MessageType.ONBOARD_NUM_DUPLICATE,
                                    MessageSeverity.WARN))
                            print("WARN:", msg, file=sys.stderr)
                        status += self._park_unit_on_airport(
                            mission, helicopter_group, heli)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      heli.id)
                        helicopter_group.add_unit(heli)

                    # check runway start
                    # if helicopter_group.points[0].airdrome_id is not None and helicopter_group.units[0].parking is None:
                    #     airport = mission.terrain.airport_by_id(helicopter_group.points[0].airdrome_id)
                    #     airport.occupy_runway(helicopter_group)
                    _country.add_helicopter_group(helicopter_group)

            if "static" in imp_country:
                for sgroup_idx in imp_country["static"]["group"]:
                    sgroup = imp_country["static"]["group"][sgroup_idx]
                    static_group = unitgroup.StaticGroup(
                        sgroup["groupId"],
                        self.get_name(mission, sgroup["name"]))
                    static_group.load_from_dict(sgroup)
                    mission.current_group_id = max(mission.current_group_id,
                                                   static_group.id)

                    Coalition._import_static_point(mission, static_group,
                                                   sgroup)

                    # units
                    for imp_unit_idx in sgroup["units"]:
                        imp_unit = sgroup["units"][imp_unit_idx]
                        if imp_unit["type"] == "FARP":
                            static = FARP(unit_id=imp_unit["unitId"],
                                          name=self.get_name(
                                              mission, imp_unit["name"]))
                        elif imp_unit["type"] == "SINGLE_HELIPAD":
                            static = SingleHeliPad(unit_id=imp_unit["unitId"],
                                                   name=self.get_name(
                                                       mission,
                                                       imp_unit["name"]))
                        else:
                            static = Static(unit_id=imp_unit["unitId"],
                                            name=self.get_name(
                                                mission, imp_unit["name"]),
                                            _type=imp_unit["type"])
                        static.load_from_dict(imp_unit)

                        mission.current_unit_id = max(mission.current_unit_id,
                                                      static.id)
                        static_group.add_unit(static)
                    _country.add_static_group(static_group)
            self.add_country(_country)
        return status