Crossfire Server, Trunk  R22047
CREArtifactPanel.cpp
Go to the documentation of this file.
1 #include <QtWidgets>
2 
3 extern "C" {
4 #include "global.h"
5 #include "artifact.h"
6 }
7 
8 #include "CREArtifactPanel.h"
9 #include "CREUtils.h"
10 #include "CREAnimationWidget.h"
11 
12 #include "assets.h"
13 #include "AssetsManager.h"
14 #include "Archetypes.h"
15 
17 {
18  myArtifact = NULL;
19 
20  QGridLayout* layout = new QGridLayout(this);
21 
22  QLabel* label = new QLabel(this);
23  label->setText("Name:");
24  layout->addWidget(label, 1, 1);
25  myName = new QLineEdit(this);
26  layout->addWidget(myName, 1, 2);
27  myName->setReadOnly(true);
28 
29  label = new QLabel(this);
30  label->setText("Chance:");
31  layout->addWidget(label, 2, 1);
32  myChance = new QLineEdit(this);
33  layout->addWidget(myChance, 2, 2);
34  myChance->setReadOnly(true);
35 
36  label = new QLabel(this);
37  label->setText("Type:");
38  layout->addWidget(label, 3, 1);
39  myType = new QLineEdit(this);
40  layout->addWidget(myType, 3, 2);
41  myType->setReadOnly(true);
42 
43  myViaAlchemy = new QLabel(this);
44  myViaAlchemy->setWordWrap(true);
45  layout->addWidget(myViaAlchemy, 4, 1, 1, 2);
46 
47  layout->addWidget(new QLabel(tr("Values:"), this), 5, 1, 1, 2);
48  myValues = new QTextEdit(this);
49  layout->addWidget(myValues, 6, 1, 1, 2);
50  myValues->setReadOnly(true);
51 
52  myArchetypes = new QTreeWidget(this);
53  layout->addWidget(myArchetypes, 7, 1, 3, 1);
54  myArchetypes->setHeaderLabel("Allowed/forbidden archetypes");
55  myArchetypes->setIconSize(QSize(32, 32));
56  myArchetypes->setRootIsDecorated(false);
57  connect(myArchetypes, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(artifactChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
58 
59  layout->addWidget(new QLabel(tr("Result:"), this), 7, 2);
60  myInstance = new QTextEdit(this);
61  layout->addWidget(myInstance, 8, 2);
62  myInstance->setReadOnly(true);
63 
64  layout->addWidget(myAnimation = new CREAnimationControl(this), 9, 2);
65  layout->addWidget(myFace = new CREAnimationWidget(this), 9, 2);
66 }
67 
69 {
70  Q_ASSERT(artifact != NULL);
71 
72  const recipelist* list;
73  const recipe* recipe;
74  const archetype* arch;
75  QStringList possible;
76 
77  for (int ing = 1; ; ing++)
78  {
79  list = get_formulalist(ing);
80  if (!list)
81  break;
82  for (recipe = list->items; recipe; recipe = recipe->next)
83  {
84  if (recipe->title == NULL)
85  continue;
86 
87  if (strcmp(recipe->title, artifact->item->name) != 0)
88  continue;
89 
90  for (size_t a = 0; a < recipe->arch_names; a++)
91  {
92  arch = find_archetype(recipe->arch_name[a]);
93  if (!arch)
94  continue;
95  if ((arch->clone.type == artifact->item->type) && legal_artifact_combination(&arch->clone, artifact))
96  {
97  if (!possible.contains(arch->name))
98  possible.append(arch->name);
99  }
100  }
101  }
102  }
103 
104  if (possible.isEmpty())
105  myViaAlchemy->setText(tr("Can't be made via alchemy."));
106  else
107  {
108  if (possible.size() == artifact->allowed_size)
109  myViaAlchemy->setText(tr("Can be made via alchemy."));
110  else
111  {
112  possible.sort();
113  myViaAlchemy->setText(tr("The following archetypes can be used via alchemy: %1").arg(possible.join(tr(", "))));
114  }
115  }
116 }
117 
125 static void addArchetypes(const artifact* artifact, const char* name, bool check, QTreeWidget* root)
126 {
127  QTreeWidgetItem* item = NULL;
128  item = NULL;
129 
130  getManager()->archetypes()->each([&artifact, &name, &check, &root, &item] (archetype *arch)
131  {
132  if (arch->head || arch->clone.type != artifact->item->type)
133  {
134  return;
135  }
136 
137  if (name == NULL || (check && (!strcmp(name, arch->clone.name) || (!strcmp(name, arch->name)))) || (!check && strcmp(name, arch->clone.name) && strcmp(name, arch->name)))
138  {
139  if (item == NULL)
140  {
141  item = new QTreeWidgetItem(root, QStringList(name == NULL ? "(all)" : name));
142  item->setCheckState(0, check ? Qt::Checked : Qt::Unchecked);
143  root->addTopLevelItem(item);
144  item->setExpanded(true);
145  }
146  CREUtils::archetypeNode(arch, item)->setData(0, Qt::UserRole, arch->name);
147  }
148  });
149 }
150 
152 {
153  Q_ASSERT(artifact);
155 
156  myName->setText(artifact->item->name);
157  myChance->setText(QString::number(artifact->chance));
158  myType->setText(QString::number(artifact->item->type));
159 
160  computeMadeViaAlchemy(artifact);
161 
162  const char* name;
163  bool check;
164 
165  myArchetypes->clear();
166  myInstance->clear();
167 
168  /* 'allowed' is either the archetype name or the item's name, so check all archetypes for each word */
169  for (const linked_char* allowed = artifact->allowed; allowed; allowed = allowed->next)
170  {
171  name = allowed->name;
172  if (name[0] == '!')
173  {
174  name = name + 1;
175  check = false;
176  }
177  else
178  check = true;
179 
180  addArchetypes(myArtifact, name, check, myArchetypes);
181  }
182 
183  /* all items are allowed, so add them */
184  if (artifact->allowed == NULL)
185  {
186  addArchetypes(myArtifact, NULL, true, myArchetypes);
187  }
188 
190  get_ob_diff(dump, myArtifact->item, &empty_archetype->clone);
191  char* final = stringbuffer_finish(dump);
192  myValues->setText(final);
193  free(final);
194 }
195 
196 void CREArtifactPanel::artifactChanged(QTreeWidgetItem* current, QTreeWidgetItem*)
197 {
198  myAnimation->setVisible(false);
199  myFace->setVisible(false);
200  myInstance->clear();
201  if (!current || current->data(0, Qt::UserRole).toString().isEmpty())
202  {
203  return;
204  }
205  archt* arch = try_find_archetype(current->data(0, Qt::UserRole).toString().toUtf8().constData());
206  if (!arch)
207  {
208  return;
209  }
210 
211  char* desc;
212  object* obj = arch_to_object(arch);
216  desc = stringbuffer_finish(describe_item(obj, NULL, 0, NULL));
217  myInstance->setText(desc);
218  free(desc);
219 
220  if (obj->animation != 0)
221  {
222  myAnimation->setVisible(true);
223  myAnimation->setAnimation(obj->animation, QUERY_FLAG(obj, FLAG_IS_TURNABLE) ? 8 : -1);
224  }
225  else
226  {
227  myFace->setVisible(true);
228  QList<int> faces;
229  faces.append(obj->face->number);
230  myFace->setAnimation(faces);
231  myFace->step();
232  }
233 
234  object_free(obj, FREE_OBJ_FREE_INVENTORY | FREE_OBJ_NO_DESTROY_CALLBACK);
235 }
static int dump(const std::set< std::string > &items, const char *name)
void object_free(object *ob, int flags)
Definition: object.c:1348
void object_give_identified_properties(object *op)
Definition: item.c:1324
#define SET_FLAG(xyz, p)
Definition: define.h:223
static QTreeWidgetItem * archetypeNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:15
static void addArchetypes(const artifact *artifact, const char *name, bool check, QTreeWidget *root)
void setAnimation(QList< int > faces)
CREArtifactPanel(QWidget *parent)
QLineEdit * myName
archetype * find_archetype(const char *name)
Definition: assets.cpp:253
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
void artifactChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
QLineEdit * myType
struct artifactstruct artifact
QLineEdit * myChance
archetype * try_find_archetype(const char *name)
Definition: assets.cpp:257
AssetsManager * getManager()
Definition: assets.cpp:304
const artifact * myArtifact
EXTERN archetype * empty_archetype
Definition: global.h:143
int legal_artifact_combination(const object *op, const artifact *art)
Definition: artifact.c:221
sstring title
Definition: recipe.h:11
char ** arch_name
Definition: recipe.h:13
object * arch_to_object(archetype *at)
Definition: arch.cpp:232
static char root[500]
Definition: mapper.c:365
CREAnimationControl * myAnimation
QTextEdit * myValues
virtual void setItem(const artifact *artifact)
struct linked_char * next
Definition: global.h:88
#define FLAG_IDENTIFIED
Definition: define.h:261
struct recipestruct recipe
linked_char * allowed
Definition: artifact.h:19
Archetypes * archetypes()
Definition: AssetsManager.h:43
CREAnimationWidget * myFace
void setAnimation(const Animations *animation, int facings=-1)
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
void give_artifact_abilities(object *op, const object *artifact)
Definition: artifact.c:203
StringBuffer * describe_item(const object *op, const object *owner, int use_media_tags, StringBuffer *buf)
Definition: item.c:935
int allowed_size
Definition: artifact.h:20
size_t arch_names
Definition: recipe.h:12
QTreeWidget * myArchetypes
struct recipestruct * next
Definition: recipe.h:24
void computeMadeViaAlchemy(const artifact *artifact) const
object * item
Definition: artifact.h:15
uint16_t chance
Definition: artifact.h:16
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Definition: object.c:4724
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
struct recipestruct * items
Definition: recipe.h:40
recipelist * get_formulalist(int i)
Definition: recipe.c:96
QTextEdit * myInstance
Definition: main.c:85
void each(std::function< void(T *)> op)
#define FLAG_IS_TURNABLE
Definition: define.h:256