Crossfire Server, Trunk
ArchetypeWrapper.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 "ArchetypeWrapper.h"
14 #include <QMimeData>
15 #include "../ResourcesManager.h"
16 #include "../CREPixmap.h"
17 #include "faces/FaceWrapper.h"
18 #include "assets.h"
19 #include "AssetsManager.h"
22 #include "MimeUtils.h"
24 
26  : AssetWithArtifacts<archetype>(parent, "Archetype", arch, resourcesManager) {
27  if (appearsOnTreasureList()) {
28  setSpecificItem(&arch->clone, false);
29  }
30 }
31 
32 QString ArchetypeWrapper::name() const {
33  return myWrappedItem->name;
34 }
35 
36 QObject* ArchetypeWrapper::clone() {
37  return myResources->wrap(&myWrappedItem->clone, this);
38 }
39 
40 QObject *ArchetypeWrapper::head() const {
41  return myWrappedItem->head ? myResources->wrap(myWrappedItem->head, nullptr) : nullptr;
42 }
43 
44 QObject *ArchetypeWrapper::more() const {
45  return myWrappedItem->more ? myResources->wrap(myWrappedItem->more, nullptr) : nullptr;
46 }
47 
49  if (myWrappedItem->head) {
50  return 0;
51  }
52  int count = 0;
53  auto part = myWrappedItem;
54  while (part->more) {
55  count++;
56  part = part->more;
57  }
58  return count + myArtifacts.size();
59 }
60 
62  auto part = myWrappedItem->more;
63  while (index > 0 && part) {
64  part = part->more;
65  index--;
66  }
67  if (part) {
68  return myResources->wrap(part, this);
69  }
71 }
72 
74  if (myWrappedItem->head) {
75  return -1;
76  }
77  int index = 0;
78  if (myWrappedItem->more) {
79  auto part = myResources->wrap(myWrappedItem->more, this);
80  while (part != child && part) {
81  part = myResources->wrap(part->wrappedItem()->more, this);
82  index++;
83  }
84  if (part) {
85  return index;
86  }
87  }
89 }
90 
91 static bool treasureContains(const treasure *t, const archetype *arch) {
92  while (t) {
93  if (t->item == arch)
94  return true;
95  if (t->name) {
96  auto other = getManager()->treasures()->find(t->name);
97  if (other && treasureContains(other->items, arch))
98  return true;
99  }
100  if (t->next_yes && treasureContains(t->next_yes, arch))
101  return true;
102  if (t->next_no && treasureContains(t->next_no, arch))
103  return true;
104  t = t->next;
105  }
106  return false;
107 }
108 
109 static bool isValidArchFlesh(const archetype *arch, const Face *fleshFace) {
110  if (!arch->clone.randomitems || !fleshFace)
111  return false;
112 
113  std::vector<const archetype *> sources;
114  getManager()->archetypes()->each([&sources, &fleshFace] (const archetype * candidate) {
115  if (candidate->clone.face == fleshFace)
116  sources.push_back(candidate);
117  });
118 
119  for (auto source : sources) {
120  if (treasureContains(arch->clone.randomitems->items, source))
121  return true;
122  }
123 
124  return false;
125 }
126 
127 AssetWrapper::PossibleUse ArchetypeWrapper::uses(const AssetWrapper *asset, std::string &hint) const {
128  auto face = dynamic_cast<const FaceWrapper *>(asset);
129  if (face != nullptr) {
130  auto key = object_get_value(&myWrappedItem->clone, "identified_face");
131  if (face->wrappedItem() == myWrappedItem->clone.face || (key && strcmp(face->wrappedItem()->name, key) == 0)) {
132  return Uses;
133  }
134 
135  auto len = strlen(myWrappedItem->name);
136  if (strncmp(myWrappedItem->name, face->wrappedItem()->name, len) == 0 && face->wrappedItem()->name[len] == '_') {
137  auto flesh = getManager()->faces()->find(face->wrappedItem()->name + len + 1);
138  if (isValidArchFlesh(myWrappedItem, flesh)) {
139  hint = "flesh face";
140  return Uses;
141  }
142  }
143 
144  return ChildrenMayUse;
145  }
146  auto anim = dynamic_cast<const AnimationWrapper *>(asset);
147  if (anim) {
148  auto key = object_get_value(&myWrappedItem->clone, "identified_animation");
149  if (anim->wrappedItem() == myWrappedItem->clone.animation || (key && strcmp(anim->wrappedItem()->name, key) == 0)) {
150  return Uses;
151  }
152  return DoesntUse;
153  }
154  auto tl = dynamic_cast<const TreasureListWrapper *>(asset);
155  if (tl) {
156  return myWrappedItem->clone.randomitems == tl->wrappedItem() ? Uses : DoesntUse;
157  }
158  auto arch = dynamic_cast<const ArchetypeWrapper *>(asset);
159  if (arch) {
160  if (wrappedItem()->clone.other_arch == arch->wrappedItem()) {
161  hint = "other arch";
162  return Uses;
163  }
164  sstring death_anim = NULL;
165  if ((death_anim = object_get_value(&myWrappedItem->clone, "death_animation")) && strcmp(death_anim, arch->wrappedItem()->name) == 0) {
166  hint = "death animation";
167  return Uses;
168  }
169  }
170  return DoesntUse;
171 }
172 
173 void ArchetypeWrapper::drag(QMimeData *data) const {
175 }
176 
178  std::function<bool(const treasure *item)> ci = [&] (const treasure *item) {
179  if (item->item == myWrappedItem) {
180  return true;
181  }
182  if (item->next_yes && ci(item->next_yes)) {
183  return true;
184  }
185  if (item->next_no && ci(item->next_no)) {
186  return true;
187  }
188  if (item->next && ci(item->next)) {
189  return true;
190  }
191  return false;
192  };
193  auto cl = [&] (const treasurelist *item) { return ci(item->items); };
194  return getManager()->treasures()->first(cl) != nullptr;
195 }
MimeUtils::Archetype
static const char * Archetype
Definition: MimeUtils.h:24
AssetTWrapper< archetype >::myWrappedItem
archetype * myWrappedItem
Definition: AssetWrapper.h:97
Face
Definition: face.h:14
MimeUtils::addMime
void addMime(QMimeData *data, const QString &mime, const QString &name)
Definition: MimeUtils.cpp:17
obj::face
const Face * face
Definition: object.h:336
archt::head
struct archt * head
Definition: object.h:471
AssetsCollection::first
T * first(std::function< bool(const T *)> op)
Definition: AssetsCollection.h:144
ArchetypeWrapper::more
QObject more
Definition: ArchetypeWrapper.h:34
ResourcesManager
Definition: ResourcesManager.h:79
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
AssetsManager.h
AnimationWrapper
Definition: AnimationWrapper.h:27
AssetWithArtifacts< archetype >::setSpecificItem
void setSpecificItem(const object *item, bool notify)
Definition: AssetWithArtifacts.h:61
AssetWrapper::PossibleUse
PossibleUse
Definition: AssetWrapper.h:28
treasureContains
static bool treasureContains(const treasure *t, const archetype *arch)
Definition: ArchetypeWrapper.cpp:91
ArchetypeWrapper::uses
virtual PossibleUse uses(const AssetWrapper *asset, std::string &) const override
Definition: ArchetypeWrapper.cpp:127
ResourcesManager::wrap
ArchetypeWrapper * wrap(archt *arch, AssetWrapper *parent)
Definition: ResourcesManager.h:124
AssetWithArtifacts::child
virtual AssetWrapper * child(int child) override
Definition: AssetWithArtifacts.h:42
TreasureListWrapper
Definition: TreasureListWrapper.h:29
ArchetypeWrapper::appearsOnTreasureList
bool appearsOnTreasureList() const
Definition: ArchetypeWrapper.cpp:177
obj::randomitems
struct treasureliststruct * randomitems
Definition: object.h:390
getManager
AssetsManager * getManager()
Definition: assets.cpp:335
AssetWrapper::ChildrenMayUse
@ ChildrenMayUse
Definition: AssetWrapper.h:28
treasurestruct
Definition: treasure.h:63
AssetsCollection::find
T * find(const Key &name)
Definition: AssetsCollection.h:85
archt
Definition: object.h:469
object_get_value
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4317
ArchetypeWrapper::ArchetypeWrapper
ArchetypeWrapper(AssetWrapper *parent, archetype *arch, ResourcesManager *resourcesManager)
Definition: ArchetypeWrapper.cpp:25
FaceWrapper
Definition: FaceWrapper.h:27
AssetsManager::treasures
Treasures * treasures()
Definition: AssetsManager.h:54
ArchetypeWrapper::head
QObject head
Definition: ArchetypeWrapper.h:33
AssetWrapper
Definition: AssetWrapper.h:22
AnimationWrapper.h
AssetWithArtifacts< archetype >::myArtifacts
std::vector< ArtifactWrapper * > myArtifacts
Definition: AssetWithArtifacts.h:59
AssetsManager::faces
Faces * faces()
Definition: AssetsManager.h:39
AssetWithArtifacts< archetype >::myResources
ResourcesManager * myResources
Definition: AssetWithArtifacts.h:58
navar-midane_time.data
data
Definition: navar-midane_time.py:11
ArchetypeWrapper.h
obj::other_arch
struct archt * other_arch
Definition: object.h:418
archt::more
struct archt * more
Definition: object.h:472
ArchetypeWrapper::clone
QObject clone
Definition: ArchetypeWrapper.h:32
sstring
const typedef char * sstring
Definition: global.h:40
disinfect.count
int count
Definition: disinfect.py:7
FaceWrapper.h
AssetWrapper::Uses
@ Uses
Definition: AssetWrapper.h:28
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:135
ArchetypeWrapper::childrenCount
virtual int childrenCount() const override
Definition: ArchetypeWrapper.cpp:48
animate.anim
string anim
Definition: animate.py:20
AssetTWrapper< archetype >::wrappedItem
archetype * wrappedItem() const
Definition: AssetWrapper.h:94
obj::animation
const Animations * animation
Definition: object.h:421
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
isValidArchFlesh
static bool isValidArchFlesh(const archetype *arch, const Face *fleshFace)
Definition: ArchetypeWrapper.cpp:109
AssetWrapperPanel.h
AssetWithArtifacts::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: AssetWithArtifacts.h:48
Floor.t
t
Definition: Floor.py:62
archt::clone
object clone
Definition: object.h:473
item
Definition: item.py:1
ArchetypeWrapper
Definition: ArchetypeWrapper.h:28
ArchetypeWrapper::name
QString name
Definition: ArchetypeWrapper.h:31
ArchetypeWrapper::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: ArchetypeWrapper.cpp:73
TreasureListWrapper.h
assets.h
npc_dialog.index
int index
Definition: npc_dialog.py:102
castle_read.key
key
Definition: castle_read.py:64
MimeUtils.h
AssetWithArtifacts
Definition: AssetWithArtifacts.h:26
archt::name
sstring name
Definition: object.h:470
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:28
say.item
dictionary item
Definition: say.py:149
ArchetypeWrapper::child
virtual AssetWrapper * child(int index) override
Definition: ArchetypeWrapper.cpp:61
ArchetypeWrapper::drag
virtual void drag(QMimeData *) const override
Definition: ArchetypeWrapper.cpp:173
treasureliststruct
Definition: treasure.h:82