public UnitCommand(UnitCommandID cmd, UnitInstance unit, Tile tile) { CommandID = cmd; UnitInstanceID = unit.InstanceID; if (tile == null) { TileLocation = new Point(-1, -1); } else { TileLocation = tile.Location; } }
/// <summary> /// Returns the target type of the command /// </summary> /// <param name="id"></param> /// <returns></returns> public static UnitCommandTargetType GetTargetType(UnitCommandID id) { switch (id) { case UnitCommandID.UNITCMD_MOVE: case UnitCommandID.UNITCMD_MELEE: return(UnitCommandTargetType.Tile); case UnitCommandID.UNITCMD_DISBAND: case UnitCommandID.UNITCMD_SLEEP: case UnitCommandID.UNITCMD_SKIP: case UnitCommandID.UNITCMD_SETTLE: default: return(UnitCommandTargetType.Instant); } }
public UnitCommandIDEventArgs(UnitCommandID cmdID) { UnitCommandID = cmdID; }
// calculates the tiles to which the select unit can move to private void CalculateMoveTargets(UnitCommandID cmd) { if (cmd != UnitCommandID.UNITCMD_MOVE) { return; // don't bother if the select command isn't move } lock (_lock_updateMoveTargets) { moveTargets.Clear(); // clear the current move targets // Init values if (client.SelectedUnit == null) { return; } Point start = client.SelectedUnit.Location; int movement = client.SelectedUnit.Movement; // Init collections HashSet <Tile> checkedMoves = new HashSet <Tile>(); HashSet <Tile> possibleMoves = new HashSet <Tile>(); Queue <Tile> uncheckedMoves = new Queue <Tile>(client.GetCachedTile(start).GetNeighbourTiles(client.CachedBoard)); // Process data while (uncheckedMoves.Count > 0) { // Get the next unchecked tile Tile uncheckedMove = uncheckedMoves.Dequeue(); // Check if we have checked it if (checkedMoves.Contains(uncheckedMove)) { continue; // Yes we have, lets not check it again } else { checkedMoves.Add(uncheckedMove); // No we haven't, lets mark ti as check so we don't check it again } // Get the path to the tile LinkedList <Point> path = Board.FindPath(uncheckedMove.Location, start, client.Board); // Check if it is possible this turn if (path != null && path.Count <= movement && uncheckedMove.GetMovementCost() != -1) { possibleMoves.Add(uncheckedMove); // Yes it is, add it to the possible moves // Try adding the unchecked tile's neighbours to the queue List <Tile> ns = uncheckedMove.GetNeighbourTiles(client.CachedBoard); foreach (Tile n in ns) { // Don't bother checking a tile we will never be able to move onto if (n.GetMovementCost() == -1) { continue; } // Add the neighbour to the queue if we haven't checked it yet if (!checkedMoves.Contains(n)) { uncheckedMoves.Enqueue(n); } } } } moveTargets.AddRange(possibleMoves); } }
/// <summary> /// Shows the gui element /// </summary> public void Show() { lock (SceneGame._lock_guiDrawCall) { // hide other forms sceneGame.HideForms(true, false); // don't show try showing the form if no unit is selected if (client.SelectedUnit == null) { Hide(); return; } // programatically build the form int height = 250; int width = 400; canvas.RemoveChild(form); form = new Form(new Rectangle(0, canvas.Bounds.Height - height, width, height), canvas); form.Draggable = false; form.CloseButtonEnabled = true; // format the form text form.Text = $"Unit - {client.DataManager.Empire.GetEmpire(client.Player.EmpireID).Adjective} {client.SelectedUnit.Name}".ToRichText(); form.CloseButton.MouseClick += (s, a) => { // when the form is closed, deselect the selected unit client.SelectedUnit = null; }; // construct the command buttons based on the selected unit's abilities int xOffset = 0; int yOffset = 40; int btnWidth = 50; int btnHeight = 50; int maxPerLine = 5; int index = 0; commandIds = client.SelectedUnit.Commands; if (commandIds == null) // if the unit has no commands, don't try build any buttons { return; } for (int i = 0; i < commandIds.Count; i++) { // move the buttons to the next line if appropriate if (i != 0 && i % maxPerLine == 0) { index = 0; yOffset = 40 + btnHeight; } // build button Button btnCmd = new Button(new Rectangle((xOffset + btnWidth) * index, yOffset, btnWidth, btnHeight), form); btnCmd.Text = UnitCommand.GetCommandIcon(commandIds[i]); btnCmd.ToolTip = new ToolTip(UnitCommand.FormatName((commandIds[i]).ToString()).ToRichText(), 200); int locali = i; // closure means we can't just use i btnCmd.MouseClick += (s, a) => { // execute the unit command UnitCommandID cmdID = commandIds[locali]; ConsoleManager.Instance.WriteLine($"Select a new command, {cmdID}"); // if the command is instant, cast is instantly if (UnitCommand.GetTargetType(cmdID) == UnitCommandTargetType.Instant) { // command the unit if (client.SelectedUnit != null) { client.CommandUnit(new UnitCommand(cmdID, client.SelectedUnit, null)); } if (cmdID == UnitCommandID.UNITCMD_DISBAND || cmdID == UnitCommandID.UNITCMD_SETTLE) { // disbanding and settling cause the unit to be removed, to close the form and deselect the unit client.SelectedUnit = null; Hide(); } } // otherwise select it else { client.SelectedCommand = cmdID; } }; index++; } } }
/// <summary> /// Returns the rich text icon of the command /// </summary> /// <param name="cmdid"></param> /// <returns></returns> public static RichText GetCommandIcon(UnitCommandID cmdid) { string iconCode = ((char)FontSymbol.NA).ToString(); switch (cmdid) { case UnitCommandID.UNITCMD_MOVE: iconCode = ((char)FontSymbol.ArrowRight).ToString(); break; case UnitCommandID.UNITCMD_DISBAND: iconCode = ((char)FontSymbol.NA).ToString(); break; case UnitCommandID.UNITCMD_SLEEP: iconCode = ((char)FontSymbol.Bed).ToString(); break; case UnitCommandID.UNITCMD_SKIP: iconCode = ((char)FontSymbol.Feather).ToString(); break; case UnitCommandID.UNITCMD_SETTLE: iconCode = ((char)FontSymbol.Flag).ToString(); break; case UnitCommandID.UNITCMD_MELEE: iconCode = ((char)FontSymbol.Crosshairs).ToString(); break; case UnitCommandID.UNITCMD_BOMBARD: iconCode = ((char)FontSymbol.Bomb).ToString(); break; case UnitCommandID.UNITCMD_BUILD_FARM: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_FORT: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_LUMBERMILL: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_MINE: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_TRADINGPOST: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_ROADS: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_RAILROADS: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_CAMP: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_FISHINGBOATS: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_OFFSHOREPLATFORM: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_PASTURE: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_PLANTATION: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_QUARRY: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_ACADEMY: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_CITIDEL: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_CUSTOMSHOUSE: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_HOLYSITE: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_LANDMARK: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_BUILD_MANUFACTORY: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_REPAIR: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_CLEAN: iconCode = ((char)FontSymbol.Gavel).ToString(); break; case UnitCommandID.UNITCMD_NULL: default: iconCode = ((char)FontSymbol.NA).ToString(); break; } return(iconCode.ToRichText(null, RichText.SymbolFont)); }
// assigns a unit a list of commands that are valid based on its context private void BuildUnitCommands(UnitInstance unit) { unit.Commands = new List <UnitCommandID>(); for (int i = 0; i < unit.BaseUnit.Commands.Count; i++) { string cmdID = unit.BaseUnit.Commands[i]; UnitCommandID cmd = UnitCommandID.UNITCMD_NULL; if (!Enum.TryParse(cmdID, out cmd)) { ConsoleManager.Instance.WriteLine($"Unknown unit command '{cmdID}'", MsgType.ServerFailed); continue; } Technology tech; switch (cmd) { case UnitCommandID.UNITCMD_MOVE: case UnitCommandID.UNITCMD_DISBAND: case UnitCommandID.UNITCMD_SLEEP: case UnitCommandID.UNITCMD_SKIP: case UnitCommandID.UNITCMD_SETTLE: case UnitCommandID.UNITCMD_MELEE: case UnitCommandID.UNITCMD_BOMBARD: case UnitCommandID.UNITCMD_BUILD_ACADEMY: case UnitCommandID.UNITCMD_BUILD_CITIDEL: case UnitCommandID.UNITCMD_BUILD_CUSTOMSHOUSE: case UnitCommandID.UNITCMD_BUILD_HOLYSITE: case UnitCommandID.UNITCMD_BUILD_LANDMARK: case UnitCommandID.UNITCMD_BUILD_MANUFACTORY: unit.Commands.Add(cmd); break; case UnitCommandID.UNITCMD_BUILD_FARM: unit.Commands.Add(cmd); break; case UnitCommandID.UNITCMD_BUILD_FORT: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_ENGINEERING"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_LUMBERMILL: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_CONSTRUCTION"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_MINE: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_MINING"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_TRADINGPOST: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_GUILDS"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_ROADS: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_THE_WHEEL"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_RAILROADS: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_RAILROAD"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_CAMP: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_TRAPPING"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_FISHINGBOATS: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_SAILING"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_OFFSHOREPLATFORM: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_REFRIGERATION"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_PASTURE: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_ANIMAL_HUSBANDRY"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_PLANTATION: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_CALENDAR"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_BUILD_QUARRY: tech = Controllers.Player.GetPlayer(unit.PlayerID).TechTreeInstance.GetTech("TECH_MASONRY"); if (tech != null && tech.Unlocked) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_REPAIR: if (Controllers.Board.GetTile(unit.Location).Pillaged) { unit.Commands.Add(cmd); } break; case UnitCommandID.UNITCMD_CLEAN: if (Controllers.Board.GetTile(unit.Location).Fallout) { unit.Commands.Add(cmd); } break; } } }