Commit 3b097906 authored by Alexander A. Maly's avatar Alexander A. Maly

Added --output option to actor Robot

to dump the resulting field after the program execution.
parent 4402d37c
Pipeline #2829 passed with stages
in 3 minutes and 49 seconds
......@@ -22,10 +22,36 @@ CFieldItem::CFieldItem()
void CFieldItem::setWalls(int wallByte)
{
upWall = (wallByte & UP_WALL) ? true : false;
downWall = (wallByte & DOWN_WALL) ? true : false;
leftWall = (wallByte & LEFT_WALL) ? true : false;
rightWall = (wallByte & RIGHT_WALL) ? true : false;
downWall = (wallByte & DOWN_WALL) ? true : false;
upWall = (wallByte & UP_WALL) ? true : false;
}
int CFieldItem::getWalls() const
{
int res = 0;
if (leftWall)
res |= LEFT_WALL;
if (rightWall)
res |= RIGHT_WALL;
if (downWall)
res |= DOWN_WALL;
if (upWall)
res |= UP_WALL;
return res;
}
bool CFieldItem::isEmpty(int wMask) const
{
return
(getWalls() & ~wMask) == 0 &&
!isColored &&
!mark &&
upChar == ' ' &&
downChar == ' ' &&
radiation == 0 &&
temperature == 0;
}
......@@ -163,6 +189,80 @@ bool ConsoleField::goRight()
return true;
}
int ConsoleField::saveToFile(const QString &filename) const
{
QFile f(filename);
bool ok = f.open(QIODevice::WriteOnly);
if (!ok) {
qDebug() << QString::fromUtf8("Ошибка сохранения обстановки ") << filename;
return 1;
}
int res = saveToDataStream(f);
f.close();
return res;
}
int ConsoleField::createWallMask(uint32_t x, uint32_t y, uint32_t mx, uint32_t my)
{
int res = 0;
if (x == 0)
res |= LEFT_WALL;
if (y == 0)
res |= UP_WALL;
if (x + 1 == mx)
res |= RIGHT_WALL;
if (y + 1 == my)
res |= DOWN_WALL;
return res;
}
int ConsoleField::saveToDataStream(QIODevice &stream) const
{
char ctmp[64];
uint32_t mx = roboCols, my = roboRows;
uint32_t rx = roboCol, ry = roboRow;
stream.write("; Field Size: x, y\n");
sprintf(ctmp, "%u %u\n", mx, my);
stream.write(ctmp);
stream.write("; Robot position: x, y\n");
sprintf(ctmp, "%u %u\n", rx, ry);
stream.write(ctmp);
stream.write("; A set of special Fields: x, y, Walls, Color, Radiation, Temperature, USymbol, DSymbol, Point\n");
for (uint32_t y = 0; y < my; y++) {
for (uint32_t x = 0; x < mx; x++) {
int wMask = createWallMask(x, y, mx, my);
CFieldItem item = items[y][x];
if (item.isEmpty(wMask)) {
continue;
}
char uChar = item.upChar.cell();
if (uChar == ' ')
uChar = '$';
char dChar = item.downChar.cell();
if (dChar == ' ')
dChar = '$';
char mark = item.mark ? '1' : '0';
char color = item.isColored ? '1' : '0';
sprintf(
ctmp, "%d %d %d %c %f %f %c %c %c\n",
x, y,
item.getWalls() & ~wMask, color,
item.radiation, item.temperature,
uChar, dChar, mark
);
stream.write(ctmp);
}
}
stream.write("; End Of File\n");
return 0;
}
int ConsoleField::loadFromFile(const QString &filename)
{
QFile f(filename);
......
......@@ -21,6 +21,8 @@ struct CFieldItem
{
CFieldItem();
void setWalls(int wallByte);
int getWalls() const;
bool isEmpty(int wMask = 0) const;
bool isColored;
bool mark;
......@@ -43,6 +45,7 @@ public:
const CFieldItem *getItem(uint32_t row, uint32_t col) const;
CFieldItem *getItem(uint32_t row, uint32_t col);
CFieldItem *getCurItem();
static int createWallMask(uint32_t x, uint32_t y, uint32_t mx, uint32_t my);
bool goLeft();
bool goRight();
......@@ -59,6 +62,8 @@ public:
uint32_t Cols() const { return roboCols; }
uint32_t Rows() const { return roboRows; }
int saveToFile(const QString &filename) const;
int saveToDataStream(QIODevice &stream) const;
int loadFromFile(const QString &filename);
int loadFromDataStream(QIODevice *stream);
......
......@@ -138,10 +138,11 @@ void RobotModule::reset()
This method is called when actor resets its state before program starts.
*/
//delete field;
qDebug() << "Reset!!";
if (!DISPLAY) { //console mode
qDebug() << "Reset::console mode";
qDebug() << "Robot::Reset::console mode";
return;
} else {
qDebug() << "Robot::Reset::gui mode";
}
field->destroyRobot();
......@@ -168,7 +169,17 @@ void RobotModule::changeGlobalState(
) {
Q_UNUSED(old);
using namespace Shared;
qDebug() << "RobotModuleBase::changeGlobalState";
qDebug() << "RobotModuleBase::changeGlobalState from " << (int) old << " to " << (int) current;
if (!DISPLAY) {
if (current == PluginInterface::GS_Observe && !oName.isEmpty()) {
int res = curConsoleField->saveToFile(oName);
qDebug() << "Dumped state to" << oName << ", res = " << res;
}
return;
}
view->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
if (current == PluginInterface::GS_Running) {
view->setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
......@@ -266,6 +277,12 @@ RobotModule::acceptableCommandLineParameters()
QVariant::String, false
)
);
params.append(
ExtensionSystem::CommandLineParameter(
true, 'o', "output", tr("Robot field output file name"),
QVariant::String, false
)
);
return params;
}
......@@ -279,6 +296,11 @@ QString RobotModule::initialize(
qDebug() << "FIELD: |" << fName << "| ";
}
if (runtimeParameters.value('o').isValid()) {
oName = runtimeParameters.value('o').toString();
qDebug() << "OUTPUT: |" << oName << "| ";
}
DISPLAY = (qobject_cast<QApplication*>(qApp) != 0);
#ifdef Q_OS_LINUX
......
......@@ -121,6 +121,7 @@ private:
void prepareNewWindow();
int CurCellSize;
bool DISPLAY;
QString oName;
ExtensionSystem::SettingsPtr curSettings;
ConsoleField *curConsoleField;
QMutex mutex;
......
......@@ -283,6 +283,7 @@ public:
_qApp->exit(_qApp->property("returnCode").isValid()
? _qApp->property("returnCode").toInt() : 1);
}
if (!manager->isGuiRequired()) {
_qApp->quit();
}
......
......@@ -712,11 +712,12 @@ KumirRunPlugin::acceptableCommandLineParameters() const
return result;
}
QString KumirRunPlugin::initialize(const QStringList &configurationArguments,
const ExtensionSystem::CommandLine &runtimeArguments)
{
QString KumirRunPlugin::initialize(
const QStringList &configurationArguments,
const ExtensionSystem::CommandLine &runtimeArguments
) {
pRun_->programLoaded = false;
const bool noBreakpoints = configurationArguments.contains("nobreakpoints");
bool noBreakpoints = configurationArguments.contains("nobreakpoints");
pRun_->setSupportBreakpoints(!noBreakpoints);
qRegisterMetaType<QVariant::Type>("QVariant::Type");
qRegisterMetaType< QList<QVariant::Type> >("QList<QVariant::Type>");
......@@ -833,6 +834,7 @@ void KumirRunPlugin::start()
pRun_->wait();
checkForErrorInConsole();
stop();
ExtensionSystem::PluginManager::instance()->switchGlobalState(Shared::PluginInterface::GS_Observe);
} else {
startTimer(0); // start thread after event loop started
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment