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 
24 {
25  addLineEdit(tr("Name:"), "name");
26  addSpinBox(tr("Chance:"), "chance", 0, 65535, false);
27  myType = addLineEdit(tr("Type:"), nullptr, true);
28 
29  myViaAlchemy = addWidget(QString(), new QLabel(this), false, nullptr, nullptr);
30  myViaAlchemy->setWordWrap(true);
31 
32  myLayout->addWidget(new QLabel(tr("Values:"), this), 5, 0, 1, 2);
33  myValues = new QTextEdit(this);
34  myLayout->addWidget(myValues, 6, 0, 1, 2);
35  myValues->setReadOnly(true);
36 
37  myArchetypes = new QTreeWidget(this);
38  myLayout->addWidget(myArchetypes, 7, 0, 3, 1);
39  myArchetypes->setHeaderLabel("Allowed/forbidden archetypes");
40  myArchetypes->setIconSize(QSize(32, 32));
41  myArchetypes->setRootIsDecorated(false);
42  connect(myArchetypes, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(artifactChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
43 
44  myLayout->addWidget(new QLabel(tr("Result:"), this), 7, 1);
45  myInstance = new QTextEdit(this);
46  myLayout->addWidget(myInstance, 8, 1);
47  myInstance->setReadOnly(true);
48 
49  myLayout->addWidget(myAnimation = new AnimationControl(this), 9, 1);
50  myLayout->addWidget(myFace = new AnimationWidget(this), 9, 1);
51 }
52 
54 {
55  Q_ASSERT(artifact != NULL);
56 
57  const recipelist* list;
58  const recipe* recipe;
59  const archetype* arch;
60  QStringList possible;
61 
62  for (int ing = 1; ; ing++)
63  {
64  list = get_formulalist(ing);
65  if (!list)
66  break;
67  for (recipe = list->items; recipe; recipe = recipe->next)
68  {
69  if (recipe->title == NULL)
70  continue;
71 
72  if (strcmp(recipe->title, artifact->item->name) != 0)
73  continue;
74 
75  for (size_t a = 0; a < recipe->arch_names; a++)
76  {
78  if (!arch)
79  continue;
80  if ((arch->clone.type == artifact->item->type) && legal_artifact_combination(&arch->clone, artifact))
81  {
82  if (!possible.contains(arch->name))
83  possible.append(arch->name);
84  }
85  }
86  }
87  }
88 
89  if (possible.isEmpty())
90  myViaAlchemy->setText(tr("Can't be made via alchemy."));
91  else
92  {
93  if (possible.size() == artifact->allowed_size)
94  myViaAlchemy->setText(tr("Can be made via alchemy."));
95  else
96  {
97  possible.sort();
98  myViaAlchemy->setText(tr("The following archetypes can be used via alchemy: %1").arg(possible.join(tr(", "))));
99  }
100  }
101 }
102 
110 static void addArchetypes(const artifact* artifact, const char* name, bool check, QTreeWidget* root)
111 {
112  QTreeWidgetItem* item = NULL;
113 
115  {
116  if (arch->head || arch->clone.type != artifact->item->type)
117  {
118  return;
119  }
120 
121  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)))
122  {
123  if (item == NULL)
124  {
125  item = new QTreeWidgetItem(root, QStringList(name == NULL ? "(all)" : name));
126  item->setCheckState(0, check ? Qt::Checked : Qt::Unchecked);
127  item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
128  root->addTopLevelItem(item);
129  item->setExpanded(true);
130  }
131  CREUtils::archetypeNode(arch, item)->setData(0, Qt::UserRole, arch->name);
132  }
133  });
134 }
135 
137 {
138  myType->setText(QString::number(myItem->item->type));
139 
141 
142  myArchetypes->clear();
143  myInstance->clear();
144 
145  /* 'allowed' is either the archetype name or the item's name, so check all archetypes for each word */
146  for (const linked_char* allowed = myItem->allowed; allowed; allowed = allowed->next) {
147  auto name = allowed->name;
148  bool check = true;
149  if (name[0] == '!') {
150  name = name + 1;
151  check = false;
152  }
153 
155  }
156 
157  /* all items are allowed, so add them */
158  if (myItem->allowed == NULL) {
159  addArchetypes(myItem, NULL, true, myArchetypes);
160  }
161 
164  char* final = stringbuffer_finish(dump);
165  myValues->setText(final);
166  free(final);
167 }
168 
169 void ArtifactPanel::artifactChanged(QTreeWidgetItem* current, QTreeWidgetItem*)
170 {
171  myAnimation->setVisible(false);
172  myFace->setVisible(false);
173  myInstance->clear();
174  if (!current || current->data(0, Qt::UserRole).toString().isEmpty()) {
175  return;
176  }
177  archt* arch = getManager()->archetypes()->find(current->data(0, Qt::UserRole).toString().toUtf8().constData());
178  if (!arch) {
179  return;
180  }
181 
182  char* desc;
183  object* obj = arch_to_object(arch);
187  desc = stringbuffer_finish(describe_item(obj, NULL, 0, NULL));
188  myInstance->setText(desc);
189  free(desc);
190 
191  if (obj->animation != nullptr) {
192  myAnimation->setVisible(true);
194  } else {
195  myFace->setVisible(true);
196  QList<int> faces;
197  faces.append(obj->face->number);
199  myFace->step();
200  }
201 
203 }
recipestruct::arch_names
size_t arch_names
Definition: recipe.h:12
empty_archetype
EXTERN archetype * empty_archetype
Definition: global.h:140
ArtifactPanel::myFace
AnimationWidget * myFace
Definition: ArtifactPanel.h:43
FREE_OBJ_NO_DESTROY_CALLBACK
#define FREE_OBJ_NO_DESTROY_CALLBACK
Definition: object.h:531
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:23
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
ArtifactPanel::computeMadeViaAlchemy
void computeMadeViaAlchemy(const artifact *artifact) const
Definition: ArtifactPanel.cpp:53
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
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:72
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:26
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:306
recipestruct::title
sstring title
Definition: recipe.h:11
AnimationWidget::setAnimation
void setAnimation(QList< int > faces)
Definition: AnimationWidget.cpp:25
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:335
AssetWrapperPanel::addSpinBox
QSpinBox * addSpinBox(const QString &label, const char *property, int min=0, int max=100, bool readOnly=true)
Definition: AssetWrapperPanel.cpp:112
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:469
obj
Definition: object.h:277
recipestruct
Definition: recipe.h:10
AssetWrapperPanel::myLayout
QGridLayout * myLayout
Definition: AssetWrapperPanel.h:54
obj::name
sstring name
Definition: object.h:314
AssetTWrapperPanel
Definition: AssetWrapperPanel.h:90
Face::number
uint16_t number
Definition: face.h:15
ArtifactPanel::myValues
QTextEdit * myValues
Definition: ArtifactPanel.h:40
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:421
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
ArtifactPanel::myType
QLineEdit * myType
Definition: ArtifactPanel.h:37
artifactstruct
Definition: artifact.h:14
StringBuffer
Definition: stringbuffer.c:25
AnimationWidget::step
void step()
Definition: AnimationWidget.cpp:31
AssetTWrapperPanel< artifact >::myItem
artifact * myItem
Definition: AssetWrapperPanel.h:104
ArtifactPanel::myInstance
QTextEdit * myInstance
Definition: ArtifactPanel.h:41
obj::type
uint8_t type
Definition: object.h:343
archt::clone
object clone
Definition: object.h:473
AnimationWidget
Definition: AnimationWidget.h:19
item
Definition: item.py:1
ArtifactPanel::artifactChanged
void artifactChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
Definition: ArtifactPanel.cpp:169
find_archetype
archetype * find_archetype(const char *name)
Definition: assets.cpp:284
addArchetypes
static void addArchetypes(const artifact *artifact, const char *name, bool check, QTreeWidget *root)
Definition: ArtifactPanel.cpp:110
AssetWrapperPanel::addWidget
T * addWidget(const QString &label, T *widget, bool sideBySide, const char *property, const char *widgetProperty)
Definition: AssetWrapperPanel.h:62
assets.h
Archetypes.h
recipestruct::next
struct recipestruct * next
Definition: recipe.h:24
AnimationControl
Definition: AnimationControl.h:26
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:41
artifactstruct::item
object * item
Definition: artifact.h:15
ArtifactWrapper.h
FREE_OBJ_FREE_INVENTORY
#define FREE_OBJ_FREE_INVENTORY
Definition: object.h:530
ArtifactPanel::myViaAlchemy
QLabel * myViaAlchemy
Definition: ArtifactPanel.h:38
ArtifactPanel::myArchetypes
QTreeWidget * myArchetypes
Definition: ArtifactPanel.h:39
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:42
if
if(!(yy_init))
Definition: loader.c:2626
ArtifactPanel::updateItem
virtual void updateItem() override
Definition: ArtifactPanel.cpp:136
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