Crossfire Server, Trunk  R20513
Data Structures | Macros | Typedefs | Functions | Variables
quest.c File Reference

Quest-related low-level mechanisms. More...

#include "global.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "output_file.h"
#include "sproto.h"
Include dependency graph for quest.c:

Go to the source code of this file.

Data Structures

struct  quest_condition
 One condition to automatically move to a quest step. More...
 
struct  quest_definition
 Definition of an in-game quest. More...
 
struct  quest_player
 Information about a player. More...
 
struct  quest_state
 Information about a quest for a player. More...
 
struct  quest_step_definition
 One step of a quest. More...
 

Macros

#define QC_CAN_RESTART   -1
 Quest status that indicates a quest was completed and may be restarted. More...
 
#define QUESTFILE_NEXTQUEST   0
 Waiting for next quest definition. More...
 
#define QUESTFILE_QUEST   1
 In a quest definition. More...
 
#define QUESTFILE_QUESTDESC   2
 In a quest description. More...
 
#define QUESTFILE_STEP   3
 In a quest step. More...
 
#define QUESTFILE_STEPCOND   5
 In a quest step conditions. More...
 
#define QUESTFILE_STEPDESC   4
 In a quest step description. More...
 

Typedefs

typedef struct quest_condition quest_condition
 One condition to automatically move to a quest step. More...
 
typedef struct quest_definition quest_definition
 Definition of an in-game quest. More...
 
typedef struct quest_player quest_player
 Information about a player. More...
 
typedef struct quest_state quest_state
 Information about a quest for a player. More...
 
typedef struct quest_step_definition quest_step_definition
 One step of a quest. More...
 

Functions

void command_quest (object *op, const char *params)
 Command handler for 'quest'. More...
 
void dump_quests (void)
 Dump all of the quests, then calls exit() - useful in terms of debugging to make sure that quests are set up and recognised correctly. More...
 
static int evaluate_quest_conditions (const quest_condition *condition, player *pl)
 Checks whether the conditions for a given step are met. More...
 
void free_quest (void)
 Free all quest status structures. More...
 
void free_quest_definitions (void)
 Free all quest definitions and steps. More...
 
static void free_state (quest_player *pq)
 Free quests structures. More...
 
static quest_stateget_new_quest_state (void)
 Return a new quest_state*, calling fatal() if memory shortage. More...
 
static quest_playerget_or_create_quest (player *pl)
 Get quest status for a player, creating it if it doesn't exist yet. More...
 
static quest_stateget_or_create_state (quest_player *pq, sstring name)
 Get the state of a quest for a player, creating it if not existing yet. More...
 
static quest_playerget_quest (player *pl)
 Get quest status for a player, not creating it if it doesn't exist. More...
 
static quest_stateget_quest_by_number (player *pl, int number)
 returns the quest state which corresponds to a certain number for the given player. More...
 
static quest_stateget_state (quest_player *pq, sstring name)
 Get the state of a quest for a player, not creating if not existing yet. More...
 
static int load_quests_from_file (const char *filename)
 Loads all of the quests which are found in the given file, any global states for quest loading are passed down into this function, but not back up again. More...
 
static void output_quests (quest_definition *parent, int level)
 Dump all defined quests on the logfile. More...
 
static quest_conditionquest_create_condition (void)
 Allocate a quest_condition, will call fatal() if out of memory. More...
 
static quest_definitionquest_create_definition (void)
 Allocate a quest_definition, will call fatal() if out of memory. More...
 
static quest_step_definitionquest_create_step (void)
 Allocate a quest_step_definition, will call fatal() if out of memory. More...
 
static void quest_display (player *pl, quest_player *pq, int showall, const char *name)
 Utility function to display a quest list. More...
 
void quest_first_player_save (player *pl)
 Ensure the quest state is correctly saved for a player. More...
 
static quest_definitionquest_get (sstring code)
 Find a quest from its code. More...
 
static quest_definitionquest_get_by_code (sstring code)
 Find a quest from its code. More...
 
int quest_get_player_state (player *pl, sstring quest_code)
 Get the quest state for a player. More...
 
static quest_step_definitionquest_get_step (quest_definition *quest, int step)
 Get a step for the specified quest. More...
 
static void quest_help (player *pl)
 Quest command help. More...
 
static void quest_info (player *pl, player *who, quest_state *qs, int level)
 Give details about a quest. More...
 
static void quest_list (player *pl, player *who, int showall, const char *name)
 Display current and completed player quests. More...
 
static void quest_load_definitions (void)
 Load all quest definitions. More...
 
static void quest_read_player_data (quest_player *pq)
 Read quest-data information for a player. More...
 
void quest_send_initial_states (player *pl)
 Send the current quest states for the specified player, if the client supports those notifications. More...
 
void quest_set_player_state (player *pl, sstring quest_code, int state)
 Set the state of a quest for a player. More...
 
static void quest_set_state (player *dm, player *pl, sstring quest_code, int state, int started)
 Set the state of a quest for a player. More...
 
void quest_start (player *pl, sstring quest_code, int state)
 Start a quest for a player. More...
 
int quest_was_completed (player *pl, sstring quest_code)
 Check if a quest was completed once for a player, without taking account the current state. More...
 
static void quest_write_player_data (const quest_player *pq)
 Write quest-data information for a player. More...
 
static void update_quests (player *pl)
 Look through all of the quests for the given player, and see if any need to be updated. More...
 

Variables

static quest_playerplayer_states = NULL
 Player quest state. More...
 
static quest_definitionquests = NULL
 All known quests. More...
 
static int quests_loaded = 0
 Did we already read the 'default.quests' file? More...
 

Detailed Description

Quest-related low-level mechanisms.

You should only need to call the public functions, all that are not static.

Data is loaded on a need-only basis - when a player quest state is queried or modified, data is read. Also, free_quest() can be called to release memory without preventing quest operations after.

Write is done for each player whenever the state changes, to ensure data integrity.

Definition in file quest.c.

Macro Definition Documentation

#define QC_CAN_RESTART   -1

Quest status that indicates a quest was completed and may be restarted.

Definition at line 38 of file quest.c.

Referenced by get_quest_by_number(), quest_display(), quest_get_player_state(), quest_info(), and quest_set_state().

Typedef Documentation

One condition to automatically move to a quest step.

Definition of an in-game quest.

typedef struct quest_player quest_player

Information about a player.

typedef struct quest_state quest_state

Information about a quest for a player.

One step of a quest.

Function Documentation

void command_quest ( object op,
const char *  params 
)

Command handler for 'quest'.

Parameters
opplayer asking for information, warning emitted if not a player.
paramsextra parameters for command.

Definition at line 1154 of file quest.c.

References quest_state::code, obj::contr, draw_ext_info(), draw_ext_info_format(), find_player_partial_name(), FLAG_WIZ, get_quest_by_number(), llevError, LOG(), MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND_FAILURE, MSG_TYPE_COMMAND_INFO, MSG_TYPE_COMMAND_SUCCESS, obj::name, NDI_UNIQUE, pl::ob, QUERY_FLAG, quest_help(), quest_info(), quest_list(), quest_set_state(), quest_state::state, and strdup().

Here is the call graph for this function:

void dump_quests ( void  )

Dump all of the quests, then calls exit() - useful in terms of debugging to make sure that quests are set up and recognised correctly.

Definition at line 1280 of file quest.c.

References output_quests(), and quest_load_definitions().

Here is the call graph for this function:

static int evaluate_quest_conditions ( const quest_condition condition,
player pl 
)
static

Checks whether the conditions for a given step are met.

Parameters
conditionthe linked list of conditions to check.
plthe player to evaluate conditions for.
Returns
1 if the conditions match, 0 if they don't.

Definition at line 680 of file quest.c.

References quest_condition::maxstep, quest_condition::minstep, quest_condition::next, quest_condition::quest_code, quest_get_player_state(), and quest_was_completed().

Referenced by update_quests().

Here is the call graph for this function:

Here is the caller graph for this function:

void free_quest ( void  )

Free all quest status structures.

It is all right to call quest functions again after that.

Definition at line 1289 of file quest.c.

References free_state(), free_string(), quest_state::next, quest_player::next, quest_player::player_name, and player_states.

Referenced by cleanup(), command_purge_quest(), and free_server().

Here is the call graph for this function:

Here is the caller graph for this function:

void free_quest_definitions ( void  )

Free all quest definitions and steps.

Can be called multiple times. Used by DMs through the 'purge_quests' command.

Definition at line 1307 of file quest.c.

References quest_step_definition::conditions, free_string(), quest_condition::next, quest_step_definition::next, quest_definition::next, quest_condition::quest_code, quest_definition::quest_code, quest_definition::quest_description, quest_definition::quest_title, quests, quests_loaded, quest_step_definition::step_description, and quest_definition::steps.

Referenced by cleanup(), command_purge_quest_definitions(), and free_server().

Here is the call graph for this function:

Here is the caller graph for this function:

static void free_state ( quest_player pq)
static

Free quests structures.

Parameters
pqwhat to free.

Definition at line 1055 of file quest.c.

References quest_state::code, free_string(), quest_state::next, and quest_player::quests.

Referenced by free_quest().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_state* get_new_quest_state ( void  )
static

Return a new quest_state*, calling fatal() if memory shortage.

Returns
new value, never NULL.

Definition at line 467 of file quest.c.

References fatal(), and OUT_OF_MEMORY.

Referenced by quest_read_player_data().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_player* get_or_create_quest ( player pl)
static

Get quest status for a player, creating it if it doesn't exist yet.

Calls fatal() if memory allocation error.

Parameters
plplayer to get information of.
Returns
quest status, never NULL.

Definition at line 655 of file quest.c.

References add_refcount(), fatal(), get_quest(), obj::name, quest_player::next, pl::ob, OUT_OF_MEMORY, quest_player::player_name, player_states, quest_definition::quest_code, quest_read_player_data(), quest_set_state(), and quest_state::state.

Referenced by get_quest_by_number(), quest_get_player_state(), quest_info(), quest_list(), quest_send_initial_states(), quest_set_state(), quest_start(), and quest_was_completed().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_state* get_or_create_state ( quest_player pq,
sstring  name 
)
static

Get the state of a quest for a player, creating it if not existing yet.

Parameters
pqplayer to get state for.
namequest to get state of.
Returns
quest's state information, newly created if it wasn't done yet.

Definition at line 611 of file quest.c.

References add_refcount(), quest_state::code, fatal(), get_state(), quest_state::next, OUT_OF_MEMORY, and quest_player::quests.

Referenced by quest_set_state(), and quest_start().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_player* get_quest ( player pl)
static

Get quest status for a player, not creating it if it doesn't exist.

Parameters
plplayer to get information of.
Returns
quest status, NULL if not loaded/found yet.

Definition at line 637 of file quest.c.

References obj::name, quest_player::next, pl::ob, quest_player::player_name, and player_states.

Referenced by get_or_create_quest(), and quest_first_player_save().

Here is the caller graph for this function:

static quest_state* get_quest_by_number ( player pl,
int  number 
)
static

returns the quest state which corresponds to a certain number for the given player.

Parameters
plplayer asking for details.
numberquest number.
Returns
quest state corresponding to the number provided, NULL if there is no such quest state.

Definition at line 954 of file quest.c.

References quest_state::code, get_or_create_quest(), quest_state::is_complete, quest_state::next, quest_definition::parent, QC_CAN_RESTART, quest_get(), quest_player::quests, and quest_state::state.

Referenced by command_quest().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_state* get_state ( quest_player pq,
sstring  name 
)
static

Get the state of a quest for a player, not creating if not existing yet.

Parameters
pqplayer to get state for.
namequest to get state of.
Returns
NULL if quest isn't started yet for this player, else quest's state information.

Definition at line 593 of file quest.c.

References quest_state::code, quest_state::next, and quest_player::quests.

Referenced by get_or_create_state(), quest_get_player_state(), and quest_was_completed().

Here is the caller graph for this function:

static int load_quests_from_file ( const char *  filename)
static
static void output_quests ( quest_definition parent,
int  level 
)
static

Dump all defined quests on the logfile.

Will call itself recursively.

Parameters
parentonly quests with a parent of this value will be displayed. Use NULL to display top-level quests.
levelnumber of '-' to display before the quest's name.

Definition at line 1245 of file quest.c.

References logfile, MAX_BUF, quest_step_definition::next, quest_definition::next, quest_definition::parent, quest_definition::quest_code, quest_definition::quest_restart, quest_definition::quest_title, quests, and quest_definition::steps.

Referenced by dump_quests().

Here is the caller graph for this function:

static quest_condition* quest_create_condition ( void  )
static

Allocate a quest_condition, will call fatal() if out of memory.

Returns
new structure.

Definition at line 110 of file quest.c.

References fatal(), and OUT_OF_MEMORY.

Referenced by load_quests_from_file().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_definition* quest_create_definition ( void  )
static

Allocate a quest_definition, will call fatal() if out of memory.

Returns
new structure.

Definition at line 122 of file quest.c.

References fatal(), and OUT_OF_MEMORY.

Referenced by load_quests_from_file().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_step_definition* quest_create_step ( void  )
static

Allocate a quest_step_definition, will call fatal() if out of memory.

Returns
new structure.

Definition at line 99 of file quest.c.

References fatal(), and OUT_OF_MEMORY.

Referenced by load_quests_from_file().

Here is the call graph for this function:

Here is the caller graph for this function:

static void quest_display ( player pl,
quest_player pq,
int  showall,
const char *  name 
)
static

Utility function to display a quest list.

Will show a header before the list if not empty.

Parameters
plplayer to display list of quests.
pqquests to display.
showallif 0, only shows quests in progress and a summary of completed quests, else shows all quests.
nameeither 'You' or the player's name, if pl is a DM asking about another player.

Definition at line 843 of file quest.c.

References quest_state::code, draw_ext_info_format(), quest_state::is_complete, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, NDI_UNIQUE, quest_state::next, pl::ob, quest_definition::parent, QC_CAN_RESTART, quest_get(), quest_definition::quest_title, quest_player::quests, and quest_state::state.

Referenced by quest_list().

Here is the call graph for this function:

Here is the caller graph for this function:

void quest_first_player_save ( player pl)

Ensure the quest state is correctly saved for a player.

This function should only be called once, when the player's save directory is created. All other quest functions save the state automatically, but save can only happen when the player directory exists.

Parameters
plwho to save quests for.

Definition at line 1407 of file quest.c.

References get_quest(), quest_write_player_data(), and quest_player::quests.

Referenced by save_player().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_definition* quest_get ( sstring  code)
static

Find a quest from its code.

Parameters
codequest to search.
Returns
quest, , or NULL if no such quest in which case a llevError is emitted.

Definition at line 450 of file quest.c.

References llevError, LOG(), quest_get_by_code(), and quest_load_definitions().

Referenced by get_quest_by_number(), quest_display(), quest_get_player_state(), quest_info(), quest_set_state(), and quest_start().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_definition* quest_get_by_code ( sstring  code)
static

Find a quest from its code.

This is called by quest_get and also used in the quest loading code

Parameters
codequest to search.
Returns
quest, or NULL if no such quest.

Definition at line 135 of file quest.c.

References quest_definition::next, quest_definition::quest_code, and quests.

Referenced by load_quests_from_file(), quest_get(), quest_read_player_data(), and quest_send_initial_states().

Here is the caller graph for this function:

int quest_get_player_state ( player pl,
sstring  quest_code 
)

Get the quest state for a player.

Parameters
plplayer.
quest_codeinternal quest code.
Returns
QC_COMPLETED if finished and quest can't be replayed, 0 if not started or finished and can be replayed, else quest-specific value.

Definition at line 1076 of file quest.c.

References get_or_create_quest(), get_state(), QC_CAN_RESTART, quest_get(), quest_definition::quest_restart, and quest_state::state.

Referenced by cfapi_player_quest(), evaluate_quest_conditions(), and update_quests().

Here is the call graph for this function:

Here is the caller graph for this function:

static quest_step_definition* quest_get_step ( quest_definition quest,
int  step 
)
static

Get a step for the specified quest.

Parameters
questquest to consider.
stepstep to find.
Returns
step, or NULL if no such step in which case a llevError is emitted.

Definition at line 431 of file quest.c.

References llevError, LOG(), quest_step_definition::next, quest_definition::quest_code, quest_step_definition::step, and quest_definition::steps.

Referenced by quest_info(), quest_read_player_data(), quest_send_initial_states(), and quest_set_state().

Here is the call graph for this function:

Here is the caller graph for this function:

static void quest_help ( player pl)
static

Quest command help.

Parameters
plplayer to display help for.

Definition at line 933 of file quest.c.

References draw_ext_info(), FLAG_WIZ, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, NDI_UNIQUE, pl::ob, and QUERY_FLAG.

Referenced by command_quest().

Here is the call graph for this function:

Here is the caller graph for this function:

static void quest_info ( player pl,
player who,
quest_state qs,
int  level 
)
static

Give details about a quest.

Parameters
plplayer to give quest details to.
whoplayer to give quest details of.
qsquest_state to give details about
levelThe level of recursion for the quest info that's being provided

Definition at line 989 of file quest.c.

References quest_state::code, draw_ext_info(), draw_ext_info_format(), FLAG_WIZ, get_or_create_quest(), quest_state::is_complete, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, NDI_UNIQUE, quest_state::next, quest_step_definition::next, pl::ob, quest_definition::parent, QC_CAN_RESTART, QUERY_FLAG, quest_definition::quest_code, quest_definition::quest_description, quest_get(), quest_get_step(), quest_definition::quest_restart, quest_definition::quest_title, quest_player::quests, quest_state::state, quest_step_definition::step, quest_step_definition::step_description, and quest_definition::steps.

Referenced by command_quest().

Here is the call graph for this function:

Here is the caller graph for this function:

static void quest_list ( player pl,
player who,
int  showall,
const char *  name 
)
static

Display current and completed player quests.

Parameters
plplayer to display to.
whoplayer to display information for.
showall- whether to show all of the quests in full, just summary information for the completed ones
nameeither 'You' or the player's name, if pl is a DM asking about another player.

Definition at line 916 of file quest.c.

References draw_ext_info_format(), get_or_create_quest(), MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, NDI_UNIQUE, pl::ob, quest_display(), and quest_player::quests.

Referenced by command_quest().

Here is the call graph for this function:

Here is the caller graph for this function:

static void quest_load_definitions ( void  )
static

Load all quest definitions.

Can be called multiple times, will be ignored.

Definition at line 412 of file quest.c.

References llevError, llevInfo, load_quests_from_file(), LOG(), and quests_loaded.

Referenced by dump_quests(), quest_get(), quest_read_player_data(), and quest_send_initial_states().

Here is the call graph for this function:

Here is the caller graph for this function:

static void quest_read_player_data ( quest_player pq)
static
void quest_send_initial_states ( player pl)
void quest_set_player_state ( player pl,
sstring  quest_code,
int  state 
)

Set the state of a quest for a player.

Parameters
plplayer to set the state for.
quest_codequest internal code.
statenew state for the quest, must be greater than 0 else forced to 100 and a warning is emitted.

Definition at line 1132 of file quest.c.

References quest_set_state().

Referenced by cfapi_player_quest().

Here is the call graph for this function:

Here is the caller graph for this function:

static void quest_set_state ( player dm,
player pl,
sstring  quest_code,
int  state,
int  started 
)
static

Set the state of a quest for a player.

Parameters
dmif NULL then the player is actually playing, else a DM is changing the quest's state manually.
plplayer to set the state for.
quest_codequest internal code.
statenew state for the quest, must be greater than 0 else forced to 100 and a warning is emitted.
startedif 1, quest must have been started first or a warning is emitted, else it doesn't matter.

Definition at line 744 of file quest.c.

References quest_definition::client_code, draw_ext_info(), draw_ext_info_format(), esrv_send_face(), quest_definition::face, socket_struct::faces_sent, get_or_create_quest(), get_or_create_state(), pl::has_directory, quest_state::is_complete, quest_step_definition::is_completion_step, llevDebug, llevError, LOG(), MSG_TYPE_ADMIN_DM, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE, MSG_TYPE_COMMAND_INFO, obj::name, NDI_UNIQUE, socket_struct::notifications, NS_FACESENT_FACE, pl::ob, quest_definition::parent, QC_CAN_RESTART, quest_get(), quest_get_step(), quest_definition::quest_restart, quest_definition::quest_title, quest_write_player_data(), Send_With_Handling(), quest_state::sent_to_client, pl::socket, SockList_AddChar(), SockList_AddInt(), SockList_AddLen16Data(), SockList_AddString(), SockList_Init(), SockList_Term(), quest_state::state, quest_step_definition::step_description, update_quests(), and quest_state::was_completed.

Referenced by command_quest(), get_or_create_quest(), quest_set_player_state(), quest_start(), and update_quests().

Here is the call graph for this function:

Here is the caller graph for this function:

void quest_start ( player pl,
sstring  quest_code,
int  state 
)

Start a quest for a player.

Will notify the player.

Parameters
plplayer.
quest_codeinternal quest code.
stateinitial quest state, must be greater than 0 else forced to 100 and warning emitted.

Definition at line 1096 of file quest.c.

References draw_ext_info_format(), get_or_create_quest(), get_or_create_state(), llevDebug, llevError, LOG(), MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, obj::name, NDI_UNIQUE, pl::ob, quest_get(), quest_set_state(), quest_definition::quest_title, and quest_state::state.

Referenced by cfapi_player_quest().

Here is the call graph for this function:

Here is the caller graph for this function:

int quest_was_completed ( player pl,
sstring  quest_code 
)

Check if a quest was completed once for a player, without taking account the current state.

Parameters
plwho to check for.
quest_codequest internal code.
Returns
1 if the quest was already completed at least once, 0 else.

Definition at line 1142 of file quest.c.

References get_or_create_quest(), get_state(), quest_state::state, and quest_state::was_completed.

Referenced by cfapi_player_quest(), and evaluate_quest_conditions().

Here is the call graph for this function:

Here is the caller graph for this function:

static void quest_write_player_data ( const quest_player pq)
static

Write quest-data information for a player.

Parameters
pqplayer to write data for.
Todo:
rename/backup, stuff like that

Definition at line 556 of file quest.c.

References quest_state::code, draw_ext_info(), Settings::localdir, MAX_BUF, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE, NDI_ALL_DMS, NDI_UNIQUE, quest_state::next, of_close(), of_open(), quest_player::player_name, Settings::playerdir, quest_player::quests, settings, snprintf, quest_state::state, and quest_state::was_completed.

Referenced by quest_first_player_save(), and quest_set_state().

Here is the call graph for this function:

Here is the caller graph for this function:

static void update_quests ( player pl)
static

Look through all of the quests for the given player, and see if any need to be updated.

Parameters
pl

Definition at line 706 of file quest.c.

References quest_step_definition::conditions, evaluate_quest_conditions(), quest_step_definition::next, quest_definition::next, quest_definition::quest_code, quest_get_player_state(), quest_set_state(), quests, quest_step_definition::step, and quest_definition::steps.

Referenced by quest_set_state().

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

quest_player* player_states = NULL
static

Player quest state.

Definition at line 58 of file quest.c.

Referenced by free_quest(), get_or_create_quest(), and get_quest().

quest_definition* quests = NULL
static
int quests_loaded = 0
static

Did we already read the 'default.quests' file?

Definition at line 92 of file quest.c.

Referenced by free_quest_definitions(), and quest_load_definitions().