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 extern "C" {
22 #include "global.h"
23 #include "artifact.h"
24 #include "libproto.h"
25 }
26 
28  : AssetWithArtifacts<treasure>(parent, "Treasure", tr, resources), myNextYes(nullptr), myNextNo(nullptr)
29 {
30  if (myWrappedItem->next_yes) {
31  myNextYes = new TreasureYesNo(this, myWrappedItem->next_yes, resources, true);
32  }
33  if (myWrappedItem->next_no) {
34  myNextNo = new TreasureYesNo(this, myWrappedItem->next_no, resources, false);
35  }
36  if (tr->item) {
37  setSpecificItem(&tr->item->clone, false);
38  }
39 }
40 
42 }
43 
45  QString name;
46  if (myWrappedItem->item) {
47  name = tr("%1 (%2)").arg(myWrappedItem->item->clone.name, myWrappedItem->item->name);
48  }
49  else if (!myWrappedItem->name || strcmp(myWrappedItem->name, "NONE") == 0) {
50  name = "Nothing";
51  }
52  else {
54  }
55 
56  auto tlw = dynamic_cast<TreasureListWrapper *>(displayParent());
57  if (tlw && tlw->totalChance() != 0) {
58  name = tr("%1 (%2%3%, %4 chances on %5)")
59  .arg(name)
60  .arg(myWrappedItem->nrof > 0 ? tr("1 to %2, ").arg(myWrappedItem->nrof) : "")
61  .arg(qRound((float)100 * myWrappedItem->chance / tlw->totalChance()))
62  .arg(myWrappedItem->chance)
63  .arg(tlw->totalChance());
64  } else {
65  name = tr("%1 (%2%3%)")
66  .arg(name)
67  .arg(myWrappedItem->nrof > 0 ? tr("1 to %2, ").arg(myWrappedItem->nrof) : "")
68  .arg(myWrappedItem->chance);
69  }
70 
71  return name;
72 }
73 
75  if (myWrappedItem->item) {
77  }
78  if (!myWrappedItem->name || strcmp(myWrappedItem->name, "NONE") == 0) {
79  return QIcon();
80  }
81 
82  auto tl = getManager()->treasures()->find(myWrappedItem->name);
83  if (!tl) {
84  return QIcon();
85  }
86  return tl->total_chance == 0 ? CREPixmap::getTreasureIcon() : CREPixmap::getTreasureOneIcon();
87 }
88 
90  int count = 0;
91  if (myWrappedItem->next_yes) {
92  count++;
93  }
94  if (myWrappedItem->next_no) {
95  count++;
96  }
98 }
99 
101  if (myNextYes) {
102  if (child == 0) {
103  return myNextYes;
104  }
105  child--;
106  }
107  if (myNextNo) {
108  if (child == 0) {
109  return myNextNo;
110  }
111  child--;
112  }
114 }
115 
117  int index = 0;
118  if (myNextYes) {
119  if (child == myNextYes) {
120  return index;
121  }
122  index++;
123  }
124  if (myNextNo) {
125  if (child == myNextNo) {
126  return index;
127  }
128  index++;
129  }
130 
132  if (c != -1) {
133  return c + index;
134  }
135  return -1;
136 }
137 
140  myResources->remove(*ti);
141  treasure_free(*ti);
142  (*ti) = nullptr;
143  delete *tr;
144  (*tr) = nullptr;
146  return;
147 }
148 
150  if (child == myNextYes) {
152  return;
153  }
154  if (child == myNextNo) {
156  }
157 }
158 
161  (*ti) = get_empty_treasure();
162  (*my) = new TreasureYesNo(this, *ti, myResources, isYes);
163  (*ti)->item = arch;
164  if (tl) {
165  (*ti)->name = add_string(tl->name);
166  }
168 }
169 
171  if (!myNextYes) {
172  doAddChild(&myNextYes, &myWrappedItem->next_yes, true, 0, tl, arch);
173  return;
174  }
175  if (!myNextNo) {
176  doAddChild(&myNextNo, &myWrappedItem->next_no, false, 1, tl, arch);
177  }
178 }
179 
180 bool TreasureWrapper::canDrop(const QMimeData *data, int) const {
181  return
182  data->hasFormat(MimeUtils::Archetype)
183  || data->hasFormat(MimeUtils::TreasureList)
184  ;
185 }
186 
187 void TreasureWrapper::drop(const QMimeData *data, int) {
188  auto archs = MimeUtils::extract(data, MimeUtils::Archetype, getManager()->archetypes());
189  for (auto arch : archs) {
190  addChild(nullptr, arch);
191  }
192 
194  for (auto list : lists) {
195  addChild(list, nullptr);
196  }
197 }
198 
199 uint8_t TreasureWrapper::chance() const {
200  return myWrappedItem->chance;
201 }
202 
203 void TreasureWrapper::setChance(uint8_t chance) {
204  if (chance != myWrappedItem->chance) {
207  }
208 }
209 
210 uint8_t TreasureWrapper::magic() const {
211  return myWrappedItem->magic;
212 }
213 
214 void TreasureWrapper::setMagic(uint8_t magic) {
215  if (magic != myWrappedItem->magic) {
218  }
219 }
220 
221 uint16_t TreasureWrapper::nrof() const {
222  return myWrappedItem->nrof;
223 }
224 
225 void TreasureWrapper::setNrof(uint16_t nrof) {
226  if (nrof != myWrappedItem->nrof) {
229  }
230 }
231 
232 const treasurelist *TreasureWrapper::list() const {
233  return myWrappedItem->name ? getManager()->treasures()->find(myWrappedItem->name) : nullptr;
234 }
235 
237  if (myWrappedItem->name != (list ? list->name : nullptr)) {
239  if (list) {
241  myWrappedItem->item = nullptr;
242  setSpecificItem(nullptr, true);
243  }
245  }
246 }
247 
248 const archetype *TreasureWrapper::arch() const {
249  return myWrappedItem->item;
250 }
251 
253  if (arch != myWrappedItem->item) {
254  myWrappedItem->item = const_cast<archetype *>(arch);
257  }
259  setSpecificItem(arch ? &arch->clone : nullptr, true);
260  }
261 }
262 
263 void TreasureWrapper::fillMenu(QMenu *menu) {
264  connect(menu->addAction(tr("Delete")), &QAction::triggered, [this] () { myParent->removeChild(this); });
265  if (myNextYes || myNextNo) {
266  connect(menu->addAction(tr("Swap 'yes' and 'no'")), &QAction::triggered, this, &TreasureWrapper::swapYesNo);
267  }
268 }
269 
271  if (myNextYes || myNextNo) {
273  if (myNextYes) {
274  myNextYes->setIsYes(false);
275  }
276  if (myNextNo) {
277  myNextNo->setIsYes(true);
278  }
279  std::swap(myNextYes, myNextNo);
282  }
283 }
284 
286  auto arch = dynamic_cast<const ArchetypeWrapper *>(asset);
287  if (arch) {
288  if (wrappedItem()->item == arch->wrappedItem()) {
289  return Uses;
290  }
291  return ChildrenMayUse;
292  }
293  auto list = dynamic_cast<const TreasureListWrapper *>(asset);
294  if (list) {
295  return myWrappedItem->name == list->wrappedItem()->name ? Uses : DoesntUse;
296  }
297  return DoesntUse;
298 }
299 
301  : AssetWrapper(parent, "empty"), myIsYes(isYes) {
302  myWrapped = resources->wrap(tr, this);
303  }
304 
305 void TreasureYesNo::fillMenu(QMenu *menu) {
306  connect(menu->addAction(tr("Delete")), &QAction::triggered, [this] () { myParent->removeChild(this); });
307  connect(menu->addAction(tr("Swap 'yes' and 'no'")), &QAction::triggered, static_cast<TreasureWrapper *>(myParent), &TreasureWrapper::swapYesNo);
308 }
MimeUtils::Archetype
static const char * Archetype
Definition: MimeUtils.h:24
treasurestruct::item
struct archt * item
Definition: treasure.h:64
AssetTWrapper< treasure >::myWrappedItem
treasure * myWrappedItem
Definition: AssetWrapper.h:97
AssetWrapper::AfterLayoutChange
@ AfterLayoutChange
Definition: AssetWrapper.h:29
treasurestruct::chance
uint8_t chance
Definition: treasure.h:70
FREE_AND_CLEAR_STR_IF
#define FREE_AND_CLEAR_STR_IF(xyz)
Definition: global.h:197
global.h
TreasureYesNo::setIsYes
void setIsYes(bool isYes)
Definition: TreasureWrapper.h:96
treasurestruct::next_yes
struct treasurestruct * next_yes
Definition: treasure.h:67
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
TreasureWrapper::TreasureWrapper
TreasureWrapper(AssetWrapper *parent, treasure *tr, ResourcesManager *resources)
Definition: TreasureWrapper.cpp:27
obj::face
const Face * face
Definition: object.h:336
TreasureWrapper::canDrop
virtual bool canDrop(const QMimeData *data, int row) const override
Definition: TreasureWrapper.cpp:180
TreasureWrapper::addChild
void addChild(treasurelist *tl, archetype *arch)
Definition: TreasureWrapper.cpp:170
AssetWrapper::AssetUpdated
@ AssetUpdated
Definition: AssetWrapper.h:29
AssetWithArtifacts::childrenCount
virtual int childrenCount() const override
Definition: AssetWithArtifacts.h:38
TreasureWrapper::setList
void setList(const treasurelist *list)
Definition: TreasureWrapper.cpp:236
ResourcesManager
Definition: ResourcesManager.h:79
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
AssetWrapper::AfterChildAdd
@ AfterChildAdd
Definition: AssetWrapper.h:29
AssetsManager.h
lists
ignore_list ** lists
Definition: gridarta-types-convert.c:73
c
static event_registration c
Definition: citylife.cpp:427
TreasureWrapper::chance
quint8 chance
Definition: TreasureWrapper.h:35
TreasureWrapper::setMagic
void setMagic(uint8_t magic)
Definition: TreasureWrapper.cpp:214
AssetWithArtifacts< treasure >::setSpecificItem
void setSpecificItem(const object *item, bool notify)
Definition: AssetWithArtifacts.h:61
CREPixmap::getTreasureIcon
static QIcon getTreasureIcon()
Definition: CREPixmap.cpp:104
AssetWrapper::PossibleUse
PossibleUse
Definition: AssetWrapper.h:28
guildoracle.list
list
Definition: guildoracle.py:87
ResourcesManager::wrap
ArchetypeWrapper * wrap(archt *arch, AssetWrapper *parent)
Definition: ResourcesManager.h:124
AssetWrapper::AfterChildRemove
@ AfterChildRemove
Definition: AssetWrapper.h:29
AssetWrapper::removeChild
virtual void removeChild(AssetWrapper *)
Definition: AssetWrapper.h:67
CREPixmap::getIcon
static QIcon getIcon(uint16_t faceNumber)
Definition: CREPixmap.cpp:67
TreasureWrapper::myNextNo
TreasureYesNo * myNextNo
Definition: TreasureWrapper.h:76
TreasureWrapper::arch
const archetype arch
Definition: TreasureWrapper.h:39
AssetWithArtifacts::child
virtual AssetWrapper * child(int child) override
Definition: AssetWithArtifacts.h:42
artifact.h
treasure_free
void treasure_free(treasure *t)
Definition: treasure.c:1374
TreasureListWrapper
Definition: TreasureListWrapper.h:29
treasureliststruct::name
sstring name
Definition: treasure.h:83
TreasureWrapper::setChance
void setChance(uint8_t chance)
Definition: TreasureWrapper.cpp:203
treasurestruct::next_no
struct treasurestruct * next_no
Definition: treasure.h:68
TreasureWrapper::removeChild
virtual void removeChild(AssetWrapper *child) override
Definition: TreasureWrapper.cpp:149
TreasureYesNo::myWrapped
AssetWrapper * myWrapped
Definition: TreasureWrapper.h:100
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
TreasureYesNo::TreasureYesNo
TreasureYesNo(TreasureWrapper *parent, treasure *tr, ResourcesManager *resources, bool isYes)
Definition: TreasureWrapper.cpp:300
archt
Definition: object.h:469
TreasureWrapper::displayIcon
virtual QIcon displayIcon() const override
Definition: TreasureWrapper.cpp:74
MimeUtils::TreasureList
static const char * TreasureList
Definition: MimeUtils.h:25
AssetWrapper::BeforeChildAdd
@ BeforeChildAdd
Definition: AssetWrapper.h:29
TreasureWrapper::drop
virtual void drop(const QMimeData *data, int row) override
Definition: TreasureWrapper.cpp:187
obj::name
sstring name
Definition: object.h:314
TreasureWrapper::doRemoveChild
void doRemoveChild(TreasureYesNo **tr, treasure **ti, int index)
Definition: TreasureWrapper.cpp:138
TreasureWrapper
Definition: TreasureWrapper.h:32
AssetsManager::treasures
Treasures * treasures()
Definition: AssetsManager.h:54
TreasureWrapper::childrenCount
virtual int childrenCount() const override
Definition: TreasureWrapper.cpp:89
TreasureYesNo::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: TreasureWrapper.cpp:305
treasurestruct::magic
uint8_t magic
Definition: treasure.h:71
AssetWrapper
Definition: AssetWrapper.h:22
AssetWrapper::myParent
AssetWrapper * myParent
Definition: AssetWrapper.h:82
AssetWithArtifacts< treasure >::myResources
ResourcesManager * myResources
Definition: AssetWithArtifacts.h:58
navar-midane_time.data
data
Definition: navar-midane_time.py:11
ArchetypeWrapper.h
disinfect.count
int count
Definition: disinfect.py:7
AssetWrapper::Uses
@ Uses
Definition: AssetWrapper.h:28
TreasureWrapper::uses
virtual PossibleUse uses(const AssetWrapper *asset, std::string &) const override
Definition: TreasureWrapper.cpp:285
AssetTWrapper< treasure >::wrappedItem
treasure * wrappedItem() const
Definition: AssetWrapper.h:94
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:270
FREE_AND_CLEAR_STR
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:195
TreasureYesNo
Definition: TreasureWrapper.h:83
AssetWithArtifacts::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: AssetWithArtifacts.h:48
TreasureWrapper::myNextYes
TreasureYesNo * myNextYes
Definition: TreasureWrapper.h:75
TreasureWrapper::child
virtual AssetWrapper * child(int child) override
Definition: TreasureWrapper.cpp:100
archt::clone
object clone
Definition: object.h:473
get_empty_treasure
treasure * get_empty_treasure(void)
Definition: treasure.c:1353
TreasureWrapper::magic
quint8 magic
Definition: TreasureWrapper.h:36
ArchetypeWrapper
Definition: ArchetypeWrapper.h:28
ResourcesManager::remove
void remove(treasure *tr)
Definition: ResourcesManager.h:143
TreasureListWrapper.h
CREPixmap::getTreasureOneIcon
static QIcon getTreasureOneIcon()
Definition: CREPixmap.cpp:110
treasurestruct::nrof
uint16_t nrof
Definition: treasure.h:76
TreasureWrapper::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: TreasureWrapper.cpp:116
AssetWrapper::BeforeLayoutChange
@ BeforeLayoutChange
Definition: AssetWrapper.h:29
assets.h
npc_dialog.index
int index
Definition: npc_dialog.py:102
TreasureWrapper::setArch
void setArch(const archetype *arch)
Definition: TreasureWrapper.cpp:252
TreasureWrapper::nrof
quint16 nrof
Definition: TreasureWrapper.h:37
TreasureWrapper::~TreasureWrapper
virtual ~TreasureWrapper()
Definition: TreasureWrapper.cpp:41
AssetWrapper::BeforeChildRemove
@ BeforeChildRemove
Definition: AssetWrapper.h:29
MimeUtils.h
AssetWrapper::markModified
void markModified(ChangeType change, int extra=0)
Definition: AssetWrapper.h:51
AssetWithArtifacts
Definition: AssetWithArtifacts.h:26
TreasureWrapper::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: TreasureWrapper.cpp:263
TreasureWrapper::setNrof
void setNrof(uint16_t nrof)
Definition: TreasureWrapper.cpp:225
archt::name
sstring name
Definition: object.h:470
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:28
connect
Definition: connect.py:1
TreasureWrapper::displayName
virtual QString displayName() const override
Definition: TreasureWrapper.cpp:44
TreasureWrapper.h
treasurestruct::name
sstring name
Definition: treasure.h:65
AssetWrapper::displayParent
AssetWrapper * displayParent() const
Definition: AssetWrapper.h:42
TreasureWrapper::list
const treasurelist list
Definition: TreasureWrapper.h:38
libproto.h
treasureliststruct
Definition: treasure.h:82
give.name
name
Definition: give.py:27
TreasureWrapper::doAddChild
void doAddChild(TreasureYesNo **my, treasure **ti, bool isYes, int index, treasurelist *tl, archetype *arch)
Definition: TreasureWrapper.cpp:159