Crossfire Server, Trunk
ArtifactPanel.cpp
Go to the documentation of this file.
1 #include "ArtifactPanel.h"
2 #include "CREUtils.h"
4 
5 #include "assets.h"
6 #include "AssetsManager.h"
7 #include "Archetypes.h"
8 #include "assets/AssetWrapper.h"
10 
12 {
13  myArtifact = NULL;
14 
15  addLineEdit(tr("Name:"), "name");
16  addSpinBox(tr("Chance:"), "chance", 0, 65535, false);
17  myType = addLineEdit(tr("Type:"), nullptr, true);
18 
19  myViaAlchemy = addWidget(QString(), new QLabel(this), false, nullptr, nullptr);
20  myViaAlchemy->setWordWrap(true);
21 
22  myLayout->addWidget(new QLabel(tr("Values:"), this), 5, 0, 1, 2);
23  myValues = new QTextEdit(this);
24  myLayout->addWidget(myValues, 6, 0, 1, 2);
25  myValues->setReadOnly(true);
26 
27  myArchetypes = new QTreeWidget(this);
28  myLayout->addWidget(myArchetypes, 7, 0, 3, 1);
29  myArchetypes->setHeaderLabel("Allowed/forbidden archetypes");
30  myArchetypes->setIconSize(QSize(32, 32));
31  myArchetypes->setRootIsDecorated(false);
32  connect(myArchetypes, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(artifactChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
33 
34  myLayout->addWidget(new QLabel(tr("Result:"), this), 7, 1);
35  myInstance = new QTextEdit(this);
36  myLayout->addWidget(myInstance, 8, 1);
37  myInstance->setReadOnly(true);
38 
39  myLayout->addWidget(myAnimation = new AnimationControl(this), 9, 1);
40  myLayout->addWidget(myFace = new AnimationWidget(this), 9, 1);
41 }
42 
44 {
45  Q_ASSERT(artifact != NULL);
46 
47  const recipelist* list;
48  const recipe* recipe;
49  const archetype* arch;
50  QStringList possible;
51 
52  for (int ing = 1; ; ing++)
53  {
54  list = get_formulalist(ing);
55  if (!list)
56  break;
57  for (recipe = list->items; recipe; recipe = recipe->next)
58  {
59  if (recipe->title == NULL)
60  continue;
61 
62  if (strcmp(recipe->title, artifact->item->name) != 0)
63  continue;
64 
65  for (size_t a = 0; a < recipe->arch_names; a++)
66  {
68  if (!arch)
69  continue;
70  if ((arch->clone.type == artifact->item->type) && legal_artifact_combination(&arch->clone, artifact))
71  {
72  if (!possible.contains(arch->name))
73  possible.append(arch->name);
74  }
75  }
76  }
77  }
78 
79  if (possible.isEmpty())
80  myViaAlchemy->setText(tr("Can't be made via alchemy."));
81  else
82  {
83  if (possible.size() == artifact->allowed_size)
84  myViaAlchemy->setText(tr("Can be made via alchemy."));
85  else
86  {
87  possible.sort();
88  myViaAlchemy->setText(tr("The following archetypes can be used via alchemy: %1").arg(possible.join(tr(", "))));
89  }
90  }
91 }
92 
100 static void addArchetypes(const artifact* artifact, const char* name, bool check, QTreeWidget* root)
101 {
102  QTreeWidgetItem* item = NULL;
103 
105  {
106  if (arch->head || arch->clone.type != artifact->item->type)
107  {
108  return;
109  }
110 
111  if (name == NULL || (check && arch->clone.name && (!strcmp(name, arch->clone.name) || (!strcmp(name, arch->name)))) || (!check && (arch->clone.name && strcmp(name, arch->clone.name)) && strcmp(name, arch->name)))
112  {
113  if (item == NULL)
114  {
115  item = new QTreeWidgetItem(root, QStringList(name == NULL ? "(all)" : name));
116  item->setCheckState(0, check ? Qt::Checked : Qt::Unchecked);
117  item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
118  root->addTopLevelItem(item);
119  item->setExpanded(true);
120  }
121  CREUtils::archetypeNode(arch, item)->setData(0, Qt::UserRole, arch->name);
122  }
123  });
124 }
125 
127 {
129 
130  auto aw = dynamic_cast<ArtifactWrapper *>(item);
131  auto art = (((AssetTWrapper<artifact> *)aw)->wrappedItem());
132  Q_ASSERT(art);
133  myArtifact = art;
134 
135  myType->setText(QString::number(myArtifact->item->type));
136 
138 
139  myArchetypes->clear();
140  myInstance->clear();
141 
142  /* 'allowed' is either the archetype name or the item's name, so check all archetypes for each word */
143  for (const linked_char* allowed = myArtifact->allowed; allowed; allowed = allowed->next) {
144  auto name = allowed->name;
145  bool check = true;
146  if (name[0] == '!') {
147  name = name + 1;
148  check = false;
149  }
150 
152  }
153 
154  /* all items are allowed, so add them */
155  if (myArtifact->allowed == NULL) {
156  addArchetypes(myArtifact, NULL, true, myArchetypes);
157  }
158 
161  char* final = stringbuffer_finish(dump);
162  myValues->setText(final);
163  free(final);
164 }
165 
166 void ArtifactPanel::artifactChanged(QTreeWidgetItem* current, QTreeWidgetItem*)
167 {
168  myAnimation->setVisible(false);
169  myFace->setVisible(false);
170  myInstance->clear();
171  if (!current || current->data(0, Qt::UserRole).toString().isEmpty()) {
172  return;
173  }
174  archt* arch = getManager()->archetypes()->find(current->data(0, Qt::UserRole).toString().toUtf8().constData());
175  if (!arch) {
176  return;
177  }
178 
179  char* desc;
180  object* obj = arch_to_object(arch);
184  desc = stringbuffer_finish(describe_item(obj, NULL, 0, NULL));
185  myInstance->setText(desc);
186  free(desc);
187 
188  if (obj->animation != nullptr) {
189  myAnimation->setVisible(true);
191  } else {
192  myFace->setVisible(true);
193  QList<int> faces;
194  faces.append(obj->face->number);
196  myFace->step();
197  }
198 
200 }
recipestruct::arch_names
size_t arch_names
Definition: recipe.h:12
AssetWrapperPanel
Definition: AssetWrapperPanel.h:16
empty_archetype
EXTERN archetype * empty_archetype
Definition: global.h:142
ArtifactPanel::myFace
AnimationWidget * myFace
Definition: ArtifactPanel.h:32
FREE_OBJ_NO_DESTROY_CALLBACK
#define FREE_OBJ_NO_DESTROY_CALLBACK
Definition: object.h:532
get_formulalist
recipelist * get_formulalist(int i)
Definition: recipe.c:96
object_free
void object_free(object *ob, int flags)
Definition: object.c:1578
ArtifactPanel::ArtifactPanel
ArtifactPanel(QWidget *parent)
Definition: ArtifactPanel.cpp:11
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
ArtifactPanel::computeMadeViaAlchemy
void computeMadeViaAlchemy(const artifact *artifact) const
Definition: ArtifactPanel.cpp:43
obj::face
const Face * face
Definition: object.h:336
give_artifact_abilities
void give_artifact_abilities(object *op, const object *artifact)
Definition: artifact.c:238
ArtifactWrapper
Definition: ArtifactWrapper.h:15
AssetWrapper.h
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
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
AssetWrapperPanel::addLineEdit
QLineEdit * addLineEdit(const QString &label, const char *property, bool readOnly=true)
Definition: AssetWrapperPanel.cpp:39
FLAG_IS_TURNABLE
#define FLAG_IS_TURNABLE
Definition: define.h:256
disinfect.a
a
Definition: disinfect.py:13
CREUtils::archetypeNode
static QTreeWidgetItem * archetypeNode(QTreeWidgetItem *parent)
Definition: CREUtils.cpp:14
CREUtils.h
get_ob_diff
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Definition: object.c:4956
guildoracle.list
list
Definition: guildoracle.py:87
AnimationWidget.h
root
static char root[500]
Definition: mapper.cpp:305
recipestruct::title
sstring title
Definition: recipe.h:11
AssetWrapperPanel::setItem
virtual void setItem(AssetWrapper *item) override
Definition: AssetWrapperPanel.cpp:13
AnimationWidget::setAnimation
void setAnimation(QList< int > faces)
Definition: AnimationWidget.cpp:13
describe_item
StringBuffer * describe_item(const object *op, const object *owner, int use_media_tags, StringBuffer *buf)
Definition: item.c:941
getManager
AssetsManager * getManager()
Definition: assets.cpp:333
AssetWrapperPanel::addSpinBox
QSpinBox * addSpinBox(const QString &label, const char *property, int min=0, int max=100, bool readOnly=true)
Definition: AssetWrapperPanel.cpp:74
AssetsCollection::find
T * find(const Key &name)
Definition: AssetsCollection.h:85
linked_char
Definition: global.h:86
reputation_trigger_connect.check
def check()
Definition: reputation_trigger_connect.py:18
archt
Definition: object.h:470
obj
Definition: object.h:277
recipestruct
Definition: recipe.h:10
AssetWrapperPanel::myLayout
QGridLayout * myLayout
Definition: AssetWrapperPanel.h:37
ArtifactPanel::setItem
virtual void setItem(AssetWrapper *item) override
Definition: ArtifactPanel.cpp:126
obj::name
sstring name
Definition: object.h:314
Face::number
uint16_t number
Definition: face.h:15
AssetWrapper
Definition: AssetWrapper.h:11
ArtifactPanel::myValues
QTextEdit * myValues
Definition: ArtifactPanel.h:29
recipestruct::arch_name
char ** arch_name
Definition: recipe.h:13
dump
Definition: quest.c:859
ArtifactPanel.h
artifactstruct::allowed_size
int allowed_size
Definition: artifact.h:20
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
linked_char::next
struct linked_char * next
Definition: global.h:88
object_give_identified_properties
void object_give_identified_properties(object *op)
Definition: item.c:1344
recipeliststruct
Definition: recipe.h:37
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:135
obj::animation
const Animations * animation
Definition: object.h:422
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
ArtifactPanel::myType
QLineEdit * myType
Definition: ArtifactPanel.h:26
artifactstruct
Definition: artifact.h:14
StringBuffer
Definition: stringbuffer.c:25
AnimationWidget::step
void step()
Definition: AnimationWidget.cpp:19
ArtifactPanel::myInstance
QTextEdit * myInstance
Definition: ArtifactPanel.h:30
obj::type
uint8_t type
Definition: object.h:343
archt::clone
object clone
Definition: object.h:474
AnimationWidget
Definition: AnimationWidget.h:7
item
Definition: item.py:1
ArtifactPanel::artifactChanged
void artifactChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
Definition: ArtifactPanel.cpp:166
find_archetype
archetype * find_archetype(const char *name)
Definition: assets.cpp:282
addArchetypes
static void addArchetypes(const artifact *artifact, const char *name, bool check, QTreeWidget *root)
Definition: ArtifactPanel.cpp:100
AssetWrapperPanel::addWidget
T * addWidget(const QString &label, T *widget, bool sideBySide, const char *property, const char *widgetProperty)
Definition: AssetWrapperPanel.h:44
assets.h
Archetypes.h
recipestruct::next
struct recipestruct * next
Definition: recipe.h:24
AnimationControl
Definition: AnimationControl.h:14
ArtifactPanel::myArtifact
const artifact * myArtifact
Definition: ArtifactPanel.h:23
dragon_attune.faces
dictionary faces
Definition: dragon_attune.py:31
arch_to_object
object * arch_to_object(archetype *at)
Definition: arch.cpp:232
AnimationControl::setAnimation
void setAnimation(const Animations *animation, int facings=-1)
Definition: AnimationControl.cpp:29
artifactstruct::item
object * item
Definition: artifact.h:15
ArtifactWrapper.h
FREE_OBJ_FREE_INVENTORY
#define FREE_OBJ_FREE_INVENTORY
Definition: object.h:531
say.item
dictionary item
Definition: say.py:149
ArtifactPanel::myViaAlchemy
QLabel * myViaAlchemy
Definition: ArtifactPanel.h:27
ArtifactPanel::myArchetypes
QTreeWidget * myArchetypes
Definition: ArtifactPanel.h:28
connect
Definition: connect.py:1
replace.current
current
Definition: replace.py:64
legal_artifact_combination
int legal_artifact_combination(const object *op, const artifact *art)
Definition: artifact.c:260
ArtifactPanel::myAnimation
AnimationControl * myAnimation
Definition: ArtifactPanel.h:31
if
if(!(yy_init))
Definition: loader.c:2626
artifactstruct::allowed
linked_char * allowed
Definition: artifact.h:19
FLAG_IDENTIFIED
#define FLAG_IDENTIFIED
Definition: define.h:261
give.name
name
Definition: give.py:27
recipe
struct recipestruct recipe
AssetTWrapper< artifact >