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