home · contact · privacy
cd4040a6dd5b4a107624dd7a1abafad687e1f1e4
[plomrogue] / src / server / things.h
1 /* src/server/things.h
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  *
7  * Structs for things and their type and action definitions, and routines to
8  * initialize these.
9  */
10
11 #ifndef THINGS_H
12 #define THINGS_H
13
14 #include <stdint.h> /* uint8_t, int16_t */
15 #include "../common/yx_uint8.h" /* yx_uint8 */
16
17
18
19 struct Thing
20 {
21     struct Thing * next;
22     uint8_t id;                   /* individual thing's unique identifier */
23     struct Thing * owns;          /* chain of things owned / in inventory */
24     struct ThingInMemory * t_mem; /* chain of things remembered */
25     struct yx_uint8 pos;          /* coordinate on map */
26     char * fov_map;               /* thing's FOV map; 'v':visible, 'H':hidden */
27     char * mem_map;               /* map knowledge of thing by FOV and memory */
28     char * mem_depth_map;         /* map of map memory up-to-dateness */
29     uint8_t type;                 /* ID of appropriate thing definition */
30     uint8_t lifepoints;           /* 0: thing is inanimate; >0: hitpoints */
31     uint8_t command;              /* thing's current action; 0 if none */
32     uint8_t arg;                  /* optional field for .command argument */
33     uint8_t progress;             /* turns already passed to realize .command */
34 };
35
36 struct ThingInMemory
37 {
38     struct ThingInMemory * next;
39     struct yx_uint8 pos;                             /* position on memorized */
40     uint8_t type;                                    /* thing type identifier */
41 };
42
43 struct ThingType
44 {
45     struct ThingType * next;
46     uint8_t id;          /* thing type identifier / sets .type */
47     char char_on_map;    /* thing symbol to appear on map */
48     char * name;         /* string to describe thing in game log */
49     uint8_t corpse_id;   /* type to change thing into upon destruction */
50     uint8_t lifepoints;  /* default start value for thing's .lifepoints */
51     uint8_t consumable;  /* can be eaten if !0, for so much hitpoint win */
52     uint8_t start_n;     /* how many of these does the map start with? */
53     uint8_t proliferate; /* if >0: inverse of chance to proliferate */
54 };
55
56 struct ThingAction
57 {
58     struct ThingAction * next;
59     uint8_t id;   /* identifies action in Thing.command; therefore must be >0 */
60     void (* func) (struct Thing *);    /* function called after .effort turns */
61     char * name;                       /* human-readable identifier */
62     uint8_t effort;                    /* how many turns the action takes */
63 };
64
65
66
67 /* Add thing action of "id" to world.thing_actions, with .name defaulting to
68  * s[S_CMD_WAIT], .func to actor_wait() and .effort to 1. If "id" is not >= 1
69  * and <= UINT8_MAX, use lowest unused id. Return thing action.
70  */
71 extern struct ThingAction * add_thing_action(uint8_t id);
72
73 /* Add thing type of "id" to world.thing_types, with .corpse_id defaulting to
74  * the new thing type's .id, .name to "(none)" and the remaining values to 0. If
75  * "id" is not >= 0 and <= UINT8_MAX, use lowest unused id. Return thing type.
76  */
77 extern struct ThingType * add_thing_type(int16_t id);
78
79 /* Add thing of "id" and "type" on position of "y"/x" to world.things. If "id"
80  * is not >= 0 and <= UINT8_MAX, use lowest unused id. Build .fov_map if
81  * world.exists is non-zero. Return thing.
82  */
83 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x);
84
85 /* Add to thing memory of "t" thing of type id "type" and position "y"/"x". */
86 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
87                                     uint8_t y, uint8_t x);
88
89 /* Free ThingAction / ThingType / Thing / ThingInMemory chain starting at "ta" /
90  * "tt" / "t" / "tm".
91  */
92 extern void free_thing_actions(struct ThingAction * ta);
93 extern void free_thing_types(struct ThingType * tt);
94 extern void free_things(struct Thing * t);
95 extern void free_things_in_memory(struct ThingInMemory * tm);
96
97 /* Return pointer to ThingAction/ThingType of "id", or NULL if none found. */
98 extern struct ThingAction * get_thing_action(uint8_t id);
99 extern struct ThingType * get_thing_type(uint8_t id);
100
101 /* Return world.thing_actions ThingAction.id for "name" or 0 if none found. */
102 extern uint8_t get_thing_action_id_by_name(char * name);
103
104 /* Return thing of "id" in chain at "ptr", search inventories too if "deep".
105  * Return NULL if nothing found.
106  */
107 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep);
108
109 /* Get pointer to the non-owend Thing struct that represents the player, or NULL
110  * if none found.
111  */
112 extern struct Thing * get_player();
113
114 /* Try to create "t" offspring on random passable neighbor cell if available
115  * (and, if "t" is of animate thing type, not inhabited by animate thing) and
116  * "t"'s type's .proliferation is >0, with a chance of 1/.proliferation.
117  */
118 extern void try_thing_proliferation(struct Thing * t);
119
120 /* Add thing(s) ("n": how many?) of "type" to map on random passable
121  * position(s). New animate things are never placed in the same square with
122  * other animate ones.
123  */
124 extern void add_things(uint8_t type, uint8_t n);
125
126 /* Move thing of "id" from "source" inventory to "target" inventory. */
127 extern void own_thing(struct Thing ** target, struct Thing ** source,
128                       uint8_t id);
129
130 /* Move not only "t" to "pos", but also all things owned by it. */
131 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos);
132
133
134
135 #endif