Crossfire Server, Trunk  R20513
Macros | Functions
utils.c File Reference

General convenience functions for crossfire. More...

#include "global.h"
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "sproto.h"
Include dependency graph for utils.c:

Go to the source code of this file.

Macros

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

Functions

int adjust_dir (int dir, int destination_dir)
 Adjusts a given direction by +/-1 towards a destination direction. More...
 
void decay_objects (mapstruct *m)
 Decay and destroy persihable items in a map. More...
 
StringBufferdescribe_attacktype (const char *attack, int value, StringBuffer *buf)
 Describe the specified attack type. More...
 
StringBufferdescribe_spellpath_attenuation (const char *attenuation, int value, StringBuffer *buf)
 Describe the specified path attenuation. More...
 
int die_roll (int num, int size, const object *op, int goodbad)
 Roll a number of dice (2d3, 4d6). More...
 
void fatal (enum fatal_error err)
 fatal() is meant to be called whenever a fatal signal is intercepted. More...
 
int get_random_dir (void)
 Returns a random direction (1..8). More...
 
int get_randomized_dir (int dir)
 Returns a random direction (1..8) similar to a given direction. More...
 
int isqrt (int n)
 Compute the square root. More...
 
void make_list_like (char *input)
 Taking a string as an argument, mutate it into a string that looks like a list. More...
 
materialtype_tname_to_material (const char *name)
 Convert materialname to materialtype_t. More...
 
int random_roll (int min, int max, const object *op, int goodbad)
 Roll a random number between min and max. More...
 
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. More...
 
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. More...
 
void replace_unprintable_chars (char *buf)
 Replaces any unprintable character in the given buffer with a space. More...
 
int rndm (int min, int max)
 Returns a number between min and max. More...
 
void set_materialname (object *op)
 Set the material name and type for an item, if not set. More...
 
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. More...
 
void strip_endline (char *buf)
 Removes endline from buffer (modified in place). More...
 
const char * strrstr (const char *haystack, const char *needle)
 Finds a string in a string. More...
 
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. More...
 

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.c.

Macro Definition Documentation

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

Definition at line 335 of file utils.c.

Referenced by strip_endline().

Function Documentation

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 452 of file utils.c.

References absdir(), 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:

void decay_objects ( mapstruct m)

Decay and destroy persihable items in a map.

Definition at line 174 of file utils.c.

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_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(), QUERY_FLAG, rndm(), and mapdef::unique.

Referenced by ready_map_name().

Here is the call graph for this function:

Here is the caller graph for this function:

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 561 of file utils.c.

References attacks, NROFATTACKS, stringbuffer_append_printf(), stringbuffer_append_string(), and stringbuffer_new().

Referenced by describe_item(), and describe_monster().

Here is the call graph for this function:

Here is the caller graph for this function:

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 533 of file utils.c.

References NRSPELLPATHS, spellpathnames, stringbuffer_append_printf(), stringbuffer_append_string(), and stringbuffer_new().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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 121 of file utils.c.

References llevError, LOG(), liv::luck, MAX, MIN, PLAYER, RANDOM, obj::stats, and obj::type.

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:

void fatal ( enum fatal_error  err)
int get_random_dir ( void  )

Returns a random direction (1..8).

Returns
the random direction.

Definition at line 426 of file utils.c.

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:

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 438 of file utils.c.

References absdir(), and RANDOM.

Referenced by 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:

int isqrt ( int  n)

Compute the square root.

This is now a wrapper around sqrt().

Definition at line 585 of file utils.c.

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

Here is the caller graph for this function:

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 394 of file utils.c.

References MAX_BUF.

Referenced by can_pay(), and shop_describe().

Here is the caller graph for this function:

materialtype_t* name_to_material ( const char *  name)

Convert materialname to materialtype_t.

Todo:
why use a break?

Definition at line 247 of file utils.c.

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

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

Here is the caller graph for this function:

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.c.

References llevError, LOG(), liv::luck, MAX, MIN, PLAYER, RANDOM, obj::stats, and obj::type.

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(), spellbook_type_apply(), trap_disarm(), trap_see(), use_oratory(), and write_scroll().

Here is the call graph for this function:

Here is the caller graph for this function:

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.c.

References FMT64, llevError, LOG(), liv::luck, MAX, MIN, PLAYER, RANDOM, obj::stats, and obj::type.

Referenced by pray_at_altar().

Here is the call graph for this function:

Here is the caller graph for this function:

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 353 of file utils.c.

References snprintf.

Referenced by compilePython(), enter_fixed_template_map(), enter_random_template_map(), and START_TEST().

Here is the caller graph for this function:

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 473 of file utils.c.

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

Here is the caller graph for this function:

int rndm ( int  min,
int  max 
)
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 300 of file utils.c.

References add_string(), _materialtype::material, obj::material, obj::materialname, materialt, _materialtype::name, and _materialtype::next.

Referenced by fix_generated_item(), and local_check_loaded_object().

Here is the call graph for this function:

Here is the caller graph for this function:

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 499 of file utils.c.

Referenced by account_char_load(), accounts_load(), check_split_string(), get_score(), read_client_images(), read_map_log(), and spell_consume_items().

Here is the caller graph for this function:

void strip_endline ( char *  buf)

Removes endline from buffer (modified in place).

Definition at line 340 of file utils.c.

References EOL_SIZE.

Referenced by parse_shop_string(), and send_news().

Here is the caller graph for this function:

const char* strrstr ( const char *  haystack,
const char *  needle 
)

Finds a string in a string.

Todo:
isn't there another function (porting.c?) for that?

Definition at line 324 of file utils.c.

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 266 of file utils.c.

References obj::arch, CLOAK, GIRDLE, GLOVES, IS_ARMOR, IS_SHIELD, llevError, LOG(), obj::materialname, _materialtype::mod, obj::name, archt::name, name_to_material(), NROFATTACKS, obj::resist, and obj::type.

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: