//public Object WayPointsLock = new Object(); public PathFindRequest(MoveCommand command, PathNode startNode, int priority, bool queued, bool avoidUnits) { Command = command; StartNode = startNode; AvoidUnits = avoidUnits; Priority = priority; Queued = queued; AttackCommand attackCommand = command as AttackCommand; if (attackCommand != null) { Target = attackCommand.Target; } HarvestCommand harvestCommand = command as HarvestCommand; if (harvestCommand != null) { Target = harvestCommand.TargetResource; } ReturnCargoCommand returnCargoCommand = command as ReturnCargoCommand; if (returnCargoCommand != null) { Target = returnCargoCommand.TargetStructure; } }
// 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); }
void returnCargo(ReturnCargoCommand command) { if (CargoType == ResourceType.MineResource) { Player.Players[Team].Roks += CargoAmount; Player.Players[Team].Stats.RoksCounter += CargoAmount; } //if (Commands.Count > 1) //{ NextCommand(); if (Commands.Count == 0) { HarvestCommand harvestCommand = null; if (command.Source != null && !command.Source.Depleted) { harvestCommand = new HarvestCommand(this, command.Source); } else { Resource nearestResource = findNearestResource(CargoType); if (nearestResource != null && !nearestResource.Depleted) { harvestCommand = new HarvestCommand(this, nearestResource); } } if (harvestCommand != null) { GiveCommand(harvestCommand); Rts.pathFinder.AddPathFindRequest(harvestCommand, false, false, false); } } CargoAmount = 0; CargoType = null; //} /*else if (command.Source == null || command.Source.Depleted) * { * Resource nearestResource = findNearestResource(CargoType); * if (nearestResource != null) * GiveCommand(new HarvestCommand(nearestResource, 1)); * else * NextCommand(); * } * else * GiveCommand(new HarvestCommand(command.Source, 1));*/ }
void moveToHarvestLocation(HarvestCommand command, GameTime gameTime) { checkForSmoothPath(command, gameTime); clearPushStatus(); clearHitWallStatus(); 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; command.NextWayPoint(this, Rts.pathFinder); return; } } else { Vector2 difference = wayPoint - centerPoint; if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY) //Resource resource = command.TargetResource; //if (Vector2.Distance(CenterPoint, resource.CenterPoint) < Radius + resource.Radius) { this.CenterPoint = wayPoint; HasMoved = true; lastWayPoint = wayPoint; //if (harvest(command.TargetResource)) //nextCommand(); Resource targetResource = command.TargetResource; if (targetResource == null || targetResource.Depleted) { Resource nearestResource = findNearestResource(targetResource.Type); if (nearestResource != null) { GiveCommand(new HarvestCommand(this, nearestResource)); } else { NextCommand(); } } else { harvest(command.TargetResource); } 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; /*Resource resource = command.TargetResource; * if (Vector2.Distance(CenterPoint, resource.CenterPoint) <= Radius + resource.Radius) * { * //if (harvest(command.TargetResource)) * // nextCommand(); * harvest(command.TargetResource); * }*/ if (checkForWallHit(command)) { //if (harvest(command.TargetResource)) // nextCommand(); /*if (CargoAmount == MAX_CARGO_AMOUNT && command.TargetResource.Type == CargoType) * { * TownHall townHall = findNearestTownHall(); * if (townHall != null) * GiveCommand(new ReturnCargoCommand(townHall, command.TargetResource, 1)); * else * nextCommand(); * } * else * {*/ Resource targetResource = command.TargetResource; if (targetResource == null || targetResource.Depleted) { Resource nearestResource = findNearestResource(targetResource.Type); if (nearestResource != null) { GiveCommand(new HarvestCommand(this, nearestResource)); } else { NextCommand(); } } else { harvest(command.TargetResource); } //} } //if (checkForPush(command)) // return; if (!turnTowards(wayPoint, 120 / Radius, gameTime)) { Speed = MathHelper.Max(Speed - Util.ScaleWithGameTime(acceleration, gameTime), 0); } }
void completeBuildQueueItem(BuildQueueItem item) { BuildUnitButtonType buttonType = item.Type as BuildUnitButtonType; if (buttonType != null) { Unit unit; if (buttonType.UnitType == UnitType.MeleeNublet) { unit = new MeleeNublet(new Vector2(), Team, item.ID); } else if (buttonType.UnitType == UnitType.RangedNublet) { unit = new RangedNublet(new Vector2(), Team, item.ID); } else { unit = new WorkerNublet(new Vector2(), Team, item.ID); } float angle = 0; Vector2 spawnLocation; if (rallyPoints.Count > 0) { angle = (float)Math.Atan2(rallyPoints[0].Point.Y - CenterPoint.Y, rallyPoints[0].Point.X - CenterPoint.X); angle = Util.ConvertToPositiveRadians(angle); /*if (angle < MathHelper.TwoPi / 4) * spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2); * else if (angle < MathHelper.TwoPi / 2) * spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2); * else if (angle < MathHelper.TwoPi * .75f) * spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + map.TileSize / 2); * else * spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + map.TileSize / 2);*/ PathNode closestPathNode = null; float closest = float.MaxValue; foreach (PathNode pathNode in exitPathNodes) { float distance = Vector2.Distance(pathNode.Tile.CenterPoint, RallyPoints[0].Point); if (distance < closest) { closestPathNode = pathNode; closest = distance; } } if (closestPathNode != null) { spawnLocation = closestPathNode.Tile.CenterPoint; } else if (exitPathNodes.Count > 0) { spawnLocation = exitPathNodes[0].Tile.CenterPoint; } else { spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2); } } else { spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2); } unit.CenterPoint = new Vector2(spawnLocation.X, spawnLocation.Y); unit.Rotation = angle; unit.InitializeCurrentPathNode(); if (rallyPoints.Count == 0) { unit.CheckForWallHit(); unit.CheckForPush(); } else { MoveCommand command = null; if (rallyPoints[0].Resource != null && unit is WorkerNublet) { command = new HarvestCommand(unit, rallyPoints[0].Resource); } else { command = new MoveCommand(unit, RallyPoints[0].Point); } if (command != null) { unit.GiveCommand(command); Rts.pathFinder.AddPathFindRequest(command, false, false, false); } for (int i = 1; i < RallyPoints.Count; i++) { if (rallyPoints[i].Resource != null && unit is WorkerNublet) { unit.QueueCommand(new HarvestCommand(unit, rallyPoints[i].Resource)); } else { unit.QueueCommand(new MoveCommand(unit, RallyPoints[i].Point)); } } unit.CheckForWallHit(); } } }
void returnCargo(ReturnCargoCommand command) { if (CargoType == ResourceType.Roks) { Player.Players[Team].Roks += CargoAmount; Player.Players[Team].Stats.RoksCounter += CargoAmount; } //if (Commands.Count > 1) //{ NextCommand(); if (Commands.Count == 0) { HarvestCommand harvestCommand = null; if (command.Source != null && !command.Source.Depleted) harvestCommand = new HarvestCommand(this, command.Source); else { Resource nearestResource = findNearestResource(CargoType); if (nearestResource != null && !nearestResource.Depleted) harvestCommand = new HarvestCommand(this, nearestResource); } if (harvestCommand != null) { GiveCommand(harvestCommand); Rts.pathFinder.AddPathFindRequest(harvestCommand, false, false, false); } } CargoAmount = 0; CargoType = null; //} /*else if (command.Source == null || command.Source.Depleted) { Resource nearestResource = findNearestResource(CargoType); if (nearestResource != null) GiveCommand(new HarvestCommand(nearestResource, 1)); else NextCommand(); } else GiveCommand(new HarvestCommand(command.Source, 1));*/ }
void moveToHarvestLocation(HarvestCommand command, GameTime gameTime) { checkForSmoothPath(command, gameTime); clearPushStatus(); clearHitWallStatus(); 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; command.NextWayPoint(this, Rts.pathFinder); return; } } else { Vector2 difference = wayPoint - centerPoint; if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY) //Resource resource = command.TargetResource; //if (Vector2.Distance(CenterPoint, resource.CenterPoint) < Radius + resource.Radius) { this.CenterPoint = wayPoint; HasMoved = true; lastWayPoint = wayPoint; //if (harvest(command.TargetResource)) //nextCommand(); Resource targetResource = command.TargetResource; if (targetResource == null || targetResource.Depleted) { Resource nearestResource = findNearestResource(targetResource.Type); if (nearestResource != null) GiveCommand(new HarvestCommand(this, nearestResource)); else NextCommand(); } else harvest(command.TargetResource); 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; /*Resource resource = command.TargetResource; if (Vector2.Distance(CenterPoint, resource.CenterPoint) <= Radius + resource.Radius) { //if (harvest(command.TargetResource)) // nextCommand(); harvest(command.TargetResource); }*/ if (checkForWallHit(command)) { //if (harvest(command.TargetResource)) // nextCommand(); /*if (CargoAmount == MAX_CARGO_AMOUNT && command.TargetResource.Type == CargoType) { TownHall townHall = findNearestTownHall(); if (townHall != null) GiveCommand(new ReturnCargoCommand(townHall, command.TargetResource, 1)); else nextCommand(); } else {*/ Resource targetResource = command.TargetResource; if (targetResource == null || targetResource.Depleted) { Resource nearestResource = findNearestResource(targetResource.Type); if (nearestResource != null) GiveCommand(new HarvestCommand(this, nearestResource)); else NextCommand(); } else harvest(command.TargetResource); //} } //if (checkForPush(command)) // return; if (!turnTowards(wayPoint, 120 / Radius, gameTime)) { Speed = MathHelper.Max(Speed - Util.ScaleWithGameTime(acceleration, gameTime), 0); } }
void completeBuildQueueItem(BuildQueueItem item) { BuildUnitButtonType buttonType = item.Type as BuildUnitButtonType; if (buttonType != null) { Unit unit; if (buttonType.UnitType == UnitType.MeleeNublet) unit = new MeleeNublet(new Vector2(), Team, item.ID); else if (buttonType.UnitType == UnitType.RangedNublet) unit = new RangedNublet(new Vector2(), Team, item.ID); else unit = new WorkerNublet(new Vector2(), Team, item.ID); float angle = 0; Vector2 spawnLocation; if (rallyPoints.Count > 0) { angle = (float)Math.Atan2(rallyPoints[0].Point.Y - CenterPoint.Y, rallyPoints[0].Point.X - CenterPoint.X); angle = Util.ConvertToPositiveRadians(angle); /*if (angle < MathHelper.TwoPi / 4) spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2); else if (angle < MathHelper.TwoPi / 2) spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2); else if (angle < MathHelper.TwoPi * .75f) spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + map.TileSize / 2); else spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + map.TileSize / 2);*/ PathNode closestPathNode = null; float closest = float.MaxValue; foreach (PathNode pathNode in exitPathNodes) { float distance = Vector2.Distance(pathNode.Tile.CenterPoint, RallyPoints[0].Point); if (distance < closest) { closestPathNode = pathNode; closest = distance; } } if (closestPathNode != null) spawnLocation = closestPathNode.Tile.CenterPoint; else if (exitPathNodes.Count > 0) spawnLocation = exitPathNodes[0].Tile.CenterPoint; else spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2); } else { spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2); } unit.CenterPoint = new Vector2(spawnLocation.X, spawnLocation.Y); unit.Rotation = angle; unit.InitializeCurrentPathNode(); if (rallyPoints.Count == 0) { unit.CheckForWallHit(); unit.CheckForPush(); } else { MoveCommand command = null; if (rallyPoints[0].Resource != null && unit is WorkerNublet) command = new HarvestCommand(unit, rallyPoints[0].Resource); else command = new MoveCommand(unit, RallyPoints[0].Point); if (command != null) { unit.GiveCommand(command); Rts.pathFinder.AddPathFindRequest(command, false, false, false); } for (int i = 1; i < RallyPoints.Count; i++) { if (rallyPoints[i].Resource != null && unit is WorkerNublet) unit.QueueCommand(new HarvestCommand(unit, rallyPoints[i].Resource)); else unit.QueueCommand(new MoveCommand(unit, RallyPoints[i].Point)); } unit.CheckForWallHit(); } } }