Crossfire Server, Trunk
TreasureWrapper.cpp
Go to the documentation of this file.
1 #include "TreasureWrapper.h"
2 #include "TreasureListWrapper.h"
3 #include "../ResourcesManager.h"
4 #include "assets.h"
5 #include "AssetsManager.h"
6 #include "MimeUtils.h"
7 
8 extern "C" {
9 #include "global.h"
10 #include "artifact.h"
11 #include "libproto.h"
12 }
13 
15  : AssetWithArtifacts<treasure>(parent, "Treasure", tr, resources), myNextYes(nullptr), myNextNo(nullptr)
16 {
17  if (myWrappedItem->next_yes) {
18  myNextYes = new TreasureYesNo(this, myWrappedItem->next_yes, resources, true);
19  }
20  if (myWrappedItem->next_no) {
21  myNextNo = new TreasureYesNo(this, myWrappedItem->next_no, resources, false);
22  }
23  if (tr->item) {
24  setSpecificItem(&tr->item->clone, false);
25  }
26 }
27 
29 }
30 
32  QString name;
33  if (myWrappedItem->item) {
34  name = tr("%1 (%2)").arg(myWrappedItem->item->clone.name, myWrappedItem->item->name);
35  }
36  else if (!myWrappedItem->name || strcmp(myWrappedItem->name, "NONE") == 0) {
37  name = "Nothing";
38  }
39  else {
41  }
42 
43  auto tlw = dynamic_cast<TreasureListWrapper *>(displayParent());
44  if (tlw && tlw->totalChance() != 0) {
45  name = tr("%1 (%2%3%, %4 chances on %5)")
46  .arg(name)
47  .arg(myWrappedItem->nrof > 0 ? tr("1 to %2, ").arg(myWrappedItem->nrof) : "")
48  .arg(qRound((float)100 * myWrappedItem->chance / tlw->totalChance()))
49  .arg(myWrappedItem->chance)
50  .arg(tlw->totalChance());
51  } else {
52  name = tr("%1 (%2%3%)")
53  .arg(name)
54  .arg(myWrappedItem->nrof > 0 ? tr("1 to %2, ").arg(myWrappedItem->nrof) : "")
55  .arg(myWrappedItem->chance);
56  }
57 
58  return name;
59 }
60 
62  if (myWrappedItem->item) {
64  }
65  if (!myWrappedItem->name || strcmp(myWrappedItem->name, "NONE") == 0) {
66  return QIcon();
67  }
68 
69  auto tl = getManager()->treasures()->find(myWrappedItem->name);
70  if (!tl) {
71  return QIcon();
72  }
73  return tl->total_chance == 0 ? CREPixmap::getTreasureIcon() : CREPixmap::getTreasureOneIcon();
74 }
75 
76 void TreasureWrapper::displayFillPanel(QWidget *panel) {
77  AssetWrapperPanel *p = static_cast<AssetWrapperPanel *>(panel);
78  p->setItem(this);
79 }
80 
82  int count = 0;
83  if (myWrappedItem->next_yes) {
84  count++;
85  }
86  if (myWrappedItem->next_no) {
87  count++;
88  }
90 }
91 
93  if (myNextYes) {
94  if (child == 0) {
95  return myNextYes;
96  }
97  child--;
98  }
99  if (myNextNo) {
100  if (child == 0) {
101  return myNextNo;
102  }
103  child--;
104  }
106 }
107 
109  int index = 0;
110  if (myNextYes) {
111  if (child == myNextYes) {
112  return index;
113  }
114  index++;
115  }
116  if (myNextNo) {
117  if (child == myNextNo) {
118  return index;
119  }
120  index++;
121  }
122 
124  if (c != -1) {
125  return c + index;
126  }
127  return -1;
128 }
129 
132  myResources->remove(*ti);
133  treasure_free(*ti);
134  (*ti) = nullptr;
135  delete *tr;
136  (*tr) = nullptr;
138  return;
139 }
140 
142  if (child == myNextYes) {
144  return;
145  }
146  if (child == myNextNo) {
148  }
149 }
150 
153  (*ti) = get_empty_treasure();
154  (*my) = new TreasureYesNo(this, *ti, myResources, isYes);
155  (*ti)->item = arch;
156  if (tl) {
157  (*ti)->name = add_string(tl->name);
158  }
160 }
161 
163  if (!myNextYes) {
164  doAddChild(&myNextYes, &myWrappedItem->next_yes, true, 0, tl, arch);
165  return;
166  }
167  if (!myNextNo) {
168  doAddChild(&myNextNo, &myWrappedItem->next_no, false, 1, tl, arch);
169  }
170 }
171 
172 bool TreasureWrapper::canDrop(const QMimeData *data, int) const {
173  return
174  data->hasFormat(MimeUtils::Archetype)
175  || data->hasFormat(MimeUtils::TreasureList)
176  ;
177 }
178 
179 void TreasureWrapper::drop(const QMimeData *data, int) {
180  auto archs = MimeUtils::extract(data, MimeUtils::Archetype, getManager()->archetypes());
181  for (auto arch : archs) {
182  addChild(nullptr, arch);
183  }
184 
186  for (auto list : lists) {
187  addChild(list, nullptr);
188  }
189 }
190 
191 uint8_t TreasureWrapper::chance() const {
192  return myWrappedItem->chance;
193 }
194 
195 void TreasureWrapper::setChance(uint8_t chance) {
196  if (chance != myWrappedItem->chance) {
199  }
200 }
201 
202 uint8_t TreasureWrapper::magic() const {
203  return myWrappedItem->magic;
204 }
205 
206 void TreasureWrapper::setMagic(uint8_t magic) {
207  if (magic != myWrappedItem->magic) {
210  }
211 }
212 
213 uint16_t TreasureWrapper::nrof() const {
214  return myWrappedItem->nrof;
215 }
216 
217 void TreasureWrapper::setNrof(uint16_t nrof) {
218  if (nrof != myWrappedItem->nrof) {
221  }
222 }
223 
224 const treasurelist *TreasureWrapper::list() const {
225  return myWrappedItem->name ? getManager()->treasures()->find(myWrappedItem->name) : nullptr;
226 }
227 
229  if (myWrappedItem->name != (list ? list->name : nullptr)) {
231  if (list) {
233  myWrappedItem->item = nullptr;
234  setSpecificItem(nullptr, true);
235  }
237  }
238 }
239 
240 const archetype *TreasureWrapper::arch() const {
241  return myWrappedItem->item;
242 }
243 
245  if (arch != myWrappedItem->item) {
246  myWrappedItem->item = const_cast<archetype *>(arch);
249  }
251  setSpecificItem(arch ? &arch->clone : nullptr, true);
252  }
253 }
254 
255 void TreasureWrapper::fillMenu(QMenu *menu) {
256  connect(menu->addAction(tr("Delete")), &QAction::triggered, [this] () { myParent->removeChild(this); });
257  if (myNextYes || myNextNo) {
258  connect(menu->addAction(tr("Swap 'yes' and 'no'")), &QAction::triggered, this, &TreasureWrapper::swapYesNo);
259  }
260 }
261 
263  if (myNextYes || myNextNo) {
265  if (myNextYes) {
266  myNextYes->setIsYes(false);
267  }
268  if (myNextNo) {
269  myNextNo->setIsYes(true);
270  }
271  std::swap(myNextYes, myNextNo);
274  }
275 }
276 
278  : AssetWrapper(parent, "empty"), myIsYes(isYes) {
279  myWrapped = resources->wrap(tr, this);
280  }
281 
282 void TreasureYesNo::fillMenu(QMenu *menu) {
283  connect(menu->addAction(tr("Delete")), &QAction::triggered, [this] () { myParent->removeChild(this); });
284  connect(menu->addAction(tr("Swap 'yes' and 'no'")), &QAction::triggered, static_cast<TreasureWrapper *>(myParent), &TreasureWrapper::swapYesNo);
285 }
AssetWrapperPanel
Definition: AssetWrapperPanel.h:16
MimeUtils::Archetype
static const char * Archetype
Definition: MimeUtils.h:12
treasurestruct::item
struct archt * item
Definition: treasure.h:64
AssetTWrapper< treasure >::myWrappedItem
treasure * myWrappedItem
Definition: AssetWrapper.h:87
AssetWrapper::AfterLayoutChange
@ AfterLayoutChange
Definition: AssetWrapper.h:15
treasurestruct::chance
uint8_t chance
Definition: treasure.h:70
FREE_AND_CLEAR_STR_IF
#define FREE_AND_CLEAR_STR_IF(xyz)
Definition: global.h:199
global.h
TreasureYesNo::setIsYes
void setIsYes(bool isYes)
Definition: TreasureWrapper.h:83
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:14
obj::face
const Face * face
Definition: object.h:336
TreasureWrapper::canDrop
virtual bool canDrop(const QMimeData *data, int row) const override
Definition: TreasureWrapper.cpp:172
TreasureWrapper::addChild
void addChild(treasurelist *tl, archetype *arch)
Definition: TreasureWrapper.cpp:162
AssetWrapper::AssetUpdated
@ AssetUpdated
Definition: AssetWrapper.h:15
AssetWithArtifacts::childrenCount
virtual int childrenCount() const override
Definition: AssetWithArtifacts.h:26
TreasureWrapper::setList
void setList(const treasurelist *list)
Definition: TreasureWrapper.cpp:228
ResourcesManager
Definition: ResourcesManager.h:67
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
AssetWrapper::AfterChildAdd
@ AfterChildAdd
Definition: AssetWrapper.h:15
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:23
TreasureWrapper::setMagic
void setMagic(uint8_t magic)
Definition: TreasureWrapper.cpp:206
AssetWithArtifacts< treasure >::setSpecificItem
void setSpecificItem(const object *item, bool notify)
Definition: AssetWithArtifacts.h:49
CREPixmap::getTreasureIcon
static QIcon getTreasureIcon()
Definition: CREPixmap.cpp:92
guildoracle.list
list
Definition: guildoracle.py:87
ResourcesManager::wrap
ArchetypeWrapper * wrap(archt *arch, AssetWrapper *parent)
Definition: ResourcesManager.h:112
AssetWrapper::AfterChildRemove
@ AfterChildRemove
Definition: AssetWrapper.h:15
AssetWrapper::removeChild
virtual void removeChild(AssetWrapper *)
Definition: AssetWrapper.h:52
CREPixmap::getIcon
static QIcon getIcon(uint16_t faceNumber)
Definition: CREPixmap.cpp:55
TreasureWrapper::myNextNo
TreasureYesNo * myNextNo
Definition: TreasureWrapper.h:63
TreasureWrapper::arch
const archetype arch
Definition: TreasureWrapper.h:27
AssetWithArtifacts::child
virtual AssetWrapper * child(int child) override
Definition: AssetWithArtifacts.h:30
AssetWrapperPanel::setItem
virtual void setItem(AssetWrapper *item) override
Definition: AssetWrapperPanel.cpp:13
artifact.h
treasure_free
void treasure_free(treasure *t)
Definition: treasure.c:1374
TreasureListWrapper
Definition: TreasureListWrapper.h:17
treasureliststruct::name
sstring name
Definition: treasure.h:83
TreasureWrapper::setChance
void setChance(uint8_t chance)
Definition: TreasureWrapper.cpp:195
treasurestruct::next_no
struct treasurestruct * next_no
Definition: treasure.h:68
TreasureWrapper::removeChild
virtual void removeChild(AssetWrapper *child) override
Definition: TreasureWrapper.cpp:141
TreasureYesNo::myWrapped
AssetWrapper * myWrapped
Definition: TreasureWrapper.h:87
getManager
AssetsManager * getManager()
Definition: assets.cpp:333
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:277
archt
Definition: object.h:470
TreasureWrapper::displayIcon
virtual QIcon displayIcon() const override
Definition: TreasureWrapper.cpp:61
MimeUtils::TreasureList
static const char * TreasureList
Definition: MimeUtils.h:13
AssetWrapper::BeforeChildAdd
@ BeforeChildAdd
Definition: AssetWrapper.h:15
TreasureWrapper::drop
virtual void drop(const QMimeData *data, int row) override
Definition: TreasureWrapper.cpp:179
obj::name
sstring name
Definition: object.h:314
TreasureWrapper::doRemoveChild
void doRemoveChild(TreasureYesNo **tr, treasure **ti, int index)
Definition: TreasureWrapper.cpp:130
TreasureWrapper
Definition: TreasureWrapper.h:20
AssetsManager::treasures
Treasures * treasures()
Definition: AssetsManager.h:54
TreasureWrapper::childrenCount
virtual int childrenCount() const override
Definition: TreasureWrapper.cpp:81
TreasureYesNo::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: TreasureWrapper.cpp:282
treasurestruct::magic
uint8_t magic
Definition: treasure.h:71
AssetWrapper
Definition: AssetWrapper.h:11
AssetWrapper::myParent
AssetWrapper * myParent
Definition: AssetWrapper.h:67
AssetWithArtifacts< treasure >::myResources
ResourcesManager * myResources
Definition: AssetWithArtifacts.h:46
navar-midane_time.data
data
Definition: navar-midane_time.py:11
disinfect.count
int count
Definition: disinfect.py:7
MimeUtils::extract
static QList< T * > extract(const QMimeData *data, const QString &mime, AssetsCollection< T > *assets)
Definition: MimeUtils.h:18
TreasureWrapper::swapYesNo
void swapYesNo()
Definition: TreasureWrapper.cpp:262
FREE_AND_CLEAR_STR
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:197
TreasureYesNo
Definition: TreasureWrapper.h:70
AssetWithArtifacts::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: AssetWithArtifacts.h:36
TreasureWrapper::myNextYes
TreasureYesNo * myNextYes
Definition: TreasureWrapper.h:62
TreasureWrapper::child
virtual AssetWrapper * child(int child) override
Definition: TreasureWrapper.cpp:92
archt::clone
object clone
Definition: object.h:474
get_empty_treasure
treasure * get_empty_treasure(void)
Definition: treasure.c:1353
TreasureWrapper::magic
quint8 magic
Definition: TreasureWrapper.h:24
ResourcesManager::remove
void remove(treasure *tr)
Definition: ResourcesManager.h:130
TreasureListWrapper.h
CREPixmap::getTreasureOneIcon
static QIcon getTreasureOneIcon()
Definition: CREPixmap.cpp:98
treasurestruct::nrof
uint16_t nrof
Definition: treasure.h:76
TreasureWrapper::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: TreasureWrapper.cpp:108
AssetWrapper::BeforeLayoutChange
@ BeforeLayoutChange
Definition: AssetWrapper.h:15
assets.h
npc_dialog.index
int index
Definition: npc_dialog.py:102
TreasureWrapper::setArch
void setArch(const archetype *arch)
Definition: TreasureWrapper.cpp:244
TreasureWrapper::nrof
quint16 nrof
Definition: TreasureWrapper.h:25
TreasureWrapper::displayFillPanel
virtual void displayFillPanel(QWidget *panel) override
Definition: TreasureWrapper.cpp:76
TreasureWrapper::~TreasureWrapper
virtual ~TreasureWrapper()
Definition: TreasureWrapper.cpp:28
AssetWrapper::BeforeChildRemove
@ BeforeChildRemove
Definition: AssetWrapper.h:15
MimeUtils.h
AssetWrapper::markModified
void markModified(ChangeType change, int extra=0)
Definition: AssetWrapper.h:36
AssetWithArtifacts
Definition: AssetWithArtifacts.h:14
TreasureWrapper::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: TreasureWrapper.cpp:255
TreasureWrapper::setNrof
void setNrof(uint16_t nrof)
Definition: TreasureWrapper.cpp:217
archt::name
sstring name
Definition: object.h:471
connect
Definition: connect.py:1
TreasureWrapper::displayName
virtual QString displayName() const override
Definition: TreasureWrapper.cpp:31
TreasureWrapper.h
treasurestruct::name
sstring name
Definition: treasure.h:65
AssetWrapper::displayParent
AssetWrapper * displayParent() const
Definition: AssetWrapper.h:27
TreasureWrapper::list
const treasurelist list
Definition: TreasureWrapper.h:26
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:151