Skip to content

Commit 73c798b

Browse files
committed
Simplify Plando Loading
- Avoid unnecessary yaml copies - Condense some if statements
1 parent 2af6bb6 commit 73c798b

1 file changed

Lines changed: 52 additions & 68 deletions

File tree

logic/Plandomizer.cpp

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,85 +9,32 @@ PlandomizerError loadPlandomizer(const fspath& plandoFilepath, std::vector<Pland
99
{
1010
LOG_TO_DEBUG("Loading plandomizer file");
1111

12-
YAML::Node originalPlandoTree;
13-
if(!LoadYAML(originalPlandoTree, plandoFilepath)) {
12+
YAML::Node plandoTree;
13+
if(!LoadYAML(plandoTree, plandoFilepath)) {
1414
Utility::platformLog("Will skip using plando file");
1515
return PlandomizerError::NONE;
1616
}
1717

1818
// Go through and make plandomizer objects for each world
1919
for (size_t i = 0; i < numWorlds; i++)
2020
{
21-
LOG_TO_DEBUG("Loading Plando data for world " + std::to_string(i + 1))
21+
const std::string& worldName = "World " + std::to_string(i + 1);
22+
LOG_TO_DEBUG("Loading Plando data for " + worldName);
23+
2224
plandos[i] = Plandomizer();
2325
auto& plandomizer = plandos[i];
24-
YAML::Node plandoTree = originalPlandoTree;
25-
YAML::Node plandoLocations;
26-
YAML::Node plandoEntrances;
27-
YAML::Node randomStartingItemPool;
28-
std::string plandoStartingIsland = "";
29-
std::string worldName = "World " + std::to_string(i + 1);
30-
// Grab the YAML object which holds the plando info for this world.
31-
if (plandoTree[worldName] && plandoTree[worldName].IsMap())
32-
{
33-
if (plandoTree[worldName]["locations"] && plandoTree[worldName]["locations"].IsMap())
34-
{
35-
plandoLocations = plandoTree[worldName]["locations"];
36-
}
37-
38-
if (plandoTree[worldName]["entrances"] && plandoTree[worldName]["entrances"].IsMap())
39-
{
40-
plandoEntrances = plandoTree[worldName]["entrances"];
41-
}
42-
if (plandoTree[worldName]["starting island"] && plandoTree[worldName]["starting island"].IsScalar())
43-
{
44-
plandoStartingIsland = plandoTree[worldName]["starting island"].as<std::string>();
45-
}
46-
if (plandoTree[worldName]["random starting item pool"] && plandoTree[worldName]["random starting item pool"].IsSequence())
47-
{
48-
randomStartingItemPool = plandoTree[worldName]["random starting item pool"];
49-
}
50-
}
5126

52-
// Process starting island
53-
if (!plandoStartingIsland.empty())
54-
{
55-
plandomizer.startingIslandRoomNum = islandNameToRoomNum(plandoStartingIsland);
56-
if (plandomizer.startingIslandRoomNum == 0)
57-
{
58-
ErrorLog::getInstance().log("Plandomizer Error: Starting island name \"" + plandoStartingIsland + "\" is not recognized");
59-
return PlandomizerError::BAD_STARTING_ISLAND;
60-
}
61-
LOG_TO_DEBUG(" Setting starting island to " + plandoStartingIsland);
27+
// Grab the YAML object which holds the plando info for this world.
28+
if (!plandoTree[worldName] || !plandoTree[worldName].IsMap()) {
29+
continue;
6230
}
6331

64-
// Process random starting item pool
65-
if (randomStartingItemPool)
66-
{
67-
LOG_TO_DEBUG(" Starting Item Pool: ")
68-
for (const auto& item : randomStartingItemPool)
69-
{
70-
const std::string itemName = item.as<std::string>();
71-
const GameItem gameItem = nameToGameItem(itemName);
72-
if (gameItem == GameItem::INVALID)
73-
{
74-
ErrorLog::getInstance().log("Plandomizer Error: Unknown item name \"" + itemName + "\" in random starting item pool");
75-
return PlandomizerError::UNKNOWN_ITEM_NAME;
76-
}
77-
else if (!getSupportedStartingItems().contains(gameItem))
78-
{
79-
ErrorLog::getInstance().log("Plandomizer Error: The item \"" + itemName + "\" is currently not supported as a starting item");
80-
return PlandomizerError::BAD_STARTING_ITEM;
81-
}
82-
plandomizer.randomStartingItemPool.push_back(gameItem);
83-
LOG_TO_DEBUG(std::string(" ") + itemName);
84-
}
85-
}
32+
const YAML::Node& world = plandoTree[worldName];
8633

87-
// Process Locations
88-
if (plandoLocations)
34+
// Process locations
35+
if (world["locations"] && world["locations"].IsMap())
8936
{
90-
for (const auto& locationObject : plandoLocations)
37+
for (const auto& locationObject : world["locations"])
9138
{
9239
if (locationObject.first.IsNull())
9340
{
@@ -148,10 +95,10 @@ PlandomizerError loadPlandomizer(const fspath& plandoFilepath, std::vector<Pland
14895
}
14996
}
15097

151-
// Process Entrances
152-
if (plandoEntrances)
98+
// Process entrances
99+
if (world["entrances"] && world["entrances"].IsMap())
153100
{
154-
for (const auto entrance : plandoEntrances)
101+
for (const auto entrance : world["entrances"])
155102
{
156103
if (entrance.first.IsNull())
157104
{
@@ -162,6 +109,42 @@ PlandomizerError loadPlandomizer(const fspath& plandoFilepath, std::vector<Pland
162109
plandomizer.entrancesStr.insert({entrance.first.as<std::string>(), entrance.second.as<std::string>()});
163110
}
164111
}
112+
113+
// Process starting island
114+
if (world["starting island"] && world["starting island"].IsScalar())
115+
{
116+
const std::string& island = world["starting island"].as<std::string>("");
117+
plandomizer.startingIslandRoomNum = islandNameToRoomNum(island);
118+
if (plandomizer.startingIslandRoomNum == 0)
119+
{
120+
ErrorLog::getInstance().log("Plandomizer Error: Starting island name \"" + island + "\" is not recognized");
121+
return PlandomizerError::BAD_STARTING_ISLAND;
122+
}
123+
LOG_TO_DEBUG(" Setting starting island to " + island);
124+
}
125+
126+
// Process random starting item pool
127+
if (world["random starting item pool"] && world["random starting item pool"].IsSequence())
128+
{
129+
LOG_TO_DEBUG(" Starting Item Pool: ")
130+
for (const auto& item : world["random starting item pool"])
131+
{
132+
const std::string itemName = item.as<std::string>();
133+
const GameItem gameItem = nameToGameItem(itemName);
134+
if (gameItem == GameItem::INVALID)
135+
{
136+
ErrorLog::getInstance().log("Plandomizer Error: Unknown item name \"" + itemName + "\" in random starting item pool");
137+
return PlandomizerError::UNKNOWN_ITEM_NAME;
138+
}
139+
else if (!getSupportedStartingItems().contains(gameItem))
140+
{
141+
ErrorLog::getInstance().log("Plandomizer Error: The item \"" + itemName + "\" is currently not supported as a starting item");
142+
return PlandomizerError::BAD_STARTING_ITEM;
143+
}
144+
plandomizer.randomStartingItemPool.push_back(gameItem);
145+
LOG_TO_DEBUG(std::string(" ") + itemName);
146+
}
147+
}
165148
}
166149

167150
return PlandomizerError::NONE;
@@ -190,5 +173,6 @@ std::string errorToName(PlandomizerError err)
190173
case PlandomizerError::UNKNOWN_ITEM_NAME:
191174
return "UNKNOWN_ITEM_NAME";
192175
}
176+
193177
return "UNKNOWN";
194178
}

0 commit comments

Comments
 (0)