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