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; } }*/ }
void doScheduledActions() { foreach (Player player in Player.Players) { for (int i = 0; i < player.ScheduledActions.Count;) { ScheduledAction action = player.ScheduledActions[i]; if (action.ScheduledTime <= GameClock) { ScheduledReturnCargoCommand scheduledReturnCargoCommand = action as ScheduledReturnCargoCommand; if (scheduledReturnCargoCommand != null) { ((WorkerNublet)scheduledReturnCargoCommand.ReturnCargoCommand.Unit).ReturnCargoToNearestTownHall(scheduledReturnCargoCommand.ReturnCargoCommand.Source); player.ScheduledActions.Remove(action); continue; } ScheduledUnitCommand scheduledUnitCommand = action as ScheduledUnitCommand; if (scheduledUnitCommand != null) { MoveCommand moveCommand = scheduledUnitCommand.UnitCommand as MoveCommand; if (moveCommand != null) { if (scheduledUnitCommand.Queued) { scheduledUnitCommand.UnitCommand.Unit.QueueCommand(moveCommand); } else { scheduledUnitCommand.UnitCommand.Unit.GiveCommand(moveCommand); } } else { if (scheduledUnitCommand.Queued) { scheduledUnitCommand.UnitCommand.Unit.QueueCommand(scheduledUnitCommand.UnitCommand); } else { scheduledUnitCommand.UnitCommand.Unit.GiveCommand(scheduledUnitCommand.UnitCommand); } } } ScheduledUnitBuildCommand scheduledUnitBuildCommand = action as ScheduledUnitBuildCommand; if (scheduledUnitBuildCommand != null) { } ScheduledStructureCommand scheduledStructureCommand = action as ScheduledStructureCommand; if (scheduledStructureCommand != null) { // shouldnt happen but wtf //if (scheduledStructureCommand.Structure == null) // continue; if (scheduledStructureCommand.CommandType == CommandButtonType.RallyPoint) { ScheduledStructureTargetedCommand rallyPointCommand = action as ScheduledStructureTargetedCommand; if (!rallyPointCommand.Queued) { rallyPointCommand.Structure.RallyPoints.Clear(); } rallyPointCommand.Structure.RallyPoints.Add(new RallyPoint(rallyPointCommand.Point, (Resource)rallyPointCommand.Target)); } else if (scheduledStructureCommand.CommandType == CommandButtonType.Cancel) { if (scheduledStructureCommand.Structure.UnderConstruction) { scheduledStructureCommand.Structure.Cancel(); } else { if (scheduledStructureCommand.Index >= 0) { scheduledStructureCommand.Structure.RemoveFromBuildQueue(scheduledStructureCommand.Index); } else { scheduledStructureCommand.Structure.RemoveLastItemInBuildQueue(); } } } else { scheduledStructureCommand.Structure.AddToBuildQueue((ProductionButtonType)scheduledStructureCommand.CommandType, scheduledStructureCommand.ID); } } player.ScheduledActions.Remove(action); } else { i++; } } } }