Exemple #1
0
gboolean
moo_plugin_set_enabled (MooPlugin  *plugin,
                        gboolean    enabled)
{
    g_return_val_if_fail (MOO_IS_PLUGIN (plugin), FALSE);

    if (enabled)
    {
        return plugin_enable (plugin);
    }
    else
    {
        plugin_disable (plugin);
        return TRUE;
    }
}
Exemple #2
0
command_result fortplan(color_ostream &out, vector<string> & params) {

    auto & con = out;
    std::vector<std::vector<std::string>> layout(128, std::vector<std::string>(128));
    if (params.size()) {
        coord32_t cursor;
        coord32_t userCursor;
        coord32_t startCursor;
        if (!DFHack::Gui::getCursorCoords(cursor.x, cursor.y, cursor.z)) {
            con.print("You must have an active in-game cursor.\n");
            return CR_FAILURE;
        }
        DFHack::Gui::getCursorCoords(startCursor.x, startCursor.y, startCursor.z);
        userCursor = startCursor;

        std::string cwd = Filesystem::getcwd();
        std::string filename = cwd+"/"+params[0];
        con.print("Loading file '%s'...\n",filename.c_str());
        try {
            layout = tokenizeFile(filename);
        } catch (int e) {
            con.print("Could not open the file.\n");
            return CR_FAILURE;
        }
        if (!is_enabled) {
            plugin_enable(out, true);
        }
        con.print("Loaded.\n");
        std::vector<std::vector<std::string>>::size_type x, y;
        bool started = false;
        for (y = 0; y < layout.size(); y++) {
            x = 0;
            auto hashBuild = layout[y][x].find("#build");
            if (hashBuild != layout[y][x].npos) {
                auto startLoc = layout[y][x].find("start(");
                if (startLoc != layout[y][x].npos) {
                    startLoc += 6;
                    auto nextDelimiter = layout[y][x].find(";",startLoc);
                    std::string startXStr = layout[y][x].substr(startLoc,nextDelimiter-startLoc);
                    int startXOffset = std::stoi(startXStr);
                    startLoc = nextDelimiter+1;
                    nextDelimiter = layout[y][x].find(";",startLoc);
                    std::string startYStr = layout[y][x].substr(startLoc,nextDelimiter-startLoc);
                    int startYOffset = std::stoi(startYStr);
                    startCursor.x -= startXOffset;
                    startCursor.y -= startYOffset;
                    DFHack::Gui::setCursorCoords(startCursor.x,startCursor.y,startCursor.z);
                    started = true;

                    auto startEnd = layout[y][x].find(")",nextDelimiter);

                    con.print("Starting at (%d,%d,%d) which is described as: %s\n",startCursor.x,startCursor.y,startCursor.z,layout[y][x].substr(nextDelimiter+1,startEnd-nextDelimiter).c_str());
                    std::string desc = layout[y][x].substr(startEnd+1);
                    if (desc.size()>0) {
                        con.print("Description of this plan: %s\n",desc.c_str());
                    }
                    continue;
                } else {
                    con.print("No start location found for this block\n");
                }
            } else if (!started) {
                con.print("Not a build file: %s\n",layout[y][x].c_str());
                break;
            }

            for (x = 0; x < layout[y].size(); x++) {
                if (strcmp(layout[y][x].substr(0,1).c_str(),"#")==0) {
                    continue;
                }

                if (strcmp(layout[y][x].c_str(),"`")!=0) {
                    auto dataIndex = layout[y][x].find("(");
                    std::string curCode;
                    std::vector<std::string> curData;
                    if (dataIndex != layout[y][x].npos) {
                        curCode = layout[y][x].substr(0,dataIndex);
                        int dataStart = dataIndex+1;
                        auto nextDataStart = layout[y][x].find(",",dataStart);
                        while (nextDataStart!=layout[y][x].npos) {
                            std::string nextData = layout[y][x].substr(dataStart,nextDataStart);
                            if (strcmp(nextData.substr(nextData.size()-1,1).c_str(),")")==0) {
                                nextData = nextData.substr(0,nextData.size()-1);
                            }
                            curData.push_back(nextData);
                            dataStart = nextDataStart+1;
                            nextDataStart = layout[y][x].find(",",dataStart);
                        }
                    } else {
                        curCode = layout[y][x];
                    }
                    //con.print("Found a cell with '%s' in it (layout[y][x] %d:%d-%d)\n",layout[y][x].c_str(),lineNum,start,nextInd);
                    auto buildingIndex = std::find_if(buildings.begin(), buildings.end(), MatchesCode(curCode.c_str()));

                    // = std::find(validInstructions.begin(), validInstructions.end(), layout[y][x]);
                    if(buildingIndex == buildings.end()) {
                        //con.print("That is not a valid code.\n");
                    } else {
                        //con.print("I can build that!\n");
                        BuildingInfo buildingInfo = *buildingIndex;
                        if (buildingInfo.variableSize || buildingInfo.defaultHeight > 1 || buildingInfo.defaultWidth > 1) {
                            //con.print("Found a building at (%d,%d) called %s, which has a size %dx%d or variable size\n",x,y,buildingInfo.name.c_str(),buildingInfo.defaultWidth, buildingInfo.defaultHeight);
                            // TODO: Make this function smarter, able to determine the exact shape
                            //        and location of the building
                            // For now, we just assume that we are always looking at the top left
                            //        corner of where it is located in the input file
                            coord32_t buildingSize;
                            if (!buildingInfo.variableSize) {
                                bool single = true;
                                bool block = true;
                                std::vector<std::vector<std::string>>::size_type checkX, checkY;
                                int yOffset = ((buildingInfo.defaultHeight-1)/2);
                                int xOffset = ((buildingInfo.defaultWidth-1)/2);
                                //con.print(" - Checking to see if it's a single, with %d<=x<%d and %d<=y<%d...\n",x-xOffset,x+xOffset,y-yOffset,y+yOffset);
                                // First, check to see if this is in the center of an empty square of its default size
                                for (checkY = y-yOffset; checkY <= (y+yOffset); checkY++) {
                                    for (checkX = x-xOffset; checkX <= (x+xOffset); checkX++) {
                                        if (checkX==x && checkY==y) {
                                            continue;
                                        }
                                        auto checkDataIndex = layout[checkY][checkX].find("(");
                                        std::string checkCode;
                                        if (checkDataIndex != layout[checkY][checkX].npos) {
                                            checkCode = layout[checkY][checkX].substr(0,checkDataIndex);
                                        } else {
                                            checkCode = layout[checkY][checkX];
                                        }

                                        con.print(" - Code at (%d,%d) is '%s': ",checkX,checkY,checkCode.c_str());
                                        auto checkIndex = std::find_if(buildings.begin(), buildings.end(), MatchesCode(checkCode.c_str()));
                                        //if (checkIndex == buildings.end()) {
                                        //    con.print("this is not a valid code, so we keep going.\n");
                                        //    continue;
                                        //}
                                        //if (curCode.compare(layout[checkY][checkX]) != 0) {
                                        if (checkIndex != buildings.end()) {
                                            //con.print("this is a building, so we break.\n");
                                            single = false;
                                            break;
                                        } else {
                                            //con.print("this is not a building, so we keep going.\n");
                                        }
                                    }
                                    if (!single) {
                                        //con.print("Not a single. Moving forward.\n");
                                        break;
                                    }
                                }
                                if (!single) {
                                    // If that's not the case, check to see if this is the top-left corner of
                                    //    a square of its default size
                                    //con.print(" - It's not single; checking to see if it's a block...\n");
                                    for (checkY = y; checkY < (y+buildingInfo.defaultHeight); checkY++) {
                                        for (checkX = x; checkX < (x+buildingInfo.defaultWidth); checkX++) {
                                            if (checkX==x && checkY==y) {
                                                continue;
                                            }
                                            auto checkDataIndex = layout[checkY][checkX].find("(");
                                            std::string checkCode;
                                            if (checkDataIndex != layout[checkY][checkX].npos) {
                                                checkCode = layout[checkY][checkX].substr(0,checkDataIndex);
                                            } else {
                                                checkCode = layout[checkY][checkX];
                                            }

                                            //con.print(" - Code at (%d,%d) is '%s': ",checkX,checkY,checkCode.c_str());
                                            if (curCode.compare(checkCode) != 0) {
                                                //con.print("this is not the same as '%s', so we break.\n",curCode.c_str());
                                                block = false;
                                                break;
                                            } else {
                                                //con.print("this is the same as '%s', so we erase it and move on.\n",curCode.c_str());
                                                layout[checkY][checkX] = "``";
                                            }
                                        }
                                        if (!block) {
                                            //con.print("Not a block. Moving forward.\n");
                                            break;
                                        }
                                    }
                                }

                                if (single) {
                                    //con.print("Placing a building with code '%s' centered at (%d,%d) and default size %dx%d.\n",curCode.c_str(),x,y,buildingInfo.defaultWidth,buildingInfo.defaultHeight);
                                    coord32_t offsetCursor = cursor;
                                    offsetCursor.x -= xOffset;
                                    offsetCursor.y -= yOffset;
                                    DFHack::Gui::setCursorCoords(offsetCursor.x, offsetCursor.y, offsetCursor.z);
                                    if (!buildingInfo.allocate()) {
                                        con.print("*** There was an error placing building with code '%s' centered at (%d,%d).\n",curCode.c_str(),x,y);
                                    }
                                    DFHack::Gui::setCursorCoords(cursor.x, cursor.y, cursor.z);
                                } else if (block) {
                                    //con.print("Placing a building with code '%s' with corner at (%d,%d) and default size %dx%d.\n",curCode.c_str(),x,y,buildingInfo.defaultWidth,buildingInfo.defaultHeight);
                                    if (!buildingInfo.allocate()) {
                                        con.print("*** There was an error placing building with code '%s' with corner at (%d,%d).\n",curCode.c_str(),x,y);
                                    }
                                } else {
                                    con.print("*** Found a code '%s' at (%d,%d) for a building with default size %dx%d with an invalid size designation.\n",curCode.c_str(),x,y,buildingInfo.defaultWidth,buildingInfo.defaultHeight);
                                }
                            } else {
                                //buildingSize = findBuildingExtent(layout, x, y, -1, -1, out);
                                //con.print(" - The building has variable size %dx%d\n",buildingSize.x, buildingSize.y);
                                //con.print(" - The building has a variable size, which is not yet handled.\n");
                            }
                        } else {
                            //con.print("Building a(n) %s.\n",buildingInfo.name.c_str());
                            if (!buildingInfo.allocate()) {
                                con.print("*** There was an error placing the %s at (%d,%d).\n",buildingInfo.name.c_str(),x,y);
                            }
                        }
                    }
                }
                cursor.x++;
                DFHack::Gui::setCursorCoords(cursor.x, cursor.y, cursor.z);
            }

            cursor.y++;
            cursor.x = startCursor.x;

        }
        DFHack::Gui::setCursorCoords(userCursor.x, userCursor.y, userCursor.z);
        con.print("Done with file.\n");
    } else {
        con.print("You must supply a filename to read.\n");
    }

    return CR_OK;
}
Exemple #3
0
static bool_t delete_cb (GtkWidget * window, GdkEvent * event, PluginHandle *
 plugin)
{
    plugin_enable (plugin, FALSE);
    return TRUE;
}