// Execute an order. This function takes num_ships off the source_planet, // puts them into a newly-created fleet, calculates the distance to the // destination_planet, and sets the fleet's total trip time to that // distance. Checks that the given player_id is allowed to give the given // order. If the order was carried out without any issue, and everything is // peachy, then true is returned. Otherwise, false is returned. bool GameState::ExecuteOrder(const GameDesc& desc, int playerID, int sourcePlanet, int destinationPlanet, int numShips) { PlanetState& source = planets[sourcePlanet]; if (source.owner != playerID || numShips > source.numShips || numShips < 0) { return false; } if(numShips == 0) // just ignore but dont error return true; source.numShips -= numShips; int distance = desc.Distance(sourcePlanet, destinationPlanet); Fleet f(source.owner, numShips, sourcePlanet, destinationPlanet, distance, distance); fleets.push_back(f); return true; }
// Execute an order. This function takes num_ships off the source_planet, // puts them into a newly-created fleet, calculates the distance to the // destination_planet, and sets the fleet's total trip time to that // distance. Checks that the given player_id is allowed to give the given // order. If the order was carried out without any issue, and everything is // peachy, then true is returned. Otherwise, false is returned. bool GameState::ExecuteOrder(const GameDesc& desc, int playerID, int sourcePlanet, int destinationPlanet, int numShips) { if (numShips <= 0 || sourcePlanet < 0 || planets.size() <= (Planets::size_type)sourcePlanet || destinationPlanet < 0 || planets.size() <= (Planets::size_type)destinationPlanet || sourcePlanet == destinationPlanet) { return false; } PlanetState& source = planets[sourcePlanet]; if (source.owner != playerID || numShips > source.numShips) { return false; } source.numShips -= numShips; int distance = desc.Distance(sourcePlanet, destinationPlanet); Fleet f(source.owner, numShips, sourcePlanet, destinationPlanet, distance, distance); bool done = false; for (Fleets::iterator fi = fleets.begin(); !done && (fi != fleets.end()); ++fi) { if (fi->owner == playerID && fi->sourcePlanet == sourcePlanet && fi->destinationPlanet == destinationPlanet && fi->turnsRemaining == distance) { fi->numShips += numShips; done = true; } } if (!done) { fleets.push_back(f); } return true; }