Crossfire Server, Trunk
ArtifactPanel.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 "ArtifactPanel.h"
14 #include "CREUtils.h"
16 
17 #include "assets.h"
18 #include "AssetsManager.h"
19 #include "Archetypes.h"
20 #include "assets/AssetWrapper.h"
22 #include "ResourcesManager.h"
23 
24 ArtifactPanel::ArtifactPanel(QWidget* parent, ResourcesManager *resources)
25  : AssetTWrapperPanel(parent), myResources(resources)
26 {
27  addLineEdit(tr("Name:"), "name");
28  addSpinBox(tr("Chance:"), "chance", 0, 65535, false);
29  myType = addLineEdit(tr("Type:"), nullptr, true);
30  myOrigin = addLabel(tr("File:"), nullptr, true);
31 
32  myViaAlchemy = addWidget(QString(), new QLabel(this), false, nullptr, nullptr);
33  myViaAlchemy->setWordWrap(true);
34 
35  myValues = addTextEdit(tr("Values:"), nullptr, true);
36 
37  myArchetypes = addWidget(QString(), new QTreeWidget(this), false, nullptr, nullptr);
38  myArchetypes->setHeaderLabel(tr("Allowed/forbidden archetypes"));
39  myArchetypes->setIconSize(QSize(32, 32));
40  myArchetypes->setRootIsDecorated(false);
41  connect(myArchetypes, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(artifactChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
42 
43  myInstance = addTextEdit(tr("Result:"), nullptr, true);
44 
45  myAnimation = addWidget(QString(), new AnimationControl(this), false, nullptr, nullptr);
46  myFace = addWidget(QString(), new AnimationWidget(this), false, nullptr, nullptr);
47 }
48 
50 {
51  Q_ASSERT(artifact != NULL);
52 
53  const recipelist* list;
54  const recipe* recipe;
55  const archetype* arch;
56  QStringList possible;
57 
58  for (int ing = 1; ; ing++)
59  {
60  list = get_formulalist(ing);
61  if (!list)
62  break;
63  for (recipe = list->items; recipe; recipe = recipe->next)
64  {
65  if (recipe->title == NULL)
66  continue;
67 
68  if (strcmp(recipe->title, artifact->item->name) != 0)
69  continue;
70 
71  for (size_t a = 0; a < recipe->arch_names; a++)
72  {
74  if (!arch)
75  continue;
76  if ((arch->clone.type == artifact->item->type) && legal_artifact_combination(&arch->clone, artifact))
77  {
78  if (!possible.contains(arch->name))
79  possible.append(arch->name);
80  }
81  }
82  }
83  }
84 
85  if (possible.isEmpty())
86  myViaAlchemy->setText(tr("Can't be made via alchemy."));
87  else
88  {
89  if (possible.size() == static_cast<int>(artifact->allowed.size()))
90  myViaAlchemy->setText(tr("Can be made via alchemy."));
91  else
92  {
93  possible.sort();
94  myViaAlchemy->setText(tr("The following archetypes can be used via alchemy: %1").arg(possible.join(tr(", "))));
95  }
96  }
97 }
98 
106 static void addArchetypes(const artifact* artifact, const char* name, bool check, QTreeWidget* root)
107 {
108  QTreeWidgetItem* item = NULL;
109 
111  {
112  if (arch->head || arch->clone.type != artifact->item->type)
113  {
114  return;
115  }
116 
117  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)))
118  {
119  if (item == NULL)
120  {
121  item = new QTreeWidgetItem(root, QStringList(name == NULL ? "(all)" : name));
122  item->setCheckState(0, check ? Qt::Checked : Qt::Unchecked);
123  item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
124  root->addTopLevelItem(item);
125  item->setExpanded(true);
126  }
127  CREUtils::archetypeNode(arch, item)->setData(0, Qt::UserRole, arch->name);
128  }
129  });
130 }
131 
133 {
134  myType->setText(QString::number(myItem->item->type));
135  myOrigin->setText(QString::fromStdString(myResources->originOf(myItem)));
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 auto allowed : myItem->allowed) {
144  bool check = true;
145  auto name = allowed;
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 (myItem->allowed.empty()) {
156  addArchetypes(myItem, 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  archetype* 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);
190  myAnimation->setAnimation(obj->animation, QUERY_FLAG(obj, FLAG_IS_TURNABLE) ? 8 : -1);
191  } else {
192  myFace->setVisible(true);
193  QList<int> faces;
194  faces.append(obj->face->number);
196  myFace->step();
197  }
198 
200 }
ArtifactPanel::myFace
AnimationWidget * myFace
Definition: ArtifactPanel.h:47
FREE_OBJ_NO_DESTROY_CALLBACK
#define FREE_OBJ_NO_DESTROY_CALLBACK
Definition: object.h:545
get_formulalist
recipelist * get_formulalist(int i)
Definition: recipe.cpp:98
ArtifactPanel::computeMadeViaAlchemy
void computeMadeViaAlchemy(const artifact *artifact) const
Definition: ArtifactPanel.cpp:49
AssetWrapper.h
AssetWrapperPanel::addTextEdit
QTextEdit * addTextEdit(const QString &label, const char *property, bool readOnly=true)
Definition: AssetWrapperPanel.cpp:84
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
altar_valkyrie.obj
obj
Definition: altar_valkyrie.py:33
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
recipe::arch_names
size_t arch_names
Definition: recipe.h:12
ResourcesManager
Definition: ResourcesManager.h:80
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:74
FLAG_IS_TURNABLE
#define FLAG_IS_TURNABLE
Definition: define.h:256
disinfect.a
a
Definition: disinfect.py:13
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.cpp:57
if
if(!(yy_init))
Definition: loader.cpp:36428
CREUtils.h
guildoracle.list
list
Definition: guildoracle.py:87
give_artifact_abilities
void give_artifact_abilities(object *op, const object *artifact)
Definition: artifact.cpp:230
AnimationWidget.h
artifact::item
object * item
Definition: artifact.h:15
root
static char root[500]
Definition: mapper.cpp:304
AnimationWidget::setAnimation
void setAnimation(QList< int > faces)
Definition: AnimationWidget.cpp:25
recipe::arch_name
char ** arch_name
Definition: recipe.h:13
artifact::allowed
std::vector< sstring > allowed
Definition: artifact.h:18
getManager
AssetsManager * getManager()
Definition: assets.cpp:305
CREUtils::archetypeNode
static QTreeWidgetItem * archetypeNode(const archetype *arch, QTreeWidgetItem *parent)
Definition: CREUtils.cpp:26
AssetWrapperPanel::addLabel
QLabel * addLabel(const QString &label, const char *property, bool wrapText=false)
Definition: AssetWrapperPanel.cpp:68
AssetWrapperPanel::addSpinBox
QSpinBox * addSpinBox(const QString &label, const char *property, int min=0, int max=100, bool readOnly=true)
Definition: AssetWrapperPanel.cpp:118
AssetsCollection::find
T * find(const Key &name)
Definition: AssetsCollection.h:108
ResourcesManager::originOf
std::string originOf(const archetype *arch) const
Definition: ResourcesManager.h:110
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.cpp:76
recipelist
Definition: recipe.h:37
AssetTWrapperPanel
Definition: AssetWrapperPanel.h:92
archetype::clone
object clone
Definition: object.h:487
ArtifactPanel::myValues
QTextEdit * myValues
Definition: ArtifactPanel.h:44
ArtifactPanel::myResources
ResourcesManager * myResources
Definition: ArtifactPanel.h:38
dump
Definition: quest.cpp:847
object::type
uint8_t type
Definition: object.h:348
ArtifactPanel.h
object_free
void object_free(object *ob, int flags)
Definition: object.cpp:1592
archetype
Definition: object.h:483
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:158
ArtifactPanel::myOrigin
QLabel * myOrigin
Definition: ArtifactPanel.h:48
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
ArtifactPanel::myType
QLineEdit * myType
Definition: ArtifactPanel.h:41
ResourcesManager.h
describe_item
StringBuffer * describe_item(const object *op, const object *owner, int use_media_tags, StringBuffer *buf)
Definition: item.cpp:951
StringBuffer
Definition: stringbuffer.cpp:25
AnimationWidget::step
void step()
Definition: AnimationWidget.cpp:31
recipe
Definition: recipe.h:10
AssetTWrapperPanel< artifact >::myItem
artifact * myItem
Definition: AssetWrapperPanel.h:106
ArtifactPanel::myInstance
QTextEdit * myInstance
Definition: ArtifactPanel.h:45
object::name
sstring name
Definition: object.h:319
bigchest.check
check
Definition: bigchest.py:10
AnimationWidget
Definition: AnimationWidget.h:22
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:266
addArchetypes
static void addArchetypes(const artifact *artifact, const char *name, bool check, QTreeWidget *root)
Definition: ArtifactPanel.cpp:106
AssetWrapperPanel::addWidget
T * addWidget(const QString &label, T *widget, bool sideBySide, const char *property, const char *widgetProperty)
Definition: AssetWrapperPanel.h:64
assets.h
Archetypes.h
AnimationControl
Definition: AnimationControl.h:27
dragon_attune.faces
dictionary faces
Definition: dragon_attune.py:31
get_ob_diff
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Definition: object.cpp:4986
arch_to_object
object * arch_to_object(archetype *at)
Definition: arch.cpp:229
object_give_identified_properties
void object_give_identified_properties(object *op)
Definition: item.cpp:1356
AnimationControl::setAnimation
void setAnimation(const Animations *animation, int facings=-1)
Definition: AnimationControl.cpp:41
ArtifactWrapper.h
empty_archetype
archetype * empty_archetype
Definition: init.cpp:119
FREE_OBJ_FREE_INVENTORY
#define FREE_OBJ_FREE_INVENTORY
Definition: object.h:544
recipe::next
recipe * next
Definition: recipe.h:24
ArtifactPanel::myViaAlchemy
QLabel * myViaAlchemy
Definition: ArtifactPanel.h:42
ArtifactPanel::myArchetypes
QTreeWidget * myArchetypes
Definition: ArtifactPanel.h:43
artifact
Definition: artifact.h:14
connect
Definition: connect.py:1
replace.current
current
Definition: replace.py:64
ArtifactPanel::myAnimation
AnimationControl * myAnimation
Definition: ArtifactPanel.h:46
ArtifactPanel::ArtifactPanel
ArtifactPanel(QWidget *parent, ResourcesManager *resources)
Definition: ArtifactPanel.cpp:24
ArtifactPanel::updateItem
virtual void updateItem() override
Definition: ArtifactPanel.cpp:132
FLAG_IDENTIFIED
#define FLAG_IDENTIFIED
Definition: define.h:261
give.name
name
Definition: give.py:27
legal_artifact_combination
int legal_artifact_combination(const object *op, const artifact *art)
Definition: artifact.cpp:252
recipe::title
sstring title
Definition: recipe.h:11