Ejemplo n.º 1
0
        public void RequestUnit(Command command, UnitType unitType, PlayerUnit favoriteUnit)
        {
            DispatcherRequestUnit dispatcherRequestUnit = new DispatcherRequestUnit();

            dispatcherRequestUnit.UnitType = unitType;
            dispatcherRequestUnit.Command  = command;
            if (favoriteUnit != null)
            {
                dispatcherRequestUnit.FavoriteUnits.Add(favoriteUnit);
            }
            RequestedUnits.Add(dispatcherRequestUnit);
        }
Ejemplo n.º 2
0
        // Handle unit requests, that cannot not be satisfied in the own command area
        public void HandleUnitRequests(Player player, List <PlayerUnit> moveableUnits)
        {
            List <DispatcherRequestUnit> remainingUpgradeRequests  = new List <DispatcherRequestUnit>();
            List <DispatcherRequestUnit> remainingCreationRequests = new List <DispatcherRequestUnit>();

            foreach (DispatcherRequestUnit dispatcherRequest in RequestedUnits)
            {
                if (dispatcherRequest.UnitType.MinEngineLevel == 0)
                {
                    // Does not make sense to produce a unit that cannot move to the destination
                    continue;
                }

                if (dispatcherRequest.FavoriteUnits != null &&
                    dispatcherRequest.FavoriteUnits.Count > 0)
                {
                    remainingUpgradeRequests.Add(dispatcherRequest);
                }
                else
                {
                    remainingCreationRequests.Add(dispatcherRequest);
                }
            }

            // Handle requests randomly but upgrade first and create only if no upgrades needed to
            // prevent that all output positions are locked by unfinished units.

            // Step 1: Upgrade
            while (remainingUpgradeRequests.Count > 0)
            {
                int idx = GameController.Random.Next(remainingUpgradeRequests.Count);
                DispatcherRequestUnit dispatcherRequestUnit = remainingUpgradeRequests[idx];

                bool handled = false;

                foreach (Command command in player.Commands)
                {
                    PlayerUnit playerUnit = dispatcherRequestUnit.FavoriteUnits[0];
                    if (command.UnitsAlreadyInArea != null &&
                        command.UnitsAlreadyInArea.Contains(playerUnit))
                    {
                        //foreach (CommandSource commandSource in command.CommandSources)
                        {
                            foreach (PlayerUnit assembler in command.AssigendPlayerUnits)
                            {
                                if (assembler.PossibleMoves.Count > 0)
                                {
                                    continue;
                                }

                                if (assembler.Unit.Assembler != null)
                                {
                                    if (moveableUnits.Contains(playerUnit) && assembler.Unit.Assembler.CanProduce())
                                    {
                                        List <Move> possibleUpgrademoves = new List <Move>();
                                        assembler.Unit.Assembler.ComputePossibleMoves(possibleUpgrademoves, null, MoveFilter.Upgrade);

                                        foreach (Move move in possibleUpgrademoves)
                                        {
                                            if (Assembler.DoesMoveMinRequest(move, dispatcherRequestUnit.UnitType, playerUnit.Unit))
                                            {
                                                // Take first option or random but dicide here
                                                assembler.PossibleMoves.Add(new PlayerMove(move));
                                                handled = true;
                                                break;
                                            }
                                            //Tile tile = playerUnit.Unit.Owner.Game.Map.GetTile(move.Positions[1]);
                                            //if (player.IsAttached(tile.Unit.UnitId, command))
                                            //    continue;

                                            /*
                                             * if (assembler.Unit.Assembler.HandleRequestUnit(playerUnit, dispatcherRequestUnit, move))
                                             * {
                                             * handled = true;
                                             * break;
                                             * }*/
                                        }
                                    }
                                }
                                if (handled)
                                {
                                    break;
                                }
                            }
                            if (handled)
                            {
                                break;
                            }
                            //}
                            //if (handled)
                            //    break;
                        }
                    }
                }
                remainingUpgradeRequests.Remove(dispatcherRequestUnit);
            }

            //
            if (remainingCreationRequests.Count > 0)
            {
                // Dispatch the remaining requests evenly across all assemblers
                DispatchCreationRequests(player, remainingCreationRequests, moveableUnits);
            }


            // Step 2: Create

            /*
             * while (remainingCreationRequests.Count > 0)
             * {
             *  int idx = GameController.Random.Next(remainingCreationRequests.Count);
             *  DispatcherRequestUnit dispatcherRequestUnit = remainingCreationRequests[idx];
             *
             *  bool handled = false;
             *
             *  foreach (CommandSource commandSource in dispatcherRequestUnit.Command.CommandSources)
             *  {
             *      foreach (PlayerUnit playerUnit in commandSource.Parent.UnitsAlreadyInArea)
             *      {
             *          if (playerUnit.PossibleMoves.Count > 0)
             *              continue;
             *
             *          Unit cntrlUnit = playerUnit.Unit;
             *          if (cntrlUnit.Assembler != null)
             *          {
             *              if (moveableUnits.Contains(playerUnit) && cntrlUnit.Assembler.CanProduce)
             *              {
             *                  List<Move> possibleAssemblemoves = new List<Move>();
             *                  cntrlUnit.Assembler.ComputePossibleMoves(possibleAssemblemoves, null, MoveFilter.Assemble);
             *
             *                  // Make new unit.
             *                  if (dispatcherRequestUnit.FavoriteUnits.Count == 0)
             *                  {
             *                      List<Position> preferedPosition = new List<Position>();
             *
             *                      if (dispatcherRequestUnit.UnitType.MinContainerLevel > 0)
             *                      {
             *                          // Build only next to another container if possible
             *                          foreach (PlayerUnit possibleContainer in commandSource.Parent.AssigendPlayerUnits)
             *                          {
             *                              if (possibleContainer.Unit.Container != null)
             *                              {
             *                                  Tile t = GameController.GetTile(possibleContainer.Unit.Pos);
             *                                  foreach (Tile n in t.Neighbors)
             *                                  {
             *                                      if (n.Unit == null)
             *                                          preferedPosition.Add(n.Pos);
             *                                  }
             *                              }
             *                          }
             *                      }
             *
             *                      if (preferedPosition.Count > 0)
             *                      {
             *                          // Build only at prefered position
             *                          foreach (Move move in possibleAssemblemoves)
             *                          {
             *                              if (preferedPosition.Contains(move.Positions[1]))
             *                              {
             *                                  if (Assembler.DoesMoveMinRequest(move, dispatcherRequestUnit.UnitType, null))
             *                                  {
             *                                      PlayerMove playerMove = new PlayerMove(move);
             *                                      playerMove.Command = dispatcherRequestUnit.Command;
             *                                      playerMove.NewUnitId = move.UnitId;
             *
             *                                      playerUnit.PossibleMoves.Add(playerMove);
             *                                      handled = true;
             *                                      break;
             *                                  }
             *                              }
             *                          }
             *                      }
             *                      else
             *                      {
             *
             *                          foreach (Move move in possibleAssemblemoves)
             *                          {
             *                              //if (IsOccupied(player, moves, move.Positions[move.Positions.Count - 1]))
             *                              //    continue;
             *
             *                              if (Assembler.DoesMoveMinRequest(move, dispatcherRequestUnit.UnitType, null))
             *                              {
             *                                  PlayerMove playerMove = new PlayerMove(move);
             *                                  playerMove.Command = dispatcherRequestUnit.Command;
             *                                  playerMove.NewUnitId = move.UnitId;
             *
             *                                  playerUnit.PossibleMoves.Add(playerMove);
             *                                  handled = true;
             *                                  break;
             *                              }
             *                          }
             *                      }
             *                  }
             *              }
             *          }
             *          if (handled)
             *              break;
             *      }
             *      if (handled)
             *          break;
             *  }
             *  remainingCreationRequests.Remove(dispatcherRequestUnit);
             * }*/
        }
Ejemplo n.º 3
0
        public bool CreateRequestedUnit(PlayerUnit assembler, DispatcherRequestUnit dispatcherRequestUnit, Command command)
        {
            List <Move> possibleAssemblemoves = new List <Move>();

            assembler.Unit.Assembler.ComputePossibleMoves(possibleAssemblemoves, null, MoveFilter.Assemble);

            // Make new unit.
            if (dispatcherRequestUnit.FavoriteUnits.Count == 0)
            {
                List <Position> preferedPosition = new List <Position>();

                if (dispatcherRequestUnit.UnitType.MinContainerLevel > 0)
                {
                    // Build only next to another container if possible
                    foreach (PlayerUnit possibleContainer in command.AssigendPlayerUnits)
                    {
                        if (possibleContainer.Unit.Container != null)
                        {
                            Tile t = GameController.GetTile(possibleContainer.Unit.Pos);
                            foreach (Tile n in t.Neighbors)
                            {
                                if (n.Unit == null)
                                {
                                    preferedPosition.Add(n.Pos);
                                }
                            }
                        }
                    }
                }

                if (preferedPosition.Count > 0)
                {
                    // Build only at prefered position
                    foreach (Move move in possibleAssemblemoves)
                    {
                        if (preferedPosition.Contains(move.Positions[1]))
                        {
                            if (Assembler.DoesMoveMinRequest(move, dispatcherRequestUnit.UnitType, null))
                            {
                                PlayerMove playerMove = new PlayerMove(move);
                                playerMove.Command   = dispatcherRequestUnit.Command;
                                playerMove.NewUnitId = move.UnitId;

                                assembler.PossibleMoves.Add(playerMove);

                                return(true);
                            }
                        }
                    }
                }
                else
                {
                    foreach (Move move in possibleAssemblemoves)
                    {
                        //if (IsOccupied(player, moves, move.Positions[move.Positions.Count - 1]))
                        //    continue;

                        if (Assembler.DoesMoveMinRequest(move, dispatcherRequestUnit.UnitType, null))
                        {
                            PlayerMove playerMove = new PlayerMove(move);
                            playerMove.Command   = dispatcherRequestUnit.Command;
                            playerMove.NewUnitId = move.UnitId;

                            assembler.PossibleMoves.Add(playerMove);

                            return(true);
                        }
                    }
                }
            }
            return(false);
        }