Crossfire Server, Trunk
CREUtils.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 <QtWidgets>
14 #include "CREUtils.h"
15 #include "CREPixmap.h"
16 #include "CREMapInformation.h"
17 #include "MessageFile.h"
18 #include "scripts/ScriptFile.h"
19 
20 extern "C" {
21 #include "global.h"
22 #include "recipe.h"
23 #include "libproto.h"
24 }
25 
26 QTreeWidgetItem* CREUtils::archetypeNode(QTreeWidgetItem* parent)
27 {
28  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Archetypes")));
29  return item;
30 }
31 
32 QTreeWidgetItem* CREUtils::archetypeNode(const archt* arch, QTreeWidgetItem* parent)
33 {
34  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(arch->name));
35  item->setIcon(0, CREPixmap::getIcon(arch->clone.face ? arch->clone.face->number : 0));
36  return item;
37 }
38 
39 QTreeWidgetItem* CREUtils::objectNode(const object* op, QTreeWidgetItem* parent)
40 {
41  char name[500];
42  query_name(op, name, sizeof(name));
43  QString n;
44  if (op->nrof > 1)
45  n.append(QString::number(op->nrof)).append(" ");
46  n.append(name);
47  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(n));
48  item->setIcon(0, CREPixmap::getIcon(op->face->number));
49  return item;
50 }
51 
52 QTreeWidgetItem* CREUtils::treasureNode(QTreeWidgetItem* parent)
53 {
54  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Treasures")));
55  return item;
56 }
57 
58 QTreeWidgetItem* CREUtils::treasureNode(const treasurelist* list, QTreeWidgetItem* parent)
59 {
60  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(list->name));
61  if (list->total_chance == 0)
62  item->setIcon(0, CREPixmap::getTreasureIcon());
63  else
64  item->setIcon(0, CREPixmap::getTreasureOneIcon());
65  return item;
66 }
67 
68 QTreeWidgetItem* CREUtils::treasureNode(const treasure* treasure, const treasurelist* list, QTreeWidgetItem* parent)
69 {
70  QTreeWidgetItem* item;
71  if (treasure->item)
72  {
74  if (treasure->next_yes)
75  {
76  QTreeWidgetItem* node = new QTreeWidgetItem(item, QStringList(QTreeWidget::tr("Yes")));
77  node->setIcon(0, CREPixmap::getTreasureYesIcon());
79  }
80  if (treasure->next_no)
81  {
82  QTreeWidgetItem* node = new QTreeWidgetItem(item, QStringList(QTreeWidget::tr("No")));
83  node->setIcon(0, CREPixmap::getTreasureNoIcon());
85  }
86  }
87  else
88  {
89  if (treasure->name && strcmp(treasure->name, "NONE") == 0)
90  item = new QTreeWidgetItem(parent, QStringList(QString(QTreeWidget::tr("Nothing"))));
91  else
92  {
94  item = CREUtils::treasureNode(other, parent);
95  }
96  }
97 
98  if (list->total_chance != 0)
99  {
100  item->setText(0, QTreeWidget::tr("%1 (%2%3%, %4 chances on %5)")
101  .arg(item->text(0))
102  .arg(treasure->nrof > 0 ? QTreeWidget::tr("1 to %2, ").arg(treasure->nrof) : "")
103  .arg(qRound((float)100 * treasure->chance / list->total_chance))
104  .arg(treasure->chance)
105  .arg(list->total_chance));
106  }
107  else
108  {
109  item->setText(0, QTreeWidget::tr("%1 (%2%3%)")
110  .arg(item->text(0))
111  .arg(treasure->nrof > 0 ? QTreeWidget::tr("1 to %2, ").arg(treasure->nrof) : "")
112  .arg(treasure->chance));
113  }
114 
115  return item;
116 }
117 
118 QTreeWidgetItem* CREUtils::formulaeNode(const recipe* recipe, QTreeWidgetItem* parent)
119 {
120  QString title;
121  const archt* base = NULL;
122 
123  if (recipe->arch_names == 0)
124  {
125  title = QString("%1 (no archetype?)").arg(recipe->title);
126  }
127  else
128  {
129  base = find_archetype(recipe->arch_name[0]);
130  if (!base)
131  {
132  title = QString("%1 (no archetype?)").arg(recipe->title);
133  }
134  else if (strcmp(recipe->title, "NONE") == 0)
135  {
136  if (base->clone.title)
137  title = QString("%1 %2").arg(base->clone.name, base->clone.title);
138  else
139  title = base->clone.name;
140  }
141  else
142  {
143  title = QString("%1 of %2").arg(base->clone.name, recipe->title);
144  }
145  }
146  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(title));
147  const Face *face = recipe_get_face(recipe);
148  if (!face && base != NULL)
149  {
150  face = base->clone.face;
151  }
152  if (face)
153  item->setIcon(0, CREPixmap::getIcon(face->number));
154 
155  return item;
156 }
157 
158 QTreeWidgetItem* CREUtils::faceNode(QTreeWidgetItem* parent)
159 {
160  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Faces")));
161  return item;
162 }
163 
164 QTreeWidgetItem* CREUtils::faceNode(const Face* face, QTreeWidgetItem* parent)
165 {
166  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(face->name));
167  item->setIcon(0, CREPixmap::getIcon(face->number));
168  return item;
169 }
170 
171 QTreeWidgetItem* CREUtils::mapNode(QTreeWidgetItem *parent)
172 {
173  return new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Maps")));
174 }
175 
176 QTreeWidgetItem* CREUtils::mapNode(const CREMapInformation* map, QTreeWidgetItem *parent)
177 {
178  return new QTreeWidgetItem(parent, QStringList(QObject::tr("%1 [%2]").arg(map->name(), map->path())));
179 }
180 
181 void CREUtils::addCountSuffix(QTreeWidgetItem *item, int column)
182 {
183  item->setText(column, QTreeWidget::tr("%1 (%2)").arg(item->text(column)).arg(item->childCount()));
184 }
recipestruct::arch_names
size_t arch_names
Definition: recipe.h:12
Face::name
sstring name
Definition: face.h:19
treasurestruct::item
struct archt * item
Definition: treasure.h:64
Face
Definition: face.h:14
treasurestruct::chance
uint8_t chance
Definition: treasure.h:70
global.h
treasurestruct::next_yes
struct treasurestruct * next_yes
Definition: treasure.h:67
obj::face
const Face * face
Definition: object.h:336
CREUtils::addCountSuffix
static void addCountSuffix(QTreeWidgetItem *item, int column=0)
Definition: CREUtils.cpp:181
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
CREMapInformation.h
CREUtils::faceNode
static QTreeWidgetItem * faceNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:158
CREUtils::archetypeNode
static QTreeWidgetItem * archetypeNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:26
CREUtils.h
CREPixmap::getTreasureIcon
static QIcon getTreasureIcon()
Definition: CREPixmap.cpp:104
guildoracle.list
list
Definition: guildoracle.py:87
recipe.h
CREPixmap::getIcon
static QIcon getIcon(uint16_t faceNumber)
Definition: CREPixmap.cpp:67
recipestruct::title
sstring title
Definition: recipe.h:11
find_treasurelist
treasurelist * find_treasurelist(const char *name)
Definition: assets.cpp:263
CREUtils::mapNode
static QTreeWidgetItem * mapNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:171
treasurestruct::next_no
struct treasurestruct * next_no
Definition: treasure.h:68
treasurestruct
Definition: treasure.h:63
archt
Definition: object.h:469
CREMapInformation
Definition: CREMapInformation.h:27
recipestruct
Definition: recipe.h:10
disinfect.map
map
Definition: disinfect.py:4
titlestruct
Definition: readable.c:107
obj::name
sstring name
Definition: object.h:314
ScriptFile.h
CREPixmap::getTreasureYesIcon
static QIcon getTreasureYesIcon()
Definition: CREPixmap.cpp:116
query_name
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:585
Face::number
uint16_t number
Definition: face.h:15
recipestruct::arch_name
char ** arch_name
Definition: recipe.h:13
recipe_get_face
const Face * recipe_get_face(const recipe *rp)
Definition: recipe.c:797
title
struct titlestruct title
CREUtils::formulaeNode
static QTreeWidgetItem * formulaeNode(const recipe *recipe, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:118
obj::title
sstring title
Definition: object.h:320
MessageFile.h
archt::clone
object clone
Definition: object.h:473
item
Definition: item.py:1
CREUtils::objectNode
static QTreeWidgetItem * objectNode(const object *op, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:39
give.op
op
Definition: give.py:33
find_archetype
archetype * find_archetype(const char *name)
Definition: assets.cpp:284
CREPixmap::getTreasureOneIcon
static QIcon getTreasureOneIcon()
Definition: CREPixmap.cpp:110
treasurestruct::nrof
uint16_t nrof
Definition: treasure.h:76
CREUtils::treasureNode
static QTreeWidgetItem * treasureNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:52
say.item
dictionary item
Definition: say.py:149
CREPixmap::getTreasureNoIcon
static QIcon getTreasureNoIcon()
Definition: CREPixmap.cpp:122
CREPixmap.h
treasurestruct::name
sstring name
Definition: treasure.h:65
libproto.h
treasureliststruct
Definition: treasure.h:82
give.name
name
Definition: give.py:27