qmlfrontend/game_config.cpp
branchui-scaling
changeset 15288 c4fd2813b127
parent 14280 61bc44de2b4c
equal deleted inserted replaced
13395:0135e64c6c66 15288:c4fd2813b127
       
     1 #include "game_config.h"
       
     2 
       
     3 #include <QtEndian>
       
     4 
       
     5 GameConfig::GameConfig() {}
       
     6 
       
     7 const char** GameConfig::argv() const {
       
     8   m_argv.resize(m_arguments.size());
       
     9 
       
    10   for (int i = 0; i < m_arguments.size(); ++i)
       
    11     m_argv[i] = m_arguments[i].data();
       
    12 
       
    13   return m_argv.data();
       
    14 }
       
    15 
       
    16 int GameConfig::argc() const { return m_arguments.size(); }
       
    17 
       
    18 const QList<QByteArray> GameConfig::config() const {
       
    19   QList<QByteArray> cfg = m_cfg;
       
    20   cfg.append("\x01!");
       
    21   return cfg;
       
    22 }
       
    23 
       
    24 void GameConfig::clear() {
       
    25   m_arguments.clear();
       
    26   m_cfg.clear();
       
    27 }
       
    28 
       
    29 void GameConfig::cmdSeed(const QByteArray& seed) { cfgAppend("eseed " + seed); }
       
    30 
       
    31 void GameConfig::cmdTheme(const QByteArray& theme) {
       
    32   cfgAppend("e$theme " + theme);
       
    33 }
       
    34 
       
    35 void GameConfig::cmdMapgen(int mapgen) {
       
    36   cfgAppend("e$mapgen " + QByteArray::number(mapgen));
       
    37 }
       
    38 
       
    39 void GameConfig::cmdTeam(const Team& team) {
       
    40   cfgAppend("eaddteam <hash> " + team.color + " " + team.name);
       
    41 
       
    42   for (const Hedgehog& h : team.hedgehogs()) {
       
    43     cfgAppend("eaddhh " + QByteArray::number(h.level) + " " +
       
    44               QByteArray::number(h.hp) + " " + h.name);
       
    45     cfgAppend("ehat " + h.hat);
       
    46   }
       
    47   cfgAppend(
       
    48       "eammloadt 9391929422199121032235111001200000000211100101011111000102");
       
    49   cfgAppend(
       
    50       "eammprob 0405040541600655546554464776576666666155510101115411111114");
       
    51   cfgAppend(
       
    52       "eammdelay 0000000000000205500000040007004000000000220000000600020000");
       
    53   cfgAppend(
       
    54       "eammreinf 1311110312111111123114111111111111111211111111111111111111");
       
    55   cfgAppend("eammstore");
       
    56 }
       
    57 
       
    58 void GameConfig::cfgAppend(const QByteArray& cmd) {
       
    59   Q_ASSERT(cmd.size() <= 49215);
       
    60 
       
    61   QByteArray sizeBytes;
       
    62   if (cmd.size() < 64) {
       
    63     quint8 len = static_cast<quint8>(cmd.size());
       
    64     sizeBytes = QByteArray::fromRawData(reinterpret_cast<const char*>(&len), 1);
       
    65   } else {
       
    66     quint16 size = static_cast<quint16>(cmd.size()) - 64;
       
    67     size = (size / 256 + 64) * 256 + size & 0xff;
       
    68     quint16 size_be = qToBigEndian(size);
       
    69 
       
    70     sizeBytes =
       
    71         QByteArray::fromRawData(reinterpret_cast<const char*>(&size_be), 2);
       
    72   }
       
    73 
       
    74   m_cfg.append(sizeBytes + cmd);
       
    75 }