Crossfire Server, Trunk
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 {
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) {
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 
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) {
150  return;
151  }
152  if (child == myNextNo) {
154  }
155 }
156 
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  }
166 }
167 
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
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, arch);
189  }
190 
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) {
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) {
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) {
227  }
228 }
229 
230 const treasurelist *TreasureWrapper::list() const {
231  return myWrappedItem->name ? getManager()->treasures()->find(myWrappedItem->name) : nullptr;
232 }
233 
235  if (myWrappedItem->name != (list ? list->name : nullptr)) {
237  if (list) {
239  myWrappedItem->item = nullptr;
240  setSpecificItem(nullptr, true);
241  }
243  }
244 }
245 
248 }
249 
254  }
255 }
256 
259 }
264  }
265 }
266 
267 
268 const archetype *TreasureWrapper::arch() const {
269  return myWrappedItem->item;
270 }
271 
273  if (arch != myWrappedItem->item) {
274  myWrappedItem->item = const_cast<archetype *>(arch);
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) {
290  if (!art.isEmpty()) {
291  myWrappedItem->artifact = add_string(art.toLocal8Bit().data());
292  }
294  }
295 }
296 
297 
298 void TreasureWrapper::fillMenu(QMenu *menu) {
299  connect(menu->addAction(tr("Delete")), &QAction::triggered, [this] () { myParent->removeChild(this); });
300  if (myNextYes || myNextNo) {
301  connect(menu->addAction(tr("Swap 'yes' and 'no'")), &QAction::triggered, this, &TreasureWrapper::swapYesNo);
302  }
303 }
304 
306  if (myNextYes || myNextNo) {
308  if (myNextYes) {
309  myNextYes->setIsYes(false);
310  }
311  if (myNextNo) {
312  myNextNo->setIsYes(true);
313  }
314  std::swap(myNextYes, myNextNo);
317  }
318 }
319 
321  auto arch = dynamic_cast<const ArchetypeWrapper *>(asset);
322  if (arch) {
323  if (wrappedItem()->item == arch->wrappedItem()) {
324  return Uses;
325  }
326  return ChildrenMayUse;
327  }
328  auto list = dynamic_cast<const TreasureListWrapper *>(asset);
329  if (list) {
330  return myWrappedItem->name == list->wrappedItem()->name ? Uses : DoesntUse;
331  }
332  return DoesntUse;
333 }
334 
336  : AssetWrapper(parent, "empty"), myIsYes(isYes) {
337  myWrapped = resources->wrap(tr, this);
338  }
339 
340 void TreasureYesNo::fillMenu(QMenu *menu) {
341  connect(menu->addAction(tr("Delete")), &QAction::triggered, [this] () { myParent->removeChild(this); });
342  connect(menu->addAction(tr("Swap 'yes' and 'no'")), &QAction::triggered, static_cast<TreasureWrapper *>(myParent), &TreasureWrapper::swapYesNo);
343 }
MimeUtils::Archetype
static const char * Archetype
Definition: MimeUtils.h:24
AssetTWrapper< treasure >::myWrappedItem
treasure * myWrappedItem
Definition: AssetWrapper.h:103
AssetWrapper::AfterLayoutChange
@ AfterLayoutChange
Definition: AssetWrapper.h:32
FREE_AND_CLEAR_STR_IF
#define FREE_AND_CLEAR_STR_IF(xyz)
Definition: global.h:204
global.h
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
treasure::chance
uint8_t chance
Definition: treasure.h:73
TreasureWrapper::addChild
void addChild(treasurelist *tl, archetype *arch)
Definition: TreasureWrapper.cpp:168
AssetWrapper::AssetUpdated
@ AssetUpdated
Definition: AssetWrapper.h:32
AssetWithArtifacts::childrenCount
virtual int childrenCount() const override
Definition: AssetWithArtifacts.h:38
TreasureWrapper::setList
void setList(const treasurelist *list)
Definition: TreasureWrapper.cpp:234
ResourcesManager
Definition: ResourcesManager.h:80
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
AssetWrapper::AfterChildAdd
@ AfterChildAdd
Definition: AssetWrapper.h:32
AssetsManager.h
c
static event_registration c
Definition: citylife.cpp:425
TreasureWrapper::chance
quint8 chance
Definition: TreasureWrapper.h:36
ResourcesManager::wrap
ArchetypeWrapper * wrap(archetype *arch, AssetWrapper *parent)
Definition: ResourcesManager.h:130
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:31
guildoracle.list
list
Definition: guildoracle.py:87
treasure::list_magic_value
uint8_t list_magic_value
Definition: treasure.h:68
AssetWrapper::AfterChildRemove
@ AfterChildRemove
Definition: AssetWrapper.h:32
AssetWrapper::removeChild
virtual void removeChild(AssetWrapper *)
Definition: AssetWrapper.h:70
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
artifact.h
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
getManager
AssetsManager * getManager()
Definition: assets.cpp:305
AssetWrapper::ChildrenMayUse
@ ChildrenMayUse
Definition: AssetWrapper.h:31
AssetsCollection::find
T * find(const Key &name)
Definition: AssetsCollection.h:108
TreasureYesNo::TreasureYesNo
TreasureYesNo(TreasureWrapper *parent, treasure *tr, ResourcesManager *resources, bool isYes)
Definition: TreasureWrapper.cpp:335
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:32
treasure::name
sstring name
Definition: treasure.h:66
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
Definition: TreasureWrapper.h:33
AssetsManager::treasures
Treasures * treasures()
Definition: AssetsManager.h:54
TreasureWrapper::childrenCount
virtual int childrenCount() const override
Definition: TreasureWrapper.cpp:87
TreasureYesNo::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: TreasureWrapper.cpp:340
treasurelist
Definition: treasure.h:85
archetype::clone
object clone
Definition: object.h:476
AssetWrapper
Definition: AssetWrapper.h:25
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
treasurelist::name
sstring name
Definition: treasure.h:86
treasure::nrof
uint16_t nrof
Definition: treasure.h:79
AssetWrapper::myParent
AssetWrapper * myParent
Definition: AssetWrapper.h:85
object::face
const Face * face
Definition: object.h:339
treasure_free
void treasure_free(treasure *t)
Definition: treasure.cpp:1396
AssetWithArtifacts< treasure >::myResources
ResourcesManager * myResources
Definition: AssetWithArtifacts.h:58
treasure::item
struct archetype * item
Definition: treasure.h:64
navar-midane_time.data
data
Definition: navar-midane_time.py:11
ArchetypeWrapper.h
disinfect.count
int count
Definition: disinfect.py:7
archetype
Definition: object.h:472
AssetWrapper::Uses
@ Uses
Definition: AssetWrapper.h:31
TreasureWrapper::uses
virtual PossibleUse uses(const AssetWrapper *asset, std::string &) const override
Definition: TreasureWrapper.cpp:320
AssetTWrapper< treasure >::wrappedItem
treasure * wrappedItem() const
Definition: AssetWrapper.h:100
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:305
FREE_AND_CLEAR_STR
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:202
TreasureYesNo
Definition: TreasureWrapper.h:93
treasure::next_yes
treasure * next_yes
Definition: treasure.h:70
AssetWithArtifacts::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: AssetWithArtifacts.h:48
treasure::magic
uint8_t magic
Definition: treasure.h:74
TreasureWrapper::myNextYes
TreasureYesNo * myNextYes
Definition: TreasureWrapper.h:85
TreasureWrapper::child
virtual AssetWrapper * child(int child) override
Definition: TreasureWrapper.cpp:98
object::name
sstring name
Definition: object.h:317
get_empty_treasure
treasure * get_empty_treasure(void)
Definition: treasure.cpp:1375
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:149
autojail.value
value
Definition: autojail.py:6
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:32
assets.h
npc_dialog.index
int index
Definition: npc_dialog.py:102
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:32
MimeUtils.h
AssetWrapper::markModified
void markModified(ChangeType change, int extra=0)
Definition: AssetWrapper.h:54
AssetWithArtifacts
Definition: AssetWithArtifacts.h:26
TreasureWrapper::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: TreasureWrapper.cpp:298
treasure::next_no
treasure * next_no
Definition: treasure.h:71
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
lists
ignore_list ** lists
Definition: gridarta-types-convert.cpp:73
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:31
archetype::name
sstring name
Definition: object.h:473
treasure
Definition: treasure.h:63
connect
Definition: connect.py:1
TreasureWrapper::listMagicAdjustment
qint8 listMagicAdjustment() const
Definition: TreasureWrapper.cpp:257
TreasureWrapper::displayName
virtual QString displayName() const override
Definition: TreasureWrapper.cpp:42
TreasureWrapper.h
treasure::artifact
sstring artifact
Definition: treasure.h:65
AssetWrapper::displayParent
AssetWrapper * displayParent() const
Definition: AssetWrapper.h:45
TreasureWrapper::list
const treasurelist list
Definition: TreasureWrapper.h:39
libproto.h
give.name
name
Definition: give.py:27
treasure::list_magic_adjustment
int8_t list_magic_adjustment
Definition: treasure.h:67
TreasureWrapper::doAddChild
void doAddChild(TreasureYesNo **my, treasure **ti, bool isYes, int index, treasurelist *tl, archetype *arch)
Definition: TreasureWrapper.cpp:157