#include "conf.h"
#include "sysdep.h"
#include "structs.h"
#include "utils.h"
#include "db.h"
#include "comm.h"
#include "interpreter.h"
#include "oasis.h"
#include "quest.h"
/* Structs */
struct quest_data *quest;
extern struct descriptor_data *descriptor_list;
/* Functions */
void qedit_setup_existing(struct descriptor_data *d);
void qedit_setup_new(struct descriptor_data *d);
void qedit_disp_menu(struct descriptor_data *d);
void qedit_parse(struct descriptor_data *d, char *arg);
void qedit_update(struct descriptor_data *d);
int count_hash_records(FILE * fl);
/* Global Definitions */
int top_quest = 0;
/* ACMD's */
ACMD(do_qedit);
void save_quests(void)
{
FILE *f;
int i;
struct quest_data *sav_quest;
void strip_cr(char *);
/* Open file */
if(!(f = fopen(QUEST_FILE, "wb"))) {
log("SYSERR: quest file does not exist or is unusable.");
return;
}
/* Interate through quests, saving each */
for (i = 0; i <= top_quest; i++) {
sav_quest = (quest + i);
/* Write Header data to quest file buffer */
fprintf(f, "#%d\n"
"%s%c\n",
sav_quest->number,
sav_quest->name ? sav_quest->name : "Default Quest Name",
STRING_TERMINATOR);
/* Write out the quest task descriptions */
if (sav_quest->task) {
struct quest_task *sav_task;
for (sav_task = sav_quest->task; sav_task; sav_task = sav_task->next) {
strcpy(buf, sav_task->description);
strip_cr(buf);
fprintf(f, "T\n"
"%s~\n", buf);
}
}
fprintf(f, "S\n");
}
}
void boot_quests(FILE *f, int nr) {
int rec_count = 0;
int quest_nr = 0;
char line[256];
struct task_data *new_task;
/* Open file */
if(!(f = fopen(QUEST_FILE, "rb"))) {
log("SYSERR: quest file does not exist or is unusable.");
return;
}
/* Allocate Storage for thequests */
rec_count = count_hash_records(f);
CREATE(quest, struct quest_data, rec_count);
/* Step through file loading quests */
rewind(f);
for (;;) {
if (!get_line(f, line)) {
if (nr == -1) {
log("SYSERR: Quest file is empty");
exit(1);
} }
if (*line == '$')
return;
if (*line == '#') {
sscanf(line, "#%d", &quest_nr);
quest[quest_nr].name = fread_string(f, buf);
quest[quest_nr].number = quest_nr;
top_quest++;
}
if (*line == 'T') {
CREATE(new_task, struct task_data, 1);
new_task->description = fread_string(f, buf);
new_task->next = quest[quest_nr].task;
quest[quest_nr].task = new_task;
break; }
log("Done.");
}
/**************************************************
* Quest editor OLC intergration
**************************************************/
/*
ACMD(do_qedit) {
int quest_num;
struct descriptor_data *d;
/* Parse Arguments */
two_arguments(argument, buf1, buf2);
if (!*buf1) {
send_to_char("Specify a Quest to edit\r\n", ch);
return;
}
/* Request to save changes */
if (str_cmp("save", buf1) == 0) {
save_quests();
send_to_char("Changes to Quests saved\r\n", ch);
return;
}
if (isdigit(*buf1))
quest_num = atoi(buf1);
else {
send_to_char("Watch what you're doing bub!\r\n", ch);
return; }
/*
* Check that quest isn't already being edited.
*/
for (d = descriptor_list; d; d = d->next)
if (STATE(d) == CON_QEDIT)
if (d->olc && OLC_NUM(d) == quest_num) {
sprintf(buf, "That quest is currently being edited by %s.\r\n",PERS(d->character, ch));
send_to_char(buf, ch);
return;
}
d = ch->desc;
/*
* Give descriptor an OLC structure.
*/
if (d->olc) {
mudlog("SYSERR: do_oasis: Player already had olc structure.", BRF, LVL_IMMORT, TRUE);
free(d->olc);
}
CREATE(d->olc, struct oasis_olc_data, 1);
/* Set Olc State */
OLC_NUM(d) = quest_num;
OLC_MODE(d) = QEDIT_MAIN_MENU;
STATE(d) = CON_QEDIT;
/* Setup editor struct */
if (OLC_NUM(d) > top_quest)
qedit_setup_new(d);
else
qedit_setup_existing(d);
}
void qedit_parse(struct descriptor_data *d, char *arg) {
switch (OLC_MODE(d)) {
case QEDIT_MAIN_MENU:
switch (tolower(*arg)) {
case 'q':
if (OLC_VAL(d)) { /* Anything been changed? */
send_to_char("Do you wish to save the changes to the quest? (y/n): ", d->character);
OLC_MODE(d) = QEDIT_CONFIRM_SAVE;
} else
cleanup_olc(d, CLEANUP_ALL);
return;
break;
case '1':
OLC_MODE(d) = QEDIT_NAME;
send_to_char("Name: ", d->character);
break;
case '2':
OLC_MODE(d) = QEDIT_TASK_MENU;
break;
default:
send_to_char("Invalid Option\r\n", d->character);
qedit_disp_menu(d);
return;
break;
}
case QEDIT_CONFIRM_SAVE:
switch(tolower(*arg)) {
case 'y':
qedit_update(d);
sprintf(buf, "OLC: %s edits quest %d", GET_NAME(d->character),
OLC_NUM(d));
mudlog(buf, CMP, MAX(LVL_GRGOD, GET_INVIS_LEV(d->character)),TRUE);
/* fall through */
case 'n':
cleanup_olc(d, CLEANUP_ALL);
return;
default:
send_to_char("Invalid choice!\r\n", d->character);
send_to_char("Do you wish to save the quest? : ",d->character);
return;
break;
}
break;
case QEDIT_NAME:
if (OLC_QUEST(d)->name)
free(OLC_QUEST(d)->name);
strcpy(arg, OLC_QUEST(d)->name);
OLC_VAL(d)++;
break;
}
OLC_MODE(d) = QEDIT_MAIN_MENU;
qedit_disp_menu(d);
}
void qedit_disp_menu(struct descriptor_data *d) {
struct quest_info *tmp_quest = OLC_QUEST(d);
sprintf(buf,
#if defined(CLEAR_SCREEN)
"^[[H^[[J"
#endif
"\r\n Quest Editor V1.0\r\n"
" Quest Number [%d] - Number of Tasks [%d]\r\n"
"1) Quest Name [%s]\r\n"
"2) Task Editor \r\n"
"Q) uit\r\n"
"Enter Choice:",
tmp_quest->number, tmp_quest->num_tasks, tmp_quest->name);
send_to_char(buf, d->character);
OLC_MODE(d) = QEDIT_MAIN_MENU;
}
void qedit_setup_new(struct descriptor_data *d) {
struct quest_info *quest;
/*
* Allocate a scratch quest struct
*/
CREATE(quest, struct quest_info, 1);
/* Setup Default Values */
quest->number = OLC_NUM(d);
strcpy(quest->name, "New Quest");
quest->num_tasks = 1;
strcpy(quest->task_text[0], "NULL TASK");
strcpy(quest->task_text[1], "Quest Text");
OLC_VAL(d) = 0;
OLC_QUEST(d) = quest;
qedit_disp_menu(d);
}
void qedit_setup_existing(struct descriptor_data *d) {
struct quest_info *quest;
int i;
/*
* Allocate a scratch quest struct
*/
CREATE(quest, struct quest_info, 1);
/* Setup Default Values */
quest->number = OLC_NUM(d);
strcpy(quest->name, quest_data[OLC_NUM(d)].name);
quest->num_tasks = quest_data[OLC_NUM(d)].num_tasks;
for (i = 0; i <= quest->num_tasks; i++ ) {
strcpy(quest->task_text[1], quest_data[OLC_NUM(d)].task_text[i] );
}
OLC_VAL(d) = 0;
OLC_QUEST(d) = quest;
qedit_disp_menu(d);
}
void qedit_update(struct descriptor_data *d) {
struct quest_info *sav_quest = OLC_QUEST(d);
OLC_VAL(d) = 0;
OLC_QUEST(d) = sav_quest;
cleanup_olc(d, CLEANUP_ALL);
return;
}
void parse_quest(FILE * fl, int quest_nr) {
struct task_data *new_task;
/* Setup Name and Number */
quest[quest_nr].name = fread_string(fl, buf);
quest[quest_nr].number = quest_nr;
for (;;) {
if (!get_line(fl, line)) {
log("SYSERR: Format error in quests file");
exit(1); }
switch (*line) {
case 'T':
CREATE(new_task, struct task_data, 1);
new_task->
*/