Make playing a game possible qmlfrontend
authorunc0rr
Wed, 27 Dec 2017 00:59:26 +0100 (2017-12-26)
branchqmlfrontend
changeset 12868 fe16fa088b69
parent 12866 488782d9aba9
child 12881 b544bbbd0696
Make playing a game possible
qmlfrontend/CMakeLists.txt
qmlfrontend/Page1.qml
qmlfrontend/Page1Form.ui.qml
qmlfrontend/gameconfig.cpp
qmlfrontend/gameconfig.h
qmlfrontend/hwengine.cpp
qmlfrontend/hwengine.h
qmlfrontend/team.cpp
qmlfrontend/team.h
--- a/qmlfrontend/CMakeLists.txt	Tue Dec 26 01:36:58 2017 +0100
+++ b/qmlfrontend/CMakeLists.txt	Wed Dec 27 00:59:26 2017 +0100
@@ -12,6 +12,7 @@
     "hwengine.cpp" "hwengine.h"
     "gameconfig.cpp" "gameconfig.h"
     "runqueue.cpp" "runqueue.h"
+    "team.cpp" "team.h"
     "previewimageprovider.cpp" "previewimageprovider.h"
     "flib.h")
 
--- a/qmlfrontend/Page1.qml	Tue Dec 26 01:36:58 2017 +0100
+++ b/qmlfrontend/Page1.qml	Wed Dec 27 00:59:26 2017 +0100
@@ -2,8 +2,10 @@
 import Hedgewars.Engine 1.0
 
 Page1Form {
+  gameButton.onClicked: {
+    HWEngine.runQuickGame()
+}
   button1.onClicked: {
-    console.log("Button clicked")
     HWEngine.getPreview()
   }
 
--- a/qmlfrontend/Page1Form.ui.qml	Tue Dec 26 01:36:58 2017 +0100
+++ b/qmlfrontend/Page1Form.ui.qml	Wed Dec 27 00:59:26 2017 +0100
@@ -5,6 +5,7 @@
 Item {
   property alias button1: button1
   property alias previewImage: previewImage
+  property alias gameButton: gameButton
 
     RowLayout {
         anchors.horizontalCenter: parent.horizontalCenter
@@ -12,8 +13,13 @@
         anchors.top: parent.top
 
         Button {
-            id: button1
-            text: qsTr("Run")
+          id: button1
+          text: qsTr("Preview")
+        }
+
+        Button {
+            id: gameButton
+            text: qsTr("Game")
         }
     }
 
--- a/qmlfrontend/gameconfig.cpp	Tue Dec 26 01:36:58 2017 +0100
+++ b/qmlfrontend/gameconfig.cpp	Wed Dec 27 00:59:26 2017 +0100
@@ -1,11 +1,9 @@
 #include "gameconfig.h"
 
 GameConfig::GameConfig()
+    : m_isPreview(true)
 {
-    m_arguments
-        << ""
-        << "--internal"
-        << "--landpreview";
+    setPreview(m_isPreview);
 }
 
 const char** GameConfig::argv() const
@@ -41,14 +39,54 @@
     cfgAppend("eseed " + seed);
 }
 
+void GameConfig::cmdTheme(const QByteArray& theme)
+{
+    cfgAppend("e$theme " + theme);
+}
+
 void GameConfig::cmdMapgen(int mapgen)
 {
     cfgAppend("e$mapgen " + QByteArray::number(mapgen));
 }
 
-bool GameConfig::isPreview()
+void GameConfig::cmdTeam(const Team& team)
+{
+    cfgAppend("eaddteam <hash> " + team.color + " " + team.name);
+
+    for (const Hedgehog& h : team.hedgehogs()) {
+        cfgAppend("eaddhh " + QByteArray::number(h.level) + " " + QByteArray::number(h.hp) + " " + h.name);
+        cfgAppend("ehat " + h.hat);
+    }
+    cfgAppend("eammloadt 9391929422199121032235111001200000000211100101011111000102");
+    cfgAppend("eammprob 0405040541600655546554464776576666666155510101115411111114");
+    cfgAppend("eammdelay 0000000000000205500000040007004000000000220000000600020000");
+    cfgAppend("eammreinf 1311110312111111123114111111111111111211111111111111111111");
+    cfgAppend("eammstore");
+}
+
+bool GameConfig::isPreview() const
 {
-    return true;
+    return m_isPreview;
+}
+
+void GameConfig::setPreview(bool isPreview)
+{
+    m_isPreview = isPreview;
+
+    m_arguments.clear();
+
+    if (m_isPreview) {
+        m_arguments
+            << ""
+            << "--internal"
+            << "--landpreview";
+
+    } else {
+        m_arguments
+            << ""
+            << "--internal"
+            << "--nomusic";
+    }
 }
 
 void GameConfig::cfgAppend(const QByteArray& cmd)
--- a/qmlfrontend/gameconfig.h	Tue Dec 26 01:36:58 2017 +0100
+++ b/qmlfrontend/gameconfig.h	Wed Dec 27 00:59:26 2017 +0100
@@ -4,6 +4,8 @@
 #include <QList>
 #include <QVector>
 
+#include "team.h"
+
 class GameConfig {
 public:
     explicit GameConfig();
@@ -14,14 +16,19 @@
 
     void clear();
     void cmdSeed(const QByteArray& seed);
+    void cmdTheme(const QByteArray& theme);
     void cmdMapgen(int mapgen);
+    void cmdTeam(const Team& team);
 
-    bool isPreview();
+    bool isPreview() const;
+    void setPreview(bool isPreview);
 
 private:
     mutable QVector<const char*> m_argv;
     QList<QByteArray> m_arguments;
     QList<QByteArray> m_cfg;
+    QList<Team> m_teams;
+    bool m_isPreview;
 
     void cfgAppend(const QByteArray& cmd);
 };
--- a/qmlfrontend/hwengine.cpp	Tue Dec 26 01:36:58 2017 +0100
+++ b/qmlfrontend/hwengine.cpp	Wed Dec 27 00:59:26 2017 +0100
@@ -110,12 +110,23 @@
 
 void HWEngine::getPreview()
 {
-    GameConfig cfg;
-    cfg.cmdSeed(QUuid::createUuid().toByteArray());
+    m_gameConfig.cmdSeed(QUuid::createUuid().toByteArray());
+    m_gameConfig.setPreview(true);
 
-    m_runQueue->queue(cfg);
+    m_runQueue->queue(m_gameConfig);
 }
 
 void HWEngine::runQuickGame()
 {
+    m_gameConfig.cmdTheme("Bamboo");
+    Team team1;
+    team1.name = "team1";
+    Team team2;
+    team2.name = "team2";
+    team2.color = "7654321";
+    m_gameConfig.cmdTeam(team1);
+    m_gameConfig.cmdTeam(team2);
+    m_gameConfig.setPreview(false);
+
+    m_runQueue->queue(m_gameConfig);
 }
--- a/qmlfrontend/hwengine.h	Tue Dec 26 01:36:58 2017 +0100
+++ b/qmlfrontend/hwengine.h	Wed Dec 27 00:59:26 2017 +0100
@@ -5,6 +5,7 @@
 #include <QObject>
 
 #include "flib.h"
+#include "gameconfig.h"
 
 class QQmlEngine;
 class PreviewImageProvider;
@@ -34,6 +35,7 @@
     QQmlEngine* m_engine;
     PreviewImageProvider* m_previewProvider;
     RunQueue* m_runQueue;
+    GameConfig m_gameConfig;
 
     static void guiMessagesCallback(void* context, MessageType mt, const char* msg, uint32_t len);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qmlfrontend/team.cpp	Wed Dec 27 00:59:26 2017 +0100
@@ -0,0 +1,27 @@
+#include "team.h"
+
+Hedgehog::Hedgehog()
+    : name(QObject::tr("unnamed", "default hedgehog name").toUtf8())
+    , hat("NoHat")
+    , hp(100)
+    , level(0)
+{
+}
+
+Team::Team()
+    : name(QObject::tr("unnamed", "default team name").toUtf8())
+    , color("12345678")
+    , m_hedgehogsNumber(4)
+{
+    m_hedgehogs.resize(8);
+}
+
+void Team::resize(int number)
+{
+    m_hedgehogsNumber = number;
+}
+
+QVector<Hedgehog> Team::hedgehogs() const
+{
+    return m_hedgehogs.mid(0, m_hedgehogsNumber);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qmlfrontend/team.h	Wed Dec 27 00:59:26 2017 +0100
@@ -0,0 +1,31 @@
+#ifndef TEAM_H
+#define TEAM_H
+
+#include <QObject>
+#include <QVector>
+
+struct Hedgehog {
+    Hedgehog();
+
+    QByteArray name;
+    QByteArray hat;
+    quint32 hp;
+    int level;
+};
+
+class Team {
+public:
+    explicit Team();
+
+    void resize(int number);
+    QVector<Hedgehog> hedgehogs() const;
+
+    QByteArray name;
+    QByteArray color;
+
+private:
+    QVector<Hedgehog> m_hedgehogs;
+    int m_hedgehogsNumber;
+};
+
+#endif // TEAM_H