Debian Codegraph @

server/quest.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Quest-related low-level mechanisms.
17 *
18 * You should only need to call the public functions, all that are not static.
19 *
20 * Data is loaded on a need-only basis - when a player quest state is queried
21 * or modified, data is read. Also, free_quest() can be called to release
22 * memory without preventing quest operations after.
23 *
24 * Write is done for each player whenever the state changes, to ensure data
25 * integrity.
26 */
27
28 #include "global.h"
29
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "output_file.h"
35 #include "sproto.h"
36
37 /* Media tags for information messages prefix. */
38 #define TAG_START "[color=#aa55ff]"
39 #define TAG_END "[/color]"
40
41 /** Quest status that indicates a quest was completed and may be restarted. */
42 #define QC_CAN_RESTART -1
43
44 /** Information about a quest for a player. */
45 typedef struct quest_state {
46 sstring code; /**< Quest internal code. */
47 int state; /**< State for the player. */
48 int was_completed; /**< Whether the quest was completed once or not, indepandently of the state. */
49 int is_complete; /**< Whether the quest is complete in the current playthrough */
50 int sent_to_client; /**< Whether this state was sent to the client or not. */
51 struct quest_state *next; /**< Next quest on the list. */
52 } quest_state;
53
54 /** Information about a player. */
55 typedef struct quest_player {
56 sstring player_name; /**< Player's name. */
57 struct quest_state *quests; /**< Quests done or in progress. */
58 struct quest_player *next; /**< Next player on the list. */
59 } quest_player;
60
61 /** Player quest state. */
62 static quest_player *player_states = NULL;
63
64 /** One condition to automatically move to a quest step. */
65 typedef struct quest_condition {
66 sstring quest_code; /**< The quest that triggers the condition */
67 int minstep; /**< The earliest step in the quest that triggers the condition,
68 -1 means finished, 0 means not started */
69 int maxstep; /**< The latest step that triggers the condition, to match,
70 the stages must be between minstep and maxstep */
71 struct quest_condition *next; /**< The next condition to check */
72 } quest_condition;
73
74 /** One step of a quest. */
75 typedef struct quest_step_definition {
76 int step; /**< Step identifier. */
77 sstring step_description; /**< Step description to show player. */
78 int is_completion_step:1; /**< Whether this step completes the quest (1) or not (0) */
79 struct quest_step_definition *next; /**< Next step. */
80 quest_condition *conditions; /**< The conditions that must be satisfied to trigger the step */
81 } quest_step_definition;
82
83 /** Definition of an in-game quest. */
84 typedef struct quest_definition {
85 sstring quest_code; /**< Quest internal code. */
86 sstring quest_title; /**< Quest title for player. */
87 sstring quest_description; /**< Quest longer description. */
88 int quest_restart; /**< If non zero, can be restarted. */
89 const Face *face; /**< Face associated with this quest. */
90 uint32_t client_code; /**< The code used to communicate with the client, merely a unique index. */
91 quest_step_definition *steps; /**< Quest steps. */
92 struct quest_definition *parent;/**< Parent for this quest, NULL if it is a 'top-level' quest */
93 struct quest_definition *next; /**< Next quest in the definition list. */
94 } quest_definition;
95
96 static int quests_loaded = 0; /**< Number of quests loaded. If zero, quests not yet loaded. */
97 static quest_definition *quests = NULL; /**< All known quests. */
98
99 /**
100 * Allocate a quest_step_definition, will call fatal() if out of memory.
101 * @return new structure.
102 */
103 static quest_step_definition *quest_create_step(void) {
104 quest_step_definition *step = calloc(1, sizeof(quest_step_definition));
105 if (!step)
106 fatal(OUT_OF_MEMORY);
107 return step;
108 }
109
110 /**
111 * Allocate a quest_condition, will call fatal() if out of memory.
112 * @return new structure.
113 */
114 static quest_condition *quest_create_condition(void) {
115 quest_condition *cond = calloc(1, sizeof(quest_condition));
116 if (!cond)
117 fatal(OUT_OF_MEMORY);
118 return cond;
119 }
120
121
122 /**
123 * Allocate a quest_definition, will call fatal() if out of memory.
124 * @return new structure.
125 */
126 static quest_definition *quest_create_definition(void) {
127 quest_definition *quest = calloc(1, sizeof(quest_definition));
128 if (!quest)
129 fatal(OUT_OF_MEMORY);
130 return quest;
131 }
132
133 /**
134 * Find a quest from its code. This is called by quest_get and also
135 * used in the quest loading code
136 * @param code quest to search.
137 * @return quest, or NULL if no such quest.
138 */
139 static quest_definition *quest_get_by_code(sstring code) {
140 quest_definition *quest;
141
142 quest = quests;
143 while (quest) {
144 if (quest->quest_code == code)
145 return quest;
146
147 quest = quest->next;
148 }
149 return NULL;
150 }
151
152 /**
153 * @defgroup QUESTFILE_xxx Quest file parsing state.
154 *
155 * This is the parsing state when loading a file through load_quests_from_file().
156 */
157 /*@{*/
158 #define QUESTFILE_NEXTQUEST 0 /**< Waiting for next quest definition. */
159 #define QUESTFILE_QUEST 1 /**< In a quest definition. */
160 #define QUESTFILE_QUESTDESC 2 /**< In a quest description. */
161 #define QUESTFILE_STEP 3 /**< In a quest step. */
162 #define QUESTFILE_STEPDESC 4 /**< In a quest step description. */
163 #define QUESTFILE_STEPCOND 5 /**< In a quest step conditions. */
164 /*@}*/
165
166 /**
167 * Loads all of the quests which are found in the given file, any global states
168 * for quest loading are passed down into this function, but not back up again.
169 * @param filename filename to load quests from.
170 * @return number of quests loaded from file, negative value if there was an error.
171 */
172 static int load_quests_from_file(const char *filename) {
173 int i, in = QUESTFILE_NEXTQUEST, condition_parsed;
174 int minstep, maxstep;
175 char namedquest[MAX_BUF];
176 quest_definition *quest = NULL;
177 quest_condition *cond = NULL;
178 char includefile[MAX_BUF];
179 sstring questname;
180 quest_step_definition *step = NULL;
181 char final[MAX_BUF], read[MAX_BUF];
182 FILE *file;
183 StringBuffer *buf=NULL;
184
185 int loaded_quests =0, found =0;
186 snprintf(final, sizeof(final), "%s/%s/%s", settings.datadir, settings.mapdir, filename);
187 file = fopen(final, "r");
188 if (!file) {
189 LOG(llevError, "Can't open %s for reading quests\n", filename);
190 return -1;
191 }
192
193 while (fgets(read, sizeof(read), file) != NULL) {
194 if (in == QUESTFILE_STEPCOND) {
195 if (strcmp(read, "end_setwhen\n") == 0) {
196 in = QUESTFILE_STEP;
197 continue;
198 }
199 /* we are reading in a list of conditions for the 'setwhen' block for a quest step
200 * There will be one entry per line, containing the quest, and the steps that it applies to.
201 * This may be expressed as one of the following
202 * questcode 20 (the quest questcode must be at step 20)
203 * questcode <=20 (the quest questcode must not be beyond step 20)
204 * questcode 10-20 (the quest questcode must be between steps 10 and 20)
205 * questcode finished (the quest questcode must have been completed)
206 */
207
208 minstep = 0;
209 maxstep = 0;
210 condition_parsed = 0;
211 namedquest[0]='\0';
212 if (sscanf(read, "%s %d-%d\n", namedquest, &minstep, &maxstep)!=3) {
213 if (sscanf(read, "%s <=%d\n", namedquest, &maxstep)== 2) {
214 minstep=0;
215 condition_parsed =1;
216 } else if (sscanf(read, "%s %d\n", namedquest, &minstep)==2) {
217 maxstep = minstep;
218 condition_parsed =1;
219 } else if (strstr(read, "finished")) {
220 if (sscanf(read, "%s finished\n", namedquest)==1) {
221 minstep = maxstep = -1;
222 condition_parsed =1;
223 }
224 }
225 } else
226 condition_parsed =1;
227 if (!condition_parsed) {
228 LOG(llevError, "Invalid line '%s' in setwhen block for quest %s=n", read, quest->quest_code);
229 continue;
230 }
231
232 cond = quest_create_condition();
233 cond->minstep = minstep;
234 cond->maxstep = maxstep;
235 cond->quest_code = add_string(namedquest);
236 cond->next = step->conditions;
237 step->conditions = cond;
238 LOG(llevDebug, "condition added for step %d of quest %s, looking for quest %s between steps %d and %d\n",
239 step->step, quest->quest_code, cond->quest_code, cond->minstep, cond->maxstep);
240 continue;
241 }
242 if (in == QUESTFILE_STEPDESC) {
243 if (strcmp(read, "end_description\n") == 0) {
244 char *message;
245
246 in = QUESTFILE_STEP;
247
248 message = stringbuffer_finish(buf);
249 buf = NULL;
250
251 step->step_description = add_string(message);
252 free(message);
253
254 continue;
255 }
256
257 stringbuffer_append_string(buf, read);
258 continue;
259 }
260
261 if (in == QUESTFILE_STEP) {
262 if (strcmp(read, "end_step\n") == 0) {
263 step = NULL;
264 in = QUESTFILE_QUEST;
265 continue;
266 }
267 if (strcmp(read, "finishes_quest\n") == 0) {
268 step->is_completion_step=1;
269 continue;
270 }
271 if (strcmp(read, "description\n") == 0) {
272 buf = stringbuffer_new();
273 in = QUESTFILE_STEPDESC;
274 continue;
275 }
276 if (strcmp(read, "setwhen\n") == 0) {
277 in = QUESTFILE_STEPCOND;
278 continue;
279 }
280 LOG(llevError, "quests: invalid line %s in definition of quest %s in file %s!\n",
281 read, quest->quest_code, filename);
282 continue;
283 }
284
285 if (in == QUESTFILE_QUESTDESC) {
286 if (strcmp(read, "end_description\n") == 0) {
287 char *message;
288
289 in = QUESTFILE_QUEST;
290
291 message = stringbuffer_finish(buf);
292 buf = NULL;
293
294 quest->quest_description = add_string(message);
295 free(message);
296
297 continue;
298 }
299 stringbuffer_append_string(buf, read);
300 continue;
301 }
302
303 if (in == QUESTFILE_QUEST) {
304 if (strcmp(read, "end_quest\n") == 0) {
305 quest = NULL;
306 in = QUESTFILE_NEXTQUEST;
307 continue;
308 }
309
310 if (strcmp(read, "description\n") == 0) {
311 in = QUESTFILE_QUESTDESC;
312 buf = stringbuffer_new();
313 continue;
314 }
315
316 if (strncmp(read, "title ", 6) == 0) {
317 read[strlen(read) - 1] = '\0';
318 quest->quest_title = add_string(read + 6);
319 continue;
320 }
321
322 if (sscanf(read, "step %d\n", &i)) {
323 step = quest_create_step();
324 step->step = i;
325 step->next = quest->steps;
326 quest->steps = step;
327 in = QUESTFILE_STEP;
328 continue;
329 }
330
331 if (sscanf(read, "restart %d\n", &i)) {
332 quest->quest_restart = i;
333 continue;
334 }
335 if (strncmp(read, "parent ", 7) == 0) {
336 read[strlen(read) - 1] = '\0';
337 questname = add_string(read + 7);
338 if (!quest_get_by_code(questname)) {
339 LOG(llevError, "Quest %s lists %s, as a parent, but this hasn't been defined\n", quest->quest_code, questname);
340 } else {
341 quest->parent = quest_get_by_code(questname);
342 }
343 free_string(questname);
344 continue;
345 }
346
347 if (strncmp(read, "face ", 5) == 0) {
348 const Face *face;
349 read[strlen(read) - 1] = '\0';
350 face = find_face(read + 5, NULL);
351 if (face == NULL) {
352 LOG(llevError, "Quest %s has invalid face %s.\n", quest->quest_code, read + 5);
353 } else {
354 quest->face = face;
355 }
356 continue;
357 }
358 }
359
360 if (read[0] == '#')
361 continue;
362
363 if (strncmp(read, "quest ", 6) == 0) {
364 quest = quest_create_definition();
365 read[strlen(read) - 1] = '\0';
366 quest->quest_code = add_string(read + 6);
367 if (quest_get_by_code(quest->quest_code)) {
368 LOG(llevError, "Quest %s is listed in file %s, but this quest has already been defined\n", quest->quest_code, filename);
369 }
370 /* Set a default face, which will be overwritten if a face is defined. */
371 quest->face = find_face("quest_generic.111", 0);
372 quest->next = quests;
373 if (quests != NULL)
374 quest->client_code = quests->client_code + 1;
375 else
376 quest->client_code = 1;
377 quests = quest;
378 in = QUESTFILE_QUEST;
379 loaded_quests++;
380 continue;
381 }
382 if (sscanf(read, "include %s\n", includefile)) {
383 char inc_path[HUGE_BUF];
384 path_combine_and_normalize(filename, includefile, inc_path, sizeof(inc_path));
385 found = load_quests_from_file(inc_path);
386 if (found >=0) {
387 LOG(llevDebug, "loaded %d quests from file %s\n", found, inc_path);
388 loaded_quests += found;
389 } else {
390 LOG(llevError, "Failed to load quests from file %s\n", inc_path);
391 }
392 continue;
393 }
394
395 if (strcmp(read, "\n") == 0)
396 continue;
397
398 LOG(llevError, "quest: invalid file format for %s, I don't know what to do with the line %s\n", final, read);
399 }
400
401 fclose(file);
402
403 if (in != 0) {
404 LOG(llevError, "quest: quest definition file %s read in, ends with state %d\n", final, in);
405
406 /* The buffer may not have been freed. */
407 if (buf != NULL) {
408 stringbuffer_finish(buf);
409 }
410 }
411
412 return loaded_quests;
413 }
414
415 /** Load all quest definitions. Can be called multiple times, will be ignored. */
416 static void quest_load_definitions(void) {
417 if (quests_loaded)
418 return;
419 int found = load_quests_from_file("world.quests");
420 if (found >= 0) {
421 LOG(llevInfo, "%d quests found.\n", found);
422 quests_loaded = found;
423 } else {
424 LOG(llevError, "Quest Loading Failed\n");
425 }
426 }
427
428 /**
429 * Get a step for the specified quest.
430 * @param quest quest to consider.
431 * @param step step to find.
432 * @return step, or NULL if no such step in which case a llevError is emitted.
433 */
434 static quest_step_definition *quest_get_step(quest_definition *quest, int step) {
435 quest_step_definition *qsd = quest->steps;
436
437 while (qsd) {
438 if (qsd->step == step)
439 return qsd;
440
441 qsd = qsd->next;
442 }
443
444 LOG(llevError, "quest %s has no required step %d\n", quest->quest_code, step);
445 return NULL;
446 }
447
448 /**
449 * Find a quest from its code.
450 * @param code quest to search.
451 * @return quest, , or NULL if no such quest in which case a llevError is emitted.
452 */
453 static quest_definition *quest_get(sstring code) {
454 quest_definition *quest;
455
456 quest_load_definitions();
457
458 quest = quest_get_by_code(code);
459 if (!quest) {
460 LOG(llevError, "quest %s required but not found!\n", code);
461 return NULL;
462 }
463 return quest;
464 }
465
466 /**
467 * Return a new quest_state*, calling fatal() if memory shortage.
468 * @return new value, never NULL.
469 */
470 static quest_state *get_new_quest_state(void) {
471 quest_state *qs = calloc(1, sizeof(quest_state));
472 if (qs == NULL)
473 fatal(OUT_OF_MEMORY);
474 return qs;
475 }
476
477 /**
478 * Read quest-data information for a player.
479 * @param pq player to read data for.
480 */
481 static void quest_read_player_data(quest_player *pq) {
482 FILE *file;
483 char final[MAX_BUF], read[MAX_BUF], data[MAX_BUF];
484 quest_state *qs = NULL, *prev = NULL;
485 int warned = 0, state;
486 quest_definition *quest = NULL;
487
488 /* needed, so we can check ending steps. */
489 quest_load_definitions();
490
491 snprintf(final, sizeof(final), "%s/%s/%s/%s.quest", settings.localdir, settings.playerdir, pq->player_name, pq->player_name);
492
493 file = fopen(final, "r");
494 if (!file) {
495 /* no quest yet, no big deal */
496 return;
497 }
498
499 while (fgets(read, sizeof(read), file) != NULL) {
500 if (sscanf(read, "quest %s\n", data)) {
501 qs = get_new_quest_state();
502 qs->code = add_string(data);
503 quest = quest_get_by_code(qs->code);
504 continue;
505 }
506
507 if (!qs) {
508 if (!warned)
509 LOG(llevError, "quest: invalid file format for %s\n", final);
510 warned = 1;
511 continue;
512 }
513
514 if (sscanf(read, "state %d\n", &state)) {
515 qs->state = state;
516 if (quest != NULL && state != -1) {
517 quest_step_definition *step = quest_get_step(quest, state);
518 if (step == NULL) {
519 LOG(llevError, "invalid quest step %d for %s in %s\n", state, quest->quest_code, final);
520 }
521 else if (step->is_completion_step)
522 qs->is_complete = 1;
523 }
524 continue;
525 }
526 if (strcmp(read, "end_quest\n") == 0) {
527 if (quest == NULL) {
528 LOG(llevDebug, "Unknown quest %s in quest file %s\n", qs->code, final);
529 free(qs);
530 } else {
531 if (prev == NULL) {
532 pq->quests = qs;
533 } else {
534 prev->next = qs;
535 }
536 prev = qs;
537 }
538 qs = NULL;
539 continue;
540 }
541 if (sscanf(read, "completed %d\n", &state)) {
542 qs->was_completed = state ? 1 : 0;
543 continue;
544 }
545
546 LOG(llevError, "quest: invalid line in %s: %s\n", final, read);
547 }
548
549 if (qs)
550 LOG(llevError, "quest: missing end_quest in %s\n", final);
551
552 fclose(file);
553 }
554
555 /**
556 * Write quest-data information for a player.
557 * @param pq player to write data for.
558 */
559 static void quest_write_player_data(const quest_player *pq) {
560 FILE *file;
561 OutputFile of;
562 char fname[MAX_BUF];
563 const quest_state *state;
564
565 snprintf(fname, sizeof(fname), "%s/%s/%s/%s.quest", settings.localdir, settings.playerdir, pq->player_name, pq->player_name);
566
567 file = of_open(&of, fname);
568 if (file == NULL) {
569 draw_ext_info(NDI_UNIQUE | NDI_ALL_DMS, 0, NULL, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE, "File write error on server!");
570 return;
571 }
572
573 state = pq->quests;
574
575 while (state) {
576 fprintf(file, "quest %s\n", state->code);
577 fprintf(file, "state %d\n", state->state);
578 fprintf(file, "completed %d\n", state->was_completed);
579 fprintf(file, "end_quest\n");
580 state = state->next;
581 }
582
583 if (!of_close(&of)) {
584 draw_ext_info(NDI_UNIQUE | NDI_ALL_DMS, 0, NULL, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE, "File write error on server!");
585 return;
586 }
587 /** @todo rename/backup, stuff like that */
588 }
589
590 /**
591 * Get the state of a quest for a player, not creating if not existing yet.
592 * @param pq player to get state for.
593 * @param name quest to get state of.
594 * @return NULL if quest isn't started yet for this player, else quest's state information.
595 */
596 static quest_state *get_state(quest_player *pq, sstring name) {
597 quest_state *qs = pq->quests;
598
599 while (qs) {
600 if (qs->code == name)
601 return qs;
602 qs = qs->next;
603 }
604
605 return NULL;
606 }
607
608 /**
609 * Get the state of a quest for a player, creating it if not existing yet.
610 * @param pq player to get state for.
611 * @param name quest to get state of.
612 * @return quest's state information, newly created if it wasn't done yet.
613 */
614 static quest_state *get_or_create_state(quest_player *pq, sstring name) {
615 quest_state *qs = get_state(pq, name);
616
617 if (!qs) {
618 qs = calloc(1, sizeof(quest_state));
619 if (!qs)
620 fatal(OUT_OF_MEMORY);
621 qs->code = add_refcount(name);
622 if (pq->quests != NULL) {
623 quest_state *last;
624 for (last = pq->quests ; last->next != NULL; last = last->next)
625 ;
626 last->next = qs;
627 } else {
628 pq->quests = qs;
629 }
630 }
631
632 return qs;
633 }
634
635 /**
636 * Get quest status for a player, not creating it if it doesn't exist.
637 * @param pl player to get information of.
638 * @return quest status, NULL if not loaded/found yet.
639 */
640 static quest_player *get_quest(player *pl) {
641 quest_player *pq = player_states;
642
643 while (pq) {
644 if (pq->player_name == pl->ob->name)
645 return pq;
646 pq = pq->next;
647 }
648
649 return NULL;
650 }
651
652 /**
653 * Get quest status for a player, creating it if it doesn't exist yet.
654 * Calls fatal() if memory allocation error.
655 * @param pl player to get information of.
656 * @return quest status, never NULL.
657 */
658 static quest_player *get_or_create_quest(player *pl) {
659 quest_player *pq = get_quest(pl);
660
661 if (!pq) {
662 pq = calloc(1, sizeof(quest_player));
663 if (!pq)
664 fatal(OUT_OF_MEMORY);
665 pq->player_name = add_refcount(pl->ob->name);
666 pq->next = player_states;
667 player_states = pq;
668 quest_read_player_data(pq);
669 }
670
671 return pq;
672 }
673
674 /* quest_set_state can call itself through the function update_quests, so it needs to be declared here */
675 static void quest_set_state(player* dm, player *pl, sstring quest_code, int state, int started);
676
677 /**
678 * Checks whether the conditions for a given step are met.
679 * @param condition the linked list of conditions to check.
680 * @param pl the player to evaluate conditions for.
681 * @return 1 if the conditions match, 0 if they don't.
682 */
683 static int evaluate_quest_conditions(const quest_condition *condition, player *pl) {
684 const quest_condition *cond;
685 int current_step;
686
687 if (!condition)
688 return 0;
689 cond = condition;
690 while (cond) {
691 current_step = quest_get_player_state(pl, cond->quest_code);
692 if (cond->minstep < 0 && cond->maxstep < 0) {
693 /* we are checking for the quest to have been completed. */
694 if (!quest_was_completed(pl, cond->quest_code))
695 return 0;
696 } else {
697 if (current_step < cond->minstep || current_step > cond->maxstep)
698 return 0;
699 }
700 cond = cond->next;
701 }
702 return 1;
703 }
704
705 /**
706 * Look through all of the quests for the given player, and see if any need to be updated.
707 * @param pl
708 */
709 static void update_quests(player *pl) {
710 const quest_definition *quest;
711 const quest_step_definition *step;
712
713 /* we are going to check the conditions for every step, and then find the highest
714 * numbered step for which all conditions match, this will then be updated if that
715 * is a later stage than the player is at currently.
716 */
717 int new_step, current_step;
718 quest = quests;
719 while (quest) {
720 new_step=0;
721 step = quest->steps;
722 while (step) {
723 if (step->conditions)
724 if (evaluate_quest_conditions(step->conditions, pl)) {
725 new_step=new_step<step->step?step->step:new_step;
726 }
727 step = step->next;
728 }
729 if (new_step > 0) {
730 current_step = quest_get_player_state(pl, quest->quest_code);
731 if (new_step > current_step) {
732 quest_set_state(NULL, pl, quest->quest_code, new_step, 0);
733 }
734 }
735 quest = quest->next;
736 }
737 }
738
739 /**
740 * Set the state of a quest for a player.
741 * @param dm if NULL then the player is actually playing, else a DM is changing the quest's state manually.
742 * @param pl player to set the state for.
743 * @param quest_code quest internal code.
744 * @param state new state for the quest, must be greater than 0 else forced to 100 and a warning is emitted.
745 * @param started if 1, quest must have been started first or a warning is emitted, else it doesn't matter.
746 */
747 static void quest_set_state(player* dm, player *pl, sstring quest_code, int state, int started) {
748 quest_player *pq = get_or_create_quest(pl);
749 quest_state *qs = get_or_create_state(pq, quest_code);
750 quest_definition *quest = quest_get(quest_code);
751 quest_step_definition *step;
752
753 if (!quest) {
754 if (dm) {
755 draw_ext_info_format(NDI_UNIQUE, 0, dm->ob, MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND_FAILURE, "Unknown quest %s!", quest_code);
756 } else {
757 LOG(llevError, "quest: asking for set_state of unknown quest %s!\n", quest_code);
758 }
759 return;
760 }
761
762 if (state <= 0) {
763 if (!dm) {
764 LOG(llevDebug, "quest_set_player_state: warning: called with invalid state %d for quest %s, player %s\n", state, pl->ob->name, quest_code);
765 }
766 state = 100;
767 }
768
769 step = quest_get_step(quest, state);
770 if (!step) {
771 if (dm) {
772 draw_ext_info_format(NDI_UNIQUE, 0, dm->ob, MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND_FAILURE, "Couldn't find state definition %d for quest %s", state, quest_code);
773 } else {
774 LOG(llevError, "quest_set_player_state: couldn't find state definition %d for quest %s, player %s\n", state, quest_code, pl->ob->name);
775 }
776 return;
777 }
778
779 if (started && qs->state == 0) {
780 if (!dm) {
781 LOG(llevDebug, "quest_set_player_state: warning: called for player %s not having started quest %s\n", pl->ob->name, quest_code);
782 }
783 }
784
785 qs->state = state;
786 if (step->is_completion_step) {
787 /* don't send an update note if the quest was already completed, this is just to show the outcome afterwards. */
788 if (!qs->is_complete)
789 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, "Quest %s completed.", quest->quest_title);
790 qs->was_completed = 1;
791 if (quest->quest_restart)
792 qs->state = QC_CAN_RESTART;
793 else
794 qs->is_complete =1;
795
796 } else {
797 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, "New objective for the quest '%s':", quest->quest_title);
798 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, step->step_description);
799 }
800
801 if (pl->socket.notifications > 0) {
802 SockList sl;
803 SockList_Init(&sl);
804
805 if (qs->sent_to_client) {
806 SockList_AddString(&sl, "updquest ");
807 } else {
808 SockList_AddString(&sl, "addquest ");
809 }
810
811 SockList_AddInt(&sl, quest->client_code);
812 if (qs->sent_to_client == 0) {
813 SockList_AddLen16Data(&sl, quest->quest_title, strlen(quest->quest_title));
814 if (quest->face && !(pl->socket.faces_sent[quest->face->number]&NS_FACESENT_FACE))
815 esrv_send_face(&pl->socket, quest->face, 0);
816 SockList_AddInt(&sl, quest->face ? quest->face->number : 0);
817 SockList_AddChar(&sl, quest->quest_restart ? 1 : 0);
818 SockList_AddInt(&sl, quest->parent ? quest->parent->client_code : 0);
819 }
820
821 SockList_AddChar(&sl, (step == NULL || step->is_completion_step) ? 1 : 0);
822 assert(step != NULL);
823 SockList_AddLen16Data(&sl, step->step_description,
824 strlen(step->step_description));
825
826 Send_With_Handling(&pl->socket, &sl);
827 SockList_Term(&sl);
828
829 qs->sent_to_client = 1;
830 }
831
832 if (pl->has_directory)
833 quest_write_player_data(pq);
834 update_quests(pl);
835 LOG(llevDebug, "quest_set_player_state %s %s %d\n", pl->ob->name, quest_code, state);
836
837 }
838
839 /**
840 * Utility function to display a quest list. Will show a header before the list if not empty.
841 * @param pl player to display list of quests.
842 * @param pq quests to display.
843 * @param showall if 0, only shows quests in progress and a summary of completed quests, else shows all quests.
844 * @param name either 'You' or the player's name, if pl is a DM asking about another player.
845 */
846 static void quest_display(player *pl, quest_player *pq, int showall, const char* name) {
847 quest_state *state;
848 quest_definition *quest;
849 const char *restart;
850 int completed_count = 0, restart_count = 0, total_count = 0, current_count = 0;
851
852 state = pq->quests;
853 while (state) {
854 quest = quest_get(state->code);
855 if (quest->parent == NULL) {
856 total_count++;
857 /* count up the number of completed quests first */
858 if (state->state == QC_CAN_RESTART) {
859 restart_count++;
860 completed_count++;
861 } else if(state->is_complete) {
862 completed_count++;
863 }
864 }
865 state = state->next;
866 }
867 if (completed_count > 0) {
868 if (!showall) {
869 if (restart_count > 0)
870 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS,
871 "%s completed %d out of %d quests, of which %d may be restarted", name, completed_count, quests_loaded, restart_count);
872 else
873 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS,
874 "%s completed %d quests", name, completed_count);
875 current_count = completed_count;
876 } else {
877 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS,
878 "%s completed the following quests:", name);
879 state = pq->quests;
880 while (state) {
881 quest = quest_get(state->code);
882 if (quest->parent == NULL) {
883 if (state->state == QC_CAN_RESTART || state->is_complete) {
884
885 restart = state->state == QC_CAN_RESTART?" (can be replayed)":"";
886 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS,
887 "(%3d) %s%s", ++current_count, quest->quest_title, restart);
888 }
889 }
890 state = state->next;
891 }
892 }
893 }
894 if (total_count > completed_count) {
895 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS,
896 "%s started the following quests:", name);
897 state = pq->quests;
898 while (state) {
899 quest = quest_get(state->code);
900 if (quest->parent == NULL) {
901 if (state->state != QC_CAN_RESTART && state->is_complete==0) {
902 quest = quest_get(state->code);
903 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS,
904 "(%3d) %s", ++current_count, quest->quest_title);
905 }
906 }
907 state = state->next;
908 }
909 }
910 }
911
912 /**
913 * Display current and completed player quests.
914 * @param pl player to display to.
915 * @param who player to display information for.
916 * @param showall - whether to show all of the quests in full, just summary information for the completed ones
917 * @param name either 'You' or the player's name, if pl is a DM asking about another player.
918 */
919 static void quest_list(player *pl, player *who, int showall, const char* name) {
920 quest_player *pq;
921
922 /* ensure we load data if not loaded yet */
923 pq = get_or_create_quest(who);
924 if (!pq->quests) {
925 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, "%s didn't start any quest.", name);
926 return;
927 }
928
929 quest_display(pl, pq, showall, name);
930 }
931
932 /**
933 * Quest command help.
934 * @param pl player to display help for.
935 */
936 static void quest_help(player *pl) {
937 if (QUERY_FLAG(pl->ob, FLAG_WIZ)) {
938 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, "DM Quest commands:");
939 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, " - (player name) list: displays quests the player is currently attempting, add 'all' to show completed quests also");
940 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, " - (player name) info: displays information about the specified (by number) quest");
941 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, " - (player name) set (quest code) (state): set the state of the specified quest");
942 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, "Note: (player name) may be a partial name as long as it isn't ambiguous. The player must be online.");
943 } else {
944 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, "Quest commands:");
945 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, " - list: displays quests you are currently attempting add 'all' to show completed quests also");
946 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, " - info: displays information about the specified (by number) quest");
947 }
948 }
949
950 /**
951 * returns the quest state which corresponds to a certain number for the given player.
952 * @param pl player asking for details.
953 * @param number quest number.
954 * @return quest state corresponding to the number provided, NULL if there is no such quest state.
955 */
956
957 static quest_state *get_quest_by_number(player *pl, int number) {
958 quest_state *state;
959 quest_player *pq = get_or_create_quest(pl);
960 int questnum = 0;
961
962 if (number <= 0 || !pq) {
963 return NULL;
964 }
965 /* count through completed quests first */
966 state = pq->quests;
967 while (state) {
968 /* count up the number of completed quests first */
969 if (!(quest_get(state->code)->parent) && (state->state == QC_CAN_RESTART || state->is_complete))
970 if (++questnum == number) return state;
971 state = state->next;
972 }
973 /* then active ones */
974 state = pq->quests;
975 while (state) {
976 /* count up the number of completed quests first */
977 if (!(quest_get(state->code)->parent) && state->state != QC_CAN_RESTART && state->is_complete ==0)
978 if (++questnum == number) return state;
979 state = state->next;
980 }
981 /* Ok, we didn't find our quest, return NULL*/
982 return NULL;
983 }
984
985 /**
986 * Give details about a quest.
987 * @param pl player to give quest details to.
988 * @param who player to give quest details of.
989 * @param qs quest_state to give details about
990 * @param level The level of recursion for the quest info that's being provided
991 */
992 static void quest_info(player *pl, player* who, quest_state *qs, int level) {
993 quest_definition *quest, *child;
994 quest_state *state;
995 quest_player *pq = get_or_create_quest(who);
996 quest_step_definition *step;
997 const char *prefix;
998
999 if (!qs) {
1000 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, "Invalid quest number");
1001 return;
1002 }
1003 quest = quest_get(qs->code);
1004 if (!quest) {
1005 /* already warned by quest_get */
1006 draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, "Quest: (internal error)");
1007 return;
1008 }
1009
1010 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, TAG_START "Quest:" TAG_END " %s", quest->quest_title);
1011 if (QUERY_FLAG(pl->ob, FLAG_WIZ)) {
1012 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, TAG_START "Code:" TAG_END " %s", quest->quest_code);
1013 for (step = quest->steps; step != NULL; step = step->next) {
1014 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, " " TAG_START "Step:" TAG_END " %d (%s)", step->step, step->step_description);
1015 }
1016 }
1017 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, TAG_START "Description:" TAG_END " %s", quest->quest_description);
1018
1019 step = quest_get_step(quest, qs->state);
1020 if (qs->state == QC_CAN_RESTART || qs->is_complete) {
1021 const char *restart = "";
1022 if (quest->quest_restart)
1023 restart = " (can be replayed)";
1024 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, TAG_START "This quest has been completed%s.[/color]", restart);
1025 }
1026 prefix = "";
1027 if (qs->state != QC_CAN_RESTART) {
1028 /* ie, if we are in progress or completed for a non-restartable quest */
1029 if (!step) {
1030 /* already warned by quest_get_step */
1031 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, " \nOutcome: (invalid quest)");
1032 return;
1033 }
1034 if (level > 0) {
1035 prefix = " * ";
1036 } else if (qs->is_complete)
1037 prefix = "Outcome";
1038 else
1039 prefix = "Current Status";
1040 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, " \n" TAG_START "%s:" TAG_END " %s", prefix, step->step_description);
1041 }
1042
1043 /* ok, now check all of the player's other quests for any children, and print those in order */
1044 state = pq->quests;
1045 while (state) {
1046 child = quest_get(state->code);
1047 if (child->parent == quest)
1048 quest_info(pl, who, state, level+1);
1049 state = state->next;
1050 }
1051 return;
1052 }
1053
1054 /**
1055 * Free quests structures.
1056 * @param pq what to free.
1057 */
1058 static void free_state(quest_player *pq) {
1059 quest_state *qs = pq->quests, *next;
1060
1061 while (qs) {
1062 next = qs->next;
1063 free_string(qs->code);
1064 free(qs);
1065 qs = next;
1066 }
1067 pq->quests = NULL;
1068 }
1069
1070
1071 /* public functions */
1072
1073 /**
1074 * Get the quest state for a player.
1075 * @param pl player.
1076 * @param quest_code internal quest code.
1077 * @return QC_COMPLETED if finished and quest can't be replayed, 0 if not started or finished and can be replayed, else quest-specific value.
1078 */
1079 int quest_get_player_state(player *pl, sstring quest_code) {
1080 quest_player *q = get_or_create_quest(pl);
1081 quest_state *s = get_state(q, quest_code);
1082 quest_definition *quest = quest_get(quest_code);
1083
1084 if (!s)
1085 return 0;
1086
1087 if (s->state == QC_CAN_RESTART && quest && quest->quest_restart)
1088 return 0;
1089
1090 return s->state;
1091 }
1092
1093 /**
1094 * Start a quest for a player. Will notify the player.
1095 * @param pl player.
1096 * @param quest_code internal quest code.
1097 * @param state initial quest state, must be greater than 0 else forced to 100 and warning emitted.
1098 */
1099 void quest_start(player *pl, sstring quest_code, int state) {
1100 quest_player *pq;
1101 quest_state *q;
1102 quest_definition *quest;
1103
1104 quest = quest_get(quest_code);
1105 if (!quest) {
1106 LOG(llevError, "quest_start: requested unknown quest %s\n", quest_code);
1107 return;
1108 }
1109 pq = get_or_create_quest(pl);
1110 q = get_or_create_state(pq, quest_code);
1111
1112 if (state <= 0) {
1113 state = 100;
1114 LOG(llevDebug, "quest_start: negative state %d for %s quest %s\n", state, pl->ob->name, quest_code);
1115 }
1116
1117 /* if completed already, assume the player can redo it */
1118 if (q->state > 0) {
1119 LOG(llevDebug, "quest_start: warning: player %s has already started quest %s\n", pl->ob->name, quest_code);
1120 }
1121
1122 draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_QUESTS, "New quest started: %s", quest->quest_title);
1123
1124 quest_set_state(NULL, pl, quest_code, state, 0);
1125
1126 /* saving state will be done in quest_set_state(). */
1127 }
1128
1129 /**
1130 * Set the state of a quest for a player.
1131 * @param pl player to set the state for.
1132 * @param quest_code quest internal code.
1133 * @param state new state for the quest, must be greater than 0 else forced to 100 and a warning is emitted.
1134 */
1135 void quest_set_player_state(player *pl, sstring quest_code, int state) {
1136 quest_set_state(NULL, pl, quest_code, state, 1);
1137 }
1138
1139 /**
1140 * Check if a quest was completed once for a player, without taking account the current state.
1141 * @param pl who to check for.
1142 * @param quest_code quest internal code.
1143 * @return 1 if the quest was already completed at least once, 0 else.
1144 */
1145 int quest_was_completed(player *pl, sstring quest_code) {
1146 quest_player *qp = get_or_create_quest(pl);
1147 quest_state *state = get_state(qp, quest_code);
1148
1149 return (state && state->was_completed);
1150 }
1151
1152 /**
1153 * Command handler for 'quest'.
1154 * @param op player asking for information, warning emitted if not a player.
1155 * @param params extra parameters for command.
1156 */
1157 void command_quest(object *op, const char *params) {
1158 /* who to display information about, used when called in DM mode */
1159 object *who;
1160 const char *name;
1161
1162 if (!op->contr) {
1163 LOG(llevError, "command_quest called for a non player!\n");
1164 return;
1165 }
1166
1167 if (!params || *params == '\0') {
1168 quest_help(op->contr);
1169 return;
1170 }
1171
1172 if (QUERY_FLAG(op, FLAG_WIZ)) {
1173 char* dup = strdup(params);
1174 char* space = strchr(dup, ' ');
1175 player* other;
1176 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND_QUESTS, "Command 'quest' called in DM mode.");
1177 if (space == NULL) {
1178 free(dup);
1179 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND_FAILURE, "Please specify a player name.");
1180 return;
1181 }
1182 params = params + (space - dup) + 1;
1183 *space = '\0';
1184 other = find_player_partial_name(dup);
1185 if (other == NULL) {
1186 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND_FAILURE, "%s is not online, or ambiguous name.", dup);
1187 free(dup);
1188 return;
1189 }
1190 free(dup);
1191 who = other->ob;
1192 name = who->name;
1193 } else {
1194 who = op;
1195 name = "You";
1196 }
1197
1198 if (strcmp(params, "list all") == 0) {
1199 quest_list(op->contr, who->contr, 1, name);
1200 return;
1201 }
1202
1203 if (strcmp(params, "list") == 0) {
1204 quest_list(op->contr, who->contr, 0, name);
1205 return;
1206 }
1207
1208 if (strncmp(params, "info ", 5) == 0) {
1209 int number = atoi(params+5);
1210 quest_info(op->contr, who->contr, get_quest_by_number(who->contr, number), 0);
1211 return;
1212 }
1213
1214 /*
1215 * Quest display for clients using the quest system, similar to 'info' above
1216 * but using the (shared) quest's client_code instead of the (player unique) index.
1217 */
1218 if (strncmp(params, "info_c ", 7) == 0) {
1219 int number = atoi(params+7);
1220 quest_player *qp = get_quest(who->contr);
1221 quest_state *qs = qp ? qp->quests : NULL;
1222 while (qs) {
1223 quest_definition *q = quest_get_by_code(qs->code);
1224 if (q && q->client_code == number) {
1225 break;
1226 }
1227 qs = qs->next;
1228 }
1229 if (qs) {
1230 quest_info(op->contr, who->contr, qs, 0);
1231 return;
1232 }
1233 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE, "Invalid quest number");
1234 return;
1235 }
1236
1237 if (QUERY_FLAG(op, FLAG_WIZ) && strncmp(params, "set ", 4) == 0) {
1238 char *dup = strdup(params + 4);
1239 char *space = strrchr(dup, ' ');
1240 int state, number;
1241 quest_state* q;
1242 if (space == NULL) {
1243 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND_FAILURE, "Syntax is: quest (player name) (quest number) (state).");
1244 free(dup);
1245 return;
1246 }
1247 *space = '\0';
1248 number = atoi(dup);
1249 q = get_quest_by_number(who->contr, number);
1250 if (q == NULL) {
1251 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND_FAILURE, "Invalid quest number %d.", number);
1252 free(dup);
1253 return;
1254 }
1255 state = atoi(space + 1);
1256 quest_set_state(op->contr, who->contr, q->code, state, 0);
1257 free(dup);
1258 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND_SUCCESS, "Set changed.");
1259 return;
1260 }
1261
1262 quest_help(op->contr);
1263 }
1264
1265 /**
1266 * Dump all defined quests on the logfile. Will call itself recursively.
1267 * @param parent only quests with a parent of this value will be displayed.
1268 * Use NULL to display top-level quests.
1269 * @param level number of '-' to display before the quest's name.
1270 */
1271 static void output_quests(quest_definition *parent, int level) {
1272 quest_definition *quest;
1273 quest_step_definition *step;
1274 char prefix[MAX_BUF];
1275 int questcount = 0, stepcount, i;
1276
1277 /* we only need to set the prefix once,
1278 * all quests that are printed in this call will be at the same level */
1279 prefix[0]='\0';
1280 for (i=0; i<level; i++) {
1281 strncat(prefix, "-", MAX_BUF - 1);
1282 }
1283 prefix[MAX_BUF - 1] = '\0';
1284
1285 quest = quests;
1286 while (quest) {
1287 if (quest->parent == parent) {
1288 questcount++;
1289 stepcount=0;
1290 step = quest->steps;
1291 while (step) {
1292 stepcount++;
1293 step= step->next;
1294 }
1295 fprintf(logfile, "%s%s - %s - %d steps (%srestartable)\n", prefix, quest->quest_code, quest->quest_title, stepcount, quest->quest_restart?"":"not ");
1296 output_quests(quest, level+1);
1297 }
1298 quest = quest->next;
1299 }
1300 }
1301
1302 /**
1303 * Dump all of the quests, then calls exit() - useful in terms of debugging to make sure that
1304 * quests are set up and recognised correctly.
1305 */
1306 void dump_quests(void) {
1307 quest_load_definitions();
1308 output_quests(NULL, 0);
1309 exit(0);
1310 }
1311
1312 /**
1313 * Free all quest status structures. It is all right to call quest functions again after that.
1314 */
1315 void free_quest(void) {
1316 quest_player *pq = player_states, *next;
1317
1318 while (pq) {
1319 next = pq->next;
1320 free_state(pq);
1321 free_string(pq->player_name);
1322 free(pq);
1323 pq = next;
1324 }
1325 player_states = NULL;
1326 }
1327
1328 /**
1329 * Free all quest definitions and steps.
1330 * Can be called multiple times.
1331 * Used by DMs through the 'purge_quests' command.
1332 */
1333 void free_quest_definitions(void) {
1334 quest_definition *quest = quests, *next_quest;
1335 quest_step_definition *step, *next_step;
1336 quest_condition *condition, *next_condition;
1337
1338 while (quest != NULL) {
1339 next_quest = quest->next;
1340 free_string(quest->quest_code);
1341 if (quest->quest_description != NULL)
1342 free_string(quest->quest_description);
1343 if (quest->quest_title != NULL)
1344 free_string(quest->quest_title);
1345 step = quest->steps;
1346 while (step != NULL) {
1347 next_step = step->next;
1348 free_string(step->step_description);
1349 condition = step->conditions;
1350 while (condition != NULL) {
1351 next_condition = condition->next;
1352 free_string(condition->quest_code);
1353 free(condition);
1354 condition = next_condition;
1355 }
1356 free(step);
1357 step = next_step;
1358 }
1359 free(quest);
1360 quest = next_quest;
1361 }
1362
1363 quests = NULL;
1364 quests_loaded = 0;
1365 }
1366
1367 /**
1368 * Send the current quest states for the specified player, if the client
1369 * supports those notifications.
1370 * @param pl who to send quests for.
1371 */
1372 void quest_send_initial_states(player *pl) {
1373 quest_player *states = NULL;
1374 quest_state *state = NULL;
1375 SockList sl;
1376 size_t size;
1377 quest_definition *quest;
1378 quest_step_definition *step;
1379
1380 if (pl->socket.notifications < 1)
1381 return;
1382
1383 /* ensure quest definitions are loaded */
1384 quest_load_definitions();
1385
1386 states = get_or_create_quest(pl);
1387
1388 SockList_Init(&sl);
1389 SockList_AddString(&sl, "addquest ");
1390 for (state = states->quests; state != NULL; state = state->next) {
1391
1392 quest = quest_get_by_code(state->code);
1393 if (state->state == -1)
1394 step = NULL;
1395 else
1396 step = quest_get_step(quest, state->state);
1397
1398 size = 2 + (2 + strlen(quest->quest_title)) + 4 + 1 + (2 + (step != NULL ? strlen(step->step_description) : 0));
1399
1400 if (SockList_Avail(&sl) < size) {
1401 Send_With_Handling(&pl->socket, &sl);
1402 SockList_Reset(&sl);
1403 SockList_AddString(&sl, "addquest ");
1404 }
1405
1406 SockList_AddInt(&sl, quest->client_code);
1407 SockList_AddLen16Data(&sl, quest->quest_title, strlen(quest->quest_title));
1408 if (quest->face && !(pl->socket.faces_sent[quest->face->number]&NS_FACESENT_FACE))
1409 esrv_send_face(&pl->socket, quest->face, 0);
1410 SockList_AddInt(&sl, quest->face ? quest->face->number : 0);
1411 SockList_AddChar(&sl, quest->quest_restart ? 1 : 0);
1412 SockList_AddInt(&sl, quest->parent ? quest->parent->client_code : 0);
1413 SockList_AddChar(&sl, (step == NULL || step->is_completion_step) ? 1 : 0);
1414 if (step != NULL)
1415 SockList_AddLen16Data(&sl, step->step_description, strlen(step->step_description));
1416 else
1417 SockList_AddShort(&sl, 0);
1418
1419 state->sent_to_client = 1;
1420 }
1421
1422 Send_With_Handling(&pl->socket, &sl);
1423 SockList_Term(&sl);
1424 }
1425
1426 /**
1427 * Ensure the quest state is correctly saved for a player.
1428 * This function should only be called once, when the player's save directory
1429 * is created. All other quest functions save the state automatically, but save
1430 * can only happen when the player directory exists.
1431 * @param pl who to save quests for.
1432 */
1433 void quest_first_player_save(player *pl) {
1434 quest_player *qp = get_quest(pl);
1435 if (qp != NULL && qp->quests != NULL) {
1436 quest_write_player_data(qp);
1437 }
1438 }