Crossfire Resources Editor
TreasureWrapper.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 "TreasureWrapper.h"
14 #include "TreasureListWrapper.h"
15 #include "../ResourcesManager.h"
16 #include "assets.h"
17 #include "AssetsManager.h"
18 #include "MimeUtils.h"
20 
21 #include "global.h"
22 #include "artifact.h"
23 #include "libproto.h"
24 
26  : AssetWithArtifacts<treasure>(parent, "Treasure", tr, resources), myNextYes(nullptr), myNextNo(nullptr)
27 {
28  if (myWrappedItem->next_yes) {
29  myNextYes = new TreasureYesNo(this, myWrappedItem->next_yes, resources, true);
30  }
31  if (myWrappedItem->next_no) {
32  myNextNo = new TreasureYesNo(this, myWrappedItem->next_no, resources, false);
33  }
34  if (tr->item) {
35  setSpecificItem(&tr->item->clone, false);
36  }
37 }
38 
40 }
41 
43  QString name;
44  if (myWrappedItem->item) {
45  name = tr("%1 (%2)").arg(myWrappedItem->item->clone.name, myWrappedItem->item->name);
46  }
47  else if (!myWrappedItem->name || strcmp(myWrappedItem->name, "NONE") == 0) {
48  name = "Nothing";
49  }
50  else {
51  name = myWrappedItem->name;
52  }
53 
54  auto tlw = dynamic_cast<TreasureListWrapper *>(displayParent());
55  if (tlw && tlw->totalChance() != 0) {
56  name = tr("%1 (%2%3%, %4 chances on %5)")
57  .arg(name)
58  .arg(myWrappedItem->nrof > 0 ? tr("1 to %2, ").arg(myWrappedItem->nrof) : "")
59  .arg(qRound((float)100 * myWrappedItem->chance / tlw->totalChance()))
60  .arg(myWrappedItem->chance)
61  .arg(tlw->totalChance());
62  } else {
63  name = tr("%1 (%2%3%)")
64  .arg(name)
65  .arg(myWrappedItem->nrof > 0 ? tr("1 to %2, ").arg(myWrappedItem->nrof) : "")
66  .arg(myWrappedItem->chance);
67  }
68 
69  return name;
70 }
71 
73  if (myWrappedItem->item) {
74  return CREPixmap::getIcon(myWrappedItem->item->clone.face);
75  }
76  if (!myWrappedItem->name || strcmp(myWrappedItem->name, "NONE") == 0) {
77  return QIcon();
78  }
79 
80  auto tl = getManager()->treasures()->find(myWrappedItem->name);
81  if (!tl) {
82  return QIcon();
83  }
84  return tl->total_chance == 0 ? CREPixmap::getTreasureIcon() : CREPixmap::getTreasureOneIcon();
85 }
86 
88  int count = 0;
89  if (myWrappedItem->next_yes) {
90  count++;
91  }
92  if (myWrappedItem->next_no) {
93  count++;
94  }
96 }
97 
99  if (myNextYes) {
100  if (child == 0) {
101  return myNextYes;
102  }
103  child--;
104  }
105  if (myNextNo) {
106  if (child == 0) {
107  return myNextNo;
108  }
109  child--;
110  }
112 }
113 
115  int index = 0;
116  if (myNextYes) {
117  if (child == myNextYes) {
118  return index;
119  }
120  index++;
121  }
122  if (myNextNo) {
123  if (child == myNextNo) {
124  return index;
125  }
126  index++;
127  }
128 
130  if (c != -1) {
131  return c + index;
132  }
133  return -1;
134 }
135 
136 void TreasureWrapper::doRemoveChild(TreasureYesNo **tr, treasure **ti, int index) {
138  myResources->remove(*ti);
139  treasure_free(*ti);
140  (*ti) = nullptr;
141  delete *tr;
142  (*tr) = nullptr;
144  return;
145 }
146 
148  if (child == myNextYes) {
149  doRemoveChild(&myNextYes, &myWrappedItem->next_yes, 0);
150  return;
151  }
152  if (child == myNextNo) {
153  doRemoveChild(&myNextNo, &myWrappedItem->next_no, myNextYes ? 1 : 0);
154  }
155 }
156 
157 void TreasureWrapper::doAddChild(TreasureYesNo **my, treasure **ti, bool isYes, int index, treasurelist *tl, archetype *arch) {
159  (*ti) = get_empty_treasure();
160  (*my) = new TreasureYesNo(this, *ti, myResources, isYes);
161  (*ti)->item = arch;
162  if (tl) {
163  (*ti)->name = add_string(tl->name);
164  }
165  markModified(AfterChildAdd, index);
166 }
167 
168 void TreasureWrapper::addChild(treasurelist *tl, archetype *arch) {
169  if (!myNextYes) {
170  doAddChild(&myNextYes, &myWrappedItem->next_yes, true, 0, tl, arch);
171  return;
172  }
173  if (!myNextNo) {
174  doAddChild(&myNextNo, &myWrappedItem->next_no, false, 1, tl, arch);
175  }
176 }
177 
178 bool TreasureWrapper::canDrop(const QMimeData *data, int) const {
179  return (!myNextYes || !myNextNo) &&
180  (data->hasFormat(MimeUtils::Archetype)
181  || data->hasFormat(MimeUtils::TreasureList))
182  ;
183 }
184 
185 void TreasureWrapper::drop(const QMimeData *data, int) {
186  auto archs = MimeUtils::extract(data, MimeUtils::Archetype, getManager()->archetypes());
187  for (auto arch : archs) {
188  addChild(nullptr, HEAD(arch));
189  }
190 
191  auto lists = MimeUtils::extract(data, MimeUtils::TreasureList, getManager()->treasures());
192  for (auto list : lists) {
193  addChild(list, nullptr);
194  }
195 }
196 
197 uint8_t TreasureWrapper::chance() const {
198  return myWrappedItem->chance;
199 }
200 
201 void TreasureWrapper::setChance(uint8_t chance) {
202  if (chance != myWrappedItem->chance) {
203  myWrappedItem->chance = chance;
205  }
206 }
207 
208 uint8_t TreasureWrapper::magic() const {
209  return myWrappedItem->magic;
210 }
211 
212 void TreasureWrapper::setMagic(uint8_t magic) {
213  if (magic != myWrappedItem->magic) {
214  myWrappedItem->magic = magic;
216  }
217 }
218 
219 uint16_t TreasureWrapper::nrof() const {
220  return myWrappedItem->nrof;
221 }
222 
223 void TreasureWrapper::setNrof(uint16_t nrof) {
224  if (nrof != myWrappedItem->nrof) {
225  myWrappedItem->nrof = nrof;
227  }
228 }
229 
230 const treasurelist *TreasureWrapper::list() const {
231  return myWrappedItem->name ? getManager()->treasures()->find(myWrappedItem->name) : nullptr;
232 }
233 
234 void TreasureWrapper::setList(const treasurelist *list) {
235  if (myWrappedItem->name != (list ? list->name : nullptr)) {
236  FREE_AND_CLEAR_STR_IF(myWrappedItem->name);
237  if (list) {
238  myWrappedItem->name = add_string(list->name);
239  myWrappedItem->item = nullptr;
240  setSpecificItem(nullptr, true);
241  }
243  }
244 }
245 
247  return myWrappedItem->list_magic_value;
248 }
249 
251  if (value != myWrappedItem->list_magic_value) {
252  myWrappedItem->list_magic_value = value;
254  }
255 }
256 
258  return myWrappedItem->list_magic_adjustment;
259 }
261  if (value != myWrappedItem->list_magic_adjustment) {
262  myWrappedItem->list_magic_adjustment = value;
264  }
265 }
266 
267 
268 const archetype *TreasureWrapper::arch() const {
269  return myWrappedItem->item;
270 }
271 
272 void TreasureWrapper::setArch(const archetype *arch) {
273  if (arch != myWrappedItem->item) {
274  myWrappedItem->item = const_cast<archetype *>(arch);
275  if (myWrappedItem->item && myWrappedItem->name) {
276  FREE_AND_CLEAR_STR(myWrappedItem->name);
277  }
279  setSpecificItem(arch ? &arch->clone : nullptr, true);
280  }
281 }
282 
283 const QString TreasureWrapper::artifact() const {
284  return myWrappedItem->artifact;
285 }
286 
287 void TreasureWrapper::setArtifact(const QString &art) {
288  if (art != myWrappedItem->artifact) {
289  FREE_AND_CLEAR_STR_IF(myWrappedItem->artifact);
290  if (!art.isEmpty()) {
291  myWrappedItem->artifact = add_string(art.toLocal8Bit().data());
292  }
294  }
295 }
296 
297 
298 void TreasureWrapper::fillMenu(QMenu *menu) {
299  if (myParent) {
300  myParent->fillMenu(menu);
301  menu->addSeparator();
302  }
303 
304  connect(menu->addAction(tr("Delete")), &QAction::triggered, [this] () { myParent->removeChild(this); });
305  if (myNextYes || myNextNo) {
306  connect(menu->addAction(tr("Swap 'yes' and 'no'")), &QAction::triggered, this, &TreasureWrapper::swapYesNo);
307  }
308 }
309 
311  if (myNextYes || myNextNo) {
313  if (myNextYes) {
314  myNextYes->setIsYes(false);
315  }
316  if (myNextNo) {
317  myNextNo->setIsYes(true);
318  }
319  std::swap(myNextYes, myNextNo);
320  std::swap(myWrappedItem->next_yes, myWrappedItem->next_no);
322  }
323 }
324 
326  auto arch = dynamic_cast<const ArchetypeWrapper *>(asset);
327  if (arch) {
328  if (wrappedItem()->item == arch->wrappedItem()) {
329  return Uses;
330  }
331  return ChildrenMayUse;
332  }
333  auto list = dynamic_cast<const TreasureListWrapper *>(asset);
334  if (list) {
335  return myWrappedItem->name == list->wrappedItem()->name ? Uses : DoesntUse;
336  }
337  return DoesntUse;
338 }
339 
340 TreasureYesNo::TreasureYesNo(TreasureWrapper *parent, treasure *tr, ResourcesManager *resources, bool isYes)
341  : AssetWrapper(parent, "empty"), myIsYes(isYes) {
342  myWrapped = resources->wrap(tr, this);
343  }
344 
345 void TreasureYesNo::fillMenu(QMenu *menu) {
346  connect(menu->addAction(tr("Delete")), &QAction::triggered, [this] () { myParent->removeChild(this); });
347  connect(menu->addAction(tr("Swap 'yes' and 'no'")), &QAction::triggered, static_cast<TreasureWrapper *>(myParent), &TreasureWrapper::swapYesNo);
348 }
MimeUtils::Archetype
static const char * Archetype
Definition: MimeUtils.h:24
AssetTWrapper< treasure >::myWrappedItem
treasure * myWrappedItem
Definition: AssetWrapper.h:110
AssetWrapper::AfterLayoutChange
@ AfterLayoutChange
Definition: AssetWrapper.h:33
TreasureYesNo::setIsYes
void setIsYes(bool isYes)
Definition: TreasureWrapper.h:107
TreasureWrapper::TreasureWrapper
TreasureWrapper(AssetWrapper *parent, treasure *tr, ResourcesManager *resources)
Definition: TreasureWrapper.cpp:25
TreasureWrapper::canDrop
virtual bool canDrop(const QMimeData *data, int row) const override
Definition: TreasureWrapper.cpp:178
TreasureWrapper::addChild
void addChild(treasurelist *tl, archetype *arch)
Definition: TreasureWrapper.cpp:168
AssetWrapper::AssetUpdated
@ AssetUpdated
Definition: AssetWrapper.h:33
AssetWithArtifacts::childrenCount
virtual int childrenCount() const override
Definition: AssetWithArtifacts.h:38
TreasureWrapper::setList
void setList(const treasurelist *list)
Definition: TreasureWrapper.cpp:234
ResourcesManager
Class managing all assets, tracking in which file they are, which are modified, and such.
Definition: ResourcesManager.h:80
AssetWrapper::AfterChildAdd
@ AfterChildAdd
Definition: AssetWrapper.h:33
TreasureWrapper::chance
quint8 chance
Definition: TreasureWrapper.h:36
ResourcesManager::wrap
ArchetypeWrapper * wrap(archetype *arch, AssetWrapper *parent)
Definition: ResourcesManager.h:134
TreasureWrapper::setMagic
void setMagic(uint8_t magic)
Definition: TreasureWrapper.cpp:212
AssetWithArtifacts< treasure >::setSpecificItem
void setSpecificItem(const object *item, bool notify)
Definition: AssetWithArtifacts.h:61
CREPixmap::getTreasureIcon
static QIcon getTreasureIcon()
Definition: CREPixmap.cpp:102
AssetWrapper::PossibleUse
PossibleUse
Definition: AssetWrapper.h:32
AssetWrapper::AfterChildRemove
@ AfterChildRemove
Definition: AssetWrapper.h:33
AssetWrapper::removeChild
virtual void removeChild(AssetWrapper *)
Definition: AssetWrapper.h:77
CREPixmap::getIcon
static QIcon getIcon(uint16_t faceNumber)
Definition: CREPixmap.cpp:65
TreasureWrapper::myNextNo
TreasureYesNo * myNextNo
Definition: TreasureWrapper.h:86
TreasureWrapper::arch
const archetype arch
Definition: TreasureWrapper.h:42
AssetWithArtifacts::child
virtual AssetWrapper * child(int child) override
Definition: AssetWithArtifacts.h:42
TreasureListWrapper
Definition: TreasureListWrapper.h:27
TreasureWrapper::setChance
void setChance(uint8_t chance)
Definition: TreasureWrapper.cpp:201
TreasureWrapper::removeChild
virtual void removeChild(AssetWrapper *child) override
Definition: TreasureWrapper.cpp:147
TreasureYesNo::myWrapped
AssetWrapper * myWrapped
Definition: TreasureWrapper.h:111
AssetWrapper::ChildrenMayUse
@ ChildrenMayUse
Definition: AssetWrapper.h:32
TreasureYesNo::TreasureYesNo
TreasureYesNo(TreasureWrapper *parent, treasure *tr, ResourcesManager *resources, bool isYes)
Definition: TreasureWrapper.cpp:340
TreasureWrapper::displayIcon
virtual QIcon displayIcon() const override
Definition: TreasureWrapper.cpp:72
MimeUtils::TreasureList
static const char * TreasureList
Definition: MimeUtils.h:25
AssetWrapper::BeforeChildAdd
@ BeforeChildAdd
Definition: AssetWrapper.h:33
TreasureWrapper::drop
virtual void drop(const QMimeData *data, int row) override
Definition: TreasureWrapper.cpp:185
TreasureWrapper::doRemoveChild
void doRemoveChild(TreasureYesNo **tr, treasure **ti, int index)
Definition: TreasureWrapper.cpp:136
TreasureWrapper
Wrapper for a treasure item.
Definition: TreasureWrapper.h:33
TreasureWrapper::childrenCount
virtual int childrenCount() const override
Definition: TreasureWrapper.cpp:87
TreasureYesNo::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: TreasureWrapper.cpp:345
AssetWrapper
Base class for all assets that can be displayed or edited by CRE.
Definition: AssetWrapper.h:25
AssetWrapper::myParent
AssetWrapper * myParent
Definition: AssetWrapper.h:92
AssetWithArtifacts< treasure >::myResources
ResourcesManager * myResources
Definition: AssetWithArtifacts.h:58
ArchetypeWrapper.h
AssetWrapper::fillMenu
virtual void fillMenu(QMenu *)
Definition: AssetWrapper.h:66
AssetWrapper::Uses
@ Uses
Definition: AssetWrapper.h:32
TreasureWrapper::uses
virtual PossibleUse uses(const AssetWrapper *asset, std::string &) const override
Definition: TreasureWrapper.cpp:325
AssetTWrapper< treasure >::wrappedItem
treasure * wrappedItem() const
Definition: AssetWrapper.h:107
MimeUtils::extract
static QList< T * > extract(const QMimeData *data, const QString &mime, AssetsCollection< T > *assets)
Definition: MimeUtils.h:31
TreasureWrapper::swapYesNo
void swapYesNo()
Definition: TreasureWrapper.cpp:310
TreasureYesNo
Definition: TreasureWrapper.h:93
AssetWithArtifacts::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: AssetWithArtifacts.h:48
TreasureWrapper::myNextYes
TreasureYesNo * myNextYes
Definition: TreasureWrapper.h:85
TreasureWrapper::child
virtual AssetWrapper * child(int child) override
Definition: TreasureWrapper.cpp:98
TreasureWrapper::setArtifact
void setArtifact(const QString &art)
Definition: TreasureWrapper.cpp:287
TreasureWrapper::magic
quint8 magic
Definition: TreasureWrapper.h:37
ArchetypeWrapper
Definition: ArchetypeWrapper.h:25
ResourcesManager::remove
void remove(treasure *tr)
Definition: ResourcesManager.h:153
TreasureWrapper::setListMagicAdjustment
void setListMagicAdjustment(qint8 value)
Definition: TreasureWrapper.cpp:260
TreasureWrapper::setListMagicValue
void setListMagicValue(quint8 value)
Definition: TreasureWrapper.cpp:250
TreasureListWrapper.h
CREPixmap::getTreasureOneIcon
static QIcon getTreasureOneIcon()
Definition: CREPixmap.cpp:108
TreasureWrapper::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: TreasureWrapper.cpp:114
AssetWrapper::BeforeLayoutChange
@ BeforeLayoutChange
Definition: AssetWrapper.h:33
TreasureWrapper::setArch
void setArch(const archetype *arch)
Definition: TreasureWrapper.cpp:272
TreasureWrapper::nrof
quint16 nrof
Definition: TreasureWrapper.h:38
TreasureWrapper::~TreasureWrapper
virtual ~TreasureWrapper()
Definition: TreasureWrapper.cpp:39
AssetWrapper::BeforeChildRemove
@ BeforeChildRemove
Definition: AssetWrapper.h:33
MimeUtils.h
AssetWrapper::markModified
void markModified(ChangeType change, int extra=0)
Definition: AssetWrapper.h:57
AssetWithArtifacts
Represents an asset to which artifacts may be applied.
Definition: AssetWithArtifacts.h:26
TreasureWrapper::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: TreasureWrapper.cpp:298
TreasureWrapper::listMagicValue
quint8 listMagicValue() const
Definition: TreasureWrapper.cpp:246
TreasureWrapper::artifact
QString artifact
Definition: TreasureWrapper.h:43
TreasureWrapper::setNrof
void setNrof(uint16_t nrof)
Definition: TreasureWrapper.cpp:223
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:32
TreasureWrapper::listMagicAdjustment
qint8 listMagicAdjustment() const
Definition: TreasureWrapper.cpp:257
TreasureWrapper::displayName
virtual QString displayName() const override
Definition: TreasureWrapper.cpp:42
TreasureWrapper.h
AssetWrapper::displayParent
AssetWrapper * displayParent() const
Definition: AssetWrapper.h:46
TreasureWrapper::list
const treasurelist list
Definition: TreasureWrapper.h:39
TreasureWrapper::doAddChild
void doAddChild(TreasureYesNo **my, treasure **ti, bool isYes, int index, treasurelist *tl, archetype *arch)
Definition: TreasureWrapper.cpp:157