Crossfire Server, Trunk  R22047
CRETreasurePanel.cpp
Go to the documentation of this file.
1 #include <Qt>
2 
3 extern "C" {
4 #include "global.h"
5 #include "treasure.h"
6 }
7 
8 #include "CRETreasurePanel.h"
9 #include "CREUtils.h"
10 
11 #include "assets.h"
12 #include "AssetsManager.h"
13 
15 {
16  QGridLayout* layout = new QGridLayout(this);
17 
18  myUsing = new QTreeWidget(this);
19  myUsing->setColumnCount(1);
20  myUsing->setHeaderLabel(tr("Used by"));
21  myUsing->setIconSize(QSize(32, 32));
22  layout->addWidget(myUsing, 1, 1, 2, 1);
23 
24  layout->addWidget(new QLabel("Difficulty:"), 1, 2);
25  layout->addWidget(myDifficulty = new QSpinBox(this), 1, 3);
26  myDifficulty->setRange(0, 150);
27  myDifficulty->setValue(150);
28  QPushButton* generate = new QPushButton("generate", this);
29  connect(generate, SIGNAL(clicked(bool)), this, SLOT(onGenerate(bool)));
30  layout->addWidget(generate, 1, 4);
31  layout->addWidget(myGenerated = new QTreeWidget(this), 2, 2, 1, 3);
32  myGenerated->setHeaderLabel(tr("Generation result"));
33  myGenerated->setIconSize(QSize(32, 32));
34 }
35 
37 {
38  myUsing->clear();
39  myTreasure = treas;
40 
41  QTreeWidgetItem* root = NULL;
42 
43  QString name = myTreasure->name;
44 
45  getManager()->archetypes()->each([this, &root, &name] (const archetype *arch) {
46  if (arch->clone.randomitems && name == arch->clone.randomitems->name)
47  {
48  if (root == NULL)
49  {
50  root = CREUtils::archetypeNode(NULL);
51  myUsing->addTopLevelItem(root);
52  root->setExpanded(true);
53  }
54  CREUtils::archetypeNode(arch, root);
55  }
56  });
57 
58  root = NULL;
59 
60  getManager()->treasures()->each([this, &root, &name] (treasurelist *list) {
61  for (const treasure *t = list->items; t; t = t->next)
62  {
63  if (t->name == name)
64  {
65  if (root == NULL)
66  {
67  root = CREUtils::treasureNode(NULL);
68  myUsing->addTopLevelItem(root);
69  root->setExpanded(true);
70  }
71  CREUtils::treasureNode(list, root);
72  }
73  }
74  });
75 
76  myGenerated->clear();
77 }
78 
80 {
81  const int difficulty = myDifficulty->value();
82  myGenerated->clear();
83  object* result = object_new(), *item;
84  create_treasure((treasurelist*)myTreasure, result, 0, difficulty, 0);
85  while ((item = result->inv))
86  {
87  identify(result->inv);
88  myGenerated->addTopLevelItem(CREUtils::objectNode(item, NULL));
89 
90  object_remove(item);
91  object_free(item, 0);
92  }
93 
94  object_free(result, 0);
95 }
Treasures * treasures()
Definition: AssetsManager.h:53
void object_free(object *ob, int flags)
Definition: object.c:1348
static QTreeWidgetItem * archetypeNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:15
QTreeWidget * myGenerated
void create_treasure(treasurelist *t, object *op, int flag, int difficulty, int tries)
Definition: treasure.c:252
AssetsManager * getManager()
Definition: assets.cpp:304
object * object_new(void)
Definition: object.c:1011
static char root[500]
Definition: mapper.c:365
QSpinBox * myDifficulty
QTreeWidget * myUsing
CRETreasurePanel(QWidget *parent)
Archetypes * archetypes()
Definition: AssetsManager.h:43
const treasurelist * myTreasure
struct treasurestruct * items
Definition: treasure.h:89
const char * name
Definition: treasure.h:83
static QTreeWidgetItem * objectNode(const object *op, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:29
void onGenerate(bool pressed)
virtual void setItem(const treasurelist *treasure)
object * identify(object *op)
Definition: item.c:1386
struct treasurestruct * next
Definition: treasure.h:66
void object_remove(object *op)
Definition: object.c:1588
Definition: main.c:85
void each(std::function< void(T *)> op)