private int Sorter(QueuedCommand qc1, QueuedCommand qc2) { return qc1.priority-qc2.priority; }
private bool ExecuteCommand(QueuedCommand c) { if (c.agent.getAffiliation() == null) { return false; } Console.WriteLine(c.ToString()); Console.WriteLine(c.priority); if (c.order == Command.Action.Move) { Console.WriteLine("MOVE"); Console.WriteLine("Recognized Move"); if (c.agent != null && c.agent is Ship) { Console.WriteLine("Recognized Ship"); /*if (c.targetHex.GetGameObject() != null) { return false; } ((Ship)c.agent).move(c.targetHex); return true;*/ //Need to calculate path to target, move one space towards it. int diff = Math.Abs(c.targetHex.x - c.agent.hex.x) + Math.Abs(c.targetHex.y - c.agent.hex.y); List<Hex3D> path = Pathfinder(c.agent.hex, c.targetHex); if (path == null) { Console.WriteLine("null path"); //No path. Wait? } else if (path.Count <= 1) { Console.WriteLine("already there"); //Do nothing. Already there. } else { Console.WriteLine("moving"); ((Ship)(c.agent)).move(path[1]); } } } if (c.order == Command.Action.Fire) { if (c.agent != null && c.agent is Warship) { if (c.targetHex.GetGameObject() == null) { return false; } ((Warship)c.agent).Attack((Unit)c.targetHex.GetGameObject()); //i should probly check that the target is a unit return true; } } if (c.order == Command.Action.Jump) { if (c.agent != null && c.agent is Ship) { Hex3D newTarget = null; if (c.targetHex.GetGameObject() != null) { //Best effort. HashSet<Hex3D> attemptedHexes = new HashSet<Hex3D>(); HashSet<Hex3D> NewFrontier = new HashSet<Hex3D>(); HashSet<Hex3D> OldFrontier = new HashSet<Hex3D>(); attemptedHexes.Add(c.targetHex); NewFrontier.Add(c.targetHex); while (newTarget == null) { OldFrontier = NewFrontier; NewFrontier = new HashSet<Hex3D>(); foreach (Hex3D h1 in OldFrontier) { foreach (Hex3D h2 in h1.getNeighbors().Except(attemptedHexes)) { attemptedHexes.Add(h2); if (h2.hexgrid.GetWarpable().Contains(h2)) { NewFrontier.Add(h2); if (h2.GetGameObject() == null) { newTarget = h2; break; } } } if (newTarget != null) { break; } } } } else { newTarget = c.targetHex; } if (!newTarget.hexgrid.neighbors.Contains(c.agent.hex.hexgrid)) { return false; } foreach (Hex3D h3 in newTarget.hexgrid.getHexes()) { h3.visible = true; } ((Ship)c.agent).move(newTarget); return true; } } if (c.order == Command.Action.Build) { if (c.agent != null && c.agent is Planet) { //if (!c.agent.getAffiliation().payMetal(c.shiptype.)) { return false; } ((Planet)c.agent).build(c.shiptype.CreateShip()); return true; } } if (c.order == Command.Action.Upgrade) { if (c.agent != null && c.agent is Planet) { if (((Planet)c.agent).getMaxHealth() >= 4) { return false; } ((Planet)c.agent).build(Shield.creator.CreateShip()); return true; } } if (c.order == Command.Action.Colonize) { if (c.agent != null && c.agent is Ship) { if (c.targetHex.GetGameObject() is Planet && ((Ship)c.agent).shiptype is ColonyShip) { if (((Planet)c.targetHex.GetGameObject()).getAffiliation() != null) { return false; } if (!c.targetHex.getNeighbors().Contains(c.agent.hex)) { return false; } ((Planet)c.targetHex.GetGameObject()).setAffiliation(((Ship)c.agent).getAffiliation()); c.agent.kill(); return true; } if (c.targetHex.GetGameObject() is Asteroid && ((Ship)c.agent).shiptype is MiningShip) { if (((Asteroid)c.targetHex.GetGameObject()).getAffiliation() != null) { return false; } if (!c.targetHex.getNeighbors().Contains(c.agent.hex)) { return false; } Ship s = (Ship)c.agent; int robots = s.GetMiningRobots(); if (robots > 0) { ((Asteroid)c.targetHex.GetGameObject()).setAffiliation(((Ship)c.agent).getAffiliation()); s.SetMiningRobots(robots - 1); //don't kill, just decrease mining bot //c.agent.kill(); return true; } } } } if (c.order == Command.Action.Enter) { Console.WriteLine("ENTER"); if (c.agent != null && c.agent is Carrier) { ((Carrier)c.agent).UnloadAll(); return true; } else if (c.agent != null && c.agent is Ship) { GameObject target = c.targetHex.GetGameObject(); if (target is Carrier) { Console.WriteLine("Attempt Enter"); return ((Carrier)target).LoadShip((Ship)(c.agent)); } if (target == null) { Console.WriteLine("null Target"); } return false; } } return false; }