Пример #1
0
    /// <summary>
    /// 构建扇区某条边的gate
    /// </summary>
    /// <param name="map"></param>
    public override void BuildOneEdgeGate(Side edge, Map map)
    {
        // 获取邻居扇区
        Side      neighbourEdge = Util.FlipDirection(edge);
        MapSector ms            = Findneighbour(edge, map);

        if (ms == null)
        {
            return;
        }
        List <MapSector> listChild = map.GetChildSector(this);

        if (listChild == null || listChild.Count == 0)
        {
            return;
        }

        foreach (MapSector lowerSector in listChild)
        {
            // 先判断父子扇区在指定的边是否匹配,核心目的是为了得到父扇区的边。
            if (SectorHelp.LowerSectorEdgeMatchesHigher(this, lowerSector, edge)) // match edge
            {
                List <PathNode> listPathNode = lowerSector.GetPathNodeInEdge(edge);
                foreach (PathNode node in listPathNode) // get nodes to copy from the edge
                {
                    SectorHelp.CreateGate(this, edge, node.tileConnection, ms, neighbourEdge, node.LinkOtherSectorGate.tileConnection);
                }
            }
        }
    }
Пример #2
0
    private static void SetPathData(MapTile startTile, MapTile destinationTile)
    {
        for (int level = 0; level < g_map.SectorLevelNum; level++)
        {
            PathNode s = g_map.GetWayPointPathNode(level, startTile);
            if (s == null)
            {
                MapSector startNodeSector      = startTile.GetMapSector(level, g_map);
                PathNode  startMultiSectorNode = startNodeSector.CreateWayPointInSector(startTile, g_map);
                startNodeSector.CalcWayPointJoinNode(startMultiSectorNode, g_map);
                StartSectorNodes[level] = startMultiSectorNode;
            }
            else
            {
                StartSectorNodes[level] = s;
            }

            PathNode e = g_map.GetWayPointPathNode(level, destinationTile);
            if (e == null)
            {
                MapSector destinationNodeSector      = destinationTile.GetMapSector(level, g_map);
                PathNode  destinationMultiSectorNode = destinationNodeSector.CreateWayPointInSector(destinationTile, g_map);
                destinationNodeSector.CalcWayPointJoinNode(destinationMultiSectorNode, g_map);
                DestSectorNodes[level] = destinationMultiSectorNode;
            }
            else
            {
                DestSectorNodes[level] = e;
            }
        }
    }
Пример #3
0
    /// <summary>
    /// 获取邻居扇区
    /// </summary>
    /// <param name="neighour"></param>
    /// <returns></returns>
    public List <MapSector> FindneighbourMapSector(MapSector cur)
    {
        if (cur == null)
        {
            return(null);
        }
        List <MapSector> ListMs = new List <MapSector>();
        MapSector        ms     = Findneighbour(cur, IntVector2.top);

        if (ms != null)
        {
            ListMs.Add(ms);
        }
        ms = Findneighbour(cur, IntVector2.down);
        if (ms != null)
        {
            ListMs.Add(ms);
        }
        ms = Findneighbour(cur, IntVector2.left);
        if (ms != null)
        {
            ListMs.Add(ms);
        }
        ms = Findneighbour(cur, IntVector2.right);
        if (ms != null)
        {
            ListMs.Add(ms);
        }
        return(ListMs);
    }
Пример #4
0
    /// <summary>
    /// 获取邻居扇区
    /// </summary>
    /// <param name="neighour"></param>
    /// <returns></returns>
    public MapSector Findneighbour(MapSector cur, Side neighour)
    {
        if (cur == null)
        {
            return(null);
        }

        if (neighour == Side.Top)
        {
            return(FindMapSector(cur.level, cur.Pos.Top));
        }
        else if (neighour == Side.Down)
        {
            return(FindMapSector(cur.level, cur.Pos.Down));
        }
        else if (neighour == Side.Left)
        {
            return(FindMapSector(cur.level, cur.Pos.Left));
        }
        else if (neighour == Side.Right)
        {
            return(FindMapSector(cur.level, cur.Pos.Right));
        }
        return(null);
    }
Пример #5
0
    /// <summary>
    /// Rebuild 扇区边上的节点
    /// </summary>
    /// <param name="sector">指定的扇区</param>
    /// <param name="edge">扇区的某条边</param>
    /// <param name="startInSector">扇区中tile的起始位置</param>
    /// <param name="NeighbourSector">相领扇区</param>
    /// <param name="edgeNeighbourSector">邻居扇区上的边</param>
    /// <param name="startInNeighbourSector">邻居扇区中tile的起始位置</param>
    /// <param name="direction">方向</param>
    public static void RebuildNodesOnSectorEdge(MapSector sector, Side edge, IntVector2 startInSector,
                                                MapSector NeighbourSector, Side edgeNeighbourSector, IntVector2 startInNeighbourSector,
                                                IntVector2 direction)
    {
        if (g_Map == null)
        {
            return;
        }
        // 移除本扇区edgeIndex 边上的所有节点
        sector.RemoveAllWapPointOnSectorEdge(edge, g_Map);
        if (NeighbourSector != null)
        {
            NeighbourSector.RemoveAllWapPointOnSectorEdge(edgeNeighbourSector, g_Map);
        }

        int maxStep = 0;

        if (direction == IntVector2.right)
        {
            maxStep = sector.tilesInWidth;
        }
        else
        {
            maxStep = sector.tilesInHeight;
        }

        //判断邻居扇区是否有包含Tile,没有tile就是边缘了,那这条边节点为空就好。
        if (NeighbourSector != null) // if we havent found any tiles, no reason to try and build connections
        {
            List <MapTile> listGate = CalcGate(startInSector, startInNeighbourSector, direction, maxStep);
            CreateGate(sector, edge, NeighbourSector, edgeNeighbourSector, listGate);
        }
    }
Пример #6
0
        public ActionResult SectorEdit(int ShopID, int ID, int Type)
        {
            ViewBag.ShopID = ShopID;
            ViewBag.Type   = Type;
            var sector = DB.MapSectors.FirstOrDefault(x => x.ID == ID);

            if (sector == null)
            {
                sector = new MapSector()
                {
                    ShopID = ShopID, Type = Type
                }
            }
            ;
            ViewBag.StoreList =
                DB.Stores.Where(x => x.ShopStores.Any(z => z.ShopID == ShopID)).ToList()
                .Select(
                    x =>
                    new SelectListItem()
            {
                Text     = x.Name,
                Value    = x.ID.ToString(),
                Selected = sector.StoreID == x.ID
            }).AsEnumerable();
            return(PartialView(sector));
        }
Пример #7
0
    /// <summary>
    /// 上下级的2个扇区,某条边是否匹配
    /// </summary>
    /// <param name="highSector">高一级的扇区</param>
    /// <param name="lowSector">低一级的扇区</param>
    /// <param name="edge">指定某条边</param>
    /// <returns></returns>
    public static bool LowerSectorEdgeMatchesHigher(MapSector highSector, MapSector lowSector, Side edge)
    {
        if (highSector == null || lowSector == null)
        {
            return(false);
        }
        if (edge == Side.Top && highSector.top == lowSector.top)
        {
            return(true);
        }
        if (edge == Side.Down && highSector.bottom == lowSector.bottom)
        {
            return(true);
        }
        if (edge == Side.Left && highSector.left == lowSector.left)
        {
            return(true);
        }
        if (edge == Side.Right && highSector.right == lowSector.right)
        {
            return(true);
        }

        return(false);
    }
Пример #8
0
    /// <summary>
    /// 获取受到影响下一级高级相领扇区
    /// </summary>
    /// <param name="list"></param>
    /// <param name="curLev"></param>
    /// <param name="LevelNum"></param>
    /// <returns></returns>
    public List <NeighbourSector> GetParentNeighbourSectorList(List <NeighbourSector> list, int curLev)
    {
        // 列表为空或已经是最大等级了
        if (list == null || list.Count == 0 || curLev >= SectorLevelNum - 1)
        {
            return(null);
        }
        List <NeighbourSector> l = new List <NeighbourSector>();

        foreach (NeighbourSector mc in list)
        {
            if (mc.A != null && mc.B != null && mc.A.level == curLev && mc.B.level == curLev)
            {
                MapSector high1 = GetParentMapSector(mc.A);
                MapSector high2 = GetParentMapSector(mc.B);
                if (high1 != high2)
                {
                    NeighbourSector next = new NeighbourSector(high1, high2, mc.side);
                    if (l.Contains(next) == false)
                    {
                        l.Add(next);
                    }
                }
            }
        }
        return(l);
    }
Пример #9
0
    /// <summary>
    /// 在指定的扇区层级Wap point数据中删除指定的way point点,一个way point可能又多个属性,
    /// </summary>
    /// <param name="sectorLevel">扇区层级</param>
    /// <param name="GateNode">扇区出入口节点</param>
    public void RemoveWayPointNode(int sectorLevel, PathNode WayPointNode)
    {
        Dictionary <MapTile, PathNode> WayPointGraph = GetSectorLevelWayPointLink(sectorLevel);

        if (WayPointGraph == null || WayPointNode == null || WayPointNode.tileConnection == null)
        {
            return;
        }
        //清理连接关系。
        foreach (PathNode n in WayPointNode.connections.Keys)
        {
            if (n != null && n.connections != null)
            {
                if (n.connections.ContainsKey(WayPointNode) == true)
                {
                    n.connections.Remove(WayPointNode);
                }
            }
        }
        WayPointNode.connections.Clear();
        WayPointNode.connections = null;
        //
        MapTile tile = WayPointNode.tileConnection;

        if (WayPointGraph.ContainsKey(tile) == true)
        {
            WayPointGraph.Remove(tile);
            tile.hasPathNodeConnection = false;
            MapSector s = FindMapSector(sectorLevel, WayPointNode.sector);
            if (s != null)
            {
                s.RemovePathNodeNum();
            }
        }
    }
Пример #10
0
    /// <summary>
    /// 构建扇区某条边的gate
    /// </summary>
    /// <param name="map"></param>
    public override void BuildOneEdgeGate(Side edge, Map map)
    {
        // 获取邻居扇区
        Side      neighbourEdge = Util.FlipDirection(edge);
        MapSector ms            = Findneighbour(edge, map);

        if (ms == null)
        {
            return;
        }
        if (edge == Side.Top)
        {
            SectorHelp.RebuildNodesOnSectorEdge(this, edge, new IntVector2(this.left, this.top),
                                                ms, neighbourEdge, new IntVector2(this.left, this.top - 1),
                                                IntVector2.right);
        }
        else if (edge == Side.Down)
        {
            SectorHelp.RebuildNodesOnSectorEdge(this, edge, new IntVector2(this.left, this.bottom),
                                                ms, neighbourEdge, new IntVector2(this.left, this.bottom + 1),
                                                IntVector2.right);
        }
        else if (edge == Side.Left)
        {
            SectorHelp.RebuildNodesOnSectorEdge(this, edge, new IntVector2(this.left, this.top),
                                                ms, neighbourEdge, new IntVector2(this.left - 1, this.top),
                                                IntVector2.down);
        }
        else if (edge == Side.Right)
        {
            SectorHelp.RebuildNodesOnSectorEdge(this, edge, new IntVector2(this.right, this.top),
                                                ms, neighbourEdge, new IntVector2(this.right + 1, this.top),
                                                IntVector2.down);
        }
    }
Пример #11
0
 /// <summary>
 /// 处理低级扇区,并返回高级扇区
 /// </summary>
 private static void ProcessLowSector()
 {
     //处理low 扇区的边
     g_sectorHighChanges.Clear();
     foreach (LowSector ms in g_sectorEdgeChangesLowLevel.Keys)
     {
         ms.BuildEdgeGate(g_sectorEdgeChangesLowLevel[ms], g_map);
         ms.RemoveAllWapPointCostInSector();
         if (!g_sectorChanges.Contains(ms))
         {
             g_sectorChanges.Add(ms);
         }
     }
     // 处理low 扇区的cost
     foreach (LowSector ms in g_sectorChanges)
     {
         ms.BuildSectorWayPointCost(g_map);
         MapSector highSector = g_map.GetParentMapSector(ms);
         if (highSector != null)
         {
             if (!g_sectorHighChanges.Contains(highSector))
             {
                 g_sectorHighChanges.Add(highSector);
             }
         }
     }
 }
Пример #12
0
        public Sector(int index, MapSector mapSector, SectorPlane floor, SectorPlane ceiling)
        {
            Index   = index;
            Floor   = floor;
            Ceiling = ceiling;
            SetLightLevel(mapSector.LightLevel);

            floor.Sector   = this;
            ceiling.Sector = this;
        }
Пример #13
0
 /// <summary>
 /// 获取指定扇区的子扇区
 /// </summary>
 /// <param name="curSector"></param>
 /// <returns></returns>
 public List <ushort> GetChildSectorindex(MapSector curSector)
 {
     if (curSector == null || curSector.level == 0)
     {
         return(null);
     }
     else
     {
         return(GetChildSectorindex(curSector.level, curSector.ID));
     }
 }
Пример #14
0
    /// <summary>
    /// 获取父扇区
    /// </summary>
    /// <param name="sector"></param>
    /// <returns></returns>
    public MapSector GetParentMapSector(MapSector sector)
    {
        if (sector == null && sector.level >= SectorLevelNum - 1)
        {
            return(null);
        }
        int x = Mathf.FloorToInt(sector.left / (float)levelDimensions[sector.level + 1].sectorWidth);
        int y = Mathf.FloorToInt(sector.top / (float)levelDimensions[sector.level + 1].sectorHeight);

        return(FindMapSector(sector.level + 1, new IntVector2((short)x, (short)y)));
    }
Пример #15
0
    public static void CreateGate(MapSector sector, Side edge, MapTile tile, MapSector neighbourSector, Side neighbourSectoredge, MapTile neighbourTile)
    {
        if (sector == null || neighbourSector == null || tile == null || neighbourTile == null)
        {
            return;
        }
        PathNode node          = sector.CreateWayPointInSector(tile, edge, g_Map);
        PathNode neighbourNode = neighbourSector.CreateWayPointInSector(neighbourTile, neighbourSectoredge, g_Map);

        node.LinkOtherSectorGate          = neighbourNode;
        neighbourNode.LinkOtherSectorGate = node;
        PathNode.LinkSectorNode(node, neighbourNode, 1, null);
    }
Пример #16
0
    private void InstantiateAll()
    {
        MapSectorNames data    = levels[levelIndex];
        int            offsetX = 0;

        for (int i = 0; i < data.sectors.Length; ++i)
        {
            GameObject sectorObj = Instantiate(mapSectorPrefab) as GameObject;
            MapSector  newSector = sectorObj.GetComponent <MapSector>();
            newSector.mapName = levels[levelIndex].sectors[i];
            newSector.offsetX = offsetX;
            newSector.Load();
            offsetX = newSector.right;
        }
    }
Пример #17
0
    /// <summary>
    /// 获取邻居扇区的索引
    /// </summary>
    /// <param name="neighour"></param>
    /// <returns></returns>
    public List <int> FindneighbourMapSectorID(MapSector cur)
    {
        List <int>       lID = new List <int>();
        List <MapSector> l   = FindneighbourMapSector(cur);

        if (l == null || l.Count == 0)
        {
            return(null);
        }
        foreach (MapSector ms in l)
        {
            lID.Add(ms.ID);
        }
        return(lID);
    }
Пример #18
0
    /// <summary>
    /// 创建门
    /// </summary>
    /// <param name="sector">指定的扇区</param>
    /// <param name="edge">sector与neighbourSector相邻的一条边</param>
    /// <param name="neighbourSector">sector 邻居扇区</param>
    /// <param name="neighbourSectoredge">neighbourSector与 sector 相邻的一条边</param>
    /// <param name="listGate">为CalcGate函数的结果,保持的是个门两边的Tile</param>
    private static void CreateGate(MapSector sector, Side edge, MapSector neighbourSector, Side neighbourSectoredge, List <MapTile> listGate)
    {
        if (sector == null || neighbourSector == null || listGate == null || listGate.Count == 0)
        {
            return;
        }
        if (listGate.Count % 2 == 1)
        {
            return;
        }
        int length = listGate.Count / 2;

        for (int i = 0; i < length; i++)
        {
            CreateGate(sector, edge, listGate[2 * i], neighbourSector, neighbourSectoredge, listGate[2 * i + 1]);
        }
    }
Пример #19
0
    /// <summary>
    /// 获取指定扇区的子扇区
    /// </summary>
    /// <param name="curSector"></param>
    /// <returns></returns>
    public List <MapSector> GetChildSector(MapSector curSector)
    {
        List <MapSector> listmc = new List <MapSector>();
        List <ushort>    list   = GetChildSectorindex(curSector.level, curSector.ID);

        if (list == null)
        {
            return(null);
        }
        foreach (ushort index in list)
        {
            MapSector mc = FindMapSector(curSector.level - 1, index);
            if (mc != null)
            {
                listmc.Add(mc);
            }
        }
        return(listmc);
    }
Пример #20
0
        /*private string GetSectorRepresentation(MapSector sector)
         * {
         *  if (sector == null) { throw new ArgumentNullException("sector"); }
         *
         *  if (sector.GamePiece != null)
         *  {
         *      return PawnToken;
         *  }
         *  else
         *  {
         *      return EmptyCellToken;
         *  }
         * }*/

        private string GetSectorRepresentation(MapSector sector, List <MapCoordinates> reachableSectors)
        {
            if (sector == null)
            {
                throw new ArgumentNullException("sector");
            }

            if (sector.GamePiece != null)
            {
                return("" + sector.GamePiece.Id);
            }
            else if (reachableSectors != null && reachableSectors.Contains(sector.Coordinates))
            {
                return(ReachableCellToken);
            }
            else
            {
                return(EmptyCellToken);
            }
        }
Пример #21
0
        private static void ReadSectorsOrThrow(MapData map, MapComponents components)
        {
            ByteReader reader = ByteReader.From(ByteOrder.Little, components.Sectors.Value.Data);

            int count = reader.Length / BytesPerSector;

            for (int index = 0; index < count; index++)
            {
                MapSector sector = new MapSector(index)
                {
                    FloorHeight    = reader.Short(),
                    CeilingHeight  = reader.Short(),
                    FloorTexture   = reader.StringWithoutNulls(8).ToUpper(),
                    CeilingTexture = reader.StringWithoutNulls(8).ToUpper(),
                    LightLevel     = reader.Short()
                };
                SetSpecialBits(sector, reader.UShort());
                sector.Tag = reader.UShort();

                map.Sectors.Add(sector);
            }
        }
Пример #22
0
    /// <summary>
    /// 获取父扇区列比奥
    /// </summary>
    /// <param name="list"></param>
    /// <param name="curLev"></param>
    /// <returns></returns>
    public List <MapSector> GetParentSectorList(List <MapSector> list, int curLev)
    {
        // 列表为空或已经是最大等级了
        if (list == null || list.Count == 0 || curLev >= SectorLevelNum - 1)
        {
            return(null);
        }
        List <MapSector> l = new List <MapSector>();

        foreach (MapSector mc in list)
        {
            if (mc != null && mc.level == curLev)
            {
                MapSector high = GetParentMapSector(mc);
                if (l.Contains(high) == false)
                {
                    l.Add(high);
                }
            }
        }
        return(l);
    }
Пример #23
0
 /// <summary>
 /// 获取邻居扇区
 /// </summary>
 /// <param name="neighour"></param>
 /// <returns></returns>
 public MapSector Findneighbour(MapSector cur, IntVector2 neighour)
 {
     if (cur == null)
     {
         return(null);
     }
     if (neighour == IntVector2.top)
     {
         return(FindMapSector(cur.level, cur.Pos.Top));
     }
     else if (neighour == IntVector2.down)
     {
         return(FindMapSector(cur.level, cur.Pos.Down));
     }
     else if (neighour == IntVector2.left)
     {
         return(FindMapSector(cur.level, cur.Pos.Left));
     }
     else if (neighour == IntVector2.right)
     {
         return(FindMapSector(cur.level, cur.Pos.Right));
     }
     return(null);
 }
Пример #24
0
        public void Move(int oldx, int oldy, int newx, int newy, bool walk, bool stop)
        {
            Position.X = newx;
            Position.Y = newy;
            var movedin = Map.GetSectorByPos(Position);

            if (movedin != MapSector)
            {
                MapSector.Transfer(this, movedin);
            }

            if (stop)
            {
                using (var packet = Handler8.StopObject(this))
                {
                    Map.Broadcast(packet);
                }
            }
            else
            {
                ushort speed = 0;
                if (walk)
                {
                    speed = 60;
                }
                else
                {
                    speed = 115;
                }

                using (var packet = Handler8.MoveObject(this, oldx, oldy, walk, speed))
                {
                    Map.Broadcast(packet);
                }
            }
        }
Пример #25
0
 private static void SetSpecialBits(MapSector sector, ushort bits)
 {
     // TODO
 }
Пример #26
0
        public static void ProcessOrder(StrategyGame game, QuickChatItem cmd, MapSector sector, Ship.ShipEventHandler f_ShipEvent)
        {
            // TODO: Test!
            return;

            if (cmd == null || game == null || cmd.OrderAction == string.Empty)
            {
                return;
            }

            var order      = cmd.OrderAction;
            var team       = 1;
            var t          = team - 1;
            var alliance   = game.GameSettings.TeamAlliance[t];
            var pilotCount = game.DockedPilots[t];

            if (order.StartsWith("Attack"))
            {
                // All friendly combat ships in sector
                var ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.CanAttackShips()).ToList();
                if (ships.Count == 0)
                {
                    return;
                }

                if (order.EndsWith("Base"))
                {
                    var targetBases = game.AllBases.Where(_ => _.Active && _.SectorId != sector.Id && _.Alliance != alliance).ToList();
                    if (targetBases.Count == 0)
                    {
                        return;
                    }

                    var targetBase = StrategyGame.RandomItem(targetBases);
                    ships.ForEach(_ => _.OrderShip(new SurroundOrder(game, sector.Id, targetBase, PointF.Empty)));
                    return;
                }

                // if type in sector, attack it (rnd)
                var targetTypes = GetOrderTypes(order);
                var targets     = game.AllUnits.Where(_ => _.Active && _.IsVisibleToTeam(t) && _.Alliance != alliance && _.SectorId == sector.Id && targetTypes.Contains(_.Type)).ToList();

                var  append = false;
                Ship target = null;

                // if type spotted, navigate to it (rnd)
                if (targets.Count == 0)
                {
                    targets = game.AllUnits.Where(_ => _.Active && _.IsVisibleToTeam(t) && _.Alliance != alliance && targetTypes.Contains(_.Type)).ToList();
                    if (targets.Count == 0)
                    {
                        return;
                    }

                    append = true;
                    target = StrategyGame.RandomItem(targets);
                    ships.ForEach(_ => _.OrderShip(new NavigateOrder(game, _, target.SectorId)));
                }
                if (target == null)
                {
                    target = StrategyGame.RandomItem(targets);
                }

                ships.ForEach(_ => _.OrderShip(new InterceptOrder(game, target, sector.Id, true), append));
            }
            else if (order.StartsWith("Defend"))
            {
                // All friendly combat ships in sector
                var ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.Type != EShipType.Lifepod && _.CanAttackShips()).ToList();
                if (ships.Count == 0)
                {
                    return;
                }

                if (order.EndsWith("Base"))
                {
                    var targetBases = game.AllBases.Where(_ => _.Active && _.SectorId != sector.Id && _.Alliance == alliance).ToList();
                    if (targetBases.Count == 0)
                    {
                        return;
                    }

                    var targetBase = StrategyGame.RandomItem(targetBases);
                    ships.ForEach(_ => _.OrderShip(new SurroundOrder(game, sector.Id, targetBase, PointF.Empty)));
                    return;
                }

                // if type in sector, defend it (rnd)
                var targetTypes = GetOrderTypes(order);
                var targets     = game.AllUnits.Where(_ => _.Active && _.Alliance == alliance && _.SectorId == sector.Id && targetTypes.Contains(_.Type)).ToList();

                if (targets.Count == 0)
                {
                    return;
                }
                var target = StrategyGame.RandomItem(targets);
                ships.ForEach(_ => _.OrderShip(new InterceptOrder(game, target, sector.Id, true)));
            }
            else if (order.StartsWith("Launch"))
            {
                if (pilotCount == 0)
                {
                    return;
                }

                // Get a base in sector (rnd)
                var  bases      = game.AllBases.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.CanLaunchShips()).ToList();
                Base launchBase = null;
                if (bases.Count == 0)
                {
                    // Get a base close to this sector (rnd)
                    launchBase = game.ClosestSectorWithBase(team, sector.Id);
                }
                else
                {
                    launchBase = StrategyGame.RandomItem(bases);
                }
                if (launchBase == null)
                {
                    return;
                }

                // Launch this ship type
                var types = GetOrderTypes(order);
                do
                {
                    var s = LaunchShipType(game, types, launchBase, f_ShipEvent);
                    if (s == null)
                    {
                        break;
                    }
                } while (game.DockedPilots[t] > pilotCount / 2);
            }
            else if (order.StartsWith("Hunt"))
            {
                // Get a base in sector (rnd)
                var  bases      = game.AllBases.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.CanLaunchShips()).ToList();
                Base launchBase = null;

                if (bases.Count == 0)
                {
                    // Get a base close to this sector (rnd)
                    launchBase = game.ClosestSectorWithBase(team, sector.Id);
                }
                else
                {
                    launchBase = StrategyGame.RandomItem(bases);
                }

                var targetTypes = GetOrderTypes(order);
                var targets     = game.AllUnits.Where(_ => _.Active && _.IsVisibleToTeam(team) && _.Alliance != alliance && targetTypes.Contains(_.Type)).ToList();
                var target      = StrategyGame.RandomItem(targets);

                // Launch fighter ship type
                var types = GetOrderTypes("Fighter");
                var ships = new List <CombatShip>();
                do
                {
                    var s = LaunchShipType(game, types, launchBase, f_ShipEvent);
                    if (s == null)
                    {
                        break;
                    }
                    ships.Add(s);
                } while (game.DockedPilots[t] > pilotCount / 2);

                // If type spotted, navigate & attack it (rnd)
                if (target != null)
                {
                    if (launchBase.SectorId != target.SectorId)
                    {
                        ships.ForEach(_ => _.OrderShip(new NavigateOrder(game, _, target.SectorId), true));
                    }

                    ships.ForEach(_ => _.OrderShip(new InterceptOrder(game, target, _.SectorId, true), true));
                }
                else
                {
                    // Otherwise patrol randomly until spotted
                    ships.ForEach(_ => _.OrderShip(new HuntControlOrder(game, targetTypes), true));
                }
            }
            else if (order == "Scout")
            {
                // Launch up to 3 scouts to patrol randomly
                var bases = game.AllBases.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.CanLaunchShips()).ToList();
                if (bases.Count == 0)
                {
                    return;
                }
                var launchBase = StrategyGame.RandomItem(bases);

                var types = GetOrderTypes("Scout");
                var ships = new List <CombatShip>();
                do
                {
                    var s = LaunchShipType(game, types, launchBase, f_ShipEvent);
                    if (s == null)
                    {
                        break;
                    }
                    ships.Add(s);
                } while (game.DockedPilots[t] > 0 && ships.Count < 3);

                ships.ForEach(_ => _.OrderShip(new ScoutControlOrder(game), true));
            }
            else if (order == "Dock")
            {
                // All friendly combat ships in sector
                var ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.Type != EShipType.Lifepod && _.CanAttackShips()).ToList();
                if (ships.Count == 0)
                {
                    return;
                }

                // Dock order
                ships.ForEach(_ => _.OrderShip(new DockOrder(game, _)));
            }
            else if (order == "Pause")
            {
                // All friendly combat ships in sector
                var ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.Type != EShipType.Lifepod && _.CanAttackShips()).ToList();
                if (ships.Count == 0)
                {
                    return;
                }

                // Pause order
            }
            else if (order == "Resume")
            {
                // All friendly combat ships in sector
                var ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.Type != EShipType.Lifepod && _.CanAttackShips()).ToList();
                if (ships.Count == 0)
                {
                    return;
                }

                // Unpause order
            }
        }
Пример #27
0
        public static void ProcessOrder(StrategyGame game, QuickChatItem cmd, MapSector sector, Ship.ShipEventHandler f_ShipEvent)
        {
            if (cmd == null || game == null || cmd.OrderAction == string.Empty) return;

            var targetSectorId = sector.Id;
            var launchSectorId = sector.Id;

            var team = 1;
            var t = 0;
            var alliance = game.GameSettings.TeamAlliance[t];
            var pilotCount = game.DockedPilots[t];
            var order = cmd.OrderAction;
            List<Ship> ships;

            if (order == "Scout")
            {
                var idealNumOfShips = 3;
                var orderTypes = GetOrderTypes(order);

                // Prefer ships already in sector
                ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == launchSectorId && orderTypes.Contains(_.Type)).Take(idealNumOfShips).ToList();

                if (ships.Count < idealNumOfShips && pilotCount > 0)
                {
                    // Launch more scouts if needed
                    var launchBase = game.ClosestSectorWithBase(team, sector.Id);
                    if (launchBase != null)
                    {
                        ships.AddRange(LaunchShips(game, idealNumOfShips - ships.Count, orderTypes, launchBase, f_ShipEvent));
                    }
                }
                if (ships.Count == 0) return;

                ships.ForEach(_ => _.OrderShip(new ScoutControlOrder(game), true));
            }
            else if (order == "Dock")
            {
                // All our combat ships in this sector should dock immediately
                ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.Type != EShipType.Lifepod && _.CanAttackShips() && !Ship.IsCapitalShip(_.Type)).ToList();
                if (ships.Count == 0) return;

                ships.ForEach(_ => _.OrderShip(new DockOrder(game, _)));
            }
            else if (order == "Pause")
            {
                // Interrupt the current order for all our combat ships in this sector
                ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.Type != EShipType.Lifepod && _.CanAttackShips()).ToList();
                if (ships.Count == 0) return;

                ships.ForEach(_ => _.InsertOrder(new PauseControlOrder(game)));
            }
            else if (order == "Resume")
            {
                // Resume the order queue for all our combat ships in this sector
                ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == sector.Id && _.Type != EShipType.Lifepod && _.CanAttackShips()).ToList();
                if (ships.Count == 0) return;

                ships.ForEach(_ => _.InsertOrder(new ResumeControlOrder(game)));
            }
            else if (order.StartsWith("Launch"))
            {
                if (pilotCount < 2) return;
                var orderTypes = GetOrderTypes(order);
                var idealNumOfShips = pilotCount / 2;

                var launchBase = game.ClosestSectorWithBase(team, sector.Id);
                if (launchBase == null) return;
                launchSectorId = launchBase.SectorId;

                ships = LaunchShips(game, idealNumOfShips, orderTypes, launchBase, f_ShipEvent);
                if (ships.Count == 0) return;

                if (sector.Id != launchSectorId)
                {
                    ships.ForEach(_ => _.OrderShip(new NavigateOrder(game, _, sector.Id)));
                    ships.ForEach(_ => _.OrderShip(new MoveOrder(game, launchBase.SectorId, StrategyGame.ScreenCenter, Point.Empty), true));
                }
            }
            else if (order.StartsWith("Defend") || order.StartsWith("Attack") || order.StartsWith("Hunt"))
            {
                var idealNumOfScouts = 2;
                var idealNumOfShips = game.TotalPilots[t] / 2;
                var shipTypes = GetOrderTypes("Fighter");
                var targetTypes = GetOrderTypes(order);

                Base targetBase = null;
                Ship targetShip = null;

                var defend = order.StartsWith("Defend");
                var hunt = order.StartsWith("Hunt");

                if (order.EndsWith("Base"))
                {
                    idealNumOfScouts = 0;

                    // Only bases in this sector
                    var targetBases = game.AllBases.Where(_ => _.Active && _.IsVisibleToTeam(t) && (_.Alliance != alliance || defend) && _.SectorId == sector.Id).ToList();                    
                    if (targetBase == null) return;
                    targetSectorId = targetBase.SectorId;
                }
                else
                { 
                    // Prefer targest in this sector
                    var targetShips = game.AllUnits.Where(_ => _.Active && _.IsVisibleToTeam(t) && (_.Alliance != alliance || defend) && _.SectorId == sector.Id && targetTypes.Contains(_.Type)).ToList();
                    if (targetShips.Count == 0)
                    {
                        targetShips = game.AllUnits.Where(_ => _.Active && _.IsVisibleToTeam(t) && (_.Alliance != alliance || defend) && targetTypes.Contains(_.Type)).ToList();
                    }

                    targetShip = StrategyGame.RandomItem(targetShips);

                    // Abort if we have no targets and we are not hunting
                    if (targetShip == null && !hunt) return;

                    targetSectorId = targetShip != null ? targetShip.SectorId : sector.Id;
                }

                var launchBase = game.ClosestSectorWithBase(team, targetSectorId);
                if (launchBase != null) launchSectorId = launchBase.SectorId;

                // Prefer scouts already in sector
                if (idealNumOfScouts > 0)
                {
                    ships = game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == targetSectorId && _.Type == EShipType.Scout).Take(idealNumOfScouts).ToList();

                    if (ships.Count < idealNumOfScouts && pilotCount > 0)
                    {
                        // Launch more scouts if needed
                        if (launchBase != null)
                        {
                            ships.AddRange(LaunchShips(game, idealNumOfScouts - ships.Count, new[] { EShipType.Scout }, launchBase, f_ShipEvent));
                        }
                    }
                }
                else
                {
                    ships = new List<Ship>();
                }

                // Prefer fighters already in sector
                ships.AddRange(game.AllUnits.Where(_ => _.Active && _.Team == team && _.SectorId == targetSectorId && shipTypes.Contains(_.Type)).Take(idealNumOfShips).ToList());
                if (ships.Count < idealNumOfShips && pilotCount > 0)
                {
                    // Launch more fighters if needed
                    if (launchBase != null)
                    {
                        ships.AddRange(LaunchShips(game, idealNumOfShips - ships.Count, shipTypes, launchBase, f_ShipEvent));
                    }
                }
                if (ships.Count == 0) return;

                // Get to the target's sector, if needed
                ships.ForEach(_ => _.OrderShip(new NavigateOrder(game, _, targetSectorId)));

                if (targetBase != null)
                {
                    ships.ForEach(_ => _.OrderShip(new SurroundOrder(game, targetSectorId, targetBase), true));
                }
                else if (targetShip != null)
                {
                    ships.ForEach(_ => _.OrderShip(new InterceptOrder(game, targetShip, targetSectorId), true));
                }
                else if (hunt)
                { 
                    // Otherwise, patrol randomly to hunt for the target type
                    ships.ForEach(_ => _.OrderShip(new HuntControlOrder(game, targetTypes)));
                }
            }
        }
Пример #28
0
    /// <summary>
    /// 构建地图
    /// </summary>
    private void BuildMap()
    {
        // 构建tile
        tileArray = new MapTile[TileYnum][];
        for (short y = 0; y < TileYnum; y++)
        {
            tileArray[y] = new MapTile[TileXnum];
            for (short x = 0; x < TileXnum; x++)
            {
                MapTile tile = new MapTile();
                tile.Pos = new IntVector2(x, y);
                tile.SetSectorIndex(TileXnum, TileYnum, SectorSize);
                tile.integrationValue = TileHelp.tileResetIntegrationValue;
                tileArray[y][x]       = tile;
            }
        }
        ushort sectorWidth  = SectorSize;
        ushort sectorHeight = SectorSize;

        //
        SectorLevelWayPointLink = new Dictionary <MapTile, PathNode> [SectorLevelNum];
        lookUpLowerSectors      = new SectorIndexList[SectorLevelNum - 1][];
        levelDimensions         = new SectorLevelInfo[SectorLevelNum];
        sectorArray             = new MapSector[SectorLevelNum][];
        for (ushort level = 0; level < SectorLevelNum; level++)
        {
            SectorLevelWayPointLink[level] = new Dictionary <MapTile, PathNode>();
            levelDimensions[level]         = new SectorLevelInfo(sectorWidth, sectorHeight,
                                                                 (ushort)(Mathf.CeilToInt((TileXnum / (float)sectorWidth))), (ushort)(Mathf.CeilToInt((TileYnum / (float)sectorHeight))));

            sectorArray[level] = new MapSector[levelDimensions[level].numWidth * levelDimensions[level].numHeight];
            for (short i = 0; i < levelDimensions[level].numHeight; i++)
            {
                for (short j = 0; j < levelDimensions[level].numWidth; j++)
                {
                    int index = (i * levelDimensions[level].numWidth) + j;
                    if (level == 0)
                    {
                        sectorArray[level][index] = new LowSector();
                    }
                    else
                    {
                        sectorArray[level][index] = new HighSector();
                    }
                    sectorArray[level][index].Pos           = new IntVector2(j, i);
                    sectorArray[level][index].ID            = (ushort)index;
                    sectorArray[level][index].level         = level;
                    sectorArray[level][index].top           = (ushort)(i * levelDimensions[level].sectorWidth);
                    sectorArray[level][index].bottom        = (ushort)(i * levelDimensions[level].sectorWidth + levelDimensions[level].sectorWidth - 1);
                    sectorArray[level][index].left          = (ushort)(j * levelDimensions[level].sectorHeight);
                    sectorArray[level][index].right         = (ushort)(j * levelDimensions[level].sectorHeight + levelDimensions[level].sectorHeight - 1);
                    sectorArray[level][index].tilesInWidth  = (ushort)(Mathf.Min(TileXnum - sectorArray[level][index].left, levelDimensions[level].sectorWidth));
                    sectorArray[level][index].tilesInHeight = (ushort)(Mathf.Min(TileYnum - sectorArray[level][index].top, levelDimensions[level].sectorHeight));
                    sectorArray[level][index].Init();
                }
            }
            // hight level 包含更多的格子数
            sectorWidth  *= SectorlevelScale;
            sectorHeight *= SectorlevelScale;
            if (level > 0)
            {
                lookUpLowerSectors[level - 1] = new SectorIndexList[sectorArray[level].Length];
            }
        }
        FillInLookUpLowerSectors();
    }
Пример #29
0
 /// <summary>
 /// 构建结构体
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <param name="s"></param>
 public NeighbourSector(MapSector a, MapSector b, Side s)
 {
     this.A    = a;
     this.B    = b;
     this.side = s;
 }
Пример #30
0
    /// <summary>
    /// 处理扇区边缘的tile
    /// </summary>
    /// <param name="ms">所在扇区</param>
    /// <param name="side">所在边</param>
    private static void ProcessSideTile(LowSector ms, Side side)
    {
        MapSector neighbourms = g_map.Findneighbour(ms, side);

        if (neighbourms != null)
        {
            if (g_map.SectorLevelNum > 1)
            {
                MapSector high1 = ms.GetParentMapSector(g_map);
                MapSector high2 = neighbourms.GetParentMapSector(g_map);
                // 还是高级扇区的边缘。
                if (high1 != high2)
                {
                    NeighbourSector nb = new NeighbourSector();
                    if (ms.ID < neighbourms.ID)
                    {
                        nb.side = side;
                        nb.A    = high1;
                        nb.B    = high2;
                    }
                    else
                    {
                        nb.side = Util.FlipDirection(side);
                        nb.A    = high2;
                        nb.B    = high1;
                    }

                    if (!g_sectorEdgeChangesHighLevel.Contains(nb))
                    {
                        g_sectorEdgeChangesHighLevel.Add(nb);
                    }
                }
            }
            //
            if (g_sectorEdgeChangesLowLevel.ContainsKey(neighbourms as LowSector))
            {
                if (g_sectorEdgeChangesLowLevel[neighbourms as LowSector].Contains(Util.FlipDirection(side))) // other side already filled in
                {
                    if (!g_sectorChanges.Contains(ms as LowSector))                                           // other sector exist and the side. add our sector for general change
                    {
                        g_sectorChanges.Add(ms as LowSector);
                    }
                }
                else if (!g_sectorEdgeChangesLowLevel[ms as LowSector].Contains(side)) //  other sector exist but not the side. add our sector for Edge change
                {
                    g_sectorEdgeChangesLowLevel[ms as LowSector].Add(side);
                }
            }
            else// other sector not (yet? )added.   add ourselves and other sector for genral change
            {
                if (!g_sectorChanges.Contains(neighbourms as LowSector))
                {
                    g_sectorChanges.Add(neighbourms as LowSector);
                }

                if (!g_sectorEdgeChangesLowLevel[ms].Contains(side))
                {
                    g_sectorEdgeChangesLowLevel[ms].Add(side);
                }
            }
        }
        else if (!g_sectorEdgeChangesLowLevel[ms as LowSector].Contains(side))// other sector does not exist, add ourselves
        {
            g_sectorEdgeChangesLowLevel[ms as LowSector].Add(side);
        }
    }