Crossfire Server, Trunk
MonsterResistances.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2022 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include <QVBoxLayout>
14 #include <QGraphicsScene>
15 #include <QGraphicsView>
16 #include <QLabel>
17 #include <QGraphicsRectItem>
18 
19 #include "MonsterResistances.h"
20 
21 #include "global.h"
22 #include "attack.h"
23 #include "assets.h"
24 #include "AssetsManager.h"
25 
26 class AutoFitView : public QGraphicsView {
27 public:
28  AutoFitView(QGraphicsScene *scene, QWidget *parent) : QGraphicsView(scene, parent) { }
29 
30 protected:
31  virtual void resizeEvent(QResizeEvent *event) override {
32  QGraphicsView::resizeEvent(event);
33  fitInView(scene()->sceneRect());
34  }
35 };
36 
37 MonsterResistances::MonsterResistances(QWidget *parent) : QDialog(parent) {
38  setModal(true);
39  setWindowTitle(tr("Monster resistances"));
40  auto layout = new QVBoxLayout(this);
41 
42  myScene = new QGraphicsScene(this);
43 
44  layout->addWidget(new QLabel(tr("Monster resistances (by steps of 10):")));
45  layout->addWidget(myDisplay = new AutoFitView(myScene, this));
46 
47  myResistances[ATNR_FIRE] = Qt::red;
48  myResistances[ATNR_COLD] = QColor(0, 255, 255);
49  myResistances[ATNR_ELECTRICITY] = QColor(0, 0, 255);
50  myResistances[ATNR_POISON] = Qt::green;
51 
52  buildGraph();
53 }
54 
55 #define SCALE 5
56 
58  std::map<int, std::map<int, int>> values;
59  int maxCount = 0, minResist = INT_MAX, maxResist = INT_MIN;
60 
61  getManager()->archetypes()->each([&] (const archetype *arch) {
62  if (!QUERY_FLAG(&arch->clone, FLAG_MONSTER) || arch->head) {
63  return;
64  }
65 
66  auto mr = arch->clone.resist;
67  for (auto r : myResistances) {
68  int val = mr[r.first] / 10;
69  minResist = qMin(minResist, val);
70  maxResist = qMax(maxResist, val);
71  values[r.first][val]++;
72  maxCount = qMax(values[r.first][val], maxCount);
73  }
74  });
75 
76  myScene->clear();
77 
78  int shift = 0;
79  for (auto r : myResistances) {
80  QPen p(r.second);
81  QBrush br(r.second);
82  for (int i = minResist; i <= maxResist; i++) {
83  if (values[r.first][i] == 0) {
84  continue;
85  }
86  auto item = myScene->addRect(qreal((i - minResist) * SCALE * myResistances.size() + shift), qreal(maxCount - values[r.first][i]), qreal(SCALE - 1), qreal(values[r.first][i]), p, br);
87  item->setToolTip(tr("%1% %2: %3 monsters").arg(i * 10).arg(resist_plus[r.first]).arg(values[r.first][i]));
88  }
89  shift += SCALE;
90  }
91 
92  myScene->setSceneRect(0, 0, (maxResist - minResist + 1) * SCALE * myResistances.size(), maxCount);
93  myDisplay->fitInView(myScene->sceneRect());
94 }
global.h
layout
Definition: main.cpp:84
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
AssetsManager.h
AutoFitView
Definition: MonsterResistances.cpp:26
getManager
AssetsManager * getManager()
Definition: assets.cpp:305
MonsterResistances::myResistances
std::map< int, QColor > myResistances
Definition: MonsterResistances.h:34
attack.h
MonsterResistances::buildGraph
void buildGraph()
Definition: MonsterResistances.cpp:57
MonsterResistances::myScene
QGraphicsScene * myScene
Definition: MonsterResistances.h:33
archetype
Definition: object.h:483
ATNR_POISON
#define ATNR_POISON
Definition: attack.h:59
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:158
FLAG_MONSTER
#define FLAG_MONSTER
Definition: define.h:245
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
resist_plus
const char *const resist_plus[NROFATTACKS]
Definition: init.cpp:49
MonsterResistances::myDisplay
QGraphicsView * myDisplay
Definition: MonsterResistances.h:32
ATNR_FIRE
#define ATNR_FIRE
Definition: attack.h:51
MonsterResistances.h
item
Definition: item.py:1
assets.h
AutoFitView::resizeEvent
virtual void resizeEvent(QResizeEvent *event) override
Definition: MonsterResistances.cpp:31
SCALE
#define SCALE
Definition: MonsterResistances.cpp:55
animate.event
event
DIALOGCHECK MINARGS 1 MAXARGS 2
Definition: animate.py:17
MonsterResistances::MonsterResistances
MonsterResistances(QWidget *parent)
Definition: MonsterResistances.cpp:37
ATNR_COLD
#define ATNR_COLD
Definition: attack.h:53
ATNR_ELECTRICITY
#define ATNR_ELECTRICITY
Definition: attack.h:52
ring_occidental_mages.r
r
Definition: ring_occidental_mages.py:6
AutoFitView::AutoFitView
AutoFitView(QGraphicsScene *scene, QWidget *parent)
Definition: MonsterResistances.cpp:28