Crossfire Server, Trunk
TreasureListWrapper.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 "TreasureListWrapper.h"
14 #include "../ResourcesManager.h"
15 #include "../CREPixmap.h"
16 #include "MimeUtils.h"
17 #include "TreasureLoader.h"
18 #include "assets.h"
19 #include "AssetsManager.h"
21 
23  : AssetTWrapper(parent, "TreasureList", list), myResources(resources)
24 {
25 }
26 
28  int count = 0;
29  auto item = myWrappedItem->items;
30  while (item) {
31  count++;
32  item = item->next;
33  }
34  return count;
35 }
36 
38  return itemCount();
39 }
40 
42  auto item = myWrappedItem->items;
43  while (item && child > 0) {
44  item = item->next;
45  child--;
46  }
47  return myResources->wrap(item, this);
48 }
49 
51  auto item = myWrappedItem->items;
52  int index = 0;
53  while (item) {
54  if (myResources->wrap(item, this) == child) {
55  return index;
56  }
57  item = item->next;
58  index++;
59  }
60  return -1;
61 }
62 
64  if (isSingle == isSingleItem() || myWrappedItem->items == nullptr) {
65  return;
66  }
67  if (isSingle) {
69  } else {
71  }
73 }
74 
77  auto item = myWrappedItem->items;
78  while (item) {
79  myWrappedItem->total_chance += item->chance;
80  item = item->next;
81  }
82 }
83 
86  if (childIndex(asset) != -1 && type == AssetUpdated) {
90  return;
91  }
92  }
93  AssetWrapper::wasModified(asset, type, extra);
94 }
95 
96 void TreasureListWrapper::drag(QMimeData *data) const {
97  QByteArray ba(data->data(MimeUtils::TreasureList));
98  QDataStream df(&ba, QIODevice::WriteOnly);
99  df << QString(myWrappedItem->name);
100  data->setData(MimeUtils::TreasureList, ba);
101 }
102 
103 bool TreasureListWrapper::canDrop(const QMimeData *data, int) const {
104  return
105  data->hasFormat(MimeUtils::Archetype)
106  || data->hasFormat(MimeUtils::TreasureList)
107  ;
108 }
109 
110 void TreasureListWrapper::drop(const QMimeData *data, int row) {
111  bool modified = false;
112  if (row == -1) {
113  row = childrenCount();
114  }
115 
116  auto archs = MimeUtils::extract(data, MimeUtils::Archetype, getManager()->archetypes());
117  modified |= !archs.empty();
118  for (auto arch : archs) {
120  auto item = treasure_insert(myWrappedItem, row);
121  item->item = arch;
123  row++;
124  }
125 
127  modified |= !lists.empty();
128  for (auto list : lists) {
130  auto item = treasure_insert(myWrappedItem, row);
131  item->name = add_string(list->name);
133  row++;
134  }
136  fixTotalChance();
138  }
139 }
140 
142  int index = childIndex(child);
143  if (index != -1) {
144  auto t = dynamic_cast<TreasureWrapper *>(child);
146  myResources->remove(t->wrappedItem());
150  fixTotalChance();
152  return;
153  }
154  }
155 }
156 
158  return
159  dynamic_cast<const ArchetypeWrapper *>(asset)
160  || dynamic_cast<const TreasureListWrapper *>(asset)
162 }
ResourcesManager::treasureModified
void treasureModified(treasurelist *treasure)
Definition: ResourcesManager.cpp:230
MimeUtils::Archetype
static const char * Archetype
Definition: MimeUtils.h:24
AssetTWrapper< treasurelist >::myWrappedItem
treasurelist * myWrappedItem
Definition: AssetWrapper.h:104
TreasureListWrapper::myResources
ResourcesManager * myResources
Definition: TreasureListWrapper.h:60
TreasureListWrapper::TreasureListWrapper
TreasureListWrapper(AssetWrapper *parent, treasurelist *list, ResourcesManager *resources)
Definition: TreasureListWrapper.cpp:22
AssetWrapper::AssetUpdated
@ AssetUpdated
Definition: AssetWrapper.h:33
ResourcesManager
Definition: ResourcesManager.h:80
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
treasurelist::items
treasure * items
Definition: treasure.h:92
AssetWrapper::AfterChildAdd
@ AfterChildAdd
Definition: AssetWrapper.h:33
AssetsManager.h
TreasureListWrapper::fixTotalChance
void fixTotalChance()
Definition: TreasureListWrapper.cpp:75
TreasureListWrapper::wasModified
virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra) override
Definition: TreasureListWrapper.cpp:84
ResourcesManager::wrap
ArchetypeWrapper * wrap(archetype *arch, AssetWrapper *parent)
Definition: ResourcesManager.h:132
treasurelist::total_chance
int16_t total_chance
Definition: treasure.h:87
AssetWrapper::PossibleUse
PossibleUse
Definition: AssetWrapper.h:32
guildoracle.list
list
Definition: guildoracle.py:87
TreasureListWrapper::setSingleItem
void setSingleItem(bool isSingle)
Definition: TreasureListWrapper.cpp:63
AssetWrapper::AfterChildRemove
@ AfterChildRemove
Definition: AssetWrapper.h:33
TreasureListWrapper::drag
virtual void drag(QMimeData *data) const override
Definition: TreasureListWrapper.cpp:96
TreasureLoader.h
TreasureListWrapper
Definition: TreasureListWrapper.h:27
TreasureListWrapper::child
virtual AssetWrapper * child(int child) override
Definition: TreasureListWrapper.cpp:41
getManager
AssetsManager * getManager()
Definition: assets.cpp:305
AssetWrapper::ChildrenMayUse
@ ChildrenMayUse
Definition: AssetWrapper.h:32
TreasureListWrapper::uses
virtual PossibleUse uses(const AssetWrapper *, std::string &) const override
Definition: TreasureListWrapper.cpp:157
MimeUtils::TreasureList
static const char * TreasureList
Definition: MimeUtils.h:25
AssetWrapper::BeforeChildAdd
@ BeforeChildAdd
Definition: AssetWrapper.h:33
TreasureWrapper
Definition: TreasureWrapper.h:33
treasurelist
Definition: treasure.h:85
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
AssetWrapper::ChangeType
ChangeType
Definition: AssetWrapper.h:33
TreasureListWrapper::removeChild
virtual void removeChild(AssetWrapper *child) override
Definition: TreasureListWrapper.cpp:141
navar-midane_time.data
data
Definition: navar-midane_time.py:11
ArchetypeWrapper.h
disinfect.count
int count
Definition: disinfect.py:7
TreasureListWrapper::childrenCount
virtual int childrenCount() const override
Definition: TreasureListWrapper.cpp:37
AssetWrapper::modified
void modified()
TreasureListWrapper::isSingleItem
bool isSingleItem
Definition: TreasureListWrapper.h:33
MimeUtils::extract
static QList< T * > extract(const QMimeData *data, const QString &mime, AssetsCollection< T > *assets)
Definition: MimeUtils.h:31
treasure_remove_item
void treasure_remove_item(treasurelist *list, int position)
Definition: treasure.cpp:1451
Floor.t
t
Definition: Floor.py:62
TreasureListWrapper::canDrop
virtual bool canDrop(const QMimeData *data, int row) const override
Definition: TreasureListWrapper.cpp:103
item
Definition: item.py:1
ArchetypeWrapper
Definition: ArchetypeWrapper.h:25
ResourcesManager::remove
void remove(treasure *tr)
Definition: ResourcesManager.h:151
AssetWrapper::wasModified
virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra)
Definition: AssetWrapper.h:78
TreasureListWrapper::itemCount
int itemCount
Definition: TreasureListWrapper.h:32
TreasureListWrapper.h
assets.h
npc_dialog.index
int index
Definition: npc_dialog.py:102
treasure_insert
treasure * treasure_insert(treasurelist *list, int position)
Definition: treasure.cpp:1428
AssetWrapper::BeforeChildRemove
@ BeforeChildRemove
Definition: AssetWrapper.h:33
MimeUtils.h
AssetWrapper::markModified
void markModified(ChangeType change, int extra=0)
Definition: AssetWrapper.h:55
TreasureListWrapper::drop
virtual void drop(const QMimeData *data, int row) override
Definition: TreasureListWrapper.cpp:110
lists
ignore_list ** lists
Definition: gridarta-types-convert.cpp:73
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:32
TreasureListWrapper::childIndex
virtual int childIndex(AssetWrapper *child) override
Definition: TreasureListWrapper.cpp:50
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
AssetTWrapper
Definition: AssetWrapper.h:94