X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fserver%2Fthings.c;h=30563220a26d4830fded78b757a278aab0e5dc8a;hb=acdaf7a54812da8800a0e7cbaff17672057bdd0a;hp=e73a09a283c09fb843292996c0b6ee3561f68d2f;hpb=edebb2bf9aa780ee2f7006c1d2be9168564d34df;p=plomrogue diff --git a/src/server/things.c b/src/server/things.c index e73a09a..3056322 100644 --- a/src/server/things.c +++ b/src/server/things.c @@ -1,21 +1,26 @@ -/* src/server/things.c */ +/* src/server/things.c + * + * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3 + * or any later version. For details on its copyright, license, and warranties, + * see the file NOTICE in the root directory of the PlomRogue source package. + */ #define _POSIX_C_SOURCE 200809L /* strdup() */ #include "things.h" #include /* NULL, size_t */ #include /* uint8_t, uint16_t, int16_t, UINT8_MAX, UINT16_MAX */ #include /* free() */ -#include /* memset(), strcmp(), strdup() */ +#include /* memset(), strcmp(), strdup(), strlen() */ #include "../common/rexit.h" /* exit_err() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "../common/yx_uint8.h" /* yx_uint8 */ #include "cleanup.h" /* set_cleanup_flag() */ #include "hardcoded_strings.h" /* s */ -#include "map.h" /* is_passable() */ +#include "field_of_view.h" /* build_fov_map() */ +#include "map.h" /* mv_yx_in_dir_legal() */ #include "rrand.h" /* rrand() */ #include "thing_actions.h" /* actor_wait */ #include "world.h" /* world */ -#include "yx_uint8.h" /* yx_uint8_cmp() */ @@ -67,7 +72,7 @@ static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id, } } struct NextAndId ** nai_ptr_ptr = start; - for (; NULL != * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next); + for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next); *nai_ptr_ptr = nai; return nai; } @@ -116,14 +121,31 @@ extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x) t->lifepoints = tt->lifepoints; t->pos.y = y; t->pos.x = x; + if (t->lifepoints && world.exists) + { + build_fov_map(t); + } return t; } +extern void add_thing_to_memory_map(struct Thing * t, uint8_t type, + uint8_t y, uint8_t x) +{ + struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__); + tm->type = type; + tm->pos.y = y; + tm->pos.x = x; + tm->next = t->t_mem; + t->t_mem = tm; +} + + + extern void free_thing_actions(struct ThingAction * ta) { - if (NULL == ta) + if (!ta) { return; } @@ -136,7 +158,7 @@ extern void free_thing_actions(struct ThingAction * ta) extern void free_thing_types(struct ThingType * tt) { - if (NULL == tt) + if (!tt) { return; } @@ -149,7 +171,7 @@ extern void free_thing_types(struct ThingType * tt) extern void free_things(struct Thing * t) { - if (NULL == t) + if (!t) { return; } @@ -157,6 +179,7 @@ extern void free_things(struct Thing * t) free_things(t->next); free(t->fov_map); free(t->mem_map); + free_things_in_memory(t->t_mem); free(t); if (t == world.things) /* So add_things()' NULL-delimited thing */ { /* iteration loop does not iterate over */ @@ -166,10 +189,22 @@ extern void free_things(struct Thing * t) +extern void free_things_in_memory(struct ThingInMemory * tm) +{ + if (!tm) + { + return; + } + free_things_in_memory(tm->next); + free(tm); +} + + + extern struct ThingAction * get_thing_action(uint8_t id) { struct ThingAction * ta = world.thing_actions; - for (; NULL != ta && id != ta->id; ta = ta->next); + for (; ta && id != ta->id; ta = ta->next); return ta; } @@ -178,7 +213,7 @@ extern struct ThingAction * get_thing_action(uint8_t id) extern struct ThingType * get_thing_type(uint8_t id) { struct ThingType * tt = world.thing_types; - for (; NULL != tt && id != tt->id; tt = tt->next); + for (; tt && id != tt->id; tt = tt->next); return tt; } @@ -187,7 +222,7 @@ extern struct ThingType * get_thing_type(uint8_t id) extern uint8_t get_thing_action_id_by_name(char * name) { struct ThingAction * ta = world.thing_actions; - while (NULL != ta) + while (ta) { if (0 == strcmp(ta->name, name)) { @@ -208,14 +243,14 @@ extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep) { while (1) { - if (NULL == ptr || id == ptr->id) + if (!ptr || id == ptr->id) { return ptr; } if (deep) { struct Thing * owned_thing = get_thing(ptr->owns, id, 1); - if (NULL != owned_thing) + if (owned_thing) { return ptr; } @@ -233,6 +268,55 @@ extern struct Thing * get_player() +extern void try_thing_proliferation(struct Thing * t) +{ + struct ThingType * tt = get_thing_type(t->type); + if (tt->proliferate) + { + if (1 == tt->proliferate || 1 == (rrand() % tt->proliferate)) + { + struct yx_uint8 candidates[6]; + uint8_t n_candidates = 0; + char dirs[7] = "dxswed"; + struct yx_uint8 start = t->pos; + uint8_t i; + for (i = 0; i < strlen(dirs); i++) + { + if ( mv_yx_in_dir_legal(dirs[i], &start) + && '.' == world.map.cells[start.y*world.map.length+start.x]) + { + uint8_t drop = 0; + if (tt->lifepoints) + { + for (t = world.things; t; t = t->next) + { + if ( t->lifepoints + && start.y == t->pos.y && start.x == t->pos.x) + { + drop = 1; + break; + } + } + } + if (!drop) + { + candidates[n_candidates] = start; + n_candidates++; + } + } + } + if (!n_candidates) + { + return; + } + i = rrand() % n_candidates; + add_thing(-1, tt->id, candidates[i].y, candidates[i].x); + } + } +} + + + extern void add_things(uint8_t type, uint8_t n) { uint8_t i; @@ -241,10 +325,11 @@ extern void add_things(uint8_t type, uint8_t n) struct yx_uint8 pos; while (1) { - char * err = "Space to put thing on too hard to find." - "Map too small?"; + char * err="Space to put thing on too hard to find. Map too small?"; uint16_t i_pos = 0; - for (pos.y = pos.x = 0; 0 == is_passable(pos); i_pos++) + for (pos.y = pos.x = 0; + '.' != world.map.cells[pos.y * world.map.length + pos.x]; + i_pos++) { exit_err(UINT16_MAX == i_pos, err); pos.y = rrand() % world.map.length; @@ -254,7 +339,7 @@ extern void add_things(uint8_t type, uint8_t n) uint8_t clear = 1; for (t = world.things; t; t = t->next) { - if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints) + if (0 != t->lifepoints && pos.y==t->pos.y && pos.x==t->pos.x) { clear = 0; break; @@ -295,7 +380,7 @@ extern void own_thing(struct Thing ** target, struct Thing ** source, penult->next = t->next; } struct Thing ** t_ptr_ptr = target; - for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next); + for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next); * t_ptr_ptr = t; t->next = NULL; } @@ -306,5 +391,5 @@ extern void set_thing_position(struct Thing * t, struct yx_uint8 pos) { t->pos = pos; struct Thing * owned = t->owns; - for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next); + for (; owned; set_thing_position(owned, pos), owned = owned->next); }