Crossfire Server, Trunk  R21226
CREUtils.cpp
Go to the documentation of this file.
1 #include <QtWidgets>
2 #include "CREUtils.h"
3 #include "CREPixmap.h"
4 #include "CREMapInformation.h"
5 #include "Quest.h"
6 #include "MessageFile.h"
7 
8 extern "C" {
9 #include "global.h"
10 #include "recipe.h"
11 #include "libproto.h"
12 #include "ScriptFile.h"
13 }
14 
15 QTreeWidgetItem* CREUtils::archetypeNode(QTreeWidgetItem* parent)
16 {
17  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Archetypes")));
18  return item;
19 }
20 
21 QTreeWidgetItem* CREUtils::archetypeNode(const archt* arch, QTreeWidgetItem* parent)
22 {
23  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(arch->name));
24  //item->setData(0, Qt::UserRole, arch->getId());
25  item->setIcon(0, CREPixmap::getIcon(arch->clone.face->number));
26  return item;
27 }
28 
29 QTreeWidgetItem* CREUtils::objectNode(const object* op, QTreeWidgetItem* parent)
30 {
31  char name[500];
32  query_name(op, name, sizeof(name));
33  QString n;
34  if (op->nrof > 1)
35  n.append(QString::number(op->nrof)).append(" ");
36  n.append(name);
37  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(n));
38  item->setIcon(0, CREPixmap::getIcon(op->face->number));
39  return item;
40 }
41 
42 QTreeWidgetItem* CREUtils::artifactNode(QTreeWidgetItem* parent)
43 {
44  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Artifacts")));
45  return item;
46 }
47 
48 QTreeWidgetItem* CREUtils::artifactNode(const artifact* arti, QTreeWidgetItem* parent)
49 {
50  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(arti->item->name));
51  if (arti->item->face != NULL && arti->item->face != blank_face)
52  item->setIcon(0, CREPixmap::getIcon(arti->item->face->number));
53  else
54  {
55  int face = 0;
56  for (linked_char* allowed = arti->allowed; allowed; allowed = allowed->next)
57  {
58  if (allowed->name[0] == '!')
59  continue;
60  const archt* specific = try_find_archetype(allowed->name);
61  if (!specific)
62  specific = find_archetype_by_object_name(allowed->name);
63  if (specific && specific->clone.face != NULL)
64  {
65  face = specific->clone.face->number;
66  break;
67  }
68  }
69 
70  if (face == 0)
71  {
72  const archt* generic = get_archetype_by_type_subtype(arti->item->type, -1);
73  if (generic != NULL && generic->clone.face != NULL)
74  face = generic->clone.face->number;
75  }
76 
77  if (face)
78  item->setIcon(0, CREPixmap::getIcon(face));
79  }
80  return item;
81 }
82 
83 QTreeWidgetItem* CREUtils::treasureNode(QTreeWidgetItem* parent)
84 {
85  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Treasures")));
86  return item;
87 }
88 
89 QTreeWidgetItem* CREUtils::treasureNode(const treasurelist* list, QTreeWidgetItem* parent)
90 {
91  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(list->name));
92  if (list->total_chance == 0)
93  item->setIcon(0, CREPixmap::getTreasureIcon());
94  else
95  item->setIcon(0, CREPixmap::getTreasureOneIcon());
96  return item;
97 }
98 
99 QTreeWidgetItem* CREUtils::treasureNode(const treasure* treasure, const treasurelist* list, QTreeWidgetItem* parent)
100 {
101  QTreeWidgetItem* item;
102  if (treasure->item)
103  {
104  item = CREUtils::archetypeNode(treasure->item, parent);
105  if (treasure->next_yes)
106  {
107  QTreeWidgetItem* node = new QTreeWidgetItem(item, QStringList(QTreeWidget::tr("Yes")));
108  node->setIcon(0, CREPixmap::getTreasureYesIcon());
109  CREUtils::treasureNode(treasure->next_yes, list, node);
110  }
111  if (treasure->next_no)
112  {
113  QTreeWidgetItem* node = new QTreeWidgetItem(item, QStringList(QTreeWidget::tr("No")));
114  node->setIcon(0, CREPixmap::getTreasureNoIcon());
115  CREUtils::treasureNode(treasure->next_no, list, node);
116  }
117  }
118  else
119  {
120  if (treasure->name && strcmp(treasure->name, "NONE") == 0)
121  item = new QTreeWidgetItem(parent, QStringList(QString(QTreeWidget::tr("Nothing"))));
122  else
123  {
124  treasurelist* other = find_treasurelist(treasure->name);
125  item = CREUtils::treasureNode(other, parent);
126  }
127  }
128 
129  if (list->total_chance != 0)
130  {
131  item->setText(0, QTreeWidget::tr("%1 (%2%, %3 chances on %4)").arg(item->text(0)).arg(qRound((float)100 * treasure->chance / list->total_chance)).arg(treasure->chance).arg(list->total_chance));
132  }
133  else
134  {
135  item->setText(0, QTreeWidget::tr("%1 (%2%)").arg(item->text(0)).arg(treasure->chance));
136  }
137 
138  return item;
139 }
140 
141 QTreeWidgetItem* CREUtils::formulaeNode(const recipe* recipe, QTreeWidgetItem* parent)
142 {
143  QString title;
144  const archt* base = NULL;
145 
146  if (recipe->arch_names == 0)
147  {
148  title = QString("%1 (no archetype?)").arg(recipe->title);
149  }
150  else
151  {
152  base = find_archetype(recipe->arch_name[0]);
153  if (!base)
154  {
155  title = QString("%1 (no archetype?)").arg(recipe->title);
156  }
157  else if (strcmp(recipe->title, "NONE") == 0)
158  {
159  title = base->clone.name;
160  }
161  else
162  {
163  title = QString("%1 of %2").arg(base->clone.name, recipe->title);
164  }
165  }
166  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(title));
167  if (base != NULL && base->clone.face != 0)
168  item->setIcon(0, CREPixmap::getIcon(base->clone.face->number));
169 
170  return item;
171 }
172 
173 QTreeWidgetItem* CREUtils::faceNode(QTreeWidgetItem* parent)
174 {
175  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Faces")));
176  return item;
177 }
178 
179 QTreeWidgetItem* CREUtils::faceNode(const New_Face* face, QTreeWidgetItem* parent)
180 {
181  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(face->name));
182  item->setIcon(0, CREPixmap::getIcon(face->number));
183  return item;
184 }
185 
186 QTreeWidgetItem* CREUtils::animationNode(QTreeWidgetItem* parent)
187 {
188  return new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Animations")));
189 }
190 
191 QTreeWidgetItem* CREUtils::animationNode(const Animations* anim, QTreeWidgetItem* parent)
192 {
193  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(anim->name));
194  if (anim->num_animations > 0)
195  item->setIcon(0, CREPixmap::getIcon((anim->faces[0] != NULL) ? anim->faces[0]->number : 0));
196  else
197  LOG(llevDebug, "empty animation: %s\n", anim->name);
198  return item;
199 }
200 
201 QTreeWidgetItem* CREUtils::regionNode(const QString& name, int count, QTreeWidgetItem *parent)
202 {
203  return new QTreeWidgetItem(parent, QStringList(QObject::tr("%1 - %2 maps").arg(name).arg(count)));
204 }
205 
206 QTreeWidgetItem* CREUtils::mapNode(QTreeWidgetItem *parent)
207 {
208  return new QTreeWidgetItem(parent, QStringList(QTreeWidget::tr("Maps")));
209 }
210 
211 QTreeWidgetItem* CREUtils::mapNode(const CREMapInformation* map, QTreeWidgetItem *parent)
212 {
213  return new QTreeWidgetItem(parent, QStringList(QObject::tr("%1 [%2]").arg(map->name(), map->path())));
214 }
215 
216 QTreeWidgetItem* CREUtils::questsNode()
217 {
218  return new QTreeWidgetItem(QStringList(QTreeWidget::tr("Quests")));
219 }
220 
221 QTreeWidgetItem* CREUtils::questNode(const Quest* quest, QTreeWidgetItem* parent)
222 {
223  QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList(quest->code()));
224  if (quest->faceNumber() != 0)
225  item->setIcon(0, CREPixmap::getIcon(quest->faceNumber()));
226  return item;
227 }
228 
229 QTreeWidgetItem* CREUtils::messagesNode()
230 {
231  return new QTreeWidgetItem(QStringList(QTreeWidget::tr("NPC dialogs")));
232 }
233 
234 QTreeWidgetItem* CREUtils::messageNode(const MessageFile* message, QTreeWidgetItem* parent)
235 {
236  return new QTreeWidgetItem(parent, QStringList(message->path()));
237 }
238 
239 QTreeWidgetItem* CREUtils::scriptsNode()
240 {
241  return new QTreeWidgetItem(QStringList(QTreeWidget::tr("Scripts")));
242 }
243 QTreeWidgetItem* CREUtils::scriptNode(const ScriptFile* script, QTreeWidgetItem* parent)
244 {
245  return new QTreeWidgetItem(parent, QStringList(script->path()));
246 }
archetype * find_archetype(const char *name)
Definition: arch.c:695
static QTreeWidgetItem * animationNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:186
static QIcon getTreasureIcon()
Definition: CREPixmap.cpp:45
static QTreeWidgetItem * archetypeNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:15
static QTreeWidgetItem * artifactNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:42
Definition: Quest.h:32
const QString & path() const
Definition: ScriptFile.cpp:36
const char * name
Definition: face.h:27
treasurelist * find_treasurelist(const char *name)
Definition: treasure.c:290
static QTreeWidgetItem * questsNode()
Definition: CREUtils.cpp:216
object clone
Definition: object.h:470
const QString & code() const
Definition: Quest.cpp:63
static QIcon getTreasureYesIcon()
Definition: CREPixmap.cpp:57
static QTreeWidgetItem * formulaeNode(const recipe *recipe, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:141
Definition: object.h:465
static QTreeWidgetItem * faceNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:173
struct archt * item
Definition: treasure.h:64
struct titlestruct title
static QIcon getTreasureNoIcon()
Definition: CREPixmap.cpp:63
uint16_t number
Definition: face.h:15
static QTreeWidgetItem * questNode(const Quest *quest, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:221
int16_t total_chance
Definition: treasure.h:84
const char * name
Definition: face.h:20
sstring title
Definition: recipe.h:11
char ** arch_name
Definition: recipe.h:13
static QTreeWidgetItem * scriptNode(const ScriptFile *script, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:243
struct linked_char * next
Definition: global.h:88
const char * name
Definition: object.h:311
linked_char * allowed
Definition: artifact.h:19
static QTreeWidgetItem * scriptsNode()
Definition: CREUtils.cpp:239
archetype * find_archetype_by_object_name(const char *name)
Definition: arch.c:57
uint8_t num_animations
Definition: face.h:28
int faceNumber() const
Definition: Quest.cpp:102
uint32_t nrof
Definition: object.h:333
struct treasurestruct * next_no
Definition: treasure.h:68
archetype * try_find_archetype(const char *name)
Definition: arch.c:666
const New_Face ** faces
Definition: face.h:31
uint8_t chance
Definition: treasure.h:70
const char * name
Definition: treasure.h:83
struct treasurestruct * next_yes
Definition: treasure.h:67
static QIcon getIcon(int faceNumber)
Definition: CREPixmap.cpp:27
New_Face * blank_face
Definition: image.c:39
static QTreeWidgetItem * objectNode(const object *op, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:29
static QTreeWidgetItem * messagesNode()
Definition: CREUtils.cpp:229
archetype * get_archetype_by_type_subtype(int type, int subtype)
Definition: arch.c:136
size_t arch_names
Definition: recipe.h:12
uint8_t type
Definition: object.h:338
static QTreeWidgetItem * regionNode(const QString &name, int count, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:201
static QTreeWidgetItem * messageNode(const MessageFile *message, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:234
const QString & path() const
static QTreeWidgetItem * treasureNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:83
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:623
object * item
Definition: artifact.h:15
const New_Face * face
Definition: object.h:332
const char * name
Definition: treasure.h:65
const char * name
Definition: object.h:466
static QTreeWidgetItem * mapNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:206
static QIcon getTreasureOneIcon()
Definition: CREPixmap.cpp:51