42 int random_roll(
int min,
int max,
const object *op,
int goodbad) {
43 int omin, diff, luck, base, ran;
47 base = (diff > 2) ? 20 : 50;
49 if (max < 1 || diff < 1) {
50 LOG(
llevError,
"Calling random_roll with min=%d max=%d\n", min, max);
57 return((ran%diff)+min);
60 if (
RANDOM()%base < (
unsigned int)
MIN(10, abs(luck))) {
62 ((luck > 0) ? (luck = 1) : (luck = -1));
66 ((goodbad) ? (min += luck) : (diff));
68 return(
MAX(omin,
MIN(max, (ran%diff)+min)));
70 return((ran%diff)+min);
77 int64_t
random_roll64(int64_t min, int64_t max,
const object *op,
int goodbad) {
78 int64_t omin, diff, ran;
84 base = (diff > 2) ? 20 : 50;
86 if (max < 1 || diff < 1) {
99 return((ran%diff)+min);
102 if (
RANDOM()%base < (
unsigned int)
MIN(10, abs(luck))) {
104 ((luck > 0) ? (luck = 1) : (luck = -1));
108 ((goodbad) ? (min += luck) : (diff));
110 return (
MAX(omin,
MIN(max, (ran%diff)+min)));
112 return ((ran%diff)+min);
122 int die_roll(
int num,
int size,
const object *op,
int goodbad) {
123 int min, diff, luck, total, i, gotlucky, base, ran;
127 luck = total = gotlucky = 0;
128 base = (diff > 2) ? 20 : 50;
129 if (size < 2 || diff < 1) {
130 LOG(
llevError,
"Calling die_roll with num=%d size=%d\n", num, size);
137 for (i = 0; i < num; i++) {
138 if (
RANDOM()%base < (
unsigned int)
MIN(10, abs(luck)) && !gotlucky) {
141 ((luck > 0) ? (luck = 1) : (luck = -1));
145 ((goodbad) ? (min += luck) : (diff));
147 total +=
MAX(1,
MIN(size, (ran%diff)+min));
166 if (max < 1 || diff < 1)
169 return (
RANDOM()%diff+min);
200 if (op->stats.dam < 0)
206 || op->type ==
CLOAK) {
208 if (op->stats.ac < 0)
210 }
else if (op->type ==
FOOD) {
211 op->stats.food -=
rndm(5, 20);
212 if (op->stats.food < 0)
231 if (op->material&
M_ICE &&
rndm(0, 100) > 70)
235 if (destroy &&
rndm(0, 1)) {
250 if (strcmp(name, material->name) == 0) {
302 if (op->
material & material->material) {
309 #define EOL_SIZE (sizeof("\n")-1) 315 if (strlen(buf) <
sizeof(
"\n")) {
318 if (!strcmp(buf+strlen(buf)-
EOL_SIZE,
"\n"))
327 void replace(
const char *src,
const char *key,
const char *replacement,
char *result,
size_t resultsize) {
332 if (strcmp(key, replacement) == 0) {
333 strlcpy(result, src, resultsize);
337 keylen = strlen(key);
340 while (*src !=
'\0' && resultlen+1 < resultsize) {
341 if (strncmp(src, key, keylen) == 0) {
342 snprintf(result+resultlen, resultsize-resultlen,
"%s", replacement);
343 resultlen += strlen(result+resultlen);
346 result[resultlen++] = *src++;
349 result[resultlen] =
'\0';
371 if (!input || strlen(input) >
MAX_BUF-5)
375 strncpy(tmp, input,
MAX_BUF-5);
377 for (i = strlen(tmp); i >= 0 && !isalnum(tmp[i]); i--) {
382 p = strrchr(tmp,
',');
387 strcat(input,
" and");
429 diff = (destination_dir-dir)&7;
430 if (1 <= diff && diff <= 3)
432 else if (5 <= diff && diff <= 7)
434 else if (
rndm(0, 1) == 0)
450 for (p = buf; *p !=
'\0'; p++) {
473 size_t split_string(
char *str,
char *array[],
size_t array_size,
char sep) {
487 while (pos < array_size) {
489 while (*p !=
'\0' && *p != sep)
491 if (pos >= array_size)
580 return ((sqrt(2) - 1) * a) + b;
591 const char *fatalmsgs[] = {
592 "Failed to allocate memory",
593 "Failed repeatedly to load maps",
594 "Hashtable for archetypes is too small",
595 "Fatal issue in archetype file",
603 fprintf(
logfile,
"Fatal error: %s\n", fatalmsgs[err]);
606 fprintf(
logfile,
"Exiting...\n");
std::vector< materialtype_t * > materials
void clean_tmp_files(void)
Save unique maps and clean up temporary map files unless recycling temporary maps.
fatalHook fatal_hook
If not NULL then called when fatal() is called.
#define FLAG_UNPAID
Object hasn't been paid for yet.
struct archetype * arch
Pointer to archetype.
FILE * logfile
Used by server/daemon.c.
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
int rndm(int min, int max)
Returns a number between min and max.
materialtype_t * name_to_material(const char *name)
Convert materialname to materialtype_t.
#define FLAG_IS_FLOOR
Can't see what's underneath this object.
int get_random_dir(void)
Returns a random direction (1..8).
void object_remove(object *op)
This function removes the object op from the linked list of objects which it is currently tied to...
void set_materialname(object *op)
Set the material name and type for an item, if not set.
sstring add_string(const char *str)
Share a string.
int8_t luck
Affects thaco and ac from time to time.
#define MAP_HEIGHT(m)
Map height.
#define M_ADAMANT
Adamant.
uint16_t material
What materials this object consist of.
StringBuffer * stringbuffer_new(void)
Create a new string buffer.
int ihypot(int a, int b)
Rough estimate of hypot(a, b).
Global type definitions and header inclusions.
StringBuffer * describe_spellpath_attenuation(const char *attenuation, int value, StringBuffer *buf)
Describe the specified path attenuation.
StringBuffer * describe_attacktype(const char *attack, int value, StringBuffer *buf)
Describe the specified attack type.
#define M_SOFT_METAL
Soft metal.
const char *const spellpathnames[NRSPELLPATHS]
Perhaps not the best place for this, but needs to be in some file in the common area so that standalo...
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...
int16_t resist[NROFATTACKS]
Resistance adjustments for attacks.
#define FLAG_OBJ_ORIGINAL
NEVER SET THIS.
sstring materialname
Specific material name.
sstring name
More definite name, like "generate_kobold".
uint32_t unique
If set, this is a per player unique map.
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.
const char * destination_dir
Root destination dir.
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Append a formatted string to a string buffer instance.
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Append a string to a string buffer instance.
#define QUERY_FLAG(xyz, p)
struct linked_char * next
void object_free_drop_inventory(object *ob)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
int8_t mod[NROFATTACKS]
Modification to resistances.
int adjust_dir(int dir, int destination_dir)
Adjusts a given direction by +/-1 towards a destination direction.
#define FLAG_OVERLAY_FLOOR
Object is an overlay floor.
struct Settings settings
Global settings.
#define M_ORGANIC
General organic.
int random_roll(int min, int max, const object *op, int goodbad)
Roll a random number between min and max.
int get_randomized_dir(int dir)
Returns a random direction (1..8) similar to a given direction.
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
void emergency_save(int flag)
Save all players.
const char *const attacks[NROFATTACKS]
Attack type names.
int isqrt(int n)
Compute the square root.
living stats
Str, Con, Dex, etc.
#define MAX_BUF
Used for all kinds of things.
#define NRSPELLPATHS
Number of spell paths.
void replace_unprintable_chars(char *buf)
Replaces any unprintable character in the given buffer with a space.
size_t strlcpy(char *dst, const char *src, size_t size)
Portable implementation of strlcpy(3).
#define FOR_MAP_FINISH()
Finishes FOR_MAP_PREPARE().
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.
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 p...
static event_registration m
sstring name
The name of the object, obviously...
#define MAP_WIDTH(m)
Map width.
uint8_t type
PLAYER, BULLET, etc.
void decay_objects(mapstruct *m)
Decay and destroy persihable items in a map.
void strip_endline(char *buf)
Removes endline from buffer (modified in place).
#define M_LEATHER
Leather.
void free_charlinks(linked_char *lc)
Frees a link structure and its next items.
int absdir(int d)
Computes an absolute direction.
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Constructs a loop iterating over all objects of a map tile.
void make_list_like(char *input)
Taking a string as an argument, mutate it into a string that looks like a list.
A buffer that will be expanded as content is added to it.
#define FLAG_UNIQUE
Item is really unique (UNIQUE_ITEMS)
int die_roll(int num, int size, const object *op, int goodbad)
Roll a number of dice (2d3, 4d6).
fatal_error
Fatal variables; used as arguments to fatal().