home · contact · privacy
Fixed bug that led to endless loop in nearest_enemy_dir().
[plomrogue] / src / map_object_actions.c
1 /* map_object_actions.c */
2
3 #include "map_object_actions.h"
4 #include <stdint.h> /* for uint8_t, uint16_t */
5 #include <string.h> /* for strlen(), strcmp() */
6 #include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(),
7                         * yx_uint16_cmp()
8                         */
9 #include "map_objects.h" /* for MapObj, MapObjDef structs, get_player(),
10                           * set_object_position(), own_map_object()
11                           */
12 #include "misc.h" /* for update_log(), try_malloc() */
13 #include "map.h" /* for is_passable() */
14 #include "main.h" /* for world global */
15 #include "readwrite.h" /* for try_fopen(), try_fclose(), textfile_sizes() */
16 #include "rexit.h" /* for exit_err() */
17
18
19
20 /* If "name" fits "moa"->name, set "moa"->func to "func". */
21 static uint8_t try_func_name(struct MapObjAct * moa,
22                              char * name, void (* func) (struct MapObj *));
23
24 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
25  * reach zero in the process, killing it. Generates appropriate log message.
26  */
27 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
28
29 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
30  * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
31  */
32 static void playerbonus_wait();
33 static void playerbonus_move(char d, uint8_t passable);
34 static void playerbonus_drop(uint8_t owns_none);
35 static void playerbonus_pick(uint8_t picked);
36 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object);
37
38
39
40 static uint8_t try_func_name(struct MapObjAct * moa,
41                              char * name, void (* func) (struct MapObj *))
42 {
43     if (0 == strcmp(moa->name, name))
44     {
45         moa->func = func;
46         return 1;
47     }
48     return 0;
49 }
50
51
52
53 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
54 {
55     struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
56     struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
57     struct MapObj * player = get_player();
58     char * msg1 = "You";
59     char * msg2 = "wound";
60     char * msg3 = "you";
61     if      (player != hitter)
62     {
63         msg1 = mod_hitter->name;
64         msg2 = "wounds";
65     }
66     if (player != hitted)
67     {
68         msg3 = mod_hitted->name;
69     }
70     uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
71     char msg[len];
72     sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
73     update_log(msg);
74     hitted->lifepoints--;
75     if (0 == hitted->lifepoints)
76     {
77         hitted->type = mod_hitted->corpse_id;
78         if (player == hitted)
79         {
80             update_log(" You die.");
81             return;
82         }
83         update_log(" It dies.");
84         if (player == hitter)
85         {
86             world.score = world.score + mod_hitted->lifepoints;
87         }
88     }
89 }
90
91
92
93 static void playerbonus_wait()
94 {
95         update_log("\nYou wait.");
96 }
97
98
99
100 static void playerbonus_move(char d, uint8_t passable)
101 {
102     char * dsc_dir = "north";
103     if      ('E' == d)
104     {
105         dsc_dir = "east" ;
106     }
107     else if ('S' == d)
108     {
109         dsc_dir = "south";
110     }
111     else if ('W' == d)
112     {
113         dsc_dir = "west" ;
114     }
115     char * dsc_move = "You move ";
116     if (0 == passable)
117     {
118         dsc_move = "You fail to move ";
119     }
120     char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
121     sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
122     update_log(msg);
123 }
124
125
126
127 static void playerbonus_drop(uint8_t owns_none)
128 {
129     if (0 != owns_none)
130     {
131         update_log("\nYou try to drop an object, but you own none.");
132         return;
133     }
134     update_log("\nYou drop an object.");
135     world.inventory_sel = world.inventory_sel - (0 < world.inventory_sel);
136 }
137
138
139
140 static void playerbonus_pick(uint8_t picked)
141 {
142     if (picked)
143     {
144         update_log("\nYou pick up an object.");
145         return;
146     }
147     update_log("\nYou try to pick up an object, but there is none.");
148 }
149
150
151
152 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
153 {
154     if      (no_object)
155     {
156         update_log("\nYou try to use an object, but you own none.");
157         return;
158     }
159     else if (wrong_object)
160     {
161         update_log("\nYou try to use this object, but fail.");
162         return;
163     }
164     update_log("\nYou consume MAGIC MEAT.");
165     world.inventory_sel = world.inventory_sel - (0 < world.inventory_sel);
166 }
167
168
169
170 extern void init_map_object_actions()
171 {
172     char * f_name = "init_map_object_actions()";
173     char * path = "config/map_object_actions";
174     FILE * file = try_fopen(path, "r", f_name);
175     uint16_t linemax = textfile_sizes(file, NULL);
176     char line[linemax + 1];
177     struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts;
178     char * delim = " ";
179     while (fgets(line, linemax + 1, file))
180     {
181         if ('\n' == line[0] || 0 == line[0])
182         {
183             break;
184         }
185         struct MapObjAct * moa = try_malloc(sizeof(struct MapObjAct), f_name);
186         moa->id = atoi(strtok(line, delim));
187         moa->effort = atoi(strtok(NULL, delim));
188         char * funcname = strtok(NULL, "\n");
189         uint8_t len_name = strlen(funcname) + 1;
190         moa->name = try_malloc(len_name, f_name);
191         memcpy(moa->name, funcname, len_name);
192         if (!(   try_func_name(moa, "move", actor_move)
193               || try_func_name(moa, "pick_up", actor_pick)
194               || try_func_name(moa, "drop", actor_drop)
195               || try_func_name(moa, "use", actor_use)))
196         {
197             moa->func = actor_wait;
198         }
199         moa->next = NULL;
200         * moa_ptr_ptr = moa;
201         moa_ptr_ptr = &moa->next;
202     }
203     try_fclose(file, f_name);
204 }
205
206
207
208 extern void free_map_object_actions(struct MapObjAct * moa)
209 {
210     if (NULL == moa)
211     {
212         return;
213     }
214     free(moa->name);
215     free_map_object_actions(moa->next);
216     free(moa);
217 }
218
219
220
221 extern uint8_t get_moa_id_by_name(char * name)
222 {
223     struct MapObjAct * moa = world.map_obj_acts;
224     while (NULL != moa)
225     {
226         if (0 == strcmp(moa->name, name))
227         {
228             break;
229         }
230         moa = moa->next;
231     }
232     exit_err(NULL == moa, "get_moa_id_name() did not find map object action.");
233     return moa->id;
234 }
235
236
237
238 extern void actor_wait(struct MapObj * mo)
239 {
240     if (mo == get_player())
241     {
242         playerbonus_wait();
243     }
244 }
245
246
247
248 extern void actor_move(struct MapObj * mo)
249 {
250     char d = mo->arg;
251     struct yx_uint16 target = mv_yx_in_dir(d, mo->pos);
252     struct MapObj * other_mo;
253     for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
254     {
255         if (0 == other_mo->lifepoints || other_mo == mo)
256         {
257             continue;
258         }
259         if (yx_uint16_cmp(&target, &other_mo->pos))
260         {
261             actor_hits_actor(mo, other_mo);
262             return;
263         }
264     }
265     uint8_t passable = is_passable(world.map, target);
266     if (passable)
267     {
268         set_object_position(mo, target);
269     }
270     if (mo == get_player())
271     {
272         playerbonus_move(d, passable);
273     }
274 }
275
276
277
278 extern void actor_drop(struct MapObj * mo)
279 {
280     uint8_t owns_none = (NULL == mo->owns);
281     if (!owns_none)
282     {
283         uint8_t select = mo->arg;
284         struct MapObj * owned = mo->owns;
285         uint8_t i = 0;
286         for (; i != select; i++, owned = owned->next);
287         own_map_object(&world.map_objs, &mo->owns, owned->id);
288     }
289     if (mo == get_player())
290     {
291         playerbonus_drop(owns_none);
292     }
293 }
294
295
296
297 extern void actor_pick(struct MapObj * mo)
298 {
299     struct MapObj * picked;
300     for (picked = world.map_objs; NULL != picked; picked = picked->next)
301     {
302         if (picked != mo && yx_uint16_cmp(&picked->pos, &mo->pos))
303         {
304             break;
305         }
306     }
307     if (NULL != picked)
308     {
309         own_map_object(&mo->owns, &world.map_objs, picked->id);
310         set_object_position(picked, mo->pos);
311     }
312     if (mo == get_player())
313     {
314         playerbonus_pick(NULL != picked);
315     }
316 }
317
318
319
320 extern void actor_use(struct MapObj * mo)
321 {
322     uint8_t wrong_object = 1;
323     uint8_t no_object = (NULL == mo->owns);
324     if (!no_object)
325     {
326         uint8_t select = mo->arg;
327         uint8_t i = 0;
328         struct MapObj * selected = mo->owns;
329         for (; i != select; i++, selected = selected->next);
330         struct MapObjDef * mod = get_map_object_def(selected->type);
331         if (!strcmp("MAGIC MEAT", mod->name))
332         {
333             wrong_object = 0;
334             struct MapObj * next = selected->next;
335             free(selected);
336             if (0 < select)
337             {
338                 select--;
339                 selected = mo->owns;
340                 for (i = 0; i != select; i++, selected = selected->next);
341                 selected->next = next;
342             }
343             else
344             {
345                 mo->owns = next;
346             }
347             mo->lifepoints++;
348         }
349     }
350     if (mo == get_player())
351     {
352         playerbonus_use(no_object, wrong_object);
353     }
354 }