Ejemplo n.º 1
0
        // returns true if a special command is being used
        protected override bool DoSpecialCommands(UnitCommand command, GameTime gameTime)
        {
            BuildStructureCommand buildStructureCommand = command as BuildStructureCommand;

            if (buildStructureCommand != null)
            {
                moveToBuildLocation(buildStructureCommand, gameTime);
                return(true);
            }

            HarvestCommand harvestCommand = command as HarvestCommand;

            if (harvestCommand != null)
            {
                moveToHarvestLocation(harvestCommand, gameTime);
                return(true);
            }

            ReturnCargoCommand returnCargoCommand = command as ReturnCargoCommand;

            if (returnCargoCommand != null)
            {
                moveToReturnCargo(returnCargoCommand, gameTime);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        bool buildStructure(BuildStructureCommand command)
        {
            bool allowBuild = Rts.pathFinder.Tools.CanStructureBePlaced(command.StructureLocation, command.StructureType.Size, this, command.StructureType.CutCorners);

            if (allowBuild)
            {
                //if (command.StructureType == StructureType.Barracks)
                //    new Barracks(command.StructureLocation, this, Team);

                Structure structure;

                if (command.StructureType == StructureType.TownHall)
                {
                    structure = new TownHall(command.StructureLocation, this, Team);
                }
                else
                {
                    structure = new Structure(command.StructureType, command.StructureLocation, this, Team);
                }

                CenterPoint = structure.CenterPoint;
                //IgnoringCollision = true;
                Targetable = false;
                Busy       = true;
                Building   = true;
                Unit.RemoveUnit(this);
            }

            return(allowBuild);
        }
Ejemplo n.º 3
0
        void processUnitBuildCommand(NetIncomingMessage msg)
        {
            float scheduledTime = msg.ReadFloat();
            short team          = msg.ReadInt16();

            Unit unit = Player.Players[team].UnitArray[msg.ReadInt16()];

            StructureType structureType = StructureType.StructureTypes[msg.ReadInt16()];
            Point         location      = new Point(msg.ReadInt16(), msg.ReadInt16());
            bool          queued        = msg.ReadBoolean();

            BuildStructureCommand buildStructureCommand = new BuildStructureCommand(unit, structureType, location, new Vector2(location.X * map.TileSize + structureType.Size * map.TileSize / 2, location.Y * map.TileSize + structureType.Size * map.TileSize / 2));

            Player.Players[team].ScheduledActions.Add(new ScheduledUnitBuildCommand(scheduledTime, buildStructureCommand, queued));
            //Rts.pathFinder.AddPathFindRequest(buildStructureCommand, queued, false, false);
        }
Ejemplo n.º 4
0
        void updatePlacedStructures()
        {
            placedStructures.Clear();

            foreach (Unit unit in Unit.Units)
            {
                WorkerNublet worker = unit as WorkerNublet;
                if (worker != null)
                {
                    if (worker.Commands.Count > 0)
                    {
                        foreach (UnitCommand command in worker.Commands)
                        {
                            BuildStructureCommand buildCommand = command as BuildStructureCommand;
                            if (buildCommand != null)
                            {
                                //placedStructures.Add(new ObjectLink<StructureType, Rectangle>(buildCommand.StructureType, new Rectangle((int)(buildCommand.Destination.X - buildCommand.StructureType.Size * map.TileSize / 2), (int)(buildCommand.Destination.Y - buildCommand.StructureType.Size * map.TileSize / 2), buildCommand.StructureType.Size * map.TileSize, buildCommand.StructureType.Size * map.TileSize)));
                                placedStructures.Add(new PlacedStructure(buildCommand.StructureType, new Rectangle(buildCommand.StructureLocation.X * map.TileSize, buildCommand.StructureLocation.Y * map.TileSize, buildCommand.StructureType.Size * map.TileSize, buildCommand.StructureType.Size * map.TileSize), worker.Team));
                            }
                        }
                    }
                }
            }

            foreach (Structure structure in Structure.Structures)
            {
                if (structure.Builder != null)
                {
                    WorkerNublet worker = structure.Builder as WorkerNublet;
                    if (worker != null)
                    {
                        if (worker.Commands.Count > 0)
                        {
                            foreach (UnitCommand command in worker.Commands)
                            {
                                BuildStructureCommand buildCommand = command as BuildStructureCommand;
                                if (buildCommand != null)
                                {
                                    //placedStructures.Add(new ObjectLink<StructureType, Rectangle>(buildCommand.StructureType, new Rectangle((int)(buildCommand.Destination.X - buildCommand.StructureType.Size * map.TileSize / 2), (int)(buildCommand.Destination.Y - buildCommand.StructureType.Size * map.TileSize / 2), buildCommand.StructureType.Size * map.TileSize, buildCommand.StructureType.Size * map.TileSize)));
                                    placedStructures.Add(new PlacedStructure(buildCommand.StructureType, new Rectangle(buildCommand.StructureLocation.X * map.TileSize, buildCommand.StructureLocation.Y * map.TileSize, buildCommand.StructureType.Size * map.TileSize, buildCommand.StructureType.Size * map.TileSize), worker.Team));
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        void giveBuildCommand()
        {
            if (Player.Me.Roks < placingStructureType.RoksCost)
            {
                playErrorSound();
                return;
            }

            List<WorkerNublet> workers = new List<WorkerNublet>();

            foreach (RtsObject o in SelectedUnits)
            {
                WorkerNublet worker = o as WorkerNublet;
                if (worker != null && worker.Team == Player.Me.Team)
                    workers.Add(worker);
            }

            if (workers.Count == 0)
                return;

            // sort workers by distance to build location
            for (int i = 1; i < workers.Count; i++)
            {
                for (int j = i; j >= 1 && Vector2.DistanceSquared(workers[j].CenterPoint, placingStructureCenterPoint) < Vector2.DistanceSquared(workers[j - 1].CenterPoint, placingStructureCenterPoint); j--)
                {
                    WorkerNublet tempItem = workers[j];
                    workers.RemoveAt(j);
                    workers.Insert(j - 1, tempItem);
                }
            }

            /*foreach (WorkerNublet worker in workers)
            {
                if (worker.Commands.Count == 0 || !(worker.Commands[0] is BuildStructureCommand))
                {
                    if (!usingShift)
                        worker.GiveCommand(new BuildStructureCommand(placingStructureType, placingStructureLocation, placingStructureCenterPoint, 1));
                    else
                        worker.QueueCommand(new BuildStructureCommand(placingStructureType, placingStructureLocation, placingStructureCenterPoint, 1));
                    return;
                }
            }*/

            WorkerNublet workerWithSmallestQueue = null;
            int smallest = int.MaxValue;

            foreach (WorkerNublet worker in workers)
            {
                //int count = worker.Commands.Count;
                int count = 0;
                foreach (UnitCommand command in worker.Commands)
                {
                    if (command is BuildStructureCommand)
                        count++;
                }
                if (count < smallest)
                {
                    workerWithSmallestQueue = worker;
                    smallest = count;
                }
            }

            //placingStructureCenterPoint.X = placingStructureLocation.X * map.TileSize + (placingStructureType.Size * map.TileSize) / 2;
            //placingStructureCenterPoint.Y = placingStructureLocation.Y * map.TileSize + (placingStructureType.Size * map.TileSize) / 2;

            /*if (!usingShift)
            //workers[0].GiveCommand(new BuildStructureCommand(placingStructureType, placingStructureLocation, placingStructureCenterPoint, 1));
            {
                if (!workerWithSmallestQueue.Busy)
                {
                    workerWithSmallestQueue.GiveCommand(new BuildStructureCommand(workerWithSmallestQueue, placingStructureType, placingStructureLocation, placingStructureCenterPoint, 1));
                    Player.Me.Roks -= placingStructureType.RoksCost;
                }
            }
            else
            {
                //workers[0].QueueCommand(new BuildStructureCommand(placingStructureType, placingStructureLocation, placingStructureCenterPoint, 1));
                workerWithSmallestQueue.QueueCommand(new BuildStructureCommand(workerWithSmallestQueue, placingStructureType, placingStructureLocation, placingStructureCenterPoint, 1));
                Player.Me.Roks -= placingStructureType.RoksCost;
            }*/
            BuildStructureCommand buildStructureCommand = new BuildStructureCommand(workerWithSmallestQueue, placingStructureType, placingStructureLocation, placingStructureCenterPoint);
            ScheduledUnitBuildCommand scheduledUnitBuildCommand = new ScheduledUnitBuildCommand(currentScheduleTime, buildStructureCommand, usingShift);
            Player.Players[workerWithSmallestQueue.Team].ScheduledActions.Add(scheduledUnitBuildCommand);
            Player.Players[workerWithSmallestQueue.Team].Roks -= placingStructureType.RoksCost;

            Rts.pathFinder.AddPathFindRequest(buildStructureCommand, usingShift, false, false);

            NetOutgoingMessage msg = netPeer.CreateMessage();

            msg.Write(MessageID.UNIT_BUILD_COMMAND);
            msg.Write(currentScheduleTime);
            msg.Write(workerWithSmallestQueue.Team);

            msg.Write(workerWithSmallestQueue.ID);
            msg.Write(placingStructureType.ID);
            msg.Write((short)placingStructureLocation.X);
            msg.Write((short)placingStructureLocation.Y);
            msg.Write(usingShift);

            netPeer.SendMessage(msg, connection, NetDeliveryMethod.ReliableOrdered);

            /*foreach (RtsObject o in SelectedUnits)
            {
                WorkerNublet worker = o as WorkerNublet;
                if (worker != null)
                {
                    worker.GiveCommand(new BuildStructureCommand(placingStructureType, placingStructureLocation, placingStructureCenterPoint, 1));
                    break;
                }
            }*/
        }
Ejemplo n.º 6
0
        void processUnitBuildCommand(NetIncomingMessage msg)
        {
            float scheduledTime = msg.ReadFloat();
            short team = msg.ReadInt16();

            Unit unit = Player.Players[team].UnitArray[msg.ReadInt16()];

            StructureType structureType = StructureType.StructureTypes[msg.ReadInt16()];
            Point location = new Point(msg.ReadInt16(), msg.ReadInt16());
            bool queued = msg.ReadBoolean();

            BuildStructureCommand buildStructureCommand = new BuildStructureCommand(unit, structureType, location, new Vector2(location.X * map.TileSize + structureType.Size * map.TileSize / 2, location.Y * map.TileSize + structureType.Size * map.TileSize / 2));
            Player.Players[team].ScheduledActions.Add(new ScheduledUnitBuildCommand(scheduledTime, buildStructureCommand, queued));
            //Rts.pathFinder.AddPathFindRequest(buildStructureCommand, queued, false, false);
        }
Ejemplo n.º 7
0
 public ScheduledUnitBuildCommand(float scheduledTime, BuildStructureCommand command, bool queued)
     : base(scheduledTime, command, queued)
 {
     Location = command.StructureLocation;
     Type = command.StructureType;
 }
Ejemplo n.º 8
0
        void moveToBuildLocation(BuildStructureCommand command, GameTime gameTime)
        {
            if (!Rts.pathFinder.Tools.WillStructureFit(command.StructureLocation, command.StructureType.Size, command.StructureType.CutCorners))
            {
                NextCommand();
                return;
            }

            checkForSmoothPath(command, gameTime);

            clearPushStatus();
            clearHitWallStatus();

            //if (command.WayPoints.Count == 0)
            //    command.WayPoints.Add(centerPoint);

            Vector2 wayPoint = command.WayPoints[0];

            //float moveX = Util.ScaleWithGameTime(speed.X, gameTime);
            //float moveY = Util.ScaleWithGameTime(speed.Y, gameTime);
            Speed = MathHelper.Min(Speed + acceleration, MaxSpeed);
            float moveX = Util.ScaleWithGameTime(Speed, gameTime);
            float moveY = moveX;

            if (command.WayPoints.Count > 1)
            {
                if (Contains(wayPoint))
                {
                    lastWayPoint = wayPoint;
                    //if (command.WayPoints.Count > 2)
                    command.NextWayPoint(this, Rts.pathFinder);
                    return;
                }
            }
            else
            {
                Vector2 difference = wayPoint - centerPoint;
                if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY)
                {
                    this.CenterPoint = wayPoint;
                    HasMoved         = true;

                    lastWayPoint = wayPoint;

                    //nextCommand();
                    if (!buildStructure(command))
                    {
                        Player.Players[Team].Roks += command.StructureType.RoksCost;
                        //NextCommand();
                    }

                    return;
                }
            }

            float angle = (float)Math.Atan2(wayPoint.Y - CenterPoint.Y, wayPoint.X - CenterPoint.X);

            moveX *= (float)Math.Cos(angle);
            moveY *= (float)Math.Sin(angle);

            lastMove.X       = moveX;
            lastMove.Y       = moveY;
            PrecisePosition += lastMove;
            HasMoved         = true;

            checkForWallHit(command);
            if (checkForPush(command))
            {
                return;
            }

            if (!turnTowards(wayPoint, 120 / Radius, gameTime))
            {
                Speed = MathHelper.Max(Speed - Util.ScaleWithGameTime(acceleration, gameTime), 0);
            }
        }
Ejemplo n.º 9
0
 public ScheduledUnitBuildCommand(float scheduledTime, BuildStructureCommand command, bool queued)
     : base(scheduledTime, command, queued)
 {
     Location = command.StructureLocation;
     Type     = command.StructureType;
 }
Ejemplo n.º 10
0
        void moveToBuildLocation(BuildStructureCommand command, GameTime gameTime)
        {
            if (!Rts.pathFinder.Tools.WillStructureFit(command.StructureLocation, command.StructureType.Size, command.StructureType.CutCorners))
            {
                NextCommand();
                return;
            }

            checkForSmoothPath(command, gameTime);

            clearPushStatus();
            clearHitWallStatus();

            //if (command.WayPoints.Count == 0)
            //    command.WayPoints.Add(centerPoint);

            Vector2 wayPoint = command.WayPoints[0];

            //float moveX = Util.ScaleWithGameTime(speed.X, gameTime);
            //float moveY = Util.ScaleWithGameTime(speed.Y, gameTime);
            Speed = MathHelper.Min(Speed + acceleration, MaxSpeed);
            float moveX = Util.ScaleWithGameTime(Speed, gameTime);
            float moveY = moveX;

            if (command.WayPoints.Count > 1)
            {
                if (Contains(wayPoint))
                {
                    lastWayPoint = wayPoint;
                    //if (command.WayPoints.Count > 2)
                        command.NextWayPoint(this, Rts.pathFinder);
                    return;
                }
            }
            else
            {
                Vector2 difference = wayPoint - centerPoint;
                if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY)
                {
                    this.CenterPoint = wayPoint;
                    HasMoved = true;

                    lastWayPoint = wayPoint;

                    //nextCommand();
                    if (!buildStructure(command))
                    {
                        Player.Players[Team].Roks += command.StructureType.RoksCost;
                        //NextCommand();
                    }

                    return;
                }
            }

            float angle = (float)Math.Atan2(wayPoint.Y - CenterPoint.Y, wayPoint.X - CenterPoint.X);
            moveX *= (float)Math.Cos(angle);
            moveY *= (float)Math.Sin(angle);

            lastMove.X = moveX;
            lastMove.Y = moveY;
            PrecisePosition += lastMove;
            HasMoved = true;

            checkForWallHit(command);
            if (checkForPush(command))
                return;

            if (!turnTowards(wayPoint, 120 / Radius, gameTime))
            {
                Speed = MathHelper.Max(Speed - Util.ScaleWithGameTime(acceleration, gameTime), 0);
            }
        }
Ejemplo n.º 11
0
        bool buildStructure(BuildStructureCommand command)
        {
            bool allowBuild = Rts.pathFinder.Tools.CanStructureBePlaced(command.StructureLocation, command.StructureType.Size, this, command.StructureType.CutCorners);

            if (allowBuild)
            {
                //if (command.StructureType == StructureType.Barracks)
                //    new Barracks(command.StructureLocation, this, Team);

                Structure structure;

                if (command.StructureType == StructureType.TownHall)
                    structure = new TownHall(command.StructureLocation, this, Team);
                else
                    structure = new Structure(command.StructureType, command.StructureLocation, this, Team);

                CenterPoint = structure.CenterPoint;
                //IgnoringCollision = true;
                Targetable = false;
                Busy = true;
                Building = true;
                Unit.RemoveUnit(this);
            }

            return allowBuild;
        }