Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move writeRobots from MapViewState to RobotPool #1537

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions appOPHD/RobotPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "MapObjects/Structures/RobotCommand.h"

#include <NAS2D/Utility.h>
#include <NAS2D/ParserHelper.h>
#include <NAS2D/Xml/XmlElement.h>

#include <algorithm>
#include <stdexcept>
Expand Down Expand Up @@ -72,6 +74,26 @@ namespace
}
return controlCounter;
}


NAS2D::Dictionary robotToDictionary(RobotPool::RobotTileTable& robotTileTable, Robot& robot)
{
NAS2D::Dictionary dictionary = robot.getDataDict();

const auto it = robotTileTable.find(&robot);
if (it != robotTileTable.end())
{
const auto& tile = *it->second;
const auto position = tile.xy();
dictionary += NAS2D::Dictionary{{
{"x", position.x},
{"y", position.y},
{"depth", tile.depth()},
}};
}

return dictionary;
}
}


Expand Down Expand Up @@ -253,3 +275,17 @@ void RobotPool::insertRobotIntoTable(RobotTileTable& robotMap, Robot& robot, Til

++mRobotControlCount;
}


NAS2D::Xml::XmlElement* RobotPool::writeRobots(RobotTileTable& robotMap)
{
auto* robots = new NAS2D::Xml::XmlElement("robots");

for (auto robot : mRobots)
{
auto dictionary = robotToDictionary(robotMap, *robot);
robots->linkEndChild(NAS2D::dictionaryToAttributes("robot", dictionary));
}

return robots;
}
10 changes: 10 additions & 0 deletions appOPHD/RobotPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
class Tile;
using RobotList = std::vector<Robot*>;

namespace NAS2D
{
namespace Xml
{
class XmlElement;
}
}


class RobotPool
{
Expand Down Expand Up @@ -54,6 +62,8 @@ class RobotPool

const RobotList& robots() const { return mRobots; }

NAS2D::Xml::XmlElement* writeRobots(RobotTileTable& robotMap);

private:
DiggerList mDiggers;
DozerList mDozers;
Expand Down
36 changes: 1 addition & 35 deletions appOPHD/States/MapViewStateIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,40 +79,6 @@ namespace
}


NAS2D::Dictionary robotToDictionary(RobotTileTable& robotTileTable, Robot& robot)
{
NAS2D::Dictionary dictionary = robot.getDataDict();

const auto it = robotTileTable.find(&robot);
if (it != robotTileTable.end())
{
const auto& tile = *it->second;
const auto position = tile.xy();
dictionary += NAS2D::Dictionary{{
{"x", position.x},
{"y", position.y},
{"depth", tile.depth()},
}};
}

return dictionary;
}


NAS2D::Xml::XmlElement* writeRobots(RobotPool& robotPool, RobotTileTable& robotMap)
{
auto* robots = new NAS2D::Xml::XmlElement("robots");

for (auto robot : robotPool.robots())
{
auto dictionary = robotToDictionary(robotMap, *robot);
robots->linkEndChild(NAS2D::dictionaryToAttributes("robot", dictionary));
}

return robots;
}


NAS2D::Xml::XmlElement* writeResearch(const ResearchTracker& tracker)
{
auto* research = new NAS2D::Xml::XmlElement("research");
Expand Down Expand Up @@ -189,7 +155,7 @@ void MapViewState::save(const std::string& filePath)
mTileMap->serialize(root);
mMapView->serialize(root);
root->linkEndChild(NAS2D::Utility<StructureManager>::get().serialize());
root->linkEndChild(writeRobots(mRobotPool, mRobotList));
root->linkEndChild(mRobotPool.writeRobots(mRobotList));
root->linkEndChild(writeResources(mResourceBreakdownPanel.previousResources(), "prev_resources"));
root->linkEndChild(writeResearch(mResearchTracker));
root->linkEndChild(NAS2D::dictionaryToAttributes("turns", {{{"count", mTurnCount}}}));
Expand Down