-
Notifications
You must be signed in to change notification settings - Fork 37
/
quests.cpp
280 lines (258 loc) · 6.97 KB
/
quests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// START A3HEADER
//
// This source file is part of the Atlantis PBM game program.
// Copyright (C) 1995-1999 Geoff Dunbar
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program, in the file license.txt. If not, write
// to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// See the Atlantis Project web page for details:
// http://www.prankster.com/project
//
// END A3HEADER
#include "quests.h"
#include "object.h"
#include <iterator>
#include <memory>
QuestList quests;
Quest::Quest()
{
type = -1;
target = -1;
objective.type = -1;
objective.num = 0;
building = -1;
regionnum = -1;
regionname = "-";
}
Quest::~Quest()
{
rewards.clear();
}
std::string Quest::get_rewards()
{
std::string quest_rewards;
bool first = true;
quest_rewards = "Quest rewards: ";
if (rewards.size() == 0) {
return quest_rewards + "none.";
}
for(auto i: rewards) {
if (!first) quest_rewards += ", ";
first = false;
quest_rewards += ItemString(i.type, i.num);
}
quest_rewards += ".";
return quest_rewards;
}
int QuestList::read_quests(std::istream& f)
{
int count, dests, rewards;
std::shared_ptr<Quest> quest;
AString name;
quests.clear();
f >> count;
if (count < 0)
return 0;
while (count-- > 0) {
quest = std::make_shared<Quest>();
f >> quest->type;
switch (quest->type) {
case Quest::SLAY:
f >> quest->target;
break;
case Quest::HARVEST:
quest->objective.Readin(f);
f >> quest->regionnum;
break;
case Quest::BUILD:
f >> std::ws >> name;
quest->building = LookupObject(&name);
f >> std::ws >> quest->regionname;
break;
case Quest::VISIT:
f >> std::ws >> name;
quest->building = LookupObject(&name);
f >> dests;
while (dests-- > 0) {
f >> std::ws >> name;
quest->destinations.insert(name.Str());
}
break;
case Quest::DEMOLISH:
f >> quest->target;
f >> quest->regionnum;
break;
default:
f >> quest->target;
quest->objective.Readin(f);
f >> std::ws >> name;
quest->building = LookupObject(&name);
f >> quest->regionnum;
f >> std::ws >> quest->regionname;
f >> dests;
while (dests-- > 0) {
f >> std::ws >> name;
quest->destinations.insert(name.Str());
}
break;
}
f >> rewards;
while (rewards-- > 0) {
Item item;
item.Readin(f);
if (-1 == item.type)
return 0;
quest->rewards.push_back(item);
}
quests.push_back(quest);
}
return 1;
}
void QuestList::write_quests(std::ostream& f)
{
f << quests.size() << '\n';
for(auto q: quests) {
f << q->type << '\n';
switch(q->type) {
case Quest::SLAY:
f << q->target << '\n';
break;
case Quest::HARVEST:
q->objective.Writeout(f);
f << q->regionnum << '\n';
break;
case Quest::BUILD:
f << (q->building == -1 ? "NO_OBJECT" : ObjectDefs[q->building].name) << '\n';
f << q->regionname << '\n';
break;
case Quest::VISIT:
f << (q->building == -1 ? "NO_OBJECT" : ObjectDefs[q->building].name) << '\n';
f << q->destinations.size() << '\n';
for (auto dest: q->destinations) {
f << dest << '\n';
}
break;
case Quest::DEMOLISH:
f << q->target << '\n';
f << q->regionnum << '\n';
break;
default:
f << q->target << '\n';
q->objective.Writeout(f);
f << (q->building == -1 ? "NO_OBJECT" : ObjectDefs[q->building].name) << '\n';
f << q->regionnum << '\n';
f << q->regionname << '\n';
f << q->destinations.size() << '\n';
for(auto dest: q->destinations) {
f << dest << '\n';
}
break;
}
f << q->rewards.size() << '\n';
for(auto i: q->rewards) {
i.Writeout(f);
}
}
f << 0 << '\n';
}
std::string QuestList::distribute_rewards(Unit *u, std::shared_ptr<Quest> q)
{
for(auto i: q->rewards) {
u->items.SetNum(i.type, u->items.GetNum(i.type) + i.num);
u->faction->DiscoverItem(i.type, 0, 1);
}
return q->get_rewards();
}
int QuestList::check_kill_target(Unit *u, ItemList& reward, std::string *quest_rewards)
{
for(auto q: quests) {
if (q->type == Quest::SLAY && q->target == u->num) {
// This dead thing was the target of a quest!
for(auto i: q->rewards) {
reward.SetNum(i.type, reward.GetNum(i.type) + i.num);
}
*quest_rewards = q->get_rewards();
erase(q); // this is safe since we immediately return and don't use the iterator again
return 1;
}
}
return 0;
}
int QuestList::check_harvest_target(ARegion *r, int item, int harvested, int max, Unit *u, std::string *quest_rewards)
{
for(auto q: quests) {
if (q->type == Quest::HARVEST && q->regionnum == r->num && q->objective.type == item) {
if (getrandom(max) < harvested) {
*quest_rewards = distribute_rewards(u, q);
erase(q); // this is safe since we immediately return and don't use the iterator again
return 1;
}
}
}
return 0;
}
int QuestList::check_build_target(ARegion *r, int building, Unit *u, std::string *quest_rewards)
{
for(auto q: quests) {
if (q->type == Quest::BUILD && q->building == building && q->regionname == *r->name) {
*quest_rewards = distribute_rewards(u, q);
erase(q); // this is safe since we immediately return and don't use the iterator again
return 1;
}
}
return 0;
}
int QuestList::check_visit_target(ARegion *r, Unit *u, std::string *quest_rewards)
{
std::set<std::string> intersection;
for(auto q: quests) {
if (q->type != Quest::VISIT) continue;
if (!q->destinations.count(r->name->Str())) continue;
forlist(&r->objects) {
Object *o = (Object *) elem;
if (o->type == q->building) {
u->visited.insert(r->name->Str());
intersection.clear();
set_intersection(
q->destinations.begin(),
q->destinations.end(),
u->visited.begin(),
u->visited.end(),
inserter(intersection, intersection.begin()),
std::less<std::string>()
);
if (intersection.size() == q->destinations.size()) {
// This unit has visited the required buildings in all those regions, so they completed a quest
*quest_rewards = distribute_rewards(u, q);
erase(q); // this is safe since we immediately return and don't use the iterator again
return 1;
}
}
}
}
return 0;
}
int QuestList::check_demolish_target(ARegion *r, int building, Unit *u, std::string *quest_rewards)
{
for(auto q: quests) {
if (q->type == Quest::DEMOLISH && q->regionnum == r->num && q->target == building) {
*quest_rewards = distribute_rewards(u, q);
erase(q); // this is safe since we immediately return and don't use the iterator again
return 1;
}
}
return 0;
}