qmlfrontend/game_config.cpp
author alfadur
Tue, 21 May 2019 23:04:50 +0300
changeset 15024 af2f6f4074de
parent 14275 61bc44de2b4c
permissions -rw-r--r--
add rope layers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14154
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14143
diff changeset
     1
#include "game_config.h"
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     2
14274
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
     3
#include <QtEndian>
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
     4
14273
645e4591361f Send seed to engine to receive random map previews
unC0Rr
parents: 14154
diff changeset
     5
GameConfig::GameConfig() {}
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     6
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     7
const char** GameConfig::argv() const {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     8
  m_argv.resize(m_arguments.size());
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     9
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    10
  for (int i = 0; i < m_arguments.size(); ++i)
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    11
    m_argv[i] = m_arguments[i].data();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    12
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    13
  return m_argv.data();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    14
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    15
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    16
int GameConfig::argc() const { return m_arguments.size(); }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    17
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    18
const QList<QByteArray> GameConfig::config() const {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    19
  QList<QByteArray> cfg = m_cfg;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    20
  cfg.append("\x01!");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    21
  return cfg;
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    22
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    23
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    24
void GameConfig::clear() {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    25
  m_arguments.clear();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    26
  m_cfg.clear();
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    27
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    28
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    29
void GameConfig::cmdSeed(const QByteArray& seed) { cfgAppend("eseed " + seed); }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    30
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    31
void GameConfig::cmdTheme(const QByteArray& theme) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    32
  cfgAppend("e$theme " + theme);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    33
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    34
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    35
void GameConfig::cmdMapgen(int mapgen) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    36
  cfgAppend("e$mapgen " + QByteArray::number(mapgen));
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    37
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    38
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    39
void GameConfig::cmdTeam(const Team& team) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    40
  cfgAppend("eaddteam <hash> " + team.color + " " + team.name);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    41
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    42
  for (const Hedgehog& h : team.hedgehogs()) {
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    43
    cfgAppend("eaddhh " + QByteArray::number(h.level) + " " +
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    44
              QByteArray::number(h.hp) + " " + h.name);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    45
    cfgAppend("ehat " + h.hat);
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    46
  }
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    47
  cfgAppend(
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    48
      "eammloadt 9391929422199121032235111001200000000211100101011111000102");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    49
  cfgAppend(
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    50
      "eammprob 0405040541600655546554464776576666666155510101115411111114");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    51
  cfgAppend(
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    52
      "eammdelay 0000000000000205500000040007004000000000220000000600020000");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    53
  cfgAppend(
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    54
      "eammreinf 1311110312111111123114111111111111111211111111111111111111");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    55
  cfgAppend("eammstore");
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    56
}
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    57
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    58
void GameConfig::cfgAppend(const QByteArray& cmd) {
14274
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    59
  Q_ASSERT(cmd.size() <= 49215);
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    60
14275
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14274
diff changeset
    61
  QByteArray sizeBytes;
14274
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    62
  if (cmd.size() < 64) {
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    63
    quint8 len = static_cast<quint8>(cmd.size());
14275
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14274
diff changeset
    64
    sizeBytes = QByteArray::fromRawData(reinterpret_cast<const char*>(&len), 1);
14274
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    65
  } else {
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    66
    quint16 size = static_cast<quint16>(cmd.size()) - 64;
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    67
    size = (size / 256 + 64) * 256 + size & 0xff;
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    68
    quint16 size_be = qToBigEndian(size);
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    69
14275
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14274
diff changeset
    70
    sizeBytes =
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14274
diff changeset
    71
        QByteArray::fromRawData(reinterpret_cast<const char*>(&size_be), 2);
14274
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    72
  }
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14273
diff changeset
    73
14275
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14274
diff changeset
    74
  m_cfg.append(sizeBytes + cmd);
14143
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    75
}