Crossfire Server, Trunk
|
#include "global.h"
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "sproto.h"
#include "output_file.h"
Go to the source code of this file.
Data Structures | |
struct | knowledge_item |
struct | knowledge_player |
struct | knowledge_type |
Macros | |
#define | TAG_END "[/color]" |
#define | TAG_START "[color=#ff6611]" |
Typedefs | |
typedef int(* | knowledge_add_item) (struct knowledge_player *current, const char *item, const struct knowledge_type *type) |
typedef void(* | knowledge_attempt) (player *pl, const struct knowledge_item *item) |
typedef StringBuffer *(* | knowledge_can_use_alchemy) (sstring code, const char *item, StringBuffer *buf, int index) |
typedef void(* | knowledge_detail) (const char *code, StringBuffer *buf) |
typedef int(* | knowledge_is_valid_item) (const char *code) |
typedef struct knowledge_item | knowledge_item |
typedef struct knowledge_player | knowledge_player |
typedef void(* | knowledge_summary) (const char *code, StringBuffer *buf) |
typedef struct knowledge_type | knowledge_type |
Variables | |
const typedef Face *(* | knowledge_face )(sstring code) |
static knowledge_player * | knowledge_global = NULL |
static const knowledge_type | knowledges [] |
Handling of player knowledge of various things.
Right now, the following items are considered:
Knowledge is specific for a player, and organized in 'items'. One item is a specific formulae or monster, whatever is coded.
Note that some things are done through functions in readable.c, especially item marking.
Knowledge is persistent and without errors. Maybe that could be changed in the future?
Only use non static functions when using this file.
Loading is done on a per-demand basis, writing at each change.
Knowledge is stored in a "player.knowledge" file in the player's directory.
Knowledge relies on key/values. Key is "knowledge_marker", value is specific to the item class considered and must have a left-side part, delimited by a double dot, specifying a type.
The value should be enough to determine a unique formulae or monster etc.
A type is linked to various functions for displaying, checking the key value.
During loading, obsolete items (eg formula changed) are discarded.
If the client has notifications support of at least 2, then knowledge information is sent as needed, incrementally as to not freeze the server.
Definition in file knowledge.c.
#define TAG_END "[/color]" |
Definition at line 70 of file knowledge.c.
#define TAG_START "[color=#ff6611]" |
Definition at line 69 of file knowledge.c.
typedef int(* knowledge_add_item) (struct knowledge_player *current, const char *item, const struct knowledge_type *type) |
Add knowledge information to the player's knowledge.
current | where to add the information to. |
item | what to add, format specific to the type. |
type | pointer of the handler type. |
Definition at line 103 of file knowledge.c.
typedef void(* knowledge_attempt) (player *pl, const struct knowledge_item *item) |
Attempt an alchemy based on the specified knowledge.
pl | who attempts the recipe. |
item | item to attempt, must not be NULL. |
Definition at line 129 of file knowledge.c.
typedef StringBuffer*(* knowledge_can_use_alchemy) (sstring code, const char *item, StringBuffer *buf, int index) |
Check if an item can be used for a recipe, and fill the return buffer if it's the case.
code | knowledge internal code. |
item | item's name, including title if there is one. |
buf | where to put the results. If NULL a new one can be allocated. |
index | the knowledge index for this item. |
Definition at line 113 of file knowledge.c.
typedef void(* knowledge_detail) (const char *code, StringBuffer *buf) |
Function to fill the StringBuffer with a detailed description of an item.
code | knowledge internal code. |
buf | where to write the description, must not be NULL. |
Definition at line 87 of file knowledge.c.
typedef int(* knowledge_is_valid_item) (const char *code) |
Function to check if the specified item is valid.
code | knowledge internal code. |
Definition at line 94 of file knowledge.c.
typedef struct knowledge_item knowledge_item |
One known item for a player.
typedef struct knowledge_player knowledge_player |
Information about a player.
typedef void(* knowledge_summary) (const char *code, StringBuffer *buf) |
Function to fill the StringBuffer with the short description of an item.
code | knowledge internal code. |
buf | where to write the summary, must not be NULL. |
Definition at line 80 of file knowledge.c.
typedef struct knowledge_type knowledge_type |
One item type that may be known to the player.
void command_knowledge | ( | object * | pl, |
const char * | params | ||
) |
Handle the 'knowledge' for a player.
pl | who is using the command. |
params | additional parameters. |
Definition at line 1210 of file knowledge.c.
References command_help(), knowledge_display(), knowledge_do_attempt(), knowledge_do_display(), knowledge_show(), llevError, LOG(), and roll-o-matic::params.
Referenced by commands_init().
void free_knowledge | ( | void | ) |
Free all knowledge data.
Definition at line 1277 of file knowledge.c.
References free_knowledge_player(), knowledge_global, give::next, and knowledge_player::next.
Referenced by cleanup().
|
static |
Free all knowledge items for a player.
kp | what to free. |
Definition at line 1249 of file knowledge.c.
References free_string(), say::item, knowledge_player::item_allocated, knowledge_player::item_count, and knowledge_player::items.
Referenced by free_knowledge_player().
|
static |
Totally free a knowledge structure, and its items.
kp | structure to free, becomes invalid. |
Definition at line 1268 of file knowledge.c.
References free_knowledge_items(), free_string(), and knowledge_player::player_name.
Referenced by free_knowledge(), and knowledge_process_incremental().
|
static |
Add a knowledge item to a player's store if not found yet.
current | where to look for the knowledge. |
item | internal value of the item to give, considered atomic. |
kt | how to handle the knowledge type. |
Use malloc instead of calloc here. We're setting all relevant values, anyway.
Definition at line 547 of file knowledge.c.
References add_string(), reputation_trigger_connect::check(), replace::current, and knowledge_known().
Referenced by knowledge_god_add(), and knowledge_monster_add().
Display monster details, then add to a player's knowledge if not already.
op | player asking for details. |
mon | monster |
Definition at line 1525 of file knowledge.c.
References buf, describe_item(), draw_ext_info(), mon, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_EXAMINE, obj::name, NDI_UNIQUE, give::op, rotate-tower::result, stringbuffer_append_printf(), stringbuffer_finish(), and stringbuffer_new().
Referenced by examine_monster().
|
static |
Attempt an alchemy recipe through the knowledge system.
pl | who is attempting the recipe. |
item | knowledge item, should be of type recipe. |
Definition at line 350 of file knowledge.c.
References apply_by_living_below(), obj::arch, obj::below, recipestruct::cauldron, command_take(), obj::container, disinfect::count, obj::count, draw_ext_info(), draw_ext_info_format(), env, examine(), FLAG_INV_LOCKED, FLAG_STARTEQUIP, npc_dialog::index, recipestruct::ingred, recipestruct::ingred_count, commongive::inv, obj::inv, knowledge_alchemy_get_recipe(), llevError, LOG(), disinfect::map, obj::map, MAX_BUF, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, give::name, linked_char::name, archt::name, NDI_UNIQUE, linked_char::next, obj::nrof, pl::ob, object_was_destroyed, put_object_in_sack(), QUERY_FLAG, safe_strncpy, obj::speed_left, CFweardisguise::tag, use_alchemy(), diamondslots::x, obj::x, diamondslots::y, and obj::y.
|
static |
Check if an item can be used for a recipe, and fill the return buffer if it's the case.
code | recipe internal code. |
item | item's name, including title if there is one. |
buf | where to put the results. If NULL a new one can be allocated. |
index | the knowledge index for this item. |
Definition at line 297 of file knowledge.c.
References archininventory::arch, recipestruct::arch_name, recipestruct::arch_names, buf, npc_dialog::index, recipestruct::ingred, knowledge_alchemy_get_recipe(), give::name, give::next, RANDOM, stringbuffer_append_printf(), stringbuffer_append_string(), stringbuffer_new(), recipestruct::title, and try_find_archetype().
|
static |
Give the full description of the alchemy recpie.
value | recipe internal value. |
buf | where to store the detail. |
Definition at line 240 of file knowledge.c.
References archininventory::arch, recipestruct::arch_name, recipestruct::arch_names, buf, recipestruct::cauldron, recipestruct::ingred, knowledge_alchemy_get_recipe(), MAX_BUF, give::name, give::next, query_name(), RANDOM, recipestruct::skill, stringbuffer_append_printf(), recipestruct::title, try_find_archetype(), and autojail::value.
Try to get a face for an alchemy recipe.
code | alchemy code. |
Definition at line 511 of file knowledge.c.
References knowledge_alchemy_get_recipe(), llevError, LOG(), and recipe_get_face().
|
static |
Get a recipe from the internal code.
value | code to get value for. |
Definition at line 172 of file knowledge.c.
References disinfect::count, get_formulalist(), recipestruct::index, npc_dialog::index, recipeliststruct::items, recipestruct::next, recipestruct::title, and autojail::value.
Referenced by knowledge_alchemy_attempt(), knowledge_alchemy_can_use_item(), knowledge_alchemy_detail(), knowledge_alchemy_face(), knowledge_alchemy_summary(), and knowledge_alchemy_validate().
|
static |
Give the title of the alchemy recpie.
value | recipe internal value. |
buf | where to put the information. |
Definition at line 210 of file knowledge.c.
References archininventory::arch, recipestruct::arch_name, recipestruct::arch_names, buf, knowledge_alchemy_get_recipe(), RANDOM, stringbuffer_append_printf(), recipestruct::title, try_find_archetype(), and autojail::value.
|
static |
Check if an alchemy recipe is still ok.
item | what to check for |
Definition at line 285 of file knowledge.c.
References knowledge_alchemy_get_recipe().
|
static |
Display all a player's knowledge.
pl | who to display knowledge of. |
params | additionnal parameters. |
Definition at line 1130 of file knowledge.c.
References draw_ext_info(), draw_ext_info_format(), knowledge_do_display(), knowledges, MSG_TYPE_CLIENT_NOTICE, MSG_TYPE_MISC, give::name, NDI_UNIQUE, roll-o-matic::params, is_valid_types_gen::type, and knowledge_type::type.
Referenced by command_knowledge().
|
static |
Player attempts something on a knowledge, get item and try it.
pl | who is trying. |
params | command parameters. |
Definition at line 1186 of file knowledge.c.
References disinfect::count, draw_ext_info(), say::item, knowledge_player::item_count, knowledge_player::items, knowledge_get_or_create(), MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, NDI_UNIQUE, and roll-o-matic::params.
Referenced by command_knowledge().
|
static |
Actually display knowledge list.
pl | who to display to. |
show_only | if not NULL, only display this knowledge type. |
search | if not NULL, only display items having the summary or detail contain the string. Must be either NULL or a non empty string. |
Definition at line 1065 of file knowledge.c.
References draw_ext_info(), draw_ext_info_format(), hall_of_fame::header, say::item, knowledge_player::item_count, knowledge_player::items, knowledge_get_or_create(), MSG_TYPE_CLIENT_NOTICE, MSG_TYPE_MISC, knowledge_type::name, NDI_UNIQUE, stringbuffer_finish(), stringbuffer_new(), and knowledge_type::type.
Referenced by command_knowledge(), and knowledge_display().
|
static |
Find a knowledge handler from its type.
type | internal type name. |
Definition at line 864 of file knowledge.c.
References knowledges, and is_valid_types_gen::type.
Referenced by knowledge_give(), knowledge_player_knows(), and knowledge_read_player_data().
void knowledge_first_player_save | ( | player * | pl | ) |
Ensure the knowledge state is correctly saved for the player. This function should only be called once, when the player's save directory is created. All other knowledge functions save the state automatically, but save can only happen when the player directory exists.
pl | who to save the state for. |
Definition at line 1420 of file knowledge.c.
References knowledge_global, knowledge_write_player_data(), obj::name, knowledge_player::next, pl::ob, and knowledge_player::player_name.
Referenced by save_player().
|
static |
Find or create the knowledge store for a player. Will load data if required. fatal() will be called if memory failure.
pl | who to find data for. |
Definition at line 965 of file knowledge.c.
References add_refcount(), fatal(), knowledge_global, knowledge_read_player_data(), obj::name, knowledge_player::next, socket_struct::notifications, pl::ob, OUT_OF_MEMORY, knowledge_player::player_name, knowledge_player::sent_up_to, and pl::socket.
Referenced by knowledge_do_attempt(), knowledge_do_display(), knowledge_give(), knowledge_item_can_be_used_alchemy(), knowledge_player_knows(), knowledge_send_known(), and knowledge_show().
Give a knowledge item from its code.
pl | who to give the knowldge to. |
marker | knowledge's code. |
book | optional item containing the knowledge code. |
Definition at line 993 of file knowledge.c.
References replace::current, draw_ext_info_format(), fatal(), pl::has_directory, knowledge_find(), knowledge_get_or_create(), knowledge_write_player_data(), llevError, LOG(), MSG_TYPE_CLIENT_NOTICE, MSG_TYPE_MISC, obj::name, NDI_UNIQUE, pl::ob, OUT_OF_MEMORY, TAG_END, TAG_START, and is_valid_types_gen::type.
Referenced by cfapi_player_knowledge(), and knowledge_read().
|
static |
Add god information to the player's knowledge, handling the multiple monster case.
current | where to add the information to. |
item | god to add, a dot separating the exact knowledge. |
type | pointer of the monster type. |
Definition at line 725 of file knowledge.c.
References buf, reputation_trigger_connect::check(), replace::current, free_string(), knowledge_add(), llevError, LOG(), rotate-tower::result, strdup_local, stringbuffer_append_printf(), stringbuffer_finish_shared(), stringbuffer_new(), and is_valid_types_gen::type.
|
static |
Describe in detail a god.
item | knowledge item for the god (object name and what is known). |
buf | where to put the description. |
Definition at line 674 of file knowledge.c.
References buf, archt::clone, describe_god(), find_archetype_by_object_name(), llevError, LOG(), and strdup_local.
Get the face for a god.
code | god's code. |
Definition at line 771 of file knowledge.c.
References buf, archt::clone, obj::face, MAX_BUF, tolower, and try_find_archetype().
|
static |
God information summary.
item | knowledge item. |
buf | where to put the information. |
Definition at line 658 of file knowledge.c.
References buf, strdup_local, and stringbuffer_append_printf().
|
static |
Check if a god knowledge item is still valid.
item | monster to check |
Definition at line 703 of file knowledge.c.
References find_archetype_by_object_name(), llevError, LOG(), and strdup_local.
Displays known alchemy recipes an item can be used in.
op | who to display recipes for. |
item | what to check formulae for. |
Definition at line 1331 of file knowledge.c.
References buf, draw_ext_info(), knowledge_item::handler, knowledge_item::item, knowledge_player::item_count, knowledge_player::items, knowledge_get_or_create(), MAX_BUF, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_EXAMINE, give::name, NDI_UNIQUE, give::op, PLAYER, rotate-tower::result, stringbuffer_append_string(), stringbuffer_finish(), and knowledge_type::use_alchemy.
Referenced by examine().
|
static |
Check whether a player already knows a knowledge item or not.
current | player to check. |
item | what to check for. |
kt | the knowledge type for item. |
Definition at line 529 of file knowledge.c.
References replace::current, and knowledge_type::type.
Referenced by knowledge_add(), and knowledge_player_knows().
|
static |
Give the full description of a message.
value | message internal value. |
buf | where to store the detail. |
Definition at line 815 of file knowledge.c.
References buf, get_message_body(), get_message_from_identifier(), navar-midane_pickup::msg, stringbuffer_append_printf(), and autojail::value.
Get the face for a message.
code | message's code. |
Definition at line 839 of file knowledge.c.
References get_message_face(), get_message_from_identifier(), and navar-midane_pickup::msg.
|
static |
Give the title of a message.
value | message internal value. |
buf | where to put the information. |
Definition at line 799 of file knowledge.c.
References buf, get_message_from_identifier(), get_message_title(), navar-midane_pickup::msg, stringbuffer_append_printf(), and autojail::value.
|
static |
Check if a message is still valid.
item | what to check for |
Definition at line 830 of file knowledge.c.
References get_message_from_identifier().
|
static |
Add monster information to the player's knowledge, handling the multiple monster case.
current | where to add the information to. |
item | monster to add, can be separated by dots for multiple ones. |
type | pointer of the monster type. |
Definition at line 621 of file knowledge.c.
References replace::current, knowledge_add(), strdup_local, and is_valid_types_gen::type.
|
static |
Describe in detail a monster.
item | knowledge item for the monster (archetype name). |
buf | where to put the description. |
Definition at line 592 of file knowledge.c.
References buf, archt::clone, describe_item(), obj::name, stringbuffer_append_printf(), and try_find_archetype().
Get the face for a monster.
code | monster's code. |
Definition at line 643 of file knowledge.c.
References blank_face, archt::clone, obj::face, and try_find_archetype().
|
static |
Monster information summary.
item | knowledge item. |
buf | where to put the information. |
Definition at line 578 of file knowledge.c.
References buf, archt::clone, obj::name, stringbuffer_append_printf(), and try_find_archetype().
|
static |
Check if a monster knowledge item is still valid.
item | monster to check |
Definition at line 607 of file knowledge.c.
References archt::clone, archt::head, MONSTER_EXCLUDE_FROM_READABLE_KEY, object_get_value(), try_find_archetype(), and obj::type.
int knowledge_player_knows | ( | const player * | pl, |
const char * | knowledge | ||
) |
Determines whether a player knows a specific knowledge or not.
pl | who to check knowledge for. |
knowledge | what to check for, in format "type:(type specific value)". |
Definition at line 1295 of file knowledge.c.
References replace::current, giveknowledge::knowledge, knowledge_find(), knowledge_get_or_create(), knowledge_known(), llevError, LOG(), MAX_BUF, strlcpy(), and is_valid_types_gen::type.
Referenced by cfapi_player_knowledge().
void knowledge_process_incremental | ( | void | ) |
Incrementally send knowledge information to players, and remove information for players who left.
Definition at line 1436 of file knowledge.c.
References buf, esrv_send_face(), socket_struct::faces_sent, first_player, free_knowledge_player(), say::item, knowledge_player::item_count, knowledge_player::items, knowledge_global, MIN, obj::name, pl::next, knowledge_player::next, NS_FACESENT_FACE, Face::number, pl::ob, altar_valkyrie::pl, knowledge_player::player_name, Send_With_Handling(), knowledge_player::sent_up_to, pl::socket, SockList_AddInt(), SockList_AddLen16Data(), SockList_AddString(), SockList_Avail(), SockList_Init(), SockList_Reset(), SockList_Term(), stringbuffer_finish(), stringbuffer_new(), and try_find_face().
Referenced by do_specials().
Player is reading a book, give knowledge if needed, warn player, and such.
pl | who is reading. |
book | what is read. |
Definition at line 1053 of file knowledge.c.
References knowledge_give(), object_get_value(), and sstring.
Referenced by apply_sign(), and book_type_apply().
|
static |
Read all knowledge data for a player from disk, discarding invalid data.
kp | player structure to load data into. |
Definition at line 910 of file knowledge.c.
References add_string(), mad_mage_user::file, say::item, knowledge_player::item_allocated, knowledge_player::item_count, knowledge_player::items, knowledge_find(), llevDebug, llevError, Settings::localdir, LOG(), MAX_BUF, knowledge_player::player_name, Settings::playerdir, settings, and is_valid_types_gen::type.
Referenced by knowledge_get_or_create().
void knowledge_send_info | ( | socket_struct * | ns | ) |
Send the reply_info for 'knowledge_info'.
ns | socket to send information to. |
Definition at line 1371 of file knowledge.c.
References knowledge_type::attempt_alchemy, esrv_send_face(), socket_struct::faces_sent, knowledges, give::name, NS_FACESENT_FACE, Face::number, Send_With_Handling(), SockList_AddPrintf(), SockList_AddString(), SockList_Init(), SockList_Term(), try_find_face(), is_valid_types_gen::type, and knowledge_type::type.
Referenced by request_info_cmd().
void knowledge_send_known | ( | player * | pl | ) |
Send initial known knowledge to player, if requested.
pl | who to send knowledge for. |
Definition at line 1400 of file knowledge.c.
References knowledge_get_or_create(), socket_struct::notifications, knowledge_player::sent_up_to, and pl::socket.
Referenced by check_login().
|
static |
Show the details of a knowledge item.
pl | who is asking for details. |
params | additional parameters, should contain the knowledge item number. |
Definition at line 1160 of file knowledge.c.
References buf, disinfect::count, draw_ext_info(), say::item, knowledge_player::item_count, knowledge_player::items, knowledge_get_or_create(), MSG_TYPE_CLIENT_NOTICE, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, MSG_TYPE_MISC, NDI_UNIQUE, roll-o-matic::params, stringbuffer_finish(), and stringbuffer_new().
Referenced by command_knowledge().
|
static |
Store all knowledge data for a player to disk.
kp | player data to store. |
Definition at line 879 of file knowledge.c.
References draw_ext_info(), mad_mage_user::file, say::item, knowledge_player::item_count, knowledge_player::items, Settings::localdir, MAX_BUF, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE, NDI_ALL_DMS, NDI_UNIQUE, of_close(), of_open(), knowledge_player::player_name, Settings::playerdir, and settings.
Referenced by knowledge_first_player_save(), and knowledge_give().
Get the face for a knowledge item.
code | knowledge internal code. |
Definition at line 120 of file knowledge.c.
|
static |
All known loaded knowledge for a player.
Definition at line 164 of file knowledge.c.
Referenced by free_knowledge(), knowledge_first_player_save(), knowledge_get_or_create(), and knowledge_process_incremental().
|
static |
All handled knowledge items.
Definition at line 850 of file knowledge.c.
Referenced by knowledge_display(), knowledge_find(), and knowledge_send_info().