Crossfire Server, Trunk
utils.cpp File Reference
#include "global.h"
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "sproto.h"
+ Include dependency graph for utils.cpp:

Go to the source code of this file.

Macros

#define EOL_SIZE   (sizeof("\n")-1)
 

Functions

int adjust_dir (int dir, int destination_dir)
 
void decay_objects (mapstruct *m)
 
StringBufferdescribe_attacktype (const char *attack, int value, StringBuffer *buf)
 
StringBufferdescribe_spellpath_attenuation (const char *attenuation, int value, StringBuffer *buf)
 
int die_roll (int num, int size, const object *op, int goodbad)
 
void fatal (enum fatal_error err)
 
void free_charlinks (linked_char *lc)
 
int get_random_dir (void)
 
int get_randomized_dir (int dir)
 
int isqrt (int n)
 
void make_list_like (char *input)
 
materialtype_tname_to_material (const char *name)
 
int random_roll (int min, int max, const object *op, int goodbad)
 
int64_t random_roll64 (int64_t min, int64_t max, const object *op, int goodbad)
 
void replace (const char *src, const char *key, const char *replacement, char *result, size_t resultsize)
 
void replace_unprintable_chars (char *buf)
 
int rndm (int min, int max)
 
void set_materialname (object *op)
 
size_t split_string (char *str, char *array[], size_t array_size, char sep)
 
void strip_endline (char *buf)
 
void transmute_materialname (object *op, const object *change)
 

Detailed Description

General convenience functions for crossfire.

The random functions here take luck into account when rolling random dice or numbers. This function has less of an impact the larger the difference becomes in the random numbers. IE, the effect is lessened on a 1-1000 roll, vs a 1-6 roll. This can be used by crafty programmers, to specifically disable luck in certain rolls, simply by making the numbers larger (ie, 1d1000 > 500 vs 1d6 > 3)

Definition in file utils.cpp.

Macro Definition Documentation

◆ EOL_SIZE

#define EOL_SIZE   (sizeof("\n")-1)

Definition at line 319 of file utils.cpp.

Function Documentation

◆ adjust_dir()

int adjust_dir ( int  dir,
int  destination_dir 
)

Adjusts a given direction by +/-1 towards a destination direction.

Parameters
dirthe direction to adjust
destination_dirthe destination direction to adjust towards
Returns
the adjusted direction

Definition at line 436 of file utils.cpp.

References absdir(), destination_dir, and rndm().

Referenced by move_ball_spell(), and move_missile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ decay_objects()

void decay_objects ( mapstruct m)

Decay and destroy persihable items in a map

Definition at line 175 of file utils.cpp.

References CLOAK, FLAG_IS_FLOOR, FLAG_OBJ_ORIGINAL, FLAG_OVERLAY_FLOOR, FLAG_UNIQUE, FLAG_UNPAID, FOOD, FOR_MAP_FINISH, FOR_MAP_PREPARE, GIRDLE, GLOVES, IS_ARMOR, IS_LIVE, IS_SHIELD, IS_WEAPON, m, M_ADAMANT, M_BONE, M_CLOTH, M_GLASS, M_ICE, M_IRON, M_LEATHER, M_LIQUID, M_ORGANIC, M_PAPER, M_SOFT_METAL, M_STONE, M_WOOD, MAP_HEIGHT, MAP_WIDTH, object_free_drop_inventory(), object_remove(), give::op, QUERY_FLAG, rndm(), diamondslots::x, and diamondslots::y.

Referenced by ready_map_name().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ describe_attacktype()

StringBuffer* describe_attacktype ( const char *  attack,
int  value,
StringBuffer buf 
)

Describe the specified attack type.

Parameters
attackstring describing the attack ("Clawing", and such).
valueattack type to describe.
bufwhere to describe, can be NULL.
Returns
buf, newly allocated StringBuffer the caller should free if buf was NULL.

Definition at line 545 of file utils.cpp.

References attacks, buf, NROFATTACKS, stringbuffer_append_printf(), stringbuffer_append_string(), stringbuffer_new(), and autojail::value.

Referenced by describe_item(), and describe_monster().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ describe_spellpath_attenuation()

StringBuffer* describe_spellpath_attenuation ( const char *  attenuation,
int  value,
StringBuffer buf 
)

Describe the specified path attenuation.

Parameters
attenuationstring describing if "Attenued", "Denied", "Repelled".
valuepath value to describe.
bufwhere to describe, can be NULL.
Returns
buf, newly allocated StringBuffer the caller should free if buf was NULL.

Definition at line 517 of file utils.cpp.

References buf, NRSPELLPATHS, spellpathnames, stringbuffer_append_printf(), stringbuffer_append_string(), stringbuffer_new(), and autojail::value.

Referenced by describe_god(), describe_item(), describe_monster(), ring_desc(), and stringbuffer_append_spelldesc().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ die_roll()

int die_roll ( int  num,
int  size,
const object op,
int  goodbad 
)

Roll a number of dice (2d3, 4d6). Uses op to determine luck, If goodbad is non-zero, luck increases the roll, if zero, it decreases. Generally, op should be the player/caster/hitter requesting the roll, not the recipient (ie, the poor slob getting hit). The args are num D size (ie 4d6) [garbled 20010916]

Definition at line 122 of file utils.cpp.

References llevError, LOG(), MAX, MIN, give::op, PLAYER, RANDOM, and mail_login::total.

Referenced by attempt_hide(), attempt_pick_lock(), attempt_steal(), fire_swarm(), identify_object_with_skill(), pets_summon_object(), pick_arrow_target(), player_lvl_adj(), scroll_type_apply(), and spellbook_type_apply().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fatal()

void fatal ( enum fatal_error  err)

fatal() is meant to be called whenever a fatal signal is intercepted. It will call the emergency_save and the clean_tmp_files functions.

Note
this function never returns, as it calls exit().

Definition at line 580 of file utils.cpp.

References clean_tmp_files(), emergency_save(), Settings::fatal_hook, logfile, and settings.

Referenced by account_alloc(), account_char_load(), add_to_tar(), allocate_map(), assets_pack(), bufferreader_create(), bufferreader_init_for_length(), build_stringlist(), check_generators(), check_recipes(), do_each_skill(), do_module(), ensure_available_characters(), expand_objects(), get_archetype_struct(), get_client_spell_state(), get_empty_artifact(), get_empty_artifactlist(), get_empty_book(), get_empty_booklist(), get_empty_formula(), get_empty_mat(), get_empty_treasure(), get_linked_map(), get_new_quest_state(), get_objectlink(), get_objectlinkpt(), get_or_create_quest(), get_or_create_state(), get_player(), get_region_struct(), i18n_init(), init_dynamic(), init_experience(), init_formulae(), init_library(), init_listening_socket(), init_recipelist(), init_regions(), init_server(), knowledge_get_or_create(), knowledge_give(), PngLoader::load(), MessageLoader::load(), TreasureLoader::loadTreasure(), minheap_init(), monster_compute_path(), new_connection(), new_shared_string(), object_new(), parse_dialog_information(), parse_shop_string(), player_get_delayed_buffer(), quest_create_condition(), quest_create_step(), re_init(), set_player_socket(), shop_inventory_type_apply(), SockList_AddPrintf(), SockList_Ensure(), stringbuffer_ensure(), and stringbuffer_new().

+ Here is the call graph for this function:

◆ free_charlinks()

void free_charlinks ( linked_char lc)

Frees a link structure and its next items.

Parameters
lcitem to free. Pointer is free()d too, so becomes invalid.

Definition at line 606 of file utils.cpp.

References free_charlinks(), and linked_char::next.

Referenced by free_charlinks(), and send_account_players().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_random_dir()

int get_random_dir ( void  )

Returns a random direction (1..8).

Returns
the random direction.

Definition at line 410 of file utils.cpp.

References rndm().

Referenced by move_firewall(), move_player_mover(), move_swarm_spell(), and pets_move().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_randomized_dir()

int get_randomized_dir ( int  dir)

Returns a random direction (1..8) similar to a given direction.

Parameters
dirthe exact direction
Returns
the randomized direction

Definition at line 422 of file utils.cpp.

References absdir(), and RANDOM.

Referenced by face_player(), monster_cast_spell(), monster_move(), monster_use_bow(), monster_use_range(), monster_use_scroll(), monster_use_skill(), and move_player().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isqrt()

int isqrt ( int  n)

Compute the square root. This is now a wrapper around sqrt().

Definition at line 569 of file utils.cpp.

Referenced by expand_lighted_sight(), fix_flesh_item(), get_rangevector(), get_rangevector_from_mapcoord(), monster_stand_in_light(), pick_lock(), play_sound_map(), spell_failure(), and value_limit().

+ Here is the caller graph for this function:

◆ make_list_like()

void make_list_like ( char *  input)

Taking a string as an argument, mutate it into a string that looks like a list.

A 'list' for the purposes here is a string of items, seperated by commas, except for the last entry, which has an 'and' before it, and a full stop (period) after it.

This function will also strip all trailing non alphanumeric characters.

It does not insert an oxford comma.

Parameters
inputstring to transform. Will be overwritten. Must be long enough to contain the modified string.
Todo:
use safe string functions.

Definition at line 378 of file utils.cpp.

References MAX_BUF, and Ice::tmp.

Referenced by can_pay(), and shop_describe().

+ Here is the caller graph for this function:

◆ name_to_material()

materialtype_t* name_to_material ( const char *  name)

Convert materialname to materialtype_t

Todo:
why use a break?

Definition at line 248 of file utils.cpp.

References materialt, give::name, _materialtype::name, and _materialtype::next.

Referenced by animate_weapon(), object_get_materialtype(), and transmute_materialname().

+ Here is the caller graph for this function:

◆ random_roll()

int random_roll ( int  min,
int  max,
const object op,
int  goodbad 
)

Roll a random number between min and max. Uses op to determine luck, and if goodbad is non-zero, luck increases the roll, if zero, it decreases.

Generally, op should be the player/caster/hitter requesting the roll, not the recipient (ie, the poor slob getting hit). [garbled 20010916]

Definition at line 42 of file utils.cpp.

References llevError, LOG(), MAX, say::max, MIN, give::op, PLAYER, and RANDOM.

Referenced by alchemy_failure_effect(), apply_check_personalized_blessings(), attack_ob_simple(), attempt_do_alchemy(), attempt_steal(), become_follower(), cast_detection(), cast_heal(), cast_smite_spell(), cast_spell(), charge_mana_effect(), cure_disease(), deathstrike_living(), did_make_save(), do_harvest(), do_hidden_move(), do_symptoms(), flee_player(), gate_type_process(), god_examines_priest(), god_intervention(), hit_with_one_attacktype(), infect_object(), kill_player_not_permadeath(), learn_skill(), mood_change(), pets_summon_object(), potion_type_apply(), pray_at_altar(), push_ob(), recharge(), roll_ob(), scroll_failure(), singing(), slow_living(), spellbook_type_apply(), trap_disarm(), trap_see(), try_leave_cult(), use_oratory(), and write_scroll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ random_roll64()

int64_t random_roll64 ( int64_t  min,
int64_t  max,
const object op,
int  goodbad 
)

This is a 64 bit version of random_roll() above. This is needed for exp loss calculations for players changing religions.

Definition at line 77 of file utils.cpp.

References FMT64, make_face_from_files::int, llevError, LOG(), MAX, say::max, MIN, give::op, PLAYER, and RANDOM.

Referenced by try_leave_cult().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ replace()

void replace ( const char *  src,
const char *  key,
const char *  replacement,
char *  result,
size_t  resultsize 
)

Replace in string src all occurrences of key by replacement. The resulting string is put into result; at most resultsize characters (including the terminating null character) will be written to result.

Definition at line 337 of file utils.cpp.

References castle_read::key, rotate-tower::result, and strlcpy().

Referenced by compilePython(), enter_fixed_template_map(), enter_random_template_map(), nlohmann::basic_json::patch(), and START_TEST().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ replace_unprintable_chars()

void replace_unprintable_chars ( char *  buf)

Replaces any unprintable character in the given buffer with a space.

Parameters
bufthe buffer to modify

Definition at line 457 of file utils.cpp.

References buf.

Referenced by party_form(), party_set_password(), and player_set_own_title().

+ Here is the caller graph for this function:

◆ rndm()

int rndm ( int  min,
int  max 
)

Returns a number between min and max.

It is suggested one use these functions rather than RANDOM()%, as it would appear that a number of off-by-one-errors exist due to improper use of %. This should also prevent SIGFPE.

Definition at line 162 of file utils.cpp.

References say::max, and RANDOM.

Referenced by adjust_dir(), adjust_product(), alchemy_failure_effect(), alchemy_object(), apply_changes_to_player(), attack_message(), attack_ob_simple(), cancellation(), cast_change_ability(), cast_spell(), cast_wonder(), cauldron_sound(), check_spell_knockback(), choose_cult_monster(), command_cointoss(), common_process_projectile(), common_trap_type_process(), compute_price_variation_with_bargaining(), convert_item(), counterspell(), decay_objects(), deep_swamp_type_process(), determine_holy_arch(), did_make_save_item(), do_harvest(), find_recipe(), fix_generated_item(), fix_stopped_arrow(), forklightning(), generate_animation(), generate_condition(), generate_face(), generate_monster(), generate_monster_arch(), generate_monster_inv(), generate_name(), generate_quest(), generate_random_list(), generate_random_treasure(), generate_step(), get_random_dir(), hit_player(), hit_with_one_attacktype(), identify_object_with_skill(), level_for_item(), FaceMakerDialog::makeFaces(), move_ball_spell(), move_bolt(), move_creator(), move_swarm_spell(), peacemaker_type_process(), polymorph(), polymorph_item(), polymorph_living(), races_get_random_monster(), reflwall(), resurrection_fails(), save_throw_object(), scroll_type_apply(), shuffle_attack(), spellbook_type_apply(), swap_map(), swap_random_stats(), transmute_item_to_flower(), and trap_adjust().

◆ set_materialname()

void set_materialname ( object op)

Set the material name and type for an item, if not set.

Parameters
opitem to set the material for.

Definition at line 301 of file utils.cpp.

References add_string(), _materialtype::material, materialt, _materialtype::name, _materialtype::next, and give::op.

Referenced by check_loaded_object(), fix_generated_item(), and local_check_loaded_object().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ split_string()

size_t split_string ( char *  str,
char *  array[],
size_t  array_size,
char  sep 
)

Splits a string delimited by passed in sep value into characters into an array of strings.

Parameters
strthe string to be split; will be modified
arraythe string array; will be filled with pointers into str
array_sizethe number of elements in array; if str contains more fields excess fields are not split but included into the last element
sepseperator to use.
Returns
the number of elements found; always less or equal to array_size

Definition at line 483 of file utils.cpp.

References make_face_from_files::str.

Referenced by account_char_load_from_file(), accounts_load(), assets_pack(), check_split_string(), get_score(), FacesetLoader::load(), PngLoader::load(), load_bells(), load_citylife(), map_info(), read_map_log(), and spell_consume_items().

+ Here is the caller graph for this function:

◆ strip_endline()

void strip_endline ( char *  buf)

Removes endline from buffer (modified in place).

Definition at line 324 of file utils.cpp.

References buf, and EOL_SIZE.

Referenced by parse_shop_string(), and send_news().

+ Here is the caller graph for this function:

◆ transmute_materialname()

void transmute_materialname ( object op,
const object change 
)

When doing transmutation of objects, we have to recheck the resistances, as some that did not apply previously, may apply now.

Only works on armors.

Definition at line 267 of file utils.cpp.

References CLOAK, GIRDLE, GLOVES, IS_ARMOR, IS_SHIELD, llevError, LOG(), obj::materialname, _materialtype::mod, name_to_material(), NROFATTACKS, give::op, and obj::resist.

Referenced by cfapi_object_transmute(), and make_item_from_recipe().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: