Crossfire Server, Trunk
QuestWrapper.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 <assert.h>
14 #include "QuestWrapper.h"
15 #include "faces/FaceWrapper.h"
16 #include "ResourcesManager.h"
17 #include "MimeUtils.h"
18 
19 void QuestStepWrapper::fillMenu(QMenu *menu) {
20  connect(menu->addAction(tr("Delete")), &QAction::triggered, [this] () { myParent->removeChild(this); });
21 }
22 
23 void QuestStepWrapper::drag(QMimeData *data) const {
24  QuestWrapper *qw = dynamic_cast<QuestWrapper *>(myParent);
25  if (qw) {
26  MimeUtils::addQuestStep(data, qw->code(), qw->childIndex(const_cast<QuestStepWrapper *>(this)));
27  }
28 }
29 
30 bool QuestStepWrapper::canDrop(const QMimeData *data, int) const {
31  return data->hasFormat(MimeUtils::QuestStep);
32 }
33 
34 void QuestStepWrapper::drop(const QMimeData *data, int row) {
35  myParent->drop(data, myParent->childIndex(this) + (row == -1 ? 0 : 1));
36 }
37 
38 QList<QStringList> QuestStepWrapper::conditions() const {
39  QList<QStringList> ret;
40  char buf[200];
41  for (const auto condition : myWrappedItem->conditions) {
42  quest_write_condition(buf, sizeof(buf), condition);
43  ret.append(QString(buf).split(' '));
44  Q_ASSERT(ret.back().size() == 2);
45  }
46  return ret;
47 }
48 
49 void QuestStepWrapper::setConditions(const QList<QStringList> &conditions) {
50  for (const auto cond : myWrappedItem->conditions) {
52  }
53  myWrappedItem->conditions.clear();
54 
55  for (auto single : conditions) {
56  auto cond = quest_create_condition();
57  if (!quest_condition_from_string(cond, single.join(' ').toStdString().data())) {
58  free(cond);
59  continue;
60  }
61  myWrappedItem->conditions.push_back(cond);
62  }
64 }
65 
66 AssetWrapper::PossibleUse QuestWrapper::uses(const AssetWrapper *asset, std::string &) const {
67  auto face = dynamic_cast<const FaceWrapper *>(asset);
68  if (face) {
69  return myWrappedItem->face == face->wrappedItem() ? Uses : DoesntUse;
70  }
71  return DoesntUse;
72 }
73 
75  return myWrappedItem->steps.size();
76 }
77 
79  if (index >= 0 && index < static_cast<int>(myWrappedItem->steps.size())) {
80  return myResources->wrap(myWrappedItem->steps[index], this);
81  }
82  return nullptr;
83 }
84 
86  for (int index = 0; index < static_cast<int>(myWrappedItem->steps.size()); index++) {
87  if (child == myResources->wrap(myWrappedItem->steps[index], this)) {
88  return index;
89  }
90  }
91  return -1;
92 }
93 
96  AssetTWrapper::wasModified(asset, type, extra);
97 }
98 
99 bool QuestWrapper::canDrop(const QMimeData *data, int) const {
100  return data->hasFormat(MimeUtils::QuestStep);
101 }
102 
103 void QuestWrapper::drop(const QMimeData *data, int row) {
104  std::vector<quest_step_definition *> &steps(myWrappedItem->steps);
105  if (steps.size() < 2) {
106  return;
107  }
108 
109  if (row == -1) {
110  row = 0;
111  }
112 
113  auto dropped = MimeUtils::extractQuestSteps(data);
114  for (auto single : dropped) {
115  if (single.first != code()) {
116  continue;
117  }
118 
119  if (single.second < 0 || single.second >= static_cast<int>(steps.size()) || single.second == row) {
120  continue;
121  }
122 
123  markModified(BeforeChildRemove, single.second);
124  auto step = steps[single.second];
125  steps.erase(steps.begin() + single.second);
126  markModified(AfterChildRemove, single.second);
127  if (row > single.second) {
128  row--;
129  }
130 
132  steps.insert(steps.begin() + row, step);
134  }
135 }
136 
137 void QuestWrapper::fillMenu(QMenu *menu) {
138  if (myParent) {
139  myParent->fillMenu(menu);
140  }
141  connect(menu->addAction(tr("Add step")), &QAction::triggered, [this] () { addStep(); });
142 }
143 
145  auto max = std::max_element(myWrappedItem->steps.cbegin(), myWrappedItem->steps.cend(),
146  [] (const auto &left, const auto &right) { return left->step < right->step; });
147  auto ns = quest_create_step();
148  ns->step = (max == myWrappedItem->steps.end() ? 0 : (*max)->step) + 10;
150  myWrappedItem->steps.push_back(ns);
152 }
153 
155  int index = childIndex(child);
156  if (index == -1) {
157  return;
158  }
160  auto step = myWrappedItem->steps[index];
161  myWrappedItem->steps.erase(myWrappedItem->steps.begin() + index);
162  quest_destroy_step(step);
164 }
AssetTWrapper< quest_step_definition >::myWrappedItem
quest_step_definition * myWrappedItem
Definition: AssetWrapper.h:104
quest_create_step
quest_step_definition * quest_create_step(void)
Definition: Quests.cpp:49
QuestWrapper::childrenCount
virtual int childrenCount() const override
Definition: QuestWrapper.cpp:74
AssetWrapper::AssetUpdated
@ AssetUpdated
Definition: AssetWrapper.h:33
quest_write_condition
void quest_write_condition(char *buf, size_t len, const quest_condition *cond)
Definition: Quests.cpp:130
AssetWrapper::AfterChildAdd
@ AfterChildAdd
Definition: AssetWrapper.h:33
MimeUtils::extractQuestSteps
QList< QPair< QString, int > > extractQuestSteps(const QMimeData *data)
Definition: MimeUtils.cpp:31
ResourcesManager::questModified
void questModified(quest_definition *quest)
Definition: ResourcesManager.cpp:222
ResourcesManager::wrap
ArchetypeWrapper * wrap(archetype *arch, AssetWrapper *parent)
Definition: ResourcesManager.h:131
QuestWrapper
Definition: QuestWrapper.h:66
AssetWrapper::PossibleUse
PossibleUse
Definition: AssetWrapper.h:32
MimeUtils::QuestStep
static const char * QuestStep
Definition: MimeUtils.h:26
AssetWrapper::AfterChildRemove
@ AfterChildRemove
Definition: AssetWrapper.h:33
AssetWrapper::removeChild
virtual void removeChild(AssetWrapper *)
Definition: AssetWrapper.h:71
QuestWrapper::code
QString code
Definition: QuestWrapper.h:69
QuestWrapper::face
const Face face
Definition: QuestWrapper.h:71
buf
StringBuffer * buf
Definition: readable.cpp:1551
QuestWrapper.h
FaceWrapper
Definition: FaceWrapper.h:24
QuestWrapper::drop
virtual void drop(const QMimeData *, int) override
Definition: QuestWrapper.cpp:103
AssetWrapper::BeforeChildAdd
@ BeforeChildAdd
Definition: AssetWrapper.h:33
QuestWrapper::canDrop
virtual bool canDrop(const QMimeData *, int) const override
Definition: QuestWrapper.cpp:99
MimeUtils::addQuestStep
void addQuestStep(QMimeData *data, QString code, int index)
Definition: MimeUtils.cpp:24
QuestWrapper::removeChild
virtual void removeChild(AssetWrapper *child) override
Definition: QuestWrapper.cpp:154
QuestStepWrapper
Definition: QuestWrapper.h:25
quest_definition::face
const Face * face
Definition: quest.h:43
AssetWrapper
Definition: AssetWrapper.h:25
QuestWrapper::child
virtual AssetWrapper * child(int) override
Definition: QuestWrapper.cpp:78
AssetWrapper::myParent
AssetWrapper * myParent
Definition: AssetWrapper.h:86
QuestWrapper::wasModified
virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra) override
Definition: QuestWrapper.cpp:94
AssetWrapper::ChangeType
ChangeType
Definition: AssetWrapper.h:33
navar-midane_time.data
data
Definition: navar-midane_time.py:11
AssetWrapper::fillMenu
virtual void fillMenu(QMenu *)
Definition: AssetWrapper.h:64
FaceWrapper.h
say.max
dictionary max
Definition: say.py:148
AssetWrapper::Uses
@ Uses
Definition: AssetWrapper.h:32
QuestWrapper::myResources
ResourcesManager * myResources
Definition: QuestWrapper.h:151
quest_destroy_condition
void quest_destroy_condition(quest_condition *condition)
Definition: Quests.cpp:69
AssetWrapper::childIndex
virtual int childIndex(AssetWrapper *)
Definition: AssetWrapper.h:51
QuestStepWrapper::canDrop
virtual bool canDrop(const QMimeData *, int) const override
Definition: QuestWrapper.cpp:30
AssetWrapper::drop
virtual void drop(const QMimeData *, int)
Definition: AssetWrapper.h:63
ResourcesManager.h
QuestStepWrapper::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: QuestWrapper.cpp:19
QuestWrapper::fillMenu
virtual void fillMenu(QMenu *menu) override
Definition: QuestWrapper.cpp:137
QuestStepWrapper::conditions
QList< QStringList > conditions
Definition: QuestWrapper.h:31
AssetWrapper::wasModified
virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra)
Definition: AssetWrapper.h:78
npc_dialog.index
int index
Definition: npc_dialog.py:102
QuestStepWrapper::setConditions
void setConditions(const QList< QStringList > &conditions)
Definition: QuestWrapper.cpp:49
QuestWrapper::uses
virtual PossibleUse uses(const AssetWrapper *asset, std::string &) const override
Definition: QuestWrapper.cpp:66
QuestStepWrapper::drop
virtual void drop(const QMimeData *, int) override
Definition: QuestWrapper.cpp:34
AssetWrapper::BeforeChildRemove
@ BeforeChildRemove
Definition: AssetWrapper.h:33
MimeUtils.h
AssetWrapper::markModified
void markModified(ChangeType change, int extra=0)
Definition: AssetWrapper.h:55
quest_step_definition::conditions
std::vector< quest_condition * > conditions
Definition: quest.h:33
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:32
quest_destroy_step
void quest_destroy_step(quest_step_definition *step)
Definition: Quests.cpp:74
QuestWrapper::addStep
void addStep()
Definition: QuestWrapper.cpp:144
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2603
connect
Definition: connect.py:1
QuestWrapper::childIndex
virtual int childIndex(AssetWrapper *) override
Definition: QuestWrapper.cpp:85
quest_condition_from_string
int quest_condition_from_string(quest_condition *condition, const char *buffer)
Definition: Quests.cpp:100
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
quest_create_condition
quest_condition * quest_create_condition(void)
Definition: Quests.cpp:56
QuestStepWrapper::drag
virtual void drag(QMimeData *) const override
Definition: QuestWrapper.cpp:23
quest_definition::steps
std::vector< quest_step_definition * > steps
Definition: quest.h:46