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