-
Notifications
You must be signed in to change notification settings - Fork 0
/
genqst.c
267 lines (239 loc) · 8.39 KB
/
genqst.c
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
/* ***********************************************************************
* File: genqst.c Part of CircleMUD *
* Version: 2.0 (November 2005) Written for CircleMud CWG / Suntzu *
* Purpose: To provide special quest-related code. *
* Copyright: Kenneth Ray *
* Original Version Details: *
* Copyright 1996 by Harvey Gilpin *
* Copyright 1997-2001 by George Greer ([email protected]) *
************************************************************************ */
#include "conf.h"
#include "sysdep.h"
#include "structs.h"
#include "utils.h"
#include "db.h"
#include "quest.h"
#include "genolc.h"
#include "genzon.h"
void create_world_index(int znum, const char *type);
extern zone_rnum top_of_zone_table;
/*-------------------------------------------------------------------*/
int copy_quest(struct aq_data *to, struct aq_data *from, int free_old_strings)
{
int i;
if (free_old_strings)
free_quest_strings(to);
to->vnum = from->vnum;
to->flags = from->flags;
to->type = from->type;
to->qm = from->qm;
to->target = from->target;
to->prereq = from->prereq;
to->prev_quest = from->prev_quest;
to->next_quest = from->next_quest;
for (i = 0; i < 7; i++){
to->value[i] = from->value[i];
}
to->gold_reward = from->gold_reward;
to->exp_reward = from->exp_reward;
to->obj_reward = from->obj_reward;
to->func = from->func;
return copy_quest_strings(to, from);
}
int copy_quest_strings(struct aq_data *to, struct aq_data *from)
{
if (from == NULL || to == NULL) {
log("SYSERR: GenQST: copy_quest_strings: Null values passed.");
return FALSE;
}
to->name = str_udup(from->name);
to->desc = str_udup(from->desc);
to->info = str_udup(from->info);
to->done = str_udup(from->done);
to->quit = str_udup(from->quit);
return TRUE;
}
void free_quest_strings(struct aq_data *quest)
{
if (quest->name)
free(quest->name);
if (quest->desc)
free(quest->desc);
if (quest->info)
free(quest->info);
if (quest->done)
free(quest->done);
if (quest->quit)
free(quest->quit);
}
void free_quest(struct aq_data *quest)
{
free_quest_strings(quest);
free(quest);
}
/*-------------------------------------------------------------------*/
int add_quest(struct aq_data *nqst)
{
qst_rnum rnum;
zone_rnum rznum = real_zone_by_thing(nqst->vnum);
/* The quest already exists, just update it. */
if ((rnum = real_quest(nqst->vnum)) != NOWHERE) {
copy_quest(&aquest_table[rnum], nqst, TRUE);
} else {
/* increase the number of quest table entries */
total_quests++;
RECREATE(aquest_table, struct aq_data, total_quests );
/* Initialise top quest strings to null */
QST_NAME(total_quests - 1) = NULL;
QST_DESC(total_quests - 1) = NULL;
QST_INFO(total_quests - 1) = NULL;
QST_DONE(total_quests - 1) = NULL;
QST_QUIT(total_quests - 1) = NULL;
/* Now process enties from the top down to see where the new one goes */
for (rnum = total_quests - 1; rnum > 0; rnum--) {
if (nqst->vnum > QST_NUM(rnum - 1))
break; //found the place
aquest_table[rnum] = aquest_table[rnum - 1]; //shift quest up one
}
copy_quest(&aquest_table[rnum], nqst, FALSE);
}
/* Make sure we assign spec procs to the questmaster */
if (mob_index[QST_MASTER(rnum)].func &&
mob_index[QST_MASTER(rnum)].func != questmaster)
QST_FUNC(rnum) = mob_index[QST_MASTER(rnum)].func;
mob_index[QST_MASTER(rnum)].func = questmaster;
/* And make sure we save the updated quest information to disk */
if (rznum != NOWHERE)
add_to_save_list(zone_table[rznum].number, SL_QST);
else
mudlog(BRF, ADMLVL_BUILDER, TRUE,
"SYSERR: GenOLC: Cannot determine quest zone.");
return rnum;
}
/*-------------------------------------------------------------------*/
int delete_quest(qst_rnum rnum)
{
qst_rnum i;
zone_rnum rznum = real_zone_by_thing(QST_NUM(rnum));
mob_rnum qm = QST_MASTER(rnum);
SPECIAL (*tempfunc);
int quests_remaining = 0;
if (rnum >= total_quests)
return FALSE;
log("GenOLC: delete_quest: Deleting quest #%d (%s).",
QST_NUM(rnum), QST_NAME(rnum));
/* make a note of the quest master's secondary spec proc */
tempfunc = QST_FUNC(rnum);
free_quest_strings(&aquest_table[rnum]);
for (i = rnum; i < total_quests - 1; i++) {
aquest_table[i] = aquest_table[i + 1];
}
total_quests--;
RECREATE(aquest_table, struct aq_data, total_quests);
if (rznum != NOWHERE)
add_to_save_list(zone_table[rznum].number, SL_QST);
else
mudlog(BRF, ADMLVL_BUILDER, TRUE,
"SYSERR: GenOLC: Cannot determine quest zone.");
/* does the questmaster mob have any quests left? */
if (qm != NOBODY) {
for (i = 0; i < total_quests; i++) {
if (QST_MASTER(i) == qm)
quests_remaining++;
}
if (quests_remaining == 0)
mob_index[qm].func = tempfunc; // point back to original spec proc
}
return TRUE;
}
/*-------------------------------------------------------------------*/
int save_quests(zone_rnum zone_num)
{
FILE *sf;
char filename[128], oldname[128], quest_flags[MAX_STRING_LENGTH];
char quest_desc[MAX_STRING_LENGTH], quest_info[MAX_STRING_LENGTH];
char quest_done[MAX_STRING_LENGTH], quest_quit[MAX_STRING_LENGTH];
int i, num_quests = 0;
#if CIRCLE_UNSIGNED_INDEX
if (zone_num == NOWHERE || zone_num > top_of_zone_table) {
#else
if (zone_num < 0 || zone_num > top_of_zone_table) {
#endif
log("SYSERR: GenOLC: save_quests: Invalid zone number %d passed! (0-%d)",
zone_num, top_of_zone_table);
return FALSE;
}
log("GenOLC: save_quests: Saving quests in zone #%d (%d-%d).",
zone_table[zone_num].number,
genolc_zone_bottom(zone_num), zone_table[zone_num].top);
snprintf(filename, sizeof(filename), "%s/%d.new",
QST_PREFIX, zone_table[zone_num].number);
if (!(sf = fopen(filename, "w"))) {
log("SYSERR: save_quests: %s", strerror(errno));
return FALSE;
}
for (i = genolc_zone_bottom(zone_num); i <= zone_table[zone_num].top; i++) {
qst_rnum rnum;
if ((rnum = real_quest(i)) != NOTHING) {
/* Copy the text strings and strip off trailing newlines. */
strncpy(quest_desc, QST_DESC(rnum) ? QST_DESC(rnum) : "undefined",
sizeof(quest_desc)-1 );
strncpy(quest_info, QST_INFO(rnum) ? QST_INFO(rnum) : "undefined",
sizeof(quest_info)-1 );
strncpy(quest_done, QST_DONE(rnum) ? QST_DONE(rnum) : "undefined",
sizeof(quest_done)-1 );
strncpy(quest_quit, QST_QUIT(rnum) ? QST_QUIT(rnum) : "undefined",
sizeof(quest_quit)-1 );
strip_cr(quest_desc);
strip_cr(quest_info);
strip_cr(quest_done);
strip_cr(quest_quit);
/* Save the quest details to the file. */
sprintascii(quest_flags, QST_FLAGS(rnum));
fprintf(sf,
"#%d\n"
"%s%c\n"
"%s%c\n"
"%s%c\n"
"%s%c\n"
"%s%c\n"
"%d %d %s %d %d %d %d\n"
"%d %d %d %d %d %d %d\n"
"%d %d %d\n"
"S\n",
QST_NUM(rnum),
QST_NAME(rnum) ? QST_NAME(rnum) : "Untitled", STRING_TERMINATOR,
quest_desc, STRING_TERMINATOR,
quest_info, STRING_TERMINATOR,
quest_done, STRING_TERMINATOR,
quest_quit, STRING_TERMINATOR,
QST_TYPE(rnum),
QST_MASTER(rnum) == NOBODY ? -1 : mob_index[QST_MASTER(rnum)].vnum,
quest_flags,
QST_TARGET(rnum) == NOTHING ? -1 : QST_TARGET(rnum),
QST_PREV(rnum) == NOTHING ? -1 : QST_PREV(rnum),
QST_NEXT(rnum) == NOTHING ? -1 : QST_NEXT(rnum),
QST_PREREQ(rnum) == NOTHING ? -1 : QST_PREREQ(rnum),
QST_POINTS(rnum), QST_PENALTY(rnum), QST_MINLEVEL(rnum),
QST_MAXLEVEL(rnum), QST_TIME(rnum),
QST_RETURNMOB(rnum) == NOBODY ? -1 : QST_RETURNMOB(rnum),
QST_QUANTITY(rnum), QST_GOLD(rnum), QST_EXP(rnum), QST_OBJ(rnum)
);
num_quests++;
}
}
/* Write the final line and close it. */
fprintf(sf, "$~\n");
fclose(sf);
/* Old file we're replacing. */
snprintf(oldname, sizeof(oldname), "%s/%d.qst",
QST_PREFIX, zone_table[zone_num].number);
remove(oldname);
rename(filename, oldname);
/* Do we need to update the index file? */
if (num_quests > 0)
create_world_index(zone_table[zone_num].number, "qst");
if (in_save_list(zone_table[zone_num].number, SL_QST))
remove_from_save_list(zone_table[zone_num].number, SL_QST);
return TRUE;
}