qmlfrontend/game_config.cpp
author Wuzzy <Wuzzy2@mail.ru>
Sat, 04 May 2019 00:14:12 +0200
changeset 14894 d17c1a28df17
parent 14296 61bc44de2b4c
permissions -rw-r--r--
Frontend controls: Don't show "QWERTY" for F13-F15
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14175
8354b390f1a2 Some refactoring of qmlfrontend. It now shows land preview generated by hedgewars-engine
unC0Rr
parents: 14164
diff changeset
     1
#include "game_config.h"
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
     2
14295
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
     3
#include <QtEndian>
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
     4
14294
645e4591361f Send seed to engine to receive random map previews
unC0Rr
parents: 14175
diff changeset
     5
GameConfig::GameConfig() {}
14164
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) {
14295
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    59
  Q_ASSERT(cmd.size() <= 49215);
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    60
14296
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14295
diff changeset
    61
  QByteArray sizeBytes;
14295
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    62
  if (cmd.size() < 64) {
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    63
    quint8 len = static_cast<quint8>(cmd.size());
14296
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14295
diff changeset
    64
    sizeBytes = QByteArray::fromRawData(reinterpret_cast<const char*>(&len), 1);
14295
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    65
  } else {
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    66
    quint16 size = static_cast<quint16>(cmd.size()) - 64;
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    67
    size = (size / 256 + 64) * 256 + size & 0xff;
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    68
    quint16 size_be = qToBigEndian(size);
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    69
14296
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14295
diff changeset
    70
    sizeBytes =
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14295
diff changeset
    71
        QByteArray::fromRawData(reinterpret_cast<const char*>(&size_be), 2);
14295
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    72
  }
0ae9885e1535 Support new message format in frontend
unC0Rr
parents: 14294
diff changeset
    73
14296
61bc44de2b4c It was bad idea to store QByteArray referencing memory on stack
unC0Rr
parents: 14295
diff changeset
    74
  m_cfg.append(sizeBytes + cmd);
14164
745c73e0e644 Start working on frontend to rust engine rewrite
unC0Rr
parents:
diff changeset
    75
}