X-Git-Url: https://plomlompom.com/repos/index.html?a=blobdiff_plain;ds=sidebyside;f=src%2Fserver%2Fthings.c;h=d4db1938836fac6f6bd31d6e535f5343ff31bc00;hb=b774672163cc6a1ad21b4958d3b2e8b903cd0ba2;hp=ffa30b01324b4405c143fa6d3c5617d06cb052fe;hpb=6b72c6d2208d2a6bf120ef2239c0134550ed32f0;p=plomrogue
diff --git a/src/server/things.c b/src/server/things.c
index ffa30b0..d4db193 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() */
@@ -38,6 +43,11 @@ static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
int16_t id, uint8_t struct_id,
struct NextAndId ** start);
+/* Return 1 if cell at "test_pos" is proliferable by "t", i.e. it is passable,
+ * it is not inhabited by another thing of "t"'s type, and, if "t" is animate,
+ * neither by any other animate thing; else return 0.
+ */
+static uint8_t cell_is_proliferable(struct yx_uint8 test_pos, struct Thing * t);
static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
@@ -74,6 +84,32 @@ static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
+static uint8_t cell_is_proliferable(struct yx_uint8 test_pos, struct Thing * t)
+{
+ if ('.' == world.map.cells[test_pos.y * world.map.length + test_pos.x])
+ {
+ struct Thing * t_test;
+ for (t_test = world.things; t_test; t_test = t_test->next)
+ {
+ if (t_test->pos.y == test_pos.y && t_test->pos.x == test_pos.x)
+ {
+ if (t_test->type == t->type)
+ {
+ return 0;
+ }
+ if (t_test->lifepoints && t->lifepoints)
+ {
+ return 0;
+ }
+ }
+ }
+ return 1;
+ }
+ return 0;
+}
+
+
+
extern struct ThingAction * add_thing_action(uint8_t id)
{
struct ThingAction * ta;
@@ -116,6 +152,10 @@ 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;
}
@@ -259,6 +299,39 @@ 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 test = t->pos;
+ uint8_t i;
+ for (i = 0; i < strlen(dirs); i++)
+ {
+ if ( mv_yx_in_dir_legal(dirs[i], &test)
+ && cell_is_proliferable(test, t))
+ {
+ candidates[n_candidates] = test;
+ 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;
@@ -267,10 +340,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;
@@ -280,7 +354,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;