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  auto condition = myWrappedItem->conditions;
42  while (condition) {
43  quest_write_condition(buf, sizeof(buf), condition);
44  ret.append(QString(buf).split(' '));
45  Q_ASSERT(ret.back().size() == 2);
46  condition = condition->next;
47  }
48  return ret;
49 }
50 
51 void QuestStepWrapper::setConditions(const QList<QStringList> &conditions) {
52  auto cond = myWrappedItem->conditions;
53  while (cond) {
54  auto n = cond->next;
56  cond = n;
57  }
58  myWrappedItem->conditions = nullptr;
59  quest_condition *last = nullptr;
60 
61  for (auto single : conditions) {
62  auto cond = quest_create_condition();
63  if (!quest_condition_from_string(cond, single.join(' ').toStdString().data())) {
64  free(cond);
65  continue;
66  }
67  if (last)
68  last->next = cond;
69  else
70  myWrappedItem->conditions = cond;
71  last = cond;
72  }
74 }
75 
76 AssetWrapper::PossibleUse QuestWrapper::uses(const AssetWrapper *asset, std::string &) const {
77  auto face = dynamic_cast<const FaceWrapper *>(asset);
78  if (face) {
79  return myWrappedItem->face == face->wrappedItem() ? Uses : DoesntUse;
80  }
81  return DoesntUse;
82 }
83 
85  int count = 0;
86  auto step = myWrappedItem->steps;
87  while (step) {
88  step = step->next;
89  count++;
90  }
91  return count;
92 }
93 
95  auto step = myWrappedItem->steps;
96  while (step && index > 0) {
97  step = step->next;
98  index--;
99  }
100  return step ? myResources->wrap(step, this) : nullptr;
101 }
102 
104  int index = 0;
105  auto step = myWrappedItem->steps;
106  while (step && myResources->wrap(step, this) != child) {
107  index++;
108  step = step->next;
109  }
110  return step == nullptr ? -1 : index;
111 }
112 
115  AssetTWrapper::wasModified(asset, type, extra);
116 }
117 
118 bool QuestWrapper::canDrop(const QMimeData *data, int) const {
119  return data->hasFormat(MimeUtils::QuestStep);
120 }
121 
122 void QuestWrapper::drop(const QMimeData *data, int row) {
123  std::vector<quest_step_definition *> steps;
124  auto pos = myWrappedItem->steps;
125  while (pos) {
126  steps.push_back(pos);
127  pos = pos->next;
128  }
129  if (steps.size() < 2) {
130  return;
131  }
132 
133  if (row == -1) {
134  row = 0;
135  }
136 
137  auto dropped = MimeUtils::extractQuestSteps(data);
138  for (auto single : dropped) {
139  if (single.first != code()) {
140  continue;
141  }
142 
143  if (single.second < 0 || single.second >= static_cast<int>(steps.size()) || single.second == row) {
144  continue;
145  }
146 
147  markModified(BeforeChildRemove, single.second);
148  auto step = steps[single.second];
149  steps.erase(steps.begin() + single.second);
150  markModified(AfterChildRemove, single.second);
151  if (row > single.second) {
152  row--;
153  }
154 
156  steps.insert(steps.begin() + row, step);
158  }
159 
160  pos = myWrappedItem->steps = steps[0];
161  auto step = steps.begin();
162  ++step;
163  while (step < steps.end()) {
164  pos->next = (*step);
165  pos = pos->next;
166  pos->next = nullptr;
167  ++step;
168  }
169 }
170 
171 void QuestWrapper::fillMenu(QMenu *menu) {
172  if (myParent) {
173  myParent->fillMenu(menu);
174  }
175  connect(menu->addAction(tr("Add step")), &QAction::triggered, [this] () { addStep(); });
176 }
177 
179  int index = 0, count = 0;;
180  quest_step_definition *step = myWrappedItem->steps, *last = nullptr;
181  while (step) {
182  index = qMax(index, step->step);
183  last = step;
184  step = step->next;
185  count++;
186  }
188  auto ns = quest_create_step();
189  ns->step = index + 10;
190  if (last) {
191  last->next = ns;
192  } else {
193  assert(!myWrappedItem->steps);
194  myWrappedItem->steps = ns;
195  }
197 }
198 
200  quest_step_definition *step = myWrappedItem->steps, *last = nullptr;
201  int index = 0;
202  while (myResources->wrap(step, this) != child) {
203  index++;
204  last = step;
205  step = step->next;
206  }
207 
208  if (!step) {
209  return;
210  }
211 
213  if (last == nullptr) {
214  assert(step == myWrappedItem->steps);
215  myWrappedItem->steps = step->next;
216  } else {
217  last->next = step->next;
218  }
219  quest_destroy_step(step);
221 }
AssetTWrapper< quest_step_definition >::myWrappedItem
quest_step_definition * myWrappedItem
Definition: AssetWrapper.h:103
quest_definition::steps
quest_step_definition * steps
Definition: quest.h:47
quest_create_step
quest_step_definition * quest_create_step(void)
Definition: Quests.cpp:49
QuestWrapper::childrenCount
virtual int childrenCount() const override
Definition: QuestWrapper.cpp:84
AssetWrapper::AssetUpdated
@ AssetUpdated
Definition: AssetWrapper.h:32
quest_write_condition
void quest_write_condition(char *buf, size_t len, const quest_condition *cond)
Definition: Quests.cpp:136
AssetWrapper::AfterChildAdd
@ AfterChildAdd
Definition: AssetWrapper.h:32
MimeUtils::extractQuestSteps
QList< QPair< QString, int > > extractQuestSteps(const QMimeData *data)
Definition: MimeUtils.cpp:31
ResourcesManager::questModified
void questModified(quest_definition *quest)
Definition: ResourcesManager.cpp:225
QuestWrapper
Definition: QuestWrapper.h:66
AssetWrapper::PossibleUse
PossibleUse
Definition: AssetWrapper.h:31
MimeUtils::QuestStep
static const char * QuestStep
Definition: MimeUtils.h:26
ResourcesManager::wrap
ArchetypeWrapper * wrap(archt *arch, AssetWrapper *parent)
Definition: ResourcesManager.h:126
AssetWrapper::AfterChildRemove
@ AfterChildRemove
Definition: AssetWrapper.h:32
AssetWrapper::removeChild
virtual void removeChild(AssetWrapper *)
Definition: AssetWrapper.h:70
QuestWrapper::code
QString code
Definition: QuestWrapper.h:69
quest_step_definition::step
int step
Definition: quest.h:30
QuestWrapper::face
const Face face
Definition: QuestWrapper.h:71
buf
StringBuffer * buf
Definition: readable.cpp:1611
quest_step_definition
Definition: quest.h:29
QuestWrapper.h
FaceWrapper
Definition: FaceWrapper.h:24
QuestWrapper::drop
virtual void drop(const QMimeData *, int) override
Definition: QuestWrapper.cpp:122
quest_step_definition::next
struct quest_step_definition * next
Definition: quest.h:33
AssetWrapper::BeforeChildAdd
@ BeforeChildAdd
Definition: AssetWrapper.h:32
QuestWrapper::canDrop
virtual bool canDrop(const QMimeData *, int) const override
Definition: QuestWrapper.cpp:118
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:199
QuestStepWrapper
Definition: QuestWrapper.h:25
quest_definition::face
const Face * face
Definition: quest.h:44
AssetWrapper
Definition: AssetWrapper.h:25
QuestWrapper::child
virtual AssetWrapper * child(int) override
Definition: QuestWrapper.cpp:94
AssetWrapper::myParent
AssetWrapper * myParent
Definition: AssetWrapper.h:85
QuestWrapper::wasModified
virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra) override
Definition: QuestWrapper.cpp:113
AssetWrapper::ChangeType
ChangeType
Definition: AssetWrapper.h:32
navar-midane_time.data
data
Definition: navar-midane_time.py:11
AssetWrapper::fillMenu
virtual void fillMenu(QMenu *)
Definition: AssetWrapper.h:63
disinfect.count
int count
Definition: disinfect.py:7
FaceWrapper.h
AssetWrapper::Uses
@ Uses
Definition: AssetWrapper.h:31
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:50
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:62
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:171
quest_step_definition::conditions
quest_condition * conditions
Definition: quest.h:34
quest_condition
Definition: quest.h:19
QuestStepWrapper::conditions
QList< QStringList > conditions
Definition: QuestWrapper.h:31
AssetWrapper::wasModified
virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra)
Definition: AssetWrapper.h:77
npc_dialog.index
int index
Definition: npc_dialog.py:102
QuestStepWrapper::setConditions
void setConditions(const QList< QStringList > &conditions)
Definition: QuestWrapper.cpp:51
QuestWrapper::uses
virtual PossibleUse uses(const AssetWrapper *asset, std::string &) const override
Definition: QuestWrapper.cpp:76
QuestStepWrapper::drop
virtual void drop(const QMimeData *, int) override
Definition: QuestWrapper.cpp:34
AssetWrapper::BeforeChildRemove
@ BeforeChildRemove
Definition: AssetWrapper.h:32
MimeUtils.h
AssetWrapper::markModified
void markModified(ChangeType change, int extra=0)
Definition: AssetWrapper.h:54
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:31
quest_destroy_step
void quest_destroy_step(quest_step_definition *step)
Definition: Quests.cpp:74
QuestWrapper::addStep
void addStep()
Definition: QuestWrapper.cpp:178
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2671
quest_condition::next
struct quest_condition * next
Definition: quest.h:25
connect
Definition: connect.py:1
QuestWrapper::childIndex
virtual int childIndex(AssetWrapper *) override
Definition: QuestWrapper.cpp:103
quest_condition_from_string
int quest_condition_from_string(quest_condition *condition, const char *buffer)
Definition: Quests.cpp:106
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