MrShecks / screep-studio

A cross platform client for the Screeps MMO sandbox game for programmers written in C++ and the Qt Cross-platform Framework as part of the Level1Techs "Devember2k18" coding event.
GNU General Public License v3.0
4 stars 0 forks source link

Owner-dependent rendering #3

Open Vort opened 4 years ago

Vort commented 4 years ago

As I can see, rendering code contains some styles for distinguishing between player and enemy structures. But nevertheless client shows all structures as owned by enemy (with red color). This should be fixed.

I don't know how to do this properly. But, anyway, here is my attempt to fix rendering for Extension, Controller, ConstructionSite and Rampart:

diff --git a/models/room/RoomEntity.h b/models/room/RoomEntity.h
index 32fe06f..538e94f 100644
--- a/models/room/RoomEntity.h
+++ b/models/room/RoomEntity.h
@@ -47,6 +47,7 @@ public:
     QString id() const                          { return getString("_id"); }
     QString type() const                        { return getString("type"); }
     QString room() const                        { return getString("room"); }
+    QString user() const                        { return getString("user"); }

     int posX() const                            { return getInt("x"); }
     int posY() const                            { return getInt("y"); }
diff --git a/models/room/RoomModel.cpp b/models/room/RoomModel.cpp
index d2b4038..4d568da 100644
--- a/models/room/RoomModel.cpp
+++ b/models/room/RoomModel.cpp
@@ -38,9 +38,10 @@
 // Complete representation of a single world room
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

-RoomModel::RoomModel(NetworkModel* networkModel, const QString& roomName, const QString& shardName, QObject* parent /* = nullptr */)
+RoomModel::RoomModel(NetworkModel* networkModel, const QString& userId, const QString& roomName, const QString& shardName, QObject* parent /* = nullptr */)
     : _super(parent),
       _networkModel(networkModel),
+      _userId(userId),
       _roomName(roomName),
       _shardName(shardName),
       _stateMachineOpen(new StateMachineOpen(this)),
diff --git a/models/room/RoomModel.h b/models/room/RoomModel.h
index d5672a4..1f9881e 100644
--- a/models/room/RoomModel.h
+++ b/models/room/RoomModel.h
@@ -60,7 +60,7 @@ public:
     static const int ROOM_WIDTH = RoomTerrainModel::ROOM_WIDTH;
     static const int ROOM_HEIGHT = RoomTerrainModel::ROOM_HEIGHT;

-    explicit RoomModel(NetworkModel* networkModel, const QString& roomName, const QString& shardName, QObject* parent = nullptr);
+    explicit RoomModel(NetworkModel* networkModel, const QString& userId, const QString& roomName, const QString& shardName, QObject* parent = nullptr);
     virtual ~RoomModel();

     void open();
@@ -69,7 +69,8 @@ public:
     bool isOpen() const                             { return _isOpen; }

     const QString& id() const                       { return _id; }
-    const QString& roomName() const                 { return _roomName; }
+    const QString& userId() const                   { return _userId; }
+   const QString& roomName() const                 { return _roomName; }
     const QString& shardName() const                { return _shardName; }

     const RoomTerrainModel& terrainModel() const    { return _terrainModel; }
@@ -93,6 +94,7 @@ private:
     QPointer<NetworkModel> _networkModel;

     QString _id;
+   QString _userId;
     QString _roomName;
     QString _shardName;

diff --git a/widgets/room/RoomGraphicsScene.cpp b/widgets/room/RoomGraphicsScene.cpp
index 82bd329..372879e 100644
--- a/widgets/room/RoomGraphicsScene.cpp
+++ b/widgets/room/RoomGraphicsScene.cpp
@@ -37,9 +37,10 @@ const QColor RoomGraphicsScene::DEF_COLOR_GRID              = QColor("#444444");
 // RoomGraphicsScene
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

-RoomGraphicsScene::RoomGraphicsScene(QObject* parent /* = nullptr */)
+RoomGraphicsScene::RoomGraphicsScene(const QString& userId, QObject* parent /* = nullptr */)
     : _super(0, 0, RoomModel::ROOM_WIDTH * CELL_WIDTH, RoomModel::ROOM_HEIGHT * CELL_HEIGHT, parent),
       _roomModel(0),
+      _userId(userId),
       _showGrid(false),
       _terrainRenderer(this, CELL_WIDTH, CELL_HEIGHT) {

@@ -126,7 +127,7 @@ void RoomGraphicsScene::_onRoomEntityAdded(const QString& id, const RoomEntity&
     qDebug() << "Id=" << id << ", Type=" << entity.type();
 #endif // _DEBUG_SCENE_UPDATES

-    RoomGraphicsItem* item = RoomGraphicsItem::create(entity.type(), entity, QSize(CELL_WIDTH, CELL_HEIGHT));
+    RoomGraphicsItem* item = RoomGraphicsItem::create(entity.type(), entity, _userId, QSize(CELL_WIDTH, CELL_HEIGHT));

     addItem(item);
     _entities.insert(id, item);
diff --git a/widgets/room/RoomGraphicsScene.h b/widgets/room/RoomGraphicsScene.h
index 40a76b1..0d37739 100644
--- a/widgets/room/RoomGraphicsScene.h
+++ b/widgets/room/RoomGraphicsScene.h
@@ -63,7 +63,7 @@ public:
     const static int CELL_WIDTH         = 64;
     const static int CELL_HEIGHT        = CELL_WIDTH;

-    RoomGraphicsScene(QObject* parent = nullptr);
+    RoomGraphicsScene(const QString& userId, QObject* parent = nullptr);
     virtual ~RoomGraphicsScene();

     void setModel(RoomModel::TSharedPtr roomModel);
diff --git a/widgets/room/RoomGraphicsView.cpp b/widgets/room/RoomGraphicsView.cpp
index 2363a41..2b77146 100644
--- a/widgets/room/RoomGraphicsView.cpp
+++ b/widgets/room/RoomGraphicsView.cpp
@@ -33,7 +33,7 @@

 RoomGraphicsView::RoomGraphicsView(RoomModel::TSharedPtr roomModel, QWidget* parent /* = nullptr */)
     : _super(parent),
-      _roomScene(this),
+      _roomScene(roomModel->userId(), this),
       _roomModel(roomModel) {

     setScene(&_roomScene);
diff --git a/widgets/room/entities/RoomGraphicsItem.cpp b/widgets/room/entities/RoomGraphicsItem.cpp
index ae96b92..65dfc39 100644
--- a/widgets/room/entities/RoomGraphicsItem.cpp
+++ b/widgets/room/entities/RoomGraphicsItem.cpp
@@ -64,7 +64,7 @@ RoomGraphicsItem::~RoomGraphicsItem() {

 }

-RoomGraphicsItem* RoomGraphicsItem::create(const QString& type, const RoomEntity& entity, const QSize& cellSize) {
+RoomGraphicsItem* RoomGraphicsItem::create(const QString& type, const RoomEntity& entity, const QString& userId, const QSize& cellSize) {
     RoomGraphicsItem* item = nullptr;

     // Poor mans factory ;)
@@ -76,10 +76,10 @@ RoomGraphicsItem* RoomGraphicsItem::create(const QString& type, const RoomEntity
         case Screeps::EntityType_Source:                item = new RoomGraphicsItemSource(entity, cellSize);            break;
         case Screeps::EntityType_Mineral:               item = new RoomGraphicsItemMineral(entity, cellSize);           break;
         case Screeps::EntityType_Extractor:             item = new RoomGraphicsItemExtractor(entity, cellSize);         break;
-        case Screeps::EntityType_Extension:             item = new RoomGraphicsItemExtension(entity, cellSize);         break;
-        case Screeps::EntityType_Controller:            item = new RoomGraphicsItemController(entity, cellSize);        break;
+        case Screeps::EntityType_Extension:             item = new RoomGraphicsItemExtension(entity, userId, cellSize);         break;
+        case Screeps::EntityType_Controller:            item = new RoomGraphicsItemController(entity, userId, cellSize);        break;
         case Screeps::EntityType_Wall:                  item = new RoomGraphicsItemWall(entity, cellSize);              break;
-        case Screeps::EntityType_ConstructionSite:      item = new RoomGraphicsItemConstructionSite(entity, cellSize);  break;
+        case Screeps::EntityType_ConstructionSite:      item = new RoomGraphicsItemConstructionSite(entity, userId, cellSize);  break;
         case Screeps::EntityType_Tombstone:             item = new RoomGraphicsItemTombstone(entity, cellSize);         break;
         case Screeps::EntityType_Storage:               item = new RoomGraphicsItemStorage(entity, cellSize);           break;
         case Screeps::EntityType_Container:             item = new RoomGraphicsItemContainer(entity, cellSize);         break;
@@ -89,7 +89,7 @@ RoomGraphicsItem* RoomGraphicsItem::create(const QString& type, const RoomEntity
         case Screeps::EntityType_Terminal:              item = new RoomGraphicsItemTerminal(entity, cellSize);          break;
         case Screeps::EntityType_Lab:                   item = new RoomGraphicsItemLab(entity, cellSize);               break;
         case Screeps::EntityType_Nuker:                 item = new RoomGraphicsItemNuker(entity, cellSize);             break;
-        case Screeps::EntityType_Rampart:               item = new RoomGraphicsItemRampart(entity, cellSize);           break;
+        case Screeps::EntityType_Rampart:               item = new RoomGraphicsItemRampart(entity, userId, cellSize);           break;
         case Screeps::EntityType_PowerSpawn:            item = new RoomGraphicsItemPowerSpawn(entity, cellSize);        break;
         case Screeps::EntityType_Observer:              item = new RoomGraphicsItemObserver(entity, cellSize);          break;

diff --git a/widgets/room/entities/RoomGraphicsItem.h b/widgets/room/entities/RoomGraphicsItem.h
index 97cd37c..96f7d29 100644
--- a/widgets/room/entities/RoomGraphicsItem.h
+++ b/widgets/room/entities/RoomGraphicsItem.h
@@ -136,7 +136,7 @@ public:

     virtual const RoomEntity& roomEntity() const = 0;

-    static RoomGraphicsItem* create(const QString& type, const RoomEntity& entity, const QSize& cellSize);
+    static RoomGraphicsItem* create(const QString& type, const RoomEntity& entity, const QString& userId, const QSize& cellSize);

     //
     // QGraphicsItem
diff --git a/widgets/room/entities/RoomGraphicsItemConstructionSite.cpp b/widgets/room/entities/RoomGraphicsItemConstructionSite.cpp
index c736618..e24aa47 100644
--- a/widgets/room/entities/RoomGraphicsItemConstructionSite.cpp
+++ b/widgets/room/entities/RoomGraphicsItemConstructionSite.cpp
@@ -29,7 +29,7 @@
 // RoomGraphicsItemConstructionSite
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

-RoomGraphicsItemConstructionSite::RoomGraphicsItemConstructionSite(const ConstructionSiteEntity& entity, const QSize& cellSize, QGraphicsItem* parent /* = nullptr */)
+RoomGraphicsItemConstructionSite::RoomGraphicsItemConstructionSite(const ConstructionSiteEntity& entity, const QString& userId, const QSize& cellSize, QGraphicsItem* parent /* = nullptr */)
     : _super(ItemType_ConstructionSite, entity, cellSize, ItemZOrder_ConstructionSite, parent),
       _opacityAnimation(new QPropertyAnimation(this)),
       _constructionSiteRenderer(this)  {
@@ -46,7 +46,9 @@ RoomGraphicsItemConstructionSite::RoomGraphicsItemConstructionSite(const Constru

     _opacityAnimation->start();

-    _constructionSiteRenderer.setProgress(entity.progress(), entity.progressTotal());
+   _constructionSiteRenderer.setOwner(entity.user() == userId ?
+       EntityRenderer::OwnerType::OwnerType_Player : EntityRenderer::OwnerType::OwnerType_Enemy);
+   _constructionSiteRenderer.setProgress(entity.progress(), entity.progressTotal());
 }

 RoomGraphicsItemConstructionSite::~RoomGraphicsItemConstructionSite() {
diff --git a/widgets/room/entities/RoomGraphicsItemConstructionSite.h b/widgets/room/entities/RoomGraphicsItemConstructionSite.h
index 20e4ee8..1fa100e 100644
--- a/widgets/room/entities/RoomGraphicsItemConstructionSite.h
+++ b/widgets/room/entities/RoomGraphicsItemConstructionSite.h
@@ -55,7 +55,7 @@ class RoomGraphicsItemConstructionSite : public TRoomGraphicsItem<ConstructionSi
     static const int FADE_DURATION                  = 1000 * 3;

 public:
-    RoomGraphicsItemConstructionSite(const ConstructionSiteEntity& entity, const QSize& cellSize, QGraphicsItem* parent = nullptr);
+    RoomGraphicsItemConstructionSite(const ConstructionSiteEntity& entity, const QString& userId, const QSize& cellSize, QGraphicsItem* parent = nullptr);
     virtual ~RoomGraphicsItemConstructionSite();

 private:
diff --git a/widgets/room/entities/RoomGraphicsItemController.cpp b/widgets/room/entities/RoomGraphicsItemController.cpp
index 49746bb..b8c6f68 100644
--- a/widgets/room/entities/RoomGraphicsItemController.cpp
+++ b/widgets/room/entities/RoomGraphicsItemController.cpp
@@ -29,7 +29,7 @@
 // RoomGraphicsItemController
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

-RoomGraphicsItemController::RoomGraphicsItemController(const ControllerEntity& entity, const QSize& cellSize, QGraphicsItem* parent /* = nullptr */)
+RoomGraphicsItemController::RoomGraphicsItemController(const ControllerEntity& entity, const QString& userId, const QSize& cellSize, QGraphicsItem* parent /* = nullptr */)
     : _super(ItemType_Controller, entity, cellSize, ItemZOrder_Controller, parent),
       _controllerRenderer(this),
       _glow(this) {
@@ -37,6 +37,8 @@ RoomGraphicsItemController::RoomGraphicsItemController(const ControllerEntity& e
     _glow.setScale(GLOW_EFFECT_SCALE);
     _glow.setGlowAnimation(GLOW_EFFECT_OPACITY_MIN, GLOW_EFFECT_OPACITY_MAX, 8000);

+   _controllerRenderer.setOwner(entity.user() == userId ?
+       EntityRenderer::OwnerType::OwnerType_Player : EntityRenderer::OwnerType::OwnerType_Enemy);
     _controllerRenderer.setLevel(entity.level());
 }

diff --git a/widgets/room/entities/RoomGraphicsItemController.h b/widgets/room/entities/RoomGraphicsItemController.h
index bc12c19..08c11ca 100644
--- a/widgets/room/entities/RoomGraphicsItemController.h
+++ b/widgets/room/entities/RoomGraphicsItemController.h
@@ -55,7 +55,7 @@ class RoomGraphicsItemController : public TRoomGraphicsItem<ControllerEntity> {
     static constexpr qreal GLOW_EFFECT_OPACITY_MAX  = 0.9;

 public:
-    RoomGraphicsItemController(const ControllerEntity& entity, const QSize& cellSize, QGraphicsItem* parent = nullptr);
+    RoomGraphicsItemController(const ControllerEntity& entity, const QString& userId, const QSize& cellSize, QGraphicsItem* parent = nullptr);
     virtual ~RoomGraphicsItemController();

 private:
diff --git a/widgets/room/entities/RoomGraphicsItemExtension.cpp b/widgets/room/entities/RoomGraphicsItemExtension.cpp
index 74dafa9..02ecbd3 100644
--- a/widgets/room/entities/RoomGraphicsItemExtension.cpp
+++ b/widgets/room/entities/RoomGraphicsItemExtension.cpp
@@ -30,7 +30,7 @@
 // RoomGraphicsItemExtension
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

-RoomGraphicsItemExtension::RoomGraphicsItemExtension(const ExtensionEntity& entity, const QSize& cellSize, QGraphicsItem* parent /* = nullptr */)
+RoomGraphicsItemExtension::RoomGraphicsItemExtension(const ExtensionEntity& entity, const QString& userId, const QSize& cellSize, QGraphicsItem* parent /* = nullptr */)
     : _super(ItemType_Extension, entity, cellSize, ItemZOrder_Extension, parent),
       _extensionRenderer(this),
       _glow(this) {
@@ -39,7 +39,9 @@ RoomGraphicsItemExtension::RoomGraphicsItemExtension(const ExtensionEntity& enti
     _glow.setOpacity(GLOW_EFFECT_OPACITY);
     _glow.setVisible(entity.energy() > 0);

-    _extensionRenderer.setEnergy(entity.energy());
+   _extensionRenderer.setOwner(entity.user() == userId ?
+       EntityRenderer::OwnerType::OwnerType_Player : EntityRenderer::OwnerType::OwnerType_Enemy);
+   _extensionRenderer.setEnergy(entity.energy());
     _extensionRenderer.setEnergyCapacity(entity.energyCapacity());
 }

diff --git a/widgets/room/entities/RoomGraphicsItemExtension.h b/widgets/room/entities/RoomGraphicsItemExtension.h
index 984eafb..d89ecaa 100644
--- a/widgets/room/entities/RoomGraphicsItemExtension.h
+++ b/widgets/room/entities/RoomGraphicsItemExtension.h
@@ -58,7 +58,7 @@ class RoomGraphicsItemExtension : public TRoomGraphicsItem<ExtensionEntity> {
     static constexpr qreal GLOW_EFFECT_OPACITY  = 0.2;

 public:
-    RoomGraphicsItemExtension(const ExtensionEntity& entity, const QSize& cellSize, QGraphicsItem* parent = nullptr);
+    RoomGraphicsItemExtension(const ExtensionEntity& entity, const QString& userId, const QSize& cellSize, QGraphicsItem* parent = nullptr);
     virtual ~RoomGraphicsItemExtension();

 private:
diff --git a/widgets/room/entities/RoomGraphicsItemRampart.cpp b/widgets/room/entities/RoomGraphicsItemRampart.cpp
index 15a6100..d31a74e 100644
--- a/widgets/room/entities/RoomGraphicsItemRampart.cpp
+++ b/widgets/room/entities/RoomGraphicsItemRampart.cpp
@@ -29,10 +29,12 @@
 // RoomGraphicsItemRampart
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

-RoomGraphicsItemRampart::RoomGraphicsItemRampart(const RampartEntity& entity, const QSize& cellSize, QGraphicsItem* parent /* = nullptr */)
+RoomGraphicsItemRampart::RoomGraphicsItemRampart(const RampartEntity& entity, const QString& userId, const QSize& cellSize, QGraphicsItem* parent /* = nullptr */)
     : _super(ItemType_Rampart, entity, cellSize, ItemZOrder_Rampart, parent),
       _rampartRenderer(this) {

+   _rampartRenderer.setOwner(entity.user() == userId ?
+       EntityRenderer::OwnerType::OwnerType_Player : EntityRenderer::OwnerType::OwnerType_Enemy);
     _rampartRenderer.setNeighbours(entity.neighbours());
 }

diff --git a/widgets/room/entities/RoomGraphicsItemRampart.h b/widgets/room/entities/RoomGraphicsItemRampart.h
index d68d5d3..92f2e57 100644
--- a/widgets/room/entities/RoomGraphicsItemRampart.h
+++ b/widgets/room/entities/RoomGraphicsItemRampart.h
@@ -57,7 +57,7 @@ class RoomGraphicsItemRampart : public TRoomGraphicsItem<RampartEntity> {
     typedef TRoomGraphicsItem<RampartEntity> _super;

 public:
-    RoomGraphicsItemRampart(const RampartEntity& entity, const QSize& cellSize, QGraphicsItem* parent = nullptr);
+    RoomGraphicsItemRampart(const RampartEntity& entity, const QString& userId, const QSize& cellSize, QGraphicsItem* parent = nullptr);
     virtual ~RoomGraphicsItemRampart();

 private: 
MrShecks commented 4 years ago

Hi Vort,

Thanks for looking into this. I am not sure if it is something I will be fixing soon but it is on my long list of things to do. At the moment I am working in a new branch making a lot of changes cleaning up and restructuring the code so that that I can get it into a better state to continue working on it.

My priority will be to get the server connection UI in place first so it's easier for people to connect without having to modify the code.

For the player owned colouring I have plans already on how to implement it cleanly. If you look at the RoomEntity.h/cpp in models/room you will see that, for some entities (Walls, Roads & Ramparts), when the entity is created or updated I add a new object called "$_meta_data" to the entity JSON to hold additional data specific to Screep Studio that I need the upper layers.

My plan is to add something like "is_player_owned" or "is_hero_owned" to the "$_meta_data" for all entities that will be set to TRUE if the entity belongs to the current player. This will then be exposed on the RoomEntity base class which I will use during rendering to decide on the colour scheme.

I'd like to avoid having to do a large merge when I pull the "connection-ui" branch to master so I want to get that done before making any more changes in master. so it might be a while before this gets fixed.

Vort commented 4 years ago

My priority will be to get the server connection UI in place first so it's easier for people to connect without having to modify the code.

Temporary solution may be to hold connection data inside configuration file.

Please also think about something like auto-login. So that user will not be forced to enter login & password every time he launches client.