| 26 | namespace Goals |
| 27 | { |
| 28 | std::unique_ptr<Goal> loadGoalFromStream(const std::string& goalName, std::istream& is) |
| 29 | { |
| 30 | std::string tempArguments; |
| 31 | std::unique_ptr<Goal> tempGoal; |
| 32 | |
| 33 | // Store the name and arguments of the goal so we can instantiate a specific goal subclass below. |
| 34 | getline(is, tempArguments); |
| 35 | |
| 36 | // Since getline leaves any leading whitespace we need to cut that off the beginning of the arguments string. |
| 37 | int count = 0; |
| 38 | while (tempArguments[count] == '\n' || tempArguments[count] == '\t' |
| 39 | || tempArguments[count] == ' ') |
| 40 | ++count; |
| 41 | |
| 42 | if (count > 0) |
| 43 | tempArguments = tempArguments.substr(count, tempArguments.length()); |
| 44 | |
| 45 | // Since entering an empty string in the file would break the file read we represent it with nullptr and then substitute it here. |
| 46 | if (tempArguments.compare("nullptr") == 0) |
| 47 | tempArguments = ""; |
| 48 | |
| 49 | // Parse the goal type name to find out what subclass of goal tempGoal should be instantiated as. |
| 50 | if (goalName.compare("KillAllEnemies") == 0) |
| 51 | { |
| 52 | tempGoal = Utils::make_unique<GoalKillAllEnemies>(goalName, tempArguments); |
| 53 | } |
| 54 | |
| 55 | else if (goalName.compare("ProtectCreature") == 0) |
| 56 | { |
| 57 | tempGoal = Utils::make_unique<GoalProtectCreature>(goalName, tempArguments); |
| 58 | } |
| 59 | |
| 60 | else if (goalName.compare("ClaimNTiles") == 0) |
| 61 | { |
| 62 | tempGoal = Utils::make_unique<GoalClaimNTiles>(goalName, tempArguments); |
| 63 | } |
| 64 | |
| 65 | else if (goalName.compare("MineNGold") == 0) |
| 66 | { |
| 67 | tempGoal = Utils::make_unique<GoalMineNGold>(goalName, tempArguments); |
| 68 | } |
| 69 | |
| 70 | else if (goalName.compare("ProtectDungeonTemple") == 0) |
| 71 | { |
| 72 | tempGoal = Utils::make_unique<GoalProtectDungeonTemple>(goalName, tempArguments); |
| 73 | } |
| 74 | //FIXME: This is going to break if the level file is malformed. |
| 75 | |
| 76 | // Now that the goal has been properly instantiated we check to see if there are subgoals to read in. |
| 77 | char c; |
| 78 | c = is.peek(); |
| 79 | std::string subGoalName; |
| 80 | int numSubgoals; |
| 81 | if (c == '+') |
| 82 | { |
| 83 | // There is a subgoal which should be added on success. |
| 84 | is.ignore(1); |
| 85 | is >> numSubgoals; |
no test coverage detected