Crossfire Server, Trunk  R20513
CREArtifactPanel.cpp
Go to the documentation of this file.
1 #include <QtGui>
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 
13 {
14  myArtifact = NULL;
15 
16  QGridLayout* layout = new QGridLayout(this);
17 
18  QLabel* label = new QLabel(this);
19  label->setText("Name:");
20  layout->addWidget(label, 1, 1);
21  myName = new QLineEdit(this);
22  layout->addWidget(myName, 1, 2);
23  myName->setReadOnly(true);
24 
25  label = new QLabel(this);
26  label->setText("Chance:");
27  layout->addWidget(label, 2, 1);
28  myChance = new QLineEdit(this);
29  layout->addWidget(myChance, 2, 2);
30  myChance->setReadOnly(true);
31 
32  label = new QLabel(this);
33  label->setText("Type:");
34  layout->addWidget(label, 3, 1);
35  myType = new QLineEdit(this);
36  layout->addWidget(myType, 3, 2);
37  myType->setReadOnly(true);
38 
39  myViaAlchemy = new QLabel(this);
40  myViaAlchemy->setWordWrap(true);
41  layout->addWidget(myViaAlchemy, 4, 1, 1, 2);
42 
43  layout->addWidget(new QLabel(tr("Values:"), this), 5, 1, 1, 2);
44  myValues = new QTextEdit(this);
45  layout->addWidget(myValues, 6, 1, 1, 2);
46  myValues->setReadOnly(true);
47 
48  myArchetypes = new QTreeWidget(this);
49  layout->addWidget(myArchetypes, 7, 1, 3, 1);
50  myArchetypes->setHeaderLabel("Allowed/forbidden archetypes");
51  myArchetypes->setIconSize(QSize(32, 32));
52  myArchetypes->setRootIsDecorated(false);
53  connect(myArchetypes, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(artifactChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
54 
55  layout->addWidget(new QLabel(tr("Result:"), this), 7, 2);
56  myInstance = new QTextEdit(this);
57  layout->addWidget(myInstance, 8, 2);
58  myInstance->setReadOnly(true);
59 
60  layout->addWidget(myAnimation = new CREAnimationControl(this), 9, 2);
61  layout->addWidget(myFace = new CREAnimationWidget(this), 9, 2);
62 }
63 
65 {
66  Q_ASSERT(artifact != NULL);
67 
68  const recipelist* list;
69  const recipe* recipe;
70  const archetype* arch;
71  QStringList possible;
72 
73  for (int ing = 1; ; ing++)
74  {
75  list = get_formulalist(ing);
76  if (!list)
77  break;
78  for (recipe = list->items; recipe; recipe = recipe->next)
79  {
80  if (recipe->title == NULL)
81  continue;
82 
83  if (strcmp(recipe->title, artifact->item->name) != 0)
84  continue;
85 
86  for (size_t a = 0; a < recipe->arch_names; a++)
87  {
88  arch = find_archetype(recipe->arch_name[a]);
89  if (!arch)
90  continue;
91  if ((arch->clone.type == artifact->item->type) && legal_artifact_combination(&arch->clone, artifact))
92  {
93  if (!possible.contains(arch->name))
94  possible.append(arch->name);
95  }
96  }
97  }
98  }
99 
100  if (possible.isEmpty())
101  myViaAlchemy->setText(tr("Can't be made via alchemy."));
102  else
103  {
104  if (possible.size() == artifact->allowed_size)
105  myViaAlchemy->setText(tr("Can be made via alchemy."));
106  else
107  {
108  possible.sort();
109  myViaAlchemy->setText(tr("The following archetypes can be used via alchemy: %1").arg(possible.join(tr(", "))));
110  }
111  }
112 }
113 
121 static void addArchetypes(const artifact* artifact, const char* name, bool check, QTreeWidget* root)
122 {
123  const archt* arch;
124  QTreeWidgetItem* item = NULL;
125  item = NULL;
126  for (arch = first_archetype; arch != NULL; arch = arch->next)
127  {
128  if (arch->clone.type != artifact->item->type)
129  {
130  continue;
131  }
132 
133  if (name == NULL || (check && (!strcmp(name, arch->clone.name) || (!strcmp(name, arch->name)))) || (!check && strcmp(name, arch->clone.name) && strcmp(name, arch->name)))
134  {
135  if (item == NULL)
136  {
137  item = new QTreeWidgetItem(root, QStringList(name == NULL ? "(all)" : name));
138  item->setCheckState(0, check ? Qt::Checked : Qt::Unchecked);
139  root->addTopLevelItem(item);
140  item->setExpanded(true);
141  }
142  CREUtils::archetypeNode(arch, item)->setData(0, Qt::UserRole, arch->name);
143  }
144  }
145 }
146 
148 {
149  Q_ASSERT(artifact);
151 
152  myName->setText(artifact->item->name);
153  myChance->setText(QString::number(artifact->chance));
154  myType->setText(QString::number(artifact->item->type));
155 
156  computeMadeViaAlchemy(artifact);
157 
158  const char* name;
159  bool check;
160 
161  myArchetypes->clear();
162  myInstance->clear();
163 
164  /* 'allowed' is either the archetype name or the item's name, so check all archetypes for each word */
165  for (const linked_char* allowed = artifact->allowed; allowed; allowed = allowed->next)
166  {
167  name = allowed->name;
168  if (name[0] == '!')
169  {
170  name = name + 1;
171  check = false;
172  }
173  else
174  check = true;
175 
176  addArchetypes(myArtifact, name, check, myArchetypes);
177  }
178 
179  /* all items are allowed, so add them */
180  if (artifact->allowed == NULL)
181  {
182  addArchetypes(myArtifact, NULL, true, myArchetypes);
183  }
184 
185  StringBuffer* dump = stringbuffer_new();
187  char* final = stringbuffer_finish(dump);
188  myValues->setText(final);
189  free(final);
190 }
191 
192 void CREArtifactPanel::artifactChanged(QTreeWidgetItem* current, QTreeWidgetItem*)
193 {
194  myAnimation->setVisible(false);
195  myFace->setVisible(false);
196  myInstance->clear();
197  if (!current || current->data(0, Qt::UserRole).toString().isEmpty())
198  {
199  return;
200  }
201  archt* arch = try_find_archetype(current->data(0, Qt::UserRole).toString().toUtf8().constData());
202  if (!arch)
203  {
204  return;
205  }
206 
207  char* desc;
208  object* obj = arch_to_object(arch);
212  desc = stringbuffer_finish(describe_item(obj, NULL, 0, NULL));
213  myInstance->setText(desc);
214  free(desc);
215 
216  if (obj->animation_id != 0)
217  {
218  myAnimation->setVisible(true);
220  }
221  else
222  {
223  myFace->setVisible(true);
224  QList<int> faces;
225  faces.append(obj->face->number);
226  myFace->setAnimation(faces);
227  myFace->step();
228  }
229 
231 }
Main Crossfire structure, one ingame object.
Definition: object.h:274
archetype * find_archetype(const char *name)
Finds, using the hashtable, which archetype matches the given name.
Definition: arch.c:695
List of recipes with a certain number of ingredients.
Definition: recipe.h:37
void object_give_identified_properties(object *op)
Ensure op has all its "identified" properties set.
Definition: item.c:1375
#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)
Add all possible archetypes for the specified artifact.
void setAnimation(QList< int > faces)
uint16_t animation_id
An index into the animation array.
Definition: object.h:416
QLineEdit * myName
StringBuffer * stringbuffer_new(void)
Create a new string buffer.
Definition: stringbuffer.c:57
void artifactChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
object clone
An object from which to do object_copy()
Definition: object.h:470
#define FREE_OBJ_NO_DESTROY_CALLBACK
Do not run the destroy callback.
Definition: object.h:533
Global type definitions and header inclusions.
QLineEdit * myType
The archetype structure is a set of rules on how to generate and manipulate objects which point to ar...
Definition: object.h:465
QLineEdit * myChance
uint16_t number
This is the image id.
Definition: face.h:15
const artifact * myArtifact
#define FREE_OBJ_FREE_INVENTORY
Free inventory objects; if not set, drop inventory.
Definition: object.h:532
EXTERN archetype * empty_archetype
Nice to have fast access to it.
Definition: global.h:151
int legal_artifact_combination(const object *op, const artifact *art)
Checks if op can be combined with art.
Definition: artifact.c:221
sstring title
Distinguishing name of product.
Definition: recipe.h:11
char ** arch_name
Possible archetypes of the final product made.
Definition: recipe.h:13
static char root[500]
Path to store generated files.
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
Player knows full info about item.
Definition: define.h:261
struct recipestruct recipe
One alchemy recipe.
struct artifactstruct artifact
This is one artifact, ie one special item.
const char * name
The name of the object, obviously...
Definition: object.h:311
linked_char * allowed
List of archetypes the artifact can affect.
Definition: artifact.h:19
CREAnimationWidget * myFace
EXTERN Animations * animations
Definition: global.h:165
void setAnimation(const Animations *animation, int facings=-1)
Define the animation to display.
archetype * try_find_archetype(const char *name)
Finds, using the hashtable, which archetype matches the given name.
Definition: arch.c:666
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
This is one artifact, ie one special item.
Definition: artifact.h:14
void give_artifact_abilities(object *op, const object *artifact)
Fixes the given object, giving it the abilities and titles it should have due to the second artifact-...
Definition: artifact.c:203
StringBuffer * describe_item(const object *op, const object *owner, int use_media_tags, StringBuffer *buf)
Describes an item, in all its details.
Definition: item.c:980
int allowed_size
Length of allowed, for faster computation.
Definition: artifact.h:20
size_t arch_names
Size of the arch_name[] array.
Definition: recipe.h:12
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:338
void object_free2(object *ob, int flags)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.c:1391
struct archt * next
Next archetype in a linked list.
Definition: object.h:467
QTreeWidget * myArchetypes
struct recipestruct * next
Next recipe with the same number of ingredients.
Definition: recipe.h:24
A buffer that will be expanded as content is added to it.
Definition: stringbuffer.c:25
object * item
Special values of the artifact.
Definition: artifact.h:15
uint16_t chance
Chance of the artifact to happen.
Definition: artifact.h:16
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Returns a pointer to a static string which contains all variables which are different in the two give...
Definition: object.c:4845
const New_Face * face
Face with colors.
Definition: object.h:332
EXTERN archetype * first_archetype
First archetype.
Definition: global.h:122
object * arch_to_object(archetype *at)
Creates and returns a new object which is a copy of the given archetype.
Definition: arch.c:571
Artifact-related structures.
const char * name
More definite name, like "generate_kobold".
Definition: object.h:466
char * stringbuffer_finish(StringBuffer *sb)
Deallocate the string buffer instance and return the string.
Definition: stringbuffer.c:76
struct recipestruct * items
Pointer to first recipe in this list.
Definition: recipe.h:40
recipelist * get_formulalist(int i)
Gets a formula list by ingredients count.
Definition: recipe.c:114
QTextEdit * myInstance
One alchemy recipe.
Definition: recipe.h:10
Definition: main.c:88
void computeMadeViaAlchemy(const artifact *artifact) const
#define FLAG_IS_TURNABLE
Object can change face with direction.
Definition: define.h:256