#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTimer>
#include <QQuickWindow>
#include <QProcess>
#include <QQmlContext>
#include <QTextCodec>
#include <QFont>
#include<QDebug>
#include <QDir>
#include <QScreen>
#include <QMediaPlayer>
#include <QFileInfo>
#include <QRegularExpression>
#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include<cstdlib>
#include "mainwindow.h"
#if defined(_MSC_VER)&&(_MSC_VER>=1600)
#pragma execution_character_set("utf-8")
#endif
#define qastop (a==261)||(a==406)||(a==521)||(a==533)||(a==539)||(a==563)||(a==577)||(a==585)||(a==613)||(a==667)||(a==673)||(a==681)||(a==695)||(a==841)
#define gameexit (a==849)||(a==862)||(a==857)||(a==896)||(a==911)||(a==936)||(a==939)||(a==947)||(a==971)||(a==975)
const char* sFilePath = "setting.txt";
const char* aFilePath = "a.txt";
const char* slFilePath = "saves/save.txt";
const char* s2FilePath = "saves/cuowu.txt";
const char* eFilePath = "saves/extra.txt";
//写入setting.txt
void swrite(const std::string& newContent,int lineNumber){
    // 验证行号有效性
    if (lineNumber < 1) {
        throw std::invalid_argument("行号必须大于等于1");
    }
    
    // 读取文件内容到堆区
    auto lines = std::make_unique<std::vector<std::string>>(); 
    bool fileExists = false;

    // 尝试打开文件读取内容
    std::ifstream inputFile(sFilePath);
    if (inputFile.is_open()) {
        fileExists = true;
        std::string line;
        while (std::getline(inputFile, line)) {
            lines->push_back(std::move(line));
        }
        inputFile.close();
    }

    // 文件不存在时的处理
    if (!fileExists) {
        // 创建新文件
        lines->resize(lineNumber, ""); // 填充空行直到目标行号
    }

    // 确保行号在有效范围内
    if (lineNumber > static_cast<int>(lines->size()) + 1) {
            throw std::out_of_range("行号超出文件范围");
        // 扩展文件行数
        lines->resize(lineNumber, "");
    }

    // 更新指定行的内容
    (*lines)[lineNumber - 1] = newContent;

    // 写入文件
    std::ofstream outputFile(sFilePath);

    // 写入所有行
    for (const auto& content : *lines) {
        outputFile << content << "\n";
    }
    outputFile.close();
}
//读取指定行
std::string sread(int lineNumber)
{
    // 验证行号有效性
    if (lineNumber < 1) {
        throw std::invalid_argument("行号必须大于等于1");
    }

    // 尝试打开文件读取内容
    std::ifstream inputFile(sFilePath);

    std::string line;
    int currentLine = 0;

    // 逐行读取直到目标行
    while (std::getline(inputFile, line)) {
        currentLine++;
        if (currentLine == lineNumber) {
            inputFile.close();
            return line;
        }
    }

    inputFile.close();

    // 如果到达这里，说明文件行数不足
    throw std::out_of_range("行号 " + std::to_string(lineNumber) +
        " 超出文件范围（文件共 " +
        std::to_string(currentLine) + " 行）");
}
//计算缩放因子
double calculateScaleFactor(int qjsf) {
    // 获取屏幕尺寸
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);

    // 获取工作区（排除任务栏）
    RECT workArea;
    SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
    int availableWidth = workArea.right - workArea.left;
    int availableHeight = workArea.bottom - workArea.top;

    // 基准窗口尺寸
    const int baseWidth = 720;
    const int baseHeight = 480;

    double scaleFactor = 1.0;
    if (qjsf == 1) {
        // 使用整个屏幕
        double widthRatio = static_cast<double>(screenWidth) / baseWidth;
        double heightRatio = static_cast<double>(screenHeight) / baseHeight;
        scaleFactor = (widthRatio < heightRatio) ? widthRatio : heightRatio;
    }
    else if (qjsf == 2) {
        // 使用可用区域
        double widthRatio = static_cast<double>(availableWidth) / baseWidth;
        double heightRatio = static_cast<double>(availableHeight) / baseHeight;
        scaleFactor = (widthRatio < heightRatio) ? widthRatio : heightRatio;
    }
    //写入文件
    swrite(std::to_string(qjsf),1);
    return scaleFactor;
}

void restartApplication() {
    // 重启
    QString program = QCoreApplication::applicationFilePath();
    QStringList arguments = QCoreApplication::arguments();
    QProcess::startDetached(program, arguments);
    QCoreApplication::quit();
}

int a=0;
int hang = 0;
static std::vector<std::string>*fileContent = nullptr;
QString getCurrentLine(int hang) {
        int indexa = hang - 1;
    //qDebug() << "Updating text to line" << hang;
    if (!fileContent || fileContent->empty()) {
        return "File not loaded";
    }
    return QString::fromStdString((*fileContent)[indexa]);
}
int textreturn;
void QA(QQmlApplicationEngine& engine) {
    if (qastop) {
        engine.rootContext()->setContextProperty("daanvisible", 1);
        QString Da1 = getCurrentLine(a + 1);
        QString Da2;
        if (a != 681) {
            Da2 = getCurrentLine(a + 2);
        }
        else {
            Da2 = getCurrentLine(a + 3);
        }
        Da1.remove(QRegularExpression("<\\d+>"));
        Da2.remove(QRegularExpression("<\\d+>"));
        engine.rootContext()->setContextProperty("ida1", Da1);
        engine.rootContext()->setContextProperty("ida2", Da2);
    }
    else if (a == 551) {
        engine.rootContext()->setContextProperty("shuruvisible", 1);
    }
}
void updateDisplayText(QQmlApplicationEngine& engine, int hang,float vzi) {
    static QTimer* displayTimer = nullptr;
    static QString targetText;      // 目标完整文本
    static QString currentText;    // 当前显示的文本
    static int charIndex = 0;      // 当前字符索引
    static QQmlApplicationEngine* currentEngine = nullptr; // 保存engine指针
    static int name = -1;          // 令牌数字值
    static QString nameText = "";  // 对应的名称文本
    textreturn = 0;
    // 首次调用时初始化定时器
    if (!displayTimer) {
        displayTimer = new QTimer();
        QObject::connect(displayTimer, &QTimer::timeout, [&]() {
            if (charIndex < targetText.length()) {
                // 追加下一个字符
                currentText += targetText[charIndex];
                charIndex++;

                // 更新QML属性
                currentEngine->rootContext()->setContextProperty("currentText", currentText);
                currentEngine->clearComponentCache(); // 强制刷新
                textreturn= 0;
            }
            else {
                // 文本显示完成，停止定时器
                displayTimer->stop();
                textreturn = 1;
                if (textreturn && (qastop || (a == 551))) {
                    QA(engine);
                }
            }
            });
    }

    // 停止当前定时器（如果正在运行）
    if (displayTimer->isActive()) {
        displayTimer->stop();
    }

    // 获取原始文本并处理令牌
    QString rawText = getCurrentLine(hang);
    QRegularExpression tokenRegex("<(\\d+)>");
    QRegularExpressionMatch match = tokenRegex.match(rawText);

    // 重置状态
    targetText = rawText;
    currentText = "";
    charIndex = 0;
    name = -1;
    nameText = "";
    currentEngine = &engine; // 保存当前engine引用

    // 处理令牌
    if (match.hasMatch()) {
        // 提取令牌数字
        bool ok;
        name = match.captured(1).toInt(&ok);
        if (!ok) name = -1;

        // 从文本中移除令牌
        targetText = targetText.remove(match.capturedStart(), match.capturedLength());

        // 设置对应的名称文本
        switch (name) {
        case 0:
            nameText = "\x3F\x3F\x3F";
            break;
        case 1:
            nameText = "\xE6\x98\x9F\xE7\xA9\xBA";
            break;
        case 2:
            nameText = "\xE6\xB0\x98\xE7\x81\xAB";
            break;
        case 3:
            nameText = "\xE7\x81\xAF";
            break;
        case 4:
            nameText = "\xE5\x85\xB6\xE4\xBB\x96\xE5\x90\x8C\xE5\xAD\xA6";
            break;
        case 5:
            nameText = "\xE6\x98\x9F\xE7\xA9\xBA\x26\xE7\x81\xAF";
            break;
        case 6:
            nameText = "\xE9\xA3\x9E\xE8\x88\xB9\x41\x49";
            break;
        case 7:
            nameText = "\xE6\xB0\x98\xE7\x81\xAB\x26\xE7\x81\xAF";
            break;
        case 8:
            nameText = "\xE6\x98\x9F\xE7\xA9\xBA\x26\xE6\xB0\x98\xE7\x81\xAB";
            break;
        case 9:
            nameText = "\xE8\xB7\xAF\xE4\xBA\xBA";
            break;
        default:
            nameText = "";
        }
    }

    // 更新名称属性
    engine.rootContext()->setContextProperty("nameText", nameText);

    // 立即显示第一个字符（如果有文本）
    if (!targetText.isEmpty()) {
        currentText = targetText[0];
        charIndex = 1;
        engine.rootContext()->setContextProperty("currentText", currentText);
    }

    // 启动定时器
    displayTimer->start(50/(vzi+ 0.0001));
    //qDebug() << textreturn;
}
void udText(QQmlApplicationEngine& engine, int hang) {
    static QString Text;      // 目标完整文本
    static int charIndex = 0;      // 当前字符索引
    static QQmlApplicationEngine* currentEngine = nullptr; // 保存engine指针
    static int name = -1;          // 令牌数字值
    static QString nameText = "";  // 对应的名称文本
    textreturn = 0;

    // 获取原始文本并处理令牌
    QString rawText = getCurrentLine(hang);
    QRegularExpression tokenRegex("<(\\d+)>");
    QRegularExpressionMatch match = tokenRegex.match(rawText);

    // 重置状态
    Text = rawText;
    charIndex = 0;
    name = -1;
    nameText = "";
    currentEngine = &engine; // 保存当前engine引用

    // 处理令牌
    if (match.hasMatch()) {
        // 提取令牌数字
        bool ok;
        name = match.captured(1).toInt(&ok);
        if (!ok) name = -1;

        // 从文本中移除令牌
        Text = Text.remove(match.capturedStart(), match.capturedLength());

        // 设置对应的名称文本
        switch (name) {
        case 0:
            nameText = "\x3F\x3F\x3F";
            break;
        case 1:
            nameText = "\xE6\x98\x9F\xE7\xA9\xBA";
            break;
        case 2:
            nameText = "\xE6\xB0\x98\xE7\x81\xAB";
            break;
        case 3:
            nameText = "\xE7\x81\xAF";
            break;
        case 4:
            nameText = "\xE5\x85\xB6\xE4\xBB\x96\xE5\x90\x8C\xE5\xAD\xA6";
            break;
        case 5:
            nameText = "\xE6\x98\x9F\xE7\xA9\xBA\x26\xE7\x81\xAF";
            break;
        case 6:
            nameText = "\xE9\xA3\x9E\xE8\x88\xB9\x41\x49";
            break;
        case 7:
            nameText = "\xE6\xB0\x98\xE7\x81\xAB\x26\xE7\x81\xAF";
            break;
        case 8:
            nameText = "\xE6\x98\x9F\xE7\xA9\xBA\x26\xE6\xB0\x98\xE7\x81\xAB";
            break;
        case 9:
            nameText = "\xE8\xB7\xAF\xE4\xBA\xBA";
            break;
        default:
            nameText = "";
        }
    }
    // 更新名称属性
    engine.rootContext()->setContextProperty("nameText", nameText);

    if (!Text.isEmpty()) {
        engine.rootContext()->setContextProperty("currentText", Text);
    }

}
QVariantList sjstart(QQmlApplicationEngine& engine, int dirn, int lditu) {
    engine.rootContext()->setContextProperty("blockCount", dirn);
    static QVariantList dixy = {};
    for (int i = 0; i < dirn; i++) {
        if (dixy.size() < 2 * dirn) {
            if (lditu==1){
                QVariantList ddixy = { (720 + ((1500) / dirn) * i),330 };
                dixy = dixy + ddixy;
            }
            else if (lditu == 2) {
                QVariantList ddixy = { (3000 + ((1500) / dirn) * i),330 };
                dixy = dixy + ddixy;
            }
            
        }
    }
    return dixy;
}
void updatemp3(QMediaPlayer* player) {
    int ina = a;
    inah:
    switch (ina) {
    case 1:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Memories.mp3"));
        player->play();
        break;
    case 38:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Playing_Together_With_You.mp3"));
        player->play();
        break;
    case 83:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Playing_Around_The_World.mp3"));
        player->play();
        break;
    case 143:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Universe.mp3"));
        player->play();
        break;
    case 152:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/The_Adventure.mp3"));
        player->play();
        break;
    case 192:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Playing_Together_With_You.mp3"));
        player->play();
        break;
    case 255:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Playing_Around_The_World.mp3"));
        player->play();
        break;
    case 392:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Universe.mp3"));
        player->play();
        break;
    case 403:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Playing_Around_The_World.mp3"));
        player->play();
        break;
    case 424:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Universe.mp3"));
        player->play();
        break;
    case 649:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/The_Adventure.mp3"));
        player->play();
        break;
    case 672:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Civilization.mp3"));
        player->play();
        break;
    case 702:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Memories.mp3"));
        player->play();
        break;
    case 710:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Civilization.mp3"));
        player->play();
        break;
    case 761:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
        player->play();
        break;
    case 832:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/The_Stars_Shall_Never_Forget.mp3"));
        player->play();
        break;
    case 856:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
        player->play();
        break;
    case 867:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
        player->play();
        break;
    case 876:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
        player->play();
        break;
    case 898:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
        player->play();
        break;
    case 913:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
        player->play();
        break;
    case 938:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
        player->play();
        break;
    case 941:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
        player->play();
        break;
    case 973:
        player->stop();
        player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
        player->play();
        break;
    default:
        ina--;
        goto inah;
    }
}
int dacuo = 0;
float extra;
int sj = 0;
int sjx = 0;
int sja1x = 90;
int sja1y = 310;
int sja2x = 20;
int sja2y = 310;
int sjsuo = 0;
int dirn=0;
int cuowu = 0;
QVariantList dixy = {};
QVariantList dirv = {};
int alinshi = a;
static QTimer* sjTimer = nullptr;
void updateb(QQmlApplicationEngine& engine,int Hang, QMediaPlayer* player,int load=0) {
    static QString b;
    static QString a1;
    static QString a21;
    static QString a22;
    static QString a31;
    static QString a32;
    static QString a33;
    static QString a4;
    static QString a51;
    static QString a52;
    static QString a53;
    static int bv = 0;
    static int a1v = 0;
    static int a2v = 0;
    static int a3v = 0;
    static int a1fv = 0;
    static int a3fv = 0;
    static int va = 0;
    static int sjvisible = 0;
    static int ditu1visible = 0;
    static int ditu2visible = 0;
    if (load) {
        bv = 1;
        updatemp3(player);
    }
    int bh = a;
    bshuaxin:
    switch (bh) {
    case 1:
        bv = 0;
        va = 0;
        updatemp3(player);
        break;
    case 2:
        bv = 0;
        va = 1;
        a1 = "a1-1";
        break;
    case 6:
        b = "b1";
        bv = 1;
        break;
    case 38:
        b = "b1";
        a1 = "a1-6";
        va = 1;
        updatemp3(player);
        break;
    case 39:
        b = "b2";
        a1 = "a1-1";
        va = 1;
        break;
    case 42:
        b = "b2";
        a21 = "a1-1";
        a22 = "a3-1-1";
        va = 2;
        break;
    case 43:
        b = "b2";
        a21 = "a1-1";
        a22 = "a3-1-2";
        va = 2;
        break;
    case 44:
        b = "cg1-1";
        va = 0;
        if (extra == 0) {
            extra = 1.1;
        }
        break;
    case 45:
        b = "cg1-2";
        if (extra < 1.2) {
            qDebug() << "111111111111";
            extra = 1.2;
            
        }
        break;
    case 47:
        b = "b2";
        va = 2;
        a21 = "a1-5";
        a22 = "a3-1-2";
        break;
    case 48:
        b = "b2";
        a21 = "a1-5";
        a22 = "a3-1-1";
        va = 2;
        break;
    case 50:
        b = "b2";
        a21 = "a1-1";
        a22 = "a3-1-1";
        va = 2;
        break;
    case 51:
        b = "b2";
        a21 = "a1-2";
        a22 = "a3-1-2";
        va = 2;
        break;
    case 52:
        b = "b2";
        a21 = "a1-1";
        a22 = "a3-1-1";
        va = 2;
        break;
    case 53:
        b = "b2";
        a21 = "a1-2";
        a22 = "a3-1-1";
        va = 2;
        break;
    case 67:
        b = "b2";
        a21 = "a1-1";
        a22 = "a3-1-2";
        va = 2;
        break;
    case 68:
        b = "b2";
        a21 = "a1-2";
        a22 = "a3-1-1";
        va = 2;
        break;
    case 69:
        b = "b2";
        a21 = "a1-1";
        a22 = "a3-1-2";
        va = 2;
        break;
    case 70:
        b = "b2";
        a21 = "a1-2";
        a22 = "a3-1-1";
        va = 2;
        break;
    case 71:
        b = "b2";
        a21 = "a1-5";
        a22 = "a3-1-4";
        va = 2;
        break;
    case 72:
        b = "cg2";
        va = 0;
        if (extra < 2) {
            extra = 2;
        }
        break;
    case 73:
        b = "cg3";
        if (extra < 3) {
            extra = 3;
        }
        break;
    case 83:
        b = "b3";
        updatemp3(player);
        break;
    case 95:
        b = "b4";
        a21 = "a3-2-6";
        a22 = "a1-8";
        va = 2;
        break;
    case 98:
        b = "b4";
        a21 = "a3-2-6";
        a22 = "a1-10";
        va = 2;
        break;
    case 99:
        b = "b4";
        a21 = "a3-2-7";
        a22 = "a1-8";
        va = 2;
        break;
    case 100:
        b = "b4";
        a21 = "a3-2-6";
        a22 = "a1-9";
        va = 2;
        break;
    case 101:
        b = "b4";
        a21 = "a3-2-8";
        a22 = "a1-8";
        va = 2;
        break;
    case 102:
        b = "b4";
        a21 = "a3-2-8";
        a22 = "a1-9";
        va = 2;
        break;
    case 103:
        b = "b4";
        a21 = "a3-2-10";
        a22 = "a1-10";
        va = 2;
        break;
    case 104:
        b = "b4";
        a21 = "a3-2-10";
        a22 = "a1-11";
        va = 2;
        break;
    case 105:
        b = "b4";
        a21 = "a3-2-8";
        a22 = "a1-11";
        va = 2;
        break;
    case 106:
        b = "cg4";
        va = 0;
        if (extra < 4) {
            extra = 4;
        }
        break;
    case 118:
        b = "b4";
        a21 = "a3-2-9";
        a22 = "a1-8";
        va = 2;
        break;
    case 119:
        b = "b4";
        a21 = "a3-2-10";
        a22 = "a1-10";
        va = 2;
        break;
    case 120:
        b = "b4";
        a21 = "a3-2-9";
        a22 = "a1-8";
        va = 2;
        break;
    case 121:
        b = "cg5";
        va = 0;
        if (extra < 5) {
            extra = 5;
        }
        break;
    case 122:
        b = "b5";
        a21 = "a3-2-1";
        a22 = "a1-2";
        va = 2;
        break;
    case 123:
        b = "b5";
        a21 = "a3-2-2";
        a22 = "a1-1";
        va = 2;
        break;
    case 124:
        b = "b5";
        a21 = "a3-2-1";
        a22 = "a1-2";
        va = 2;
        break;
    case 125:
        b = "b5";
        a21 = "a3-2-2";
        a22 = "a1-1";
        va = 2;
        break;
    case 126:
        b = "b5";
        a21 = "a3-2-1";
        a22 = "a1-1";
        va = 2;
        break;
    case 131:
        b = "b5";
        a21 = "a3-2-1";
        a22 = "a1-2";
        va = 2;
        break;
    case 132:
        b = "b5";
        a21 = "a3-2-2";
        a22 = "a1-1";
        va = 2;
        break;
    case 134:
        b = "b5";
        a21 = "a3-2-1";
        a22 = "a1-2";
        va = 2;
        break;
    case 135:
        b = "b5";
        a21 = "a3-2-1";
        a22 = "a1-1";
        va = 2;
        break;
    case 138:
        b = "b5";
        a21 = "a3-2-1";
        a22 = "a1-4";
        va = 2;
        break;
    case 140:
        b = "b5";
        a21 = "a3-2-5";
        a22 = "a1-4";
        va = 2;
        break;
    case 141:
        b = "b5";
        a21 = "a3-2-5";
        a22 = "a1-5";
        va = 2;
        break;
    case 143:
        b = "cg6";
        va = 0;
        if (extra < 6) {
            extra = 6;
        }
        updatemp3(player);
        break;
    case 151:
        b = "cg7-1";
        if (extra < 7.1) {
            extra = 7.1;
        }
        break;
    case 152:
        b = "cg7-2";
        if (extra < 7.2) {
            extra = 7.2;
        }
        updatemp3(player);
        break;
    case 157:
        b = "cg8";
        if (extra < 8) {
            extra = 8;
        }
        break;
    case 161:
        b = "cg9 - 1";
        if (extra < 9.1) {
            extra = 9.1;
        }
        break;
    case 163:
        b = "cg9 - 2";
        if (extra < 9.2) {
            extra = 9.2;
        }
        break;
    case 164:
        b = "cg9 - 1";
        break;
    case 166:
        b = "cg10";
        if (extra < 10) {
            extra = 10;
        }
        break;
    case 190:
        b = "cg11";
        if (extra < 11) {
            extra = 11;
        }
        break;
    case 192:
        b = "b14";
        updatemp3(player);
        break;
    case 195:
        b = "b2";
        a1 = "a1-1";
        va = 1;
        break;
    case 203:
        b = "b6";
        a1 = "a1-1";
        va = 1;
        break;
    case 215:
        b = "b7";
        va = 0;
        break;
    case 217:
        b = "b7";
        va = 1;
        a1 = "a2-1-2";
        break;
    case 223:
        b = "b2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-1-1";
        break;
    case 224:
        b = "b2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-1-1";
        break;
    case 225:
        b = "b2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-1-2";
        break;
    case 227:
        b = "b2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-1-1";
        break;
    case 229:
        b = "b2";
        va = 3;
        a31 = "a1-1";
        a32 = "a2-1-1";
        a33 = "a3-1-2";
        break;
    case 230:
        b = "b2";
        va = 3;
        a31 = "a1-1";
        a32 = "a2-1-5";
        a33 = "a3-1-1";
        break;
    case 231:
        b = "b2";
        va = 2;
        a21 = "a3-1-1";
        a22 = "a2-1-1";
        break;
    case 235:
        b = "b2";
        va = 2;
        a21 = "a3-1-1";
        a22 = "a2-1-1";
        break;
    case 238:
        b = "b2";
        va = 2;
        a21 = "a3-1-2";
        a22 = "a2-1-2";
        break;
    case 240:
        b = "cg12";
        va = 0;
        if (extra < 12) {
            extra = 12;
        }
        break;
    case 246:
        b = "b2";
        va = 2;
        a21 = "a2-1-3";
        a22 = "a3-1-1";
        break;
    case 247:
        b = "b2";
        va = 2;
        a21 = "a2-1-1";
        a22 = "a3-1-4";
        break;
    case 248:
        b = "b2";
        va = 2;
        a21 = "a2-1-4";
        a22 = "a3-1-4";
        break;
    case 250:
        b = "b2";
        va = 2;
        a21 = "a2-1-1";
        a22 = "a3-1-2";
        break;
    case 251:
        b = "b2";
        va = 2;
        a21 = "a2-1-1";
        a22 = "a3-1-1";
        break;
    case 253:
        b = "b2";
        va = 2;
        a21 = "a2-1-2";
        a22 = "a3-1-1";
        break;
    case 254:
        b = "b2";
        va = 2;
        a21 = "a2-1-5";
        a22 = "a3-1-4";
        break;
    case 255:
        b = "b8";
        va = 0;
        updatemp3(player);
        break;
    case 257:
        b = "b8";
        va = 1;
        a1 = "a1-1";
        break;
    case 260:
        b = "b9";
        va = 3;
        a31 = "a2-2-6";
        a32 = "a3-2-10";
        a33 = "a1-9";
        break;
    case 262:
        b = "b9";
        va = 3;
        a31 = "a2-2-6";
        a32 = "a3-2-10";
        a33 = "a1-10";
        break;
    case 263:
        b = "b9";
        va = 3;
        a31 = "a2-2-6";
        a32 = "a3-2-10";
        a33 = "a1-9";
        break;
    case 264:
        b = "cg13-1";
        va = 0;
        if (extra < 13.1) {
            extra = 13.1;
        }
        break;
    case 265:
        b = "cg13-2";
        if (extra < 13.2) {
            extra = 13.2;
        }
        break;
    case 267:
        b = "cg14-1";
        if (extra < 14.1) {
            extra = 14.1;
        }
        break;
    case 274:
        b = "cg14-2";
        if (extra < 14.2) {
            extra = 14.2;
        }
        break;
    case 279:
        bv = 0;
        break;
    case 282:
        bv = 0;
        va = 2;
        a21 = "a2-2-7";
        a22 = "a3-2-9";
        break;
    case 283:
        b = "b9";
        va = 2;
        a21 = "a2-2-7";
        a22 = "a3-2-9";
        break;
    case 284:
        b = "b9";
        va = 2;
        a21 = "a2-2-6";
        a22 = "a3-2-10";
        break;
    case 285:
        b = "b9";
        va = 2;
        a21 = "a2-2-7";
        a22 = "a3-2-9";
        break;
    case 288:
        b = "b11";
        va = 2;
        a21 = "a2-2-6";
        a22 = "a3-2-10";
        break;
    case 291:
        b = "b11";
        va = 2;
        a21 = "a2-2-7";
        a22 = "a3-2-10";
        break;
    case 292:
        b = "b11";
        va = 2;
        a21 = "a2-2-6";
        a22 = "a3-2-9";
        break;
    case 293:
        b = "b11";
        va = 2;
        a21 = "a2-2-7";
        a22 = "a3-2-10";
        break;
    case 294:
        b = "b11";
        va = 2;
        a21 = "a2-2-6";
        a22 = "a3-2-9";
        break;
    case 295:
        b = "b11";
        va = 2;
        a21 = "a2-2-7";
        a22 = "a3-2-10";
        break;
    case 296:
        b = "b11";
        va = 2;
        a21 = "a2-2-6";
        a22 = "a3-2-10";
        break;
    case 297:
        b = "b11";
        va = 2;
        a21 = "a2-2-6";
        a22 = "a3-2-9";
        break;
    case 298:
        b = "b11";
        va = 2;
        a21 = "a2-2-7";
        a22 = "a3-2-10";
        break;
    case 299:
        b = "b11";
        va = 2;
        a21 = "a2-2-6";
        a22 = "a3-2-10";
        break;
    case 300:
        b = "b11";
        va = 2;
        a21 = "a2-2-6";
        a22 = "a3-2-9";
        break;
    case 301:
        b = "b11";
        va = 2;
        a21 = "a2-2-6";
        a22 = "a3-2-10";
        break;
    case 302:
        b = "b11";
        va = 2;
        a21 = "a2-2-7";
        a22 = "a3-2-10";
        break;
    case 303:
        b = "cg15";
        va = 0;
        if (extra < 15) {
            extra = 15;
        }
        break;
    case 326:
        b = "b10";
        va = 3;
        a31 = "a1-8";
        a32 = "a2-2-6";
        a33 = "a3-2-10";
        break;
    case 327:
        b = "b10";
        va = 3;
        a31 = "a1-8";
        a32 = "a2-2-6";
        a33 = "a3-2-10";
        break;
    case 328:
        b = "b10";
        va = 3;
        a31 = "a1-8";
        a32 = "a2-2-6";
        a33 = "a3-2-9";
        break;
    case 329:
        b = "b10";
        va = 3;
        a31 = "a1-9";
        a32 = "a2-2-6";
        a33 = "a3-2-10";
        break;
    case 330:
        b = "b10";
        va = 3;
        a31 = "a1-8";
        a32 = "a2-2-7";
        a33 = "a3-2-10";
        break;
    case 335:
        b = "cg16";
        va = 0;
        if (extra < 16) {
            extra = 16;
        }
        break;
    case 339:
        b = "cg17-1";
        if (extra < 17.1) {
            extra = 17.1;
        }
        break;
    case 340:
        b = "cg17-2";
        if (extra < 17.2) {
            extra = 17.2;
        }
        break;
    case 341:
        b = "cg17-3";
        if (extra < 17.3) {
            extra = 17.3;
        }
        break;
    case 346:
        b = "cg17-4";
        if (extra < 17.4) {
            extra = 17.4;
        }
        break;
    case 348:
        b = "cg17-5";
        if (extra < 17.5) {
            extra = 17.5;
        }
        break;
    case 350:
        b = "cg17-6";
        if (extra < 17.6) {
            extra = 17.6;
        }
        break;
    case 354:
        b = "b11";
        va = 3;
        a31 = "a2-2-1";
        a32 = "a1-1";
        a33 = "a3-2-3";
        break;
    case 355:
        b = "b11";
        va = 3;
        a31 = "a2-2-2";
        a32 = "a1-2";
        a33 = "a3-2-1";
        break;
    case 356:
        b = "b11";
        va = 3;
        a31 = "a2-2-1";
        a32 = "a1-1";
        a33 = "a3-2-4";
        break;
    case 357:
        b = "b11";
        va = 3;
        a31 = "a2-2-2";
        a32 = "a1-2";
        a33 = "a3-2-4";
        break;
    case 358:
        b = "b11";
        va = 0;
        break;
    case 359:
        b = "b11";
        va = 3;
        a31 = "a2-2-6";
        a32 = "a1-8";
        a33 = "a3-2-10";
        break;
    case 360:
        b = "b11";
        va = 3;
        a31 = "a2-2-6";
        a32 = "a1-10";
        a33 = "a3-2-10";
        break;
    case 361:
        b = "b11";
        va = 3;
        a31 = "a2-2-7";
        a32 = "a1-8";
        a33 = "a3-2-9";
        break;
    case 362:
        b = "b11";
        va = 0;
        break;
    case 363:
        b = "cg18-1";
        if (extra < 18.1) {
            extra = 18.1;
        }
        break;
    case 365:
        b = "cg18-2";
        if (extra < 18.2) {
            extra = 18.2;
        }
        break;
    case 378:
        b = "b12";
        va = 4;
        a4 = "a1-4";
        break;
    case 380:
        b = "b12";
        va = 4;
        a4 = "a1-6";
        break;
    case 382:
        b = "b13";
        va = 5;
        a51 = "a1-1";
        a52 = "a2-2-1";
        a53 = "a3-2-1";
        break;
    case 383:
        b = "b13";
        va = 5;
        a51 = "a1-2";
        a52 = "a2-2-1";
        a53 = "a3-2-1";
        break;
    case 384:
        b = "b13";
        va = 5;
        a51 = "a1-1";
        a52 = "a2-2-2";
        a53 = "a3-2-2";
        break;
    case 385:
        b = "b13";
        va = 5;
        a51 = "a1-6";
        a52 = "a2-2-1";
        a53 = "a3-2-1";
        break;
    case 386:
        b = "b13";
        va = 5;
        a51 = "a1-2";
        a52 = "a2-2-1";
        a53 = "a3-2-1";
        break;
    case 387:
        b = "cg19-1";
        va = 0;
        if (extra < 19.1) {
            extra = 19.1;
        }
        break;
    case 389:
        b = "cg19-2";
        if (extra < 19.2) {
            extra = 19.2;
        }
        break;
    case 390:
        b = "cg19-3";
        if (extra < 19.3) {
            extra = 19.3;
        }
        break;
    case 391:
        b = "cg19-4";
        if (extra < 19.4) {
            extra = 19.4;
        }
        break;
    case 392:
        b = "cg20";
        if (extra < 20) {
            extra = 20;
        }
        updatemp3(player);
        break;
    case 403:
        b = "b14";
        va = 3;
        a51 = "a1-8";
        a52 = "a2-2-6";
        a53 = "a3-2-10";
        updatemp3(player);
        break;
    case 409:
        b = "b8";
        va = 4;
        a4 = "a1-1";
        break;
    case 412:
        b = "cg21";
        va = 0;
        if (extra < 21) {
            extra = 21;
        }
        break;
    case 419:
        b = "cg22";
        if (extra < 22) {
            extra = 22;
        }
        break;
    case 424:
        updatemp3(player);
        b = "cg23";
        if (extra < 23) {
            extra = 23;
        }
        break;
    case 438:
        b = "cg24";
        if (extra < 24) {
            extra = 24;
        }
        break;
    case 458:
        b = "cg25";
        if (extra < 25) {
            extra = 25;
        }
        break;
    case 463:
        b = "cg26";
        if (extra < 26) {
            extra = 26;
        }
        break;
    case 498:
        b = "cg27";
        if (extra < 27) {
            extra = 27;
        }
        break;
    case 499:
        b = "b17";
        break;
    case 527:
        b = "cg26";
        break;
    case 531:
        b = "b17";
        break;
    case 549:
        b = "cg26";
        break;
    case 550:
        b = "b15-1";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-1";
        break;
    case 551:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 552:
        b = "b15-2";
        va = 2;
        a21 = "a1-7";
        a22 = "a2-2-1";
        break;
    case 553:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 559:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 562:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 564:
        b = "b15-2";
        va = 2;
        a21 = "a1-7";
        a22 = "a2-2-1";
        break;
    case 565:
        b = "b15-2";
        va = 2;
        a21 = "a1-7";
        a22 = "a2-2-1";
        break;
    case 566:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 567:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 568:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-3";
        break;
    case 569:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 570:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 571:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 572:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 573:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 574:
        b = "b15-2";
        va = 2;
        a21 = "a1-7";
        a22 = "a2-2-1";
        break;
    case 575:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 576:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 578:
        b = "b15-2";
        va = 2;
        a21 = "a1-7";
        a22 = "a2-2-1";
        break;
    case 579:
        b = "b15-2";
        va = 2;
        a21 = "a1-7";
        a22 = "a2-2-1";
        break;
    case 580:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 586:
        b = "b15-2";
        va = 2;
        a21 = "a1-7";
        a22 = "a2-2-1";
        break;
    case 587:
        b = "b15-2";
        va = 2;
        a21 = "a1-7";
        a22 = "a2-2-1";
        break;
    case 588:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 589:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-3";
        break;
    case 590:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 591:
        b = "b15-2";
        va = 2;
        a21 = "a1-7";
        a22 = "a2-2-1";
        break;
    case 593:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-3";
        break;
    case 595:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 599:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 600:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-3";
        break;
    case 601:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 603:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-3";
        break;
    case 604:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 606:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-1";
        break;
    case 607:
        b = "b15-2";
        va = 2;
        a21 = "a1-6";
        a22 = "a2-2-3";
        break;
    case 608:
        b = "b15-2";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 610:
        b = "b15-2";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 613:
        b = "b15-3";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 614:
        b = "b15-3";
        va = 2;
        a21 = "a1-3";
        a22 = "a2-2-1";
        break;
    case 615:
        b = "b15-3";
        va = 2;
        a21 = "a1-3";
        a22 = "a2-2-1";
        break;
    case 616:
        b = "b15-3";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 620:
        b = "b15-3";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 623:
        b = "b15-3";
        va = 2;
        a21 = "a1-3";
        a22 = "a2-2-1";
        break;
    case 624:
        b = "b15-3";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 626:
        b = "b15-3";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 633:
        b = "b15-3";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 635:
        b = "b15-3";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 638:
        b = "b15-3";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-3";
        break;
    case 640:
        b = "b15-4";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 641:
        b = "b15-4";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-3";
        break;
    case 642:
        b = "b15-4";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 645:
        b = "b15-4";
        va = 2;
        a21 = "a1-3";
        a22 = "a2-2-1";
        break;
    case 646:
        b = "b15-4";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 647:
        b = "b15-4";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 649:
        b = "b15-4";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        updatemp3(player);
        break;
    case 651:
        b = "b15-4";
        va = 2;
        a21 = "a1-3";
        a22 = "a2-2-1";
        break;
    case 652:
        b = "b15-4";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-1";
        break;
    case 653:
        b = "b15-4";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-3";
        break;
    case 654:
        b = "b15-4";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-3";
        break;
    case 655:
        b = "b15-4";
        va = 2;
        a21 = "a1-3";
        a22 = "a2-2-2";
        break;
    case 657:
        b = "b15-4";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-3";
        break;
    case 658:
        b = "b15-4";
        va = 2;
        a21 = "a1-2";
        a22 = "a2-2-2";
        break;
    case 660:
        b = "cg22";
        va = 0;
        break;
    case 666:
        b = "cg28-1";
        if (extra < 28.1) {
            extra = 28.1;
        }
        break;
    case 669:
        b = "cg28-2";
        if (extra < 28.2) {
            extra = 28.2;
        }
        break;
    case 672:
        b = "cg28-1";
        updatemp3(player);
        break;
    case 676:
        bv = 0;
        break;
    case 677:
        bv = 1;
        b = "cg29";
        if (extra < 29) {
            extra = 29;
        }
        break;
    case 678:
        b = "cg30-1";
        if (extra < 30.1) {
            extra = 30.1;
        }
        break;
    case 684:
        b = "cg30-2";
        if (extra < 30.2) {
            extra = 30.2;
        }
        break;
    case 685:
        b = "cg30-3";
        if (extra < 30.3) {
            extra = 30.3;
        }
        break;
    case 687:
        dirn = 10 + 10 * cuowu;
        bv = 0;
        sja1x = 90;
        sja1y = 310;
        sja2x = 20;
        sja2y = 310;
        engine.rootContext()->setContextProperty("sja1x", 90);
        engine.rootContext()->setContextProperty("sja1y", 310);
        engine.rootContext()->setContextProperty("sja2x", 20);
        engine.rootContext()->setContextProperty("sja2y", 310);
        engine.rootContext()->setContextProperty("sjx", 0);
        dixy = sjstart(engine, dirn, 1);
        sjx = 0;
        break;
    case 692:
        bv = 0;
        sjsuo = 1;
        break;
    case 702:
        bv = 1;
        b = "cg31";
        if (extra < 31) {
            extra = 31;
        }
        updatemp3(player);
        sjvisible = 0;
        ditu1visible = 0;
        ditu2visible = 0;
        engine.rootContext()->setContextProperty("sjvisible", sjvisible);
        engine.rootContext()->setContextProperty("ditu1visible", ditu1visible);
        engine.rootContext()->setContextProperty("ditu2visible", ditu2visible);
        break;
    case 710:
        bv = 0;
        dirn = 30 + 20 * cuowu;
        sja1x = 90;
        sja1y = 310;
        sja2x = 20;
        sja2y = 310;
        engine.rootContext()->setContextProperty("sja1x", 90);
        engine.rootContext()->setContextProperty("sja1y", 310);
        engine.rootContext()->setContextProperty("sja2x", 20);
        engine.rootContext()->setContextProperty("sja2y", 310);
        engine.rootContext()->setContextProperty("sjx", 0);
        dixy = sjstart(engine, dirn, 2);
        sjx = 0;
        updatemp3(player);
        break;
    case 722:
        sjsuo = 1;
        bv = 0;
        break;
    case 755:
        bv = 0;
        engine.rootContext()->setContextProperty("fsa1visible", 1);
        engine.rootContext()->setContextProperty("fsa2visible", 1);
        engine.rootContext()->setContextProperty("sja1theta", 0);
        engine.rootContext()->setContextProperty("sja2theta", 3.1415);
        QTimer::singleShot(200, [&engine]() {
            engine.rootContext()->setContextProperty("fsa1visible", 0);
            engine.rootContext()->setContextProperty("fsa2visible", 0);
            });
        break;
    case 756:
        bv = 0;
        sjsuo = 1;
        break;
    case 761:
        bv = 1;
        b = "cg32-1";
        if (extra < 32.1) {
            extra = 32.1;
        }
        updatemp3(player);
        sjvisible = 0;
        ditu1visible = 0;
        ditu2visible = 0;
        engine.rootContext()->setContextProperty("sjvisible", sjvisible);
        engine.rootContext()->setContextProperty("ditu1visible", ditu1visible);
        engine.rootContext()->setContextProperty("ditu2visible", ditu2visible);
        break;
    case 798:
        b = "cg32-2";
        break;
        if (extra < 32.2) {
            extra = 32.2;
        }
    case 803:
        b = "cg32-3";
        QTimer::singleShot(200, [&engine]() {
            engine.rootContext()->setContextProperty("bVisible", 0);
            });
        if (extra < 32.3) {
            extra = 32.3;
        }
        break;
    case 804:
        bv = 0;
        break;
    case 805:
        bv = 1;
        b = "cg33-1";
        if (extra < 33.1) {
            extra = 33.1;
        }
        break;
    case 807:
        b = "cg33-2";
        if (extra < 33.2) {
            extra = 33.2;
        }
        break;
    case 808:
        b = "cg34";
        if (extra < 34) {
            extra = 34;
        }
        break;
    case 812:
        bv = 0;
        break;
    case 815:
        bv = 1;
        b = "b15-1";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-1";
        break;
    case 816:
        b = "b15-1";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-2";
        break;
    case 817:
        bv = 1;
        b = "b15-1";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-2-1";
        break;
    case 818:
        bv = 1;
        b = "b15-1";
        va = 2;
        a21 = "a1-12";
        a22 = "a2-2-9";
        break;
    case 819:
        bv = 0;
        va = 0;
        break;
    case 832:
        bv = 1;
        b = "cg35";
        if (extra < 35) {
            extra = 35;
        }
        updatemp3(player);
        break;
    case 844:
        b = "cg36";
        if (extra < 36) {
            extra = 36;
        }
        break;
    case 846:
        bv = 0;
        break;
    case 847:
        bv = 1;
        b = "b2";
        break;
    case 848:
        bv = 1;
        b = "b2";
        va = 2;
        a21 = "a2-2-10";
        a22 = "a-13";
        break;
    case 853:
        bv = 0;
        va = 0;
        break;
    case 856:
        bv = 1;
        b = "bcg1";
        updatemp3(player);
        break;
    case 864:
        bv = 0;
        va = 0;
        break;
    case 867:
        bv = 1;
        b = "bcg1";
        updatemp3(player);
        break;
    case 876:
        bv = 1;
        b = "bcg2";
        va = 0;
        updatemp3(player);
        break;
    case 889:
        bv = 0;
        break;
    case 891:
        bv = 1;
        b = "bcg3";
        break;
    case 898:
        bv = 0;
        va = 0;
        updatemp3(player);
        break;
    case 906:
        bv = 1;
        b = "bcg3";
        break;
    case 913:
        bv = 1;
        va = 0;
        b = "bcg4";
        updatemp3(player);
        break;
    case 927:
        b = "bcg5";
        break;
    case 928:
        b = "bcg6";
        break;
    case 929:
        b = "bcg7";
        break;
    case 930:
        b = "bcg8";
        break;
    case 932:
        b = "b15-1";
        break;
    case 935:
        b = "bcg9";
        QTimer::singleShot(200, [&engine]() {
            engine.rootContext()->setContextProperty("bVisible", 0);
            });
        break;
    case 936:
        bv = 0;
        break;
    case 938:
        bv = 1;
        b = "bcg9";
        updatemp3(player);
        QTimer::singleShot(200, [&engine]() {
            engine.rootContext()->setContextProperty("bVisible", 0);
            });
        va = 0;
        break;
    case 939:
        bv = 0;
        break;
    case 941:
        bv = 0;
        va = 0;
        updatemp3(player);
        break;
    case 944:
        bv = 1;
        b = "b15-1";
        va = 0;
        break;
    case 949:
        bv = 1;
        b = "bcg9";
        break;
    case 950:
        bv = 1;
        b = "b16";
        va = 4;
        a4 = "a1-2";
        break;
    case 951:
        bv = 1;
        b = "b16";
        va = 4;
        a4 = "a1-1";
        break;
    case 952:
        bv = 1;
        b = "b16";
        va = 4;
        a4 = "a1-2";
        break;
    case 953:
        bv = 1;
        b = "b16";
        va = 4;
        a4 = "a1-1";
        break;
    case 955:
        bv = 1;
        b = "b16";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-1-2";
        break;
    case 956:
        bv = 1;
        b = "b16";
        va = 2;
        a21 = "a1-1";
        a22 = "a2-1-1";
        a = alinshi - 1;
        sjTimer->start(10);
        break;
    case 958:
        bv = 1;
        b = "b16";
        va = 2;
        a21 = "a2-1-1";
        a22 = "a1-1";
        break;
    case 961:
        bv = 1;
        b = "b16";
        va = 2;
        a21 = "a2-1-1";
        a22 = "a1-2";
        break;
    case 962:
        bv = 1;
        b = "b16";
        va = 2;
        a21 = "a2-1-2";
        a22 = "a1-1";
        a = alinshi - 1;
        sjTimer->start(10);
        break;
    case 964:
        bv = 0;
        va = 0;
        a = alinshi - 1;
        sjTimer->start(10);
    case 966:
        bv = 0;
        va = 0;
        a = alinshi - 1;
        sjTimer->start(10);
    case 968:
        bv = 1;
        va = 0;
        b = "b2";
        break;
    case 970:
        bv = 1;
        b = "b2";
        va = 2;
        a21 = "a2-2-10";
        a22 = "a-13";
        break;
    case 973:
        bv = 0;
        va = 0;
        updatemp3(player);
    default:
        if (load) {
            bv = 1;
            bh--;
            va = 0;
            goto bshuaxin;
        }
    }
    if (dacuo==1) {
        va = 2;
        a21 = "a1-6";
        a22 = "a2-2-3";
    }
    if ((a >= 687) && (a < 702)) {
        sj = 1;
        va = 0;
        if ((sjvisible == 0) || (ditu1visible == 0)) {
            sjvisible = 1;
            ditu1visible = 1;
            ditu2visible = 0;
            engine.rootContext()->setContextProperty("ditu1visible", ditu1visible);
            engine.rootContext()->setContextProperty("ditu2visible", ditu2visible);
            engine.rootContext()->setContextProperty("sjvisible", sjvisible);
        }
        sjvisible = 1;
        ditu1visible = 1;
        ditu2visible = 0;
        for (int divi = 0; divi < dirn * 2; divi++) {
            dirv += QVariant(0);
        }
    }
    else if ((a >= 710) && (a < 761)) {
        sj = 2;
        va = 0;
        if ((sjvisible == 0) || (ditu2visible == 0)) {
            sjvisible = 1;
            ditu1visible = 0;
            ditu2visible = 1;
            engine.rootContext()->setContextProperty("ditu1visible", ditu1visible);
            engine.rootContext()->setContextProperty("ditu2visible", ditu2visible);
            engine.rootContext()->setContextProperty("sjvisible", sjvisible);
        }
        sjvisible = 1;
        ditu1visible = 0;
        ditu2visible = 1;
        for (int divi = 0; divi < dirn * 2; divi++) {
            dirv += QVariant(0);
        }
    }
    else {
        sj = 0;
        if (sjvisible == 1) {
            engine.rootContext()->setContextProperty("sjvisible", 0);
            ditu1visible = 0;
            ditu2visible = 0;
            engine.rootContext()->setContextProperty("ditu1visible", ditu1visible);
            engine.rootContext()->setContextProperty("ditu2visible", ditu2visible);
        }
    }
    engine.rootContext()->setContextProperty("bVisible",bv);
    engine.rootContext()->setContextProperty("bcPath", b); 
    switch (va) {
    case 1:
        a1v = 1;
        a2v = 0;
        a3v = 0;
        a1fv = 0;
        a3fv = 0;
        break;
    case 2:
        a1v = 0;
        a2v = 1;
        a3v = 0;
        a1fv = 0;
        a3fv = 0;
        break;
    case 3:
        a1v = 0;
        a2v = 0;
        a3v = 1;
        a1fv = 0;
        a3fv = 0;
        break;
    case 4:
        a1v = 0;
        a2v = 0;
        a3v = 0;
        a1fv = 1;
        a3fv = 0;
        break;
    case 5:
        a1v = 0;
        a2v = 0;
        a3v = 0;
        a1fv = 0;
        a3fv = 1;
        break;
    default:
        a1v = 0;
        a2v = 0;
        a3v = 0;
        a1fv = 0;
        a3fv = 0;
        break;
        if ((a == 664)&&(cuowu>6)) {
            a = 875;
        }
    }
    engine.rootContext()->setContextProperty("a1visible", a1v);
    engine.rootContext()->setContextProperty("a2visible", a2v);
    engine.rootContext()->setContextProperty("a3visible", a3v);
    engine.rootContext()->setContextProperty("a1fvisible", a1fv);
    engine.rootContext()->setContextProperty("a3fvisible", a3fv);
    engine.rootContext()->setContextProperty("a1Path", a1);
    engine.rootContext()->setContextProperty("a2_1Path", a21);
    engine.rootContext()->setContextProperty("a2_2Path", a22);
    engine.rootContext()->setContextProperty("a3_1Path", a31);
    engine.rootContext()->setContextProperty("a3_2Path", a32);
    engine.rootContext()->setContextProperty("a3_3Path", a33);
    engine.rootContext()->setContextProperty("a1fPath", a4);
    engine.rootContext()->setContextProperty("a3_1fPath", a51);
    engine.rootContext()->setContextProperty("a3_2fPath", a52);
    engine.rootContext()->setContextProperty("a3_3fPath", a53);
}
void gotoa(int da) {
    if (da == 1) {
        switch (a) {
        case 261:
            a = 263;
            break;
        case 406:
            a = 408;
            break;
        case 521:
            a = 523;
            break;
        case 533:
            dacuo = 1;
            cuowu++;
            a = 532;
            break;
        case 539:
            dacuo = 1;
            cuowu++;
            a = 538;
            break;
        case 563:
            a = 565;
            break;
        case 577:
            dacuo = 1;
            cuowu++;
            a = 576;
            break;
        case 585:
            a = 587;
            break;
        case 613:
            dacuo = 1;
            cuowu++;
            a = 612;
            break;
        case 667:
            a = 912;
            break;
        case 673:
            a = 675;
            break;
        case 695:
            a = 697;
            break;
        case 841:
            a = 843;
            break;
        }
    }
    else if (da == 2) {
        switch (a) {
        case 261:
            a = 852;
            break;
        case 406:
            a = 863;
            break;
        case 521:
            dacuo = 1;
            cuowu++;
            a = 520;
            break;
        case 533:
            a = 535;
            break;
        case 539:
            a = 541;
            break;
        case 563:
            dacuo = 1;
            cuowu++;
            a = 562;
            break;
        case 577:
            a = 579;
            break;
        case 585:
            dacuo = 1;
            cuowu++;
            a = 584;
            break;
        case 613:
            a = 615;
            break;
        case 667:
            a = 669;
            break;
        case 673:
            a = 897;
            break;
        case 681:
            a = 684;
            break;
        case 695:
            a = 897;
            break;
        case 841:
            a = 967;
            break;
        }
    }
}
void wuli(float& vx,float& vy, int &y) {
    if ((vx > 1) && (y > 305) && (y <= 311)) {
        vx = vx - 0.5;
    }
    else if ((vx < -1) && (y > 305)&&(y<=311)) {
        vx = vx + 0.5;
    }
    else if ((y > 305) && (y <= 311) && (vx <= 1) && (vx >= -1)) {
        vx = 0.5 * vx;
    }else if ((vx > 1) && (y > 311) && (y < 320)) {
        vx = vx - 1.2;
    }
    else if ((vx < -1) && (y > 311) && (y < 320)) {
        vx = vx + 1.2;
    }
    else if ((y > 311) && (y < 320) && (vx <= 1) && (vx >= -1)) {
        vx = 0.1 * vx;
    }
    if (y <= 305) {
        vy = vy + 0.5;
    }
    else if ((305 < y) && (y < 309)) {
        vy = vy + 0.2;
    }
    else if ((y > 311) && (y <= 320)) {
        vy = vy - 0.3;
    }
    else if (y > 320) {
        y = 320;
        vy = -0.5;
    }
    else {
        vy = 0.1 * vy;
    }
}
void wulid(float& vx, float& vy, int& y) {
    if ((vx > 1) && (y > 325) && (y <= 331)) {
        vx = vx - 0.5;
    }
    else if ((vx < -1) && (y > 325) && (y <= 331)) {
        vx = vx + 0.5;
    }
    else if ((y > 325) && (y <= 331) && (vx <= 1) && (vx >= -1)) {
        vx = 0.5 * vx;
    }
    if (y <= 325) {
        vy = vy + 0.5;
    }
    else if ((325 < y) && (y < 330)) {
        vy = vy + 0.2;
    }
    else if (y > 330) {
        y = 330;
        vy = -0.5;
    }
    else {
        vy = 0.1 * vy;
    }
}
int main(int argc, char* argv[])
{
    int fuhuo = 3;
    int fuhuo1 = 0;
    int fuhuo2 = 0;
    int suofang = 3;
    std::string vzistr=sread(2);
    std::string vhangstr = sread(3);
    std::string inliangstr = sread(4);
    float vzi = std::stof(vzistr);
    float vhang = std::stof(vhangstr);
    float inliang = std::stof(inliangstr);
    fileContent = new std::vector<std::string>();
    std::ifstream file(aFilePath);
    if (file.is_open()) {
        std::string line;
        while (std::getline(file, line)) {
            fileContent->push_back(line);
        }
        file.close();
    }
    if (!sjTimer) {
        sjTimer = new QTimer();
    }
    int scalingApplied = 0;
    std::string content = sread(1);
    scalingApplied = (content == "1") + 2 * (content == "2"); // 1表示缩放已应用
    double scaleFactorValue = calculateScaleFactor(scalingApplied);
    char scaleFactorStr[10];
    snprintf(scaleFactorStr, sizeof(scaleFactorStr), "%.2f", scaleFactorValue);
    qputenv("QT_SCALE_FACTOR", scaleFactorStr);
    QFile efile(eFilePath);
    efile.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream ein(&efile);
    QString eline;
    // 读取所有行
    while (!ein.atEnd()) {
        eline = ein.readLine();
    }
    efile.close();
    extra=eline.toFloat();
    
#if defined(Q_OS_WIN) && QT_VERSION_CHECK(5, 6, 0) <= QT_VERSION && QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);
    app.setWindowIcon(QIcon("favicon.ico"));
    MainWindow Controller;
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("Controller", &Controller);
    engine.rootContext()->setContextProperty("Vzi", vzi);
    engine.rootContext()->setContextProperty("Vhang", vhang);
    engine.rootContext()->setContextProperty("Inliang", inliang);
    QString exepath= QDir::currentPath();
    engine.rootContext()->setContextProperty("exePath", exepath);
    engine.rootContext()->setContextProperty("bVisible", 0);
    engine.rootContext()->setContextProperty("daanvisible", 0); 
    engine.rootContext()->setContextProperty("shuruvisible", 0);
    engine.rootContext()->setContextProperty("a1visible", 0);
    engine.rootContext()->setContextProperty("a2visible", 0);
    engine.rootContext()->setContextProperty("a3visible", 0);
    engine.rootContext()->setContextProperty("a1fvisible", 0);
    engine.rootContext()->setContextProperty("a3fvisible", 0);
    engine.rootContext()->setContextProperty("sjvisible", 0);
    engine.rootContext()->setContextProperty("ditu1visible", 0);
    engine.rootContext()->setContextProperty("ditu2visible", 0);
    QMediaPlayer* player = new QMediaPlayer;
    player->setMedia(QUrl::fromLocalFile("mp3/universe.mp3"));
    player->setVolume(inliang*100);
    player->play();        
    QObject::connect(&Controller, &MainWindow::suofangChanged,
        [&suofang](int newValue) {
            suofang = newValue;
            if (suofang != 3) {
                //qDebug() << "suofang=" << suofang;
                double scaleFactorValue = calculateScaleFactor(suofang);
                char scaleFactorStr[10];
                snprintf(scaleFactorStr, sizeof(scaleFactorStr), "%.2f", scaleFactorValue);
                qputenv("QT_SCALE_FACTOR", scaleFactorStr);
                // 重启
                QTimer::singleShot(100, []() {
                    //qDebug() << "触发应用程序重启";
                    restartApplication();
                    });;
            }
        });
    QObject::connect(&Controller, &MainWindow::exitChanged,
        [](int exit) {
            if (exit == 1) {
                QCoreApplication::quit();
            }
        });;
    QObject::connect(&Controller, &MainWindow::nextChanged,
        [&engine,&vzi, &Controller,&player](int next) {
            //qDebug() << "Button: Next changed signal received:" << newnext;
            if (next == 1) {
                if (!(qastop||(a==551))) {
                    if (dacuo) {
                        updateDisplayText(engine, 851, vzi);
                        updateb(engine,851,player);
                        dacuo = 0;
                    }
                    else if (gameexit) {
                        Controller.navigateindex();
                    }
                    else if(sjsuo == 0) {
                        a++;
                        hang = a;
                        updateDisplayText(engine, hang, vzi);
                        updateb(engine,851,player);
                        //qDebug() << "textreturn" << textreturn; 
                    }
                }
            }
        });
    QObject::connect(&Controller, &MainWindow::Start,
        [&engine,&fuhuo,&fuhuo1,&fuhuo2,&player](int start) {
            if (start == 1) {
                a = 0;
                engine.clearComponentCache();
                engine.rootContext()->setContextProperty("currentText", "");
                engine.rootContext()->setContextProperty("nameText", "");
                engine.rootContext()->setContextProperty("bVisible", 0);
                engine.rootContext()->setContextProperty("daanvisible", 0);
                engine.rootContext()->setContextProperty("shuruvisible", 0);
                fuhuo = 3;
                fuhuo1 = 0;
                fuhuo2 = 0;
                cuowu = 0;
            }
        });
    QObject::connect(&Controller, &MainWindow::toindex,
        [&engine, &player](int toindex) {
            if (toindex == 1) {
                player->stop();
                player->setMedia(QUrl::fromLocalFile("mp3/Universe.mp3"));
                player->play();
            }
        });
    static int bgmstop = 0;
    QObject::connect(&Controller, &MainWindow::inbextraChanged,
        [&engine,&player](int inbextra) {
            if (inbextra == 0) {
                bgmstop = 1;
                player->stop();
            }else{
                bgmstop = 0;
                player->play();
            }
        });
    engine.rootContext()->setContextProperty("texte", "\xe5\xae\x87\xe5\xae\x99");
    QObject::connect(&Controller, &MainWindow::inextraChanged,
        [&engine,&player](int inextra) {
            qDebug() << inextra;
            static int extrai = 1;
            extrai = extrai + inextra;
            if (extrai < 0) {
                extrai = extrai + 8;
            }
            extrai = extrai % 8;
            switch (extrai) {
            case 0:
                player->stop();
                player->setMedia(QUrl::fromLocalFile("mp3/Universe.mp3"));
                player->play();
                engine.rootContext()->setContextProperty("texte", "\xe5\xae\x87\xe5\xae\x99");
                break;
            case 1:
                player->stop();
                player->setMedia(QUrl::fromLocalFile("mp3/Memories.mp3"));
                player->play();
                engine.rootContext()->setContextProperty("texte", "\xe5\x9b\x9e\xe5\xbf\x86");
                break;
            case 2:
                player->stop();
                player->setMedia(QUrl::fromLocalFile("mp3/Playing_Together_With_You.mp3"));
                player->play();
                engine.rootContext()->setContextProperty("texte", "\xe4\xb8\x80\xe8\xb5\xb7\xe7\x8e\xa9");
                break;
            case 3:
                player->stop();
                player->setMedia(QUrl::fromLocalFile("mp3/Playing_Around_The_World.mp3"));
                player->play();
                engine.rootContext()->setContextProperty("texte", "\xe6\xb8\xb8\xe4\xb8\x96\xe7\x95\x8c");
                break;
            case 4:
                player->stop();
                player->setMedia(QUrl::fromLocalFile("mp3/The_Adventure.mp3"));
                player->play();
                engine.rootContext()->setContextProperty("texte", "\xe5\x8e\x86\xe9\x99\xa9");
                break;
            case 5:
                player->stop();
                player->setMedia(QUrl::fromLocalFile("mp3/Civilization.mp3"));
                player->play();
                engine.rootContext()->setContextProperty("texte", "\xe6\x96\x87\xe6\x98\x8e");
                break;
            case 6:
                player->stop();
                player->setMedia(QUrl::fromLocalFile("mp3/Farewell.mp3"));
                player->play();
                engine.rootContext()->setContextProperty("texte", "\xe5\x86\x8d\xe8\xa7\x81");
                break;
            case 7:
                player->stop();
                player->setMedia(QUrl::fromLocalFile("mp3/The_Stars_Shall_Never_Forget.mp3"));
                player->play();
                engine.rootContext()->setContextProperty("texte", "\xe7\xbe\xa4\xe6\x98\x9f\xe6\xb0\xb8\xe4\xb8\x8d\xe5\xbf\x98\xe8\xae\xb0");
                break;
            }
        });
    QObject::connect(player, &QMediaPlayer::stateChanged, [&player](QMediaPlayer::State state) {
        if ((state == QMediaPlayer::StoppedState)&&(bgmstop!=1)) {
            player->play();
        }
        });
    QObject::connect(&Controller, &MainWindow::AutoChanged,
        [&engine, &vzi,&vhang, &Controller,&player](int Auto) {
            static QTimer* refreshTimer = nullptr;
            static int textreturn = 0; // 显示状态

            if (refreshTimer) {
                refreshTimer->stop();
                refreshTimer->deleteLater();
                refreshTimer = nullptr;
            }

            if (Auto) {
                refreshTimer = new QTimer();
                refreshTimer->setInterval(500/(vhang+0.00001)); // 100ms间隔
                textreturn = 0;
                static int Autostart = 1;//固定地址防止内存出错
                Autostart = 1;

                QObject::connect(refreshTimer, &QTimer::timeout, [&]() {
                    // 执行刷新操作
                    if (Autostart || textreturn) {
                        if (qastop || (a == 551)) {

                            Auto = 0;
                            refreshTimer->stop();
                            refreshTimer->deleteLater();
                            refreshTimer = nullptr;
                            return;
                        }
                        else if (dacuo) {
                            updateDisplayText(engine, 851, vzi);
                            updateb(engine, 851,player);
                            dacuo = 0;
                        }else if (gameexit) {
                            Controller.navigateindex();
                        }
                        else if(sjsuo==0){
                            a++;
                            hang = a;
                            updateDisplayText(engine, hang, vzi);
                            updateb(engine,a,player);
                            Autostart = 0;
                        }
                    }

                    // 如果显示未完成，暂停刷新
                    if (textreturn == 0) {
                        refreshTimer->stop();
                        // 使用单次定时器检查状态
                        QTimer::singleShot((50 / (vhang + 0.00001)), [&]() {
                            // 检查中止条件和定时器有效性
                            if (!refreshTimer) return;
                            refreshTimer->start();
                            });
                    }
                    });

                // 启动定时器
                refreshTimer->start();
            }
        });
    QObject::connect(&Controller, &MainWindow::skipChanged,
        [&engine, &vzi,&Controller,&player](int skip) {
            static QTimer* refreshTimer = nullptr;

            // 清理现有定时器
            if (refreshTimer) {
                refreshTimer->stop();
                refreshTimer->deleteLater();
                refreshTimer = nullptr;
            }

            if (skip) {
                refreshTimer = new QTimer();
                refreshTimer->setInterval(100); // 100ms间隔

                QObject::connect(refreshTimer, &QTimer::timeout, [&]() {
                    // 检查中止条件

                    // 简单循环执行核心操作
                    if (qastop || (a == 551)|| ((a >= 687) && (a < 702))|| (a >= 710) && (a < 761)) {
                        updateb(engine,a,player);

                        skip = 0;
                        refreshTimer->stop();
                        refreshTimer->deleteLater();
                        refreshTimer = nullptr;
                        return;
                    }
                    else if (dacuo) {
                        updateDisplayText(engine, 851, vzi);
                        updateb(engine, 851,player);
                        dacuo = 0;
                    }
                    else if(gameexit){
                        Controller.navigateindex();
                    }else if(sjsuo == 0) {
                        a++;
                        hang = a;
                        udText(engine, hang);
                        updateb(engine,a,player);
                    }
                    });

                // 启动定时器
                refreshTimer->start();
            }
        });
    QObject::connect(&Controller, &MainWindow::vziChanged,
        [&engine, &vzi](float newvzi) {
            if (newvzi != vzi) {
                vzi = newvzi;
                swrite(std::to_string(newvzi), 2);
                engine.rootContext()->setContextProperty("Vzi", vzi);
            }
        }
    );
    QObject::connect(&Controller, &MainWindow::vhangChanged,
        [&engine, &vhang](float newvhang) {
            if (newvhang != vhang) {
                vhang = newvhang;
                swrite(std::to_string(newvhang), 3);
                engine.rootContext()->setContextProperty("Vhang", vhang);
            }
        }
    );
    QObject::connect(&Controller, &MainWindow::inliangChanged,
        [&engine, &inliang,&player](float newinliang) {
            if (newinliang != inliang) {
                inliang = newinliang;
                //qDebug() << newinliang;
                swrite(std::to_string(newinliang), 4);
                engine.rootContext()->setContextProperty("Inliang", inliang);
                player->setVolume(inliang * 100);
            }
        }
    );
    QObject::connect(&Controller, &MainWindow::saveChanged,
        [](int save) {
            if (save == 1) {
                QFile file(slFilePath);
                QFile file2(s2FilePath);
                // 尝试以追加模式打开文件
                if (file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
                    QTextStream out(&file);
                    if((a>=948)&&(a<967)){
                        a = alinshi;
                    }
                    if ((a >= 687) && (a < 702)) {
                        out << 687 << "\n";
                    }
                    else if ((a >= 710) && (a < 761)) {
                        out << 710 << "\n";
                    }
                    else {
                        out << a << "\n";
                    }
                    file.close();
                }
                if (file2.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
                    QTextStream out2(&file2);
                    out2 << cuowu << "\n";
                    file2.close();
                }
                QDir saveDir("saves");
                // 获取主窗口
                QList<QWindow*> windows = QGuiApplication::topLevelWindows();

                QWindow* mainWindow = windows.first();
                if (!mainWindow->isVisible()) {
                    qWarning() << "主窗口不可见";
                    return;
                }
                QTimer::singleShot(200, []() {
                    
                    });
                // 获取屏幕
                QScreen* screen = mainWindow->screen();
                if (!screen) {
                    screen = QGuiApplication::primaryScreen();
                }

                if (!screen) {
                    qWarning() << "无法获取屏幕";
                    return;
                }

                // 截取窗口
                QPixmap screenshot = screen->grabWindow(mainWindow->winId());
                if (screenshot.isNull()) {
                    qWarning() << "截图失败";
                    return;
                }

                // 查找最大编号
                int maxNumber = 0;
                QStringList nameFilters;
                nameFilters << "*.png";
                QFileInfoList fileList = saveDir.entryInfoList(nameFilters, QDir::Files, QDir::Name);

                for (const QFileInfo& fileInfo : fileList) {
                    QString fileName = fileInfo.baseName();
                    bool ok;
                    int fileNumber = fileName.toInt(&ok);
                    if (ok) {
                        if (fileNumber > maxNumber) {
                            maxNumber = fileNumber;
                        }
                    }
                    else {
                        qDebug() << "忽略非数字文件:" << fileName;
                    }
                }

                // 计算新文件名
                int newNumber = maxNumber + 1;
                QString savePath = saveDir.absoluteFilePath(QString("%1.png").arg(newNumber));

                // 保存截图
                if (screenshot.save(savePath, "PNG")) {
                    qDebug() << "截图保存成功:" << savePath;
                }
                else {
                    qWarning() << "截图保存失败:" << savePath;
                }

                qDebug() << "===== 保存流程完成 =====";
                save = 0;
                }
                QFile efile(eFilePath);
                if (efile.open(QIODevice::WriteOnly | QIODevice::Text)) {
                    QTextStream eout(&efile);
                    eout << extra;
                    efile.close();
                }
            });
    QObject::connect(&Controller, &MainWindow::Load,
        [&engine,&vzi,&fuhuo,&fuhuo1,&fuhuo2,&player](int loadn) {
            QFile file(slFilePath);
            QFile file2(s2FilePath);
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                QString error = QString("无法打开存档文件: %1").arg(file.errorString());
                qWarning() << error;
                return;
            }
            if (!file2.open(QIODevice::ReadOnly | QIODevice::Text)) {
                QString error = QString("无法打开存档文件: %1").arg(file2.errorString());
                qWarning() << error;
                return;
            }
            QTextStream in(&file);
            QStringList lines;
            int totalLines = 0;

            QTextStream in2(&file2);
            QStringList lines2;
            int totalLines2 = 0;

            // 读取所有行
            while (!in.atEnd()) {
                QString line = in.readLine();
                lines.append(line);
                totalLines++;
            }
            file.close();
            while (!in2.atEnd()) {
                QString line2 = in2.readLine();
                lines2.append(line2);
                totalLines2++;
            }
            file2.close();

            // 计算索引
            int index = totalLines - loadn;
            if (index < 0 || index >= totalLines) {
                QString error = QString("存档位置超出范围: %1 (总存档数: %2)").arg(loadn).arg(totalLines);
                return;
            }

            QString saveData = lines[index].trimmed();
            qDebug() << "找到存档内容[" << loadn  << saveData;

            // 转换为整数
            bool ok;
            int value = saveData.toInt(&ok);

            if (!ok) {
                QString error = QString("存档内容不是有效的整数: '%1'").arg(saveData);
                qWarning() << error;
                return;
            }
            // 计算索引
            int index2 = totalLines2 - loadn;
            if (index2 < 0 || index2 >= totalLines2) {
                QString error = QString("存档位置超出范围: %1 (总存档数: %2)").arg(loadn).arg(totalLines2);
                return;
            }

            QString saveData2 = lines2[index2].trimmed();
            qDebug() << "找到存档内容[" << loadn << saveData2;

            // 转换为整数
            bool ok2;
            int value2 = saveData2.toInt(&ok2);

            if (!ok2) {
                QString error = QString("存档内容不是有效的整数: '%1'").arg(saveData2);
                qWarning() << error;
                return;
            }

            // 赋值给全局变量 a
            a = value;
            cuowu = value2;

            // 更新显示
            hang = a;
            updateDisplayText(engine, hang, vzi);
            engine.rootContext()->setContextProperty("daanvisible", 0);
            engine.rootContext()->setContextProperty("shuruvisible", 0);
            updateb(engine, a,player,1);
            fuhuo = 3;
            fuhuo1 = 0;
            fuhuo2 = 0;
        });
    QObject::connect(&Controller, &MainWindow::Drop, 
        [&engine,&Controller](int dropn) {
            // 1. 删除文本文件中的指定行
            QFile textFile(slFilePath);
            QFile textFile2(s2FilePath);

            if(textFile.open(QIODevice::ReadWrite | QIODevice::Text)) {
                // 读取原始内容
                QString allText = textFile.readAll();
                QStringList lines = allText.split("\n", Qt::SkipEmptyParts);

                qDebug() << "原始行数:" << lines.size();

                // 删除指定行
                if (dropn >= 0 && dropn <= lines.size()) {
                    lines.removeAt(dropn-1);
                    qDebug() << "删除后行数:" << lines.size();

                    // 清空文件并重新写入
                    textFile.resize(0);
                    textFile.seek(0);
                    QTextStream out(&textFile);
                    out << lines.join("\n");

                    // 确保文件以换行结束（如果需要）
                    if (lines.size() > 0) {
                        out << "\n";
                    }

                    textFile.flush();
                }
                else {
                    qWarning() << "无效的删除位置:" << dropn << "有效范围:0-" << lines.size() - 1;
                }
                textFile.close();

                // 验证文件内容
                if (QFile::exists(slFilePath)) {
                    QFile verifyFile(slFilePath);
                    if (verifyFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
                        qDebug() << "更新后的文件内容:";
                        while (!verifyFile.atEnd()) {
                            qDebug() << verifyFile.readLine().trimmed();
                        }
                        verifyFile.close();
                    }
                }
            }
            if (textFile2.open(QIODevice::ReadWrite | QIODevice::Text)) {
                // 读取原始内容
                QString allText2 = textFile2.readAll();
                QStringList lines2 = allText2.split("\n", Qt::SkipEmptyParts);

                qDebug() << "原始行数:" << lines2.size();

                // 删除指定行
                if (dropn >= 0 && dropn <= lines2.size()) {
                    lines2.removeAt(dropn - 1);
                    qDebug() << "删除后行数:" << lines2.size();

                    // 清空文件并重新写入
                    textFile2.resize(0);
                    textFile2.seek(0);
                    QTextStream out2(&textFile2);
                    out2 << lines2.join("\n");

                    // 确保文件以换行结束（如果需要）
                    if (lines2.size() > 0) {
                        out2 << "\n";
                    }

                    textFile2.flush();
                }
                else {
                    qWarning() << "无效的删除位置:" << dropn << "有效范围:0-" << lines2.size() - 1;
                }
                textFile2.close();

                // 验证文件内容
                if (QFile::exists(s2FilePath)) {
                    QFile verifyFile2(s2FilePath);
                    if (verifyFile2.open(QIODevice::ReadOnly | QIODevice::Text)) {
                        qDebug() << "更新后的文件内容:";
                        while (!verifyFile2.atEnd()) {
                            qDebug() << verifyFile2.readLine().trimmed();
                        }
                        verifyFile2.close();
                    }
                }
            }

            // 2. 删除对应的图片文件
            QString imagePath = QString("saves/%1.png").arg(dropn);
            QFile imageFile(imagePath);
            if (imageFile.exists()) {
                if (!imageFile.remove()) {
                    qWarning() << "无法删除图片文件:" << imagePath;
                }
            }

            // 3. 重命名后续图片文件
            QDir saveDir("saves");

            // 获取所有图片文件
            QStringList nameFilters;
            nameFilters << "*.png";
            QFileInfoList fileList = saveDir.entryInfoList(nameFilters, QDir::Files);

            // 创建从文件名到文件信息的映射
            QMap<int, QString> fileMap;
            for (const QFileInfo& fileInfo : fileList) {
                bool ok;
                int num = fileInfo.baseName().toInt(&ok);
                if (ok) {
                    fileMap.insert(num, fileInfo.absoluteFilePath());
                }
            }

            // 重命名
            for (int currentNum = dropn + 1; ; currentNum++) {
                if (!fileMap.contains(currentNum)) {
                    // 没有更大编号的文件了，停止处理
                    break;
                }

                QString oldPath = fileMap.value(currentNum);
                QString newPath = QString("saves/%1.png").arg(currentNum - 1);

                QFile oldFile(oldPath);
                if (oldFile.exists()) {
                    // 确保目标文件不存在（可能已被删除）
                    QFile::remove(newPath);

                    if (oldFile.rename(newPath)) {
                        qDebug() << "重命名成功:" << oldPath << "->" << newPath;
                    }
                    else {
                        qWarning() << "重命名失败:" << oldPath << "->" << newPath;
                    }
                }
            }
            Controller.requestSaveCount();
        });
    QObject::connect(&Controller, &MainWindow::wheelChanged,
        [&engine,&vzi, &Controller,&player,&inliang](int udwheel) {
            if ((udwheel == 1)&&(hang>1)) {
                hang--;
                udText(engine, hang);
            }
            else if (udwheel == -1) {
                if (hang < a) {
                    hang++;
                    udText(engine, hang);
                }
                else if(!(qastop || (a == 551))) {
                    if (dacuo) {
                        updateDisplayText(engine, 851, vzi);
                        updateb(engine, 851,player);
                        dacuo = 0;
                    }
                    else if (gameexit) {
                        Controller.navigateindex();
                    }
                    else if (sjsuo == 0) {
                        a++;
                        hang = a;
                        updateDisplayText(engine, hang, vzi);
                        updateb(engine, a,player);
                    }
                }
            }
        });
    QObject::connect(&Controller, &MainWindow::daChanged,
        [&engine, &vzi,&player,&inliang](int da) {
            engine.rootContext()->setContextProperty("daanvisible", 0);
            if (da == 1) {
                updateDisplayText(engine, a+1,vzi);
                updateb(engine,a+1,player);
            }else if(da==2){
                if (a != 681) {
                    updateDisplayText(engine, a + 2,vzi);
                    updateb(engine, a + 2,player);
                }
                else {
                    updateDisplayText(engine, a + 3,vzi);
                    updateb(engine, a + 3,player);
                }
            }
            gotoa(da);
        });
    QObject::connect(&Controller, &MainWindow::shuruChanged,
        [&engine, &vzi,&player,&inliang](QString shuru) {
            int dui = 0;
            if (shuru.contains("\xE8\xAF\xAD") || shuru.contains("\xE8\xA8\x80") || shuru.contains("\xE8\xAF\x9D")) {
                dui = 1;
            }
            if (dui==1) {
                a=a+1;
                hang = a;
                updateDisplayText(engine, hang, vzi);
                updateb(engine, a,player);
            }
            else {
                updateb(engine, 552,player);
                a = 550;
                engine.rootContext()->setContextProperty("nameText", "\xE6\x98\x9F\xE7\xA9\xBA");
                engine.clearComponentCache();
                engine.rootContext()->setContextProperty("currentText", shuru);
                dacuo = 1;
            }
            engine.rootContext()->setContextProperty("shuruvisible", 0);
        });
    QObject::connect(&Controller, &MainWindow::Extra,
        [&engine](int extrac) {
            if (extrac) {
                QFile efile(eFilePath);
                efile.open(QIODevice::ReadOnly | QIODevice::Text);
                QTextStream ein(&efile);
                QString eline;
                // 读取所有行
                while (!ein.atEnd()) {
                    eline = ein.readLine();
                }
                efile.close();
                extra = eline.toFloat();
                engine.rootContext()->setContextProperty("extra", extra);
            }
        });
    engine.rootContext()->setContextProperty("sja1x", sja1x);
    engine.rootContext()->setContextProperty("sja1y", sja1y);
    engine.rootContext()->setContextProperty("sja2x", sja2x);
    engine.rootContext()->setContextProperty("sja2y", sja2y);
    engine.rootContext()->setContextProperty("sjx", sjx);
    engine.rootContext()->setContextProperty("fsa1visible", 0);
    engine.rootContext()->setContextProperty("fsa2visible", 0);
    engine.rootContext()->setContextProperty("ditu1visible", 0); 
    engine.rootContext()->setContextProperty("ditu2visible", 0);
    static float vx1 = 0;
    static float vy1 = 0;
    static float vx2 = 0;
    static float vy2 = 0;
    QObject::connect(sjTimer, &QTimer::timeout, [&Controller,&engine,&fuhuo,&fuhuo1,&fuhuo2]() {
        QObject::connect(&Controller, &MainWindow::wsChanged,
            [&engine](int ws) {
                switch (ws) {
                case 1:
                    if ((sja1y >= 305) && (vy1 > -12)) {
                        vy1 = vy1 - 0.1;
                    }
                    break;
                case 2:
                    if (305 < sja1y) {
                        sja1y = 320;
                        vy1 = 0;
                    }
                    break;
                }
            });
        QObject::connect(&Controller, &MainWindow::adChanged,
            [&engine](int ad) {
                switch (ad) {
                case 1:
                    if ((vx1 > -8) && (sja1y > 305)) {
                        vx1 = vx1 - 0.03;
                    }
                    break;
                case 2:
                    if ((vx1 < 8) && (sja1y > 305)) {
                        vx1 = vx1 + 0.03;
                    }
                    break;
                }
            });
        wuli(vx1, vy1, sja1y);
        sja1x = sja1x + vx1;
        sja1y = sja1y + vy1;
        float r = sja1x - sja2x;
        if (r > 0) {
            vx2 = vx2 + (0.01 * pow(1.0001, -0.1 * r) * (r - 69));
        }
        else {
            vx2 = vx2 + (0.01 * pow(1.0001, 0.1 * r) * (r - 69));
        }
        wuli(vx2, vy2, sja2y);
        sja2x = sja2x + vx2;
        sja2y = sja2y + vy2;

        if (sj == 1) {
            if ((((sja1x + sjx > 450) && (vx1 > 0)) || ((sja1x + sjx < 50) && (vx1 < 0))) && (sjx <= 0) && (sjx >= -1280)) {
                sjx = sjx - vx1;
                if (sjx > 0) {
                    sjx = 0;
                }
                else if (sjx < -1280) {
                    sjx = -1280;
                }
            }
        }
        else if(sj==2){
            if ((((sja1x + sjx > 450) && (vx1 > 0)) || ((sja1x + sjx < 50) && (vx1 < 0))) && (sjx <= 0) && (sjx >= -3280)) {
                sjx = sjx - vx1;
                if (sjx > 0) {
                    sjx = 0;
                }
                else if (sjx < -3280) {
                    sjx = -3280;
                }
            }
        }
        engine.rootContext()->setContextProperty("sjx", sjx);
        engine.rootContext()->setContextProperty("sja1x", sja1x);
        engine.rootContext()->setContextProperty("sja1y", sja1y);
        engine.rootContext()->setContextProperty("sja2x", sja2x);
        engine.rootContext()->setContextProperty("sja2y", sja2y);
        static float sja1tan;
        static int fsa1visible = 0;
        static int fsa2visible = 0;
        QObject::connect(&Controller, &MainWindow::weizhiChanged,
            [&engine](float sx, float sy) {
                sja1tan = (sy - sja1y) / (sx - sja1x);
                float sja1theta;
                if (sx >= sja1x) {
                    sja1theta = atan(sja1tan);
                }
                else {
                    sja1theta = atan(sja1tan) + 3.1415;
                }
                if (sj!=0) {
                    engine.rootContext()->setContextProperty("sja1theta", sja1theta);
                }
            });
        QObject::connect(&Controller, &MainWindow::fsChanged,
            [&engine](int fs) {
                if (fs) {
                    fsa1visible = 1;
                }
                else {
                    fsa1visible = 0;
                }
                engine.rootContext()->setContextProperty("fsa1visible", fsa1visible);
            });
        for (int sji = 0; sji / 2 < dirn; sji = sji + 2) {
            float divx = dirv[sji].toFloat();
            float divy = dirv[sji + 1].toFloat();
            int dix = dixy[sji].toInt();
            int diy = dixy[sji + 1].toInt();
            float dird = abs(sja1x - dix) < abs(sja2x - dix) ? (sja1x - dix) : (sja2x - dix);
            if (dird > -600) {
                if ((dird > 0) && (diy > 325) && (diy < 331)) {
                    divy = -6.5;
                    divx = 15;
                }
                else if ((dird < 0) && (diy > 325) && (diy < 331)) {
                    divy = -6.5;
                    divx = -15;
                }
                wulid(divx, divy, diy);
                dirv[sji] = divx;
                dirv[sji + 1] = divy;
                dixy[sji + 1] = diy;
                dixy[sji] = dixy[sji].toInt() + dirv[sji].toInt();
                dixy[sji + 1] = dixy[sji + 1].toInt() + dirv[sji + 1].toInt();
            }
            if (abs(sja2x - dix) < 470) {
                QTimer::singleShot(150, [&]() {
                    fsa2visible = 1;
                    });
                float sja2tan = (diy - sja2y + 0.00001) / (dix - sja2x + 0.000001);
                float sja2theta;
                if (dix >= sja2x) {
                    sja2theta = atan(sja2tan);
                }
                else {
                    sja2theta = atan(sja2tan) + 3.1415;
                }
                engine.rootContext()->setContextProperty("sja2theta", sja2theta);
                if (fsa2visible) {
                    if ((abs(sja2tan) * (diy > sja2y ? abs(dix - sja2x) : -abs(dix - sja2x)) - (diy - sja2y) > 0) && (abs(sja2tan) * (diy > sja2y ? abs(dix - sja2x) : -abs(dix - sja2x)) - (diy - sja2y) < 20)) {
                        dixy.removeAt(sji + 1);
                        dixy.removeAt(sji);
                        dirn--;
                    }
                }
            }
            else {
                fsa1visible = 0;
            }
            if ((abs(sja2x - dix) < 160) && (sja2y > 305)) {
                vy2 = -14;
            }
            engine.rootContext()->setContextProperty("fsa2visible", fsa2visible);
            if (fsa1visible) {
                if ((abs(sja1tan) * (diy > sja1y ? abs(dix - sja1x) : -abs(dix - sja1x)) - (diy - sja1y) > 0) && (abs(sja1tan) * (diy > sja1y ? abs(dix - sja1x) : -abs(dix - sja1x)) - (diy - sja1y) < 20)) {
                    dixy.removeAt(sji + 1);
                    dixy.removeAt(sji);
                    dirn--;
                }
            }
            if ((sja1x - dix > -40) && (sja1x - dix < 0) && (sja1y - diy > -40) && (sja1y - diy < 0)) {
                dix = dix + 100;
                alinshi=a;
                if ((fuhuo > 0) && !fuhuo1) {
                    a = 957;
                    Controller.next1();
                    fuhuo1 = 1;
                }else if ((fuhuo > 0) && fuhuo1) {
                    a = 965;
                    Controller.next1();
                }
                else {
                    a = 937;
                    Controller.next1();
                }
                qDebug() << "zzzzzzzzzzz";
                sjTimer->stop();
            }
            if ((sja2x - dix > -40) && (sja2x - dix < 0) && (sja2y - diy > -40) && (sja2y - diy < 0)) {
                dix = dix + 100;
                alinshi = a;
                if ((fuhuo > 0) && !fuhuo2) {
                    a = 948;
                    Controller.next1();
                    fuhuo2 = 1;
                }
                else if ((fuhuo > 0) && fuhuo2) {
                    a = 963;
                    Controller.next1();
                }
                else {
                    if (sj == 1) {
                        a = 940;
                        Controller.next1();
                    }
                    else if (sj == 2) {
                        a = 972;
                        Controller.next1();
                    }
                }
                qDebug() << "zzzzzzzzzz";
                sjTimer->stop();
            }
        }
        if (fsa2visible) {
            QTimer::singleShot(200, [&]() {
                fsa2visible = 0;
                engine.rootContext()->setContextProperty("fsa2visible", fsa2visible);
                });
        }
        if (sj == 1) {
            if ((sja1x > 1832) && (dirn == 0)) {
                sjsuo = 0;
            }
        }
        else {
            if ((sja1x > 3530) && (dirn == 0)) {
                sjsuo = 0;
            }
            else if ((sja1x > 1380)&& (sja1x < 3530)) {
                sjsuo = 0;
            }
        }
        Controller.updateElementArray(dixy);
        });
    sjTimer->start(10);
    app.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
    QTextCodec* codec = QTextCodec::codecForName("GBK");s
#else
    QTextCodec* codec = QTextCodec::codecForName("UTF-8");
#endif
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);
#else
    QTextCodec* codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);
#endif
    engine.load(QUrl(QStringLiteral("qrc:/qt/qml/qtquickapplication1/main.qml")));

    if (engine.rootObjects().isEmpty()) {
        return -1;
    }

    // 获取主窗口
    QQuickWindow* mainWindow = qobject_cast<QQuickWindow*>(engine.rootObjects().first());

    if (mainWindow) {
        if (scalingApplied == 1) {
            mainWindow->showFullScreen();
        }
        else if (scalingApplied == 2) {
            mainWindow->showMaximized();
        }
        else {
            mainWindow->show();
        }
    }
    return app.exec();
    if (fileContent) {
        delete fileContent;
        fileContent = nullptr;
    }
}