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

Refactor Robot related code #1343

Merged
merged 8 commits into from
May 8, 2023
17 changes: 9 additions & 8 deletions OPHD/MapObjects/Robot.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "Robot.h"

Robot::Robot(const std::string& name, const std::string& sprite_path, Type t) :
MapObject(name, sprite_path, "running"),
mType{t}
Robot::Robot(const std::string& name, const std::string& spritePath, Type type) :
MapObject(name, spritePath, "running"),
mType{type}
{}


Robot::Robot(const std::string& name, const std::string& sprite_path, const std::string& initialAction, Type t) :
MapObject(name, sprite_path, initialAction),
mType{t}
Robot::Robot(const std::string& name, const std::string& spritePath, const std::string& initialAction, Type type) :
MapObject(name, spritePath, initialAction),
mType{type}
{}


void Robot::startTask(int turns)
{
if (turns < 1) { throw std::runtime_error("Robot::startTask() called with a value less than 1."); }
if (turns < 1) { throw std::runtime_error("Robot task time must be at least 1 turn"); }
mTurnsToCompleteTask = turns;
}

Expand All @@ -29,11 +29,12 @@ NAS2D::Dictionary Robot::getDataDict() const
}


void Robot::updateTask()
void Robot::update()
{
if (mSelfDestruct)
{
die();
return;
}

if (mCancelTask)
Expand Down
3 changes: 2 additions & 1 deletion OPHD/MapObjects/Robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Robot : public MapObject
Robot(const std::string&, const std::string&, Type);
Robot(const std::string&, const std::string&, const std::string&, Type);

void update() override;

void startTask(int turns);

void fuelCellAge(int age) { mFuelCellAge = age; }
Expand All @@ -45,7 +47,6 @@ class Robot : public MapObject

protected:
void incrementFuelCellAge() { mFuelCellAge++; }
void updateTask();

private:
int mFuelCellAge = 0;
Expand Down
2 changes: 0 additions & 2 deletions OPHD/MapObjects/Robots/Robodigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class Robodigger : public Robot
void direction(Direction dir) { mDirection = dir; }
Direction direction() const { return mDirection; }

void update() override { updateTask(); }

NAS2D::Dictionary getDataDict() const override
{
auto dictionary = Robot::getDataDict();
Expand Down
2 changes: 0 additions & 2 deletions OPHD/MapObjects/Robots/Robodozer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class Robodozer : public Robot
void tileIndex(std::size_t index) { mTileIndex = index; }
std::size_t tileIndex() const { return mTileIndex; }

void update() override { updateTask(); }

private:
std::size_t mTileIndex = 0;
};
2 changes: 0 additions & 2 deletions OPHD/MapObjects/Robots/Robominer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ class Robominer : public Robot
Robominer(): Robot(constants::Robominer, "robots/robominer.sprite", Robot::Type::Miner)
{
}

void update() override { updateTask(); }
};
4 changes: 1 addition & 3 deletions OPHD/RobotPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void RobotPool::update()
}


bool RobotPool::insertRobotIntoTable(RobotTileTable& robotMap, Robot& robot, Tile& tile)
void RobotPool::insertRobotIntoTable(RobotTileTable& robotMap, Robot& robot, Tile& tile)
{
// Add pre-check for control count against max capacity, with one caveat
// When loading saved games a control max won't have been set yet as robots are loaded before structures
Expand All @@ -252,6 +252,4 @@ bool RobotPool::insertRobotIntoTable(RobotTileTable& robotMap, Robot& robot, Til
tile.pushMapObject(&robot);

++mRobotControlCount;

return true;
}
2 changes: 1 addition & 1 deletion OPHD/RobotPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RobotPool

void clear();
void erase(Robot* robot);
bool insertRobotIntoTable(RobotTileTable& robotMap, Robot& robot, Tile& tile);
void insertRobotIntoTable(RobotTileTable& robotMap, Robot& robot, Tile& tile);

std::size_t robotControlMax() const { return mRobotControlMax; }
std::size_t currentControlCount() const { return mRobotControlCount; }
Expand Down
4 changes: 1 addition & 3 deletions OPHD/States/MapViewState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,6 @@ void MapViewState::placeRobot(Tile& tile)

void MapViewState::placeRobodozer(Tile& tile)
{
auto& robot = mRobotPool.getDozer();

if (tile.thing() && !tile.thingIsStructure())
{
return;
Expand Down Expand Up @@ -1035,10 +1033,10 @@ void MapViewState::placeRobodozer(Tile& tile)
NAS2D::Utility<StructureManager>::get().removeStructure(*structure);
tile.deleteMapObject();
NAS2D::Utility<StructureManager>::get().disconnectAll();
robot.tileIndex(static_cast<std::size_t>(TerrainType::Dozed));
updateConnectedness();
}

auto& robot = mRobotPool.getDozer();
int taskTime = tile.index() == TerrainType::Dozed ? 1 : static_cast<int>(tile.index());
robot.startTask(taskTime);
mRobotPool.insertRobotIntoTable(mRobotList, robot, tile);
Expand Down